forked from Code-Bullet/NEAT-Template-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketchOld.js
More file actions
552 lines (470 loc) · 15.1 KB
/
sketchOld.js
File metadata and controls
552 lines (470 loc) · 15.1 KB
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//this is a template to add a NEAT ai to any game
//note //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
//this means that there is some information specific to the game to input here
var nextConnectionNo = 1000;
var population;
var speed = 60;
var showBest = true; //true if only show the best of the previous generation
var runBest = false; //true if replaying the best ever game
var humanPlaying = false; //true if the user is playing
var humanPlayer;
var showBrain = false;
var showBestEachGen = false;
var upToGen = 0;
var genPlayerTemp; //player
var showNothing = false;
let treats = [];
let enemies = [];
let anti = [];
let ball;
let ban;
let ballRespawnTime = 0;
let treatRespawnTime = 0;
let PBRespawnTime = 0;
let bandanaRespawnTime = 0;
let enemyRespawnTime = 0;
let pb;
var bg;
var blockImg;
var dog;
var acorn;
var squirrel;
var peanut;
var yum;
let wall;
let blocks = [];
//--------------------------------------------------------------------------------------------------------------------------------------------------
function preload(){
bg = loadImage("images/Library_Map.png");
blockImg = loadImage("images/square.png");
acorn = loadImage("images/Acorn_Item.png");
squirrel = loadImage("spriteSheets/Enemy_Side_Template.png");
yum = loadImage("images/Dog_Treat.png");
peanut = loadImage("images/Peanut_Butter.png");
dog = loadImage("spriteSheets/Dog_template_smaller.png");
}
//--------------------------------------------------------------------------------------------------------------------------------------------------
function setup() {
let canvas = createCanvas(1080, 900);
canvas.parent("canvasContainer");
population = new Population(500); //maybe make smaller, lots of lag/slowdown
wall = new Wall(MAP_DATA); //map for collisions
let blockSize = 10;
let wallWidth = wall.getColumns() * blockSize;
let wallHeight = wall.getRows() * blockSize;
let offsetX = (width - wallWidth) / 2;
let offsetY = (height - wallHeight) / 2;
for (let i = 0; i < wall.getRows(); i++) {
for (let j = 0; j < wall.getColumns(); j++) {
if (wall.getElement(i, j) === '*') {
blocks.push(new Block(j * blockSize + offsetX, i * blockSize + offsetY));
}
}
}
for (let i = 0; i < 15; i++) {
treats.push(new Treat());
}
ball = new TennisBall();
ballRespawnTime = 0; // allow immediate usage on start
ban = new Bandana();
bandanaRespawnTime = 0;
pb = new PeanutButter();
PBRespawnTime = 0;
for (let i = 0; i < 5; i++) {
enemies.push(new Enemy());
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function draw() {
background(255);
//add treats/collectibles to the screen
if (bg) {
image(bg, 0, 0, width, height);
}
for(var i = 0; i < blocks.length; i++)
blocks[i].show();
for (let i = treats.length - 1; i >= 0; i--) {
treats[i].show();
}
if (ball) {
ball.show();
}
if (ban) {
ban.show();
}
if (pb) {
pb.show();
}
handleRespawns(); //handle respawning of items
//move and show enemies, remove inactive
for (let i = enemies.length - 1; i >= 0; i--) {
enemies[i].move();
enemies[i].show();
if (!enemies[i].isActive) {
enemies.splice(i, 1);
}
}
for (let a of anti) {
a.show();
}
//placeholder text so player knows how to start playing
if (!humanPlaying && !runBest && !showBestEachGen) {
fill(255);
textAlign(RIGHT, BOTTOM);
textSize(24);
text("Press P to play as human", width - 10, height - 10);
}
//placeholder text for debugging
if (!runBest ) {
fill(255);
textAlign(LEFT, BOTTOM);
textSize(24);
text("Press Space to see all AI runs", 10, height - 10);
}
drawToScreen();
if (showBestEachGen) { //show the best of each gen
showBestPlayersForEachGeneration();
} else if (humanPlaying) { //if the user is controling the ship
showHumanPlaying();
} else if (runBest) { //if replaying the best ever game
showBestEverPlayer();
} else { //if just evolving normally
if (!population.done()) { //if any players are alive then update them
population.updateAlive();
for (let i = 0; i < population.players.length; i++) {
if (!population.players[i].dead) {
handleInteractions(population.players[i]);
}
}
} else { //all dead
//genetic algorithm
population.naturalSelection();
//resetGame(); //reset the game state for the next generation
}
}
//drawGrid();
if (humanPlaying && humanPlayer && humanPlayer.stamina !== undefined) {
drawStaminaBar(humanPlayer);
}
}
function handleRespawns() {
//respawn Peanut Butter if missing and timer passed
if ((!pb || pb.life < millis()) && millis() > PBRespawnTime) {
pb = new PeanutButter();
PBRespawnTime = millis() + 60000;
} else if (pb && pb.life < millis()) {
//mark pb for respawn and clear current instance
pb = null;
PBRespawnTime = millis() + 60000;
}
//respawn Tennis Ball if missing and timer passed
if ((!ball || ball.life < millis()) && millis() > ballRespawnTime) {
ball = new TennisBall();
ballRespawnTime = millis() + 10000;
} else if (ball && ball.life < millis()) {
ball = null;
ballRespawnTime = millis() + 10000;
}
//respawn Bandana if missing and timer passed
if ((!ban || ban.life < millis()) && millis() > bandanaRespawnTime) {
ban = new Bandana();
bandanaRespawnTime = millis() + 60000;
} else if (ban && ban.life < millis()) {
ban = null;
bandanaRespawnTime = millis() + 60000;
}
//respawn enemies if gone and timer passed
if (millis() > enemyRespawnTime && enemies.length < 5) {
enemies.push(new Enemy());
enemyRespawnTime = millis() + 5000;
}
//respawn treats if count low and timer passed
if (millis() > treatRespawnTime && treats.length < 15) {
treats.push(new Treat());
treatRespawnTime = millis() + 3000;
}
//remove expired treats
for (let i = treats.length - 1; i >= 0; i--) {
if (treats[i].life < millis()) {
treats[i].eaten(); //return spawn point to the array if needed
treats.splice(i, 1);
}
}
//remove expired anti items
for (let i = anti.length - 1; i >= 0; i--) {
if (anti[i].life < millis()) {
anti.splice(i, 1);
}
}
}
function drawStaminaBar(player) {
let barWidth = 200;
let barHeight = 20;
let staminaRatio = player.stamina / player.maxStamina;
fill(50);
rect(20, 20, barWidth, barHeight);
fill(0, 200, 255);
rect(20, 20, barWidth * staminaRatio, barHeight);
stroke(255);
noFill();
rect(20, 20, barWidth, barHeight);
}
//temp function to draw a grid for mapping out patrolls and treats
function drawGrid() {
let gridSize = 100;
stroke(200);
strokeWeight(1);
//vertical lines
for (let x = 0; x <= width; x += gridSize) {
line(x, 0, x, height);
}
//horizontal lines
for (let y = 0; y <= height; y += gridSize) {
line(0, y, width, y);
}
noStroke();
}
//-----------------------------------------------------------------------------------
function showBestPlayersForEachGeneration() {
if (!genPlayerTemp.dead) { //if current gen player is not dead then update it
genPlayerTemp.look();
genPlayerTemp.think();
genPlayerTemp.update();
genPlayerTemp.show();
handleInteractions(genPlayerTemp);
} else { //if dead move on to the next generation
upToGen++;
if (upToGen >= population.genPlayers.length) { //if at the end then return to the start and stop doing it
upToGen = 0;
showBestEachGen = false;
} else { //if not at the end then get the next generation
genPlayerTemp = population.genPlayers[upToGen].cloneForReplay();
}
}
}
//-----------------------------------------------------------------------------------
function showHumanPlaying() {
if (!humanPlayer.dead) { //if the player isnt dead then move and show the player based on input
humanPlayer.look();
humanPlayer.update();
humanPlayer.show();
handleInteractions(humanPlayer); //handle interactions with treats, enemies, and the tennis ball
}
else { //once done return to ai
humanPlaying = false;
}
}
//-----------------------------------------------------------------------------------
function showBestEverPlayer() {
if (!population.bestPlayer.dead) { //if best player is not dead
population.bestPlayer.look();
population.bestPlayer.think();
population.bestPlayer.update();
population.bestPlayer.show();
handleInteractions(bestPlayer);
} else { //once dead
runBest = false; //stop replaying it
population.bestPlayer = population.bestPlayer.cloneForReplay(); //reset the best player so it can play again
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//draws the display screen
function drawToScreen() {
if (!showNothing) {
//pretty stuff
drawBrain();
writeInfo();
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function drawBrain() { //show the brain of whatever genome is currently showing
var startX = 0; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
var startY = 0;
var w = 0;
var h = 0;
if (runBest) {
population.bestPlayer.brain.drawGenome(startX, startY, w, h);
} else
if (humanPlaying) {
showBrain = false;
} else if (showBestEachGen) {
genPlayerTemp.brain.drawGenome(startX, startY, w, h);
} else {
population.players[0].brain.drawGenome(startX, startY, w, h);
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//writes info about the current player
function writeInfo() {
let info = "";
if (showBestEachGen) {
info += "Score: " + genPlayerTemp.score + "<br>";
info += "Gen: " + (genPlayerTemp.gen + 1) + "<br>";
} else if (humanPlaying) {
info += "Score: " + humanPlayer.score + "<br>";
} else if (runBest) {
info += "Score: " + population.bestPlayer.score + "<br>";
info += "Gen: " + population.gen + "<br>";
} else {
if (showBest) {
info += "Score: " + population.players[0].score + "<br>";
info += "Gen: " + population.gen + "<br>";
info += "Species: " + population.species.length + "<br>";
info += "Global Best Score: " + population.bestScore + "<br>";
}
}
//write the info to the HTML div
let infoDiv = document.getElementById("gameInfo");
if (infoDiv) {
infoDiv.innerHTML = info;
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------
function keyPressed() {
switch (key) {
case ' ':
//toggle showBest
showBest = !showBest;
break;
case '+': //speed up frame rate
speed += 10;
frameRate(speed);
prvarln(speed);
break;
case '-': //slow down frame rate
if(speed > 10) {
speed -= 10;
frameRate(speed);
prvarln(speed);
}
break;
case 'B': //run the best
runBest = !runBest;
break;
case 'G': //show generations
showBestEachGen = !showBestEachGen;
upToGen = 0;
genPlayerTemp = population.genPlayers[upToGen].clone();
break;
case 'N': //show absolutely nothing in order to speed up computation
showNothing = !showNothing;
break;
case 'P': //play
humanPlaying = !humanPlaying;
humanPlayer = new Player();
break;
}
//any of the arrow keys
switch (keyCode) {
case UP_ARROW:
if (humanPlaying) humanPlayer.move("w");
break;
case DOWN_ARROW:
if (humanPlaying) humanPlayer.move("s");
break;
case LEFT_ARROW:
if (humanPlaying) humanPlayer.move("a");
break;
case RIGHT_ARROW: //right is used to move through the generations
if (showBestEachGen) { //if showing the best player each generation then move on to the next generation
upToGen++;
if (upToGen >= population.genPlayers.length) { //if reached the current generation then exit out of the showing generations mode
showBestEachGen = false;
} else {
genPlayerTemp = population.genPlayers[upToGen].cloneForReplay();
}
} else if (humanPlaying) { //if the user is playing then move player right
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
}
break;
}
}
//maybe have unique function for humanplayer where things disappear
function handleInteractions(player) {
if (player.dead) return;
//Treats
for (let i = treats.length - 1; i >= 0; i--) {
if (treats[i].checkCollision(player) && !treats[i].idList.includes(player.uuid)) {
player.score += 1;
treats[i].idList.push(player.uuid); //add player id to the treat
player.lastScoreMillis = millis();
}
}
//Tennis Ball
if (ball && ball.checkCollision(player) && !ball.idList.includes(player.uuid)) {
player.stamina = player.maxStamina; //reset stamina
ball.idList.push(player.uuid); //add player id
ballRespawnTime = millis() + 10000;
}
//Bandana
if (ban && ban.checkCollision(player && !ban.idList.includes(player.uuid))) {
player.isInvincible = true;
player.isInvinUntil = millis() + 10000;
ban.idList.push(player.uuid); //add player id
bandanaRespawnTime = millis() + 60000;
}
//Peanut Butter
if (pb && pb.checkCollision(player)&& !pb.idList.includes(player.uuid)) {
pb.idList.push(player.uuid); //add player id
player.score += 10;
PBRespawnTime = millis() + 60000;
player.lastScoreMillis = millis();
}
//Anti
for (let i = anti.length - 1; i >= 0; i--) {
if (anti[i].checkCollision(player) && !player.isInvincible && !anti[i].idList.includes(player.uuid)) {
anti[i].idList.push(player.uuid); //add player id
player.score -= 5;
}
//get rid of anti penalty
if (anti[i]) {
if (player.isInvincible) {
anti[i].playInvin = true;
} else {
anti[i].playInvin = false;
}
}
}
//Enemies
for (let i = enemies.length - 1; i >= 0; i--) {
if (enemies[i].checkCollision(player) && !player.isInvincible) {
player.dead = true;
}
//enemy invincibility handling
if (enemies[i]) {
if (player.isInvincible) {
enemies[i].playInvin = true;
} else {
enemies[i].playInvin = false;
}
}
}
}
//function to reset the game state
function resetGame() {
// Clear all dynamic game state
treats = [];
enemies = [];
anti = [];
ball = null;
ban = null;
pb = null;
// Reset collectibles
ball = new TennisBall();
ban = new Bandana();
pb = new PeanutButter();
// Set their respawn times based on current millis
ballRespawnTime = millis() + 10000;
bandanaRespawnTime = millis() + 60000;
PBRespawnTime = millis() + 60000;
// Reset treat timer
treatRespawnTime = millis() + 3000;
enemyRespawnTime = millis() + 5000;
// Add initial treats and enemies
for (let i = 0; i < 15; i++) {
treats.push(new Treat());
}
for (let i = 0; i < 5; i++) {
enemies.push(new Enemy());
}
}