forked from TheGLander/tworld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lxlogic.c
2043 lines (1842 loc) · 53.1 KB
/
lxlogic.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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* lxlogic.c: The game logic for the Lynx ruleset.
*
* Copyright (C) 2001-2015 by Brian Raiter and Eric Schmidt, under the GNU
* General Public License. No warranty. See COPYING for details.
*/
#include <stdlib.h>
#include <string.h>
#include "defs.h"
#include "err.h"
#include "state.h"
#include "random.h"
#include "logic.h"
/* A number well above the maximum number of creatures that could possibly
* exist simultaneously.
*/
#define MAX_CREATURES (2 * CXGRID * CYGRID)
/* The maximum number of creatures on the original Atari Lynx version.
*/
#define PMAX_CREATURES 128
/* Temporary "holding" values used in place of a direction.
*/
#define WALKER_TURN (NORTH | SOUTH | EAST)
#define BLOB_TURN (NORTH | SOUTH | WEST)
/* TRUE if dir is a diagonal move.
*/
#define isdiagonal(dir) (((dir) & (NORTH | SOUTH)) && ((dir) & (EAST | WEST)))
/* Internal assertion macro.
*/
#ifdef NDEBUG
#define _assert(test) ((void)0)
#else
#define _assert(test) ((test) || (die("internal error: failed sanity check" \
" (%s)\nPlease report this error to" \
" [email protected]", #test), 0))
#endif
/* A list of ways for Chip to lose.
*/
enum {
CHIP_OKAY = 0,
CHIP_DROWNED, CHIP_BURNED, CHIP_BOMBED, CHIP_OUTOFTIME, CHIP_COLLIDED,
CHIP_NOTOKAY
};
/* Pedantic mode flag. (Having this variable defined here is a hack,
* but this is the only module that actually uses it.)
*/
int pedanticmode = FALSE;
/* Declarations of (indirectly recursive) functions.
*/
static int canmakemove(creature const *cr, int dir, int flags);
static int advancecreature(creature *cr, int releasing);
/* Used to calculate movement offsets.
*/
static int const delta[] = { 0, -CXGRID, -1, 0, +CXGRID, 0, 0, 0, +1 };
/* The direction used the last time something stepped onto a random
* slide floor.
*/
static int lastrndslidedir = NORTH;
/* The most recently used stepping phase value.
*/
static int laststepping = 0;
/* The memory used to hold the list of creatures.
*/
static creature *creaturearray = NULL;
/* A pointer to the game state, used so that it doesn't have to be
* passed to every single function.
*/
static gamestate *state;
/*
* Accessor macros for various fields in the game state. Many of the
* macros can be used as an lvalue.
*/
#define setstate(p) (state = (p)->state)
#define creaturelist() (state->creatures)
#define getchip() (creaturelist())
#define chippos() (getchip()->pos)
#define chipisalive() (getchip()->id == Chip)
#define mainprng() (&state->mainprng)
#define timelimit() (state->timelimit)
#define timeoffset() (state->timeoffset)
#define currenttime() (state->currenttime)
#define currentinput() (state->currentinput)
#define lastmove() (state->lastmove)
#define stepping() (state->stepping)
#define rndslidedir() (state->initrndslidedir)
#define xviewpos() (state->xviewpos)
#define yviewpos() (state->yviewpos)
#define setnosaving() (state->statusflags |= SF_NOSAVING)
#define showhint() (state->statusflags |= SF_SHOWHINT)
#define hidehint() (state->statusflags &= ~SF_SHOWHINT)
#define markinvalid() (state->statusflags |= SF_INVALID)
#define ismarkedinvalid() (state->statusflags & SF_INVALID)
#define chipsneeded() (state->chipsneeded)
#define clonerlist() (state->cloners)
#define clonerlistsize() (state->clonercount)
#define traplist() (state->traps)
#define traplistsize() (state->trapcount)
#define getlxstate() (state->lxstate)
#define completed() (getlxstate().completed)
#define togglestate() (getlxstate().togglestate)
#define couldntmove() (getlxstate().couldntmove)
#define chippushing() (getlxstate().pushing)
#define chipstuck() (getlxstate().stuck)
#define mapbreached() (getlxstate().mapbreached)
#define chiptopos() (getlxstate().chiptopos)
#define chiptocr() (getlxstate().chiptocr)
#define putwall() (getlxstate().putwall)
#define prngvalue1() (getlxstate().prng1)
#define prngvalue2() (getlxstate().prng2)
#define xviewoffset() (getlxstate().xviewoffset)
#define yviewoffset() (getlxstate().yviewoffset)
#define creaturelistend() (getlxstate().crend)
#define inendgame() (getlxstate().endgametimer)
#define startendgametimer() (getlxstate().endgametimer = 12 + 1)
#define decrendgametimer() (--getlxstate().endgametimer)
#define resetendgametimer() (getlxstate().endgametimer = 0)
#define addsoundeffect(sfx) (state->soundeffects |= 1 << (sfx))
#define stopsoundeffect(sfx) (state->soundeffects &= ~(1 << (sfx)))
#define floorat(pos) (state->map[pos].top.id)
#define possession(obj) (*_possession(obj))
static short *_possession(int obj)
{
switch (obj) {
case Key_Red: return &state->keys[0];
case Key_Blue: return &state->keys[1];
case Key_Yellow: return &state->keys[2];
case Key_Green: return &state->keys[3];
case Boots_Ice: return &state->boots[0];
case Boots_Slide: return &state->boots[1];
case Boots_Fire: return &state->boots[2];
case Boots_Water: return &state->boots[3];
case Door_Red: return &state->keys[0];
case Door_Blue: return &state->keys[1];
case Door_Yellow: return &state->keys[2];
case Door_Green: return &state->keys[3];
case Ice: return &state->boots[0];
case IceWall_Northwest: return &state->boots[0];
case IceWall_Northeast: return &state->boots[0];
case IceWall_Southwest: return &state->boots[0];
case IceWall_Southeast: return &state->boots[0];
case Slide_North: return &state->boots[1];
case Slide_West: return &state->boots[1];
case Slide_South: return &state->boots[1];
case Slide_East: return &state->boots[1];
case Slide_Random: return &state->boots[1];
case Fire: return &state->boots[2];
case Water: return &state->boots[3];
}
warn("Invalid object %d handed to possession()\n", obj);
_assert(!"possession() called with an invalid object");
return NULL;
}
/* The pseudorandom number generator, used by walkers and blobs. This
* exactly matches the PRNG used in the original Lynx game.
*/
static unsigned char lynx_prng(void)
{
unsigned char n;
n = (prngvalue1() >> 2) - prngvalue1();
if (!(prngvalue1() & 0x02))
--n;
prngvalue1() = (prngvalue1() >> 1) | (prngvalue2() & 0x80);
prngvalue2() = (prngvalue2() << 1) | (n & 0x01);
return (prngvalue1() ^ prngvalue2()) & 0xFF;
}
/*
* Simple floor functions.
*/
/* Floor state flags.
*/
#define FS_CLAIMED 0x40 /* spot is claimed by a creature */
#define FS_ANIMATED 0x20 /* spot is playing an animation */
#define FS_BEARTRAP 0x01 /* there is or was a beartrap here */
#define FS_TELEPORT 0x02 /* there is or was a teleport here */
/* Accessor macros for the floor states.
*/
#define claimlocation(pos) (state->map[pos].top.state |= FS_CLAIMED)
#define removeclaim(pos) (state->map[pos].top.state &= ~FS_CLAIMED)
#define islocationclaimed(pos) (state->map[pos].top.state & FS_CLAIMED)
#define markanimated(pos) (state->map[pos].top.state |= FS_ANIMATED)
#define clearanimated(pos) (state->map[pos].top.state &= ~FS_ANIMATED)
#define ismarkedanimated(pos) (state->map[pos].top.state & FS_ANIMATED)
#define markbeartrap(pos) (state->map[pos].top.state |= FS_BEARTRAP)
#define ismarkedbeartrap(pos) (state->map[pos].top.state & FS_BEARTRAP)
#define markteleport(pos) (state->map[pos].top.state |= FS_TELEPORT)
#define ismarkedteleport(pos) (state->map[pos].top.state & FS_TELEPORT)
/* Translate a slide floor into the direction it points in. In the
* case of a random slide floor, if advance is TRUE a new direction
* shall be selected; otherwise the current direction is used.
*/
static int getslidedir(int floor, int advance)
{
switch (floor) {
case Slide_North: return NORTH;
case Slide_West: return WEST;
case Slide_South: return SOUTH;
case Slide_East: return EAST;
case Slide_Random:
if (advance)
lastrndslidedir = right(lastrndslidedir);
return lastrndslidedir;
}
warn("Invalid floor %d handed to getslidedir()\n", floor);
_assert(!"getslidedir() called with an invalid object");
return NIL;
}
/* Alter a creature's direction if they are at an ice wall.
*/
static void applyicewallturn(creature *cr)
{
int floor, dir;
floor = floorat(cr->pos);
dir = cr->dir;
switch (floor) {
case IceWall_Northeast:
dir = dir == SOUTH ? EAST : dir == WEST ? NORTH : dir;
break;
case IceWall_Southwest:
dir = dir == NORTH ? WEST : dir == EAST ? SOUTH : dir;
break;
case IceWall_Northwest:
dir = dir == SOUTH ? WEST : dir == EAST ? NORTH : dir;
break;
case IceWall_Southeast:
dir = dir == NORTH ? EAST : dir == WEST ? SOUTH : dir;
break;
}
cr->dir = dir;
}
/* Find the location of a beartrap from one of its buttons.
*/
static int trapfrombutton(int pos)
{
xyconn *xy;
int i;
if (pedanticmode) {
i = pos;
for (;;) {
++i;
if (i == CXGRID * CYGRID)
i = 0;
if (i == pos)
break;
if (floorat(i) == Beartrap)
return i;
if (ismarkedbeartrap(i))
return -1;
}
} else {
for (xy = traplist(), i = traplistsize() ; i ; ++xy, --i)
if (xy->from == pos)
return xy->to;
}
return -1;
}
/* Find the location of a clone machine from one of its buttons.
*/
static int clonerfrombutton(int pos)
{
xyconn *xy;
int i;
if (pedanticmode) {
i = pos;
for (;;) {
++i;
if (i == CXGRID * CYGRID)
i = 0;
if (i == pos)
break;
if (floorat(i) == CloneMachine)
return i;
}
} else {
for (xy = clonerlist(), i = clonerlistsize() ; i ; ++xy, --i)
if (xy->from == pos)
return xy->to;
}
return -1;
}
/* Quell any continuous sound effects coming from what Chip is
* standing on. If includepushing is TRUE, also quell the sound of any
* blocks being pushed.
*/
static void resetfloorsounds(int includepushing)
{
stopsoundeffect(SND_SKATING_FORWARD);
stopsoundeffect(SND_SKATING_TURN);
stopsoundeffect(SND_FIREWALKING);
stopsoundeffect(SND_WATERWALKING);
stopsoundeffect(SND_ICEWALKING);
stopsoundeffect(SND_SLIDEWALKING);
stopsoundeffect(SND_SLIDING);
if (includepushing)
stopsoundeffect(SND_BLOCK_MOVING);
}
/*
* Functions that manage the list of entities.
*/
/* Creature state flags.
*/
#define CS_FDIRMASK 0x0F /* temp storage for forced moves */
#define CS_SLIDETOKEN 0x10 /* can move off of a slide floor */
#define CS_REVERSE 0x20 /* needs to turn around */
#define CS_PUSHED 0x40 /* block was pushed by Chip */
#define CS_TELEPORTED 0x80 /* creature was just teleported */
#define getfdir(cr) ((cr)->state & CS_FDIRMASK)
#define setfdir(cr, d) ((cr)->state = ((cr)->state & ~CS_FDIRMASK) \
| ((d) & CS_FDIRMASK))
/* Return the creature located at pos. Ignores Chip unless includechip
* is TRUE. (This is important in the case when Chip and a second
* creature are currently occupying a single location.)
*/
static creature *lookupcreature(int pos, int includechip)
{
creature *cr;
cr = creaturelist();
if (!includechip)
++cr;
for ( ; cr->id ; ++cr)
if (cr->pos == pos && !cr->hidden && !isanimation(cr->id))
return cr;
return NULL;
}
/* Return a fresh creature.
*/
static creature *newcreature(void)
{
creature *cr;
for (cr = creaturelist() + 1 ; cr->id ; ++cr) {
if (cr->hidden)
return cr;
}
if (cr - creaturelist() >= MAX_CREATURES) {
warn("Ran out of room in the creatures array!");
return NULL;
}
if (pedanticmode && cr - creaturelist() >= PMAX_CREATURES)
return NULL;
cr->hidden = TRUE;
cr[1].id = Nothing;
creaturelistend() = cr;
return cr;
}
/* Flag all tanks to turn around.
*/
static void turntanks(void)
{
creature *cr;
for (cr = creaturelist() ; cr->id ; ++cr) {
if (cr->hidden)
continue;
if (cr->id != Tank)
continue;
if (floorat(cr->pos) == CloneMachine || isice(floorat(cr->pos)))
continue;
cr->state ^= CS_REVERSE;
}
}
/* Start an animation sequence at the spot (formerly) occupied by the
* given creature. The creature's slot in the creature list is reused
* by the animation sequence.
*/
static void removecreature(creature *cr, int animationid)
{
if (cr->id != Chip)
removeclaim(cr->pos);
if (cr->state & CS_PUSHED)
stopsoundeffect(SND_BLOCK_MOVING);
cr->id = animationid;
cr->frame = ((currenttime() + stepping()) & 1) ? 12 : 11;
--cr->frame;
cr->hidden = FALSE;
cr->state = 0;
cr->tdir = NIL;
if (cr->moving == 8) {
cr->pos -= delta[cr->dir];
cr->moving = 0;
}
markanimated(cr->pos);
}
/* End the given animation sequence (thus removing the final vestige
* of an ex-creature).
*/
static void removeanimation(creature *cr)
{
cr->hidden = TRUE;
clearanimated(cr->pos);
if (cr == creaturelistend()) {
cr->id = Nothing;
--creaturelistend();
}
}
/* Abort the animation sequence occuring at the given location.
*/
static int stopanimationat(int pos)
{
creature *anim;
for (anim = creaturelist() ; anim->id ; ++anim) {
if (!anim->hidden && anim->pos == pos && isanimation(anim->id)) {
removeanimation(anim);
return TRUE;
}
}
return FALSE;
}
/* What happens when Chip dies. reason indicates the cause of death.
* also is either NULL or points to a creature that dies with Chip.
*/
static void removechip(int reason, creature *also)
{
creature *chip = getchip();
switch (reason) {
case CHIP_DROWNED:
addsoundeffect(SND_WATER_SPLASH);
removecreature(chip, Water_Splash);
break;
case CHIP_BOMBED:
addsoundeffect(SND_BOMB_EXPLODES);
removecreature(chip, Bomb_Explosion);
break;
case CHIP_OUTOFTIME:
removecreature(chip, Entity_Explosion);
break;
case CHIP_BURNED:
addsoundeffect(SND_CHIP_LOSES);
removecreature(chip, Entity_Explosion);
break;
case CHIP_COLLIDED:
addsoundeffect(SND_CHIP_LOSES);
removecreature(chip, Entity_Explosion);
if (also && also != chip)
removecreature(also, Entity_Explosion);
break;
}
resetfloorsounds(FALSE);
startendgametimer();
timeoffset() = 1;
}
/*
* The laws of movement across the various floors.
*
* Chip, blocks, and other creatures all have slightly different rules
* about what sort of tiles they are permitted to move into and out
* of. The following lookup table encapsulates these rules. Note that
* these rules are only the first check; a creature may be generally
* permitted a particular type of move but still prevented in a
* specific situation.
*/
#define DIR_IN(dir) (dir)
#define DIR_OUT(dir) ((dir) << 4)
#define NORTH_IN DIR_IN(NORTH)
#define WEST_IN DIR_IN(WEST)
#define SOUTH_IN DIR_IN(SOUTH)
#define EAST_IN DIR_IN(EAST)
#define NORTH_OUT DIR_OUT(NORTH)
#define WEST_OUT DIR_OUT(WEST)
#define SOUTH_OUT DIR_OUT(SOUTH)
#define EAST_OUT DIR_OUT(EAST)
#define ALL_IN (NORTH_IN | WEST_IN | SOUTH_IN | EAST_IN)
#define ALL_OUT (NORTH_OUT | WEST_OUT | SOUTH_OUT | EAST_OUT)
#define ALL_IN_OUT (ALL_IN | ALL_OUT)
static struct { unsigned char chip, block, creature; } const movelaws[] = {
/* Nothing */
{ 0, 0, 0 },
/* Empty */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* Slide_North */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* Slide_West */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* Slide_South */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* Slide_East */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* Slide_Random */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* Ice */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* IceWall_Northwest */
{ NORTH_OUT | WEST_OUT | SOUTH_IN | EAST_IN,
NORTH_OUT | WEST_OUT | SOUTH_IN | EAST_IN,
NORTH_OUT | WEST_OUT | SOUTH_IN | EAST_IN },
/* IceWall_Northeast */
{ NORTH_OUT | EAST_OUT | SOUTH_IN | WEST_IN,
NORTH_OUT | EAST_OUT | SOUTH_IN | WEST_IN,
NORTH_OUT | EAST_OUT | SOUTH_IN | WEST_IN },
/* IceWall_Southwest */
{ SOUTH_OUT | WEST_OUT | NORTH_IN | EAST_IN,
SOUTH_OUT | WEST_OUT | NORTH_IN | EAST_IN,
SOUTH_OUT | WEST_OUT | NORTH_IN | EAST_IN },
/* IceWall_Southeast */
{ SOUTH_OUT | EAST_OUT | NORTH_IN | WEST_IN,
SOUTH_OUT | EAST_OUT | NORTH_IN | WEST_IN,
SOUTH_OUT | EAST_OUT | NORTH_IN | WEST_IN },
/* Gravel */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_OUT },
/* Dirt */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* Water */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* Fire */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* Bomb */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* Beartrap */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* Burglar */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* HintButton */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* Button_Blue */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* Button_Green */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* Button_Red */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* Button_Brown */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* Teleport */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* Wall */
{ ALL_OUT, ALL_OUT, ALL_OUT },
/* Wall_North */
{ NORTH_IN | WEST_IN | EAST_IN | WEST_OUT | SOUTH_OUT | EAST_OUT,
NORTH_IN | WEST_IN | EAST_IN | WEST_OUT | SOUTH_OUT | EAST_OUT,
NORTH_IN | WEST_IN | EAST_IN | WEST_OUT | SOUTH_OUT | EAST_OUT },
/* Wall_West */
{ NORTH_IN | WEST_IN | SOUTH_IN | NORTH_OUT | SOUTH_OUT | EAST_OUT,
NORTH_IN | WEST_IN | SOUTH_IN | NORTH_OUT | SOUTH_OUT | EAST_OUT,
NORTH_IN | WEST_IN | SOUTH_IN | NORTH_OUT | SOUTH_OUT | EAST_OUT },
/* Wall_South */
{ WEST_IN | SOUTH_IN | EAST_IN | NORTH_OUT | WEST_OUT | EAST_OUT,
WEST_IN | SOUTH_IN | EAST_IN | NORTH_OUT | WEST_OUT | EAST_OUT,
WEST_IN | SOUTH_IN | EAST_IN | NORTH_OUT | WEST_OUT | EAST_OUT },
/* Wall_East */
{ NORTH_IN | SOUTH_IN | EAST_IN | NORTH_OUT | WEST_OUT | SOUTH_OUT,
NORTH_IN | SOUTH_IN | EAST_IN | NORTH_OUT | WEST_OUT | SOUTH_OUT,
NORTH_IN | SOUTH_IN | EAST_IN | NORTH_OUT | WEST_OUT | SOUTH_OUT },
/* Wall_Southeast */
{ SOUTH_IN | EAST_IN | NORTH_OUT | WEST_OUT,
SOUTH_IN | EAST_IN | NORTH_OUT | WEST_OUT,
SOUTH_IN | EAST_IN | NORTH_OUT | WEST_OUT },
/* HiddenWall_Perm */
{ ALL_OUT, ALL_OUT, ALL_OUT },
/* HiddenWall_Temp */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* BlueWall_Real */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* BlueWall_Fake */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* SwitchWall_Open */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* SwitchWall_Closed */
{ ALL_OUT, ALL_OUT, ALL_OUT },
/* PopupWall */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* CloneMachine */
{ ALL_OUT, ALL_OUT, ALL_OUT },
/* Door_Red */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* Door_Blue */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* Door_Yellow */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* Door_Green */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* Socket */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* Exit */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* ICChip */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* Key_Red */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* Key_Blue */
{ ALL_IN_OUT, ALL_IN_OUT, ALL_IN_OUT },
/* Key_Yellow */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* Key_Green */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* Boots_Slide */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* Boots_Ice */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* Boots_Water */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* Boots_Fire */
{ ALL_IN_OUT, ALL_OUT, ALL_OUT },
/* Block_Static */
{ 0, 0, 0 },
/* Drowned_Chip */
{ 0, 0, 0 },
/* Burned_Chip */
{ 0, 0, 0 },
/* Bombed_Chip */
{ 0, 0, 0 },
/* Exited_Chip */
{ 0, 0, 0 },
/* Exit_Extra_1 */
{ 0, 0, 0 },
/* Exit_Extra_2 */
{ 0, 0, 0 },
/* Overlay_Buffer */
{ 0, 0, 0 },
/* Floor_Reserved2 */
{ 0, 0, 0 },
/* Floor_Reserved1 */
{ 0, 0, 0 }
};
/* Including the flag CMM_RELEASING in a call to canmakemove()
* indicates that the creature in question is being moved out of a
* beartrap or clone machine, moves that would normally be forbidden.
* CMM_CLEARANIMATIONS causes animations in the destination square to
* be immediately quelled. CMM_STARTMOVEMENT indicates that this is
* the final check before movement begins, thus triggering side
* effects such as exposing hidden walls. CMM_PUSHBLOCKS causes blocks
* to be pushed when in the way of Chip. CMM_PUSHBLOCKSNOW causes
* blocks to be pushed immediately, instead of waiting for the block's
* turn to move.
*/
#define CMM_RELEASING 0x0001
#define CMM_CLEARANIMATIONS 0x0002
#define CMM_STARTMOVEMENT 0x0004
#define CMM_PUSHBLOCKS 0x0008
#define CMM_PUSHBLOCKSNOW 0x0010
/* Return TRUE if the given block is allowed to be moved in the given
* direction. If flags includes CMM_PUSHBLOCKSNOW, then the indicated
* movement of the block will be initiated.
*/
static int canpushblock(creature *block, int dir, int flags)
{
_assert(block && block->id == Block);
_assert(floorat(block->pos) != CloneMachine);
_assert(dir != NIL);
if (!canmakemove(block, dir, flags)) {
if (!block->moving && (flags & (CMM_PUSHBLOCKS | CMM_PUSHBLOCKSNOW))) {
block->dir = dir;
block->tdir = dir;
}
return FALSE;
}
if (flags & (CMM_PUSHBLOCKS | CMM_PUSHBLOCKSNOW)) {
block->dir = dir;
block->tdir = dir;
block->state |= CS_PUSHED;
if (flags & CMM_PUSHBLOCKSNOW)
advancecreature(block, FALSE);
}
return TRUE;
}
/* Return TRUE if the given creature is allowed to attempt to move in
* the given direction. Side effects can and will occur from calling
* this function, as indicated by flags.
*/
static int canmakemove(creature const *cr, int dir, int flags)
{
creature *other;
int floor;
int to, y, x;
_assert(cr);
_assert(dir != NIL);
floor = floorat(cr->pos);
switch (floor) {
case Wall_North: if (dir & NORTH) return FALSE; break;
case Wall_West: if (dir & WEST) return FALSE; break;
case Wall_South: if (dir & SOUTH) return FALSE; break;
case Wall_East: if (dir & EAST) return FALSE; break;
case Wall_Southeast: if (dir & (SOUTH | EAST)) return FALSE; break;
case IceWall_Northwest: if (dir & (SOUTH | EAST)) return FALSE; break;
case IceWall_Northeast: if (dir & (SOUTH | WEST)) return FALSE; break;
case IceWall_Southwest: if (dir & (NORTH | EAST)) return FALSE; break;
case IceWall_Southeast: if (dir & (NORTH | WEST)) return FALSE; break;
case Beartrap:
case CloneMachine:
if (!(flags & CMM_RELEASING))
return FALSE;
break;
}
if (isslide(floor) && (cr->id != Chip || !possession(Boots_Slide))
&& getslidedir(floor, FALSE) == back(dir))
return FALSE;
y = cr->pos / CXGRID;
x = cr->pos % CXGRID;
y += dir == NORTH ? -1 : dir == SOUTH ? +1 : 0;
x += dir == WEST ? -1 : dir == EAST ? +1 : 0;
to = y * CXGRID + x;
if (x < 0 || x >= CXGRID)
return FALSE;
if (y < 0 || y >= CYGRID) {
if (pedanticmode) {
if (flags & CMM_STARTMOVEMENT) {
mapbreached() = TRUE;
warn("map breach in pedantic mode at (%d %d)", x, y);
}
}
return FALSE;
}
floor = floorat(to);
if (floor == SwitchWall_Open || floor == SwitchWall_Closed)
floor ^= togglestate();
if (cr->id == Chip) {
if (!(movelaws[floor].chip & dir))
return FALSE;
if (floor == Socket && chipsneeded() > 0)
return FALSE;
if (isdoor(floor) && !possession(floor))
return FALSE;
if (ismarkedanimated(to))
return FALSE;
other = lookupcreature(to, FALSE);
if (other && other->id == Block) {
if (!canpushblock(other, dir, flags & ~CMM_RELEASING))
return FALSE;
}
if (floor == HiddenWall_Temp || floor == BlueWall_Real) {
if (flags & CMM_STARTMOVEMENT)
floorat(to) = Wall;
return FALSE;
}
} else if (cr->id == Block) {
if (cr->moving > 0)
return FALSE;
if (!(movelaws[floor].block & dir))
return FALSE;
if (islocationclaimed(to))
return FALSE;
if (flags & CMM_CLEARANIMATIONS)
if (ismarkedanimated(to))
stopanimationat(to);
} else {
if (!(movelaws[floor].creature & dir))
return FALSE;
if (islocationclaimed(to))
return FALSE;
if (floor == Fire && cr->id != Fireball)
return FALSE;
if (flags & CMM_CLEARANIMATIONS)
if (ismarkedanimated(to))
stopanimationat(to);
}
return TRUE;
}
/*
* How everyone selects their move.
*/
/* This function embodies the movement behavior of all the creatures.
* Given a creature, this function enumerates its desired direction
* of movement and selects the first one that is permitted.
*/
static void choosecreaturemove(creature *cr)
{
int choices[4] = { NIL, NIL, NIL, NIL };
int dir, pdir;
int floor;
int y, x, m, n;
if (isanimation(cr->id))
return;
cr->tdir = NIL;
if (cr->id == Block)
return;
if (getfdir(cr) != NIL)
return;
floor = floorat(cr->pos);
if (floor == CloneMachine || floor == Beartrap) {
cr->tdir = cr->dir;
return;
}
dir = cr->dir;
pdir = NIL;
_assert(dir != NIL);
switch (cr->id) {
case Tank:
choices[0] = dir;
break;
case Ball:
choices[0] = dir;
choices[1] = back(dir);
break;
case Glider:
choices[0] = dir;
choices[1] = left(dir);
choices[2] = right(dir);
choices[3] = back(dir);
break;
case Fireball:
choices[0] = dir;
choices[1] = right(dir);
choices[2] = left(dir);
choices[3] = back(dir);
break;
case Bug:
choices[0] = left(dir);
choices[1] = dir;
choices[2] = right(dir);
choices[3] = back(dir);
break;
case Paramecium:
choices[0] = right(dir);
choices[1] = dir;
choices[2] = left(dir);
choices[3] = back(dir);
break;
case Walker:
choices[0] = dir;
choices[1] = WALKER_TURN;
break;
case Blob:
choices[0] = BLOB_TURN;
break;
case Teeth:
if ((currenttime() + stepping()) & 4)
return;
y = chippos() / CXGRID - cr->pos / CXGRID;
x = chippos() % CXGRID - cr->pos % CXGRID;
n = y < 0 ? NORTH : y > 0 ? SOUTH : NIL;
if (y < 0)
y = -y;
m = x < 0 ? WEST : x > 0 ? EAST : NIL;
if (x < 0)
x = -x;
if (x > y) {
choices[0] = m;
choices[1] = n;
} else {
choices[0] = n;
choices[1] = m;
}
pdir = choices[0];
break;
}
for (n = 0 ; n < 4 && choices[n] != NIL ; ++n) {
if (choices[n] == WALKER_TURN) {
m = lynx_prng() & 3;
choices[n] = cr->dir;
while (m--)
choices[n] = right(choices[n]);
} else if (choices[n] == BLOB_TURN) {
int cw[4] = { NORTH, EAST, SOUTH, WEST };
choices[n] = cw[random4(mainprng())];
}
cr->tdir = choices[n];
if (canmakemove(cr, choices[n], CMM_CLEARANIMATIONS))
return;
}
if (pdir != NIL)
cr->tdir = pdir;
}
/* Determine the direction of Chip's next move. If discard is TRUE,
* then Chip is not currently permitted to select a direction of
* movement, and the player's input should not be retained.
*/
static void choosechipmove(creature *cr, int discard)
{
int dir;
int f1, f2;
chippushing() = FALSE;
dir = currentinput();
currentinput() = NIL;
if (!directionalcmd(dir))
dir = NIL;
if (dir == NIL || discard || chipstuck()) {
cr->tdir = NIL;
return;
}
lastmove() = dir;
cr->tdir = dir;
if (cr->tdir != NIL)
dir = cr->tdir;
else if (getfdir(cr) != NIL)
dir = getfdir(cr);
else
return;
if (isdiagonal(dir)) {
if (cr->dir & dir) {
f1 = canmakemove(cr, cr->dir, CMM_PUSHBLOCKS);
f2 = canmakemove(cr, cr->dir ^ dir, CMM_PUSHBLOCKS);
dir = !f1 && f2 ? dir ^ cr->dir : cr->dir;
} else {
if (canmakemove(cr, dir & (EAST | WEST), CMM_PUSHBLOCKS))
dir &= EAST | WEST;
else
dir &= NORTH | SOUTH;
}
cr->tdir = dir;
} else {
(void)canmakemove(cr, dir, CMM_PUSHBLOCKS);
}
}
/* This function determines if the given creature is currently being
* forced to move. (Ice, slide floors, and teleports are the three
* possible causes of this. Bear traps and clone machines also cause
* forced movement, but these are handled outside of the normal
* movement sequence.) If so, the direction is stored in the
* creature's fdir field, and TRUE is returned unless the creature can
* override the forced move.
*/
static int getforcedmove(creature *cr)
{
int floor;
setfdir(cr, NIL);
floor = floorat(cr->pos);
if (currenttime() == 0)
return FALSE;
if (isice(floor)) {