Fix Simulation Reset not Replicating the Starting Pattern

Now keeps the original parameters for the first pattern of a grid stored and can replicate them reliably.
This commit is contained in:
Marty Oehme 2018-07-07 21:42:07 +02:00
parent bdc5dc1af1
commit 2d2365e606
1 changed files with 29 additions and 6 deletions

View File

@ -12,6 +12,12 @@ class Grid {
final List<List<Cell>> map;
bool _dirty = true;
int _startingSeed;
int _x;
int _y;
int _amount;
int _dispersal;
Pattern _pattern;
Grid(int w, int h)
: this.w = w,
@ -22,9 +28,21 @@ class Grid {
print("Grid creation finished");
}
void startingPattern(
{Pattern pattern, int x, int y, int amount, int dispersal}) {
math.Random rng = new math.Random();
void reset() {
map.setAll(0, _buildGrid(w, h));
if(_startingSeed != null) addPattern(pattern: _pattern, dispersal: _dispersal,amount: _amount,seed: _startingSeed, x: _x, y: _y);
_dirty = true;
}
void addPattern(
{Pattern pattern, int x, int y, int amount, int dispersal, int seed}) {
_startingSeed = seed ?? DateTime.now().millisecondsSinceEpoch;
math.Random rng = new math.Random(_startingSeed);
_x = x;
_y = y;
_amount = amount ?? rng.nextInt(20);
_dispersal = dispersal ?? 10;
_pattern = pattern;
int cx = x ?? rng.nextInt(w ~/ 3) + (w ~/ 3);
int cy = y ?? rng.nextInt(h ~/ 3) + (h ~/ 3);
switch (pattern) {
@ -54,9 +72,14 @@ class Grid {
setCellState(0 + cx, 2 + cy, true);
break;
default:
for (var i = 0; i < amount ?? rng.nextInt(20); i++) {
setCellState(cx + dispersal ?? rng.nextInt(10),
cy + dispersal ?? rng.nextInt(10), true);
int sanityCheck = 0;
for (var i = 0; i < (_amount); i++) {
sanityCheck++;
getCellState(cx, cy)
? i--
: setCellState(cx + rng.nextInt(_dispersal),
cy + rng.nextInt(_dispersal), true);
if (sanityCheck > 100 && sanityCheck > i*3) break;
}
break;
}