BlueGit

blue-star cellular-evolution

debug drawing, fix issue with ID's in synapse generation

timfa Tim · pushed by timfa

Authored 2021-10-13 · 5 years ago

Hash
9905161385497e8df2f73037634aafd7aedc0420
Parent
79702582
7 files changed +269 −22
js/game.js +188 −11
@@ -6,6 +6,9 @@ var cell =
6 6 {
7 7 localPos: new vector(0, 0),
8 8 parentCreature: null,
9 cellRef: -1,
10 currentSensorSignal: 0, //only used for sensors, else forever zero
11 lastSum: 0, //used for debug purposes
9 12
10 13 start: function()
11 14 {
@@ -21,6 +24,14 @@ var cell =
21 24 scaleUpTween.start();
22 25 },
23 26
27 draw: function()
28 {
29 this.defaultDraw();
30
31 GameWindow.canvasContext.fillStyle = 'black';
32 GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);
33 },
34
24 35 update: function()
25 36 {
26 37 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
@@ -51,20 +62,99 @@ var creature =
51 62 {
52 63 for(var i = 0; i < this.dna.length; i++)
53 64 {
54 var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_orb, 500);
65 var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_cell, 400);
55 66
56 67 newCell.setType(this.dna[i].type);
57 68 newCell.parentCreature = this;
58 69 newCell.localPos = new vector(this.dna[i].pos.x * 32, this.dna[i].pos.y * 32)
59 70
71 newCell.cellRef = i;
72
60 73 this.cellInstances.push(newCell);
61 74 }
62 75 },
63 76
64 77 update: function(deltaTime)
65 78 {
66 this.transform.rotation += deltaTime * 60;
67 this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))
79 for(var i = this.cellInstances.length -1; i >= 0; i--) //reverse loop for deletion of references to destroyed cells
80 {
81 if(this.cellInstances[i].destroyed)
82 this.cellInstances.splice(i, 1);
83 }
84
85 for(var i = 0; i < this.cellInstances.length; i++) //loop through instances instead of dna so that destroyed cells are immediately ignored
86 {
87 var type = this.dna[this.cellInstances[i].cellRef].type;
88
89 if(type == TYPES.LOCOMOTOR || type == TYPES.ROTATOR)
90 {
91 if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)
92 {
93 var sum = sigmoid(this.getCellInputSum(this.cellInstances[i]));
94 this.cellInstances[i].lastSum = sum;
95
96 //TODO: Act on the sum we've received (ALWAYS between 0 and 1, but VERY rarely exactly 0 or 1)
97 }
98 }
99 }
100
101 //this.transform.rotation += deltaTime * 60;
102 //this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))
103 },
104
105 draw: function()
106 {
107 for(var i = 0; i < this.cellInstances.length; i++)
108 {
109 if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)
110 {
111 for(var c = 0; c < this.dna[this.cellInstances[i].cellRef].inputs.length; c++)
112 {
113 var pos = this.findCellInstanceByCellId(this.dna[this.cellInstances[i].cellRef].inputs[c].id).transform.position;
114
115 GameWindow.canvasContext.strokeStyle = 'red';
116 GameWindow.canvasContext.beginPath();
117 GameWindow.canvasContext.moveTo(this.cellInstances[i].transform.position.x, this.cellInstances[i].transform.position.y);
118 GameWindow.canvasContext.lineTo(pos.x, pos.y);
119 GameWindow.canvasContext.stroke();
120 }
121 }
122 }
123 },
124
125 getCellInputSum: function(cellInstance, tracer = [])
126 {
127 var cellId = this.dna[cellInstance.cellRef].id;
128
129 if(tracer.indexOf(cellId) >= 0) //is my id already in the tracer?
130 return 0;
131
132 tracer.push(cellId)
133
134 sum = cellInstance.currentSensorSignal;
135
136 if(this.dna[cellInstance.cellRef].inputs.length > 0)
137 {
138 for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)
139 {
140 sum += sigmoid(this.getCellInputSum(
141 this.findCellInstanceByCellId(this.dna[cellInstance.cellRef].inputs[i].id)
142 , tracer) * this.dna[cellInstance.cellRef].inputs[i].weight);
143 }
144 }
145
146 cellInstance.lastSum = sum;
147
148 return sum;
149 },
150
151 findCellInstanceByCellId: function(id)
152 {
153 for(var i = 0; i < this.cellInstances.length; i++)
154 {
155 if(this.dna[this.cellInstances[i].cellRef].id == id)
156 return this.cellInstances[i];
157 }
68 158 },
69 159
70 160 onDestroy: function()
@@ -101,7 +191,7 @@ var creature =
101 191 *
102 192 * Types of cells:
103 193 * ACTIVATORS
104 * - Mouth: can Bite, which will eat whatever is in front of it, be that a cell or a food pellet
194 * - Mouth: can Bite, which will eat whatever is in front of it, be that a cell or a food pellet (TEMP: automatically works without input for now)
105 195 * - Locomotor: When activated, pushes the creature in the direction of the creature's center, relative from the locomotor cell
106 196 * - Rotator: Applies torque when activated. 0.5 is neural, 0 is full left, 1 is full right
107 197 *
@@ -163,10 +253,94 @@ function GenerateNewDNA(startingCells = 10)
163 253 for(var i = 0; i < startingCells; i++)
164 254 AddRandomCell(dna)
165 255
256 for(var i = 0; i < startingCells; i++)
257 AddRandomSynapse(dna)
258
259 dna[0].inputs = [];
260
166 261 return dna;
167 262 }
168 263
169 function AddRandomCell(creature)
264 function MutateDNA(dna)
265 {
266 var rand = Math.random();
267
268 if(rand < 0.1)
269 AddRandomCell(dna);
270
271 if(rand < 0.2)
272 AddRandomSynapse(dna);
273
274 if(rand < 0.7)
275 MutateSynapses(dna, 1 - rand);
276 }
277
278 function MutateSynapses(dna, strength)
279 {
280 for(var i = 0; i < dna.length; i++)
281 {
282 if(dna[i].inputs.length > 0)
283 {
284 for(var o = 0; o < dna[i].inputs.length; o++)
285 {
286 dna[i].inputs[o].strength += (-0.5 + Math.random()) * strength;
287 }
288 }
289 }
290 }
291
292 function AddRandomSynapse(dna, att = 0)
293 {
294 if(att > 10)
295 return; //can't be done, give up
296
297 var receivers = [];
298 var senders = [];
299
300 for(var i = 0; i < dna.length; i++)
301 {
302 if(dna[i].type == TYPES.BRAIN)
303 {
304 receivers.push(i);
305 senders.push(i);
306 }
307
308 if(dna[i].type == TYPES.EYE)
309 {
310 senders.push(i);
311 }
312
313 if(dna[i].type == TYPES.LOCOMOTOR || dna[i].type == TYPES.ROTATOR)
314 {
315 receivers.push(i);
316 }
317 }
318
319 var ri = Math.floor(Math.random() * receivers.length);
320 var si = Math.floor(Math.random() * senders.length)
321
322 var tri = receivers[ri]
323 var tsi = senders[si];
324
325 var OK = true
326
327 for(var i = 0; i < dna[tri].inputs.length; i++)
328 {
329 if(dna[tri].inputs[i].id == dna[tsi].id)
330 OK = false; //duplicate
331 }
332
333 if(ri != si && ri != 0 && si != 0 && OK) //todo: check for duplicates
334 {
335 dna[tri].inputs.push({id:dna[tsi].id, strength: 0.5 + Math.random()});
336 }
337 else
338 {
339 AddRandomSynapse(dna, att + 1); //re-try
340 }
341 }
342
343 function AddRandomCell(dna)
170 344 {
171 345 var totalWeight = 0;
172 346 var weights = Object.values(Weights);
@@ -192,9 +366,9 @@ function AddRandomCell(creature)
192 366
193 367 var id = 1;
194 368
195 for(var i = 0; i < creature.length; i++)
369 for(var i = 0; i < dna.length; i++)
196 370 {
197 while(id == creature[i].id)
371 while(id == dna[i].id)
198 372 id++;
199 373 }
200 374
@@ -205,9 +379,9 @@ function AddRandomCell(creature)
205 379 {
206 380 done = true;
207 381
208 for(var i = 0; i < creature.length; i++)
382 for(var i = 0; i < dna.length; i++)
209 383 {
210 if(pos.x == creature[i].pos.x && pos.y == creature[i].pos.y)
384 if(pos.x == dna[i].pos.x && pos.y == dna[i].pos.y)
211 385 {
212 386 done = false;
213 387 var val = Math.random() > 0.5? -1 : 1;
@@ -227,7 +401,7 @@ function AddRandomCell(creature)
227 401 pos: pos
228 402 }
229 403
230 creature.push(cell);
404 dna.push(cell);
231 405 }
232 406
233 407 var TYPES =
@@ -434,6 +608,7 @@ var gemSpawner =
434 608
435 609 var s_head = new image("images/head.png", 4, 4);
436 610 var s_orb = new image("images/orb.png", 8, 4);
611 var s_cell = new image("images/cells.png", 8, 4);
437 612 var s_gem = new image("images/gems.png", 72, 6);
438 613
439 614 /**
@@ -476,7 +651,9 @@ function gameStart()
476 651 function update(deltaTime)
477 652 {
478 653
479 }
654 }
655
656
480 657
481 658 /**
482 659 * All sounds in the game
@@ -6,6 +6,9 @@ var cell =
6{
6{
7 localPos: new vector(0, 0),
7 localPos: new vector(0, 0),
8 parentCreature: null,
8 parentCreature: null,
9 cellRef: -1,
10 currentSensorSignal: 0, //only used for sensors, else forever zero
11 lastSum: 0, //used for debug purposes
9
12
10 start: function()
13 start: function()
11 {
14 {
@@ -21,6 +24,14 @@ var cell =
21 scaleUpTween.start();
24 scaleUpTween.start();
22 },
25 },
23
26
27 draw: function()
28 {
29 this.defaultDraw();
30
31 GameWindow.canvasContext.fillStyle = 'black';
32 GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);
33 },
34
24 update: function()
35 update: function()
25 {
36 {
26 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
37 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
@@ -51,20 +62,99 @@ var creature =
51 {
62 {
52 for(var i = 0; i < this.dna.length; i++)
63 for(var i = 0; i < this.dna.length; i++)
53 {
64 {
54 var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_orb, 500);
65 var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_cell, 400);
55
66
56 newCell.setType(this.dna[i].type);
67 newCell.setType(this.dna[i].type);
57 newCell.parentCreature = this;
68 newCell.parentCreature = this;
58 newCell.localPos = new vector(this.dna[i].pos.x * 32, this.dna[i].pos.y * 32)
69 newCell.localPos = new vector(this.dna[i].pos.x * 32, this.dna[i].pos.y * 32)
59
70
71 newCell.cellRef = i;
72
60 this.cellInstances.push(newCell);
73 this.cellInstances.push(newCell);
61 }
74 }
62 },
75 },
63
76
64 update: function(deltaTime)
77 update: function(deltaTime)
65 {
78 {
66 this.transform.rotation += deltaTime * 60;
79 for(var i = this.cellInstances.length -1; i >= 0; i--) //reverse loop for deletion of references to destroyed cells
67 this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))
80 {
81 if(this.cellInstances[i].destroyed)
82 this.cellInstances.splice(i, 1);
83 }
84
85 for(var i = 0; i < this.cellInstances.length; i++) //loop through instances instead of dna so that destroyed cells are immediately ignored
86 {
87 var type = this.dna[this.cellInstances[i].cellRef].type;
88
89 if(type == TYPES.LOCOMOTOR || type == TYPES.ROTATOR)
90 {
91 if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)
92 {
93 var sum = sigmoid(this.getCellInputSum(this.cellInstances[i]));
94 this.cellInstances[i].lastSum = sum;
95
96 //TODO: Act on the sum we've received (ALWAYS between 0 and 1, but VERY rarely exactly 0 or 1)
97 }
98 }
99 }
100
101 //this.transform.rotation += deltaTime * 60;
102 //this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))
103 },
104
105 draw: function()
106 {
107 for(var i = 0; i < this.cellInstances.length; i++)
108 {
109 if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)
110 {
111 for(var c = 0; c < this.dna[this.cellInstances[i].cellRef].inputs.length; c++)
112 {
113 var pos = this.findCellInstanceByCellId(this.dna[this.cellInstances[i].cellRef].inputs[c].id).transform.position;
114
115 GameWindow.canvasContext.strokeStyle = 'red';
116 GameWindow.canvasContext.beginPath();
117 GameWindow.canvasContext.moveTo(this.cellInstances[i].transform.position.x, this.cellInstances[i].transform.position.y);
118 GameWindow.canvasContext.lineTo(pos.x, pos.y);
119 GameWindow.canvasContext.stroke();
120 }
121 }
122 }
123 },
124
125 getCellInputSum: function(cellInstance, tracer = [])
126 {
127 var cellId = this.dna[cellInstance.cellRef].id;
128
129 if(tracer.indexOf(cellId) >= 0) //is my id already in the tracer?
130 return 0;
131
132 tracer.push(cellId)
133
134 sum = cellInstance.currentSensorSignal;
135
136 if(this.dna[cellInstance.cellRef].inputs.length > 0)
137 {
138 for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)
139 {
140 sum += sigmoid(this.getCellInputSum(
141 this.findCellInstanceByCellId(this.dna[cellInstance.cellRef].inputs[i].id)
142 , tracer) * this.dna[cellInstance.cellRef].inputs[i].weight);
143 }
144 }
145
146 cellInstance.lastSum = sum;
147
148 return sum;
149 },
150
151 findCellInstanceByCellId: function(id)
152 {
153 for(var i = 0; i < this.cellInstances.length; i++)
154 {
155 if(this.dna[this.cellInstances[i].cellRef].id == id)
156 return this.cellInstances[i];
157 }
68 },
158 },
69
159
70 onDestroy: function()
160 onDestroy: function()
@@ -101,7 +191,7 @@ var creature =
101 *
191 *
102 * Types of cells:
192 * Types of cells:
103 * ACTIVATORS
193 * ACTIVATORS
104 * - Mouth: can Bite, which will eat whatever is in front of it, be that a cell or a food pellet
194 * - Mouth: can Bite, which will eat whatever is in front of it, be that a cell or a food pellet (TEMP: automatically works without input for now)
105 * - Locomotor: When activated, pushes the creature in the direction of the creature's center, relative from the locomotor cell
195 * - Locomotor: When activated, pushes the creature in the direction of the creature's center, relative from the locomotor cell
106 * - Rotator: Applies torque when activated. 0.5 is neural, 0 is full left, 1 is full right
196 * - Rotator: Applies torque when activated. 0.5 is neural, 0 is full left, 1 is full right
107 *
197 *
@@ -163,10 +253,94 @@ function GenerateNewDNA(startingCells = 10)
163 for(var i = 0; i < startingCells; i++)
253 for(var i = 0; i < startingCells; i++)
164 AddRandomCell(dna)
254 AddRandomCell(dna)
165
255
256 for(var i = 0; i < startingCells; i++)
257 AddRandomSynapse(dna)
258
259 dna[0].inputs = [];
260
166 return dna;
261 return dna;
167}
262}
168
263
169function AddRandomCell(creature)
264function MutateDNA(dna)
265{
266 var rand = Math.random();
267
268 if(rand < 0.1)
269 AddRandomCell(dna);
270
271 if(rand < 0.2)
272 AddRandomSynapse(dna);
273
274 if(rand < 0.7)
275 MutateSynapses(dna, 1 - rand);
276}
277
278function MutateSynapses(dna, strength)
279{
280 for(var i = 0; i < dna.length; i++)
281 {
282 if(dna[i].inputs.length > 0)
283 {
284 for(var o = 0; o < dna[i].inputs.length; o++)
285 {
286 dna[i].inputs[o].strength += (-0.5 + Math.random()) * strength;
287 }
288 }
289 }
290}
291
292function AddRandomSynapse(dna, att = 0)
293{
294 if(att > 10)
295 return; //can't be done, give up
296
297 var receivers = [];
298 var senders = [];
299
300 for(var i = 0; i < dna.length; i++)
301 {
302 if(dna[i].type == TYPES.BRAIN)
303 {
304 receivers.push(i);
305 senders.push(i);
306 }
307
308 if(dna[i].type == TYPES.EYE)
309 {
310 senders.push(i);
311 }
312
313 if(dna[i].type == TYPES.LOCOMOTOR || dna[i].type == TYPES.ROTATOR)
314 {
315 receivers.push(i);
316 }
317 }
318
319 var ri = Math.floor(Math.random() * receivers.length);
320 var si = Math.floor(Math.random() * senders.length)
321
322 var tri = receivers[ri]
323 var tsi = senders[si];
324
325 var OK = true
326
327 for(var i = 0; i < dna[tri].inputs.length; i++)
328 {
329 if(dna[tri].inputs[i].id == dna[tsi].id)
330 OK = false; //duplicate
331 }
332
333 if(ri != si && ri != 0 && si != 0 && OK) //todo: check for duplicates
334 {
335 dna[tri].inputs.push({id:dna[tsi].id, strength: 0.5 + Math.random()});
336 }
337 else
338 {
339 AddRandomSynapse(dna, att + 1); //re-try
340 }
341}
342
343function AddRandomCell(dna)
170{
344{
171 var totalWeight = 0;
345 var totalWeight = 0;
172 var weights = Object.values(Weights);
346 var weights = Object.values(Weights);
@@ -192,9 +366,9 @@ function AddRandomCell(creature)
192
366
193 var id = 1;
367 var id = 1;
194
368
195 for(var i = 0; i < creature.length; i++)
369 for(var i = 0; i < dna.length; i++)
196 {
370 {
197 while(id == creature[i].id)
371 while(id == dna[i].id)
198 id++;
372 id++;
199 }
373 }
200
374
@@ -205,9 +379,9 @@ function AddRandomCell(creature)
205 {
379 {
206 done = true;
380 done = true;
207
381
208 for(var i = 0; i < creature.length; i++)
382 for(var i = 0; i < dna.length; i++)
209 {
383 {
210 if(pos.x == creature[i].pos.x && pos.y == creature[i].pos.y)
384 if(pos.x == dna[i].pos.x && pos.y == dna[i].pos.y)
211 {
385 {
212 done = false;
386 done = false;
213 var val = Math.random() > 0.5? -1 : 1;
387 var val = Math.random() > 0.5? -1 : 1;
@@ -227,7 +401,7 @@ function AddRandomCell(creature)
227 pos: pos
401 pos: pos
228 }
402 }
229
403
230 creature.push(cell);
404 dna.push(cell);
231}
405}
232
406
233var TYPES =
407var TYPES =
@@ -434,6 +608,7 @@ var gemSpawner =
434
608
435var s_head = new image("images/head.png", 4, 4);
609var s_head = new image("images/head.png", 4, 4);
436var s_orb = new image("images/orb.png", 8, 4);
610var s_orb = new image("images/orb.png", 8, 4);
611var s_cell = new image("images/cells.png", 8, 4);
437var s_gem = new image("images/gems.png", 72, 6);
612var s_gem = new image("images/gems.png", 72, 6);
438
613
439/**
614/**
@@ -476,7 +651,9 @@ function gameStart()
476function update(deltaTime)
651function update(deltaTime)
477{
652{
478
653
479}
654}
655
656
480
657
481/**
658/**
482 * All sounds in the game
659 * All sounds in the game
js/lastbuild.txt +21 −0
@@ -37,3 +37,24 @@
37 37 -12-10-2021- 21:19
38 38 -12-10-2021- 21:20
39 39 -12-10-2021- 21:21
40 -13-10-2021- 12:23
41 -13-10-2021- 12:23
42 -13-10-2021- 12:24
43 -13-10-2021- 12:25
44 -13-10-2021- 12:25
45 -13-10-2021- 12:26
46 -13-10-2021- 12:29
47 -13-10-2021- 12:31
48 -13-10-2021- 12:33
49 -13-10-2021- 12:35
50 -13-10-2021- 12:41
51 -13-10-2021- 12:41
52 -13-10-2021- 12:42
53 -13-10-2021- 12:42
54 -13-10-2021- 12:42
55 -13-10-2021- 12:44
56 -13-10-2021- 12:51
57 -13-10-2021- 12:51
58 -13-10-2021- 12:56
59 -13-10-2021- 13:00
60 -13-10-2021- 13:14
@@ -37,3 +37,24 @@
37-12-10-2021- 21:19
37-12-10-2021- 21:19
38-12-10-2021- 21:20
38-12-10-2021- 21:20
39-12-10-2021- 21:21
39-12-10-2021- 21:21
40-13-10-2021- 12:23
41-13-10-2021- 12:23
42-13-10-2021- 12:24
43-13-10-2021- 12:25
44-13-10-2021- 12:25
45-13-10-2021- 12:26
46-13-10-2021- 12:29
47-13-10-2021- 12:31
48-13-10-2021- 12:33
49-13-10-2021- 12:35
50-13-10-2021- 12:41
51-13-10-2021- 12:41
52-13-10-2021- 12:42
53-13-10-2021- 12:42
54-13-10-2021- 12:42
55-13-10-2021- 12:44
56-13-10-2021- 12:51
57-13-10-2021- 12:51
58-13-10-2021- 12:56
59-13-10-2021- 13:00
60-13-10-2021- 13:14
src/cell.js +9 −0
@@ -8,6 +8,7 @@ var cell =
8 8 parentCreature: null,
9 9 cellRef: -1,
10 10 currentSensorSignal: 0, //only used for sensors, else forever zero
11 lastSum: 0, //used for debug purposes
11 12
12 13 start: function()
13 14 {
@@ -23,6 +24,14 @@ var cell =
23 24 scaleUpTween.start();
24 25 },
25 26
27 draw: function()
28 {
29 this.defaultDraw();
30
31 GameWindow.canvasContext.fillStyle = 'black';
32 GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);
33 },
34
26 35 update: function()
27 36 {
28 37 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
@@ -8,6 +8,7 @@ var cell =
8 parentCreature: null,
8 parentCreature: null,
9 cellRef: -1,
9 cellRef: -1,
10 currentSensorSignal: 0, //only used for sensors, else forever zero
10 currentSensorSignal: 0, //only used for sensors, else forever zero
11 lastSum: 0, //used for debug purposes
11
12
12 start: function()
13 start: function()
13 {
14 {
@@ -23,6 +24,14 @@ var cell =
23 scaleUpTween.start();
24 scaleUpTween.start();
24 },
25 },
25
26
27 draw: function()
28 {
29 this.defaultDraw();
30
31 GameWindow.canvasContext.fillStyle = 'black';
32 GameWindow.canvasContext.fillText(this.lastSum, this.transform.position.x, this.transform.position.y);
33 },
34
26 update: function()
35 update: function()
27 {
36 {
28 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
37 var rotated = this.localPos.rotate(this.parentCreature.transform.rotation);
src/creature.js +33 −8
@@ -16,7 +16,7 @@ var creature =
16 16 {
17 17 for(var i = 0; i < this.dna.length; i++)
18 18 {
19 var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_orb, 500);
19 var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_cell, 400);
20 20
21 21 newCell.setType(this.dna[i].type);
22 22 newCell.parentCreature = this;
@@ -38,13 +38,16 @@ var creature =
38 38
39 39 for(var i = 0; i < this.cellInstances.length; i++) //loop through instances instead of dna so that destroyed cells are immediately ignored
40 40 {
41 var type = this.dna[this.cellInstances[i]].type;
41 var type = this.dna[this.cellInstances[i].cellRef].type;
42 42
43 43 if(type == TYPES.LOCOMOTOR || type == TYPES.ROTATOR)
44 44 {
45 45 if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)
46 46 {
47 var sum = this.getCellInputSum(this.cellInstances[i]);
47 var sum = sigmoid(this.getCellInputSum(this.cellInstances[i]));
48 this.cellInstances[i].lastSum = sum;
49
50 //TODO: Act on the sum we've received (ALWAYS between 0 and 1, but VERY rarely exactly 0 or 1)
48 51 }
49 52 }
50 53 }
@@ -53,9 +56,29 @@ var creature =
53 56 //this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))
54 57 },
55 58
59 draw: function()
60 {
61 for(var i = 0; i < this.cellInstances.length; i++)
62 {
63 if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)
64 {
65 for(var c = 0; c < this.dna[this.cellInstances[i].cellRef].inputs.length; c++)
66 {
67 var pos = this.findCellInstanceByCellId(this.dna[this.cellInstances[i].cellRef].inputs[c].id).transform.position;
68
69 GameWindow.canvasContext.strokeStyle = 'red';
70 GameWindow.canvasContext.beginPath();
71 GameWindow.canvasContext.moveTo(this.cellInstances[i].transform.position.x, this.cellInstances[i].transform.position.y);
72 GameWindow.canvasContext.lineTo(pos.x, pos.y);
73 GameWindow.canvasContext.stroke();
74 }
75 }
76 }
77 },
78
56 79 getCellInputSum: function(cellInstance, tracer = [])
57 80 {
58 var cellId = this.dna[this.cellInstances[i].cellRef].id;
81 var cellId = this.dna[cellInstance.cellRef].id;
59 82
60 83 if(tracer.indexOf(cellId) >= 0) //is my id already in the tracer?
61 84 return 0;
@@ -68,16 +91,18 @@ var creature =
68 91 {
69 92 for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)
70 93 {
71 sum += this.getCellInputSum(
72 this.findCellByCellInstanceId(this.dna[cellInstance.cellRef].inputs[i].id)
73 );
94 sum += sigmoid(this.getCellInputSum(
95 this.findCellInstanceByCellId(this.dna[cellInstance.cellRef].inputs[i].id)
96 , tracer) * this.dna[cellInstance.cellRef].inputs[i].weight);
74 97 }
75 98 }
76 99
100 cellInstance.lastSum = sum;
101
77 102 return sum;
78 103 },
79 104
80 findCellByCellInstanceId: function(id)
105 findCellInstanceByCellId: function(id)
81 106 {
82 107 for(var i = 0; i < this.cellInstances.length; i++)
83 108 {
@@ -16,7 +16,7 @@ var creature =
16 {
16 {
17 for(var i = 0; i < this.dna.length; i++)
17 for(var i = 0; i < this.dna.length; i++)
18 {
18 {
19 var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_orb, 500);
19 var newCell = GameObject.create(cell, this.transform.position.x + (this.dna[i].pos.x * 32), this.transform.position.y + (this.dna[i].pos.y * 32), s_cell, 400);
20
20
21 newCell.setType(this.dna[i].type);
21 newCell.setType(this.dna[i].type);
22 newCell.parentCreature = this;
22 newCell.parentCreature = this;
@@ -38,13 +38,16 @@ var creature =
38
38
39 for(var i = 0; i < this.cellInstances.length; i++) //loop through instances instead of dna so that destroyed cells are immediately ignored
39 for(var i = 0; i < this.cellInstances.length; i++) //loop through instances instead of dna so that destroyed cells are immediately ignored
40 {
40 {
41 var type = this.dna[this.cellInstances[i]].type;
41 var type = this.dna[this.cellInstances[i].cellRef].type;
42
42
43 if(type == TYPES.LOCOMOTOR || type == TYPES.ROTATOR)
43 if(type == TYPES.LOCOMOTOR || type == TYPES.ROTATOR)
44 {
44 {
45 if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)
45 if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)
46 {
46 {
47 var sum = this.getCellInputSum(this.cellInstances[i]);
47 var sum = sigmoid(this.getCellInputSum(this.cellInstances[i]));
48 this.cellInstances[i].lastSum = sum;
49
50 //TODO: Act on the sum we've received (ALWAYS between 0 and 1, but VERY rarely exactly 0 or 1)
48 }
51 }
49 }
52 }
50 }
53 }
@@ -53,9 +56,29 @@ var creature =
53 //this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))
56 //this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))
54 },
57 },
55
58
59 draw: function()
60 {
61 for(var i = 0; i < this.cellInstances.length; i++)
62 {
63 if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)
64 {
65 for(var c = 0; c < this.dna[this.cellInstances[i].cellRef].inputs.length; c++)
66 {
67 var pos = this.findCellInstanceByCellId(this.dna[this.cellInstances[i].cellRef].inputs[c].id).transform.position;
68
69 GameWindow.canvasContext.strokeStyle = 'red';
70 GameWindow.canvasContext.beginPath();
71 GameWindow.canvasContext.moveTo(this.cellInstances[i].transform.position.x, this.cellInstances[i].transform.position.y);
72 GameWindow.canvasContext.lineTo(pos.x, pos.y);
73 GameWindow.canvasContext.stroke();
74 }
75 }
76 }
77 },
78
56 getCellInputSum: function(cellInstance, tracer = [])
79 getCellInputSum: function(cellInstance, tracer = [])
57 {
80 {
58 var cellId = this.dna[this.cellInstances[i].cellRef].id;
81 var cellId = this.dna[cellInstance.cellRef].id;
59
82
60 if(tracer.indexOf(cellId) >= 0) //is my id already in the tracer?
83 if(tracer.indexOf(cellId) >= 0) //is my id already in the tracer?
61 return 0;
84 return 0;
@@ -68,16 +91,18 @@ var creature =
68 {
91 {
69 for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)
92 for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)
70 {
93 {
71 sum += this.getCellInputSum(
94 sum += sigmoid(this.getCellInputSum(
72 this.findCellByCellInstanceId(this.dna[cellInstance.cellRef].inputs[i].id)
95 this.findCellInstanceByCellId(this.dna[cellInstance.cellRef].inputs[i].id)
73 );
96 , tracer) * this.dna[cellInstance.cellRef].inputs[i].weight);
74 }
97 }
75 }
98 }
76
99
100 cellInstance.lastSum = sum;
101
77 return sum;
102 return sum;
78 },
103 },
79
104
80 findCellByCellInstanceId: function(id)
105 findCellInstanceByCellId: function(id)
81 {
106 {
82 for(var i = 0; i < this.cellInstances.length; i++)
107 for(var i = 0; i < this.cellInstances.length; i++)
83 {
108 {
src/dnastring.js +15 −2
@@ -85,6 +85,8 @@ function GenerateNewDNA(startingCells = 10)
85 85 for(var i = 0; i < startingCells; i++)
86 86 AddRandomSynapse(dna)
87 87
88 dna[0].inputs = [];
89
88 90 return dna;
89 91 }
90 92
@@ -146,9 +148,20 @@ function AddRandomSynapse(dna, att = 0)
146 148 var ri = Math.floor(Math.random() * receivers.length);
147 149 var si = Math.floor(Math.random() * senders.length)
148 150
149 if(true) //todo: check for duplicates
151 var tri = receivers[ri]
152 var tsi = senders[si];
153
154 var OK = true
155
156 for(var i = 0; i < dna[tri].inputs.length; i++)
157 {
158 if(dna[tri].inputs[i].id == dna[tsi].id)
159 OK = false; //duplicate
160 }
161
162 if(ri != si && ri != 0 && si != 0 && OK) //todo: check for duplicates
150 163 {
151 dna[ri].inputs.push({id:dna[si].id, strength: 0.5 + Math.random()});
164 dna[tri].inputs.push({id:dna[tsi].id, strength: 0.5 + Math.random()});
152 165 }
153 166 else
154 167 {
@@ -85,6 +85,8 @@ function GenerateNewDNA(startingCells = 10)
85 for(var i = 0; i < startingCells; i++)
85 for(var i = 0; i < startingCells; i++)
86 AddRandomSynapse(dna)
86 AddRandomSynapse(dna)
87
87
88 dna[0].inputs = [];
89
88 return dna;
90 return dna;
89}
91}
90
92
@@ -146,9 +148,20 @@ function AddRandomSynapse(dna, att = 0)
146 var ri = Math.floor(Math.random() * receivers.length);
148 var ri = Math.floor(Math.random() * receivers.length);
147 var si = Math.floor(Math.random() * senders.length)
149 var si = Math.floor(Math.random() * senders.length)
148
150
149 if(true) //todo: check for duplicates
151 var tri = receivers[ri]
152 var tsi = senders[si];
153
154 var OK = true
155
156 for(var i = 0; i < dna[tri].inputs.length; i++)
157 {
158 if(dna[tri].inputs[i].id == dna[tsi].id)
159 OK = false; //duplicate
160 }
161
162 if(ri != si && ri != 0 && si != 0 && OK) //todo: check for duplicates
150 {
163 {
151 dna[ri].inputs.push({id:dna[si].id, strength: 0.5 + Math.random()});
164 dna[tri].inputs.push({id:dna[tsi].id, strength: 0.5 + Math.random()});
152 }
165 }
153 else
166 else
154 {
167 {
src/images.js +1 −0
@@ -4,4 +4,5 @@
4 4
5 5 var s_head = new image("images/head.png", 4, 4);
6 6 var s_orb = new image("images/orb.png", 8, 4);
7 var s_cell = new image("images/cells.png", 8, 4);
7 8 var s_gem = new image("images/gems.png", 72, 6);
@@ -4,4 +4,5 @@
4
4
5var s_head = new image("images/head.png", 4, 4);
5var s_head = new image("images/head.png", 4, 4);
6var s_orb = new image("images/orb.png", 8, 4);
6var s_orb = new image("images/orb.png", 8, 4);
7var s_cell = new image("images/cells.png", 8, 4);
7var s_gem = new image("images/gems.png", 72, 6);
8var s_gem = new image("images/gems.png", 72, 6);
src/main.js +2 −1
@@ -38,4 +38,5 @@ function gameStart()
38 38 function update(deltaTime)
39 39 {
40 40
41 }
41 }
42
@@ -38,4 +38,5 @@ function gameStart()
38function update(deltaTime)
38function update(deltaTime)
39{
39{
40
40
41}
41}
42