-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
5810 lines (5035 loc) · 275 KB
/
script.js
File metadata and controls
5810 lines (5035 loc) · 275 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
// Simulation of T6SS-mediated Bacterial Interactions - ver. 6.4 (26.11.2025)
// Copyright (c) 2025 Marek Basler
// Licensed under the Creative Commons Attribution 4.0 International License (CC BY 4.0)
// Details: https://creativecommons.org/licenses/by/4.0/
//
// If you use, adapt, or redistribute this software or its derivatives,
// please provide attribution to: Marek Basler - University of Basel
// --- Global Constants & Colors ---
// Transparency can be set, but for clarity it is off (alpha = 1.0)
// --- Global Constants & Colors (Mapped from config.js) ---
const ATTACKER_COLOR = AppConfig.colors.ATTACKER;
const PREY_COLOR = AppConfig.colors.PREY;
const DEFENDER_COLOR = AppConfig.colors.DEFENDER;
const DEAD_CELL_COLOR = AppConfig.colors.DEAD_CELL;
const BARRIER_COLOR = AppConfig.colors.BARRIER;
const LYSING_CELL_COLOR = AppConfig.colors.LYSING_CELL;
const EMPTY_COLOR_STROKE = AppConfig.colors.EMPTY_STROKE;
const FIRING_SECTOR_COLOR = AppConfig.colors.FIRING_SECTOR;
const MISS_FIRING_SECTOR_COLOR = AppConfig.colors.MISS_FIRING_SECTOR;
const DEFAULT_CANVAS_BG_COLOR = AppConfig.colors.DEFAULT_CANVAS_BG;
const AXIAL_DIRECTIONS = [
{ q: 1, r: 0 }, { q: 1, r: -1 }, { q: 0, r: -1 },
{ q: -1, r: 0 }, { q: -1, r: 1 }, { q: 0, r: 1 }
];
// --- DOM Elements ---
// Canvas and general UI
const canvasContainer = document.getElementById('canvasContainer');
const canvas = document.getElementById('simulationCanvas');
const ctx = canvas.getContext('2d');
const simulationErrorDisplay = document.getElementById('simulationErrorDisplay');
const hoverInfoPanel = document.getElementById('hoverInfoPanel');
// Cell type param selection
const cellTypeSelectionButtons = document.getElementById('cellTypeSelectionButtons');
const attackerParamsSection = document.getElementById('attackerParamsSection');
const preyParamsSection = document.getElementById('preyParamsSection');
const defenderParamsSection = document.getElementById('defenderParamsSection');
const selectAttackerParamsButton = document.getElementById('selectAttackerParamsButton');
const selectPreyParamsButton = document.getElementById('selectPreyParamsButton');
const selectDefenderParamsButton = document.getElementById('selectDefenderParamsButton');
// Setup mode
const manualPlacementControls = document.getElementById('manualPlacementControls');
const selectAttackerButton = document.getElementById('selectAttackerButton');
const selectPreyButton = document.getElementById('selectPreyButton');
const selectDefenderButton = document.getElementById('selectDefenderButton');
const selectBarrierButton = document.getElementById('selectBarrierButton');
const selectRemoveButton = document.getElementById('selectRemoveButton');
// const currentPlacementTypeDisplay = document.getElementById('currentPlacementTypeDisplay');
const manualRandomPlacementButton = document.getElementById('manualRandomPlacementButton');
const clearManualPlacementButton = document.getElementById('clearManualPlacementButton');
const importManualArenaButton = document.getElementById('importManualArenaButton'); // New
const exportManualArenaButton = document.getElementById('exportManualArenaButton'); // New
// Simulation control
const startButton = document.getElementById('startButton');
const pauseButton = document.getElementById('pauseButton');
const stepButton = document.getElementById('stepButton');
const stopButton = document.getElementById('stopButton');
const resetSimulationButton = document.getElementById('resetSimulationButton');
const simulationSpeedInput = document.getElementById('simulationSpeedInput');
const totalSimulationMinutesInput = document.getElementById('totalSimulationMinutesInput');
const simulationSeedInput = document.getElementById('simulationSeedInput');
const newSeedButton = document.getElementById('newSeedButton');
const resetRngButton = document.getElementById('resetRngButton');
const resyncCellsButton = document.getElementById('resyncCellsButton');
// General settings
const arenaGridRadiusInput = document.getElementById('arenaGridRadiusInput');
const importSettingsButton = document.getElementById('importSettingsButton');
const exportSettingsButtonMain = document.getElementById('exportSettingsButtonMain');
const resetSettingsToDefaultsButton = document.getElementById('resetSettingsToDefaultsButton');
// Exports Settings (Renamed Section)
const saveImagesCheckbox = document.getElementById('saveImagesCheckbox');
const saveArenaStatesCheckbox = document.getElementById('saveArenaStatesCheckbox'); // New
const imageExportWidthInput = document.getElementById('imageExportWidthInput');
// Attacker params
const initialAttackersInput = document.getElementById('initialAttackersInput');
const attackerReplicationMeanInput = document.getElementById('attackerReplicationMeanInput');
const attackerReplicationRangeInput = document.getElementById('attackerReplicationRangeInput');
const t6ssFireCooldownMinInput = document.getElementById('t6ssFireCooldownMinInput');
const t6ssFireCooldownMaxInput = document.getElementById('t6ssFireCooldownMaxInput');
const attackerPrecisionInput = document.getElementById('attackerPrecisionInput');
const attackerContactSensingBiasInput = document.getElementById('attackerContactSensingBiasInput');
const attackerKinExclusionInput = document.getElementById('attackerKinExclusionInput');
const attackerKinExclusionPenaltyInput = document.getElementById('attackerKinExclusionPenaltyInput');
const attNonLyticUnitsPerHitInput = document.getElementById('attNonLyticUnitsPerHitInput');
const attNonLyticDeliveryChanceInput = document.getElementById('attNonLyticDeliveryChanceInput');
const attLyticUnitsPerHitInput = document.getElementById('attLyticUnitsPerHitInput');
const attLyticDeliveryChanceInput = document.getElementById('attLyticDeliveryChanceInput');
const attNonLyticUnitsToDieInput = document.getElementById('attNonLyticUnitsToDieInput');
const attLyticUnitsToLyseInput = document.getElementById('attLyticUnitsToLyseInput');
const attBaseLysisDelayInput = document.getElementById('attBaseLysisDelayInput');
// Attacker movement
const attackerMoveCooldownMinInput = document.getElementById('attackerMoveCooldownMinInput');
const attackerMoveCooldownMaxInput = document.getElementById('attackerMoveCooldownMaxInput');
const attackerMoveProbabilityInput = document.getElementById('attackerMoveProbabilityInput');
const attackerMoveDirectionalityInput = document.getElementById('attackerMoveDirectionalityInput');
const attackerMovePreyAiAttractionInput = document.getElementById('attackerMovePreyAiAttractionInput');
const attackerMovePreyAiAttractionThresholdInput = document.getElementById('attackerMovePreyAiAttractionThresholdInput'); // New
const attackerLysesPerReplicationInput = document.getElementById('attackerLysesPerReplicationInput');
const attackerReplicationRewardMeanInput = document.getElementById('attackerReplicationRewardMeanInput');
const attackerReplicationRewardRangeInput = document.getElementById('attackerReplicationRewardRangeInput');
// Attacker QS
const attackerQSProductionRateInput = document.getElementById('attackerQSProductionRateInput');
const attackerQSDegradationRateInput = document.getElementById('attackerQSDegradationRateInput');
const attackerQSDiffusionRateInput = document.getElementById('attackerQSDiffusionRateInput');
const attackerQSMidpointInput = document.getElementById('attackerQSMidpointInput');
const attackerQSCooperativityInput = document.getElementById('attackerQSCooperativityInput');
// Prey params
const initialPreyInput = document.getElementById('initialPreyInput');
const preyReplicationMeanInput = document.getElementById('preyReplicationMeanInput');
const preyReplicationRangeInput = document.getElementById('preyReplicationRangeInput');
const preyNonLyticUnitsToDieAttInput = document.getElementById('preyNonLyticUnitsToDieAttInput');
const preyLyticUnitsToLyseAttInput = document.getElementById('preyLyticUnitsToLyseAttInput');
const preyBaseLysisDelayAttInput = document.getElementById('preyBaseLysisDelayAttInput');
const preyNonLyticResistanceAttInput = document.getElementById('preyNonLyticResistanceAttInput');
const preyLyticResistanceAttInput = document.getElementById('preyLyticResistanceAttInput');
const preyNonLyticUnitsToDieDefInput = document.getElementById('preyNonLyticUnitsToDieDefInput');
const preyLyticUnitsToLyseDefInput = document.getElementById('preyLyticUnitsToLyseDefInput');
const preyBaseLysisDelayDefInput = document.getElementById('preyBaseLysisDelayDefInput');
const preyNonLyticResistanceDefInput = document.getElementById('preyNonLyticResistanceDefInput');
const preyLyticResistanceDefInput = document.getElementById('preyLyticResistanceDefInput');
const lacZPerPreyInput = document.getElementById('lacZPerPreyInput');
const preyCapsuleSystemEnabledCheckbox = document.getElementById('preyCapsuleSystemEnabledCheckbox');
const preyCapsuleMaxProtectionInput = document.getElementById('preyCapsuleMaxProtectionInput');
const preyCapsuleDerepressionMidpointInput = document.getElementById('preyCapsuleDerepressionMidpointInput');
const preyCapsuleCooperativityInput = document.getElementById('preyCapsuleCooperativityInput');
const preyCapsuleCooldownMinInput = document.getElementById('preyCapsuleCooldownMinInput');
const preyCapsuleCooldownMaxInput = document.getElementById('preyCapsuleCooldownMaxInput');
// Prey movement
const preyMoveCooldownMinInput = document.getElementById('preyMoveCooldownMinInput');
const preyMoveCooldownMaxInput = document.getElementById('preyMoveCooldownMaxInput');
const preyMoveProbabilityInput = document.getElementById('preyMoveProbabilityInput');
const preyMoveDirectionalityInput = document.getElementById('preyMoveDirectionalityInput');
// Defender params
const initialDefendersInput = document.getElementById('initialDefendersInput');
const defenderReplicationMeanInput = document.getElementById('defenderReplicationMeanInput');
const defenderReplicationRangeInput = document.getElementById('defenderReplicationRangeInput');
const defenderSenseChanceInput = document.getElementById('defenderSenseChanceInput');
const defenderMaxRetaliationsInput = document.getElementById('defenderMaxRetaliationsInput');
const defenderRandomFireCooldownMinInput = document.getElementById('defenderRandomFireCooldownMinInput');
const defenderRandomFireCooldownMaxInput = document.getElementById('defenderRandomFireCooldownMaxInput');
const defenderRandomFireChanceInput = document.getElementById('defenderRandomFireChanceInput');
const defNonLyticUnitsPerHitInput = document.getElementById('defNonLyticUnitsPerHitInput');
const defNonLyticDeliveryChanceInput = document.getElementById('defNonLyticDeliveryChanceInput');
const defLyticUnitsPerHitInput = document.getElementById('defLyticUnitsPerHitInput');
const defLyticDeliveryChanceInput = document.getElementById('defLyticDeliveryChanceInput');
const defNonLyticUnitsToDieInput = document.getElementById('defNonLyticUnitsToDieInput');
const defLyticUnitsToLyseInput = document.getElementById('defLyticUnitsToLyseInput');
const defBaseLysisDelayInput = document.getElementById('defBaseLysisDelayInput');
const defNonLyticResistanceInput = document.getElementById('defNonLyticResistanceInput');
const defLyticResistanceInput = document.getElementById('defLyticResistanceInput');
// Defender movement
const defenderMoveCooldownMinInput = document.getElementById('defenderMoveCooldownMinInput');
const defenderMoveCooldownMaxInput = document.getElementById('defenderMoveCooldownMaxInput');
const defenderMoveProbabilityInput = document.getElementById('defenderMoveProbabilityInput');
const defenderMoveDirectionalityInput = document.getElementById('defenderMoveDirectionalityInput');
const defenderLysesPerReplicationInput = document.getElementById('defenderLysesPerReplicationInput');
const defenderReplicationRewardMeanInput = document.getElementById('defenderReplicationRewardMeanInput');
const defenderReplicationRewardRangeInput = document.getElementById('defenderReplicationRewardRangeInput');
// CPRG reporter settings
const initialCPRGSubstrateInput = document.getElementById('initialCPRGSubstrateInput');
const lacZKcatInput = document.getElementById('lacZKcatInput');
const lacZKmInput = document.getElementById('lacZKmInput');
// Stats display
const timeStepsDisplay = document.getElementById('timeStepsDisplay');
const attackerCountDisplay = document.getElementById('attackerCountDisplay');
const livePreyCountDisplay = document.getElementById('livePreyCountDisplay');
const defenderCountDisplay = document.getElementById('defenderCountDisplay');
const deadLysingAttackersDisplay = document.getElementById('deadLysingAttackersDisplay');
const deadLysingPreyDisplay = document.getElementById('deadLysingPreyDisplay');
const deadLysingDefendersDisplay = document.getElementById('deadLysingDefendersDisplay');
const totalCellCountDisplay = document.getElementById('totalCellCountDisplay');
const firingsThisStepDisplay = document.getElementById('firingsThisStepDisplay');
const attKilledThisStepDisplay = document.getElementById('attKilledThisStepDisplay');
const preyKilledThisStepDisplay = document.getElementById('preyKilledThisStepDisplay');
const defKilledThisStepDisplay = document.getElementById('defKilledThisStepDisplay');
const attLysedThisStepDisplay = document.getElementById('attLysedThisStepDisplay');
const preyLysedThisStepDisplay = document.getElementById('preyLysedThisStepDisplay');
const defLysedThisStepDisplay = document.getElementById('defLysedThisStepDisplay');
const cumulativeFiringsDisplay = document.getElementById('cumulativeFiringsDisplay');
const cumulativeAttKilledDisplay = document.getElementById('cumulativeAttKilledDisplay');
const cumulativePreyKilledDisplay = document.getElementById('cumulativePreyKilledDisplay');
const cumulativeDefKilledDisplay = document.getElementById('cumulativeDefKilledDisplay');
const cumulativeAttLysedDisplay = document.getElementById('cumulativeAttLysedDisplay');
const cumulativePreyLysedDisplay = document.getElementById('cumulativePreyLysedDisplay');
const cumulativeDefLysedDisplay = document.getElementById('cumulativeDefLysedDisplay');
const totalCPRGConvertedDisplay = document.getElementById('totalCPRGConvertedDisplay');
const totalSpacesDisplay = document.getElementById('totalSpacesDisplay');
const percentFullDisplay = document.getElementById('percentFullDisplay');
// Modals
const reportModalOverlay = document.getElementById('reportModalOverlay');
const reportModalTitle = document.getElementById('reportModalTitle');
const reportModalBody = document.getElementById('reportModalBody');
const closeReportModalButton = document.getElementById('closeReportModalButton');
const reportOutcome = document.getElementById('reportOutcome');
const reportDuration = document.getElementById('reportDuration');
const reportAttackersRemaining = document.getElementById('reportAttackersRemaining');
const reportLivePreyRemaining = document.getElementById('reportLivePreyRemaining');
const reportDefendersRemainingContainer = document.getElementById('reportDefendersRemainingContainer');
const reportDefendersRemaining = document.getElementById('reportDefendersRemaining');
const reportDeadLysingAttackers = document.getElementById('reportDeadLysingAttackers');
const reportDeadLysingPrey = document.getElementById('reportDeadLysingPrey');
const reportDeadLysingDefendersContainer = document.getElementById('reportDeadLysingDefendersContainer');
const reportDeadLysingDefenders = document.getElementById('reportDeadLysingDefenders');
const reportCumulativeFirings = document.getElementById('reportCumulativeFirings');
const reportCumulativeAttKilled = document.getElementById('reportCumulativeAttKilled');
const reportCumulativePreyKilled = document.getElementById('reportCumulativePreyKilled');
const reportCumulativeDefKilledContainer = document.getElementById('reportCumulativeDefKilledContainer');
const reportCumulativeDefKilled = document.getElementById('reportCumulativeDefKilled');
const reportCumulativeAttLysed = document.getElementById('reportCumulativeAttLysed');
const reportCumulativePreyLysed = document.getElementById('reportCumulativePreyLysed');
const reportCumulativeDefLysedContainer = document.getElementById('reportCumulativeDefLysedContainer');
const reportCumulativeDefLysed = document.getElementById('reportCumulativeDefLysed');
const reportTotalCPRGConverted = document.getElementById('reportTotalCPRGConverted');
const openHelpModalButton = document.getElementById('openHelpModal');
const helpModalOverlay = document.getElementById('helpModalOverlay');
const closeHelpModalButton = document.getElementById('closeHelpModalButton');
const openLiteratureModalButton = document.getElementById('openLiteratureModal'); // New
const literatureModalOverlay = document.getElementById('literatureModalOverlay'); // New
const closeLiteratureModalButton = document.getElementById('closeLiteratureModalButton'); // New
const viewGraphButton = document.getElementById('viewGraphButton');
const loadStateGroup = document.getElementById('loadStateGroup'); // NEW
const loadStepNumberInput = document.getElementById('loadStepNumberInput'); // NEW
const loadArenaStateToManualButton = document.getElementById('loadArenaStateToManualButton'); // NEW
const graphModalOverlay = document.getElementById('graphModalOverlay');
const closeGraphModalButton = document.getElementById('closeGraphModalButton');
let simulationChart = null;
// Presets Modal
const openPresetsModalButton = document.getElementById('openPresetsModalButton');
const presetsModalOverlay = document.getElementById('presetsModalOverlay');
const closePresetsModalButton = document.getElementById('closePresetsModalButton');
const presetsModalBody = document.getElementById('presetsModalBody');
const applyActivePresetButton = document.getElementById('applyActivePresetButton');
// Preset Group 1: Density
const densityFillSlider = document.getElementById('densityFillSlider');
const densityFillDisplay = document.getElementById('densityFillDisplay');
const densityAttPreyRatioSlider = document.getElementById('densityAttPreyRatioSlider');
const densityRatioDisplay = document.getElementById('densityRatioDisplay');
// Preset Group 2: Sensitivity
const sensitivityFillSlider = document.getElementById('sensitivityFillSlider');
const sensitivityFillDisplay = document.getElementById('sensitivityFillDisplay');
const sensitivityAttPreyRatioSlider = document.getElementById('sensitivityAttPreyRatioSlider');
const sensitivityRatioDisplay = document.getElementById('sensitivityRatioDisplay');
// Preset Group 3: Contact & Kin Exclusion
const contactKinContactSensingSlider = document.getElementById('contactKinContactSensingSlider');
const contactKinContactSensingDisplay = document.getElementById('contactKinContactSensingDisplay');
const contactKinKinExclusionSlider = document.getElementById('contactKinKinExclusionSlider');
const contactKinKinExclusionDisplay = document.getElementById('contactKinKinExclusionDisplay');
const contactKinFillSlider = document.getElementById('contactKinFillSlider');
const contactKinFillDisplay = document.getElementById('contactKinFillDisplay');
const contactKinAttPreyRatioSlider = document.getElementById('contactKinAttPreyRatioSlider');
const contactKinRatioDisplay = document.getElementById('contactKinRatioDisplay');
// Preset Group 4: Tit-for-Tat
const titfortatFillSlider = document.getElementById('titfortatFillSlider');
const titfortatFillDisplay = document.getElementById('titfortatFillDisplay');
// Preset Group 5: Capsule
const capsuleProtectionSlider = document.getElementById('capsuleProtectionSlider');
const capsuleProtectionDisplay = document.getElementById('capsuleProtectionDisplay');
const capsuleTimeSlider = document.getElementById('capsuleTimeSlider');
const capsuleTimeDisplay = document.getElementById('capsuleTimeDisplay');
const capsuleFillSlider = document.getElementById('capsuleFillSlider');
const capsuleFillDisplay = document.getElementById('capsuleFillDisplay');
const capsuleAttPreyRatioSlider = document.getElementById('capsuleAttPreyRatioSlider');
const capsuleRatioDisplay = document.getElementById('capsuleRatioDisplay');
// Preset Group 6: Predation
const predationLysesPerRepSlider = document.getElementById('predationLysesPerRepSlider');
const predationLysesPerRepDisplay = document.getElementById('predationLysesPerRepDisplay');
const predationFillSlider = document.getElementById('predationFillSlider');
const predationFillDisplay = document.getElementById('predationFillDisplay');
const predationAttPreyRatioSlider = document.getElementById('predationAttPreyRatioSlider');
const predationRatioDisplay = document.getElementById('predationRatioDisplay');
// Preset Group 7: Movement
const movementPreyAiProdSlider = document.getElementById('movementPreyAiProdSlider');
const movementPreyAiProdDisplay = document.getElementById('movementPreyAiProdDisplay');
const movementAttMoveProbSlider = document.getElementById('movementAttMoveProbSlider');
const movementAttMoveProbDisplay = document.getElementById('movementAttMoveProbDisplay');
const movementAttMoveDirSlider = document.getElementById('movementAttMoveDirSlider');
const movementAttMoveDirDisplay = document.getElementById('movementAttMoveDirDisplay');
const movementArenaRadiusSlider = document.getElementById('movementArenaRadiusSlider');
const movementArenaRadiusDisplay = document.getElementById('movementArenaRadiusDisplay');
const movementFillSlider = document.getElementById('movementFillSlider');
const movementFillDisplay = document.getElementById('movementFillDisplay');
const movementAttPreyRatioSlider = document.getElementById('movementAttPreyRatioSlider');
const movementRatioDisplay = document.getElementById('movementRatioDisplay');
// Preset Group 8: Quorum Sensing
const attackerQSProdSlider = document.getElementById('attackerQSProdSlider');
const attackerQSProdDisplay = document.getElementById('attackerQSProdDisplay');
const attackerQSKSlider = document.getElementById('attackerQSKSlider');
const attackerQSKDisplay = document.getElementById('attackerQSKDisplay');
const attackerQSNSlider = document.getElementById('attackerQSNSlider');
const attackerQSNDisplay = document.getElementById('attackerQSNDisplay');
const preyQSProdSlider = document.getElementById('preyQSProdSlider');
const preyQSProdDisplay = document.getElementById('preyQSProdDisplay');
const preyQSKSlider = document.getElementById('preyQSKSlider');
const preyQSKDisplay = document.getElementById('preyQSKDisplay');
const preyQSNSlider = document.getElementById('preyQSNSlider');
const preyQSNDisplay = document.getElementById('preyQSNDisplay');
const attackerQSArenaFillSlider = document.getElementById('attackerQSArenaFillSlider');
const attackerQSArenaFillDisplay = document.getElementById('attackerQSArenaFillDisplay');
const attackerQSAttPreyRatioSlider = document.getElementById('attackerQSAttPreyRatioSlider');
const attackerQSRatioDisplay = document.getElementById('attackerQSRatioDisplay');
// Preset Group 9: Battle Royale
const brArenaRadiusSlider = document.getElementById('brArenaRadiusSlider');
const brArenaRadiusDisplay = document.getElementById('brArenaRadiusDisplay');
const brFillSlider = document.getElementById('brFillSlider');
const brFillDisplay = document.getElementById('brFillDisplay');
const brAttackerPercentSlider = document.getElementById('brAttackerPercentSlider');
const brAttackerPercentDisplay = document.getElementById('brAttackerPercentDisplay');
const brAttackerPercentMaxDisplay = document.getElementById('brAttackerPercentMaxDisplay');
const brDefenderPercentSlider = document.getElementById('brDefenderPercentSlider');
const brDefenderPercentDisplay = document.getElementById('brDefenderPercentDisplay');
const brDefenderPercentMaxDisplay = document.getElementById('brDefenderPercentMaxDisplay');
const brMixDisplay = document.getElementById('brMixDisplay');
// Sliders
const brAttMovementSlider = document.getElementById('brAttMovementSlider');
const brAttMovementDisplay = document.getElementById('brAttMovementDisplay');
const brDefSelectivitySlider = document.getElementById('brDefSelectivitySlider');
const brDefSelectivityDisplay = document.getElementById('brDefSelectivityDisplay');
// Checkboxes
const brAttQsCheckbox = document.getElementById('brAttQsCheckbox');
const brAttKinCheckbox = document.getElementById('brAttKinCheckbox');
const brAttContactCheckbox = document.getElementById('brAttContactCheckbox');
const brAttPredationCheckbox = document.getElementById('brAttPredationCheckbox');
const brPreyMovementCheckbox = document.getElementById('brPreyMovementCheckbox');
const brPreyAiCheckbox = document.getElementById('brPreyAiCheckbox');
const brPreyCapsuleCheckbox = document.getElementById('brPreyCapsuleCheckbox');
const brDefMovementCheckbox = document.getElementById('brDefMovementCheckbox');
const brDefPredationCheckbox = document.getElementById('brDefPredationCheckbox');
const parameterToElementIdMap = {
"Arena_Radius": "arenaGridRadiusInput",
"Simulation_Duration_Minutes": "totalSimulationMinutesInput",
"Simulation_Step_Delay_ms": "simulationSpeedInput",
"Simulation_Render_Rate_every_N_steps": "renderRateInput",
"Simulation_Seed": "simulationSeedInput",
"Arena_State_Export_Enabled": "saveArenaStatesCheckbox",
"Full_State_History_Enabled": "saveFullHistoryCheckbox",
"Image_Export_Enabled": "saveImagesCheckbox",
"Image_Export_Size_px": "imageExportWidthInput",
"Image_Buffer_Size_Limit_MB": "imageBufferSizeLimitInput",
"History_Buffer_Size_Limit_MB": "historyBufferSizeLimitInput",
"Arena_State_Buffer_Size_Limit_MB": "arenaStateBufferSizeLimitInput",
"Attacker_Initial_Count": "initialAttackersInput",
"Attacker_Replication_Mean_min": "attackerReplicationMeanInput",
"Attacker_Replication_Range_min": "attackerReplicationRangeInput",
"Attacker_T6SS_Fire_Cooldown_Min_min": "t6ssFireCooldownMinInput",
"Attacker_T6SS_Fire_Cooldown_Max_min": "t6ssFireCooldownMaxInput",
"Attacker_T6SS_Precision_Percent": "attackerPrecisionInput",
"Attacker_T6SS_Contact_Sensing_Bias_Percent": "attackerContactSensingBiasInput",
"Attacker_T6SS_Kin_Exclusion_Percent": "attackerKinExclusionInput",
"Attacker_T6SS_Kin_Exclusion_Penalty_min": "attackerKinExclusionPenaltyInput",
"Attacker_T6SS_NL_Units_per_Hit": "attNonLyticUnitsPerHitInput",
"Attacker_T6SS_NL_Delivery_Chance_Percent": "attNonLyticDeliveryChanceInput",
"Attacker_T6SS_L_Units_per_Hit": "attLyticUnitsPerHitInput",
"Attacker_T6SS_L_Delivery_Chance_Percent": "attLyticDeliveryChanceInput",
"Attacker_Sensitivity_NL_Units_to_Die": "attNonLyticUnitsToDieInput",
"Attacker_Sensitivity_L_Units_to_Lyse": "attLyticUnitsToLyseInput",
"Attacker_Sensitivity_Base_Lysis_Delay_min": "attBaseLysisDelayInput",
"Attacker_Movement_Cooldown_Min_min": "attackerMoveCooldownMinInput",
"Attacker_Movement_Cooldown_Max_min": "attackerMoveCooldownMaxInput",
"Attacker_Movement_Probability_Percent": "attackerMoveProbabilityInput",
"Attacker_Movement_Directionality_Percent": "attackerMoveDirectionalityInput",
"Attacker_Movement_Prey_AI_Attraction_Percent": "attackerMovePreyAiAttractionInput",
"Attacker_Movement_Prey_AI_Attraction_Threshold": "attackerMovePreyAiAttractionThresholdInput",
"Attacker_QS_Production_Rate_per_min": "attackerQSProductionRateInput",
"Attacker_QS_Degradation_Rate_Percent_per_min": "attackerQSDegradationRateInput",
"Attacker_QS_Diffusion_Rate": "attackerQSDiffusionRateInput",
"Attacker_QS_Activation_Midpoint_K": "attackerQSMidpointInput",
"Attacker_QS_Cooperativity_n": "attackerQSCooperativityInput",
"Attacker_Replication_Reward_Lyses_per_Reward": "attackerLysesPerReplicationInput",
"Attacker_Replication_Reward_Mean_min": "attackerReplicationRewardMeanInput",
"Attacker_Replication_Reward_Range_min": "attackerReplicationRewardRangeInput",
"Prey_Initial_Count": "initialPreyInput",
"Prey_Replication_Mean_min": "preyReplicationMeanInput",
"Prey_Replication_Range_min": "preyReplicationRangeInput",
"Prey_Sensitivity_vs_Att_NL_Units_to_Die": "preyNonLyticUnitsToDieAttInput",
"Prey_Sensitivity_vs_Att_L_Units_to_Lyse": "preyLyticUnitsToLyseAttInput",
"Prey_Sensitivity_vs_Att_Base_Lysis_Delay_min": "preyBaseLysisDelayAttInput",
"Prey_Resistance_vs_Att_NL_Percent": "preyNonLyticResistanceAttInput",
"Prey_Resistance_vs_Att_L_Percent": "preyLyticResistanceAttInput",
"Prey_Sensitivity_vs_Def_NL_Units_to_Die": "preyNonLyticUnitsToDieDefInput",
"Prey_Sensitivity_vs_Def_L_Units_to_Lyse": "preyLyticUnitsToLyseDefInput",
"Prey_Sensitivity_vs_Def_Base_Lysis_Delay_min": "preyBaseLysisDelayDefInput",
"Prey_Resistance_vs_Def_NL_Percent": "preyNonLyticResistanceDefInput",
"Prey_Resistance_vs_Def_L_Percent": "preyLyticResistanceDefInput",
"Prey_LacZ_Units_per_Lysis": "lacZPerPreyInput",
"Prey_Movement_Cooldown_Min_min": "preyMoveCooldownMinInput",
"Prey_Movement_Cooldown_Max_min": "preyMoveCooldownMaxInput",
"Prey_Movement_Probability_Percent": "preyMoveProbabilityInput",
"Prey_Movement_Directionality_Percent": "preyMoveDirectionalityInput",
"Prey_QS_Production_Rate_per_min": "preyQSProductionRateInput",
"Prey_QS_Degradation_Rate_Percent_per_min": "preyQSDegradationRateInput",
"Prey_QS_Diffusion_Rate": "preyQSDiffusionRateInput",
"Prey_Capsule_System_Enabled": "preyCapsuleSystemEnabledCheckbox",
"Prey_Capsule_Max_Protection_Percent": "preyCapsuleMaxProtectionInput",
"Prey_Capsule_Derepression_Midpoint_K": "preyCapsuleDerepressionMidpointInput",
"Prey_Capsule_Cooperativity_n": "preyCapsuleCooperativityInput",
"Prey_Capsule_Cooldown_Min_min": "preyCapsuleCooldownMinInput",
"Prey_Capsule_Cooldown_Max_min": "preyCapsuleCooldownMaxInput",
"Defender_Initial_Count": "initialDefendersInput",
"Defender_Replication_Mean_min": "defenderReplicationMeanInput",
"Defender_Replication_Range_min": "defenderReplicationRangeInput",
"Defender_Retaliation_Sense_Chance_Percent": "defenderSenseChanceInput",
"Defender_Retaliation_Max_Shots": "defenderMaxRetaliationsInput",
"Defender_Random_Fire_Cooldown_Min_min": "defenderRandomFireCooldownMinInput",
"Defender_Random_Fire_Cooldown_Max_min": "defenderRandomFireCooldownMaxInput",
"Defender_Random_Fire_Chance_Percent": "defenderRandomFireChanceInput",
"Defender_T6SS_NL_Units_per_Hit": "defNonLyticUnitsPerHitInput",
"Defender_T6SS_NL_Delivery_Chance_Percent": "defNonLyticDeliveryChanceInput",
"Defender_T6SS_L_Units_per_Hit": "defLyticUnitsPerHitInput",
"Defender_T6SS_L_Delivery_Chance_Percent": "defLyticDeliveryChanceInput",
"Defender_Sensitivity_vs_Att_NL_Units_to_Die": "defNonLyticUnitsToDieInput",
"Defender_Sensitivity_vs_Att_L_Units_to_Lyse": "defLyticUnitsToLyseInput",
"Defender_Sensitivity_vs_Att_Base_Lysis_Delay_min": "defBaseLysisDelayInput",
"Defender_Resistance_vs_Att_NL_Percent": "defNonLyticResistanceInput",
"Defender_Resistance_vs_Att_L_Percent": "defLyticResistanceInput",
"Defender_Movement_Cooldown_Min_min": "defenderMoveCooldownMinInput",
"Defender_Movement_Cooldown_Max_min": "defenderMoveCooldownMaxInput",
"Defender_Movement_Probability_Percent": "defenderMoveProbabilityInput",
"Defender_Movement_Directionality_Percent": "defenderMoveDirectionalityInput",
"Defender_Replication_Reward_Lyses_per_Reward": "defenderLysesPerReplicationInput",
"Defender_Replication_Reward_Mean_min": "defenderReplicationRewardMeanInput",
"Defender_Replication_Reward_Range_min": "defenderReplicationRewardRangeInput",
"CPRG_Initial_Substrate_Units": "initialCPRGSubstrateInput",
"CPRG_LacZ_kcat_Units_per_min_per_LacZ": "lacZKcatInput",
"CPRG_LacZ_Km_Units": "lacZKmInput"
// Add any other parameters you export/import here
};
// This is the single source of truth for all simulation parameters.
// It is applied on page load to set the default state.
// All constants are in config.js
const SIMULATION_DEFAULTS = AppConfig.defaults;
// This table lists *only* the parameters that *differ* from the baseline.
// All constants are in config.js
const PRESET_OVERRIDES = AppConfig.baselineOverrides;
// This schema is CRUCIAL for saving and loading space-efficiently.
// --- Mappings to convert repetitive strings to integers for space efficiency ---
const TYPE_TO_INT = { 'attacker': 0, 'prey': 1, 'defender': 2, 'barrier': 3 };
const INT_TO_TYPE = ['attacker', 'prey', 'defender', 'barrier'];
// --- The schema is updated to store the numerical part of the ID ---
const CELL_SCHEMA = [
'q', 'r', 'type', 'id_num', // 'id' is now 'id_num'
'movementCooldown', 'replicationCooldown',
'accumulatedNonLyticToxins', 'accumulatedLyticToxins',
'isDead', 'isLysing', 'lysisTimer', 'isEffectivelyGone',
// Attacker-specific
't6ssFireCooldownTimer',
// Defender-specific
'sensedAttackFromKey', 'isRetaliating', 'retaliationTargetKey',
'retaliationsRemainingThisBurst', 'currentMaxRetaliationsForBurst',
't6ssRandomFireCooldownTimer',
// Prey-specific
'capsuleLayers', 'capsuleCooldown', 'isFormingCapsule',
'kills', 'lyses',
'claimedReplicationRewards'
];
let rng; // This will hold our PRNG instance
// --- Simulation State ---
let simState = {
cells: new Map(),
nextCellId: 0,
isInitialized: false,
isRunning: false,
isStepping: false,
manualSetupActive: false,
selectedManualCellType: 'prey',
simulationStepCount: 0,
timeoutId: null,
historyEnabled: true,
saveArenaStatesEnabled: true,
saveImagesEnabled: false,
imageExportResolution: { width: 1000, height: 1000 },
capturedImagesDataURLs: [],
capturedArenaStatesTSV: [],
capturedArenaStatesTSVTotalSize: 0,
directoryHandle: null, // New property to store the directory handle
isDrawingWithDrag: false, // True when mouse is down and dragging to draw
lastPlacedHexKeyDuringDrag: null, // Stores the 'q,r' key of the last hex a cell was placed in during a drag
activePresetConfig: { ...AppConfig.presetDefaults }, // will be populated from defaults in config.js
config: {}, // will be populated from defaults in config.js
offsetX: 0,
offsetY: 0,
activeFiringsThisStep: new Map(),
attackerAiGrid: new Map(),
preyAiGrid: new Map(),
lastHoveredHexKey: null, // To store the 'q,r' key of the last valid hex hovered
firingsThisStep: 0,
killedThisStep: { attacker: 0, prey: 0, defender: 0 },
lysedThisStep: { attacker: 0, prey: 0, defender: 0 },
cumulativeFirings: 0,
cumulativeKills: { attacker: 0, prey: 0, defender: 0 },
cumulativeLyses: { attacker: 0, prey: 0, defender: 0 },
totalCPRGConverted: 0,
remainingCPRGSubstrate: 0,
totalActiveLacZReleased: 0,
totalArenaSpaces: 0,
historicalData: [],
finalStateRecorded: false,
areCellsInSync: true, // Tracks if cells match the current seed state
rngDrawCount: 0, // Tracks how many times the RNG has been used since last seed
history: [],
isScrubbing: false, // To know when the user is using the time-travel slider
capturedImagesTotalSize: 0,
isWaitingForBatchDownload: false,
optimizedHistoryFrames: new Map(),
capturedHistoryTotalSize: 0,
lastMouseX: null,
lastMouseY: null,
runTimestamp: null,
lastRngCounts: [],
};
// --- Utility Functions ---
function applySettingsObject(settingsObject) {
for (const [paramName, value] of Object.entries(settingsObject)) {
const elementId = parameterToElementIdMap[paramName];
if (elementId) {
// Use "true" to dispatch a change event, just in case
updateInputElement(elementId, value, true);
} else {
// We don't warn for Simulation_Seed, it's handled separately
if (paramName !== "Simulation_Seed") {
console.warn(`Preset Warning: Parameter "${paramName}" not found in map.`);
}
}
}
}
function applyPresetByName(presetName) {
// 1. Validate the preset name exists in the config
// We check if it exists in overrides OR logic.
// Note: The UI uses keys like 'battleroyale', 'density', etc.
const group = presetName.toLowerCase();
if (!PRESET_OVERRIDES[group] && !AppConfig.presetLogic[group]) {
console.warn(`Preset '${group}' not found.`);
return;
}
console.log(`Applying preset defaults for: ${group}`);
// 2. Update the internal active config state to match the requested group
simState.activePresetConfig.group = group;
// 3. Apply Baseline Overrides (Static settings from config.js)
const baselineOverrides = PRESET_OVERRIDES[group] || {};
applySettingsObject(baselineOverrides);
// 4. Execute Dynamic Logic
// We use AppConfig.presetDefaults as the source of truth for the preset's
// "default" slider positions (e.g., brFillPercent: 30).
const presetLogicHandler = AppConfig.presetLogic[group]?.handler;
if (presetLogicHandler) {
// We pass the global preset defaults. The handler picks the specific fields it needs.
const dynamicSettings = presetLogicHandler(AppConfig.presetDefaults);
if (dynamicSettings) {
applySettingsObject(dynamicSettings);
}
}
// 5. Update UI to reflect that a preset is active (Optional, but good for UX)
// This highlights the correct group in the modal if the user opens it later.
const groupElementId = `presetGroup${group.charAt(0).toUpperCase() + group.slice(1)}`;
setActivePresetGroup(groupElementId);
}
function generateShareableLink() {
const baseUrl = window.location.href.split('?')[0];
const params = new URLSearchParams();
// 1. Capture the Seed (Always included for reproducibility)
const currentSeed = document.getElementById('simulationSeedInput').value;
params.append('Simulation_Seed', currentSeed);
// 2. Capture All Other Settings (Only if different from Default)
for (const [paramName, elementId] of Object.entries(parameterToElementIdMap)) {
// Skip Seed (handled above)
if (paramName === 'Simulation_Seed') continue;
const element = document.getElementById(elementId);
if (!element) continue;
let currentValue;
// Handle Checkboxes vs Numbers/Strings
if (element.type === 'checkbox') {
currentValue = element.checked;
} else {
const val = parseFloat(element.value);
currentValue = isNaN(val) ? element.value : val;
}
// Retrieve Default Value
const defaultValue = SIMULATION_DEFAULTS[paramName];
// logic: If the UI value differs from the default, add it to the URL
if (currentValue !== defaultValue && defaultValue !== undefined) {
params.append(paramName, currentValue);
}
}
// 3. Generate the Full URL
const fullUrl = `${baseUrl}?${params.toString()}`;
// 4. Copy to Clipboard AND Show to User
navigator.clipboard.writeText(fullUrl).then(() => {
showLinkModal(fullUrl, true); // true = success copy
}).catch(err => {
console.error('Clipboard write failed', err);
showLinkModal(fullUrl, false); // false = manual copy needed
});
}
// Helper function to reuse the Info Alert Modal for displaying links
function showLinkModal(url, clipboardSuccess) {
const overlay = document.getElementById('infoAlertModalOverlay');
const title = document.getElementById('infoAlertModalTitle');
const body = document.getElementById('infoAlertModalBody');
const okBtn = document.getElementById('okInfoAlertButton');
// Customize Title
title.textContent = clipboardSuccess ? "Link Copied & Generated!" : "Link Generated";
// Customize Body with an Input Field for the URL
body.innerHTML = `
<p class="mb-2 text-sm text-gray-600">
${clipboardSuccess
? "The configuration link has been copied to your clipboard."
: "Could not auto-copy. Please copy the link below:"}
</p>
<div class="relative">
<input type="text" readonly value="${url}"
class="w-full p-2 border border-gray-300 rounded bg-gray-50 text-xs font-mono text-blue-600 focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
onclick="this.select();">
</div>
<p class="mt-2 text-xs text-gray-500">
Includes Seed + only changed settings. Cell positions are random unless an Arena file is used.
</p>
`;
const closeModal = () => {
overlay.classList.add('hidden');
};
// 1. Clone the button to remove any old event listeners from previous alerts
// This prevents "stacking" actions or zombie listeners.
const newOkBtn = okBtn.cloneNode(true);
okBtn.parentNode.replaceChild(newOkBtn, okBtn);
// 2. Add the new click listener to close the modal
newOkBtn.addEventListener('click', closeModal, { once: true });
// 3. Also allow closing by clicking the background overlay
// (We assume the previous overlay listener is either gone or harmless,
// but adding a specific one for this instance is good practice)
overlay.addEventListener('click', (e) => {
if (e.target === overlay) {
closeModal();
}
}, { once: true });
// Show Modal
overlay.classList.remove('hidden');
}
function generateTimestamp() {
const now = new Date();
const year = String(now.getFullYear()).slice(-2);
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
return `${year}${month}${day}${hours}${minutes}${seconds}`;
}
function initializeSeededRNG(seed) {
if (typeof Math.seedrandom === 'undefined') {
console.error("Math.seedrandom function not found. Cannot create a seeded PRNG.");
rng = Math.random;
return;
}
// 1. Create the original, "private" seeded function
const privateRng = new Math.seedrandom(seed);
// 2. Redefine the global 'rng' function as a wrapper
rng = function() {
if (simState.rngDrawCount === 0) {
// On the first draw, immediately update the UI
updateSyncAndRngButtons();
}
simState.rngDrawCount++;
// 3. Call the original, private function and return its value
return privateRng();
};
// 4. Reset the draw count and update the UI
simState.rngDrawCount = 0;
updateSyncAndRngButtons();
console.warn(`--- RNG RESET with seed: "${seed}" ---`);
}
// generate a random 6-digit seed
function generateNewSeed() {
return Math.floor(100000 + Math.random() * 900000);
}
// get a random number based on the given seed
function getRandomIntInRange(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(rng() * (max - min + 1)) + min;
}
function synchronizeRNG(targetCount) {
// This function assumes the RNG has already been re-seeded and its draw count is 0.
console.log(`Synchronizing RNG. Target draws: ${targetCount}. Current draws: ${simState.rngDrawCount}`);
const numbersToBurn = targetCount - simState.rngDrawCount;
if (numbersToBurn > 0) {
console.log(`Fast-forwarding by burning ${numbersToBurn} random numbers...`);
for (let i = 0; i < numbersToBurn; i++) {
rng();
}
console.log(`RNG synchronized. Final draw count: ${simState.rngDrawCount}`);
}
}
function checkForRngSpike() {
// This check can only run if we have data from the previous two steps
if (simState.lastRngCounts.length < 2) {
return;
}
const count_N_minus_2 = simState.lastRngCounts[0];
const count_N_minus_1 = simState.lastRngCounts[1];
const current_count_N = simState.rngDrawCount;
const previous_delta = count_N_minus_1 - count_N_minus_2;
const current_delta = current_count_N - count_N_minus_1;
// Trigger if the current jump is 50% larger than the previous one,
// and only for significant jumps (e.g., > 100) to avoid flagging small initial fluctuations.
if (current_delta > (previous_delta * 1.5) && current_delta > 100) {
console.warn(
`--- UNEXPECTED RNG SPIKE DETECTED at Step: ${simState.simulationStepCount} ---
Previous Step's Change (+${previous_delta})
Current Step's Change (+${current_delta})`
);
}
}
function seededShuffle(array, seededRng) {
let currentIndex = array.length;
let randomIndex;
// While there remain elements to shuffle.
while (currentIndex !== 0) {
// Pick a remaining element.
randomIndex = Math.floor(seededRng() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];
}
return array;
}
async function fetchFileContent(fileURL, fileTypeDesc, responseType = 'text') {
try {
const response = await fetch(fileURL);
if (!response.ok) {
let errorDetail = response.statusText || `HTTP error ${response.status}`;
try {
const errorBody = await response.text();
if (errorBody) errorDetail += `: ${errorBody.substring(0, 100)}`;
} catch (e) { /* ignore */ }
throw new Error(errorDetail);
}
let content;
if (responseType === 'arrayBuffer') {
content = await response.arrayBuffer();
} else {
content = await response.text();
}
if (!content || (responseType === 'text' && content.trim() === "") || (responseType === 'arrayBuffer' && content.byteLength === 0)) {
throw new Error(`File from ${fileURL} is empty or invalid.`);
}
console.log(`${fileTypeDesc} file content fetched successfully from ${fileURL}`);
return content;
} catch (error) {
console.error(`Error fetching ${fileTypeDesc} file from ${fileURL}:`, error);
simulationErrorDisplay.textContent = `Error fetching ${fileTypeDesc} file (${fileURL}): ${error.message}. Check URL, network, and CORS policy.`;
simulationErrorDisplay.classList.remove('hidden');
return null;
}
}
function parseQrCellString(cellsStr) {
const operations = [];
let failedCount = 0;
if (!cellsStr || cellsStr.trim() === '') {
return { operations, failedCount };
}
// Split the string before each 'q' to separate concatenated cell definitions.
// The filter(s => s) removes any empty strings that might result from the split.
const potentialCellParts = cellsStr.split(/(?=q)/).filter(s => s);
// This regex now validates a SINGLE, complete cell definition from start (^) to end ($).
// It only allows the valid type characters [APDEB].
const singleCellRegex = /^q(-?\d+)r(-?\d+)([APDEB])$/;
for (const part of potentialCellParts) {
const match = part.match(singleCellRegex);
if (match) {
// The part is valid, e.g., "q10r0P"
try {
const q = parseInt(match[1], 10);
const r = parseInt(match[2], 10);
const typeChar = match[3];
let cellType = null;
let action = 'place';
if (typeChar === 'A') cellType = 'attacker';
else if (typeChar === 'P') cellType = 'prey';
else if (typeChar === 'D') cellType = 'defender';
else if (typeChar === 'B') cellType = 'barrier';
else if (typeChar === 'E') {
action = 'remove';
cellType = null;
}
// This check is mostly for safety; the regex should ensure q and r are numbers.
if (!isNaN(q) && !isNaN(r)) {
operations.push({ q, r, type: cellType, action: action });
} else {
failedCount++;
}
} catch (e) {
console.error("Error processing valid cell part:", part, e);
failedCount++;
}
} else {
// The part is malformed, e.g., "q0r0C". It did not match the strict regex.
console.warn(`URL 'cellsData': Malformed part ignored: "${part}"`);
failedCount++;
}
}
return { operations, failedCount };
}
function calculateTheoreticalMaxAI() {
const p = simState.config.prey.qs.productionRate;
const d = simState.config.prey.qs.degradationRate; // This is a decimal, e.g., 0.02 for 2%
// If there is no production, the max is zero.
if (p === 0) return 0;
// If there is production but no degradation, the concentration
// would grow infinitely. We return Infinity to represent this.
if (d === 0) return Infinity;
return p / d;
}
// --- Cell Class ---
class Cell {
constructor(q, r, type, id, isForRehydration = false) {
this.q = q;
this.r = r;
this.type = type;
this.id = id;
// This is crucial for the optimized save/load functions.
// Universal properties
this.kills = 0;
this.lyses = 0;
this.claimedReplicationRewards = 0;
this.isDead = false;
this.isLysing = false;
this.isEffectivelyGone = false;
this.lysisTimer = 0;
this.replicationCooldown = Infinity; // Default to non-replicating
this.movementCooldown = Infinity; // Default to non-motile
this.accumulatedNonLyticToxins = 0;
this.accumulatedLyticToxins = 0;
// Attacker-specific properties (defaults for others)
this.t6ssFireCooldownTimer = 0;
// Defender-specific properties (defaults for others)
this.sensedAttackFromKey = null;
this.isRetaliating = false;
this.retaliationTargetKey = null;
this.retaliationsRemainingThisBurst = 0;
this.currentMaxRetaliationsForBurst = 0;
this.t6ssRandomFireCooldownTimer = 0;
// Prey-specific properties (defaults for others)
this.capsuleLayers = 0;
this.capsuleCooldown = 0;
this.isFormingCapsule = false;
if (type === 'barrier') {
// Barrier-specific overrides. This part is simple and has no RNG.
this.replicationCooldown = Infinity;
// No other overrides are needed because the defaults are correct for a barrier.
} else {
// This 'else' block handles ALL biological cells ('attacker', 'prey', 'defender').
// 3. This is the fix: Only run RNG-dependent code for NEW cells.
if (!isForRehydration) {
// Initialize random cooldowns for movement and replication
this.movementCooldown = this.getRandomMoveTime();
this.replicationCooldown = this.getRandomReplicationTime();
// Stagger the initial start times randomly
this.movementCooldown = getRandomIntInRange(0, this.movementCooldown);
this.replicationCooldown = getRandomIntInRange(0, this.replicationCooldown);
// Set initial random T6SS cooldowns for attackers
if (type === 'attacker') {
this.resetT6SSFireCooldown(true);
}
if (type === 'defender') {
this.resetRandomFireCooldown(true);
}
}
}
}
getRandomReplicationTime() {
if (this.type === 'barrier') return Infinity;
let rateConfig;
if (this.type === 'attacker') rateConfig = simState.config.attacker.replication;
else if (this.type === 'prey') rateConfig = simState.config.prey.replication;
else if (this.type === 'defender') rateConfig = simState.config.defender.replication;
if (rateConfig && rateConfig.mean < 0) {
return Infinity; // Special value means no replication
}
if (!rateConfig || typeof rateConfig.mean !== 'number' || typeof rateConfig.range !== 'number') {
console.warn(`Invalid replication config for ${this.type}. Using default 20.`);
return 20;
}
return getRandomIntInRange(
Math.max(1, rateConfig.mean - rateConfig.range),
rateConfig.mean + rateConfig.range
);
}
checkAndApplyReplicationReward() {
if (this.type !== 'attacker' && this.type !== 'defender') return;
const rewardConfig = simState.config[this.type].replicationReward;
// Use 0 to disable the feature, as requested.
if (!rewardConfig || rewardConfig.lysesPerReward === 0) {
return;
}
// Check if the number of rewards earned is greater than rewards claimed.
const rewardsEarned = Math.floor(this.lyses / rewardConfig.lysesPerReward);
if (rewardsEarned > this.claimedReplicationRewards) {
const mean = rewardConfig.mean;
const range = rewardConfig.range;
// Claim the reward *before* applying it.
this.claimedReplicationRewards++;
// If mean is -1, the reward is immediate replication.
if (mean === -1) {
this.replicationCooldown = 0;
return;
}