Commit 3975c688 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Tweak strategy parameters, add delay on each move to avoid inaccurate state...

Tweak strategy parameters, add delay on each move to avoid inaccurate state parsing, refactor some code
parent ed7e949a
#!/usr/bin/env python3 #!/usr/bin/env python3
import os import os
import time import time
from collections import deque
from itertools import count from itertools import count
from strategy import State, moves_to_keys from Xlib import error
from interaction import get_exapunks_window, focus_window, \ from strategy import State
screenshot_board, press_keys, listen_keys from interaction import get_exapunks_window, focus_window, screenshot_board, \
press_keys, listen_keys, KEY_DELAY
MAX_SPEED_ROWS = 3
def save_screenshot(win): def save_screenshot(win):
...@@ -19,46 +24,63 @@ def save_screenshot(win): ...@@ -19,46 +24,63 @@ def save_screenshot(win):
if __name__ == '__main__': if __name__ == '__main__':
win = get_exapunks_window() try:
focus_window(win) win = get_exapunks_window()
focus_window(win)
listen_keys({'s': lambda: save_screenshot(win)})
listen_keys({'s': lambda: save_screenshot(win)})
prev_score = None
solutions = deque([], maxlen=3)
while True:
board = screenshot_board(win) while True:
try:
try: board = screenshot_board(win)
state = State.detect(board)
except (TypeError, AssertionError): state = State.detect(board)
print('error during parsing, wait for a bit') print('\033c', end='')
time.sleep(.1) print('parsed:')
continue state.print()
print()
print('\033c', end='')
print('parsed:') start = time.time()
state.print() solution = state.solve()
print() end = time.time()
print('thought for', round((end - start) * 1000, 1), 'milliseconds')
start = time.time() except (TypeError, AssertionError):
moves = state.solve() print('\rerror during parsing, wait for a bit...', end='')
end = time.time() time.sleep(0.05)
print('thought for %.4f seconds' % (end - start)) continue
except error.BadMatch:
if moves: print('\rEXAPUNKS window lost, wait for a bit...', end='')
print('moves:', moves_to_keys(moves)) time.sleep(0.5)
points, newstate = state.simulate(moves) continue
score = newstate.score(points, moves, state)
print(' score:', score) if len(solutions) == 3 and solution.loops(solutions.popleft()):
print('prev score:', prev_score) print('\rloop detected, wait for a bit...', end='')
print() time.sleep(0.03)
prev_score = score elif solution.moves:
print('moves:', solution.keys())
print('target after moves:') print(' score:', solution.score)
newstate.print() if solutions:
print() print('prev score:', solutions[-1].score)
print()
press_keys(win, moves_to_keys(moves))
else: print('target after moves:')
print('no moves') solution.newstate.print()
press_keys(win, solution.keys())
keys_delay = len(solution.moves) * 2 * KEY_DELAY
moves_delay = max(0, solution.delay() - keys_delay)
print('wait for', moves_delay, 'ms')
time.sleep(moves_delay / 1000)
elif state.nrows() - 2 <= MAX_SPEED_ROWS:
print('no moves, speed up')
press_keys(win, 'l')
time.sleep(0.03)
else:
print('no moves')
solutions.append(solution)
except KeyboardInterrupt:
print('interrupted, quitting')
...@@ -10,7 +10,7 @@ BOARD_X = 367 ...@@ -10,7 +10,7 @@ BOARD_X = 367
BOARD_Y = 129 BOARD_Y = 129
BOARD_WIDTH = 420 BOARD_WIDTH = 420
BOARD_HEIGHT = 638 BOARD_HEIGHT = 638
KEY_DELAY = 0.015 KEY_DELAY = 14 # milliseconds
disp = display.Display() disp = display.Display()
...@@ -60,11 +60,11 @@ def press_keys(window, keys): ...@@ -60,11 +60,11 @@ def press_keys(window, keys):
ext.xtest.fake_input(disp, X.KeyPress, keycode) ext.xtest.fake_input(disp, X.KeyPress, keycode)
disp.sync() disp.sync()
time.sleep(KEY_DELAY) time.sleep(KEY_DELAY / 1000)
ext.xtest.fake_input(disp, X.KeyRelease, keycode) ext.xtest.fake_input(disp, X.KeyRelease, keycode)
disp.sync() disp.sync()
time.sleep(KEY_DELAY) time.sleep(KEY_DELAY / 1000)
def listen_keys(handlers): def listen_keys(handlers):
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment