-
Notifications
You must be signed in to change notification settings - Fork 0
/
Creature.pde
258 lines (228 loc) · 6.44 KB
/
Creature.pde
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
class Creature extends Entity {
// Member variables
int energy;
float vx;
float vy;
int moveTimer;
// Genes
final int moveRate;
final float moveStrength;
final int breedEnergy;
final int childEnergy;
final int size;
// Initializer for start of simulation
Creature() {
moveRate = (int) random(100);
//moveTimer = moveRate;
moveTimer = moveRate;
moveStrength = random(50);
size = (int) random(1, 5);
breedEnergy = (int) random(size*size, size*size*100);
childEnergy = (int) random(size*size, breedEnergy);
energy = 50;
x = random(width);
y = random(height);
vx = 0;
vy = 0;
}
// Initialize with parents
Creature(Creature p1, Creature p2) {
moveRate = (int) random(p1.moveRate, p2.moveRate) + int(randomGaussian() * 20);
moveTimer = 1;
moveStrength = max(0.1,
random(p1.moveStrength, p2.moveStrength) + randomGaussian() / 5
);
size = max(1,
int(
random(p1.size, p2.size) + random(-2, 2)
)
);
breedEnergy = (int) max(size,
random(p1.breedEnergy, p2.breedEnergy) + int(randomGaussian() * 20)
);
childEnergy = (int) constrain(
random(p1.childEnergy, p2.childEnergy) + int(randomGaussian() * 10),
size,
breedEnergy
);
x = p1.x;
y = p1.y;
energy = p1.childEnergy + p2.childEnergy;
vx = 0;
vy = 0;
}
// Pay energy to push towards the given entity
void goTowards(Entity closest, boolean invert) {
if (closest == null) {
return;
}
if (moveTimer <= 0) {
float dir = atan((closest.y - y)/(closest.x - x));
if (closest.x < x)
dir += PI;
if (invert) {
vx -= cos(dir) * moveStrength;
vy -= sin(dir) * moveStrength;
} else {
vx += cos(dir) * moveStrength;
vy += sin(dir) * moveStrength;
}
moveTimer += moveRate;
energy -= myMoveEnergy();
}
}
void goTowards(Entity closest) {
goTowards(closest, false);
}
float mySight() {
return gl.sightBase +
size * gl.sightLinear +
log(size) * gl.sightLog;
}
float myMoveEnergy() {
return moveStrength *
(
gl.moveEnergyBase +
log(size)*gl.moveEnergyLog +
size*gl.moveEnergyLinear +
size*size*gl.moveEnergyQuadratic
);
}
Plant findPlant(World world) {
float minDist = mySight();
Plant closest = null;
for (Plant plant : world.plants) {
if (dist(plant.x, plant.y, x, y) < minDist) {
minDist = dist(plant.x, plant.y, x, y);
closest = plant;
}
}
return closest;
}
Creature findMeat(World world) {
float minDist = mySight();
Creature closest = null;
for (Creature c : world.pop) {
if (dist(c.x, c.y, x, y) < minDist
&& c != this
&& c.size < size / 2)
{
minDist = dist(c.x, c.y, x, y);
closest = c;
}
}
return closest;
}
Creature findMate(World world) {
if (energy < breedEnergy) {
return null;
}
float minDist = mySight();
Creature closest = null;
for (Creature c : world.pop) {
if (dist(c.x, c.y, x, y) < minDist
&& c != this
&& c.size >= size / 2
&& c.size <= size * 2
&& c.energy >= c.breedEnergy)
{
minDist = dist(c.x, c.y, x, y);
closest = c;
}
}
return closest;
}
Creature findPredator(World world) {
float minDist = mySight();
Creature closest = null;
for (Creature c : world.pop) {
if (dist(c.x, c.y, x, y) < minDist
&& c != this
&& c.size > size * 2)
{
minDist = dist(c.x, c.y, x, y);
closest = c;
}
}
return closest;
}
World step(World world) {
moveTimer -= 1;
// Find the nearest food and mate, for later
Plant closestPlant = findPlant(world);
Creature closestMeat = findMeat(world);
Creature closestMate = findMate(world);
Creature closestPredator = findPredator(world);
// Decide if I'm ready to mate or want to eat
if (energy >= breedEnergy && closestMate != null)
goTowards(closestMate);
else if (closestPlant != null || closestMeat != null) {
if (closestPlant == null && closestMeat != null)
goTowards(closestMeat);
else if (closestPlant != null && closestMeat == null)
goTowards(closestPlant);
else if (dist(closestPlant.x, closestPlant.y, x, y) < dist(closestMeat.x, closestMeat.y, x, y))
goTowards(closestPlant);
else
goTowards(closestMeat);
}
if (closestPredator != null && gl.runFromPredators) {
goTowards(closestPredator, true);
}
// Update velocities and positions
x += vx;
y += vy;
if ((x == 0 || y == 0 || x == width || y == height) && gl.cliffs)
world.removeCreature(this);
x = constrain(x, 0, width);
y = constrain(y, 0, height);
if (x == 0)
vx = max(0, vx);
else if (x == width)
vx = min(0, vx);
if (y == 0)
vy = max(0, vy);
else if (y == height)
vy = min(0, vy);
if (gl.variableFriction) {
vx *= (1 - 1/(gl.variableFrictionCoeff*size));
vy *= (1 - 1/(gl.variableFrictionCoeff*size)); //<>//
} else {
vx *= gl.staticFrictionCoeff;
vy *= gl.staticFrictionCoeff;
}
// See if I'm on plant - if so, eat it
if (closestPlant != null && dist(closestPlant.x, closestPlant.y, x, y) < 2+(size/2)) {
energy += gl.plantEnergy;
world.removePlant(closestPlant);
}
// See if I'm on meat - if so, eat it
if (closestMeat != null && dist(closestMeat.x, closestMeat.y, x, y) < (size + closestMeat.size)/2) {
energy += max(closestMeat.energy, gl.minMeatEnergy);
world.removeCreature(closestMeat);
}
// See if I'm on a potential mate - if so, breed
if (closestMate != null && dist(closestMate.x, closestMate.y, x, y) < (size + closestMate.size + 2)/2) {
Creature child = new Creature(this, closestMate);
energy -= childEnergy;
closestMate.energy -= closestMate.childEnergy;
world.pop.add(child);
}
// See if I'm dead
if (energy <= 0) {
//world.addFood(x, y);
world.removeCreature(this);
}
return world;
}
void draw() {
if (energy >= breedEnergy) {
stroke(255,0,0);
fill(255,0,0);
} else {
stroke(0,0,255);
fill(0,0,255);
}
ellipse(x, y, size, size);
}
}