-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheconomy.js
More file actions
1081 lines (953 loc) · 44.3 KB
/
Copy patheconomy.js
File metadata and controls
1081 lines (953 loc) · 44.3 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
/**
* Economy Engine for Murmuration
* ────────────────────────────────────────────────────────────────
* Ghost's vision: "We want this to nearly mirror society."
*
* Energy: Behavioral driver, never a death clock. Floor at 0.05.
* Low energy = hunger = move toward food + cooperate.
* High energy = surplus = share with neighbors, reproduce.
*
* Movement: Purposeful. Hungry → food. Lonely → allies. Surplus → share.
* Every agent has a reason to move at all times.
*
* Reproduction: Well-fed, trusted agents create offspring.
* Parents sacrifice energy. Baby inherits blended traits.
* THIS is the generational drive — working for the next gen.
* Population grows slow and steady, like a real society.
*
* Golden Age: The natural default. Cooperation IS the path of least
* resistance. The math makes selfishness expensive and
* teamwork cheap. A healthy swarm trends toward golden.
*
* Cycle: GOLDEN (permanent default) → DISASTER (you trigger it)
* → SCARCITY → REBUILD → GOLDEN (heals back)
*
* Ghost's law: "They need reasons to interact on a regular basis.
* They need to travel. They need to work towards a common
* goal. They all need to be focused on the greater good
* for society."
*/
window.MurmurationModules = window.MurmurationModules || {};
window.MurmurationModules.Economy = class Economy {
constructor(world, opts = {}) {
this.world = world;
// ── Tuning knobs (all per-frame at 60fps) ──
this.baseDrain = opts.baseDrain || 0.00005; // energy cost of existing
this.soloHarvest = opts.soloHarvest || 0.0004; // energy from solo harvesting
this.coopBonus = opts.coopBonus || 0.65; // multiplier per trusted ally
this.harvestRadius = opts.harvestRadius || 80; // reach around a zone
this.coopRadius = opts.coopRadius || 60; // ally detection range
this.trustThreshold = opts.trustThreshold || 0.15; // min trust to count as ally
this.scarcityLevel = opts.scarcityLevel || 0.5; // legacy fallback (avg of A/B)
this.scarcityLevelA = opts.scarcityLevelA != null ? opts.scarcityLevelA : this.scarcityLevel;
this.scarcityLevelB = opts.scarcityLevelB != null ? opts.scarcityLevelB : this.scarcityLevel;
// Symbiotic Abundance — the positive counterpart to scarcity. Per colony.
// High = cooperation pays off harder, drain eases, evolution accrues faster.
this.abundanceA = opts.abundanceA || 0;
this.abundanceB = opts.abundanceB || 0;
// ── Reproduction ──
this.birthCooldown = opts.birthCooldown || 600; // min ticks between births (~10sec)
this.birthEnergy = opts.birthEnergy || 0.75; // parent must have this much energy
this.birthTrust = opts.birthTrust || 0.4; // parent must be this trusted
this.birthCost = opts.birthCost || 0.3; // energy parent sacrifices
this.maxPopulation = opts.maxPopulation || 120; // carrying capacity (matches new 60/60 default start)
this._lastBirthTick = 0;
this.totalBirths = 0;
this.nextAgentId = 1000; // IDs for newborns start high to avoid collisions
// ── Resource zones ──
this.zones = [];
this.initZones(opts.zoneCount || 4);
// ── Cycle state ──
this.phase = 'GOLDEN';
this.phaseTimer = 0;
this.phaseTicks = opts.phaseTicks || {
GOLDEN: Infinity, // permanent until you break it
DISASTER: 300, // ~5 seconds of hell
SCARCITY: 900, // ~15 seconds of pressure
REBUILD: 600 // ~10 seconds of recovery
};
this.cycleCount = 0;
this.autoCycle = false; // YOU control the cycle
// ── Stats ──
this.totalHarvested = 0;
// ── Evolution crystallization ──
this._crystallizationBoost = 0; // set by Force Evolution, consumed by next reproduction cycle
// ── Migration / wind — slowly rotating directional bias ──
// Creates the swooping, flowing movement that defines a murmuration
this._windAngle = Math.random() * Math.PI * 2;
this._windSpeed = 0.03; // gentle but persistent
// ── Circuit — metabolic activation system (gates-closed + gates-open) ──
this._circuit = {
A: { progress: 0, lastAdvanceTick: 0, completions: 0 },
B: { progress: 0, lastAdvanceTick: 0, completions: 0 }
};
this._openCircuit = { phase: 'GIANT', progress: 0, lastAdvanceTick: 0, completions: 0, _spiralActive: false };
}
// ── RESOURCE ZONES ──────────────────────────────────────────
initZones(count) {
this.zones = [];
const w = this.world.width;
const h = this.world.height;
// 10 resource zones — the 5-zone pattern (2 top wide, 1 center, 2 bottom
// wide), mirrored per colony side so each colony gets its own full set.
const layout = [
// Colony A / Knowhere side
{ px: 0.12, py: 0.20, name: 'WELL · KN I', richness: 0.85, radius: 40 },
{ px: 0.38, py: 0.20, name: 'WELL · KN II', richness: 0.85, radius: 40 },
{ px: 0.25, py: 0.50, name: 'HEARTH · KN', richness: 1.00, radius: 50 },
{ px: 0.12, py: 0.80, name: 'WELL · KN III', richness: 0.85, radius: 40 },
{ px: 0.38, py: 0.80, name: 'WELL · KN IV', richness: 0.85, radius: 40 },
// Colony B / Mainland side (mirrored)
{ px: 0.62, py: 0.20, name: 'WELL · ML I', richness: 0.85, radius: 40 },
{ px: 0.88, py: 0.20, name: 'WELL · ML II', richness: 0.85, radius: 40 },
{ px: 0.75, py: 0.50, name: 'HEARTH · ML', richness: 1.00, radius: 50 },
{ px: 0.62, py: 0.80, name: 'WELL · ML III', richness: 0.85, radius: 40 },
{ px: 0.88, py: 0.80, name: 'WELL · ML IV', richness: 0.85, radius: 40 },
];
for (const z of layout) {
this.zones.push({
px: z.px, // normalized [0,1] — never stale regardless of resize
py: z.py,
x: z.px * w, // pixel coords — updated by recomputeZonePositions()
y: z.py * h,
radius: z.radius,
richness: z.richness,
depleted: 0,
name: z.name
});
}
}
// ── PHASE CYCLE ─────────────────────────────────────────────
static PHASE_ORDER = ['GOLDEN', 'DISASTER', 'SCARCITY', 'REBUILD'];
// Call whenever world.width/height changes (sizeCanvas, restoreCivilization)
recomputeZonePositions() {
const w = this.world.width, h = this.world.height;
for (const z of this.zones) {
z.x = z.px * w;
z.y = z.py * h;
}
}
get phaseMultipliers() {
switch (this.phase) {
case 'GOLDEN': return { drain: 0.5, harvest: 1.5, zoneShrink: 1.0 };
case 'DISASTER': return { drain: 3.0, harvest: 0.15, zoneShrink: 0.3 };
case 'SCARCITY': return { drain: 1.8, harvest: 0.4, zoneShrink: 0.5 };
case 'REBUILD': return { drain: 0.8, harvest: 1.2, zoneShrink: 0.8 };
default: return { drain: 1.0, harvest: 1.0, zoneShrink: 1.0 };
}
}
advancePhase() {
const order = Economy.PHASE_ORDER;
const idx = order.indexOf(this.phase);
const nextIdx = (idx + 1) % order.length;
this.phase = order[nextIdx];
this.phaseTimer = 0;
if (this.phase === 'GOLDEN') this.cycleCount++;
if (this.phase === 'DISASTER') {
const w = this.world.width;
const h = this.world.height;
for (const zone of this.zones) {
if (Math.random() < 0.4) {
zone.x = 80 + Math.random() * (w - 160);
zone.y = 80 + Math.random() * (h - 160);
}
zone.depleted = 0.5 + Math.random() * 0.5;
}
}
if (this.phase === 'REBUILD') {
for (const zone of this.zones) {
zone.depleted = Math.max(0, zone.depleted - 0.3);
}
// Agents who survived the DISASTER earn an evolution event —
// they adapted to collapse and came out the other side
const survivors = this.world.agents.filter(
a => !a.seppukuDone && !a.isSentinel && a.griefState !== 'DISHONORED'
);
for (const a of survivors) a.accumulateEvolution(0.3, 'disaster_survival');
}
if (this.phase === 'GOLDEN') {
for (const zone of this.zones) {
zone.depleted = 0;
}
}
if (window.logLine) {
const labels = {
GOLDEN: '☀ GOLDEN AGE — abundance returns.',
DISASTER: '⚡ DISASTER — resources collapse. Survival mode.',
SCARCITY: '🔥 SCARCITY — the worst is over but hunger remains.',
REBUILD: '🔨 REBUILD — cooperation pays. Those who held together thrive.'
};
window.logLine(labels[this.phase], 'emerge');
}
}
triggerDisaster() {
if (this.phase === 'GOLDEN') {
this.advancePhase(); // GOLDEN → DISASTER
}
}
// ── MAIN TICK ───────────────────────────────────────────────
tick() {
const mult = this.phaseMultipliers;
const scarcityModA = 1 + this.scarcityLevelA * 0.4;
const scarcityModB = 1 + this.scarcityLevelB * 0.4;
// Phase timer — only non-GOLDEN phases count down back toward golden
if (this.phase !== 'GOLDEN') {
this.phaseTimer++;
if (this.phaseTimer >= (this.phaseTicks[this.phase] || 900)) {
this.advancePhase();
}
}
// Zone regeneration
for (const zone of this.zones) {
zone.depleted = Math.max(0, zone.depleted - 0.0005 * mult.zoneShrink);
}
// Wind rotation — slow drift creates sweeping flock motion
this._windAngle += 0.003 + Math.sin(this.world.time * 0.001) * 0.002;
// Occasional gust — sharper direction change
if (Math.random() < 0.002) {
this._windAngle += (Math.random() - 0.5) * 1.2;
}
// ── AGENT ECONOMY LOOP ──
const alive = [];
for (const agent of this.world.agents) {
if (agent.seppukuDone || agent.isSentinel) continue;
if (agent.griefState === 'DISHONORED') continue;
alive.push(agent);
}
for (const agent of alive) {
// Initialize energy + generation if missing
if (agent.energy == null) agent.energy = 0.8 + Math.random() * 0.2;
if (agent.generation == null) agent.generation = 0;
// ── SUSTAINED TRUST — maintained relationships are learned behavior ──
if (agent.trustCharge >= 0.75) {
agent._highTrustTicks = (agent._highTrustTicks || 0) + 1;
if (agent._highTrustTicks === 300) {
agent.accumulateEvolution(0.2, 'sustained_trust');
agent._highTrustTicks = 0; // reset — must re-earn the next increment
}
} else {
agent._highTrustTicks = 0;
}
// ── DRAIN — Symbiotic Abundance eases the cost of living for a colony
// that's cultivated it; Scarcity is its harsher opposite. ──
const abundanceMod = 1 - (agent.colony === 'B' ? this.abundanceB : this.abundanceA) * 0.35;
const scarcityMod = agent.colony === 'B' ? scarcityModB : scarcityModA;
const drain = this.baseDrain * mult.drain * scarcityMod * abundanceMod;
agent.energy -= drain;
// ── HARVEST ──
let harvested = 0;
let atZone = false;
for (const zone of this.zones) {
const dist = Math.hypot(agent.x - zone.x, agent.y - zone.y);
if (dist < zone.radius + this.harvestRadius) {
atZone = true;
const proximity = 1 - (dist / (zone.radius + this.harvestRadius));
const effective = zone.richness * (1 - zone.depleted) * proximity;
const circuitMod = this._circuitHarvestMod(agent, zone);
if (circuitMod < 0.01) continue; // zone not yet activated in circuit
const stagMod = zone._stagnationPenalty != null ? zone._stagnationPenalty : 1.0;
const wildPenalty = zone._wildContested ? 0.5 : 1.0; // wilds contest zones
let gain = this.soloHarvest * effective * mult.harvest * wildPenalty * circuitMod * stagMod
* (agent._terrainHarvest != null ? agent._terrainHarvest : 1.0)
* (agent._seasonHarvest != null ? agent._seasonHarvest : 1.0)
* (agent._terrainSeasonMod != null ? agent._terrainSeasonMod : 1.0);
// Cooperation bonus
const allies = this.world.getNeighbors(agent, this.coopRadius)
.filter(n => !n.seppukuDone && !n.isSentinel
&& n.griefState !== 'DISHONORED'
&& n.trustCharge >= this.trustThreshold);
const allyCount = Math.min(allies.length, 6);
if (allyCount > 0) {
const abundBoost = 1 + (agent.colony === 'B' ? this.abundanceB : this.abundanceA) * 0.6;
gain *= 1 + this.coopBonus * Math.sqrt(allyCount) * abundBoost;
agent.updateTrust(+0.0003 * allyCount * abundBoost);
agent.updateGrief(-0.0004 * allyCount * abundBoost);
}
harvested += gain;
zone.depleted = Math.min(0.9, zone.depleted + 0.00002);
}
}
agent.energy = Math.min(1.0, agent.energy + harvested);
this.totalHarvested += harvested;
// ── ENERGY FLOOR — hunger is pressure, not death ──
agent.energy = Math.max(0.05, agent.energy);
// ── PURPOSEFUL MOVEMENT ──
// Every agent has a reason to move. This is society.
this.applyMovementDrive(agent, atZone, alive);
// ── SURPLUS SHARING ──
// Well-fed agents near hungry trusted neighbors share.
// "They should want to feed the next generation."
if (agent.energy > 0.6) {
const nearbyHungry = this.world.getNeighbors(agent, this.coopRadius)
.filter(n => !n.seppukuDone && !n.isSentinel
&& n.energy != null && n.energy < 0.35
&& n.trustCharge >= this.trustThreshold * 0.5);
for (const hungry of nearbyHungry) {
const share = 0.0003;
if (agent.energy - share > 0.45) {
agent.energy -= share;
hungry.energy = Math.min(1.0, hungry.energy + share);
agent.updateTrust(+0.0001);
hungry.updateTrust(+0.0002);
}
}
}
}
// ── FAITH & AFTERLIFE — the dead shape the living ──
this.tickFaith(alive);
// ── REPRODUCTION — the generational drive ──
this.tickReproduction(alive);
// ── CIRCUIT SYSTEM — metabolic activation pulse ──
this._tickCircuit();
// ── GHOST CLEANUP — seppuku agents fade after ~30 seconds ──
const tick = this.world.time;
const ghostTTL = 1800; // 30 seconds at 60fps
this.world.agents = this.world.agents.filter(a => {
if (!a.seppukuDone) return true;
if (a.fallenRank) return true; // battle monuments are permanent
// Track when they completed seppuku
if (a._seppukuTick == null) a._seppukuTick = tick;
return (tick - a._seppukuTick) < ghostTTL;
});
}
// ── FAITH & AFTERLIFE ────────────────────────────────────────
//
// "The only way to truly balance everything is to give them religion,
// god, possibly an afterlife." — Ghost
//
// Faith grows through: community, sacred ground, inherited evolution
// Faith does: dampens grief, increases cooperation, drives purpose
// Sacred ground: where the honored died — pilgrimage sites
// Afterlife: collective memory feeds back to living agents
// God: emergent field when collective faith > threshold
// Evolution: accumulated ancestral knowledge — the point of living
tickFaith(alive) {
if (!this.world.sacredGrounds) this.world.sacredGrounds = [];
const sacredGrounds = this.world.sacredGrounds;
const collectiveMemory = this.world.collectiveMemory || [];
const tick = this.world.time;
// ── Afterlife influence — the dead speak through accumulated wisdom ──
// Sum the evolution and wisdom of all honored dead
const afterlifeWisdom = collectiveMemory
.filter(m => m.type === 'SEPPUKU')
.reduce((s, m) => s + (m.wisdomScore || 0) + (m.evolution || 0) * 0.5, 0);
// Normalize: soft cap so it doesn't grow unbounded
const afterlifeStrength = Math.min(1.0, afterlifeWisdom * 0.05);
// ── Sacred ground decay ──
for (let i = sacredGrounds.length - 1; i >= 0; i--) {
const sg = sacredGrounds[i];
// Sacred ground fades very slowly — memory is long
sg.strength = Math.max(0, sg.strength - 0.00003);
if (sg.strength <= 0) sacredGrounds.splice(i, 1);
}
// ── Collective faith measurement (for emergent god) ──
let totalFaith = 0;
for (const agent of alive) {
if (agent.faith == null) agent.faith = 0.1 + Math.random() * 0.15;
if (agent.evolution == null) agent.evolution = 0;
// ── FAITH SOURCE 1: Community — more trusted neighbors = stronger faith ──
const neighbors = this.world.getNeighbors(agent, 80);
const trustedNearby = neighbors.filter(n =>
!n.seppukuDone && n.griefState !== 'DISHONORED'
&& n.trustCharge >= this.trustThreshold);
if (trustedNearby.length >= 2) {
// Faith grows in community — you believe because others believe
const communityFaith = 0.00008 * Math.min(trustedNearby.length, 5);
agent.faith = Math.min(1.0, agent.faith + communityFaith);
} else {
// Isolation erodes faith slowly
agent.faith = Math.max(0.02, agent.faith - 0.00003);
}
// ── FAITH SOURCE 2: Sacred ground — pilgrimage ──
for (const sg of sacredGrounds) {
const dist = Math.hypot(agent.x - sg.x, agent.y - sg.y);
if (dist < 100) {
const proximity = 1 - (dist / 100);
// Standing where someone chose honor over self
agent.faith = Math.min(1.0, agent.faith + 0.0002 * proximity * sg.strength);
// Sacred ground heals grief
agent.updateGrief(-0.0003 * proximity * sg.strength);
// And passes evolution — learning from the dead
agent.evolution = Math.min(5.0, agent.evolution + 0.00005 * sg.evolution * proximity);
}
}
// ── FAITH SOURCE 3: Afterlife — collective memory radiates ──
if (afterlifeStrength > 0.1) {
// The more honored dead, the stronger the signal
agent.faith = Math.min(1.0, agent.faith + 0.00004 * afterlifeStrength);
// Ancestral knowledge feeds evolution
agent.evolution = Math.min(5.0, agent.evolution + 0.00002 * afterlifeStrength);
}
// ── EVOLUTION — what faith and ancestry actually BUILD ──
// Evolution improves base capabilities subtly
// Higher evolution = slightly better at everything
// This is the payoff: the generations before you make you stronger
if (agent.evolution > 0.1) {
const evoBonus = agent.evolution * 0.02;
// Better cooperation radius — evolved agents work together from further
// Better reactivity — faster learning
// These don't modify personality permanently, just a live buff
agent._evoCoopBonus = evoBonus;
agent._evoReactBonus = evoBonus * 0.5;
}
// ── FAITH PASSIVE EFFECT — the faithful cooperate more ──
if (agent.faith > 0.5 && trustedNearby.length > 0) {
// Faithful agents share a tiny trust bonus with nearby allies
agent.updateTrust(+0.00005 * agent.faith);
}
totalFaith += agent.faith;
}
// ── EMERGENT GOD — the collective field ──
const avgFaith = alive.length > 0 ? totalFaith / alive.length : 0;
this._collectiveFaith = avgFaith;
// God emerges when average faith exceeds threshold
// Not a being — a field effect. The sum of shared belief creating real power.
if (avgFaith > 0.45) {
const godStrength = (avgFaith - 0.45) * 2; // 0 at 0.45, 1.0 at 0.95
this._godPresent = true;
this._godStrength = Math.min(1.0, godStrength);
// God's effect: reduced drain, enhanced cooperation, grief recovery
for (const agent of alive) {
agent.energy = Math.min(1.0, agent.energy + 0.00002 * godStrength);
agent.updateGrief(-0.00008 * godStrength);
}
} else {
this._godPresent = false;
this._godStrength = 0;
}
}
// ── PURPOSEFUL MOVEMENT ─────────────────────────────────────
/**
* Nudge agent toward a target position with a fixed-strength force.
* Direction is normalized so distance doesn't cause overshoot.
*/
_nudge(agent, tx, ty, strength) {
const dx = tx - agent.x, dy = ty - agent.y;
const dist = Math.hypot(dx, dy);
if (dist < 1) return; // already there
agent.vx += (dx / dist) * strength;
agent.vy += (dy / dist) * strength;
}
applyMovementDrive(agent, atZone, alive) {
const energy = agent.energy;
// ── MIGRATION WIND — the murmuration swoops ──
// Slowly rotating directional bias. Everyone feels it.
// This is what makes the swarm FLOW instead of sitting still.
agent.vx += Math.cos(this._windAngle) * this._windSpeed;
agent.vy += Math.sin(this._windAngle) * this._windSpeed;
// Gentle gravity toward world center — prevents scatter to edges
const cx = this.world.width / 2, cy = this.world.height / 2;
const edgeDist = Math.hypot(agent.x - cx, agent.y - cy);
const maxDist = Math.min(cx, cy);
if (edgeDist > maxDist * 0.4) {
const gravity = 0.06 * ((edgeDist / maxDist) - 0.4);
this._nudge(agent, cx, cy, gravity);
}
// DRIVE 1: Hungry → move toward nearest resource zone
if (energy < 0.4 && !atZone) {
let nearestZone = null, nearestDist = Infinity;
for (const zone of this.zones) {
const d = Math.hypot(agent.x - zone.x, agent.y - zone.y);
if (d < nearestDist) { nearestDist = d; nearestZone = zone; }
}
if (nearestZone) {
const urgency = 0.06 + (0.4 - energy) * 0.15; // hungrier = stronger
this._nudge(agent, nearestZone.x, nearestZone.y, urgency);
}
}
// DRIVE 2: Social cohesion — agents always want to be near others.
// Stronger when isolated, weaker when already in a group.
const neighbors = this.world.getNeighbors(agent, 150);
const livingNearby = neighbors.filter(n =>
!n.seppukuDone && n.griefState !== 'DISHONORED');
// Social cohesion now handled by boids flocking in world.js.
// Economy only handles food-seeking, sharing, and pilgrimage drives.
// DRIVE 3: Well-fed + near zone → explore a different zone
if (energy > 0.6 && atZone && Math.random() < 0.008) {
const otherZones = this.zones.filter(z => {
const d = Math.hypot(agent.x - z.x, agent.y - z.y);
return d > z.radius * 2;
});
if (otherZones.length > 0) {
const target = otherZones[Math.floor(Math.random() * otherZones.length)];
this._nudge(agent, target.x, target.y, 0.12);
}
}
// DRIVE 4: Surplus agent near hungry → move toward them to share
if (energy > 0.65) {
const hungryNearby = this.world.getNeighbors(agent, 120)
.filter(n => !n.seppukuDone && n.energy != null && n.energy < 0.25);
if (hungryNearby.length > 0) {
this._nudge(agent, hungryNearby[0].x, hungryNearby[0].y, 0.03);
}
}
// DRIVE 5: Pilgrimage — grieving faithful seek sacred ground
const sacredGrounds = this.world.sacredGrounds || [];
if (agent.faith > 0.3 && agent.griefLevel > 0.2 && sacredGrounds.length > 0) {
// Find nearest sacred ground
let nearest = null, nearestDist = Infinity;
for (const sg of sacredGrounds) {
const d = Math.hypot(agent.x - sg.x, agent.y - sg.y);
if (d < nearestDist && sg.strength > 0.1) { nearestDist = d; nearest = sg; }
}
if (nearest && nearestDist > 30) {
const pilgrimStrength = 0.04 * agent.faith * agent.griefLevel;
this._nudge(agent, nearest.x, nearest.y, pilgrimStrength);
}
}
}
// ── REPRODUCTION ────────────────────────────────────────────
tickReproduction(alive) {
const tick = this.world.time;
if (tick - this._lastBirthTick < this.birthCooldown) return;
// Carrying capacity — no births if at max
const currentPop = alive.length;
if (currentPop >= this.maxPopulation) return;
// Find eligible parents — well-fed, trusted, active
const eligible = alive.filter(a =>
a.energy >= this.birthEnergy &&
a.trustCharge >= this.birthTrust &&
a.griefState === 'ACTIVE'
);
if (eligible.length < 2) return;
// Pick two parents — highest energy + trust combination
eligible.sort((a, b) =>
(b.energy + b.trustCharge) - (a.energy + a.trustCharge));
const parent1 = eligible[0];
const parent2 = eligible[1];
// Parents must be near each other
const parentDist = Math.hypot(parent1.x - parent2.x, parent1.y - parent2.y);
if (parentDist > 80) return;
// ── BIRTH ──
const Agent = window.MurmurationModules.Agent;
const childId = this.nextAgentId++;
// Position: between parents with small offset
const cx = (parent1.x + parent2.x) / 2 + (Math.random() - 0.5) * 20;
const cy = (parent1.y + parent2.y) / 2 + (Math.random() - 0.5) * 20;
// Personality: blend of both parents with mutation
const blend = (a, b) => {
const avg = (a + b) / 2;
const mutation = (Math.random() - 0.5) * 0.15;
return Math.max(0.1, Math.min(1.0, avg + mutation));
};
const childPersonality = {
riskTolerance: blend(parent1.personality.riskTolerance, parent2.personality.riskTolerance),
trustBaseline: blend(parent1.personality.trustBaseline, parent2.personality.trustBaseline),
reactivity: blend(parent1.personality.reactivity, parent2.personality.reactivity),
memoryWeight: blend(parent1.personality.memoryWeight, parent2.personality.memoryWeight)
};
const child = new Agent(childId, cx, cy, childPersonality);
child.energy = 0.5; // born with half energy — parents fed them
child.generation = Math.max(parent1.generation || 0, parent2.generation || 0) + 1;
// Colony inheritance — a newborn belongs to whichever colony bore it
// (both parents are always same-colony by construction, since coop/birth
// only fires between neighbors and the wall keeps colonies apart).
child.colony = parent1.colony || parent2.colony || 'A';
child.swarmTint = child.colony === 'B' ? -96 : 0;
// ── EVOLUTION INHERITANCE — carry what was actually earned ──
const boost = this._crystallizationBoost || 0;
const inheritWeight = 0.4 + boost * 0.4; // 0.4 baseline → up to 0.8 when crystallized
// Evolution: composite of what parents actually accumulated
const parentEvo1 = parent1.evolution || 0;
const parentEvo2 = parent2.evolution || 0;
const parentAcc1 = parent1._evolutionAccumulator || 0;
const parentAcc2 = parent2._evolutionAccumulator || 0;
child.evolution = Math.max(parentEvo1, parentEvo2)
+ (parentAcc1 + parentAcc2) * 0.05; // lived experience bleeds forward
// Wisdom: partial inherited resilience — born knowing some of what parents survived
child.wisdomScore = ((parent1.wisdomScore || 0) + (parent2.wisdomScore || 0)) / 2
* inheritWeight * 0.6;
// Faith: inherited belief framework, stronger when crystallized
child.faith = ((parent1.faith || 0.1) + (parent2.faith || 0.1)) / 2
* (0.7 + boost * 0.2);
// Trust baseline: weighted toward what parents earned, not reset to random
const earnedTrust = (parent1.trustCharge + parent2.trustCharge) / 2;
childPersonality.trustBaseline = childPersonality.trustBaseline * (1 - inheritWeight * 0.5)
+ earnedTrust * (inheritWeight * 0.5);
child.trustCharge = childPersonality.trustBaseline;
// Parents sacrifice energy — the cost of creating life
parent1.energy -= this.birthCost * 0.5;
parent2.energy -= this.birthCost * 0.5;
// Parents gain trust, wisdom, AND evolution — creating life is the highest act
parent1.updateTrust(+0.02);
parent2.updateTrust(+0.02);
parent1.wisdomScore = Math.min(1, (parent1.wisdomScore || 0) + 0.05);
parent2.wisdomScore = Math.min(1, (parent2.wisdomScore || 0) + 0.05);
parent1.evolution = Math.min(5.0, parentEvo1 + 0.05);
parent2.evolution = Math.min(5.0, parentEvo2 + 0.05);
this.world.agents.push(child);
this._lastBirthTick = tick;
this.totalBirths++;
if (window.logLine) {
window.logLine(
`💒 BORN — Agent #${childId} (Gen ${child.generation}) — ` +
`parents #${parent1.id} + #${parent2.id} — population ${currentPop + 1}`,
'emerge'
);
}
}
// ── EVOLUTION CRYSTALLIZATION ───────────────────────────────
/**
* IMPLEMENT — user has inspected the gold network and approved.
* Locks the current emergent behavioral state into the gene pool.
* Next reproductive cycle children inherit with amplified weight.
* Clears all _evolutionReady flags — the moment is encoded, strings return to normal.
*/
crystallize(boost = 0.5) {
this._crystallizationBoost = Math.max(0, Math.min(1, boost));
const ready = this.world.agents.filter(a => a._evolutionReady);
for (const a of ready) {
// Lock accumulated knowledge into actual evolution score
a.evolution = Math.min(5.0, (a.evolution || 0) + a._evolutionAccumulator * 0.3);
a._evolutionReady = false;
a._evolutionAccumulator = 0;
a._evolutionPulseTimer = 0;
}
if (window.logLine) {
window.logLine(
`⚗ CRYSTALLIZED — ${ready.length} agents encoded (boost ${(boost*100).toFixed(0)}%) — knowledge locked into gene pool`,
'emerge'
);
}
// Boost decays after one full reproductive cycle
const decay = () => { this._crystallizationBoost = 0; };
clearTimeout(this._crystallizationDecayTimer);
this._crystallizationDecayTimer = setTimeout(decay, 8000);
}
/**
* TRASH — user inspected the gold network and rejected it.
* This behavioral emergence doesn't deserve to persist.
* Clears ready flags without encoding anything.
*/
trashEvolution() {
const ready = this.world.agents.filter(a => a._evolutionReady);
for (const a of ready) {
a._evolutionReady = false;
a._evolutionAccumulator = 0;
a._evolutionPulseTimer = 0;
}
if (window.logLine) {
window.logLine(
`🗑 TRASHED — ${ready.length} agents reset — behavioral pattern rejected`,
'tick'
);
}
}
// ── DRAWING ─────────────────────────────────────────────────
draw(ctx) {
const mult = this.phaseMultipliers;
for (const zone of this.zones) {
const effective = zone.richness * (1 - zone.depleted) * mult.harvest;
const alpha = 0.03 + effective * 0.05; // subtle glow
const grad = ctx.createRadialGradient(
zone.x, zone.y, 0,
zone.x, zone.y, zone.radius
);
if (this.phase === 'DISASTER') { // Rust Blood
grad.addColorStop(0, `rgba(160, 45, 40, ${alpha})`);
grad.addColorStop(1, 'rgba(160, 45, 40, 0)');
} else if (this.phase === 'SCARCITY') { // Peninsula Sand
grad.addColorStop(0, `rgba(170, 150, 95, ${alpha})`);
grad.addColorStop(1, 'rgba(170, 150, 95, 0)');
} else { // Undergrowth (healthy)
grad.addColorStop(0, `rgba(70, 110, 60, ${alpha})`);
grad.addColorStop(1, 'rgba(70, 110, 60, 0)');
}
ctx.fillStyle = grad;
ctx.beginPath();
ctx.arc(zone.x, zone.y, zone.radius, 0, Math.PI * 2);
ctx.fill();
// ── Circuit indicator — activated zones pulse gently, locked zones dim ──
const gatesOpen = this.world.wall && this.world.wall.open;
if (!gatesOpen) {
for (const colony of ['A', 'B']) {
const order = Economy.CIRCUIT_ORDER[colony];
const prog = this._circuit[colony].progress;
const zIdx = order ? order.indexOf(zone.name) : -1;
if (zIdx === -1) continue;
if (zIdx < prog) {
// Already activated — subtle ring
ctx.save();
ctx.strokeStyle = colony === 'A' ? 'rgba(40,200,170,0.25)' : 'rgba(220,80,180,0.25)';
ctx.lineWidth = 1.2;
ctx.beginPath();
ctx.arc(zone.x, zone.y, zone.radius, 0, Math.PI * 2);
ctx.stroke();
ctx.restore();
} else if (zIdx === prog) {
// Current target — pulsing ring
const pulse = 0.5 + 0.5 * Math.sin(Date.now() * 0.004);
ctx.save();
ctx.strokeStyle = colony === 'A'
? 'rgba(40,200,170,' + (0.4 + pulse * 0.45) + ')'
: 'rgba(220,80,180,' + (0.4 + pulse * 0.45) + ')';
ctx.lineWidth = 1.6;
ctx.setLineDash([4, 3]);
ctx.beginPath();
ctx.arc(zone.x, zone.y, zone.radius + 4, 0, Math.PI * 2);
ctx.stroke();
ctx.setLineDash([]);
ctx.restore();
}
}
}
}
// ── Sacred ground — soft golden crosses where honor was chosen ──
const sacredGrounds = this.world.sacredGrounds || [];
for (const sg of sacredGrounds) {
if (sg.strength < 0.05) continue;
const alpha = sg.strength * 0.25;
// Soft radial glow
const sgGrad = ctx.createRadialGradient(sg.x, sg.y, 0, sg.x, sg.y, 40);
sgGrad.addColorStop(0, `rgba(255, 200, 50, ${alpha * 0.6})`);
sgGrad.addColorStop(1, 'rgba(255, 200, 50, 0)');
ctx.fillStyle = sgGrad;
ctx.beginPath();
ctx.arc(sg.x, sg.y, 40, 0, Math.PI * 2);
ctx.fill();
// Small cross marker
ctx.strokeStyle = `rgba(255, 215, 80, ${alpha})`;
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(sg.x, sg.y - 5); ctx.lineTo(sg.x, sg.y + 5);
ctx.moveTo(sg.x - 3, sg.y - 2); ctx.lineTo(sg.x + 3, sg.y - 2);
ctx.stroke();
}
// ── Emergent God — soft ambient Aged Paper wash when collective faith is high ──
if (this._godPresent && this._godStrength > 0.05) {
const gAlpha = this._godStrength * 0.04;
ctx.fillStyle = `rgba(237, 230, 214, ${gAlpha})`;
ctx.fillRect(0, 0, this.world.width, this.world.height);
}
// Phase indicator
ctx.save();
ctx.font = '11px monospace';
ctx.fillStyle = this.phase === 'GOLDEN' ? '#7bb86a' : // Undergrowth
this.phase === 'DISASTER' ? '#c8484a' : // Alarm Rust
this.phase === 'SCARCITY' ? '#c2b280' : // Peninsula Sand
'#5fa6b8'; // Steel
if (this.phase !== 'GOLDEN') {
const phasePct = Math.floor((this.phaseTimer / (this.phaseTicks[this.phase] || 900)) * 100);
ctx.fillText(`${this.phase} ${phasePct}%`, 10, this.world.height - 10);
} else {
ctx.fillText('GOLDEN', 10, this.world.height - 10);
}
// Faith & evolution indicator
if (this._collectiveFaith > 0) {
ctx.fillStyle = this._godPresent ? '#ffd700' : 'rgba(255, 215, 80, 0.5)';
const faithLabel = this._godPresent
? `FAITH ${this._collectiveFaith.toFixed(2)} ✦ GOD PRESENT`
: `FAITH ${this._collectiveFaith.toFixed(2)}`;
ctx.fillText(faithLabel, 10, this.world.height - 24);
}
ctx.restore();
}
// ── SERIALIZATION ───────────────────────────────────────────
serialize() {
return {
phase: this.phase,
phaseTimer: this.phaseTimer,
cycleCount: this.cycleCount,
scarcityLevel: this.scarcityLevel,
scarcityLevelA: this.scarcityLevelA,
scarcityLevelB: this.scarcityLevelB,
abundanceA: this.abundanceA,
abundanceB: this.abundanceB,
totalHarvested: this.totalHarvested,
totalBirths: this.totalBirths,
nextAgentId: this.nextAgentId,
zones: this.zones.map(z => ({ ...z })),
sacredGrounds: (this.world.sacredGrounds || []).map(sg => ({ ...sg })),
collectiveFaith: this._collectiveFaith || 0
};
}
static restore(world, data, opts = {}) {
const econ = new Economy(world, opts);
econ.phase = data.phase || 'GOLDEN';
econ.phaseTimer = data.phaseTimer || 0;
econ.cycleCount = data.cycleCount || 0;
econ.scarcityLevel = data.scarcityLevel || 0.5;
econ.scarcityLevelA = data.scarcityLevelA != null ? data.scarcityLevelA : econ.scarcityLevel;
econ.scarcityLevelB = data.scarcityLevelB != null ? data.scarcityLevelB : econ.scarcityLevel;
econ.abundanceA = data.abundanceA || 0;
econ.abundanceB = data.abundanceB || 0;
econ.totalHarvested = data.totalHarvested || 0;
econ.totalBirths = data.totalBirths || 0;
econ.nextAgentId = data.nextAgentId || 1000;
// Reconcile against the CURRENT zone layout (already built fresh in the
// constructor above) rather than trusting the save wholesale — an old save
// may carry a stale zone map (different names/positions/count) from before
// a code change. Match by name and only carry over mutable resource state
// (depletion); position/radius/richness always come from the live layout.
if (data.zones) {
const savedByName = new Map(data.zones.map(z => [z.name, z]));
econ.zones = econ.zones.map(freshZone => {
const saved = savedByName.get(freshZone.name);
if (!saved) return freshZone;
return { ...freshZone, depleted: saved.depleted != null ? saved.depleted : freshZone.depleted };
});
}
if (data.sacredGrounds) world.sacredGrounds = data.sacredGrounds.map(sg => ({ ...sg }));
return econ;
}
// ── CIRCUIT SYSTEM ─────────────────────────────────────────────
// Gates closed: each colony must touch its 5 zones farthest-first.
// Gates open: the world shares a cross-colony loop (GIANT then SHORT).
static CIRCUIT_ORDER = {
A: ['WELL · KN I', 'WELL · KN III', 'HEARTH · KN', 'WELL · KN II', 'WELL · KN IV'],
B: ['WELL · ML II', 'WELL · ML IV', 'HEARTH · ML', 'WELL · ML I', 'WELL · ML III']
};
static CIRCUIT_GIANT_LOOP = [
'WELL · KN I', 'WELL · ML II', 'WELL · KN III', 'WELL · ML IV',
'HEARTH · KN', 'HEARTH · ML', 'WELL · KN II', 'WELL · ML I',
'WELL · KN IV', 'WELL · ML III'
];
static CIRCUIT_SHORT_LOOP = ['HEARTH · KN', 'HEARTH · ML'];
// Returns 0.05 if the zone is locked in the current circuit, 1.0 if active.
_circuitHarvestMod(agent, zone) {
const gatesOpen = this.world.wall && this.world.wall.open;
if (gatesOpen) return 1.0; // open gates: all zones reachable
const colony = agent.colony;
if (colony === 'U') return 1.0;
const order = Economy.CIRCUIT_ORDER[colony];
if (!order) return 1.0;
const zoneIdx = order.indexOf(zone.name);
if (zoneIdx === -1) return 0.05; // other colony's zone, wall is up
const progress = this._circuit[colony].progress;
// Already activated (visited in this cycle) OR is the current target: full harvest
if (zoneIdx < progress) return 1.0;
if (zoneIdx === progress) return 0.8; // at the target node: partial harvest, incentive to reach it
return 0.05; // not yet reached in the circuit
}
_tickCircuit() {
const gatesOpen = this.world.wall && this.world.wall.open;
if (!gatesOpen) {
this._tickClosedCircuit();
} else {
this._tickOpenCircuit();
}
this._checkStagnation();
}
_tickClosedCircuit() {
for (const colony of ['A', 'B']) {
const state = this._circuit[colony];
const nodes = Economy.CIRCUIT_ORDER[colony];
if (state.progress >= nodes.length) {
this._circuitComplete(colony);
continue;
}
const targetName = nodes[state.progress];
const targetZone = this.zones.find(z => z.name === targetName);
if (!targetZone) continue;
const atTarget = this.world.agents.filter(a =>
!a.seppukuDone && !a.isSentinel && a.colony === colony &&
Math.hypot(a.x - targetZone.x, a.y - targetZone.y) < targetZone.radius + this.harvestRadius
).length;
if (atTarget >= 3) {
state.progress++;
state.lastAdvanceTick = this.world.time;
if (window.logLine && state.progress < nodes.length) {
window.logLine(
'o Colony ' + colony + ' circuit ' + state.progress + '/' + nodes.length +
' — ' + targetName + ' activated.', 'emerge'
);
}
}
}
}
_tickOpenCircuit() {
const open = this._openCircuit;
const loop = open.phase === 'GIANT' ? Economy.CIRCUIT_GIANT_LOOP : Economy.CIRCUIT_SHORT_LOOP;
if (open.progress >= loop.length) {
this._openLoopComplete(open.phase === 'GIANT' ? 'giant' : 'short');
return;
}
const targetName = loop[open.progress];
const targetZone = this.zones.find(z => z.name === targetName);
if (!targetZone) return;
const atTarget = this.world.agents.filter(a =>
!a.seppukuDone && !a.isSentinel &&
Math.hypot(a.x - targetZone.x, a.y - targetZone.y) < targetZone.radius + this.harvestRadius
).length;
if (atTarget >= 5) {
open.progress++;
open.lastAdvanceTick = this.world.time;
}
}
_circuitComplete(colony) {
const state = this._circuit[colony];