1 // Hyperbolic Rogue -- Complex features
2 // Copyright (C) 2011-2019 Zeno Rogue, see 'hyper.cpp' for details
3 
4 /** \file complex.cpp
5  *  \brief This file implements the gameplay/generation for the more complex lands and mechanics.
6  *
7  *  Includes: whirlwind, whirlpool, elec, princess, clearing, mirror, hive, heat + livecaves, etc.
8  */
9 
10 #include "hyper.h"
11 namespace hr {
12 
13 EX namespace whirlwind {
14 
fzebra3(cell * c)15   EX int fzebra3(cell *c) {
16     if(arcm::in()) return 0;
17     if(euclid) {
18       if(bounded) return 0;
19       auto co = euc2_coordinates(c);
20       int y = co.second;
21       return 1+((((signed short)(y)+int(50000))/3)%3);
22       }
23     if(S7 == 5) return getHemisphere(c, 0) > 0 ? 1 : 2;
24     if(S7 < 5) {
25       int d = celldistance(currentmap->gamestart(), c);
26       if(d == 0) return 0;
27       if(S7 == 4 && d == 3) return 0;
28       if(S7 == 3 && d == 2 && !ctof(c)) return 0;
29       return 1;
30       }
31     return zebra3(c);
32     }
33 
switchTreasure(cell * c)34   EX void switchTreasure(cell *c) {
35     c->item = itNone;
36     if(safety) return;
37     if(hrand(5000) < PT(100 + 2 * (kills[moAirElemental] + kills[moWindCrow]), 200) && notDippingFor(itWindstone)
38       && (!shmup::on || getGhostcount() < 2))
39       c->item = itWindstone;
40     else if(hrand(5000) < 20*PRIZEMUL)
41       placeLocalOrbs(c);
42     }
43 
cat(cell * c)44   EX int cat(cell *c) {
45     if(c->land != laWhirlwind) return 0;
46     if(c->wall != waNone && c->wall != waChasm &&
47       c->wall != waSea && !isAlchAny(c) &&
48       c->wall != waMineMine && c->wall != waFire) return 0;
49     if(c->item == itKey || c->item == itOrbYendor) return 0;
50     if(airdist(c) < 3) return 0;
51     if(c->monst == moHexSnake || c->monst == moHexSnakeTail) return 0;
52     return fzebra3(c);
53     }
54 
55   cell *where;
56   EX int dfrom[2], dto[2];
57   EX int qdirs;
58 
gdist(int d,int e)59   int gdist(int d, int e) { return dirdiff(d-e, where->type); }
60 
calcdirs(cell * c)61   EX void calcdirs(cell *c) {
62     where = c;
63     int d = cat(c);
64     qdirs = 0;
65     if(d == 0) return;
66     int qdf = 0, qdt = 0;
67     vector<int> cats(c->type);
68     for(int i=0; i<c->type; i++)
69       cats[i] = cat(createMov(c,i));
70     for(int i=0; i<c->type; i++)
71       if(cats[i] == d) {
72         bool c1 = (cats[(i+1)%c->type] != d);
73         bool c2 = (cats[(i+c->type-1)%c->type] != d);
74         if(c1 && !c2) dto[qdt++] = i;
75         if(c2 && !c1) dfrom[qdf++] = i;
76         }
77     qdirs = qdf;
78     if(qdirs == 2) {
79       int cur = gdist(dfrom[0], dto[0]) + gdist(dfrom[1], dto[1]);
80       int alt = gdist(dfrom[0], dto[1]) + gdist(dfrom[1], dto[0]);
81       if(alt < cur) swap(dto[0], dto[1]);
82       }
83     }
84 
mindist(int d,int * tab)85   int mindist(int d, int *tab) {
86     if(qdirs == 0) return NODIR;
87     if(qdirs == 1) return gdist(d, tab[0]);
88     return min(gdist(d, tab[0]), gdist(d, tab[1]));
89     }
90 
winddir(int d)91   EX int winddir(int d) {
92     if(d == -1) return 0;
93     int mdf = mindist(d, dfrom);
94     int mdt = mindist(d, dto);
95     // printf("dir = %d mdf = %d mdt = %d\n", d, mdf, mdt);
96     if(mdf < mdt) return -1;
97     if(mdf > mdt) return 1;
98     return 0;
99     }
100 
build(vector<cell * > & whirlline,int d)101   void build(vector<cell*>& whirlline, int d) {
102     again:
103     cell *at = whirlline[isize(whirlline)-1];
104     cell *prev = whirlline[isize(whirlline)-2];
105     for(int i=0; i<at->type; i++)
106       if(at->move(i) && (euclid || at->move(i)->master->alt) && celldistAlt(at->move(i)) == d && at->move(i) != prev) {
107         whirlline.push_back(at->move(i));
108         goto again;
109         }
110     }
111 
moveAt(cell * c,manual_celllister & cl)112   void moveAt(cell *c, manual_celllister& cl) {
113     if(cl.listed(c)) return;
114     calcdirs(c);
115     if(qdirs != 1) return;
116     vector<cell*> whirlline;
117     whirlline.push_back(c);
118     cell *prev = c;
119     cell *c2 = c->move(dfrom[0]);
120     while(true) {
121       // printf("c = %p dist = %d\n", c2, c2->mpdist);
122       if(c == c2) break;
123       calcdirs(c2);
124       if(qdirs == 0) break;
125       cell *cc2 = c2;
126       if(qdirs == 1) whirlline.push_back(c2), c2 = c2->move(dfrom[0]);
127       else if(c2->move(dto[0]) == prev)
128         c2 = c2->move(dfrom[1]);
129       else
130         c2 = c2->move(dfrom[0]);
131       prev = cc2;
132       }
133     int z = isize(whirlline);
134     // printf("Cycle built from %p, length = %d\n", c, z);
135     for(int i=0; i<z; i++) {
136       // printf("%d%c", whirlline[i]->mpdist, whirlline[i]->item ? '*' : ' ');
137       cl.add(whirlline[i]);
138       if(whirlline[i]->mpdist == BARLEV)
139         switchTreasure(whirlline[i]);
140       }
141     for(int i=0; i<z-1; i++) {
142       moveItem(whirlline[i], whirlline[i+1], true);
143       if(whirlline[i]->item)
144         animateMovement(match(whirlline[i+1], whirlline[i]), LAYER_BOAT);
145       }
146     for(int i=0; i<z; i++)
147       pickupMovedItems(whirlline[i]);
148     }
149 
move()150   EX void move() {
151     manual_celllister cl;
152     for(int i=0; i<isize(dcal); i++) {
153       cell *c = dcal[i];
154       moveAt(c, cl);
155       }
156     // Keys and Orbs of Yendor always move
157     using namespace yendor;
158     for(int i=0; i<isize(yi); i++) {
159       moveAt(yi[i].path[0], cl);
160       moveAt(yi[i].key(), cl);
161       }
162     }
163 
jumpFromWhereTo(cell * c,bool player)164   EX cell *jumpFromWhereTo(cell *c, bool player) {
165     for(int i=0; i<2; i++) {
166       calcdirs(c);
167       if(qdirs != 1) return NULL;
168       cell *c2 = c->move(dfrom[0]);
169       if(!passable(c, c2, P_JUMP1)) return NULL;
170       if(player && i == 0 && !passable(c, c2, P_ISPLAYER)) return NULL;
171       c = c2;
172       }
173     calcdirs(c);
174     if(qdirs != 1) return NULL;
175     return c;
176     }
177 
jumpDestination(cell * c)178   EX cell *jumpDestination(cell *c) {
179     for(int i=0; i<2; i++) {
180       calcdirs(c);
181       if(qdirs != 1) return NULL;
182       c = c->move(dto[0]);
183       }
184     calcdirs(c);
185     if(qdirs != 1) return NULL;
186     return c;
187     }
188 EX }
189 
190 EX namespace elec {
191 
192   EX bool havecharge, haveelec, havethunder;
193   EX bool afterOrb; // extra charge from the Orb of Lightning
194 
195   #if HDR
196   enum eCharge {
197     ecCharged, ecGrounded, ecIsolator, ecConductor
198     };
199   #endif
200 
conduct(eCharge cf,eCharge ct)201   bool conduct(eCharge cf, eCharge ct) {
202     if(ct == ecIsolator) return false;
203     if(ct == ecConductor) return true;
204     return ct != cf;
205     }
206 
getCharge(cell * c)207   EX eCharge getCharge(cell *c) {
208     bool ao = afterOrb && c->ligon;
209 
210     /* not yet generated */
211     if(c->land == laNone) return ecIsolator;
212 
213     if(c->wall == waCharged) return ecCharged;
214     if(c->wall == waSea || c->wall == waGrounded) return ecGrounded;
215     if(c->wall == waSandstone || c->wall == waDeadTroll ||
216       c->wall == waDeadTroll2 ||
217       among(c->wall, waBigTree, waSmallTree, waExplosiveBarrel, waRed1, waRed2, waRed3) ||
218       c->wall == waVinePlant ||
219       c->wall == waMetal || isAlchAny(c))
220         return isElectricLand(c) ? ecConductor : ecGrounded;
221     if(c->wall == waBarrier)
222       return ecIsolator;
223     if(c->wall == waChasm)
224       return ecIsolator;
225 
226     if(shmup::on ? isPlayerOn(c) : (isPlayerOn(c) || (items[itOrbEmpathy] && isFriendly(c)))) {
227       if(items[itOrbShield]) return ecIsolator;
228       if(afterOrb) return ecIsolator;
229       if(!items[itOrbAether]) return isElectricLand(c) ? ecConductor : ecGrounded;
230       }
231 
232     // if(c->monst && stalemate::moveto) printf("%p: isKilled = %d\n", c, stalemate::isKilled(c));
233 
234     else if(c->monst
235       && c->monst != moGhost && c->monst != moIvyDead && c->monst != moIvyNext
236       && !(isDragon(c->monst) && !c->hitpoints)
237       )
238       return isElectricLand(c) ? (ao ? ecCharged : ecConductor) : ecGrounded;
239 
240     if(!isElectricLand(c))
241       return ecGrounded;
242 
243     if(ao) return ecCharged;
244 
245     return ecIsolator;
246     }
247 
248   // To process conductivity, consider the following graph:
249 
250   // - edges are between conductors and adjacent charged/grounded/conductor cells
251   // - all charged cells are connected to one special cell '0'
252   // - all grounded cells are connected to one special cell '1'
253   // - cells '0' and '1' are connected
254 
255   // If A and B are in the same biconnected component, then there is a closed circuit,
256   // consisting of all other cells in that component.
257 
258   // To find biconnected components, we are using the Hopcroft-Tarjan algorithm.
259 
260   struct chargedata {
261     cell *c;
262     int otmp;
263     int lowlink;
264     bool instack;
265     bool fire;
266     };
267 
268   vector<chargedata> charges;
269 
270   vector<pair<int, int> > xstack;
271 
272   vector<cell*> chargecells;
273 
hasdata(cell * c)274   bool hasdata(cell *c) {
275     return c->listindex >= 0 && c->listindex < isize(charges) && charges[c->listindex].c == c;
276     }
277 
connect(int from,cell * c)278   void connect(int from, cell *c) {
279     if(hasdata(c)) {
280       // seen again: set the lowlink
281       if(!charges[c->listindex].instack) return;
282       // printf("edge %d-%d\n", from, c->listindex);
283       if(c->listindex < charges[from].lowlink)
284         charges[from].lowlink = c->listindex;
285       }
286     else {
287       int id = isize(charges);
288       charges.push_back(chargedata());
289 
290       {chargedata& ch(charges[id]);
291       ch.c = c; ch.otmp = c->listindex; ch.lowlink = id; c->listindex = id;
292       ch.instack = true; ch.fire = false;
293       }
294       // c->landparam = id;
295 
296       // printf("edge %d-%d [%s]\n", from, id, dnameof(c->wall));
297 
298       xstack.push_back(make_pair(from, id));
299 
300       eCharge chh = getCharge(c);
301 
302       if(chh == ecGrounded) {
303         xstack.push_back(make_pair(id, 0));
304         charges[id].lowlink = 0;
305         }
306       else if(chh == ecCharged) {
307         xstack.push_back(make_pair(id, 1));
308         if(from != 1) charges[id].lowlink = 1;
309         }
310 
311       for(cell *c2: adj_minefield_cells(c)) {
312         if(c2->listindex == from) continue;
313         eCharge ct = getCharge(c2);
314         if(conduct(chh, ct))
315           connect(id, c2);
316         }
317 
318       // printf("lowlink of %d [%s] = %d\n", id, dnameof(c->wall), ch.lowlink);
319       if(charges[id].lowlink < charges[from].lowlink)
320         charges[from].lowlink = charges[id].lowlink;
321 
322       if(charges[id].lowlink >= from) {
323         while(xstack.back().first != from || xstack.back().second != id) {
324           // printf("bcc %d,%d\n", xstack.back().first, xstack.back().second);
325           xstack.pop_back();
326           }
327         // printf("bcc %d,%d\n", xstack.back().first, xstack.back().second);
328         xstack.pop_back();
329         // printf("\n");
330         }
331 
332       charges[id].instack = false;
333       }
334     }
335 
affect(cell * c)336   void affect(cell *c) {
337     c->ligon = true;
338     if(c->monst) {
339       if(c->monst == moMetalBeast2 && !c->item)
340         c->item = itFulgurite;
341       killMonster(c, moLightningBolt);
342       }
343     if(isPlayerOn(c)) {
344       killThePlayerAt(moLightningBolt, c, 0);
345       }
346     if(c->wall == waSandstone) {
347       c->wall = waNone, drawParticles(c, winf[waSandstone].color, 16);
348       if(c->land == laStorms)
349         c->item = itFulgurite;
350       }
351     if(c->wall == waRed1) c->wall = waNone;
352     if(c->wall == waRed2) c->wall = waRed1;
353     if(c->wall == waRed3) c->wall = waRed2;
354     if(c->wall == waDeadTroll) c->wall = waCavefloor;
355     if(c->wall == waBigTree || c->wall == waSmallTree)
356       makeflame(c, 10, false);
357     if(c->wall == waExplosiveBarrel)
358       explodeBarrel(c);
359     if(c->wall == waDeadTroll2 || isAlchAny(c) || c->wall == waVinePlant)
360       drawParticles(c, winf[c->wall].color, 16),
361       c->wall = waNone;
362 
363     /* destroy charged walls on the border */
364     if(c->wall == waCharged) forCellEx(c1, c) if(c1->wall == waGrounded)
365       c->wall = waNone;
366     /* if(c->wall == waCharged)
367       c->wall = waMetal; */
368     }
369 
listChargedCells(cell * c,manual_celllister & cl,eCharge last=ecConductor)370   void listChargedCells(cell *c, manual_celllister& cl, eCharge last = ecConductor) {
371     if(cl.listed(c)) return;
372     eCharge here = getCharge(c);
373     /* if(c->cpdist <= 2) {
374       printf("monst=%s ", dnameof(c->monst));
375       printf("wall=%s ", dnameof(c->wall));
376       printf("c=%p here=%d last=%d\n", c, here, last);
377       } */
378     if(here == ecIsolator) cl.add(c);
379     if(!conduct(last, here)) return;
380     if(here == ecCharged) chargecells.push_back(c);
381     cl.add(c);
382     for(int i=0; i<c->type; i++) {
383       cell *c2 = c->move(i);
384       if(c2) listChargedCells(c2, cl, here);
385       }
386     }
387 
init()388   EX void init() {
389     chargecells.clear();
390     if(!haveelec && !afterOrb) return;
391     if(1) {
392       manual_celllister cl;
393       for(int i=0; i<isize(dcal); i++) listChargedCells(dcal[i], cl);
394       }
395 
396     charges.resize(2);
397     charges[0].lowlink = 0; charges[1].lowlink = 1;
398     if(!havecharge) return;
399 
400     xstack.clear();
401 
402     for(int i=0; i<isize(chargecells); i++)
403       connect(1, chargecells[i]);
404 
405     havethunder = charges[1].lowlink == 0;
406     if(havethunder) {
407       for(int i=0; i<isize(xstack); i++) {
408         int k = xstack[i].first;
409         int l = xstack[i].second;
410         // printf("connected %d-%d\n", k, l);
411         charges[k].fire = true;
412         charges[l].fire = true;
413         }
414       }
415     }
416 
fire()417   void fire() {
418     if(havethunder) {
419       addMessage(XLAT("There is a flash of thunder!"));
420       playSound(NULL, "storm");
421       drawLightning();
422       for(int i=2; i<isize(charges); i++) if(charges[i].fire)
423         affect(charges[i].c);
424       }
425     }
426 
cleanup()427   EX void cleanup() {
428     for(int i=2; i<isize(charges); i++)
429       charges[i].c->listindex = charges[i].otmp;
430     charges.resize(0);
431     }
432 
draw(cell * c,eCharge what)433   void draw(cell *c, eCharge what) {
434     if(c->ligon) return;
435     c->ligon = true;
436     for(int i=0; i<c->type; i++) {
437       cell *c2 = c->move(i);
438       if(!c2) continue;
439       eCharge ch = getCharge(c2);
440       if(conduct(what, ch))
441         draw(c2, ch);
442       }
443     }
444 
drawcharges()445   void drawcharges() {
446     for(int i=0; i<isize(dcal); i++)
447       if(getCharge(dcal[i]) == ecCharged)
448         draw(dcal[i], ecCharged);
449     }
450 
affected(cell * c)451   EX bool affected(cell *c) {
452     if(c->listindex >= 0 && c->listindex < isize(charges) && charges[c->listindex].c == c)
453       return charges[c->listindex].fire;
454     return false;
455     }
456 
457   #if HDR
458   struct builder {
builderhr::elec::builder459     builder() { init(); }
~builderhr::elec::builder460     ~builder() { cleanup(); }
461     };
462   #endif
463 
act()464   EX void act() {
465     int k = tkills();
466     for(int i=0; i<numplayers(); i++)
467       if(multi::playerActive(i) && isElectricLand(playerpos(i)) && !afterOrb)
468         markOrb(itOrbShield), markOrb(itOrbAether);
469     builder b;
470     fire();
471     if(!afterOrb)
472       achievement_count("ELEC", tkills() - k, 0);
473     }
474 
475   // 0 = no close escape, 1 = close escape, 2 = message already shown
476   EX int lightningfast;
checklightningfast()477   EX void checklightningfast() {
478     changes.value_keep(lightningfast);
479     if(lightningfast == 1) {
480       addMessage(XLAT("Wow! That was close."));
481       lightningfast = 2;
482       }
483     if(lightningfast > 1) return;
484     builder b;
485     for(int i=0; i<numplayers(); i++)
486       if(multi::playerActive(i) && elec::affected(playerpos(i)))
487         lightningfast = 1;
488     }
489 
490 
491   EX }
492 
493 EX namespace princess {
494 
495 #if HDR
496 #define OUT_OF_PRISON 200
497 #define OUT_OF_PALACE 250
498 #define PRADIUS0 (141)
499 #define PRADIUS1 (150)
500 
501 struct info {
502   int id;         // id of this info
503   cell *prison;   // where was the Princess locked
504   heptagon *alt;  // alt of the prison
505   int bestdist;   // best dist achieved
506   int bestnear;   // best dist achieved, by the player
507   int value;      // number of Rugs at 120
508   cell *princess; // where is the Princess currently
509   int together;   // in which turn have we been together -- for the hug animation
510   };
511 
512 #endif
513 
coords()514   EX gp::loc coords() { return gp::loc(39, 21); }
515 
516   EX bool generating = false;
517   EX bool challenge = false;
518   EX bool saved = false;
519   EX bool everSaved = false;
520 
521   EX bool forceVizier = false;
522   EX bool forceMouse = false;
523   EX bool gotoPrincess = false;
524   EX bool nodungeon = false;
525   EX bool squeaked = false;
526 
527   EX int saveHP = 0;
528   EX int saveArmedHP = 0;
529 
530   EX int reviveAt;
531 
532   EX vector<info*> infos;
533 
assign(info * i)534   void assign(info *i) {
535     if(i->alt) i->alt->emeraldval = i->id;
536     }
537 
newInfo(cell * c)538   EX int newInfo(cell *c) {
539     info *i = new info;
540     i->prison = c;
541     i->princess = c;
542     i->alt = c->master->alt;
543     i->id = isize(infos);
544     i->bestdist = 0;
545     i->bestnear = OUT_OF_PRISON;
546     i->together = -INF;
547     infos.push_back(i);
548     assign(i);
549     return i->id;
550     }
551 
newFakeInfo(cell * c)552   EX void newFakeInfo(cell *c) {
553     info *i = new info;
554     i->prison = NULL;
555     i->princess = c;
556     i->alt = NULL;
557     i->id = isize(infos);
558     i->bestdist = items[itSavedPrincess] ? OUT_OF_PALACE : OUT_OF_PRISON;
559     i->bestnear = 0;
560     i->together = -INF;
561     infos.push_back(i);
562     assign(i);
563     }
564 
getPrisonInfo(cell * c)565   EX info *getPrisonInfo(cell *c) {
566     if(euclid || quotient || sphere) return NULL;
567     if(c->land != laPalace) return NULL;
568     if(!c->master->alt) return NULL;
569     int ev = c->master->alt->alt->emeraldval; // NEWYEARFIX
570     if(ev < 0 || ev >= isize(infos)) return NULL;
571     if(infos[ev]->alt != c->master->alt->alt) return NULL;
572     return infos[ev];
573     }
574 
getPrincessInfo(cell * c)575   EX info *getPrincessInfo(cell *c) {
576     for(int i=0; i<isize(infos); i++) if(infos[i]->princess == c) {
577       while(i) {
578         infos[i]->id = i-1; assign(infos[i]);
579         infos[i-1]->id = i; assign(infos[i-1]);
580         swap(infos[i], infos[i-1]);
581         i--;
582         }
583       return infos[i];
584       }
585     return NULL;
586     }
587 
dist(cell * c)588   EX int dist(cell *c) {
589     if(c->land != laPalace && c->land != laDungeon) return OUT_OF_PALACE;
590     else if(quotient || sphere) return OUT_OF_PRISON;
591     else if(euclid) return celldistAlt(c);
592     else if(!c->master->alt) return OUT_OF_PRISON;
593     else return celldistAlt(c);
594     }
595 
clear()596   void clear() {
597     for(int i=0; i<isize(infos); i++) delete infos[i];
598     infos.clear();
599     }
600 
bringBackAt(cell * c)601   bool bringBackAt(cell *c) {
602     if(!c) return false;
603     if(!passable(c, NULL, 0)) return false;
604     c->monst = moPrincessArmed;
605     c->stuntime = 0;
606     c->hitpoints = palaceHP();
607     drawFlash(c);
608     playSound(c, princessgender() ? "heal-princess" : "heal-prince");
609 
610     info *inf = NULL;
611     for(int i=0; i<isize(infos); i++) {
612       if(infos[i]->princess && infos[i]->bestdist == OUT_OF_PALACE && isPrincess(infos[i]->princess->monst))
613         inf = infos[i];
614       }
615     if(inf) { inf->princess->monst = moNone; inf->princess = c; }
616     else newFakeInfo(c);
617     return true;
618     }
619 
bringBack()620   EX void bringBack() {
621     if(bringBackAt(cwt.peek())) return;
622     for(int i=1; i<isize(dcal); i++)
623       if(bringBackAt(dcal[i])) return;
624     }
625 
setdist(info * i,int newdist)626   void setdist(info *i, int newdist) {
627     changes.value_keep(*i);
628     if(newdist < ALTDIST_ERROR && newdist > i->bestdist) {
629       i->bestdist = newdist;
630 //    printf("Improved dist to %d\n", newdist);
631       if(newdist == OUT_OF_PALACE) {
632         if(!princess::saved)
633 #if CAP_INV
634         if(!inv::on || !inv::usedForbidden)
635 #endif
636           achievement_gain_once("PRINCESS1");
637         changes.value_set(princess::saved, true);
638         changes.value_set(princess::everSaved, true);
639         if(inv::on && !princess::reviveAt)
640           changes.value_set(princess::reviveAt, gold(NO_LOVE));
641         items[itSavedPrincess]++;
642         }
643       if(newdist == OUT_OF_PRISON && princess::challenge) {
644         addMessage(XLAT("Congratulations! Your score is %1.", its(i->value)));
645         achievement_gain_once("PRINCESS2");
646         if(!cheater) achievement_score(36, i->value);
647         LATE( showMissionScreen(); )
648         }
649       }
650     if(i->princess->land == laDungeon && !saved && !nodungeon) {
651       addMessage(XLAT("%The1 says, \"not this place, it looks even worse...\"", moPrincess));
652       nodungeon = true;
653       }
654     }
655 
save(cell * princess)656   EX void save(cell *princess) {
657     if(euclid) return;
658     princess::info *i = princess::getPrincessInfo(princess);
659     if(!i || i->bestdist <= 3) changes.ccell(princess), princess->monst = moNone;
660     else if(i) setdist(i, OUT_OF_PRISON);
661     }
662 
move(const movei & mi)663   EX void move(const movei& mi) {
664     auto& cf = mi.s;
665     auto& ct = mi.t;
666     if(euclid) return;
667     princess::info *i = princess::getPrincessInfo(cf);
668     if(!i) {
669       // note: OK if mapediting or loading
670       printf("Warning: unknown princess\n");
671       if(!cheater)
672         addMessage("Warning: unknown princess (that's a bug, please report)");
673       newFakeInfo(ct);
674       }
675     else {
676       changes.value_keep(*i);
677       i->princess = ct;
678       setdist(i, dist(ct));
679 
680       forCellIdEx(cp, j, ct) if(isPlayerOn(cp)) {
681         bool safe = true;
682         for(cell *test: {ct, cp}) forCellEx(c1, test) forCellEx(c2, test)
683           if(isActiveEnemy(c1, moPlayer) || isActiveEnemy(c2, moPlayer))
684             safe = false;
685         if(safe) {
686           if(turncount > i->together + 20) {
687             animateHug(movei(ct, j), LAYER_SMALL);
688             animateHug(movei(ct, j).rev(), LAYER_SMALL);
689             }
690           i->together = turncount;
691           }
692         }
693       }
694     }
695 
mouseSqueak(cell * c)696   EX void mouseSqueak(cell *c) {
697     eMonster m = c->monst;
698     info *i = getPrisonInfo(c);
699     int d = dist(c);
700 
701     playSound(c, "mousesqueak", 40);
702     if(!i)
703        addMessage(XLAT("%The1 squeaks in a confused way.", m));
704     else if(i->bestdist >= 6)
705        addMessage(XLAT("%The1 squeaks gratefully!", m));
706     else if(!i->princess)
707        addMessage(XLAT("%The1 squeaks hopelessly.", m));
708     else if(d > 120)
709        addMessage(XLAT("%The1 squeaks in despair.", m));
710     else if(d > 90)
711        addMessage(XLAT("%The1 squeaks sadly.", m));
712     else if(d > 60)
713        addMessage(XLAT("%The1 squeaks with hope!", m));
714     else if(d > 30)
715        addMessage(XLAT("%The1 squeaks happily!", m));
716     else
717        addMessage(XLAT("%The1 squeaks excitedly!", m));
718     }
719 
line(cell * c)720   EX void line(cell *c) {
721     int d = (euclid || c->master->alt) ? celldistAlt(c) : 200;
722     eMonster m = c->monst;
723 
724     static int msgid = 0;
725     changes.value_keep(msgid);
726 
727     playSound(c, princessgender() ? "speak-princess" : "speak-prince");
728     retry:
729     if(msgid >= 127) msgid = 0;
730 
731     bool inpalace = c->land == laPalace || c->land == laDungeon;
732 
733     if(msgid == 0 && d < 20 && inpalace) {
734       addMessage(XLAT("%The1 kisses you, and begs you to bring %him1 away from here.", m));
735       }
736     else if(msgid == 1 && d >= 20 && inpalace && !peace::on) {
737       if(m == moPrincess)
738         addMessage(XLAT("\"I want my revenge. Stun a guard and leave him for me!\"", m));
739       else
740         addMessage(XLAT("\"That felt great. Thanks!\"", m));
741       }
742     else if(msgid  == 2 && d >= 70 && inpalace) {
743       addMessage(XLAT("\"Bring me out of here please!\"", m));
744       }
745     else if(msgid == 3 && !inpalace) {
746       addMessage(XLAT("%The1 kisses you, and thanks you for saving %him1.", m));
747       }
748     else if(msgid == 4 && !inpalace && m == moPrincess && !peace::on) {
749       addMessage(XLAT("\"I have been trained to fight with a Hypersian scimitar, you know?\"", m));
750       }
751     else if(msgid == 16 && !inpalace) {
752       addMessage(XLAT("\"I would love to come to your world with you!\"", m));
753       }
754     else if(msgid == 20 && !inpalace) {
755       addMessage(XLAT("\"I do not like butterflies. They are treacherous.\"", m));
756       }
757     else if(msgid == 32 && !inpalace) {
758       addMessage(XLAT("\"Straight lines stay close to each other forever, this is so romantic!\"", m));
759       }
760     else if(msgid == 40 && !inpalace) {
761       addMessage(XLAT("\"I hate roses.\"", m));
762       }
763     else if(msgid == 48 && !inpalace) {
764       addMessage(XLAT("\"Maps... Just like the world, but smaller... how is that even possible?!\"", m));
765       }
766     else if(msgid == 64) {
767       addMessage(XLAT("\"In this world there is plenty of space for everyone. We do not need wars.\"", m));
768       }
769     else if(msgid == 65) {
770       addMessage(XLAT("\"Only the stupid hyperbugs do not understand this.\"", m));
771       }
772     else if(msgid == 72 && !inpalace) {
773       addMessage(XLAT("\"I have once talked to a Yendorian researcher... he was only interested in infinite trees.\"", m));
774       }
775     else if(msgid == 73 && !inpalace) {
776       addMessage(XLAT("\"Infinite trees are boring. I prefer other graphs.\"", m));
777       }
778     else if(msgid == 74 && !inpalace) {
779       addMessage(XLAT("\"Did you know that the Cultists are relatives of the Desert Men?\"", m));
780       }
781     else if(msgid == 80) {
782       addMessage(XLAT("\"Are there Temples of Cthulhu in your world? Why not?\"", m));
783       }
784     else {
785       msgid++; goto retry;
786       }
787 
788     msgid++;
789     }
790 
playernear(cell * c)791   EX void playernear(cell *c) {
792     info *i = getPrisonInfo(c);
793     int d = dist(c);
794     // if(i) printf("d=%d bn=%d\n", d, i->bestnear);
795     if(i && d < i->bestnear) {
796       changes.value_keep(*i);
797       if(i->bestnear > 100 && d <= 100) {
798         i->value = items[itPalace];
799         if(princess::challenge)
800           addMessage(XLAT("Hardness frozen at %1.", its(i->value)));
801         }
802       i->bestnear = d;
803       }
804     }
805 
806 EX }
807 
808 EX namespace clearing {
809 
810   #if HDR
811   struct clearingdata {
812     cell *root;
813     int dist;
814     bool buggy;
clearingdatahr::clearing::clearingdata815     clearingdata() { root = nullptr; }
816     };
817   #endif
818 
819   EX std::map<heptagon*, clearingdata> bpdata;
820 
821   EX cell *current_root;
822 
new_root()823   EX void new_root() {
824     if(!current_root || current_root->monst != moMutant) {
825       auto& ac = currentmap->allcells();
826       int iter = 0;
827       while(!current_root || pseudohept(current_root) || current_root->cpdist < 3) {
828         if(iter++ > 100) return;
829         current_root = ac[hrand(isize(ac))];
830         }
831       current_root->monst = moMutant;
832       current_root->mondir = NODIR;
833       current_root->stuntime = (mutantphase + 1) & 15;
834       }
835     }
836 
plantdir(cell * c)837   int plantdir(cell *c) {
838     if(have_alt(c))
839       gen_alt_around(c);
840     int d = celldistAlt(c);
841 
842     if(PURE) {
843       forCellIdCM(c2, i, c) {
844         if(!pseudohept(c2) && celldistAlt(c2) == d-1)
845           return i;
846         }
847       forCellIdCM(c2, i, c) {
848         if(celldistAlt(c2) == d-1)
849           return geometry == gBinary4 ? i : (i+1) % c->type;
850         }
851       }
852 
853     forCellIdCM(c2, i, c) {
854       if(!pseudohept(c2) && celldistAlt(c2) == d-1)
855         return i;
856       }
857 
858     int quseful = 0, tuseful = 0, tuseful2 = 0;
859 
860     forCellIdCM(c2, i, c) if(!pseudohept(c2)) {
861       if(celldistAlt(c2) == d) {
862         bool useful = false;
863         for(int j=1; j<S6; j++) {
864           cell *c3 = createMov(c2, j);
865           if(celldistAlt(c3) == d-1)
866             useful = true;
867           }
868         if(useful) quseful++, tuseful += (1<<i), tuseful2 = i;
869         }
870       }
871     if(quseful == 1) return tuseful2;
872     if(quseful == 2) {
873       int i;
874       if(tuseful == (1<<3)+(1<<5)) i = 3;
875       if(tuseful == (1<<5)+(1<<1)) i = 5;
876       if(tuseful == (1<<1)+(1<<3)) i = 1;
877       if(tuseful == (1<<5)+(1<<7)) i = 5;
878       if(tuseful == (1<<7)+(1<<1)) i = 7;
879       if((d & 7) < 4) i = (i+2) % c->type;
880       return i;
881       }
882     printf("error in plantdir\n");
883     return 1;
884     }
885 
886   vector<cell*> onpath;
887   vector<int>   pdir;
888   vector<cell*> rpath;
889 
generate(cell * c)890   EX void generate(cell *c) {
891     if(euclid) {
892       if(quotient) return; // fix cylinder
893       if(pseudohept(c)) return;
894       c->monst = moMutant;
895 
896       auto co = euc2_coordinates(c);
897       int x = co.first, y = co.second;
898 
899       int xco = x * 2 + y + 1;
900       c->stuntime = (8-xco/2) & 15;
901       // 2, 4, 5, 7
902 
903       if(pseudohept(createMov(c, 0)))
904         c->mondir = 1 + hrand(2) * 4;
905       else
906         c->mondir = 0;
907       return;
908       }
909 
910     if(!eubinary && !horo_ok()) return;
911     // cell *oc = c;
912     gen_alt(c);
913     if(pseudohept(c)) return;
914     heptagon *a = eubinary ? NULL : c->master->alt->alt;
915     clearingdata& bd(bpdata[a]);
916     if(!bd.root) { bd.root = c; bd.dist = 8; bd.buggy = false; }
917     if(bd.buggy) return;
918 
919     onpath.clear(); pdir.clear(); rpath.clear();
920 
921     int steps = 0;
922 
923     int ds;
924 
925     int stepcount = 0;
926     while(true) {
927       if(c == bd.root) {ds = bd.dist; break; }
928 
929       // printf("R %4d C %4d\n", celldistAlt(bd.root), celldistAlt(c));
930       if(celldistAlt(c) > celldistAlt(bd.root)) {
931         stepcount++;
932         if(stepcount > iteration_limit) {
933           printf("buggy #1\n");
934           bd.buggy = true;
935           return;
936           }
937 
938         if(c->mpdist <= 6) {
939           if(c->monst != moMutant) return; // already cut!
940           // ... else simply extend it
941           ds = c->stuntime; break;
942           }
943 
944         int d = plantdir(c);
945         steps++;
946         onpath.push_back(c); pdir.push_back(d);
947         // printf("c [%4d] %p -> %p\n", celldistAlt(c), c, c->move(d));
948         c = c->move(d);
949         }
950       else {
951         bd.dist--;
952         if(bd.dist < -iteration_limit) {
953           for(int i=0; i<steps; i++)
954             onpath[i]->item = itBuggy;
955           for(int i=0; i<(int) rpath.size(); i++)
956             rpath[i]->item = itBuggy;
957           printf("buggy #2\n");
958           bd.buggy = true;
959           return;
960           }
961         rpath.push_back(bd.root);
962         // printf("r [%4d] %p -> %p\n", celldistAlt(bd.root), bd.root, bd.root->move(plantdir(bd.root)));
963         bd.root = createMov(bd.root, plantdir(bd.root));
964         }
965       }
966 
967     // printf("steps = %d dist = %d [%d]\n", steps, bd.dist, oc->mpdist);
968 
969     onpath.push_back(c); pdir.push_back(plantdir(c));
970     while(steps >= 0) {
971       c = onpath[steps];
972       if(steps == 0) {
973         c->monst = moMutant;
974         c->mondir = pdir[steps];
975         if(pdir[steps] != plantdir(c)) {
976           printf("pdir i/ plantdir\n");
977           exit(1);
978           }
979         c->stuntime = ds & 15;
980         }
981       if(c->mpdist <= 7 && c->monst != moMutant)
982         break;
983       steps--; ds++;
984       }
985     }
986 
987   /** cells with the same celltype are likely to have the same number of descendant leaves */
988   typedef tuple<int, int, int, int> celltype;
989 
990   /** stats about the number of descendant leaves for each celltype */
991   map<celltype, pair<bignum, int> > stats;
992 
993   /** the total number of leaves killed, approximated from the actual numbers and Clearing structure */
994   EX bignum imputed;
995 
996   /** the total number of leaves killed directly */
997   EX int direct;
998 
999   map<cell*, pair<bignum, int> > score;
1000 
get_celltype(cell * c)1001   celltype get_celltype(cell *c) {
1002     cell *c1 = c;
1003     if(c->mondir < c->type)
1004       c1 = c->move(c->mondir);
1005     return make_tuple(
1006       celldistAlt(c), type_in(expansion, c, celldistAlt),
1007       celldistAlt(c1), type_in(expansion, c1, celldistAlt)
1008       );
1009     }
1010 
imput(cell * c)1011   EX void imput(cell *c) {
1012     if(bounded) return;
1013     if(score.count(c)) return;
1014     changes.map_value(score, c);
1015     auto& is = score[c];
1016     celltype t = get_celltype(c);
1017     changes.map_value(stats, t);
1018     auto& stat = stats[t];
1019     is.second = c->mondir;
1020     if(c->mpdist <= 6) {
1021       is.first = 1;
1022       forCellEx(c2, c) if(score.count(c2) && c2->move(score[c2].second) == c)
1023         is.first += score[c2].first;
1024       stat.first += is.first;
1025       stat.second++;
1026       }
1027     else {
1028       is.first = stat.second ? stat.first.randomized_div(stat.second) : bignum(1);
1029       imputed += is.first;
1030       }
1031     }
1032 
1033 EX }
1034 
1035 EX namespace whirlpool {
1036 
1037   EX bool escaped = false; // escaped the Whirlpool?
1038 
1039   // next == +1 -> next
1040   // next == -1 -> prev
get(cell * c,int next)1041   EX cell *get(cell *c, int next) {
1042     int i = 0;
1043     if(!eubinary && !c->master->alt) return NULL;
1044     int d = celldistAlt(c);
1045     int d2;
1046     while(true) {
1047       if(i == c->type) return NULL;
1048       if(c->move(i) && (d2 = celldistAlt(c->move(i))) != d)
1049         break;
1050       i++;
1051       }
1052     if(i == c->type) return NULL;
1053     if(d>d2) next = -next;
1054     for(int j=1; j<c->type; j++) {
1055       cell *c2 = c->modmove(i+next*j);
1056       if(celldistAlt(c2) == d) return c2;
1057       }
1058     return NULL;
1059     }
1060 
build(vector<cell * > & whirlline,int d)1061   void build(vector<cell*>& whirlline, int d) {
1062     again:
1063     cell *at = whirlline[isize(whirlline)-1];
1064     cell *prev = whirlline[isize(whirlline)-2];
1065     for(int i=0; i<at->type; i++)
1066       if(at->move(i) && (eubinary || at->move(i)->master->alt) && celldistAlt(at->move(i)) == d && at->move(i) != prev) {
1067         if(at->move(i) == whirlline[0]) return; // loops in weird geometries?
1068         if(at->move(i) == whirlline[isize(whirlline)/2]) return; // even weirder geometry?
1069         whirlline.push_back(at->move(i));
1070         goto again;
1071         }
1072     }
1073 
generate(cell * wto)1074   EX void generate(cell *wto) {
1075     if(wto->wall == waBoat || wto->monst)
1076       return;
1077 
1078     wto->wall = waSea;
1079 
1080     if(hrand(35000) < 40 + items[itWhirlpool] + yendor::hardness())
1081       wto->monst = moCShark;
1082     else if(hrand(5000) < 500)
1083        wto->wall = waBoat;
1084 
1085     if(wto->wall == waBoat && (eubinary || wto->master->alt)) {
1086       int d = celldistAlt(wto);
1087       if(yendor::on) d -= 200;
1088       // 250 : hard
1089       if(hrand(5000) < 60 + 3 * items[itWhirlpool] + yendor::hardness())
1090         wto->monst = moPirate;
1091       if(hrand(5000) < 20 && d < -20 && !tactic::on && !inv::on)
1092         wto->item = itOrbSafety;
1093       else if(hrand(5000) < 20 && d < -20 && !tactic::on && markOrb(itOrbLuck))
1094         wto->item = itOrbSafety;
1095       else if(hrand(5000) < 20*PRIZEMUL && d < -20)
1096         placePrizeOrb(wto);
1097       else if(!inv::on && items[itWhirlpool] >= 10 && hrand(5000) < 20 && d < -15)
1098         wto->item = itOrbWater;
1099       else if(d<-10 && hrand(5000) < 1000-d)
1100         wto->item = itWhirlpool;
1101       }
1102     }
1103 
whirlMove(cell * wto,cell * wfrom)1104   void whirlMove(cell *wto, cell *wfrom) {
1105     // monsters don't move
1106     if(wfrom && (isPlayerOn(wfrom) || wfrom->monst))
1107       return;
1108     // disappear
1109     if(!wto) { wfrom->wall = waSea; wfrom->item = itNone; }
1110 
1111     if(wfrom && wto && wfrom->wall == waBoat && wto->wall == waSea && !wto->monst) {
1112       wfrom->wall = waSea; wto->wall = waBoat;
1113       wto->mondir = neighborId(wto, wfrom);
1114       animateMovement(moveimon(wto).rev(), LAYER_BOAT);
1115       }
1116 
1117     if(wfrom && wto && wfrom->item && !wto->item && wfrom->wall != waBoat) {
1118       // Keys and Orbs of Yendor never disappear!
1119       if(wfrom->item == itKey || wfrom->item == itOrbYendor)
1120         for(int i=0; i<wto->type; i++) createMov(wto, i);
1121       moveItem(wfrom, wto, false);
1122       pickupMovedItems(wfrom);
1123       pickupMovedItems(wto);
1124       }
1125 
1126     if(wto && !wfrom)
1127       generate(wto);
1128     }
1129 
moveAt(cell * c,manual_celllister & cl)1130   void moveAt(cell *c, manual_celllister& cl) {
1131     if(c->land != laWhirlpool) return;
1132     if(cl.listed(c)) return;
1133     if(!(eubinary || c->master->alt)) return;
1134     cell *c2 = get(c, 1);
1135     if(!c2) return;
1136     int d = celldistAlt(c);
1137     vector<cell*> whirlline;
1138     whirlline.push_back(c);
1139     whirlline.push_back(c2);
1140     build(whirlline, d);
1141     reverse(whirlline.begin(), whirlline.end());
1142     build(whirlline, d);
1143     int z = isize(whirlline);
1144 
1145     for(int i=0; i<z; i++)
1146       cl.add(whirlline[i]);
1147 
1148     whirlMove(NULL, whirlline[0]);
1149 
1150     for(int i=0; i<z-1; i++)
1151       whirlMove(whirlline[i], whirlline[i+1]);
1152 
1153     whirlMove(whirlline[z-1], NULL);
1154     }
1155 
move()1156   EX void move() {
1157     manual_celllister cl;
1158     for(int i=0; i<isize(dcal); i++) {
1159       cell *c = dcal[i];
1160       moveAt(c, cl);
1161       }
1162     // Keys and Orbs of Yendor always move
1163     using namespace yendor;
1164     for(int i=0; i<isize(yi); i++) {
1165       moveAt(yi[i].path[0], cl);
1166       moveAt(yi[i].path[YDIST-1], cl);
1167       }
1168     }
1169   EX }
1170 
1171 EX namespace mirror {
1172 
1173   #if HDR
1174   static const int SPINSINGLE = 1;
1175   static const int SPINMULTI = 2;
1176   static const int GO = 4;
1177   static const int ATTACK = 8;
1178   #endif
1179 
build(cell * c)1180   EX bool build(cell *c) {
1181     if(kite::in() || sol) return false;
1182     #if CAP_GP
1183     if(GOLDBERG) {
1184       if(c == c->master->c7) {
1185         c->wall = ((gp::param.second == 0 || gp::param.first == gp::param.second) && hrand(2)) ? waMirror : waCloud;
1186         return true;
1187         }
1188       return false;
1189       }
1190     #endif
1191     #if CAP_ARCM
1192     if(arcm::in()) {
1193       c->wall = hrand(2) ? waMirror : waCloud;
1194       return true;
1195       }
1196     #endif
1197     if(bt::in() || IRREGULAR) {
1198       // mirrors not supported
1199       if(is_mirrorland(c)) {
1200         c->item = itShard;
1201         return true;
1202         }
1203       return false;
1204       }
1205     if(PURE?pseudohept(c):!ishept(c)) {
1206       c->wall = hrand(2) ? waMirror : waCloud;
1207       return true;
1208       }
1209     return false;
1210     }
1211 
1212   EX vector<pair<int, cellwalker>> mirrors;
1213   #if HDR
1214   constexpr int LIGHTNING = -1; // passed instead of cpid
1215   #endif
1216 
noMirrorOn(cell * c)1217   bool noMirrorOn(cell *c) {
1218     return c->monst || (!shmup::on && isPlayerOn(c)) || (!bounded && c->cpdist > gamerange());
1219     }
1220 
cellMirrorable(cell * c)1221   bool cellMirrorable(cell *c) {
1222     if(noMirrorOn(c)) return false;
1223     return
1224       c->wall == waNone || c->wall == waCavefloor || isAlchAny(c) ||
1225       c->wall == waFrozenLake || c->wall == waDeadfloor || c->wall == waDeadfloor2 ||
1226       c->wall == waGiantRug || c->wall == waCIsland || c->wall == waCIsland2 ||
1227       c->wall == waGargoyleFloor || c->wall == waRubble ||
1228       c->wall == waGargoyleBridge || c->wall == waTempFloor || c->wall == waTempBridge ||
1229       c->wall == waMirrorWall || c->wall == waPetrifiedBridge;
1230     }
1231 
destroyKilled()1232   void destroyKilled() {
1233     int j = 0;
1234     for(int i=0; i<isize(mirrors); i++)
1235       if(mirrors[i].second.at->monst == moMimic)
1236         mirrors[j++] = mirrors[i];
1237     mirrors.resize(j);
1238     }
1239 
unlist()1240   void unlist() {
1241     for(auto& m: mirrors)
1242       if(m.second.at->monst == moMimic)
1243         changes.ccell(m.second.at),
1244         m.second.at->monst = moNone;
1245     }
1246 
list()1247   void list() {
1248     for(auto& m: mirrors)
1249       changes.ccell(m.second.at),
1250       m.second.at->monst = moMimic;
1251     }
1252 
destroyAll()1253   EX void destroyAll() {
1254     changes.value_keep(mirrors);
1255     unlist();
1256     mirrors.clear();
1257     }
1258 
createMirror(cellwalker cw,int cpid)1259   EX void createMirror(cellwalker cw, int cpid) {
1260     if(!shmup::on && inmirror(cw))
1261       cw = reflect(cw);
1262     changes.ccell(cw.at);
1263     if(cpid == LIGHTNING)
1264       castLightningBolt(cw);
1265     else if(cellMirrorable(cw.at)) {
1266       for(auto& m: mirrors)
1267         if(m == make_pair(cpid,cw))
1268           return;
1269       mirrors.emplace_back(cpid, cw);
1270       }
1271     }
1272 
1273   #if CAP_ARCM
1274   // we go by heptagons in Archimedean,
equal(heptspin h1,heptspin h2,int lev)1275   bool equal(heptspin h1, heptspin h2, int lev) {
1276     if(h1.at->degree() != h2.at->degree()) return false;
1277     if(arcm::is_vertex(h1.at) != arcm::is_vertex(h2.at)) return false;
1278     if(lev) for(int i=0; i<h1.at->degree(); i++) {
1279       heptspin h11 = h1 + i + wstep;
1280       heptspin h21 = h2 + i + wstep;
1281       if(!equal(h11, h21, lev-1)) return false;
1282       }
1283     return true;
1284     }
1285 
create_archimedean_rec(heptspin hs,int cpid,heptspin hs0,int lev)1286   int create_archimedean_rec(heptspin hs, int cpid, heptspin hs0, int lev) {
1287     int result = 0;
1288     for(int i=0; i<hs.at->degree(); i++) {
1289       heptspin hs1 = hs + i;
1290       if(lev == 0) {
1291         if(hs1.at != hs0.at && equal(hs1, hs0, 3)) {
1292           createMirror(hs1 + cth, cpid);
1293           result++;
1294           }
1295         }
1296       else result += create_archimedean_rec(hs1 + wstep, cpid, hs0, lev-1);
1297       }
1298     return result;
1299     }
1300 
create_archimedean(cellwalker cw,int cpid,bool mirrored)1301   void create_archimedean(cellwalker cw, int cpid, bool mirrored) {
1302     heptspin hs = cw + cth;
1303     heptspin hsx = hs;
1304     if(mirrored) hsx += wmirror;
1305     if(create_archimedean_rec(hsx, cpid, hs, 2)) return;
1306     if(create_archimedean_rec(hsx, cpid, hs, 3)) return;
1307     if(create_archimedean_rec(hsx, cpid, hs, 4)) return;
1308     }
1309   #endif
1310 
createMirrors(cellwalker cw,int cpid)1311   EX void createMirrors(cellwalker cw, int cpid) {
1312 
1313     if(kite::in() || sol) return;
1314 
1315     #if CAP_ARCM
1316     if(arcm::in()) {
1317       create_archimedean(cw, cpid, true);
1318       return;
1319       }
1320     #endif
1321 
1322     cw.mirrored = !cw.mirrored;
1323     cell *c = cw.at;
1324 
1325     #if MAXMDIM >= 4
1326     if(WDIM == 3 && !bt::in()) {
1327       if(shmup::on) for(int i=0; i<cw.at->type; i++)
1328         createMirror(cw + i + wstep - i, cpid);
1329       return;
1330       }
1331     #endif
1332 
1333     #if CAP_GP
1334     if(GOLDBERG) {
1335       for(int i=0; i<cw.at->type; i++) {
1336         createMirror(cw + cth + i + wstep + i - (gp::param.first == gp::param.second ? 1 : 0) + cth, cpid);
1337         }
1338       return;
1339       }
1340     #endif
1341     for(int i=0; i<cw.at->type; i++) {
1342       auto cws = cw + wstep;
1343       if(cws.at->type == c->type)
1344         createMirror(cws+i, cpid);
1345       cw += 1;
1346       }
1347     }
1348 
createMirages(cellwalker cw,int cpid)1349   EX void createMirages(cellwalker cw, int cpid) {
1350     #if CAP_ARCM
1351     if(arcm::in()) {
1352       create_archimedean(cw, cpid, false);
1353       return;
1354       }
1355     #endif
1356     #if MAXMDIM >= 4
1357     if(WDIM == 3 && !bt::in()) {
1358       if(shmup::on) for(int i=0; i<cw.at->type; i++)
1359         createMirror(cw + i + wstep - i, cpid);
1360       return;
1361       }
1362     #endif
1363     #if CAP_GP
1364     if(GOLDBERG && !(S7 & 1)) {
1365       for(int i=0; i<cw.at->type; i++) {
1366         createMirror(cw + cth + i + wstep + 1 + wstep + 1 + (S7/2) - i + 1 + cth, cpid);
1367         }
1368       return;
1369       }
1370     if(GOLDBERG && (S7 & 1)) {
1371       for(int i=0; i<cw.at->type; i++) {
1372         createMirror(cw + cth + i + wstep + (S7/2) + wstep - 2 + wstep + (S7/2) - i + cth, cpid);
1373         }
1374       return;
1375       }
1376     #endif
1377     if(PURE && !(S7 & 1)) {
1378       for(int i=0; i<cw.at->type; i++)
1379         createMirror(cw + i + wstep + 1 + wstep + 1 + (S7/2) - i, cpid);
1380       return;
1381       }
1382     if(PURE && (S7 & 1) && (S3 == 4)) {
1383       for(int i=0; i<cw.at->type; i++)
1384         createMirror(cw + i + wstep + 1 + wstep - (S7/2) + wstep - (S7/2) - i, cpid);
1385       return;
1386       }
1387     if(PURE && (S7 & 1)) {
1388       for(int i=0; i<cw.at->type; i++)
1389         createMirror(cw + i + wstep + (S7/2) + wstep - 2 + wstep + (S7/2) - i, cpid);
1390       return;
1391       }
1392     for(int i=0; i<S6; i++) {
1393       auto cw0 = cw + i + wstep;
1394       if(!ctof(cw0.at)) {
1395         createMirror(cw0 + 2 + wstep - (2+i), cpid);
1396         createMirror(cw0 - 2 + wstep + (2-i), cpid);
1397         }
1398       }
1399     }
1400 
createHere(cellwalker cw,int cpid)1401   EX void createHere(cellwalker cw, int cpid) {
1402     if(!cw.at) return;
1403     if(cw.at->wall == waCloud)
1404       createMirages(cw, cpid);
1405     if(cw.at->wall == waMirror)
1406       createMirrors(cw, cpid);
1407     }
1408 
breakMirror(cellwalker cw,int pid)1409   EX void breakMirror(cellwalker cw, int pid) {
1410     if(!cw.at) return;
1411     cell *c = cw.at;
1412     if(c->wall == waMirror || c->wall == waCloud) {
1413       drawParticles(c, winf[c->wall].color, 16);
1414       playSound(c, "pickup-mirror", 50);
1415       if(pid >= 0 && (cw.at->land == laMirror || cw.at->land == laMirrorOld)) {
1416         dynamicval<int> d(multi::cpid, pid);
1417         gainShard(cw.at, c->wall == waMirror ? "The mirror shatters!" : "The cloud turns into a bunch of images!");
1418         }
1419       changes.ccell(c);
1420       c->wall = waNone;
1421       }
1422     }
1423 
isKilledByMirror(cell * c)1424   EX bool isKilledByMirror(cell *c) {
1425     for(auto& m: mirrors) {
1426       cell *c1 = (m.second + wstep).at;
1427       if(inmirror(c1)) c1 = reflect(cellwalker(c1, 0, false)).at;
1428       if(c1 == c && canAttack(m.second.at, moMimic, c, c->monst, 0))
1429         return true;
1430       }
1431     return false;
1432     }
1433 
go(bool fwd)1434   void go(bool fwd) {
1435     changes.value_keep(mirrors);
1436     int tk = tkills();
1437     int nummirage = 0;
1438     int j = 0;
1439 
1440     for(int i=0; i<isize(mirrors); i++) {
1441       auto& m = mirrors[i];
1442       bool survive = true;
1443       if(m.first == multi::cpid) {
1444         cell *c = m.second.at;
1445         changes.ccell(c);
1446         if(!m.second.mirrored) nummirage++;
1447         auto cw2 = m.second + wstep;
1448         if(inmirror(cw2)) cw2 = reflect(cw2);
1449         cell *c2 = cw2.at;
1450         changes.ccell(c2);
1451         if(c2->monst) {
1452           c->monst = moMimic;
1453           eMonster m2 = c2->monst;
1454           if(!peace::on && canAttack(c,moMimic,c2,m2, 0)) {
1455             attackMonster(c2, AF_NORMAL | AF_MSG, moMimic);
1456             if(!fwd) produceGhost(c2, m2, moMimic);
1457             sideAttack(c, m.second.spin, m2, 0);
1458             }
1459           c->monst = moNone;
1460           }
1461         if(c2->wall == waBigTree)
1462           c2->wall = waSmallTree;
1463         else if(c2->wall == waSmallTree)
1464           c2->wall = waNone;
1465         if(fwd) {
1466           if(noMirrorOn(c2) || !passable_for(moMimic, c2, c, P_MONSTER | P_MIRROR | P_MIRRORWALL)) {
1467             survive = false;
1468             continue;
1469             }
1470           c->monst = moMimic;
1471           moveMonster(m.second);
1472           c2->monst = moNone;
1473           empathyMove(m.second);
1474           m.second = cw2;
1475           }
1476         }
1477       if(survive) {
1478         mirrors[j++] = m;
1479         }
1480       }
1481     mirrors.resize(j);
1482     achievement_count("MIRRORKILL", tkills(), tk);
1483     achievement_count("MIRAGE", nummirage, 0);
1484     }
1485 
act(int d,int flags)1486   EX void act(int d, int flags) {
1487     changes.value_keep(mirrors);
1488     destroyKilled();
1489     unlist();
1490     if(multi::players == 1) multi::cpid = 0;
1491     bool spinning =
1492       flags & (multi::players > 1 ? SPINMULTI : SPINSINGLE);
1493     if(spinning && d) {
1494       for(auto& m: mirrors)
1495         if(m.first == multi::cpid)
1496           m.second += d;
1497       }
1498     if(flags & ATTACK)
1499       go(flags & GO);
1500     list();
1501     }
1502 
breakAll()1503   EX void breakAll() {
1504     changes.value_keep(mirrors);
1505     destroyKilled();
1506     unlist();
1507     if(numplayers() == 1)
1508       createHere(cwt, 0);
1509     else for(int i=0; i<numplayers(); i++)
1510       createHere(multi::player[i], i);
1511     for(int i=0; i<isize(mirrors); i++)
1512       createHere(mirrors[i].second, mirrors[i].first);
1513     if(numplayers() == 1)
1514       breakMirror(cwt, 0);
1515     else for(int i=0; i<numplayers(); i++)
1516       breakMirror(multi::player[i], i);
1517     for(int i=0; i<isize(mirrors); i++)
1518       breakMirror(mirrors[i].second, -1);
1519     list();
1520     }
1521 
mirrordir(cell * c)1522   EX int mirrordir(cell *c) {
1523     if(c->type == 7) return c->bardir;
1524 
1525    int icount = 0, isum = 0;
1526    for(int i=0; i<6; i+=2) {
1527      if(createMov(c, i)->bardir == c->c.spin(i))
1528        icount++, isum+=i;
1529      }
1530    if(icount != 1) return -1;
1531    return isum;
1532    }
1533 
traceback(vector<int> & v,cellwalker cw)1534   pair<bool, cellwalker> traceback(vector<int>& v, cellwalker cw) {
1535     bool goout = false;
1536     for(int i=isize(v)-1; i>=0; i--) {
1537       if(v[i]) cw -= v[i];
1538       else {
1539         cw += wstep;
1540         if(cw.at->land == laMirrorWall || cw.at->land == laMirror) goout = true;
1541         }
1542       }
1543     return make_pair(goout, cw);
1544     }
1545 
depth(cell * c)1546   int depth(cell *c) { return c->landparam & 255; }
1547 
reflect0(cellwalker cw)1548   cellwalker reflect0(cellwalker cw) {
1549     int stepcount = 0;
1550     cellwalker cwcopy = cw;
1551     static vector<int> v;
1552     v.clear();
1553     while(true) {
1554       if(!inmirror(cw)) break;
1555       stepcount++; if(stepcount > 10000) {
1556          return cw;
1557          }
1558       cell *c0 = (cw+wstep).at;
1559       int go = 0;
1560       if(!inmirror(c0)) go = 2;
1561       else if(depth(c0) && depth(c0) < depth(cw.at)) go = 1;
1562       if(go) {
1563         v.push_back(0);
1564         cw += wstep;
1565         if(go == 2) break;
1566         }
1567       else {
1568         v.push_back(1);
1569         cw += 1;
1570         }
1571       }
1572     if(cw.at->land == laMirrorWall || cw.at->land == laMirrorWall2) {
1573       if(cw.at->type == 7) {
1574         while(cw.spin != cw.at->bardir) {
1575           cw += 1;
1576           v.push_back(1);
1577           stepcount++; if(stepcount > 10000) { printf("failhep\n"); return cw; }
1578           }
1579         if(PURE && (cw+wstep).at == cwcopy.at)
1580           v.pop_back();
1581         if(PURE && (cw+3+wstep).at->land == laMirrored && (cw+2+wstep).at->land == laMirrorWall) {
1582           cw += wmirror;
1583           auto p = traceback(v, cw);
1584           if(p.first) return p.second;
1585           cw += 2;
1586           v.push_back(2);
1587           cw += wstep;
1588           v.push_back(0);
1589           cw += 3;
1590           v.push_back(3);
1591           }
1592         }
1593       else {
1594         while((cw+wstep).at->type != 7) {
1595           cw ++;
1596           v.push_back(1);
1597           }
1598         int icount = 0;
1599         for(int i=0; i<3; i++) {
1600           if((cw+wstep).at->bardir == cw.at->c.spin(cw.spin))
1601             icount++;
1602           cw += 2;
1603           }
1604         if(icount >= 2) {
1605           cellwalker cwcopy = cw;
1606           for(int a=0; a<3; a++) for(int m=0; m<2; m++) {
1607             cellwalker cw = cwcopy;
1608             if(m) cw.mirrored = !cw.mirrored;
1609             cw += (a*2);
1610             auto p = traceback(v,cw);
1611             if(p.first) return p.second;
1612             }
1613           printf("icount >= 2 but failed\n");
1614           return cw;
1615           }
1616         while((cw+wstep).at->bardir != cw.at->c.spin(cw.spin)) {
1617           stepcount++; if(stepcount > 10000) { printf("fail2\n"); return cw; }
1618           cw += 2;
1619           v.push_back(1);
1620           v.push_back(1);
1621           }
1622         }
1623       }
1624     else v.pop_back();
1625     cw.mirrored = !cw.mirrored;
1626     cw = traceback(v,cw).second;
1627     return cw;
1628     }
1629 
1630   static const int CACHESIZE = 1<<12; // must be a power of 2
1631   static const int CACHEMASK = CACHESIZE-1;
1632 
1633   pair<cell*, cellwalker> cache[CACHESIZE];
1634   int nextcache;
1635 
clearcache()1636   void clearcache() {
1637     for(int i=0; i<CACHESIZE; i++) cache[i].first = NULL;
1638     }
1639 
reflect(const cellwalker & cw)1640   EX cellwalker reflect(const cellwalker& cw) {
1641     if(!cw.at) return cw;
1642     if((cw.at->landparam & 255) == 0) {
1643       bool cando = false;
1644       forCellEx(c2, cw.at) if(c2->landparam & 255) cando = true;
1645       if(cando) buildEquidistant(cw.at);
1646       }
1647     if((cw.at->landparam & 255) == 0) return cw;
1648     int cid = (cw.at->landparam >> 8) & CACHEMASK;
1649     if(cache[cid].first != cw.at) {
1650       cid = nextcache++;
1651       nextcache &= CACHEMASK;
1652       cw.at->landparam &= ~ (CACHEMASK << 8);
1653       cw.at->landparam |= (cid << 8);
1654       cache[cid].first = cw.at;
1655       cellwalker cw0(cw.at, 0, false);
1656       cache[cid].second = reflect0(cw0);
1657       int tries = 64;
1658       while(inmirror(cache[cid].second.at) && tries--)
1659         cache[cid].second = reflect0(cache[cid].second);
1660       }
1661     cellwalker res = cache[cid].second + cw.spin;
1662     if(cw.mirrored) res.mirrored = !res.mirrored;
1663     return res;
1664     }
1665 
1666   EX }
1667 
1668 EX namespace hive {
1669 
hivehard()1670   EX int hivehard() {
1671     return items[itRoyalJelly] + hardness_empty();
1672     // 0, 5, 40, 135
1673     }
1674 
randomHyperbug()1675   EX eMonster randomHyperbug() {
1676     int h = hivehard();
1677     if(hrand(200) < h)
1678       return moBug2;
1679     return eMonster(moBug0 + hrand(BUGCOLORS));
1680     // 50: 25/25/50
1681     // 100:
1682     }
1683 
1684   struct buginfo_t {
1685     cell *where;
1686     short dist[BUGCOLORS];
1687     };
1688 
1689   vector<buginfo_t> buginfo;
1690 
1691   vector<int> bugqueue[BUGCOLORS];
1692   vector<int> bugqueue4[BUGCOLORS];
1693 
1694   struct bugtomove_t {
1695     int dist, moves, index;
bugtomove_thr::hive::bugtomove_t1696     bugtomove_t(int d, int m, int i) { dist=d; moves=m; index=i; }
1697     };
1698 
operator <(const bugtomove_t & m1,const bugtomove_t & m2)1699   bool operator < (const bugtomove_t& m1, const bugtomove_t& m2) {
1700     if(m1.dist != m2.dist) return m1.dist < m2.dist;
1701     if(m1.moves != m2.moves) return m1.moves < m2.moves;
1702     return false;
1703     }
1704 
1705   vector<bugtomove_t> bugtomove;
1706   vector<cell*> deadbug;
1707   vector<cell*> bugcellq;
1708 
1709   EX int bugcount[BUGCOLORS];
1710 
isBugEnemy(cell * c,int k)1711   bool isBugEnemy(cell *c, int k) {
1712     if(isPlayerOn(c) && !invismove) return true;
1713     if(!c->monst) return false;
1714     if(c->monst == moBug0+k) return false;
1715     if(isIvy(c)) return false;
1716     return true;
1717     }
1718 
1719   // list bugs and targets for each color
1720   #define BUGINF 29999
1721 
bugQueueInsert(int k,int i,int d)1722   void bugQueueInsert(int k, int i, int d) {
1723     if(buginfo[i].dist[k] > d) {
1724       if(buginfo[i].dist[k] != BUGINF) {
1725         printf("%d -> %d\n", buginfo[i].dist[k], d);
1726         }
1727       buginfo[i].dist[k] = d;
1728       bugqueue[k].push_back(i);
1729       }
1730     }
1731 
bugcell(cell * c)1732   void bugcell(cell *c) {
1733     int& i(c->listindex);
1734     if(i >= 0 && i < isize(buginfo) && buginfo[i].where == c)
1735       return;
1736     i = isize(buginfo);
1737     buginfo.resize(i+1);
1738     buginfo_t& b(buginfo[i]);
1739     b.where = c;
1740     for(int k=0; k<BUGCOLORS; k++) {
1741       b.dist[k] = BUGINF;
1742       bool havebug = false, haveother = false;
1743       for(int dir=0; dir<c->type; dir++) {
1744         cell *c2 = c->move(dir);
1745         if(c2 && isBugEnemy(c2,k) && canAttack(c,eMonster(moBug0+k),c2,c2->monst, AF_TOUGH | AF_NOSHIELD | AF_GETPLAYER)) {
1746           if(isBug(c2)) havebug = true;
1747           else haveother = true;
1748           }
1749         }
1750       if(havebug) bugQueueInsert(k, i, 0);
1751       else if(haveother) bugqueue4[k].push_back(i);
1752       }
1753   /*// bugs communicate if the distance is at most 2
1754     // also all nearby cells are inserted to the buginfo structure
1755     if(isize(buginfo) < 30000) {
1756       for(int dir=0; dir<c->type; dir++) {
1757         cell *c2 = c->move(dir);
1758         if(c2) {
1759           // if(isBug(c)) bugcellq.push_back(c2); => does not help...
1760           for(int t=0; t<c2->type; t++)
1761             if(c2->move(t) && isBug(c2->move(t)))
1762               bugcellq.push_back(c2),
1763               bugcellq.push_back(c2->move(t));
1764           }
1765         }
1766       }*/
1767 
1768     // use pheromones!
1769     if(c->land == laHive && c->landparam > 1 && c->wall != waWaxWall) {
1770       c->landparam --;
1771       for(int dir=0; dir<c->type; dir++) {
1772         cell *c2 = c->move(dir);
1773         if(c2) {
1774           for(int t=0; t<c2->type; t++)
1775             if(c2->move(t))
1776               bugcellq.push_back(c2),
1777               bugcellq.push_back(c2->move(t));
1778           }
1779         }
1780       }
1781     }
1782 
1783   int last_d = -1;
1784 
handleBugQueue(int k,int t)1785   void handleBugQueue(int k, int t) {
1786     int i = bugqueue[k][t];
1787     buginfo_t& b(buginfo[i]);
1788     cell *c = b.where;
1789     int d = b.dist[k];
1790     last_d = d;
1791     int goodmoves = 0;
1792     for(int dir=0; dir<c->type; dir++) {
1793       cell *c2 = c->move(dir);
1794       if(!c2) continue;
1795       if(c2->listindex < 0 || c2->listindex >= isize(buginfo)) continue;
1796       if(!passable(c, c2, P_MONSTER)) continue;
1797       int j = c2->listindex;
1798       if(buginfo[j].where != c2) continue;
1799       if(buginfo[j].dist[k] < d) goodmoves++;
1800       bugQueueInsert(k, j, d+1);
1801       }
1802     if(isBug(c) && c->monst == moBug0+k) {
1803       bugcount[c->monst - moBug0]++;
1804       bugtomove.push_back(bugtomove_t(d,goodmoves,i));
1805       }
1806     }
1807 
fightspam(cell * c)1808   bool fightspam(cell *c) {
1809     return c->cpdist >= gamerange() ||
1810       isMetalBeast(c->monst) || c->monst == moSkeleton ||
1811       isIvy(c->monst) || isMutantIvy(c->monst);
1812     }
1813 
movebugs()1814   EX void movebugs() {
1815     buginfo.clear();
1816     for(int k=0; k<BUGCOLORS; k++) bugqueue[k].clear();
1817     for(int k=0; k<BUGCOLORS; k++) bugqueue4[k].clear();
1818     for(int k=0; k<BUGCOLORS; k++) bugcount[k] = 0;
1819     bugtomove.clear();
1820     deadbug.clear();
1821 
1822     int xdcs = isize(dcal); for(int i=0; i<xdcs; i++) bugcell(dcal[i]);
1823     for(int i=0; i<isize(bugcellq); i++) bugcell(bugcellq[i]);
1824     bugcellq.clear();
1825 
1826     // printf("buginfo = %d\n", isize(buginfo));
1827 
1828     for(int k=0; k<BUGCOLORS; k++) {
1829       int t = 0;
1830       last_d = -1;
1831       int invadist = 4 - (items[itRoyalJelly]+10) / 20;
1832       if(invadist<0) invadist = 0;
1833       for(; t<isize(bugqueue[k]) && last_d < invadist-1; t++) handleBugQueue(k, t);
1834       for(int u=0; u<isize(bugqueue4[k]); u++)
1835         bugQueueInsert(k, bugqueue4[k][u], invadist);
1836       bugqueue4[k].clear();
1837       for(; t<isize(bugqueue[k]); t++) handleBugQueue(k, t);
1838       }
1839 
1840     for(int k=0; k<BUGCOLORS; k++) {
1841       set<int> check;
1842       for(int t=0; t<isize(bugqueue[k]); t++) {
1843         if(check.count(bugqueue[k][t])) {
1844           printf("REPETITION! [%d]\n", t);
1845           }
1846         check.insert(bugqueue[k][t]);
1847         }
1848       }
1849 
1850     hrandom_shuffle(bugtomove);
1851     sort(bugtomove.begin(), bugtomove.end());
1852 
1853     int battlecount = 0;
1854     for(int t=0; t<isize(bugtomove); t++) {
1855       bugtomove_t& bm(bugtomove[t]);
1856       int i = bm.index;
1857 
1858       buginfo_t& b(buginfo[i]);
1859       cell *c = b.where;
1860       if(!isBug(c)) continue;
1861       if(c->stuntime) continue;
1862       eMonster m = c->monst;
1863       int k = (m - moBug0) % BUGCOLORS;
1864       int gmoves[8], q=0, bqual = -1;
1865 
1866       if(againstRose(c, NULL)) bqual = -40;
1867 
1868       for(int dir=0; dir<c->type; dir++) {
1869         cell *c2 = c->move(dir);
1870         int qual = -10;
1871         if(!c2) continue;
1872         else if(againstRose(c, c2)) qual = -50;
1873         else if(canAttack(c, m, c2, c2->monst, AF_GETPLAYER))
1874           qual = c2->monst == moDeadBug ? -60: isBugEnemy(c2,k) ? 2 : -20;
1875         else if(!passable(c2, c, 0))
1876           qual = passable(c2, c, P_DEADLY) ? -30 : -60;
1877         else if(c2->listindex < 0 || c2->listindex >= isize(buginfo)) qual = -15;
1878         else if(buginfo[c2->listindex].where != c2) qual = -15;
1879         else if(buginfo[c2->listindex].dist[k] < b.dist[k])
1880           qual = 1;
1881         else if(buginfo[c2->listindex].dist[k] == b.dist[k])
1882           qual = 0;
1883         // printf("%d->#%d %d: %d\n", i, dir, c2->tmp, qual);
1884         if(qual > bqual) bqual = qual, q=0;
1885         if(qual == bqual) gmoves[q++] = dir;
1886         }
1887 
1888       if(!q) { if(c->land == laHive) c->landparam += 3; continue; }
1889       int d = gmoves[hrand(q)];
1890       movei mi(c, d);
1891       auto& c2 = mi.t;
1892       if(c2->monst || isPlayerOn(c2)) {
1893         eMonster killed = c2->monst;
1894         if(isPlayerOn(c2)) killed = moPlayer;
1895         if(isBug(killed)) battlecount++;
1896         else if(killed != moPlayer && !fightspam(c2))
1897           addMessage(XLAT("%The1 fights with %the2!", c->monst, killed));
1898         attackMonster(c2, AF_NORMAL | AF_GETPLAYER, c->monst);
1899         // killMonster(c);
1900         if(isBug(killed)) {
1901           c2->monst = moDeadBug, deadbug.push_back(c2);
1902           bugcount[killed - moBug0]--;
1903           }
1904   //    c->monst = moDeadBug, deadbug.push_back(c);
1905         }
1906       else {
1907         moveMonster(mi);
1908         // pheromones!
1909         if(c->land == laHive && c->landparam < 90) c->landparam += 5;
1910         if(c2->land == laHive && c2->landparam < 90) c2->landparam += 5;
1911   /*      if(c2->item == itRoyalJelly && !isQueen(m)) {
1912           // advance!
1913           c2->monst = eMonster(m+BUGCOLORS);
1914           c2->item = itNone;
1915           } */
1916         }
1917       }
1918 
1919     // cleanup
1920     for(int i=0; i<isize(deadbug); i++) deadbug[i]->monst = moNone;
1921     if(battlecount)
1922       addMessage(XLAT("The Hyperbugs are fighting!"));
1923 
1924     int maxbug = 0;
1925     for(int k=0; k<BUGCOLORS; k++) if(bugcount[k] > maxbug) maxbug = bugcount[k];
1926 
1927     achievement_count("BUG", maxbug, 0);
1928     }
1929 
bugcitycell(cell * c,int d)1930   void bugcitycell(cell *c, int d) {
1931     int& i = c->listindex;
1932     if(i >= 0 && i < isize(buginfo) && buginfo[i].where == c)
1933       return;
1934     i = isize(buginfo);
1935     buginfo_t b;
1936     b.where = c;
1937     b.dist[0] = d;
1938     buginfo.push_back(b);
1939     }
1940 
createBugArmy(cell * c)1941   EX void createBugArmy(cell *c) {
1942     int k = randomHyperbug() - moBug0;
1943     int minbugs = 50, maxbugs = 50;
1944     int var = 5 + items[itRoyalJelly];
1945     if(var>25) var=25;
1946     // minbugs += 100; maxbugs += 100;
1947     minbugs -= var; maxbugs += var;
1948     maxbugs += items[itRoyalJelly];
1949     int numbugs = minbugs + hrand(maxbugs - minbugs + 1);
1950 
1951     /* int i = items[itRoyalJelly];
1952     int chance = 20 + 25 * i + 9000;
1953     // i=0: 16%
1954     // i=10: 73%
1955     // i=50: 1270 vs 6000
1956     eMonster m = eMonster(moBug0 + hrand(BUGCOLORS));
1957     if(c->wall) return;
1958     for(int i=0; i<c->type; i++) {
1959       cell *c2 = createMov(c,i);
1960       if(hrand(100+chance) < chance) {
1961         if(!c2->wall) c2->monst = m;
1962         for(int j=2; j<=c2->type-2; j++) {
1963           int jj = (j+c->spn[i]) % c2->type;
1964           cell *c3 = createMov(c2, jj);
1965           if(hrand(6000+chance) < chance && !c3->wall)
1966             c3->monst = m;
1967           }
1968         }
1969       }
1970     c->monst = eMonster(m + BUGCOLORS); */
1971 
1972     int gdir = -1;
1973     for(int i=0; i<c->type; i++) {
1974       if(c->move(i) && c->move(i)->mpdist < c->mpdist) gdir = i;
1975       }
1976     if(!gdir) return;
1977     cellwalker bf(c, gdir);
1978     int radius = 9;
1979     if(getDistLimit() <= 6) radius = 6;
1980     if(ls::any_chaos()) radius = 5;
1981     if(ls::any_chaos() && getDistLimit() <= 5) radius = 4;
1982     if(getDistLimit() <= 3) radius = 3;
1983 
1984     for(int i=(ls::any_chaos()?0:2); i<radius; i++)
1985       bf += revstep;
1986     cell *citycenter = bf.at;
1987     buginfo.clear();
1988 
1989 
1990     // mark the area with BFS
1991     bugcitycell(citycenter, 0);
1992     for(int i=0; i<isize(buginfo); i++) {
1993       buginfo_t& b(buginfo[i]);
1994       cell *c = b.where;
1995       int d = b.dist[0];
1996       // ERRORS!
1997       if(c->land != laHive && c->land != laNone) return;
1998       if(c->bardir != NODIR) return;
1999       if(c->land == laHive && c->landparam >= 100) return;
2000       // bfs
2001       if(d < radius) for(int t=0; t<c->type; t++)
2002         bugcitycell(createMov(c,t), d+1);
2003       }
2004 
2005     // place everything
2006     for(int i=0; i<isize(buginfo); i++) {
2007       buginfo_t& b(buginfo[i]);
2008       cell *c = b.where;
2009       int d = b.dist[0];
2010       if(d <= 1 && c->wall == waNone)
2011         c->item = itRoyalJelly;
2012       preventbarriers(c);
2013       if(d == 9 || d == 6 || d == 3)
2014         c->barleft = eLand(d/3),
2015         c->barright = eLand(k);
2016       else
2017         c->barleft = laNone;
2018       if(numbugs && c->wall == waNone)
2019         c->monst = eMonster(moBug0 + k), numbugs--;
2020       c->land = laHive;
2021       // prevent barriers
2022       if(c->mpdist == INFD) c->mpdist = BUGLEV;
2023       }
2024     }
2025 
2026   EX }
2027 
2028 #if HDR
HEAT(cell * c)2029 inline float& HEAT(cell *c) { return c->LHU.heat; }
2030 #endif
2031 
2032 EX namespace heat {
2033 
affect(cell * c,double delta)2034   EX void affect(cell *c, double delta) {
2035     if(isIcyLand(c)) HEAT(c) += delta;
2036     }
2037 
absheat(cell * c)2038   EX double absheat(cell *c) {
2039     if(c->land == laCocytus) return HEAT(c) -.6;
2040     if(c->land == laIce || c->land == laBlizzard || c->land == laEclectic) return HEAT(c) -.4;
2041     return 0;
2042     }
2043 
celsius(cell * c)2044   EX double celsius(cell *c) { return absheat(c) * 60; }
2045 
2046   // adjust to the improved heat transfer algorithm in 9.4
2047   const float FIX94 = 1.5;
2048 
2049   EX vector<cell*> offscreen_heat, offscreen_fire; // offscreen cells to take care off
2050 
2051   EX void processheat(double rate IS(1)) {
2052     if(markOrb(itOrbSpeed)) rate /= 2;
2053     if(racing::on) return;
2054     int oldmelt = kills[0];
2055 
2056     vector<cell*> offscreen2;
2057 
2058     manual_celllister cl;
2059 
2060     int gr = gamerange();
2061 
2062     for(cell *c: offscreen_heat) {
2063       if(c->cpdist > gr && !doall) {
2064         if(!cl.add(c)) continue;
2065         if(isIcyLand(c)) {
2066           if(HEAT(c) < .01 && HEAT(c) > -.01)
2067             HEAT(c) = 0;
2068           else {
2069             HEAT(c) *= 1 - rate/10;
2070             offscreen2.push_back(c);
2071             }
2072           }
2073         }
2074       }
2075 
2076     offscreen_heat = move(offscreen2);
2077 
2078     for(int i=0; i<numplayers(); i++) {
2079       cell *c = playerpos(i);
2080       if(!c) continue;
2081       double xrate = (c->land == laCocytus && shmup::on) ? rate/3 : rate;
2082       if(PURE) xrate *= 1.7; // todo-variation
2083       if(!shmup::on) xrate /= FIX94;
2084       if(isIcyLand(c))
2085         HEAT(c) += (markOrb(itOrbWinter) ? -1.2 : 1.2) * xrate;
2086       }
2087 
2088     vector<cell*>& allcells = currentmap->allcells();
2089 
2090     int dcs = isize(allcells);
2091 
2092     vector<ld> hmods(dcs, 0);
2093 
2094     int divby = 10;
2095     if(S7 > 10) divby *= 2;
2096     if(arcm::in()) divby *= 2;
2097     if(WDIM == 3) divby *= 2;
2098 
2099     for(int i=0; i<dcs; i++) {
2100       cell *c = allcells[i];
2101       double xrate = (c->land == laCocytus && shmup::on) ? 1/3. : 1;
2102       if(PURE) xrate *= 1.7; // todo-variation
2103       if(!shmup::on) xrate /= FIX94;
2104       if(c->cpdist > gr && !doall) break;
2105 
2106       if(isIcyLand(c)) {
2107         ld hmod = 0;
2108 
2109         if(c->monst == moRanger) hmod += 3 * xrate;
2110         if(c->monst == moDesertman) hmod += 4 * xrate;
2111         if(c->monst == moAngryDie) hmod += 4 * xrate;
2112         if(c->monst == moMonkey) hmod += xrate;
2113         if(c->wall == waDeadTroll) hmod -= 2 * xrate;
2114         if(c->wall == waDeadTroll2) hmod -= 1.5 * xrate;
2115         if(c->wall == waBigStatue) hmod -= .5 * xrate;
2116         if(c->monst == moLesser || c->monst == moLesserM || c->monst == moGreater || c->monst == moGreaterM)
2117           hmod += (c->land == laCocytus ? 1.5 : 10) * xrate;
2118         if(c->monst == moGreaterShark)
2119           hmod += 2 * xrate;
2120         if(c->monst == moCultist) hmod += 3 * xrate;
2121         if(c->monst == moCultistLeader) hmod += 4 * xrate;
2122         if(c->monst == moPyroCultist) hmod += 6 * xrate;
2123         if(c->monst == moFireFairy) hmod += 6 * xrate;
2124         if(c->monst == moFireElemental) hmod += 8 * xrate;
2125         if(isDragon(c->monst)) hmod += 2 * xrate;
2126         if(c->monst == moGhost) hmod -= xrate;
2127         if(c->monst == moFriendlyGhost) hmod -= xrate;
2128         if(c->monst == moSkeleton) hmod -= .2 * xrate;
2129         if(c->monst == moDraugr) hmod -= .75 * xrate;
2130         if(c->monst == moWaterElemental) hmod -= xrate;
2131         if(c->monst == moAirElemental) hmod -= .4 * xrate;
2132         if(isFire(c)) hmod += 4 * xrate;
2133         if(isPrincess(c->monst)) hmod += (markEmpathy(itOrbWinter) ? -1.2 : 1.2) * xrate;
2134 
2135         auto ls = adj_minefield_cells(c);
2136 
2137         for(cell* ct: ls) {
2138           if(!isIcyLand(ct) && isFire(ct))
2139             hmod += xrate*.1;
2140           if(ct->land == laVolcano)
2141             hmod += xrate * (ct->wall == waMagma ? .4 : .2);
2142           }
2143 
2144         for(cell* ct: ls) {
2145           if(!isIcyLand(ct)) {
2146             // make sure that we can still enter Cocytus,
2147             // it won't heat up right away even without Orb of Winter or Orb of Speed
2148             if(isPlayerOn(ct) && (c->land != laCocytus || markOrb(itOrbWinter)))
2149               hmod += (markOrb(itOrbWinter) ? -1.2 : 1.2) / 4 * xrate;
2150             continue;
2151             }
2152           ld hdiff = absheat(ct) - absheat(c);
2153           hdiff /= divby;
2154 
2155           #if CAP_FIELD
2156           if(ct->land == laBlizzard && c->land == laBlizzard) {
2157             int v = (windmap::at(ct) - windmap::at(c)) & 255;
2158             if(v > 128) v -= 256;
2159             if(v < windmap::NOWINDFROM && v > -windmap::NOWINDFROM)
2160               hdiff = hdiff * (1 - v * 5. / windmap::NOWINDFROM);
2161             }
2162           #endif
2163 
2164           if(shmup::on && (c->land == laCocytus || ct->land == laCocytus))
2165             hdiff /= 3;
2166           // if(c->move(j)->cpdist > 7 && !quotient) hdiff += -HEAT(c) / 30;
2167           hmod += hdiff;
2168           }
2169         // printf("%d ", vsum);
2170 
2171         hmods[i] = hmod;
2172         }
2173 
2174       if(HEAT(c) && !doall)
2175         offscreen_heat.push_back(c);
2176       }
2177 
2178     #if HDR
2179     #define MELTCOLOR 0xA04040
2180     #endif
2181 
2182     for(int i=0; i<dcs; i++) {
2183       cell *c = allcells[i];
2184       if(!isIcyLand(c)) continue;
2185       HEAT(c) += hmods[i] * rate;
2186       if(c->monst == moCrystalSage && HEAT(c) >= SAGEMELT) {
2187         addMessage(XLAT("%The1 melts away!", c->monst));
2188         fallMonster(c);
2189         }
2190       if(c->wall == waIcewall && HEAT(c) > .4)
2191         drawParticles(c, MELTCOLOR, 4, 60),
2192         c->wall = waNone, kills[0]++;
2193       if(c->wall == waFrozenLake && HEAT(c) > (c->land == laCocytus ? .6 : .4))
2194         drawParticles(c, MELTCOLOR, 4, 60),
2195         playSound(c, "trapdoor", 50),
2196         c->wall = waLake, kills[0]++;
2197 
2198       if(c->wall == waLake && HEAT(c) < (c->land == laCocytus ? -.4 : .4) && c->monst != moGreaterShark) {
2199         c->wall = waFrozenLake;
2200         if(c->monst == moShark || c->monst == moCShark) {
2201           addMessage(XLAT("%The1 is frozen!", c->monst));
2202           fallMonster(c);
2203           }
2204         }
2205       }
2206 
2207     if(kills[0] != oldmelt) bfs();
2208     }
2209 
2210   vector<pair<cell*, int> > newfires;
2211 
processfires()2212   EX void processfires() {
2213     newfires.clear();
2214 
2215     vector<cell*> offscreen2;
2216 
2217     manual_celllister cl;
2218 
2219     vector<cell*>& allcells = currentmap->allcells();
2220 
2221     for(int x: {0,1}) for(cell *c: x==0 ? allcells : offscreen_fire) {
2222       if(!cl.add(c)) continue;
2223 
2224       if(isFireOrMagma(c)) {
2225         if(c->wall == waMagma) c->wparam = 20;
2226 
2227         cell *last = c->move(c->type-1);
2228 
2229         auto ls = adj_minefield_cells(c);
2230         for(cell* c2: ls) {
2231 
2232           if(c->wall == waPartialFire) {
2233             // two partial fires adjacent are necessary to spread
2234             bool ok = false;
2235             forCellEx(c3, c2) if(c3 != c && c3->wall == waPartialFire)
2236               ok = true;
2237             if(!ok) continue;
2238             }
2239 
2240           if(c2->wall == waNone && c2->land == laRose && c->wparam >= 10)
2241             newfires.emplace_back(c2, c->wparam);
2242           if(c2->wall == waFire && c2->land == laRose && c->wparam >= 10 && c2->wparam < c->wparam/2)
2243             newfires.emplace_back(c2, c->wparam);
2244           if(againstWind(c, c2) && c->wall != waEternalFire && c->wparam >= 10) {
2245             if(isFire(c2)) {
2246               if(c2->wparam < c->wparam/2)
2247                 newfires.emplace_back(c2, c->wparam);
2248               }
2249             else {
2250               newfires.emplace_back(c2, c->wparam);
2251               useup(c);
2252               }
2253             }
2254           if(c2->land == laDryForest && c2->wall != waEternalFire) {
2255             c2->landparam++;
2256             if(c2->landparam >= (isStandardTree(c2) ? 1 : 10)) newfires.emplace_back(c2, 12);
2257             else offscreen2.push_back(c);
2258             }
2259           else if(c2->wall == waVinePlant || c2->wall == waRose || c2->wall == waSaloon ||
2260             c2->wall == waWeakBranch || c2->wall == waCanopy || c2->wall == waTrunk || c2->wall == waSolidBranch ||
2261             c2->wall == waBigBush || c2->wall == waSmallBush || c2->wall == waBonfireOff || c2->wall == waSmallTree ||
2262             c2->wall == waDock)
2263             newfires.emplace_back(c2, 12);
2264           else if(cellHalfvine(c2) && last && last->wall == c2->wall)
2265             newfires.emplace_back(c2, 12);
2266           else if(c2->wall == waExplosiveBarrel)
2267             newfires.emplace_back(c2, 12);
2268           else if(c2->wall == waFireTrap)
2269             newfires.emplace_back(c2, 12);
2270           // both halfvines have to be near fire at once
2271           last = c2;
2272           }
2273 
2274         }
2275 
2276       if(hasTimeout(c)) {
2277         if(c->mpdist == 8 && (c->land == laWineyard || c->land == laEndorian || c->land == laDocks)) {
2278           // do not expire, do not store in 'offscreen', do not generate more land
2279           }
2280         else {
2281           useup(c);
2282           offscreen2.push_back(c);
2283           }
2284         }
2285 
2286       if(c->wall == waArrowTrap && c->wparam && !shmup::on) {
2287         c->wparam++;
2288         if(c->wparam == 3) {
2289           if(canAttack(c, moArrowTrap, c, c->monst, AF_GETPLAYER))
2290             attackMonster(c, AF_NORMAL | AF_MSG | AF_GETPLAYER, moArrowTrap);
2291           }
2292         if(c->wparam == 4) c->wparam = 0;
2293         }
2294 
2295       if(c->wall == waFireTrap && c->wparam && !shmup::on) {
2296         c->wparam++;
2297         if(c->wparam == 3) {
2298           c->wall = waNone;
2299           explosion(c, 5, 10);
2300           }
2301         }
2302       }
2303 
2304    for(int i=0; i<isize(newfires); i++) {
2305       cell* c = newfires[i].first;
2306       int qty = newfires[i].second;
2307       qty /= 2;
2308       if(c->wall == waExplosiveBarrel) explodeBarrel(c);
2309       else if(c->wall == waFireTrap) {
2310         if(c->wparam == 0) c->wparam = 1;
2311         continue;
2312         }
2313       else if(c->wall == waBonfireOff) activateActiv(c, false);
2314       else if(cellHalfvine(c)) destroyHalfvine(c, waPartialFire, 6);
2315       else makeflame(c, qty, false);
2316       if(c->wparam < qty) c->wparam = qty;
2317       offscreen2.push_back(c);
2318       if(c->land == laRose || c->land == laWildWest || c->land == laOvergrown || isHaunted(c->land) || c->land == laMountain || c->land == laIce) {
2319         for(int j=c->mpdist-1; j>=7; j--) setdist(c, j, NULL);
2320         }
2321       }
2322 
2323     offscreen_fire = move(offscreen2);
2324     }
2325 
2326 EX }
2327 
2328 bool gardener = false;
2329 
livecaves()2330 EX void livecaves() {
2331   vector<cell*>& allcells = currentmap->allcells();
2332   int dcs = isize(allcells);
2333 
2334   vector<cell*> bringlife;
2335   int gr = gamerange();
2336 
2337   std::vector<int> heatvals(dcs);
2338 
2339   for(int i=0; i<dcs; i++) {
2340     cell *c = allcells[i];
2341     if(!doall && c->cpdist > gr+1) break;
2342 
2343     int & hv = heatvals[i];
2344 
2345     if(c->wall == waCavefloor || c->wall == waCavewall || c->wall == waDeadTroll) {
2346       hv = 0;
2347       if(c->monst == moDarkTroll) c->monst = moTroll;
2348       if(c->item || c->monst || c->cpdist == 0) continue;
2349       for(cell *c2: adj_minefield_cells(c)) {
2350         eWall w = c2->wall;
2351         if(w == waDeadfloor) hv++, bringlife.push_back(c2);
2352         else if(w == waDeadwall || (w == waDeadfloor2 && !c2->monst && !isPlayerOn(c2)))
2353           hv--, bringlife.push_back(c2);
2354         else if(w == waCavefloor) hv++;
2355         else if(w == waCavewall) hv--;
2356         else if(w == waRubble) hv--;
2357         else if(w == waGargoyle) hv--;
2358         else if(w == waGargoyleFloor) hv--;
2359         else if(w == waGargoyleBridge) hv--;
2360         else if(w == waStone) ;
2361         else if(w == waDeadTroll) hv -= 5;
2362         else if(w == waDeadTroll2) hv -= 3;
2363         else if(w == waPetrified || w == waPetrifiedBridge) hv -= 2;
2364         else if(w == waVinePlant) hv--;
2365         else if(ls::any_chaos() && c2->land != laCaves && c2->land != laEmerald) ;
2366         else if(c2->land == laTrollheim) ; // trollheim floor does not count
2367         else if(w != waBarrier) hv += 5;
2368 
2369         if(sword::at(c)) hv += 500;
2370 
2371         if(c2->cpdist == 0 && markOrb(itOrbDigging)) hv+=100;
2372         if(items[itOrbEmpathy] && isFriendly(c2) && markEmpathy(itOrbDigging))
2373           hv+=100;
2374         if(w == waThumperOn) hv+=100;
2375         if(w == waFire) hv+=100;
2376         if(w == waBigStatue) hv-=100;
2377         if(c2->item && !peace::on) hv+=2;
2378         if(c2->monst == moZombie) hv += 10;
2379         if(c2->monst == moGhost) hv += 10;
2380         if(c2->monst == moTentacleGhost) hv += 10;
2381         if(c2->monst == moFriendlyGhost) hv += 10;
2382         if(c2->monst == moSkeleton) hv ++;
2383         if(c2->monst == moGargoyle) hv--;
2384         if(c2->monst == moDraugr) hv--;
2385         if(isDragon(c2->monst)) hv++;
2386         if(c2->monst == moNecromancer) hv += 10;
2387         if(c2->monst == moWormtail) hv++;
2388         if(c2->monst == moTentacletail) hv-=2;
2389         if(c2->monst == moAngryDie) hv++;
2390         if(isIvy(c2)) hv--;
2391         if(isDemon(c2)) hv-=3;
2392         // if(c2->monst) c->tmp++;
2393         // if(c2->monst == moTroll) c->tmp -= 3;
2394         }
2395       }
2396     else if(c->land == laLivefjord) {
2397       hv = 0;
2398       if(c->monst == moWaterElemental)
2399         hv += 1000;
2400       if(isPlayerInBoatOn(c) && markOrb(itOrbWater))
2401         hv += 1000;
2402       if(c->monst == moEarthElemental)
2403         hv -= 1000;
2404       if(isPlayerOn(c) && markOrb(itOrbDigging))
2405         hv -= 1000;
2406       for(int j=0; j<c->type; j++) if(c->move(j)) {
2407         cell *c2 = c->move(j);
2408         if(c2->wall == waNone || c2->wall == waStrandedBoat)
2409           hv -= (c2->land == laLivefjord ? 1 : 100);
2410         if(c2->wall == waTempFloor || c2->wall == waTempBridge || c2->wall == waTempBridgeBlocked)
2411           ;
2412         else if(c2->wall == waDeadTroll || c2->wall == waDeadTroll2 || c2->wall == waThumperOn || isFire(c2) || snakelevel(c2))
2413           hv -= 10;
2414         else if(c2->wall == waPetrified || c2->wall == waPetrifiedBridge)
2415           hv -= 10;
2416         if(c2->wall == waBigStatue)
2417           hv -= 10;
2418         if(c2->wall == waSea || c2->wall == waBoat)
2419           hv += (c2->land == laLivefjord ? 1 : 100);
2420         if(c2->monst == moWaterElemental)
2421           hv += 1000;
2422         if(isPlayerOn(c2) && c2->wall == waBoat && markOrb(itOrbWater))
2423           hv += 1000;
2424         if(c2->monst == moEarthElemental)
2425           hv -= 1000;
2426         if(isPlayerOn(c2) && markOrb(itOrbDigging))
2427           hv -= 1000;
2428         if(items[itOrbEmpathy] && isFriendly(c2) && markEmpathy(itOrbDigging))
2429           hv -= 1000;
2430 
2431         if(c2->wall == waBarrier) {
2432           bool landbar = false;
2433           for(int k=0; k<c2->type; k++)
2434             if(c2->move(k)) {
2435               cell *c3 = c2->move(k);
2436               if(!isSealand(c3->land))
2437                 landbar = true;
2438               }
2439           if(landbar) hv -= 5;
2440           else hv += 5;
2441           }
2442         }
2443       }
2444     }
2445 
2446   for(int i=0; i<dcs; i++) {
2447     cell *c = allcells[i];
2448     if(!doall && c->cpdist > gr+1) break;
2449     int hv = heatvals[i];
2450 
2451     if(c->wall == waCavefloor || c->wall == waCavewall) {
2452   //  if(c->land != laCaves) continue;
2453   //  if(c->wall == waThumper || c->wall == waBonfire) continue;
2454 
2455       if(hv > 0) c->wall = waCavefloor;
2456       if(hv < 0) {
2457         c->wall = waCavewall;
2458         if(c->land != laCaves && c->land != laDeadCaves && c->land != laEmerald && !gardener) {
2459           gardener = true;
2460           achievement_gain_once("GARDENER");
2461           }
2462         }
2463       }
2464     else if(c->land == laLivefjord) {
2465       if(hv > 0 && c->wall == waStrandedBoat) c->wall = waBoat;
2466       if(hv > 0 && c->wall == waNone) {
2467         if(c->item && c->cpdist == 1 && markOrb(itOrbWater)) {
2468           bool saf = c->item == itOrbSafety;
2469           collectItem(c);
2470           if(saf) return;
2471           }
2472         c->wall = waSea;
2473         }
2474       if(hv < 0 && c->wall == waBoat) c->wall = waStrandedBoat;
2475       if(hv < 0 && c->wall == waSea) c->wall = waNone;
2476       }
2477     }
2478 
2479   for(int i=0; i<isize(bringlife); i++) {
2480     cell *c = bringlife[i];
2481     if(c->land == laDeadCaves)
2482       achievement_gain_once("LIFEBRINGER");
2483     if(c->wall == waDeadfloor) c->wall = waCavefloor;
2484     if(c->wall == waDeadfloor2) c->wall = waCavewall;
2485     if(c->wall == waDeadwall) c->wall = waCavewall;
2486     if(c->wall == waCavewall && c->item) c->wall = waCavefloor;
2487     if(c->land == laDeadCaves) c->land = laCaves;
2488     if(c->item == itSilver) c->item = itGold;
2489     if(c->item == itGreenStone && !inv::on) c->item = itOrbLife;
2490     if(c->monst == moEarthElemental) {
2491       addMessage(XLAT("%The1 is destroyed by the forces of Life!", c->monst));
2492       fallMonster(c);
2493       c->item = itOrbDigging;
2494       }
2495     }
2496   }
2497 
2498 /* evolver */
2499 
2500 EX namespace tortoise {
2501   EX map<cell*, int> emap;
2502   EX map<cell*, int> babymap;
2503   EX int last;
2504 
2505   #if HDR
2506   enum tflag {
2507     tfShell, tfScute0, tfScute1, tfScute2, tfScute3,
2508     tfEdge1, tfEdge, tfEdge3,
2509     tfLongNeck, tfFront, tfRear, tfTail,
2510     tfEyeHue, tfShellHue, tfScuteHue, tfSkinHue,
2511     tfShellSat, tfScuteSat, tfSkinSat,
2512     tfShellDark, tfSkinDark,
2513     tfCOUNT
2514     };
2515   #endif
2516 
2517   EX const int numbits = (int) tfCOUNT;
2518   EX const int mask = (1<<numbits)-1;
2519 
getb(cell * where)2520   EX int getb(cell *where) {
2521     if(emap.count(where)) return emap[where];
2522     return getBits(where) & ((1<<numbits)-1);
2523     }
2524 
countBits(int c)2525   EX int countBits(int c) {
2526     int bi = 0;
2527     for(int i=0; i<numbits; i++) if((c >> i)&1) bi++;
2528     return bi;
2529     }
2530 
getBit(int bits,int id)2531   EX int getBit(int bits, int id) { return (bits >> id) & 1; }
2532 
getRandomBits()2533   EX int getRandomBits() { return hrand(1 << numbits); }
2534 
seek()2535   EX bool seek() { return items[itBabyTortoise] % 5; }
2536   EX int seekbits;
2537   double seekval[numbits];
2538   double currval[numbits];
2539 
update(double & val,double target,int delta)2540   void update(double& val, double target, int delta) {
2541     double d = delta / 300.;
2542     if(d>1) d = 1;
2543     if(target>val+d) val += d;
2544     else if(target<val-d) val -= d;
2545     else val = target;
2546     }
2547 
2548   EX bool shading_enabled = true;
2549 
shading_on()2550   EX bool shading_on() {
2551     return shading_enabled && seek();
2552     }
2553 
updateVals(int delta)2554   EX void updateVals(int delta) {
2555     int currbits = getBits(cwt.at);
2556     for(int i=0; i<numbits; i++)
2557       update(seekval[i], shading_on() ? getBit(seekbits, i) : .5, delta);
2558     for(int i=0; i<numbits; i++)
2559       update(currval[i], getBit(currbits, i), delta);
2560     }
2561 
getScent(int bits)2562   EX double getScent(int bits) {
2563     double res = 0;
2564     for(int i=0; i<numbits; i++)
2565       /* if(getBit(bits, i) != getBit(getBits(cwt.at), i))
2566         res += (1 - 2*getBit(bits, i)); */
2567       res += (2* seekval[i] - 1) * (getBit(bits, i) - currval[i]);
2568 
2569     // seek curr bit => res
2570     //    1    1   1 => 0
2571     //    1    1   0 => -1
2572     //    1    0   1 => +1
2573     //    1    0   0 => 0
2574     //    0    1   1 => 0
2575     //    0    1   0 => +1
2576     //    0    0   1 => -1
2577     //    0    0   0 => 0
2578     return res;
2579     }
2580 
diff(int bits)2581   EX int diff(int bits) { return countBits(bits ^ tortoise::seekbits); }
progress(int bits)2582   int progress(int bits) { return numbits - diff(bits); }
2583 
measure(int bits)2584   EX string measure(int bits) {
2585     return "(" + its(progress(bits)) + "/" + its(tortoise::numbits) + ")";
2586     }
2587 
move_baby(cell * c1,cell * c2)2588   EX void move_baby(cell *c1, cell *c2) {
2589     swap_data(babymap, c1, c2);
2590     }
2591 
move_adult(cell * c1,cell * c2)2592   EX void move_adult(cell *c1, cell *c2) {
2593     swap_data(emap, c1, c2);
2594     }
2595 EX }
2596 
2597 EX namespace dragon {
2598 
2599   EX int whichturn; // which turn has the target been set on
2600   EX cell *target; // actually for all Orb of Domination
2601 
pullback(cell * c)2602   void pullback(cell *c) {
2603     int maxlen = iteration_limit;
2604     while(maxlen-->0) {
2605       auto mi = moveimon(c).rev();
2606       mountmove(mi, true);
2607       c->monst = mi.s->monst;
2608       c->hitpoints = mi.s->hitpoints;
2609       animateMovement(mi, LAYER_BIG);
2610       c->stuntime = 2;
2611       if(mi.s->mondir == NODIR) { c->mondir = NODIR; mi.s->monst = moNone; return; }
2612       c = mi.s;
2613       }
2614     }
2615 
2616   bool dragbugs = false;
2617 
findhead(cell * c)2618   EX cell *findhead(cell *c) {
2619     cell *cor = c;
2620     int maxlen=iteration_limit;
2621     findhead:
2622     if(maxlen--<0) return c;
2623     if(c->monst == moDragonHead) return c;
2624     for(int i=0; i<c->type; i++)
2625       if(c->move(i) && isDragon(c->move(i)->monst) && c->move(i)->mondir == c->c.spin(i)) {
2626         c = c->move(i); goto findhead;
2627         }
2628     if(cmode & sm::MAP) return c;
2629     if(!history::includeHistory) {
2630       printf("dragon bug #3 (%p -> %p)\n", hr::voidp(cor), hr::voidp(c));
2631       dragbugs = true;
2632       }
2633     c->monst = moDragonHead; return c;
2634     }
2635 
validate(const char * where)2636   void validate(const char *where) {
2637     dragbugs = false;
2638     for(int i=0; i<isize(dcal); i++)
2639       if(dcal[i]->monst == moDragonTail)
2640         findhead(dcal[i]);
2641     if(dragbugs) {
2642       printf("DRAGON BUG in %s\n", where);
2643       exit(1);
2644       }
2645     }
2646 
bodypart(cell * c,cell * head)2647   EX int bodypart(cell *c, cell *head) {
2648     int i = 0, j = 0;
2649     int maxlen = iteration_limit;
2650     while(maxlen-->0) {
2651       if(head == c) i = j;
2652       j++;
2653       if(head->mondir == NODIR) break;
2654       head = head->move(head->mondir);
2655       }
2656     if(i == 0) return 'h';
2657     if(i == 1) return 'l';
2658     if(i == j-2) return '2';
2659     if(i == j-1) return 't';
2660     if(i == 2) return 'w';
2661     return 0;
2662     }
2663 
kill(cell * c,eMonster who)2664   EX void kill(cell *c, eMonster who) {
2665     int delay = false;
2666     kills[moDragonHead]++;
2667     int penalty = 0;
2668     int maxlen = iteration_limit;
2669     while(maxlen-->0) {
2670       changes.ccell(c);
2671       makeflame(c, 5, false);
2672       eMonster m = c->monst;
2673       drawFireParticles(c, 16);
2674       c->monst = moNone;
2675       if(checkOrb(who, itOrbUndeath))
2676         c->monst = moFriendlyGhost;
2677       if(checkOrb(who, itOrbStone))
2678         c->wparam = m, c->wall = waPetrified;
2679       else if(c->wall == waFire) {
2680         if(delay) delay = false;
2681         else {
2682           if(c->land != laDragon) penalty += 3;
2683           if(penalty) penalty--;
2684           else {
2685             c->item = itDragon;
2686             c->landparam = shmup::on ? shmup::curtime : turncount;
2687             delay = true;
2688             }
2689           }
2690         }
2691       if(c->mondir == NODIR) break;
2692       c = c->move(c->mondir);
2693       }
2694     }
2695 
totalhp(cell * c)2696   EX int totalhp(cell *c) {
2697     int total = 0;
2698     int maxlen = iteration_limit;
2699     while(maxlen-->0) {
2700       if(!isDragon(c->monst)) {
2701         if(!history::includeHistory) printf("dragon bug #4\n");
2702         return total;
2703         }
2704       total += c->hitpoints;
2705       if(c->mondir == NODIR) return total;
2706       c = c->move(c->mondir);
2707       }
2708     return total;
2709     }
2710 
2711   #define SWAPBITFIELD(x,y,t) { t bak=x; x=y; y=bak; }
2712 
pullfront(cell * c,cell * until)2713   void pullfront(cell *c, cell *until) {
2714     int maxlen = iteration_limit;
2715     static vector<cell*> allcells;
2716     allcells.clear();
2717     while(maxlen-->0) {
2718       allcells.push_back(c);
2719       // SWAPBITFIELD(c->monst, buffer->monst, eMonster);
2720       // SWAPBITFIELD(c->hitpoints, buffer->hitpoints, int);
2721       c->stuntime = 2;
2722       if(c == until) {
2723         for(int i=isize(allcells)-2; i>=0; i--) {
2724           cell *cmt = allcells[i+1];
2725           cell *cft = allcells[i];
2726           cmt->hitpoints = cft->hitpoints;
2727           cmt->monst = cft->monst;
2728           cft->monst = moNone;
2729           auto mi = moveimon(cft);
2730           mountmove(mi, true);
2731           animateMovement(mi, LAYER_BIG);
2732           }
2733         while(c->mondir != NODIR) {
2734           c = c->move(c->mondir);
2735           c->stuntime = 2;
2736           }
2737         break;
2738         }
2739       if(c->mondir == NODIR) { printf("dragon bug\n"); break; }
2740       c = c->move(c->mondir);
2741       if(!c) {
2742         if(!history::includeHistory) printf("dragon bug #2\n");
2743         break;
2744         }
2745       }
2746     }
2747 
move(const movei & mi)2748   EX bool move(const movei& mi) {
2749     auto& df = mi.s;
2750     auto& dt = mi.t;
2751     if(df->monst == moDragonHead) {
2752       dt->mondir = neighborId(dt,df);
2753 //    printf("pull back\n");
2754       pullback(dt);
2755       dt->stuntime = 2;
2756       return true;
2757       }
2758     if(df->monst == moDragonTail && df->stuntime == 0) {
2759       cell *head = findhead(df);
2760       if(df->mondir == NODIR) {
2761         df->mondir = neighborId(df,dt);
2762         dt->mondir = NODIR;
2763 //      printf("pull all: head = %p, df=%p, dt=%p\n", head, df, dt);
2764         pullfront(head, dt);
2765         }
2766       else {
2767         cell *c2 = df->move(df->mondir);
2768         if(!c2) return false;
2769         int id = neighborId(dt, c2);
2770         if(id == -1) return false;
2771         dt->mondir = id;
2772         df->mondir = neighborId(df, dt);
2773 //      printf("pull front: head = %p, df=%p, dt=%p\n", head, df, dt);
2774         pullfront(head, dt);
2775         }
2776       return true;
2777       }
2778     return false;
2779     }
2780 
2781 EX }
2782 
2783 EX namespace sword {
2784 
2785   #if HDR
2786   struct sworddir {
2787     int angle;
2788     transmatrix T;
2789     };
2790 
2791   /** dimensions available to the Sword */
2792   #define SWORDDIM (hybri ? 2 : WDIM)
2793 
2794   #endif
2795 
2796   EX int sword_angles;
2797 
2798   EX array<sworddir, MAXPLAYER> dir;
2799 
possible_divisor(int s)2800   void possible_divisor(int s) { sword_angles *= s / gcd(sword_angles, s); }
2801 
determine_sword_angles()2802   EX void determine_sword_angles() {
2803     sword_angles = 2;
2804     if(SWORDDIM == 3) sword_angles = 1;
2805     #if CAP_IRR
2806     else if(IRREGULAR) sword_angles = 840;
2807     #endif
2808     #if CAP_BT
2809     else if(bt::in()) sword_angles = 42;
2810     #endif
2811     #if CAP_ARCM
2812     else if(arcm::in()) {
2813       if(!PURE) possible_divisor((BITRUNCATED ? 2 : 1) * isize(arcm::current.faces));
2814       if(!DUAL) for(int f: arcm::current.faces) possible_divisor(f);
2815       }
2816     #endif
2817     else {
2818       possible_divisor(S7);
2819       if(BITRUNCATED) possible_divisor(S3);
2820       }
2821     }
2822 
pos2(cell * c,int s)2823   EX cell *pos2(cell *c, int s) {
2824     int t = c->type;
2825     if(hybri) t -= 2;
2826     s *= 2;
2827     s += sword_angles/t;
2828     s %= (2 * sword_angles);
2829     if(s<0) s += sword_angles * 2;
2830     s *= t;
2831     s /= (sword_angles * 2);
2832     return c->move(s);
2833     }
2834 
pos(cell * c,const sworddir & sd,bool rev)2835   EX cell *pos(cell *c, const sworddir& sd, bool rev) {
2836     if(SWORDDIM == 2)
2837       return pos2(c, sd.angle + (rev ? sword_angles/2 : 0));
2838     else {
2839       cell *best = NULL;
2840       ld bdist = HUGE_VAL;
2841       for(int i=0; i<S7; i++) {
2842         ld dist = hdist(sd.T * xpush0(rev?-0.1:0.1), tC0(currentmap->relative_matrix(c->move(i)->master, c->master, C0)));
2843         if(dist < bdist) bdist = dist, best = c->move(i);
2844         }
2845       return best;
2846       }
2847     }
2848 
orbof(bool rev)2849   eItem orbof(bool rev) { return rev ? itOrbSword2 : itOrbSword; }
orbcount(bool rev)2850   EX int orbcount(bool rev) { return items[orbof(rev)]; }
2851 
pos(int id,bool rev)2852   EX cell *pos(int id, bool rev) {
2853     if(!orbcount(rev)) return NULL;
2854     return pos(playerpos(id), dir[id], rev);
2855     }
2856 
at(cell * where,bool noplayer IS (false))2857   EX bool at(cell *where, bool noplayer IS(false)) {
2858     if(noplayer) return false;
2859     if(!orbcount(0) && !orbcount(1)) return false;
2860     for(int i=0; i<numplayers(); i++) if(multi::playerActive(i)) for(int b=0; b<2; b++)
2861       if(pos(i,b) == where) return true;
2862     return false;
2863     }
2864 
isnear(cell * where)2865   EX bool isnear(cell *where) {
2866     if(at(where, false)) return true;
2867     forCellEx(w2, where) if(at(w2, false)) return true;
2868     return false;
2869     }
2870 
2871   // from c1 to c2
shift(movei mi,sworddir d)2872   EX sworddir shift(movei mi, sworddir d) {
2873     cell *c1 = mi.s;
2874     cell *c2 = mi.t;
2875     if(!mi.proper()) return d;
2876     int s1 = mi.d;
2877     int s2 = mi.rev_dir_force();
2878     neighborId(c2, c1);
2879     if(s1 < 0 || s2 < 0) return d;
2880     if(SWORDDIM == 2) {
2881       int sub = (hybri) ? 2 : 0;
2882       int t2 = c2->type - sub;
2883       int t1 = c1->type - sub;
2884       if(t1 == 0 || t2 == 0) return d;
2885       if(c1->c.mirror(s1))
2886         d.angle = ((s2*sword_angles/t2 - d.angle + s1*sword_angles/t1) + sword_angles/2) % sword_angles;
2887       else
2888         d.angle = ((s2*sword_angles/t2 - s1*sword_angles/t1) + sword_angles/2 + d.angle) % sword_angles;
2889       }
2890     else {
2891       transmatrix T = currentmap->relative_matrix(c1, c2, C0);
2892       T = gpushxto0(tC0(T)) * T;
2893       d.T = T * d.T;
2894       fixmatrix(d.T);
2895       }
2896     return d;
2897     }
2898 
initial_matrix()2899   transmatrix initial_matrix() {
2900     if(S7 == 6) return Id;
2901     return cspin(0, 1, 0.1) * cspin(0, 2, 0.1) * cspin(1, 2, 0.1) * Id;
2902     }
2903 
initial(cell * c)2904   EX sworddir initial(cell *c) {
2905     sworddir res;
2906     res.angle = (sword::sword_angles / cwt.at->type + 1) / 2;
2907     if(SWORDDIM == 3) res.T = initial_matrix();
2908     return res;
2909     }
2910 
shuffle(int i)2911   void shuffle(int i) {
2912     changes.value_keep(dir[i].angle);
2913     changes.value_keep(dir[i].T);
2914     dir[i].angle = hrand(sword_angles);
2915     if(SWORDDIM == 3) dir[i].T = initial_matrix();
2916     }
2917 
reset()2918   EX void reset() {
2919     items[itOrbSword] = items[itOrbSword2] = 0;
2920     shuffle(multi::cpid);
2921     }
2922 
shuffle()2923   void shuffle() {
2924     for(int i=0; i<MAXPLAYER; i++) shuffle(i);
2925     }
2926 EX }
2927 
2928 EX namespace kraken {
2929 
head(cell * c)2930   EX cell *head(cell *c) {
2931     if(c->monst == moKrakenH) return c;
2932     if(c->monst == moKrakenT) return c->move(c->mondir);
2933     return NULL;
2934     }
2935 
kill(cell * c,eMonster who)2936   EX void kill(cell *c, eMonster who) {
2937     changes.ccell(c);
2938     drawParticles(c, minf[moKrakenH].color, 16);
2939     c->monst = moNone;
2940     if(checkOrb(who, itOrbUndeath)) c->monst = moFriendlyGhost;
2941     if(c->land == laKraken && !c->item) c->item = itKraken;
2942     kills[moKrakenH]++;
2943     if(checkOrb(who, itOrbStone)) c->wall = waNone;
2944     forCellEx(c1, c)
2945       if(c1->monst == moKrakenT) {
2946         changes.ccell(c1);
2947         drawParticles(c, minf[moKrakenT].color, 16);
2948         c1->monst = moNone;
2949         if(checkOrb(who, itOrbStone)) {
2950           if(isWatery(c1))
2951             c1->wall = waNone;
2952           else
2953             c1->wall = waPetrified, c1->wparam = moKrakenT;
2954           }
2955         }
2956     }
2957 
totalhp(cell * c)2958   EX int totalhp(cell *c) {
2959     int total = 0;
2960     forCellEx(c1, c)
2961       if(c1->monst == moKrakenT)
2962         total += c1->hitpoints;
2963     return total;
2964     }
2965 
2966   void trymove(cell *c);
2967 
sleep(cell * c)2968   void sleep(cell *c) {
2969     if(c->monst == moKrakenT) c = c->move(c->mondir);
2970     c->stuntime = 1;
2971     forCellEx(c2, c) c2->stuntime = 1;
2972     }
2973 
attacks()2974   EX void attacks() {
2975     pathdata pd(2);
2976     bool offboat[MAXPLAYER];
2977     for(int i=0; i<MAXPLAYER; i++) offboat[i] = false;
2978     for(int i=0; i<isize(dcal); i++) {
2979       cell *c = dcal[i];
2980       if(c->monst == moKrakenT && !c->stuntime) forCellEx(c2, c) {
2981         if (!logical_adjacent(c2,moKrakenT,c)) continue;
2982         bool dboat = false;
2983         if(c2->monst && canAttack(c, moKrakenT, c2, c2->monst, AF_ONLY_FBUG)) {
2984           attackMonster(c2, AF_NORMAL | AF_MSG, c->monst);
2985           sleep(c);
2986           }
2987         else for(int i=0; i<numplayers(); i++) if(playerpos(i) == c2) {
2988           if(isPlayerInBoatOn(c2, i)) {
2989             addMessage(XLAT("%The1 destroys your boat!", moKrakenH));
2990             dboat = true;
2991             offboat[i] = true;
2992             }
2993           else if(offboat[i]) ;
2994           else killThePlayer(moKrakenH, i, 0);
2995           sleep(c);
2996           }
2997         if(dboat) destroyBoats(c2, c, true);
2998         }
2999       }
3000     for(int i=0; i<isize(dcal); i++) {
3001       cell *c = dcal[i];
3002       if(c->monst == moKrakenH && !c->stuntime && !isWateryOrBoat(c)) {
3003         vector<cell*> ctab;
3004         forCellEx(c2, c) if(isWatery(c2)) ctab.push_back(c2);
3005         hrandom_shuffle(ctab);
3006         for(auto& cc: ctab) trymove(cc);
3007         }
3008       }
3009     }
3010 
3011   // c is the tentacle which will be the head after the move
trymove(cell * c)3012   EX void trymove(cell *c) {
3013     if(kraken_pseudohept(c)) return;
3014     cell *c2 = c->move(c->mondir);
3015     if(!isWatery(c)) return;
3016     if(againstCurrent(c, c2)) return;
3017     forCellIdEx(c3, i, c) {
3018       if(c3->monst && c3 != c2 && !(c3->mondir < c3->type &&
3019         c3->move(c3->mondir) == c2))
3020         return;
3021       if(isPlayerOn(c3)) return;
3022       if(sword::at(c3)) return;
3023       if(!passable(c3, c, P_FISH | P_MONSTER)) return;
3024       }
3025     if(c2->stuntime) return;
3026     int hpcount[10];
3027     forCellIdEx(c3, i, c2) {
3028       hpcount[i] = c3->hitpoints;
3029       c3->monst = moNone;
3030       }
3031     c->monst = moKrakenH;
3032     vector<pair<cell*, cell*> > acells;
3033     acells.push_back(make_pair(c2, c));
3034     forCellIdEx(c3, i, c) {
3035       c3->monst = moKrakenT, c3->mondir = c->c.spin(i), c3->monmirror = c->monmirror ^ c->c.mirror(i), onpath(c3, 0);
3036       int i0 = (i+c->c.spin(c->mondir)-c->mondir+96+c->type/2) % c2->type;
3037       c3->hitpoints = hpcount[i0];
3038       acells.push_back(make_pair(c2->move(i0), c3));
3039       if(c3->wall == waBoat) {
3040         addMessage(XLAT("%The1 destroys %the2!", moKrakenH, waBoat));
3041         c3->wall = waSea;
3042         }
3043       if(c3->wall == waStrandedBoat) {
3044         addMessage(XLAT("%The1 destroys %the2!", moKrakenH, waBoat));
3045         c3->wall = waNone;
3046         }
3047       }
3048 
3049     while(isize(acells)) {
3050       // bool found = false;
3051       for(int i=0; i<isize(acells); i++) {
3052         /* bool noconflict = true;
3053         for(int j=0; j<isize(acells); j++)
3054           if(acells[i].second == acells[j].first)
3055             noconflict = false; */
3056         /* if(noconflict) */ {
3057           // found = true;
3058           indAnimateMovement(match(acells[i].first, acells[i].second), LAYER_BIG);
3059           acells[i] = acells[isize(acells)-1];
3060           acells.resize(isize(acells)-1);
3061           i--;
3062           }
3063         }
3064       }
3065     commitAnimations(LAYER_BIG);
3066     sleep(c);
3067     onpath(c, 0);
3068     return;
3069     }
3070 
3071 EX }
3072 
barrierhept(cell * c)3073 bool barrierhept(cell *c) {
3074   return c->bardir != NOBARRIERS && c->bardir != NODIR;
3075   }
3076 
3077 #if CAP_FIELD
3078 EX namespace prairie {
3079 
3080   using namespace fieldpattern;
3081 
getfval(cell * c3)3082   int getfval(cell *c3) {
3083     if(barrierhept(c3)) return btspin(fieldval(c3).first, c3->bardir)+1;
3084     return 0;
3085     }
3086 
spread(cell * c,cell * from)3087   EX void spread(cell *c, cell *from) {
3088     int rd;
3089 
3090     c->LHU.fi.flowerdist = 8;
3091     c->LHU.fi.walldist = 8;
3092     c->LHU.fi.walldist2 = 8;
3093 
3094     if(ls::any_chaos()) {
3095       c->LHU.fi.rval = 0;
3096       }
3097     else if(quotient) { // fix cylinder
3098       c->LHU.fi.rval = 0;
3099       }
3100     else if(euclid) {
3101       auto co = euc2_coordinates(c);
3102       int y = co.second;
3103       c->LHU.fi.rval = (y&15);
3104       }
3105     else if(sphere) {
3106       c->LHU.fi.rval = celldistance(c, cwt.at) + 8 - (PURE ? 2 : 3);
3107       }
3108     else if(weirdhyperbolic) {
3109       c->LHU.fi.rval = max(celldist(c), 15);
3110       }
3111     else {
3112       if(geometry == gFieldQuotient || !from) {
3113         c->fval = currfp.distflower0;
3114         }
3115       else if(from && from->land == laPrairie && from->fval)
3116         c->fval = from->fval;
3117       else {
3118         forCellEx(c2, c) if(c2->land == laPrairie && c2->fval)
3119           c->fval = c2->fval;
3120         if(!c->fval) forCellEx(c2, c) if(!c->fval) c->fval = getfval(c2);
3121         if(!c->fval) forCellEx(c2, c) forCellEx(c3, c2) if(!c->fval) c->fval = getfval(c3);
3122         if(!c->fval) {
3123 
3124           int barclose = 0;
3125 
3126           forCellEx(c2, c) if(barrierhept(c2)) barclose++;
3127 
3128           forCellEx(c2, c) forCellEx(c3, c2)
3129           if(barrierhept(c3)) barclose++;
3130 
3131           printf("c = %p bc = %d\n", hr::voidp(c), barclose);
3132 
3133           raiseBuggyGeneration(c, "could not set river fval");
3134           }
3135         }
3136       pair<int,bool> fv = fieldpattern::fieldval(c);
3137       fv = currfp.gmul(fv, currfp.inverses[c->fval-1]);
3138 
3139       rd = currfp.getdist(fv, currfp.distriver);
3140       int rl = currfp.getdist(fv, currfp.distriverleft);
3141       int rr = currfp.getdist(fv, currfp.distriverright);
3142 
3143       c->LHU.fi.flowerdist = currfp.getdist(fv, currfp.distflower);
3144       c->LHU.fi.walldist   = currfp.getdist(fv, currfp.distwall);
3145       c->LHU.fi.walldist2  = currfp.getdist(fv, currfp.distwall2);
3146 
3147       c->LHU.fi.rval = 0;
3148       if(rd <= 7 && rl < rr)
3149         c->LHU.fi.rval = 8 + rd;
3150       if(rd <= 7 && rl > rr)
3151         c->LHU.fi.rval = 7 - rd;
3152       }
3153 
3154     if(c->LHU.fi.flowerdist == 0 && c->type != 6) c->item = itOrbSafety;
3155 
3156     if(c->LHU.fi.walldist == 0) c->wall = waBarrier;
3157 
3158     if(0) if(c->type == 7) for(int i=0; i<7; i++) {
3159       eItem m = currfp.markers[fieldpattern::btspin(c->master->fieldval,i)];
3160       if(m) {
3161         if(c->item) c->item = itBuggy2;
3162         else c->item = m, c->mondir = i;
3163         if(c->wall == waSea) c->wall = waBoat;
3164         }
3165       }
3166     }
3167 
3168   #define RLOW (sphere?(PURE?7:6):PURE?4:2)
3169   #define RHIGH (sphere?(PURE?8:9):PURE?11:13)
3170 
no_worms(cell * c)3171   EX bool no_worms(cell *c) {
3172     if(c->land != laPrairie) return false;
3173     int rv = c->LHU.fi.rval;
3174     return rv > RLOW+1 && rv < RHIGH-1;
3175     }
3176 
isriver(cell * c)3177   EX bool isriver(cell *c) {
3178     return c->land == laPrairie && c->LHU.fi.rval <= RHIGH && c->LHU.fi.rval >= RLOW;
3179     }
3180 
mainriver(cell * c)3181   bool mainriver(cell *c) {
3182     return c->LHU.fi.rval <= 8 && c->LHU.fi.rval >= 7;
3183     }
3184 
nearriver(cell * c)3185   EX bool nearriver(cell *c) {
3186     return c->LHU.fi.rval == RHIGH+1 || c->LHU.fi.rval == RLOW-1;
3187     }
3188 
3189   cell *enter;
3190 
opposite(cell * c)3191   bool opposite(cell *c) {
3192     return (c->LHU.fi.rval ^ enter->LHU.fi.rval) & 8;
3193     }
3194 
isleft(cell * c)3195   bool isleft(cell *c) {
3196     return c->LHU.fi.rval & 8;
3197     }
3198 
towerleft(cell * c)3199   int towerleft(cell *c) {
3200     return c->LHU.fi.rval;
3201     }
3202 
towerright(cell * c)3203   int towerright(cell *c) {
3204     return 15^c->LHU.fi.rval;
3205     }
3206 
3207   EX cell *next(cell *c, int pv IS(1)) {
3208     for(int i=0; i<c->type; i++) {
3209       cell *c1 = createMov(c, i);
3210       cell *c2 = createMov(c, (i+pv+c->type)%c->type);
3211       if(c1 && c1->LHU.fi.rval == c->LHU.fi.rval)
3212       if(c2 && c2->LHU.fi.rval == c->LHU.fi.rval+1)
3213       if(isNeighbor(c1,c2))
3214         return c1;
3215       }
3216     return NULL;
3217     }
3218 
prev(cell * c)3219   cell *prev(cell *c) { return next(c, -1); }
3220 
beastdist(cell * c,int dir)3221   int beastdist(cell *c, int dir) {
3222     cell *cx = c; int n=0;
3223     while(true) {
3224       if(cx->monst == moHerdBull) return n;
3225       cx = next(cx, dir);
3226       n++;
3227       if(!cx || n >= 1000) return 1000;
3228       }
3229     }
3230 
3231   EX vector<cell*> beaststogen;
3232 
generateBeast(cell * c)3233   EX void generateBeast(cell *c) {
3234     int beastdistance = min(beastdist(c, 1), beastdist(c, -1));
3235     if(hrand(1000) >= 15 * beastdistance + 2 * items[itGreenGrass]) return;
3236     c->monst = moHerdBull;
3237     cell *c2 = prev(c);
3238     if(c2) c->mondir = neighborId(c, c2);
3239     }
3240 
moveAt(cell * c,manual_celllister & cl)3241   void moveAt(cell *c, manual_celllister& cl) {
3242     if(!cl.add(c)) return;
3243     vector<cell*> whirlline;
3244     whirlline.push_back(c);
3245     cell *c2 = prev(c);
3246     while(c2 && cl.add(c2)) {
3247       whirlline.push_back(c2);
3248       c2 = prev(c2);
3249       // in sphere/quotient geometries, never break before a bull
3250       if((sphere || quotient) && !c2->monst) break;
3251       }
3252     reverse(whirlline.begin(), whirlline.end());
3253     c2 = next(c);
3254     while(c2 && cl.add(c2)) whirlline.push_back(c2), c2 = next(c2);
3255     int qty = isize(whirlline);
3256     if(shmup::on) {
3257       for(int i=0; i<qty; i++) if(whirlline[i]->cpdist <= gamerange()) {
3258         generateBeast(whirlline[i]);
3259         break;
3260         }
3261       }
3262     else {
3263       c2 = whirlline[qty-1];
3264       if(c2->monst == moHerdBull) c2->monst = moNone;
3265       if(!shmup::on) for(int q=qty-2; q>=0; q--) {
3266         cell *cp = whirlline[q];
3267         cell *cn = whirlline[q+1];
3268         /* just pretend the roadblocks disappear */
3269         if(cn->monst == moRagingBull && cn->cpdist == INFD && cn->stuntime) {
3270           cn->stuntime--;
3271           if(cn->stuntime == 0) cn->monst = moNone;
3272           }
3273         if(cp->monst == moHerdBull && !cp->stuntime) {
3274           cp->mondir = neighborId(cp, cn);
3275           beastAttack(cp, true, true);
3276 
3277           if(!cn->monst && !isPlayerOn(cn) && passable_for(cp->monst, cn, cp, P_DEADLY))
3278             moveMonster(movei(cp, cn, TELEPORT));
3279           else {
3280             playSound(NULL, "hit-axe"+pick123());
3281             beastcrash(cn, cp);
3282             cp->monst = moRagingBull;
3283             cp->stuntime = 3;
3284             cp->mondir = NODIR;
3285             }
3286           }
3287         }
3288       if(!sphere && !quotient) generateBeast(whirlline[0]);
3289       }
3290     }
3291 
move()3292   EX void move() {
3293     if(ls::any_chaos()) return;
3294     manual_celllister cl;
3295     for(int i=0; i<isize(dcal); i++) {
3296       cell *c = dcal[i];
3297       if(isriver(c)) moveAt(c, cl);
3298       }
3299     for(int i=0; i<isize(beaststogen); i++)
3300       generateBeast(beaststogen[i]);
3301     beaststogen.clear();
3302     }
3303 
3304   vector<cell*> tchoices;
3305 
3306   cell *lasttreasure;
3307 
3308   // vector<cell*> orbs;
3309 
thisriver(cell * c)3310   bool thisriver(cell *c) {
3311     forCellEx(c2, c) if(c2->mpdist < c->mpdist && !isriver(c2)) return false;
3312     forCellEx(c2, c) if(c2->mpdist < c->mpdist) return thisriver(c2);
3313     return true;
3314     }
3315 
generateTreasure(cell * c)3316   EX void generateTreasure(cell *c) {
3317 //    if(nearriver(c) && op
3318     if(enter && nearriver(c) && opposite(c) && thisriver(c)) {
3319       int hr = hrand(100);
3320       if(hr == 0 && items[itGreenGrass] >= 10 && !inv::on) {
3321         c->item = itOrbBull;
3322         // orbs.push_back(c);
3323         }
3324       else if(hr < 1+PRIZEMUL) {
3325         placePrizeOrb(c);
3326         // if(c->item) orbs.push_back(c);
3327         }
3328       else tchoices.push_back(c);
3329       }
3330     }
3331 
treasures()3332   EX void treasures() {
3333     if(enter && !isriver(cwt.at)) enter = NULL;
3334     else if(!enter && isriver(cwt.at)) enter = cwt.at;
3335     if(isize(tchoices)) {
3336       if(lasttreasure && lasttreasure->item == itGreenGrass) {
3337         if(celldistance(lasttreasure, cwt.at) >= (PURE ? 7 : 10)) {
3338           lasttreasure->item = itNone;
3339           forCellEx(c2, lasttreasure) if(c2->item == itGreenGrass) c2->item = itNone;
3340           }
3341         else { tchoices.clear(); return; }
3342         }
3343       if(isize(tchoices) < 3) { tchoices.clear(); return; }
3344       lasttreasure = tchoices[hrand(isize(tchoices))];
3345       lasttreasure->item = itGreenGrass;
3346       for(int i=0; i<isize(tchoices); i++) if(isNeighbor(tchoices[i], lasttreasure))
3347         tchoices[i]->item = itGreenGrass;
3348       tchoices.clear();
3349       }
3350     }
3351 EX }
3352 #else
3353 namespace prairie {
no_worms(cell * c)3354   bool no_worms(cell *c) { return false; }
treasures()3355   void treasures() { }
move()3356   void move() { }
isriver(cell * c)3357   bool isriver(cell *c) { return false; }
3358   }
3359 #endif
3360 
3361 EX namespace ca {
3362   EX ld prob = .2;
3363   static const int MAX_NEIGHBOR = 60; /* may be larger than MAX_EDGE due to mineadj */
3364   string carule[MAX_NEIGHBOR][2];
3365 
3366   EX eWall wlive = waFloorA;
3367 
3368   EX set<cell*> changed;
3369 
list_adj(cell * c)3370   EX void list_adj(cell *c) {
3371     changed.insert(c);
3372     for(cell* c1: adj_minefield_cells(c))
3373       changed.insert(c1);
3374     }
3375 
3376   // you can also do -mineadj
3377 
fillup(string s)3378   EX string fillup(string s) {
3379     while(isize(s) < MAX_NEIGHBOR) s += '0';
3380     return s;
3381     }
3382 
init()3383   EX void init() {
3384     // hexagonal variant of Game of Life, as suggested by Wikipedia
3385     for(int i=0; i<MAX_NEIGHBOR; i++)
3386       carule[i][0] = fillup("00100"),
3387       carule[i][1] = fillup("00011");
3388     }
3389 
3390 #if CAP_COMMANDLINE
readArg()3391   bool readArg() {
3392     using namespace arg;
3393     if(argis("-caprob")) {
3394       shift(); prob = argf();
3395       return 0;
3396       }
3397     if(argis("-calive")) {
3398       shift(); wlive = eWall(argi());
3399       return 0;
3400       }
3401     if(argis("-carun")) {
3402       shift(); int iter = argi();
3403       start_game();
3404       for(int i=0; i<iter; i++) simulate();
3405       return 0;
3406       }
3407     if(args()[0] != '-') return 1;
3408     if(args()[1] != 'c') return 1;
3409     int livedead = args()[2] - '0';
3410     if(livedead < 0 || livedead > 1) return 1;
3411     int nei = -1;
3412     if(args()[3]) {
3413       nei = atoi(argcs()+3);
3414       if(nei < 0 || nei >= MAX_NEIGHBOR) return 1;
3415       }
3416     shift();
3417     string s = args();
3418     if(nei == -1) for(int i=0; i<MAX_NEIGHBOR; i++)
3419       carule[i][livedead] = fillup(s);
3420     else
3421       carule[nei][livedead] = fillup(s);
3422     return 0;
3423     }
3424 
3425   auto ah = addHook(hooks_args, 0, readArg);
3426 #endif
3427 
simulate()3428   EX void simulate() {
3429     if(cwt.at->land != laCA) return;
3430     if(items[itOrbAether] < 2) items[itOrbAether] = 2;
3431     vector<cell*> allcells;
3432     for(cell *c: changed) allcells.push_back(c);
3433     changed.clear();
3434     int dcs = isize(allcells);
3435     std::vector<bool> willlive(dcs);
3436     int old = 0, xold = 0;
3437     for(int i=0; i<dcs; i++) {
3438       cell *c = allcells[i];
3439       if(c->land != laCA) continue;
3440       int nei = 0, live = 0;
3441       for(cell *c2: adj_minefield_cells(c)) {
3442         nei++; if(c2->wall == wlive) live++;
3443         }
3444       int welive = 0; if(c->wall == wlive) welive++;
3445       willlive[i] = carule[nei][welive][live] == '1';
3446       old += welive, xold += live;
3447       }
3448     for(int i=0; i<dcs; i++) {
3449       cell *c = allcells[i];
3450       auto last = c->wall;
3451       c->wall = willlive[i] ? wlive : waNone;
3452       if(c->wall != last) {
3453         dynamicval<ld> d(prob, 0);
3454         setdist(c, 7, nullptr);
3455         list_adj(c);
3456         }
3457       }
3458     println(hlog, make_tuple(dcs, old, xold, isize(changed)));
3459     }
3460 EX }
3461 
__anone93748550102() 3462 auto ccm = addHook(hooks_clearmemory, 0, [] () {
3463   heat::offscreen_heat.clear();
3464   heat::offscreen_fire.clear();
3465   princess::clear();
3466   mirror::mirrors.clear();
3467   clearing::bpdata.clear();
3468   clearing::stats.clear();
3469   clearing::score.clear();
3470   tortoise::emap.clear();
3471   tortoise::babymap.clear();
3472   dragon::target = NULL;
3473   #if CAP_FIELD
3474   prairie::lasttreasure = NULL;
3475   prairie::enter = NULL;
3476   prairie::tchoices.clear();
3477   prairie::beaststogen.clear();
3478   #endif
3479   mirror::clearcache();
3480   }) +
__anone93748550202(gamedata* gd) 3481   addHook(hooks_gamedata, 0, [] (gamedata* gd) {
3482     gd->store(heat::offscreen_heat);
3483     gd->store(heat::offscreen_fire);
3484     gd->store(princess::infos);
3485     gd->store(mirror::mirrors);
3486     gd->store(clearing::bpdata);
3487     gd->store(tortoise::emap);
3488     gd->store(tortoise::babymap);
3489     gd->store(prairie::lasttreasure);
3490     gd->store(prairie::enter);
3491     gd->store(prairie::tchoices);
3492     gd->store(prairie::beaststogen);
3493     gd->store(sword::dir);
3494     gd->store(elec::haveelec);
3495     gd->store(elec::havecharge);
3496     gd->store(elec::lightningfast);
3497     gd->store(elec::havethunder);
3498     gd->store(elec::afterOrb);
3499     }) +
__anone93748550302() 3500   addHook(hooks_removecells, 0, [] () {
3501     for(cell *c: removed_cells) clearing::score.erase(c);
3502     eliminate_if(heat::offscreen_heat, is_cell_removed);
3503     eliminate_if(heat::offscreen_fire, is_cell_removed);
3504     eliminate_if(princess::infos, [] (princess::info*& i) {
3505       if(is_cell_removed(i->princess) || is_cell_removed(i->prison)) {
3506         DEBB(DF_MEMORY, ("removing a princess"))
3507         if(i->princess && !is_cell_removed(i->princess)) {
3508           DEBB(DF_MEMORY, ("faking a princess"))
3509           princess::newFakeInfo(i->princess);
3510           }
3511         delete i;
3512         return true;
3513         }
3514       return false;
3515       });
3516     set_if_removed(dragon::target, NULL);
3517     #if CAP_FIELD
3518     set_if_removed(prairie::lasttreasure, NULL);
3519     set_if_removed(prairie::enter, NULL);
3520     eliminate_if(prairie::tchoices, is_cell_removed);
3521     eliminate_if(prairie::beaststogen, is_cell_removed);
3522     #endif
3523     for(auto& bpd: clearing::bpdata) set_if_removed(bpd.second.root, NULL);
3524     });
3525 
3526 #if CAP_FIELD
3527 // windcode arrays precomputed for speed
3528 
3529 int windcodes18920[] = {164,182,171,158,148,155,163,174,188,205,193,160,167,160,140,128,146,153,159,161,167,180,190,186,202,229,234,24,120,146,165,177,179,169,146,125,113,93,131,157,147,155,158,161,162,157,157,162,173,187,204,198,176,163,242,249,234,238,1,30,77,120,140,153,165,173,173,204,200,184,165,150,130,119,104,86,70,71,54,208,168,141,133,167,169,158,161,161,166,171,165,142,144,153,159,166,175,186,203,225,221,216,191,168,149,114,70,26,10,241,220,229,244,251,7,19,50,85,110,129,143,148,145,173,180,178,174,166,144,54,248,215,182,170,166,153,134,116,118,122,114,96,77,57,47,69,78,62,18,228,189,164,148,126,110,81,250,217,169,151,156,163,168,156,154,170,183,192,182,149,118,120,138,152,158,156,158,164,169,171,181,202,217,234,252,241,243,235,206,182,169,159,149,125,97,77,53,40,28,24,4,237,218,192,204,220,244,246,248,250,1,4,12,31,56,98,120,127,129,134,148,156,161,139,105,153,209,205,187,177,180,177,174,167,152,117,81,44,4,232,201,160,162,161,168,174,165,153,139,115,94,103,120,129,127,125,118,94,66,50,40,28,10,50,78,89,97,81,76,54,16,230,195,174,162,157,151,139,122,98,81,58,27,249,245,2,7,89,127,132,146,158,162,174,191,230,64,124,150,173,193,209,224,211,203,174,146,119,87,90,93,118,139,159,167,164,157,149,150,155,165,172,169,159,161,172,195,214,219,221,206,26,19,252,253,11,9,253,225,206,187,170,168,166,155,149,141,124,101,83,81,68,55,48,45,38,29,29,43,24,254,232,219,199,171,159,174,187,208,249,17,219,241,250,248,241,244,248,1,1,3,17,30,37,63,128,134,147,141,135,125,124,134,150,157,161,173,186,161,114,79,52,50,236,241,243,236,163,169,168,181,186,184,184,178,171,172,170,149,131,108,91,73,64,41,10,233,216,202,112,119,144,154,152,161,177,186,182,176,166,152,145,133,106,85,67,76,85,112,135,142,129,124,134,132,139,125,89,71,42,32,26,32,45,227,220,243,26,71,91,97,112,127,106,89,83,88,88,65,30,255,229,201,174,171,164,157,154,161,153,146,142,139,137,93,78,75,67,57,36,3,232,233,241,255,26,30,48,73,93,105,116,121,132,147,161,169,157,151,180,201,219,232,6,32,94,129,142,159,177,190,207,220,241,252,237,226,223,212,193,170,144,119,95,72,56,60,62,70,96,124,136,149,170,177,183,178,171,154,149,144,141,144,150,157,168,175,184,178,167,146,139,143,148,180,196,208,228,229,232,231,209,170,117,73,52,48,251,241,251,6,26,41,37,26,242,232,217,212,191,169,155,160,167,174,177,161,147,145,144,150,147,130,100,75,65,83,79,79,75,40,45,49,48,49,46,45,35,18,19,41,83,56,19,255,239,220,221,219,208,184,155,138,133,145,160,167,179,185,226,14,46,119,196,237,243,251,8,1,243,232,231,234,235,249,9,5,248,245,247,7,18,21,21,21,29,33,223,187,158,154,168,167,153,142,128,115,118,114,116,133,167,159,162,162,170,184,194,216,208,209,109,92,65,45,19,13,255,241,247,253,255,14,26,82,139,164,157,144,167,183,193,192,188,192,197,189,179,173,167,169,173,184,191,172,151,138,123,108,98,87,81,75,78,70,51,29,248,222,213,198,230,39,67,83,91,123,146,157,156,140,140,151,176,195,198,187,185,185,182,175,159,145,144,146,150,141,96,68,57,51,50,54,54,63,99,129,153,157,160,140,124,114,89,153,144,133,140,158,162,146,59,58,72,60,18,3,6,6,16,22,45,75,123,170,197,219,228,245,50,102,98,102,98,100,110,149,166,132,108,84,80,82,87,90,102,120,108,92,254,11,248,236,226,205,171,147,159,161,163,163,153,145,141,154,185,165,146,146,144,142,143,150,174,224,57,69,68,66,63,68,69,64,49,42,15,226,206,217,228,232,231,245,27,49,47,43,37,50,71,90,102,100,103,106,117,113,117,126,136,148,165,177,199,246,63,107,124,217,215,225,230,225,221,238,2,15,13,1,171,134,132,135,147,165,179,185,191,202,212,227,244,9,18,24,1,248,240,242,233,221,220,209,192,178,161,143,125,108,96,79,64,53,37,29,34,38,51,53,68,86,116,140,130,136,154,173,189,190,194,193,191,191,189,164,139,145,149,144,141,140,129,128,143,154,145,159,174,173,173,186,203,193,183,177,158,129,126,120,121,116,113,165,195,207,214,207,201,20,239,229,239,251,248,225,198,178,149,133,93,76,72,73,101,137,214,221,227,242,253,1,14,31,51,79,73,78,114,201,238,243,210,212,220,226,209,189,167,147,142,145,156,166,173,173,194,199,171,152,142,137,138,134,134,154,165,166,152,141,118,88,64,35,15,101,91,76,72,78,103,163,251,11,33,45,52,56,42,20,87,58,42,49,57,23,244,2,14,8,221,138,125,95,86,40,255,4,246,224,206,203,213,221,224,220,204,185,165,146,124,108,115,117,127,127,138,148,161,165,163,152,165,201,243,35,54,79,118,160,201,238,20,248,228,253,10,24,25,255,234,231,221,224,221,223,224,223,233,254,16,28,21,7,236,227,232,236,241,254,15,26,22,18,13,10,9,19,19,12,254,254,228,205,183,163,150,161,188,194,194,173,160,147,131,118,105,99,114,119,108,97,93,54,215,185,161,156,163,167,162,158,164,176,197,196,209,229,246,238,238,0,23,54,135,85,38,49,35,13,249,251,243,244,233,224,253,0,254,1,2,10,25,34,59,92,114,146,167,181,171,126,109,136,177,193,199,207,201,196,188,183,193,206,210,204,194,183,165,172,169,158,161,169,159,171,190,209,214,199,174,153,140,143,143,120,114,115,102,83,84,83,75,72,79,90,87,90,81,73,49,254,228,215,209,208,202,180,148,66,58,49,52,66,60,63,66,94,122,144,155,168,166,169,208,49,109,129,126,172,199,215,215,212,193,183,183,184,187,188,195,196,181,166,152,141,131,135,142,143,152,185,246,69,117,77,26,31,37,51,45,43,40,38,28,31,35,73,105,127,150,169,177,177,179,172,153,132,114,112,106,89,47,244,180,142,120,125,138,155,172,179,191,196,247,30,39,34,56,97,88,29,250,235,220,235,245,252,2,7,5,15,37,61,78,99,125,135,165,198,212,216,219,220,221,217,221,150,130,111,112,113,102,97,101,104,90,114,213,202,216,168,126,122,119,84,52,69,73,78,88,91,85,80,100,125,149,147,135,142,165,198,4,41,13,235,241,239,232,217,189,156,128,101,150,154,154,155,160,174,164,156,147,139,126,129,137,146,230,228,212,133,137,141,144,145,143,141,140,142,149,157,171,208,242,16,58,67,64,63,68,62,51,52,77,80,78,79,65,51,46,58,50,245,184,186,177,197,204,220,233,234,237,219,195,201,47,50,54,71,69,64,54,24,29,39,55,72,87,99,106,119,107,97,96,95,93,114,132,146,66,94,114,126,131,135,139,151,175,180,166,197,248,15,25,35,63,86,80,44,3,222,224,238,247,238,227,221,211,202,216,239,9,18,22,24,6,247,217,178,148,131,125,131,130,131,139,156,174,187,185,184,193,201,205,205,212,221,233,249,8,18,37,58,86,4,7,6,1,236,248,9,0,239,217,216,226,233,221,217,208,193,180,170,159,153,135,125,110,103,96,82,74,69,57,52,50,42,25,15,255,5,11,33,40,44,45,45,50,56,71,108,144,168,161,90,117,125,139,157,178,193,205,205,206,209,201,196,191,197,203,212,213,182,151,119,110,130,150,159,146,137,134,142,146,142,126,100,113,146,166,170,149,128,121,187,178,176,177,169,169,188,210,232,211,197,195,189,185,178,152,117,95,108,115,110,109,102,89,96,83,71,243,207,203,210,226,231,233,216,173,116,69,51,2,213,220,234,0,25,14,7,245,232,213,195,190,164,113,136,133,49,67,75,77,78,75,89,124,135,149,174,197,202,212,216,230,242,252,252,245,4,18,24,31,50,81,119,108,97,97,103,124,154,174,204,2,25,75,167,189,210,223,229,240,242,227,209,188,169,135,115,145,134,129,130,143,157,161,180,187,166,162,242,230,228,195,166,150,143,139,131,130,135,131,124,120,110,139,168,180,191,176,164,161,155,138,132,114,77,55,28,1,250,221,188,151,115,93,77,65,64,57,63,92,131,165,199,214,238,254,16,42,56,39,52,72,72,56,22,244,187,130,79,44,20,31,52,64,82,99,196,220,237,249,0,34,43,219,213,195,160,144,134,118,100,101,124,212,227,3,24,10,250,230,205,194,192,185,199,215,226,228,228,230,237,221,205,192,159,161,163,142,115,85,92,96,99,106,118,121,120,114,121,128,132,153,166,168,163,155,143,129,128,141,181,208,233,17,62,74,69,60,80,123,146,179,204,199,233,30,57,78,169,196,237,9,14,15,18,35,45,57,42,216,227,235,223,210,207,218,223,213,211,216,217,219,215,213,217,242,6,22,35,43,43,34,22,248,218,210,203,214,224,238,237,230,241,4,21,31,44,27,22,20,14,14,3,251,253,7,14,21,27,4,246,237,6,39,9,243,222,210,200,193,177,156,122,144,165,187,219,223,224,216,191,175,171,159,149,141,127,117,111,108,80,71,111,124,133,124,106,89,78,84,74,52,14,228,201,169,155,142,147,163,169,170,175,168,154,148,158,157,141,216,214,197,189,200,216,234,251,19,13,251,248,254,18,23,17,4,218,181,177,1,7,14,46,65,36,255,241,234,234,236,232,228,232,229,219,219,213,185,83,21,254,246,255,9,0,4,6,18,36,36,37,43,80,107,110,115,126,177,182,193,199,195,202,27,71,79,100,111,145,211,208,210,206,213,222,214,207,203,196,188,181,171,180,209,222,219,220,217,217,207,201,194,166,122,170,179,172,163,145,144,150,171,184,112,145,170,191,213,238,234,228,225,222,197,173,150,131,126,130,160,167,166,73,100,117,135,133,107,73,64,76,85,88,86,77,64,63,75,86,100,106,97,96,102,103,100,106,93,100,2,230,222,208,209,212,194,202,202,206,206,179,147,121,97,79,71,46,33,31,53,66,66,46,37,39,42,60,73,89,123,160,159,148,163,191,179,170,166,179,209,244,26,79,119,134,123,102,82,252,216,219,229,234,233,245,206,192,184,175,177,179,175,185,191,188,179,192,224,217,197,183,170,163,159,145,141,132,120,116,135,147,145,139,134,131,178,244,28,43,30,202,177,168,244,252,251,0,12,57,63,50,34,36,49,38,26,29,24,18,1,2,2,21,51,84,116,132,144,158,167,180,184,201,198,191,186,187,185,166,148,131,113,98,105,107,114,116,104,73,37,5,223,192,163,133,111,87,101,118,134,155,164,169,189,190,201,210,196,231,21,23,32,36,30,26,15,7,228,152,153,187,3,11,23,201,209,199,186,204,219,235,249,251,248,253,1,252,249,254,2,23,56,66,69,77,87,95,108,115,109,127,162,199,230,223,217,217,215,218,226,219,211,205,204,209,198,187,165,138,112,107,119,128,126,108,98,91,90,98,117,147,68,59,72,47,15,239,227,233,247,10,101,112,120,125,132,139,120,20,4,52,71,63,65,75,84,95,100,98,85,65,56,85,116,138,150,174,177,178,162,159,148,150,154,141,154,2,30,80,111,199,218,236,251,249,248,243,238,230,230,208,179,151,118,77,33,253,176,162,153,147,149,152,148,127,195,195,175,161,155,153,144,136,125,100,103,120,145,142,104,63,5,242,245,1,47,96,125,140,137,136,142,149,150,143,145,145,135,137,137,138,148,154,153,145,151,189,220,235,1,255,238,64,78,71,62,58,56,54,70,79,70,35,28,16,60,106,95,89,86,91,97,78,70,49,34,33,57,76,79,86,79,145,146,158,164,145,151,188,187,191,210,227,241,243,236,236,246,252,164,142,161,165,139,105,71,54,44,56,100,97,84,73,79,105,10,243,5,21,34,44,57,69,77,88,101,102,108,120,135,139,122,104,86,89,95,91,84,75,84,123,143,162,182,223,17,62,94,116,123,130,133,132,127,128,134,138,141,143,235,209,185,146,111,23,15,17,28,35,29,11,11,35,48,65,76,85,64,32,24,36,214,205,211,254,5,21,6,236,226,225,225,218,207,195,183,189,209,232,2,26,26,26,27,38,44,24,255,244,236,228,204,175,152,137,131,117,111,126,136,136,124,116,120,134,146,165,179,188,202,195,186,181,179,184,198,207,211,209,201,200,205,218,222,222,226,254,16,18,12,13,30,51,73,108,142,179,236,5,5,10,26,48,213,208,239,8,33,36,6,239,9,198,196,209,226,239,250,244,228,217,222,227,210,195,185,178,175,172,162,162,169,148,119,123,114,99,97,105,105,97,81,69,70,75,67,55,48,47,43,54,55,33,20,20,10,249,233,233,235,234,13,51,47,34,35,40,42,47,40,38,41,33,44,55,77,104,155,179,191,196,243,41,87,107,116,117,125,139,151,168,186,192,202,209,224,211,214,219,221,218,211,202,198,198,188,182,204,211,217,233,238,245,213,185,160,141,92,80,83,107,130,164,174,178,159,143,134,126,125,133,145,156,153,150,149,161,73,57,75,87,184,184,199,226,146,153,146,122,85,19,214,187,171,170,179,182,186,178,165,145,155,198,209,245,14,254,230,214,179,191,220,186,179,192,197,200,178,153,102,72,60,83,102,114,123,93,101,109,101,77,61,82,92,90,68,53,23,251,228,207,199,197,206,222,239,249,1,5,246,6,177,157,135,98,70,61,70,86,170,180,194,225,224,224,243,34,68,40,23,16,13,2,243,238,227,203,190,194,202,209,246,66,63,98,153,173,213,7,39,70,89,75,73,88,85,73,67,49,131,146,143,146,144,142,160,177,182,187,195,199,205,205,211,221,229,239,1,10,2,235,220,243,4,13,19,21,22,22,39,65,81,124,174,146,130,114,110,99,97,110,114,125,146,163,161,163,191,34,47,45,62,88,123,159,172,191,218,227,233,228,236,252,255,1,249,230,216,202,187,175,188,141,72,14,179,155,138,116,113,111,122,131,142,175,157,131,175,195,201,215,186,152,127,73,20,5,1,2,239,217,189,155,136,134,140,145,145,130,119,120,120,134,139,133,124,118,114,104,82,77,114,152,184,196,205,225,223,150,160,163,169,182,174,134,128,137,141,160,79,67,61,47,23,247,233,236,227,209,200,199,187,170,144,111,98,86,75,60,53,61,53,30,38,49,65,84,118,146,131,218,224,211,219,226,236,250,7,10,11,89,92,31,1,218,137,104,97,72,78,67,242,252,250,221,190,163,152,110,76,51,13,242,1,23,44,80,70,77,91,103,133,189,209,210,221,230,235,236,237,252,55,95,143,186,205,216,220,219,191,164,143,138,142,136,116,94,88,90,115,140,171,197,194,246,28,46,44,34,14,248,241,212,182,188,190,184,191,178,160,178,194,208,221,232,238,233,227,223,230,242,5,248,212,203,215,235,151,127,131,203,182,147,138,142,53,30,70,102,94,83,79,84,104,116,126,124,122,118,110,101,107,114,119,108,111,147,161,170,175,176,174,162,153,143,135,126,116,107,96,106,105,158,200,192,197,217,1,37,66,97,106,116,68,41,37,24,119,137,140,147,157,205,230,212,190,156,64,56,57,73,86,105,119,144,174,222,255,19,27,26,26,12,7,15,26,56,62,66,78,87,126,169,205,229,245,251,231,213,203,194,191,207,226,237,222,202,198,197,207,216,215,215,219,221,210,200,190,197,229,254,9,13,24,38,48,47,49,53,61,58,55,37,4,233,212,193,192,185,185,196,205,213,231,251,249,240,226,213,224,232,247,17,34,27,54,85,56,32,19,21,26,16,7,12,20,25,245,230,233,250,2,9,13,16,19,35,61,21,236,235,222,207,233,41,80,89,48,12,247,230,219,215,207,203,209,213,179,189,194,95,71,116,144,151,162,178,212,247,2,0,255,232,238,217,188,175,177,184,171,154,138,153,152,140,124,112,103,106,123,130,100,44,31,2,105,133,132,136,147,148,133,116,93,80,65,54,80,85,78,72,63,35,19,4,235,215,176,151,144,132,117,126,136,158,173,174,174,165,170,186,186,165,140,135,134,150,163,168,133,105,58,251,221,214,204,182,171,186,203,212,222,236,247,252,15,49,52,46,7,248,248,247,3,25,40,40,40,16,251,228,213,201,183,173,184,227,238,0,7,7,234,129,99,59,35,23,194,217,221,227,233,228,226,225,220,218,215,226,230,227,218,206,208,216,227,226,193,143,106,44,3,242,245,237,233,243,31,24,253,246,253,7,250,2,27,47,53,45,39,49,21,26,116,116,126,118,117,109,87,30,203,202,205,204,211,208,209,210,200,202,234,10,30,53,63,78,82,82,91,112,122,50,22,239,217,223,214,209,219,231,241,226,219,216,212,206,201,199,190,180,185,173,153,154,151,201,250,225,225,230,225,218,221,229,230,215,209,212,208,205,204,231,65,81,87,3,206,183,170,171,165,138,123,127,124,131,171,191,218,13,66,108,136,159,173,183,197,215,236,7,17,241,214,228,239,248,244,219,195,180,163,150,132,110,106,104,109,140,188,195,198,220,243,3,62,102,110,125,152,166,170,207,17,44,39,46,64,74,82,95,95,93,90,86,67,44,49,49,67,79,82,91,110,120,121,107,97,96,101,112,113,118,116,121,129,122,110,114,149,180,221,214,209,202,188,191,202,226,253,179,170,194,204,202,194,199,235,236,14,91,126,130,119,84,84,88,88,91,64,30,16,3,9,34,60,73,84,84,56,30,25,11,13,12,26,49,62,68,61,63,87,97,186,200,184,162,111,136,183,223,226,198,181,172,159,156,160,185,214,226,231,246,6,47,88,117,145,154,134,113,107,98,66,33,8,236,221,221,229,232,243,1,243,251,18,36,122,170,186,181,176,164,165,175,179,177,163,164,191,202,200,191,180,166,157,117,28,3,220,209,196,189,179,170,155,153,176,153,132,137,151,138,129,123,118,103,76,114,155,155,150,151,160,123,117,121,115,23,57,70,84,33,230,209,194,191,190,205,225,245,247,241,220,222,212,176,100,81,75,67,58,46,23,6,38,75,67,45,23,5,19,25,16,13,19,21,197,223,234,237,248,15,29,61,83,101,134,140,148,146,151,165,169,176,192,191,182,194,223,229,213,197,186,183,188,197,199,203,182,163,156,148,133,110,93,80,82,96,100,97,112,128,143,131,119,99,80,53,32,15,251,245,217,195,177,151,133,125,106,75,52,64,93,113,121,127,145,172,180,152,151,185,213,205,194,214,226,236,209,181,154,58,44,34,28,23,38,44,39,23,16,19,18,6,249,229,213,197,178,181,186,208,226,245,0,14,55,119,165,181,185,180,178,141,175,199,193,204,245,250,2,4,249,239,237,0,6,254,242,239,240,245,246,249,246,251,243,71,80,67,62,68,73,80,86,90,89,97,118,111,98,87,94,97,126,159,201,246,9,252,230,211,213,227,216,208,209,221,243,238,218,203,198,192,194,197,204,208,210,210,206,195,187,218,61,95,89,91,120,138,145,140,146,122,98,95,98,91,80,76,76,89,112,139,167,197,239,25,37,59,63,61,48,38,15,253,237,233,236,236,248,24,70,107,113,103,109,117,126,132,135,142,156,165,191,219,237,221,211,125,90,79,45,42,57,72,83,83,83,101,113,113,113,98,91,72,46,32,20,55,81,107,138,150,149,167,180,187,205,208,195,163,130,143,156,145,123,105,109,127,89,50,33,255,167,134,146,166,198,213,227,246,1,10,4,0,252,252,251,248,236,228,235,7,230,203,183,164,151,140,113,60,25,4,251,236,212,181,165,156,152,147,134,139,147,157,153,139,127,90,30,228,208,187,169,156,154,155,152,164,155,139,135,131,129,118,66,62,83,96,124,158,184,225,55,79,80,56,221,241,247,242,245,0,13,31,64,96,111,133,153,152,140,131,126,138,147,156,158,161,144,135,142,155,156,139,115,132,138,135,130,130,144,153,154,165,187,112,124,127,134,170,213,234,239,245,3,27,23,243,193,146,106,99,82,70,68,60,48,56,56,49,22,66,89,95,98,120,24,241,223,142,131,124,114,104,88,84,93,100,106,131,95,65,76,86,66,39,21,5,0,55,77,80,82,101,106,108,96,106,121,132,130,128,135,163,153,116,107,67,223,190,169,171,177,197,211,222,234,252,3,251,236,233,232,236,246,12,43,74,102,109,122,139,161,186,169,147,128,99,69,49,43,37,20,224,136,122,116,125,111,88,52,69,111,132,161,188,212,210,229,252,13,32,33,35,40,45,59,77,75,75,85,106,116,106,99,99,110,123,131,145,158,158,141,125,107,86,68,71,81,96,101,94,80,70,62,66,66,34,203,160,166,184,182,184,194,210,234,255,31,57,78,102,136,127,127,133,133,137,144,137,126,130,143,142,117,121,113,72,44,0,221,202,169,133,110,82,54,23,13,20,30,27,19,54,56,63,237,240,240,7,22,32,39,52,62,67,88,107,93,72,41,15,15,41,64,123,185,202,180,114,34,26,19,21,56,49,14,234,209,214,216,225,233,228,223,214,208,199,185,176,168,171,165,180,200,229,0,19,36,46,41,35,24,25,36,53,61,69,50,35,9,243,237,233,239,242,243,225,201,187,159,144,130,126,132,123,104,94,89,114,125,140,152,147,126,136,97,92,105,126,140,134,136,158,178,185,187,199,212,221,213,199,182,176,176,181,175,171,180,204,220,222,220,215,217,207,192,189,190,199,211,225,235,225,213,211,210,228,21,8,6,0,2,23,36,51,76,107,140,151,164,164,169,197,251,10,20,9,252,251,30,60,80,91,119,152,181,227,251,11,34,62,48,62,95,196,219,251,42,108,163,178,193,203,218,230,241,1,9,6,1,233,217,206,207,213,251,251,229,208,189,184,182,182,173,171,177,190,159,151,162,178,192,178,159,120,77,109,136,122,106,86,81,85,107,121,117,115,104,81,62,53,62,60,80,85,78,66,49,45,44,43,37,28,35,75,69,71,65,247,18,32,23,10,254,237,226,218,219,215,214,203,193,236,27,69,92,73,51,18,12,42,39,32,39,52,56,49,37,27,28,37,31,13,16,30,38,56,66,59,21,204,203,199,198,215,246,27,66,103,116,117,114,112,110,113,115,126,139,144,157,179,196,197,194,202,212,207,225,245,253,222,198,218,233,234,236,226,222,219,220,210,197,191,202,210,191,167,143,187,227,217,219,218,225,246,2,10,20,9,241,210,188,159,139,154,170,253,47,52,59,56,84,102,111,119,210,200,195,196,190,176,154,134,131,124,115,115,116,128,128,132,145,161,175,168,158,147,153,164,184,200,234,17,34,43,50,36,7,227,195,192,209,242,24,65,120,145,160,164,148,128,98,72,35,240,203,190,181,174,156,152,162,196,186,186,197,195,184,184,204,111,100,55,231,213,193,173,3,35,51,37,15,230,241,10,174,149,151,100,151,174,193,206,209,218,225,204,182,182,160,57,56,38,37,24,50,76,92,100,104,138,168,11,57,83,104,122,117,95,56,35,24,57,81,97,106,106,82,58,41,29,17,6,250,242,233,211,191,200,197,187,193,211,224,238,251,251,9,21,38,31,248,1,18,48,105,150,160,147,122,98,79,62,52,47,66,76,82,98,126,158,173,172,153,56,248,224,217,209,214,240,84,88,117,101,18,29,29,13,11,24,19,254,236,237,246,246,245,214,180,178,191,196,199,207,217,237,4,26,39,35,34,55,65,239,204,196,194,226,9,17,9,51,100,113,99,71,44,80,122,104,96,71,67,67,47,5,209,166,152,153,153,154,151,149,141,128,127,143,176,191,178,176,172,182,210,187,192,198,198,197,202,208,216,221,221,231,234,226,209,192,195,221,249,14,15,16,21,22,20,18,19,20,9,11,25,67,74,77,63,227,215,201,183,159,132,121,118,125,123,85,81,86,105,118,103,105,118,146,165,175,166,155,146,131,131,97,67,59,72,71,64,65,73,87,107,124,141,160,167,167,187,235,240,233,245,251,223,221,230,249,10,9,10,4,8,13,16,249,231,216,210,203,197,180,164,155,247,240,45,34,23,249,214,182,171,161,144,117,99,96,90,83,107,125,128,130,125,124,225,243,76,84,84,179,201,206,206,223,247,243,216,181,138,114,97,80,41,2,11,22,24,28,26,3,241,232,218,203,172,143,125,105,115,122,141,152,153,155,155,136,111,105,114,104,96,118,135,145,148,138,129,118,113,118,117,108,104,94,63,41,38,79,125,155,182,200,204,216,214,221,240,13,70,124,143,152,156,159,169,173,217,222,226,125,104,114,115,130,145,143,157,210,2,53,73,65,63,60,53,29,246,213,215,215,226,230,228,221,200,188,188,198,203,208,189,170,181,158,120,95,100,101,81,68,65,47,39,37,54,70,67,41,244,3,28,47,53,51,50,70,110,146,173,156,56,44,9,240,245,232,202,185,240,211,215,233,246,3,5,18,132,129,193,220,213,210,183,139,128,137,190,33,73,81,91,107,155,205,242,3,14,8,241,217,193,158,161,174,189,138,84,74,61,32,246,227,202,220,235,3,6,224,134,100,77,71,74,86,97,101,108,110,102,15,224,212,204,196,206,223,234,231,226,222,226,224,230,221,216,137,109,136,147,168,186,194,205,216,223,228,246,245,216,189,171,149,129,132,133,141,152,152,153,135,110,81,70,70,69,81,104,130,141,157,196,194,183,165,148,57,41,64,69,49,55,59,51,33,242,237,249,11,230,183,160,144,214,204,175,171,164,242,186,153,132,136,155,174,199,206,211,222,231,235,240,254,251,229,233,217,214,224,234,244,4,31,37,49,209,186,183,218,234,244,24,75,118,110,92,45,4,224,198,160,127,119,125,154,178,215,245,238,3,107,129,121,103,89,85,58,54,57,90,117,111,116,128,141,131,127,125,122,120,113,97,87,95,98,94,110,134,111,76,78,53,147,164,162,168,174,174,175,186,189,188,173,154,155,150,140,133,148,116,114,128,116,94,76,76,81,87,85,81,74,247,229,190,175,170,186,202,226,12,31,45,63,90,122,135,151,174,171,246,194,155,145,138,140,148,149,143,125,35,251,4,249,211,201,200,170,135,99,69,59,58,68,74,77,91,111,124,111,110,108,133,153,198,235,0,21,26,30,39,37,40,45,27,255,253,250,255,2,14,34,85,92,76,67,75,88,93,100,110,131,153,182,209,226,237,254,19,20,246,219,203,205,206,192,178,180,173,185,199,222,248,2,242,214,198,188,181,181,189,195,209,225,221,210,209,211,216,226,237,221,209,195,180,159,164,174,206,233,254,15,16,8,8,12,32,44,55,58,56,51,46,49,55,64,73,75,78,85,68,51,20,253,239,222,212,195,172,171,174,165,167,173,188,190,190,194,203,220,239,3,18,13,4,242,229,209,194,200,211,220,228,232,246,33,45,53,42,20,245,149,120,85,56,34,15,3,14,24,23,9,10,8,11,24,45,81,170,194,200,206,239,6,2,3,3,12,17,14,14,13,4,26,55,82,100,108,200,219,224,221,208,197,179,175,133,64,80,111,122,129,117,21,13,15,240,233,224,221,219,220,214,206,197,198,210,244,56,140,150,194,237,239,249,36,39,32,59,155,155,146,146,149,160,161,188,219,243,17,22,38,31,35,49,225,237,255,3,248,221,203,188,178,163,142,119,113,192,172,157,146,132,119,114,106,94,76,84,138,148,161,148,122,117,201,248,250,224,189,167,146,155,150,141,133,140,158,164,173,160,144,119,103,84,70,59,50,46,22,31,120,109,88,72,80,87,99,56,37,23,14,26,12,248,228,229,222,136,140,138,141,133,126,111,92,100,111,117,135,155,175,185,182,183,185,171,152,153,162,182,201,204,212,195,146,122,119,116,113,131,143,153,164,99,110,87,74,45,238,209,201,225,219,196,163,153,147,167,179,195,206,209,215,221,225,248,254,252,238,254,30,57,82,85,97,101,4,248,236,242,0,247,235,241,1,35,50,53,56,62,82,39,0,7,249,214,210,210,210,200,185,176,165,149,110,13,235,212,217,34,8,1,10,28,2,228,186,143,118,79,38,17,26,44,88,140,185,205,205,216,224,233,242,233,220,221,218,218,218,217,209,203,199,211,221,231,235,228,222,216,205,189,197,196,206,221,238,246,251,224,169,132,133,128,104,255,250,232,222,239,249,244,222,218,198,148,79,85,124,199,234,238,234,240,255,21,25,206,227,239,14,42,58,66,67,56,48,27,31,64,134,136,144,135,126,120,122,122,103,77,46,2,230,201,196,208,221,231,219,215,221,223,215,204,207,221,238,171,184,194,234,2,12,11,9,32,42,56,72,87,84,71,53,67,79,92,104,117,96,49,55,59,31,234,194,209,255,236,216,199,201,220,239,251,6,1,238,221,218,222,221,220,212,203,202,200,201,206,200,191,177,159,227,189,156,141,136,138,133,122,119,42,38,45,145,206,232,242,250,220,214,213,216,227,245,247,247,223,211,209,211,222,222,214,206,202,207,220,220,1,48,44,115,88,57,29,239,205,183,169,160,166,178,186,189,100,106,110,112,115,106,98,76,170,190,194,207,217,241,28,53,64,83,108,134,161,171,170,175,184,182,187,225,230,239,1,28,34,56,74,169,200,220,227,242,5,19,11,8,243,185,170,157,156,154,137,117,94,79,83,83,91,91,100,98,194,222,217,220,214,227,245,241,242,235,228,219,129,108,105,109,115,136,168,184,195,193,211,233,246,1,11,16,8,22,33,54,66,68,70,76,92,108,105,101,98,91,88,94,100,89,39,14,32,39,36,36,55,82,86,85,78,80,100,118,129,141,136,136,123,109,93,91,94,96,104,113,126,123,121,145,144,147,128,100,32,192,154,152,200,242,223,200,199,200,198,186,171,169,171,153,233,230,250,40,99,144,141,155,186,205,213,209,199,184,167,117,20,9,1,13,23,51,79,96,116,133,148,193,37,58,73,86,93,101,97,99,118,102,45,11,253,240,226,237,247,16,27,41,71,91,70,90,124,104,68,42,17,9,17,245,246,237,250,7,50,51,53,66,79,88,54,20,49,71,82,87,81,36,244,218,67,65,89,99,161,223,251,10,2,221,190,177,178,178,165,145,137,135,145,162,186,216,245,233,225,221,226,233,235,252,24,61,98,125,147,170,174,182,158,142,117,128,139,48,35,21,22,21,250,228,220,215,215,228,244,241,205,233,16,56,237,245,249,12,35,40,53,77,106,134,162,186,202,172,164,186,238,78,133,163,183,184,181,183,176,157,130,106,200,190,205,225,219,207,187,181,175,165,152,151,136,105,82,60,69,226,202,208,217,205,195,194,185,176,171,159,144,127,72,228,206,171,137,108,110,146,179,168,147,131,127,122,120,127,126,109,79,36,11,190,177,164,160,153,148,151,162,180,205,37,83,104,119,128,40,62,80,93,115,122,124,202,210,214,213,199,188,192,200,196,195,198,204,210,244,255,248,242,245,228,181,194,198,187,177,153,143,128,102,87,80,73,70,65,58,57,42,15,254,222,205,115,93,90,85,58,34,16,255,233,242,25,36,28,8,0,2,6,25,48,97,146,183,207,222,231,224,226,222,245,31,26,13,19,97,82,72,117,159,153,152,157,161,153,146,143,157,178,178,172,169,182,206,215,209,168,173,184,213,245,254,14,245,218,195,180,178,181,180,185,191,206,211,211,218,224,202,172,149,151,157,160,149,135,122,95,82,72,72,61,69,90,99,103,94,78,94,113,131,154,170,161,144,128,115,113,105,92,60,34,22,31,34,8,255,211,195,197,191,181,164,136,117,138,131,108,81,51,35,15,38,113,114,117,116,124,135,146,193,210,226,63,108,117,175,192,204,222,190,164,251,245,250,10,7,229,199,177,151,104,61,45,48,59,34,29,10,55,62,55,50,33,14,3,253,13,25,25,19,4,243,228,211,209,207,210,204,193,189,190,187,198,212,221,224,232,250,246,247,250,21,55,97,137,154,166,168,169,172,166,167,183,182,100,90,79,240,255,248,253,9,18,12,2,253,233,217,208,236,16,22,20,7,250,233,224,227,232,246,243,235,239,251,239,229,248,246,232,204,163,115,103,82,51,59,68,71,75,84,88,93,94,88,89,76,57,137,141,109,87,81,76,74,76,93,120,140,166,173,7,30,25,42,30,21,251,226,195,186,222,2,243,222,197,197,214,240,9,10,9,229,221,200,184,188,188,183,181,184,190,201,203,203,207,219,216,210,208,201,204,215,245,93,87,81,68,70,66,97,132,154,165,155,145,152,167,170,145,108,75,78,89,104,112,96,84,78,59,57,54,71,81,93,113,134,191,209,230,255,12,18,20,41,67,72,65,75,69,55,49,47,35,21,254,238,230,232,235,243,237,220,229,235,2,34,68,108,124,124,129,89,75,94,107,114,125,135,138,134,128,140,147,144,166,179,187,182,187,226,220,203,190,161,132,106,104,116,63,5,16,14,33,54,78,84,91,91,81,63,75,116,126,132,126,129,135,108,96,100,104,84,59,37,11,250,235,254,29,59,85,96,126,162,166,164,145,133,158,188,193,170,150,241,240,240,218,131,108,102,115,159,178,158,130,99,87,86,83,69,83,173,153,67,70,77,57,244,208,178,162,151,148,156,162,182,201,208,215,212,216,250,255,250,16,27,18,10,6,1,253,249,1,6,7,0,245,206,163,69,45,3,232,215,205,180,168,150,147,156,152,155,194,14,14,250,249,245,245,237,236,233,229,214,195,177,162,155,149,146,154,157,139,114,120,134,142,155,167,168,164,145,134,137,134,81,43,6,231,217,221,202,183,174,160,149,160,165,148,135,210,177,150,131,127,129,128,129,131,131,136,142,222,11,19,50,68,86,93,92,183,192,209,219,236,0,24,69,104,86,89,133,173,202,225,251,6,242,234,237,245,254,24,37,54,87,115,105,100,120,146,178,170,161,145,135,126,119,108,127,165,165,170,181,175,148,129,128,123,149,179,168,164,162,170,88,140,143,146,139,120,119,122,119,141,156,166,152,152,165,192,230,16,62,97,120,114,119,211,231,236,249,248,249,241,241,254,19,46,72,53,222,208,170,154,136,130,120,113,98,78,61,61,71,75,76,31,19,90,67,53,57,68,29,226,222,146,115,110,94,100,121,149,174,201,218,193,177,154,135,126,132,133,124,117,101,83,70,69,92,107,106,99,120,149,170,181,19,51,72,88,102,108,89,64,31,6,250,234,231,150,102,95,88,61,45,132,130,126,115,132,111,89,90,103,117,123,126,134,138,104,182,186,195,104,91,95,89,86,72,20,228,202,171,149,146,153,159,175,191,221,227,228,240,6,18,27,12,3,239,221,226,231,228,226,231,234,241,8,44,68,83,99,105,95,102,121,159,196,175,163,148,142,125,110,82,57,35,33,39,43,44,26,245,203,170,142,128,113,118,132,150,142,128,131,40,19,26,247,153,155,196,187,194,191,191,184,201,209,229,250,255,38,55,49,5,22,39,44,36,37,50,78,100,83,72,70,68,74,71,105,139,105,100,91,88,86,113,131,138,134,124,157,167,182,179,176,157,145,141,136,74,62,48,51,53,75,100,110,116,108,100,86,71,64,55,49,59,63,73,66,232,185,144,154,195,205,194,188,185,175,179,188,206,218,227,238,3,14,36,64,73,78,88,125,174,171,98,119,132,139,139,127,134,150,164,153,122,117,124,143,169,222,63,96,111,124,144,73,74,79,84,48,21,245,229,218,189,158,133,112,100,87,74,53,33,15,9,4,9,19,35,50,65,225,185,108,75,62,73,95,144,189,213,213,213,243,26,24,30,30,39,52,59,53,41,81,109,130,124,119,111,76,44,17,245,31,60,55,52,78,118,166,201,218,222,127,101,82,66,44,33,31,31,22,248,156,100,87,56,17,218,195,181,194,203,204,213,225,240,248,239,233,209,204,215,207,192,180,167,159,156,167,166,156,145,149,166,179,207,230,250,15,20,30,41,54,62,59,53,47,28,12,17,19,29,46,62,78,86,92,81,69,60,48,18,250,237,231,227,236,244,250,254,254,2,228,213,204,197,183,138,127,122,113,115,113,131,93,76,67,48,74,117,113,116,138,160,172,171,191,107,99,132,252,45,67,105,122,136,156,159,135,113,117,151,174,191,192,191,183,181,194,224,235,238,223,207,186,172,173,166,167,174,189,194,169,156,154,159,187,215,227,244,240,236,223,216,218,224,229,220,202,179,174,175,176,186,196,206,212,214,215,255,5,238,220,202,195,200,202,202,204,255,254,2,2,249,238,229,19,37,28,33,49,63,80,105,139,163,174,178,176,167,170,158,157,160,189,33,41,30,32,37,27,4,235,226,45,75,103,92,89,103,105,106,103,132,151,217,242,248,2,3,26,61,90,103,62,104,163,195,202,204,223,244,12,36,90,116,136,159,173,181,188,193,205,213,237,0,20,19,18,13,19,20,20,249,223,213,206,198,193,194,175,138,51,32,6,193,175,179,178,180,183,191,183,164,176,188,208,236,77,122,138,157,184,196,205,212,204,194,183,130,88,56,30,17,181,150,132,121,111,91,65,75,71,51,132,135,128,128,134,123,113,97,75,52,35,35,57,64,37,14,93,97,88,89,87,76,53,35,40,41,41,42,44,44,36,24,0,170,115,84,73,72,95,148,191,233,40,47,57,41,19,14,4,1,249,211,213,212,209,208,209,206,195,179,173,149,166,247,3,48,83,116,130,113,87,66,229,166,75,52,42,25,6,246,83,68,62,68,60,53,50,28,13,6,23,36,46,40,5,252,250,246,6,20,29,40,59,77,69,52,27,214,197,208,207,198,187,194,212,222,250,24,49,67,90,127,133,138,128,121,111,109,109,108,106,107,106,108,111,127,144,145,137,137,149,172,196,210,209,207,190,198,204,211,242,208,178,215,251,3,1,148,174,199,222,243,0,248,249,250,237,223,221,231,219,221,232,224,203,190,190,208,222,231,213,194,166,133,96,91,16,2,229,220,220,226,234,255,12,19,23,41,43,26,21,12,242,205,196,198,178,144,120,103,200,221,252,22,38,44,44,38,26,39,66,91,109,111,100,76,40,1,226,221,211,212,207,193,201,198,178,153,132,117,127,134,123,113,108,107,104,100,114,129,135,122,117,126,135,152,165,191,177,164,146,130,147,156,161,177,199,205,205,211,232,244,13,40,35,25,14,9,3,223,203,182,180,203,234,9,27,51,45,34,118,143,168,183,179,170,157,148,121,101,83,77,53,254,232,210,179,183,182,181,168,138,131,123,76,237,214,197,184,178,184,211,217,202,183,178,170,207,11,32,56,76,58,29,12,244,222,207,198,168,126,81,44,67,74,85,62,57,59,96,182,199,232,21,72,104,134,136,133,138,128,138,153,176,202,210,214,217,223,232,240,0,235,214,193,185,200,215,249,24,27,36,28,22,15,10,5,255,7,52,84,94,98,97,93,180,183,206,10,43,49,61,84,104,122,143,137,137,120,98,32,1,246,235,9,35,107,115,121,121,128,107,81,63,56,39,17,20,19,16,7,254,250,248,245,222,186,150,184,220,209,198,186,177,172,172,206,215,224,236,255,12,3,250,252,13,80,123,207,227,248,8,23,25,135,153,166,176,164,156,138,116,97,77,66,60,57,47,27,18,57,82,85,69,75,87,97,109,135,155,169,180,178,170,153,28,230,213,215,216,210,192,194,127,112,115,131,154,162,202,233,13,52,48,23,2,254,253,18,42,41,40,14,242,240,249,252,2,8,5,3,170,148,157,166,185,197,198,195,194,200,207,218,232,246,250,14,19,40,11,253,74,67,26,244,229,227,210,180,166,183,236,26,34,29,8,236,216,130,123,125,139,128,120,97,74,35,247,242,168,163,73,33,74,82,67,51,26,249,220,195,188,170,140,137,169,159,162,166,156,145,153,158,147,134,114,110,118,123,131,216,188,168,167,169,163,155,170,19,56,148,179,193,196,196,194,192,190,195,198,202,205,211,217,221,218,204,220,240,245,224,201,196,184,173,172,170,193,227,251,14,34,25,18,14,16,27,27,24,23,18,15,14,18,24,22,11,235,197,111,87,71,70,84,89,76,25,247,230,211,206,216,203,183,136,124,121,118,116,120,133,149,171,11,50,61,59,71,101,121,60,88,91,96,104,135,162,172,186,195,177,164,159,148,139,125,113,105,96,81,77,72,66,60,68,66,62,64,72,78,89,105,118,125,132,145,162,173,173,154,141,154,156,77,19,236,2,18,34,238,192,200,207,222,16,23,19,4,1,4,10,20,40,49,11,245,237,224,209,209,212,211,212,210,193,180,162,125,109,80,42,18,8,255,242,219,185,167,174,176,177,166,154,130,101,89,83,92,87,77,58,118,144,134,124,135,143,119,103,64,37,64,88,64,38,6,223,216,211,215,213,254,17,15,5,248,224,196,155,121,106,89,92,94,94,83,245,227,18,34,43,46,52,47,49,25,8,247,233,235,239,235,230,220,187,131,128,86,77,94,98,103,124,149,159,164,160,158,154,163,174,156,121,86,121,131,101,70,70,94,122,143,148,137,133,127,116,99,104,116,132,128,95,107,111,86,51,25,255,2,255,32,54,111,138,142,189,210,212,203,208,221,235,5,36,63,104,136,139,143,152,154,152,150,148,169,182,165,136,56,18,2,255,1,38,73,80,95,105,175,147,133,126,183,247,252,6,20,81,53,52,75,64,56,67,57,39,252,202,182,189,196,202,215,216,224,234,230,177,168,174,178,190,199,208,221,233,217,184,144,85,238,203,179,121,89,69,78,110,119,122,100,71,65,74,29,17,20,22,32,42,60,86,91,89,118,197,205,222,229,1,30,57,63,53,54,32,12,34,55,88,118,146,182,205,202,29,56,47,22,253,232,217,42,13,238,196,151,84,20,223,177,183,195,220,234,220,26,4,254,11,30,145,160,157,167,171,181,188,191,208,217,218,222,134,122,130,123,125,178,208,233,46,99,94,87,85,83,113,137,151,173,198,9,21,33,26,24,7,229,221,225,8,106,139,155,165,180,197,233,240,54,63,73,78,72,69,56,19,214,206,195,222,254,11,6,242,217,191,167,132,95,82,71,67,63,68,82,95,107,98,100,109,112,111,106,95,69,29,246,224,216,210,204,191,179,183,239,233,219,211,214,219,216,210,223,225,220,179,183,106,109,121,170,174,168,169,179,195,192,188,195,203,216,230,229,216,249,220,203,189,181,157,130,113,114,122,129,127,132,148,162,159,159,155,140,124,81,70,61,45,50,53,59,64,69,91,118,144,148,141,177,168,165,162,138,87,36,40,66,98,98,117,41,38,80,87,122,195,215,226,236,243,5,41,67,153,172,173,150,126,88,48,171,155,157,156,130,72,38,32,110,137,125,106,111,147,158,185,217,216,210,202,204,225,239,237,238,230,234,246,11,25,15,187,202,207,213,223,230,236,240,253,18,40,54,62,81,150,174,11,250,241,225,250,35,73,107,131,123,107,91,77,61,34,238,221,193,168,135,105,93,87,65,153,162,178,184,195,219,229,183,157,151,137,139,118,97,83,79,83,119,221,19,30,38,46,134,147,123,111,127,144,159,157,142,129,127,128,127,124,120,75,71,81,102,101,88,66,77,110,132,162,192,37,56,67,52,22,253,222,186,173,166,166,169,172,180,179,176,171,166,179,199,208,201,199,149,159,156,136,123,113,128,181,193,21,74,126,151,143,124,88,61,41,54,64,83,57,64,78,80,77,78,68,38,252,237,145,153,149,155,158,175,204,205,199,212,61,52,38,32,50,60,69,85,126,143,140,153,177,200,205,202,220,236,241,192,178,160,145,137,134,135,143,158,159,148,150,148,126,86,43,11,254,8,30,28,42,127,180,212,231,155,130,101,72,64,68,54,49,51,65,80,77,66,70,89,108,126,137,146,135,95,90,91,73,98,121,126,143,178,226,247,4,32,27,32,28,38,56,45,36,46,59,58,60,17,232,234,240,239,245,246,240,236,250,5,22,23,9,62,53,69,82,91,97,102,104,100,103,113,118,130,145,164,178,196,216,233,232,25,51,52,48,16,247,224,200,185,199,171,164,170,172,164,156,165,176,194,208,242,23,15,17,21,14,219,202,190,190,194,161,156,163,182,192,241,221,203,199,201,203,210,210,213,228,249,9,238,219,214,212,201,187,177,158,138,150,177,188,213,238,0,25,36,25,251,250,20,42,53,60,69,65,61,60,49,38,41,46,54,60,68,77,83,77,84,92,102,106,99,82,74,4,247,237,226,216,216,219,208,195,174,150,152,151,162,167,156,143,177,187,197,193,177,179,180,187,196,210,219,227,243,15,23,35,38,33,235,238,236,213,180,171,189,193,199,230,222,222,211,121,71,61,64,63,63,244,207,180,152,135,113,95,72,50,41,25,10,248,241,252,21,46,26,16,22,9,1,251,9,13,15,43,76,115,147,177,178,169,168,172,195,200,25,30,8,3,4,6,255,250,11,7,22,17,253,231,250,22,51,105,119,122,144,200,218,217,218,220,221,218,206,186,178,171,166,161,138,106,82,144,155,146,151,152,177,206,243,12,30,61,166,217,231,229,218,216,216,219,225,229,220,217,213,207,183,187,196,228,37,67,94,111,131,136,120,33,27,13,1,228,195,243,29,43,24,253,2,235,203,175,162,149,140,137,134,141,154,138,163,198,227,244,10,31,41,39,47,65,60,52,55,59,91,140,185,222,251,17,16,14,18,26,64,222,213,202,188,180,176,158,138,124,105,93,69,26,231,199,180,170,162,150,138,127,117,108,110,111,110,94,71,45,32,239,164,172,184,183,163,131,107,103,143,182,215,236,245,236,227,181,150,153,150,151,161,174,168,167,155,140,119,123,145,195,186,176,166,135,107,108,106,95,75,68,59,49,40,41,47,41,22,243,232,192,161,150,131,81,62,57,69,84,95,110,131,147,231,17,23,60,47,27,251,225,216,220,242,6,127,143,142,138,129,122,116,105,88,62,76,84,92,95,106,119,131,152,172,184,186,195,199,185,164,149,130,131,136,153,175,196,214,211,214,229,233,63,107,111,112,106,102,96,91,112,145,141,147,155,151,114,140,132,90,83,89,83,80,156,192,198,202,186,209,199,121,130,126,114,140,156,163,173,192,204,209,208,203,209,214,215,227,21,15,11,9,241,205,221,240,17,42,54,141,133,146,177,223,249,234,217,224,237,4,37,7,232,218,222,224,75,59,52,60,71,65,89,125,138,217,228,253,11,26,205,203,218,228,195,176,178,182,164,146,136,128,114,74,37,251,94,33,8,254,248,245,28,68,53,27,255,230,206,174,153,33,5,249,2,18,37,55,67,93,118,159,181,196,203,190,196,222,225,216,226,252,223,211,207,214,209,211,213,220,184,181,198,215,217,225,238,242,246,234,225,222,220,218,212,189,160,176,188,193,185,187,205,221,234,246,252,6,10,26,8,250,215,108,84,160,186,211,225,227,218,205,191,220,240,0,196,216,218,202,182,157,115,95,88,118,131,144,162,187,206,229,220,225,228,240,0,32,63,114,165,200,203,205,221,253,32,57,60,62,69,83,83,80,63,55,60,61,236,0,4,230,164,137,144,153,164,151,131,134,124,115,120,122,132,144,126,99,72,52,27,254,226,222,207,191,178,183,204,249,254,237,220,215,218,225,235,240,233,213,198,185,201,222,241,23,85,146,172,180,169,140,47,16,15,12,16,19,23,37,62,68,69,86,107,98,94,78,49,12,37,59,73,86,82,83,95,122,144,185,8,28,54,71,78,150,169,183,181,104,54,12,241,224,182,174,182,204,220,236,252,5,17,22,207,211,214,224,229,228,228,228,200,203,195,195,204,213,214,210,204,192,180,160,120,65,20,230,194,157,123,139,138,125,123,127,127,114,101,43,82,118,152,186,221,242,253,5,19,38,142,192,209,218,211,204,210,219,19,1,230,207,203,206,205,203,219,217,204,196,194,202,211,226,238,50,16,208,150,119,70,64,59,60,14,232,219,201,180,163,155,153,162,182,199,216,244,20,61,84,89,100,112,106,96,106,115,104,245,208,200,200,205,211,211,210,215,223,7,44,56,60,57,65,85,129,157,182,185,181,163,160,172,192,7,246,234,231,230,248,19,43,48,45,94,125,154,177,201,217,226,226,222,225,237,21,30,40,35,24,21,18,7,183,166,154,151,161,166,171,146,121,110,113,86,57,44,59,69,74,77,82,81,87,87,76,57,21,239,235,220,223,237,252,17,2,243,238,224,223,209,172,129,109,99,103,101,99,100,106,113,140,167,188,211,210,209,206,248,250,252,255,250,0,3,244,242,251,8,16,35,49,64,78,68,63,59,67,68,77,94,112,107,102,94,84,78,86,95,106,113,113,127,207,238,236,9,31,35,33,29,27,26,25,73,117,98,85,87,103,80,53,64,137,149,158,153,147,146,142,136,132,122,77,76,83,96,95,89,91,97,103,111,123,143,143,123,119,152,146,156,165,167,154,137,139,42,8,246,242,152,138,138,127,114,32,19,246,216,187,176,184,189,204,202,189,179,172,151,106,56,7,236,234,240,14,50,83,119,134,138,121,116,131,188,205,216,217,224,220,216,206,197,185,164,144,23,9,18,30,22,11,27,53,73,81,80,82,99,115,137,150,165,182,203,243,37,58,58,71,92,103,94,84,125,133,70,94,127,145,146,251,232,223,210,202,206,219,225,245,11,56,102,115,160,251,11,179,173,151,118,78,62,52,30,3,245,245,10,229,226,212,217,230,226,194,96,69,48,39,54,59,60,94,124,217,221,245,80,73,86,96,96,76,41,19,238,47,48,67,72,251,244,247,8,30,58,196,170,170,176,185,192,180,173,161,136,113,112,113,125,138,150,148,160,7,22,216,210,215,217,219,213,217,58,81,106,126,147,155,172,185,195,200,202,204,181,155,141,145,166,177,218,10,26,16,6,15,33,46,203,215,78,148,164,209,235,39,71,108,156,206,240,50,41,43,52,56,58,84,106,104,113,131,160,188,206,229,165,212,247,29,65,185,196,194,192,185,181,192,192,184,174,155,120,27,13,102,163,199,246,231,223,198,176,166,154,137,138,159,87,89,87,93,189,189,193,190,212,251,219,196,190,195,176,166,177,175,162,149,145,10,240,158,91,87,70,188,185,159,137,119,117,108,122,136,138,136,127,103,13,246,219,221,206,180,169,166,165,163,157,150,143,142,153,167,174,182,200,227,251,24,51,67,83,101,126,137,51,64,81,88,102,137,158,144,138,126,136,165,196,213,206,217,225,221,205,183,196,208,197,199,200,193,189,206,35,21,11,245,248,8,55,135,141,167,179,189,186,159,139,134,145,119,95,82,74,73,74,75,69,67,65,59,64,252,6,229,204,181,155,131,105,82,93,110,110,26,20,250,234,219,108,69,50,248,238,242,246,253,83,108,130,158,170,179,198,227,215,208,199,73,45,23,255,236,188,141,104,76,57,37,29,206,187,160,153,161,165,140,165,183,168,158,160,172,200,224,226,235,246,47,132,139,152,4,9,11,32,56,34,249,220,207,173,192,179,173,179,187,187,185,218,224,226,239,253,175,178,152,120,136,140,156,161,154,142,133,57,49,70,91,55,25,41,58,83,99,101,106,112,71,93,118,132,150,173,183,199,183,185,173,152,129,109,103,119,121,111,84,53,24,13,12,12,25,33,35,45,125,175,177,207,214,203,174,163,120,104,88,105,193,160,134,122,107,37,18,0,234,243,18,108,113,106,115,121,131,136,127,111,27,237,229,230,10,57,78,84,65,31,205,199,197,190,223,133,100,69,37,8,255,1,39,48,249,215,198,186,177,169,152,120,79,44,33,26,16,61,83,112,210,240,228,149,99,76,64,68,61,41,28,19,0,245,233,231,41,34,31,22,28,27,209,194,196,195,194,212,218,221,210,196,191,192,195,199,188,180,185,209,216,221,233,228,226,222,228,20,14,228,227,234,231,88,127,159,167,172,175,166,163,161,162,146,148,154,173,190,74,51,33,7,249,241,243,242,245,13,29,38,33,14,254,255,8,12,245,222,176,203,241,24,55,49,41,7,255,243,216,220,241,9,0,234,216,220,229,250,12,10,191,205,243,9,1,243,228,208,189,186,174,148,127,113,101,44,59,72,73,68,68,71,80,94,88,90,97,105,104,83,85,97,105,81,43,15,228,184,180,149,89,78,64,69,69,71,72,64,74,95,110,134,154,168,169,145,100,59,76,61,53,39,14,253,239,205,172,172,154,136,55,39,253,1,243,175,169,214,241,1,18,31,29,43,101,202,230,243,225,200,174,181,186,185,178,176,185,197,209,208,205,199,198,222,219,216,207,204,203,203,201,217,216,83,67,50,53,50,39,45,70,102,145,193,197,175,160,141,134,149,185,193,72,38,53,62,74,87,110,71,84,116,44,36,40,33,52,72,85,80,77,85,118,137,204,208,221,238,249,7,12,8,6,4,96,90,47,52,59,49,41,49,12,240,249,226,217,215,34,0,229,191,197,235,227,217,234,3,36,70,104,138,160,184,3,39,52,111,109,116,126,138,148,141,137,132,109,109,135,130,125,170,193,195,189,176,163,229,213,194,181,166,109,76,103,131,228,224,232,250,252,248,5,24,41,70,85,100,105,105,91,73,40,145,148,132,153,73,87,110,127,111,66,58,50,226,209,198,221,242,10,34,104,94,79,100,195,188,186,166,146,125,109,211,210,220,190,158,133,96,72,48,20,12,8,242,99,85,243,207,185,169,143,56,52,126,81,69,33,245,204,254,84,121,122,158,220,214,212,194,173,150,147,148,155,166,160,156,169,197,214,222,223,211,33,14,218,0,41,42,46,32,26,9,250,238,241,6,18,18,24,15,3,252,247,160,127,92,80,228,208,211,167,182,151,121,143,155,177,215,236,248,0,249,230,242,236,226,239,243,234,215,201,187,180,169,152,157,153,142,141,144,139,146,151,167,170,183,126,133,143,151,171,179,0,38,36,20,239,247,229,184,173,175,168,159,166,182,189,154,126,94,50,136,118,117,122,122,121,122,127,126,131,132,133,144,169,202,236,245,49,67,83,106,75,69,62,19,233,224,238,246,129,156,129,42,70,85,113,145,161,176,203,214,224,31,5,231,219,221,234,243,241,22,23,27,24,82,112,133,149,120,104,126,134,179,224,193,180,172,171,156,136,122,84,107,177,171,195,192,212,157,122,106,120,134,128,104,72,225,195,182,172,165,206,247,149,153,160,156,152,126,94,98,114,65,145,150,161,176,195,163,138,138,141,156,246,253,21,49,114,110,103,95,77,33,243,239,241,248,1,7,221,237,247,10,23,28,153,195,224,198,162,135,143,136,139,132,121,121,119,109,88,67,52,71,81,83,85,99,109,192,224,54,63,201,202,202,194,166,131,128,117,77,97,162,204,193,172,175,172,166,153,128,115,117,125,137,146,150,126,114,102,93,72,55,40,113,127,119,141,161,172,190,195,222,1,33,53,63,74,87,104,110,114,138,119,94,77,51,24,3,234,233,210,130,105,105,108,114,73,34,25,3,226,185,158,155,140,111,106,185,167,135,67,81,94,112,122,119,122,121,116,121,170,228,232,207,214,241,16,51,87,96,88,101,89,47,251,211,206,195,151,142,135,132,152,160,173,192,230,229,217,222,241,6,24,31,44,51,31,12,8,13,251,208,224,238,234,226,221,228,231,229,83,79,62,72,85,126,159,190,175,171,159,151,155,149,136,135,131,67,56,39,12,17,17,31,48,62,217,188,173,134,139,157,176,168,165,149,172,213,250,253,8,15,11,250,203,249,228,196,185,191,195,181,242,215,144,221,226,55,55,42,27,17,126,130,101,74,66,65,62,59,66,72,70,58,32,196,104,107,95,82,65,148,139,146,151,158,148,103,71,230,188,174,205,193,190,184,170,145,157,165,59,43,30,33,33,98,118,122,125,113,106,102,92,77,65,56,54,51,37,58,56,64,80,104,146,106,126,213,193,190,176,162,165,170,169,189,213,204,214,13,10,3,12,86,86,79,68,99,152,190,220,226,11,57,140,138,146,146,152,146,97,111,129,151,173,190,175,117,113,176,203,8,40,69,120,132,147,178,240,94,106,233,243,240,236,213,192,171,150,129,86,69,86,87,73,59,40,30,20,255,0,253,254,37,51,116,148,176,184,168,151,145,52,92,120,157,185,198,201,192,192,187,204,230,12,24,30,26,57,70,63,250,152,116,130,177,159,113,65,52,33,19,247,85,84,35,47,65,103,173,199,216,218,226,242,17,54,93,104,85,60,11,201,178,106,68,224,194,189,172,164,150,172,183,193,213,230,245,5,7,251,243,237,191,205,238,229,200,185,187,139,143,159,176,176,161,146,140,129,129,122,137,189,217,29,31,34,39,52,68,72,72,67,75,63,9,13,33,50,56,62,80,98,104,115,112,92,83,81,76,69,39,8,247,233,228,235,226,243,250,248,248,9,14,232,238,215,215,202,200,206,210,181,76,101,109,102,96,121,5,16,253,180,131,92,98,100,119,142,167,190,190,169,193,212,67,100,0,16,4,99,119,121,127,186,150,113,79,78,175,194,205,192,183,170,165,229,233,233,6,9,248,234,213,189,169,158,159,165,180,199,208,225,212,125,196,226,231,231,6,0,254,238,220,209,212,218,218,238,235,227,209,182,160,163,160,162,207,212,208,180,125,43,39,233,219,208,183,173,204,198,247,248,251,2,7,1,247,236,221,216,199,186,102,4,5,30,48,58,60,65,131,161,165,201,188,158,128,203,163,143,129,135,103,66,60,45,40,41,21,200,94,120,152,142,60,76,119,110,88,68,66,94,112,99,67,248,255,1,10,255,65,90,105,118,135,92,181,204,197,193,195,202,13,215,123,115,118,129,142,151,167,178,177,181,198,213,212,251,18,44,37,27,21,20,14,12,27,35,40,47,30,202,206,203,197,186,168,148,131,33,173,173,177,178,188,205,203,158,182,186,192,200,214,238,31,73,112,122,139,152,169,214,229,198,195,220,47,201,174,145,131,135,132,118,111,67,28,47,63,86,145,245,146,149,136,128,131,141,152,12,41,1,252,226,94,81,83,91,108,19,18,30,41,42,38,36,38,45,45,47,53,44,25,224,198,155,115,65,54,72,107,132,158,172,187,202,136,93,45,6,248,14,1,1,254,212,219,214,200,197,212,207,192,176,153,137,114,107,61,12,236,250,82,76,151,165,146,141,121,101,188,76,48,28,239,162,131,107,68,49,72,59,62,70,52,20,253,28,34,54,214,246,2,244,235,227,225,14,23,22,28,28,39,58,85,107,98,61,55,38,178,217,194,188,175,179,192,219,249,16,40,50,76,103,134,130,114,101,106,107,106,106,112,104,102,95,103,110,103,92,106,137,151,140,123,118,172,215,216,222,252,174,169,206,25,32,124,149,166,5,27,15,12,9,1,171,185,197,205,19,6,255,255,245,226,209,206,218,234,245,248,249,206,190,189,210,244,227,222,208,162,147,121,90,54,64,68,60,41,19,248,210,238,245,25,82,63,62,45,26,19,26,32,36,18,197,221,205,173,133,99,75,41,205,226,254,22,30,31,38,13,254,11,26,48,75,96,107,119,127,129,100,55,36,234,229,228,215,196,179,181,223,221,219,155,90,159,114,107,102,104,108,103,98,86,86,96,112,132,104,98,109,120,127,138,159,212,200,184,171,150,127,101,105,164,177,192,216,218,218,212,205,199,198,229,74,60,60,225,218,207,182,161,163,159,181,202,226,2,26,38,54,72,245,203,176,202,216,195,184,181,183,112,94,83,74,79,220,232,237,152,165,176,183,180,142,114,114,114,117,100,66,26,233,220,183,162,193,232,241,246,228,173,37,31,41,36,65,69,45,30,13,8,10,20,255,200,212,195,160,130,107,98,74,49,86,54,83,100,104,131,164,240,42,76,111,112,143,127,151,113,132,150,170,193,217,222,217,216,222,216,220,229,249,6,22,27,11,245,178,173,189,19,29,17,7,22,23,23,242,242,249,3,255,205,113,85,103,90,206,201,186,173,85,55,42,46,66,89,105,121,141,162,165,154,145,128,127,127,215,198,194,225,19,131,128,128,137,149,144,129,64,74,46,13,248,255,12,17,22,19,13,249,252,249,244,195,144,105,87,255,237,229,223,215,199,182,177,178,177,208,200,231,223,218,29,22,5,249,224,242,253,8,19,31,36,141,156,173,182,192,193,178,168,167,132,114,109,101,80,62,59,249,235,3,100,86,96,107,87,101,121,160,170,175,182,191,190,183,172,163,251,197,185,179,207,213,220,128,133,146,174,218,214,223,196,108,252,243,241,234,233,0,32,58,62,58,64,62,38,5,250,0,252,251,13,4,16,47,91,129,125,129,142,151,168,202,192,187,188,216,219,227,247,7,249,242,0,38,79,61,222,225,193,153,108,221,241,243,59,37,39,53,241,241,221,212,125,126,133,142,117,103,94,239,203,206,194,186,192,200,230,238,73,214,193,167,193,94,38,210,191,163,150,162,179,152,154,145,139,123,99,90,91,104,118,127,122,230,202,150,152,130,118,78,62,94,161,181,188,187,183,188,191,192,198,203,204,206,211,216,225,207,176,179,220,245,6,11,242,210,174,166,143,253,13,27,16,9,39,31,26,27,29,23,16,12,32,33,33,50,61,68,119,127,33,17,1,247,237,238,8,98,117,124,120,115,113,107,112,221,9,44,53,48,81,114,43,76,94,88,77,80,80,94,123,156,179,218,156,145,135,123,97,76,70,71,74,74,75,72,66,50,52,59,56,60,56,60,70,78,73,122,132,151,124,118,128,147,153,137,117,55,235,11,38,51,63,62,67,150,166,173,182,187,204,215,26,28,11,8,10,38,69,76,89,243,244,245,236,216,198,199,202,207,219,216,207,175,8,11,7,2,255,144,188,185,176,172,154,139,128,78,65,70,66,76,90,94,87,73,141,178,171,140,100,121,148,152,166,96,92,5,25,111,125,78,45,33,19,22,16,248,233,219,210,215,226,58,40,30,28,40,34,253,2,15,4,242,212,162,133,117,102,95,82,70,101,120,157,194,29,66,61,66,77,61,41,25,10,249,237,223,217,228,238,248,215,133,150,241,35,80,90,107,122,141,182,174,173,172,168,165,165,153,141,155,167,180,201,190,175,49,135,153,155,141,48,29,42,47,69,83,101,126,149,145,123,134,135,132,95,64,84,96,112,135,151,147,138,131,109,86,72,22,6,245,223,223,224,216,234,16,7,217,120,204,190,202,217,231,160,137,155,153,147,138,124,135,162,191,213,187,140,74,56,51,35,23,11,244,64,96,221,200,115,118,106,90,85,19,18,11,10,16,7,253,71,34,17,223,122,88,60,44,51,166,157,224,224,206,211,218,230,249,242,156,157,158,164,171,176,184,195,199,206,240,210,175,139,110,192,49,38,53,140,141,136,142,141,127,98,72,107,109,3,13,26,34,32,174,200,199,198,199,206,229,1,24,84,38,241,233,251,57,75,98,77,81,76,207,181,137,90,60,27,3,225,62,35,242,158,184,198,240,18,150,165,182,177,176,179,163,153,169,159,173,202,212,219,224,229,39,99,105,115,131,137,215,212,137,129,90,101,94,55,151,177,191,32,28,67,45,32,28,34,42,52,221,212,54,158,151,158,175,186,184,187,231,36,61,67,75,186,184,213,242,16,27,26,13,252,204,206,197,75,72,66,64,54,63,73,83,93,114,79,92,97,93,81,213,212,211,208,205,207,176,169,166,170,246,247,213,198,194,202,203,192,203,243,233,222,217,221,28,83,97,81,68,172,209,207,209,182,170,184,200,219,204,183,201,214,233,247,245,239,219,192,174,205,204,197,189,194,200,178,156,131,103,93,108,104,108,123,136,132,117,120,124,132,168,175,167,163,163,153,40,60,72,75,60,41,48,59,62,55,53,52,74,174,156,148,139,37,248,5,19,55,126,116,122,161,215,12,28,101,106,117,144,153,161,184,208,239,237,240,249,24,64,148,180,192,182,166,125,117,95,67,73,131,154,140,66,47,38,44,50,76,102,79,87,89,142,166,196,231,251,238,227,214,196,232,244,13,248,228,250,232,241,254,14,38,57,61,150,174,215,211,194,199,213,234,235,235,234,241,134,119,126,100,220,186,212,226,15,31,74,126,116,99,85,72,47,27,233,212,188,171,148,125,111,86,70,53,12,218,174,176,187,194,188,185,181,183,203,227,241,178,176,172,168,146,127,147,172,141,70,73,108,161,13,8,24,37,33,5,220,185,177,149,107,133,147,161,178,175,170,156,130,131,132,131,123,118,62,68,56,57,101,122,114,114,99,73,130,145,30,6,251,211,202,185,171,167,168,171,169,172,173,162,208,231,240,181,201,208,154,173,100,208,225,255,29,165,163,151,141,106,68,46,24,44,21,60,82,87,81,71,67,75,86,20,30,128,144,130,144,157,218,174,174,158,44,59,62,53,149,158,160,148,138,126,205,230,217,207,212,222,243,246,222,239,187,148,132,170,179,163,166,143,247,236,0,38,57,48,158,206,253,77,87,41,54,77,101,92,108,120,126,155,162,156,151,159,52,82,108,108,108,121,133,144,248,235,32,43,36,49,74,81,63,38,16,45,224,226,227,230,217,219,232,243,248,8,20,35,29,9,21,40,58,67,78,91,97,91,95,161,166,175,186,204,218,229,248,244,48,74,88,84,77,53,33,12,248,227,208,187,175,159,161,153,151,155,161,173,184,164,73,40,29,26,18,3,32,78,109,186,206,12,246,207,188,194,218,21,46,37,2,226,207,206,215,222,216,206,194,180,177,109,131,172,203,174,182,208,233,246,38,60,60,34,234,217,253,52,63,64,63,76,82,77,68,61,46,37,25,28,35,43,40,48,59,60,62,66,86,73,73,84,115,82,250,207,204,221,234,204,181,154,132,133,138,131,191,198,211,213,171,175,206,208,214,221,220,220,40,35,24,31,51,49,58,255,235,187,141,142,137,185,191,191,189,188,227,213,210,212,137,97,74,65,68,76,64,59,78,206,193,172,158,144,130,125,115,101,83,62,42,35,45,37,21,227,79,247,0,28,1,100,129,159,179,185,173,159,144,132,207,191,168,120,70,51,20,241,37,42,28,255,229,202,232,34,54,131,129,122,111,29,243,227,222,165,162,159,146,91,75,177,179,165,157,157,158,160,178,201,208,245,165,194,216,229,237,242,237,225,217,218,219,217,213,170,185,70,66,80,129,237,39,133,150,167,12,36,244,237,221,195,157,151,139,123,169,200,42,45,55,56,54,54,60,66,61,60,157,169,16,72,237,219,216,175,176,189,154,130,123,119,108,90,81,51,13,237,220,216,201,130,124,119,99,107,115,117,48,247,215,185,188,196,206,220,197,171,150,127,98,72,247,232,226,239,1,87,132,137,146,166,175,98,127,222,203,199,187,153,82,101,111,57,65,63,42,33,34,42,55,220,200,250,30,44,104,106,118,132,147,149,173,191,214,246,78,99,89,68,53,21,195,209,230,246,10,147,157,150,144,143,135,128,120,114,117,66,36,28,48,68,88,104,113,115,117,120,140,160,171,192,209,208,214,207,200,167,161,151,136,109,102,110,115,131,146,165,178,191,215,242,219,203,243,109,115,108,107,109,81,68,78,92,134,175,178,149,86,100,98,184,190,200,206,200,219,27,87,118,118,113,100,163,155,151,157,161,211,206,195,192,202,209,218,136,55,53,39,172,190,49,41,162,153,153,211,8,197,210,210,218,223,196,250,203,207,200,209,215,209,101,46,54,76,65,94,127,156,156,216,163,168,170,172,183,198,193,129,133,125,97,74,123,206,245,243,245,254,243,208,123,98,77,68,52,27,4,240,228,220,211,191,235,237,228,235,239,0,15,28,65,122,152,216,202,175,160,146,231,242,235,212,198,49,216,205,209,199,193,196,201,204,206,218,163,165,178,191,212,231,223,209,242,228,123,124,181,207,197,170,166,208,225,234,243,17,29,5,255,26,61,70,48,187,191,221,202,224,241,133,164,1,237,223,206,189,135,146,149,150,150,155,172,189,196,62,85,101,196,186,174,56,66,97,87,66,54,137,233,250,208,149,169,104,101,122,152,37,25,5,214,202,205,149,155,157,218,245,255,255,183,232,235,249,23,68,117,149,159,139,112,22,2,3,14,75,93,80,69,110,132,126,110,104,108,213,91,103,85,67,187,204,213,242,5,40,54,69,83,84,152,160,174,178,177,161,130,97,228,233,250,3,12,24,34,31,29,191,201,204,202,215,227,233,235,232,234,235,237,207,186,219,217,216,203,190,170,117,66,54,45,27,4,231,196,160,114,27,55,87,109,131,252,72,134,162,173,195,211,222,239,38,134,178,195,211,217,45,30,9,238,206,184,193,198,204,197,186,202,212,197,195,186,196,202,205,206,219,7,39,19,248,209,161,130,170,207,36,57,71,93,68,224,218,145,142,165,16,254,39,56,84,84,102,123,93,72,232,214,202,212,218,213,203,198,204,219,53,61,63,66,58,47,45,47,202,207,207,197,173,151,145,144,154,165,186,10,252,239,228,241,9,44,58,60,61,54,153,164,176,191,221,227,230,230,236,227,216,218,217,203,152,62,19,25,29,94,116,152,20,19,17,30,66,69,76,77,73,75,254,217,225,241,0,16,40,5,234,228,217,209,194,154,111,97,92,97,174,127,25,235,215,208,254,252,249,253,12,6,240,222,241,4,24,40,58,68,70,57,59,70,97,117,105,68,66,75,88,217,239,14,55,42,33,29,242,171,158,123,70,73,90,153,161,152,146,147,144,138,140,158,56,77,100,84,89,162,134,133,180,181,181,222,4,6,247,246,73,111,130,152,130,126,235,204,171,173,170,173,193,178,146,58,71,46,30,13,243,225,223,222,152,147,129,104,98,86,79,223,228,220,223,152,142,248,35,52,41,247,5,16,74,66,64,145,152,159,190,217,103,121,133,181,227,30,66,84,120,154,157,160,162,240,220,205,197,189,195,207,210,223,241,10,146,115,108,157,211,225,4,14,11,230,187,216,30,61,57,13,244,236,219,220,224,251,18,221,222,213,189,192,192,207,215,226,226,203,152,105,79,57,43,32,63,27,227,121,149,112,99,81,97,113,112,112,102,15,69,76,252,252,11,39,170,140,146,174,176,166,175,178,175,155,130,99,113,126,151,156,153,130,41,32,43,203,208,193,201,74,86,95,163,166,192,196,199,172,150,139,125,140,171,164,192,232,18,6,2,250,243,188,109,116,114,116,100,206,103,104,116,147,182,32,40,52,58,59,59,40,117,119,108,94,95,105,111,134,159,185,205,209,219,233,248,9,30,201,201,184,218,197,205,182,168,161,26,26,45,170,15,248,240,241,222,180,162,160,102,51,83,177,198,169,254,179,188,190,156,185,168,150,247,97,86,197,206,209,180,157,141,122,147,150,143,141,245,168,162,156,146,174,192,26,41,57,112,145,149,61,91,190,174,118,208,238,231,231,192,201,171,74,52,39,29,5,34,110,113,132,189,126,115,113,122,78,71,67,77,82,64,225,0,52,147,138,127,84,46,70,83,101,130,139,51,38,253,230,227,217,213,236,225,223,229,232,245,253,53,82,107,116,170,162,176,206,44,25,8,246,204,154,124,101,94,22,9,145,118,164,171,155,216,233,241,247,14,50,87,105,123,125,136,25,29,11,11,24,50,70,99,26,237,204,203,183,216,218,170,172,184,194,201,162,224,229,227,222,227,236,12,148,196,116,126,128,166,172,226,43,93,143,238,2,30,44,104,109,58,89,111,166,187,237,204,180,204,209,189,133,114,91,86,81,106,136,76,247,251,254,6,10,10,253,36,59,175,184,161,129,32,239,225,213,35,80,83,71,66,67,28,246,228,138,128,131,129,123,207,220,253,128,101,72,4,248,233,217,79,83,74,60,56,42,37,82,85,66,49,47,251,255,3,94,198,186,177,42,228,140,107,113,193,187,169,165,141,109,70,32,13,42,38,47,33,19,9,24,67,188,173,177,202,221,211,192,178,179,164,170,250,223,220,203,75,50,56,108,139,183,174,161,157,158,156,127,125,180,240,235,226,41,60,57,39,234,236,14,24,33,28,3,233,246,32,86,72,61,202,206,224,237,5,41,31,91,135,169,178,219,1,20,243,231,209,186,115,65,81,83,61,63,63,93,81,85,134,90,50,129,114,208,191,183,51,61,62,64,69,78,76,63,52,55,83,88,81,113,179,182,168,142,101,105,90,78,77,63,30,12,5,180,145,144,162,166,86,129,220,242,255,10,29,145,123,118,0,254,254,10,25,52,47,34,41,74,115,160,15,0,229,182,175,192,197,205,222,219,185,213,204,196,184,185,205,81,65,41,28,44,46,43,68,84,117,193,188,136,133,119,114,195,205,212,255,17,61,96,28,30,57,44,90,138,142,149,199,211,231,2,14,14,18,12,115,44,54,62,76,68,184,250,6,15,90,28,251,244,203,13,251,222,196,208,225,242,16,35,56,83,106,230,6,17,95,105,153,137,131,134,80,74,81,39,74,206,194,188,146,137,247,225,179,171,18,208,209,247,237,7,27,62,82,117,103,92,163,243,38,109,124,148,163,204,28,53,188,206,223,209,144,130,111,248,214,66,73,77,6,251,3,207,164,165,248,216,156,203,206,142,145,163,177,183,168,231,232,237,40,184,168,75,61,65,248,220,26,38,40,16,253,253,254,250,250,165,149,136,121,110,110,181,202,119,147,144,161,182,231,231,252,248,1,250,228,210,186,182,164,136,166,126,161,178,181,167,133,140,207,223,232,251,18,47,41,27,72,21,3,0,5,90,147,171,171,167,167,175,187,209,121,120,103,85,67,115,100,98,123,122,121,111,122,131,135,138,165,51,65,65,241,32,59,80,14,225,224,196,29,54,64,96,156,158,156,179,228,218,215,192,194,205,223,237,246,254,14,11,248,173,133,123,136,164,185,178,117,108,27,19,245,214,190,177,174,177,183,173,150,59,67,77,147,132,100,66,193,190,179,174,165,156,165,173,170,167,168,108,64,51,216,161,178,202,134,254,241,249,48,104,110,100,85,70,70,7,228,4,25,28,28,165,186,164,118,99,158,139,132,119,98,80,95,86,78,100,113,146,54,190,200,203,188,162,138,131,158,148,171,169,168,201,116,120,128,150,157,164,109,102,44,24,179,162,176,196,99,118,123,118,96,81,164,100,86,69,44,102,105,116,118,25,245,234,188,186,83,112,125,126,114,97,96,222,241,251,223,207,213,248,250,167,111,106,253,224,182,133,135,127,126,151,156,189,239,242,220,204,206,221,241,9,32,41,53,62,66,59,2,235,226,227,229,229,46,15,133,149,171,188,167,167,189,173,157,152,25,252,9,253,167,163,166,179,182,200,20,16,25,233,190,224,201,180,202,213,192,181,36,193,209,210,59,51,41,155,64,57,52,53,62,84,69,75,71,42,21,246,113,107,95,159,148,178,126,66,229,207,184,237,213,192,152,171,185,193,50,45,20,23,116,104,105,84,53,26,51,65,123,139,12,117,172,159,145,152,156,162,158,49,57,34,148,3,160,154,38,78,104,208,196,124,133,142,152,119,250,193,176,48,7,94,76,61,16,8,254,248,61,138,145,159,175,168,74,98,181,173,173,169,176,179,0,16,24,31,37,21,197,146,222,219,199,219,46,58,25,9,245,29,31,46,17,228,227,221,215,33,102,177,181,188,186,186,148,140,121,119,153,170,181,201,210,235,251,7,29,25,20,248,247,240,11,6,213,173,174,184,121,149,154,168,193,196,151,119,119,111,206,29,35,38,78,85,72,8,10,65,85,98,118,130,104,88,81,87,86,244,233,230,223,218,251,27,12,253,26,42,62,137,174,150,228,207,189,170,211,222,224,37,69,86,79,76,70,104,112,137,111,146,201,214,218,253,237,203,129,127,185,53,34,213,196,188,174,140,232,202,154,57,30,28,146,213,216,228,250,5,14,254,236,206,198,227,215,249,210,183,141,153,211,143,97,73,64,69,247,11,1,249,203,198,188,174,34,62,210,175,146,127,116,97,83,55,62,69,39,36,162,186,200,60,139,123,55,23,32,100,108,99,139,149,75,29,239,228,185,255,198,120,114,123,179,187,183,179,199,215,219,13,29,26,62,48,36,30,15,30,18,3,247,14,43,71,201,200,150,142,132,165,167,199,216,215,222,137,172,204,194,185,63,157,147,182,220,195,170,125,78,116,156,195,230,153,144,146,160,249,241,229,69,77,5,51,31,26,32,52,47,47,47,52,64,57,31,224,192,163,21,44,163,167,164,169,134,217,16,9,4,1,207,220,203,184,224,142,118,102,176,157,151,78,106,159,153,149,134,127,138,86,72,41,228,222,12,21,65,89,121,80,222,251,15,29,79,95,107,106,106,94,110,126,100,144,131,111,218,235,15,26,57,40,14,234,44,22,179,187,186,185,12,0,184,247,242,248,2,95,190,226,236,250,123,83,54,77,242,234,66,67,5,8,18,38,46,53,55,186,157,63,250,210,186,157,31,18,25,43,37,31,22,238,242,248,9,21,90,49,4,244,228,169,240,247,102,80,83,81,95,124,84,89,102,113,127,237,231,163,149,129,104,77,66,74,180,187,199,220,245,238,231,199,192,97,98,143,158,183,202,41,50,60,220,251,188,192,86,58,71,140,144,155,196,121,107,86,88,96,110,223,42,20,4,30,37,71,4,253,59,52,196,159,100,14,104,89,105,135,65,190,206,209,223,236,25,32,25,12,252,226,236,255,15,6,253,110,84,210,169,59,31,30,177,146,156,157,200,113,156,147,138,136,167,102,108,249,239,229,34,28,1,0,245,165,108,97,76,58,35,255,235,230,233,230,182,181,252,234,36,44,26,244,249,25,32,125,135,158,176,191,174,80,60,91,109,177,191,179,169,168,171,158,155,206,212,135,135,159,191,221,235,216,19,52,81,68,63,73,88,83,63,57,25,9,0,252,251,7,127,107,107,147,154,179,182,221,216,228,254,24,34,8,248,252,234,249,94,213,13,24,32,156,202,116,129,128,137,148,167,184,205,226,173,138,108,41,247,222,214,190,166,142,130,143,177,65,95,78,45,5,192,194,187,200,205,117,41,248,5,15,26,40,30,27,34,42,47,55,46,48,147,8,11,114,129,140,235,68,62,49,74,155,74,99,112,102,64,70,185,195,157,150,142,138,68,63,63,70,81,76,68,80,73,30,52,48,52,68,81,96,72,143,119,102,95,161,158,217,244,16,42,68,164,22,22,66,108,114,252,252,251,229,221,222,8,2,15,127,188,173,32,54,61,51,52,100,101,98,143,180,210,78,43,164,158,163,174,115,163,164,16,39,245,233,220,83,40,66,81,212,16,108,143,160,171,64,59,14,240,246,129,142,170,226,171,116,145,157,179,213,167,166,176,13,24,163,121,141,147,159,2,40,82,95,157,165,163,127,118,105,98,186,231,233,229,194,58,108,208,183,198,153,153,176,203,229,0,81,73,65,248,204,104,39,24,5,250,241,11,242,75,54,33,150,10,233,202,180,1,20,9,140,156,180,170,174,192,198,196,199,156,127,120,115,45,27,157,150,145,148,134,86,121,112,254,8,21,38,218,192,182,216,25,224,71,83,125,113,116,128,148,114,127,157,182,205,189,213,86,109,97,150,142,101,134,137,150,172,190,201,193,181,176,161,23,99,75,182,174,46,6,74,66,59,62,49,94,65,42,96,145,159,157,161,167,0,18,2,164,174,176,189,189,183,214,216,2,11,91,233,181,191,190,6,188,196,179,158,134,95,87,92,113,169,25,42,53,71,63,39,236,1,152,128,104,99,137,134,177,149,140,2,40,72,104,39,130,176,200,239,2,236,247,254,252,14,44,65,202,170,178,191,208,234,233,194,191,137,40,20,96,77,61,51,202,51,30,249,148,200,212,204,197,193,166,189,139,118,162,92,30,245,192,68,92,140,154,181,179,172,133,132,141,120,115,64,80,93,53,6,125,248,216,165,176,171,165,164,252,159,191,227,244,14,175,172,163,161,156,126,90,62,19,66,66,128,141,62,67,161,148,207,73,205,241,28,68,132,42,29,115,90,138,94,122,137,129,240,205,62,69,78,100,7,243,221,223,225,222,202,209,218,235,230,210,22,5,44,11,12,31,48,104,109,93,166,179,176,221,237,44,60,90,105,128,196,148,160,146,22,25,34,42,116,141,168,196,231,70,207,227,236,226,201,197,185,72,91,152,248,239,98,86,216,80,85,96,89,97,31,17,30,187,172,121,198,199,202,220,60,47,248,57,19,196,121,100,70,69,96,163,123,125,130,128,23,36,233,240,109,159,173,166,158,226,46,19,193,126,91,50,142,77,161,160,162,161,75,62,204,159,161,162,204,219,219,187,170,120,209,219,250,230,221,223,224,225,154,173,76,64,41,139,105,38,192,75,47,53,165,159,145,218,127,38,5,227,122,130,252,226,207,214,8,30,133,118,103,175,82,84,75,56,218,190,188,235,131,121,123,106,89,47,191,201,121,14,90,107,218,226,27,130,87,78,72,84,7,119,55,51,184,183,191,49,131,113,145,149,143,195,163,159,211,204,172,190,197,191,183,193,214,44,46,42,7,92,133,165,164,177,190,152,106,87,88,154,152,137,77,30,238,228,223,227,220,226,25,159,203,185,105,138,182,181,189,198,196,204,144,157,164,165,168,243,138,145,232,233,235,244,197,228,238,107,74,44,31,7,244,223,207,143,155,63,50,62,138,175,186,130,130,113,243,166,253,131,92,75,146,111,125,110,117,212,220,205,203,227,4,68,65,93,148,144,172,131,116,3,2,8,41,40,180,194,200,222,198,185,13,29,62,224,172,100,132,182,232,3,56,174,188,203,60,4,173,196,182,205,191,189,188,193,255,31,196,197,229,188,62,20,216,79,56,163,246,250,245,239,57,79,253,3,0,62,57,244,219,211,208,209,120,123,85,63,202,201,4,153,247,19,30,8,128,53,58,198,154,143,138,85,95,111,80,90,94,207,164,155,143,123,84,242,129,58,37,10,154,186,172,170,217,199,182,181,203,198,30,234,252,194,168,171,175,187,224,184,153,129,44,98,110,239,241,119,151,173,80,79,196,140,139,123,101,107,138,166,196,224,127,121,86,82,105,143,176,199,148,126,79,90,132,222,242,12,203,165,23,21,243,247,195,122,146,162,194,186,179,99,88,226,234,240,221,197,142,152,34,54,190,229,197,109,83,60,54,52,209,153,142,158,241,240,69,82,170,89,88,210,218,228,215,210,235,252,51,74,254,243,107,8,162,250,14,50,120,121,121,58,9,82,193,202,204,217,225,24,249,248,125,117,146,109,211,75,54,56,34,250,2,2,6,121,111,20,55,52,36,20,114,146,161,210,179,166,185,0,36,42,46,120,142,160,170,243,65,66,146,63,48,22,71,109,91,189,194,111,113,253,11,1,249,80,71,29,168,165,120,83,10,246,165,147,147,185,195,211,232,68,18,30,61,22,248,207,31,89,92,29,217,26,8,222,14,44,72,12,38,118,132,146,163,182,157,226,193,1,122,81,217,26,38,4,232,3,251,173,123,130,153,160,217,202,204,20,107,69,60,6,114,100,89,78,44,207,176,101,230,103,251,73,236,220,56,13,35,152,172,72,206,209,110,106,112,144,82,94,10,23,197,228,201,199,180,138,202,31,49,65,139,151,190,25,24,35,60,219,200,180,167,51,46,39,49,54,24,155,16,193,37,31,41,30,2,65,112,140,124,137,139,134,52,246,159,160,71,162,158,245,39,230,228,254,32,23,108,159,165,174,6,55,164,21,8,218,88,202,45,127,198,190,117,28,48,113,89,53,166,155,52,158,25,195,39,35,20,2,229,185,147,10,13,50,78,11,217,120,120,90,159,150,18,99,220,185,173,252,245,14,65,70,22,40,73,187,21,194,136,224,37,28,90,12,19,22,24,168,147,39,237,244,32,131,62,71,91,183,44,20,13,195,162,135,12,123,118,91,232,167,119,32,58,53,127,130,117,153,12,53,37,77,97,175,180,77,89,47,127,39,108,20,236,228,16,165,173,89,126,167,95,150,61,50,26,6,249,230,225,141,49,41,134,145,163,167,250,254,122,136,212,182,186,69,58,157,159,168,185,190,217,169,70,78,34,61,104,39,229,177,51,251,149,181,114,107,133,218,228,177,7,176,175,223,151,139,104,117,222,10,199,218,133,160,251,205,112,116,43,151,163,166,70,42,219,223,83,48,121,195,252,177,125,192,189,239,31,209,158,77,134,46,52,198,205,198,158,21,43,230,178,114,29,192,243,228,8,191,52,41,74,205,2,27,170,189,148,22,74,128,188,};
3530 int windcodes5676[] = {152,138,172,172,141,158,157,124,119,130,125,143,190,206,205,187,171,175,136,86,111,80,0,210,177,155,125,96,80,66,162,135,121,119,150,136,86,91,93,193,201,212,203,213,227,230,243,228,210,186,138,68,242,217,216,172,145,199,31,43,57,82,110,119,95,49,31,4,13,17,254,198,173,161,182,204,108,133,122,87,88,103,64,47,44,29,26,255,235,192,133,122,156,154,84,67,68,129,169,191,181,182,140,91,49,51,62,52,76,79,52,19,241,207,175,208,225,235,225,202,207,193,192,235,236,236,224,242,250,237,252,23,13,250,231,230,217,194,193,209,173,105,76,52,39,11,246,236,233,245,240,1,246,206,177,151,91,98,170,212,207,238,5,22,29,34,43,44,45,57,74,80,85,142,153,157,129,126,110,52,13,27,44,41,2,241,248,5,11,18,34,58,59,37,16,216,191,168,172,169,165,167,137,127,231,222,228,251,24,47,81,113,147,163,142,122,82,38,59,80,81,109,154,156,55,33,36,19,42,43,30,16,12,15,24,33,20,13,2,3,6,5,252,231,191,139,91,90,82,103,158,195,205,223,229,57,90,55,42,16,20,30,37,72,128,161,184,207,200,213,220,204,198,206,212,213,185,153,132,103,75,49,23,21,23,24,50,63,53,40,25,25,45,77,92,96,75,69,54,25,23,23,21,2,243,236,220,180,133,134,162,212,241,238,247,236,246,252,250,249,234,196,169,203,221,220,206,162,168,161,106,11,247,241,246,253,242,213,17,235,193,193,240,1,13,22,0,241,228,215,238,10,29,53,31,53,47,12,9,254,234,232,235,249,234,235,228,218,205,167,182,199,208,174,255,247,237,151,184,16,74,73,59,38,23,46,76,10,31,24,252,232,240,253,252,5,222,199,228,9,13,253,241,242,3,42,27,1,12,21,242,213,177,159,186,191,167,112,58,28,47,46,67,119,196,222,238,231,219,195,195,191,16,249,250,3,12,15,18,22,34,31,24,26,41,44,39,45,44,36,23,45,54,44,43,50,88,115,38,53,38,64,105,158,192,191,194,161,189,186,161,152,91,158,144,155,162,111,71,28,1,230,234,7,28,21,28,46,88,76,42,1,224,238,227,233,224,235,251,8,4,2,4,9,26,22,1,20,54,58,57,83,97,120,83,38,31,32,36,109,195,200,202,208,216,130,148,166,191,168,169,180,169,158,161,168,182,197,158,125,131,93,52,34,17,17,250,231,231,243,230,218,235,47,30,16,25,30,21,41,55,96,104,102,113,133,180,196,216,185,142,113,145,150,108,105,63,34,253,250,13,46,63,73,111,85,74,54,77,103,159,197,219,133,191,216,243,16,26,14,15,21,28,38,205,244,32,48,58,44,44,52,40,22,247,254,5,251,8,19,16,255,4,25,35,43,51,34,1,12,20,15,9,255,247,1,17,16,25,4,10,18,14,12,45,17,245,239,228,220,174,150,124,94,87,41,51,68,80,52,45,80,89,104,87,249,196,205,223,224,237,246,240,228,218,211,3,32,62,154,137,79,47,22,24,24,25,6,237,229,234,7,19,32,30,26,12,19,36,85,117,158,175,180,173,157,244,221,220,234,222,204,196,209,233,239,242,232,220,223,196,183,208,206,231,234,224,203,221,237,233,221,209,179,160,138,137,142,132,118,91,76,78,71,56,31,19,9,2,11,14,17,11,3,0,249,19,35,37,80,79,65,53,51,39,26,22,19,12,5,13,16,19,22,45,66,113,102,103,113,135,104,73,60,63,82,65,74,90,50,21,19,1,3,18,24,17,19,35,38,48,249,240,252,244,224,246,253,244,247,223,206,179,138,92,118,139,89,120,126,162,214,243,252,3,3,2,253,236,210,45,25,194,226,255,3,2,254,1,8,5,5,5,14,245,240,248,234,148,161,120,121,180,224,219,230,236,233,225,229,217,233,211,103,110,143,171,157,184,146,102,86,46,16,6,236,250,241,230,238,250,255,253,6,13,5,252,244,229,161,106,53,29,15,246,204,175,173,144,146,247,250,9,2,243,24,30,31,40,52,21,4,246,242,237,237,238,237,191,158,202,239,232,3,14,19,28,38,52,98,152,244,249,43,63,94,100,93,39,21,6,253,2,23,36,50,207,225,213,233,245,226,221,233,14,3,236,198,228,1,251,228,226,220,205,222,228,226,188,129,89,185,167,171,213,211,218,245,36,137,131,85,48,11,241,250,11,14,34,63,87,133,177,193,217,235,8,54,101,90,76,62,57,67,59,41,13,1,6,23,29,41,80,134,168,223,11,31,31,41,38,27,23,16,236,201,194,222,233,237,0,10,4,249,251,14,40,31,7,236,167,136,172,171,15,28,40,44,36,14,5,14,253,234,234,242,244,239,230,0,34,74,88,69,69,16,245,253,248,222,55,61,91,4,5,252,239,217,213,200,150,131,139,145,160,185,209,220,228,210,202,170,131,108,11,246,242,14,27,26,25,28,34,45,60,64,90,151,246,229,236,255,247,253,2,243,235,229,244,10,177,173,195,198,185,202,187,135,103,81,31,246,232,245,241,250,246,251,10,17,16,6,15,21,10,10,19,18,23,29,59,46,31,19,14,20,24,22,10,24,62,57,58,45,28,27,42,58,48,43,53,38,47,38,10,249,15,17,36,68,69,47,29,31,47,12,17,31,48,52,62,121,140,174,210,254,22,47,56,29,2,234,53,39,48,80,88,121,162,179,203,214,223,214,212,229,254,215,158,112,161,208,228,222,218,184,171,158,174,217,242,51,32,156,130,127,156,189,199,210,194,157,105,77,94,64,37,18,14,6,230,217,207,203,224,224,201,230,11,18,38,55,33,18,3,9,16,34,19,218,145,145,120,90,85,66,51,39,20,238,209,173,180,253,245,219,218,237,238,231,232,197,184,236,254,248,240,252,14,30,13,254,4,255,246,254,8,253,4,233,7,36,53,36,33,30,9,12,33,65,85,80,71,52,39,47,61,77,121,112,92,153,144,154,148,115,63,8,20,25,25,19,41,34,30,15,207,101,75,170,169,188,209,201,201,177,207,217,216,199,214,229,254,28,56,102,137,163,167,149,205,163,130,145,159,191,203,188,171,194,164,148,165,128,141,179,189,150,165,170,184,226,234,221,193,227,60,88,134,165,150,143,102,59,43,29,20,17,22,28,23,21,23,27,45,39,20,224,225,237,227,225,243,255,253,252,231,188,180,214,211,218,229,153,124,78,33,17,13,3,3,25,31,46,33,7,251,3,22,30,31,24,40,71,122,79,15,177,111,86,100,117,105,169,207,199,206,220,237,251,247,231,221,177,178,98,98,60,73,134,182,173,182,176,144,64,60,113,149,97,47,36,38,24,254,218,225,242,241,229,221,166,76,39,25,45,69,109,156,187,87,77,86,89,91,31,17,33,67,62,65,67,106,136,204,213,210,207,216,11,17,36,72,101,15,233,224,231,243,250,251,252,25,28,23,11,1,11,4,253,16,21,11,4,13,29,53,69,109,171,198,237,229,18,55,52,68,37,48,111,68,45,41,33,30,45,63,77,67,59,27,26,39,94,189,218,234,238,6,10,244,246,25,24,20,41,34,6,255,248,246,234,229,12,34,36,29,36,56,38,38,47,74,72,56,39,39,190,224,254,12,12,20,36,24,7,11,22,47,220,205,50,30,7,207,201,20,26,32,28,13,236,67,19,2,243,254,3,13,19,16,29,30,31,8,253,1,29,79,113,71,72,225,234,252,2,254,234,220,237,0,3,226,209,181,132,133,155,150,98,84,57,76,129,169,10,25,6,16,17,33,49,72,101,101,65,40,30,22,10,16,52,95,93,84,78,80,104,138,124,79,46,25,251,211,171,183,224,207,207,230,255,243,229,238,242,251,0,244,232,228,233,226,219,224,187,169,159,254,2,3,15,22,61,47,247,190,169,183,189,161,105,67,52,63,40,19,247,4,14,9,255,30,30,20,36,32,21,236,243,238,215,198,188,197,189,239,245,254,12,5,34,30,46,33,23,27,33,25,18,7,246,244,241,4,12,16,47,87,108,120,105,115,159,201,212,218,171,99,10,214,113,65,22,27,6,232,201,202,213,3,252,238,238,244,230,220,186,175,181,190,182,223,229,231,5,247,1,5,9,242,233,237,249,225,201,235,0,236,215,182,170,164,148,196,229,239,202,177,218,244,249,3,6,228,182,166,188,245,247,247,251,248,251,234,225,241,238,1,189,197,180,167,175,162,144,133,124,117,129,147,145,169,134,123,154,123,123,124,80,61,77,53,58,78,97,90,58,63,74,50,43,36,9,7,4,9,5,0,242,241,0,5,14,15,10,12,21,22,20,8,242,241,22,248,245,235,219,203,19,8,21,32,39,46,63,253,88,68,96,94,61,49,40,66,49,28,73,61,23,21,21,25,12,18,15,12,25,22,14,0,254,242,233,3,43,25,16,10,7,10,5,2,15,25,38,42,35,62,91,160,170,126,109,93,95,107,118,112,147,122,86,68,56,49,47,59,53,48,46,75,123,102,54,27,45,79,116,122,127,90,52,36,8,229,18,43,18,4,1,231,238,254,1,29,46,26,16,12,17,10,10,5,32,74,39,20,35,69,113,99,201,230,223,230,241,13,14,10,239,211,192,207,223,4,27,22,240,243,248,8,23,18,226,217,220,227,214,201,200,178,151,77,66,80,104,144,200,241,45,40,52,93,116,119,123,94,103,140,180,216,235,249,3,248,12,20,12,11,10,2,12,4,10,26,10,15,4,243,237,223,191,162,110,76,90,66,57,100,157,173,184,222,245,25,7,5,9,11,12,2,252,244,0,240,252,26,27,8,4,5,11,17,10,4,248,17,50,40,3,234,230,232,1,231,250,47,107,125,169,211,116,104,86,86,75,89,125,193,228,1,253,227,211,219,225,248,241,245,246,238,251,239,193,208,253,246,2,185,204,243,253,0,245,251,21,39,83,94,68,74,126,154,109,9,205,153,209,203,181,164,125,85,76,88,97,95,71,23,9,10,11,21,26,251,193,200,247,255,11,255,242,225,219,222,222,236,243,246,2,8,18,251,246,1,255,9,14,14,22,26,18,21,5,243,250,4,255,254,251,217,121,132,111,99,87,70,33,28,15,33,43,36,10,0,255,233,188,155,138,144,220,206,146,125,88,87,94,191,253,231,230,27,37,17,8,0,251,198,142,56,37,44,46,21,31,34,47,36,33,58,92,84,44,15,10,0,7,1,242,230,237,229,246,226,233,237,252,224,239,247,253,20,8,209,152,139,119,102,141,195,0,173,213,248,23,24,18,10,13,21,23,24,36,43,44,43,25,40,80,113,102,132,144,175,215,245,250,242,216,206,130,57,62,104,113,96,130,150,133,132,129,72,31,254,11,18,15,7,8,241,228,246,246,16,37,25,39,65,65,77,82,140,160,172,193,228,243,160,173,224,238,244,9,9,250,223,208,203,203,215,223,244,14,36,34,17,3,5,241,201,160,142,186,225,3,27,20,10,28,1,222,234,227,224,231,205,182,180,210,230,244,250,254,241,214,203,169,130,90,72,41,27,0,233,204,177,133,120,74,244,218,197,206,247,254,10,29,44,93,149,171,233,46,70,87,85,69,46,39,29,1,232,215,220,242,37,25,37,55,63,61,65,87,103,148,179,195,207,195,164,205,220,211,202,241,238,242,13,25,51,105,137,97,80,85,77,73,73,44,39,40,61,63,85,87,70,47,39,31,46,83,169,222,22,27,26,22,31,51,87,126,143,174,177,207,227,246,10,29,37,53,41,31,19,21,55,46,43,24,14,24,37,39,47,24,3,245,226,196,152,142,162,186,210,221,224,231,204,234,3,13,10,8,26,27,1,251,2,236,254,10,16,35,56,82,55,35,26,106,69,88,151,169,151,146,140,88,48,23,30,69,61,51,40,49,85,74,59,28,16,254,242,11,40,32,17,3,251,234,206,217,196,7,247,239,245,249,242,246,211,191,214,3,9,19,43,94,79,79,101,122,102,47,18,252,224,188,235,7,9,1,251,230,195,182,123,73,72,92,124,153,12,11,7,4,253,249,252,247,229,190,190,224,226,221,228,201,174,75,91,103,97,173,127,133,123,148,174,202,174,189,237,251,237,244,253,241,228,212,220,213,219,7,90,120,116,124,112,107,246,235,221,237,190,222,254,21,32,26,14,33,20,20,21,24,35,41,53,45,42,43,25,184,143,76,56,244,235,218,231,2,18,21,12,246,20,11,252,253,218,230,195,223,246,252,8,32,99,130,158,193,158,136,229,225,209,208,199,176,203,161,95,87,92,120,55,44,24,8,231,188,134,43,8,245,236,232,241,5,248,230,237,251,12,16,29,29,21,27,23,3,252,0,17,47,31,18,10,249,4,248,22,10,236,59,74,97,75,44,47,41,18,6,12,8,6,17,27,31,29,19,8,22,44,234,5,45,94,104,62,59,54,55,91,63,7,13,20,18,20,23,48,51,76,51,30,31,27,52,83,59,52,23,14,48,38,17,250,205,234,29,100,86,68,59,41,32,20,11,8,82,103,11,247,243,244,247,6,17,27,47,50,35,30,24,43,77,139,173,167,150,162,204,190,207,237,254,252,253,36,47,75,90,67,28,13,188,130,32,12,18,9,106,90,61,65,90,92,99,141,186,165,155,196,230,224,215,253,217,209,206,201,231,255,10,40,47,19,245,207,172,177,77,60,89,181,190,224,248,255,249,244,238,240,151,175,183,220,65,135,187,209,253,255,240,23,26,45,39,10,120,146,117,227,177,218,226,230,209,207,217,197,139,115,76,58,101,111,70,34,29,27,19,4,6,20,14,12,234,197,187,202,182,161,200,237,227,207,178,160,176,36,13,7,24,46,38,55,78,95,56,26,3,8,22,49,57,254,226,160,162,174,163,180,145,171,124,66,54,124,84,70,55,44,40,46,249,215,229,231,146,127,147,181,190,151,88,27,4,234,249,231,223,224,224,243,254,241,132,133,57,17,6,15,13,2,247,249,226,208,4,40,45,55,17,1,217,246,12,1,2,245,237,230,245,6,1,243,243,246,45,138,176,228,251,21,92,48,44,31,46,28,6,21,15,24,40,84,115,118,68,116,108,83,61,55,47,38,61,64,46,54,72,62,83,140,127,123,54,6,203,155,133,128,165,182,206,178,134,101,101,96,16,22,36,32,11,247,67,66,39,44,24,31,30,30,15,252,223,171,59,44,170,179,174,163,187,218,253,206,215,220,207,185,132,112,18,222,224,241,184,171,195,207,226,216,212,10,16,28,25,27,51,75,116,128,147,192,170,192,184,150,104,126,92,87,132,117,146,222,204,177,159,168,219,237,232,91,236,217,160,109,66,95,156,186,208,212,226,43,97,164,155,157,118,25,245,1,250,235,223,216,253,7,20,49,70,83,93,109,222,106,74,26,19,23,22,5,5,12,242,252,46,45,35,17,33,22,37,22,46,70,67,62,68,87,180,211,227,229,217,255,197,213,219,3,253,248,8,240,196,131,133,154,236,226,174,175,208,233,218,188,144,147,58,36,5,3,16,26,7,4,237,221,5,49,43,19,62,59,69,19,0,236,230,248,246,26,33,22,0,13,19,140,122,117,69,24,255,235,206,128,94,77,63,69,66,79,107,69,68,3,233,241,228,199,217,223,208,239,251,7,3,9,15,9,247,244,0,9,247,186,157,101,121,28,24,24,24,98,138,186,223,219,197,182,172,217,183,166,217,145,216,134,169,199,201,162,91,61,26,25,23,31,41,34,31,11,0,212,205,216,218,223,247,253,251,218,216,200,143,121,129,84,54,23,13,249,37,35,39,35,134,158,229,255,53,76,63,132,155,170,245,255,10,1,250,71,76,55,43,49,55,45,82,126,132,95,89,227,244,250,223,218,203,190,40,22,19,18,33,38,41,108,116,75,24,255,251,246,233,14,241,250,247,3,29,54,51,18,13,37,28,3,241,251,16,252,234,10,41,25,17,29,19,223,10,26,252,254,66,84,59,100,113,242,220,203,205,222,52,57,90,73,98,109,60,30,8,135,140,155,48,21,21,51,244,9,18,21,42,52,70,98,101,66,90,68,33,10,4,15,25,26,25,55,205,233,228,215,226,14,252,221,235,26,25,16,254,34,89,245,240,255,236,233,194,222,247,24,33,47,44,37,44,37,21,8,34,76,62,39,95,67,74,88,74,35,19,57,88,120,144,173,212,238,9,18,27,63,38,32,9,6,15,29,30,45,150,187,177,172,138,118,48,58,55,158,174,156,92,32,17,24,48,24,29,35,31,22,252,170,138,3,11,245,2,30,8,0,13,39,44,43,43,53,40,235,246,251,244,21,49,101,130,177,113,70,96,135,148,159,0,9,12,15,14,5,247,239,239,241,3,36,24,29,107,109,152,183,191,178,108,56,93,47,28,32,40,40,83,142,224,6,28,32,13,239,255,255,253,12,8,12,26,28,52,146,135,143,85,50,18,246,0,2,244,19,36,74,127,109,103,90,81,68,72,85,166,132,81,39,47,45,35,33,31,225,221,205,183,167,129,11,113,183,208,222,7,240,223,227,252,227,11,10,3,238,249,11,242,137,190,159,12,0,247,239,254,254,195,92,42,204,211,206,219,215,199,156,127,30,22,250,162,124,219,34,27,11,10,242,205,229,42,46,42,5,22,52,52,49,56,47,253,234,204,183,148,183,178,126,141,85,9,251,242,247,243,12,12,32,62,77,53,30,20,40,41,36,25,19,15,21,19,251,9,208,240,221,2,3,3,30,63,85,107,114,113,134,112,66,70,113,141,171,215,210,248,214,103,78,75,58,19,38,4,250,232,197,156,180,57,46,22,0,248,251,229,244,248,242,230,239,241,213,163,152,145,155,203,206,193,0,158,235,1,7,7,12,22,245,244,0,254,162,156,188,223,254,11,251,243,239,227,229,213,136,168,120,96,109,165,217,222,244,187,154,118,138,200,203,249,32,24,17,19,21,5,251,5,3,252,2,6,9,1,206,240,12,255,243,250,106,178,208,160,147,195,170,166,162,142,133,114,154,116,138,189,152,119,94,193,179,127,107,168,130,32,31,30,28,37,38,37,48,108,126,100,117,70,33,29,47,40,54,41,32,252,2,245,5,234,225,208,237,6,2,14,11,22,19,10,255,17,22,30,212,207,228,61,160,219,252,251,201,201,170,241,249,10,114,60,47,91,130,85,49,39,36,112,76,30,22,0,36,3,4,254,33,23,12,254,251,14,43,22,44,17,253,252,243,243,7,1,200,189,211,9,45,86,73,10,252,8,16,7,0,17,15,15,18,24,41,41,41,30,11,245,92,72,73,158,125,106,91,127,118,58,117,109,98,122,149,118,92,86,72,45,58,72,86,52,40,62,58,26,102,151,182,147,77,14,2,11,27,3,161,128,156,178,130,33,53,54,63,23,169,97,72,70,230,236,255,3,254,40,48,92,5,254,1,19,13,11,255,253,6,233,20,92,119,61,14,252,2,71,121,137,201,228,242,214,227,2,45,24,26,9,254,218,217,207,183,100,63,44,59,100,201,243,238,237,244,253,62,94,196,209,192,44,183,210,210,192,109,17,21,16,117,98,107,145,66,82,63,162,243,8,2,241,19,11,20,7,9,16,18,11,26,253,10,4,247,229,244,238,234,158,78,65,98,79,69,40,47,184,172,183,142,112,231,3,255,12,8,4,255,243,227,226,251,14,200,224,53,36,249,12,7,45,7,9,225,230,252,45,60,81,90,65,228,238,252,45,146,232,246,2,38,72,108,65,77,58,56,68,63,83,234,246,4,17,26,226,213,195,213,221,254,222,224,23,47,71,163,243,89,141,103,31,20,13,6,12,10,255,242,85,106,117,67,136,176,204,143,82,137,68,54,54,99,122,102,13,243,236,240,17,16,39,99,18,250,242,7,34,30,205,204,219,213,222,230,244,12,24,23,5,219,16,253,247,244,13,10,23,49,34,225,228,232,17,23,105,149,147,126,108,87,176,1,31,21,12,17,245,51,1,253,241,235,18,89,139,143,126,250,226,177,246,128,81,44,26,19,12,253,216,152,39,68,61,65,35,8,51,30,24,34,8,129,41,26,253,19,14,18,249,221,211,230,3,243,239,225,227,251,161,180,254,248,255,254,7,222,227,172,122,98,136,101,99,155,223,8,27,12,247,248,37,23,13,30,43,38,67,56,8,37,73,62,70,114,145,120,129,153,209,254,2,248,251,82,207,161,75,57,189,188,146,168,139,96,60,255,11,25,22,10,3,1,224,198,59,38,12,28,46,67,128,90,60,117,227,68,130,179,214,247,250,0,18,13,216,8,248,21,30,17,9,74,1,211,201,159,189,146,219,249,237,195,207,99,58,40,1,237,255,251,243,178,147,120,94,98,21,206,6,19,239,242,21,130,173,227,20,47,64,131,125,31,32,56,53,40,42,195,31,1,31,61,78,78,66,67,94,117,186,203,187,14,231,215,209,225,218,209,236,10,24,40,81,143,84,87,54,76,60,73,68,20,25,92,82,106,117,36,26,16,31,56,206,33,33,95,95,125,157,218,236,10,10,19,22,59,44,33,20,7,37,65,26,13,251,4,45,47,47,52,255,192,160,129,118,145,194,184,194,223,154,208,228,23,14,253,240,208,122,29,230,25,7,4,19,15,40,65,175,198,161,135,113,111,57,52,47,98,68,33,38,29,119,127,112,245,234,217,243,17,44,241,207,222,39,3,10,4,223,244,253,0,244,230,251,186,169,184,7,18,7,67,116,136,20,17,14,172,124,135,249,9,13,5,242,238,70,42,53,94,19,17,11,2,0,1,8,6,18,254,143,145,2,239,192,202,170,104,76,215,130,22,231,242,249,1,14,219,5,32,29,27,15,33,63,201,179,252,203,200,17,16,29,15,17,44,239,36,169,217,147,4,33,142,100,105,124,102,8,189,95,117,147,20,43,50,36,14,172,136,162,153,90,22,225,223,34,237,221,209,218,21,56,39,25,12,27,43,250,253,204,111,53,9,255,234,246,35,228,183,27,23,35,52,249,21,38,24,7,248,12,40,79,121,135,70,73,65,153,241,5,20,3,21,25,5,0,26,28,130,124,76,43,46,33,15,39,40,46,6,253,3,193,247,223,241,216,250,252,231,67,44,19,24,18,50,177,33,8,106,164,250,8,6,233,239,47,26,27,128,12,241,13,251,240,135,120,81,24,70,103,86,109,100,144,193,246,179,192,203,182,153,18,9,9,18,35,76,47,225,241,40,0,13,15,254,178,188,199,230,226,229,235,90,12,18,33,26,7,12,19,234,205,189,171,189,244,215,168,241,245,16,47,138,81,90,104,3,12,232,172,199,170,147,79,111,219,52,70,72,50,61,81,240,205,206,2,106,178,168,207,92,58,21,8,13,18,31,15,189,155,46,71,64,14,33,77,6,19,239,224,239,255,138,177,119,247,152,17,29,150,58,42,59,41,74,58,37,35,26,246,157,3,216,227,246,88,136,59,55,107,97,71,20,61,33,29,234,207,26,160,177,172,139,49,51,132,234,89,108,33,202,201,9,26,14,9,35,124,118,24,225,236,244,36,55,52,140,47,13,13,229,248,255,250,13,36,26,17,236,200,250,56,3,2,31,60,21,73,72,82,164,254,238,199,34,29,90,102,66,81,238,248,177,161,60,15,248,238,247,24,161,200,52,211,12,31,80,57,154,248,10,6,32,21,3,235,229,31,132,9,9,98,102,248,150,160,73,6,201,224,41,63,67,15,52,27,197,200,201,142,36,21,57,76,82,71,2,249,18,229,51,128,119,4,254,251,19,29,13,42,236,16,236,66,15,178,247,211,91,115,111,77,57,10,134,236,34,40,220,230,1,63,97,206,2,214,235,213,251,4,46,44,50,49,30,23,221,0,14,63,97,181,21,82,113,134,144,255,6,8,24,23,20,18,209,169,80,98,116,180,170,153,70,45,14,15,22,31,41,5,244,54,47,45,52,254,253,241,24,142,27,117,137,151,11,39,17,9,14,177,200,200,43,33,20,37,24,11,23,35,244,13,46,9,14,241,244,8,32,51,46,234,16,23,30,96,17,60,127,30,16,48,45,54,63,65,190,180,152,207,229,219,233,222,136,237,249,8,116,181,185,36,28,17,40,74,81,10,235,163,217,43,49,32,17,4,250,37,11,26,242,8,244,39,103,132,143,150,136,236,54,31,219,186,114,105,69,34,7,251,6,213,204,185,225,165,200,216,11,8,18,248,92,181,9,16,243,231,248,154,185,123,74,242,11,8,237,141,161,121,221,172,143,13,24,23,2,30,31,55,77,31,253,17,10,167,207,140,38,242,224,243,246,15,193,120,161,17,19,121,84,218,2,6,225,125,41,13,23,7,229,23,1,213,245,244,212,233,216,133,113,64,39,46,221,38,81,66,62,253,16,192,226,2,51,52,95,29,214,185,86,221,42,89,5,38,46,68,37,44,43,59,248,237,222,171,63,59,52,35,4,1,224,216,168,25,33,34,29,32,221,40,38,75,220,26,74,181,105,32,7,245,247,142,59,110,198,49,162,56,23,51,17,17,194,151,224,21,180,168,69,62,109,176,222,64,93,121,73,177,118,255,253,48,61,56,9,185,41,9,254,196,43,42,136,33,41,33,216,194,169,29,14,0,32,32,30,65,128,120,144,101,39,241,127,15,64,240,248,124,40,119,226,2,118,173,100,41,39,166,15,21,37,9,60,24,239,166,139,7,235,142,88,39,};
3531 
3532 EX namespace windmap {
3533 
3534   #if HDR
3535   static const int NOWINDBELOW = 8;
3536   static const int NOWINDFROM = 120;
3537   #endif
3538 
3539   map<int, int> getid;
3540   vector<vector<int> > neighbors;
3541   vector<cellwalker> samples;
3542 
getId(cell * c)3543   EX int getId(cell *c) {
3544     auto i = fieldpattern::fieldval_uniq(c);
3545     return getid[i]-1;
3546     }
3547 
getId(cellwalker cw)3548   int getId(cellwalker cw) {
3549     auto i = fieldpattern::fieldval_uniq(cw.at);
3550     auto &id = getid[i];
3551     if(id == 0) { samples.push_back(cw); id = isize(samples); }
3552     return id-1;
3553     }
3554 
3555   EX vector<unsigned char> windcodes;
3556 
wcheck(cell * a,cell * b)3557   void wcheck(cell *a, cell *b) {
3558     int i = getId(a);
3559     if(b) {
3560       int j = getId(b);
3561       for(auto nn: neighbors[i]) if(nn == j) return;
3562       printf("neigbor not found!\n");
3563       }
3564     else {
3565       printf("%d", windcodes[i]);
3566       for(auto nn: neighbors[i]) printf("n%d ", windcodes[nn]);
3567       printf("\n");
3568       }
3569     }
3570 
create()3571   EX void create() {
3572     if(cgflags & qPORTALSPACE) return;
3573     samples.clear();
3574     neighbors.clear();
3575     getid.clear();
3576     getId(cellwalker(currentmap->gamestart(), 0));
3577     for(int k=0; k<isize(samples); k++) {
3578       cellwalker cw = samples[k];
3579       // cw.spin = 0;
3580       neighbors.emplace_back();
3581       auto &v = neighbors.back();
3582       if(NONSTDVAR && !sphere && !arcm::in() && !hybri && !INVERSE)
3583         for(int l=0; l<S7; l++) {
3584           v.push_back(getId(cw + cth + l + wstep + cth));
3585           }
3586       else
3587         for(int l=0; l<cw.at->type; l++) v.push_back(getId(cw+l+wstep));
3588       }
3589 
3590     int N = isize(samples);
3591 
3592     int* precomp = NULL;
3593 
3594     windcodes.resize(N);
3595 
3596     if(N == 18920) precomp = windcodes18920;
3597     if(N == 5676) precomp = windcodes5676;
3598 
3599     if(precomp && (hyperbolic || hybri) && isize(currfp.matrices)) {
3600       int randval = hrand(isize(currfp.matrices));
3601       for(int i=0; i<N; i++)
3602         windcodes[i] = precomp[getid[fieldpattern::fieldval_uniq_rand(samples[i].at, randval)]-1];
3603       return;
3604       }
3605 
3606     int tries = 0;
3607     int maxtries = specialland == laVolcano || specialland == laBlizzard || ls::any_chaos() ? 20 : 1;
3608     tryagain:
3609 
3610     for(int i=0; i<N; i++) windcodes[i] = hrand(256);
3611 
3612     vector<bool> inqueue(N, true);
3613     vector<int> tocheck;
3614     for(int i=0; i<N; i++) tocheck.push_back(i);
3615     hrandom_shuffle(tocheck);
3616 
3617     for(int a=0; a<isize(tocheck); a++) {
3618       if(a >= 200*N) { printf("does not converge\n"); break; }
3619       int bestval = 1000000000, best = 0;
3620       int i = tocheck[a];
3621       for(int k=0; k<256; k++) {
3622         int j = windcodes[i] + k;
3623         int cval = 0;
3624         for(int c2: neighbors[i]) {
3625           int d = (j - windcodes[c2]) & 255;
3626           if(d > 128) d = 256 - d;
3627           cval += d*d;
3628           }
3629         if(cval < bestval) bestval = cval, best = j;
3630         }
3631       if(windcodes[i] != best)
3632         for(int c2: neighbors[i])
3633           if(!inqueue[c2])
3634             inqueue[c2] = true, tocheck.push_back(c2);
3635       inqueue[i] = false;
3636       windcodes[i] = best;
3637       }
3638 
3639     int ingroup[4];
3640     for(int u=0; u<4; u++) ingroup[u] = 0;
3641     for(int i=0; i<N; i++) ingroup[windcodes[i] >> 6]++;
3642     for(int u=0; u<4; u++) if(!ingroup[u]) {
3643       tries++;
3644       if(tries < maxtries) goto tryagain;
3645       }
3646     if(tries >= maxtries && maxtries >= 20) {
3647       addMessage("Failed to generate an interesting wind/lava pattern.");
3648       }
3649     else if(false) {
3650       printf("tocheck size = %d\n", isize(tocheck));
3651       printf("if(N == %d) {\n", N);
3652       printf("  windcodes = {");
3653       for(int i=0; i<N; i++) printf("%d,", windcodes[i]);
3654       printf("};\n");
3655       printf("  return;\n");
3656       printf("  }\n");
3657       }
3658     }
3659 
at(cell * c)3660   EX int at(cell *c) {
3661     return windmap::windcodes[windmap::getId(c)];
3662     }
3663 
3664 EX }
3665 #endif
3666 
3667 // Halloween namespace
3668 
3669 EX namespace halloween {
3670   EX cell *dragoncells[4];
3671   vector<cell*> srch;
3672 
farempty(bool lastresort=false)3673   cell *farempty(bool lastresort = false) {
3674     int maxdist = 0;
3675     vector<cell*> validcells;
3676     int firstfar1 = 0;
3677     for(int i=1; i<isize(dcal); i++) {
3678       bool okay =
3679         dcal[i]->item == itNone && dcal[i]->monst == moNone && dcal[i]->wall == waNone;
3680       if(lastresort)
3681         okay = dcal[i]->wall != waChasm && !isMultitile(dcal[i]->monst);
3682       if(okay) {
3683         if(dcal[i]->cpdist > maxdist)
3684           firstfar1 = isize(validcells),
3685           maxdist = dcal[i]->cpdist;
3686         validcells.push_back(dcal[i]);
3687         }
3688       }
3689 
3690     int qvc = isize(validcells);
3691     if(qvc == firstfar1) return farempty(true);
3692 
3693     return validcells[firstfar1 + hrand(qvc - firstfar1)];
3694     }
3695 
3696   int demoncount;
3697   int swordpower;
3698   int dragoncount;
3699 
reset()3700   void reset() {
3701     demoncount = 0;
3702     swordpower = 0;
3703     dragoncount = 0;
3704     }
3705 
nextDemon()3706   eMonster nextDemon() {
3707     demoncount++;
3708     if(demoncount % 9 == 0) return moGreater;
3709     return moLesser;
3710     }
3711 
putMonster(eMonster m)3712   void putMonster(eMonster m) {
3713     cell *c = farempty();
3714     changes.ccell(c);
3715     c->hitpoints = 3;
3716     c->monst = m;
3717     playSeenSound(c);
3718     if(!kills[m]) switch(m) {
3719       case moGhost:
3720         addMessage(XLAT("Ghosts can move through chasms!"));
3721         break;
3722       case moSkeleton:
3723         addMessage(XLAT("Push Skeletons into the holes!"));
3724         break;
3725       case moDraugr:
3726         addMessage(XLAT("You'll need your magical sword against the Draugar!"));
3727         break;
3728       case moLesser:
3729         addMessage(XLAT("Demons are slow, but you'll need the experience against stronger ones..."));
3730         break;
3731       case moGreater:
3732         addMessage(XLAT("You will need more experience to defeat the Greater Demon!"));
3733         break;
3734       case moPyroCultist:
3735         addMessage(XLAT("Cultists throw fire at you from distance!"));
3736         break;
3737       case moFlailer:
3738         addMessage(XLAT("Defeat Flail Guards by moving away from them."));
3739         break;
3740       case moVampire:
3741         addMessage(XLAT("Vampire Bats drain your magical powers!"));
3742         break;
3743       default: break;
3744       }
3745     c->stuntime = 2;
3746     }
3747 
getTreat(cell * where)3748   EX void getTreat(cell *where) {
3749     if(!items[itTreat]) reset();
3750     gainItem(itTreat);
3751     changes.at_commit(after_treat);
3752     }
3753 
after_treat()3754   EX void after_treat() {
3755     farempty()->item = itTreat;
3756     int itr = items[itTreat];
3757     items[itOrbTime] += 30;
3758     changes.value_keep(dragoncount);
3759     changes.value_keep(demoncount);
3760     changes.value_keep(swordpower);
3761 
3762     int monpower = 19 + itr + hrand(itr);
3763     int mcount = 0;
3764     while(monpower > 0) {
3765       int id = hrand(10000);
3766 
3767 #define CHANCE(x) (id -= x, id < 0)
3768       if(CHANCE(400) && itr >= 5) {
3769         putMonster(moGhost);
3770         monpower -= 30;
3771         mcount++;
3772         }
3773       else if(CHANCE(400)) {
3774         putMonster(pick(moWitch, moZombie));
3775         monpower -= 20;
3776         mcount++;
3777         }
3778       else if(CHANCE(400) && itr >= 5) {
3779         putMonster(nextDemon());
3780         monpower -= 10;
3781         mcount++;
3782         }
3783       else if(CHANCE(100) && itr >= 12) {
3784         putMonster(moVampire);
3785         monpower -= 10;
3786         swordpower -= 5;
3787         if(swordpower < 0) swordpower = 0;
3788         mcount++;
3789         }
3790       else if(CHANCE(400) && swordpower > 0 && !mcount && itr >= 15) {
3791         putMonster(moDraugr);
3792         swordpower -= 3;
3793         monpower -= 100;
3794         mcount++;
3795         }
3796       else if(CHANCE(400) && itr >= 10 && !mcount && itr >= 10) {
3797         putMonster(moSkeleton);
3798         monpower -= 60;
3799         mcount++;
3800         }
3801       else if(CHANCE(10) && itr >= 15) {
3802         putMonster(moWitchFire);
3803         monpower -= 35;
3804         mcount++;
3805         }
3806       else if(CHANCE(100) && itr >= 5) {
3807         putMonster(moFlailer);
3808         monpower -= 35;
3809         mcount++;
3810         }
3811       else if(CHANCE(100) && itr >= 5) {
3812         putMonster(moPyroCultist);
3813         monpower -= 35;
3814         mcount++;
3815         }
3816       else if(CHANCE(5) && itr >= 20 && kills[moSkeleton]) {
3817         putMonster(moFatGuard);
3818         monpower -= 35;
3819         mcount++;
3820         }
3821       else if(CHANCE(5) && itr >= 30 && kills[moFlailer]) {
3822         putMonster(moHedge);
3823         monpower -= 35;
3824         mcount++;
3825         }
3826       else if(CHANCE(5) && itr >= 40 && kills[moHedge]) {
3827         putMonster(moLancer);
3828         monpower -= 60;
3829         mcount++;
3830         }
3831       else if(CHANCE(1) && itr >= 50 && kills[moHedge]) {
3832         putMonster(moFireFairy);
3833         monpower -= 40;
3834         mcount++;
3835         }
3836       else if(CHANCE(5) && itr >= 60) {
3837         putMonster(moBomberbird);
3838         monpower -= 25;
3839         mcount++;
3840         }
3841       else if(CHANCE(5) && itr >= 60) {
3842         putMonster(moRatlingAvenger);
3843         monpower -= 30;
3844         mcount++;
3845         }
3846       else if(CHANCE(5) && itr >= 60) {
3847         putMonster(moVineBeast);
3848         monpower -= 30;
3849         mcount++;
3850         }
3851       else if(CHANCE(5) && itr >= 60) {
3852         dragoncount++;
3853         }
3854       else if(dragoncount && BITRUNCATED && geometry == gSphere && !mcount) {
3855         bool fill = false;
3856         for(int i=0; i<4; i++)
3857           if(!dragoncells[i] || dragoncells[i]->monst)
3858             fill = true;
3859         swap(dragoncells[0], dragoncells[3]);
3860         swap(dragoncells[1], dragoncells[2]);
3861         if(fill) continue;
3862         for(int i=0; i<4; i++) {
3863           dragoncells[i]->monst = i ? moDragonTail : moDragonHead;
3864           dragoncells[i]->mondir = i==3 ? NODIR : neighborId(dragoncells[i], dragoncells[i+1]);
3865           dragoncells[i]->hitpoints = 1;
3866           dragoncells[i]->stuntime = 1;
3867           playSeenSound(dragoncells[i]);
3868           }
3869         monpower -= 200;
3870         mcount++;
3871         dragoncount--;
3872         }
3873       }
3874     int id = hrand(100);
3875     if(items[itTreat] == 1) {
3876 #if !ISMOBILE
3877       addMessage(XLAT("Hint: use arrow keys to scroll."));
3878 #endif
3879       }
3880     else if(items[itTreat] == 2) {
3881 #if !ISMOBILE
3882       addMessage(XLAT("Hint: press 1 to change the projection."));
3883 #endif
3884       }
3885     else if(items[itTreat] == 3) {
3886       items[itOrbShell] += 20;
3887       addMessage(XLAT("You gain a protective Shell!"));
3888       }
3889     else if(items[itTreat] == 4) {
3890       items[itOrbShell] += 10;
3891       addMessage(XLAT("Hint: magical powers from Treats expire after a number of uses."));
3892       }
3893     else if(kills[moSkeleton] && CHANCE(10)) {
3894       addMessage(XLAT("A magical energy sword. Don't waste its power!"));
3895       items[itOrbSword] += 5; // todo facing
3896       swordpower += 5;
3897       }
3898     else if(kills[moDraugr] && items[itOrbSword] && CHANCE(10)) {
3899       addMessage(XLAT("Another energy sword!"));
3900       items[itOrbSword2] += 5;
3901       swordpower += 5;
3902       }
3903     else if(kills[moFlailer] && CHANCE(10)) {
3904       items[itOrbThorns] += 5;
3905       addMessage(XLAT("You got Thorns! Stab monsters by moving around them."));
3906       }
3907     else if(kills[moGhost] && CHANCE(10)) {
3908       items[itOrbAether] += 5;
3909       addMessage(XLAT("Aethereal powers let you go through the holes."));
3910       }
3911     else {
3912       if(items[itOrbShell] > ORBBASE)
3913         addMessage(XLAT("The tasty treat increases your protection."));
3914       else
3915         addMessage(XLAT("You gain your protective Shell back!"));
3916       items[itOrbShell] += 5;
3917       }
3918     }
3919 
start_all()3920   EX void start_all() {
3921       popScreenAll();
3922       resetModes('g');
3923       stampbase = ticks;
3924       if(!sphere) {
3925         stop_game();
3926         specialland = laHalloween;
3927         set_geometry(gSphere);
3928         start_game();
3929         pconf.alpha = 999;
3930         pconf.scale = 998;
3931         }
3932       }
3933 
3934 EX }
3935 
3936 // ... also includes the Ivory Tower
3937 
3938 EX namespace dungeon {
3939 
towerError(cell * c)3940   void towerError(cell *c) {
3941     // only care in the standard geometry -- weird ones are intentionally left buggy
3942     if(!weirdhyperbolic && !sphere && !quotient)
3943       raiseBuggyGeneration(c, "ivory tower/dungeon generation error");
3944     }
3945 
buildIvoryTower(cell * c)3946   void buildIvoryTower(cell *c) {
3947     /* if(int(c->landparam) % 5 == 0)
3948       c->wall = waCamelot;
3949       */
3950 
3951     if(euclid) {
3952       if(quotient) return;
3953       auto co = euc2_coordinates(c);
3954       int x = co.first, y = co.second;
3955       string tab6[] = {
3956         ".####...",
3957         "L...L...",
3958         ".L..L...",
3959         "L...L...",
3960         "........",
3961         "........"
3962         };
3963       string tab4[] = {
3964         "L####L..",
3965         "L....L..",
3966         "L....L..",
3967         "L....L..",
3968         "........",
3969         "........"
3970         };
3971       int y0 = y; if(y>32768) y0 -= 65536;
3972 
3973       y0 += 5; y0 %= 12; if(y0<0) y0+=12;
3974 
3975       if(y0 >= 6) { y0 -= 6; x += 4; }
3976 
3977       char ch = euc::in(2,6) ? tab6[y0][(x+(y+1)/2)&7] : tab4[y0][x&7];
3978 
3979       if(ch == '#')
3980         c->wall = waPlatform;
3981       else if(ch == 'L')
3982         c->wall = waLadder;
3983       }
3984 
3985     else if(WDIM == 3) {
3986       int cnt = 0;
3987       int below = 0;
3988       manual_celllister cl;
3989       cl.add(c);
3990       for(int i=0; i<isize(cl.lst); i++) {
3991         cell *c1 = cl.lst[i];
3992         generate_around(c1);
3993         if(coastvalEdge(c1) == coastvalEdge(c) - 3) {
3994           if(c1->landflags == 3) cnt++;
3995           continue;
3996           }
3997         if(c1->landflags == 3) below++;
3998         forCellEx(c2, c1) if(coastvalEdge(c2) < coastvalEdge(c1))
3999           cl.add(c2);
4000         }
4001       if(cnt) c->wall = waPlatform;
4002       else if(below && coastvalEdge(c) < 3) c->wall = waPlatform;
4003       }
4004 
4005     else if(true) {
4006 
4007       cell *c2 = c;
4008       cell *c3 = c;
4009 
4010       bool rdepths[5];
4011       for(int i=0; i<5; i++) {
4012         if(coastvalEdge(c2) == 0) {
4013           rdepths[i] = false;
4014           }
4015         else {
4016           cell *c4 = c2;
4017           if(c2 != c3 && !isNeighbor(c2, c3)) {
4018             for(int i=0; i<c2->type; i++) if(c2->move(i) && isNeighbor(c2->move(i), c3))
4019               c4 = c2->move(i);
4020             }
4021           rdepths[i] = c2 && c3 && c4 && (c2->landflags == 3 || c3->landflags == 3 || c4->landflags == 3);
4022           if(c2) generate_around(c2);
4023           if(c3) generate_around(c3);
4024           c2 = ts::left_parent(c2, coastvalEdge);
4025           c3 = ts::right_parent(c3, coastvalEdge);
4026           if(!c2) { towerError(c); return; }
4027           if(!c3) { towerError(c); return; }
4028           }
4029         }
4030 
4031       if(rdepths[3]) {
4032         c->wall = waPlatform;
4033   //        if(!c4->item) c4->item = itPalace;
4034         }
4035       else if(!rdepths[2] && !rdepths[4] && !rdepths[1]) {
4036         c2 = c;
4037         c3 = c;
4038         generate_around(c);
4039         cell *c4 = ts::left_of(c, coastvalEdge);
4040         cell *c5 = ts::right_of(c, coastvalEdge);
4041         for(int i=0; i<3; i++) {
4042           if(coastvalEdge(c2) == 0) break;
4043           for(cell *cx: {c2, c3, c4, c5}) if(cx) generate_around(cx);
4044 
4045           if(c2 && c4 && c4->landflags == 3 && c2->landflags != 3 && c4 == ts::left_of(c2, coastvalEdge))
4046             c->wall = waLadder;
4047           if(c3 && c5 && c5->landflags == 3 && c3->landflags != 3 && c5 == ts::right_of(c3, coastvalEdge))
4048             c->wall = waLadder;
4049           buildEquidistant(c4); buildEquidistant(c5);
4050           if(c2) c2 = ts::left_parent(c2, coastvalEdge);
4051           if(c3) c3 = ts::right_parent(c3, coastvalEdge);
4052           if(c4) c4 = ts::left_parent(c4, coastvalEdge);
4053           if(c5) c5 = ts::right_parent(c5, coastvalEdge);
4054           }
4055         }
4056       }
4057     else c->wall = waCIsland;
4058     }
4059 
dungeonFlags(cell * c)4060   int dungeonFlags(cell *c) {
4061     if(!c) return 0;
4062     buildEquidistant(c);
4063     bool rdepths[5];
4064     int switchcount = 0;
4065 
4066     if(WDIM == 3) {
4067       for(int i=0; i<5; i++) rdepths[i] = false;
4068 
4069       manual_celllister cl;
4070       cl.add(c);
4071       int d = coastvalEdge(c);
4072 
4073       for(int i=0; i<isize(cl.lst); i++) {
4074         cell *c1 = cl.lst[i];
4075         generate_around(c1);
4076         int d1 = d - coastvalEdge(c);
4077         if(c1->landflags == 3) rdepths[d1] = true;
4078         if(c1->landflags == 1) switchcount++;
4079         if(d1 == 4) break;
4080         forCellEx(c2, c1) if(coastvalEdge(c2) < coastvalEdge(c1))
4081           cl.add(c2);
4082         }
4083       }
4084 
4085     else {
4086 
4087       cell *c2 = c;
4088       cell *c3 = c;
4089 
4090       for(int i=0; i<5; i++) {
4091         if(coastvalEdge(c2) == 0) {
4092           rdepths[i] = false;
4093           }
4094         else {
4095           cell *c4 = c2;
4096           if(c2 != c3 && !isNeighbor(c2, c3)) {
4097             for(int i=0; i<c2->type; i++) if(c2->move(i) && isNeighbor(c2->move(i), c3))
4098               c4 = c2->move(i);
4099             }
4100           rdepths[i] = c2 && c3 && c4 && (c2->landflags == 3 || c3->landflags == 3 || c4->landflags == 3);
4101           if((c2&&c2->landflags == 1) || (c3&&c3->landflags == 1) || (c4&&c4->landflags == 1))
4102             switchcount++;
4103           generate_around(c2);
4104           generate_around(c3);
4105           c2 = ts::left_parent(c2, coastvalEdge);
4106           c3 = ts::right_parent(c3, coastvalEdge);
4107           if(!c2) { towerError(c); return 0; }
4108           if(!c3) { towerError(c); return 0; }
4109           }
4110         }
4111       }
4112 
4113     int res = 0;
4114 
4115     if(rdepths[3]) res |= 1;
4116     if(rdepths[2]) res |= 2;
4117     if(switchcount&1) res |= 4;
4118 
4119     return res;
4120     }
4121 
placeGate(cell * c,eWall w)4122   void placeGate(cell *c, eWall w) {
4123     if(w == waOpenGate) {
4124       c->wall = waClosedGate;
4125       toggleGates(c, waOpenPlate, 0);
4126       }
4127     if(w == waClosedGate) {
4128       c->wall = waOpenGate;
4129       toggleGates(c, waClosePlate, 0);
4130       }
4131     }
4132 
isGate(eWall w)4133   bool isGate(eWall w) {
4134     return w == waOpenGate || w == waClosedGate;
4135     }
4136 
placeRandomGate(cell * c)4137   void placeRandomGate(cell *c) {
4138     placeGate(c, hrand(2) ? waOpenGate : waClosedGate);
4139     }
4140 
random_child(cell * c,const cellfunction & cf)4141   cell *random_child(cell *c, const cellfunction& cf) {
4142     generate_around(c);
4143     vector<cell*> children;
4144     forCellEx(c2, c) if(cf(c2) > cf(c)) children.push_back(c2);
4145     if(!isize(children)) return NULL;
4146     return children[hrand(isize(children))];
4147     }
4148 
build(cell * c)4149   void build(cell *c) {
4150     /* if(int(c->landparam) % 5 == 0)
4151       c->wall = waCamelot;
4152       */
4153 
4154     if(true) {
4155 
4156       if(coastvalEdge(c) == 1) forCellEx(c2, c)
4157         if(c2->land != laBarrier && c2->land != laDungeon) {
4158           c->wall = waLadder;
4159           c->wparam = 3;
4160           }
4161 
4162       int df = dungeonFlags(c);
4163 
4164       if(df&1) {
4165         generate_around(c);
4166         int df1 = WDIM == 3 ? 0 : dungeonFlags(ts::left_of(c, coastvalEdge));
4167         int df2 = WDIM == 3 ? 0 : dungeonFlags(ts::right_of(c, coastvalEdge));
4168 
4169         c->wparam = 0;
4170         if(hrand(100) < (c->landparam % 5 == 0 ? 80 : 20)) {
4171           if(!(df1&1)) c->wparam = 1;
4172           if(!(df2&1)) c->wparam = 2;
4173           }
4174 
4175         if(df&4)
4176           placeRandomGate(c);
4177         else if(c->wparam == 0 && c->landparam % 5 == 0 &&  hrand(100) < 10) {
4178           c->wall = waLadder;
4179           c->wparam = 3 + hrand(2);
4180           }
4181         else
4182           c->wall = waPlatform;
4183         }
4184 
4185       if(c->wparam) {
4186         cell *c2 =
4187           WDIM == 3 ? random_child(c, coastvalEdge) :
4188           c->wparam == 1 ? ts::add(c, 1, 2, coastvalEdge) :
4189           c->wparam == 2 ? ts::add(c, -1, -2, coastvalEdge) :
4190           c->wparam == 3 ? ts::add(c, 1, 3, coastvalEdge) :
4191           c->wparam == 4 ? ts::add(c, -1, -3, coastvalEdge) :
4192           NULL;
4193 
4194         if(c2) {
4195           c2->wall = c->wall, c2->wparam = c->wparam;
4196           if(c2->wall == waPlatform && hrand(10) < 2)
4197             placeRandomGate(c2);
4198           if(isGate(c2->wall) && hrand(10) < 2)
4199             c2->wall = waPlatform;
4200           }
4201         }
4202       }
4203     else c->wall = waCIsland;
4204     }
4205 
buildPlates(cell * c)4206   void buildPlates(cell *c) {
4207     if(c->wall) return;
4208     int neargate = 0;
4209     int neargateDown = 0;
4210     int neargateEq = 0;
4211     int qup = 0;
4212     forCellEx(c2, c) {
4213       int d = coastvalEdge(c2) - coastvalEdge(c);
4214       if(isGate(c2->wall)) {
4215         neargate++;
4216         if(d>0) neargateDown++;
4217         if(d==0) neargateEq = 0;
4218         }
4219       if(d<0) qup++;
4220       }
4221 
4222     if(!neargate) return;
4223 
4224     int hr = 99;
4225 
4226     if(neargate == neargateDown && qup == 1)
4227       hr = hrand(12);
4228     else if((zebra40(c) >= 40) && !(neargateEq && neargateDown))
4229       hr = hrand(36);
4230     else if(zebra40(c) >= 40)
4231       hr = hrand(5000);
4232 
4233     if(hr < 5) c->wall = waClosePlate;
4234     else if(hr < 10) c->wall = waOpenPlate;
4235     }
4236 
is02(int i)4237   bool is02(int i) { return i == 0 || i == 2; }
4238 
all(cell * c,int d)4239   EX void all(cell *c, int d) {
4240     if(d == 8 && (c->land == laIvoryTower || c->land == laDungeon) && !euclid) {
4241 
4242       if(hrand(1000) < 75 && (WDIM == 3 || (c->landparam & 1))) {
4243         c->landflags = 3;
4244         }
4245       else c->landflags = 0;
4246       }
4247 
4248     if(d == 8 && c->land == laDungeon && !euclid) {
4249       if(hrand(1000) < 240 && is02(c->landparam%5) ) {
4250         c->landflags = 3;
4251         }
4252       else if(hrand(1000) < 90)
4253         c->landflags = 1;
4254       else c->landflags = 0;
4255       }
4256 
4257     if(d == 7 && c->land == laIvoryTower) buildIvoryTower(c);
4258     if(d == (BARLEV == 8 ? 7 : 8) && c->land == laDungeon) build(c);
4259     if(d == 7 && c->land == laDungeon) buildPlates(c);
4260     }
4261 EX }
4262 
4263 }
4264