BlueGit

blue-star cellular-evolution

add network signal traversal (untested)

timfa Tim · pushed by timfa

Authored 2021-10-13 · 5 years ago

Hash
797025828b1dc943ee418a4c04892746e4bddb20
Parent
beb2dd8d
2 files changed +58 −2
src/cell.js +2 −0
@@ -6,6 +6,8 @@ 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
9 11
10 12 start: function()
11 13 {
@@ -6,6 +6,8 @@ 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
9
11
10 start: function()
12 start: function()
11 {
13 {
src/creature.js +56 −2
@@ -22,14 +22,68 @@ var creature =
22 22 newCell.parentCreature = this;
23 23 newCell.localPos = new vector(this.dna[i].pos.x * 32, this.dna[i].pos.y * 32)
24 24
25 newCell.cellRef = i;
26
25 27 this.cellInstances.push(newCell);
26 28 }
27 29 },
28 30
29 31 update: function(deltaTime)
30 32 {
31 this.transform.rotation += deltaTime * 60;
32 this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))
33 for(var i = this.cellInstances.length -1; i >= 0; i--) //reverse loop for deletion of references to destroyed cells
34 {
35 if(this.cellInstances[i].destroyed)
36 this.cellInstances.splice(i, 1);
37 }
38
39 for(var i = 0; i < this.cellInstances.length; i++) //loop through instances instead of dna so that destroyed cells are immediately ignored
40 {
41 var type = this.dna[this.cellInstances[i]].type;
42
43 if(type == TYPES.LOCOMOTOR || type == TYPES.ROTATOR)
44 {
45 if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)
46 {
47 var sum = this.getCellInputSum(this.cellInstances[i]);
48 }
49 }
50 }
51
52 //this.transform.rotation += deltaTime * 60;
53 //this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))
54 },
55
56 getCellInputSum: function(cellInstance, tracer = [])
57 {
58 var cellId = this.dna[this.cellInstances[i].cellRef].id;
59
60 if(tracer.indexOf(cellId) >= 0) //is my id already in the tracer?
61 return 0;
62
63 tracer.push(cellId)
64
65 sum = cellInstance.currentSensorSignal;
66
67 if(this.dna[cellInstance.cellRef].inputs.length > 0)
68 {
69 for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)
70 {
71 sum += this.getCellInputSum(
72 this.findCellByCellInstanceId(this.dna[cellInstance.cellRef].inputs[i].id)
73 );
74 }
75 }
76
77 return sum;
78 },
79
80 findCellByCellInstanceId: function(id)
81 {
82 for(var i = 0; i < this.cellInstances.length; i++)
83 {
84 if(this.dna[this.cellInstances[i].cellRef].id == id)
85 return this.cellInstances[i];
86 }
33 87 },
34 88
35 89 onDestroy: function()
@@ -22,14 +22,68 @@ var creature =
22 newCell.parentCreature = this;
22 newCell.parentCreature = this;
23 newCell.localPos = new vector(this.dna[i].pos.x * 32, this.dna[i].pos.y * 32)
23 newCell.localPos = new vector(this.dna[i].pos.x * 32, this.dna[i].pos.y * 32)
24
24
25 newCell.cellRef = i;
26
25 this.cellInstances.push(newCell);
27 this.cellInstances.push(newCell);
26 }
28 }
27 },
29 },
28
30
29 update: function(deltaTime)
31 update: function(deltaTime)
30 {
32 {
31 this.transform.rotation += deltaTime * 60;
33 for(var i = this.cellInstances.length -1; i >= 0; i--) //reverse loop for deletion of references to destroyed cells
32 this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))
34 {
35 if(this.cellInstances[i].destroyed)
36 this.cellInstances.splice(i, 1);
37 }
38
39 for(var i = 0; i < this.cellInstances.length; i++) //loop through instances instead of dna so that destroyed cells are immediately ignored
40 {
41 var type = this.dna[this.cellInstances[i]].type;
42
43 if(type == TYPES.LOCOMOTOR || type == TYPES.ROTATOR)
44 {
45 if(this.dna[this.cellInstances[i].cellRef].inputs.length > 0)
46 {
47 var sum = this.getCellInputSum(this.cellInstances[i]);
48 }
49 }
50 }
51
52 //this.transform.rotation += deltaTime * 60;
53 //this.transform.position = this.transform.position.add(this.transform.forward().stretch(deltaTime * 250))
54 },
55
56 getCellInputSum: function(cellInstance, tracer = [])
57 {
58 var cellId = this.dna[this.cellInstances[i].cellRef].id;
59
60 if(tracer.indexOf(cellId) >= 0) //is my id already in the tracer?
61 return 0;
62
63 tracer.push(cellId)
64
65 sum = cellInstance.currentSensorSignal;
66
67 if(this.dna[cellInstance.cellRef].inputs.length > 0)
68 {
69 for(var i = 0; i < this.dna[cellInstance.cellRef].inputs.length; i++)
70 {
71 sum += this.getCellInputSum(
72 this.findCellByCellInstanceId(this.dna[cellInstance.cellRef].inputs[i].id)
73 );
74 }
75 }
76
77 return sum;
78 },
79
80 findCellByCellInstanceId: function(id)
81 {
82 for(var i = 0; i < this.cellInstances.length; i++)
83 {
84 if(this.dna[this.cellInstances[i].cellRef].id == id)
85 return this.cellInstances[i];
86 }
33 },
87 },
34
88
35 onDestroy: function()
89 onDestroy: function()