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__':
try:
win = get_exapunks_window() win = get_exapunks_window()
focus_window(win) 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: while True:
try:
board = screenshot_board(win) board = screenshot_board(win)
try:
state = State.detect(board) state = State.detect(board)
except (TypeError, AssertionError):
print('error during parsing, wait for a bit')
time.sleep(.1)
continue
print('\033c', end='') print('\033c', end='')
print('parsed:') print('parsed:')
state.print() state.print()
print() print()
start = time.time() start = time.time()
moves = state.solve() solution = state.solve()
end = time.time() end = time.time()
print('thought for %.4f seconds' % (end - start)) print('thought for', round((end - start) * 1000, 1), 'milliseconds')
except (TypeError, AssertionError):
if moves: print('\rerror during parsing, wait for a bit...', end='')
print('moves:', moves_to_keys(moves)) time.sleep(0.05)
points, newstate = state.simulate(moves) continue
score = newstate.score(points, moves, state) except error.BadMatch:
print(' score:', score) print('\rEXAPUNKS window lost, wait for a bit...', end='')
print('prev score:', prev_score) time.sleep(0.5)
continue
if len(solutions) == 3 and solution.loops(solutions.popleft()):
print('\rloop detected, wait for a bit...', end='')
time.sleep(0.03)
elif solution.moves:
print('moves:', solution.keys())
print(' score:', solution.score)
if solutions:
print('prev score:', solutions[-1].score)
print() print()
prev_score = score
print('target after moves:') print('target after moves:')
newstate.print() solution.newstate.print()
print()
press_keys(win, solution.keys())
press_keys(win, moves_to_keys(moves)) 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: else:
print('no moves') 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