forked from zenorogue/hyperrogue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heptagon.cpp
222 lines (198 loc) · 6.25 KB
/
heptagon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Hyperbolic Rogue
// Copyright (C) 2011-2012 Zeno Rogue, see 'hyper.cpp' for details
// heptagon here refers to underlying heptagonal tesselation
// (which you can see by changing the conditions in graph.cpp)
#define MIRR(x) x.mirrored
// automaton state
enum hstate { hsOrigin, hsA, hsB, hsError, hsA0, hsA1, hsB0, hsB1, hsC };
int fixrot(int a) { return (a+490)% S7; }
int fix42(int a) { return (a+420)% S42; }
struct heptagon;
struct cell;
cell *newCell(int type, heptagon *master);
// spintable functions
int tspin(uint32_t& t, int d) {
return (t >> (d<<2)) & 7;
}
int tmirror(uint32_t& t, int d) {
return (t >> ((d<<2)+3)) & 1;
}
void tsetspin(uint32_t& t, int d, int spin) {
t &= ~(15 << (d<<2));
t |= spin << (d<<2);
}
struct heptagon {
// automaton state
hstate s : 6;
int dm4: 2;
// we are spin[i]-th neighbor of move[i]
uint32_t spintable;
int spin(int d) { return tspin(spintable, d); }
int mirror(int d) { return tmirror(spintable, d); }
void setspin(int d, int sp) { tsetspin(spintable, d, sp); }
// neighbors; move[0] always goes towards origin,
// and then we go clockwise
heptagon* move[7];
// distance from the origin
short distance;
// emerald/wineyard generator
short emeraldval;
// fifty generator
short fiftyval;
// zebra generator (1B actually)
short zebraval;
// field id
int fieldval;
// evolution data
short rval0, rval1;
struct cdata *cdata;
// central cell
cell *c7;
// associated generator of alternate structure, for Camelot and horocycles
heptagon *alt;
// functions
heptagon*& modmove(int i) { return move[fixrot(i)]; }
unsigned char gspin(int i) { return spin(fixrot(i)); }
};
// the automaton is used to generate each heptagon in an unique way
// (you can see the tree obtained by changing the conditions in graph.cpp)
// from the origin we can go further in any direction, and from other heptagons
// we can go in directions 3 and 4 (0 is back to origin, so 3 and 4 go forward),
// and sometimes in direction 5
hstate transition(hstate s, int dir) {
if(sphere) {
if(S7 == 4) {
if(s == hsOrigin) return dir == 0 ? hsB0 : hsB1;
}
if(S7 == 3) {
if(s == hsOrigin) return hsB1;
}
if(s == hsOrigin) return dir == 0 ? hsA0 : hsA1;
if(s == hsA0 && dir == 2) return hsB0;
if(s == hsA1 && dir == 2) return hsB1;
if(s == hsB0 && dir == S7-2) return hsC;
return hsError;
}
else {
if(s == hsOrigin) return hsA;
if(s == hsA && dir >= 3 && dir <= 4) return hsA;
if(s == hsA && dir == 5) return hsB;
if(s == hsB && dir == 4) return hsB;
if(s == hsB && dir == 3) return hsA;
}
return hsError;
}
// create h->move[d] if not created yet
heptagon *createStep(heptagon *h, int d);
// create a new heptagon
heptagon *buildHeptagon(heptagon *parent, int d, hstate s, int pard = 0) {
heptagon *h = new heptagon;
h->alt = NULL;
h->s = s;
for(int i=0; i<7; i++) h->move[i] = NULL;
h->spintable = 0;
h->move[pard] = parent; tsetspin(h->spintable, pard, d);
parent->move[d] = h; tsetspin(parent->spintable, d, pard);
if(parent->c7) {
h->c7 = newCell(7, h);
h->emeraldval = emerald_heptagon(parent->emeraldval, d);
h->zebraval = zebra_heptagon(parent->zebraval, d);
h->fieldval = fp43.connections[fieldpattern::btspin(parent->fieldval, d)];
h->rval0 = h->rval1 = 0; h->cdata = NULL;
if(parent->s == hsOrigin)
h->fiftyval = fiftytable[0][d];
else
h->fiftyval = nextfiftyval(parent->fiftyval, parent->move[0]->fiftyval, d);
}
else {
h->c7 = NULL;
h->emeraldval = 0;
h->fiftyval = 0;
}
//generateEmeraldval(parent);
//generateEmeraldval(h);
if(pard == 0) {
h->dm4 = parent->dm4+1;
if(purehepta) h->distance = parent->distance + 1;
else if(parent->s == hsOrigin) h->distance = 2;
else if(h->spin(0) == 5)
h->distance = parent->distance + 1;
else if(h->spin(0) == 4 && h->move[0]->s == hsB)
h->distance = createStep(h->move[0], (h->spin(0)+2)%7)->distance + 3;
else h->distance = parent->distance + 2;
}
else {
h->distance = parent->distance - (purehepta?1:2);
h->dm4 = parent->dm4-1;
}
return h;
}
void addSpin(heptagon *h, int d, heptagon *from, int rot, int spin) {
rot = fixrot(rot);
createStep(from, rot);
h->move[d] = from->move[rot];
h->setspin(d, fixrot(from->spin(rot) + spin));
h->move[d]->move[fixrot(from->spin(rot) + spin)] = h;
h->move[d]->setspin(fixrot(from->spin(rot) + spin), d);
//generateEmeraldval(h->move[d]); generateEmeraldval(h);
}
extern int hrand(int);
heptagon *createStep(heptagon *h, int d) {
d = fixrot(d);
if(!h->move[0] && h->s != hsOrigin) {
buildHeptagon(h, 0, hsA, 3 + hrand(2));
}
if(h->move[d]) return h->move[d];
if(h->s == hsOrigin) {
buildHeptagon(h, d, hsA);
}
else if(d == 1) {
addSpin(h, d, h->move[0], h->spin(0)-1, -1);
}
else if(d == 6) {
addSpin(h, d, h->move[0], h->spin(0)+1, +1);
}
else if(d == 2) {
createStep(h->move[0], h->spin(0)-1);
addSpin(h, d, h->move[0]->modmove(h->spin(0)-1), 5 + h->move[0]->gspin(h->spin(0)-1), -1);
}
else if(d == 5 && h->s == hsB) {
createStep(h->move[0], h->spin(0)+1);
addSpin(h, d, h->move[0]->modmove(h->spin(0)+1), 2 + h->move[0]->gspin(h->spin(0)+1), +1);
}
else
buildHeptagon(h, d, (d == 5 || (h->s == hsB && d == 4)) ? hsB : hsA);
return h->move[d];
}
// a structure used to walk on the heptagonal tesselation
// (remembers not only the heptagon, but also direction)
struct heptspin {
heptagon *h;
int spin;
bool mirrored;
heptspin() { mirrored = false; }
};
heptspin hsstep(const heptspin &hs, int spin) {
createStep(hs.h, hs.spin);
heptspin res;
res.h = hs.h->move[hs.spin];
res.mirrored = hs.mirrored ^ hs.h->mirror(hs.spin);
res.spin = fixrot(hs.h->spin(hs.spin) + (MIRR(res)?-spin:spin));
return res;
}
heptspin hsspin(const heptspin &hs, int val) {
heptspin res;
res.h = hs.h;
res.spin = fixrot(hs.spin + (MIRR(hs)?-val:val));
res.mirrored = hs.mirrored;
return res;
}
// display the coordinates of the heptagon
void backtrace(heptagon *pos) {
if(pos->s == hsOrigin) return;
backtrace(pos->move[0]);
printf(" %d", pos->spin(0));
}
void hsshow(const heptspin& t) {
printf("ORIGIN"); backtrace(t.h); printf(" (spin %d)\n", t.spin);
}