-
Notifications
You must be signed in to change notification settings - Fork 0
/
automaton.js
346 lines (317 loc) · 9.77 KB
/
automaton.js
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
(function(automaton, undefined) {
// cross-browser support for requestAnimationFrame and cancelAnimationFrame
const requestAnimFrame = window.requestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.msRequestAnimationFrame
|| window.mozRequestAnimationFrame
|| function(callback) { return window.setTimeout(callback, 1000 / 60); };
const cancelAnimFrame = window.cancelAnimationFrame
|| window.webkitCancelRequestAnimationFrame || window.webkitCancelAnimationFrame
|| window.msCancelRequestAnimationFrame || window.msCancelAnimationFrame
|| window.mozCancelRequestAnimationFrame || window.mozCancelAnimationFrame
|| window.oCancelRequestAnimationFrame || window.oCancelAnimationFrame
|| function(id) { clearTimeout(id); };
const clamp = function(value, min, max) {
if(value < min) {
return min;
} else if(value > max) {
return max;
}
return value;
}
const NeighborType = {
DIRECT : 1,
CORNER : 2
}
class Settings {
constructor(guiSettings) {
this.blockSize = Math.floor(guiSettings.blockSize);
this.numColors = Math.floor(guiSettings.numColors) * 2 + 1;
this.initialPopulation = clamp(guiSettings.initialPopulation, 0.0, 1.0);
this.apply(guiSettings);
}
apply(guiSettings) {
this.borders = { "W": null, "E": -1, "U": undefined }[guiSettings.borders];
this.neighbors = { "D": NeighborType.DIRECT, "DC": NeighborType.DIRECT|NeighborType.CORNER }[guiSettings.neighbors];
this.initialCellLife = Math.floor(guiSettings.initialCellLife);
this.running = guiSettings.running === true;
this.speed = clamp(guiSettings.speed, 0.001, 2.0);
}
}
class Cell {
set(colorId, life) {
this.colorId = colorId;
this.life = life || settings.initialCellLife;
}
copy(cell) {
this.colorId = cell.colorId;
this.life = cell.life;
}
constructor(colorId, life) {
this.set(colorId, life);
}
}
let settings;
const changedCells = new Map();
changedCells.addCell = function(x, y, colorId) {
const positions = changedCells.get(colorId) || [];
positions.push({ x : x, y : y });
changedCells.set(colorId, positions);
}
let data;
let oldData;
let width;
let height;
let canvas;
let context;
let bufferCanvas;
let bufferContext;
let timeUntilUpdate;
let animFrameReqId;
let lastFrameTime;
function initCanvas() {
bufferCanvas = document.createElement("canvas");
[canvas, bufferCanvas].forEach(can => {
can.width = window.innerWidth;
can.height = window.innerHeight;
});
canvas.addEventListener("mousedown", mouseHandler, false);
canvas.addEventListener("mousemove", mouseHandler, false);
canvas.addEventListener("mouseup", mouseHandler, false);
canvas.addEventListener("wheel", mouseHandler, false);
context = canvas.getContext("2d", { alpha: false });
bufferContext = bufferCanvas.getContext("2d", { alpha: false });
[context, bufferContext].forEach(ctx => {
ctx.msImageSmoothingEnabled = false;
ctx.imageSmoothingEnabled = false;
});
}
const mouseHandler = {
mouseDown : false,
mouseChangedCells : new Array(),
brushSize : 1,
handleEvent : function(event) {
if(event.type == "mousedown" && event.buttons == 1) {
mouseHandler.handleCellClick(event);
mouseHandler.mouseDown = true;
} else if(event.type == "mousemove" && mouseHandler.mouseDown) {
mouseHandler.handleCellClick(event);
} else if(event.type == "mouseup" && event.buttons == 0) {
mouseHandler.mouseDown = false;
mouseHandler.mouseChangedCells = new Array();
} else if(event.type == "wheel" && event.deltaY != 0) {
mouseHandler.brushSize = clamp(mouseHandler.brushSize + (event.deltaY > 0 ? -2 : 2), 1, 101);
}
},
handleCellClick : function(event) {
const baseX = Math.floor(event.pageX / settings.blockSize);
const baseY = Math.floor(event.pageY / settings.blockSize);
for(let offsetY = -(mouseHandler.brushSize-1)/2; offsetY <= (mouseHandler.brushSize-1)/2; offsetY++) {
for(let offsetX = -(mouseHandler.brushSize-1)/2; offsetX <= (mouseHandler.brushSize-1)/2; offsetX++) {
const x = baseX + offsetX;
const y = baseY + offsetY;
if(x < 0 || y < 0 || x >= width || y >= height || mouseHandler.mouseChangedCells.find(cell => cell.x == x && cell.y == y)) {
continue;
}
if(event.shiftKey) {
data[y][x].set(-1);
} else {
data[y][x].set((data[y][x].colorId + 1) % settings.numColors);
}
changedCells.addCell(x, y, data[y][x].colorId);
mouseHandler.mouseChangedCells.push({ x : x, y : y });
}
}
}
}
function initData() {
width = Math.floor(canvas.width / settings.blockSize);
height = Math.floor(canvas.height / settings.blockSize);
data = new Array(height);
oldData = new Array(height);
changedCells.clear();
for(let y = 0; y < height; y++) {
data[y] = new Array(width);
oldData[y] = new Array(width);
for(let x = 0; x < width; x++) {
if(Math.random() < settings.initialPopulation) {
data[y][x] = new Cell(Math.floor(Math.random() * settings.numColors));
} else {
data[y][x] = new Cell(-1);
}
oldData[y][x] = new Cell(-1);
changedCells.addCell(x, y, data[y][x].colorId);
}
}
timeUntilUpdate = 0;
lastFrameTime = undefined;
}
function getNeighbor(type, data, x, y) {
if((settings.neighbors & type) !== type) {
return undefined;
}
if(x < 0) {
if(settings.borders === null) {
x += width;
} else {
return settings.borders;
}
}
if(x >= width) {
if(settings.borders === null) {
x -= width;
} else {
return settings.borders;
}
}
if(y < 0) {
if(settings.borders === null) {
y += height;
} else {
return settings.borders;
}
}
if(y >= height) {
if(settings.borders === null) {
y -= height;
} else {
return settings.borders;
}
}
return data[y][x].colorId;
}
function calculateIndividualScore(own, other) {
if(own === other) {
return 0;
}
if(own === -1 || own === undefined) {
// empty loses against anything else
return -1;
}
if(other === -1 || other === undefined) {
// anything non-empty wins against empty
return 1;
}
// generalized rock-paper-scissors rules:
// if own and other have the same parity, the larger one wins
// if they have different parity, the smaller one wins
// https://math.stackexchange.com/a/3229687
if(own % 2 == other % 2) {
return own > other ? 1 : -1;
}
return own < other ? 1 : -1;
}
function calculateCell(current, top, bottom, left, right, topLeft, topRight, bottomLeft, bottomRight) {
let totalScore = 0;
const allScores = new Map();
for(let neighbor of [top, bottom, left, right, topLeft, topRight, bottomLeft, bottomRight]) {
if(neighbor === undefined) {
continue;
}
const score = calculateIndividualScore(current.colorId, neighbor);
totalScore += score;
allScores.set(neighbor, (allScores.get(neighbor) || 0) + score);
}
if(totalScore >= 0) {
return current;
}
let maxNeighbor = undefined;
let numWithMax = 0;
allScores.forEach((neighborScore, neighbor) => {
if(neighborScore < 0) {
const score = calculateIndividualScore(neighbor, maxNeighbor);
if(score > 0) {
maxNeighbor = neighbor;
numWithMax = 1;
} else if(score == 0) {
numWithMax++;
}
}
});
if(maxNeighbor !== undefined && numWithMax == 1) {
current.life += allScores.get(maxNeighbor); // the score is negative
if(current.life <= 0) {
return new Cell(maxNeighbor);
} else {
return current;
}
}
return new Cell(-1);
}
automaton.performStep = function() {
for(let y = 0; y < height; y++) {
for(let x = 0; x < width; x++) {
oldData[y][x].copy(data[y][x]);
}
}
for(let y = 0; y < height; y++) {
for(let x = 0; x < width; x++) {
data[y][x].copy(calculateCell(
oldData[y][x],
getNeighbor(NeighborType.DIRECT, oldData, x, y - 1),
getNeighbor(NeighborType.DIRECT, oldData, x, y + 1),
getNeighbor(NeighborType.DIRECT, oldData, x - 1, y),
getNeighbor(NeighborType.DIRECT, oldData, x + 1, y),
getNeighbor(NeighborType.CORNER, oldData, x - 1, y - 1),
getNeighbor(NeighborType.CORNER, oldData, x + 1, y - 1),
getNeighbor(NeighborType.CORNER, oldData, x - 1, y + 1),
getNeighbor(NeighborType.CORNER, oldData, x + 1, y + 1)));
if(data[y][x].colorId != oldData[y][x].colorId) {
changedCells.addCell(x, y, data[y][x].colorId);
}
}
}
}
function selectColor(colorId) {
if(colorId < 0) {
return "black";
}
return "hsl(" + (colorId * 137.508) + ", 100%, 50%)"; // golden angle approximation
}
function redrawChangedCells() {
const blockSize = settings.blockSize;
changedCells.forEach((cells, colorId) => {
bufferContext.fillStyle = selectColor(colorId);
cells.forEach(cell =>
bufferContext.fillRect(
cell.x * blockSize,
cell.y * blockSize,
blockSize,
blockSize)
);
});
changedCells.clear();
}
function updateAndDrawFrame(timestamp) {
if(settings.running && !mouseHandler.mouseDown) {
if(lastFrameTime === undefined) {
lastFrameTime = timestamp;
}
const millisPerFrame = 1000 / 30.0;
timeUntilUpdate += clamp(timestamp - lastFrameTime, 0.0, millisPerFrame) / millisPerFrame * settings.speed;
while(timeUntilUpdate >= 1.0) {
automaton.performStep();
timeUntilUpdate -= 1.0;
}
lastFrameTime = timestamp;
}
redrawChangedCells();
context.drawImage(bufferCanvas, 0, 0);
animFrameReqId = requestAnimFrame(updateAndDrawFrame);
}
automaton.setCanvas = function(canvasElement) {
canvas = canvasElement;
}
automaton.start = function(guiSettings) {
settings = new Settings(guiSettings);
initCanvas();
initData();
if(animFrameReqId !== undefined) {
cancelAnimFrame(animFrameReqId);
}
animFrameReqId = requestAnimFrame(updateAndDrawFrame);
}
automaton.applySettings = function(guiSettings) {
settings.apply(guiSettings);
}
}(window.automaton = window.automaton || {}));