-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgent.cs
90 lines (80 loc) · 2.85 KB
/
Agent.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
namespace ReactionDiffusionLibrary;
public class Agent
{
public Species ParentSpecies { get; private set; }
public string AgentId { get; set; }
public int AgentIndex { get; set; }
public double X { get; set; }
public double Y { get; set; }
public double Energy { get; set; }
public double StepSize = 1.0;
public Agent(Species species, int agentNumber, double? energy = null)
{
this.ParentSpecies = species;
var yOrDCondition = (species.PredOrPrey == "Prey") ? 'y' : 'd';
AgentId = $"{yOrDCondition}{agentNumber}";
AgentIndex = agentNumber;
X = ParentSpecies.SpeciesCoords[agentNumber][0];
Y = ParentSpecies.SpeciesCoords[agentNumber][1];
Random rnd = new Random();
Energy = species.InitialEnergy * (1/2 + rnd.NextDouble());
}
public Agent(Species species, int agentNumber, Agent parent, double? energy = null)
{
this.ParentSpecies = species;
var yOrDCondition = (species.PredOrPrey == "Prey") ? 'y' : 'd';
AgentId = $"{yOrDCondition}{agentNumber}";
AgentIndex = agentNumber;
X = parent.X;
Y = parent.Y;
ParentSpecies.SpeciesCoords.Add(new double[] {X, Y});
Random rnd = new Random();
if (energy != null) Energy = (double)energy;
else Energy = parent.Energy/2;
}
public void AddToDeathList()
{
ParentSpecies.DeathList.Add(this);
}
public Agent Procreate(Agent other)
{
var baby = new Agent(ParentSpecies, this.AgentIndex, this, (Energy + other.Energy) / 2);
Energy /= 3;
other.Energy /= 3;
ParentSpecies.Babies.Add(baby);
ParentSpecies.SpeciesCoords.Add(new double[] {X, Y});
return baby;
}
public Agent Procreate()
{
var baby = new Agent(ParentSpecies, this.AgentIndex, this, Energy / 2);
Energy /= 2;
ParentSpecies.Babies.Add(baby);
ParentSpecies.SpeciesCoords.Add(new double[] {X, Y});
return baby;
}
public void Move(string direction)
{
Energy -= StepSize;
switch (direction)
{
case "LEFT":
X += (X - StepSize > 0) ? -StepSize : StepSize;
break;
case "RIGHT":
X += (X + StepSize < Grid.GridXSize - 1) ? StepSize : -StepSize;
break;
case "UP":
Y += (Y + StepSize < Grid.GridYSize - 1) ? StepSize : -StepSize;
break;
case "DOWN":
Y += (Y - StepSize > 0) ? -StepSize : StepSize;
break;
case "STAY":
if (ParentSpecies.PredOrPrey == "Prey") Energy += Energy/5 + 20; // += Energy/8 +12;
if (ParentSpecies.PredOrPrey == "Predator") Energy += 4;
break;
}
if (Energy <= 0) AddToDeathList();
}
}