-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevo.js
442 lines (399 loc) · 11.5 KB
/
evo.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// --- Feature Wishlist ---
// pretty up the animal functions, too much code in large functions now
// Help Box
// Optionally skip rendering frames to increase ticks per second
// generalized, importable/exportable types
// special organism designs: locust, pirana, tree
// do experiments with 0 mutation systems, like black/white plants and animals
// make it possible to run many simulations side by side!
// live changing size of the map
// animals non-grid based
// larger map than viewsize
// tick and render speed not dependent on javascripts scheduler
// use setTimeout instead, enable a stop counter that counts down frames until stop
// 1 for pause command, 10-60 for extinction (to let plants regrow before rendering stops)
// this would be a good way to add a scheduler independent turbo-mode
var screenW = document.body.clientWidth;
var screenH = document.body.clientHeight;
var statsdiv = document.getElementById("stats");
var messagediv = document.getElementById("message");
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
canvas.width = screenW;
canvas.height = screenH;
var defaultCfg = {
tilesize: 20,
eatlimit: 40,
endOnEmpty: true,
startPlants: 0.1,
startHerbivores: 0.01,
startCarnivores: 0.001,
plantBorder: 1,
gamespeed: 16,
plantMutation: 10,
animalMutation: 10,
viewDistance: 100
};
var cfg = copyCfg(defaultCfg);
cfg.startPlants = 0.2;
cfg.startHerbivores = 0.01;
cfg.startCarnivores = 0.0002;
cfg.endOnEmpty = true;
cfg.gamespeed = 16;
cfg.tilesize = 10;
cfg.plantBorder = 1;
cfg.eatlimit = 40;
cfg.animalMutation = 15;
cfg.plantMutation = 5;
cfg.viewDistance = 200;
var game;
function Game(config) {
this.config = config;
this.frameNumber = 0;
this.interval;
this.rerender = [[]];
this.running = false;
this.plants = [[]];
this.plantsAlive = 0;
this.animals = [];
// TODO: move these into a map based object?
this.max_x = Math.floor(screenW/config.tilesize - 1);
this.max_y = Math.floor(screenH/config.tilesize - 1);
var mapsize = this.max_x * this.max_y;
var startPlants = Math.floor(this.config.startPlants*mapsize);
if (startPlants < 1) startPlants = 1;
var startHerbivores = Math.floor(this.config.startHerbivores*mapsize);
if (startHerbivores < 1) startHerbivores = 1;
var startCarnivores = Math.floor(this.config.startCarnivores*mapsize);
if (startCarnivores < 1) startCarnivores = 1;
context.fillStyle = "black";
context.fillRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < startPlants; i++) {
var x = randomInt(0, this.max_x);
var y = randomInt(0, this.max_y);
var plotFree = !(this.plants[x] != null && this.plants[x][y] != null);
if (plotFree) {
this.plantsAlive++;
if (!this.plants[x]) {
this.plants[x] = [];
}
this.plants[x][y] = new Plant(x, y, new Color(0, randomInt(128, 255), 0));
}
}
for (var i = 0; i < startHerbivores; i++) {
var x = randomInt(0, this.max_x);
var y = randomInt(0, this.max_y);
var na = new Animal(x, y, new Color(0, 255, 0), herbivore);
na.life += 200;
na.energy += 400;
this.animals.push(na);
}
for (var i = 0; i < startCarnivores; i++) {
var x = randomInt(0, this.max_x);
var y = randomInt(0, this.max_y);
var na = new Animal(x, y, new Color(0, 255, 0), carnivore);
na.life += 200;
na.energy += 400;
this.animals.push(na);
}
this.deaths = {
oldage: 0,
starvation: 0,
predation: 0,
nuke: 0
}
this.lastRenderTime = 0;
this.fpsa = [];
this.fps = 0;
}
Game.prototype.start = function () {
this.running = true;
this.interval = setInterval(function() {
game.tick();
game.render();
game.calculateFPS();
}, this.config.gamespeed);
setInterval(function() {
// TODO: does not behave as intended, is javascripts scheduler weird?
game.updateStats();
}, 1000);
}
Game.prototype.updateStats = function () {
var herbs = 0;
var carns = 0;
for (var i = 0; i < this.animals.length; i++) {
if (this.animals[i].def == herbivore) {
herbs++;
} else {
carns++;
}
}
statsdiv.innerHTML = "Size: " + this.max_x + "x" + this.max_y + "<br>" +
"Plants: " + this.plantsAlive + "<br>" +
"Herbivores: " + herbs + "<br>" +
"Carnivores: " + carns + "<br>" +
"FPS: " + this.fps;
}
Game.prototype.calculateFPS = function () {
var fps;
if (this.lastRenderTime)
fps = Math.floor(1000/(Date.now() - this.lastRenderTime));
else
fps = 0;
this.fpsa.push(fps);
if (this.fpsa.length > 60)
this.fpsa.shift();
sum = this.fpsa.reduce((pv, cv) => pv+cv, 0);
sum = sum/this.fpsa.length;
if (sum > 10) {
sum = Math.floor(sum);
} else {
sum = Math.floor(sum*10)/10;
}
this.fps = sum;
this.lastRenderTime = Date.now();
}
Game.prototype.stop = function () {
this.running = false;
clearInterval(this.interval);
this.render();
}
Game.prototype.tick = function () {
for (var i = 0; i <= game.max_x; i++) {
for (var j = 0; j <= game.max_y; j++) {
var plant = this.getPlant(i, j);
if (plant) {
plant.grow();
}
}
}
for (var i = 0; i < this.animals.length; i++) {
this.animals[i].ai();
}
if (this.config.endOnEmpty && this.plantsAlive == 0) {
messagediv.innerHTML = "Plants went extinct";
messagediv.style.display = "block";
this.stop();
} else if (this.config.endOnEmpty && this.animals.length == 0) {
messagediv.innerHTML = "<p>☠Animals went extinct☠</p>" +
"Starvation: " + this.deaths.starvation + "<br>" +
"Old age: " + this.deaths.oldage + "<br>" +
"Predation: " + this.deaths.predation + "<br>" +
"Nuked: " + this.deaths.nuke + "<br>" +
"Click to restart";
messagediv.style.display = "block";
this.stop();
}
}
Game.prototype.render = function () {
this.frameNumber++;
for (var i = 0; i < this.rerender.length; i++) {
if (!this.rerender[i])
continue;
for (var j = 0; j < this.rerender[i].length; j++) {
if (!this.rerender[i][j])
continue;
this.rerender[i][j].draw(context);
}
}
this.rerender = [[]];
for (var i = 0; i < this.animals.length; i++) {
this.animals[i].draw(context);
}
}
Game.prototype.rerenderRq = function (obj) {
if (!this.rerender[obj.x]) {
this.rerender[obj.x] = [];
this.rerender[obj.x][obj.y] = obj;
} else if (!this.rerender[obj.x][obj.y]) {
this.rerender[obj.x][obj.y] = obj;
} else if (this.rerender[obj.x][obj.y] instanceof RerenderSq) {
this.rerender[obj.x][obj.y] = obj;
}
}
Game.prototype.getPlant = function (x, y) {
if (this.plants[x]) {
return this.plants[x][y];
}
return null;
}
function Color(r, g, b) {
this.r = r !== undefined ? r : randomInt(0, 255);
this.g = g !== undefined ? g : randomInt(0, 255);
this.b = b !== undefined ? b : randomInt(0, 255);
}
Color.prototype.toString = function () {
return "rgb(" + this.r + "," + this.g + "," + this.b + ")";
}
Color.prototype.mutate = function (mut) {
var c = new Color(this.r, this.g, this.b);
c.r += randomInt(-mut, mut);
c.g += randomInt(-mut, mut);
c.b += randomInt(-mut, mut);
if (c.r > 255) { c.r = 255; }
if (c.r < 0) { c.r = 0; }
if (c.g > 255) { c.g = 255; }
if (c.g < 0) { c.g = 0; }
if (c.b > 255) { c.b = 255; }
if (c.b < 0) { c.b = 0; }
return c;
}
function RerenderSq(x, y) {
this.x = x;
this.y = y;
}
RerenderSq.prototype.draw = function (context) {
var t = game.config.tilesize;
context.fillStyle = 'black';
context.fillRect(this.x*t, this.y*t, t, t);
}
function deleteAnimal(a) {
var i = game.animals.indexOf(a);
if (i >= 0) {
game.animals.splice(i, 1);
if (a.target) {
console.log('unset target');
a.target.targeted = false;
}
return true;
} else {
return false;
}
}
function randomInt(min, max) {
return Math.floor(Math.random()*(max-min+1)+min);
}
function randomDir(x, y) {
var d = {x, y};
var r = randomInt(0, 3);
switch (r) {
case 0:
d.x = x+1;
d.y = y;
break;
case 1:
d.x = x;
d.y = y+1;
break;
case 2:
d.x = x-1;
d.y = y;
break;
default:
d.x = x;
d.y = y-1;
}
if (d.x > game.max_x) { d.x = game.max_x; }
if (d.x < 0) { d.x = 0; }
if (d.y > game.max_y) { d.y = game.max_y; }
if (d.y < 0) { d.y = 0; }
return d;
}
function copyCfg(cfg) {
return JSON.parse(JSON.stringify(cfg)); // weird way to copy object
}
var handFunction = createHerbivore;
document.addEventListener("click", function (e) {
if (e.toElement == messagediv && !game.running) {
messagediv.style.display = "none";
game = new Game(cfg);
game.start();
} else if (e.toElement == canvas) {
if (messagediv.style.display == "block") {
messagediv.style.display = "none";
game.start();
}
var x = Math.floor(e.pageX/game.config.tilesize);
var y = Math.floor(e.pageY/game.config.tilesize);
handFunction(x, y);
}
});
var prevspdbtn = document.getElementsByClassName("selected-speed-btn")[0];
var pausebtn = document.getElementById("pause");
function uiSetSpeed(speed, caller) {
if (speed == 0 && game.running) { // puase button selected
pausebtn.classList.add("selected-speed-btn");
game.stop();
} else if (speed == 0) { // pause button re-selected
pausebtn.classList.remove("selected-speed-btn");
prevspdbtn.classList.add("selected-speed-btn");
game.start();
} else { // other button selected
pausebtn.classList.remove('selected-speed-btn');
prevspdbtn.classList.remove('selected-speed-btn');
prevspdbtn = caller;
caller.classList.add("selected-speed-btn");
game.stop();
game.config.gamespeed = speed;
cfg.gamespeed = speed;
game.start();
}
}
var prevhandbtn = document.getElementsByClassName("selected-hand-btn")[0];
function uiHandSelect(name, caller) {
if (name == "herbivore") {
handFunction = createHerbivore;
} else if (name == 'carnivore') {
handFunction = createCarnivore;
} else if (name == 'nuke') {
handFunction = nuke;
} else if (name == 'inspect') {
handFunction = inspect;
}
prevhandbtn.classList.remove("selected-hand-btn");
caller.classList.add("selected-hand-btn");
prevhandbtn = caller;
}
function createAnimal(x, y, type) {
var p = game.getPlant(x, y);
var na;
if (p) {
na = new Animal(p.x, p.y, p.color, type);
} else {
na = new Animal(x, y, new Color(), type);
}
game.animals.push(na);
console.log("Hand of God created:", na);
game.render();
}
function createHerbivore(x, y) {
createAnimal(x, y, herbivore);
}
function createCarnivore(x, y) {
createAnimal(x, y, carnivore);
}
// TODO click draggable nuke size
function nuke(x, y) {
console.log('Hand of God nuked', x, y);
var r = 10;
for (var i = 0; i <= game.max_x; i++) {
for (var j = 0; j <= game.max_y; j++) {
var a = x-i;
var b = y-j;
if (a*a + b*b < r*r) {
game.rerenderRq(new RerenderSq(i, j));
var p = game.getPlant(i, j);
if (p) {
p.size = -1;
game.plants[p.x][p.y] = null;
game.plantsAlive--;
}
}
}
}
game.animals.forEach((ani) => {
var a = x - ani.x;
var b = y - ani.y;
if (a*a + b*b < r*r) {
game.rerenderRq(new RerenderSq(ani.x, ani.y));
game.deaths.nuke++;
deleteAnimal(ani);
}
});
game.render();
}
function inspect(x, y) {
// TODO
}
game = new Game(cfg);
game.start();