@@ -4,43 +4,51 @@ function addCell(c)
4
4
table.insert (cells ,c )
5
5
end
6
6
7
- function newCell (x , y )
8
- local cell = { }
9
- cell . nucleus = { x = x , y = y , vx = 0 , vy = 0 , ax = 0 , ay = 0 }
7
+ function newNode (x ,y , vx , vy , ax , ay )
8
+ return { x = x , y = y , vx = vx , vy = vy , ax = ax , ay = ay }
9
+ end
10
10
11
- cell .membrane = {}
12
- for i = 1 , 23 , 2 do
13
- cell .membrane [i ] = x + 50 * math.cos (math.rad (15 * i ))
14
- cell .membrane [i + 1 ] = y + 50 * math.sin (math.rad (15 * i ))
15
- end
16
- cell .mbsize = table .getn (cell .membrane )
11
+ function insertNode (m ,idx ,n )
12
+ table.insert (m ,idx ,n )
13
+ end
17
14
18
- cell .springs = {}
19
- for i = 1 ,13 do cell .springs [i ] = fNucSpringLength (cell ) end
20
- cell .membraneVel = { }
21
- for i = 1 ,cell .mbsize do
22
- cell .membraneVel [i ] = 0 -- set initial velocities to zero
23
- end
24
- -- membraneVel[1] = 0.5
25
- -- membraneVel[2] = 0.5
26
- cell .membraneAcc = { }
27
- for i = 1 ,cell .mbsize do
28
- cell .membraneAcc [i ] = 0 -- set initial acceleration to zero
29
- end
30
- cell .genes = {}
31
- cell .genes .growtime = 2 -- 1 --time to grow a new node, in seconds
32
- cell .genes .splitnodes = 18 -- 18 --# membrane nodes to divide at
33
- cell .genes .speed = 15 -- 15 --movement speed
34
- cell .genes .attackdist = 100 -- how close it has to be to player to attack
35
- cell .genes .bombgrav = 0 -- how attracted (+) or repelled (-) it is by bombs
36
- cell .genes .attackstyle = " bump" -- either "bump" or "engulf"
37
- cell .genes .acidity = 0 -- how much player is damaged when inside cell
38
- cell .genes .damagestyle = " shrink" -- either "shrink" or "split"
39
-
40
- cell .gtimer = 0 -- in seconds
41
- cell .dir = math.random ()* 2 * math.pi -- movement direction
15
+ function newCell (x , y )
16
+ local cell = {}
17
+ cell .nucleus = {x = x ,y = y ,vx = 0 ,vy = 0 ,ax = 0 ,ay = 0 }
18
+
19
+ cell .membrane = {}
20
+ for i = 1 ,13 do
21
+ cell .membrane [i ] = {x = x + 50 * math.cos (math.rad (15 * i )),y = y + 50 * math.sin (math.rad (15 * i )),vx = 0 ,vy = 0 ,ax = 0 ,ay = 0 }
22
+ -- cell.membrane[i] = x+50*math.cos(math.rad(15*i))
23
+ -- cell.membrane[i+1] = y+50*math.sin(math.rad(15*i))
24
+ end
25
+ cell .mbsize = table .getn (cell .membrane )
42
26
43
- return cell
27
+ cell .springs = {}
28
+ for i = 1 ,13 do cell .springs [i ] = fNucSpringLength (cell ) end
29
+ cell .membraneVel = { }
30
+ for i = 1 ,cell .mbsize do
31
+ cell .membraneVel [i ] = 0 -- set initial velocities to zero
32
+ end
33
+ -- membraneVel[1] = 0.5
34
+ -- membraneVel[2] = 0.5
35
+ cell .membraneAcc = { }
36
+ for i = 1 ,cell .mbsize do
37
+ cell .membraneAcc [i ] = 0 -- set initial acceleration to zero
38
+ end
39
+ cell .genes = {}
40
+ cell .genes .growtime = 2 -- 1 --time to grow a new node, in seconds
41
+ cell .genes .splitnodes = 18 -- 18 --# membrane nodes to divide at
42
+ cell .genes .speed = 15 -- 15 --movement speed
43
+ cell .genes .attackdist = 100 -- how close it has to be to player to attack
44
+ cell .genes .bombgrav = 0 -- how attracted (+) or repelled (-) it is by bombs
45
+ cell .genes .attackstyle = " bump" -- either "bump" or "engulf"
46
+ cell .genes .acidity = 0 -- how much player is damaged when inside cell
47
+ cell .genes .damagestyle = " shrink" -- either "shrink" or "split"
48
+
49
+ cell .gtimer = 0 -- in seconds
50
+ cell .dir = math.random ()* 2 * math.pi -- movement direction
51
+ return cell
44
52
end
45
53
46
54
function mutate (c )
82
90
83
91
function mitosis (_n )
84
92
local c = cells [_n ]
85
- --[[ io.write("c: {")
86
- for itr = 1,c.mbsize do io.write(c.membrane[itr]," ") end
87
- io.write("}\n")]]
88
- local split = (c .mbsize / 2 )+ 1 -- divide in two; currently just midpoint; should do random split maybe?
89
- print (" split:" ,split )
93
+ local oldsize = table .getn (c .membrane )
94
+ local split = oldsize / 2
90
95
local oldc = c
91
- io.write (" oldc: {" )
92
- for itr = 1 ,oldc .mbsize do io.write (oldc .membrane [itr ]," " ) end
93
- io.write (" }\n " )
94
96
c = {}
95
97
c .membrane = {}
96
- c .membraneVel = {}
97
- c .membraneAcc = {}
98
- c .springs = {}
99
98
c .nucleus = {}
99
+
100
100
local newcell = {}
101
101
newcell .membrane = {}
102
- newcell .membraneVel = {}
103
- newcell .membraneAcc = {}
104
- newcell .springs = {}
105
102
newcell .nucleus = {}
103
+
106
104
for i = 1 ,split - 1 do
107
105
c .membrane [i ] = oldc .membrane [i ]
108
- c .membraneVel [i ] = oldc .membraneVel [i ]
109
- c .membraneAcc [i ] = oldc .membraneAcc [i ]
110
106
end
111
- for i = split ,oldc .mbsize do
107
+ table.insert (c .membrane ,table .copy (oldc .nucleus ))
108
+
109
+ for i = split ,oldsize do
112
110
newcell .membrane [i + 1 - split ] = oldc .membrane [i ]
113
- newcell .membraneVel [i + 1 - split ] = oldc .membraneVel [i ]
114
- newcell .membraneAcc [i + 1 - split ] = oldc .membraneAcc [i ]
115
111
end
116
- -- add extra node
117
- c .membrane [split ] = oldc .nucleus .x
118
- c .membrane [split + 1 ] = oldc .nucleus .y
119
- c .membraneVel [split ] = oldc .nucleus .vx
120
- c .membraneVel [split + 1 ] = oldc .nucleus .vy
121
- c .membraneAcc [split ] = oldc .nucleus .ax
122
- c .membraneAcc [split + 1 ] = oldc .nucleus .ay
123
-
124
- c .mbsize = table .getn (c .membrane )
125
- for itr = 1 ,c .mbsize / 2 do c .springs [itr ] = fNucSpringLength (c ) end
126
- io.write (" c: {" )
127
- for itr = 1 ,c .mbsize do io.write (c .membrane [itr ]," " ) end
128
- io.write (" }\n " )
129
- newcell .mbsize = table .getn (newcell .membrane )
130
-
131
- newcell .membrane [newcell .mbsize + 1 ] = oldc .nucleus .x
132
- newcell .membrane [newcell .mbsize + 2 ] = oldc .nucleus .y
133
- newcell .membraneVel [newcell .mbsize + 1 ] = oldc .nucleus .vx
134
- newcell .membraneVel [newcell .mbsize + 2 ] = oldc .nucleus .vy
135
- newcell .membraneAcc [newcell .mbsize + 1 ] = oldc .nucleus .ax
136
- newcell .membraneAcc [newcell .mbsize + 2 ] = oldc .nucleus .ay
112
+ table.insert (newcell .membrane ,table .copy (oldc .nucleus ))
137
113
138
- newcell .mbsize = table .getn (newcell .membrane )
139
- for itr = 1 ,newcell .mbsize / 2 do newcell .springs [itr ] = fNucSpringLength (newcell ) end
140
- print (" n mbsz" ,newcell .mbsize )
141
- io.write (" newcell: {" )
142
- for itr = 1 ,newcell .mbsize do io.write (newcell .membrane [itr ]," " ) end
143
- io.write (" }\n " )
144
114
local cnx = 0
145
115
local cny = 0
146
- local j = 1
147
- while j < c .mbsize do
148
- cnx = cnx + c .membrane [j ]
149
- cny = cny + c .membrane [j + 1 ]
150
- j = j + 2
116
+ local csize = table .getn (c .membrane )
117
+ for i = 1 ,csize do
118
+ cnx = cnx + c .membrane [i ].x
119
+ cny = cny + c .membrane [i ].y
151
120
end
152
- c .nucleus .x = cnx / ( c . mbsize / 2 )
153
- c .nucleus .y = cny / ( c . mbsize / 2 )
121
+ c .nucleus .x = cnx / csize
122
+ c .nucleus .y = cny / csize
154
123
c .nucleus .vx = oldc .nucleus .vx -- / 2
155
124
c .nucleus .vy = oldc .nucleus .vy -- / 2
156
125
c .nucleus .ax = oldc .nucleus .ax -- 0
157
126
c .nucleus .ay = oldc .nucleus .ay -- 0
127
+
158
128
local nnx = 0
159
129
local nny = 0
160
- local j = 1
161
- while j < newcell .mbsize do
162
- nnx = nnx + newcell .membrane [j ]
163
- nny = nny + newcell .membrane [j + 1 ]
164
- j = j + 2
130
+ local nsize = table .getn (newcell .membrane )
131
+ for i = 1 ,nsize do
132
+ nnx = nnx + newcell .membrane [i ].x
133
+ nny = nny + newcell .membrane [i ].y
165
134
end
166
- newcell .nucleus .x = nnx / ( newcell . mbsize / 2 )
167
- newcell .nucleus .y = nny / ( newcell . mbsize / 2 )
135
+ newcell .nucleus .x = nnx / nsize
136
+ newcell .nucleus .y = nny / nsize
168
137
newcell .nucleus .vx = oldc .nucleus .vx -- / 2
169
138
newcell .nucleus .vy = oldc .nucleus .vy -- / 2
170
139
newcell .nucleus .ax = oldc .nucleus .ax -- 0
171
140
newcell .nucleus .ay = oldc .nucleus .ay -- 0
172
141
173
- -- TODO: mutations
174
- c .genes = {}
142
+ c .genes = table .copy (oldc .genes )
175
143
c .gtimer = 0
176
- newcell .genes = {}
144
+ newcell .genes = table . copy ( oldc . genes )
177
145
newcell .gtimer = 0
178
146
179
- for k ,v in pairs (oldc .genes ) do
180
- c .genes [k ] = v
181
- newcell .genes [k ] = v
182
- end
183
-
184
147
mutate (c )
185
148
mutate (newcell )
186
149
0 commit comments