-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorganismAndFoodSource.js
More file actions
383 lines (320 loc) · 13.7 KB
/
organismAndFoodSource.js
File metadata and controls
383 lines (320 loc) · 13.7 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
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
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const organismSize = 5;
const startingOrganismsCount = 200;
const startingFoodSourcesCount = 100
const organisms = [];
const scanRadius = 200;
const movementCost = .001;
const newOrganismFrequency = 0.1; // Set the desired frequency (e.g., 0.01 for 1% chance per frame)
const newFoodSourceFrequency = 0.1; // Set the desired frequency (e.g., 0.005 for 0.5% chance per frame)
const reproductionEnergyThreshold = 2000;
const maxStartingEnergy = 100;
const sameSpeciesColorDistance = 50; // The "color distance" that is considered the same species
const sameFoodColorDistance = 150;
const eatRate = 0.01; // Adjust this value to control the speed of size transfer
class Organism {
constructor(x, y, color) {
this.x = x;
this.y = y;
// Calculate the average of the input color components
const inputColorAverage = (color.r + color.g + color.b) / 3;
// Calculate the normalization factor to achieve an average of 127.5
const normalizationFactor = 127.5 / inputColorAverage;
// Normalize the input color
this.color = {
r: Math.min(255, Math.round(color.r * normalizationFactor)),
g: Math.min(255, Math.round(color.g * normalizationFactor)),
b: Math.min(255, Math.round(color.b * normalizationFactor)),
};
this.energy = Math.random() * (maxStartingEnergy - 1) + 1;
this.radius = Math.sqrt(this.energy / Math.PI); // Update the radius based on energy
this.speed = this.color.b / 255 + 1; // You can set a constant value or use another attribute for speed
this.directionVector = { x: 0, y: 0 };
}
draw() {
this.radius = Math.sqrt(this.energy / Math.PI); // Update the radius based on energy
ctx.fillStyle = `rgba(${this.color.r}, ${this.color.g}, ${this.color.b}, 255)`;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
ctx.closePath();
ctx.fill();
}
move() {
let directionVectors = [];
// Interaction with other organisms
for (const otherOrganism of organisms) {
if (otherOrganism === this) continue;
const colorDistance = Math.sqrt(
Math.pow(this.color.r - otherOrganism.color.r, 2) +
Math.pow(this.color.g - otherOrganism.color.g, 2) +
Math.pow(this.color.b - otherOrganism.color.b, 2)
);
const dx = otherOrganism.x - this.x;
const dy = otherOrganism.y - this.y;
const distance = Math.sqrt(dx * dx + dy * dy) - this.radius - otherOrganism.radius;
if (distance < scanRadius) {
let vectorMagnitude;
if (colorDistance <= sameSpeciesColorDistance) {
if (this.energy > reproductionEnergyThreshold && otherOrganism.energy > reproductionEnergyThreshold) {
vectorMagnitude = this.color.b / 255; // Move toward organisms of the same color if the energy is at least reproductionEnergyThreshold
} else {
vectorMagnitude = -(this.color.b / 255); // Move away from organisms of the same color if the energy is below reproductionEnergyThreshold
}
} else if (otherOrganism.energy > this.energy) {
vectorMagnitude = -(this.color.r / 255); // Move away from larger organisms
} else {
vectorMagnitude = this.color.r / 255; // Move toward smaller organisms
}
const normalizedVector = {
x: (dx/distance) * vectorMagnitude / distance,
y: (dy/distance) * vectorMagnitude / distance,
};
directionVectors.push(normalizedVector);
}
}
// Interaction with food sources
for (const foodSource of foodSources) {
const dx = foodSource.x - this.x;
const dy = foodSource.y - this.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < scanRadius) {
const colorDistance = Math.sqrt(
Math.pow(this.color.r - foodSource.colorPreference.r, 2) +
Math.pow(this.color.g - foodSource.colorPreference.g, 2) +
Math.pow(this.color.b - foodSource.colorPreference.b, 2)
);
// Calculate the food source element based on the color distance
const foodSourceElement = Math.max(0, (sameFoodColorDistance - colorDistance) / 255);
const vectorMagnitude = this.color.g / 255 * foodSourceElement;
const normalizedVector = {
x: (dx/distance) * vectorMagnitude / distance,
y: (dy/distance) * vectorMagnitude / distance,
};
directionVectors.push(normalizedVector);
}
}
// Select the vector with the greatest magnitude
let maxMagnitude = 0;
let maxIndex = -1;
for (let i = 0; i < directionVectors.length; i++) {
const magnitude = Math.sqrt(
directionVectors[i].x * directionVectors[i].x +
directionVectors[i].y * directionVectors[i].y
);
if (magnitude > maxMagnitude) {
maxMagnitude = magnitude;
maxIndex = i;
}
}
if (maxIndex >= 0) {
const directionVector = directionVectors[maxIndex];
// Calculate the energy cost of the movement
const energyCost = Math.pow(this.radius, 2) * Math.pow(this.speed, 3) * movementCost;
// Move only if there's enough energy
if (this.energy - energyCost >= 0) {
const newX = this.x + directionVector.x / maxMagnitude * this.speed;
const newY = this.y + directionVector.y / maxMagnitude * this.speed;
// Check if the new position is within the canvas bounds
if (newX >= 0 && newX <= canvas.width) {
this.x = newX;
}
if (newY >= 0 && newY <= canvas.height) {
this.y = newY;
}
this.energy -= energyCost;
}
this.checkForCollisions();
}
}
checkForCollisions() {
for (const otherOrganism of organisms) {
if (otherOrganism === this) continue;
const dx = otherOrganism.x - this.x;
const dy = otherOrganism.y - this.y;
const distance = Math.sqrt(dx * dx + dy * dy) - this.radius - otherOrganism.radius;
if (distance < 1) {
const colorDistance = Math.sqrt(
Math.pow(this.color.r - otherOrganism.color.r, 2) +
Math.pow(this.color.g - otherOrganism.color.g, 2) +
Math.pow(this.color.b - otherOrganism.color.b, 2)
);
if (colorDistance <= sameSpeciesColorDistance) {
if (this.energy > reproductionEnergyThreshold && otherOrganism.energy > reproductionEnergyThreshold) {
this.reproduce();
otherOrganism.reproduce();
}
continue;
} else {
if (this.energy > otherOrganism.energy) {
const transferEnergy = Math.min(otherOrganism.energy, eatRate * this.energy);
this.energy += transferEnergy; // Transfer energy from the smaller organism to the larger organism at the specified rate
otherOrganism.energy -= transferEnergy;
if (otherOrganism.energy <= 0) {
organisms.splice(organisms.indexOf(otherOrganism), 1);
}
} else {
const transferEnergy = Math.min(this.energy, eatRate * otherOrganism.energy);
otherOrganism.energy += transferEnergy; // Transfer energy from the smaller organism to the larger organism at the specified rate
this.energy -= transferEnergy;
if (this.energy <= 0) {
organisms.splice(organisms.indexOf(this), 1);
}
}
}
}
}
}
reproduce() {
if (this.energy > reproductionEnergyThreshold) {
console.log(`Reproducing. Color: ${this.color.r}, ${this.color.g}, ${this.color.b}`);
const offspringEnergy = this.energy * 0.5;
const offspringColor = {
r: Math.floor(this.color.r + (Math.random() * 20 - 10)),
g: Math.floor(this.color.g + (Math.random() * 20 - 10)),
b: Math.floor(this.color.b + (Math.random() * 20 - 10)),
};
// Randomly choose an angle for the offspring's position
const angle = Math.random() * 2 * Math.PI;
const distanceFromParent = this.radius + Math.sqrt(offspringEnergy / Math.PI);
const offspringX = this.x + distanceFromParent * Math.cos(angle);
const offspringY = this.y + distanceFromParent * Math.sin(angle);
const offspring = new Organism(offspringX, offspringY, offspringColor);
offspring.energy = offspringEnergy;
organisms.push(offspring);
this.energy -= offspringEnergy;
}
}
consumeFood() {
for (const foodSource of foodSources) {
const dx = foodSource.x - this.x;
const dy = foodSource.y - this.y;
const distance = Math.sqrt(dx * dx + dy * dy);
const minDistance = (this.radius + 5) / 2;
if (distance < minDistance) {
const colorDistance = Math.sqrt(
Math.pow(this.color.r - foodSource.colorPreference.r, 2) +
Math.pow(this.color.g - foodSource.colorPreference.g, 2) +
Math.pow(this.color.b - foodSource.colorPreference.b, 2)
);
const energyGain = Math.max(0, (sameFoodColorDistance - colorDistance) / 255 * foodSource.energy);
this.energy += energyGain;
foodSource.energy -= energyGain;
if (foodSource.energy <= 1) {
console.log(`Food source depleted. Color: ${this.color.r}, ${this.color.g}, ${this.color.b}`);
foodSources.splice(foodSources.indexOf(foodSource), 1);
}
}
}
}
}
function createOrganisms() {
for (let i = 0; i < startingOrganismsCount; i++) {
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height;
const color = {
r: Math.floor(Math.random() * 256),
g: Math.floor(Math.random() * 256),
b: Math.floor(Math.random() * 256),
};
organisms.push(new Organism(x, y, color));
}
}
class FoodSource {
constructor(x, y, colorPreference) {
this.x = x;
this.y = y;
this.colorPreference = colorPreference;
this.energy = 1000;
}
draw() {
ctx.fillStyle = `rgb(${this.colorPreference.r}, ${this.colorPreference.g}, ${this.colorPreference.b})`;
ctx.beginPath();
ctx.arc(this.x, this.y, 5, 0, 2 * Math.PI);
ctx.closePath();
ctx.fill();
}
}
const foodSources = [];
function createFoodSources() {
for (let i = 0; i < startingFoodSourcesCount; i++) {
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height;
const colorPreference = {
r: Math.floor(Math.random() * 256),
g: Math.floor(Math.random() * 256),
b: Math.floor(Math.random() * 256),
};
foodSources.push(new FoodSource(x, y, colorPreference));
}
}
function addRandomOrganism() {
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height;
const color = {
r: Math.floor(Math.random() * 256),
g: Math.floor(Math.random() * 256),
b: Math.floor(Math.random() * 256),
};
organisms.push(new Organism(x, y, color));
}
function addRandomFoodSource() {
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height;
const colorPreference = {
r: Math.floor(Math.random() * 256),
g: Math.floor(Math.random() * 256),
b: Math.floor(Math.random() * 256),
};
foodSources.push(new FoodSource(x, y, colorPreference));
}
function addFoodSource(x, y, color) {
const colorPreference = {
r: color.r,
g: color.g,
b: color.b,
};
foodSources.push(new FoodSource(x, y, colorPreference));
}
function addSelectedFoodSource(event) {
const x = event.clientX - canvas.offsetLeft;
const y = event.clientY - canvas.offsetTop;
const colorPicker = document.getElementById('foodSourceColor');
const colorValue = colorPicker.value;
const color = {
r: parseInt(colorValue.slice(1, 3), 16),
g: parseInt(colorValue.slice(3, 5), 16),
b: parseInt(colorValue.slice(5, 7), 16),
};
addFoodSource(x, y, color);
updateCurrentAction('Food Source');
}
addFoodSourceBtn.addEventListener('click', (event) => {
canvas.removeEventListener('click', addOrganismOnClick);
canvas.addEventListener('click', addSelectedFoodSource);
updateCurrentAction('Food Source');
});
function updateCurrentAction(action) {
const currentActionElement = document.getElementById('currentAction');
currentActionElement.innerText = `Adding: ${action}`;
}
function addOrganismOnClick(event) {
const x = event.clientX - canvas.offsetLeft;
const y = event.clientY - canvas.offsetTop;
const color = {
r: Math.floor(Math.random() * 256),
g: Math.floor(Math.random() * 256),
b: Math.floor(Math.random() * 256),
};
organisms.push(new Organism(x, y, color));
updateCurrentAction('Organism');
}
function switchToAddOrganismMode() {
canvas.removeEventListener('click', addSelectedFoodSource);
canvas.addEventListener('click', addOrganismOnClick);
updateCurrentAction('Organism');
}
const addOrganismBtn = document.getElementById('addOrganismBtn');
addOrganismBtn.addEventListener('click', switchToAddOrganismMode);
createOrganisms();
createFoodSources();