-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
complex.cpp
4402 lines (3899 loc) · 213 KB
/
complex.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 -- Complex features
// Copyright (C) 2011-2019 Zeno Rogue, see 'hyper.cpp' for details
/** \file complex.cpp
* \brief This file implements the gameplay/generation for the more complex lands and mechanics.
*
* Includes: whirlwind, whirlpool, elec, princess, clearing, mirror, hive, heat + livecaves, etc.
*/
#include "hyper.h"
namespace hr {
EX namespace whirlwind {
EX int fzebra3(cell *c) {
if(arcm::in()) return 0;
if(euclid) {
if(closed_manifold) return 0;
auto co = euc2_coordinates(c);
int y = co.second;
return 1+((((signed short)(y)+int(50000))/3)%3);
}
if(S7 == 5) return getHemisphere(c, 0) > 0 ? 1 : 2;
if(S7 < 5) {
int d = celldistance(currentmap->gamestart(), c);
if(d == 0) return 0;
if(S7 == 4 && d == 3) return 0;
if(S7 == 3 && d == 2 && !ctof(c)) return 0;
return 1;
}
return zebra3(c);
}
EX void switchTreasure(cell *c) {
c->item = itNone;
if(safety) return;
if(hrand(5000) < PT(100 + 2 * (kills[moAirElemental] + kills[moWindCrow]), 200) && notDippingFor(itWindstone)
&& (!shmup::on || getGhostcount() < 2))
c->item = itWindstone;
else if(hrand(5000) < 20*PRIZEMUL)
placeLocalOrbs(c);
}
EX int cat(cell *c) {
if(c->land != laWhirlwind) return 0;
if(c->wall != waNone && c->wall != waChasm &&
c->wall != waSea && !isAlchAny(c) &&
c->wall != waMineMine && c->wall != waFire) return 0;
if(c->item == itKey || c->item == itOrbYendor) return 0;
if(airdist(c) < 3) return 0;
if(c->monst == moHexSnake || c->monst == moHexSnakeTail) return 0;
return fzebra3(c);
}
cell *where;
EX int dfrom[2], dto[2];
EX int qdirs;
int gdist(int d, int e) { return dirdiff(d-e, where->type); }
EX void calcdirs(cell *c) {
where = c;
int d = cat(c);
qdirs = 0;
if(d == 0) return;
int qdf = 0, qdt = 0;
vector<int> cats(c->type);
for(int i=0; i<c->type; i++)
cats[i] = cat(createMov(c,i));
for(int i=0; i<c->type; i++)
if(cats[i] == d) {
bool c1 = (cats[(i+1)%c->type] != d);
bool c2 = (cats[(i+c->type-1)%c->type] != d);
if(c1 && !c2) dto[qdt++] = i;
if(c2 && !c1) dfrom[qdf++] = i;
}
qdirs = qdf;
if(qdirs == 2) {
int cur = gdist(dfrom[0], dto[0]) + gdist(dfrom[1], dto[1]);
int alt = gdist(dfrom[0], dto[1]) + gdist(dfrom[1], dto[0]);
if(alt < cur) swap(dto[0], dto[1]);
}
}
int mindist(int d, int *tab) {
if(qdirs == 0) return NODIR;
if(qdirs == 1) return gdist(d, tab[0]);
return min(gdist(d, tab[0]), gdist(d, tab[1]));
}
EX int winddir(int d) {
if(d == -1) return 0;
int mdf = mindist(d, dfrom);
int mdt = mindist(d, dto);
// printf("dir = %d mdf = %d mdt = %d\n", d, mdf, mdt);
if(mdf < mdt) return -1;
if(mdf > mdt) return 1;
return 0;
}
void build(vector<cell*>& whirlline, int d) {
again:
cell *at = whirlline[isize(whirlline)-1];
cell *prev = whirlline[isize(whirlline)-2];
if(looped(whirlline)) return;
for(int i=0; i<at->type; i++)
if(at->move(i) && (euclid || at->move(i)->master->alt) && celldistAlt(at->move(i)) == d && at->move(i) != prev) {
whirlline.push_back(at->move(i));
goto again;
}
}
void moveAt(cell *c, manual_celllister& cl) {
if(cl.listed(c)) return;
calcdirs(c);
if(qdirs != 1) return;
vector<cell*> whirlline;
whirlline.push_back(c);
cell *prev = c;
cell *c2 = c->move(dfrom[0]);
while(true) {
// printf("c = %p dist = %d\n", c2, c2->mpdist);
if(c == c2) break;
calcdirs(c2);
if(qdirs == 0) break;
cell *cc2 = c2;
if(qdirs == 1) whirlline.push_back(c2), c2 = c2->move(dfrom[0]);
else if(c2->move(dto[0]) == prev)
c2 = c2->move(dfrom[1]);
else
c2 = c2->move(dfrom[0]);
prev = cc2;
}
int z = isize(whirlline);
// printf("Cycle built from %p, length = %d\n", c, z);
for(int i=0; i<z; i++) {
// printf("%d%c", whirlline[i]->mpdist, whirlline[i]->item ? '*' : ' ');
cl.add(whirlline[i]);
if(whirlline[i]->mpdist == BARLEV)
switchTreasure(whirlline[i]);
}
for(int i=0; i<z-1; i++) {
moveItem(whirlline[i], whirlline[i+1], true);
if(whirlline[i]->item)
animateMovement(match(whirlline[i+1], whirlline[i]), LAYER_BOAT);
}
for(int i=0; i<z; i++)
pickupMovedItems(whirlline[i], i==z-1 ? whirlline[0] : whirlline[i+1]);
}
EX void move() {
manual_celllister cl;
for(int i=0; i<isize(dcal); i++) {
cell *c = dcal[i];
moveAt(c, cl);
}
// Keys and Orbs of Yendor always move
using namespace yendor;
for(int i=0; i<isize(yi); i++) {
moveAt(yi[i].path[0], cl);
moveAt(yi[i].key(), cl);
}
}
EX cell *jumpFromWhereTo(cell *c, bool player, struct jumpdata& jdata) {
jdata.uniq = true;
for(int i=0; i<2; i++) {
calcdirs(c);
if(qdirs != 1) return NULL;
auto mi = movei(c, dfrom[0]);
jdata.moves.push_back(mi.rev());
cell *c2 = c->move(dfrom[0]);
if(!passable(c, c2, P_JUMP1)) return NULL;
if(player && i == 0 && !passable(c, c2, P_ISPLAYER)) return NULL;
c = c2;
}
reverse(jdata.moves.begin(), jdata.moves.end());
calcdirs(c);
if(qdirs != 1) return NULL;
return c;
}
EX cell *jumpDestination(cell *c) {
for(int i=0; i<2; i++) {
calcdirs(c);
if(qdirs != 1) return NULL;
c = c->move(dto[0]);
}
calcdirs(c);
if(qdirs != 1) return NULL;
return c;
}
EX }
EX namespace elec {
EX bool havecharge, haveelec, havethunder;
EX bool afterOrb; // extra charge from the Orb of Lightning
#if HDR
enum eCharge {
ecCharged, ecGrounded, ecIsolator, ecConductor
};
#endif
bool conduct(eCharge cf, eCharge ct) {
if(ct == ecIsolator) return false;
if(ct == ecConductor) return true;
return ct != cf;
}
EX eCharge getCharge(cell *c) {
bool ao = afterOrb && c->ligon;
/* not yet generated */
if(c->land == laNone) return ecIsolator;
if(c->wall == waCharged) return ecCharged;
if(c->wall == waSea || c->wall == waGrounded) return ecGrounded;
if(c->wall == waSandstone || c->wall == waDeadTroll ||
c->wall == waDeadTroll2 ||
c->wall == waExplosiveBarrel ||
c->wall == waVinePlant ||
c->wall == waMetal || isAlchAny(c))
return isElectricLand(c) ? ecConductor : ecGrounded;
if(c->wall == waBigTree || c->wall == waSmallTree)
return ecGrounded;
if(among(c->wall, waRed1, waRed2, waRed3, waRubble, waDeadfloor2))
return ecIsolator;
if(c->wall == waBarrier)
return ecIsolator;
if(c->wall == waChasm)
return ecIsolator;
if(shmup::on ? isPlayerOn(c) : (isPlayerOn(c) || (items[itOrbEmpathy] && isFriendly(c)))) {
if(items[itOrbShield]) return ecIsolator;
if(afterOrb) return ecIsolator;
if(!items[itOrbAether]) return isElectricLand(c) ? ecConductor : ecGrounded;
}
// if(c->monst && stalemate::moveto) printf("%p: isKilled = %d\n", c, stalemate::isKilled(c));
else if(c->monst
&& c->monst != moGhost && c->monst != moIvyDead && c->monst != moIvyNext
&& !(isDragon(c->monst) && !c->hitpoints)
)
return isElectricLand(c) ? (ao ? ecCharged : ecConductor) : ecGrounded;
if(!isElectricLand(c))
return ecGrounded;
if(ao) return ecCharged;
return ecIsolator;
}
// To process conductivity, consider the following graph:
// - edges are between conductors and adjacent charged/grounded/conductor cells
// - all charged cells are connected to one special cell '0'
// - all grounded cells are connected to one special cell '1'
// - cells '0' and '1' are connected
// If A and B are in the same biconnected component, then there is a closed circuit,
// consisting of all other cells in that component.
// To find biconnected components, we are using the Hopcroft-Tarjan algorithm.
struct chargedata {
cell *c;
int otmp;
int lowlink;
bool instack;
bool fire;
};
vector<chargedata> charges;
vector<pair<int, int> > xstack;
vector<cell*> chargecells;
bool hasdata(cell *c) {
return c->listindex >= 0 && c->listindex < isize(charges) && charges[c->listindex].c == c;
}
void connect(int from, cell *c) {
if(hasdata(c)) {
// seen again: set the lowlink
if(!charges[c->listindex].instack) return;
// printf("edge %d-%d\n", from, c->listindex);
if(c->listindex < charges[from].lowlink)
charges[from].lowlink = c->listindex;
}
else {
int id = isize(charges);
charges.push_back(chargedata());
{chargedata& ch(charges[id]);
ch.c = c; ch.otmp = c->listindex; ch.lowlink = id; c->listindex = id;
ch.instack = true; ch.fire = false;
}
// c->landparam = id;
// printf("edge %d-%d [%s]\n", from, id, dnameof(c->wall));
xstack.push_back(make_pair(from, id));
eCharge chh = getCharge(c);
if(chh == ecGrounded) {
xstack.push_back(make_pair(id, 0));
charges[id].lowlink = 0;
}
else if(chh == ecCharged) {
xstack.push_back(make_pair(id, 1));
if(from != 1) charges[id].lowlink = 1;
}
for(cell *c2: adj_minefield_cells(c)) {
if(c2->listindex == from) continue;
eCharge ct = getCharge(c2);
if(conduct(chh, ct))
connect(id, c2);
}
// printf("lowlink of %d [%s] = %d\n", id, dnameof(c->wall), ch.lowlink);
if(charges[id].lowlink < charges[from].lowlink)
charges[from].lowlink = charges[id].lowlink;
if(charges[id].lowlink >= from) {
while(xstack.back().first != from || xstack.back().second != id) {
// printf("bcc %d,%d\n", xstack.back().first, xstack.back().second);
xstack.pop_back();
}
// printf("bcc %d,%d\n", xstack.back().first, xstack.back().second);
xstack.pop_back();
// printf("\n");
}
charges[id].instack = false;
}
}
void affect(cell *c) {
c->ligon = true;
if(c->monst) {
if(c->monst == moMetalBeast2 && !c->item)
c->item = itFulgurite;
killMonster(c, moLightningBolt);
}
if(isPlayerOn(c)) {
killThePlayerAt(moLightningBolt, c, 0);
}
if(c->wall == waSandstone) {
c->wall = waNone, drawParticles(c, winf[waSandstone].color, 16);
if(c->land == laStorms)
c->item = itFulgurite;
}
if(c->wall == waRed1) c->wall = waNone;
if(c->wall == waRed2) c->wall = waRed1;
if(c->wall == waRed3) c->wall = waRed2;
if(c->wall == waDeadTroll) c->wall = waCavefloor;
if(c->wall == waBigTree || c->wall == waSmallTree)
makeflame(c, 10, false);
if(c->wall == waExplosiveBarrel)
explodeBarrel(c);
if(c->wall == waDeadTroll2 || isAlchAny(c) || c->wall == waVinePlant)
drawParticles(c, winf[c->wall].color, 16),
c->wall = waNone;
/* destroy charged walls on the border */
if(c->wall == waCharged) forCellEx(c1, c) if(c1->wall == waGrounded)
c->wall = waNone;
/* if(c->wall == waCharged)
c->wall = waMetal; */
}
void listChargedCells(cell *c, manual_celllister& cl, eCharge last = ecConductor) {
if(cl.listed(c)) return;
eCharge here = getCharge(c);
/* if(c->cpdist <= 2) {
printf("monst=%s ", dnameof(c->monst));
printf("wall=%s ", dnameof(c->wall));
printf("c=%p here=%d last=%d\n", c, here, last);
} */
if(here == ecIsolator) cl.add(c);
if(!conduct(last, here)) return;
if(here == ecCharged) chargecells.push_back(c);
cl.add(c);
for(int i=0; i<c->type; i++) {
cell *c2 = c->move(i);
if(c2) listChargedCells(c2, cl, here);
}
}
EX void init() {
chargecells.clear();
if(!haveelec && !afterOrb) return;
if(1) {
manual_celllister cl;
for(int i=0; i<isize(dcal); i++) listChargedCells(dcal[i], cl);
}
charges.resize(2);
charges[0].lowlink = 0; charges[1].lowlink = 1;
if(!havecharge) return;
xstack.clear();
for(int i=0; i<isize(chargecells); i++)
connect(1, chargecells[i]);
havethunder = charges[1].lowlink == 0;
if(havethunder) {
for(int i=0; i<isize(xstack); i++) {
int k = xstack[i].first;
int l = xstack[i].second;
// printf("connected %d-%d\n", k, l);
charges[k].fire = true;
charges[l].fire = true;
}
}
}
void fire() {
if(havethunder) {
addMessage(XLAT("There is a flash of thunder!"));
playSound(NULL, "storm");
drawLightning();
for(int i=2; i<isize(charges); i++) if(charges[i].fire)
affect(charges[i].c);
}
}
EX void cleanup() {
for(int i=2; i<isize(charges); i++)
charges[i].c->listindex = charges[i].otmp;
charges.resize(0);
}
void draw(cell *c, eCharge what) {
if(c->ligon) return;
c->ligon = true;
for(int i=0; i<c->type; i++) {
cell *c2 = c->move(i);
if(!c2) continue;
eCharge ch = getCharge(c2);
if(conduct(what, ch))
draw(c2, ch);
}
}
void drawcharges() {
for(int i=0; i<isize(dcal); i++)
if(getCharge(dcal[i]) == ecCharged)
draw(dcal[i], ecCharged);
}
EX bool affected(cell *c) {
if(c->listindex >= 0 && c->listindex < isize(charges) && charges[c->listindex].c == c)
return charges[c->listindex].fire;
return false;
}
#if HDR
struct builder {
builder() { init(); }
~builder() { cleanup(); }
};
#endif
EX void act() {
int k = tkills();
for(int i=0; i<numplayers(); i++)
if(multi::playerActive(i) && isElectricLand(playerpos(i)) && !afterOrb)
markOrb(itOrbShield), markOrb(itOrbAether);
builder b;
fire();
if(!afterOrb)
achievement_count("ELEC", tkills() - k, 0);
}
// 0 = no close escape, 1 = close escape, 2 = message already shown
EX int lightningfast;
EX void checklightningfast() {
changes.value_keep(lightningfast);
if(lightningfast == 1) {
addMessage(XLAT("Wow! That was close."));
lightningfast = 2;
}
if(lightningfast > 1) return;
builder b;
for(int i=0; i<numplayers(); i++)
if(multi::playerActive(i) && elec::affected(playerpos(i)))
lightningfast = 1;
}
EX }
EX namespace princess {
#if HDR
#define OUT_OF_PRISON 200
#define OUT_OF_PALACE 250
#define PRADIUS0 (141)
#define PRADIUS1 (150)
struct info {
int id; // id of this info
cell *prison; // where was the Princess locked
heptagon *alt; // alt of the prison
int bestdist; // best dist achieved
int bestnear; // best dist achieved, by the player
int value; // number of Rugs at 120
cell *princess; // where is the Princess currently
int together; // in which turn have we been together -- for the hug animation
};
#endif
EX gp::loc coords() { return gp::loc(39, 21); }
EX bool generating = false;
EX bool challenge = false;
EX bool saved = false;
EX bool everSaved = false;
EX bool forceVizier = false;
EX bool forceMouse = false;
EX bool gotoPrincess = false;
EX bool nodungeon = false;
EX bool squeaked = false;
EX int saveHP = 0;
EX int saveArmedHP = 0;
EX int reviveAt;
EX vector<info*> infos;
void assign(info *i) {
if(i->alt) i->alt->emeraldval = i->id;
}
EX int newInfo(cell *c) {
info *i = new info;
i->prison = c;
i->princess = c;
i->alt = c->master->alt;
i->id = isize(infos);
i->bestdist = 0;
i->bestnear = OUT_OF_PRISON;
i->together = -INF;
infos.push_back(i);
assign(i);
return i->id;
}
EX void newFakeInfo(cell *c) {
info *i = new info;
i->prison = NULL;
i->princess = c;
i->alt = NULL;
i->id = isize(infos);
i->bestdist = items[itSavedPrincess] ? OUT_OF_PALACE : OUT_OF_PRISON;
i->bestnear = 0;
i->together = -INF;
infos.push_back(i);
assign(i);
}
EX info *getPrisonInfo(cell *c) {
if(euclid || quotient || sphere) return NULL;
if(c->land != laPalace) return NULL;
if(!c->master->alt) return NULL;
int ev = c->master->alt->alt->emeraldval; // NEWYEARFIX
if(ev < 0 || ev >= isize(infos)) return NULL;
if(infos[ev]->alt != c->master->alt->alt) return NULL;
return infos[ev];
}
EX info *getPrincessInfo(cell *c) {
for(int i=0; i<isize(infos); i++) if(infos[i]->princess == c) {
while(i) {
infos[i]->id = i-1; assign(infos[i]);
infos[i-1]->id = i; assign(infos[i-1]);
swap(infos[i], infos[i-1]);
i--;
}
return infos[i];
}
return NULL;
}
EX int dist(cell *c) {
if(c->land != laPalace && c->land != laDungeon) return OUT_OF_PALACE;
else if(quotient || sphere) return OUT_OF_PRISON;
else if(euclid) return celldistAlt(c);
else if(!c->master->alt) return OUT_OF_PRISON;
else return celldistAlt(c);
}
void clear() {
for(int i=0; i<isize(infos); i++) delete infos[i];
infos.clear();
}
bool bringBackAt(cell *c) {
if(!c) return false;
if(!passable(c, NULL, 0)) return false;
c->monst = moPrincessArmed;
c->stuntime = 0;
c->hitpoints = palaceHP();
drawFlash(c);
playSound(c, princessgender() ? "heal-princess" : "heal-prince");
info *inf = NULL;
for(int i=0; i<isize(infos); i++) {
if(infos[i]->princess && infos[i]->bestdist == OUT_OF_PALACE && isPrincess(infos[i]->princess->monst))
inf = infos[i];
}
if(inf) { inf->princess->monst = moNone; inf->princess = c; }
else newFakeInfo(c);
return true;
}
EX void bringBack() {
if(bringBackAt(cwt.peek())) return;
for(int i=1; i<isize(dcal); i++)
if(bringBackAt(dcal[i])) return;
}
void setdist(info *i, int newdist) {
changes.value_keep(*i);
if(newdist < ALTDIST_ERROR && newdist > i->bestdist) {
i->bestdist = newdist;
// printf("Improved dist to %d\n", newdist);
if(newdist == OUT_OF_PALACE) {
if(!princess::saved)
#if CAP_INV
if(!inv::on || !inv::usedForbidden)
#endif
achievement_gain_once("PRINCESS1");
changes.value_set(princess::saved, true);
changes.value_set(princess::everSaved, true);
if(inv::on && !princess::reviveAt)
changes.value_set(princess::reviveAt, gold(NO_LOVE));
items[itSavedPrincess]++;
}
if(newdist == OUT_OF_PRISON && princess::challenge) {
addMessage(XLAT("Congratulations! Your score is %1.", its(i->value)));
achievement_gain_once("PRINCESS2", rg::princess);
if(!cheater) achievement_score(LB_PRINCESS, i->value);
LATE( showMissionScreen(); )
}
}
if(i->princess->land == laDungeon && !saved && !nodungeon) {
addMessage(XLAT("%The1 says, \"not this place, it looks even worse...\"", moPrincess));
nodungeon = true;
}
}
EX void save(cell *princess) {
if(euclid) return;
princess::info *i = princess::getPrincessInfo(princess);
if(!i || i->bestdist <= 3) changes.ccell(princess), princess->monst = moNone;
else if(i) setdist(i, OUT_OF_PRISON);
}
EX void move(const movei& mi) {
auto& cf = mi.s;
auto& ct = mi.t;
if(euclid) return;
princess::info *i = princess::getPrincessInfo(cf);
if(!i) {
// note: OK if mapediting or loading
printf("Warning: unknown princess\n");
if(!cheater)
addMessage("Warning: unknown princess (that's a bug, please report)");
newFakeInfo(ct);
}
else {
changes.value_keep(*i);
i->princess = ct;
setdist(i, dist(ct));
forCellIdEx(cp, j, ct) if(isPlayerOn(cp)) {
bool safe = true;
for(cell *test: {ct, cp}) forCellEx(c1, test) forCellEx(c2, test)
if(isActiveEnemy(c1, moPlayer) || isActiveEnemy(c2, moPlayer))
safe = false;
if(safe) {
if(turncount > i->together + 20) {
animateHug(movei(ct, j), LAYER_SMALL);
animateHug(movei(ct, j).rev(), LAYER_SMALL);
}
i->together = turncount;
}
}
}
}
EX void mouseSqueak(cell *c) {
eMonster m = c->monst;
info *i = getPrisonInfo(c);
int d = dist(c);
playSound(c, "mousesqueak", 40);
if(!i)
addMessage(XLAT("%The1 squeaks in a confused way.", m));
else if(i->bestdist >= 6)
addMessage(XLAT("%The1 squeaks gratefully!", m));
else if(!i->princess)
addMessage(XLAT("%The1 squeaks hopelessly.", m));
else if(d > 120)
addMessage(XLAT("%The1 squeaks in despair.", m));
else if(d > 90)
addMessage(XLAT("%The1 squeaks sadly.", m));
else if(d > 60)
addMessage(XLAT("%The1 squeaks with hope!", m));
else if(d > 30)
addMessage(XLAT("%The1 squeaks happily!", m));
else
addMessage(XLAT("%The1 squeaks excitedly!", m));
}
EX void line(cell *c) {
int d = (euclid || c->master->alt) ? celldistAlt(c) : 200;
eMonster m = c->monst;
static int msgid = 0;
changes.value_keep(msgid);
playSound(c, princessgender() ? "speak-princess" : "speak-prince");
retry:
if(msgid >= 127) msgid = 0;
bool inpalace = c->land == laPalace || c->land == laDungeon;
if(msgid == 0 && d < 20 && inpalace) {
addMessage(XLAT("%The1 kisses you, and begs you to bring %him1 away from here.", m));
}
else if(msgid == 1 && d >= 20 && inpalace && !peace::on) {
if(m == moPrincess)
addMessage(XLAT("\"I want my revenge. Stun a guard and leave him for me!\"", m));
else
addMessage(XLAT("\"I wouldn't say killing that guard was not pleasant...\"", m));
}
else if(msgid == 2 && d >= 70 && inpalace) {
addMessage(XLAT("\"Bring me out of here please!\"", m));
}
else if(msgid == 3 && !inpalace) {
addMessage(XLAT("%The1 kisses you, and thanks you for saving %him1.", m));
}
else if(msgid == 4 && !inpalace && m == moPrincess && !peace::on) {
addMessage(XLAT("\"I have been trained to fight with a Hypersian scimitar, you know?\"", m));
}
else if(msgid == 16 && !inpalace) {
addMessage(XLAT("\"I would love to come to your world with you!\"", m));
}
else if(msgid == 20 && !inpalace) {
addMessage(XLAT("\"I do not like butterflies. They are treacherous.\"", m));
}
else if(msgid == 32 && !inpalace) {
addMessage(XLAT("\"Straight lines stay close to each other forever, this is so romantic!\"", m));
}
else if(msgid == 40 && !inpalace) {
addMessage(XLAT("\"I hate roses.\"", m));
}
else if(msgid == 48 && !inpalace) {
addMessage(XLAT("\"Maps... Just like the world, but smaller... how is that even possible?!\"", m));
}
else if(msgid == 64) {
addMessage(XLAT("\"In this world there is plenty of space for everyone. We do not need wars.\"", m));
}
else if(msgid == 65) {
addMessage(XLAT("\"Only the stupid hyperbugs do not understand this.\"", m));
}
else if(msgid == 72 && !inpalace) {
addMessage(XLAT("\"I have once talked to a Yendorian researcher... he was only interested in infinite trees.\"", m));
}
else if(msgid == 73 && !inpalace) {
addMessage(XLAT("\"Infinite trees are boring. I prefer other graphs.\"", m));
}
else if(msgid == 74 && !inpalace) {
addMessage(XLAT("\"Did you know that the Cultists are relatives of the Desert Men?\"", m));
}
else if(msgid == 80) {
addMessage(XLAT("\"Are there Temples of Cthulhu in your world? Why not?\"", m));
}
else {
msgid++; goto retry;
}
msgid++;
}
EX void playernear(cell *c) {
info *i = getPrisonInfo(c);
int d = dist(c);
// if(i) printf("d=%d bn=%d\n", d, i->bestnear);
if(i && d < i->bestnear) {
changes.value_keep(*i);
if(i->bestnear > 100 && d <= 100) {
i->value = items[itPalace];
if(princess::challenge)
addMessage(XLAT("Hardness frozen at %1.", its(i->value)));
}
i->bestnear = d;
}
}
EX }
EX namespace clearing {
#if HDR
struct clearingdata {
cell *root;
int dist;
bool buggy;
clearingdata() { root = nullptr; }
};
#endif
EX std::map<heptagon*, clearingdata> bpdata;
EX cell *current_root;
EX void new_root() {
if(!current_root || current_root->monst != moMutant) {
auto& ac = currentmap->allcells();
int iter = 0;
while(!current_root || pseudohept(current_root) || current_root->cpdist < 3) {
if(iter++ > 100) return;
current_root = ac[hrand(isize(ac))];
}
current_root->monst = moMutant;
current_root->mondir = NODIR;
current_root->stuntime = (mutantphase + 1) & 15;
}
}
int plantdir(cell *c) {
if(have_alt(c))
gen_alt_around(c);
int d = celldistAlt(c);
if(PURE) {
forCellIdCM(c2, i, c) {
if(!pseudohept(c2) && celldistAlt(c2) == d-1)
return i;
}
forCellIdCM(c2, i, c) {
if(celldistAlt(c2) == d-1)
return geometry == gBinary4 ? i : (i+1) % c->type;
}
}
forCellIdCM(c2, i, c) {
if(!pseudohept(c2) && celldistAlt(c2) == d-1)
return i;
}
int quseful = 0, tuseful = 0, tuseful2 = 0;
forCellIdCM(c2, i, c) if(!pseudohept(c2)) {
if(celldistAlt(c2) == d) {
bool useful = false;
for(int j=1; j<S6; j++) {
cell *c3 = createMov(c2, j);
if(celldistAlt(c3) == d-1)
useful = true;
}
if(useful) quseful++, tuseful += (1<<i), tuseful2 = i;
}
}
if(quseful == 1) return tuseful2;
if(quseful == 2) {
int i;
if(tuseful == (1<<3)+(1<<5)) i = 3;
if(tuseful == (1<<5)+(1<<1)) i = 5;
if(tuseful == (1<<1)+(1<<3)) i = 1;
if(tuseful == (1<<5)+(1<<7)) i = 5;
if(tuseful == (1<<7)+(1<<1)) i = 7;
if((d & 7) < 4) i = (i+2) % c->type;
return i;
}
printf("error in plantdir\n");
return 1;
}
vector<cell*> onpath;
vector<int> pdir;
vector<cell*> rpath;
EX void generate(cell *c) {
if(euclid) {
if(quotient) return; // fix cylinder
if(pseudohept(c)) return;
c->monst = moMutant;
auto co = euc2_coordinates(c);
int x = co.first, y = co.second;
int xco = x * 2 + y + 1;
c->stuntime = (8-xco/2) & 15;
// 2, 4, 5, 7
if(pseudohept(createMov(c, 0)))
c->mondir = 1 + hrand(2) * 4;
else
c->mondir = 0;
return;
}
if(c->land == laClearing && ls::horodisk_structure() && celldistAlt(c) >= -1) return;
if(c->land == laClearing && ls::voronoi_structure() && celldistAlt(c) >= -20) return;
if(!eubinary && !horo_ok()) return;
// cell *oc = c;
gen_alt(c);
if(pseudohept(c)) return;
heptagon *a = eubinary ? NULL : c->master->alt->alt;
clearingdata& bd(bpdata[a]);
if(!bd.root) { bd.root = c; bd.dist = 8; bd.buggy = false; }
if(bd.buggy) return;
onpath.clear(); pdir.clear(); rpath.clear();
int steps = 0;
int ds;
int stepcount = 0;
while(true) {
if(c == bd.root) {ds = bd.dist; break; }
// printf("R %4d C %4d\n", celldistAlt(bd.root), celldistAlt(c));
if(celldistAlt(c) > celldistAlt(bd.root)) {
stepcount++;
if(stepcount > iteration_limit) {
printf("buggy #1\n");
bd.buggy = true;
return;
}
if(c->mpdist <= 6) {
if(c->monst != moMutant) return; // already cut!
// ... else simply extend it
ds = c->stuntime; break;
}
int d = plantdir(c);
steps++;
onpath.push_back(c); pdir.push_back(d);
// printf("c [%4d] %p -> %p\n", celldistAlt(c), c, c->move(d));
c = c->move(d);
}
else {
bd.dist--;
if(bd.dist < -iteration_limit) {
for(int i=0; i<steps; i++)
onpath[i]->item = itBuggy;
for(int i=0; i<(int) rpath.size(); i++)
rpath[i]->item = itBuggy;
printf("buggy #2\n");
bd.buggy = true;
return;
}
rpath.push_back(bd.root);
// printf("r [%4d] %p -> %p\n", celldistAlt(bd.root), bd.root, bd.root->move(plantdir(bd.root)));
bd.root = createMov(bd.root, plantdir(bd.root));
}
}
// printf("steps = %d dist = %d [%d]\n", steps, bd.dist, oc->mpdist);
onpath.push_back(c); pdir.push_back(plantdir(c));
while(steps >= 0) {
c = onpath[steps];
if(steps == 0) {
c->monst = moMutant;
c->mondir = pdir[steps];
if(pdir[steps] != plantdir(c)) {
printf("pdir i/ plantdir\n");
exit(1);
}
c->stuntime = ds & 15;
}
if(c->mpdist <= 7 && c->monst != moMutant)
break;
steps--; ds++;
}
}
/** cells with the same celltype are likely to have the same number of descendant leaves */
typedef tuple<int, int, int, int> celltype;