-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmastery_calc.py
More file actions
executable file
·2064 lines (1748 loc) · 71.3 KB
/
mastery_calc.py
File metadata and controls
executable file
·2064 lines (1748 loc) · 71.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import argparse
import bisect
import copy
import dataclasses
import math
from typing import Iterator, Self
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import sciform
# Global constants
# fmt: off
# Base game data
GAME_SPEED = 5.0 * 0.8
WAVE_DURATION = 26.0
WAVE_COOLDOWN = 5.0
TIERS = list(range(1, 19))
TIER_COIN_BONUS = [1.0, 1.8, 2.6, 3.4, 4.2, 5.0, 5.8, 6.6, 7.5, 8.7, 10.3, 12.2, 14.7, 17.6, 21.3, 25.2, 29.1, 33.0]
assert len(TIERS) == len(TIER_COIN_BONUS)
TIER_CELL_DROP_MIN = [*([1] * 13), 7, 11, 12, 12, 12]
assert len(TIERS) == len(TIER_CELL_DROP_MIN)
TIER_CELL_DROP_MAX = [*range(1, 14), 14, 15, 16, 17, 18]
assert len(TIERS) == len(TIER_CELL_DROP_MAX)
TIER_REROLL_DROP = [1, 2, 3, 4, 6, 8, 12, 18, 25, 32, 40, 45, 50, 55, 60, 65, 70, 75]
assert len(TIERS) == len(TIER_REROLL_DROP)
TIER_BOSS_PERIOD = [*([10] * 13), 9, 8, 7, 6, 5]
assert len(TIERS) == len(TIER_BOSS_PERIOD)
SPAWN_RATE_SEQUENCE = [10, 11, 13, 15, 17, 19, 20, 22, 24, 26, 28, 30, 32, 34, 36, 37, 39, 40, 42, 44, 46, 48, 49, 50, 52, 54, 56]
SPAWN_RATE_WAVES = [1, 3, 6, 20, 40, 60, 80, 100, 150, 200, 250, 300, 400, 600, 800, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500]
assert len(SPAWN_RATE_SEQUENCE) == len(SPAWN_RATE_WAVES)
SPAWN_RATE_FACTOR = 8 * 1.9 / 100 # 8 times per second, +90% from EB, convert to percent
SPAWN_CHANCE_TABLE = {
"fast": [0.05, 0.05, 0.06, 0.07, 0.08, 0.09, 0.10, 0.10, 0.11, 0.11, 0.12, 0.12, 0.13, 0.13, 0.13, 0.14, 0.15, 0.17, 0.18, 0.19, 0.20, 0.21, 0.21, 0.22, 0.23, 0.24, 0.24],
"tank": [0.00, 0.02, 0.04, 0.06, 0.07, 0.08, 0.08, 0.09, 0.10, 0.11, 0.12, 0.13, 0.13, 0.14, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.19, 0.20, 0.20, 0.20, 0.21, 0.21, 0.22],
"ranged": [0.00, 0.00, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.06, 0.07, 0.07, 0.08, 0.09, 0.10, 0.11, 0.11, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.19, 0.19, 0.20, 0.21],
"protector": [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.01, 0.01, 0.01, 0.02, 0.02, 0.02, 0.03, 0.03, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04],
}
assert all(len(row) == len(SPAWN_RATE_SEQUENCE) for row in SPAWN_CHANCE_TABLE.values())
SPAWN_CHANCE_TABLE["basic"] = [
1.0 - sum(row[i] for row in SPAWN_CHANCE_TABLE.values())
for i in range(len(SPAWN_RATE_SEQUENCE))
]
COIN_DROP_TABLE = {
"basic": 0.33,
"fast": 2.0,
"tank": 4.0,
"ranged": 2.0,
"protector": 3.0,
}
assert sorted(COIN_DROP_TABLE.keys()) == sorted(SPAWN_CHANCE_TABLE.keys())
COIN_DROP_TABLE["scatter"] = 4.0
COIN_DROP_TABLE["vampire"] = 4.0
COIN_DROP_TABLE["ray"] = 4.0
COIN_DROP_TABLE["boss"] = 0.0
COIN_DROP_TABLE["saboteur"] = 0.0
COIN_DROP_TABLE["commander"] = 0.0
COIN_DROP_TABLE["overcharge"] = 0.0
ELITE_SPAWN_CHANCE_TABLE = [0.00, 0.01, 0.04, 0.09, 0.15, 0.24, 0.36, 0.48, 0.63, 0.81, 1.00]
ELITE_SINGLE_SPAWN_WAVES_TABLE = [
[0, 500, 1000, 1500, 2000, 3000, 4000, 5000, 6000, 7000, 8000],
[0, 450, 900, 1350, 1800, 2700, 3600, 4500, 5400, 6300, 7200],
[0, 405, 810, 1215, 1620, 2430, 3240, 4050, 4860, 5670, 6480],
[0, 365, 729, 1094, 1458, 2187, 2916, 3645, 4374, 5103, 5832],
[0, 328, 656, 984, 1312, 1968, 2624, 3281, 3937, 4593, 5249],
[0, 295, 590, 886, 1181, 1771, 2362, 2952, 3543, 4133, 4724],
[0, 266, 531, 797, 1063, 1594, 2126, 2657, 3189, 3720, 4252],
[0, 239, 478, 717, 957, 1435, 1913, 2391, 2870, 3348, 3826],
[0, 215, 430, 646, 861, 1291, 1722, 2152, 2583, 3013, 3444],
[0, 194, 387, 581, 775, 1162, 1550, 1937, 2325, 2712, 3099],
[0, 174, 349, 523, 697, 1046, 1395, 1743, 2092, 2441, 2789],
[0, 157, 314, 471, 628, 941, 1255, 1569, 1883, 2197, 2510],
[0, 141, 282, 424, 565, 847, 1130, 1412, 1695, 1977, 2259],
[0, 127, 254, 381, 508, 763, 1017, 1271, 1525, 1779, 2033],
[0, 114, 229, 343, 458, 686, 915, 1144, 1373, 1601, 1830],
[0, 41, 102, 205, 308, 411, 617, 823, 1029, 1235, 1441],
[0, 37, 92, 185, 277, 370, 555, 741, 926, 1111, 1297],
[0, 33, 83, 166, 250, 333, 500, 667, 833, 1000, 1167],
]
assert all(len(row) == len(ELITE_SPAWN_CHANCE_TABLE) for row in ELITE_SINGLE_SPAWN_WAVES_TABLE)
ELITE_DOUBLE_SPAWN_WAVES_TABLE = [
[0, 8000, 9000, 10000, 11000, 12000, 13000, 14000, 15000, 16000, 17000],
[0, 7200, 8100, 9000, 9900, 10800, 11700, 12600, 13500, 14400, 15300],
[0, 6480, 7290, 8100, 8910, 9720, 10530, 11340, 12150, 12960, 13770],
[0, 5832, 6561, 7290, 8019, 8748, 9477, 10206, 10935, 11664, 12393],
[0, 5249, 5905, 6561, 7217, 7873, 8529, 9185, 9841, 10497, 11153],
[0, 4724, 5314, 5905, 6495, 7086, 7676, 8267, 8857, 9448, 10038],
[0, 4252, 4783, 5314, 5846, 6377, 6909, 7440, 7972, 8503, 9034],
[0, 3826, 4305, 4783, 5261, 5740, 6218, 6696, 7174, 7653, 8131],
[0, 3444, 3874, 4305, 4735, 5166, 5596, 6027, 6457, 6887, 7318],
[0, 3099, 3487, 3874, 4262, 4649, 5036, 5424, 5811, 6199, 6586],
[0, 2789, 3138, 3487, 3835, 4184, 4533, 4881, 5230, 5579, 5928],
[0, 2510, 2824, 3138, 3452, 3766, 4080, 4394, 4708, 5022, 5336],
[0, 2259, 2542, 2824, 3107, 3389, 3672, 3954, 4236, 4519, 4801],
[0, 2033, 2288, 2542, 2796, 3050, 3304, 3559, 3813, 4067, 4321],
[0, 1830, 2059, 2288, 2516, 2745, 2974, 3203, 3432, 3660, 3889],
[0, 1441, 1647, 1853, 2058, 2264, 2470, 2676, 2882, 3088, 3294],
[0, 1297, 1482, 1667, 1853, 2038, 2223, 2408, 2594, 2779, 2964],
[0, 1167, 1334, 1500, 1667, 1834, 2001, 2168, 2334, 2501, 2668],
]
assert all(len(row) == len(ELITE_SPAWN_CHANCE_TABLE) for row in ELITE_DOUBLE_SPAWN_WAVES_TABLE)
assert len(ELITE_SINGLE_SPAWN_WAVES_TABLE) == len(ELITE_DOUBLE_SPAWN_WAVES_TABLE)
FLEET_MIN_WAVE_SPAWN_TABLE = [*([None] * 13), 2495, 1495, 995, 495, 95, 45, 5, 5]
FLEET_SPAWN_PERIOD_WAVE_TABLE = [*([None] * 13), 1000, 750, 500, 250, 100, 50, 10, 10]
FLEET_SPAWN_COUNT_TABLE = [*([0] * 13), 1, 1, 1, 1, 1, 1, 1, 2]
FLEET_REROLL_SHARD_DROP_TABLE = [*([0] * 13), 1080, 1200, 1350, 1500, 1650, 1800, 1950, 2100]
assert len(FLEET_MIN_WAVE_SPAWN_TABLE) == len(FLEET_SPAWN_PERIOD_WAVE_TABLE)
assert len(FLEET_MIN_WAVE_SPAWN_TABLE) == len(FLEET_SPAWN_COUNT_TABLE)
assert len(FLEET_MIN_WAVE_SPAWN_TABLE) == len(FLEET_REROLL_SHARD_DROP_TABLE)
FLEET_MODULE_SHARD_DROP_TABLE = [
(1, 5), (250, 6), (500, 7), (750, 8),
(1000, 9), (1250, 10), (1500, 11), (1750, 12),
(2000, 13), (2250, 14), (2500, 15), (2750, 16),
(3000, 17), (3250, 18), (3500, 19), (3750, 20),
(4000, 21), (4250, 22), (4500, 23), (4750, 24),
(5000, 25),
]
FLEET_REROLL_SHARD_DROP_CHANCE = 0.8
FLEET_MODULE_SHARD_DROP_CHANCE = 0.2
BOSS_REROLL_SHARD_DROP_CHANCE = 0.15
BOSS_COMMON_MODULE_DROP_CHANCE = 0.03
COMMON_MODULE_VALUE = 10
RARE_MODULE_DROP_CHANCE = 0.015
RARE_MODULE_VALUE = 30
RECOVERY_PACKAGE_CHANCE = 82 # %
# Perk data
STANDARD_PERK_CHANCE = 0.65
ULTIMATE_PERK_CHANCE = 0.20
TRADEOFF_PERK_CHANCE = 0.15
assert STANDARD_PERK_CHANCE + ULTIMATE_PERK_CHANCE + TRADEOFF_PERK_CHANCE == 1.0
# Quantity of each standard perk
STANDARD_PERKS = {
"std-health": 5,
"std-damage": 5,
"std-coin-bonus": 5,
"std-defabs": 5,
"std-cash-bonus": 5,
"std-regen": 5,
"std-interest": 5,
"std-lm-damage": 5,
"std-freeup-chance": 5,
"std-def%": 5,
"std-bounce-shot": 3,
"std-pwr": 3,
"std-orbs": 2,
"std-random-uw": 1,
"std-game-speed": 1,
}
# Quantity of each UW perk
ULTIMATE_PERKS = {
"uw-sm": 1,
"uw-ps": 1,
"uw-dw": 1,
"uw-ilm": 1,
"uw-gt": 1,
"uw-cl": 1,
"uw-cf": 1,
"uw-bh": 1,
"uw-sl": 1,
}
# Quantity of each TO perk
TRADEOFF_PERKS = {
"to-tower-damage": 1,
"to-coin": 1,
"to-enemy-health": 1,
"to-enemy-damage": 1,
"to-enemy-range": 1,
"to-enemy-speed": 1,
"to-cash": 1,
"to-regen": 1,
"to-boss-health": 1,
"to-lifesteal": 1,
}
ALL_PERKS = {**STANDARD_PERKS, **ULTIMATE_PERKS, **TRADEOFF_PERKS}
# Bonuses for each perk
PERK_BONUSES = {
"std-coin-bonus": 1.15,
"std-pwr": -0.2,
"std-game-speed": 1.0,
"std-freeup-chance": 0.05,
"uw-gt": 1.5,
"to-coin": 1.8,
}
PERK_PRIORITY_ORDER = [
"std-pwr",
"std-game-speed",
"to-coin",
"uw-gt",
"std-coin-bonus",
"std-freeup-chance",
]
FIRST_PERK_CHOICE = "std-pwr"
PERK_BANS = [
"to-tower-damage",
"to-enemy-health",
"to-enemy-range",
"to-enemy-speed",
"to-cash",
"to-boss-health",
"std-defabs",
"std-interest",
]
# Card data
WAVE_SKIP_CHANCE = 0.19
WAVE_SKIP_BONUS = 1.10
# Mastery data
CASH_MASTERY_TABLE = [0.004, 0.008, 0.012, 0.016, 0.020, 0.024, 0.028, 0.032, 0.036, 0.040]
COIN_MASTERY_TABLE = [1.03, 1.06, 1.09, 1.12, 1.15, 1.18, 1.21, 1.24, 1.27, 1.30]
CRITICAL_COIN_MASTERY_TABLE = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
ENEMY_BALANCE_MASTERY_TABLE = [0.06, 0.12, 0.18, 0.24, 0.30, 0.36, 0.42, 0.48, 0.54, 0.60]
EXTRA_ORB_MASTERY_TABLE = [1.04, 1.08, 1.12, 1.16, 1.20, 1.24, 1.28, 1.32, 1.36, 1.40]
INTRO_SPRINT_MASTERY_TABLE = [180, 360, 540, 720, 900, 1080, 1260, 1440, 1620, 1800]
RECOVERY_PACKAGE_CHANCE_MASTERY_TABLE = [0.004, 0.008, 0.012, 0.016, 0.020, 0.024, 0.028, 0.032, 0.036, 0.040]
WAVE_ACCELERATOR_MASTERY_TABLE = [
[1, 3, 5, 18, 36, 55, 73, 91, 136, 182, 227, 273, 364, 545, 727, 909, 1364, 1818, 2273, 2727, 3182, 3636, 4091, 4545, 5000, 5455, 5909],
[1, 3, 5, 17, 33, 50, 67, 83, 125, 167, 208, 250, 333, 500, 667, 833, 1250, 1667, 2083, 2500, 2917, 3333, 3750, 4167, 4583, 5000, 5417],
[1, 2, 5, 15, 31, 46, 62, 77, 115, 154, 192, 231, 308, 462, 615, 769, 1154, 1538, 1923, 2308, 2692, 3077, 3462, 3846, 4231, 4615, 5000],
[1, 2, 4, 14, 29, 43, 57, 71, 107, 143, 179, 214, 286, 429, 571, 714, 1071, 1429, 1786, 2143, 2500, 2857, 3214, 3571, 3929, 4286, 4643],
[1, 2, 4, 13, 27, 40, 53, 67, 100, 133, 167, 200, 267, 400, 533, 667, 1000, 1333, 1667, 2000, 2333, 2667, 3000, 3333, 3667, 4000, 4333],
[1, 2, 4, 13, 25, 38, 50, 63, 94, 125, 156, 188, 250, 375, 500, 625, 938, 1250, 1563, 1875, 2188, 2500, 2813, 3125, 3438, 3750, 4063],
[1, 2, 4, 12, 24, 35, 47, 59, 88, 118, 147, 176, 235, 353, 471, 588, 882, 1176, 1471, 1765, 2059, 2353, 2647, 2941, 3235, 3529, 3824],
[1, 2, 3, 11, 22, 33, 44, 56, 83, 111, 139, 167, 222, 333, 444, 556, 833, 1111, 1389, 1667, 1944, 2222, 2500, 2778, 3056, 3333, 3611],
[1, 2, 3, 11, 21, 32, 42, 53, 79, 105, 132, 158, 211, 316, 421, 526, 789, 1053, 1316, 1579, 1842, 2105, 2368, 2632, 2895, 3158, 3421],
[1, 2, 3, 10, 20, 30, 40, 50, 75, 100, 125, 150, 200, 300, 400, 500, 750, 1000, 1250, 1500, 1750, 2000, 2250, 2500, 2750, 3000, 3250]
]
assert all(len(row) == len(SPAWN_RATE_SEQUENCE) for row in WAVE_ACCELERATOR_MASTERY_TABLE)
WAVE_SKIP_MASTERY_TABLE = [0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55]
MASTERY_LEVELS = [None, *range(0, 10)]
MASTERY_LEVEL_NAMES = ["locked"] + [str(x) for x in range(0, 10)]
MASTERY_DISPLAY_NAMES = {
"cash": "Cash",
"coin": "Coin",
"critical-coin": "CritCoin",
"enemy-balance": "EB",
"extra-orb": "EO",
"intro-sprint": "IS",
"recovery-package": "RPC",
"wave-accelerator": "WA",
"wave-skip": "WS",
}
MASTERY_STONE_COSTS = {
"cash": 500,
"coin": 1250,
"critical-coin": 1000,
"enemy-balance": 1000,
"extra-orb": 750,
"intro-sprint": 1250,
"recovery-package": 1000,
"wave-accelerator": 1000,
"wave-skip": 1000,
}
REWARD_NAMES = ["coins", "cells", "rerolls", "modules"]
# fmt: on
# Data types
@dataclasses.dataclass
class Simulation:
name: str = ""
mastery: str | None = None
level: int | None = None
skip: bool = False
# Estimates / inputs
tier: int = 1
max_waves: int = 0
orb_hits: float = 1.0
reward: str = "coins"
sum_total_stone_cost: bool = False
bhd_bonus: float = 0
golden_combo: float = 0
# Workshop stats
free_upgrade_chances: dict[str, float] = dataclasses.field(default_factory=dict)
package_chance: float = RECOVERY_PACKAGE_CHANCE / 100
enemy_level_skip_chances: dict[str, float] = dataclasses.field(default_factory=dict)
# Lab levels
standard_perk_bonus_lab: int = 25
improved_tradeoff_perk_lab: int = 10
perk_option_quantity_lab: int = 2
perk_waves_required_lab: int = 25
first_perk_choice: str = FIRST_PERK_CHOICE
perk_priority_order: list[str] = dataclasses.field(default_factory=lambda: PERK_PRIORITY_ORDER)
perk_bans: list[str] = dataclasses.field(default_factory=lambda: PERK_BANS)
# Mastery levels
cash: int | None = None
coin: int | None = None
critical_coin: int | None = None
enemy_balance: int | None = None
extra_orb: int | None = None
intro_sprint: int | None = None
recovery_package: int | None = None
wave_accelerator: int | None = None
wave_skip: int | None = None
def stone_cost(self) -> int:
if not self.sum_total_stone_cost:
return 0 if self.mastery is None else MASTERY_STONE_COSTS[self.mastery]
stone_cost = 0
if self.cash is not None:
stone_cost += MASTERY_STONE_COSTS["cash"]
if self.coin is not None:
stone_cost += MASTERY_STONE_COSTS["coin"]
if self.critical_coin is not None:
stone_cost += MASTERY_STONE_COSTS["critical-coin"]
if self.enemy_balance is not None:
stone_cost += MASTERY_STONE_COSTS["enemy-balance"]
if self.extra_orb is not None:
stone_cost += MASTERY_STONE_COSTS["extra-orb"]
if self.intro_sprint is not None:
stone_cost += MASTERY_STONE_COSTS["intro-sprint"]
if self.recovery_package is not None:
stone_cost += MASTERY_STONE_COSTS["recovery-package"]
if self.wave_accelerator is not None:
stone_cost += MASTERY_STONE_COSTS["wave-accelerator"]
if self.wave_skip is not None:
stone_cost += MASTERY_STONE_COSTS["wave-skip"]
return stone_cost
def standard_perk_bonus(self) -> float:
return 1 + self.standard_perk_bonus_lab / 100
def tradeoff_perk_bonus(self) -> float:
return 1 + self.improved_tradeoff_perk_lab / 100
def perk_option_quantity(self) -> int:
return 2 + self.perk_option_quantity_lab
def perk_waves_required(self, base: int) -> int:
return base - self.perk_waves_required_lab
def perks_default_factory() -> dict[str, float]:
return {
name: 0.0 for name in ALL_PERKS.keys()
}
@dataclasses.dataclass
class Perks:
perks: dict[str, float] = dataclasses.field(default_factory=perks_default_factory)
def pwr_bonus(self, sim: Simulation) -> float:
return 1.0 + self.perks.get("std-pwr", 0) * PERK_BONUSES["std-pwr"] * sim.standard_perk_bonus()
def game_speed_factor(self, sim: Simulation) -> float:
return (GAME_SPEED + self.perks.get("std-game-speed", 0) * PERK_BONUSES["std-game-speed"] * sim.standard_perk_bonus()) / GAME_SPEED
def coin_bonus(self, sim: Simulation) -> float:
bonus = 1.0
bonus *= self.perks.get("std-coin-bonus", 0) * PERK_BONUSES["std-coin-bonus"] * sim.standard_perk_bonus()
bonus *= self.perks.get("uw-gt", 0) * PERK_BONUSES["uw-gt"]
bonus *= self.perks.get("to-coin", 0) * PERK_BONUSES["to-coin"] * sim.tradeoff_perk_bonus()
return 1.0 + bonus
def free_upgrade_chance_bonus(self, sim: Simulation) -> float:
return self.perks.get("std-freeup-chance", 0) * PERK_BONUSES["std-freeup-chance"] * sim.standard_perk_bonus()
def free_upgrades_default_factory() -> dict[str, float]:
return {
"attack": 0.0,
"defense": 0.0,
"utility": 0.0,
}
def enemy_level_skip_default_factory() -> dict[str, float]:
return {
"attack": 0.0,
"health": 0.0,
}
def enemies_default_factory() -> dict[str, float]:
return {name: 0.0 for name in COIN_DROP_TABLE.keys()}
@dataclasses.dataclass
class Events:
wave: int = 0
wave_skip: float = 0.0
free_upgrades: dict[str, float] = dataclasses.field(
default_factory=free_upgrades_default_factory
)
recovery_packages: float = 0.0
enemy_level_skips: dict[str, float] = dataclasses.field(
default_factory=enemy_level_skip_default_factory
)
enemies: dict[str, float] = dataclasses.field(
default_factory=enemies_default_factory
)
def elite_enemy_count(self) -> float:
return self.enemies["scatter"] + self.enemies["vampire"] + self.enemies["ray"]
def fleet_enemy_count(self) -> float:
return self.enemies["saboteur"] + self.enemies["commander"] + self.enemies["overcharge"]
def scatter_children_count(self) -> float:
# Each scatter splits in half 4 times.
return self.enemies["scatter"] * sum(1 << i for i in range(1, 5))
def total_enemy_count(self) -> float:
return sum(self.enemies.values()) + self.scatter_children_count()
def __iadd__(self, other: Self) -> Self:
self.wave_skip += other.wave_skip
for key, value in other.free_upgrades.items():
self.free_upgrades[key] += value
self.recovery_packages += other.recovery_packages
for key, value in other.enemy_level_skips.items():
self.enemy_level_skips[key] += value
for key, value in other.enemies.items():
self.enemies[key] += value
return self
def __add__(self, other: Self) -> Self:
result = copy.deepcopy(self)
result += other
return result
@dataclasses.dataclass
class Rewards:
coins: float = 0.0
elite_cells: float = 0.0
reroll_shards: float = 0.0
module_shards: float = 0.0
def __iadd__(self, other: Self) -> Self:
self.coins += other.coins
self.elite_cells += other.elite_cells
self.reroll_shards += other.reroll_shards
self.module_shards += other.module_shards
return self
def __add__(self, other: Self) -> Self:
result = copy.deepcopy(self)
result += other
return result
def __isub__(self, other: Self) -> Self:
self.coins -= other.coins
self.elite_cells -= other.elite_cells
self.reroll_shards -= other.reroll_shards
self.module_shards -= other.module_shards
return self
def __sub__(self, other: Self) -> Self:
result = copy.deepcopy(self)
result -= other
return result
def __imul__(self, factor: float | int) -> Self:
self.coins *= factor
self.elite_cells *= factor
self.reroll_shards *= factor
self.module_shards *= factor
return self
def __mul__(self, factor: float | int) -> Self:
result = copy.deepcopy(self)
result *= factor
return result
@dataclasses.dataclass
class SimulationWaveResult:
wave: int
elapsed_time: float
cumulative_events: Events
cumulative_rewards: Rewards
@dataclasses.dataclass
class SimulationRunResult:
wave_results: list[SimulationWaveResult]
total: float | None = None
relative: float | None = None
roi: float | None = None
@dataclasses.dataclass
class PlotLine:
name: str
mastery: str | None = None
xs: list[float] = dataclasses.field(default_factory=list)
ys: list[float] = dataclasses.field(default_factory=list)
relative: float | None = None
roi: float | None = None
@dataclasses.dataclass
class Plot:
title: str
xlabel: str
ylabel: str
top: float | None = None
bottom: float | None = None
lines: list[PlotLine | None] = dataclasses.field(default_factory=list)
# Argument handling
def tier_and_wave_arg(arg: str) -> tuple[int, int]:
tier, _, wave = arg.partition(":")
return int(tier), int(wave)
def add_common_args(parser: argparse.ArgumentParser):
# Simulation events
parser.add_argument(
"--tier",
type=int,
choices=TIERS,
default=1,
help="Tier to simulate",
)
parser.add_argument(
"--orb-hits",
type=float,
default=1.0,
help="Average portion of enemies hit by orbs [0.0-1.0]",
)
parser.add_argument(
"--freeup-chance",
nargs=3,
type=int,
default=[75, 75, 75],
help="Free-upgrade chances (attack %%, defense %%, utility %%)",
)
parser.add_argument(
"--package-chance",
type=int,
default=RECOVERY_PACKAGE_CHANCE,
help="Free-upgrade chances (attack %%, defense %%, utility %%)",
)
parser.add_argument(
"--bhd",
choices=[0, 3, 5, 7, 10],
type=int,
default=0,
help="BHD free-upgrade coin multiplier (%%)",
)
parser.add_argument(
"--golden-combo",
type=float,
default=0,
help="Golden Combo multiplier (%%)",
)
# Reward normalization
parser.add_argument(
"--reward",
type=str,
default="coins",
choices=REWARD_NAMES,
help="Which reward to plot and compare",
)
parser.add_argument(
"--difference",
"-d",
action="store_true",
default=False,
help="Subtract the baseline configuration from all results",
)
parser.add_argument(
"--elapsed",
action="store_true",
default=False,
help="Normalize results by elapsed time",
)
parser.add_argument(
"--relative",
"-r",
action="store_true",
default=False,
help="Normalize all results against the baseline configuration",
)
parser.add_argument(
"--roi",
action="store_true",
help="Normalize all results against mastery stone cost",
)
parser.add_argument(
"--sum-total-stone-cost",
action="store_true",
default=False,
help="Consider the stone cost of all masteries when normalizing results",
)
# Output options
parser.add_argument(
"--truncate",
action="store_true",
default=False,
help="Truncate runs to the end of the shortest simulation",
)
parser.add_argument(
"--crop",
action="store_true",
default=False,
help="Crop results of the plot vertically",
)
parser.add_argument(
"--no-print",
action="store_false",
default=True,
dest="print",
help="Do not print results",
)
parser.add_argument(
"--no-plot",
action="store_false",
default=True,
dest="plot",
help="Do not plot results",
)
parser.add_argument("--output", "-o", default=None, help="Filename for saved plot")
# Masteries
parser.add_argument(
"--cash",
choices=MASTERY_LEVEL_NAMES,
default=None,
help="Cash mastery level",
)
parser.add_argument(
"--coin",
choices=MASTERY_LEVEL_NAMES,
default=None,
help="Coin mastery level",
)
parser.add_argument(
"--critical-coin",
choices=MASTERY_LEVEL_NAMES,
default=None,
help="Critical coin mastery level",
)
parser.add_argument(
"--enemy-balance",
choices=MASTERY_LEVEL_NAMES,
default=None,
help="Enemy balance mastery level",
)
parser.add_argument(
"--extra-orb",
choices=MASTERY_LEVEL_NAMES,
default=None,
help="Extra orb mastery level",
)
parser.add_argument(
"--recovery-package",
choices=MASTERY_LEVEL_NAMES,
default=None,
help="Recovery package mastery level",
)
parser.add_argument(
"--intro-sprint",
choices=MASTERY_LEVEL_NAMES,
default=None,
help="Intro sprint mastery level",
)
parser.add_argument(
"--wave-accelerator",
choices=MASTERY_LEVEL_NAMES,
default=None,
help="Wave accelerator mastery level",
)
parser.add_argument(
"--wave-skip",
choices=MASTERY_LEVEL_NAMES,
default=None,
help="Wave skip mastery level",
)
parser.add_argument(
"--rerolls-with-cash",
choices=MASTERY_LEVEL_NAMES,
default=None,
help="The sim changing reroll-affecting masteries (EB#, IS#, WS#) should set cash to this level",
)
def mastery_level(name: str | None) -> int | None:
if name is None or name == "locked":
return None
if int(name) in range(0, 10):
return int(name)
raise ValueError(f"Invalid mastery level: {name}")
def convert_mastery_args(args: argparse.Namespace) -> None:
args.cash = mastery_level(args.cash)
args.coin = mastery_level(args.coin)
args.critical_coin = mastery_level(args.critical_coin)
args.enemy_balance = mastery_level(args.enemy_balance)
args.extra_orb = mastery_level(args.extra_orb)
args.intro_sprint = mastery_level(args.intro_sprint)
args.recovery_package = mastery_level(args.recovery_package)
args.wave_accelerator = mastery_level(args.wave_accelerator)
args.wave_skip = mastery_level(args.wave_skip)
args.rerolls_with_cash = mastery_level(args.rerolls_with_cash)
def common_args_description(args: argparse.Namespace, baseline_name: str) -> list[str]:
desc = []
# Simulation events
if args.tier is not None:
assert args.wave is not None
desc.append(f"T{args.tier}W{args.wave}")
if args.orb_hits != 1.0:
desc.append(f"orbs {args.orb_hits:.2%}")
if args.bhd > 0:
desc.append(f"bhd {args.bhd}%")
if args.golden_combo > 0:
desc.append(f"GT+ {args.golden_combo}%")
# Reward normalization
desc.append(args.reward)
if args.elapsed:
desc.append("per hour")
if args.difference:
desc.append(f"minus {baseline_name}")
if args.relative:
desc.append(f"over {baseline_name}")
if args.roi:
desc.append("per stone")
# Output options
if args.truncate:
desc.append("truncated")
if args.crop:
desc.append("cropped")
# Masteries
if args.cash is not None:
desc.append(f"{MASTERY_DISPLAY_NAMES['cash']}#{args.cash}")
if args.coin is not None:
desc.append(f"{MASTERY_DISPLAY_NAMES['coin']}#{args.coin}")
if args.critical_coin is not None:
desc.append(f"{MASTERY_DISPLAY_NAMES['critical-coin']}#{args.critical_coin}")
if args.enemy_balance is not None:
desc.append(f"{MASTERY_DISPLAY_NAMES['enemy-balance']}#{args.enemy_balance}")
if args.extra_orb is not None:
desc.append(f"{MASTERY_DISPLAY_NAMES['extra-orb']}#{args.extra_orb}")
if args.intro_sprint is not None:
desc.append(f"{MASTERY_DISPLAY_NAMES['intro-sprint']}#{args.intro_sprint}")
if args.recovery_package is not None:
desc.append(
f"{MASTERY_DISPLAY_NAMES['recovery-package']}#{args.recovery_package}"
)
if args.wave_accelerator is not None:
desc.append(
f"{MASTERY_DISPLAY_NAMES['wave-accelerator']}#{args.wave_accelerator}"
)
if args.wave_skip is not None:
desc.append(f"{MASTERY_DISPLAY_NAMES['wave-skip']}#{args.wave_skip}")
if args.rerolls_with_cash is not None:
desc.append(
f"rerolls with {MASTERY_DISPLAY_NAMES['cash']}#{args.rerolls_with_cash}"
)
return desc
# Simulation logic
def max_intro_wave(sim: Simulation) -> int:
return (
100
if sim.intro_sprint is None
else INTRO_SPRINT_MASTERY_TABLE[sim.intro_sprint]
)
def perk_count_at_wave(
sim: Simulation,
perks: Perks,
wave: int,
) -> float:
pwr_bonus = perks.pwr_bonus(sim)
pwr_waves = [
(20, sim.perk_waves_required(200) * pwr_bonus),
(20, sim.perk_waves_required(250) * pwr_bonus),
(10, sim.perk_waves_required(300) * pwr_bonus),
]
last_wave = 0
perk_count = 0
for perk_quantity, waves_per_perk in pwr_waves:
max_wave = last_wave + perk_quantity * waves_per_perk
if wave < max_wave:
return perk_count + ((wave - last_wave) / waves_per_perk)
last_wave = max_wave
perk_count += perk_quantity
return perk_count
def perks_confidence_default_factory() -> dict[str, list[float]]:
return {
perk: [0.0] * qty
for perk, qty in ALL_PERKS.items()
}
@dataclasses.dataclass
class PerksConfidence:
"""
Confidence that each perk has been selected a given number of times.
Factors in first-perk, bans, and priority.
"""
count: int = 0
# For each perk, the confidence that it has been selected 1..n times.
# n is the maximum quantity of the perk.
perks: dict[str, list[float]] = dataclasses.field(default_factory=perks_confidence_default_factory)
def __iadd__(self, other: Self) -> Self:
for perk in self.perks.keys():
for i in range(0, len(self.perks[perk])):
self.perks[perk][i] += other.perks[perk][i]
return self
def __add__(self, other: Self) -> Self:
result = copy.deepcopy(self)
result += other
return result
def __imul__(self, factor: float | int) -> Self:
for perk in self.perks.keys():
for i in range(0, len(self.perks[perk])):
self.perks[perk][i] *= factor
return self
def __mul__(self, factor: float | int) -> Self:
result = copy.deepcopy(self)
result *= factor
return result
def reduce(self) -> Perks:
return Perks(perks={perk: sum(confidences) for perk, confidences in self.perks.items()})
def perk_options_default_factory() -> dict[str, float]:
return {
perk: 0.0
for perk in ALL_PERKS.keys()
}
@dataclasses.dataclass
class PerkOptions:
options: dict[str, float] = dataclasses.field(default_factory=perk_options_default_factory)
def __iadd__(self, other: Self) -> Self:
for perk in self.options.keys():
self.options[perk] += other.options[perk]
return self
def __add__(self, other: Self) -> Self:
result = copy.deepcopy(self)
result += other
return result
def __imul__(self, factor: float | int) -> Self:
for perk in self.options.keys():
self.options[perk] *= factor
return self
def __mul__(self, factor: float | int) -> Self:
result = copy.deepcopy(self)
result *= factor
return result
def inorm(self) -> Self:
magnitude = sum(self.options.values())
if magnitude:
for perk in self.options.keys():
self.options[perk] /= magnitude
return self
def norm(self) -> Self:
result = copy.deepcopy(self)
result.inorm()
return result
@dataclasses.dataclass
class PerkWaveEstimator:
confidences: list[PerksConfidence]
def lower(self, perk_count: float) -> PerksConfidence:
if perk_count < 1.0:
return PerksConfidence()
return self.confidences[math.floor(perk_count - 1.0)]
def higher(self, perk_count: float) -> PerksConfidence:
if perk_count >= len(self.confidences):
return self.confidences[-1]
return self.confidences[math.floor(perk_count)]
def average(self, perk_count: float) -> PerksConfidence:
lower_confidence = self.lower(perk_count)
upper_confidence = self.higher(perk_count)
lambda_ = perk_count % 1.0
return lower_confidence + (upper_confidence + lower_confidence * -1.0) * lambda_
def estimate(self, sim: Simulation, wave: int, perks: Perks) -> Perks:
perk_count = sum(perks.perks.values())
next_perk_count = perk_count_at_wave(sim, perks, wave)
while perk_count < next_perk_count:
perk_count = next_perk_count
perks = self.average(perk_count).reduce()
next_perk_count = perk_count_at_wave(sim, perks, wave)
return perks
def perk_category_option_chances(sim: Simulation, options: PerkOptions, confidence: PerksConfidence, category: dict[str, int], factor: float) -> PerkOptions:
next_options = PerkOptions()
for perk in category.keys():
if perk not in sim.perk_bans:
# Perks can't appear if they are already in the option set, or if their
# quantity has been fully exhausted.
chance = (1.0 - options.options[perk])
chance *= (1.0 - confidence.perks[perk][-1])
next_options.options[perk] = chance
return next_options.inorm() * factor
def perk_option_chances(sim: Simulation, options: PerkOptions, confidence: PerksConfidence) -> PerkOptions:
sperks = perk_category_option_chances(sim, options, confidence, STANDARD_PERKS, STANDARD_PERK_CHANCE)
uperks = perk_category_option_chances(sim, options, confidence, ULTIMATE_PERKS, ULTIMATE_PERK_CHANCE)
tperks = perk_category_option_chances(sim, options, confidence, TRADEOFF_PERKS, TRADEOFF_PERK_CHANCE)
return (sperks + uperks + tperks).inorm()
def perk_option_set_chances(sim: Simulation, confidence: PerksConfidence) -> PerkOptions:
options = PerkOptions()
options_count = sim.perk_option_quantity()
if confidence.count == 0:
options.options[sim.first_perk_choice] = 1.0
options_count -= 1
for _ in range(0, options_count):
options += perk_option_chances(sim, options, confidence)
return options
def active_perks_confidence(sim: Simulation, confidence: PerksConfidence) -> PerksConfidence:
options = perk_option_set_chances(sim, confidence)
lower_priority = set(ALL_PERKS.keys())
for high_perk in sim.perk_priority_order:
lower_priority.remove(high_perk)
for lower_perk in lower_priority:
options.options[lower_perk] *= (1.0 - options.options[high_perk])
options.inorm()
next_confidence = copy.deepcopy(confidence)
next_confidence.count += 1
for perk, probseq in next_confidence.perks.items():
for i in range(0, min(len(probseq), next_confidence.count)):