-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworm.js
More file actions
704 lines (639 loc) · 18.4 KB
/
Copy pathworm.js
File metadata and controls
704 lines (639 loc) · 18.4 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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
let locations = ["head", "butt", "left_side", "right_side", "left_butt", "right_butt"];
let DNAstructure = [1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1];
/*
eye num 1
eye location 2
cooldown 1
#inputs#
speed 1
rotation: 1
body_feel: 1
energy: 1
inverse_energy: 1
constant: 1
random: 1
num_of_appendages: 1
x_position: 1
y_position: 1
max energy 2
#outputs#
moveFwd: 1,
turnLeft: 1,
turnRight: 1,
eat: 1,
#layer config#
layers: 1,
nodes: 1,
activation0: 1,
activation1: 1,
activation2: 1,
*/
class Worm {
constructor(x, y) {
this.origin_x = x;
this.origin_y = y;
this.p = new Vector(x, y);
this.old_p = this.p.copy();
this.r = Math.random() * 360;
this.v = new Vector();
this.old_v = new Vector();
this.a = new Vector();
this.brain = null;
this.hitbox = new Hitbox(x, y, UNIT, UNIT * 2);
this.hitbox.config.rotation = "center";
this.energy = 100;
this.fitness = 0;
this.isTurning = false;
this.isWalking = false;
this.walkCooldown = 2;
this.restNeeded = 2;
this.turnTarget = 0;
this.currentBehaviour = "";
this.dead = false;
this.bounds = {
x1: 0,
x2: width || 0,
y1: 0,
y2: height || 0,
};
this.appendages = [];
this.MAX_ENERGY = 100;
this.CRITICAL_LOW_ENERGY = 20;
this.isDnaParsed = false;
this.inputToggles = {
speed: true,
rotation: true,
inverse_rotation: true,
energy: true,
inverse_energy: true,
body_feel: true, //something in the hitbox
constant: true,
random: true,
random2: true,
num_of_appendages: false,
x_position: false,
y_position: false,
};
this.outputToggles = {
moveFwd: true,
turnLeft: true,
turnRight: true,
eat: true,
clone: false,
moveBkwd: false,
changeBehaviour: false,
};
this.layerConfig = {
layers: 1,
nodes: this.calculateInputs() + 3,
activation0: "sigmoid",
activation1: "sigmoid",
activation2: "sigmoid",
};
this.DNA = {};
this.brain = {};
}
init() {
if (!this.isDnaParsed) {
if (Object.keys(this.DNA).length === 0) {
this.DNA = this.createDNA();
}
this.parseDNA();
}
if (Object.keys(this.brain).length === 0) this.brain = this.createBrain();
}
update(dt) {
if (this.dead) return;
//behaviour
this.doBehaviour();
//thinking
this.handleThoughts(this.think());
//turning
if (this.isTurning) this.doTurn();
//movement
//walk cooldown
if (this.isWalking) {
this.walkCooldown -= dt / 1000;
if (this.walkCooldown <= 0) {
this.isWalking = false;
}
}
//dt scaling to make the numbers kinda work well
dt = dt / (UNIT * 3);
//dt is from like 3 to 20
let a_fric = this.a.copy().mult(-2 * FRICTION_COEFF * Math.sqrt(dt));
if (a_fric.mag() > this.a.mag()) {
this.a.clear();
} else {
this.a.add(a_fric);
}
this.v.add(this.a.copy().mult(dt));
let v_fric = this.v.copy().mult(-1 * FRICTION_COEFF * Math.sqrt(dt));
if (v_fric.mag() > this.v.mag()) {
this.v.clear();
} else {
this.v.add(v_fric);
}
let real_v = this.v.copy().add(this.old_v).div(2);
real_v.mult(dt).limit(SPEED_LIMIT);
this.p.add(real_v);
if (this.v.mag() < 0.01) this.v.floor();
if (this.a.mag() < 0.01) this.a.floor();
this.old_v = this.v.copy();
this.handleOOB();
//move hitbox
this.hitbox.rotateTo(this.r);
this.hitbox.moveTo(this.p.copy());
//move appendages
this.appendages.forEach((appendage) => {
appendage.update(this.p, this.r);
});
//handle energy
if (this.energy > this.MAX_ENERGY) {
if (this.appendages.map((x) => x.name).filter((x) => x === "fat").length) {
let needToAllocate = this.energy - this.MAX_ENERGY;
this.energy = this.MAX_ENERGY;
this.allocateFat(needToAllocate);
} else {
this.addFat();
}
}
if (this.energy < this.CRITICAL_LOW_ENERGY) {
this.energy += this.getEnergyFromFat(10); //TODO maybe not 10
}
//handle energy loss and fitness
this.energy -= TICK_DECAY;
//this.fitness += TICK_DECAY/5;
let d = this.old_p.dist(this.p);
//this.fitness += d/400;
this.old_p = this.p.copy();
//die if no energy
if (this.energy <= 0) this.die();
}
addFat() {
if (this.energy <= 10) return;
this.energy -= 10; //TODO starting fat energy num
let newFat = Appendage.create("fat");
let newLocation = this.getFreeAppendageLocation();
if (newLocation) {
this.addAppendage(newFat, newLocation);
return true;
}
return false;
}
allocateFat(number) {
if (number === 0) return;
let allFat = this.appendages.filter((x) => x.name === "fat");
allFat.forEach((fat) => {
if (!fat.full && number > 0) {
number = fat.addEnergy(number);
}
});
if (number > 0) {
if (this.addFat()) {
this.energy += number;
console.log("energy is at " + this.energy);
}
}
}
getEnergyFromFat(number) {
let allFat = this.appendages.filter((x) => x.name === "fat");
if (allFat.length === 0) return 0;
let taken = 0;
let needed = number;
allFat.forEach((fat) => {
if (!fat.dead && taken <= number) {
//.getEnergy returns max possible to give, so any remainder needed must be accounted for
taken += fat.getEnergy(needed);
needed -= taken;
}
});
return taken;
}
getFreeAppendageLocation() {
let usedLocations = this.appendages.map((x) => x.location);
let freeLocations = locations.filter((x) => !usedLocations.includes(x));
let targetLocation = null;
if (freeLocations.length) {
targetLocation = getRandom(freeLocations);
}
return targetLocation;
}
turn(deg) {
if (!this.isTurning) {
this.turnTarget = this.r + deg;
this.isTurning = true;
}
}
doTurn() {
if (!this.isTurning) return;
if (Math.abs(this.turnTarget - this.r) < 1) {
this.r = this.turnTarget;
this.isTurning = false;
return;
}
this.r = (this.r + this.turnTarget) / 2;
}
walk(strength) {
if (this.isWalking) return;
if (!strength) strength = 0;
this.energy -= strength;
let direction = Vector.fromAngle(this.r).mult(strength);
this.a.add(direction);
this.isWalking = true;
this.walkCooldown = (this.restNeeded * (this.MAX_ENERGY - this.energy)) / this.MAX_ENERGY;
}
doBehaviour() {
// 'random walk'
switch (this.currentBehaviour) {
case "randomWalk":
if (Math.random() > 0.99) this.walk(1);
if (Math.random() > 0.95) this.turn(getRandom(-20, 20));
if (Math.random() > 0.97) {
let food = FOODQUAD.query(this.p.x, this.p.y, UNIT * 3).map((x) => x.data);
let potential = this.eat(food);
}
break;
case "rotate":
this.turn(5);
break;
case "moveForeward":
this.walk(1);
break;
default:
}
}
die() {
this.dead = true;
}
createBrain() {
let num_of_inputs = this.calculateInputs();
if (!num_of_inputs) {
(this.dead = true), (num_of_inputs = 1);
}
let brain = new NeuralNetwork(num_of_inputs);
//the following also might be controlled by dna idk
for (let i = 0; i < this.layerConfig.layers; i++) {
brain.addLayer(this.layerConfig.nodes, "dense", { activation_fn: this.layerConfig["activation" + i] });
}
//outputs might be different later on due to outputable appendages
let num_of_outputs = this.calculateOutputs();
if (!num_of_outputs) {
(this.dead = true), (num_of_outputs = 1);
}
brain.addLayer(num_of_outputs, "dense", { activation_fn: "relu" });
brain.init();
brain.randomizeAll();
return brain;
}
calculateInputs() {
//in the future they may have a lot of eyes or something idk
let inputs = 0;
//counts how many appendages have an input to care about
inputs += this.appendages.filter((x) => x.hasInput).length;
//next few might be controlled later on by their dna idk
//here would be the code that conrolls the input toggles
// counts the number of true in the top object and sums them and adds to inputs
inputs += Object.values(this.inputToggles).reduce((tot, a) => tot + (a ? 1 : 0), 0);
//TODO variable brains
return inputs;
}
calculateOutputs() {
let outputs = 0;
//same as above
outputs += this.appendages.filter((x) => x.hasOutput).length;
//here would be the code that conrolls the output toggles
outputs += Object.values(this.outputToggles).reduce((tot, a) => tot + (a ? 1 : 0), 0);
//TODO variable brains
return outputs;
}
think() {
let inputs = [];
/*
# should start with ones that will mostly always be true, end with inputs
# 0 - speed
# 1 - rotation
# 2 - energy
# 3 - body feel
# 4 - constant
# 5 - random
# 6,7 - x y positions
# 8+ inputs
*/
//needed more than once here so optimized
let nearbyFood = FOODQUAD.query(this.p.x, this.p.y, UNIT * 6);
if (this.inputToggles.speed) {
inputs.push(this.v.copy().mag() / SPEED_LIMIT); //normalized
}
if (this.inputToggles.rotation) {
//rotation away from north
let angle = 1 - Math.abs((this.r % 360) - 180) / 180;
inputs.push(angle);
}
if (this.inputToggles.inverse_rotation) {
//rotation towards north
let angle = 1 - Math.abs(this.r % 360) / 180;
inputs.push(angle);
}
if (this.inputToggles.energy) {
inputs.push(this.energy / this.MAX_ENERGY);
}
if (this.inputToggles.inverse_energy) {
inputs.push((this.MAX_ENERGY - this.energy) / this.MAX_ENERGY);
}
if (this.inputToggles.body_feel) {
let stuffIsThere = this.feelForThings(nearbyFood).length > 0 ? 1 : 0;
inputs.push(stuffIsThere);
}
if (this.inputToggles.constant) {
inputs.push(1);
}
if (this.inputToggles.random) {
inputs.push(Math.random());
}
if (this.inputToggles.random2) {
inputs.push(Math.random());
}
if (this.inputToggles.x_position) {
inputs.push(Math.abs(this.p.x - width / 2) / width); //distance from center
}
if (this.inputToggles.y_position) {
inputs.push(Math.abs(this.p.y - height / 2) / height); //distance from center
}
if (this.inputToggles.num_of_appendages) {
inputs.push(this.appendages.length / 6); //uhh not sure TODO
}
this.appendages
.filter((x) => x.hasInput)
.forEach((part) => {
let canSeeStuff = part.use(nearbyFood).length > 0 ? 1 : 0;
inputs.push(canSeeStuff);
});
let output = [];
try {
output = this.brain.predict(inputs);
} catch {
this.die();
}
return output;
}
handleThoughts(outputArray) {
/*
# this.outputToggles = {
moveFwd: true,
turnLeft: true,
turnRight: true,
eat: true,
clone: false,
moveBkwd: false,
changeBehaviour: false,
};
*/
//needs to stay in order
let order = ["moveFwd", "turnRight", "turnLeft", "eat", "moveBkwd", "changeBehaviour"];
outputArray.forEach((output, i) => {
if (this.outputToggles[order[i]]) {
switch (order[i]) {
case "moveFwd":
if (output > 0) {
this.walk(3 * output);
}
break;
case "turnRight":
if (output > 0) {
this.turn(60 * output);
}
break;
case "turnLeft":
if (output > 0) {
this.turn(-60 * output);
}
break;
case "eat":
if (output > 0.01) {
this.eat(FOODQUAD.query(this.p.x, this.p.y, UNIT * 3).map((x) => x.data));
}
break;
case "moveBkwd":
if (output > 0.3) {
this.walk(-3 * output);
}
break;
case "changeBehaviour":
//TODO
break;
}
}
});
}
feelForThings(arrayOfObjects) {
//TODO need to figure out what things are feelable
let felt = [];
arrayOfObjects.forEach((point) => {
if (this.hitbox.contains(point)) felt.push(point);
});
return felt;
}
handleOOB() {
if (this.p.x < this.bounds.x1) {
this.p.x = this.bounds.x1;
this.energy -= 1;
this.fitness -= 0.01;
}
if (this.p.x > this.bounds.x2) {
this.p.x = this.bounds.x2;
this.energy -= 1;
this.fitness -= 0.01;
}
if (this.p.y < this.bounds.y1) {
this.p.y = this.bounds.y1;
this.energy -= 1;
this.fitness -= 0.01;
}
if (this.p.y > this.bounds.y2) {
this.p.y = this.bounds.y2;
this.energy -= 1;
this.fitness -= 0.01;
}
}
eat(arrayOfPoints) {
let potentials = [];
arrayOfPoints.forEach((point) => {
if (point instanceof FoodPiece) {
if (this.hitbox.contains(point.p)) {
if (!point.eaten) potentials.push(point);
}
} else {
console.error(`[worm] I can't eat a ${point}`);
}
});
if (potentials.length > 0) {
let eaten = getRandom(potentials);
eaten.eaten = true;
this.energy += eaten.energy * 2;
this.fitness += eaten.energy * 1.1;
return eaten;
} else {
return null;
}
}
addAppendage(appendage, location) {
if (typeof appendage === "string") {
appendage = Appendage.create(appendage);
}
if (location === "random") {
location = this.getFreeAppendageLocation();
}
if (this.getFreeAppendageLocation() === null) {
// console.log(`[worm] theres no room for a new appendage`);
return;
}
appendage.p = this.p;
appendage.r = this.r;
appendage.setLocation(location);
this.appendages.push(appendage);
}
removeAppendage(name, location) {
if (this.appendages.length < 1) {
// console.log(`[worm] I don't have any appendages to remove`);
return;
}
if (name === "random") {
let lost = this.appendages.splice((Math.random() * this.appendages.length) | 0, 1);
// console.log(`[worm] My ${lost.name} was removed from my ${lost.location}`);
return;
}
let appendage = this.appendages.filter((part) => {
return part.name === name && part.location === location;
});
if (appendage.length > 0) {
this.appendages.splice(this.appendages.indexOf(appendage[0]), 1);
appendage[0].remove();
// console.log(`[worm] My ${appendage[0].name} was removed from my ${appendage[0].location}`);
} else {
// console.log(`[worm] I dont have a(n) ${name} on my ${location}`);
}
}
getFitness() {
return this.fitness > 0 ? this.fitness ** 2 : 0;
}
copy() {
let worm = new Worm(this.origin_x, this.origin_y);
worm.DNA = this.DNA.copy();
worm.parseDNA();
worm.brain = this.brain.copy();
return worm;
}
setP(vector) {
//since travel distance is needed for fitness, old_p and p must be the same on start
this.p = vector.copy();
this.old_p = vector.copy();
}
log() {
// console.group("worm");
// console.log("Energy: " + this.energy);
// console.log("Fitness: " + this.fitness);
// console.log("CooldownNeeded: " + this.restNeeded);
// console.log("inputs", this.inputToggles);
// console.log("outputs", this.outputToggles);
// console.log("layers", this.layerConfig);
// console.groupEnd();
}
createDNA() {
return new DNA(DNAstructure);
}
parseDNA() {
//this should be run first before anything else
let dna = this.DNA.values.map((x) => parseInt(x, 16));
let debug = false;
//
// eye num 1
//1-3 eyes are needed so;
let eyeNum = (1, (dna[0] / 4) | 0);
// if (debug) console.log("eyenum", eyeNum);
// eye location 2
let locs = [];
let locsLog = [];
let nums = [5, 2, 3, 1, 4]; //guaranteed random
for (let i = 0; i < eyeNum; i++) {
//get a random value from that above array, splice it out, find the corresponding location
let loc = locations[nums.splice(dna[2] % (nums.length - 1), 1)[0]];
locs.push(i === 0 ? "head" : loc);
locsLog.push(i);
//TODO for now first eye will always be on head
}
// if (debug) console.log("locs", locs, locsLog);
for (let i = 0; i < eyeNum; i++) {
this.addAppendage("eye", locs[i]);
}
// cooldown 1
this.restNeeded = (dna[2] % 5) + 1;
// if (debug) console.log("restNeeded", this.restNeeded);
//
// #inputs#
//
// speed 1
// rotation: 1
// body_feel: 1
// energy: 1
// inverse_energy: 1
// constant: 1
// random: 1
// num_of_appendages: 1
// x_position: 1
// y_position: 1
// percentages will vary due to importance of some of these
this.inputToggles = {
speed: dna[3] >= 3,
rotation: dna[4] >= 7,
energy: dna[5] >= 7,
inverse_energy: dna[6] >= 7,
body_feel: dna[7] >= 0, //something in the hitbox //should always exist
constant: dna[8] >= 7,
random: dna[9] >= 7,
random2: dna[10] >= 7,
num_of_appendages: false, //10 TODO
x_position: dna[11] >= 10, //11
y_position: dna[12] >= 10, //12
};
// if (debug) console.log("this.inputToggles", this.inputToggles);
//
// max energy 2
this.MAX_ENERGY = (dna[13] / 255) * 100;
// if (debug) console.log(" this.MAX_ENERGY ", this.MAX_ENERGY);
//
// #outputs#
//
// moveFwd: 1,
// turnLeft: 1,
// turnRight: 1,
// eat: 1,
//
this.outputToggles = {
moveFwd: dna[14] >= 1,
turnLeft: dna[15] >= 2,
turnRight: dna[16] >= 2,
eat: dna[17] >= 0, //this whould always be a thing it can do
clone: false,
moveBkwd: false,
changeBehaviour: false,
};
// if (debug) console.log(" this.outputToggles ", this.outputToggles);
// #layer config#
//
// layers: 1,
// nodes: 1,
// activation0: 1,
// activation1: 1,
// activation2: 1,
this.layerConfig = {
layers: 1, //TODO relative brain mixing
nodes: this.calculateInputs() + 2,
activation0: dna[20] >= 7 ? "relu" : "sigmoid", //maybe add tanh
activation1: dna[21] >= 7 ? "relu" : "sigmoid",
activation2: dna[22] >= 7 ? "relu" : "sigmoid",
};
// if (debug) console.log(" this.layerConfig ", this.layerConfig);
this.isDnaParsed = true;
}
}