Split up addRandomPattern function
This commit is contained in:
parent
0b5b1de454
commit
474adb740c
1 changed files with 32 additions and 13 deletions
|
@ -9,6 +9,10 @@ class Simulation {
|
||||||
Grid<bool> map;
|
Grid<bool> map;
|
||||||
Grid<bool> _snapshot;
|
Grid<bool> _snapshot;
|
||||||
|
|
||||||
|
final int _RANDOM_PATTERN_AMOUNT = 20;
|
||||||
|
final int _RANDOM_PATTERN_DISPERSAL = 10;
|
||||||
|
final double _RANDOM_PATTERN_SPREAD_FROM_CENTER = 1 / 3;
|
||||||
|
|
||||||
RuleSet rules = GameOfLife();
|
RuleSet rules = GameOfLife();
|
||||||
|
|
||||||
bool dirty = true;
|
bool dirty = true;
|
||||||
|
@ -16,7 +20,6 @@ class Simulation {
|
||||||
bool _renderEdges = true;
|
bool _renderEdges = true;
|
||||||
bool get renderEdges => _renderEdges;
|
bool get renderEdges => _renderEdges;
|
||||||
|
|
||||||
int _amount;
|
|
||||||
int _dispersal;
|
int _dispersal;
|
||||||
|
|
||||||
math.Point get gridSize => math.Point<int>(map.width, map.height);
|
math.Point get gridSize => math.Point<int>(map.width, map.height);
|
||||||
|
@ -38,25 +41,41 @@ class Simulation {
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
void addRandomPattern({int amount, int dispersal}) {
|
void addRandomPattern(
|
||||||
int _startingSeed = DateTime.now().millisecondsSinceEpoch;
|
{int seed, int amount, int dispersal, num spreadFromCenter}) {
|
||||||
math.Random rng = new math.Random(_startingSeed);
|
math.Random rng = _getRNG(seed ?? DateTime.now().millisecondsSinceEpoch);
|
||||||
_amount = amount ?? rng.nextInt(20);
|
amount ??= rng.nextInt(_RANDOM_PATTERN_AMOUNT);
|
||||||
_dispersal = dispersal ?? 10;
|
dispersal ??= _RANDOM_PATTERN_DISPERSAL;
|
||||||
int cx = rng.nextInt(map.width ~/ 3) + (map.width ~/ 3);
|
spreadFromCenter ??= _RANDOM_PATTERN_SPREAD_FROM_CENTER;
|
||||||
int cy = rng.nextInt(map.height ~/ 3) + (map.height ~/ 3);
|
|
||||||
|
|
||||||
int sanityCheck = 0;
|
int sanityCheck = 0;
|
||||||
for (var i = 0; i < (_amount); i++) {
|
Map<int, bool> changeSet = {};
|
||||||
|
for (var i = 0; i < (amount); i++) {
|
||||||
sanityCheck++;
|
sanityCheck++;
|
||||||
getCellState(cx, cy)
|
math.Point cell = _getRandomPoint(rng, gridSize, spreadFromCenter);
|
||||||
|
getCellState(cell.x, cell.y)
|
||||||
? i--
|
? i--
|
||||||
: setCellState(
|
: changeSet[map.toIndex(cell.x, cell.y)] = true;
|
||||||
cx + rng.nextInt(_dispersal), cy + rng.nextInt(_dispersal), true);
|
|
||||||
if (sanityCheck > 100 && sanityCheck > i * 3) break;
|
if (sanityCheck > 100 && sanityCheck > i * 3) break;
|
||||||
}
|
}
|
||||||
|
mergeStateChanges(changeSet);
|
||||||
|
}
|
||||||
|
|
||||||
dirty = true;
|
math.Random _getRNG(int seed) {
|
||||||
|
math.Random rng = new math.Random(seed);
|
||||||
|
return rng;
|
||||||
|
}
|
||||||
|
|
||||||
|
math.Point<int> _getRandomPoint(
|
||||||
|
math.Random rng, math.Point size, num spreadFromCenter) {
|
||||||
|
math.Point absoluteSpread =
|
||||||
|
math.Point(map.width * spreadFromCenter, map.height * spreadFromCenter);
|
||||||
|
math.Point center = math.Point(map.width / 2, map.height / 2);
|
||||||
|
num cx = rng.nextInt(absoluteSpread.x.toInt()) +
|
||||||
|
(center.x - absoluteSpread.x / 2);
|
||||||
|
num cy = rng.nextInt(absoluteSpread.y.toInt()) +
|
||||||
|
(center.y - absoluteSpread.y / 2);
|
||||||
|
return math.Point<int>(cx.toInt(), cy.toInt());
|
||||||
}
|
}
|
||||||
|
|
||||||
void setCellState(int x, int y, bool state) {
|
void setCellState(int x, int y, bool state) {
|
||||||
|
|
Loading…
Reference in a new issue