-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgent.cs
262 lines (234 loc) · 10.3 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
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
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class Agent {
private static int NEXT_ID = 0;
public enum Sex {
MALE,
FEMALE
}
public int id;
public bool isAlive { get; private set; } = true;
public readonly int endowment;
public int sugar { get; private set; }
public readonly int vision;
public readonly int metabolism;
public readonly Sex sex;
public int age { get; private set; } = 0;
public readonly int maximumAge;
public readonly int minimumFertileAge;
public readonly int maximumFertileAge;
public bool isFertile {
get {
return isAlive && age >= minimumFertileAge && age < maximumFertileAge && sugar >= endowment;
}
}
public Location location;
public GameObject gameObject;
public Renderer renderer;
public Agent () : this(
Utils.RandomIntBetween(Simulation.Parameters.Endowment.MIN, Simulation.Parameters.Endowment.MAX),
Utils.RandomIntBetween(Simulation.Parameters.Vision.MIN, Simulation.Parameters.Vision.MAX),
Utils.RandomIntBetween(Simulation.Parameters.Metabolism.MIN, Simulation.Parameters.Metabolism.MAX)
) { }
public Agent (int endowment, int vision, int metabolism) {
id = NEXT_ID++;
this.endowment = endowment;
sugar = endowment;
this.vision = vision;
this.metabolism = metabolism;
sex = Random.value < 0.5 ? Sex.MALE : Sex.FEMALE;
maximumAge = Utils.RandomIntBetween(Simulation.Parameters.Lifespan.MIN, Simulation.Parameters.Lifespan.MAX);
if ( sex == Sex.MALE ) {
minimumFertileAge = Utils.RandomIntBetween(Simulation.Parameters.Fertility.Male.Begin.MIN, Simulation.Parameters.Fertility.Male.Begin.MAX);
maximumFertileAge = Utils.RandomIntBetween(Simulation.Parameters.Fertility.Male.End.MIN, Simulation.Parameters.Fertility.Male.End.MAX);
} else {
minimumFertileAge = Utils.RandomIntBetween(Simulation.Parameters.Fertility.Female.Begin.MIN, Simulation.Parameters.Fertility.Female.Begin.MAX);
maximumFertileAge = Utils.RandomIntBetween(Simulation.Parameters.Fertility.Female.End.MIN, Simulation.Parameters.Fertility.Female.End.MAX);
}
InitGameObject();
}
public void Destroy () {
Object.Destroy(gameObject);
}
public void Step () {
Move();
Harvest();
Eat();
if ( sugar < 0 || age == maximumAge ) {
Die();
return;
}
Age();
if ( isFertile ) {
Reproduce();
}
}
public void Render () {
gameObject.transform.localPosition = new Vector3(location.y, 0, location.x);
switch ( State.COLORING_OPTION ) {
case State.ColoringOptions.DEFAULT:
renderer.sharedMaterial = Materials.DEFAULT;
break;
case State.ColoringOptions.BY_SEX:
renderer.sharedMaterial = sex == Sex.MALE ? Materials.MALE : Materials.FEMALE;
break;
case State.ColoringOptions.BY_VISION:
{
float firstTercile = Utils.GetFirstTercile(Simulation.Parameters.Vision.MIN, Simulation.Parameters.Vision.MAX);
float secondTercile = Utils.GetSecondTercile(Simulation.Parameters.Vision.MIN, Simulation.Parameters.Vision.MAX);
if ( vision < firstTercile ) {
renderer.sharedMaterial = Materials.LOW_VISION;
} else if ( vision <= secondTercile ) {
renderer.sharedMaterial = Materials.MEDIUM_VISION;
} else {
renderer.sharedMaterial = Materials.HIGH_VISION;
}
}
break;
case State.ColoringOptions.BY_METABOLISM:
{
float firstTercile = Utils.GetFirstTercile(Simulation.Parameters.Metabolism.MIN, Simulation.Parameters.Metabolism.MAX);
float secondTercile = Utils.GetSecondTercile(Simulation.Parameters.Metabolism.MIN, Simulation.Parameters.Metabolism.MAX);
if ( metabolism < firstTercile ) {
renderer.sharedMaterial = Materials.LOW_METABOLISM;
} else if ( metabolism <= secondTercile ) {
renderer.sharedMaterial = Materials.MEDIUM_METABOLISM;
} else {
renderer.sharedMaterial = Materials.HIGH_METABOLISM;
}
}
break;
}
}
public int ConsumeSugarRequiredToReproduce () {
int required = Mathf.CeilToInt(endowment / 2f);
sugar -= required;
return required;
}
private void Move () {
int nextSugar = -1;
int potentialSugar = -1;
Location nextLocation = null;
Location potentialLocation = null;
List<List<Location>> allPotentialLocations = Utils.Shuffle(location.GetAllLocationsInSight(vision));
switch ( Simulation.Parameters.MOVEMENT_STRATEGY ) {
case Simulation.MovementStrategies.CLASSIC:
{
for ( int i = 0 ; i < vision ; i++ ) {
for ( int j = 0 ; j < 4 ; j++ ) {
potentialLocation = allPotentialLocations[j][i];
if ( potentialLocation.agent != null ) {
continue;
}
potentialSugar = potentialLocation.sugar;
if ( nextLocation == null || potentialSugar > nextSugar ) {
nextSugar = potentialSugar;
nextLocation = potentialLocation;
}
}
}
}
break;
case Simulation.MovementStrategies.CUSTOM:
{
if ( isFertile ) {
for ( int i = 0 ; i < vision ; i++ ) {
for ( int j = 0 ; j < 4 ; j++ ) {
if ( i == 0 ) {
potentialLocation = location;
} else {
potentialLocation = allPotentialLocations[j][0];
if ( potentialLocation.agent != null ) {
continue;
}
}
Agent potentialMate = allPotentialLocations[j][i].agent;
if ( potentialMate != null && potentialMate.sex != sex && potentialMate.isFertile ) {
nextLocation = potentialLocation;
j = 4;
i = vision;
break;
}
}
}
}
if ( nextLocation == null ) {
for ( int i = 0 ; i < 4 ; i++ ) {
potentialLocation = allPotentialLocations[i][0];
if ( potentialLocation.agent != null ) {
continue;
}
potentialSugar = allPotentialLocations[i].Select(x => x.sugar).Sum();
if ( nextLocation == null || potentialSugar > nextSugar ) {
nextSugar = potentialSugar;
nextLocation = potentialLocation;
}
}
}
}
break;
}
if ( nextLocation != null ) {
location.agent = null;
location = nextLocation;
location.agent = this;
}
}
private void Harvest () {
sugar += location.Harvest();
}
private void Eat () {
sugar -= metabolism;
}
private void Reproduce () {
List<Agent> oppositeSexNeighbors = GetNeighbors().FindAll(x => x.sex != sex);
foreach ( Agent neighbor in Utils.Shuffle(oppositeSexNeighbors) ) {
if ( neighbor.isFertile ) {
List<Location> potentialLocations = location.GetNeighboringLocations();
potentialLocations.AddRange(neighbor.location.GetNeighboringLocations());
potentialLocations = potentialLocations.FindAll(x => x.agent == null);
potentialLocations = Utils.Shuffle(potentialLocations);
if ( potentialLocations.Count > 0 ) {
int endowment = ConsumeSugarRequiredToReproduce() + neighbor.ConsumeSugarRequiredToReproduce();
Agent baby = new Agent(
endowment,
Random.value < 0.5 ? vision : neighbor.vision,
Random.value < 0.5 ? metabolism : neighbor.metabolism
);
Simulation.agents.Add(baby);
baby.location = potentialLocations[0];
potentialLocations[0].agent = baby;
baby.Render();
}
}
}
}
private void Die () {
isAlive = false;
location.agent = null;
Object.Destroy(gameObject);
}
private void Age () {
age++;
}
private List<Agent> GetNeighbors () {
List<Agent> neighbors = new List<Agent>();
neighbors.Add(location.north.agent);
neighbors.Add(location.south.agent);
neighbors.Add(location.east.agent);
neighbors.Add(location.west.agent);
return neighbors.FindAll(n => n != null);
}
private void InitGameObject () {
gameObject = GameObject.CreatePrimitive(PrimitiveType.Capsule);
gameObject.name = "Agent" + id;
gameObject.transform.localScale = 0.9f * Vector3.one;
Object.Destroy(gameObject.GetComponent<Collider>());
renderer = gameObject.GetComponent<Renderer>();
renderer.receiveShadows = false;
renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
renderer.sharedMaterial = Materials.DEFAULT;
}
}