-
Notifications
You must be signed in to change notification settings - Fork 0
/
bustrophedon.c
86 lines (68 loc) · 2.26 KB
/
bustrophedon.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
#include "bustrophedon.h"
void ba(robot *r) {
int busIndex = 1;
while (1) {
r->map->tiles[r->posX][r->posY]->bustrophedonIndex = busIndex;
bustrophedon(busIndex++, r);
tile *closest = findClosestOpen(r);
if (closest == NULL)
break;
tileSet *path = getAstarRoute(newTile(r->posX, r->posY), closest, r->map);
if (path == NULL || path->count == 0){
printf("PATH NOT FOUND\n");
break;
}
int i;
for (i = path->count - 1; i >= 0; i--) {
stepToTile(r, path->tiles[i]);
r->map->tiles[r->posX][r->posY]->bustrophedonIndex = -1;
}
}
}
void bustrophedon(int busIndex, robot *r) {
while (!isCritical(r)) {
if (canStepUpVirtual(r))
stepUp(r);
else if (canStepDownVirtual(r))
stepDown(r);
else if (canStepRightVirtual(r))
stepRight(r);
else
stepLeft(r);
r->map->tiles[r->posX][r->posY]->bustrophedonIndex = busIndex;
}
}
int isCritical(robot *r) {
return !(canStepUpVirtual(r) || canStepDownVirtual(r) || canStepLeftVirtual(r) || canStepRightVirtual(r));
}
tile *findClosestOpen(robot *r) {
tile *closest = NULL;
int x, y;
for (x = 0; x < r->map->width; x++) {
for (y = 0; y < r->map->height; y++) {
if (r->map->tiles[x][y]->type == OPEN && r->map->tiles[x][y]->bustrophedonIndex == 0) {
if (closest == NULL || isCloser(r, r->map->tiles[x][y], closest)) {
closest = r->map->tiles[x][y];
}
}
}
}
return closest;
}
int isCloser(robot *r, tile *t1, tile *t2) {
int est1 = costEstimate(newTile(r->posX, r->posY), t1),
est2 = costEstimate(newTile(r->posX, r->posY), t2);
return (est1 < est2);
}
int canStepUpVirtual(robot *r) {
return canStepUp(r) && r->map->tiles[r->posX][r->posY - 1]->bustrophedonIndex == 0;
}
int canStepDownVirtual(robot *r) {
return canStepDown(r) && r->map->tiles[r->posX][r->posY + 1]->bustrophedonIndex == 0;
}
int canStepRightVirtual(robot *r) {
return canStepRight(r) && r->map->tiles[r->posX + 1][r->posY]->bustrophedonIndex == 0;
}
int canStepLeftVirtual(robot *r) {
return canStepLeft(r) && r->map->tiles[r->posX - 1][r->posY]->bustrophedonIndex == 0;
}