forked from tanjeffreyz/auto-maple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponents.py
366 lines (288 loc) · 11.1 KB
/
components.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
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
"""A collection of classes used to execute a Routine."""
import config
import settings
import utils
import math
import time
from vkeys import key_down, key_up, press
#################################
# Routine Components #
#################################
class Component:
id = 'Routine Component'
PRIMITIVES = {int, str, bool, float}
def __init__(self, *args, **kwargs):
if len(args) > 1:
raise TypeError('Component superclass __init__ only accepts 1 (optional) argument: LOCALS.')
if len(kwargs) != 0:
raise TypeError('Component superclass __init__ does not accept any keyword arguments.')
if len(args) == 0:
self.kwargs = {}
elif type(args[0]) != dict:
raise TypeError("Component superclass __init__ only accepts arguments of type 'dict'.")
else:
self.kwargs = args[0].copy()
self.kwargs.pop('__class__')
self.kwargs.pop('self')
@utils.run_if_enabled
def execute(self):
self.main()
def main(self):
pass
def update(self, *args, **kwargs):
"""Updates this Component's constructor arguments with new arguments."""
self.__class__(*args, **kwargs) # Validate arguments before actually updating values
self.__init__(*args, **kwargs)
def info(self):
"""Returns a dictionary of useful information about this Component."""
return {
'name': self.__class__.__name__,
'vars': self.kwargs.copy()
}
def encode(self):
"""Encodes an object using its ID and its __init__ arguments."""
arr = [self.id]
for key, value in self.kwargs.items():
if key != 'id' and type(self.kwargs[key]) in Component.PRIMITIVES:
arr.append(f'{key}={value}')
return ', '.join(arr)
class Point(Component):
"""Represents a location in a user-defined routine."""
id = '*'
def __init__(self, x, y, frequency=1, skip='False', adjust='False'):
super().__init__(locals())
self.x = float(x)
self.y = float(y)
self.location = (self.x, self.y)
self.frequency = settings.validate_nonnegative_int(frequency)
self.counter = int(settings.validate_boolean(skip))
self.adjust = settings.validate_boolean(adjust)
if not hasattr(self, 'commands'): # Updating Point should not clear commands
self.commands = []
def main(self):
"""Executes the set of actions associated with this Point."""
if self.counter == 0:
move = config.bot.command_book['move']
move(*self.location).execute()
if self.adjust:
adjust = config.bot.command_book.get('adjust') # TODO: adjust using step('up')?
adjust(*self.location).execute()
for command in self.commands:
command.execute()
self._increment_counter()
@utils.run_if_enabled
def _increment_counter(self):
"""Increments this Point's counter, wrapping back to 0 at the upper bound."""
self.counter = (self.counter + 1) % self.frequency
def info(self):
curr = super().info()
curr['vars'].pop('location', None)
curr['vars']['commands'] = ', '.join([c.id for c in self.commands])
return curr
def __str__(self):
return f' * {self.location}'
class Label(Component):
id = '@'
def __init__(self, label):
super().__init__(locals())
self.label = str(label)
if self.label in config.routine.labels:
raise ValueError
self.links = set()
self.index = None
def set_index(self, i):
self.index = i
def encode(self):
return '\n' + super().encode()
def info(self):
curr = super().info()
curr['vars']['index'] = self.index
return curr
def __delete__(self, instance):
del self.links
config.routine.labels.pop(self.label)
def __str__(self):
return f'{self.label}:'
class Jump(Component):
"""Jumps to the given Label."""
id = '>'
def __init__(self, label, frequency=1, skip='False'):
super().__init__(locals())
self.label = str(label)
self.frequency = settings.validate_nonnegative_int(frequency)
self.counter = int(settings.validate_boolean(skip))
self.link = None
def main(self):
if self.link is None:
print(f"\n[!] Label '{self.label}' does not exist.")
else:
if self.counter == 0:
config.routine.index = self.link.index
self._increment_counter()
@utils.run_if_enabled
def _increment_counter(self):
self.counter = (self.counter + 1) % self.frequency
def bind(self):
"""
Binds this Goto to its corresponding Label. If the Label's index changes, this Goto
instance will automatically be able to access the updated value.
:return: Whether the binding was successful
"""
if self.label in config.routine.labels:
self.link = config.routine.labels[self.label]
self.link.links.add(self)
return True
return False
def __delete__(self, instance):
if self.link is not None:
self.link.links.remove(self)
def __str__(self):
return f' > {self.label}'
class Setting(Component):
"""Changes the value of the given setting variable."""
id = '$'
def __init__(self, target, value):
super().__init__(locals())
self.key = str(target)
if self.key not in settings.SETTING_VALIDATORS:
raise ValueError(f"Setting '{target}' does not exist")
self.value = settings.SETTING_VALIDATORS[self.key](value)
def main(self):
setattr(settings, self.key, self.value)
def __str__(self):
return f' $ {self.key} = {self.value}'
SYMBOLS = {
'*': Point,
'@': Label,
'>': Jump,
'$': Setting
}
#############################
# Shared Commands #
#############################
class Command(Component):
id = 'Command Superclass'
def __init__(self, *args):
super().__init__(*args)
self.id = self.__class__.__name__
def __str__(self):
variables = self.__dict__
result = ' ' + self.id
if len(variables) - 1 > 0:
result += ':'
for key, value in variables.items():
if key != 'id':
result += f'\n {key}={value}'
return result
class Move(Command):
"""Moves to a given position using the shortest path based on the current Layout."""
def __init__(self, x, y, max_steps=15):
super().__init__(locals())
self.target = (float(x), float(y))
self.max_steps = settings.validate_nonnegative_int(max_steps)
self.prev_direction = ''
def _new_direction(self, new):
key_down(new)
if self.prev_direction and self.prev_direction != new:
key_up(self.prev_direction)
self.prev_direction = new
def main(self):
counter = self.max_steps
path = config.layout.shortest_path(config.player_pos, self.target)
for i, point in enumerate(path):
toggle = True
self.prev_direction = ''
local_error = utils.distance(config.player_pos, point)
global_error = utils.distance(config.player_pos, self.target)
while config.enabled and counter > 0 and \
local_error > settings.move_tolerance and \
global_error > settings.move_tolerance:
if toggle:
d_x = point[0] - config.player_pos[0]
if abs(d_x) > settings.move_tolerance / math.sqrt(2):
if d_x < 0:
key = 'left'
else:
key = 'right'
self._new_direction(key)
step(key, point)
counter -= 1
if i < len(path) - 1:
time.sleep(0.15)
else:
d_y = point[1] - config.player_pos[1]
if abs(d_y) > settings.move_tolerance / math.sqrt(2):
if d_y < 0:
key = 'up'
else:
key = 'down'
self._new_direction(key)
step(key, point)
counter -= 1
if i < len(path) - 1:
time.sleep(0.05)
local_error = utils.distance(config.player_pos, point)
global_error = utils.distance(config.player_pos, self.target)
toggle = not toggle
if self.prev_direction:
key_up(self.prev_direction)
class Adjust(Command):
"""Fine-tunes player position using small movements."""
def __init__(self, x, y, max_steps=5):
super().__init__(locals())
self.target = (float(x), float(y))
self.max_steps = settings.validate_nonnegative_int(max_steps)
def step(direction, target):
"""
The default 'step' function. If not overridden, immediately stops the bot.
:param direction: The direction in which to move.
:param target: The target location to step towards.
:return: None
"""
print("\n[!] Function 'step' not implemented in current command book, aborting process.")
config.enabled = False
class Wait(Command):
"""Waits for a set amount of time."""
def __init__(self, duration):
super().__init__(locals())
self.duration = float(duration)
def main(self):
time.sleep(self.duration)
class Walk(Command):
"""Walks in the given direction for a set amount of time."""
def __init__(self, direction, duration):
super().__init__(locals())
self.direction = settings.validate_horizontal_arrows(direction)
self.duration = float(duration)
def main(self):
key_down(self.direction)
time.sleep(self.duration)
key_up(self.direction)
time.sleep(0.05)
class Fall(Command):
"""
Performs a down-jump and then free-falls until the player exceeds a given distance
from their starting position.
"""
def __init__(self, distance=settings.move_tolerance / 2):
super().__init__(locals())
self.distance = float(distance)
def main(self):
start = config.player_pos
key_down('down')
time.sleep(0.05)
if config.stage_fright and utils.bernoulli(0.5):
time.sleep(utils.rand_float(0.2, 0.4))
counter = 6
while config.enabled and \
counter > 0 and \
utils.distance(start, config.player_pos) < self.distance:
press('space', 1, down_time=0.1)
counter -= 1
key_up('down')
time.sleep(0.05)
class Buff(Command):
"""Undefined 'buff' command for the default command book."""
def main(self):
print("\n[!] 'Buff' command not implemented in current command book, aborting process.")
config.enabled = False