-
Notifications
You must be signed in to change notification settings - Fork 1
/
hunt_and_kill.c
111 lines (94 loc) · 3.09 KB
/
hunt_and_kill.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "maze.h"
#include <stdlib.h>
void hunt_and_kill(Map * map) {
int visitedCount = 0;
int visited[(map->width * map->height)];
for (int i = 0; i < (map->width * map->height); i++) {
visited[i] = 0;
}
Tile * tile = randomTile(map);
Tile * neighborTile;
int pathIndex = 0;
Tile * path[map->width * map->height];
Direction pathDir[map->width * map->height];
path[pathIndex] = tile;
visited[(path[pathIndex]->y * map->width) + path[pathIndex]->x] = 1;
visitedCount++;
pathIndex++;
int at = accessableTiles(map);
while(visitedCount < at) {
pathDir[pathIndex] = randomUnvisitedNeighbor(map, tile, visited, &path[pathIndex]);
if (pathDir[pathIndex] == NO_DIRECTION) {
// hunt
pathIndex = 0;
int found = 0;
for (int y = 0; y < map->height; y++) {
for (int x = 0; x < map->width; x++) {
if (found == 0 && visited[(y * map->width) + x] == 0) {
startRender();
// render path
renderMap(map);
debugRenderCursor(x, y, 222, 222, 222);
endRender();
delay(100);
pathDir[pathIndex] = randomVisitedNeighbor(map, &map->tiles[(y * map->width) + x], visited, &path[pathIndex]);
if (pathDir[pathIndex] == NO_DIRECTION) {
// continue..
} else {
// connect and switch modes
tile = &map->tiles[(y * map->width) + x];
visited[(y * map->width) + x] = 1;
visitedCount++;
neighborTile = path[pathIndex];
Direction dir = pathDir[pathIndex];
if (dir == NORTH) {
neighborTile->connections |= DOWN;
} else if (dir == SOUTH) {
tile->connections |= DOWN;
} else if (dir == EAST) {
tile->connections |= RIGHT;
} else if (dir == WEST) {
neighborTile->connections |= RIGHT;
}
found = 1;
}
}
}
}
tile = path[pathIndex];
pathIndex++;
} else {
startRender();
// render path
renderMap(map);
debugRenderCursor(path[pathIndex]->x, path[pathIndex]->y, 0, 100, 0);
for (int x = 0; x < map->width; x++) {
for (int y = 0; y < map->height; y++) {
if (visited[(y * map->width) + x] == 1) {
debugRenderCursor(x, y, 222, 222, 222);
}
}
}
endRender();
delay(100);
// kill
visited[(path[pathIndex]->y * map->width) + path[pathIndex]->x] = 1;
visitedCount++;
tile = path[pathIndex - 1];
neighborTile = path[pathIndex];
// Direction is on the neighbor, 0 index never has as direction
Direction dir = pathDir[pathIndex];
if (dir == NORTH) {
neighborTile->connections |= DOWN;
} else if (dir == SOUTH) {
tile->connections |= DOWN;
} else if (dir == EAST) {
tile->connections |= RIGHT;
} else if (dir == WEST) {
neighborTile->connections |= RIGHT;
}
tile = path[pathIndex];
pathIndex++;
}
}
}