author | viric@llimona |
Sat, 17 Feb 2007 15:14:30 +0100 | |
changeset 28 | cd27cb410375 |
parent 26 | 95fccfcbd04c |
permissions | -rw-r--r-- |
6 | 1 |
#include <assert.h> |
2 |
#include <string.h> |
|
28
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
3 |
#include <stdlib.h> |
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 |
||
15 |
#if 0 /*** THIS IS AN ERROR!!! The box_will_be_blocked function doesn't work!*/ |
|
16 |
Situation: |
|
17 |
||
18 |
->@$ # |
|
19 |
$ |
|
20 |
*/ |
|
21 |
int box_will_be_blocked(const struct Map * m, const struct Position mov, |
|
22 |
const struct Position box_pos) |
|
23 |
{ |
|
24 |
struct Position next_pos2, tmp, tmp2[2]; |
|
25 |
int i; |
|
26 |
||
27 |
next_pos2.x = box_pos.x + mov.x; |
|
28 |
next_pos2.y = box_pos.y + mov.y; |
|
29 |
||
30 |
tmp.x = next_pos2.x + mov.x; |
|
31 |
tmp.y = next_pos2.y + mov.y; |
|
32 |
tmp2[0].x = next_pos2.x + mov.y; |
|
33 |
tmp2[0].y = next_pos2.y + mov.x; |
|
34 |
tmp2[1].x = next_pos2.x - mov.y; |
|
35 |
tmp2[1].y = next_pos2.y - mov.x; |
|
36 |
for (i=0; i < 2; i++) |
|
37 |
{ |
|
38 |
if (m->man_moves[tmp.y][tmp.x] == WALL && |
|
39 |
m->man_moves[tmp2[i].y][tmp2[i].x] == BOX) |
|
40 |
{ |
|
41 |
return TRUE; |
|
42 |
} |
|
43 |
else if (m->man_moves[tmp.y][tmp.x] == BOX && |
|
44 |
m->man_moves[tmp2[i].y][tmp2[i].x] == WALL) |
|
45 |
{ |
|
46 |
return TRUE; |
|
47 |
} |
|
48 |
else if (m->man_moves[tmp.y][tmp.x] == BOX && |
|
49 |
m->man_moves[tmp2[i].y][tmp2[i].x] == BOX) |
|
50 |
{ |
|
51 |
return TRUE; |
|
52 |
} |
|
53 |
} |
|
54 |
||
55 |
return FALSE; |
|
56 |
} |
|
57 |
||
58 |
int is_corner_area(const struct Map * m, const struct Position p, |
|
59 |
const struct Position box, const struct Position new_box) |
|
60 |
{ |
|
61 |
int NumMoves, NewMoves; |
|
62 |
struct Position pos[MAX_MOVES]; |
|
63 |
struct Position new_pos[MAX_MOVES]; |
|
64 |
char corners[MAX_Y][MAX_X]; |
|
65 |
int i,j; |
|
66 |
struct Position next_pos; |
|
67 |
char *next_cell; |
|
68 |
||
69 |
||
70 |
/* Blank the garden */ |
|
71 |
for (j = 0; j<m->SizeY; j++) |
|
72 |
for (i=0; i<m->SizeX; i++) |
|
73 |
corners[j][i] = m->Cells[j][i]; |
|
74 |
||
75 |
/* Let's put the boxes */ |
|
76 |
for (i = 0; i<m->NumBoxes; i++) |
|
77 |
corners[m->Box[i].y][m->Box[i].x] = BOX; |
|
78 |
||
79 |
/* Let's put our box - it can be simply added */ |
|
80 |
corners[new_box.y][new_box.x] = BOX; |
|
81 |
/* Let's remove the original box. */ |
|
82 |
corners[box.y][box.x] = BLANK; |
|
83 |
||
84 |
NewMoves = 1; |
|
85 |
new_pos[0] = p; |
|
86 |
while (NewMoves > 0) |
|
87 |
{ |
|
88 |
/* The before named "New Moves" become the moves we have |
|
89 |
to analyze */ |
|
90 |
NumMoves = NewMoves; |
|
91 |
for (i=0; i<NewMoves; i++) |
|
92 |
{ |
|
93 |
pos[i] = new_pos[i]; |
|
94 |
} |
|
95 |
||
96 |
/* Search new positions for each position */ |
|
97 |
NewMoves = 0; |
|
98 |
for (i=0; i<NumMoves; i++) |
|
99 |
{ |
|
100 |
/* For each direction */ |
|
101 |
for (j=0; j<4; j++) |
|
102 |
{ |
|
103 |
next_pos.x = pos[i].x + move_vectors[j].x; |
|
104 |
next_pos.y = pos[i].y + move_vectors[j].y; |
|
105 |
next_cell = &corners[next_pos.y][next_pos.x]; |
|
106 |
if(*next_cell == BLANK || |
|
107 |
*next_cell == PLATFORM) |
|
108 |
{ |
|
109 |
return FALSE; |
|
110 |
} |
|
111 |
else if(*next_cell == CORNER) |
|
112 |
{ |
|
113 |
new_pos[NewMoves] = next_pos; |
|
114 |
*next_cell = MANCANMOVE; |
|
115 |
NewMoves++; |
|
116 |
} |
|
117 |
} |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
return TRUE; |
|
122 |
} |
|
123 |
||
124 |
int does_box_close_corners(const struct Map * m, const struct Position mov, |
|
125 |
const struct Position box_pos) |
|
126 |
{ |
|
127 |
struct Position p, p2; |
|
128 |
||
129 |
p.x = box_pos.x + mov.x; |
|
130 |
p.y = box_pos.y + mov.y; |
|
131 |
||
132 |
/* Let's plan the checks */ |
|
133 |
/* The point will be marked with a MANCANMOVE */ |
|
134 |
p2.x = p.x + mov.x; |
|
135 |
p2.y = p.y + mov.y; |
|
136 |
if (m->Cells[p2.y][p2.x] == CORNER) |
|
137 |
{ |
|
138 |
if (is_corner_area(m, p2, box_pos, p)) |
|
139 |
return TRUE; |
|
140 |
} |
|
141 |
||
142 |
p2.x = p.x + mov.y; |
|
143 |
p2.y = p.y + mov.x; |
|
144 |
if (m->Cells[p2.y][p2.x] == CORNER) |
|
145 |
{ |
|
146 |
if (is_corner_area(m, p2, box_pos, p)) |
|
147 |
return TRUE; |
|
148 |
} |
|
149 |
||
150 |
p2.x = p.x - mov.y; |
|
151 |
p2.y = p.y - mov.x; |
|
152 |
if (m->Cells[p2.y][p2.x] == CORNER) |
|
153 |
{ |
|
154 |
if (is_corner_area(m, p2, box_pos, p)) |
|
155 |
return TRUE; |
|
156 |
} |
|
157 |
return FALSE; |
|
158 |
} |
|
159 |
#endif |
|
160 |
||
161 |
/* |
|
162 |
Catch the situation where a box is moved next to a corner, where the box |
|
163 |
next to it will not be able to be moved. |
|
164 |
*/ |
|
165 |
static int is_dead1(const struct Map * m, const struct Position mov, |
|
166 |
const struct Position new_pos) |
|
167 |
{ |
|
168 |
struct Position opposite1, opposite2; |
|
169 |
||
170 |
/* The wall corners must exist */ |
|
171 |
opposite1.x = -mov.y; |
|
172 |
opposite1.y = -mov.x; |
|
173 |
opposite2.x = mov.y; |
|
174 |
opposite2.y = mov.x; |
|
175 |
||
176 |
#ifdef DEBUG |
|
177 |
ShowMap(m); |
|
178 |
#endif |
|
179 |
||
180 |
/* Test the first corner */ |
|
181 |
if (m->Cells[new_pos.y+mov.y+opposite1.y][new_pos.x+mov.x+opposite1.x] |
|
182 |
== WALL) |
|
183 |
{ |
|
184 |
/* Test wall at opposites 1*/ |
|
185 |
if (m->Cells[new_pos.y+opposite1.y][new_pos.x+opposite1.x] |
|
186 |
== WALL && |
|
187 |
m->man_moves[new_pos.y+mov.y][new_pos.x+mov.x] == BOX) |
|
188 |
{ |
|
189 |
return TRUE; |
|
190 |
} |
|
191 |
else |
|
192 |
if (m->man_moves[new_pos.y+opposite1.y][new_pos.x+opposite1.x] |
|
193 |
== BOX && |
|
194 |
m->Cells[new_pos.y+mov.y][new_pos.x+mov.x] == WALL) |
|
195 |
{ |
|
196 |
return TRUE; |
|
197 |
} |
|
198 |
/* Test wall at opposites 2 */ |
|
199 |
if (m->Cells[new_pos.y+opposite2.y][new_pos.x+opposite2.x] |
|
200 |
== WALL && |
|
201 |
m->man_moves[new_pos.y+mov.y][new_pos.x+mov.x] == BOX) |
|
202 |
{ |
|
203 |
return TRUE; |
|
204 |
} |
|
205 |
else |
|
206 |
if (m->man_moves[new_pos.y+opposite2.y][new_pos.x+opposite2.x] |
|
207 |
== BOX && |
|
208 |
m->Cells[new_pos.y+mov.y][new_pos.x+mov.x] == WALL) |
|
209 |
{ |
|
210 |
return TRUE; |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
/* Test the second corner */ |
|
215 |
if (m->Cells[new_pos.y+mov.y+opposite2.y][new_pos.x+mov.x+opposite2.x] |
|
216 |
== WALL) |
|
217 |
{ |
|
218 |
/* Test wall at opposites 1*/ |
|
219 |
if (m->Cells[new_pos.y+opposite1.y][new_pos.x+opposite1.x] |
|
220 |
== WALL && |
|
221 |
m->man_moves[new_pos.y+mov.y][new_pos.x+mov.x] == BOX) |
|
222 |
{ |
|
223 |
return TRUE; |
|
224 |
} |
|
225 |
else |
|
226 |
if (m->man_moves[new_pos.y+opposite1.y][new_pos.x+opposite1.x] |
|
227 |
== BOX && |
|
228 |
m->Cells[new_pos.y+mov.y][new_pos.x+mov.x] == WALL) |
|
229 |
{ |
|
230 |
return TRUE; |
|
231 |
} |
|
232 |
/* Test wall at opposites 2 */ |
|
233 |
if (m->Cells[new_pos.y+opposite2.y][new_pos.x+opposite2.x] |
|
234 |
== WALL && |
|
235 |
m->man_moves[new_pos.y+mov.y][new_pos.x+mov.x] == BOX) |
|
236 |
{ |
|
237 |
return TRUE; |
|
238 |
} |
|
239 |
else |
|
240 |
if (m->man_moves[new_pos.y+opposite2.y][new_pos.x+opposite2.x] |
|
241 |
== BOX && |
|
242 |
m->Cells[new_pos.y+mov.y][new_pos.x+mov.x] == WALL) |
|
243 |
{ |
|
244 |
return TRUE; |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
return FALSE; |
|
249 |
} |
|
250 |
||
251 |
/* |
|
252 |
Catch the situation where a corner gets surrounded by boxes. |
|
253 |
*/ |
|
254 |
static int is_dead2(const struct Map * m, const struct Position mov, |
|
255 |
const struct Position new_pos) |
|
256 |
{ |
|
257 |
struct Position next, opposite1, opposite2; |
|
258 |
||
259 |
next.x = new_pos.x+mov.x; |
|
260 |
next.y = new_pos.y+mov.y; |
|
261 |
||
262 |
/* The corner must exist */ |
|
263 |
if (m->Cells[next.y][next.x] != CORNER) |
|
264 |
return FALSE; |
|
265 |
||
266 |
||
267 |
/* The wall corners must exist */ |
|
268 |
opposite1.x = next.x -mov.y; |
|
269 |
opposite1.y = next.y -mov.x; |
|
270 |
opposite2.x = next.x + mov.y; |
|
271 |
opposite2.y = next.y + mov.x; |
|
272 |
||
273 |
if (m->man_moves[opposite1.y][opposite1.x] == BOX) |
|
274 |
{ |
|
275 |
if(m->Cells[opposite1.y+mov.y][opposite1.x+mov.x] == WALL |
|
276 |
&& m->Cells[opposite1.y-mov.y][opposite1.x-mov.x] == WALL) |
|
277 |
return TRUE; |
|
278 |
} |
|
279 |
||
280 |
if (m->man_moves[opposite2.y][opposite2.x] == BOX) |
|
281 |
{ |
|
282 |
if(m->Cells[opposite2.y+mov.y][opposite2.x+mov.x] == WALL |
|
283 |
&& m->Cells[opposite2.y-mov.y][opposite2.x-mov.x] == WALL) |
|
284 |
return TRUE; |
|
285 |
} |
|
286 |
return FALSE; |
|
287 |
} |
|
288 |
||
289 |
static int is_box_movable(const struct Map * m, const struct Position mov, |
|
290 |
const struct Position box_pos) |
|
291 |
{ |
|
292 |
struct Position next_pos2; |
|
293 |
||
294 |
next_pos2.x = box_pos.x + mov.x; |
|
295 |
next_pos2.y = box_pos.y + mov.y; |
|
296 |
||
297 |
if((m->Cells[next_pos2.y][next_pos2.x] != BLANK && |
|
298 |
m->Cells[next_pos2.y][next_pos2.x] != PLATFORM) || |
|
299 |
m->man_moves[next_pos2.y][next_pos2.x] == BOX) |
|
300 |
{ |
|
301 |
return FALSE; |
|
302 |
} |
|
303 |
else if (is_dead1(m, mov, next_pos2) == TRUE) |
|
304 |
{ |
|
305 |
return FALSE; |
|
306 |
} |
|
307 |
else if (is_dead2(m, mov, next_pos2) == TRUE) |
|
308 |
{ |
|
309 |
return FALSE; |
|
310 |
} |
|
311 |
return TRUE; |
|
312 |
} |
|
313 |
||
314 |
/* It modifies m->man_moves */ |
|
315 |
static int get_box_movements(struct Map * m, |
|
316 |
struct BoxMove movements[]) |
|
317 |
{ |
|
318 |
struct Position pos[MAX_MOVES]; |
|
319 |
struct Position new_pos[MAX_MOVES]; |
|
320 |
int NumMoves, NewMoves; |
|
321 |
int j, i; |
|
322 |
struct Position next_pos; |
|
323 |
char *next_cell; |
|
324 |
int num_box_movements = 0; |
|
325 |
||
326 |
/* Let's the map with only walls in man_moves - other, blanks */ |
|
327 |
for (j = 0; j<m->SizeY; j++) |
|
328 |
for (i=0; i<m->SizeX; i++) |
|
329 |
{ |
|
330 |
if (m->Cells[j][i] == WALL) |
|
331 |
m->man_moves[j][i] = WALL; |
|
332 |
else |
|
333 |
m->man_moves[j][i] = BLANK; |
|
334 |
} |
|
335 |
||
336 |
/* Let's put the boxes */ |
|
337 |
for (i = 0; i<m->NumBoxes; i++) |
|
338 |
{ |
|
339 |
m->man_moves[m->Box[i].y][m->Box[i].x] = BOX; |
|
340 |
m->cells_boxes[m->Box[i].y][m->Box[i].x] = i; |
|
341 |
} |
|
342 |
||
343 |
NewMoves = 1; |
|
344 |
new_pos[0].x = m->Man.x; |
|
345 |
new_pos[0].y = m->Man.y; |
|
346 |
m->man_moves[m->Man.y][m->Man.x] = MANCANMOVE; |
|
347 |
while (NewMoves > 0) |
|
348 |
{ |
|
349 |
/* The before named "New Moves" become the moves we have |
|
350 |
to analyze */ |
|
351 |
NumMoves = NewMoves; |
|
352 |
for (i=0; i<NewMoves; i++) |
|
353 |
{ |
|
354 |
pos[i] = new_pos[i]; |
|
355 |
} |
|
356 |
||
357 |
/* Search new positions for each position */ |
|
358 |
NewMoves = 0; |
|
359 |
for (i=0; i<NumMoves; i++) |
|
360 |
{ |
|
361 |
/* For each direction */ |
|
362 |
for (j=0; j<4; j++) |
|
363 |
{ |
|
364 |
next_pos.x = pos[i].x + move_vectors[j].x; |
|
365 |
next_pos.y = pos[i].y + move_vectors[j].y; |
|
366 |
next_cell = &m->man_moves[next_pos.y][next_pos.x]; |
|
367 |
if(*next_cell == BLANK) |
|
368 |
{ |
|
369 |
new_pos[NewMoves] = next_pos; |
|
370 |
*next_cell = MANCANMOVE; |
|
371 |
NewMoves++; |
|
372 |
} |
|
373 |
else if (*next_cell == BOX) |
|
374 |
{ |
|
375 |
||
376 |
/* Check if the box is movable */ |
|
377 |
||
378 |
if (is_box_movable(m, move_vectors[j], |
|
379 |
next_pos )) |
|
380 |
{ |
|
381 |
{ |
|
382 |
movements[num_box_movements].box = |
|
383 |
m->cells_boxes[next_pos.y][next_pos.x]; |
|
384 |
movements[num_box_movements].dir = |
|
385 |
move_vectors[j]; |
|
386 |
num_box_movements++; |
|
387 |
} |
|
388 |
} |
|
389 |
||
390 |
} |
|
391 |
||
392 |
} |
|
393 |
} |
|
394 |
} |
|
395 |
||
396 |
return num_box_movements; |
|
397 |
} |
|
398 |
||
10 | 399 |
static void force_move_box(struct Map *m, const struct BoxMove move) |
6 | 400 |
{ |
401 |
struct Position newpos; |
|
402 |
||
403 |
||
404 |
/* Add coords */ |
|
405 |
newpos.x = m->Box[move.box].x + move.dir.x; |
|
406 |
newpos.y = m->Box[move.box].y + move.dir.y; |
|
407 |
||
408 |
/* Be certain the move can be done */ |
|
409 |
assert(m->Cells[newpos.y][newpos.x] != BOX); |
|
410 |
assert(m->Cells[newpos.y][newpos.x] != WALL); |
|
411 |
||
412 |
/* Control if we moved the box to a platform */ |
|
413 |
if(m->Cells[newpos.y][newpos.x] == PLATFORM) |
|
414 |
{ |
|
415 |
m->NumBoxesInPlatform++; |
|
416 |
} |
|
417 |
||
418 |
/* Control if we moved the box from a platform */ |
|
419 |
if (m->Cells[m->Box[move.box].y][m->Box[move.box].x] == PLATFORM) |
|
420 |
{ |
|
421 |
m->NumBoxesInPlatform--; |
|
422 |
} |
|
423 |
m->Man = m->Box[move.box]; |
|
424 |
||
425 |
m->Box[move.box] = newpos; |
|
426 |
} |
|
427 |
||
428 |
||
28
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
429 |
static void update_depth_counters(const int depth) |
6 | 430 |
{ |
431 |
if (depth > max_depth) |
|
432 |
max_depth = depth; |
|
433 |
if (depth > max_depth_period) |
|
434 |
max_depth_period = depth; |
|
435 |
if (depth < min_depth_period) |
|
436 |
min_depth_period = depth; |
|
28
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
437 |
} |
6 | 438 |
|
28
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
439 |
struct Map *all_maps; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
440 |
struct BoxMove *all_movements; /* DEPTH movements of MAX_MOVES */ |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
441 |
int *all_mov_tries; /* The actual step in movements for every depth */ |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
442 |
int *all_mov_max; /* Maximum of movements per all_movement element */ |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
443 |
float *percent; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
444 |
float *percent_part; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
445 |
int depth; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
446 |
|
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
447 |
int search_loop(const struct Map *origin) |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
448 |
{ |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
449 |
int found; /* bool */ |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
450 |
|
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
451 |
all_maps = malloc(sizeof(*all_maps) * (MAX_STEPS+1)); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
452 |
all_movements = malloc(sizeof(*all_movements)*MAX_MOVES*(MAX_STEPS+1)); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
453 |
all_mov_tries = malloc(sizeof(*all_mov_tries)*(MAX_STEPS+1)); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
454 |
all_mov_max = malloc(sizeof(*all_mov_max)*(MAX_STEPS+1)); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
455 |
percent = malloc(sizeof(*percent)*(MAX_STEPS+1)); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
456 |
percent_part = malloc(sizeof(*percent)*(MAX_STEPS+1)); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
457 |
|
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
458 |
|
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
459 |
if (load_state()) |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
460 |
{ |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
461 |
--depth; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
462 |
if (all_mov_tries[depth] != 0) |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
463 |
all_mov_tries[depth] = 0; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
464 |
} else |
6 | 465 |
{ |
28
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
466 |
depth = 0; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
467 |
CopyMap(&all_maps[0], origin); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
468 |
all_mov_max[0] = get_box_movements(&all_maps[0], &all_movements[0]); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
469 |
assert(all_mov_max[0] < MAX_MOVES); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
470 |
assert(all_mov_max[0] > 0); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
471 |
percent[0] = 0.; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
472 |
percent_part[0] = 100.; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
473 |
all_mov_tries[0] = 0; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
474 |
} |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
475 |
|
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
476 |
|
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
477 |
found = 0; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
478 |
do |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
479 |
{ |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
480 |
struct BoxMove *new_movements = &all_movements[(depth+1)*MAX_MOVES]; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
481 |
struct BoxMove *movements = &all_movements[depth*MAX_MOVES]; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
482 |
int num_movements = all_mov_max[depth]; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
483 |
int *num_new_movements = &all_mov_max[depth+1]; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
484 |
int *step = &all_mov_tries[depth]; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
485 |
struct Map *new_map = &all_maps[depth+1]; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
486 |
|
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
487 |
update_depth_counters(depth); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
488 |
|
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
489 |
percent_part[depth+1] = percent_part[depth] / num_movements; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
490 |
|
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
491 |
CopyMap(new_map, &all_maps[depth]); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
492 |
|
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
493 |
/* DEBUG */ |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
494 |
#if DEBUG |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
495 |
ShowMap(new_map); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
496 |
show_tries(depth); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
497 |
printf("Nummovs[%i]: %i\n", depth, num_movements); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
498 |
printf("Step[%i]: %i\n", depth, *step); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
499 |
#endif |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
500 |
|
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
501 |
/* Now four things can happen: |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
502 |
* - look for depth + 1 |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
503 |
* - keep looking for movements in depth |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
504 |
* - go one depth back. |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
505 |
* - solve the puzle */ |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
506 |
|
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
507 |
/* Go one depth back */ |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
508 |
if (*step >= num_movements) |
6 | 509 |
{ |
28
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
510 |
--depth; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
511 |
continue; |
6 | 512 |
} |
513 |
||
28
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
514 |
force_move_box(new_map, movements[(*step)]); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
515 |
|
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
516 |
++(*step); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
517 |
|
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
518 |
/* Solve the puzzle */ |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
519 |
if (new_map->NumPlatforms == new_map->NumBoxesInPlatform) |
6 | 520 |
{ |
28
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
521 |
PrintMoves(all_movements, all_mov_tries, depth+1); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
522 |
actual_map = &all_maps[depth+1]; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
523 |
show_percent_and_map(); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
524 |
save_state(); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
525 |
found = 1; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
526 |
} |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
527 |
|
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
528 |
if (is_new_map(all_maps, depth+1)) |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
529 |
{ |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
530 |
*num_new_movements = |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
531 |
get_box_movements(new_map, new_movements); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
532 |
assert(*num_new_movements < MAX_MOVES); |
6 | 533 |
} |
534 |
else |
|
28
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
535 |
*num_new_movements = 0; |
6 | 536 |
|
28
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
537 |
percent[depth] = percent[depth] + percent_part[depth+1]; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
538 |
percent_to_show = percent[depth]; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
539 |
|
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
540 |
/* Keep looking for movements in depth */ |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
541 |
if (*num_new_movements == 0) |
6 | 542 |
{ |
28
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
543 |
depth_to_show = depth; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
544 |
actual_map = &all_maps[depth]; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
545 |
continue; |
6 | 546 |
} |
28
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
547 |
|
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
548 |
/* Look for depth + 1*/ |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
549 |
if (depth+1 < MAX_STEPS) |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
550 |
{ |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
551 |
percent[depth+1] = percent[depth]; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
552 |
all_mov_tries[depth+1] = 0; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
553 |
++depth; |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
554 |
} |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
555 |
} while(!found && depth >= 0); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
556 |
free(all_maps); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
557 |
free(all_movements); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
558 |
free(all_mov_tries); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
559 |
free(all_mov_max); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
560 |
|
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
561 |
return found; |
6 | 562 |
} |
563 |
||
564 |
||
26
95fccfcbd04c
Less calls to libc. Profiling is much better now.
viric@llimona
parents:
25
diff
changeset
|
565 |
int solve_map(const struct Map *origin) |
6 | 566 |
{ |
567 |
struct BoxMove new_movements[MAX_MOVES]; |
|
568 |
int num_new_movements; |
|
26
95fccfcbd04c
Less calls to libc. Profiling is much better now.
viric@llimona
parents:
25
diff
changeset
|
569 |
int ret; |
6 | 570 |
|
571 |
init_os(); |
|
572 |
||
28
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
573 |
ret = search_loop(origin); |
cd27cb410375
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@llimona
parents:
26
diff
changeset
|
574 |
|
26
95fccfcbd04c
Less calls to libc. Profiling is much better now.
viric@llimona
parents:
25
diff
changeset
|
575 |
return ret; |
6 | 576 |
} |