forked from ANRAR4/AutoBTD6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplay.py
More file actions
1272 lines (1120 loc) · 69.3 KB
/
replay.py
File metadata and controls
1272 lines (1120 loc) · 69.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
from helper import *
from ocr import custom_ocr
smallActionDelay = 0.05
actionDelay = 0.2
menuChangeDelay = 1
def getResolutionDependentData(resolution = pyautogui.size(), gamemode=''):
nativeResolution = (2560, 1440)
requiredComparisonImages = [{'category': 'screens', 'name': 'startmenu'}, {'category': 'screens', 'name': 'map_selection'}, {'category': 'screens', 'name': 'difficulty_selection'}, {'category': 'screens', 'name': 'gamemode_selection'}, {'category': 'screens', 'name': 'hero_selection'}, {'category': 'screens', 'name': 'ingame'}, {'category': 'screens', 'name': 'ingame_paused'}, {'category': 'screens', 'name': 'victory_summary'}, {'category': 'screens', 'name': 'victory'}, {'category': 'screens', 'name': 'defeat'}, {'category': 'screens', 'name': 'overwrite_save'}, {'category': 'screens', 'name': 'levelup'}, {'category': 'screens', 'name': 'apopalypse_hint'}, {'category': 'screens', 'name': 'round_100_insta'}, {'category': 'game_state', 'name': 'game_paused'}, {'category': 'game_state', 'name': 'game_playing_slow'}, {'category': 'game_state', 'name': 'game_playing_fast'}]
optionalComparisonImages = [{'category': 'screens', 'name': 'collection_claim_chest', 'for': [Mode.CHASE_REWARDS.name]}]
requiredLocateImages = [{'name': 'remove_obstacle_confirm_button'}, {'name': 'button_home'}]
optionalLocateImages = [{'name': 'unknown_insta', 'for': [Mode.CHASE_REWARDS.name]}, {'name': 'unknown_insta_mask', 'for': [Mode.CHASE_REWARDS.name]}]
rawSegmentCoordinates = {
"2560x1440": {
# pre v35.0
# 'lives': (158, 32, 330, 84),
# 'mana_lives': (60, 74, 173, 142),
# 'money': (459, 31, 959, 80),
# 'round': (1847, 39, 2084, 95),
'lives': (187, 32, 350, 84),
'mana_lives': (89, 78, 185, 132),
'money': (486, 25, 959, 90),
'round': (1912, 39, 2080, 95),
}
}
if gamemode == 'impoppable' or gamemode == 'chimps':
rawSegmentCoordinates['2560x1440']['round'] = (1880, 39, 2080, 95)
if getResolutionString(resolution) in rawSegmentCoordinates:
segmentCoordinates = rawSegmentCoordinates[getResolutionString(resolution)]
else:
segmentCoordinates = {}
for key in rawSegmentCoordinates[getResolutionString(nativeResolution)]:
segmentCoordinates[key] = [round(x * resolution[0] / nativeResolution[0]) if i % 2 == 0 else round(x * resolution[1] / nativeResolution[1]) for i, x in enumerate(rawSegmentCoordinates[getResolutionString(nativeResolution)][key])]
imagesDir = 'images/' + getResolutionString(resolution) + '/'
comparisonImages = {}
locateImages = {}
if not exists(imagesDir):
return None
supportedModes = dict.fromkeys([e.name for e in Mode], True)
for img in requiredComparisonImages:
filename = (img['filename'] if 'filename' in img else img['name']) + '.png'
if not exists(imagesDir + filename):
print(filename + ' missing!')
return None
elif 'category' in img:
if not img['category'] in comparisonImages:
comparisonImages[img['category']] = {}
comparisonImages[img['category']][img['name']] = cv2.imread(imagesDir + filename)
else:
comparisonImages[img['name']] = cv2.imread(imagesDir + filename)
for img in optionalComparisonImages:
filename = (img['filename'] if 'filename' in img else img['name']) + '.png'
if not exists(imagesDir + filename):
if 'for' in img:
for mode in img['for']:
supportedModes.pop(mode, None)
elif 'category' in img:
if not img['category'] in comparisonImages:
comparisonImages[img['category']] = {}
comparisonImages[img['category']][img['name']] = cv2.imread(imagesDir + filename)
else:
comparisonImages[img['name']] = cv2.imread(imagesDir + filename)
for img in requiredLocateImages:
filename = (img['filename'] if 'filename' in img else img['name']) + '.png'
if not exists(imagesDir + filename):
print(filename + ' missing!')
return None
elif 'category' in img:
if not img['category'] in locateImages:
locateImages[img['category']] = {}
locateImages[img['category']][img['name']] = cv2.imread(imagesDir + filename)
else:
locateImages[img['name']] = cv2.imread(imagesDir + filename)
for img in optionalLocateImages:
filename = (img['filename'] if 'filename' in img else img['name']) + '.png'
if not exists(imagesDir + filename):
if 'for' in img:
for mode in img['for']:
supportedModes.pop(mode, None)
elif 'category' in img:
if not img['category'] in locateImages:
locateImages[img['category']] = {}
locateImages[img['category']][img['name']] = cv2.imread(imagesDir + filename)
else:
locateImages[img['name']] = cv2.imread(imagesDir + filename)
locateImages['collection'] = {}
if exists(imagesDir + 'collection_events'):
for filename in os.listdir(imagesDir + 'collection_events'):
locateImages['collection'][filename.replace('.png', '')] = cv2.imread(imagesDir + 'collection_events/' + filename)
return {'comparisonImages': comparisonImages, 'locateImages': locateImages, 'segmentCoordinates': segmentCoordinates, 'supportedModes': supportedModes, 'resolution': resolution}
class State(Enum):
UNDEFINED = 0
IDLE = 1
INGAME = 2
GOTO_HOME = 3
GOTO_INGAME = 4
SELECT_HERO = 5
FIND_HARDEST_INCREASED_REWARDS_MAP = 6
MANAGE_OBJECTIVES = 7
EXIT = 8
class Screen(Enum):
UNKNOWN = 0
STARTMENU = 1
MAP_SELECTION = 3
DIFFICULTY_SELECTION = 4
GAMEMODE_SELECTION = 5
HERO_SELECTION = 6
INGAME = 7
INGAME_PAUSED = 8
VICTORY_SUMMARY = 9
VICTORY = 10
DEFEAT = 11
OVERWRITE_SAVE = 12
LEVELUP = 13
APOPALYPSE_HINT = 14
ROUND_100_INSTA = 15
COLLECTION_CLAIM_CHEST = 16
BTD6_UNFOCUSED = 17
class Mode(Enum):
ERROR = 0
SINGLE_MAP = 1
RANDOM_MAP = 2
CHASE_REWARDS = 3
DO_ACHIEVEMENTS = 4
MISSING_MAPS = 5
XP_FARMING = 6
MM_FARMING = 7
MISSING_STATS = 8
VALIDATE_PLAYTHROUGHS = 9
VALIDATE_COSTS = 10
def getGamemodePosition(gamemode):
while not isinstance(imageAreas["click"]["gamemode_positions"][gamemode], list):
gamemode = imageAreas["click"]["gamemode_positions"][gamemode]
return imageAreas["click"]["gamemode_positions"][gamemode]
def getNextNonSellAction(steps):
for step in steps:
if step['action'] != 'sell' and step['action'] != 'await_round':
return step
return {'action': 'nop', 'cost': 0}
def sumAdjacentSells(steps):
gain = 0
for step in steps:
if step['action'] != 'sell':
return gain
gain += -step['cost']
return gain
exitAfterGame = False
def setExitAfterGame():
global exitAfterGame
activeWindow = ahk.get_active_window()
if not activeWindow or not isBTD6Window(activeWindow.title):
return
customPrint("script will stop after finishing the current game!")
exitAfterGame = True
def signalHandler(signum, frame):
customPrint('received SIGINT! exiting!')
sys.exit(0)
def main():
signal.signal(signal.SIGINT, signalHandler)
data = getResolutionDependentData()
if not data:
print('unsupported resolution! reference images missing!')
return
comparisonImages = data['comparisonImages']
locateImages = data['locateImages']
segmentCoordinates = data['segmentCoordinates']
supportedModes = data['supportedModes']
resolution = data['resolution']
allAvailablePlaythroughs = getAllAvailablePlaythroughs(['own_playthroughs'], considerUserConfig=True)
allAvailablePlaythroughsList = allPlaythroughsToList(allAvailablePlaythroughs)
mode = Mode.ERROR
logStats = True
isContinue = False
repeatObjectives = False
doAllStepsBeforeStart = False
listAvailablePlaythroughs = False
handlePlaythroughValidation = ValidatedPlaythroughs.EXCLUDE_NON_VALIDATED
usesAllAvailablePlaythroughsList = False
hasSeenStartCommand = False
collectionEvent = None
valueUnit = ''
originalObjectives = []
objectives = []
categoryRestriction = None
gamemodeRestriction = None
argv = np.array(sys.argv)
parsedArguments = []
# Additional flags:
# -ns: disable stats logging
if len(np.where(argv == '-ns')[0]):
customPrint('stats logging disabled!')
parsedArguments.append('-ns')
logStats = False
else:
customPrint('stats logging enabled!')
# -r: after finishing all objectives the program restarts with the first objective
if len(np.where(argv == '-r')[0]):
customPrint('repeating objective indefinitely! cancel with ctrl + c!')
parsedArguments.append('-r')
repeatObjectives = True
# -mk: after finishing all objectives the program restarts with the first objective
if len(np.where(argv == '-mk')[0]):
customPrint('including playthroughs with monkey knowledge enabled and adjusting prices according to userconfig.json!')
parsedArguments.append('-mk')
setMonkeyKnowledgeStatus(True)
# -nomk: after finishing all objectives the program restarts with the first objective
elif len(np.where(argv == '-nomk')[0]):
customPrint('ignoring playthroughs with monkey knowledge enabled!')
parsedArguments.append('-nomk')
setMonkeyKnowledgeStatus(False)
else:
customPrint('"-mk" (for monkey knowledge enabled) or "-nomk" (for monkey knowledge disabled) must be specified! exiting!')
return
# -l: list all available playthroughs(only works with specific modes)
if len(np.where(argv == '-l')[0]):
parsedArguments.append('-l')
listAvailablePlaythroughs = True
# -nv: include non validated playthroughs. ignored when mode = validate
if len(np.where(argv == '-nv')[0]):
parsedArguments.append('-nv')
handlePlaythroughValidation = ValidatedPlaythroughs.INCLUDE_ALL
iArg = 1
if len(argv) <= iArg:
customPrint('arguments missing! Usage: py replay.py <mode> <mode arguments...> <flags>')
return
# py replay.py file <filename> [continue <(int start)|-> [until (int end)]]
# replays the specified file
# if continue is specified it is assumed you are already in game. the script starts with instruction start(0 for first instruction)
# if the value for continue equals "-" all instructions are executed before the game is started
# if until is specified the script only executes instructions until instruction end(start=0, end=1 -> only first instruction is executed)
# the continue option is mainly for creating/debugging playthroughs
# -r for indefinite playing only works if continue is not set
elif argv[iArg] == 'file':
# run single map, next argument should be the filename
iAdditionalStart = iArg + 2
iAdditional = iAdditionalStart
if len(argv) <= iArg + 1:
customPrint('requested running a playthrough but no playthrough provided! exiting!')
return
parsedArguments.append(argv[iArg + 1])
instructionOffset = -1
instructionLast = -1
gamemode = None
if len(argv) > iAdditional and argv[iAdditional] in gamemodes:
gamemode = argv[iAdditional]
parsedArguments.append(argv[iAdditional])
iAdditional += 1
if len(argv) > iAdditional + 1 and argv[iAdditional] == 'continue':
parsedArguments.append(argv[iAdditional])
isContinue = True
if str(argv[iAdditional + 1]) == '-':
instructionOffset = 0
doAllStepsBeforeStart = True
elif str(argv[iAdditional + 1]).isdigit():
instructionOffset = int(argv[iAdditional + 1])
else:
customPrint('continue of playthrough requested but no instruction offset provided!')
return
customPrint('stats logging disabled!')
logStats = False
parsedArguments.append(argv[iAdditional + 1])
iAdditional += 2
if len(argv) >= iAdditional + 1 and argv[iAdditional] == 'until':
if str(argv[iAdditional + 1]).isdigit():
instructionLast = int(argv[iAdditional + 1])
else:
customPrint('cutting of instructions for playthrough requested but no index provided!')
return
parsedArguments.append(argv[iAdditional])
parsedArguments.append(argv[iAdditional + 1])
iAdditional += 2
if not parseBTD6InstructionFileName(argv[iArg + 1]):
customPrint('"' + str(argv[iArg + 1]) + '" can\'t be recognized as a playthrough filename! exiting!')
return
elif str(argv[iArg + 1]).count('/') or str(argv[iArg + 1]).count('\\') and exists(argv[iArg + 1]):
filename = argv[iArg + 1]
elif exists('own_playthroughs/' + argv[iArg + 1]):
filename = 'own_playthroughs/' + argv[iArg + 1]
elif exists('playthroughs/' + argv[iArg + 1]):
filename = 'playthroughs/' + argv[iArg + 1]
elif exists('unvalidated_playthroughs/' + argv[iArg + 1]):
filename = 'unvalidated_playthroughs/' + argv[iArg + 1]
else:
customPrint('requested playthrough ' + str(argv[iArg + 1]) + ' not found! exiting!')
return
mapConfig = parseBTD6InstructionsFile(filename, gamemode=gamemode)
segmentCoordinates = getResolutionDependentData(resolution, mapConfig['gamemode'])['segmentCoordinates']
mode = Mode.SINGLE_MAP
if instructionOffset == -1:
originalObjectives.append({'type': State.GOTO_HOME})
if 'hero' in mapConfig:
originalObjectives.append({'type': State.SELECT_HERO, 'mapConfig': mapConfig})
originalObjectives.append({'type': State.GOTO_HOME})
originalObjectives.append({'type': State.GOTO_INGAME, 'mapConfig': mapConfig})
else:
if instructionOffset >= len(mapConfig['steps']) or (instructionLast != -1 and instructionOffset >= instructionLast):
customPrint('instruction offset > last instruction (' + (str(instructionLast) if instructionLast != -1 else str(len(mapConfig['steps']))) + ')')
return
if instructionLast != -1:
mapConfig['steps'] = mapConfig['steps'][(instructionOffset + mapConfig['extrainstructions']):instructionLast]
else:
mapConfig['steps'] = mapConfig['steps'][(instructionOffset + mapConfig['extrainstructions']):]
customPrint('continuing playthrough. first instruction:')
customPrint(mapConfig['steps'][0])
originalObjectives.append({'type': State.INGAME, 'mapConfig': mapConfig})
originalObjectives.append({'type': State.MANAGE_OBJECTIVES})
# py replay.py random [category] [gamemode]
# plays a random game from all available playthroughs (which fullfill the category and gamemode requirement if specified)
elif argv[iArg] == 'random':
iAdditional = iArg + 1
if len(argv) > iAdditional and argv[iAdditional] in mapsByCategory:
categoryRestriction = argv[iAdditional]
parsedArguments.append(argv[iAdditional])
iAdditional += 1
if len(argv) > iAdditional and argv[iAdditional] in gamemodes:
gamemodeRestriction = argv[iAdditional]
parsedArguments.append(argv[iAdditional])
iAdditional += 1
customPrint('Mode: playing random games' + (f' on {gamemodeRestriction}' if gamemodeRestriction else '') + (f' in {categoryRestriction} category' if categoryRestriction else '') + '!')
allAvailablePlaythroughs = filterAllAvailablePlaythroughs(allAvailablePlaythroughs, getMonkeyKnowledgeStatus(), handlePlaythroughValidation, categoryRestriction, gamemodeRestriction)
allAvailablePlaythroughsList = allPlaythroughsToList(allAvailablePlaythroughs)
originalObjectives.append({'type': State.MANAGE_OBJECTIVES})
mode = Mode.RANDOM_MAP
usesAllAvailablePlaythroughsList = True
# py replay.py chase <event> [category] [gamemode]
# chases increased rewards for the specified event
# if category is not provided it finds the map with increased rewards in expert category and plays the most valuable available playthrough and downgrades category if no playthrough is available
# use -r to farm indefinitely
elif argv[iArg] == 'chase':
if len(argv) <= iArg + 1 or not argv[iArg + 1] in locateImages['collection']:
customPrint('requested chasing event rewards but no event specified or unknown event! exiting!')
return
collectionEvent = argv[iArg + 1]
parsedArguments.append(argv[iArg + 1])
iAdditional = iArg + 2
if len(argv) > iAdditional and argv[iAdditional] in mapsByCategory:
categoryRestriction = argv[iAdditional]
parsedArguments.append(argv[iAdditional])
iAdditional += 1
if len(argv) > iAdditional and argv[iAdditional] in gamemodes:
gamemodeRestriction = argv[iAdditional]
parsedArguments.append(argv[iAdditional])
iAdditional += 1
if collectionEvent == 'golden_bloon':
allAvailablePlaythroughs = filterAllAvailablePlaythroughs(allAvailablePlaythroughs, getMonkeyKnowledgeStatus(), handlePlaythroughValidation, categoryRestriction, gamemodeRestriction, requiredFlags=['gB'])
customPrint(f'Mode: playing games with golden bloons using special playthroughs' + (f' on {gamemodeRestriction}' if gamemodeRestriction else '') + (f' in {categoryRestriction} category' if categoryRestriction else '') + '!')
else:
allAvailablePlaythroughs = filterAllAvailablePlaythroughs(allAvailablePlaythroughs, getMonkeyKnowledgeStatus(), handlePlaythroughValidation, categoryRestriction, gamemodeRestriction)
customPrint(f'Mode: playing games with increased {collectionEvent} collection event rewards' + (f' on {gamemodeRestriction}' if gamemodeRestriction else '') + (f' in {categoryRestriction} category' if categoryRestriction else '') + '!')
allAvailablePlaythroughsList = allPlaythroughsToList(allAvailablePlaythroughs)
originalObjectives.append({'type': State.MANAGE_OBJECTIVES})
mode = Mode.CHASE_REWARDS
usesAllAvailablePlaythroughsList = True
# py replay.py achievements [achievement]
# plays all achievement related playthroughs
# if achievement is provided it just plays plays until said achievement is unlocked
# userconfig.json can be used to specify which achievements have already been unlocked or to document progress(e. g. games won only using primary monkeys)
# refer to userconfig.example.json for an example
elif argv[iArg] == 'achievements':
pass
# py replay.py missing [category]
# plays all playthroughs with missing medals
# if category is not provided from easiest category to hardest
# if category is provided in said category
# requires userconfig.json to specify which medals have already been earned
# unlocking of maps has do be done manually
elif argv[iArg] == 'missing':
pass
# py replay.py xp [int n=1]
# plays one of the n most efficient(in terms of xp/hour) playthroughs
# with -r: plays indefinitely
elif argv[iArg] == 'xp':
allAvailablePlaythroughsList = sortPlaythroughsByXPGain(allAvailablePlaythroughsList)
if len(argv) > iArg + 1 and argv[iArg + 1].isdigit():
allAvailablePlaythroughsList = allAvailablePlaythroughsList[:int(argv[iArg + 1])]
parsedArguments.append(argv[iArg + 1])
else:
allAvailablePlaythroughsList = allAvailablePlaythroughsList[:1]
originalObjectives.append({'type': State.MANAGE_OBJECTIVES})
mode = Mode.XP_FARMING
valueUnit = 'XP/h'
usesAllAvailablePlaythroughsList = True
# py replay.py mm [int n=1]
# plays one of the n most efficient(in terms of mm/hour) playthroughs
# with -r: plays indefinitely
elif argv[iArg] == 'mm' or argv[iArg] == 'monkey_money':
allAvailablePlaythroughsList = sortPlaythroughsByMonkeyMoneyGain(allAvailablePlaythroughsList)
if len(argv) > iArg + 1 and argv[iArg + 1].isdigit():
allAvailablePlaythroughsList = allAvailablePlaythroughsList[:int(argv[iArg + 1])]
parsedArguments.append(argv[iArg + 1])
else:
allAvailablePlaythroughsList = allAvailablePlaythroughsList[:1]
originalObjectives.append({'type': State.MANAGE_OBJECTIVES})
mode = Mode.MM_FARMING
valueUnit = 'MM/h'
usesAllAvailablePlaythroughsList = True
# py replay.py validate file <filename>
# or
# py replay.py validate all [category]
elif argv[iArg] == 'validate':
if len(argv) <= iArg + 1:
customPrint('requested validation but arguments missing!')
return
parsedArguments.append(argv[iArg + 1])
if getMonkeyKnowledgeStatus():
customPrint('Mode validate only works with monkey knowledge disabled!')
return
if argv[iArg + 1] == 'file':
if len(argv) <= iArg + 2:
customPrint('no filename provided!')
return
if not parseBTD6InstructionFileName(argv[iArg + 2]):
customPrint('"' + str(argv[iArg + 2]) + '" can\'t be recognized as a playthrough filename! exiting!')
return
elif str(argv[iArg + 1]).count('/') or str(argv[iArg + 2]).count('\\') and exists(argv[iArg + 2]):
filename = argv[iArg + 1]
elif exists('own_playthroughs/' + argv[iArg + 2]):
filename = 'own_playthroughs/' + argv[iArg + 2]
elif exists('playthroughs/' + argv[iArg + 2]):
filename = 'playthroughs/' + argv[iArg + 2]
elif exists('unvalidated_playthroughs/' + argv[iArg + 2]):
filename = 'unvalidated_playthroughs/' + argv[iArg + 2]
else:
customPrint('requested playthrough ' + str(argv[iArg + 2]) + ' not found! exiting!')
return
parsedArguments.append(argv[iArg + 2])
fileConfig = parseBTD6InstructionFileName(filename)
allAvailablePlaythroughsList = [{'filename': filename, 'fileConfig': fileConfig, 'gamemode': fileConfig['gamemode'], 'isOriginalGamemode': True}]
elif argv[iArg + 1] == 'all':
iAdditional = iArg + 2
if len(argv) > iAdditional and argv[iAdditional] in mapsByCategory:
categoryRestriction = argv[iAdditional]
parsedArguments.append(argv[iAdditional])
iAdditional += 1
customPrint('Mode: validating all playthroughs' + (' in ' + categoryRestriction + ' category' if categoryRestriction else '') + '!')
allAvailablePlaythroughs = filterAllAvailablePlaythroughs(allAvailablePlaythroughs, True, ValidatedPlaythroughs.EXCLUDE_VALIDATED if handlePlaythroughValidation == ValidatedPlaythroughs.INCLUDE_ALL else ValidatedPlaythroughs.INCLUDE_ALL, categoryRestriction, gamemodeRestriction, onlyOriginalGamemodes=True)
allAvailablePlaythroughsList = allPlaythroughsToList(allAvailablePlaythroughs)
originalObjectives.append({'type': State.MANAGE_OBJECTIVES})
usesAllAvailablePlaythroughsList = True
mode = Mode.VALIDATE_PLAYTHROUGHS
# py replay.py costs [+heros]
# determines the base cost and cost of each upgrade for each monkey as well as the base cost for each hero if '+heros' is specified
elif argv[iArg] == 'costs':
if getMonkeyKnowledgeStatus():
customPrint('Mode validate costs only works with monkey knowledge disabled!')
return
includeHeros = False
if len(argv) >= iArg + 2 and argv[iArg + 1] == '+heros':
includeHeros = True
parsedArguments.append(argv[iArg + 1])
customPrint('Mode: validating monkey costs' + (' including heros' if includeHeros else '') + '!')
allTestPositions = json.load(open('test_positions.json'))
if getResolutionString() in allTestPositions:
testPositions = allTestPositions[getResolutionString()]
else:
testPositions = json.loads(convertPositionsInString(json.dumps(allTestPositions['2560x1440']), (2560, 1440), pyautogui.size()))
selectedMap = None
for mapname in testPositions:
if getAvailableSandbox(mapname, ['medium_sandbox']):
selectedMap = mapname
break
if selectedMap is None:
customPrint('This mode requires access to medium sandbox for one of the maps in "test_positions.json"!')
return
costs = {'monkeys': {}}
baseMapConfig = {'category': maps[selectedMap]['category'], 'map': selectedMap, 'page': maps[selectedMap]['page'], 'pos': maps[selectedMap]['pos'], 'difficulty': 'medium', 'gamemode': 'medium_sandbox', 'steps': [], 'extrainstructions': 1, 'filename': None}
monkeySteps = []
monkeySteps.append({'action': 'click', 'pos': imageAreas['click']['gamemode_deflation_message_confirmation'], 'cost': 0})
pos = testPositions[selectedMap]
pos['any'] = pos['land']
for monkeyType in towers['monkeys']:
costs['monkeys'][monkeyType] = {'base': 0, 'upgrades': np.zeros((3, 5))}
for iPath in range(0, 3):
monkeySteps.append({'action': 'place', 'type': monkeyType, 'name': f"{monkeyType}{iPath}", 'key': keybinds['monkeys'][monkeyType], 'pos': pos[towers['monkeys'][monkeyType]['class']], 'cost': 1, 'extra': {'group': 'monkeys', 'type': monkeyType}})
for iUpgrade in range(1, 6):
monkeySteps.append({'action': 'upgrade', 'name': f"{monkeyType}{iPath}", 'key': keybinds['path'][str(iPath)], 'pos': pos[towers['monkeys'][monkeyType]['class']], 'path': iPath, 'cost': 1, 'extra': {'group': 'monkeys', 'type': monkeyType, 'upgrade': (iPath, iUpgrade)}})
if upgradeRequiresConfirmation({'type': monkeyType, 'upgrades': [(iUpgrade if iTmp == iPath else 0) for iTmp in range(0, 3)]}, iPath):
monkeySteps.append({'action': 'click', 'name': f"{monkeyType}{iPath}", 'pos': imageAreas['click']['paragon_message_confirmation'], 'cost': 0})
monkeySteps.append({'action': 'sell', 'name': f"{monkeyType}{iPath}", 'key': keybinds['others']['sell'], 'pos': pos[towers['monkeys'][monkeyType]['class']], 'cost': -1})
monkeyMapConfig = copy.deepcopy(baseMapConfig)
monkeyMapConfig['steps'] = monkeySteps
originalObjectives.append({'type': State.GOTO_HOME})
originalObjectives.append({'type': State.GOTO_INGAME, 'mapConfig': monkeyMapConfig})
originalObjectives.append({'type': State.INGAME, 'mapConfig': monkeyMapConfig})
if includeHeros:
costs['heros'] = {}
for hero in towers['heros']:
costs['heros'][hero] = {'base' : 0}
heroMapConfig = copy.deepcopy(baseMapConfig)
heroMapConfig['hero'] = hero
heroMapConfig['steps'] = [{'action': 'click', 'pos': imageAreas['click']['gamemode_deflation_message_confirmation'], 'cost': 0}, {'action': 'place', 'type': 'hero', 'name': 'hero0', 'key': keybinds['monkeys']['hero'], 'pos': pos[towers['heros'][hero]['class']], 'cost': 1, 'extra': {'group': 'heros', 'type': hero}}]
originalObjectives.append({'type': State.GOTO_HOME})
originalObjectives.append({'type': State.SELECT_HERO, 'mapConfig': heroMapConfig})
originalObjectives.append({'type': State.GOTO_HOME})
originalObjectives.append({'type': State.GOTO_INGAME, 'mapConfig': heroMapConfig})
originalObjectives.append({'type': State.INGAME, 'mapConfig': heroMapConfig})
originalObjectives.append({'type': State.MANAGE_OBJECTIVES})
usesAllAvailablePlaythroughsList = False
mode = Mode.VALIDATE_COSTS
if mode == Mode.ERROR:
customPrint('invalid arguments! exiting!')
return
if not mode.name in supportedModes:
customPrint('mode not supported due to missing images!')
return
parsedArguments.append(argv[0])
parsedArguments.append(argv[1])
unparsedArguments = []
parsedArgumentsTmp = np.array(parsedArguments)
for arg in sys.argv:
if len(np.where(parsedArgumentsTmp == arg)[0]):
parsedArgumentsTmp = np.delete(parsedArgumentsTmp, np.where(parsedArgumentsTmp == arg)[0])
else:
unparsedArguments.append(arg)
if len(unparsedArguments):
customPrint('unrecognized arguments:')
customPrint(unparsedArguments)
customPrint('exiting!')
return
if listAvailablePlaythroughs:
if usesAllAvailablePlaythroughsList:
customPrint(str(len(allAvailablePlaythroughsList)) + ' playthroughs found:')
for playthrough in allAvailablePlaythroughsList:
customPrint(playthrough['filename'] + ': ' + playthrough['fileConfig']['map'] + ' - ' + playthrough['gamemode'] + (' with ' + str(playthrough['value']) + (' ' + valueUnit if len(valueUnit) else '') if 'value' in playthrough else ''))
else:
customPrint('Mode doesn\'t qualify for listing all available playthroughs')
return
if usesAllAvailablePlaythroughsList and len(allAvailablePlaythroughsList) == 0:
customPrint('no playthroughs matching requirements found!')
keyboard.add_hotkey('ctrl+space', setExitAfterGame)
objectives = copy.deepcopy(originalObjectives)
state = objectives[0]['type']
lastStateTransitionSuccessful = True
objectiveFailed = False
mapConfig = objectives[0]['mapConfig'] if 'mapConfig' in objectives[0] else None
gamesPlayed = 0
lastIterationBalance = -1
lastIterationRound = -1
lastIterationScreenshotAreas = []
lastIterationCost = 0
iterationBalances = []
thisIterationAction = None
lastIterationAction = None
fast = True
validationResult = None
playthroughLog = {}
lastHeroSelected = None
increasedRewardsPlaythrough = None
lastPlaythrough = None
lastPlaythroughStats = {}
lastScreen = Screen.UNKNOWN
lastState = State.UNDEFINED
unknownScreenHasWaited = False
while True:
screenshot = np.array(pyautogui.screenshot())[:, :, ::-1].copy()
screen = Screen.UNKNOWN
activeWindow = ahk.get_active_window()
if not activeWindow or not isBTD6Window(activeWindow.title):
screen = Screen.BTD6_UNFOCUSED
else:
bestMatchDiff = None
for screenCfg in [
(Screen.STARTMENU, comparisonImages["screens"]["startmenu"], imageAreas["compare"]["screens"]["startmenu"]),
(Screen.MAP_SELECTION, comparisonImages["screens"]["map_selection"], imageAreas["compare"]["screens"]["map_selection"]),
(Screen.DIFFICULTY_SELECTION, comparisonImages["screens"]["difficulty_selection"], imageAreas["compare"]["screens"]["difficulty_selection"]),
(Screen.GAMEMODE_SELECTION, comparisonImages["screens"]["gamemode_selection"], imageAreas["compare"]["screens"]["gamemode_selection"]),
(Screen.HERO_SELECTION, comparisonImages["screens"]["hero_selection"], imageAreas["compare"]["screens"]["hero_selection"]),
(Screen.INGAME, comparisonImages["screens"]["ingame"], imageAreas["compare"]["screens"]["ingame"]),
(Screen.INGAME_PAUSED, comparisonImages["screens"]["ingame_paused"], imageAreas["compare"]["screens"]["ingame_paused"]),
(Screen.VICTORY_SUMMARY, comparisonImages["screens"]["victory_summary"], imageAreas["compare"]["screens"]["victory_summary"]),
(Screen.VICTORY, comparisonImages["screens"]["victory"], imageAreas["compare"]["screens"]["victory"]),
(Screen.DEFEAT, comparisonImages["screens"]["defeat"], imageAreas["compare"]["screens"]["defeat"]),
(Screen.OVERWRITE_SAVE, comparisonImages["screens"]["overwrite_save"], imageAreas["compare"]["screens"]["overwrite_save"]),
(Screen.LEVELUP, comparisonImages["screens"]["levelup"], imageAreas["compare"]["screens"]["levelup"]),
(Screen.APOPALYPSE_HINT, comparisonImages["screens"]["apopalypse_hint"], imageAreas["compare"]["screens"]["apopalypse_hint"]),
(Screen.ROUND_100_INSTA, comparisonImages["screens"]["round_100_insta"], imageAreas["compare"]["screens"]["round_100_insta"]),
(Screen.COLLECTION_CLAIM_CHEST, comparisonImages["screens"]["collection_claim_chest"], imageAreas["compare"]["screens"]["collection_claim_chest"]),
]:
diff = cv2.matchTemplate(cutImage(screenshot, screenCfg[2]), cutImage(screenCfg[1], screenCfg[2]), cv2.TM_SQDIFF_NORMED)[0][0]
if diff < 0.05 and (bestMatchDiff is None or diff < bestMatchDiff):
bestMatchDiff = diff
screen = screenCfg[0]
if screen != lastScreen:
customPrint("screen " + screen.name + "!")
if screen == Screen.BTD6_UNFOCUSED:
pass
# don't do anything when ctrl is pressed: useful for alt + tab / sending SIGINT(ctrl + c) to the script
elif keyboard.is_pressed('ctrl'):
pass
elif state == State.MANAGE_OBJECTIVES:
customPrint("entered objective management!")
if exitAfterGame:
state = State.EXIT
continue
if mode == Mode.VALIDATE_PLAYTHROUGHS:
if validationResult != None:
customPrint('validation result: playthrough ' + lastPlaythrough['filename'] + ' is ' + ('valid' if validationResult else 'invalid') + '!')
updatePlaythroughValidationStatus(lastPlaythrough['filename'], validationResult)
if len(allAvailablePlaythroughsList):
playthrough = allAvailablePlaythroughsList.pop(0)
customPrint('validation playthrough chosen: ' + playthrough['fileConfig']['map'] + ' on ' + playthrough['gamemode'] + ' (' + playthrough['filename'] + ')')
gamemode = getAvailableSandbox(playthrough['fileConfig']['map'])
if gamemode:
mapConfig = parseBTD6InstructionsFile(playthrough['filename'], gamemode=gamemode)
objectives = []
objectives.append({'type': State.GOTO_HOME})
if 'hero' in mapConfig and lastHeroSelected != mapConfig['hero']:
objectives.append({'type': State.SELECT_HERO, 'mapConfig': mapConfig})
objectives.append({'type': State.GOTO_HOME})
objectives.append({'type': State.GOTO_INGAME, 'mapConfig': mapConfig})
objectives.append({'type': State.INGAME, 'mapConfig': mapConfig})
objectives.append({'type': State.MANAGE_OBJECTIVES})
validationResult = True
lastPlaythrough = playthrough
else:
customPrint('missing sandbox access for ' + playthrough['fileConfig']['map'])
objectives = []
objectives.append({'type': State.MANAGE_OBJECTIVES})
else:
objectives = []
objectives.append({'type': State.EXIT})
elif mode == Mode.VALIDATE_COSTS:
oldTowers = copy.deepcopy(towers)
changes = 0
for monkeyType in costs['monkeys']:
if costs['monkeys'][monkeyType]['base'] and costs['monkeys'][monkeyType]['base'] != oldTowers['monkeys'][monkeyType]['base']:
print(f"{monkeyType} base cost: {oldTowers['monkeys'][monkeyType]['base']} -> {int(costs['monkeys'][monkeyType]['base'])}")
towers['monkeys'][monkeyType]['base'] = int(costs['monkeys'][monkeyType]['base'])
changes += 1
for iPath in range(0, 3):
for iUpgrade in range(0, 5):
if costs['monkeys'][monkeyType]['upgrades'][iPath][iUpgrade] and costs['monkeys'][monkeyType]['upgrades'][iPath][iUpgrade] != oldTowers['monkeys'][monkeyType]['upgrades'][iPath][iUpgrade]:
print(f"{monkeyType} path {iPath + 1} upgrade {iUpgrade + 1} cost: {oldTowers['monkeys'][monkeyType]['upgrades'][iPath][iUpgrade]} -> {int(costs['monkeys'][monkeyType]['upgrades'][iPath][iUpgrade])}")
towers['monkeys'][monkeyType]['upgrades'][iPath][iUpgrade] = int(costs['monkeys'][monkeyType]['upgrades'][iPath][iUpgrade])
changes += 1
if 'heros' in costs:
for hero in costs['heros']:
if costs['heros'][hero]['base'] and costs['heros'][hero]['base'] != oldTowers['heros'][hero]['base']:
print(f"hero {hero} base cost: {oldTowers['heros'][hero]['base']} -> {int(costs['heros'][hero]['base'])}")
towers['heros'][hero]['base'] = int(costs['heros'][hero]['base'])
changes += 1
if changes:
print(f"updating \"towers.json\" with {changes} changes!")
fp = open('towers_backup.json', "w")
fp.write(json.dumps(oldTowers, indent=4))
fp.close()
fp = open('towers.json', "w")
fp.write(json.dumps(towers, indent=4))
fp.close()
else:
print(f"no price changes in comparison to \"towers.json\" detected!")
return
elif repeatObjectives or gamesPlayed == 0:
if mode == Mode.SINGLE_MAP:
objectives = copy.deepcopy(originalObjectives)
elif mode == Mode.RANDOM_MAP or mode == Mode.XP_FARMING or mode == Mode.MM_FARMING:
objectives = []
playthrough = random.choice(allAvailablePlaythroughsList)
customPrint('random playthrough chosen: ' + playthrough['fileConfig']['map'] + ' on ' + playthrough['gamemode'] + ' (' + playthrough['filename'] + ')')
mapConfig = parseBTD6InstructionsFile(playthrough['filename'], gamemode=playthrough['gamemode'])
segmentCoordinates = getResolutionDependentData(resolution, mapConfig['gamemode'])['segmentCoordinates']
objectives.append({'type': State.GOTO_HOME})
if 'hero' in mapConfig and lastHeroSelected != mapConfig['hero']:
objectives.append({'type': State.SELECT_HERO, 'mapConfig': mapConfig})
objectives.append({'type': State.GOTO_HOME})
objectives.append({'type': State.GOTO_INGAME, 'mapConfig': mapConfig})
objectives.append({'type': State.INGAME, 'mapConfig': mapConfig})
objectives.append({'type': State.MANAGE_OBJECTIVES})
lastPlaythrough = playthrough
elif mode == Mode.CHASE_REWARDS:
objectives = []
if increasedRewardsPlaythrough:
playthrough = increasedRewardsPlaythrough
customPrint('highest reward playthrough chosen: ' + playthrough['fileConfig']['map'] + ' on ' + playthrough['gamemode'] + ' (' + playthrough['filename'] + ')')
mapConfig = parseBTD6InstructionsFile(playthrough['filename'], gamemode=playthrough['gamemode'])
segmentCoordinates = getResolutionDependentData(resolution, mapConfig['gamemode'])['segmentCoordinates']
objectives.append({'type': State.GOTO_HOME})
if 'hero' in mapConfig and lastHeroSelected != mapConfig['hero']:
objectives.append({'type': State.SELECT_HERO, 'mapConfig': mapConfig})
objectives.append({'type': State.GOTO_HOME})
objectives.append({'type': State.GOTO_INGAME, 'mapConfig': mapConfig})
objectives.append({'type': State.INGAME, 'mapConfig': mapConfig})
objectives.append({'type': State.MANAGE_OBJECTIVES})
increasedRewardsPlaythrough = None
lastPlaythrough = playthrough
else:
objectives.append({'type': State.GOTO_HOME})
objectives.append({'type': State.FIND_HARDEST_INCREASED_REWARDS_MAP})
objectives.append({'type': State.MANAGE_OBJECTIVES})
else:
objectives = copy.deepcopy(originalObjectives)
else:
objectives = []
objectives.append({'type': State.EXIT})
state = objectives[0]['type']
lastStateTransitionSuccessful = True
objectiveFailed = False
elif state == State.UNDEFINED:
customPrint("entered state management!")
if exitAfterGame:
state = State.EXIT
if objectiveFailed:
customPrint("objective failed on step " + objectives[0]['type'].name + "(screen " + lastScreen.name + ")!")
if repeatObjectives:
state = State.MANAGE_OBJECTIVES
else:
state = State.EXIT
elif not lastStateTransitionSuccessful:
state = objectives[0]['type']
if 'mapConfig' in objectives[0]:
mapConfig = objectives[0]['mapConfig']
lastStateTransitionSuccessful = True
elif lastStateTransitionSuccessful and len(objectives):
objectives.pop(0)
state = objectives[0]['type']
if 'mapConfig' in objectives[0]:
mapConfig = objectives[0]['mapConfig']
else:
state = State.EXIT
elif state == State.IDLE:
pass
elif state == State.EXIT:
customPrint("goal EXIT! exiting!")
return
elif state == State.GOTO_HOME:
if screen == Screen.STARTMENU:
customPrint("goal GOTO_HOME fullfilled!")
state = State.UNDEFINED
elif screen == Screen.UNKNOWN:
if lastScreen == Screen.UNKNOWN and unknownScreenHasWaited:
unknownScreenHasWaited = False
sendKey('{Esc}')
else:
unknownScreenHasWaited = True
time.sleep(2)
elif screen == Screen.INGAME:
sendKey('{Esc}')
elif screen == Screen.INGAME_PAUSED:
pyautogui.click(imageAreas["click"]["screen_ingame_paused_button_home"])
elif screen == Screen.HERO_SELECTION:
sendKey('{Esc}')
elif screen == Screen.GAMEMODE_SELECTION:
sendKey('{Esc}')
elif screen == Screen.DIFFICULTY_SELECTION:
sendKey('{Esc}')
elif screen == Screen.MAP_SELECTION:
sendKey('{Esc}')
elif screen == Screen.DEFEAT:
result = cv2.matchTemplate(screenshot, locateImages['button_home'], cv2.TM_SQDIFF_NORMED)
pyautogui.click(cv2.minMaxLoc(result)[2])
elif screen == Screen.VICTORY_SUMMARY:
pyautogui.click(imageAreas["click"]["screen_victory_summary_button_next"])
elif screen == Screen.VICTORY:
pyautogui.click(imageAreas["click"]["screen_victory_button_home"])
elif screen == Screen.OVERWRITE_SAVE:
sendKey('{Esc}')
elif screen == Screen.LEVELUP:
pyautogui.click(100, 100)
time.sleep(menuChangeDelay)
pyautogui.click(100, 100)
elif screen == Screen.ROUND_100_INSTA:
pyautogui.click(100, 100)
time.sleep(menuChangeDelay)
elif screen == Screen.COLLECTION_CLAIM_CHEST:
pyautogui.click(imageAreas["click"]["collection_claim_chest"])
time.sleep(menuChangeDelay * 2)
while True:
newScreenshot = np.array(pyautogui.screenshot())[:, :, ::-1].copy()
result = [cv2.minMaxLoc(cv2.matchTemplate(newScreenshot, locateImages['unknown_insta'], cv2.TM_SQDIFF_NORMED, mask=locateImages['unknown_insta_mask']))[i] for i in [0,2]]
if result[0] < 0.01:
pyautogui.click(result[1])
time.sleep(menuChangeDelay)
pyautogui.click(result[1])
time.sleep(menuChangeDelay)
else:
break
pyautogui.click(round(resolution[0] / 2), round(resolution[1] / 2))
time.sleep(menuChangeDelay)
sendKey('{Esc}')
elif screen == Screen.APOPALYPSE_HINT:
pyautogui.click(imageAreas["click"]["gamemode_apopalypse_message_confirmation"])
elif state == State.GOTO_INGAME:
if screen == Screen.STARTMENU:
pyautogui.click(imageAreas["click"]["screen_startmenu_button_play"])
time.sleep(menuChangeDelay)
if mapConfig['category'] == 'beginner':
pyautogui.click(imageAreas["click"]["map_categories"]['advanced'])
time.sleep(menuChangeDelay)
pyautogui.click(imageAreas["click"]["map_categories"][mapConfig['category']])
time.sleep(menuChangeDelay)
else:
pyautogui.click(imageAreas["click"]["map_categories"]['beginner'])
time.sleep(menuChangeDelay)
pyautogui.click(imageAreas["click"]["map_categories"][mapConfig['category']])
time.sleep(menuChangeDelay)
tmpClicks = mapConfig['page']
while tmpClicks > 0:
pyautogui.click(imageAreas["click"]["map_categories"][mapConfig['category']])
tmpClicks -= 1
time.sleep(menuChangeDelay)
pyautogui.click(imageAreas["click"]["map_positions"][mapConfig['pos']])
time.sleep(menuChangeDelay)
pyautogui.click(imageAreas["click"]["gamedifficulty_positions"][mapConfig['difficulty']])
time.sleep(menuChangeDelay)
pyautogui.click(getGamemodePosition(mapConfig['gamemode']))
elif screen == Screen.OVERWRITE_SAVE:
pyautogui.click(imageAreas["click"]["screen_overwrite_save_button_ok"])
elif screen == Screen.APOPALYPSE_HINT:
pyautogui.click(imageAreas["click"]["gamemode_apopalypse_message_confirmation"])
elif screen == Screen.INGAME:
customPrint("goal GOTO_INGAME fullfilled!")
customPrint("game: " + mapConfig['map'] + ' - ' + mapConfig['difficulty'])
iterationBalances = []
if logStats:
lastPlaythroughStats = {'gamemode': mapConfig['gamemode'], 'time': [], 'result': PlaythroughResult.UNDEFINED}
lastPlaythroughStats['time'].append(('start', time.time()))
lastIterationBalance = -1
lastIterationCost = 0
state = State.UNDEFINED
elif screen == Screen.UNKNOWN:
pass
else:
customPrint("task GOTO_INGAME, but not in startmenu!")
state = State.GOTO_HOME
lastStateTransitionSuccessful = False
elif state == State.SELECT_HERO:
if screen == Screen.STARTMENU:
pyautogui.click(imageAreas["click"]["screen_startmenu_button_hero_selection"])
time.sleep(menuChangeDelay)
pyautogui.click(imageAreas["click"]["hero_positions"][mapConfig['hero']])
time.sleep(menuChangeDelay)
pyautogui.click(imageAreas["click"]["screen_hero_selection_select_hero"])
customPrint("goal SELECT_HERO " + mapConfig['hero'] + " fullfilled!")
lastHeroSelected = mapConfig['hero']
state = State.UNDEFINED
elif screen == Screen.UNKNOWN:
pass
else:
customPrint("task SELECT_HERO, but not in startmenu!")
state = State.GOTO_HOME
lastStateTransitionSuccessful = False
elif state == State.FIND_HARDEST_INCREASED_REWARDS_MAP:
if screen == Screen.STARTMENU:
pyautogui.click(imageAreas["click"]["screen_startmenu_button_play"])
time.sleep(menuChangeDelay)
if categoryRestriction:
pyautogui.click(imageAreas["click"]["map_categories"][('advanced' if categoryRestriction == 'beginner' else 'beginner')])
time.sleep(menuChangeDelay)
mapname = None
for page in range(0, categoryPages[categoryRestriction]):
pyautogui.click(imageAreas["click"]["map_categories"][categoryRestriction])
if collectionEvent == 'golden_bloon':
time.sleep(4)
else:
time.sleep(menuChangeDelay)
newScreenshot = np.array(pyautogui.screenshot())[:, :, ::-1].copy()
result = findImageInImage(newScreenshot, locateImages['collection'][collectionEvent])
if result[0] < 0.05:
mapname = findMapForPxPos(categoryRestriction, page, result[1])
break
if not mapname:
customPrint('no maps with increased rewards found! exiting!')
return
customPrint('best map: ' + mapname)
increasedRewardsPlaythrough = getHighestValuePlaythrough(allAvailablePlaythroughs, mapname, playthroughLog)
if not increasedRewardsPlaythrough:
customPrint('no playthroughs for map found! exiting!')
return
else:
iTmp = 0
for category in reversed(list(mapsByCategory.keys())):
if iTmp == 0: