author | viric@llimona |
Sun, 07 May 2006 13:32:52 +0200 | |
changeset 15 | 866013971139 |
parent 13 | c2b272938a1f |
child 16 | fe078fb2e8b4 |
permissions | -rw-r--r-- |
6 | 1 |
#include <assert.h> |
2 |
#include <string.h> |
|
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
3 |
#include <stdio.h> /**********************************PROVA */ |
6 | 4 |
#include "general.h" |
5 |
||
6 |
/* Variables related to the showing of information by os */ |
|
7 |
extern float percent_to_show; |
|
8 |
extern int depth_to_show; |
|
9 |
||
10 |
extern int max_depth; |
|
11 |
extern int min_depth_period; |
|
12 |
extern int max_depth_period; |
|
13 |
extern struct Map * actual_map; |
|
14 |
||
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
15 |
/* Prototipes */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
16 |
static Bool are_there_fixed_boxes(struct Map *m, |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
17 |
struct BoxMove movements[MAX_MOVES], int *num_movements); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
18 |
static void force_move_box(struct Map *m, const struct BoxMove move); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
19 |
static Bool search_did_found(struct Map maps[], int depth, |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
20 |
struct BoxMove movements[], |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
21 |
int num_movements, float percent, float total_percent); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
22 |
static void fill_deps(struct Map *m); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
23 |
|
6 | 24 |
#if 0 /*** THIS IS AN ERROR!!! The box_will_be_blocked function doesn't work!*/ |
25 |
Situation: |
|
26 |
||
27 |
->@$ # |
|
28 |
$ |
|
29 |
*/ |
|
30 |
int box_will_be_blocked(const struct Map * m, const struct Position mov, |
|
31 |
const struct Position box_pos) |
|
32 |
{ |
|
33 |
struct Position next_pos2, tmp, tmp2[2]; |
|
34 |
int i; |
|
35 |
||
36 |
next_pos2.x = box_pos.x + mov.x; |
|
37 |
next_pos2.y = box_pos.y + mov.y; |
|
38 |
||
39 |
tmp.x = next_pos2.x + mov.x; |
|
40 |
tmp.y = next_pos2.y + mov.y; |
|
41 |
tmp2[0].x = next_pos2.x + mov.y; |
|
42 |
tmp2[0].y = next_pos2.y + mov.x; |
|
43 |
tmp2[1].x = next_pos2.x - mov.y; |
|
44 |
tmp2[1].y = next_pos2.y - mov.x; |
|
45 |
for (i=0; i < 2; i++) |
|
46 |
{ |
|
47 |
if (m->man_moves[tmp.y][tmp.x] == WALL && |
|
48 |
m->man_moves[tmp2[i].y][tmp2[i].x] == BOX) |
|
49 |
{ |
|
50 |
return TRUE; |
|
51 |
} |
|
52 |
else if (m->man_moves[tmp.y][tmp.x] == BOX && |
|
53 |
m->man_moves[tmp2[i].y][tmp2[i].x] == WALL) |
|
54 |
{ |
|
55 |
return TRUE; |
|
56 |
} |
|
57 |
else if (m->man_moves[tmp.y][tmp.x] == BOX && |
|
58 |
m->man_moves[tmp2[i].y][tmp2[i].x] == BOX) |
|
59 |
{ |
|
60 |
return TRUE; |
|
61 |
} |
|
62 |
} |
|
63 |
||
64 |
return FALSE; |
|
65 |
} |
|
66 |
||
67 |
int is_corner_area(const struct Map * m, const struct Position p, |
|
68 |
const struct Position box, const struct Position new_box) |
|
69 |
{ |
|
70 |
int NumMoves, NewMoves; |
|
71 |
struct Position pos[MAX_MOVES]; |
|
72 |
struct Position new_pos[MAX_MOVES]; |
|
73 |
char corners[MAX_Y][MAX_X]; |
|
74 |
int i,j; |
|
75 |
struct Position next_pos; |
|
76 |
char *next_cell; |
|
77 |
||
78 |
||
79 |
/* Blank the garden */ |
|
80 |
for (j = 0; j<m->SizeY; j++) |
|
81 |
for (i=0; i<m->SizeX; i++) |
|
82 |
corners[j][i] = m->Cells[j][i]; |
|
83 |
||
84 |
/* Let's put the boxes */ |
|
85 |
for (i = 0; i<m->NumBoxes; i++) |
|
86 |
corners[m->Box[i].y][m->Box[i].x] = BOX; |
|
87 |
||
88 |
/* Let's put our box - it can be simply added */ |
|
89 |
corners[new_box.y][new_box.x] = BOX; |
|
90 |
/* Let's remove the original box. */ |
|
91 |
corners[box.y][box.x] = BLANK; |
|
92 |
||
93 |
NewMoves = 1; |
|
94 |
new_pos[0] = p; |
|
95 |
while (NewMoves > 0) |
|
96 |
{ |
|
97 |
/* The before named "New Moves" become the moves we have |
|
98 |
to analyze */ |
|
99 |
NumMoves = NewMoves; |
|
100 |
for (i=0; i<NewMoves; i++) |
|
101 |
{ |
|
102 |
pos[i] = new_pos[i]; |
|
103 |
} |
|
104 |
||
105 |
/* Search new positions for each position */ |
|
106 |
NewMoves = 0; |
|
107 |
for (i=0; i<NumMoves; i++) |
|
108 |
{ |
|
109 |
/* For each direction */ |
|
110 |
for (j=0; j<4; j++) |
|
111 |
{ |
|
112 |
next_pos.x = pos[i].x + move_vectors[j].x; |
|
113 |
next_pos.y = pos[i].y + move_vectors[j].y; |
|
114 |
next_cell = &corners[next_pos.y][next_pos.x]; |
|
115 |
if(*next_cell == BLANK || |
|
116 |
*next_cell == PLATFORM) |
|
117 |
{ |
|
118 |
return FALSE; |
|
119 |
} |
|
120 |
else if(*next_cell == CORNER) |
|
121 |
{ |
|
122 |
new_pos[NewMoves] = next_pos; |
|
123 |
*next_cell = MANCANMOVE; |
|
124 |
NewMoves++; |
|
125 |
} |
|
126 |
} |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
return TRUE; |
|
131 |
} |
|
132 |
||
133 |
int does_box_close_corners(const struct Map * m, const struct Position mov, |
|
134 |
const struct Position box_pos) |
|
135 |
{ |
|
136 |
struct Position p, p2; |
|
137 |
||
138 |
p.x = box_pos.x + mov.x; |
|
139 |
p.y = box_pos.y + mov.y; |
|
140 |
||
141 |
/* Let's plan the checks */ |
|
142 |
/* The point will be marked with a MANCANMOVE */ |
|
143 |
p2.x = p.x + mov.x; |
|
144 |
p2.y = p.y + mov.y; |
|
145 |
if (m->Cells[p2.y][p2.x] == CORNER) |
|
146 |
{ |
|
147 |
if (is_corner_area(m, p2, box_pos, p)) |
|
148 |
return TRUE; |
|
149 |
} |
|
150 |
||
151 |
p2.x = p.x + mov.y; |
|
152 |
p2.y = p.y + mov.x; |
|
153 |
if (m->Cells[p2.y][p2.x] == CORNER) |
|
154 |
{ |
|
155 |
if (is_corner_area(m, p2, box_pos, p)) |
|
156 |
return TRUE; |
|
157 |
} |
|
158 |
||
159 |
p2.x = p.x - mov.y; |
|
160 |
p2.y = p.y - mov.x; |
|
161 |
if (m->Cells[p2.y][p2.x] == CORNER) |
|
162 |
{ |
|
163 |
if (is_corner_area(m, p2, box_pos, p)) |
|
164 |
return TRUE; |
|
165 |
} |
|
166 |
return FALSE; |
|
167 |
} |
|
168 |
#endif |
|
169 |
||
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
170 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
171 |
static void force_move_box(struct Map *m, const struct BoxMove move) |
6 | 172 |
{ |
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
173 |
struct Position newpos; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
174 |
|
6 | 175 |
|
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
176 |
/* Add coords */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
177 |
newpos.x = m->Box[move.box].x + move.dir.x; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
178 |
newpos.y = m->Box[move.box].y + move.dir.y; |
6 | 179 |
|
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
180 |
/* Be certain the move can be done */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
181 |
assert(m->Cells[newpos.y][newpos.x] != BOX); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
182 |
assert(m->Cells[newpos.y][newpos.x] != WALL); |
6 | 183 |
|
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
184 |
/* Control if we moved the box to a platform */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
185 |
if(m->Cells[newpos.y][newpos.x] == PLATFORM) |
6 | 186 |
{ |
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
187 |
m->NumBoxesInPlatform++; |
6 | 188 |
} |
189 |
||
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
190 |
/* Control if we moved the box from a platform */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
191 |
if (m->Cells[m->Box[move.box].y][m->Box[move.box].x] == PLATFORM) |
6 | 192 |
{ |
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
193 |
m->NumBoxesInPlatform--; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
194 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
195 |
m->Man = m->Box[move.box]; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
196 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
197 |
m->Box[move.box] = newpos; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
198 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
199 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
200 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
201 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
202 |
static Bool search_did_found(struct Map maps[], int depth, |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
203 |
struct BoxMove movements[], |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
204 |
int num_movements, float percent, float total_percent) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
205 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
206 |
struct BoxMove new_movements[MAX_MOVES]; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
207 |
int num_new_movements; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
208 |
struct Map *m; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
209 |
int i; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
210 |
float next_percent; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
211 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
212 |
next_percent = percent / num_movements; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
213 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
214 |
m = &maps[depth+1]; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
215 |
if (depth > max_depth) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
216 |
max_depth = depth; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
217 |
if (depth > max_depth_period) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
218 |
max_depth_period = depth; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
219 |
if (depth < min_depth_period) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
220 |
min_depth_period = depth; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
221 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
222 |
for (i=0; i < num_movements; i++) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
223 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
224 |
CopyMap(m, &maps[depth]); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
225 |
force_move_box(m, movements[i]); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
226 |
/* Let's see if we finished */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
227 |
if (m->NumPlatforms == m->NumBoxesInPlatform) |
6 | 228 |
{ |
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
229 |
PrintMove(movements[i]); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
230 |
actual_map = &maps[depth+1]; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
231 |
show_percent_and_map(); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
232 |
return TRUE; |
6 | 233 |
} |
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
234 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
235 |
if (is_new_map(maps, depth+1)) |
6 | 236 |
{ |
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
237 |
actual_map = &maps[depth+1]; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
238 |
#ifdef DEBUG /* to be out */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
239 |
show_percent_and_map(); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
240 |
#endif |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
241 |
fill_deps(m); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
242 |
if (are_there_fixed_boxes(m, |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
243 |
new_movements, &num_new_movements)) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
244 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
245 |
/* That means that the map is illegal */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
246 |
/* Maybe we could update the percent here... */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
247 |
return FALSE; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
248 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
249 |
/* the assert should be IN the function before, |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
250 |
before OVERfilling new_movements */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
251 |
assert(num_new_movements < MAX_MOVES); |
6 | 252 |
} |
253 |
else |
|
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
254 |
num_new_movements = 0; |
6 | 255 |
|
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
256 |
if (num_new_movements == 0) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
257 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
258 |
percent_to_show = total_percent + next_percent*i; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
259 |
depth_to_show = depth; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
260 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
261 |
else |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
262 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
263 |
if (depth+1 < MAX_STEPS) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
264 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
265 |
if(search_did_found(maps, depth+1, new_movements, |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
266 |
num_new_movements, next_percent, |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
267 |
total_percent + next_percent*i) == TRUE) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
268 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
269 |
PrintMove(movements[i]); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
270 |
actual_map = &maps[depth+1]; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
271 |
show_percent_and_map(); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
272 |
return TRUE; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
273 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
274 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
275 |
} |
6 | 276 |
} |
277 |
return FALSE; |
|
278 |
} |
|
279 |
||
280 |
||
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
281 |
/* It only fills the m->man_moves structure */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
282 |
static void paint_mancanmove(struct Map *m) |
6 | 283 |
{ |
284 |
struct Position pos[MAX_MOVES]; |
|
285 |
struct Position new_pos[MAX_MOVES]; |
|
286 |
int NumMoves, NewMoves; |
|
287 |
int j, i; |
|
288 |
struct Position next_pos; |
|
289 |
char *next_cell; |
|
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
290 |
|
6 | 291 |
/* Let's the map with only walls in man_moves - other, blanks */ |
292 |
for (j = 0; j<m->SizeY; j++) |
|
293 |
for (i=0; i<m->SizeX; i++) |
|
294 |
{ |
|
295 |
if (m->Cells[j][i] == WALL) |
|
296 |
m->man_moves[j][i] = WALL; |
|
297 |
else |
|
298 |
m->man_moves[j][i] = BLANK; |
|
299 |
} |
|
300 |
||
301 |
/* Let's put the boxes */ |
|
302 |
for (i = 0; i<m->NumBoxes; i++) |
|
303 |
{ |
|
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
304 |
m->man_moves[m->Box[i].y][m->Box[i].x] = WALL; |
6 | 305 |
} |
306 |
||
307 |
NewMoves = 1; |
|
308 |
new_pos[0].x = m->Man.x; |
|
309 |
new_pos[0].y = m->Man.y; |
|
310 |
m->man_moves[m->Man.y][m->Man.x] = MANCANMOVE; |
|
311 |
while (NewMoves > 0) |
|
312 |
{ |
|
313 |
/* The before named "New Moves" become the moves we have |
|
314 |
to analyze */ |
|
315 |
NumMoves = NewMoves; |
|
316 |
for (i=0; i<NewMoves; i++) |
|
317 |
{ |
|
318 |
pos[i] = new_pos[i]; |
|
319 |
} |
|
320 |
||
321 |
/* Search new positions for each position */ |
|
322 |
NewMoves = 0; |
|
323 |
for (i=0; i<NumMoves; i++) |
|
324 |
{ |
|
325 |
/* For each direction */ |
|
326 |
for (j=0; j<4; j++) |
|
327 |
{ |
|
328 |
next_pos.x = pos[i].x + move_vectors[j].x; |
|
329 |
next_pos.y = pos[i].y + move_vectors[j].y; |
|
330 |
next_cell = &m->man_moves[next_pos.y][next_pos.x]; |
|
331 |
if(*next_cell == BLANK) |
|
332 |
{ |
|
333 |
new_pos[NewMoves] = next_pos; |
|
334 |
*next_cell = MANCANMOVE; |
|
335 |
NewMoves++; |
|
336 |
} |
|
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
337 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
338 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
339 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
340 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
341 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
342 |
/* Returns TRUE if there are fixed boxes that should be moved, FALSE otherwise*/ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
343 |
/* For 'm' it modifies only m->boxes[][] */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
344 |
static Bool are_there_fixed_boxes(struct Map *m, |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
345 |
struct BoxMove movements[MAX_MOVES], int *num_movements) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
346 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
347 |
int i,j; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
348 |
enum e_direction d; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
349 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
350 |
/* Box dependencies. The first depends on seconds. */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
351 |
struct |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
352 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
353 |
Bool box[MAX_BOXES][MAX_BOXES]; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
354 |
int ndeps[MAX_BOXES]; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
355 |
} dep; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
356 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
357 |
/* Free boxes' arrays */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
358 |
struct |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
359 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
360 |
Bool box_is_free[MAX_BOXES]; /* This is only used in 'free' */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
361 |
int box[MAX_BOXES]; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
362 |
int n; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
363 |
} free, /* List of all boxes free-to-move */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
364 |
new_free, /* List of boxes for the next loop (code) */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
365 |
tfree; /* List of boxes of the loop (code) */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
366 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
367 |
struct Position tpos; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
368 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
369 |
Bool is_not_set_free; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
370 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
371 |
/* Initialize to 0 the dependency structures */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
372 |
memset((void *) &dep, 0, sizeof(dep)); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
373 |
for(i=0; i<m->NumBoxes; i++) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
374 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
375 |
new_free.box[i] = FALSE; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
376 |
} |
6 | 377 |
|
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
378 |
/* Let's fill the structure */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
379 |
for(i=0; i<m->NumBoxes; i++) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
380 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
381 |
/* See if the box is blocked. That could be |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
382 |
done in fill_deps */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
383 |
if (m->box_deps[i].dep_dir[0] == BLOCKED && |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
384 |
m->box_deps[i].dep_dir[1] == BLOCKED && |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
385 |
m->box_deps[i].dep_dir[2] == BLOCKED && |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
386 |
m->box_deps[i].dep_dir[3] == BLOCKED ) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
387 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
388 |
if (m->Cells[m->Box[i].y][m->Box[i].x] != |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
389 |
PLATFORM) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
390 |
return TRUE; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
391 |
/* This is not used actually |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
392 |
else |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
393 |
m->boxes[m->Box[i].y][m->Box[i].x] = |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
394 |
WALL; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
395 |
*/ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
396 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
397 |
/* Let's take note of the dependencies */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
398 |
is_not_set_free = TRUE; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
399 |
for(d=0; d<4; d++) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
400 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
401 |
if(m->box_deps[i].dep_dir[d] > 0) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
402 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
403 |
dep.box[i][m->box_deps[i].dep_dir[d]] = |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
404 |
TRUE; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
405 |
dep.ndeps[i]++; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
406 |
} else |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
407 |
if(m->box_deps[i].dep_dir[d] == FREE) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
408 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
409 |
if (is_not_set_free) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
410 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
411 |
new_free.box[new_free.n++] = i; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
412 |
is_not_set_free = FALSE; |
6 | 413 |
} |
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
414 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
415 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
416 |
} |
6 | 417 |
|
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
418 |
/* Let's analyze if the free boxes can really be moved, and |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
419 |
set them as dependant, if they're dependant. |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
420 |
If some can be really moved, we should know _where to_. */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
421 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
422 |
/* Paint MANCANMOVE at each cell the man can reach */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
423 |
paint_mancanmove(m); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
424 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
425 |
/* For each free movable box, let's see if the man can reach them |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
426 |
fine. If we can, add it to the possible movements. */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
427 |
*num_movements=0; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
428 |
for(i=0; i<new_free.n; i++) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
429 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
430 |
/* For each direction, try if the box can be moved */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
431 |
for(d=0; d<4; d++) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
432 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
433 |
if(m->box_deps[i].dep_dir[d] == FREE) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
434 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
435 |
add_position3(tpos, m->Box[i], move_vectors[OPPOSITE_DIR(d)]) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
436 |
if (m->man_moves[tpos.y][tpos.x] == MANCANMOVE) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
437 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
438 |
movements[*num_movements].box = i; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
439 |
movements[*num_movements].dir = |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
440 |
move_vectors[d]; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
441 |
(*num_movements)++; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
442 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
443 |
/* ELSE - we should catch those cases, |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
444 |
where free boxes cannot be moved. Will they |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
445 |
ever be movable? |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
446 |
THIS HAS TO BE IMPROVED */ |
6 | 447 |
} |
448 |
} |
|
449 |
} |
|
450 |
||
451 |
||
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
452 |
/* ---------------- The next possible movements have been calculated -- |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
453 |
We can still find that this map is illegal. We need to find |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
454 |
further more if there are boxes unmovable (due to dependencies) */ |
6 | 455 |
|
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
456 |
/* Let's take out all dependencies from 'free' boxes */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
457 |
/* Prepare the 'free' list */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
458 |
for(i=0; i<m->NumBoxes;i++) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
459 |
free.box_is_free[i] = FALSE; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
460 |
free.n = new_free.n; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
461 |
for(i=0; i<new_free.n; i++) |
6 | 462 |
{ |
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
463 |
free.box[i] = new_free.box[i]; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
464 |
free.box_is_free[i] = TRUE; |
6 | 465 |
} |
466 |
||
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
467 |
/* KACXO-loop */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
468 |
while (new_free.n > 0) |
6 | 469 |
{ |
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
470 |
/* Copy new_free into free */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
471 |
tfree.n = new_free.n; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
472 |
for(i=0; i<new_free.n; i++) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
473 |
tfree.box[i] = new_free.box[i]; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
474 |
new_free.n = 0; |
6 | 475 |
|
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
476 |
for(i=0; i<tfree.n; i++) |
6 | 477 |
{ |
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
478 |
for(j=0; j<m->NumBoxes; j++) |
6 | 479 |
{ |
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
480 |
/* The box j no more depends on |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
481 |
the box tfree[i] */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
482 |
if(dep.box[j][tfree.box[i]]) |
6 | 483 |
{ |
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
484 |
dep.box[j][tfree.box[i]] = FALSE; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
485 |
dep.ndeps[j]--; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
486 |
assert(dep.ndeps[j] >= 0); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
487 |
if (dep.ndeps[j] == 0 && |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
488 |
!free.box_is_free[i]) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
489 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
490 |
new_free.box[new_free.n++]=j; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
491 |
free.box[free.n++]=j; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
492 |
} |
6 | 493 |
} |
494 |
} |
|
495 |
} |
|
496 |
} |
|
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
497 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
498 |
/* Now are left only boxes which depend on others-non-free */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
499 |
for(i=0; i<m->NumBoxes; i++) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
500 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
501 |
for(j=0; j<m->NumBoxes; j++) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
502 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
503 |
/* We only do one-level search !!! */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
504 |
/* If the box only depends on another, |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
505 |
which also only depends on this... */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
506 |
if (dep.box[i][j] && dep.ndeps[i] == 1 |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
507 |
&& dep.box[j][i] && dep.ndeps[j] == 1) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
508 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
509 |
if (m->Cells[m->Box[i].y][m->Box[i].x]!= |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
510 |
PLATFORM) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
511 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
512 |
return TRUE; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
513 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
514 |
/* We don't set any wall here, they're not |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
515 |
important */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
516 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
517 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
518 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
519 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
520 |
return FALSE; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
521 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
522 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
523 |
static void init_cell_boxes(struct Map *m) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
524 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
525 |
int i,j; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
526 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
527 |
/* Let's the map with only walls in man_moves - other, blanks */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
528 |
for (j = 0; j<m->SizeY; j++) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
529 |
for (i=0; i<m->SizeX; i++) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
530 |
m->cells_boxes[j][i] = -1; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
531 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
532 |
for(i=0; i<m->NumBoxes; i++) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
533 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
534 |
m->cells_boxes[m->Box[i].y][m->Box[i].x] = i; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
535 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
536 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
537 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
538 |
/* It fills only m->box_deps */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
539 |
static void fill_deps(struct Map *m) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
540 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
541 |
int i; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
542 |
enum e_direction dir; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
543 |
struct Position n; /* Position next to the box */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
544 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
545 |
/* TO BE OPTIMIZED - get into force_move_box */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
546 |
init_cell_boxes(m); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
547 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
548 |
for(i=0; i<m->NumBoxes; i++) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
549 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
550 |
/* Initialize the move freedom */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
551 |
for(dir=0;dir<4; dir++) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
552 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
553 |
m->box_deps[i].dep_dir[dir] = FREE; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
554 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
555 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
556 |
/* Limit the freedom */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
557 |
for(dir=0;dir<4; dir++) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
558 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
559 |
add_position3(n,m->Box[i],move_vectors[dir]); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
560 |
if(m->Cells[n.y][n.x] == WALL) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
561 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
562 |
/* IMPROVEMENT: The fixed-box detection |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
563 |
could be done here */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
564 |
m->box_deps[i].dep_dir[dir] = BLOCKED; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
565 |
m->box_deps[i].dep_dir[OPPOSITE_DIR(dir)] = |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
566 |
BLOCKED; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
567 |
continue; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
568 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
569 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
570 |
/* if there is Wall limit, we don't want to set box |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
571 |
dependency */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
572 |
if(m->box_deps[i].dep_dir[dir] == WALL) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
573 |
continue; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
574 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
575 |
/* Put the box dependency in the list */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
576 |
if(m->cells_boxes[n.y][n.x] >= 0) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
577 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
578 |
m->box_deps[i].dep_dir[dir] = |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
579 |
m->cells_boxes[n.y][n.x]; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
580 |
/* If the other site is not limited by another |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
581 |
box, we set it up too. */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
582 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
583 |
/* *** MAYBE IT'S BETTER NOT TO SET IT */ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
584 |
if(m->box_deps[i].dep_dir[dir] == FREE) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
585 |
{ |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
586 |
m->box_deps[i].dep_dir[OPPOSITE_DIR(dir)] = |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
587 |
m->cells_boxes[n.y][n.x]; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
588 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
589 |
continue; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
590 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
591 |
if(m->Cells[n.y][n.x] == CORNER) |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
592 |
m->box_deps[i].dep_dir[dir] = BLOCKED; |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
593 |
|
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
594 |
} |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
595 |
} |
6 | 596 |
} |
597 |
||
598 |
||
599 |
int solve_map(const struct Map origin) |
|
600 |
{ |
|
601 |
struct Map maps[MAX_STEPS+1]; |
|
602 |
struct BoxMove new_movements[MAX_MOVES]; |
|
603 |
int num_new_movements; |
|
604 |
||
605 |
CopyMap(&maps[0], &origin); |
|
606 |
||
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
607 |
fill_deps(&maps[0]); |
13
c2b272938a1f
actual_map is initialized at first, so almost never a SIGSEGV can happen.
viric@llimona
parents:
12
diff
changeset
|
608 |
/* Initializing actual_map here AT LEAST gives protection against |
c2b272938a1f
actual_map is initialized at first, so almost never a SIGSEGV can happen.
viric@llimona
parents:
12
diff
changeset
|
609 |
SIGSEGV */ |
c2b272938a1f
actual_map is initialized at first, so almost never a SIGSEGV can happen.
viric@llimona
parents:
12
diff
changeset
|
610 |
actual_map = &maps[0]; |
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
611 |
assert(!are_there_fixed_boxes(&maps[0], new_movements, |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
612 |
&num_new_movements)); |
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
613 |
search_did_found(maps, 0, new_movements, num_new_movements, 100, 0); |
6 | 614 |
|
12
a2c334a71a6b
A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents:
10
diff
changeset
|
615 |
return 0; |
6 | 616 |
} |