1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-12-26 18:10:35 +00:00
hyperrogue/cell.cpp

945 lines
25 KiB
C++
Raw Normal View History

2016-08-26 09:58:03 +00:00
// Hyperbolic Rogue -- cells
// Copyright (C) 2011-2018 Zeno Rogue, see 'hyper.cpp' for details
2015-08-08 13:57:52 +00:00
// cells the game is played on
namespace hr {
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 = tailored_alloc<cell> (type);
2015-08-08 13:57:52 +00:00
c->type = type;
c->master = master;
initcell(c);
return c;
}
struct cdata {
int val[4];
int bits;
};
// -- hrmap ---
hrmap *currentmap;
vector<hrmap*> allmaps;
hrmap *newAltMap(heptagon *o) { return new hrmap_hyperbolic(o); }
// --- hyperbolic geometry ---
hrmap_hyperbolic::hrmap_hyperbolic(heptagon *o) { origin = o; }
hrmap_hyperbolic::hrmap_hyperbolic() {
// printf("Creating hyperbolic map: %p\n", this);
origin = tailored_alloc<heptagon> (S7);
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;
h.alt = NULL;
h.distance = 0;
mvar = variation;
2019-02-17 17:28:20 +00:00
if(0);
#if CAP_BT
else if(binarytiling) {
2018-08-09 17:28:53 +00:00
#if DEBUG_BINARY_TILING
binary::xcode.clear();
binary::rxcode.clear();
binary::xcode[&h] = (1 << 16);
binary::rxcode[1<<16] = &h;
#endif
h.zebraval = 0, h.emeraldval = 0,
2019-03-06 15:31:10 +00:00
h.c7 = newCell(DIM == 3 ? S7 : 6, origin);
2018-08-09 17:28:53 +00:00
}
2019-02-17 17:28:20 +00:00
#endif
#if CAP_IRR
else if(IRREGULAR)
2018-07-16 18:05:23 +00:00
irr::link_start(origin);
2019-02-17 17:28:20 +00:00
#endif
2018-07-16 18:05:23 +00:00
else
h.c7 = newCell(S7, origin);
}
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(masterless && !c->move(d)) {
int id = decodeId(c->master);
2015-08-08 13:57:52 +00:00
for(int dx=-1; dx<=1; dx++)
for(int dy=-1; dy<=1; dy++)
euclideanAtCreate(id + pair_to_vec(dx, dy));
if(!c->move(d)) {
println(hlog, "id = ", id, " vec_to_pair(id) = ", vec_to_pair(id), ": failed to create move ", d, " in Euclidean\n");
exit(0);
}
2015-08-08 13:57:52 +00:00
}
if(c->move(d)) return c->move(d);
2019-02-17 17:28:20 +00:00
#if CAP_IRR
else if(IRREGULAR) {
2018-07-16 18:05:23 +00:00
irr::link_cell(c, d);
}
2019-02-17 17:28:20 +00:00
#endif
#if CAP_GP
else if(GOLDBERG) {
2018-04-09 15:40:12 +00:00
gp::extend_map(c, d);
if(!c->move(d)) {
2018-04-03 21:39:18 +00:00
printf("extend failed to create for %p/%d\n", c, d);
exit(1);
}
}
2019-02-17 17:28:20 +00:00
#endif
#if CAP_ARCM
2018-08-30 00:11:43 +00:00
else if(archimedean && PURE) {
if(arcm::id_of(c->master) < arcm::current.N * 2) {
2018-08-17 11:29:00 +00:00
heptspin hs = heptspin(c->master, d) + wstep + 2 + wstep + 1;
c->c.connect(d, hs.at->c7, hs.spin, hs.mirrored);
2018-08-17 11:29:00 +00:00
}
else c->c.connect(d, c, d, false);
2018-08-17 11:29:00 +00:00
}
2018-08-30 00:11:43 +00:00
else if(archimedean && DUAL) {
if(arcm::id_of(c->master) >= arcm::current.N * 2) {
heptagon *h2 = createStep(c->master, d*2);
int d1 = c->master->c.spin(d*2);
c->c.connect(d, h2->c7, d1/2, false);
}
else {
printf("bad connection\n");
c->c.connect(d,c,d,false);
}
}
2019-02-17 17:28:20 +00:00
#endif
2018-08-30 00:11:43 +00:00
else if(archimedean || PURE) {
2016-08-26 09:58:03 +00:00
heptagon *h2 = createStep(c->master, d);
c->c.connect(d, h2->c7,c->master->c.spin(d), c->master->c.mirror(d));
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
2018-05-01 17:34:09 +00:00
heptspin hs(c->master, d, 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
2018-05-01 17:34:09 +00:00
for(int u=0; u<S6; u+=2) {
if(hs.mirrored && geometry == gSmallElliptic) hs+=1;
hs.at->c7->c.connect(hs.spin, n, u, hs.mirrored);
2018-05-01 17:34:09 +00:00
if(hs.mirrored && geometry == gSmallElliptic) hs+=-1;
hs = hs + alt3 + wstep - alt4;
2017-10-28 08:04:28 +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 {
2018-05-01 17:34:09 +00:00
cellwalker cw(c, d, false);
cellwalker cw2 = cw - 1 + wstep - 1 + wstep - 1;
c->c.connect(d, cw2);
2015-08-08 13:57:52 +00:00
}
return c->move(d);
2015-08-08 13:57:52 +00:00
}
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;
return c->move(d);
2016-01-02 10:09:13 +00:00
}
void eumerge(cell* c1, int s1, cell *c2, int s2, bool mirror) {
2015-08-08 13:57:52 +00:00
if(!c2) return;
c1->move(s1) = c2; c1->c.setspin(s1, s2, mirror);
c2->move(s2) = c1; c2->c.setspin(s2, s1, mirror);
}
2015-08-08 13:57:52 +00:00
// map<pair<eucoord, eucoord>, cell*> euclidean;
euc_pointer euclideanAt(int vec) {
if(fulltorus) { printf("euclideanAt called\n"); exit(1); }
hrmap_euclidean* euc = dynamic_cast<hrmap_euclidean*> (currentmap);
return euc->at(vec);
2015-08-08 13:57:52 +00:00
}
euc_pointer euclideanAtCreate(int vec) {
euc_pointer ep = euclideanAt(vec);
cell*& c = *ep.first;
2015-08-08 13:57:52 +00:00
if(!c) {
2018-11-27 15:17:20 +00:00
if(euwrap) {
int x, y;
tie(x, y) = vec_to_pair(vec);
torusconfig::be_canonical(x, y);
vec = pair_to_vec(x, y);
}
c = newCell(8, encodeId(vec));
// euclideanAt(vec) = c;
2018-12-15 13:50:11 +00:00
build_euclidean_moves(c, vec, [c,vec] (int delta, int d, int d2) {
euc_pointer ep2 = euclideanAt(vec + delta);
cell* c2 = *ep2.first;
if(!c2) return;
2018-11-27 15:17:20 +00:00
// if(ep.second) d = c->c.fix(torusconfig::mobius_dir(c) - d);
if(ep2.second) d2 = c2->c.fix(torusconfig::mobius_dir(c2) - d2);
eumerge(c, d, c2, d2, ep2.second);
});
2015-08-08 13:57:52 +00:00
}
return ep;
2015-08-08 13:57:52 +00:00
}
2018-11-30 13:37:59 +00:00
hookset<hrmap*()> *hooks_newmap;
2015-08-08 13:57:52 +00:00
// 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
2018-11-30 13:37:59 +00:00
hrmap* res = callhandlers((hrmap*)nullptr, hooks_newmap);
if(res) currentmap = res;
2019-02-17 17:28:20 +00:00
#if CAP_CRYSTAL
else if(geometry == gCrystal) currentmap = crystal::new_map();
2019-02-17 17:28:20 +00:00
#endif
#if CAP_ARCM
2018-11-30 13:37:59 +00:00
else if(archimedean) currentmap = arcm::new_map();
2019-02-17 17:28:20 +00:00
#endif
else if(fulltorus) currentmap = new hrmap_torus;
else if(euclid && DIM == 3) currentmap = euclid3::new_map();
2017-06-09 01:41:33 +00:00
else if(euclid) currentmap = new hrmap_euclidean;
2019-03-02 23:43:31 +00:00
else if(DIM == 3 && !binarytiling) currentmap = reg3::new_map();
else if(sphere) currentmap = new hrmap_spherical;
else if(quotient) currentmap = new quotientspace::hrmap_quotient;
else if(binarytiling) currentmap = binary::new_map();
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
2019-02-17 17:33:15 +00:00
#if CAP_FIELD
windmap::create();
2019-02-17 17:33:15 +00:00
#endif
// 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->move(t)) {
DEBMEM ( printf("mov %p [%p] S%d\n", c->move(t), c->move(t)->move(c->c.spin(t)), c->c.spin(t)); )
if(c->move(t)->move(c->c.spin(t)) != NULL &&
c->move(t)->move(c->c.spin(t)) != c) {
printf("type = %d %d -> %d\n", c->type, t, c->c.spin(t));
2015-08-08 13:57:52 +00:00
printf("cell error\n");
exit(1);
}
c->move(t)->move(c->c.spin(t)) = NULL;
2015-08-08 13:57:52 +00:00
}
DEBMEM ( printf("DEL %p\n", c); )
tailored_delete(c);
2015-08-08 13:57:52 +00:00
}
heptagon deletion_marker;
2018-04-10 22:16:33 +00:00
template<class T> void subcell(cell *c, const T& t) {
if(GOLDBERG) {
forCellEx(c2, c) if(c2->move(0) == c && c2 != c2->master->c7) {
2018-04-10 22:16:33 +00:00
subcell(c2, t);
}
}
else if(BITRUNCATED && !archimedean && !binarytiling)
2018-04-10 22:16:33 +00:00
forCellEx(c2, c) t(c2);
t(c);
}
2017-03-23 10:53:57 +00:00
void clearHexes(heptagon *at) {
if(at->c7 && at->cdata) {
delete at->cdata;
at->cdata = NULL;
}
2019-02-17 17:28:20 +00:00
if(0);
#if CAP_IRR
else if(IRREGULAR) irr::clear_links(at);
#endif
2018-08-17 19:37:33 +00:00
else if(at->c7) subcell(at->c7, clearcell);
2017-03-23 10:53:57 +00:00
}
void unlink_cdata(heptagon *h) {
if(h->alt && h->c7) {
if(h->alt->cdata == (cdata*) h)
h->alt->cdata = NULL;
}
}
2015-08-08 13:57:52 +00:00
void clearfrom(heptagon *at) {
2019-03-06 15:36:10 +00:00
if(!at) return;
2015-08-08 13:57:52 +00:00
queue<heptagon*> q;
unlink_cdata(at);
2015-08-08 13:57:52 +00:00
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); )
if(!at->c7) {
heptagon *h = (heptagon*) at->cdata;
if(h) {
if(h->alt != at) printf("alt error :: h->alt = %p\n", h->alt);
cell *c = h->c7;
2018-04-10 22:16:33 +00:00
subcell(c, destroycellcontents);
h->alt = NULL;
at->cdata = NULL;
}
}
int edges = at->degree();
2019-03-09 00:18:47 +00:00
if(binarytiling && DIM == 2) edges = at->c7->type;
for(int i=0; i<edges; i++) if(at->move(i)) {
if(at->move(i)->alt != &deletion_marker)
q.push(at->move(i));
unlink_cdata(at->move(i));
at->move(i)->alt = &deletion_marker;
DEBMEM ( printf("!mov %p [%p]\n", at->move(i), at->move(i)->move[at->c.spin(i)]); )
if(at->move(i)->move(at->c.spin(i)) != NULL &&
at->move(i)->move(at->c.spin(i)) != at) {
2015-08-08 13:57:52 +00:00
printf("hept error\n");
exit(1);
}
at->move(i)->move(at->c.spin(i)) = NULL;
at->move(i) = NULL;
2015-08-08 13:57:52 +00:00
}
2017-03-23 10:53:57 +00:00
clearHexes(at);
tailored_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->move(i);
2015-08-08 13:57:52 +00:00
if(c2) {
if(!masterless && BITRUNCATED && c == c->master->c7) verifycell(c2);
if(c2->move(c->c.spin(i)) && c2->move(c->c.spin(i)) != c) {
printf("cell error %p:%d [%d] %p:%d [%d]\n", c, i, c->type, c2, c->c.spin(i), c2->type);
2017-03-23 10:53:57 +00:00
exit(1);
}
2015-08-08 13:57:52 +00:00
}
}
}
void verifycells(heptagon *at) {
if(GOLDBERG || IRREGULAR || archimedean) return;
for(int i=0; i<S7; i++) if(at->move(i) && at->move(i)->move(at->c.spin(i)) && at->move(i)->move(at->c.spin(i)) != at) {
printf("hexmix error %p [%d s=%d] %p %p\n", at, i, at->c.spin(i), at->move(i), at->move(i)->move(at->c.spin(i)));
2015-08-08 13:57:52 +00:00
}
if(!sphere && !quotient)
for(int i=0; i<S7; i++) if(at->move(i) && at->c.spin(i) == 0 && at->s != hsOrigin)
verifycells(at->move(i));
2015-08-08 13:57:52 +00:00
verifycell(at->c7);
}
int eudist(int sx, int sy) {
2015-08-08 13:57:52 +00:00
int z0 = abs(sx);
int z1 = abs(sy);
if(a4 && BITRUNCATED)
return (z0 == z1 && z0 > 0) ? z0+1: max(z0, z1);
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);
}
int eudist(int vec) {
auto p = vec_to_pair(vec);
return eudist(p.first, p.second);
}
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(fulltorus)
2018-08-17 11:29:00 +00:00
return torusmap()->dists[decodeId(c->master)];
2018-11-27 15:17:20 +00:00
if(euwrap)
return torusconfig::cyldist(decodeId(c->master), 0);
2018-08-17 11:29:00 +00:00
if(masterless)
2018-11-27 15:17:20 +00:00
return eudist(decodeId(c->master));
2019-02-27 14:50:26 +00:00
if(sphere || binarytiling || DIM == 3 || geometry == gCrystal) return celldistance(c, currentmap->gamestart());
2019-02-17 17:28:20 +00:00
#if CAP_IRR
if(IRREGULAR) return irr::celldist(c, false);
2019-02-17 17:28:20 +00:00
#endif
if(archimedean || ctof(c)) return c->master->distance;
2019-02-17 17:28:20 +00:00
#if CAP_GP
if(GOLDBERG) return gp::compute_dist(c, celldist);
2019-02-17 17:28:20 +00:00
#endif
2017-10-28 23:57:34 +00:00
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
int celldistAlt(cell *c) {
if(masterless) {
2018-11-27 15:17:20 +00:00
if(fulltorus) return celldist(c);
2018-12-04 18:13:26 +00:00
if(euwrap) return cylinder_alt(c);
int x, y;
tie(x,y) = vec_to_pair(decodeId(c->master));
2015-08-08 13:57:52 +00:00
return euclidAlt(x, y);
}
2019-02-17 17:28:20 +00:00
#if CAP_BT
if(binarytiling) return c->master->distance + (specialland == laCamelot && !tactic::on? 30 : 0);
2019-02-17 17:28:20 +00:00
#endif
#if CAP_CRYSTAL
if(geometry == gCrystal)
return crystal::dist_alt(c);
2019-02-17 17:28:20 +00:00
#endif
2017-10-30 21:47:07 +00:00
if(sphere || quotient) {
return celldist(c) - 3;
}
#if MAXMDIM == 4
2019-02-27 14:50:26 +00:00
if(euclid && DIM == 3) return euclid3::dist_alt(c);
#endif
if(hyperbolic && DIM == 3) return reg3::altdist(c->master);
2017-03-23 10:53:57 +00:00
if(!c->master->alt) return 0;
2019-02-17 17:28:20 +00:00
#if CAP_IRR
if(IRREGULAR) return irr::celldist(c, true);
2019-02-17 17:28:20 +00:00
#endif
2017-10-28 23:57:34 +00:00
if(ctof(c)) return c->master->alt->distance;
2019-02-17 17:28:20 +00:00
#if CAP_GP
if(GOLDBERG) return gp::compute_dist(c, celldistAlt);
2019-02-17 17:28:20 +00:00
#endif
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->move(i) == cto) return i;
2016-01-02 10:09:13 +00:00
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
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) {
h->cdata = new cdata(orig_cdata);
for(int& v: h->cdata->val) v = 0;
h->cdata->bits = reptilecheat ? (1 << 21) - 1 : 0;
if(yendor::on && specialland == laVariant) h->cdata->bits |= (1 << 8) | (1 << 9) | (1 << 12);
return h->cdata;
2016-08-26 09:58:03 +00:00
}
cdata mydata = *getHeptagonCdata(h->move(0));
2016-08-26 09:58:03 +00:00
for(int di=3; di<5; di++) {
heptspin hs(h, di, false);
2016-08-26 09:58:03 +00:00
int signum = +1;
while(true) {
heptspin hstab[15];
hstab[7] = hs;
for(int i=8; i<12; i++) {
2018-03-24 11:59:01 +00:00
hstab[i] = hstab[i-1];
hstab[i] += ((i&1) ? 4 : 3);
hstab[i] += wstep;
hstab[i] += ((i&1) ? 3 : 4);
2016-08-26 09:58:03 +00:00
}
for(int i=6; i>=3; i--) {
2018-03-24 11:59:01 +00:00
hstab[i] = hstab[i+1];
hstab[i] += ((i&1) ? 3 : 4);
hstab[i] += wstep;
hstab[i] += ((i&1) ? 4 : 3);
2016-08-26 09:58:03 +00:00
}
if(hstab[3].at->distance < hstab[7].at->distance) {
2016-08-26 09:58:03 +00:00
hs = hstab[3]; continue;
}
if(hstab[11].at->distance < hstab[7].at->distance) {
2016-08-26 09:58:03 +00:00
hs = hstab[11]; continue;
}
int jj = 7;
for(int k=3; k<12; k++) if(hstab[k].at->distance < hstab[jj].at->distance) jj = k;
2016-08-26 09:58:03 +00:00
int ties = 0, tiespos = 0;
for(int k=3; k<12; k++) if(hstab[k].at->distance == hstab[jj].at->distance)
2016-08-26 09:58:03 +00:00
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;
}
2018-03-24 11:59:01 +00:00
hs = hs + 3 + wstep;
setHeptagonRval(hs.at);
2016-08-26 09:58:03 +00:00
affect(mydata, hs.spin ? hs.at->rval0 : hs.at->rval1, signum);
2016-08-26 09:58:03 +00:00
/* if(!(spins[hs.at] & hs.spin)) {
spins[hs.at] |= (1<<hs.spin);
2016-08-26 09:58:03 +00:00
int t = 0;
for(int k=0; k<7; k++) if(spins[hs.at] & (1<<k)) t++;
2016-08-26 09:58:03 +00:00
static bool wast[256];
if(!wast[spins[hs.at]]) {
printf("%p %4x\n", hs.at, spins[hs.at]);
wast[spins[hs.at]] = true;
2016-08-26 09:58:03 +00:00
}
} */
}
return h->cdata = new cdata(mydata);
}
cdata *getEuclidCdata(int h) {
2017-06-09 01:41:33 +00:00
if(euwrap) { // fix cylinder?
2017-06-09 01:41:33 +00:00
static cdata xx;
return &xx;
}
int 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
tie(x,y) = vec_to_pair(h);
2016-08-26 09:58:03 +00:00
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++) {
int x1 = x + (k<2 ? ord : 0);
int y1 = y - (k>0 ? ord : 0);
2016-08-26 09:58:03 +00:00
if((x1&ord) || (y1&ord)) continue;
int x2 = x - (k<2 ? ord : 0);
int y2 = y + (k>0 ? ord : 0);
2016-08-26 09:58:03 +00:00
cdata *d1 = getEuclidCdata(pair_to_vec(x1,y1));
cdata *d2 = getEuclidCdata(pair_to_vec(x2,y2));
2016-08-26 09:58:03 +00:00
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(masterless) return getEuclidCdata(decodeId(c->master))->val[j];
else if(geometry) return 0;
else if(ctof(c)) return getHeptagonCdata(c->master)->val[j]*3;
2016-08-26 09:58:03 +00:00
else {
int jj = 0;
auto ar = gp::get_masters(c);
for(int k=0; k<3; k++)
2018-07-16 18:05:23 +00:00
jj += getHeptagonCdata(ar[k])->val[j];
2016-08-26 09:58:03 +00:00
return jj;
}
}
int getBits(cell *c) {
if(masterless) return getEuclidCdata(decodeId(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 {
auto ar = gp::get_masters(c);
2018-07-16 18:05:23 +00:00
int b0 = getHeptagonCdata(ar[0])->bits;
int b1 = getHeptagonCdata(ar[1])->bits;
int b2 = getHeptagonCdata(ar[2])->bits;
2016-08-26 09:58:03 +00:00
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->c.spin(d);
2017-03-23 10:53:57 +00:00
s += 3; s %= 6;
return createMov(c2, s);
}
else return createMov(c, d);
}
2018-04-06 21:28:58 +00:00
int heptdistance(heptagon *h1, heptagon *h2) {
// very rough distance
int d = 0;
2019-02-17 17:28:20 +00:00
#if CAP_CRYSTAL
if(geometry == gCrystal) return crystal::space_distance(h1->c7, h2->c7);
2019-02-17 17:28:20 +00:00
#endif
2018-04-06 21:28:58 +00:00
while(true) {
if(h1 == h2) return d;
for(int i=0; i<S7; i++) if(h1->move(i) == h2) return d + 1;
2018-04-06 21:28:58 +00:00
int d1 = h1->distance, d2 = h2->distance;
2018-08-09 17:28:53 +00:00
if(d1 >= d2) d++, h1 = createStep(h1, binarytiling ? 5 : 0);
if(d2 > d1) d++, h2 = createStep(h2, binarytiling ? 5 : 0);
2018-04-06 21:28:58 +00:00
}
}
int heptdistance(cell *c1, cell *c2) {
2019-02-17 17:28:20 +00:00
#if CAP_CRYSTAL
if(geometry == gCrystal) return crystal::space_distance(c1, c2);
2019-02-17 17:28:20 +00:00
#endif
2019-02-26 12:08:05 +00:00
if(!hyperbolic || quotient || DIM == 3) return celldistance(c1, c2);
2018-04-06 21:28:58 +00:00
else return heptdistance(c1->master, c2->master);
}
2018-04-10 05:59:48 +00:00
map<pair<cell*, cell*>, int> saved_distances;
2016-08-26 09:58:03 +00:00
int celldistance(cell *c1, cell *c2) {
2017-03-23 10:53:57 +00:00
if((masterless) && (euclid6 || (euclid4 && PURE))) {
if(!euwrap)
return eudist(decodeId(c1->master) - decodeId(c2->master)); // fix cylinder
else if(euwrap && torusconfig::torus_mode == 0)
return torusmap()->dists[torusconfig::vec_to_id(decodeId(c1->master)-decodeId(c2->master))];
2018-11-27 15:17:20 +00:00
else if(euwrap && !fulltorus)
return torusconfig::cyldist(decodeId(c1->master), decodeId(c2->master));
2017-03-23 10:53:57 +00:00
}
2019-02-17 17:33:15 +00:00
#if CAP_FIELD
if(geometry == gFieldQuotient && !GOLDBERG)
return currfp.getdist(fieldpattern::fieldval(c1), fieldpattern::fieldval(c2));
2019-02-17 17:33:15 +00:00
#endif
2018-11-30 14:26:50 +00:00
if(bounded) {
2018-04-10 05:59:48 +00:00
if(saved_distances.count(make_pair(c1,c2)))
return saved_distances[make_pair(c1,c2)];
celllister cl(c1, 100, 100000000, NULL);
2018-06-22 12:47:24 +00:00
for(int i=0; i<isize(cl.lst); i++)
2018-04-10 05:59:48 +00:00
saved_distances[make_pair(c1, cl.lst[i])] = cl.dists[i];
if(saved_distances.count(make_pair(c1,c2)))
return saved_distances[make_pair(c1,c2)];
return 64;
}
2019-02-17 17:28:20 +00:00
#if CAP_CRYSTAL
if(geometry == gCrystal) return crystal::precise_distance(c1, c2);
2019-02-17 17:28:20 +00:00
#endif
2018-11-30 14:26:50 +00:00
if(masterless || archimedean || quotient) {
2018-04-10 05:59:48 +00:00
if(saved_distances.count(make_pair(c1,c2)))
return saved_distances[make_pair(c1,c2)];
2018-06-21 06:38:11 +00:00
2018-06-22 12:47:24 +00:00
if(isize(saved_distances) > 1000000) saved_distances.clear();
2017-03-23 10:53:57 +00:00
celllister cl(c1, 64, 1000, c2);
2018-06-22 12:47:24 +00:00
for(int i=0; i<isize(cl.lst); i++)
saved_distances[make_pair(c1, cl.lst[i])] = cl.dists[i];
if(saved_distances.count(make_pair(c1,c2)))
return saved_distances[make_pair(c1,c2)];
2018-04-10 05:59:48 +00:00
2018-04-03 21:39:18 +00:00
return 64;
2017-03-23 10:53:57 +00:00
}
2019-02-21 17:47:32 +00:00
#if CAP_BT && MAXMDIM >= 4
if(binarytiling && DIM == 3)
return binary::celldistance3(c1, c2);
#endif
2019-02-24 21:12:32 +00:00
if(euclid && DIM == 3)
return euclid3::celldistance(c1, c2);
2019-03-02 23:43:31 +00:00
if(hyperbolic && DIM == 3) return reg3::celldistance(c1, c2);
2019-02-24 21:12:32 +00:00
return hyperbolic_celldistance(c1, c2);
2016-08-26 09:58:03 +00:00
}
2018-12-04 21:35:00 +00:00
vector<cell*> build_shortest_path(cell *c1, cell *c2) {
2019-02-17 17:28:20 +00:00
#if CAP_CRYSTAL
2018-12-04 21:35:00 +00:00
if(geometry == gCrystal) return crystal::build_shortest_path(c1, c2);
2019-02-17 17:28:20 +00:00
#endif
2018-12-04 21:35:00 +00:00
vector<cell*> p;
if(euclid) {
2018-12-04 21:35:00 +00:00
using namespace hyperpoint_vec;
p.push_back(c1);
hyperpoint h = tC0(calc_relative_matrix(c2, c1, C0)) - C0;
cell *x = c1;
hyperpoint h1 = C0;
int d = celldistance(c1, c2);
for(int i=0; i<=d * 10; i++) {
h1 += h / d / 10.;
virtualRebase(x, h1, true);
while(x != p.back()) {
forCellCM(c, p.back())
if(celldistance(c, x) < celldistance(p.back(), x)) {
p.push_back(c);
break;
}
}
2018-12-04 21:35:00 +00:00
}
if(isize(p) != d + 1)
println(hlog, "warning: path size ", isize(p), " should be ", d+1);
}
else if(c2 == currentmap->gamestart()) {
while(c1 != c2) {
p.push_back(c1);
forCellCM(c, c1) if(celldist(c) < celldist(c1)) { c1 = c; goto next1; }
println(hlog, "could not build_shortest_path"); exit(1);
next1: ;
}
p.push_back(c1);
}
else if(c1 == currentmap->gamestart()) {
p = build_shortest_path(c2, c1);
reverse(p.begin(), p.end());
}
else {
while(c1 != c2) {
p.push_back(c1);
forCellCM(c, c1) if(celldistance(c, c2) < celldistance(c1, c2)) { c1 = c; goto next; }
println(hlog, "could not build_shortest_path"); exit(1);
next: ;
}
p.push_back(c1);
}
return p;
}
2017-03-23 10:53:57 +00:00
void clearCellMemory() {
2018-06-22 12:47:24 +00:00
for(int i=0; i<isize(allmaps); i++)
if(allmaps[i])
delete allmaps[i];
allmaps.clear();
last_cleared = NULL;
2018-04-10 05:59:48 +00:00
saved_distances.clear();
pd_from = NULL;
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
}