1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-12-19 07:20:25 +00:00
hyperrogue/cell.cpp

1226 lines
32 KiB
C++
Raw Normal View History

2016-08-26 09:58:03 +00:00
// Hyperbolic Rogue -- cells
// Copyright (C) 2011-2016 Zeno Rogue, see 'hyper.cpp' for details
2015-08-08 13:57:52 +00:00
// cells the game is played on
2017-10-28 08:04:28 +00:00
int fix6(int a) { return (a+MODFIXER)%S6; }
int fix7(int a) { return (a+MODFIXER)%S7; }
2015-08-08 13:57:52 +00:00
2016-08-26 09:58:03 +00:00
int dirdiff(int dd, int t) {
dd %= t;
if(dd<0) dd += t;
if(t-dd < dd) dd = t-dd;
return dd;
}
2016-01-02 10:09:13 +00:00
int fixdir(int a, cell *c) { a %= c->type; if(a<0) a += c->type; return a; }
2015-08-08 13:57:52 +00:00
int cellcount = 0;
void initcell(cell *c); // from game.cpp
cell *newCell(int type, heptagon *master) {
cell *c = new cell;
c->type = type;
c->master = master;
2017-10-27 18:07:58 +00:00
for(int i=0; i<MAX_EDGE; i++) c->mov[i] = NULL;
2015-08-08 13:57:52 +00:00
initcell(c);
return c;
}
2017-03-23 10:53:57 +00:00
void merge(cell *c, int d, cell *c2, int d2, bool mirrored = false) {
2015-08-08 13:57:52 +00:00
c->mov[d] = c2;
2017-03-23 10:53:57 +00:00
tsetspin(c->spintable, d, d2 + (mirrored?8:0));
2015-08-08 13:57:52 +00:00
c2->mov[d2] = c;
2017-03-23 10:53:57 +00:00
tsetspin(c2->spintable, d2, d + (mirrored?8:0));
2015-08-08 13:57:52 +00:00
}
struct cdata {
int val[4];
int bits;
};
// -- hrmap ---
hrmap *currentmap;
vector<hrmap*> allmaps;
// --- auxiliary hyperbolic map for horocycles ---
struct hrmap_alternate : hrmap {
heptagon *origin;
hrmap_alternate(heptagon *o) { origin = o; }
~hrmap_alternate() { clearfrom(origin); }
};
2017-10-29 13:19:51 +00:00
hrmap *newAltMap(heptagon *o) { return new hrmap_alternate(o); }
// --- hyperbolic geometry ---
hrmap_hyperbolic::hrmap_hyperbolic() {
// printf("Creating hyperbolic map: %p\n", this);
origin = new heptagon;
heptagon& h = *origin;
h.s = hsOrigin;
h.emeraldval = a46 ? 0 : 98;
h.zebraval = 40;
h.fiftyval = 0;
h.fieldval = 0;
h.rval0 = h.rval1 = 0;
h.cdata = NULL;
for(int i=0; i<MAX_EDGE; i++) h.move[i] = NULL;
h.spintable = 0;
h.alt = NULL;
h.distance = 0;
isnontruncated = nontruncated;
h.c7 = newCell(S7, origin);
}
// --- spherical geometry ---
int spherecells() {
if(S7 == 5) return (elliptic?6:12);
if(S7 == 4) return (elliptic?3:6);
if(S7 == 3) return 4;
if(S7 == 2) return (elliptic?1:2);
if(S7 == 1) return 1;
return 12;
}
2017-12-03 17:24:34 +00:00
vector<int> siblings;
struct hrmap_spherical : hrmap {
heptagon *dodecahedron[12];
bool isnontruncated;
hrmap_spherical() {
isnontruncated = nontruncated;
for(int i=0; i<spherecells(); i++) {
heptagon& h = *(dodecahedron[i] = new heptagon);
h.s = hsOrigin;
h.emeraldval = i;
h.zebraval = i;
h.fiftyval = i;
h.rval0 = h.rval1 = 0;
h.alt = NULL;
h.cdata = NULL;
h.spintable = 0;
2017-12-03 17:24:34 +00:00
h.fieldval = i;
for(int i=0; i<S7; i++) h.move[i] = NULL;
h.c7 = newCell(S7, &h);
}
2017-12-03 17:24:34 +00:00
if(S7 == 5)
siblings = {1, 0, 10, 4, 3, 8, 9, 11, 5, 6, 2, 7};
else
siblings = {1, 0, 3, 2, 5, 4};
for(int i=0; i<S7; i++) {
dodecahedron[0]->move[i] = dodecahedron[i+1];
dodecahedron[0]->setspin(i, 0);
dodecahedron[i+1]->move[0] = dodecahedron[0];
dodecahedron[i+1]->setspin(0, i);
dodecahedron[i+1]->move[1] = dodecahedron[(i+S7-1)%S7+1];
dodecahedron[i+1]->setspin(1, S7-1);
dodecahedron[i+1]->move[S7-1] = dodecahedron[(i+1)%S7+1];
dodecahedron[i+1]->setspin(S7-1, 1);
if(S7 == 5 && elliptic) {
dodecahedron[i+1]->move[2] = dodecahedron[(i+2)%S7+1];
dodecahedron[i+1]->setspin(2, 3 + 8);
dodecahedron[i+1]->move[3] = dodecahedron[(i+3)%S7+1];
dodecahedron[i+1]->setspin(3, 2 + 8);
}
else if(S7 == 5) {
dodecahedron[6]->move[i] = dodecahedron[7+i];
dodecahedron[6]->setspin(i, 0);
dodecahedron[7+i]->move[0] = dodecahedron[6];
dodecahedron[7+i]->setspin(0, i);
dodecahedron[i+7]->move[1] = dodecahedron[(i+4)%5+7];
dodecahedron[i+7]->setspin(1, 4);
dodecahedron[i+7]->move[4] = dodecahedron[(i+1)%5+7];
dodecahedron[i+7]->setspin(4, 1);
dodecahedron[i+1]->move[2] = dodecahedron[7+(10-i)%5];
dodecahedron[i+1]->setspin(2, 2);
dodecahedron[7+(10-i)%5]->move[2] = dodecahedron[1+i];
dodecahedron[7+(10-i)%5]->setspin(2, 2);
dodecahedron[i+1]->move[3] = dodecahedron[7+(9-i)%5];
dodecahedron[i+1]->setspin(3, 3);
dodecahedron[7+(9-i)%5]->move[3] = dodecahedron[i+1];
dodecahedron[7+(9-i)%5]->setspin(3, 3);
}
if(S7 == 4) {
dodecahedron[5]->move[3-i] = dodecahedron[i+1];
dodecahedron[5]->setspin(3-i, 2);
dodecahedron[i+1]->move[2] = dodecahedron[5];
dodecahedron[i+1]->setspin(2, 3-i);
}
}
}
heptagon *getOrigin() { return dodecahedron[0]; }
~hrmap_spherical() {
dynamicval<bool> ph(nontruncated, isnontruncated);
for(int i=0; i<spherecells(); i++) clearHexes(dodecahedron[i]);
for(int i=0; i<spherecells(); i++) delete dodecahedron[i];
}
void verify() {
for(int i=0; i<spherecells(); i++) for(int k=0; k<S7; k++) {
heptspin hs;
hs.h = dodecahedron[i];
hs.spin = k;
hs = hsstep(hs, 0);
hs = hsspin(hs, S7-1);
hs = hsstep(hs, 0);
hs = hsspin(hs, S7-1);
hs = hsstep(hs, 0);
hs = hsspin(hs, S7-1);
if(hs.h != dodecahedron[i]) printf("error %d,%d\n", i, k);
}
for(int i=0; i<spherecells(); i++) verifycells(dodecahedron[i]);
}
};
heptagon *getDodecahedron(int i) {
hrmap_spherical *s = dynamic_cast<hrmap_spherical*> (currentmap);
if(!s) return NULL;
return s->dodecahedron[i];
}
// --- euclidean geometry ---
2015-08-08 13:57:52 +00:00
cell*& euclideanAtCreate(eucoord x, eucoord y);
2017-06-09 01:41:33 +00:00
namespace torusconfig {
// the configuration of the torus topology.
// torus cells are indexed [0..qty),
// where the cell to the right from i is indexed i+dx,
2017-11-06 18:24:02 +00:00
// and the cell to the down-right is numbered i+dy
// Changed with command line option: -tpar <qty>,<dx>,<dy>
// Ideally, qty, dx, and dy should have the same "modulo 3"
// values as the default -- otherwise the three-color
// pattern breaks. Also, they should have no common
// prime divisor.
2017-11-06 20:18:40 +00:00
int def_qty = 127*3, dx = 1, def_dy = -11*2;
int qty = def_qty, dy = def_dy;
// new values to change
int newqty, newdy;
int torus_cx, torus_cy;
2017-06-09 01:41:33 +00:00
}
int decodeId(heptagon* h);
heptagon* encodeId(int id);
struct hrmap_torus : hrmap {
vector<cell*> all;
vector<int> dists;
virtual vector<cell*>& allcells() { return all; }
cell *gamestart() {
return all[0];
}
hrmap_torus() {
using namespace torusconfig;
all.resize(qty);
for(int i=0; i<qty; i++) {
all[i] = newCell(6, NULL);
all[i]->master = encodeId(i);
}
dx %= qty;
dy %= qty;
for(int i=0; i<qty; i++) {
all[i]->mov[0] = all[(i+dx+2*qty)%qty];
all[i]->mov[1] = all[(i+dy+2*qty)%qty];
all[i]->mov[2] = all[(i+dy-dx+2*qty)%qty];
all[i]->mov[3] = all[(i-dx+2*qty)%qty];
all[i]->mov[4] = all[(i-dy+2*qty)%qty];
all[i]->mov[5] = all[(i-dy+dx+2*qty)%qty];
for(int j=0; j<6; j++)
tsetspin(all[i]->spintable, j, (j+3) % 6);
}
celllister cl(gamestart(), 100, 100000000, NULL);
dists.resize(qty);
for(int i=0; i<size(cl.lst); i++)
dists[decodeId(cl.lst[i]->master)] = cl.dists[i];
}
~hrmap_torus() {
for(cell *c: all) delete c;
}
};
int toridMod(int id) {
using namespace torusconfig;
id %= qty; if(id < 0) id += qty;
return id;
}
hrmap_torus *torusmap() {
return dynamic_cast<hrmap_torus*> (currentmap);
}
cell *getTorusId(int id) {
hrmap_torus *cur = torusmap();
if(!cur) return NULL;
return cur->all[toridMod(id)];
}
struct hrmap_euclidean : hrmap {
cell *gamestart() {
return euclideanAtCreate(0,0);
}
struct euclideanSlab {
cell* a[256][256];
euclideanSlab() {
for(int y=0; y<256; y++) for(int x=0; x<256; x++)
a[y][x] = NULL;
}
~euclideanSlab() {
for(int y=0; y<256; y++) for(int x=0; x<256; x++)
if(a[y][x]) delete a[y][x];
}
};
euclideanSlab* euclidean[256][256];
hrmap_euclidean() {
for(int y=0; y<256; y++) for(int x=0; x<256; x++)
euclidean[y][x] = NULL;
}
cell*& at(eucoord x, eucoord y) {
euclideanSlab*& slab = euclidean[y>>8][x>>8];
if(!slab) slab = new hrmap_euclidean::euclideanSlab;
return slab->a[y&255][x&255];
}
map<heptagon*, struct cdata> eucdata;
~hrmap_euclidean() {
for(int y=0; y<256; y++) for(int x=0; x<256; x++)
if(euclidean[y][x]) {
delete euclidean[y][x];
euclidean[y][x] = NULL;
}
eucdata.clear();
}
};
2015-08-08 13:57:52 +00:00
union heptacoder {
heptagon *h;
struct { eucoord x; eucoord y; } c;
2017-06-09 01:41:33 +00:00
int id;
2015-08-08 13:57:52 +00:00
};
void decodeMaster(heptagon *h, eucoord& x, eucoord& y) {
2017-06-09 01:41:33 +00:00
if(torus) { printf("decodeMaster on torus\n"); exit(1); }
2015-08-08 13:57:52 +00:00
heptacoder u;
u.h = h; x = u.c.x; y = u.c.y;
}
2017-06-09 01:41:33 +00:00
int decodeId(heptagon* h) {
heptacoder u;
u.h = h; return u.id;
}
2015-08-08 13:57:52 +00:00
heptagon* encodeMaster(eucoord x, eucoord y) {
2017-06-09 01:41:33 +00:00
if(torus) { printf("encodeMaster on torus\n"); exit(1); }
2015-08-08 13:57:52 +00:00
heptacoder u;
u.c.x = x; u.c.y = y;
return u.h;
}
2017-06-09 01:41:33 +00:00
heptagon* encodeId(int id) {
heptacoder u;
u.id = id;
return u.h;
}
// --- quotient geometry ---
namespace quotientspace {
struct code {
int c[8];
};
bool operator == (const code& c1, const code &c2) {
for(int i=0; i<8; i++) if(c1.c[i] != c2.c[i]) return false;
return true;
}
bool operator < (const code& c1, const code &c2) {
for(int i=0; i<8; i++) if(c1.c[i] != c2.c[i]) return c1.c[i] < c2.c[i];
return false;
}
int cod(heptagon *h) {
return zebra40(h->c7);
}
code get(heptspin hs) {
code res;
res.c[0] = cod(hs.h);
for(int i=1; i<8; i++) {
res.c[i] = cod(hsstep(hs, 0).h);
hs = hsspin(hs, 1);
}
return res;
}
int rvadd = 0, rvdir = 1;
int rv(int x) { return (rvadd+x*rvdir) % 7; }
struct hrmap_quotient : hrmap {
hrmap_hyperbolic base;
vector<cell*> celllist;
cell *origin;
map<quotientspace::code, int> reachable;
vector<heptspin> bfsq;
vector<int> connections;
void add(const heptspin& hs) {
code g = get(hs);
if(!reachable.count(g)) {
reachable[g] = bfsq.size();
bfsq.push_back(hs);
add(hsspin(hs, 1));
}
}
vector<heptagon*> allh;
hrmap_quotient() {
if(quotient == 2) {
connections = currfp.connections;
}
else {
heptspin hs; hs.h = base.origin; hs.spin = 0;
reachable.clear();
bfsq.clear();
connections.clear();
add(hs);
for(int i=0; i<(int)bfsq.size(); i++) {
hs = hsstep(bfsq[i], 0);
add(hs);
connections.push_back(reachable[get(hs)]);
}
}
2017-11-06 23:40:46 +00:00
int TOT = connections.size() / S7;
printf("heptagons = %d\n", TOT);
2017-11-06 23:40:46 +00:00
printf("all cells = %d\n", TOT*(S7+S3)/S3);
if(!TOT) exit(1);
allh.resize(TOT);
for(int i=0; i<TOT; i++) allh[i] = new heptagon;
// heptagon *oldorigin = origin;
allh[0]->alt = base.origin;
for(int i=0; i<TOT; i++) {
heptagon *h = allh[i];
if(i) {
h->alt = NULL;
}
if(true) {
h->s = hsOrigin;
h->emeraldval = 0;
h->zebraval = 0;
h->fiftyval = 0;
2017-11-06 23:40:46 +00:00
h->fieldval = S7*i;
h->rval0 = h->rval1 = 0; h->cdata = NULL;
h->distance = 0;
2017-10-27 18:07:58 +00:00
h->c7 = newCell(S7, h);
}
2017-11-06 23:40:46 +00:00
for(int j=0; j<S7; j++) {
h->move[rv(j)] = allh[connections[i*S7+j]/S7];
h->setspin(rv(j), rv(connections[i*S7+j]%S7));
}
}
for(int i=0; i<TOT; i++) {
generateAlts(allh[i]);
allh[i]->emeraldval = allh[i]->alt->emeraldval;
allh[i]->zebraval = allh[i]->alt->zebraval;
allh[i]->fiftyval = allh[i]->alt->fiftyval;
allh[i]->distance = allh[i]->alt->distance;
/* for(int j=0; j<7; j++)
allh[i]->move[j]->alt = createStep(allh[i]->alt, j); */
}
celllister cl(gamestart(), 100, 100000000, NULL);
celllist = cl.lst;
}
heptagon *getOrigin() { return allh[0]; }
~hrmap_quotient() {
for(int i=0; i<size(allh); i++) {
clearHexes(allh[i]);
delete allh[i];
}
}
vector<cell*>& allcells() { return celllist; }
};
};
// --- general ---
2017-10-28 08:04:28 +00:00
cell *createMov(cell *c, int d);
void cwspin(cellwalker& cw, int d) {
cw.spin = (cw.spin+(MIRR(cw)?-d:d) + MODFIXER) % cw.c->type;
}
bool cwstepcreates(cellwalker& cw) {
return cw.c->mov[cw.spin] == NULL;
}
cell *cwpeek(cellwalker cw, int dir) {
2017-12-03 19:01:15 +00:00
return createMov(cw.c, (cw.spin+MODFIXER+(MIRR(cw)?-dir:dir)) % cw.c->type);
2017-10-28 08:04:28 +00:00
}
void cwmirrorat(cellwalker& cw, int d) {
cw.spin = (d+d - cw.spin + MODFIXER) % cw.c->type;
cw.mirrored = !cw.mirrored;
}
void cwstep(cellwalker& cw) {
createMov(cw.c, cw.spin);
int nspin = cw.c->spn(cw.spin);
if(cw.c->mirror(cw.spin)) cw.mirrored = !cw.mirrored;
cw.c = cw.c->mov[cw.spin];
cw.spin = nspin;
}
void cwrev(cellwalker& cw) {
cwspin(cw, cw.c->type/2 + ((cw.c->type&1)?hrand(2):0));
}
void cwrevstep(cellwalker& cw) {
cwrev(cw); cwstep(cw);
}
2015-08-08 13:57:52 +00:00
// very similar to createMove in heptagon.cpp
cell *createMov(cell *c, int d) {
if(d<0 || d>= c->type) {
printf("ERROR createmov\n");
}
2015-08-08 13:57:52 +00:00
if(euclid && !c->mov[d]) {
eucoord x, y;
decodeMaster(c->master, x, y);
for(int dx=-1; dx<=1; dx++)
for(int dy=-1; dy<=1; dy++)
euclideanAtCreate(x+dx, y+dy);
if(!c->mov[d]) { printf("fail!\n"); }
}
if(c->mov[d]) return c->mov[d];
else if(nontruncated) {
2016-08-26 09:58:03 +00:00
heptagon *h2 = createStep(c->master, d);
2017-03-23 10:53:57 +00:00
merge(c,d,h2->c7,c->master->spin(d),false);
2016-08-26 09:58:03 +00:00
}
2017-10-28 08:04:28 +00:00
else if(c == c->master->c7) {
cell *n = newCell(S6, c->master);
2015-08-08 13:57:52 +00:00
2017-03-23 10:53:57 +00:00
merge(c,d,n,0,false);
2015-08-08 13:57:52 +00:00
2017-03-23 10:53:57 +00:00
heptspin hs; hs.h = c->master; hs.spin = d; hs.mirrored = false;
2015-08-08 13:57:52 +00:00
int alt3 = c->type/2;
int alt4 = alt3+1;
2017-10-28 08:04:28 +00:00
/*
heptspin hs2 = hsstep(hsspin(hs, a3), -alt4);
2017-03-23 10:53:57 +00:00
merge(hs2.h->c7, hs2.spin, n, 2, hs2.mirrored);
2015-08-08 13:57:52 +00:00
heptspin hs3 = hsstep(hsspin(hs, a4), -alt3);
2017-10-28 08:04:28 +00:00
merge(hs3.h->c7, hs3.spin, n, S6-2, hs3.mirrored);
*/
for(int u=2; u<S6; u+=2) {
hs = hsstep(hsspin(hs, alt3), -alt4);
2017-10-28 08:04:28 +00:00
merge(hs.h->c7, hs.spin, n, u, hs.mirrored);
}
2015-08-08 13:57:52 +00:00
2017-03-23 10:53:57 +00:00
extern void verifycell(cell *c);
verifycell(n);
2015-08-08 13:57:52 +00:00
}
2017-10-28 08:04:28 +00:00
else {
bool mirr = c->mirror(d-1);
int di = fixrot(c->spn(d-1)-(mirr?-1:1));
cell *c2 = createMov(c->mov[d-1], di);
bool nmirr = mirr ^ c->mov[d-1]->mirror(di);
merge(c, d, c2, fix6(c->mov[d-1]->spn(di) - (nmirr?-1:1)), nmirr);
2015-08-08 13:57:52 +00:00
}
return c->mov[d];
}
2016-01-02 10:09:13 +00:00
cell *createMovR(cell *c, int d) {
2017-10-27 18:07:58 +00:00
d %= MODFIXER; d += MODFIXER; d %= c->type;
2016-01-02 10:09:13 +00:00
return createMov(c, d);
}
cell *getMovR(cell *c, int d) {
2017-10-27 18:07:58 +00:00
d %= MODFIXER; d += MODFIXER; d %= c->type;
2016-01-02 10:09:13 +00:00
return c->mov[d];
}
2015-08-08 13:57:52 +00:00
void eumerge(cell* c1, cell *c2, int s1, int s2) {
if(!c2) return;
2017-03-23 10:53:57 +00:00
c1->mov[s1] = c2; tsetspin(c1->spintable, s1, s2);
c2->mov[s2] = c1; tsetspin(c2->spintable, s2, s1);
2015-08-08 13:57:52 +00:00
}
// map<pair<eucoord, eucoord>, cell*> euclidean;
cell*& euclideanAt(eucoord x, eucoord y) {
2017-06-09 01:41:33 +00:00
if(torus) { printf("euclideanAt called\n"); exit(1); }
hrmap_euclidean* euc = dynamic_cast<hrmap_euclidean*> (currentmap);
return euc->at(x, y);
2015-08-08 13:57:52 +00:00
}
cell*& euclideanAtCreate(eucoord x, eucoord y) {
cell*& c = euclideanAt(x,y);
2015-08-08 13:57:52 +00:00
if(!c) {
2017-12-18 12:00:36 +00:00
c = newCell(a4 ? (nontruncated || ((x^y^1) & 1) ? 4 : 8) : 6, NULL);
2015-08-08 13:57:52 +00:00
c->master = encodeMaster(x,y);
euclideanAt(x,y) = c;
2017-12-18 12:00:36 +00:00
if(c->type == 4) {
int m = nontruncated ? 1 : 2;
eumerge(c, euclideanAt(x+1,y), 0, 2 * m);
eumerge(c, euclideanAt(x,y+1), 1, 3 * m);
eumerge(c, euclideanAt(x-1,y), 2, 0 * m);
eumerge(c, euclideanAt(x,y-1), 3, 1 * m);
}
else if(c->type == 8) {
eumerge(c, euclideanAt(x+1,y), 0, 2);
eumerge(c, euclideanAt(x+1,y+1), 1, 5);
eumerge(c, euclideanAt(x,y+1), 2, 3);
eumerge(c, euclideanAt(x-1,y+1), 3, 7);
eumerge(c, euclideanAt(x-1,y), 4, 0);
eumerge(c, euclideanAt(x-1,y-1), 5, 1);
eumerge(c, euclideanAt(x,y-1), 6, 1);
eumerge(c, euclideanAt(x+1,y-1), 7, 3);
}
else /* 6 */ {
eumerge(c, euclideanAt(x+1,y), 0, 3);
eumerge(c, euclideanAt(x,y+1), 1, 4);
eumerge(c, euclideanAt(x-1,y+1), 2, 5);
eumerge(c, euclideanAt(x-1,y), 3, 0);
eumerge(c, euclideanAt(x,y-1), 4, 1);
eumerge(c, euclideanAt(x+1,y-1), 5, 2);
}
2015-08-08 13:57:52 +00:00
}
return c;
}
// initializer (also inits origin from heptagon.cpp)
void initcells() {
2016-08-26 09:58:03 +00:00
DEBB(DF_INIT, (debugfile,"initcells\n"));
2017-03-23 10:53:57 +00:00
2017-06-09 01:41:33 +00:00
if(torus) currentmap = new hrmap_torus;
else if(euclid) currentmap = new hrmap_euclidean;
else if(sphere) currentmap = new hrmap_spherical;
else if(quotient) currentmap = new quotientspace::hrmap_quotient;
else currentmap = new hrmap_hyperbolic;
2015-08-08 13:57:52 +00:00
allmaps.push_back(currentmap);
2017-10-27 18:07:58 +00:00
windmap::create();
// origin->emeraldval =
2015-08-08 13:57:52 +00:00
}
void clearcell(cell *c) {
if(!c) return;
DEBMEM ( printf("c%d %p\n", c->type, c); )
for(int t=0; t<c->type; t++) if(c->mov[t]) {
2017-03-23 10:53:57 +00:00
DEBMEM ( printf("mov %p [%p] S%d\n", c->mov[t], c->mov[t]->mov[c->spn(t)], c->spn(t)); )
if(c->mov[t]->mov[c->spn(t)] != NULL &&
c->mov[t]->mov[c->spn(t)] != c) {
printf("type = %d %d -> %d\n", c->type, t, c->spn(t));
2015-08-08 13:57:52 +00:00
printf("cell error\n");
exit(1);
}
2017-03-23 10:53:57 +00:00
c->mov[t]->mov[c->spn(t)] = NULL;
2015-08-08 13:57:52 +00:00
}
DEBMEM ( printf("DEL %p\n", c); )
delete c;
}
heptagon deletion_marker;
2017-03-23 10:53:57 +00:00
void clearHexes(heptagon *at) {
if(at->c7) {
if(!nontruncated) for(int i=0; i<7; i++)
2017-03-23 10:53:57 +00:00
clearcell(at->c7->mov[i]);
clearcell(at->c7);
}
}
2015-08-08 13:57:52 +00:00
void clearfrom(heptagon *at) {
queue<heptagon*> q;
q.push(at);
at->alt = &deletion_marker;
//int maxq = 0;
while(!q.empty()) {
at = q.front();
// if(q.size() > maxq) maxq = q.size();
q.pop();
DEBMEM ( printf("from %p\n", at); )
for(int i=0; i<7; i++) if(at->move[i]) {
if(at->move[i]->alt != &deletion_marker)
q.push(at->move[i]);
at->move[i]->alt = &deletion_marker;
2017-03-23 10:53:57 +00:00
DEBMEM ( printf("!mov %p [%p]\n", at->move[i], at->move[i]->move[at->spin(i)]); )
if(at->move[i]->move[at->spin(i)] != NULL &&
at->move[i]->move[at->spin(i)] != at) {
2015-08-08 13:57:52 +00:00
printf("hept error\n");
exit(1);
}
2017-03-23 10:53:57 +00:00
at->move[i]->move[at->spin(i)] = NULL;
2015-08-08 13:57:52 +00:00
at->move[i] = NULL;
}
2017-03-23 10:53:57 +00:00
clearHexes(at);
delete at;
2015-08-08 13:57:52 +00:00
}
//printf("maxq = %d\n", maxq);
}
void verifycell(cell *c) {
int t = c->type;
for(int i=0; i<t; i++) {
cell *c2 = c->mov[i];
if(c2) {
if(!euclid && !nontruncated && c == c->master->c7) verifycell(c2);
2017-03-23 10:53:57 +00:00
if(c2->mov[c->spn(i)] && c2->mov[c->spn(i)] != c) {
printf("cell error %p:%d [%d] %p:%d [%d]\n", c, i, c->type, c2, c->spn(i), c2->type);
exit(1);
}
2015-08-08 13:57:52 +00:00
}
}
}
void verifycells(heptagon *at) {
for(int i=0; i<S7; i++) if(at->move[i] && at->move[i]->move[at->spin(i)] && at->move[i]->move[at->spin(i)] != at) {
2017-03-23 10:53:57 +00:00
printf("hexmix error %p [%d s=%d] %p %p\n", at, i, at->spin(i), at->move[i], at->move[i]->move[at->spin(i)]);
2015-08-08 13:57:52 +00:00
}
if(!sphere && !quotient)
2017-10-27 18:07:58 +00:00
for(int i=0; i<S7; i++) if(at->move[i] && at->spin(i) == 0 && at->s != hsOrigin)
verifycells(at->move[i]);
2015-08-08 13:57:52 +00:00
verifycell(at->c7);
}
int eudist(short sx, short sy) {
int z0 = abs(sx);
int z1 = abs(sy);
2017-12-18 12:00:36 +00:00
if(a4) return z0 + z1;
2015-08-08 13:57:52 +00:00
int z2 = abs(sx+sy);
return max(max(z0,z1), z2);
}
2017-10-28 23:57:34 +00:00
int compdist(int dx[]) {
int mi = dx[0];
for(int u=0; u<S3; u++) mi = min(mi, dx[u]);
for(int u=0; u<S3; u++)
if(dx[u] > mi+2)
return -1; // { printf("cycle error!\n"); exit(1); }
for(int u=0; u<S3; u++)
if(dx[u] == mi+2)
return mi+1;
int cnt = 0;
for(int u=0; u<S3; u++)
if(dx[u] == mi) cnt++;
if(cnt < 2)
2017-03-23 10:53:57 +00:00
return mi+1;
return mi;
}
2015-08-08 13:57:52 +00:00
int celldist(cell *c) {
if(euclid) {
2017-06-09 01:41:33 +00:00
if(torus)
return torusmap()->dists[decodeId(c->master)];
2015-08-08 13:57:52 +00:00
eucoord x, y;
decodeMaster(c->master, x, y);
return eudist(x, y);
}
if(sphere) return celldistance(c, currentmap->gamestart());
2017-10-28 23:57:34 +00:00
if(ctof(c)) return c->master->distance;
int dx[MAX_S3];
for(int u=0; u<S3; u++)
2015-08-08 13:57:52 +00:00
dx[u] = createMov(c, u+u)->master->distance;
2017-03-23 10:53:57 +00:00
return compdist(dx);
2015-08-08 13:57:52 +00:00
}
#define ALTDIST_BOUNDARY 99999
#define ALTDIST_UNKNOWN 99998
2016-01-02 10:09:13 +00:00
#define ALTDIST_ERROR 90000
2015-08-08 13:57:52 +00:00
// defined in 'game'
int euclidAlt(short x, short y);
int celldistAlt(cell *c) {
if(euclid) {
2017-06-09 01:41:33 +00:00
if(torus) return celldist(c);
2015-08-08 13:57:52 +00:00
eucoord x, y;
decodeMaster(c->master, x, y);
return euclidAlt(x, y);
}
2017-10-30 21:47:07 +00:00
if(sphere || quotient) {
return celldist(c) - 3;
}
2017-03-23 10:53:57 +00:00
if(!c->master->alt) return 0;
2017-10-28 23:57:34 +00:00
if(ctof(c)) return c->master->alt->distance;
2017-10-30 21:47:07 +00:00
int dx[MAX_S3]; dx[0] = 0;
2017-10-28 23:57:34 +00:00
for(int u=0; u<S3; u++) if(createMov(c, u+u)->master->alt == NULL)
2015-08-08 13:57:52 +00:00
return ALTDIST_UNKNOWN;
2017-10-28 23:57:34 +00:00
for(int u=0; u<S3; u++)
2015-08-08 13:57:52 +00:00
dx[u] = createMov(c, u+u)->master->alt->distance;
2017-10-28 23:57:34 +00:00
// return compdist(dx); -> not OK because of boundary conditions
int mi = dx[0];
for(int i=1; i<S3; i++) mi = min(mi, dx[i]);
for(int i=0; i<S3; i++) if(dx[i] > mi+2)
2015-08-08 13:57:52 +00:00
return ALTDIST_BOUNDARY; // { printf("cycle error!\n"); exit(1); }
2017-10-28 23:57:34 +00:00
for(int i=0; i<S3; i++) if(dx[i] == mi+2)
2015-08-08 13:57:52 +00:00
return mi+1;
return mi;
}
2016-01-02 10:09:13 +00:00
int dirfromto(cell *cfrom, cell *cto) {
for(int i=0; i<cfrom->type; i++) if(cfrom->mov[i] == cto) return i;
return -1;
}
2016-08-26 09:58:03 +00:00
#define RPV_MODULO 5
#define RPV_RAND 0
#define RPV_ZEBRA 1
#define RPV_EMERALD 2
#define RPV_PALACE 3
#define RPV_CYCLE 4
int getCdata(cell *c, int j);
// x mod 5 = pattern type
// x mod (powers of 2) = pattern type specific
// (x/5) mod 15 = picture for drawing floors
// x mod 7 = chance of pattern-specific pic
// whole = randomization
2016-01-02 10:09:13 +00:00
bool randpattern(cell *c, int rval) {
int i, sw=0;
2016-08-26 09:58:03 +00:00
switch(rval%5) {
2016-01-02 10:09:13 +00:00
case 0:
2016-08-26 09:58:03 +00:00
if(rval&1) {
return hrandpos() < rval;
}
else {
int cd = getCdata(c, 0);
return !((cd/(((rval/2)&15)+1))&1);
}
2016-01-02 10:09:13 +00:00
case 1:
i = zebra40(c);
if(i&1) { if(rval&4) sw^=1; i &= ~1; }
if(i&2) { if(rval&8) sw^=1; i &= ~2; }
i >>= 2;
i--; i /= 3;
if(rval & (16<<i)) sw^=1;
return sw;
case 2:
i = emeraldval(c);
if(i&1) { if(rval&4) sw^=1; i &= ~1; }
if(i&2) { if(rval&8) sw^=1; i &= ~2; }
2016-08-26 09:58:03 +00:00
i >>= 2; i--;
2016-01-02 10:09:13 +00:00
if(rval & (16<<i)) sw^=1;
return sw;
case 3:
if(polara50(c)) { if(rval&4) sw^=1; }
if(polarb50(c)) { if(rval&8) sw^=1; }
2016-08-26 09:58:03 +00:00
i = fiftyval049(c); i += 6; i /= 7;
2016-01-02 10:09:13 +00:00
if(rval & (16<<i)) sw^=1;
return sw;
2016-08-26 09:58:03 +00:00
case 4:
i = (rval&3);
if(i == 1 && (celldist(c)&1)) sw ^= 1;
if(i == 2 && (celldist(c)&2)) sw ^= 1;
if(i == 3 && ((celldist(c)/3)&1)) sw ^= 1;
if(rval & (4<<towerval(c, celldist))) sw ^= 1;
return sw;
2016-01-02 10:09:13 +00:00
}
return 0;
}
2016-08-26 09:58:03 +00:00
extern int randompattern[landtypes];
string describeRPM(eLand l) {
int rval = randompattern[l];
switch(rval%5) {
case 0:
if(rval&1)
return "R:"+its(rval/(HRANDMAX/100))+"%";
else
return "Landscape/"+its(((rval/2)&15)+1);
case 1:
return "Z/"+its((rval>>2)&3)+"/"+its((rval>>4)&15);
case 2:
return "E/"+its((rval>>2)&3)+"/"+its((rval>>4)&2047);
case 3:
return "P/"+its((rval>>2)&3)+"/"+its((rval>>4)&255);
case 4:
return "C/"+its(rval&3)+"/"+its((rval>>2)&65535);
}
return "?";
}
2016-01-02 10:09:13 +00:00
int randpatternCode(cell *c, int rval) {
2016-08-26 09:58:03 +00:00
switch(rval % RPV_MODULO) {
2016-01-02 10:09:13 +00:00
case 1:
return zebra40(c);
case 2:
return emeraldval(c);
case 3:
return fiftyval049(c) + (polara50(c)?50:0) + (polarb50(c)?1000:0);
2016-08-26 09:58:03 +00:00
case 4:
return towerval(c, celldist) * 6 + celldist(c) % 6;
2016-01-02 10:09:13 +00:00
}
return 0;
}
#define RANDITER 31
char rpm_memoize[3][256][RANDITER+1];
void clearMemoRPM() {
for(int a=0; a<3; a++) for(int b=0; b<256; b++) for(int i=0; i<RANDITER+1; i++)
rpm_memoize[a][b][i] = 2;
}
bool randpatternMajority(cell *c, int ival, int iterations) {
int rval = 0;
if(ival == 0) rval = randompattern[laCaves];
if(ival == 1) rval = randompattern[laLivefjord];
if(ival == 2) rval = randompattern[laEmerald];
2016-08-26 09:58:03 +00:00
if(rval%RPV_MODULO == RPV_RAND) return randpattern(c, rval);
2016-01-02 10:09:13 +00:00
int code = randpatternCode(c, rval);
char& memo(rpm_memoize[ival][code][iterations]);
if(memo < 2) return memo;
int z = 0;
if(iterations) for(int i=0; i<c->type; i++) {
if(randpatternMajority(createMov(c,i), ival, iterations-1))
z++;
else
z--;
}
if(z!=0) memo = (z>0);
else memo = randpattern(c, rval);
// printf("%p] rval = %X code = %d iterations = %d result = %d\n", c, rval, code, iterations, memo);
return memo;
}
2016-08-26 09:58:03 +00:00
map<heptagon*, int> spins;
#define RVAL_MASK 0x10000000
#define DATA_MASK 0x20000000
cdata orig_cdata;
void affect(cdata& d, short rv, signed char signum) {
if(rv&1) d.val[0]+=signum; else d.val[0]-=signum;
if(rv&2) d.val[1]+=signum; else d.val[1]-=signum;
if(rv&4) d.val[2]+=signum; else d.val[2]-=signum;
if(rv&8) d.val[3]+=signum; else d.val[3]-=signum;
int id = (rv>>4) & 63;
if(id < 32)
d.bits ^= (1 << id);
}
void setHeptagonRval(heptagon *h) {
if(!(h->rval0 || h->rval1)) {
h->rval0 = hrand(0x10000);
h->rval1 = hrand(0x10000);
}
}
cdata *getHeptagonCdata(heptagon *h) {
if(h->cdata) return h->cdata;
if(sphere || quotient) h = currentmap->gamestart()->master;
2017-03-23 10:53:57 +00:00
if(h == currentmap->gamestart()->master) {
2016-08-26 09:58:03 +00:00
return h->cdata = new cdata(orig_cdata);
}
cdata mydata = *getHeptagonCdata(h->move[0]);
for(int di=3; di<5; di++) {
heptspin hs; hs.h = h; hs.spin = di;
int signum = +1;
while(true) {
heptspin hstab[15];
hstab[7] = hs;
for(int i=8; i<12; i++) {
hstab[i] = hsspin(hstab[i-1], (i&1) ? 4 : 3);
hstab[i] = hsstep(hstab[i], 0);
hstab[i] = hsspin(hstab[i], (i&1) ? 3 : 4);
}
for(int i=6; i>=3; i--) {
hstab[i] = hsspin(hstab[i+1], (i&1) ? 3 : 4);
hstab[i] = hsstep(hstab[i], 0);
hstab[i] = hsspin(hstab[i], (i&1) ? 4 : 3);
}
if(hstab[3].h->distance < hstab[7].h->distance) {
hs = hstab[3]; continue;
}
if(hstab[11].h->distance < hstab[7].h->distance) {
hs = hstab[11]; continue;
}
int jj = 7;
for(int k=3; k<12; k++) if(hstab[k].h->distance < hstab[jj].h->distance) jj = k;
int ties = 0, tiespos = 0;
for(int k=3; k<12; k++) if(hstab[k].h->distance == hstab[jj].h->distance)
ties++, tiespos += (k-jj);
// printf("ties=%d tiespos=%d jj=%d\n", ties, tiespos, jj);
if(ties == 2) jj += tiespos/2;
if(jj&1) signum = -1;
hs = hstab[jj];
break;
}
hs = hsstep(hsspin(hs, 3), 0);
setHeptagonRval(hs.h);
affect(mydata, hs.spin ? hs.h->rval0 : hs.h->rval1, signum);
/* if(!(spins[hs.h] & hs.spin)) {
spins[hs.h] |= (1<<hs.spin);
int t = 0;
for(int k=0; k<7; k++) if(spins[hs.h] & (1<<k)) t++;
static bool wast[256];
if(!wast[spins[hs.h]]) {
printf("%p %4x\n", hs.h, spins[hs.h]);
wast[spins[hs.h]] = true;
}
} */
}
return h->cdata = new cdata(mydata);
}
cdata *getEuclidCdata(heptagon *h) {
2017-06-09 01:41:33 +00:00
if(torus) {
static cdata xx;
return &xx;
}
2016-08-26 09:58:03 +00:00
eucoord x, y;
hrmap_euclidean* euc = dynamic_cast<hrmap_euclidean*> (currentmap);
if(euc->eucdata.count(h)) return &(euc->eucdata[h]);
2017-06-09 01:41:33 +00:00
2016-08-26 09:58:03 +00:00
decodeMaster(h, x, y);
if(x == 0 && y == 0) {
cdata xx;
for(int i=0; i<4; i++) xx.val[i] = 0;
xx.bits = 0;
return &(euc->eucdata[h] = xx);
2016-08-26 09:58:03 +00:00
}
int ord = 1, bid = 0;
while(!((x|y)&ord)) ord <<= 1, bid++;
for(int k=0; k<3; k++) {
eucoord x1 = x + (k<2 ? ord : 0);
eucoord y1 = y - (k>0 ? ord : 0);
if((x1&ord) || (y1&ord)) continue;
eucoord x2 = x - (k<2 ? ord : 0);
eucoord y2 = y + (k>0 ? ord : 0);
cdata *d1 = getEuclidCdata(encodeMaster(x1,y1));
cdata *d2 = getEuclidCdata(encodeMaster(x2,y2));
cdata xx;
double disp = pow(2, bid/2.) * 6;
for(int i=0; i<4; i++) {
double dv = (d1->val[i] + d2->val[i])/2 + (hrand(1000) - hrand(1000))/1000. * disp;
xx.val[i] = floor(dv);
if(hrand(1000) / 1000. < dv - floor(dv)) xx.val[i]++;
}
xx.bits = 0;
for(int b=0; b<32; b++) {
bool gbit = ((hrand(2)?d1:d2)->bits >> b) & 1;
int flipchance = (1<<bid);
if(flipchance > 512) flipchance = 512;
if(hrand(1024) < flipchance) gbit = !gbit;
if(gbit) xx.bits |= (1<<b);
}
return &(euc->eucdata[h] = xx);
2016-08-26 09:58:03 +00:00
}
// impossible!
return NULL;
}
int getCdata(cell *c, int j) {
if(euclid) return getEuclidCdata(c->master)->val[j];
else if(geometry) return 0;
2017-10-27 18:07:58 +00:00
else if(c->type != 6) return getHeptagonCdata(c->master)->val[j]*3;
2016-08-26 09:58:03 +00:00
else {
int jj = 0;
for(int k=0; k<6; k++) if(c->mov[k] && c->mov[k]->type == 7)
jj += getHeptagonCdata(c->mov[k]->master)->val[j];
return jj;
}
}
int getBits(cell *c) {
if(euclid) return getEuclidCdata(c->master)->bits;
else if(geometry) return 0;
2017-10-27 18:07:58 +00:00
else if(c->type != 6) return getHeptagonCdata(c->master)->bits;
2016-08-26 09:58:03 +00:00
else {
int b0 = getHeptagonCdata(createMov(c, 0)->master)->bits;
int b1 = getHeptagonCdata(createMov(c, 2)->master)->bits;
int b2 = getHeptagonCdata(createMov(c, 4)->master)->bits;
return (b0 & b1) | (b1 & b2) | (b2 & b0);
}
}
2017-03-23 10:53:57 +00:00
cell *heptatdir(cell *c, int d) {
if(d&1) {
cell *c2 = createMov(c, d);
int s = c->spin(d);
s += 3; s %= 6;
return createMov(c2, s);
}
else return createMov(c, d);
}
2016-08-26 09:58:03 +00:00
int celldistance(cell *c1, cell *c2) {
int d = 0;
2017-03-23 10:53:57 +00:00
if(euclid) {
2017-06-09 01:41:33 +00:00
if(torus)
return torusmap()->dists[toridMod(decodeId(c1->master)-decodeId(c2->master))];
2017-03-23 10:53:57 +00:00
eucoord x1, y1, x2, y2;
decodeMaster(c1->master, x1, y1);
decodeMaster(c2->master, x2, y2);
return eudist(x1-x2, y1-y2);
}
if(sphere || quotient == 1) {
celllister cl(c1, 64, 1000, c2);
2017-07-06 13:23:33 +00:00
for(int i=0; i<size(cl.lst); i++)
if(cl.lst[i] == c2) return cl.dists[i];
2017-03-23 10:53:57 +00:00
}
if(quotient == 2)
return currfp.getdist(fieldpattern::fieldval(c1), fieldpattern::fieldval(c2));
2017-03-23 10:53:57 +00:00
2017-04-08 15:18:29 +00:00
int d1 = celldist(c1), d2 = celldist(c2);
2016-08-26 09:58:03 +00:00
cell *cl1=c1, *cr1=c1, *cl2=c2, *cr2=c2;
while(true) {
2017-10-30 21:47:07 +00:00
if(weirdhyperbolic) {
if(cl1 == cl2) return d;
if(cl1 == cr2) return d;
if(cr1 == cl2) return d;
if(cr1 == cr2) return d;
if(isNeighbor(cl1, cl2)) return d+1;
if(isNeighbor(cl1, cr2)) return d+1;
if(isNeighbor(cr1, cl2)) return d+1;
if(isNeighbor(cr1, cr2)) return d+1;
}
2017-04-08 15:18:29 +00:00
if(d1 == d2) for(int u=0; u<2; u++) {
cell *ac0 = u ? cr1 : cr2, *ac = ac0;
cell *tgt = u ? cl2 : cl1;
cell *xtgt = u ? cr2 : cr1;
if(ac == tgt) return d;
ac = chosenDown(ac, 1, 1, celldist);
if(ac == tgt) return d+1;
if(ac == xtgt) return d;
ac = chosenDown(ac, 1, 1, celldist);
if(ac == tgt) return d+2;
if(!nontruncated) {
2017-04-08 15:18:29 +00:00
ac = chosenDown(ac, 1, 1, celldist);
if(ac == tgt) {
if(chosenDown(ac0, 1, 0, celldist) ==
chosenDown(tgt, -1, 0, celldist))
return d+2;
return d+3;
}
}
}
2017-10-30 21:47:07 +00:00
if(weirdhyperbolic) {
forCellEx(c, cl2) if(isNeighbor(c, cr1)) return d+2;
forCellEx(c, cl1) if(isNeighbor(c, cr2)) return d+2;
forCellEx(ca, cl2) forCellEx(cb, cr1) if(isNeighbor(ca, cb)) return d+3;
forCellEx(ca, cl1) forCellEx(cb, cr2) if(isNeighbor(ca, cb)) return d+3;
2016-08-26 09:58:03 +00:00
2017-10-30 21:47:07 +00:00
forCellEx(ca, cl2) forCellEx(cb, cr1) forCellEx(cc, cb) if(isNeighbor(ca, cc)) return d+4;
forCellEx(ca, cl1) forCellEx(cb, cr2) forCellEx(cc, cb) if(isNeighbor(ca, cc)) return d+4;
}
2016-08-26 09:58:03 +00:00
if(d1 >= d2) {
cl1 = chosenDown(cl1, -1, 0, celldist);
// cl1->item = eItem(rand() % 10);
cr1 = chosenDown(cr1, 1, 0, celldist);
// cr1->item = eItem(rand() % 10);
2017-04-08 15:18:29 +00:00
d++; d1--;
2016-08-26 09:58:03 +00:00
}
2017-04-08 15:18:29 +00:00
if(d1 < d2) {
2016-08-26 09:58:03 +00:00
cl2 = chosenDown(cl2, -1, 0, celldist);
// cl2->item = eItem(rand() % 10);
cr2 = chosenDown(cr2, 1, 0, celldist);
// cr2->item = eItem(rand() % 10);
2017-04-08 15:18:29 +00:00
d++; d2--;
2016-08-26 09:58:03 +00:00
}
}
}
2017-03-23 10:53:57 +00:00
void clearCellMemory() {
for(int i=0; i<size(allmaps); i++) delete allmaps[i];
allmaps.clear();
2017-03-23 10:53:57 +00:00
}
2017-07-10 18:47:38 +00:00
auto cellhooks = addHook(clearmemory, 500, clearCellMemory);
2017-03-23 10:53:57 +00:00