-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
8616 lines (7573 loc) · 393 KB
/
Copy pathscript.js
File metadata and controls
8616 lines (7573 loc) · 393 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. 10.42 (9.7.2026)
// 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');
const renderFromHistoryButton = document.getElementById('renderFromHistoryButton');
const renderFromStepInput = document.getElementById('renderFromStepInput');
const renderToStepInput = document.getElementById('renderToStepInput');
const renderRateStepInput = document.getElementById('renderRateStepInput');
const cancelRenderButton = document.getElementById('cancelRenderButton');
const cancelImportSessionButton = document.getElementById('cancelImportSessionButton');
// 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');
const attackerResistanceVsPreyToxinNLInput = document.getElementById('attackerResistanceVsPreyToxinNLInput');
const attackerResistanceVsPreyToxinLInput = document.getElementById('attackerResistanceVsPreyToxinLInput');
const attackerAbsorptRateVsPreyToxinNLInput = document.getElementById('attackerAbsorptRateVsPreyToxinNLInput');
const attackerAbsorptRateVsPreyToxinLInput = document.getElementById('attackerAbsorptRateVsPreyToxinLInput');
const attackerThresholdVsPreyToxinNLInput = document.getElementById('attackerThresholdVsPreyToxinNLInput');
const attackerThresholdVsPreyToxinLInput = document.getElementById('attackerThresholdVsPreyToxinLInput');
// 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');
const preyToxinNLProductionRateInput = document.getElementById('preyToxinNLProductionRateInput');
const preyToxinNLDegradationRateInput = document.getElementById('preyToxinNLDegradationRateInput');
const preyToxinNLDiffusionRateInput = document.getElementById('preyToxinNLDiffusionRateInput');
const preyToxinLProductionRateInput = document.getElementById('preyToxinLProductionRateInput');
const preyToxinLDegradationRateInput = document.getElementById('preyToxinLDegradationRateInput');
const preyToxinLDiffusionRateInput = document.getElementById('preyToxinLDiffusionRateInput');
const preyToxinTriggerModeSelect = document.getElementById('preyToxinTriggerModeSelect');
const preyToxinQSDerepressionMidpointInput = document.getElementById('preyToxinQSDerepressionMidpointInput');
const preyToxinQSCooperativityInput = document.getElementById('preyToxinQSCooperativityInput');
const preyToxinReleaseOnLysisCheckbox = document.getElementById('preyToxinReleaseOnLysisCheckbox');
const preyToxinLysisThresholdMinInput = document.getElementById('preyToxinLysisThresholdMinInput');
const preyToxinLysisThresholdMaxInput = document.getElementById('preyToxinLysisThresholdMaxInput');
const preyToxinStartProbabilityInput = document.getElementById('preyToxinStartProbabilityInput');
const preyToxinInitiationThresholdInput = document.getElementById('preyToxinInitiationThresholdInput');
// 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');
const defenderResistanceVsPreyToxinNLInput = document.getElementById('defenderResistanceVsPreyToxinNLInput');
const defenderResistanceVsPreyToxinLInput = document.getElementById('defenderResistanceVsPreyToxinLInput');
const defenderAbsorptRateVsPreyToxinNLInput = document.getElementById('defenderAbsorptRateVsPreyToxinNLInput');
const defenderAbsorptRateVsPreyToxinLInput = document.getElementById('defenderAbsorptRateVsPreyToxinLInput');
const defenderThresholdVsPreyToxinNLInput = document.getElementById('defenderThresholdVsPreyToxinNLInput');
const defenderThresholdVsPreyToxinLInput = document.getElementById('defenderThresholdVsPreyToxinLInput');
// 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: Prey Toxin Production
const preyToxinArenaRadiusSlider = document.getElementById('preyToxinArenaRadiusSlider');
const preyToxinArenaRadiusDisplay = document.getElementById('preyToxinArenaRadiusDisplay');
const preyToxinFillSlider = document.getElementById('preyToxinFillSlider');
const preyToxinFillDisplay = document.getElementById('preyToxinFillDisplay');
const preyToxinAttPreyRatioSlider = document.getElementById('preyToxinAttPreyRatioSlider');
const preyToxinRatioDisplay = document.getElementById('preyToxinRatioDisplay');
const preyToxinStartProbabilitySlider = document.getElementById('preyToxinStartProbabilitySlider');
const preyToxinStartProbabilityDisplay = document.getElementById('preyToxinStartProbabilityDisplay');
const preyToxinNLProdSlider = document.getElementById('preyToxinNLProdSlider');
const preyToxinNLProdDisplay = document.getElementById('preyToxinNLProdDisplay');
const preyToxinLProdSlider = document.getElementById('preyToxinLProdSlider');
const preyToxinLProdDisplay = document.getElementById('preyToxinLProdDisplay');
// Preset Group 10: 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 brPreyToxinCheckbox = document.getElementById('brPreyToxinCheckbox');
const brPreyToxinStartProbabilitySlider = document.getElementById('brPreyToxinStartProbabilitySlider');
const brPreyToxinStartProbabilityDisplay = document.getElementById('brPreyToxinStartProbabilityDisplay');
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_History_Record_Rate": "historyRecordRateInput",
"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",
"Prey_Toxin_NL_Production_Rate_per_min": "preyToxinNLProductionRateInput",
"Prey_Toxin_NL_Degradation_Rate_Percent_per_min": "preyToxinNLDegradationRateInput",
"Prey_Toxin_NL_Diffusion_Rate": "preyToxinNLDiffusionRateInput",
"Prey_Toxin_L_Production_Rate_per_min": "preyToxinLProductionRateInput",
"Prey_Toxin_L_Degradation_Rate_Percent_per_min": "preyToxinLDegradationRateInput",
"Prey_Toxin_L_Diffusion_Rate": "preyToxinLDiffusionRateInput",
"Prey_Toxin_Trigger_Mode": "preyToxinTriggerModeSelect",
"Prey_Toxin_QS_Derepression_Midpoint_K": "preyToxinQSDerepressionMidpointInput",
"Prey_Toxin_QS_Cooperativity_n": "preyToxinQSCooperativityInput",
"Prey_Toxin_Release_On_Lysis": "preyToxinReleaseOnLysisCheckbox",
"Prey_Toxin_Lysis_Threshold_Min": "preyToxinLysisThresholdMinInput",
"Prey_Toxin_Lysis_Threshold_Max": "preyToxinLysisThresholdMaxInput",
"Prey_Toxin_Start_Probability_Percent": "preyToxinStartProbabilityInput",
"Prey_Toxin_Initiation_Threshold": "preyToxinInitiationThresholdInput",
"Attacker_Resistance_vs_Prey_Toxin_NL_Percent": "attackerResistanceVsPreyToxinNLInput",
"Attacker_Resistance_vs_Prey_Toxin_L_Percent": "attackerResistanceVsPreyToxinLInput",
"Attacker_Prey_Toxin_NL_Threshold": "attackerThresholdVsPreyToxinNLInput",
"Attacker_Prey_Toxin_L_Threshold": "attackerThresholdVsPreyToxinLInput",
"Attacker_Prey_Toxin_NL_Absorption_Rate_Percent": "attackerAbsorptRateVsPreyToxinNLInput",
"Attacker_Prey_Toxin_L_Absorption_Rate_Percent": "attackerAbsorptRateVsPreyToxinLInput",
"Defender_Resistance_vs_Prey_Toxin_NL_Percent": "defenderResistanceVsPreyToxinNLInput",
"Defender_Resistance_vs_Prey_Toxin_L_Percent": "defenderResistanceVsPreyToxinLInput",
"Defender_Prey_Toxin_NL_Threshold": "defenderThresholdVsPreyToxinNLInput",
"Defender_Prey_Toxin_L_Threshold": "defenderThresholdVsPreyToxinLInput",
"Defender_Prey_Toxin_NL_Absorption_Rate_Percent": "defenderAbsorptRateVsPreyToxinNLInput",
"Defender_Prey_Toxin_L_Absorption_Rate_Percent": "defenderAbsorptRateVsPreyToxinLInput"
};
// 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',
'accumulatedPreyToxinNL', 'accumulatedPreyToxinL',
'isDead', 'isLysing', 'lysisTimer', 'isEffectivelyGone',
// Attacker-specific
't6ssFireCooldownTimer',
// Defender-specific
'sensedAttackFromKey', 'isRetaliating', 'retaliationTargetKey',
'retaliationsRemainingThisBurst', 'currentMaxRetaliationsForBurst',
't6ssRandomFireCooldownTimer',
// Prey-specific
'capsuleLayers', 'capsuleCooldown', 'isFormingCapsule',
'kills', 'lyses',
'claimedReplicationRewards',
'internalPreyToxinNL', 'internalPreyToxinL', 'isPreyToxinProducer',
'preyToxinLysisThreshold',
'continuousToxinProduced'
];
let rng; // This will hold our PRNG instance
// --- Optimized Struct-of-Arrays Wrappers ---
class CellMap extends Map {
constructor(radius) {
super();
this.radius = radius || 0;
this.width = (this.radius * 2) + 1;
this.maxCells = this.width * this.width;
this.grid = new Array(this.maxCells);
this.sortedActiveCells = [];
}
getIndex(q, r) { if (q < -this.radius || q > this.radius || r < -this.radius || r > this.radius) return -1; return (q + this.radius) * this.width + (r + this.radius); }
clear() {
super.clear();
this.grid = new Array(this.maxCells);
this.sortedActiveCells = [];
}
has(key) {
return super.has(key);
}
get(key) {
return super.get(key);
}
getByCoords(q, r) {
const idx = this.getIndex(q, r);
if (idx >= 0 && idx < this.maxCells) return this.grid[idx];
return undefined;
}
set(key, cell) {
const idx = this.getIndex(cell.q, cell.r);
if (idx < 0 || idx >= this.maxCells) return this;
this.grid[idx] = cell;
super.set(key, cell);
if (!cell._inSortedList) {
cell._inSortedList = true;
let low = 0, high = this.sortedActiveCells.length;
while (low < high) {
let mid = (low + high) >>> 1;
if (this.sortedActiveCells[mid].id < cell.id) low = mid + 1;
else high = mid;
}
this.sortedActiveCells.splice(low, 0, cell);
}
return this;
}
delete(key) {
const [q, r] = key.split(',').map(Number);
const idx = this.getIndex(q, r);
const cell = this.get(key);
if (cell && cell._inSortedList) {
cell._inSortedList = false;
let low = 0, high = this.sortedActiveCells.length;
while (low < high) {
let mid = (low + high) >>> 1;
if (this.sortedActiveCells[mid].id < cell.id) low = mid + 1;
else high = mid;
}
if (this.sortedActiveCells[low] && this.sortedActiveCells[low].id === cell.id) {
this.sortedActiveCells.splice(low, 1);
}
}
if (idx >= 0 && idx < this.maxCells && this.grid[idx] !== undefined) {
this.grid[idx] = undefined;
super.delete(key);
return true;
}
return false;
}
compact() {}
}
class FloatGrid {
constructor(radius) {
this.radius = radius || 0;
this.width = (this.radius * 2) + 1;
this.maxCells = this.width * this.width;
this.grid = new Float64Array(this.maxCells);
}
getIndex(q, r) { if (q < -this.radius || q > this.radius || r < -this.radius || r > this.radius) return -1; return (q + this.radius) * this.width + (r + this.radius); }
clear() { this.grid.fill(0); }
_fastIndex(key) {
const comma = key.indexOf(',');
const q = +(key.slice(0, comma));
const r = +(key.slice(comma + 1));
return this.getIndex(q, r);
}
has(key) {
const idx = this._fastIndex(key);
return idx >= 0 && idx < this.maxCells;
}
getByCoords(q, r) {
const idx = this.getIndex(q, r);
return (idx >= 0 && idx < this.maxCells) ? this.grid[idx] : 0;
}
setByCoords(q, r, value) {
const idx = this.getIndex(q, r);
if (idx >= 0 && idx < this.maxCells) {
this.grid[idx] = value;
}
return this;
}
get(key) {
const idx = this._fastIndex(key);
return (idx >= 0 && idx < this.maxCells) ? this.grid[idx] : 0;
}
set(key, value) {
const idx = this._fastIndex(key);
if (idx >= 0 && idx < this.maxCells) {
this.grid[idx] = value;
}
return this;
}
forEach(callback) {
// This is very slow, but maintains compatibility for drawing/exports
for (let q = -this.radius; q <= this.radius; q++) {
const rMin = Math.max(-this.radius, -q - this.radius);
const rMax = Math.min(this.radius, -q + this.radius);
for (let r = rMin; r <= rMax; r++) {
const idx = this.getIndex(q, r);
const val = this.grid[idx];
if (val > 0) callback(val, `${q},${r}`, this);
}
}
}
entries() {
const entriesArr = [];
this.forEach((val, key) => entriesArr.push([key, val]));
return entriesArr;
}
[Symbol.iterator]() { return this.entries()[Symbol.iterator](); }
}
// --- Simulation State ---
let simState = {
cells: new CellMap(0),
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 FloatGrid(0),
preyAiGrid: new FloatGrid(0),
preyToxinNLGrid: new FloatGrid(0),
preyToxinLGrid: new FloatGrid(0),
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
isRenderingFromHistory: false,
renderCancelled: false,
isImportingSession: false,
importSessionCancelled: false,
capturedImagesTotalSize: 0,
isWaitingForBatchDownload: false,
optimizedHistoryFrames: new Map(),
capturedHistoryTotalSize: 0,
lastMouseX: null,
lastMouseY: null,
runTimestamp: null,
realTimeStartTime: null,
realTimeElapsedMs: 0,
lastUiUpdateTime: 0,
lastRngCounts: [],
isHistoryPlaying: false,
};
let neighborCache = new Map();
let neighborCacheIdx = null;
let pixelCoordinatesCache = new Map();
let mainEmptyGridCanvas = null;
let exportEmptyGridCanvas = null;
let exportEmptyGridCanvasKey = "";
const BR_TOXIN_START_PROB_VALUES = [0.001, 0.003, 0.01, 0.03, 0.1, 0.3];
class SimulationDB {
constructor() {
this.sessionId = Date.now().toString(36) + Math.random().toString(36).substr(2, 5);
this.dbName = "BacFighT6_DB_" + this.sessionId;
this.dbVersion = 1;
this.db = null;
window.addEventListener("beforeunload", () => {
if (this.db) {
this.db.close();
}
if (indexedDB && indexedDB.deleteDatabase) {
indexedDB.deleteDatabase(this.dbName);
}
});
}
async cleanupOldDatabases() {
try {
if (indexedDB.databases) {
const dbs = await indexedDB.databases();
for (const dbInfo of dbs) {
if (dbInfo.name && dbInfo.name.startsWith("BacFighT6_DB_") && dbInfo.name !== this.dbName) {
indexedDB.deleteDatabase(dbInfo.name);
}
}
} else {
// Fallback legacy cleanup
if (indexedDB.deleteDatabase) {
indexedDB.deleteDatabase("BacFighT6_DB");
}
}
} catch (e) {
console.warn("Could not cleanup old databases", e);
}
}
init() {
this.cleanupOldDatabases();
return new Promise((resolve, reject) => {
const request = indexedDB.open(this.dbName, this.dbVersion);
request.onupgradeneeded = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains("history")) {
db.createObjectStore("history", { keyPath: "step" });
}
if (!db.objectStoreNames.contains("images")) {
db.createObjectStore("images", { keyPath: "step" });
}
if (!db.objectStoreNames.contains("arena_states")) {
db.createObjectStore("arena_states", { keyPath: "step" });
}
};
request.onsuccess = (event) => {
this.db = event.target.result;
resolve(this.db);
};
request.onerror = (event) => {
console.error("IndexedDB initialization error:", event.target.error);
reject(event.target.error);
};
});
}
clearAll() {
return new Promise((resolve, reject) => {
if (!this.db) { resolve(); return; }
const transaction = this.db.transaction(["history", "images", "arena_states"], "readwrite");
transaction.objectStore("history").clear();
transaction.objectStore("images").clear();
transaction.objectStore("arena_states").clear();
transaction.oncomplete = () => resolve();
transaction.onerror = (e) => reject(e.target.error);
});
}
// History methods
putHistory(step, data) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction("history", "readwrite");
transaction.objectStore("history").put({ step, data });
transaction.oncomplete = () => resolve();
transaction.onerror = (e) => reject(e.target.error);
});
}
getHistory(step) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction("history", "readonly");
const request = transaction.objectStore("history").get(step);
request.onsuccess = () => resolve(request.result ? request.result.data : null);
request.onerror = (e) => reject(e.target.error);
});
}
getAllHistory() {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction("history", "readonly");
const request = transaction.objectStore("history").getAll();
request.onsuccess = () => resolve(request.result.map(item => item.data));
request.onerror = (e) => reject(e.target.error);
});
}
clearHistory() {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction("history", "readwrite");
transaction.objectStore("history").clear();
transaction.oncomplete = () => resolve();
transaction.onerror = (e) => reject(e.target.error);
});
}
deleteHistoryGreaterThan(step) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction("history", "readwrite");
const store = transaction.objectStore("history");
const range = IDBKeyRange.lowerBound(step, true);
const request = store.openCursor(range);
request.onsuccess = (event) => {
const cursor = event.target.result;
if (cursor) {
store.delete(cursor.key);
cursor.continue();
} else { resolve(); }
};
request.onerror = (e) => reject(e.target.error);
});
}
// Images methods
putImage(step, dataURL) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction("images", "readwrite");
transaction.objectStore("images").put({ step, dataURL });
transaction.oncomplete = () => resolve();
transaction.onerror = (e) => reject(e.target.error);
});
}
getImage(step) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction("images", "readonly");
const request = transaction.objectStore("images").get(step);
request.onsuccess = () => resolve(request.result ? request.result.dataURL : null);
request.onerror = (e) => reject(e.target.error);
});
}
getAllImages() {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction("images", "readonly");
const request = transaction.objectStore("images").getAll();
request.onsuccess = () => resolve(request.result.map(item => ({ step: item.step, dataURL: item.dataURL })));
request.onerror = (e) => reject(e.target.error);
});
}
getImagesBySteps(steps) {
return new Promise((resolve, reject) => {
if (steps.length === 0) return resolve(new Map());
const transaction = this.db.transaction("images", "readonly");
const store = transaction.objectStore("images");
const results = new Map();
let completed = 0;
let hasError = false;
for (const step of steps) {
const request = store.get(step);
request.onsuccess = () => {
if (hasError) return;
if (request.result) results.set(step, request.result.dataURL);
if (++completed === steps.length) resolve(results);
};
request.onerror = (e) => {
if (!hasError) {
hasError = true;
reject(e.target.error);
}
};
}
});
}
deleteImage(step) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction("images", "readwrite");
transaction.objectStore("images").delete(step);
transaction.oncomplete = () => resolve();
transaction.onerror = (e) => reject(e.target.error);
});
}
deleteImagesGreaterThan(step) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction("images", "readwrite");
const store = transaction.objectStore("images");
const range = IDBKeyRange.lowerBound(step, true);
const request = store.openCursor(range);
request.onsuccess = (event) => {
const cursor = event.target.result;
if (cursor) {
store.delete(cursor.key);
cursor.continue();
} else { resolve(); }
};
request.onerror = (e) => reject(e.target.error);
});
}
clearImages() {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction("images", "readwrite");
transaction.objectStore("images").clear();
transaction.oncomplete = () => resolve();
transaction.onerror = (e) => reject(e.target.error);
});
}
// Arena states methods
putArenaState(step, tsvData) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction("arena_states", "readwrite");
transaction.objectStore("arena_states").put({ step, tsvData });
transaction.oncomplete = () => resolve();
transaction.onerror = (e) => reject(e.target.error);
});
}
getArenaState(step) {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction("arena_states", "readonly");
const request = transaction.objectStore("arena_states").get(step);
request.onsuccess = () => resolve(request.result ? request.result.tsvData : null);
request.onerror = (e) => reject(e.target.error);
});
}
getAllArenaStates() {
return new Promise((resolve, reject) => {
const transaction = this.db.transaction("arena_states", "readonly");
const request = transaction.objectStore("arena_states").getAll();
request.onsuccess = () => resolve(request.result.map(item => ({ step: item.step, tsvData: item.tsvData })));