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:
parent
bdc5dc1af1
commit
2d2365e606
1 changed files with 29 additions and 6 deletions
|
@ -12,6 +12,12 @@ class Grid {
|
||||||
final List<List<Cell>> map;
|
final List<List<Cell>> map;
|
||||||
|
|
||||||
bool _dirty = true;
|
bool _dirty = true;
|
||||||
|
int _startingSeed;
|
||||||
|
int _x;
|
||||||
|
int _y;
|
||||||
|
int _amount;
|
||||||
|
int _dispersal;
|
||||||
|
Pattern _pattern;
|
||||||
|
|
||||||
Grid(int w, int h)
|
Grid(int w, int h)
|
||||||
: this.w = w,
|
: this.w = w,
|
||||||
|
@ -22,9 +28,21 @@ class Grid {
|
||||||
print("Grid creation finished");
|
print("Grid creation finished");
|
||||||
}
|
}
|
||||||
|
|
||||||
void startingPattern(
|
void reset() {
|
||||||
{Pattern pattern, int x, int y, int amount, int dispersal}) {
|
map.setAll(0, _buildGrid(w, h));
|
||||||
math.Random rng = new math.Random();
|
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 cx = x ?? rng.nextInt(w ~/ 3) + (w ~/ 3);
|
||||||
int cy = y ?? rng.nextInt(h ~/ 3) + (h ~/ 3);
|
int cy = y ?? rng.nextInt(h ~/ 3) + (h ~/ 3);
|
||||||
switch (pattern) {
|
switch (pattern) {
|
||||||
|
@ -54,9 +72,14 @@ class Grid {
|
||||||
setCellState(0 + cx, 2 + cy, true);
|
setCellState(0 + cx, 2 + cy, true);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
for (var i = 0; i < amount ?? rng.nextInt(20); i++) {
|
int sanityCheck = 0;
|
||||||
setCellState(cx + dispersal ?? rng.nextInt(10),
|
for (var i = 0; i < (_amount); i++) {
|
||||||
cy + dispersal ?? rng.nextInt(10), true);
|
sanityCheck++;
|
||||||
|
getCellState(cx, cy)
|
||||||
|
? i--
|
||||||
|
: setCellState(cx + rng.nextInt(_dispersal),
|
||||||
|
cy + rng.nextInt(_dispersal), true);
|
||||||
|
if (sanityCheck > 100 && sanityCheck > i*3) break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue