cellular-automata/lib/src/Simulation.dart

184 lines
4.9 KiB
Dart

import 'dart:html' as html;
import 'dart:math' as math;
import 'package:rules_of_living/src/Cell.dart';
import 'package:rules_of_living/src/Grid.dart';
enum CellPattern { SpaceShip, Blinker }
class Simulation {
final Grid<Cell> map;
bool _dirty = true;
bool _renderEdges = true;
int _startingSeed;
int _x;
int _y;
int _amount;
int _dispersal;
CellPattern _pattern;
int get w => map.width;
int get h => map.height;
Simulation(int w, int h) : this.map = new Grid(w, h) {
for (int i = 0; i < map.length; i++) {
map[i] = _getGOLCell();
}
print("Grid creation finished");
}
Cell _getGOLCell([bool defaultState = false]) {
Cell cell = Cell();
return cell;
}
void reset() {
map.setAll(0, List.filled(map.length, _getGOLCell()));
if (_startingSeed != null)
addPattern(
pattern: _pattern,
dispersal: _dispersal,
amount: _amount,
seed: _startingSeed,
x: _x,
y: _y);
_dirty = true;
}
void addPattern(
{CellPattern 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(map.width ~/ 3) + (map.width ~/ 3);
int cy = y ?? rng.nextInt(map.height ~/ 3) + (map.height ~/ 3);
switch (pattern) {
// Two blocks, offset
// ##
// ##
case CellPattern.Blinker:
setCellState(cx, cy, true);
setCellState(cx + 1, cy, true);
setCellState(cx, cy + 1, true);
setCellState(cx + 1, cy + 1, true);
setCellState(cx + 2, cy + 2, true);
setCellState(cx + 3, cy + 2, true);
setCellState(cx + 2, cy + 3, true);
setCellState(cx + 3, cy + 3, true);
break;
// A 'gliding' Spaceship
// #
// #
// ###
case CellPattern.SpaceShip:
setCellState(1 + cx, 0 + cy, true);
setCellState(2 + cx, 1 + cy, true);
setCellState(2 + cx, 2 + cy, true);
setCellState(1 + cx, 2 + cy, true);
setCellState(0 + cx, 2 + cy, true);
break;
default:
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;
}
_dirty = true;
}
void setCellState(int x, int y, bool state) {
if (y >= map.height || x >= map.width) return;
state ? map.set(x, y, Cell()) : map.set(x, y, null);
}
bool getCellState(int x, int y) {
if (y >= map.height || x >= map.width) return null;
return map.get(x, y) == null ? false : true;
}
Map<int, Cell> update() {
Map<int, Cell> stateChanges = Map();
for (int i = 0; i < map.length; i++) {
math.Point p = map.toCoordinates(i);
Cell el = map[i];
bool changed = false;
int neighbors = getSurroundingNeighbors(p.x, p.y, 1);
if (el == null && neighbors == 3) {
stateChanges[i] = Cell();
} else if (el != null && neighbors != 2 && neighbors != 3) {
stateChanges[i] = null;
}
}
stateChanges.forEach((i, el) => map[i] = el);
stateChanges.length != 0 ? _dirty = true : false;
return stateChanges;
}
int getSurroundingNeighbors(int x, int y, int range) {
int count = 0;
for (int ix = -range + x; ix <= range + x; ix++) {
for (int iy = -range + y; iy <= range + y; iy++) {
if (ix >= 0 &&
iy >= 0 &&
ix < map.width &&
iy < map.height &&
map.get(ix, iy) != null &&
!(x == ix && y == iy)) count++;
}
}
return count;
}
void render(html.CanvasElement canvas, [num interp]) {
// only renders if any cells changed between renders
if (!_dirty) return;
html.CanvasRenderingContext2D ctx = canvas.getContext('2d');
int brickW = (canvas.width ~/ map.width);
int brickH = (canvas.height ~/ map.height);
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (int i = 0; i < map.length; i++) {
math.Point p = map.toCoordinates(i);
if (_renderEdges) {
ctx.setStrokeColorRgb(100, 100, 100);
ctx.strokeRect(p.x * brickW, p.y * brickH, brickW, brickH);
}
if (map[i] != null)
ctx.setFillColorRgb(155, 155, 255);
else
ctx.setFillColorRgb(0, 0, 0);
ctx.fillRect(p.x * brickW, p.y * brickH, brickW, brickH);
}
_dirty = false;
}
void set renderEdges(bool on) {
_renderEdges = on;
_dirty = true;
}
bool get renderEdges => _renderEdges;
}