1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/*** /
This file is part of Golly, a Game of Life Simulator.
Copyright (C) 2011 Andrew Trevorrow and Tomas Rokicki.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Web site: http://sourceforge.net/projects/golly
Authors: rokicki@gmail.com andrew@trevorrow.com
/ ***/
#include "lifealgo.h"
#include "string.h"
using namespace std ;
lifealgo::~lifealgo() {
poller = 0 ;
maxCellStates = 2 ;
}
int lifealgo::verbose ;
/*
* Right now, the base/expo should match the current increment.
* We do not check this.
*/
int lifealgo::startrecording(int basearg, int expoarg) {
if (timeline.framecount) {
// already have a timeline; skip to its end
gotoframe(timeline.framecount-1) ;
} else {
// use the current frame and increment to start a new timeline
void *now = getcurrentstate() ;
if (now == 0)
return 0 ;
timeline.base = basearg ;
timeline.expo = expoarg ;
timeline.frames.push_back(now) ;
timeline.framecount = 1 ;
timeline.end = timeline.start = generation ;
timeline.inc = increment ;
}
timeline.next = timeline.end ;
timeline.next += timeline.inc ;
timeline.recording = 1 ;
return timeline.framecount ;
}
pair<int, int> lifealgo::stoprecording() {
timeline.recording = 0 ;
timeline.next = 0 ;
return make_pair(timeline.base, timeline.expo) ;
}
void lifealgo::extendtimeline() {
if (timeline.recording && generation == timeline.next) {
void *now = getcurrentstate() ;
if (now && timeline.framecount < MAX_FRAME_COUNT) {
timeline.frames.push_back(now) ;
timeline.framecount++ ;
timeline.end = timeline.next ;
timeline.next += timeline.inc ;
}
}
}
/*
* Note that this *also* changes inc, so don't call unless this is
* what you want to do. It does not update or change the base or
* expo if the base != 2, so they can get out of sync.
*
* Currently this is only used by bgolly, and it will only work
* properly if the increment argument is a power of two.
*/
void lifealgo::pruneframes() {
if (timeline.framecount > 1) {
for (int i=2; i<timeline.framecount; i += 2)
timeline.frames[i >> 1] = timeline.frames[i] ;
timeline.framecount = (timeline.framecount + 1) >> 1 ;
timeline.frames.resize(timeline.framecount) ;
timeline.inc += timeline.inc ;
timeline.end = timeline.inc ;
timeline.end.mul_smallint(timeline.framecount-1) ;
timeline.end += timeline.start ;
timeline.next = timeline.end ;
timeline.next += timeline.inc ;
if (timeline.base == 2)
timeline.expo++ ;
}
}
int lifealgo::gotoframe(int i) {
if (i < 0 || i >= timeline.framecount)
return 0 ;
setcurrentstate(timeline.frames[i]) ;
// AKT: avoid mul_smallint(i) crashing with divide-by-zero if i is 0
if (i > 0) {
generation = timeline.inc ;
generation.mul_smallint(i) ;
} else {
generation = 0;
}
generation += timeline.start ;
return timeline.framecount ;
}
void lifealgo::destroytimeline() {
timeline.frames.clear() ;
timeline.recording = 0 ;
timeline.framecount = 0 ;
timeline.end = 0 ;
timeline.start = 0 ;
timeline.inc = 0 ;
timeline.next = 0 ;
}
// AKT: the next 2 routines provide support for a bounded universe
const char* lifealgo::setgridsize(const char* suffix) {
// parse a rule suffix like ":T100,200" and set the various grid parameters;
// note that we allow any legal partial suffix -- this lets people type a
// suffix into the Set Rule dialog without the algorithm changing to UNKNOWN
const char *p = suffix;
char topology = 0;
gridwd = gridht = 0;
hshift = vshift = 0;
htwist = vtwist = false;
boundedplane = false;
sphere = false;
p++;
if (*p == 0) return 0; // treat ":" like ":T0,0"
if (*p == 't' || *p == 'T') {
// torus or infinite tube
topology = 'T';
} else if (*p == 'p' || *p == 'P') {
boundedplane = true;
topology = 'P';
} else if (*p == 's' || *p == 'S') {
sphere = true;
topology = 'S';
} else if (*p == 'k' || *p == 'K') {
// Klein bottle (either htwist or vtwist should become true)
topology = 'K';
} else if (*p == 'c' || *p == 'C') {
// cross-surface
htwist = vtwist = true;
topology = 'C';
} else {
return "Unknown grid topology.";
}
p++;
if (*p == 0) return 0; // treat ":<char>" like ":T0,0"
while ('0' <= *p && *p <= '9') {
if (gridwd >= 200000000) {
gridwd = 2000000000; // keep width within editable limits
} else {
gridwd = 10 * gridwd + *p - '0';
}
p++;
}
if (*p == '*') {
if (topology != 'K') return "Only specify a twist for a Klein bottle.";
htwist = true;
p++;
}
if (*p == '+' || *p == '-') {
if (topology == 'P') return "Plane can't have a shift.";
if (topology == 'S') return "Sphere can't have a shift.";
if (topology == 'C') return "Cross-surface can't have a shift.";
if (topology == 'K' && !htwist) return "Shift must be on twisted edges.";
if (gridwd == 0) return "Can't shift infinite width.";
int sign = *p == '+' ? 1 : -1;
p++;
while ('0' <= *p && *p <= '9') {
hshift = 10 * hshift + *p - '0';
p++;
}
if (hshift >= (int)gridwd) hshift = hshift % (int)gridwd;
hshift *= sign;
}
if (*p == ',' && topology != 'S') {
p++;
} else if (*p) {
return "Unexpected stuff after grid width.";
}
// gridwd has been set
if ((topology == 'K' || topology == 'C' || topology == 'S') && gridwd == 0) {
return "Given topology can't have an infinite width.";
}
if (*p == 0) {
// grid height is not specified so set it to grid width;
// ie. treat ":T100" like ":T100,100";
// this also allows us to have ":S100" rather than ":S100,100"
gridht = gridwd;
} else {
while ('0' <= *p && *p <= '9') {
if (gridht >= 200000000) {
gridht = 2000000000; // keep height within editable limits
} else {
gridht = 10 * gridht + *p - '0';
}
p++;
}
if (*p == '*') {
if (topology != 'K') return "Only specify a twist for a Klein bottle.";
if (htwist) return "Klein bottle can't have both horizontal and vertical twists.";
vtwist = true;
p++;
}
if (*p == '+' || *p == '-') {
if (topology == 'P') return "Plane can't have a shift.";
if (topology == 'C') return "Cross-surface can't have a shift.";
if (topology == 'K' && !vtwist) return "Shift must be on twisted edges.";
if (hshift != 0) return "Can't have both horizontal and vertical shifts.";
if (gridht == 0) return "Can't shift infinite height.";
int sign = *p == '+' ? 1 : -1;
p++;
while ('0' <= *p && *p <= '9') {
vshift = 10 * vshift + *p - '0';
p++;
}
if (vshift >= (int)gridht) vshift = vshift % (int)gridht;
vshift *= sign;
}
if (*p) return "Unexpected stuff after grid height.";
}
// gridht has been set
if ((topology == 'K' || topology == 'C') && gridht == 0) {
return "Klein bottle or cross-surface can't have an infinite height.";
}
if (topology == 'K' && !(htwist || vtwist)) {
// treat ":K10,20" like ":K10,20*"
vtwist = true;
}
if ((hshift != 0 || vshift != 0) && (gridwd == 0 || gridht == 0)) {
return "Shifting is not allowed if either grid dimension is unbounded.";
}
// now ok to set grid edges
if (gridwd > 0) {
gridleft = -int(gridwd) / 2;
gridright = int(gridwd) - 1;
gridright += gridleft;
} else {
// play safe and set these to something
gridleft = bigint::zero;
gridright = bigint::zero;
}
if (gridht > 0) {
gridtop = -int(gridht) / 2;
gridbottom = int(gridht) - 1;
gridbottom += gridtop;
} else {
// play safe and set these to something
gridtop = bigint::zero;
gridbottom = bigint::zero;
}
return 0;
}
const char* lifealgo::canonicalsuffix() {
if (gridwd > 0 || gridht > 0) {
static char bounds[64];
if (boundedplane) {
sprintf(bounds, ":P%u,%u", gridwd, gridht);
} else if (sphere) {
// sphere requires a square grid (gridwd == gridht)
sprintf(bounds, ":S%u", gridwd);
} else if (htwist && vtwist) {
// cross-surface if both horizontal and vertical edges are twisted
sprintf(bounds, ":C%u,%u", gridwd, gridht);
} else if (htwist) {
// Klein bottle if only horizontal edges are twisted
if (hshift != 0 && (gridwd & 1) == 0) {
// twist and shift is only possible if gridwd is even and hshift is 1
sprintf(bounds, ":K%u*+1,%u", gridwd, gridht);
} else {
sprintf(bounds, ":K%u*,%u", gridwd, gridht);
}
} else if (vtwist) {
// Klein bottle if only vertical edges are twisted
if (vshift != 0 && (gridht & 1) == 0) {
// twist and shift is only possible if gridht is even and vshift is 1
sprintf(bounds, ":K%u,%u*+1", gridwd, gridht);
} else {
sprintf(bounds, ":K%u,%u*", gridwd, gridht);
}
} else if (hshift < 0) {
// torus with -ve horizontal shift
sprintf(bounds, ":T%u%d,%u", gridwd, hshift, gridht);
} else if (hshift > 0) {
// torus with +ve horizontal shift
sprintf(bounds, ":T%u+%d,%u", gridwd, hshift, gridht);
} else if (vshift < 0) {
// torus with -ve vertical shift
sprintf(bounds, ":T%u,%u%d", gridwd, gridht, vshift);
} else if (vshift > 0) {
// torus with +ve vertical shift
sprintf(bounds, ":T%u,%u+%d", gridwd, gridht, vshift);
} else {
// unshifted torus, or an infinite tube
sprintf(bounds, ":T%u,%u", gridwd, gridht);
}
return bounds;
} else {
// unbounded universe
return 0;
}
}
int staticAlgoInfo::nextAlgoId = 0 ;
staticAlgoInfo *staticAlgoInfo::head = 0 ;
staticAlgoInfo::staticAlgoInfo() {
id = nextAlgoId++ ;
next = head ;
head = this ;
// init default icon data
defxpm7x7 = NULL;
defxpm15x15 = NULL;
}
staticAlgoInfo *staticAlgoInfo::byName(const char *s) {
for (staticAlgoInfo *i=head; i; i=i->next)
if (strcmp(i->algoName, s) == 0)
return i ;
return 0 ;
}
int staticAlgoInfo::nameToIndex(const char *s) {
staticAlgoInfo *r = byName(s) ;
if (r == 0)
return -1 ;
return r->id ;
}
staticAlgoInfo &staticAlgoInfo::tick() {
return *(new staticAlgoInfo()) ;
}
|