-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
327 lines (295 loc) · 9.18 KB
/
Copy pathsketch.js
File metadata and controls
327 lines (295 loc) · 9.18 KB
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
let width, height;
let bunnies = [];
let aliveBunnies = [];
let foodPiles = [];
let tempBunnies = [];
let newBunnies = [];
let wolves = [];
let tempWolves = [];
let newWolves = [];
let generationBunnies = 1;
let numAlive = 0;
let numAliveP;
let debugMode = false;
let checkboxDebug;
let numBunniesGlobal = 80;
let numWolvesGlobal = 6;
let xOffset = 0;
let zOffset = 0;
function preload() {
rabbitModel = loadModel('rabbit.obj');
wolfModel = loadModel('wolf.obj');
}
function setup() {
height = 1500;
width = 1500;
createCanvas(width, height, WEBGL);
for (let numBunnies = 0; numBunnies < numBunniesGlobal; numBunnies++) {
aliveBunnies.push(new Boid(numBunnies));
bunnies.push(new Boid(numBunnies));
numAlive++;
}
for (let numFoodPiles = 0; numFoodPiles < 100; numFoodPiles++) {
foodPiles.push(new Food());
}
for (let numWolves = 0; numWolves < numWolvesGlobal; numWolves++) {
wolves.push(new Wolf(numWolves));
}
frameRate(60);
numAliveP = createP("numalive");
checkboxDebug = createCheckbox(
"Debug Mode Bunnies <br/>Pink border: canMate<br/>Color: Hunger<br/>LeftColumn: maxSpeed <br/>RightColumn: maxForce",
false
);
}
function draw() {
push();
//2D setup
//translate(width / 2, height / 2, 0);
//3D setup
//pointLight(200, 200, 200, -mouseX + width/2, -mouseY+height/2, 500);
ambientLight(155, 155, 155);
//translate(0,0,-mouseY*2+650);
//rotateX(PI / 3);
//rotateZ(mouseX / 100);
if (keyIsDown(LEFT_ARROW)) {
xOffset+= 10;
} else if (keyIsDown(RIGHT_ARROW)) {
xOffset-= 10;
} else if (keyIsDown(UP_ARROW)) {
zOffset+= 10;
} else if (keyIsDown(DOWN_ARROW)) {
zOffset-= 10;
}
rotateX(xOffset * .001);
translate(-mouseX + width/2,-mouseY + height/2,zOffset);
background(220, 220, 220, 150);
fill(0, 0, 0, 255);
//Loops through all wolves, finds
//1st target if hungry, closest bunny in perception
//2nd mate if not hungry and matingRate > hungerRate
wolves.forEach((wolf, index) => {
if (wolf.willMate && wolf.active && !wolf.isEating) {
console.log("willMate");
//shallow copy the bunnies array so we can remove the current bunny and find mate
tempWolves = wolves;
let shallowCopy = [];
tempWolves.forEach((tempWolf) => {
shallowCopy.push(tempWolf);
});
shallowCopy.splice(index, 1);
//finds the closest mate
let mate = wolf.findMate(shallowCopy);
//if they find a mate and they are colliding and both active, make them "mate" by adding a genetic copy bunny to newBunnies
if (mate) {
//fiirst checks collision w mate / active status
if (wolf.checkCollision(mate.pos) && wolf.active && mate.active) {
//putting bunnies into "mating" mode
wolf.mate();
mate.mate();
//having a child
newWolves.push(new Wolf(wolves.length));
//crossover
newWolves[newWolves.length - 1].DNA = wolf.DNA.crossover(mate.DNA);
newWolves[newWolves.length - 1].updateDNA();
//mutation
newWolves[newWolves.length - 1].DNA.mutate(0.025);
newWolves[newWolves.length - 1].pos.x = wolf.pos.x;
newWolves[newWolves.length - 1].pos.y = wolf.pos.y;
} else {
wolf.seek(mate.pos);
}
}
} else if (wolf.hungry && wolf.active) {
let closestBunny = wolf.findClosest(aliveBunnies);
if (closestBunny) {
if (wolf.checkCollision(closestBunny.pos)) {
wolf.eat();
aliveBunnies.splice(closestBunny.index, 1);
for (let i = 0; i < aliveBunnies.length; i++) {
aliveBunnies[i].index = i;
}
if (
aliveBunnies.length === 1 ||
closestBunny.index >= aliveBunnies.length
) {
aliveBunnies.pop();
for (
let numBunnies = 0;
numBunnies < numBunniesGlobal;
numBunnies++
) {
aliveBunnies.push(new Boid(numBunnies));
bunnies.push(new Boid(numBunnies));
wolves = [];
numAlive++;
}
} else {
aliveBunnies[closestBunny.index].active = false;
aliveBunnies[closestBunny.index].hunger = 10;
}
}
wolf.seek(closestBunny.pos);
}
}
wolves.push(...newWolves);
newWolves = [];
});
//Loops through all alive bunnies
aliveBunnies.forEach((bunny, index) => {
if (checkboxDebug.checked()) {
bunny.debugMode = true;
} else {
bunny.debugMode = false;
}
//check collision for each piece of food
//first checks to see if wolf nearby
let closestWolf = bunny.findClosestWolf(wolves);
if (closestWolf && bunny.active) {
bunny.flee(closestWolf.pos);
bunny.isFleeing = true;
} else {
bunny.isFleeing = false;
}
//if bunny hungry find closest food not being eaten or "roam"
if (bunny.hungry && bunny.active && !bunny.isFleeing) {
let closestFood = bunny.findClosest(foodPiles);
if (closestFood) {
bunny.seek(closestFood.pos);
} else {
//bunny.roam();
}
//if not hungry && willMate find closest who also willMate
}
if (bunny.willMate && bunny.active && !bunny.isFleeing) {
//shallow copy the bunnies array so we can remove the current bunny and find mate
tempBunnies = aliveBunnies;
let shallowCopy = [];
tempBunnies.forEach((tempBunny) => {
shallowCopy.push(tempBunny);
});
shallowCopy.splice(index, 1);
//finds the closest mate
let mate = bunny.findMate(shallowCopy);
//if they find a mate and they are colliding and both active, make them "mate" by adding a genetic copy bunny to newBunnies
if (mate) {
//fiirst checks collision w mate / active status
if (bunny.checkCollision(mate.pos) && bunny.active && mate.active) {
//putting bunnies into "mating" mode
bunny.mate();
mate.mate();
//having a child
newBunnies.push(new Boid(aliveBunnies.length));
//crossover
newBunnies[newBunnies.length - 1].DNA = bunny.DNA.crossover(mate.DNA);
newBunnies[newBunnies.length - 1].updateDNA();
//mutation
newBunnies[newBunnies.length - 1].DNA.mutate(0.025);
newBunnies[newBunnies.length - 1].pos.x = bunny.pos.x;
newBunnies[newBunnies.length - 1].pos.y = bunny.pos.y;
} else {
bunny.seek(mate.pos);
}
} else {
bunny.roam();
}
}
foodPiles.forEach((foodPile) => {
if (!foodPile.beingEaten && bunny.checkCollision(foodPile.pos)) {
foodPile.beingEaten = true;
foodPile.eatingTime = bunny.eatingTime;
bunny.eat();
} else if (foodPile.beingEaten) {
foodPile.inactiveCount++;
}
});
//figure out alive from dead inside aliveBunnies
aliveBunnies.push(...newBunnies);
for (let i = aliveBunnies.length - 1; i >= 0; i--) {
if (aliveBunnies[i].hunger >= 1) {
aliveBunnies.splice(i, 1);
}
}
for (let i = 0; i < aliveBunnies.length - 1; i++) {
aliveBunnies[i].index = i;
}
newBunnies = [];
}); //end of forEach bunny
for (let i = wolves.length - 1; i >= 0; i--) {
if (wolves[i].hunger >= 2) {
wolves.splice(i, 1);
}
}
for (let i = 0; i < wolves.length - 1; i++) {
wolves[i].index = i;
}
wolves.forEach((wolf) => {
wolf.update();
wolf.draw();
});
aliveBunnies.forEach((bunny) => {
bunny.update();
bunny.draw();
});
//loop through foodPiles backwards and draws/destroys them if eaten for long enough
for (
let numFoodPiles = foodPiles.length - 1;
numFoodPiles--;
numFoodPiles >= 0
) {
if (
foodPiles[numFoodPiles].inactiveCount >=
foodPiles[numFoodPiles].eatingTime
) {
foodPiles.splice(numFoodPiles, 1);
} else {
foodPiles[numFoodPiles].draw();
}
foodPiles[numFoodPiles].draw();
}
//ambientMaterial(5,105,55);
ambientMaterial(244,164,96)
ambientLight(255,255,255);
translate(0,0,-2);
noStroke();
plane(width,height);
//box(width, height, 162);
pop();
//post draw logic, check if all bunnies are dead, if so, add 4 new bunnies
//also prints num alive and allows debugging mode to be enabled/disabled
if (
aliveBunnies.length == 0 ||
aliveBunnies.length == 1 ||
wolves.length == 0 ||
wolves.length == 1
) {
aliveBunnies = [];
for (let numBunnies = 0; numBunnies < numBunniesGlobal; numBunnies++) {
aliveBunnies.push(new Boid(numBunnies));
bunnies.push(new Boid(numBunnies));
numAlive++;
}
wolves = [];
for (let numWolves = 0; numWolves < numWolvesGlobal; numWolves++) {
wolves.push(new Wolf(numWolves));
}
console.log("generation: " + generationBunnies);
generationBunnies++;
}
numAliveP.html(
"Time: " +
floor(frameCount / 60) +
"<br/>Number of Bunnies: " +
aliveBunnies.length +
"<br/> Number of Wolves: " +
wolves.length +
"<br/> Generation: " +
generationBunnies +
"<br/>Number of Food Piles: " +
foodPiles.length +
"<br/>"
);
if (frameCount % 5 == 0) {
foodPiles.push(new Food());
}
}