forked from zenorogue/hyperrogue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
7065 lines (6160 loc) · 219 KB
/
game.cpp
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
// Hyperbolic Rogue
// main game routines: movement etc.
// Copyright (C) 2011-2016 Zeno Rogue, see 'hyper.cpp' for details
int lastsafety;
int mutantphase;
int turncount;
int rosewave, rosephase;
int avengers, mirrorspirits;
cell *lastmove;
enum eLastmovetype {lmSkip, lmMove, lmAttack, lmSpecial, lmPush, lmTree};
eLastmovetype lastmovetype;
int sval = 100000000;
bool hauntedWarning;
bool survivalist;
bool hardcore = false;
int hardcoreAt;
flagtype havewhat, hadwhat;
#define HF_BUG (1<<0)
#define HF_EARTH (1<<1)
#define HF_BIRD (1<<2)
#define HF_LEADER (1<<3)
#define HF_HEX (1<<4)
#define HF_WHIRLPOOL (1<<5)
#define HF_WATER (1<<6)
#define HF_AIR (1<<7)
#define HF_MUTANT (1<<8)
#define HF_OUTLAW (1<<9)
#define HF_WHIRLWIND (1<<10)
#define HF_ROSE (1<<11)
#define HF_DRAGON (1<<12)
#define HF_KRAKEN (1<<13)
#define HF_SHARK (1<<14)
#define HF_BATS (1<<15)
#define HF_REPTILE (1<<16)
#define HF_EAGLES (1<<17)
#define HF_SLOW (1<<18)
#define HF_FAST (1<<19)
#define HF_WARP (1<<20)
#define HF_MOUSE (1<<21)
#define HF_RIVER (1<<22)
#define HF_MIRROR (1<<23)
bool seenSevenMines = false;
bool pureHardcore() { return hardcore && hardcoreAt < PUREHARDCORE_LEVEL; }
bool canmove = true;
int sagephase = 0;
// number of Grails collected, to show you as a knight
int knighted = 0;
bool usedSafety = false;
eLand safetyland;
int safetyseed;
int showid = 0;
bool invismove = false, invisfish = false; // last move was invisible [due to Fish]
int noiseuntil; // noise until the given turn
void createNoise(int t) {
noiseuntil = max(noiseuntil, turncount+t);
invismove = false;
if(shmup::on) shmup::visibleFor(100 * t);
}
int currentLocalTreasure;
bool landvisited[landtypes];
extern void DEBT(const char *buf);
#define DEBT(x) DEBB(DF_TURN, (debugfile,"%s\n", x))
bool eq(short a, short b) { return a==b; }
// game state
int items[ittypes], kills[motypes], explore[10], exploreland[10][landtypes], landcount[landtypes];
map<modecode_t, array<int, ittypes> > hiitems;
bool orbused[ittypes], lastorbused[ittypes];
bool playermoved = true; // center on the PC?
bool flipplayer = true; // flip the player image after move, do not flip after attack
int cheater = 0; // did the player cheat?
int anthraxBonus = 0; // for using Safety in tactical Camelot
vector<cell*> dcal; // queue for cpdist
vector<cell*> pathq; // queue for pathdist
vector<cell*> offscreen; // offscreen cells to take care off
vector<cell*> pathqm; // list of monsters to move (pathq restriced to monsters)
vector<cell*> targets; // list of monster targets
// monsters of specific types to move
vector<cell*> worms, ivies, ghosts, golems, hexsnakes;
vector<cell*> temps; // temporary changes during bfs
vector<eMonster> tempval; // restore temps
// a bit nicer way of DFS
vector<int> reachedfrom;
// additional direction information for BFS algorithms
// it remembers from where we have got to this location
// the opposite cell will be added to the queue first,
// which helps the AI
vector<cell*> movesofgood[8];
int first7; // the position of the first monster at distance 7 in dcal
cellwalker cwt; // single player character position
inline cell*& singlepos() { return cwt.c; }
inline bool singleused() { return !(shmup::on || multi::players > 1); }
#include <random>
mt19937 r;
void shrand(int i) {
r.seed(i);
}
int hrandpos() { return r() & HRANDMAX; }
int hrand(int i) {
return r() % i;
}
void initcell(cell *c) {
c->mpdist = INFD; // minimum distance from the player, ever
c->cpdist = INFD; // current distance from the player
c->pathdist = PINFD;// current distance from the player, along paths (used by yetis)
c->landparam = 0; c->landflags = 0; c->wparam = 0;
c->aitmp = 0;
c->wall = waNone;
c->item = itNone;
c->monst = moNone;
c->bardir = NODIR;
c->mondir = NODIR;
c->barleft = c->barright = laNone;
c->land = laNone;
c->ligon = 0;
c->stuntime = 0;
}
bool doesnotFall(cell *c) {
if(c->wall == waChasm) return false;
else if(cellUnstable(c)) {
fallingFloorAnimation(c);
c->wall = waChasm;
return false;
}
return true;
}
bool doesFall(cell *c) { return !doesnotFall(c); }
bool doesFallSound(cell *c) {
if(c->land != laMotion && c->land != laZebra)
playSound(c, "trapdoor");
return !doesnotFall(c);
}
bool itemHidden(cell *c) {
return isWatery(c) && !(shmup::on && shmup::boatAt(c));
}
bool playerInWater() {
for(int i=0; i<numplayers(); i++)
if(multi::playerActive(i) && isWatery(playerpos(i)) && !playerInBoat(i))
return true;
return false;
}
bool itemHiddenFromSight(cell *c) {
return isWatery(c) && !items[itOrbInvis] && !(items[itOrbFish] && playerInWater())
&& !(shmup::on && shmup::boatAt(c));
}
int numplayers() {
return multi::players;
}
cell *playerpos(int i) {
if(shmup::on) return shmup::playerpos(i);
if(multi::players > 1) return multi::player[i].c;
return singlepos();
}
bool allPlayersInBoats() {
for(int i=0; i<numplayers(); i++)
if(multi::playerActive(i) && playerpos(i)->wall != waBoat) return true;
return false;
}
int whichPlayerOn(cell *c) {
if(singleused()) return c == singlepos() ? 0 : -1;
for(int i=0; i<numplayers(); i++)
if(playerpos(i) == c) return i;
return -1;
}
bool isPlayerOn(cell *c) {
return whichPlayerOn(c) >= 0;
}
bool isPlayerInBoatOn(cell *c, int i) {
return
(playerpos(i) == c && (
c->wall == waBoat || c->wall == waStrandedBoat || (shmup::on && shmup::playerInBoat(i))
));
}
bool playerInBoat(int i) {
return isPlayerInBoatOn(playerpos(i), i);
}
bool isPlayerInBoatOn(cell *c) {
for(int i=0; i<numplayers(); i++) if(isPlayerInBoatOn(c, i)) return true;
return false;
}
void destroyBoats(cell *c, cell *c2, bool strandedToo) {
if(c->wall == waBoat) placeWater(c, c2);
if(strandedToo && c->wall == waStrandedBoat) c->wall = waNone;
shmup::destroyBoats(c);
}
bool playerInPower() {
if(singleused())
return singlepos()->land == laPower || singlepos()->land == laHalloween;
for(int i=0; i<numplayers(); i++)
if(multi::playerActive(i) && (playerpos(i)->land == laPower || playerpos(i)->land == laHalloween))
return true;
return false;
}
eItem localTreasureType() {
lastland = singlepos()->land;
return treasureType(lastland);
}
void countLocalTreasure() {
eItem i = localTreasureType();
currentLocalTreasure = i ? items[i] : 0;
if(i != itHyperstone) for(int i=0; i<size(dcal); i++) {
cell *c2 = dcal[i];
if(c2->cpdist > 3) break;
eItem i2 = treasureType(c2->land);
if(i2 && items[i2] < currentLocalTreasure)
currentLocalTreasure = items[i2];
}
}
int gold(int no) {
int i = 0;
if(!(no & NO_YENDOR)) i += items[itOrbYendor] * 50;
if(!(no & NO_GRAIL)) i += items[itHolyGrail] * 10;
if(!(no & NO_LOVE)) {
bool love = items[itOrbLove];
#if CAP_INV
if(inv::on && inv::remaining[itOrbLove])
love = true;
#endif
if(love) i += 30;
}
if(!(no & NO_TREASURE))
for(int t=0; t<ittypes; t++)
if(itemclass(eItem(t)) == IC_TREASURE)
i += items[t];
return i;
}
int maxgold() {
int mg = 0;
for(int i=0; i<ittypes; i++)
if(itemclass(eItem(i)) == IC_TREASURE && items[i] > mg)
mg = items[i];
return mg;
}
int* killtable[] = {
&kills[moYeti], &kills[moWolf], &
kills[moRanger], &kills[moTroll], &kills[moGoblin], &
kills[moWorm], &kills[moDesertman], &kills[moIvyRoot], &
kills[moMonkey], &kills[moEagle], &kills[moSlime], &kills[moSeep], &
kills[moRunDog], &
kills[moCultist], &kills[moTentacle], &kills[moPyroCultist], &
kills[moLesser], &kills[moGreater], &
kills[moZombie], &kills[moGhost], &kills[moNecromancer], &
kills[moHedge], &kills[moFireFairy], &
kills[moCrystalSage], &kills[moShark], &kills[moGreaterShark], &
kills[moMiner], &kills[moFlailer], &kills[moLancer], &
kills[moVineSpirit], &kills[moVineBeast], &
kills[moBug0], &kills[moBug1], &kills[moBug2], &
kills[moDarkTroll], &kills[moEarthElemental], &
kills[moWitch], &kills[moEvilGolem], &kills[moWitchFlash], &kills[moWitchFire], &
kills[moWitchWinter], &kills[moWitchSpeed], &
kills[moCultistLeader], &
kills[moPirate], &kills[moCShark], &kills[moParrot], &
kills[moHexSnake], &kills[moRedTroll], &
kills[moPalace], &kills[moSkeleton], &kills[moFatGuard], &kills[moVizier], &
kills[moViking], &kills[moFjordTroll], &kills[moWaterElemental], &
kills[moAlbatross], &kills[moBomberbird], &
kills[moAirElemental], &kills[moFireElemental], &
kills[moGargoyle], &kills[moFamiliar], &kills[moOrangeDog], &
items[itMutant], &kills[moMetalBeast], &kills[moMetalBeast2], &
kills[moOutlaw], &kills[moForestTroll], &kills[moStormTroll], &
kills[moRedFox], &kills[moWindCrow], &
kills[moFalsePrincess], &kills[moRoseLady], &
kills[moRoseBeauty], &
kills[moRatling], &kills[moRatlingAvenger], &
kills[moDragonHead], &
kills[moGadfly], &kills[moSparrowhawk], &kills[moResearcher],
&kills[moKrakenH], &kills[moDraugr],
&kills[moBat], &kills[moReptile],
&kills[moHerdBull], &kills[moSleepBull], &kills[moRagingBull],
&kills[moGadfly], &kills[moButterfly],
&kills[moNarciss], &kills[moMirrorSpirit],
NULL
};
int tkills() {
int res = 0;
for(int i=0; killtable[i]; i++) res += killtable[i][0];
return res;
}
int killtypes() {
int res = 0;
for(int i=0; killtable[i]; i++) if(killtable[i][0]) res++;
return res;
}
bool isWarped(cell *c) {
return isWarped(c->land) || (items[itOrb37] && c->cpdist <= 4);
}
// returns ishept in the normal tiling;
// in the 'pure heptagonal' tiling, returns true for a set of cells
// which roughly corresponds to the heptagons in the normal tiling
bool pseudohept(cell *c) {
if(purehepta) {
if(sphere)
return
c->master == getDodecahedron(3) ||
c->master == getDodecahedron(5) ||
c->master == getDodecahedron(6);
int z = zebra40(c);
return z == 5 || z == 8 || z == 15;
}
else return ishept(c);
}
bool nonAdjacent(cell *c, cell *c2) {
if(isWarped(c) && isWarped(c2) && !pseudohept(c) && !pseudohept(c2)) {
/* int i = neighborId(c, c2);
cell *c3 = c->mov[(i+1)%6], *c4 = c->mov[(i+5)%6];
if(c3 && !isTrihepta(c3)) return false;
if(c4 && !isTrihepta(c4)) return false; */
return true;
}
return false;
}
bool nonAdjacentPlayer(cell *c, cell *c2) {
return nonAdjacent(c, c2) && !markOrb(itOrb37);
}
bool thruVine(cell *c, cell *c2) {
return (cellHalfvine(c) && c2->wall == c->wall && c2 != c);
// ((c->wall == waFloorC || c->wall == waFloorD) && c2->wall == c->wall && !c2->item && !c->item);
}
// === MOVEMENT FUNCTIONS ===
// w = from->mov[d]
bool againstCurrent(cell *w, cell *from) {
if(from->land != laWhirlpool) return false;
if(againstWind(from, w)) return false; // wind is stronger than current
if(!euclid && (!from->master->alt || !w->master->alt)) return false;
int dfrom = celldistAlt(from);
int dw = celldistAlt(w);
if(dw < dfrom) return false;
if(dfrom < dw) return true;
for(int d=0; d<from->type; d++)
if(from->mov[d] == w) {
cell *c3 = from->mov[(d+from->type-1) % from->type];
if(!c3) return false;
return celldistAlt(c3) < dfrom;
}
return false;
}
bool boatGoesThrough(cell *c) {
if(isGravityLand(c->land)) return false;
return
(c->wall == waNone && c->land != laMotion && c->land != laZebra && c->land != laReptile) ||
isAlchAny(c) ||
c->wall == waCavefloor || c->wall == waFrozenLake || isReptile(c->wall) ||
c->wall == waDeadfloor || c->wall == waCIsland || c->wall == waCIsland2 ||
c->wall == waMineUnknown || c->wall == waMineMine || c->wall == waMineOpen ||
c->wall == waBonfireOff || c->wall == waFire || c->wall == waPartialFire;
}
void placeWater(cell *c, cell *c2) {
if(isWatery(c)) ;
else if(c2 && isAlchAny(c2))
c->wall = c2->wall;
else if(isIcyLand(c))
c->wall = waLake;
else
c->wall = waSea;
// destroy the ancient treasure!
if(c->item == itBarrow) c->item = itNone;
}
int incline(cell *cfrom, cell *cto) {
return snakelevel(cto) - snakelevel(cfrom);
}
#define F(x) checkflags(flags,x)
bool checkflags(flagtype flags, int x) {
if(flags & x) return true;
if(flags & P_ISPLAYER) {
if((x & P_WINTER) && markOrb(itOrbWinter)) return true;
if((x & P_IGNORE37) && markOrb(itOrb37)) return true;
if((x & P_FISH) && markOrb(itOrbFish)) return true;
if((x & P_MARKWATER) && markOrb(itOrbWater)) return true;
if((x & P_AETHER) && markOrb2(itOrbAether) && !(flags&P_NOAETHER)) return true;
}
if(flags & P_ISFRIEND) if(items[itOrbEmpathy])
if(checkflags(flags ^ P_ISPLAYER ^ P_ISFRIEND, x) && markOrb(itOrbEmpathy))
return true;
return false;
}
bool strictlyAgainstGravity(cell *w, cell *from, bool revdir, flagtype flags) {
return
cellEdgeUnstable(w, flags) && cellEdgeUnstable(from, flags) &&
!(shmup::on && from == w) && gravityLevel(w) != gravityLevel(from) + (revdir?1:-1);
}
bool passable(cell *w, cell *from, flagtype flags) {
bool revdir = (flags&P_REVDIR);
if(from && from != w && nonAdjacent(from, w) && !F(P_IGNORE37 | P_BULLET)) return false;
for(int i=0; i<numplayers(); i++) {
cell *pp = playerpos(i);
if(!pp) continue;
if(w == pp && F(P_ONPLAYER)) return true;
if(from == pp && F(P_ONPLAYER) && F(P_REVDIR)) return true;
if(from && !((flags & P_ISPLAYER) && getMount(i))) {
int i = revdir ? incline(w, from) : incline(from, w);
if(i < -1 && F(P_ROSE)) return false;
if((i > 1) && !F(P_JUMP1 | P_JUMP2 | P_BULLET | P_FLYING | P_BLOW | P_CLIMBUP | P_AETHER | P_REPTILE))
return false;
if((i < -2) && !F(P_DEADLY | P_JUMP1 | P_JUMP2 | P_BULLET | P_FLYING | P_BLOW | P_CLIMBDOWN | P_AETHER | P_REPTILE))
return false;
}
}
if(F(P_ROSE)) {
if(airdist(w) < 3) return false;
if(againstWind(w,from)) return false;
}
if(from && strictlyAgainstGravity(w, from, revdir, flags)
&& !F(P_GRAVITY | P_BLOW | P_JUMP1 | P_JUMP2 | P_FLYING | P_BULLET | P_AETHER)
) return false;
if(from && (revdir ? againstWind(from,w) : againstWind(w, from)) && !F(P_WIND | P_BLOW | P_JUMP1 | P_JUMP2 | P_BULLET | P_AETHER)) return false;
if(revdir && from && w->monst && passable(from, w, flags &~ (P_REVDIR|P_MONSTER)))
return true;
if(!shmup::on && sword::at(w, flags & P_ISPLAYER) && !F(P_DEADLY | P_BULLET | P_ROSE))
return false;
bool alch1 = w->wall == waFloorA && from && from->wall == waFloorB && !w->item && !from->item;
alch1 |= w->wall == waFloorB && from && from->wall == waFloorA && !w->item && !from->item;
if(alch1) {
bool alchok = F(P_JUMP1 | P_JUMP2 | P_FLYING | P_TELE | P_BLOW | P_AETHER | P_BULLET)
&& !F(P_ROSE);
if(!alchok) return false;
}
if(from && thruVine(from, w) && !F(P_AETHER)) return false;
if(w->monst == moMouse && F(P_JUMP1)) ;
else if(w->monst && isFriendly(w) && F(P_FRIENDSWAP)) ;
else if(w->monst && !F(P_MONSTER)) return false;
if(w->wall == waMirror || w->wall == waCloud)
return F(P_MIRROR | P_AETHER);
if(w->wall == waMirrorWall)
return F(P_MIRRORWALL);
if(F(P_BULLET)) {
if(isFire(w) || w->wall == waBonfireOff || cellHalfvine(w) ||
w->wall == waAncientGrave || w->wall == waFreshGrave || w->wall == waRoundTable)
return true;
}
if(F(P_LEADER)) {
if(from && from->wall == waBoat && isWateryOrBoat(w) && !againstCurrent(w, from))
return true;
if(from && isWatery(from) && w->wall == waBoat && F(P_CHAIN))
return true;
if(from && isWatery(from) && isWatery(w) && F(P_CHAIN) && !againstCurrent(w, from))
return true;
if(w->wall == waBigStatue && from && canPushStatueOn(from)) return true;
}
if(F(P_EARTHELEM)) {
// cannot go through Living Caves...
if(w->wall == waCavefloor) return false;
// but can dig through...
if(w->wall == waDeadwall || w->wall == waDune || w->wall == waStone)
return true;
// and can swim through...
if(w->wall == waSea && w->land == laLivefjord)
return true;
}
if(F(P_WATERELEM)) {
if(isWatery(w) || boatGoesThrough(w) ||
w->wall == waBoat ||
w->wall == waDeadTroll || w->wall == waDeadTroll2) return true;
return false;
}
if(isThorny(w->wall) && F(P_BLOW | P_DEADLY)) return true;
if(isFire(w)) {
if(!F(P_AETHER | P_WINTER | P_BLOW | P_JUMP1 | P_BULLET | P_DEADLY)) return false;
}
if(isWatery(w)) {
if(from && from->wall == waBoat && F(P_USEBOAT) &&
(!againstCurrent(w, from) || F(P_MARKWATER))) ;
else if(!F(P_AETHER | P_FISH | P_FLYING | P_BLOW | P_JUMP1 | P_BULLET | P_DEADLY | P_REPTILE)) return false;
}
if(isChasmy(w)) {
if(!F(P_AETHER | P_FLYING | P_BLOW | P_JUMP1 | P_BULLET | P_DEADLY | P_REPTILE)) return false;
}
if(w->wall == waRoundTable && from && from->wall != waRoundTable && (flags & P_ISPLAYER)) return true;
if(isNoFlight(w) && F(P_FLYING | P_BLOW | P_JUMP1)) return false;
if(isWall(w)) {
// a special case: empathic aethereal beings cannot go through Round Table
// (but truly aetheral beings can)
if(w->wall == waRoundTable) {
if(!(flags & P_AETHER)) return false;
}
else if(!F(P_AETHER)) return false;
}
return true;
}
int airdir;
vector<pair<cell*, int> > airmap;
int airdist(cell *c) {
if(!(havewhat & HF_AIR)) return 3;
vector<pair<cell*, int> >::iterator it =
lower_bound(airmap.begin(), airmap.end(), make_pair(c,0));
if(it != airmap.end() && it->first == c) return it->second;
return 3;
}
void calcAirdir(cell *c) {
if(!c || c->monst == moAirElemental || !passable(c, NULL, P_BLOW))
return;
for(int i=0; i<c->type; i++) {
cell *c2 = c->mov[i];
if(c2 && c2->monst == moAirElemental) {
airdir = c->spn(i) * S42 / c2->type;
return;
}
}
for(int i=0; i<c->type; i++) {
cell *c2 = c->mov[i];
if(!c2) continue;
if(!passable(c2, c, P_BLOW | P_MONSTER)) continue;
if(!passable(c, c2, P_BLOW | P_MONSTER)) continue;
for(int i=0; i<c2->type; i++) {
cell *c3 = c2->mov[i];
if(c3 && c3->monst == moAirElemental) {
airdir = c2->spn(i) * S42 / c3->type;
return;
}
}
}
return;
}
bool againstWind(cell *cto, cell *cfrom) {
if(!cfrom || !cto) return false;
if(airdist(cto) < airdist(cfrom)) return true;
whirlwind::calcdirs(cfrom);
int d = neighborId(cfrom, cto);
if(whirlwind::winddir(d) == -1) return true;
return false;
}
bool ghostmove(eMonster m, cell* to, cell* from) {
if(!isGhost(m) && nonAdjacent(to, from)) return false;
if(sword::at(to, 0)) return false;
if(!shmup::on && isPlayerOn(to)) return false;
if(to->monst && !(to->monst == moTentacletail && isGhost(m) && m != moFriendlyGhost)
&& !(to->monst == moTortoise && isGhost(m) && m != moFriendlyGhost))
return false;
if((m == moWitchGhost || m == moWitchWinter) && to->land != laPower)
return false;
if(isGhost(m))
for(int i=0; i<to->type; i++) {
if(inmirror(to->mov[i])) return false;
if(to->mov[i] && to->mov[i] != from && isGhost(to->mov[i]->monst) &&
(to->mov[i]->monst == moFriendlyGhost) == (m== moFriendlyGhost))
return false;
}
if(isGhost(m) || m == moWitchGhost) return true;
if(m == moGreaterShark) return isWatery(to);
if(m == moWitchWinter)
return passable(to, from, P_WINTER | P_ONPLAYER);
return false;
}
bool slimepassable(cell *w, cell *c) {
if(w == c || !c) return true;
int u = dirfromto(c, w);
if(nonAdjacent(w,c)) return false;
if(isPlayerOn(w)) return true;
int group = slimegroup(c);
if(!group) return false;
int ogroup = slimegroup(w);
if(!ogroup) return false;
bool hv = (group == ogroup);
if(sword::at(w, 0)) return false;
if(w->item) return false;
// only travel to halfvines correctly
if(cellHalfvine(c)) {
int i=0;
for(int t=0; t<c->type; t++) if(c->mov[t] && c->mov[t]->wall == c->wall) i=t;
int z = i-u; if(z<0) z=-z; z%=6;
if(z>1) return false;
hv=(group == ogroup);
}
// only travel from halfvines correctly
if(cellHalfvine(w)) {
int i=0;
for(int t=0; t<w->type; t++) if(w->mov[t] && w->mov[t]->wall == w->wall) i=t;
int z = i-c->spn(u); if(z<0) z=-z; z%=6;
if(z>1) return false;
hv=(group == ogroup);
}
if(!hv) return false;
return true;
}
bool sharkpassable(cell *w, cell *c) {
if(w == c || !c) return true;
if(nonAdjacent(w,c)) return false;
if(isPlayerOn(w)) return true;
if(!isWatery(w)) return false;
if(sword::at(w, 0)) return false;
// don't go against the current
if(isWateryOrBoat(w) && isWateryOrBoat(c))
return !againstCurrent(w, c);
return true;
}
bool canPushStatueOn(cell *c) {
return passable(c, NULL, P_MONSTER) && c->wall != waBoat && !snakelevel(c) &&
!isWorm(c->monst) && !isReptile(c->wall) && !peace::on;
}
void moveBoat(cell *to, cell *from) {
eWall x = to->wall; to->wall = from->wall; from->wall = x;
to->mondir = neighborId(to, from);
moveItem(from, to, false);
animateMovement(from, to, LAYER_BOAT);
}
void moveBoatIfUsingOne(cell *to, cell *from) {
if(from->wall == waBoat && isWatery(to)) moveBoat(to, from);
else if(from->wall == waBoat && boatGoesThrough(to) && markEmpathy(itOrbWater)) {
placeWater(to, from);
moveBoat(to, from);
}
}
bool passable_for(eMonster m, cell *w, cell *from, flagtype extra) {
if(w->monst && !(extra & P_MONSTER) && !isPlayerOn(w))
return false;
if(m == moWolf) {
return isIcyLand(w) && (isPlayerOn(w) || passable(w, from, extra));
}
if(normalMover(m) || isBug(m) || isDemon(m) || m == moHerdBull) {
if((isWitch(m) || m == moEvilGolem) && w->land != laPower && w->land != laHalloween)
return false;
return passable(w, from, extra);
}
if(isShark(m))
return sharkpassable(w, from);
if(isSlimeMover(m))
return slimepassable(w, from);
if(m == moKrakenH) {
if(extra & P_ONPLAYER) {
if(isPlayerOn(w)) return true;
}
if((extra & P_ONPLAYER) && isPlayerOn(w))
return true;
if(pseudohept(w) || pseudohept(from)) return false;
if(w->wall != waBoat && !slimepassable(w, from)) return false;
forCellEx(w2, w) if(w2->wall != waBoat && !passable(w2, w, P_FISH | P_MONSTER)) return false;
return true;
}
if(m == moEarthElemental)
return passable(w, from, extra | P_EARTHELEM);
if(m == moWaterElemental)
return passable(w, from, extra | P_WATERELEM);
if(m == moGreaterShark)
return isWatery(w) || w->wall == waBoat || w->wall == waFrozenLake;
if(isGhostMover(m) || m == moFriendlyGhost)
return ghostmove(m, w, from);
// for the purpose of Shmup this is correct
if(m == moTameBomberbird)
return passable(w, from, extra | P_FLYING | P_ISFRIEND);
if(m == moHexSnake)
return !pseudohept(w) && passable(w, from, extra|P_WIND|P_FISH);
if(isBird(m))
return passable(w, from, extra | P_FLYING);
if(m == moReptile)
return passable(w, from, extra | P_REPTILE);
if(isDragon(m))
return passable(w, from, extra | P_FLYING | P_WINTER);
if(m == moAirElemental)
return passable(w, from, extra | P_FLYING | P_WIND);
if(isLeader(m)) {
if(from && from->wall == waBoat && from->item == itCoral && !from->monst) return false; // don't move Corals!
return passable(w, from, extra | P_LEADER);
}
if(isPrincess(m))
return passable(w, from, extra | P_ISFRIEND | P_USEBOAT);
if(isGolemOrKnight(m))
return passable(w, from, extra | P_ISFRIEND);
if(isWorm(m))
return passable(w, from, extra) && !cellUnstable(w) && ((m != moWorm && m != moTentacle) || !cellEdgeUnstable(w));
return false;
}
eMonster movegroup(eMonster m) {
if(isWitch(m) || m == moEvilGolem) {
if(m == moWitchGhost) return moWitchGhost;
if(m == moWitchWinter) return moWitchWinter;
return moWitch;
}
if(normalMover(m)) return moYeti;
if(m == moShark || m == moCShark) return moShark;
if(isSlimeMover(m)) return moSlime;
if(m == moEarthElemental) return moEarthElemental;
if(isLeader(m)) return moPirate;
if(m == moButterfly) return moButterfly;
if(isAngryBird(m)) return moEagle;
if(isBird(m)) return moTameBomberbird;
if(m == moReptile) return moReptile;
if(m == moGhost) return moGhost;
if(m == moFriendlyGhost) return moFriendlyGhost;
if(m == moGreaterShark) return moGreaterShark;
if(m == moWolf) return moWolf;
if(isDemon(m)) return moLesser;
if(isDragon(m)) return moDragonHead;
if(isBug(m)) return m;
if(m == moWaterElemental) return moWaterElemental;
if(m == moAirElemental) return moAirElemental;
if(isBull(m)) return moRagingBull;
return moNone;
}
void useup(cell *c) {
c->wparam--;
if(c->wparam == 0) {
drawParticles(c, c->wall == waFire ? 0xC00000 : winf[c->wall].color, 10, 50);
if(c->wall == waTempFloor)
c->wall = waChasm;
else if(c->wall == waTempBridge || c->wall == waTempBridgeBlocked)
placeWater(c, c);
else
c->wall = c->land == laCaribbean ? waCIsland2 : waNone;
}
}
int realstuntime(cell *c) {
if(isMutantIvy(c)) return (c->stuntime - mutantphase) & 15;
return c->stuntime;
}
bool childbug = false;
// is w killed if killed is killed?
bool isChild(cell *w, cell *killed) {
if(isAnyIvy(w->monst)) {
int lim = 0;
// printf("w = %p mondir = %d **\n", w, w->mondir);
while(w != killed && w->mondir != NODIR) {
lim++; if(lim == 100000) {
childbug = true;
printf("childbug!\n");
w->item = itBuggy; break;
}
if(!isAnyIvy(w->monst)) {
return false;
}
w = w->mov[w->mondir];
// printf("w = %p mondir = %d\n", w, w->mondir);
}
}
return w == killed;
}
bool canAttack(cell *c1, eMonster m1, cell *c2, eMonster m2, flagtype flags) {
// cannot eat worms
if((flags & AF_EAT) && isWorm(m2)) return false;
if((flags & AF_GETPLAYER) && isPlayerOn(c2)) m2 = moPlayer;
if(!m2) return false;
if(m2 == moPlayer && peace::on) return false;
if((flags & AF_ONLY_FRIEND) && m2 != moPlayer && !isFriendly(c2)) return false;
if((flags & AF_ONLY_FBUG) && m2 != moPlayer && !isFriendlyOrBug(c2)) return false;
if((flags & AF_ONLY_ENEMY) && (m2 == moPlayer || isFriendlyOrBug(c2))) return false;
if(m2 == moHedge && !(flags & (AF_STAB | AF_TOUGH | AF_EAT | AF_MAGIC | AF_LANCE | AF_SWORD_INTO | AF_HORNS | AF_BULL)))
if(!checkOrb(m1, itOrbThorns)) return false;
// krakens do not try to fight even with Discord
if((m1 == moKrakenT || m1 == moKrakenH) &&
(m2 == moKrakenT || m2 == moKrakenH))
return false;
if(m2 == moDraugr && !(flags & (AF_SWORD | AF_MAGIC | AF_SWORD_INTO | AF_HORNS))) return false;
// if(m2 == moHerdBull && !(flags & AF_MAGIC)) return false;
if(isBull(m2) && !(flags & (AF_MAGIC | AF_HORNS | AF_SWORD_INTO))) return false;
if(m2 == moButterfly && !(flags & (AF_MAGIC | AF_BULL | AF_HORNS | AF_SWORD_INTO))) return false;
if(!(flags & AF_NOSHIELD) && ((flags & AF_NEXTTURN) ? checkOrb2 : checkOrb)(m2, itOrbShield)) return false;
if((flags & AF_STAB) && m2 != moHedge)
if(!checkOrb(m1, itOrbThorns)) return false;
if(flags & AF_BACK) {
if(m2 == moFlailer && !c2->stuntime) flags |= AF_IGNORE_UNARMED;
else if(m2 == moVizier && !isUnarmed(m1)) ;
else return false;
}
if(flags & AF_APPROACH) {
if(m2 == moLancer) ;
else if((flags & AF_HORNS) && checkOrb(m1, itOrbHorns)) ;
else return false;
}
if(!(flags & AF_IGNORE_UNARMED) && isUnarmed(m1)) return false;
if(m2 == moGreater || m2 == moGreaterM)
if(!(flags & (AF_MAGIC | AF_SWORD_INTO | AF_HORNS))) return false;
if(!(flags & AF_GUN)) {
if(c1 && c2 && nonAdjacent(c1,c2) && !attackNonAdjacent(m1) && !attackNonAdjacent(m2) &&
!checkOrb(m1, itOrb37) && !checkOrb(m1, itOrbAether) && !checkOrb(m2, itOrbAether))
return false;
if(c1 && c2 && thruVine(c1,c2) && !attackThruVine(m1) && !attackThruVine(m2) &&
!checkOrb(m1, itOrbAether) && !checkOrb(m2, itOrbAether))
return false;
if(!(flags & (AF_LANCE | AF_STAB | AF_BACK | AF_APPROACH)))
if(c1 && c2 && againstRose(c1, c2) && !ignoresSmell(m1))
return false;
}
if(m2 == moShadow && !(flags & AF_SWORD)) return false;
if(isWorm(m2) && m2 != moTentacleGhost && !isDragon(m2)) return false;
// dragon can't attack itself, or player who mounted it
if(c1 && c2 && isWorm(c1->monst) && isWorm(c2->monst) && wormhead(c1) == wormhead(c2)
&& m1 != moTentacleGhost && m2 != moTentacleGhost)
return false;
// if(m2 == moTortoise && !(flags & AF_MAGIC)) return false;
if(m2 == moRoseBeauty)
if(!(flags & (AF_MAGIC | AF_LANCE | AF_GUN | AF_SWORD_INTO | AF_BULL)))
if(!isMimic(m1))
if(!checkOrb(m1, itOrbBeauty) && !checkOrb(m1, itOrbAether) && !checkOrb(m1, itOrbShield))
if(!c1 || !c2 || !withRose(c1,c2))
return false;
if(m2 == moFlailer && !c2->stuntime)
if(!(flags & (AF_MAGIC | AF_TOUGH | AF_EAT | AF_HORNS | AF_LANCE | AF_BACK | AF_SWORD_INTO | AF_BULL))) return false;
if(m2 == moVizier && c2->hitpoints > 1)
if(!(flags & (AF_MAGIC | AF_TOUGH | AF_EAT | AF_HORNS | AF_LANCE | AF_BACK | AF_FAST | AF_BULL))) return false;
return true;
}
bool stalemate1::isKilled(cell *w) {
if(w->monst == moNone || w == killed) return true;
if(!moveto) return false;
for(int b=0; b<2; b++)
if((w == swordnext[b] || w == swordtransit[b]) && canAttack(moveto, who, w, w->monst, AF_SWORD))
return true;
if(isNeighbor(w, moveto) && moveto != comefrom) {
int wid = neighborId(moveto, w);
int wfrom = neighborId(moveto, comefrom);
int flag = AF_APPROACH;
if(wid >= 0 && wfrom >= 0 && angledist(moveto, wfrom, wid) >= 3) flag |= AF_HORNS;
if(canAttack(moveto, who, w, w->monst, flag)) return true;
}
if(isNeighbor(w, comefrom) && isNeighbor(w, moveto) && moveto != comefrom)
if(canAttack(moveto, who, w, w->monst, AF_STAB))
return true;
if(who == moPlayer && (killed || moveto != comefrom) && mirror::isKilledByMirror(w)) return true;
if(w->monst == moIvyHead || w->monst == moIvyBranch || isMutantIvy(w))
return isChild(w, killed);
if(isDragon(w->monst) && killed && isDragon(killed->monst) && killed->hitpoints) {
cell *head1 = dragon::findhead(w);
cell *head2 = dragon::findhead(killed);
if(head1 == head2 && dragon::totalhp(head1) ==1) return true;
}
if(w->monst == moKrakenT && killed && killed->monst == moKrakenT && killed->hitpoints) {
cell *head1 = w->mov[w->mondir];
cell *head2 = killed->mov[killed->mondir];
if(head1 == head2 && kraken::totalhp(head1) == 1) return true;
}
return false;
}
bool stalemate::isKilled(cell *w) {
for(int f=0; f<size(moves); f++)
if(moves[f].isKilled(w)) return true;
return false;
};
bool isNeighbor(cell *c1, cell *c2) {
for(int i=0; i<c1->type; i++) if(c1->mov[i] == c2) return true;
return false;
}
bool isNeighborCM(cell *c1, cell *c2) {
for(int i=0; i<c1->type; i++) if(createMov(c1, i) == c2) return true;
return false;
}