-
Notifications
You must be signed in to change notification settings - Fork 1
/
mslogic.c
2270 lines (2041 loc) · 56.3 KB
/
mslogic.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
/* mslogic.c: The game logic for the MS ruleset.
*
* Copyright (C) 2001-2006 by Brian Raiter, 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"
#ifdef NDEBUG
#define _assert(test) ((void)0)
#else
#define _assert(test) ((test) || (die("internal error: failed sanity check" \
" (%s)\nPlease report this error to" \
" breadbox@muppetlabs.com", #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
};
/* Status information specific to the MS game logic.
*/
struct msstate {
unsigned char chipwait; /* ticks since Chip's last movement */
unsigned char chipstatus; /* Chip's status (one of CHIP_*) */
unsigned char controllerdir; /* current controller direction */
unsigned char lastslipdir; /* Chip's last involuntary movement */
unsigned char completed; /* level completed successfully */
short goalpos; /* mouse spot to move Chip towards */
signed char xviewoffset; /* offset of map view center */
signed char yviewoffset; /* position from position of Chip */
};
/* Forward declaration of a central function.
*/
static int advancecreature(creature *cr, int dir);
/* The most recently used stepping phase value.
*/
static int laststepping = 0;
/* 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 getchip() (creatures[0])
#define chippos() (getchip()->pos)
#define chipdir() (getchip()->dir)
#define chipsneeded() (state->chipsneeded)
#define clonerlist() (state->cloners)
#define clonerlistsize() (state->clonercount)
#define traplist() (state->traps)
#define traplistsize() (state->trapcount)
#define timelimit() (state->timelimit)
#define timeoffset() (state->timeoffset)
#define stepping() (state->stepping)
#define currenttime() (state->currenttime)
#define currentinput() (state->currentinput)
#define xviewpos() (state->xviewpos)
#define yviewpos() (state->yviewpos)
#define mainprng() (&state->mainprng)
#define lastmove() (state->lastmove)
#define addsoundeffect(sfx) (state->soundeffects |= 1 << (sfx))
#define cellat(pos) (&state->map[pos])
#define setnosaving() (state->statusflags |= SF_NOSAVING)
#define showhint() (state->statusflags |= SF_SHOWHINT)
#define hidehint() (state->statusflags &= ~SF_SHOWHINT)
#define getmsstate() ((struct msstate*)state->localstateinfo)
#define completed() (getmsstate()->completed)
#define chipstatus() (getmsstate()->chipstatus)
#define chipwait() (getmsstate()->chipwait)
#define controllerdir() (getmsstate()->controllerdir)
#define lastslipdir() (getmsstate()->lastslipdir)
#define xviewoffset() (getmsstate()->xviewoffset)
#define yviewoffset() (getmsstate()->yviewoffset)
#define goalpos() (getmsstate()->goalpos)
#define hasgoal() (goalpos() >= 0)
#define cancelgoal() (goalpos() = -1)
#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()", obj);
_assert(!"possession() called with an invalid object");
return NULL;
}
/*
* Memory allocation functions for the various arenas.
*/
/* The creature pool is a linked list of lumps, each lump holding this
* many creatures.
*/
#define crpoollumpsize 256
/* The data that makes up one lump of the creature pool.
*/
typedef struct crpoollump crpoollump;
struct crpoollump {
int count; /* number of unused creatures */
crpoollump *prev; /* the previously allocated lump */
crpoollump *next; /* the next lump after this one */
creature lump[crpoollumpsize]; /* the lump proper */
};
/* The data associated with a sliding object.
*/
typedef struct slipper {
creature *cr;
int dir;
} slipper;
/* The linked list of creature pools, forming the creature arena.
*/
static crpoollump *currentcrpoollump = NULL;
/* The list of active creatures.
*/
static creature **creatures = NULL;
static int creaturecount = 0;
static int creaturesallocated = 0;
/* The list of "active" blocks.
*/
static creature **blocks = NULL;
static int blockcount = 0;
static int blocksallocated = 0;
/* The list of sliding creatures.
*/
static slipper *slips = NULL;
static int slipcount = 0;
static int slipsallocated = 0;
/* Mark all entries in the creature arena as unused.
*/
static void resetcreaturepool(void)
{
if (currentcrpoollump)
while (currentcrpoollump->prev)
currentcrpoollump = currentcrpoollump->prev;
}
/* Destroy the creature arena.
*/
static void freecreaturepool(void)
{
crpoollump *next;
resetcreaturepool();
while (currentcrpoollump) {
next = currentcrpoollump->next;
free(currentcrpoollump);
currentcrpoollump = next;
}
}
/* Return a pointer to a fresh creature.
*/
static creature *allocatecreature(void)
{
crpoollump *next;
creature *cr;
if (!currentcrpoollump || currentcrpoollump->count == 0) {
if (currentcrpoollump && currentcrpoollump->next) {
currentcrpoollump = currentcrpoollump->next;
currentcrpoollump->count = crpoollumpsize;
} else {
next = malloc(sizeof *next);
if (!next)
memerrexit();
next->count = crpoollumpsize;
next->prev = currentcrpoollump;
next->next = NULL;
if (currentcrpoollump)
currentcrpoollump->next = next;
currentcrpoollump = next;
}
}
--currentcrpoollump->count;
cr = currentcrpoollump->lump + currentcrpoollump->count;
cr->id = Nothing;
cr->pos = -1;
cr->dir = NIL;
cr->tdir = NIL;
cr->state = 0;
cr->frame = 0;
cr->hidden = FALSE;
cr->moving = 0;
return cr;
}
/* Empty the list of active creatures.
*/
static void resetcreaturelist(void)
{
creaturecount = 0;
}
/* Append the given creature to the end of the creature list.
*/
static creature *addtocreaturelist(creature *cr)
{
if (creaturecount >= creaturesallocated) {
creaturesallocated = creaturesallocated ? creaturesallocated * 2 : 16;
creatures = realloc(creatures, creaturesallocated * sizeof *creatures);
if (!creatures)
memerrexit();
}
creatures[creaturecount++] = cr;
return cr;
}
/* Empty the list of "active" blocks.
*/
static void resetblocklist(void)
{
blockcount = 0;
}
/* Append the given block to the end of the block list.
*/
static creature *addtoblocklist(creature *cr)
{
if (blockcount >= blocksallocated) {
blocksallocated = blocksallocated ? blocksallocated * 2 : 16;
blocks = realloc(blocks, blocksallocated * sizeof *blocks);
if (!blocks)
memerrexit();
}
blocks[blockcount++] = cr;
return cr;
}
/* Empty the list of sliding creatures.
*/
static void resetsliplist(void)
{
slipcount = 0;
}
/* Append the given creature to the end of the slip list.
*/
static creature *appendtosliplist(creature *cr, int dir)
{
int n;
for (n = 0 ; n < slipcount ; ++n) {
if (slips[n].cr == cr) {
slips[n].dir = dir;
return cr;
}
}
if (slipcount >= slipsallocated) {
slipsallocated = slipsallocated ? slipsallocated * 2 : 16;
slips = realloc(slips, slipsallocated * sizeof *slips);
if (!slips)
memerrexit();
}
slips[slipcount].cr = cr;
slips[slipcount].dir = dir;
++slipcount;
return cr;
}
/* Add the given creature to the start of the slip list.
*/
static creature *prependtosliplist(creature *cr, int dir)
{
int n;
if (slipcount && slips[0].cr == cr) {
slips[0].dir = dir;
return cr;
}
if (slipcount >= slipsallocated) {
slipsallocated = slipsallocated ? slipsallocated * 2 : 16;
slips = realloc(slips, slipsallocated * sizeof *slips);
if (!slips)
memerrexit();
}
for (n = slipcount ; n ; --n)
slips[n] = slips[n - 1];
++slipcount;
slips[0].cr = cr;
slips[0].dir = dir;
return cr;
}
/* Return the sliding direction of a creature on the slip list.
*/
static int getslipdir(creature *cr)
{
int n;
for (n = 0 ; n < slipcount ; ++n)
if (slips[n].cr == cr)
return slips[n].dir;
return NIL;
}
/* Remove the given creature from the slip list.
*/
static void removefromsliplist(creature *cr)
{
int n;
for (n = 0 ; n < slipcount ; ++n)
if (slips[n].cr == cr)
break;
if (n == slipcount)
return;
--slipcount;
for ( ; n < slipcount ; ++n)
slips[n] = slips[n + 1];
}
/*
* Simple floor functions.
*/
/* Floor state flags.
*/
#define FS_BUTTONDOWN 0x01 /* button press is deferred */
#define FS_CLONING 0x02 /* clone machine is activated */
#define FS_BROKEN 0x04 /* teleport/toggle wall doesn't work */
#define FS_HASMUTANT 0x08 /* beartrap contains mutant block */
#define FS_MARKER 0x10 /* marker used during initialization */
/* Translate a slide floor into the direction it points in. In the
* case of a random slide floor, a new direction is selected.
*/
static int getslidedir(int floor)
{
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: return 1 << random4(mainprng());
}
return NIL;
}
/* Alter a creature's direction if they are at an ice wall.
*/
static int icewallturn(int floor, int dir)
{
switch (floor) {
case IceWall_Northeast:
return dir == SOUTH ? EAST : dir == WEST ? NORTH : dir;
case IceWall_Southwest:
return dir == NORTH ? WEST : dir == EAST ? SOUTH : dir;
case IceWall_Northwest:
return dir == SOUTH ? WEST : dir == EAST ? NORTH : dir;
case IceWall_Southeast:
return dir == NORTH ? EAST : dir == WEST ? SOUTH : dir;
}
return dir;
}
/* Find the location of a bear trap from one of its buttons.
*/
static int trapfrombutton(int pos)
{
xyconn *traps;
int i;
traps = traplist();
for (i = traplistsize() ; i ; ++traps, --i)
if (traps->from == pos)
return traps->to;
return -1;
}
/* Find the location of a clone machine from one of its buttons.
*/
static int clonerfrombutton(int pos)
{
xyconn *cloners;
int i;
cloners = clonerlist();
for (i = clonerlistsize() ; i ; ++cloners, --i)
if (cloners->from == pos)
return cloners->to;
return -1;
}
/* Return the floor tile found at the given location.
*/
static int floorat(int pos)
{
mapcell *cell;
cell = cellat(pos);
if (!iskey(cell->top.id) && !isboots(cell->top.id)
&& !iscreature(cell->top.id))
return cell->top.id;
if (!iskey(cell->bot.id) && !isboots(cell->bot.id)
&& !iscreature(cell->bot.id))
return cell->bot.id;
return Empty;
}
/* Return a pointer to the tile that forms the floor at the given
* location.
*/
static maptile *getfloorat(int pos)
{
mapcell *cell;
cell = cellat(pos);
if (!iskey(cell->top.id) && !isboots(cell->top.id)
&& !iscreature(cell->top.id))
return &cell->top;
if (!iskey(cell->bot.id) && !isboots(cell->bot.id)
&& !iscreature(cell->bot.id))
return &cell->bot;
return &cell->bot; /* ? */
}
/* Return TRUE if the brown button at the give location is currently
* held down.
*/
static int istrapbuttondown(int pos)
{
return pos >= 0 && pos < CXGRID * CYGRID
&& cellat(pos)->top.id != Button_Brown;
}
/* Place a new tile at the given location, causing the current upper
* tile to become the lower tile.
*/
static void pushtile(int pos, maptile tile)
{
mapcell *cell;
cell = cellat(pos);
cell->bot = cell->top;
cell->top = tile;
}
/* Remove the upper tile from the given location, causing the current
* lower tile to become uppermost.
*/
static maptile poptile(int pos)
{
maptile tile;
mapcell *cell;
cell = cellat(pos);
tile = cell->top;
cell->top = cell->bot;
cell->bot.id = Empty;
cell->bot.state = 0;
return tile;
}
/* Return TRUE if a bear trap is currently passable.
*/
static int istrapopen(int pos, int skippos)
{
xyconn *traps;
int i;
traps = traplist();
for (i = traplistsize() ; i ; ++traps, --i)
if (traps->to == pos && traps->from != skippos
&& istrapbuttondown(traps->from))
return TRUE;
return FALSE;
}
/* Flip-flop the state of any toggle walls.
*/
static void togglewalls(void)
{
mapcell *cell;
int pos;
for (pos = 0 ; pos < CXGRID * CYGRID ; ++pos) {
cell = cellat(pos);
if ((cell->top.id == SwitchWall_Open
|| cell->top.id == SwitchWall_Closed)
&& !(cell->top.state & FS_BROKEN))
cell->top.id ^= SwitchWall_Open ^ SwitchWall_Closed;
if ((cell->bot.id == SwitchWall_Open
|| cell->bot.id == SwitchWall_Closed)
&& !(cell->bot.state & FS_BROKEN))
cell->bot.id ^= SwitchWall_Open ^ SwitchWall_Closed;
}
}
/*
* Functions that manage the list of entities.
*/
/* Creature state flags.
*/
#define CS_RELEASED 0x01 /* can leave a beartrap */
#define CS_CLONING 0x02 /* cannot move this tick */
#define CS_HASMOVED 0x04 /* already used current move */
#define CS_TURNING 0x08 /* is turning around */
#define CS_SLIP 0x10 /* is on the slip list */
#define CS_SLIDE 0x20 /* is on the slip list but can move */
#define CS_DEFERPUSH 0x40 /* button pushes will be delayed */
#define CS_MUTANT 0x80 /* block is mutant, looks like Chip */
/* Return the creature located at pos. Ignores Chip unless includechip
* is TRUE. Return NULL if no such creature is present.
*/
static creature *lookupcreature(int pos, int includechip)
{
int n;
if (!creatures)
return NULL;
for (n = 0 ; n < creaturecount ; ++n) {
if (creatures[n]->hidden)
continue;
if (creatures[n]->pos == pos)
if (creatures[n]->id != Chip || includechip)
return creatures[n];
}
return NULL;
}
/* Return the block located at pos. If the block in question is not
* currently "active", it is automatically added to the block list.
*/
static creature *lookupblock(int pos)
{
creature *cr;
int id, n;
if (blocks) {
for (n = 0 ; n < blockcount ; ++n)
if (blocks[n]->pos == pos && !blocks[n]->hidden)
return blocks[n];
}
cr = allocatecreature();
cr->id = Block;
cr->pos = pos;
id = cellat(pos)->top.id;
if (id == Block_Static)
cr->dir = NIL;
else if (creatureid(id) == Block)
cr->dir = creaturedirid(id);
else
_assert(!"lookupblock() called on blockless location");
return addtoblocklist(cr);
}
/* Update the given creature's tile on the map to reflect its current
* state.
*/
static void updatecreature(creature const *cr)
{
maptile *tile;
int id, dir;
if (cr->hidden)
return;
tile = &cellat(cr->pos)->top;
id = cr->id;
if (id == Block) {
tile->id = Block_Static;
if (cr->state & CS_MUTANT)
tile->id = crtile(Chip, NORTH);
return;
} else if (id == Chip) {
if (chipstatus()) {
switch (chipstatus()) {
case CHIP_BURNED: tile->id = Burned_Chip; return;
case CHIP_DROWNED: tile->id = Drowned_Chip; return;
}
} else if (cellat(cr->pos)->bot.id == Water) {
id = Swimming_Chip;
}
}
dir = cr->dir;
if (cr->state & CS_TURNING)
dir = right(dir);
tile->id = crtile(id, dir);
tile->state = 0;
}
/* Add the given creature's tile to the map.
*/
static void addcreaturetomap(creature const *cr)
{
static maptile const dummy = { Empty, 0 };
if (cr->hidden)
return;
pushtile(cr->pos, dummy);
updatecreature(cr);
}
/* Enervate an inert creature.
*/
static creature *awakencreature(int pos)
{
creature *new;
int tileid;
tileid = cellat(pos)->top.id;
if (!iscreature(tileid) || creatureid(tileid) == Chip)
return NULL;
new = allocatecreature();
new->id = creatureid(tileid);
new->dir = creaturedirid(tileid);
new->pos = pos;
return new->id == Block ? addtoblocklist(new) : addtocreaturelist(new);
}
/* Mark a creature as dead.
*/
static void removecreature(creature *cr)
{
cr->state &= ~(CS_SLIP | CS_SLIDE);
if (cr->id == Chip) {
if (chipstatus() == CHIP_OKAY)
chipstatus() = CHIP_NOTOKAY;
} else
cr->hidden = TRUE;
}
/* Turn around any and all tanks. (A tank that is halfway through the
* process of moving at the time is given special treatment.)
*/
static void turntanks(creature const *inmidmove)
{
int n;
for (n = 0 ; n < creaturecount ; ++n) {
if (creatures[n]->hidden || creatures[n]->id != Tank)
continue;
creatures[n]->dir = back(creatures[n]->dir);
if (!(creatures[n]->state & CS_TURNING))
creatures[n]->state |= CS_TURNING | CS_HASMOVED;
if (creatures[n] != inmidmove) {
if (creatureid(cellat(creatures[n]->pos)->top.id) == Tank) {
updatecreature(creatures[n]);
} else {
if (creatures[n]->state & CS_TURNING) {
creatures[n]->state &= ~CS_TURNING;
updatecreature(creatures[n]);
creatures[n]->state |= CS_TURNING;
}
creatures[n]->dir = back(creatures[n]->dir);
}
}
}
}
/*
* Maintaining the slip list.
*/
/* Add the given creature to the slip list if it is not already on it
* (assuming that the given floor is a kind that causes slipping).
*/
static void startfloormovement(creature *cr, int floor)
{
int dir;
cr->state &= ~(CS_SLIP | CS_SLIDE);
if (isice(floor))
dir = icewallturn(floor, cr->dir);
else if (isslide(floor))
dir = getslidedir(floor);
else if (floor == Teleport)
dir = cr->dir;
else if (floor == Beartrap && cr->id == Block)
dir = cr->dir;
else
return;
if (cr->id == Chip) {
cr->state |= isslide(floor) ? CS_SLIDE : CS_SLIP;
prependtosliplist(cr, dir);
cr->dir = dir;
updatecreature(cr);
} else {
cr->state |= CS_SLIP;
appendtosliplist(cr, dir);
}
}
/* Remove the given creature from the slip list.
*/
static void endfloormovement(creature *cr)
{
cr->state &= ~(CS_SLIP | CS_SLIDE);
removefromsliplist(cr);
}
/* Clean out deadwood entries in the slip list.
*/
static void updatesliplist()
{
int n;
for (n = slipcount - 1 ; n >= 0 ; --n)
if (!(slips[n].cr->state & (CS_SLIP | CS_SLIDE)))
endfloormovement(slips[n].cr);
}
/*
* 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. The
* following lookup table encapsulates these rules. Note that these
* rules are only the first check; a creature may be occasionally
* permitted a particular type of move but still prevented in a
* specific situation.
*/
#define NWSE (NORTH | WEST | SOUTH | EAST)
static struct { unsigned char chip, block, creature; } const movelaws[] = {
/* Nothing */ { 0, 0, 0 },
/* Empty */ { NWSE, NWSE, NWSE },
/* Slide_North */ { NWSE, NWSE, NWSE },
/* Slide_West */ { NWSE, NWSE, NWSE },
/* Slide_South */ { NWSE, NWSE, NWSE },
/* Slide_East */ { NWSE, NWSE, NWSE },
/* Slide_Random */ { NWSE, NWSE, 0 },
/* Ice */ { NWSE, NWSE, NWSE },
/* IceWall_Northwest */ { SOUTH | EAST, SOUTH | EAST, SOUTH | EAST },
/* IceWall_Northeast */ { SOUTH | WEST, SOUTH | WEST, SOUTH | WEST },
/* IceWall_Southwest */ { NORTH | EAST, NORTH | EAST, NORTH | EAST },
/* IceWall_Southeast */ { NORTH | WEST, NORTH | WEST, NORTH | WEST },
/* Gravel */ { NWSE, NWSE, 0 },
/* Dirt */ { NWSE, 0, 0 },
/* Water */ { NWSE, NWSE, NWSE },
/* Fire */ { NWSE, NWSE, NWSE },
/* Bomb */ { NWSE, NWSE, NWSE },
/* Beartrap */ { NWSE, NWSE, NWSE },
/* Burglar */ { NWSE, 0, 0 },
/* HintButton */ { NWSE, NWSE, NWSE },
/* Button_Blue */ { NWSE, NWSE, NWSE },
/* Button_Green */ { NWSE, NWSE, NWSE },
/* Button_Red */ { NWSE, NWSE, NWSE },
/* Button_Brown */ { NWSE, NWSE, NWSE },
/* Teleport */ { NWSE, NWSE, NWSE },
/* Wall */ { 0, 0, 0 },
/* Wall_North */ { NORTH | WEST | EAST,
NORTH | WEST | EAST,
NORTH | WEST | EAST },
/* Wall_West */ { NORTH | WEST | SOUTH,
NORTH | WEST | SOUTH,
NORTH | WEST | SOUTH },
/* Wall_South */ { WEST | SOUTH | EAST,
WEST | SOUTH | EAST,
WEST | SOUTH | EAST },
/* Wall_East */ { NORTH | SOUTH | EAST,
NORTH | SOUTH | EAST,
NORTH | SOUTH | EAST },
/* Wall_Southeast */ { SOUTH | EAST, SOUTH | EAST, SOUTH | EAST },
/* HiddenWall_Perm */ { 0, 0, 0 },
/* HiddenWall_Temp */ { NWSE, 0, 0 },
/* BlueWall_Real */ { NWSE, 0, 0 },
/* BlueWall_Fake */ { NWSE, 0, 0 },
/* SwitchWall_Open */ { NWSE, NWSE, NWSE },
/* SwitchWall_Closed */ { 0, 0, 0 },
/* PopupWall */ { NWSE, 0, 0 },
/* CloneMachine */ { 0, 0, 0 },
/* Door_Red */ { NWSE, 0, 0 },
/* Door_Blue */ { NWSE, 0, 0 },
/* Door_Yellow */ { NWSE, 0, 0 },
/* Door_Green */ { NWSE, 0, 0 },
/* Socket */ { NWSE, 0, 0 },
/* Exit */ { NWSE, NWSE, 0 },
/* ICChip */ { NWSE, 0, 0 },
/* Key_Red */ { NWSE, NWSE, NWSE },
/* Key_Blue */ { NWSE, NWSE, NWSE },
/* Key_Yellow */ { NWSE, NWSE, NWSE },
/* Key_Green */ { NWSE, NWSE, NWSE },
/* Boots_Ice */ { NWSE, NWSE, 0 },
/* Boots_Slide */ { NWSE, NWSE, 0 },
/* Boots_Fire */ { NWSE, NWSE, 0 },
/* Boots_Water */ { NWSE, NWSE, 0 },
/* Block_Static */ { NWSE, 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_NOLEAVECHECK in a call to canmakemove()
* indicates that the tile the creature is moving out of is
* automatically presumed to permit such movement. CMM_NOEXPOSEWALLS
* causes blue and hidden walls to remain unexposed.
* CMM_CLONECANTBLOCK means that the creature will not be prevented
* from moving by an identical creature standing in the way.
* CMM_NOPUSHING prevents Chip from pushing blocks inside this
* function. CMM_TELEPORTPUSH indicates to the block-pushing logic
* that Chip is teleporting. This prevents a stack of two blocks from
* being treated as a single block, and allows Chip to push a slipping
* block away from him. CMM_NOFIRECHECK causes bugs and walkers to not
* avoid fire. Finally, CMM_NODEFERBUTTONS causes buttons pressed by
* pushed blocks to take effect immediately.
*/
#define CMM_NOLEAVECHECK 0x0001
#define CMM_NOEXPOSEWALLS 0x0002
#define CMM_CLONECANTBLOCK 0x0004
#define CMM_NOPUSHING 0x0008
#define CMM_TELEPORTPUSH 0x0010
#define CMM_NOFIRECHECK 0x0020
#define CMM_NODEFERBUTTONS 0x0040
/* Move a block at the given position forward in the given direction.
* FALSE is returned if the block cannot be pushed.
*/
static int pushblock(int pos, int dir, int flags)
{
creature *cr;
int slipdir, r;
_assert(cellat(pos)->top.id == Block_Static);
_assert(dir != NIL);
cr = lookupblock(pos);
if (!cr) {
warn("%d: attempt to push disembodied block!", currenttime());
return FALSE;
}
if (cr->state & (CS_SLIP | CS_SLIDE)) {
slipdir = getslipdir(cr);
if (dir == slipdir || dir == back(slipdir))
if (!(flags & CMM_TELEPORTPUSH))
return FALSE;
}
if (!(flags & CMM_TELEPORTPUSH) && cellat(pos)->bot.id == Block_Static)
cellat(pos)->bot.id = Empty;
if (!(flags & CMM_NODEFERBUTTONS))
cr->state |= CS_DEFERPUSH;
r = advancecreature(cr, dir);
if (!(flags & CMM_NODEFERBUTTONS))
cr->state &= ~CS_DEFERPUSH;
if (!r)
cr->state &= ~(CS_SLIP | CS_SLIDE);
return r;
}
/* 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)
{
int to;
int floor;
int id, y, x;
_assert(cr);
_assert(dir != NIL);
y = cr->pos / CXGRID;
x = cr->pos % CXGRID;
y += dir == NORTH ? -1 : dir == SOUTH ? +1 : 0;
x += dir == WEST ? -1 : dir == EAST ? +1 : 0;
if (y < 0 || y >= CYGRID || x < 0 || x >= CXGRID)
return FALSE;
to = y * CXGRID + x;
if (!(flags & CMM_NOLEAVECHECK)) {
switch (cellat(cr->pos)->bot.id) {
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 Beartrap:
if (!(cr->state & CS_RELEASED))
return FALSE;
break;
}
}
if (cr->id == Chip) {
floor = floorat(to);
if (!(movelaws[floor].chip & dir))
return FALSE;
if (floor == Socket && chipsneeded() > 0)
return FALSE;
if (isdoor(floor) && !possession(floor))
return FALSE;
if (iscreature(cellat(to)->top.id)) {
id = creatureid(cellat(to)->top.id);
if (id == Chip || id == Swimming_Chip || id == Block)
return FALSE;
}
if (floor == HiddenWall_Temp || floor == BlueWall_Real) {
if (!(flags & CMM_NOEXPOSEWALLS))
getfloorat(to)->id = Wall;
return FALSE;
}
if (floor == Block_Static) {
if (!pushblock(to, dir, flags))
return FALSE;
else if (flags & CMM_NOPUSHING)
return FALSE;
if ((flags & CMM_TELEPORTPUSH) && floorat(to) == Block_Static
&& cellat(to)->bot.id == Empty)
return TRUE;
return canmakemove(cr, dir, flags | CMM_NOPUSHING);
}
} else if (cr->id == Block) {
floor = cellat(to)->top.id;
if (iscreature(floor)) {
id = creatureid(floor);
return id == Chip || id == Swimming_Chip;
}
if (!(movelaws[floor].block & dir))
return FALSE;
} else {
floor = cellat(to)->top.id;
if (iscreature(floor)) {
id = creatureid(floor);
if (id == Chip || id == Swimming_Chip) {
floor = cellat(to)->bot.id;
if (iscreature(floor)) {
id = creatureid(floor);
return id == Chip || id == Swimming_Chip;