-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
127 lines (96 loc) · 3.21 KB
/
main.py
File metadata and controls
127 lines (96 loc) · 3.21 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
# Standard
import os
import sys
from time import sleep
# Third Party
# Local
from src.args import parse_arguments
from src.errors import (
ChestNotFoundException,
GoldRecognitionFailureException,
SameDungeonException,
)
from src.game import GameManager
from src.gems import GemIdentifier
from src.gold import GoldIdentifier
def setup():
global ARGS, PATH, last_gold
if getattr(sys, "frozen", False): # handles compiled to exe
PATH = os.path.dirname(sys.executable)
else:
PATH = os.path.dirname(os.path.abspath(__file__))
ARGS = parse_arguments(sys.argv[1:])
last_gold = 0
def scan_dungeon(game, gold_identifier, gem_identifier=None):
"""
Scans dungeon for target gold and/or gems.
If target gold is found, returns int
If target gem is found, returns tuple (gem STR, confidence INT)
Otherwise returns None
"""
global last_gold
screenshot = game.screenshot()
gold = gold_identifier.identify(screenshot)
if gold == last_gold:
raise SameDungeonException
last_gold = gold
if gem_identifier:
gems = gem_identifier.identify(screenshot)
if ARGS.gold and gold >= ARGS.gold:
return gold
if gem_identifier:
for gem in gems:
if gem[0] in ARGS.gems:
return gem
def main():
print("Press CTRL + C on this console to exit at any time")
setup()
if ARGS.emulator == "bluestacks":
game = GameManager(PATH, debug=ARGS.debug)
gold_identifier = GoldIdentifier(PATH, debug=ARGS.debug)
gem_identifier = None
if ARGS.gems or (ARGS.debug and "screenshotmode" in ARGS.debug):
gem_identifier = GemIdentifier(PATH, tgems=ARGS.gems, debug=ARGS.debug)
# debug screenshot mode
if ARGS.debug and "screenshotmode" in ARGS.debug:
try:
while True:
input("Press enter when a totem is visible")
screenshot = game.screenshot()
gold_identifier.identify(screenshot)
gem_identifier.identify(screenshot)
except KeyboardInterrupt:
# prevents keyboardinterrupt causing normal program function to begin
return
# main loop
retries = 0
while True:
if retries > ARGS.retries:
game.skip()
sleep(ARGS.delay)
retries = 0
try:
result = scan_dungeon(game, gold_identifier, gem_identifier=gem_identifier)
except (
SameDungeonException,
GoldRecognitionFailureException,
ChestNotFoundException,
) as e:
print(e.err + ((" - retry " + str(retries)) if retries else ""))
retries += 1
sleep(ARGS.delay)
continue
retries = 0
if not result:
game.skip()
sleep(ARGS.delay)
elif isinstance(result, int):
print(f"Dungeon gold {result} meets target gold")
input("Press Enter to continue")
elif isinstance(result, tuple):
print(
f"Dungeon gem {result[0]} with {result[1]*100:.2f}% confidence meets target gem"
)
input("Press Enter to continue")
if __name__ == "__main__":
main()