-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabsorption-2.js
More file actions
1146 lines (1005 loc) · 31.4 KB
/
absorption-2.js
File metadata and controls
1146 lines (1005 loc) · 31.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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// CONSTANTS
const CONSTANTS = {
// World parameters
WORLD: {
WIDTH: 256, // Grid cells wide
HEIGHT: 144, // Grid cells tall
SCALE_SIZE: 3, // Size of each cell in pixels
TICK_INTERVAL: 40, // Milliseconds between ticks in play mode
},
// Flux parameters
FLUX: {
P_SUN: 0.005, // Probability of sun spawning per claimed void per tick (dramatically slower)
N_WATER_INITIAL: 50, // Initial number of water particles
EVAPORATION_CHANCE: 0.001, // Water -> Vapor chance
CONDENSATION_CHANCE: 0.01, // Vapor -> Water chance near other vapor
},
// Growth parameters
GROWTH: {
ENERGY_TO_GROW: 5, // Energy needed to grow
GROWTH_COST: 3, // Energy consumed when growing
MAX_ENERGY: 20, // Maximum energy a plant can store
},
// Visual parameters
COLORS: {
SUN: { r: 255, g: 255, b: 0, alpha: 0.8 },
WATER: { r: 0, g: 100, b: 255, alpha: 0.8 },
VAPOR: { r: 200, g: 255, b: 255, alpha: 0.6 },
SEED: { r: 139, g: 69, b: 19, alpha: 1.0 },
STEM: { r: 34, g: 139, b: 34, alpha: 1.0 },
LEAF: { r: 0, g: 255, b: 0, alpha: 1.0 },
FLOWER: { r: 255, g: 105, b: 180, alpha: 1.0 },
SOIL: { r: 101, g: 67, b: 33, alpha: 1.0 },
},
// Genetics parameters
GENETICS: {
MUTATION_RATE: 0.1, // 10% chance of mutation per gene
MUTATION_STRENGTH: 0.2, // How much genes can change (±20%)
TRAIT_COUNT: 8, // Number of genetic traits
},
// Evolution parameters
EVOLUTION: {
REPRODUCTION_SUN: 25, // Sun energy needed to reproduce
REPRODUCTION_WATER: 25, // Water energy needed to reproduce
SEED_ENERGY: 8, // Energy given to each seed - enough to grow first stem and leaf
MAX_SEEDS: 3, // Maximum seeds per reproduction
LIFESPAN: 50000, // Maximum age before death (extremely long life)
},
};
// Particle modes
const Mode = {
SUN: "SUN",
WATER: "WATER",
VAPOR: "VAPOR",
SEED: "SEED",
STEM: "STEM",
LEAF: "LEAF",
FLOWER: "FLOWER",
SOIL: "SOIL",
};
// Plant lifecycle stages
const PlantStage = {
SEED: "SEED",
GERMINATING: "GERMINATING",
STEM_GROWTH: "STEM_GROWTH",
LEAF_DEVELOPMENT: "LEAF_DEVELOPMENT",
MATURE: "MATURE",
FLOWERING: "FLOWERING",
REPRODUCING: "REPRODUCING",
DEAD: "DEAD",
};
// Global variables
let app, cellTextures, particles, occupancyGrid, scaleSize, idCounter;
let rows, cols, frame;
let fpsText, countText;
let paused = false; // Start unpaused by default
let tickInterval;
// Crown shyness - track claimed void cells by plant ID
let claimedCells = []; // Array to track which plant owns each void cell
let plantIdCounter = 1; // Separate counter for plant IDs
// Initialize global variables
idCounter = 1;
frame = 0;
// Simulation parameters
const NUM_INITIAL_WATER = 343; // Much more water for better distribution
const NUM_INITIAL_PLANTS = 5;
// Fast-forward settings
let fastForward = false;
let fastForwardFactor = 10;
document.addEventListener("DOMContentLoaded", async () => {
app = new PIXI.Application({
width: window.innerWidth,
height: window.innerHeight,
backgroundColor: 0x000000,
clearBeforeRender: true,
});
document.getElementById("canvas-div").appendChild(app.view);
// Helper function to convert RGB values to hex
function rgbToHex(r, g, b) {
return (r << 16) + (g << 8) + b;
}
// Generate textures
cellTextures = Object.entries(CONSTANTS.COLORS).reduce(
(acc, [type, { r, g, b, alpha }]) => {
const graphics = new PIXI.Graphics();
const hexColor = rgbToHex(r, g, b);
graphics.beginFill(hexColor, alpha);
graphics.drawRect(0, 0, 1, 1);
graphics.endFill();
acc[type] = app.renderer.generateTexture(graphics);
return acc;
},
{}
);
// Setup performance monitoring
const textStyle = new PIXI.TextStyle({
fontFamily: "Arial",
fontSize: 24,
fill: "white",
});
fpsText = new PIXI.Text("Paused", textStyle);
countText = new PIXI.Text("Cells: 0", textStyle);
fpsText.x = 10;
fpsText.y = 10;
countText.x = 10;
countText.y = 40;
app.stage.addChild(fpsText);
app.stage.addChild(countText);
// Core simulation parameters
scaleSize = CONSTANTS.WORLD.SCALE_SIZE;
cols = CONSTANTS.WORLD.WIDTH;
rows = CONSTANTS.WORLD.HEIGHT;
particles = [];
// Initialize occupancy grid
occupancyGrid = new OccupancyGrid(cols, rows);
// Initialize claimed cells grid
claimedCells = new Array(cols * rows).fill(null);
// Initialize particles
initializeSimulation();
// Log world details
console.log(`World initialized:
- Dimensions: ${cols}x${rows} cells
- Cell size: ${scaleSize}px
- Initial water particles: ${NUM_INITIAL_WATER}
- Initial plants: ${NUM_INITIAL_PLANTS}`);
// Start simulation
startMainLoop();
fpsText.text = "Running";
// Add event listeners
document.addEventListener("keydown", (e) => {
if (e.key === " ") {
if (paused) {
advanceTick();
}
e.preventDefault();
}
if (e.key === "p" || e.key === "P") {
paused = !paused;
fpsText.text = paused ? "Paused" : "Running";
}
if (e.key === "f" || e.key === "F") {
fastForward = !fastForward;
}
});
// Add click listener for debugging
app.view.addEventListener("click", (e) => {
const rect = app.view.getBoundingClientRect();
const x = Math.floor((e.clientX - rect.left) / scaleSize);
const y = Math.floor((e.clientY - rect.top) / scaleSize);
const particle = occupancyGrid.get(x, y);
if (particle) {
console.log(`Particle at (${x},${y}):`, {
mode: particle.mode,
storedSun: particle.storedSun,
storedWater: particle.storedWater,
plantId: particle.plantId,
id: particle.id,
});
} else {
console.log(`Empty space at (${x},${y})`);
}
});
});
function initializeSimulation() {
// Create initial water particles spread randomly across all unoccupied cells
for (let i = 0; i < NUM_INITIAL_WATER; i++) {
let attempts = 0;
let x, y;
do {
x = Math.floor(Math.random() * cols);
y = Math.floor(Math.random() * rows);
attempts++;
} while (occupancyGrid.isOccupied(x, y) && attempts < 100);
if (attempts < 100) {
const water = new Particle(x, y, Mode.WATER);
particles.push(water);
occupancyGrid.set(x, y, water);
}
}
// Create initial seeds with random genetics - can be placed anywhere with empty space around them
for (let i = 0; i < NUM_INITIAL_PLANTS; i++) {
let attempts = 0;
let x, y;
do {
x = Math.floor(Math.random() * cols);
y = Math.floor(Math.random() * rows);
attempts++;
} while (
(occupancyGrid.isOccupied(x, y) ||
!occupancyGrid.isEmptyMooreNeighborhood(x, y)) &&
attempts < 100
);
if (attempts < 100) {
const seed = new Particle(x, y, Mode.SEED);
seed.genetics = new PlantGenetics(); // Random first generation genetics
seed.storedSun = CONSTANTS.EVOLUTION.SEED_ENERGY;
seed.storedWater = CONSTANTS.EVOLUTION.SEED_ENERGY;
particles.push(seed);
occupancyGrid.set(x, y, seed);
}
}
}
function startMainLoop() {
function mainLoop() {
const updatesThisFrame = fastForward ? fastForwardFactor : 1;
if (!paused) {
for (let i = 0; i < updatesThisFrame; i++) {
advanceTick();
}
}
render();
requestAnimationFrame(mainLoop);
}
mainLoop();
}
function advanceTick() {
frame++;
// Spawn sun particles
spawnSunParticles();
// Update all particles
for (let i = particles.length - 1; i >= 0; i--) {
const particle = particles[i];
if (particle && !particle.destroyed) {
particle.update();
}
}
// Remove destroyed particles
particles = particles.filter((p) => !p.destroyed);
}
function spawnSunParticles() {
// For each claimed void cell, check if it should spawn a sun particle
for (let i = 0; i < claimedCells.length; i++) {
const plantId = claimedCells[i];
if (plantId !== null && Math.random() < CONSTANTS.FLUX.P_SUN) {
const x = i % cols;
const y = Math.floor(i / cols);
// Make sure the cell is still void
if (!occupancyGrid.get(x, y)) {
const sun = new Particle(x, y, Mode.SUN);
sun.targetPlantId = plantId;
particles.push(sun);
// Don't register sun in occupancy grid - it moves through cells
}
}
}
}
function render() {
const plantCount = particles.filter((p) =>
[Mode.SEED, Mode.STEM, Mode.LEAF, Mode.FLOWER].includes(p.mode)
).length;
countText.text = `Particles: ${particles.length} | Plants: ${plantCount} | Frame: ${frame}`;
app.renderer.render(app.stage);
}
// Plant Genetics System
class PlantGenetics {
constructor(parentA = null, parentB = null) {
if (parentA && parentB) {
// Sexual reproduction - combine traits from two parents
this.traits = this.combineParents(parentA, parentB);
} else if (parentA) {
// Asexual reproduction - inherit from single parent
this.traits = this.inheritFromParent(parentA);
} else {
// Random genesis - first generation
this.traits = this.generateRandom();
}
// Apply mutations
this.mutate();
// Calculate fitness-based color
this.calculateColor();
}
generateRandom() {
return {
growthSpeed: 0.5 + Math.random() * 0.5, // 0.5-1.0: How fast the plant grows
energyEfficiency: 0.5 + Math.random() * 0.5, // 0.5-1.0: How efficiently it uses energy
reproductionRate: 0.3 + Math.random() * 0.4, // 0.3-0.7: How quickly it reproduces
leafCount: Math.floor(2 + Math.random() * 4), // 2-5: Number of leaves to grow
stemHeight: Math.floor(3 + Math.random() * 5), // 3-7: Maximum stem height
flowerSize: 0.5 + Math.random() * 0.5, // 0.5-1.0: Flower size affects seed count
droughtTolerance: Math.random(), // 0-1: Tolerance to low water
coldTolerance: Math.random(), // 0-1: Tolerance to harsh conditions
};
}
inheritFromParent(parent) {
return {
growthSpeed: parent.traits.growthSpeed,
energyEfficiency: parent.traits.energyEfficiency,
reproductionRate: parent.traits.reproductionRate,
leafCount: parent.traits.leafCount,
stemHeight: parent.traits.stemHeight,
flowerSize: parent.traits.flowerSize,
droughtTolerance: parent.traits.droughtTolerance,
coldTolerance: parent.traits.coldTolerance,
};
}
combineParents(parentA, parentB) {
// Simple genetic crossover - randomly pick traits from each parent
return {
growthSpeed:
Math.random() < 0.5
? parentA.traits.growthSpeed
: parentB.traits.growthSpeed,
energyEfficiency:
Math.random() < 0.5
? parentA.traits.energyEfficiency
: parentB.traits.energyEfficiency,
reproductionRate:
Math.random() < 0.5
? parentA.traits.reproductionRate
: parentB.traits.reproductionRate,
leafCount:
Math.random() < 0.5
? parentA.traits.leafCount
: parentB.traits.leafCount,
stemHeight:
Math.random() < 0.5
? parentA.traits.stemHeight
: parentB.traits.stemHeight,
flowerSize:
Math.random() < 0.5
? parentA.traits.flowerSize
: parentB.traits.flowerSize,
droughtTolerance:
Math.random() < 0.5
? parentA.traits.droughtTolerance
: parentB.traits.droughtTolerance,
coldTolerance:
Math.random() < 0.5
? parentA.traits.coldTolerance
: parentB.traits.coldTolerance,
};
}
mutate() {
Object.keys(this.traits).forEach((trait) => {
if (Math.random() < CONSTANTS.GENETICS.MUTATION_RATE) {
const change =
(Math.random() - 0.5) * 2 * CONSTANTS.GENETICS.MUTATION_STRENGTH;
this.traits[trait] += change;
// Clamp values to reasonable ranges
if (trait === "leafCount" || trait === "stemHeight") {
this.traits[trait] = Math.max(1, Math.floor(this.traits[trait]));
} else {
this.traits[trait] = Math.max(0.1, Math.min(1.0, this.traits[trait]));
}
}
});
}
calculateColor() {
// Calculate fitness score (higher is better)
const fitness =
(this.traits.growthSpeed +
this.traits.energyEfficiency +
this.traits.reproductionRate +
this.traits.droughtTolerance +
this.traits.coldTolerance) /
5;
// Color plants based on fitness - rare high-fitness plants get special colors
if (fitness > 0.9) {
this.color = { r: 255, g: 215, b: 0 }; // Gold for exceptional plants
} else if (fitness > 0.8) {
this.color = { r: 255, g: 165, b: 0 }; // Orange for high fitness
} else if (fitness > 0.7) {
this.color = { r: 0, g: 255, b: 0 }; // Bright green for good fitness
} else {
this.color = { r: 34, g: 139, b: 34 }; // Standard green for average fitness
}
}
getFitness() {
return (
(this.traits.growthSpeed +
this.traits.energyEfficiency +
this.traits.reproductionRate +
this.traits.droughtTolerance +
this.traits.coldTolerance) /
5
);
}
}
class OccupancyGrid {
constructor(cols, rows) {
this.cols = cols;
this.rows = rows;
this.grid = new Array(cols * rows).fill(null);
}
getIndex(x, y) {
return y * this.cols + x;
}
set(x, y, particle) {
if (x >= 0 && x < this.cols && y >= 0 && y < this.rows) {
this.grid[this.getIndex(x, y)] = particle;
}
}
get(x, y) {
if (x >= 0 && x < this.cols && y >= 0 && y < this.rows) {
return this.grid[this.getIndex(x, y)];
}
return null;
}
remove(x, y) {
if (x >= 0 && x < this.cols && y >= 0 && y < this.rows) {
this.grid[this.getIndex(x, y)] = null;
}
}
clear() {
this.grid.fill(null);
}
isOccupied(x, y) {
if (x < 0 || x >= this.cols || y < 0 || y >= this.rows) return true;
return this.get(x, y) !== null;
}
// Get all neighbors in radius (including empty spaces for energy calculation)
getNeighborsInRadius(x, y, radius) {
let neighbors = [];
for (let dx = -radius; dx <= radius; dx++) {
for (let dy = -radius; dy <= radius; dy++) {
if (dx === 0 && dy === 0) continue;
let nx = x + dx;
let ny = y + dy;
if (nx >= 0 && nx < this.cols && ny >= 0 && ny < this.rows) {
const particle = this.get(nx, ny);
if (particle !== null) {
neighbors.push(particle);
}
}
}
}
return neighbors;
}
// Count empty cardinal neighbors (for energy collection)
getCardinalEmptySpaces(x, y) {
const cardinals = [
{ dx: 0, dy: -1 },
{ dx: 1, dy: 0 },
{ dx: 0, dy: 1 },
{ dx: -1, dy: 0 },
];
let emptySpaces = 0;
cardinals.forEach(({ dx, dy }) => {
const nx = x + dx,
ny = y + dy;
if (nx >= 0 && nx < this.cols && ny >= 0 && ny < this.rows) {
if (!this.get(nx, ny)) emptySpaces++;
}
});
return emptySpaces;
}
// Check if all Moore neighborhood cells are empty
isEmptyMooreNeighborhood(x, y) {
for (let dx = -1; dx <= 1; dx++) {
for (let dy = -1; dy <= 1; dy++) {
if (dx === 0 && dy === 0) continue;
const nx = x + dx,
ny = y + dy;
if (nx < 0 || nx >= this.cols || ny < 0 || ny >= this.rows) continue;
if (this.get(nx, ny)) return false;
}
}
return true;
}
}
// Evolutionary Angiosperm Particle class
class Particle {
constructor(x, y, mode) {
this.id = idCounter++;
this.x = x;
this.y = y;
this.mode = mode;
this.destroyed = false;
this.energy = 0;
this.plantId = null;
this.targetPlantId = null;
this.momentum = 0;
this.age = 0;
// Plant-specific properties
this.genetics = null;
this.plantStage = null;
this.parentId = null;
this.stemHeight = 0;
this.leavesGrown = 0;
this.reproductionCooldown = 0;
this.bodyParts = []; // References to stem/leaf particles that belong to this plant
this.storedSun = 0; // Separate sun energy storage
this.storedWater = 0; // Separate water energy storage
// Set up visual representation
this.sprite = new PIXI.Sprite(cellTextures[mode]);
this.sprite.x = x * scaleSize;
this.sprite.y = y * scaleSize;
this.sprite.scale.set(scaleSize, scaleSize);
app.stage.addChild(this.sprite);
// Initialize based on mode
if (mode === Mode.SEED) {
this.plantStage = PlantStage.SEED;
this.plantId = plantIdCounter++;
}
}
updatePlantColor() {
// Update sprite color based on genetics if this is a plant part
if (
this.genetics &&
[Mode.SEED, Mode.STEM, Mode.LEAF, Mode.FLOWER].includes(this.mode)
) {
const color = this.genetics.color;
this.sprite.tint = (color.r << 16) + (color.g << 8) + color.b;
}
}
claimTerritory() {
// Claim adjacent cells for crown shyness
const directions = [
{ dx: 0, dy: -1 },
{ dx: 1, dy: 0 },
{ dx: 0, dy: 1 },
{ dx: -1, dy: 0 },
];
for (const { dx, dy } of directions) {
const nx = this.x + dx;
const ny = this.y + dy;
if (nx >= 0 && nx < cols && ny >= 0 && ny < rows) {
const index = ny * cols + nx;
if (!occupancyGrid.get(nx, ny)) {
claimedCells[index] = this.plantId;
}
}
}
}
destroy() {
this.destroyed = true;
if (this.sprite.parent) {
app.stage.removeChild(this.sprite);
}
occupancyGrid.remove(this.x, this.y);
// If this is a seed/main plant, destroy all body parts
if (this.bodyParts && this.bodyParts.length > 0) {
this.bodyParts.forEach((part) => {
if (!part.destroyed) {
part.destroy();
}
});
}
}
moveTo(newX, newY) {
occupancyGrid.remove(this.x, this.y);
this.x = newX;
this.y = newY;
this.sprite.x = newX * scaleSize;
this.sprite.y = newY * scaleSize;
occupancyGrid.set(newX, newY, this);
}
update() {
this.age++;
switch (this.mode) {
case Mode.WATER:
this.updateWater();
break;
case Mode.SUN:
this.updateSun();
break;
case Mode.VAPOR:
this.updateVapor();
break;
case Mode.SEED:
this.updateSeed();
break;
case Mode.STEM:
case Mode.LEAF:
case Mode.FLOWER:
this.updatePlantPart();
break;
case Mode.SOIL:
// Static
break;
}
}
updateWater() {
// Water falls down, with momentum for fluid dynamics
let moved = false;
// 10% chance to move diagonally down instead of straight down
const moveDirections = [];
if (Math.random() < 0.1) {
// Random diagonal movement - down-left or down-right
const diagDir = Math.random() < 0.5 ? -1 : 1;
moveDirections.push({ dx: diagDir, dy: 1 }); // Diagonal first
moveDirections.push({ dx: 0, dy: 1 }); // Straight down as backup
} else {
// Normal downward movement
moveDirections.push({ dx: 0, dy: 1 }); // Straight down first
// Add diagonal options as backups
if (this.momentum === -1) {
moveDirections.push({ dx: -1, dy: 1 });
moveDirections.push({ dx: 1, dy: 1 });
} else if (this.momentum === 1) {
moveDirections.push({ dx: 1, dy: 1 });
moveDirections.push({ dx: -1, dy: 1 });
} else {
const randomDir = Math.random() < 0.5 ? -1 : 1;
moveDirections.push({ dx: randomDir, dy: 1 });
moveDirections.push({ dx: -randomDir, dy: 1 });
}
}
// Try each movement direction with vertical torus topology
for (const { dx, dy } of moveDirections) {
const nextY = this.y + dy;
const wrappedY = nextY >= rows ? 0 : nextY; // Wrap to top if at bottom
const newX = this.x + dx;
if (newX >= 0 && newX < cols && !occupancyGrid.get(newX, wrappedY)) {
this.moveTo(newX, wrappedY);
this.momentum = dx;
moved = true;
break;
}
}
// If can't move down, try lateral movement
if (!moved && this.momentum !== 0) {
const newX = this.x + this.momentum;
if (newX >= 0 && newX < cols && !occupancyGrid.get(newX, this.y)) {
this.moveTo(newX, this.y);
moved = true;
}
}
// Check for absorption by plants
this.checkWaterAbsorption();
}
checkWaterAbsorption() {
// Check if we're in a claimed territory
const index = this.y * cols + this.x;
const plantId = claimedCells[index];
if (plantId !== null) {
// Find the seed/plant and give it water energy
const plant = particles.find(
(p) => p.plantId === plantId && p.mode === Mode.SEED
);
if (
plant &&
plant.genetics &&
plant.storedWater < CONSTANTS.EVOLUTION.REPRODUCTION_WATER + 10
) {
const waterValue = 1 * plant.genetics.traits.energyEfficiency;
plant.storedWater += waterValue;
this.destroy();
}
}
}
updateSun() {
// Sun moves toward its target plant
if (this.targetPlantId) {
const targetPlant = particles.find(
(p) => p.plantId === this.targetPlantId && p.mode === Mode.SEED
);
// If target plant no longer exists, destroy this sun particle
if (!targetPlant) {
this.destroy();
return;
}
const dx = Math.sign(targetPlant.x - this.x);
const dy = Math.sign(targetPlant.y - this.y);
const newX = this.x + dx;
const newY = this.y + dy;
// Check if we hit any plant part
const hitParticle = occupancyGrid.get(newX, newY);
if (hitParticle && hitParticle.plantId === this.targetPlantId) {
// Check if plant is oversaturated (has maximum sun energy)
if (
targetPlant.storedSun >=
CONSTANTS.EVOLUTION.REPRODUCTION_SUN + 10
) {
// Plant is oversaturated - create twinkling effect and destroy
this.createTwinkleEffect();
this.destroy();
return;
}
// Give sun energy to the main seed/plant and trigger respiration
if (targetPlant.genetics) {
const sunValue = 1 * targetPlant.genetics.traits.energyEfficiency;
targetPlant.storedSun += sunValue;
// Trigger respiration if this sun hit a leaf
if (hitParticle.mode === Mode.LEAF) {
this.triggerRespiration(hitParticle);
}
}
this.destroy();
return;
}
// Check if we hit another plant (shadow effect)
if (
hitParticle &&
[Mode.SEED, Mode.STEM, Mode.LEAF, Mode.FLOWER].includes(
hitParticle.mode
)
) {
this.destroy();
return;
}
// Move toward target
if (newX >= 0 && newX < cols && newY >= 0 && newY < rows) {
this.x = newX;
this.y = newY;
this.sprite.x = newX * scaleSize;
this.sprite.y = newY * scaleSize;
} else {
// Sun particle went out of bounds, destroy it
this.destroy();
return;
}
}
// Age the sun particle and destroy if too old (prevents infinite buildup)
if (this.age > 100) {
this.destroy();
}
}
createTwinkleEffect() {
// Create a longer visual effect for oversaturated plants with opacity fade
const twinkle = new PIXI.Graphics();
twinkle.beginFill(0xffff00, 0.8); // Bright yellow
twinkle.drawRect(0, 0, scaleSize, scaleSize);
twinkle.endFill();
twinkle.x = this.x * scaleSize;
twinkle.y = this.y * scaleSize;
app.stage.addChild(twinkle);
// Fade out over 10 frames
let frame = 0;
const fadeInterval = setInterval(() => {
frame++;
const opacity = 0.8 * (1 - frame / 10); // Fade from 0.8 to 0
twinkle.alpha = Math.max(0, opacity);
if (frame >= 10) {
clearInterval(fadeInterval);
if (twinkle.parent) {
app.stage.removeChild(twinkle);
}
}
}, CONSTANTS.WORLD.TICK_INTERVAL);
}
triggerRespiration(leafParticle) {
// Find the seed/plant that owns this leaf
const plant = particles.find(
(p) => p.plantId === leafParticle.plantId && p.mode === Mode.SEED
);
if (plant && plant.storedWater >= 1) {
// Consume 1 water energy and emit vapor
plant.storedWater -= 1;
// Try to emit vapor in an adjacent empty space
const directions = [
{ dx: 0, dy: -1 },
{ dx: 1, dy: 0 },
{ dx: 0, dy: 1 },
{ dx: -1, dy: 0 },
];
for (const { dx, dy } of directions) {
const nx = leafParticle.x + dx;
const ny = leafParticle.y + dy;
if (
nx >= 0 &&
nx < cols &&
ny >= 0 &&
ny < rows &&
!occupancyGrid.get(nx, ny)
) {
const vapor = new Particle(nx, ny, Mode.VAPOR);
particles.push(vapor);
break;
}
}
}
}
updateVapor() {
// Random walk with 2x upward bias
const directions = [
{ dx: -1, dy: 0 },
{ dx: 1, dy: 0 },
{ dx: 0, dy: -1 }, // Up
{ dx: 0, dy: -1 }, // Up (2x bias)
{ dx: 0, dy: 1 },
{ dx: 0, dy: 0 },
];
const dir = directions[Math.floor(Math.random() * directions.length)];
const newX = this.x + dir.dx;
const newY = this.y + dir.dy;
if (newX >= 0 && newX < cols && newY >= 0 && newY < rows) {
this.x = newX;
this.y = newY;
this.sprite.x = newX * scaleSize;
this.sprite.y = newY * scaleSize;
}
// Condense at top
if (this.y <= 0) {
const water = new Particle(this.x, 0, Mode.WATER);
particles.push(water);
occupancyGrid.set(this.x, 0, water);
this.destroy();
}
}
updateSeed() {
// Seed lifecycle management
if (this.age > CONSTANTS.EVOLUTION.LIFESPAN) {
this.destroy();
return;
}
switch (this.plantStage) {
case PlantStage.SEED:
if (
this.storedSun >= 3 &&
this.storedWater >= 3 &&
occupancyGrid.isEmptyMooreNeighborhood(this.x, this.y)
) {
this.plantStage = PlantStage.GERMINATING;
}
break;
case PlantStage.GERMINATING:
if (this.storedSun >= 3 && this.storedWater >= 3) {
this.startStemGrowth();
}
break;
case PlantStage.STEM_GROWTH:
this.growStem();
break;
case PlantStage.LEAF_DEVELOPMENT:
this.growLeaves();
break;
case PlantStage.MATURE:
if (
this.storedSun >= CONSTANTS.EVOLUTION.REPRODUCTION_SUN &&
this.storedWater >= CONSTANTS.EVOLUTION.REPRODUCTION_WATER
) {
this.plantStage = PlantStage.FLOWERING;
this.createFlower();
}
break;
case PlantStage.FLOWERING:
this.reproductionCooldown--;
if (this.reproductionCooldown <= 0) {
this.reproduce();
}
break;
}
// Energy collection now handled by sun and water particle absorption
}
startStemGrowth() {
this.plantStage = PlantStage.STEM_GROWTH;
this.stemHeight = 0;
this.claimTerritory();
}
growStem() {
if (this.stemHeight >= this.genetics.traits.stemHeight) {
this.plantStage = PlantStage.LEAF_DEVELOPMENT;
this.leavesGrown = 0;
return;
}
if (
this.storedSun >= 1 &&
this.storedWater >= 1 &&
Math.random() < this.genetics.traits.growthSpeed * 0.2
) {
const stemY = this.y - this.stemHeight - 1;
if (stemY >= 0 && !occupancyGrid.get(this.x, stemY)) {
const stem = new Particle(this.x, stemY, Mode.STEM);
stem.genetics = this.genetics;
stem.plantId = this.plantId;
stem.parentId = this.id;
stem.updatePlantColor();
particles.push(stem);
occupancyGrid.set(this.x, stemY, stem);
this.bodyParts.push(stem);