-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.c
775 lines (704 loc) · 24.2 KB
/
ai.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
// Copyright 2024-2025 Viacheslav Chimishuk <[email protected]>
//
// This file is part of loderunner-ng.
//
// loderunner-ng is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// loderunner-ng is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with loderunner-ng. If not, see <http://www.gnu.org/licenses/>.
#include <stdlib.h>
#include "ai.h"
#include "exit.h"
#include "game.h"
#include "gold.h"
#include "guard.h"
#include "level.h"
#include "phys.h"
#include "sound.h"
// TODO: Should we join ai.c and guard.c.
// Move Policy configuration.
#define MP_NGUARDS 12 // Same as game.h:MAX_GUARDS actually.
#define MP_NMOVES 6
// Algorithm traces possible routes and calculate rating (score) for every
// route. Lower rating is better.
// Maximum rating constant works like a "disabled" value -- no route here.
#define RATING_MAX 255
// We want routes leading to level above the runner beat routes
// below him. To make it works we use this trick with rating offset.
#define RATING_BASE_BELLOW 200
#define RATING_BASE_ABOVE 100
static int move_policy[MP_NGUARDS][MP_NMOVES] = {
{0, 0, 0, 0, 0, 0},
{0, 1, 1, 0, 1, 1},
{1, 1, 1, 1, 1, 1},
{1, 2, 1, 1, 2, 1},
{1, 2, 2, 1, 2, 2},
{2, 2, 2, 2, 2, 2},
{2, 2, 3, 2, 2, 3},
{2, 3, 3, 2, 3, 3},
{3, 3, 3, 3, 3, 3},
{3, 3, 4, 3, 3, 4},
{3, 4, 4, 3, 4, 4},
{4, 4, 4, 4, 4, 4},
};
// Direction to move the guard to.
enum dir {
DIR_DOWN,
DIR_FALL,
DIR_LEFT,
DIR_NONE,
DIR_RIGHT,
DIR_UP,
};
// Return true if there is a guard present at x:y position and it not
// the same guard as one represented by `me` argument.
static bool occupied(struct game *game, struct guard *me, int x, int y)
{
struct guard *g = game_guard_get(game, x, y);
return g != NULL && g != me;
}
// Return random X coordinate to reborn guard at.
static int ai_rand_rebornx(void)
{
static int row[MAP_WIDTH - 1];
static int idx = MAP_WIDTH;
if (idx >= MAP_WIDTH) {
for (int i = 0; i < MAP_WIDTH; i++) {
row[i] = i;
}
for (int i = 0; i < MAP_WIDTH; i++) {
int j = (int) random() % MAP_WIDTH;
int t = row[i];
row[i] = row[j];
row[j] = t;
}
idx = 0;
}
return row[idx++];
}
static int ai_rand_goldholds(void)
{
return (random() % 26) + 11; // 11..36
}
// Try to drop gold if it is time.
// Every time guard moves to the new map tile gold holding counter
// is decremented. When counter reaches 0 gold is dropped.
static void ai_drop_gold(struct game *game, struct guard *guard)
{
int x = guard->x;
int y = guard->y;
if (guard->goldholds < 0) {
guard->goldholds++;
} else if (guard->goldholds == 0
&& guard->gold != NULL
&& is_tile(game, x, y, MAP_TILE_EMPTY)
&& ((y == MAP_HEIGHT - 1)
|| (is_tile(game, x, y + 1, MAP_TILE_BRICK)
|| is_tile(game, x, y + 1, MAP_TILE_SOLID)
|| is_tile(game, x, y + 1, MAP_TILE_LADDER)))) {
gold_drop(guard->gold, x, y);
guard->gold = NULL;
// Set gold holding counter to -1 to prevent picking up the gold we have
// just dropped before moving to the next tile.
guard->goldholds = -1;
} else if (guard->goldholds > 0) {
guard->goldholds--;
}
}
// Drop gold when trapped. Lost gold if it is not possible to drop gold.
// In this case as a result runner have to pickup one gold less.
static void ai_drop_gold_trapped(struct game *game, struct guard *guard)
{
if (guard->gold == NULL) {
return;
}
int x = guard->x;
int y = guard->y;
if (is_tile(game, x, y - 1, MAP_TILE_EMPTY)) {
gold_drop(guard->gold, x, y - 1);
} else {
gold_lose(guard->gold);
}
guard->gold = NULL;
// Set goldholds small enough to prevent guard from picking up the gold back
// he just dropped.
guard->goldholds = -2;
}
// Return true if tested map tile acts like a hole dug by the runner.
static bool ai_hole(struct game *g, int x, int y)
{
return g->map[y][x]->curt == MAP_TILE_EMPTY
&& g->map[y][x]->baset == MAP_TILE_BRICK;
}
// Check if tile at x:y coordinates has requested type and ignore holes dug
// by the runner. Scanning doesn't treat holes as an empty space but keep seeing
// them as bricks. However, since same-level scanning ignores bricks it ignores
// holes as well.
bool is_tilenh(struct game *game, int x, int y, enum map_tile_t t)
{
if (t == MAP_TILE_BRICK) {
return is_tile(game, x, y, t) || ai_hole(game, x, y);
}
return is_tile(game, x, y, t);
}
// Return true if guard is looking right.
static bool ai_looking_right(enum guard_state s)
{
return s == GSTATE_CLIMB_RIGHT
|| s == GSTATE_FALL_RIGHT
|| s == GSTATE_RIGHT;
}
// If guard and the runner on the same level (map row) guard moves
// directly toward the runner.
static enum dir ai_scan_level(struct game *game, struct guard *guard)
{
int rx = game->runner->x;
int ry = game->runner->y;
int gx = guard->x;
int gy = guard->y;
if (ry != gy) {
return DIR_NONE;
}
while (gx != rx) {
enum map_tile_t lvl = game->map[gy][gx]->baset;
enum map_tile_t nextlvl;
if (gy < MAP_HEIGHT) {
nextlvl = game->map[gy + 1][gx]->baset;
} else {
nextlvl = MAP_TILE_SOLID;
}
// Check if we can walk on the next level or use ladder or rope on
// the current level to avoid falling.
//
// TODO: Handle also situations when there is a hole with a guard
// trapped in that hole. In this case we can move on his head.
// Check level 43.
//
// TODO: Check nextlvl == MAP_TILE_ROPE for the level 92?
if (lvl == MAP_TILE_LADDER || lvl == MAP_TILE_ROPE
|| nextlvl == MAP_TILE_SOLID || nextlvl == MAP_TILE_LADDER
|| nextlvl == MAP_TILE_BRICK) {
if (gx < rx) {
gx++;
} else {
gx--;
}
} else {
break;
}
}
if (gx == rx) {
if (guard->x < rx) {
return DIR_RIGHT;
} else if (guard->x > rx) {
return DIR_LEFT;
} else {
if (guard->tx < game->runner->tx) {
return DIR_RIGHT;
} else {
return DIR_LEFT;
}
}
}
// Route tracing for the current level has not succeeded,
// try other directions.
return DIR_NONE;
}
// Scan downward direction.
// See also ai_scan_up() implementation.
static int ai_scan_down(struct game *game, int x, int y, int startx)
{
// Return "no route" if cannot move down.
if (y < MAP_HEIGHT - 1
&& (is_tilenh(game, x, y + 1, MAP_TILE_BRICK)
|| is_tilenh(game, x, y + 1, MAP_TILE_SOLID))) {
return RATING_MAX;
}
// Until we haven't reached the ground.
while (y < MAP_HEIGHT && !is_tilenh(game, x, y + 1, MAP_TILE_BRICK)
&& !is_tilenh(game, x, y + 1, MAP_TILE_SOLID)) {
// Try to trace left and right if we can (not in a freefall mode).
if (!is_tilenh(game, x, y, MAP_TILE_EMPTY)) {
// Check if we can turn left.
if (x > 0) {
// Check if we can potentially move left.
// This check looks strange, as we do not check if there is an empty
// tile at x-1:y where the guard can move into. But this is how it
// is implemented in the original code.
if (is_tilenh(game, x - 1, y + 1, MAP_TILE_BRICK)
|| is_tilenh(game, x - 1, y + 1, MAP_TILE_SOLID)
|| is_tilenh(game, x - 1, y + 1, MAP_TILE_LADDER)
|| is_tilenh(game, x - 1, y, MAP_TILE_ROPE)) {
// No need to keep moving down if we are already
// below the runner.
if (y >= game->runner->y) {
break;
}
}
}
// The same check for right.
if (x < MAP_WIDTH - 1) {
if (is_tilenh(game, x + 1, y + 1, MAP_TILE_BRICK)
|| is_tilenh(game, x + 1, y + 1, MAP_TILE_SOLID)
|| is_tilenh(game, x + 1, y + 1, MAP_TILE_LADDER)
|| is_tilenh(game, x + 1, y, MAP_TILE_ROPE)) {
if (y >= game->runner->y) {
break;
}
}
}
}
y++;
}
if (y == game->runner->y) {
return abs(startx - x);
} else if (y > game->runner->y) {
return RATING_BASE_BELLOW + (y - game->runner->y);
} else {
return RATING_BASE_ABOVE + (game->runner->y - y);
}
}
// Scan upward direction.
// Our main goal here is to move above the runner. The first level above
// the runner we can reach going up wins.
static int ai_scan_up(struct game *game, int x, int y, int startx)
{
// We cannot move up without ladder for sure.
if (!is_tilenh(game, x, y, MAP_TILE_LADDER)) {
return RATING_MAX;
}
while (y > 0 && is_tilenh(game, x, y, MAP_TILE_LADDER)) {
y--;
if (x > 0) {
// Check if we can potentially move left.
// This check looks strange, as we do not check if there is an empty
// tile at x-1:y where the guard can move into. But this is how it
// is implemented in the original code.
if (is_tilenh(game, x - 1, y + 1, MAP_TILE_BRICK)
|| is_tilenh(game, x - 1, y + 1, MAP_TILE_SOLID)
|| is_tilenh(game, x - 1, y + 1, MAP_TILE_LADDER)
|| is_tilenh(game, x - 1, y, MAP_TILE_ROPE)) {
// No need to keep moving up if we are already above the runner.
if (y <= game->runner->y) {
break;
}
}
}
// Perform the same logic for the right edge of the ladder.
if (x < MAP_WIDTH) {
if (is_tilenh(game, x + 1, y + 1, MAP_TILE_BRICK)
|| is_tilenh(game, x + 1, y + 1, MAP_TILE_SOLID)
|| is_tilenh(game, x + 1, y + 1, MAP_TILE_LADDER)
|| is_tilenh(game, x + 1, y, MAP_TILE_ROPE)) {
if (y <= game->runner->y) {
break;
}
}
}
}
if (y == game->runner->y) {
return abs(startx - x);
} else if (y > game->runner->y) {
return RATING_BASE_BELLOW + (y - game->runner->y);
} else {
return RATING_BASE_ABOVE + (game->runner->y - y);
}
}
// Scan horizontally right or left.
// Looking horizontally we execute up and down scan for the every step we
// can perform left or right depends on the `left` flag. Up and down branches
// are ranged by rating, more distant route wins between two routes with the
// same rating.
static int ai_scan_horizontal(struct game *game, int x, int y, bool left)
{
int rating = RATING_MAX;
int startx = x;
int dx = left ? -1 : 1;
for (;;) {
// Check if we have reached edge of the screen.
if (left && x == 0) {
break;
} else if (!left && x == MAP_WIDTH - 1) {
break;
}
// Horisontal scanning should not ignore holes, so guard can walks
// out from a cave.
if (is_tile(game, x + dx, y, MAP_TILE_BRICK)
|| is_tile(game, x + dx, y, MAP_TILE_SOLID)) {
// We have reached a wall.
break;
}
// Can climb left despite what is under the feet.
bool climb = is_tilenh(game, x + dx, y, MAP_TILE_LADDER)
|| is_tilenh(game, x + dx, y, MAP_TILE_ROPE);
// Can walk over the solid ground.
bool walk = (y == MAP_HEIGHT - 1)
|| (is_tilenh(game, x + dx, y + 1, MAP_TILE_BRICK)
|| is_tilenh(game, x + dx, y + 1, MAP_TILE_SOLID)
|| is_tilenh(game, x + dx, y + 1, MAP_TILE_LADDER));
x += dx;
// Usually of the two routes with the same rating the first traced wins
// but for right and left tracing original code uses another strategy.
// It traces starting from the farthest point and moves to the runner.
// Since I start from the runner and move left we need this rating
// comparison inversion.
// Again, I'm not this is really matters because after runner moves
// using this route it will be recalculated and up/down lookup goes
// first. As a result, probably, this can be simplified.
int r = ai_scan_down(game, x, y, startx);
int q = ai_scan_up(game, x, y, startx);
if (q < r) {
r = q;
}
if (r <= rating) {
rating = r;
}
if (!climb && !walk) {
// Falling down.
// For me it looks like this complex check be replaced with
// a simple falling down check. Strange that the original code
// doesn't do that. Am I missing something?
break;
}
}
return rating;
}
// Check if the guard must freefall down into a hole or over regular empty tile.
static bool ai_falling(struct game *game, struct guard *guard)
{
int x = guard->x;
int y = guard->y;
int ty = guard->ty;
int hdy = MOVE_DY / 2;
if (is_tile(game, x, y, MAP_TILE_LADDER)
|| (is_tile(game, x, y, MAP_TILE_ROPE) && (ty > -hdy && ty <= hdy))) {
return false;
}
if (ty < 0
|| (y < MAP_HEIGHT - 1
&& !is_tile(game, x, y + 1, MAP_TILE_BRICK)
&& !is_tile(game, x, y + 1, MAP_TILE_SOLID)
&& !is_tile(game, x, y + 1, MAP_TILE_LADDER)
&& !game_guard_get(game, x, y + 1))) {
return true;
}
return false;
}
// Guard's AI is completely copied from Simon Hung's LodeRunner TotalRecall
// implementation. See https://github.com/SimonHung/LodeRunner_TotalRecall
// Code understanding was easy with help of great TotalRecall's source code
// overview from Ahab which is available at
// https://datadrivengamer.blogspot.com/2023/01/championship-lode-runner-guard.html
//
// Look for the direction to move the guard.
// General AI algorithm works as follows.
// * If guard and the runner on the same level (map row) then guard tries
// to move directly towards the runner if he can. However, rules are a
// little bit tricky: walls and holes dug by the runner are ignore during
// the trace.
// If guard and the runner not on the same level next directions are traced.
// All four directions are traced and rating for each potential route is
// calculated. Route with the lowest rating wins.
// * Trace down path: either guard can freefall or climb using a ladder.
// Every time try to turn left or right as close to the runner's level as
// possible.
// * Trace up path: the same logic as tracing down actually.
// * Trace left path: scan down and up for every step we can perform
// moving left.
// * Trace right path: the same logic as tracing left actually.
static enum dir ai_scan(struct game *game, struct guard *guard)
{
// Avoid falling back to the hole we have just climbed out from by
// disabling all movement or falling down.
bool no_down = guard->hole;
if (guard->state == GSTATE_CLIMB_OUT) {
return DIR_UP;
}
if (!no_down && ai_falling(game, guard)) {
return DIR_FALL;
}
// Move towards the runner on the same level if we can.
enum dir d = ai_scan_level(game, guard);
if (d != DIR_NONE) {
return d;
}
int score = RATING_MAX;
int s;
if (!no_down) {
s = ai_scan_down(game, guard->x, guard->y, guard->x);
if (s < score) {
score = s;
d = DIR_DOWN;
}
}
s = ai_scan_up(game, guard->x, guard->y, guard->x);
if (s < score) {
score = s;
d = DIR_UP;
}
s = ai_scan_horizontal(game, guard->x, guard->y, true);
if (s < score) {
score = s;
d = DIR_LEFT;
}
s = ai_scan_horizontal(game, guard->x, guard->y, false);
if (s < score) {
score = s;
d = DIR_RIGHT;
}
return d;
}
// Make guard to make a single step in the calculated direction if we can.
static void ai_move_guard(struct game *game, struct guard *guard, enum dir d)
{
enum guard_state state = guard->state;
bool move = false;
int x = guard->x;
int y = guard->y;
int tx = guard->tx;
int ty = guard->ty;
switch (d) {
case DIR_DOWN:
ty += MOVE_DY;
tx = 0;
if (ty > TILE_MAP_HEIGHT / 2) {
y += 1;
ty -= TILE_MAP_HEIGHT;
ai_drop_gold(game, guard);
}
if (occupied(game, guard, x, y)
|| (ty >= 0 && occupied(game, guard, x, y + 1))
|| (ty >= 0 && !can_move(game, x, y + 1))) {
move = false;
} else {
if (!is_tile(game, x, y, MAP_TILE_LADDER) &&
!is_tile(game, x, y + 1, MAP_TILE_LADDER)) {
if (ai_looking_right(state)) {
state = GSTATE_FALL_RIGHT;
} else {
state = GSTATE_FALL_LEFT;
}
} else {
state = GSTATE_UPDOWN;
}
move = true;
}
break;
case DIR_FALL:
ty += MOVE_DY;
tx = 0;
if (ty > TILE_MAP_HEIGHT / 2) {
y += 1;
ty -= TILE_MAP_HEIGHT;
ai_drop_gold(game, guard);
}
bool trapped = false;
if (ty < 0 && ai_hole(game, x, y)) {
guard->holey = y;
}
if (ty >= 0) {
if (ai_hole(game, x, y) && guard->holey == y) {
trapped = true;
guard->hole = true;
ty = 0;
ai_drop_gold_trapped(game, guard);
game_score(game, SCORE_TRAP);
sound_play(SOUND_TRAP);
} else if (occupied(game, guard, x, y)
|| (!can_move(game, x, y + 1)
&& !is_tile(game, x, y + 1, MAP_TILE_FALSE))) {
ty = 0;
}
}
if (ai_looking_right(state)) {
state = trapped ? GSTATE_TRAP_RIGHT : GSTATE_FALL_RIGHT;
} else {
state = trapped ? GSTATE_TRAP_LEFT : GSTATE_FALL_LEFT;
}
move = true;
break;
case DIR_LEFT:
tx -= MOVE_DX;
ty = 0;
if (tx < -(TILE_MAP_WIDTH / 2)) {
x -= 1;
tx += TILE_MAP_WIDTH;
guard->hole = false;
guard->holey = -1;
ai_drop_gold(game, guard);
}
if (occupied(game, guard, x, y)
|| (tx < 0 && !can_move(game, x - 1, y))) {
move = false;
} else {
if (is_tile(game, x, y, MAP_TILE_ROPE)) {
state = GSTATE_CLIMB_LEFT;
} else {
state = GSTATE_LEFT;
}
move = true;
}
break;
case DIR_NONE:
break;
case DIR_RIGHT:
tx += MOVE_DX;
ty = 0;
if (tx > TILE_MAP_WIDTH / 2) {
x += 1;
tx -= TILE_MAP_WIDTH;
guard->hole = false;
guard->holey = -1;
ai_drop_gold(game, guard);
}
if (occupied(game, guard, x, y)
|| (tx > 0 && !can_move(game, x + 1, y))) {
move = false;
} else {
if (is_tile(game, x, y, MAP_TILE_ROPE)) {
state = GSTATE_CLIMB_RIGHT;
} else {
state = GSTATE_RIGHT;
}
move = true;
}
break;
case DIR_UP:
ty -= MOVE_DY;
tx = 0;
if (ty < -(TILE_MAP_HEIGHT / 2)) {
y -= 1;
ty += TILE_MAP_HEIGHT;
ai_drop_gold(game, guard);
}
bool onladder = is_tile(game, x, y, MAP_TILE_LADDER);
bool climb_out = guard->state == GSTATE_CLIMB_OUT;
if (climb_out) {
if (ai_hole(game, x, y) && guard->holey == y) {
if (!occupied(game, guard, x, y) && can_move(game, x, y - 1)) {
// Climb up from the hole if we can.
move = true;
}
} else {
// We are over the hole now, let's move left or right now.
state = GSTATE_UPDOWN;
move = true;
}
} else if (occupied(game, guard, x, y)
|| (ty < 0 && occupied(game, guard, x, y - 1))
|| (ty < 0 && (!onladder || !can_move(game, x, y - 1)))) {
move = false;
} else {
state = GSTATE_UPDOWN;
move = true;
}
break;
default:
die("illegal state");
}
if (move) {
guard->x = x;
guard->y = y;
guard->tx = tx;
guard->ty = ty;
animation_tick(guard->cura);
}
if (guard->state != state) {
// When stopped keep current animation.
if (state != GSTATE_STOP) {
guard->cura = guard_state_animation(guard, state);
}
guard->state = state;
}
}
// Set guard into reborn state after he died immured in the wall.
void ai_reborn(struct game *game, struct guard *guard)
{
int x = ai_rand_rebornx();
int y = 1;
int xs = x;
// Avoid guard to be born in holes or where gold lays.
while (!is_tile(game, x, y, MAP_TILE_EMPTY) || ai_hole(game, x, y)
|| gold_get(game, x, y) != NULL) {
x = ai_rand_rebornx();
if (x == xs) {
// We have tried all positions this row, let's move to the next one.
y++;
if (y == MAP_HEIGHT) {
die("guard cannot be born");
}
}
}
guard->x = x;
guard->y = y;
guard->hole = false;
guard->holey = -1;
guard->state = GSTATE_REBORN;
guard->cura = guard_state_animation(guard, GSTATE_REBORN);
}
// Callback to move guards.
// Called on every game loop tick, calculates direction to move for every
// guard and make the move. On every call a few guards are moved depends on
// the policy defined by move_policy table.
void ai_tick(struct game *game)
{
static int imoves = MP_NMOVES;
static int iguard = 0;
if (++imoves >= MP_NMOVES) {
imoves = 0;
}
// Regular (running) guards move logic.
int moves = move_policy[game->nguards][imoves];
while (moves-- > 0) {
if (++iguard >= game->nguards) {
iguard = 0;
}
struct guard *g = game->guards[iguard];
if (g->state == GSTATE_TRAP_LEFT
|| g->state == GSTATE_TRAP_RIGHT
|| g->state == GSTATE_REBORN) {
continue;
}
enum dir d = ai_scan(game, g);
ai_move_guard(game, g, d);
}
// Rebornd and trapped guards climbing out logic.
for (int i = 0; i < game->nguards; i++) {
struct guard *g = game->guards[i];
if (g->state == GSTATE_TRAP_LEFT
|| g->state == GSTATE_TRAP_RIGHT) {
if (animation_tick(g->cura)) {
g->state = GSTATE_CLIMB_OUT;
g->cura = guard_state_animation(g, GSTATE_CLIMB_OUT);
}
} else if (g->state == GSTATE_REBORN) {
if (animation_tick(g->cura)) {
g->state = GSTATE_FALL_RIGHT;
g->cura = guard_state_animation(g, GSTATE_FALL_RIGHT);
game_score(game, SCORE_DEAD);
sound_play(SOUND_REBORN);
}
}
// Pick up gold when step over it.
if (g->gold == NULL && g->goldholds == 0) {
struct gold *gld = gold_pickup(game, g->x, g->y, g->tx, g->ty);
if (gld != NULL) {
g->gold = gld;
g->goldholds = ai_rand_goldholds();
}
}
// If guard walled up in the wall it becomes dead and should be reborn.
if (g->state != GSTATE_REBORN
&& is_tile(game, g->x, g->y, MAP_TILE_BRICK)) {
ai_reborn(game, g);
}
}
}