-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlimeMold.cpp
More file actions
613 lines (516 loc) · 22.4 KB
/
Copy pathSlimeMold.cpp
File metadata and controls
613 lines (516 loc) · 22.4 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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#include "SlimeMold.h"
SlimeMold::SlimeMold(Panel& panel)
: _panel(panel), _activeParticles(0), _trailMap(nullptr), _foodMap(nullptr) {
}
SlimeMold::~SlimeMold() {
if (_trailMap != nullptr) {
free(_trailMap);
_trailMap = nullptr;
}
if (_foodMap != nullptr) {
free(_foodMap);
_foodMap = nullptr;
}
}
void SlimeMold::begin(uint32_t seed) {
_seed = seed;
_tuning = SimTuning::forPanel(_panel.type());
_screenW = _panel.width();
_screenH = _panel.height();
_gridW = _screenW / GRID_SCALE;
_gridH = _screenH / GRID_SCALE;
// The Arduino core ignores a seed of 0 and would leave rand() unseeded,
// so remap it to a fixed substitute.
randomSeed(seed == 0 ? 0xA5A5A5A5UL : seed);
// Initialize pattern dynamism variables
_lastParamUpdateTime = 0;
_lastStateCheckTime = 0;
_previousStateHash = 0;
_stalenessCounter = 0;
// Palette comes from the shared split-complementary generator, so the
// colony previewed on the installer page and the one on the panel use the
// same colours for a given seed. See include/Palette.h.
Palette palette = generatePalette(seed);
_baseColor = {palette.base.r, palette.base.g, palette.base.b};
_foodColor = {palette.food.r, palette.food.g, palette.food.b};
_searchColor = {palette.search.r, palette.search.g, palette.search.b};
// Initialize dynamic parameters with larger variations
_moveSpeed = 4 + random(-3, 4); // 1-7
_sensorAngle = 60 + random(-25, 26); // 35-85 degrees
_sensorDistance = 15 + random(-5, 11); // 10-25
_rotationAngle = 25 + random(-10, 11); // 15-35 degrees
_foodDepositAmount = 50 + random(-10, 11); // 40-60
_foodClusterRadius = 4 + random(-2, 3); // 2-6
_particleSize = _tuning.particleSizeBase + random(_tuning.particleSizeSpan);
_foodConsumptionRate = 2 + random(-1, 2); // 1-3
_foodRegenRate = 4 + random(-2, 3); // 2-6
_foodRegenChance = 5 + random(-2, 3); // 3-7%
// Allocate trail and food maps
if (psramFound()) {
_trailMap = (uint8_t*)ps_malloc(_gridW * _gridH);
_foodMap = (uint8_t*)ps_malloc(_gridW * _gridH);
} else {
_trailMap = (uint8_t*)malloc(_gridW * _gridH);
_foodMap = (uint8_t*)malloc(_gridW * _gridH);
}
if (_trailMap == nullptr || _foodMap == nullptr) {
Serial.println("Failed to allocate maps!");
while(1);
}
// Clear maps
memset(_trailMap, 0, _gridW * _gridH);
memset(_foodMap, 0, _gridW * _gridH);
// Initialize simulation
initializeParticles();
initializeFoodMap();
// Clear screen
_panel.fillScreen(0x0000);
}
void SlimeMold::initializeParticles() {
_activeParticles = MAX_PARTICLES;
// Initialize particles with random positions across the entire screen
for (uint16_t i = 0; i < _activeParticles; i++) {
_particles[i].x = random(_screenW);
_particles[i].y = random(_screenH);
_particles[i].angle = random(360) * PI / 180.0f;
_particles[i].active = true;
}
}
void SlimeMold::initializeFoodMap() {
// Create initial food clusters
uint8_t numClusters = _tuning.foodClusters;
for (uint8_t c = 0; c < numClusters; c++) {
// Pick random center point
uint16_t centerX = random(_gridW);
uint16_t centerY = random(_gridH);
// Create food cluster
for (int8_t dy = -_foodClusterRadius; dy <= _foodClusterRadius; dy++) {
for (int8_t dx = -_foodClusterRadius; dx <= _foodClusterRadius; dx++) {
// Calculate distance from center
float dist = sqrt(dx*dx + dy*dy);
if (dist <= _foodClusterRadius) {
uint16_t x = (centerX + dx + _gridW) % _gridW;
uint16_t y = (centerY + dy + _gridH) % _gridH;
uint16_t idx = y * _gridW + x;
// More food near center of cluster
_foodMap[idx] = random(150, 200) * (1.0f - dist/_foodClusterRadius);
}
}
}
}
}
float SlimeMold::senseTrail(float x, float y, float angle) {
// Calculate sensor position
float sensorX = x + cos(angle) * _sensorDistance;
float sensorY = y + sin(angle) * _sensorDistance;
// Wrap around screen edges
sensorX = fmod(sensorX + _screenW, _screenW);
sensorY = fmod(sensorY + _screenH, _screenH);
// Convert to grid coordinates
uint16_t gridX = (uint16_t)(sensorX / GRID_SCALE);
uint16_t gridY = (uint16_t)(sensorY / GRID_SCALE);
// Sample area around sensor
float sum = 0;
int samples = 0;
for (int8_t dy = -SENSOR_SIZE; dy <= SENSOR_SIZE; dy++) {
for (int8_t dx = -SENSOR_SIZE; dx <= SENSOR_SIZE; dx++) {
uint16_t sampleX = (gridX + dx + _gridW) % _gridW;
uint16_t sampleY = (gridY + dy + _gridH) % _gridH;
sum += _trailMap[sampleY * _gridW + sampleX];
samples++;
}
}
return sum / samples;
}
float SlimeMold::senseFood(float x, float y, float angle) {
// Calculate sensor position
float sensorX = x + cos(angle) * _sensorDistance;
float sensorY = y + sin(angle) * _sensorDistance;
// Wrap around screen edges
sensorX = fmod(sensorX + _screenW, _screenW);
sensorY = fmod(sensorY + _screenH, _screenH);
// Convert to grid coordinates
uint16_t gridX = (uint16_t)(sensorX / GRID_SCALE);
uint16_t gridY = (uint16_t)(sensorY / GRID_SCALE);
// Sample area around sensor
float sum = 0;
int samples = 0;
for (int8_t dy = -SENSOR_SIZE; dy <= SENSOR_SIZE; dy++) {
for (int8_t dx = -SENSOR_SIZE; dx <= SENSOR_SIZE; dx++) {
uint16_t sampleX = (gridX + dx + _gridW) % _gridW;
uint16_t sampleY = (gridY + dy + _gridH) % _gridH;
sum += _foodMap[sampleY * _gridW + sampleX];
samples++;
}
}
return sum / samples;
}
void SlimeMold::rotateBasedOnSensors(Particle& p) {
// Calculate sensor angles
float leftAngle = p.angle + _sensorAngle * PI / 180.0f;
float rightAngle = p.angle - _sensorAngle * PI / 180.0f;
// Get trail sensor readings
float centerTrail = senseTrail(p.x, p.y, p.angle);
float leftTrail = senseTrail(p.x, p.y, leftAngle);
float rightTrail = senseTrail(p.x, p.y, rightAngle);
// Get food sensor readings
float centerFood = senseFood(p.x, p.y, p.angle);
float leftFood = senseFood(p.x, p.y, leftAngle);
float rightFood = senseFood(p.x, p.y, rightAngle);
// Randomly ignore strong trails to break up static patterns
if (random(100) < 15) { // 15% chance to focus on food instead of trails
centerTrail *= 0.2f;
leftTrail *= 0.2f;
rightTrail *= 0.2f;
centerFood *= 2.0f;
leftFood *= 2.0f;
rightFood *= 2.0f;
} else {
// Reduce influence of very strong trails
float trailScale = 1.5f;
if (centerTrail > 180) centerTrail *= 0.5f;
if (leftTrail > 180) leftTrail *= 0.5f;
if (rightTrail > 180) rightTrail *= 0.5f;
// Add some randomness to trail influence
trailScale *= (1.0f + (random(-20, 21) / 100.0f)); // ±20% variation
centerTrail *= trailScale;
leftTrail *= trailScale;
rightTrail *= trailScale;
}
// Combine readings
float centerReading = centerTrail + centerFood;
float leftReading = leftTrail + leftFood;
float rightReading = rightTrail + rightFood;
// Add random movement to break symmetry
if (random(100) < 5) { // 5% chance for random rotation
p.angle += random(-PI/4, PI/4);
return;
}
// Rotate based on highest reading
if (leftReading > rightReading && leftReading > centerReading) {
p.angle += _rotationAngle * PI / 180.0f;
} else if (rightReading > leftReading && rightReading > centerReading) {
p.angle -= _rotationAngle * PI / 180.0f;
}
}
void SlimeMold::depositTrail(float x, float y) {
// Convert to grid coordinates
uint16_t gridX = (uint16_t)(x / GRID_SCALE);
uint16_t gridY = (uint16_t)(y / GRID_SCALE);
// Ensure within bounds
if (gridX >= _gridW || gridY >= _gridH) return;
uint16_t idx = gridY * _gridW + gridX;
// Calculate local trail density
float localDensity = 0;
int samples = 0;
for (int8_t dy = -1; dy <= 1; dy++) {
for (int8_t dx = -1; dx <= 1; dx++) {
uint16_t sampleX = (gridX + dx + _gridW) % _gridW;
uint16_t sampleY = (gridY + dy + _gridH) % _gridH;
localDensity += _trailMap[sampleY * _gridW + sampleX];
samples++;
}
}
localDensity /= samples;
// Adjust deposit amount based on local density
uint8_t depositAmount = _foodDepositAmount;
if (localDensity > 200) {
// Reduce deposit in dense areas to prevent stagnation
depositAmount = max(1, depositAmount / 2);
} else if (localDensity < 50) {
// Increase deposit in sparse areas to encourage exploration
depositAmount = min(255, depositAmount * 2);
}
// Add some randomness to deposit amount
depositAmount = depositAmount + random(-depositAmount/4, depositAmount/4);
// Apply the deposit with bounds checking
uint16_t newValue = _trailMap[idx] + depositAmount;
_trailMap[idx] = newValue > 255 ? 255 : newValue;
}
void SlimeMold::consumeFood(float x, float y) {
// Convert to grid coordinates
uint16_t gridX = (uint16_t)(x / GRID_SCALE);
uint16_t gridY = (uint16_t)(y / GRID_SCALE);
// Ensure within bounds
if (gridX >= _gridW || gridY >= _gridH) return;
// Consume food
uint16_t idx = gridY * _gridW + gridX;
if (_foodMap[idx] >= _foodConsumptionRate) {
_foodMap[idx] -= _foodConsumptionRate;
// Deposit more trail when food is found
_trailMap[idx] = min(255, _trailMap[idx] + _foodDepositAmount * 2);
}
}
void SlimeMold::moveParticle(Particle& p) {
if (!p.active) return;
// Sense and rotate
rotateBasedOnSensors(p);
// Store old position for clearing
float oldX = p.x;
float oldY = p.y;
// Update position
p.x += cos(p.angle) * _moveSpeed;
p.y += sin(p.angle) * _moveSpeed;
// Add small random angle change
p.angle += (random(100) - 50) * 0.01f;
// Wrap around screen edges
p.x = fmod(p.x + _screenW, _screenW);
p.y = fmod(p.y + _screenH, _screenH);
// Interact with environment
depositTrail(p.x, p.y);
consumeFood(p.x, p.y);
// Draw particle
_panel.fillRect(oldX, oldY, _particleSize, _particleSize, 0x0000);
// Get trail and food intensity at particle position
uint16_t gridX = (uint16_t)(p.x / GRID_SCALE);
uint16_t gridY = (uint16_t)(p.y / GRID_SCALE);
uint16_t idx = gridY * _gridW + gridX;
uint8_t trailIntensity = _trailMap[idx];
float foodLevel = _foodMap[idx] / 255.0f;
// Calculate particle color using same gradient logic as trails
SlimeMold::Color particleColor;
if (trailIntensity < 128) {
float t = trailIntensity / 127.0f;
t = t * t * (3 - 2 * t); // Smooth step function
particleColor = lerpColor(_searchColor, _baseColor, t);
} else {
float t = (trailIntensity - 128) / 127.0f;
t = t * t * (3 - 2 * t); // Smooth step function
particleColor = lerpColor(_baseColor, _foodColor, t);
}
// More subtle food color blending
if (foodLevel > 0.2f) {
float blendAmount = foodLevel * 0.3f; // Reduced from 0.5f
particleColor = lerpColor(particleColor, _foodColor, blendAmount);
}
// Very subtle brightness boost
particleColor = {
(uint8_t)min(255, (int)(particleColor.r * 1.05f)),
(uint8_t)min(255, (int)(particleColor.g * 1.05f)),
(uint8_t)min(255, (int)(particleColor.b * 1.05f))
};
uint16_t color = colorToRGB565(particleColor, 255);
_panel.fillRect(p.x, p.y, _particleSize, _particleSize, color);
}
void SlimeMold::updateParticles() {
for (uint16_t i = 0; i < _activeParticles; i++) {
moveParticle(_particles[i]);
}
}
void SlimeMold::drawTrails() {
// Draw trail map to screen
for (uint16_t y = 0; y < _gridH; y++) {
for (uint16_t x = 0; x < _gridW; x++) {
uint8_t intensity = _trailMap[y * _gridW + x];
if (intensity > 0) {
float foodLevel = _foodMap[y * _gridW + x] / 255.0f;
SlimeMold::Color trailColor;
// Create a three-color gradient based on intensity
if (intensity < 128) {
float t = intensity / 127.0f;
t = t * t * (3 - 2 * t); // Smooth step function
trailColor = lerpColor(_searchColor, _baseColor, t);
} else {
float t = (intensity - 128) / 127.0f;
t = t * t * (3 - 2 * t); // Smooth step function
trailColor = lerpColor(_baseColor, _foodColor, t);
}
// More subtle food color blending
if (foodLevel > 0.2f) {
float blendAmount = foodLevel * 0.3f; // Reduced from 0.5f
trailColor = lerpColor(trailColor, _foodColor, blendAmount);
}
// Apply gamma correction to intensity
float gamma = 2.2f;
float normalizedIntensity = pow(intensity / 255.0f, 1.0f / gamma);
uint8_t correctedIntensity = (uint8_t)(normalizedIntensity * 255);
uint16_t color = colorToRGB565(trailColor, correctedIntensity);
_panel.fillRect(x * GRID_SCALE, y * GRID_SCALE,
GRID_SCALE, GRID_SCALE, color);
}
}
}
}
void SlimeMold::updateTrails() {
// Dynamic trail decay with focus on breaking up strong trails
static uint8_t decayVariation = 1;
static uint32_t lastMajorDecay = 0;
uint32_t currentTime = millis();
// Periodically trigger major decay of strong trails
if (currentTime - lastMajorDecay > 3000) { // Every 3 seconds (reduced from 5)
lastMajorDecay = currentTime;
for (uint32_t i = 0; i < _gridW * _gridH; i++) {
if (_trailMap[i] > 180) { // Target strong trails (reduced threshold)
uint8_t decayAmount = random(70, 120); // Increased decay amount
_trailMap[i] = (decayAmount >= _trailMap[i]) ? 0 : _trailMap[i] - decayAmount;
}
}
}
// Regular decay with intensity-based variation
for (uint32_t i = 0; i < _gridW * _gridH; i++) {
if (_trailMap[i] > 0) {
// Higher intensity trails decay faster
uint8_t baseDecay = 1 + (_trailMap[i] > 100 ? 3 : 0); // Increased base decay
uint8_t decayAmount = baseDecay;
// Random additional decay
if (_trailMap[i] > 180 && random(100) < 30) { // 30% chance for strong trails
decayAmount += random(5, 12); // Increased random decay
} else if (random(100) < 10) { // 10% chance for normal trails
decayAmount += random(2, 6);
}
// Apply decay with bounds check
_trailMap[i] = (decayAmount >= _trailMap[i]) ? 0 : _trailMap[i] - decayAmount;
}
}
}
void SlimeMold::updateFood() {
static uint32_t lastSpawnTime = 0;
uint32_t currentTime = millis();
// Only attempt food spawning every 1000ms
if (currentTime - lastSpawnTime > 1000) {
lastSpawnTime = currentTime;
// Occasionally spawn a new food cluster
if (random(100) < _foodRegenChance) { // Dynamic chance each second
uint16_t centerX = random(_gridW);
uint16_t centerY = random(_gridH);
// Create food cluster
for (int8_t dy = -_foodClusterRadius; dy <= _foodClusterRadius; dy++) {
for (int8_t dx = -_foodClusterRadius; dx <= _foodClusterRadius; dx++) {
float dist = sqrt(dx*dx + dy*dy);
if (dist <= _foodClusterRadius) {
uint16_t x = (centerX + dx + _gridW) % _gridW;
uint16_t y = (centerY + dy + _gridH) % _gridH;
uint16_t idx = y * _gridW + x;
if (_foodMap[idx] < 100) { // Only spawn in areas with low food
_foodMap[idx] = random(150, 200) * (1.0f - dist/_foodClusterRadius);
}
}
}
}
}
}
}
uint16_t SlimeMold::colorToRGB565(const Color& color, uint8_t intensity) const {
// Use gamma correction for smoother intensity scaling
float gamma = 2.2f;
float normalizedIntensity = pow(intensity / 255.0f, 1.0f / gamma);
// Scale RGB values with gamma-corrected intensity
uint8_t r = (uint8_t)(color.r * normalizedIntensity);
uint8_t g = (uint8_t)(color.g * normalizedIntensity);
uint8_t b = (uint8_t)(color.b * normalizedIntensity);
return _panel.color565(r, g, b);
}
SlimeMold::Color SlimeMold::lerpColor(const Color& a, const Color& b, float t) const {
// Use smooth step function for more natural transitions
t = max(0.0f, min(1.0f, t));
t = t * t * (3 - 2 * t); // Smooth step function
// Linear interpolation between colors with value clamping
return SlimeMold::Color{
(uint8_t)max(0, min(255, (int)(a.r + (b.r - a.r) * t))),
(uint8_t)max(0, min(255, (int)(a.g + (b.g - a.g) * t))),
(uint8_t)max(0, min(255, (int)(a.b + (b.b - a.b) * t)))
};
}
uint16_t SlimeMold::calculateStateHash() const {
// Calculate a simple hash of the trail map state
uint16_t hash = 0;
const uint16_t stride = 8; // Sample every 8th pixel for performance
for (uint16_t i = 0; i < _gridW * _gridH; i += stride) {
hash = (hash * 17 + _trailMap[i]) & 0xFFFF;
}
return hash;
}
void SlimeMold::updateDynamicParameters() {
// More dramatic parameter variations
if (_stalenessCounter > 2) { // Lower threshold
// Significant variability when patterns start becoming stale
_moveSpeed = 4 + random(-3, 4); // 1-8
_sensorAngle = 60 + random(-30, 31); // 30-90 degrees
_sensorDistance = 15 + random(-7, 8); // 8-23
_rotationAngle = 25 + random(-15, 16); // 10-40 degrees
_foodRegenChance = min(20, _foodRegenChance + 2); // More aggressive food spawn rate
// Occasionally make more extreme changes
if (random(100) < 30) { // 30% chance
_moveSpeed = random(_tuning.perturbSpeedMin, _tuning.perturbSpeedMax);
_sensorAngle = random(30, 120); // Much wider angle range
_rotationAngle = random(10, 45); // More rotation variation
}
_stalenessCounter = 0;
} else {
// Normal parameter variations with more range
_moveSpeed = 4 + random(-2, 3); // 2-7
_sensorAngle = 60 + random(-15, 16); // 45-75 degrees
_sensorDistance = 15 + random(-4, 5); // 11-20
_rotationAngle = 25 + random(-10, 11); // 15-35 degrees
}
}
void SlimeMold::perturbSystem() {
// Add more dynamic perturbations to break static patterns
for (uint16_t i = 0; i < _activeParticles; i++) {
if (random(100) < 40) { // Increased chance to 40%
// More varied angle changes
if (random(100) < 30) { // 30% chance for major direction change
_particles[i].angle = random(360) * PI / 180.0f; // Complete random direction
} else {
_particles[i].angle += random(-PI/2, PI/2); // Wider angle variation
}
// More varied position changes
float jumpDistance = random(5, _tuning.jumpDistanceMax); // Variable jump distance
float jumpAngle = random(360) * PI / 180.0f;
_particles[i].x += cos(jumpAngle) * jumpDistance;
_particles[i].y += sin(jumpAngle) * jumpDistance;
// Wrap around screen edges
_particles[i].x = fmod(_particles[i].x + _screenW, _screenW);
_particles[i].y = fmod(_particles[i].y + _screenH, _screenH);
}
}
// Create more varied food spots
uint8_t numSpots = random(3, _tuning.foodSpotsMax); // Variable number of food spots
for (uint8_t i = 0; i < numSpots; i++) {
uint16_t x = random(_gridW);
uint16_t y = random(_gridH);
uint8_t radius = random(1, _tuning.foodSpotRadiusMax); // Variable size food spots
// Create small food clusters
for (int8_t dy = -radius; dy <= radius; dy++) {
for (int8_t dx = -radius; dx <= radius; dx++) {
if (dx*dx + dy*dy <= radius*radius) {
uint16_t fx = (x + dx + _gridW) % _gridW;
uint16_t fy = (y + dy + _gridH) % _gridH;
_foodMap[fy * _gridW + fx] = random(150, 250); // More varied food amounts
}
}
}
}
}
void SlimeMold::update() {
uint32_t currentTime = millis();
// Check for static patterns more aggressively
if (currentTime - _lastStateCheckTime >= STATE_CHECK_INTERVAL) {
uint16_t currentHash = calculateStateHash();
if (abs((int)currentHash - (int)_previousStateHash) < 2000) { // Increased threshold
_stalenessCounter += 2; // Faster staleness accumulation
} else {
_stalenessCounter = max(0, _stalenessCounter - 1);
}
_previousStateHash = currentHash;
_lastStateCheckTime = currentTime;
// More frequent perturbation
if (_stalenessCounter > 3) { // Reduced threshold
perturbSystem();
_stalenessCounter = 0;
}
}
// Update dynamic parameters periodically
if (currentTime - _lastParamUpdateTime >= PARAM_UPDATE_INTERVAL) {
updateDynamicParameters();
_lastParamUpdateTime = currentTime;
}
// Update particle positions and environment
updateParticles();
updateTrails();
updateFood();
// Draw everything
drawTrails();
// Small delay
delay(20);
}