-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecies.js
422 lines (400 loc) · 13.5 KB
/
species.js
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
417
418
419
420
421
422
/* eslint-disable import/extensions */
import Critter from './critter.js';
import { globals } from './globals.js';
import { state } from './state.js';
import { determineEnergyInCollision } from './physics.js';
// TODO: Should this be part of the class?
const drawCrittersGroup = (
shape,
numberOfCritters,
pixelsPerXAxis,
pixelsPerYAxis,
spacingPerCritter,
critterSize,
xOffset,
yOffset,
) => {
const critterPositions = [];
const shapePixelsPerAxis = 30;
switch (shape) {
case 'square_filled': {
const crittersInAxis = Math.ceil(Math.sqrt(numberOfCritters));
for (let x = 0; x < crittersInAxis; x += 1) {
for (let y = 0; y < crittersInAxis; y += 1) {
const xPos =
xOffset +
spacingPerCritter * (shapePixelsPerAxis / crittersInAxis) * x;
const yPos =
yOffset +
spacingPerCritter * (shapePixelsPerAxis / crittersInAxis) * y;
critterPositions.push({ x: xPos, y: yPos });
}
}
return critterPositions;
}
case 'circle_empty': {
const radsPerCritter = (2 * Math.PI) / numberOfCritters;
for (let j = 0; j < numberOfCritters; j++) {
critterPositions.push({
x: xOffset + shapePixelsPerAxis * Math.sin(radsPerCritter * j),
y: yOffset + shapePixelsPerAxis * Math.cos(radsPerCritter * j),
});
}
return critterPositions;
}
case 'circle_filled': {
const crittersPerRing = [0, 1];
let crittersPlaced = 1;
// Work out the critters per radius
while (crittersPlaced < numberOfCritters) {
const remainingCritters = numberOfCritters - crittersPlaced;
let desiredNextRing = Math.min(
crittersPerRing[crittersPerRing.length - 1] + 2,
remainingCritters,
);
// We don't want subsequent rings to be smaller than current ones
if (remainingCritters - desiredNextRing < desiredNextRing) {
desiredNextRing += remainingCritters - desiredNextRing;
}
crittersPerRing.push(desiredNextRing);
crittersPlaced = crittersPerRing.reduce((total, sum) => total + sum);
}
// Generate the different number of radii
let radii = [...Array(crittersPerRing.length).keys()];
radii = radii.map((r) => r + 1);
for (let i = 0; i < crittersPerRing.length; i += 1) {
const radsPerCritter = (2 * Math.PI) / crittersPerRing[i];
for (let j = 0; j < crittersPerRing[i]; j += 1) {
const radius = radii[i];
const angle = j * radsPerCritter;
critterPositions.push({
x: xOffset + 2 * radius * Math.cos(angle),
y: yOffset + 2 * radius * Math.sin(angle),
});
}
}
return critterPositions;
}
case 'chevron': {
const crittersPerEdge = Math.ceil(numberOfCritters / 3);
for (let j = 0; j < crittersPerEdge; j += 1) {
// top left to middle right
critterPositions.push({
x: xOffset + j * (shapePixelsPerAxis / crittersPerEdge),
y: yOffset + j * 0.5 * (shapePixelsPerAxis / crittersPerEdge),
});
// middle right to bottom left
critterPositions.push({
x:
xOffset +
shapePixelsPerAxis -
j * (shapePixelsPerAxis / crittersPerEdge),
y:
yOffset +
shapePixelsPerAxis / 2 +
j * (shapePixelsPerAxis / crittersPerEdge),
});
}
return critterPositions;
}
case 'triangle_empty': {
const crittersPerEdge = Math.ceil(numberOfCritters / 3);
for (let j = 0; j < crittersPerEdge; j += 1) {
// bottom left to middle top
critterPositions.push({
x: xOffset + j * 0.5 * (shapePixelsPerAxis / crittersPerEdge),
y:
yOffset +
shapePixelsPerAxis -
j * (shapePixelsPerAxis / crittersPerEdge),
});
// middle top to bottom right
critterPositions.push({
x:
xOffset +
shapePixelsPerAxis / 2 +
j * 0.5 * (shapePixelsPerAxis / crittersPerEdge),
y: yOffset + j * (shapePixelsPerAxis / crittersPerEdge),
});
// bottom edge
critterPositions.push({
x: xOffset + j * (shapePixelsPerAxis / crittersPerEdge),
y: yOffset + shapePixelsPerAxis,
});
}
return critterPositions;
}
case 'spiral': {
const minCrittersPerRing = 5;
const numberOfRings = Math.ceil(numberOfCritters / minCrittersPerRing);
const radsPerCritter = (2 * Math.PI) / minCrittersPerRing;
for (let x = 1; x < numberOfRings + 1; x += 1) {
for (let j = 0; j < minCrittersPerRing; j += 1) {
const randomOffset = (x * Math.PI) / 7;
critterPositions.push({
x:
xOffset +
spacingPerCritter +
4 * x * Math.sin(radsPerCritter * j + randomOffset),
y:
yOffset +
spacingPerCritter +
4 * x * Math.cos(radsPerCritter * j + randomOffset),
});
}
}
return critterPositions;
}
case 'square_empty': {
const crittersPerEdge = Math.ceil(numberOfCritters / 4);
for (let j = 0; j < crittersPerEdge; j += 1) {
critterPositions.push({
x:
xOffset +
spacingPerCritter +
j * (shapePixelsPerAxis / crittersPerEdge),
y: yOffset,
});
critterPositions.push({
x: xOffset + shapePixelsPerAxis,
y:
yOffset +
spacingPerCritter +
j * (shapePixelsPerAxis / crittersPerEdge),
});
critterPositions.push({
x: xOffset,
y:
yOffset +
spacingPerCritter +
j * (shapePixelsPerAxis / crittersPerEdge),
});
critterPositions.push({
x:
xOffset +
spacingPerCritter +
j * (shapePixelsPerAxis / crittersPerEdge),
y: yOffset + shapePixelsPerAxis,
});
}
return critterPositions;
}
default:
break;
}
return critterPositions;
};
/**
*
* @param {number} id - a unique identifier
* @param {string} name - species name
* @param {string} colour - a colour string that will be used for critters
* @param {number} groupSize - the maximum number of critters in a group
* @param {number} groupShape - the shape of critters in a group
* @param {number} direction - the direction a critter travels
* @param {number} scaredDirection - the direction a critter turns when scared
* @param {number} respawnRate - how frequently are new critters created (0 - 1)
* @param {number} critterSpeed -the speed of each critter
* @param {number} critterSize - the size of each critter (integer)
* @param {number} critterSpacing - the distance around each critter in a group (integer)
* @param {number} totalEnergy - the total amount of energy for a species (integer)
* @param {number} critterEnergy - the energy used for battle for critter (integer)
* @param {number} scaredRadius - the distance around a critter that's checked for enemy critters to determine if it's scared (integer)
* @param {number} safetyRadius - the distance around a critter that's checked for critters to determine if it's safe (integer)
* @param {number} calmSafetyNumber - the number of calm critters in a radius before it's considered safe
* @param {number} scaredSafetyNumber - the number of scared critters in a radius before it's considered safe
* @param {number} maxAge - the number of cycles before a critter dies (integer)
*/
const Species = class {
constructor(
id,
name,
colour,
groupSize,
groupShape,
direction,
scaredBehaviour,
respawnRate,
critterSpeed,
critterSize,
critterSpacing,
totalEnergy,
critterEnergy,
scaredRadius,
safetyRadius,
calmSafetyNumber,
scaredSafetyNumber,
description,
difficulty,
) {
this.id = id;
this.name = name;
this.colour = colour;
this.groupSize = groupSize;
this.groupShape = groupShape;
this.direction = direction;
this.scaredBehaviour = scaredBehaviour;
this.respawnRate = respawnRate;
this.critterSpeed = critterSpeed;
this.critterEnergy = critterEnergy;
this.critterSize = critterSize;
this.critterSpacing = critterSpacing;
this.totalEnergy = totalEnergy;
this.totalCritters = Math.ceil(this.totalEnergy / this.critterEnergy);
this.totalGroups = Math.ceil(this.totalCritters / this.groupSize);
this.scaredRadius = scaredRadius;
this.safetyRadius = safetyRadius;
this.calmSafetyNumber = calmSafetyNumber;
this.scaredSafetyNumber = scaredSafetyNumber;
this.description = description;
this.difficulty = difficulty;
}
/*
dN/dT = Rmax * N((K-N) / K)
where:
- Rmax = growth rate
- N = population size
- K = carrying capacity (max size)
*/
respawnCritters() {
if (state.cycle % 10 === 0) {
if (this.critters.length === 0) {
return;
}
const rMax = this.respawnRate / globals.respawnRateConstant;
const N = this.getScore();
const K = globals.totalSpeciesEnergy;
const amountOfEnergyLeft = rMax * N * ((K - N) / K);
const numberOfNewCritters = Math.ceil(
amountOfEnergyLeft / (this.critterSize * this.critterSpeed),
);
if (numberOfNewCritters < 0) {
return;
}
for (let i = 0; i < numberOfNewCritters; i++) {
const randomCritter = this.critters[
Math.floor(Math.random() * this.critters.length)
];
const respawnPosition = {
x: Math.min(
globals.canvasWidth,
randomCritter.position.x +
Math.ceil((Math.random() - 0.5) * 20 * this.critterSpacing),
),
y: Math.min(
globals.canvasHeight,
randomCritter.position.y +
Math.ceil((Math.random() - 0.5) * 20 * this.critterSpacing),
),
};
// TODO: this seems to fire critters off randomly
const randomDirection = () => {
if (Math.random() > 0.85) {
return this.direction;
}
if (Math.random() > 0.5) {
return this.direction * 1.02;
}
return this.direction / 1.02;
};
this.critters.push(
new Critter(
this,
respawnPosition,
randomDirection(),
this.scaredDirection,
Math.random() < globals.mutationRate
? this.critterSpeed * 1.5
: randomCritter.speed, // Mutations
Math.random() < globals.mutationRate
? this.critterSize * 1.5
: randomCritter.size, // Mutations
Math.random() < globals.mutationRate
? this.critterEnergy * 1.5
: randomCritter.energy, // Mutations
false,
),
);
}
}
}
determineCritterPositions() {
const startX = this.id * (globals.canvasWidth / 2);
const endX = (this.id + 1) * (globals.canvasWidth / 2);
const startY = this.id * (globals.canvasHeight / 2);
const endY = (this.id + 1) * (globals.canvasHeight / 2);
// Number of groups in each axis
const maxGroupsPerAxis = Math.ceil(Math.sqrt(this.totalGroups));
// Number of critters in each group axis
const pixelsPerXGroup = (endX - startX) / maxGroupsPerAxis;
const pixelsPerYGroup = (endY - startY) / maxGroupsPerAxis;
const critterPositions = [];
for (let i = 0; i < this.totalGroups; i += 1) {
for (let x = 0; x < maxGroupsPerAxis; x += 1) {
for (let y = 0; y < maxGroupsPerAxis; y += 1) {
const xOffset = x * pixelsPerXGroup + startX;
const yOffset = y * pixelsPerYGroup + startY;
// Start with a naive approach
const groupCrittersPositions = drawCrittersGroup(
this.groupShape,
this.groupSize,
pixelsPerXGroup,
pixelsPerYGroup,
this.critterSpacing,
this.critterSize,
xOffset,
yOffset,
);
critterPositions.push(groupCrittersPositions);
}
}
}
return critterPositions.flat();
}
generateCritters() {
const critterPositions = this.determineCritterPositions();
const critters = [];
for (let i = 0; i < this.totalCritters; i += 1) {
const c = new Critter(
this,
critterPositions.pop(),
this.direction,
this.scaredBehaviour,
this.critterSpeed,
this.critterSize,
this.critterEnergy,
false,
);
critters.push(c);
}
this.critters = critters;
}
/**
* @param {{x: number, y: number}} position - the position that critters will be searched from
* @param {number} radius - the radius which will be searched for critters
*/
getCrittersInRegion(position, radius) {
return this.critters.filter((c) => {
const dx = c.position.x - position.x;
const dy = c.position.y - position.y;
const distance = Math.sqrt(dx * dx + dy * dy);
return distance < Math.floor(c.size) + radius;
});
}
getScore() {
const score = this.critters.reduce((acc, cv) => acc + cv.energy, 0);
return score;
}
// The actual energy in a region
getEnergyInRegion(position, radius) {
const collisionCritters = this.getCrittersInRegion(position, radius);
return collisionCritters.reduce(
(acc, cv) => determineEnergyInCollision(cv, position),
0,
);
}
getScaredCritters() {
return this.critters.filter((c) => c.scared);
}
};
export default Species;