-
Notifications
You must be signed in to change notification settings - Fork 0
/
bj3wc.py
286 lines (215 loc) · 12.5 KB
/
bj3wc.py
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
from bejeweled import *
from challenges import *
from queue_items import *
import queue
MINIQUEST_GOALS: list = json.load(open(pathlib.Path(PATH, "miniquest_goals.json")))
class Bejeweled3WorldChampionships:
def __init__(self):
self.challenge = None
self.game = None
self.gui_queue = queue.Queue() # Sends actions to the GUI (scores, reset, etc)
self.action_queue = queue.Queue() # Receives actions from the GUI (opened challenge)
self.challenge_end_time = 0
self.penalty = 120
self.scores = {}
self.fail_type = None
def add_subchallenge_extra(self, subchallenge: Subchallenge):
if subchallenge.objective in ["Avalanche", "Balance", "Butterflies", "ButterClear", "ButterCombo", "MatchBomb", "TimeBomb"]:
if subchallenge.objective == "Balance":
extra_pointer = Pointer(self.game, 0xbe0, 0x323c)
else:
extra_pointer = Pointer(self.game, 0xbe0, 0x3238)
if subchallenge.extra == None: # Apply defaults
match subchallenge.objective:
case "Avalanche":
extra_pointer.set_value(5) # 5 gems fall per turn
case "Balance":
extra_pointer.set_value(9) # Fix gem scale speed.
case "Butterflies" | "ButterClear" | "ButterCombo":
extra_pointer.set_value(1) # Butterflies move up every match
case "MatchBomb" | "TimeBomb":
extra_pointer.set_value(30) # Initial value for bombs is 30 turns or seconds.
else:
extra_pointer.set_value(subchallenge.extra)
def do_subchallenge(self, subchallenge: Subchallenge):
self.game.reset_scores()
condition_pointer = self.get_condition_pointer(subchallenge)
condition_pointer.update_address()
condition_pointer.set_value(subchallenge.get_initial_value()) # Reset checked value
difference = self.modify_quest_goals(subchallenge)
if difference == None: difference = 0
self.add_subchallenge_extra(subchallenge)
match subchallenge.mode:
case "value":
over_condition = lambda: condition_pointer.get_value() >= subchallenge.condition + difference
if subchallenge.time_bonus_enabled:
subchallenge_end_time = time.time() + subchallenge.time
case "timed":
subchallenge_end_time = time.time() + subchallenge.time
over_condition = lambda: time.time() >= subchallenge_end_time
case "endless":
over_condition = lambda: time.time() >= self.challenge_end_time
if subchallenge.objective == "Stratamax":
over_condition = lambda: Pointer(self.game, 0xbe0, 0x323c).get_value() <= 0
score_pointer = self.get_score_pointer(subchallenge)
get_score = lambda: score_pointer.get_value() - difference
current_score = get_score()
while not over_condition():
score_pointer.update_address()
previous_score = current_score
current_score = get_score()
if current_score < previous_score:
print("Challenge failed! (game over or reset detected)")
self.fail_type = "reset"
if self.challenge.mode == "timed":
print(f"{self.penalty} second penalty!")
self.challenge_end_time -= self.penalty
get_score = lambda: previous_score - difference
break
elif subchallenge.time_bonus_enabled:
if subchallenge_end_time <= time.time():
print("Challenge failed! (time ran out)")
self.fail_type = "time out"
if self.challenge.mode == "timed":
print(f"{self.penalty} second penalty!")
self.challenge_end_time -= self.penalty
break
elif self.is_challenge_time_up():
if len(self.challenge.subchallenges) != 0:
self.fail_type = "challenge end"
break
if subchallenge.time_bonus_enabled:
get_score = lambda: int(subchallenge_end_time - time.time()) * 1000
return max(0, get_score())
def get_condition_pointer(self, subchallenge: Subchallenge):
pointer_offsets = CHALLENGE_DATA[subchallenge.objective]["pointer_offsets"]
if subchallenge.objective == "PokerHand":
poker_hands = ["Pair", "Spectrum", "2 Pair", "3 of a Kind", "Full House", "4 of a Kind", "Flush"]
return Pointer(self.game, 0xbe0, 0x39d8 + 4 * poker_hands.index(subchallenge.extra))
for i in range(len(pointer_offsets)):
try:
pointer_offsets[i] = int(pointer_offsets[i], 0)
except:
continue
return Pointer(self.game, *pointer_offsets)
def get_score_pointer(self, subchallenge: Subchallenge):
quest_challenges = ["DiamondMine", "DiamondDepth", "DiamondTreasure",
"Balance", "Stratamax", "GoldRush", "Alchemy",
"TimeBomb", "MatchBomb", "Avalanche", "WallBlast",
"Treasure", "Sand"
]
if subchallenge.objective not in quest_challenges:
score_pointer = Pointer(self.game, 0xbe0, 0xd20)
else:
score_pointer = Pointer(self.game, 0xbe0, 0xe00)
return score_pointer
def is_challenge_time_up(self):
if self.challenge.mode == "marathon": return False
return time.time() >= self.challenge_end_time
def modify_quest_goals(self, subchallenge: Subchallenge):
difference = None
match subchallenge.objective:
case "Avalanche":
if subchallenge.mode == "value":
difference = MINIQUEST_GOALS[self.game.get_quest_id()] - subchallenge.condition
else:
difference = -1000
Pointer(self.game, 0xbe0, 0xe00).set_value(difference)
case "Balance":
balance_amount = (subchallenge.condition // 2) if subchallenge.mode == "value" else 34710
Pointer(self.game, 0xbe0, 0x3238).set_value(balance_amount) # Set BalanceGoal
case "BuriedTreasure" | "GoldRush" | "Sandstorm" | "WallBlast":
Pointer(self.game, 0xbe0, 0x3238).set_value(1800) # Set time left to 30 minutes
case "Poker" | "PokerLimit" | "PokerHand" | "PokerSkull":
if self.game.get_quest_id() != 1000: # This will never be met - Poker quests only start on the secret Poker mode.
Pointer(self.game, 0xbe0, 0x323c).set_value(100000) # Set PokerGoal to absurdly high number
Pointer(self.game, 0xbe0, 0x395c).set_value(1000) # Change number of hands remaining to 1000
case "TimeBomb" | "MatchBomb":
if subchallenge.mode == "value":
difference = MINIQUEST_GOALS[self.game.get_quest_id()] - subchallenge.condition
else:
difference = -1000
Pointer(self.game, 0xbe0, 0xe00).set_value(difference)
case "Stratamax":
difference = -1000
Pointer(self.game, 0xbe0, 0xe00).set_value(difference)
Pointer(self.game, 0xbe0, 0x3238).set_value(subchallenge.condition)
Pointer(self.game, 0xbe0, 0x323c).set_value(subchallenge.condition)
return difference
def open_challenge(self, challenge: Challenge):
self.challenge = challenge
def process_loop(self):
running = True
self.game = BejeweledThreeProcess() # Fixes worlds strangest bug (pointers dont read if the game isnt found inside of the process)
self.gui_queue.put(QueueItem("game_opened"))
while running:
if self.action_queue.empty(): continue
action: QueueItem = self.action_queue.get()
match action.function:
case "abort":
running = False
case "open":
self.open_challenge(action.arguments[0])
case "start":
self.start()
def start(self):
if self.challenge == None:
raise ValueError("No challenge has been loaded. Use Bejeweled3WorldChampionships().open_challenge()")
if self.challenge.mode == "timed":
self.challenge_end_time = time.time() + self.challenge.time
self.scores = {}
self.subchallenge_loop()
print("Time!")
add_and_display_scores(self.scores)
self.gui_queue.put(QueueItem("challenge_end", self.scores))
def subchallenge_loop(self):
while not (self.challenge.is_over() or self.is_challenge_time_up()):
subchallenge = self.challenge.next()
print(subchallenge)
self.gui_queue.put(QueueItem("subchallenge", subchallenge.get_gui_string(), str(subchallenge)))
if subchallenge.objective in ["ClasLevel", "ZenLevel"]: # Zero-indexed internally, One-indexed externally
subchallenge.condition -= 1
dordle_time = time.time()
self.wait_until_open(subchallenge)
time.sleep(0.5)
if self.challenge.mode == "timed":
self.challenge_end_time += time.time() - dordle_time
self.gui_queue.put(QueueItem("new_end_time", self.challenge_end_time))
print("Go!")
self.fail_type = None
final_score = self.do_subchallenge(subchallenge)
if self.fail_type != None:
self.gui_queue.put(QueueItem("score", final_score, self.fail_type))
else:
self.gui_queue.put(QueueItem("score", final_score))
self.scores[subchallenge.objective] = {"multiplier": subchallenge.multiplier, "score": final_score}
def wait_until_open(self, subchallenge: Subchallenge):
"""
Blocks subchallenges from starting until the right mode is entered.
"""
open_requirements = CHALLENGE_DATA[subchallenge.objective]["requirements"]
if open_requirements["board_types"] == []:
correct_board_type = lambda: self.game.get_board_type() != BoardType.UNKNOWN_BOARD
else:
correct_board_type = lambda: self.game.get_board_type().name in open_requirements["board_types"]
if open_requirements["quest_ids"] == []:
correct_quest_id = lambda: True
else:
correct_quest_id = lambda: self.game.get_quest_id() in open_requirements["quest_ids"]
correct_game_open = lambda: correct_board_type() and correct_quest_id()
initial_value = subchallenge.get_initial_value()
condition_pointer = self.get_condition_pointer(subchallenge)
score_reset = lambda: condition_pointer.get_value() == initial_value
new_game_started = lambda: correct_game_open() and score_reset()
while not new_game_started():
condition_pointer.update_address()
def add_and_display_scores(challenge_dict: dict):
total_score = 0
print("---------------")
for key in challenge_dict:
subchallenge = challenge_dict[key]
print(f"{key}: {subchallenge["score"]:,} * {subchallenge["multiplier"]:,} = {subchallenge["score"] * subchallenge["multiplier"]:,}")
total_score += subchallenge["score"] * subchallenge["multiplier"]
print("---------------")
print(f"TOTAL SCORE: {total_score:,}")
print("---------------")