-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlangant.js
307 lines (268 loc) · 7 KB
/
langant.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
//langtons Ant App
var cstep = 0;
var gridCols;
var gridRows;
var grid;
var cDraw = 0;
var drawDelay = 3;
var res = 20;
var canvas;
var cWidth = 600;
var cHeight = 400;
var cellSize;
var ants = [];
var antsPos = [];
var antsRdy = [];
var last;
var boot = true;
var speedCalled = false;
var paused = true;
var allowRepeat = false;
var endOfGen = false;
var showGrid = false;
var bgonce = true;
var code = "RL";
var colorCodes = [
"#FFFFFF",
"#000000",
"#FF4D4D",
"#668CFF",
"#70DB70",
"#FFA64D",
"#FFFF66",
"#80FFFF",
"#FF1AFF",
];
//Setup Function
function setup() {
canvas = createCanvas(cWidth, cHeight);
canvas.parent("cell_canvas");
gridCols = round(width / res);
gridRows = round(height / res);
cellSize = cHeight / gridRows;
grid = gridCreate(gridCols, gridRows);
if (boot) {
bgonce = true;
boot = false;
handlers.init();
setTimeout(function(){
setDim(80);
applyCode(code);
spawnAnt(Math.floor(gridCols / 2),Math.floor(gridRows / 2));
},1);
}
}
//Create a new empty Grid
function gridCreate(cols, rows) {
var arr = new Array(cols);
for (var i = 0; i < arr.length; i++) {
arr[i] = new Array(rows);
for (var j = 0; j < arr[i].length; j++) {
arr[i][j] = -1;
}
}
return arr;
}
//Main Draw function
function draw() {
if (bgonce) { background(180); bgonce = false; }
cDraw++;
//Draw all Tiles
for (var i = 0; i < gridCols; i++) {
for (var j = 0; j < gridRows; j++) {
var x = i * res;
var y = j * res;
var co = grid[i][j];
if (co != -1) {
co = (co + 9) % 9;
fill(colorCodes[co]);
stroke(0);
rect(x, y, res - 1, res - 1);
}
}
}
//If all Ants moved and ready for next command, then update grid and send command
var rdy = true;
for (var i = 0; i < antsRdy.length; i++) {
if (ants[i] == null) antsRdy[i] = true;
if (antsRdy[i] == false) rdy = false;
}
if (cDraw >= drawDelay && !paused && !endOfGen && rdy) {
for (var i = 0; i < antsRdy.length; i++) {
antsRdy[i] = false
}
cDraw = 0;
for (var i = 0; i < ants.length; i++) {
if (ants[i] == null) continue;
ants[i].postMessage(["MOVE", grid]);
}
cstep++;
}
document.getElementById("genDisp").innerHTML = cstep;
document.getElementById("codeDisp").innerHTML = code;
document.getElementById("inputSpeed").value = drawDelay;
}
//Change Window Dimension from DOM Input
function changeDim() {
var h = document.getElementById("inputDim").value;
setDim(h);
paused = true;
}
//Set new Window Dimension
function setDim(dim) {
h = dim;
if (h >= 3 && h <= 200) {
if (h >= 100) {
handlers.errorHandler("Dimensions above 100 will use a lot of processing power!", "warn");
msg.send("Dimensions above 100 will use a lot of processing power!");
}
h = floor(cHeight / dim);
res = h;
setup();
} else {
handlers.errorHandler("Invalid Dimension on setDim(" + dim + ")!");
}
}
//Start / Pause
function gridStart() {
if (paused) {
paused = false;
} else {
paused = true;
}
}
//Clear complete Grid
function gridClear() {
setup();
paused = true;
cstep = 0;
msg.clear();
ants = [];
bgonce = true;
console.log("killAnt(*);");
spawnAnt(Math.floor(gridCols / 2),Math.floor(gridRows / 2));
}
//Change Tick Speed
function changeSpeed() {
var newSpd = document.getElementById("inputSpeed").value;
if (newSpd >= 0 || newSpd == "") {
drawDelay = newSpd;
} else {
handlers.errorHandler("Invalid Speed on changeSpeed(" + newSpd + ")!");
}
}
//Message Service
var msg = {
send: function(msgs) {
document.getElementById("msgService").innerHTML = msgs;
},
clear: function() {
document.getElementById("msgService").innerHTML = "";
}
}
//All Handlers (Mouse, Error)
var handlers = {
mousedown: false,
lastX: 0,
lastY: 0,
init: function() {
setTimeout(function(){
var c = document.getElementById("cell_canvas");
handlers.registerEvent(c, 'mousedown', handlers.canvasMouseDown, false);
handlers.registerEvent(document, 'mouseup', handlers.canvasMouseUp, false);
}, 5);
},
errorHandler: function(e = "Unknown Error", t = "error") {
if (t == "log") console.log(e);
if (t == "info") console.info("INFO: " + e);
if (t == "warn") console.warn("WARN: " + e);
if (t == "error") console.error("ERROR: " + e);
},
canvasMouseDown: function(event) {
var position = handlers.mousePosition(event);
spawnAnt(position[0], position[1]);
handlers.lastX = position[0];
handlers.lastY = position[1];
handlers.mouseDown = true;
},
canvasMouseUp: function() {
handlers.mouseDown = false;
},
mousePosition: function (e) {
var event, x, y, domObject, posx = 0, posy = 0, top = 0, left = 0;
event = e;
if (!event) {
event = window.event;
}
if (event.pageX || event.pageY) {
posx = event.pageX;
posy = event.pageY;
} else if (event.clientX || event.clientY) {
posx = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
posy = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
domObject = event.target || event.srcElement;
while ( domObject.offsetParent ) {
left += domObject.offsetLeft;
top += domObject.offsetTop;
domObject = domObject.offsetParent;
}
domObject.pageTop = top;
domObject.pageLeft = left;
x = Math.ceil(((posx - domObject.pageLeft)/cellSize) - 1);
y = Math.ceil(((posy - domObject.pageTop)/cellSize) - 1);
return [x, y];
},
registerEvent: function (element, event, handler, capture) {
element.addEventListener(event, handler, capture);
}
}
//Spawn a new Ant
function spawnAnt(x, y) {
var a = ants.length;
console.log("spawnAnt(" + a + ");");
ants[a] = new Worker("ant.js");
antsPos[a] = [x, y];
antsRdy[a] = true;
grid[x][y] = 0;
ants[a].postMessage(["setup", grid, gridCols, gridRows, res, null, antsPos[a], a, code]);
ants[a].onmessage = function(e) {
if (e.data[0] == "sendPos") {
if (ants[a] == null) return;
antsPos[a][0] = e.data[1][0];
antsPos[a][1] = e.data[1][1];
//grid = e.data[2];
var g = e.data[2];
//Apply to Grid
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
var xx = (antsPos[a][0] - 1 + i);
var yy = (antsPos[a][1] - 1 + j);
if (xx >= gridCols || xx <= -1) continue;
if (yy >= gridRows || yy <= -1) continue;
grid[xx][yy] = g[xx][yy];
}
}
antsRdy[a] = true;
}
if (e.data[0] == "KILL") {
ants[a] = null;
console.log("Killed Ant(" + a + ")\nReason: " + e.data[1]);
}
}
}
//Apply a new Code
function applyCode(c = document.getElementById("inputCode").value) {
var nc = "";
for (var i = 0; i < c.length; i++) {
var sub = c.substring(i,i + 1).toUpperCase();
if (sub == "L" || sub == "R") {
nc = nc + sub;
}
}
code = nc.toUpperCase();
document.getElementById("inputCode").value = code;
for (var i = 0; i < ants.length; i++) {
ants[i].postMessage(["applyCode", code]);
}
}