-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.cs
416 lines (364 loc) · 16.3 KB
/
Cell.cs
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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using Vector2 = System.Numerics.Vector2;
namespace C_EcoSimApp
{
internal class Cell : GameElement
{
public List<Component> ActiveComponents = new List<Component>();
public List<Connection> Connections = new List<Connection>();
public List<Component> InputComponents = new List<Component>();
public List<Component> HiddenComponents = new List<Component>();
public List<Component> OutputComponents = new List<Component>();
public List<Connection> ToHidddenConnections = new List<Connection>();
public List<Connection> ToOutputConnections = new List<Connection>();
public int zonesPerRow = GameInst.worldSizeX / GameInst.ZoneSize - 1;
//position information
public Vector2 oldPosition = new Vector2();
public float rotation;
public HashSet<int> NearZones = new HashSet<int>();
public int previousZone;
//physical characteristics
public int diameter = 32;
public int sa=0;
//velocity information
public float angularVelocity = 0;
public float velocity = 5f;
//ComponentInformation
public List<SRay> rays = new List<SRay>();
public string QuickComponentLookup = "00000000000000000";
public Cell()
{
radius = 16;
}
public void InitializeComponents(bool Custom = false)
{
if (Custom == false)
{
currLife = GameInst.Mrand.Next(500,1500);
foreach (Component component in ActiveComponents)
{
component.cellParent = this;
if (component.componentType == "INPUT") InputComponents.Add(component);
else if (component.componentType == "HIDDEN") HiddenComponents.Add(component);
else OutputComponents.Add(component);
int pos = component.component_ID;
QuickComponentLookup = QuickComponentLookup.ReplaceAt(pos, 1, "1");
}
foreach (Connection connection in Connections)
{
if (connection.HiddenLayerOrOutput == "HiddenLayer") ToHidddenConnections.Add(connection);
else ToOutputConnections.Add(connection);
}
}
else
{
//SIGHT
Sight s = new Sight(nameof(GameElement));
s.cellParent = this;
s.fov = (float)0.017 * (30 + 4 * 15); ;
s.lenght = 100;
s.raysN = 10;
s.component_ID = 0;
string searchNValue0 = Convert.ToString(s.component_ID, 2).PadLeft(5, '0');
s.name = Component.componentsName[searchNValue0];
s.initializeRays(true);
ActiveComponents.Clear();
ActiveComponents.Add(s);
InputComponents.Add(s);
int pos = s.component_ID;
QuickComponentLookup = QuickComponentLookup.ReplaceAt(pos, 1, "1");
//CILIA
Cilia c = new Cilia();
c.activated = true;
c.cellParent = this;
c.ActivationValue = 4;
c.costPT = 0;
c.speed = 0.05f;
c.rotationSpeed = 0.0007f;
c.component_ID = 14;
c.DebugName = "ccacc";
string searchNValue = Convert.ToString(c.component_ID, 2).PadLeft(5, '0');
c.name = Component.componentsName[searchNValue];
ActiveComponents.Add(c);
OutputComponents.Add(c);
int pos2 = c.component_ID;
QuickComponentLookup = QuickComponentLookup.ReplaceAt(pos2, 1, "1");
Connections.Clear();
for (int i = 0; i < s.raysN / 2; i++)
{
Connection connection = new Connection("", "", "", "", "", "");
connection.optionStart = i;
connection.optionEnd = 0;
connection.connectionStart = s.component_ID;
connection.connectionEnd = c.component_ID;
connection.weight = -1f;
connection.bias = 0;
connection.HiddenLayerOrOutput = "Output";
connection.InputOrHiddenLayer = "Input";
Connections.Add(connection);
if (connection.HiddenLayerOrOutput == "HiddenLayer") ToHidddenConnections.Add(connection);
else ToOutputConnections.Add(connection);
}
for (int i = (int)s.raysN / 2; i < s.raysN; i++)
{
Connection connection = new Connection("", "", "", "", "", "");
connection.optionStart = i;
connection.optionEnd = 0;
connection.connectionStart = s.component_ID;
connection.connectionEnd = c.component_ID;
connection.weight = 1f;
connection.bias = 0;
connection.HiddenLayerOrOutput = "Output";
connection.InputOrHiddenLayer = "Input";
Connections.Add(connection);
if (connection.HiddenLayerOrOutput == "HiddenLayer") ToHidddenConnections.Add(connection);
else ToOutputConnections.Add(connection);
}
}
/*
Console.WriteLine(ActiveComponents.Count);
string ALL = "";
string CONALL = "";
ActiveComponents.ForEach(x => ALL+= x.REPR());
Connections.ForEach(x => CONALL += x.REPR());
Console.WriteLine($"Components: {ALL} \n Connections: {CONALL} \n ____END____");*/
}
public Vector2 randomPos()
{
Random random = new Random();
Vector2 returnVal = new Vector2();
returnVal.X = random.Next(0, GameInst.worldSizeX);
returnVal.Y = random.Next(0, GameInst.worldSizeY);
return returnVal;
}
public float randomRot()
{
Random random = new Random();
float X = (float)(random.NextDouble()*6.28);
return X;
}
public override void drawSelf(Texture2D cellTexture, SpriteBatch _spriteBatch)
{
foreach (Component component in InputComponents)
{
if (component.componentFamilyName == "SIGHT")
{
Sight s = (Sight)component;
s.VisualRepr(_spriteBatch);
}
}
_spriteBatch.Draw(cellTexture, new Rectangle((int)position.X, (int)position.Y, diameter, diameter), null, Color.White, rotation, new Vector2(diameter, diameter), SpriteEffects.None, 0);
}
public float CalcDist(GameElement A, GameElement B)
{
float distx = Math.Abs(A.position.X- B.position.X);
float disty = Math.Abs(A.position.Y- B.position.Y);
float dist = (float)(0.5 * (distx + disty + Math.Max(distx, disty)));
return dist;
}
public List<CollisionInfo> CheckCollision(List<GameElement> cells)
{
List<CollisionInfo> CinfoList = new List<CollisionInfo>();
int collided = 0;
foreach (GameElement cell in cells)
{
if (cell == this || NearZones.Contains(cell.CurrentZone)==false) continue;
float dist = CalcDist(cell, this);
if (dist < diameter )
{
collided++;
CollisionInfo CI = new CollisionInfo();
CI.collides = true;
CI.collideswith = cell;
CI.distance = dist;
CinfoList.Add(CI);
}
}
if (collided == 0)
{
return null;
}
else return CinfoList;
}
public void UpdateZones()
{
CurrentZone = (int)(Math.Floor(position.X / GameInst.ZoneSize)+ (zonesPerRow - 1)* Math.Floor(position.Y/GameInst.ZoneSize));
if (CurrentZone != previousZone)
{
previousZone = CurrentZone;
NearZones = new HashSet<int>
{
CurrentZone,
CurrentZone + 1,
CurrentZone - 1,
CurrentZone - zonesPerRow,
CurrentZone - zonesPerRow - 1,
CurrentZone - zonesPerRow + 1,
CurrentZone + zonesPerRow,
CurrentZone + zonesPerRow - 1,
CurrentZone + zonesPerRow + 1
};
}
}
public void WorldBorderCheck()
{
/*
if (position.X < 0) position.X = 1;
else if (position.X > GameInst.worldSizeX) position.X = GameInst.worldSizeX-1;
if (position.Y < 0) position.Y = 1;
else if (position.Y > GameInst.worldSizeY) position.Y = GameInst.worldSizeY - 1;
*/
if (position.X < 0) position.X = GameInst.worldSizeX - 1;
else if (position.X > GameInst.worldSizeX) position.X = 1;
if (position.Y < 0) position.Y = GameInst.worldSizeY - 1;
else if (position.Y > GameInst.worldSizeY) position.Y = 1;
}
public override void Update(List<GameElement> cells, List<GameElement> plants, List<GameElement> objects, SpriteBatch _spriteBatch)
{
//Position rotation handler
oldPosition = position;
rotation += angularVelocity * GameInst.deltatime;
if (angularVelocity > 0.0002f) angularVelocity -= 0.0002f;
else if (angularVelocity < -0.0002f) angularVelocity += 0.0002f;
if (angularVelocity > 1) angularVelocity = 1;
if (angularVelocity < -1) angularVelocity = -1;
float x_ADD = -(float)Math.Cos(1.57+rotation)*velocity;
float y_ADD = -(float)Math.Sin(1.57+rotation)*velocity;
position.X += x_ADD * GameInst.deltatime;
position.Y += y_ADD * GameInst.deltatime;
/*if (velocity > 0.02f) velocity -= 0.02f;
else if (velocity < -0.02f) velocity += 0.02f;
*/
if (velocity > 5) velocity = 5;
if (velocity < -5) velocity = -5;
UpdateZones();
//InterCells collision
List<CollisionInfo> collisionInfo = CheckCollision(objects);
if(collisionInfo!=null)
{
position = oldPosition;
//Bounce Back
foreach(CollisionInfo ci in collisionInfo)
{
Vector2 dir = (this.position - ci.collideswith.position);
dir.X *= (float)0.1*GameInst.deltatime;
dir.Y *= (float)0.1*GameInst.deltatime;
ci.collideswith.position -= dir;
position += dir;
}
}
WorldBorderCheck();
float EnergyBalance = 0;
//RUN input Components
foreach (Component component in InputComponents)
{
List<float> result = component.RUN(objects);
if(result!=null) component.results = result;
else component.results = new List<float>();
EnergyBalance -= component.CostXtime;
}
/*foreach (Connection connection in ToHidddenConnections)
{
}*/
foreach(Connection connection in ToOutputConnections)
{
int startP = connection.connectionStart;
int endP = connection.connectionEnd;
//GET Values to input
float result = 0;
if (connection.InputOrHiddenLayer == "Input")
{
if (QuickComponentLookup[startP].ToString() != "1") continue;
string searchNValue = Convert.ToString(startP, 2).PadLeft(5, '0');
string searchN = Component.componentsName[searchNValue];
Component foundComp = new Component();
foreach (Component component in InputComponents)
{
if (component.name == searchN) foundComp = component;
}
//CHECK
//if(foundComp.results.Count <= connection.optionStart) connection.optionStart = foundComp.results.Count - 1;
int l = foundComp.results.Count;
if (foundComp.results.Count == 0) result = 0;
else if(connection.optionStart == 0)
{
//Left Rays
for (int i = 0; i<(int)l/2; i++)
{
if (foundComp.results.Count>0)
{
result += foundComp.results[i];
}
}
}
else
{
for (int i = (int)l/2; i < l; i++)
{
result += foundComp.results[i];
}
}
// else result = foundComp.results[(int)connection.optionStart]; OLD METHOD, ONE RAY CHOOSEN
}
//Calculate input*weight*bias, select output component, pass value
if (connection.HiddenLayerOrOutput == "Output")
{
//CHECK
//endP += 7;
if (QuickComponentLookup.Length <= endP) endP = QuickComponentLookup.Length - 1;
if (QuickComponentLookup[endP].ToString() != "1") continue;
string searchNValue = Convert.ToString(endP, 2).PadLeft(5, '0');
string searchN = Component.componentsName[searchNValue];
Component foundComp = new Component();
foreach (Component component in OutputComponents)
{
if (component.name == searchN)
{
foundComp = component;
break;
}
}
float passedResult = result * connection.weight + connection.bias;
foundComp.inputV += passedResult;
foundComp.inputN += 1;
}
}
foreach (Component component in OutputComponents)
{
component.RUN(objects);
EnergyBalance -= component.CostXtime;
}
currLife += EnergyBalance;
//if(EnergyBalance<-0.5f) Console.WriteLine($"life: {currLife}, expenses: {EnergyBalance}");
}
public (bool result, List<Component> found) CheckComponentPresence(string ComponentFamilyName)
{
List<Component> Found = new List<Component> ();
int ActiveSight = 0;
foreach (Component c in ActiveComponents)
{
if (c.componentFamilyName == ComponentFamilyName)
{
Found.Add (c);
ActiveSight++;
}
}
if (ActiveSight != 0)
{
return (true, Found);
}
return (false, null);
}
}
}