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
import os
import time
from collections import deque
from itertools import count
from strategy import State, moves_to_keys
from interaction import get_exapunks_window, focus_window, \
screenshot_board, press_keys, listen_keys
from Xlib import error
from strategy import State
from interaction import get_exapunks_window, focus_window, screenshot_board, \
press_keys, listen_keys, KEY_DELAY
MAX_SPEED_ROWS = 3
def save_screenshot(win):
......@@ -19,46 +24,63 @@ def save_screenshot(win):
if __name__ == '__main__':
try:
win = get_exapunks_window()
focus_window(win)
listen_keys({'s': lambda: save_screenshot(win)})
prev_score = None
solutions = deque([], maxlen=3)
while True:
try:
board = screenshot_board(win)
try:
state = State.detect(board)
except (TypeError, AssertionError):
print('error during parsing, wait for a bit')
time.sleep(.1)
continue
print('\033c', end='')
print('parsed:')
state.print()
print()
start = time.time()
moves = state.solve()
solution = state.solve()
end = time.time()
print('thought for %.4f seconds' % (end - start))
if moves:
print('moves:', moves_to_keys(moves))
points, newstate = state.simulate(moves)
score = newstate.score(points, moves, state)
print(' score:', score)
print('prev score:', prev_score)
print('thought for', round((end - start) * 1000, 1), 'milliseconds')
except (TypeError, AssertionError):
print('\rerror during parsing, wait for a bit...', end='')
time.sleep(0.05)
continue
except error.BadMatch:
print('\rEXAPUNKS window lost, wait for a bit...', end='')
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()
prev_score = score
print('target after moves:')
newstate.print()
print()
solution.newstate.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:
print('no moves')
solutions.append(solution)
except KeyboardInterrupt:
print('interrupted, quitting')
......@@ -10,7 +10,7 @@ BOARD_X = 367
BOARD_Y = 129
BOARD_WIDTH = 420
BOARD_HEIGHT = 638
KEY_DELAY = 0.015
KEY_DELAY = 14 # milliseconds
disp = display.Display()
......@@ -60,11 +60,11 @@ def press_keys(window, keys):
ext.xtest.fake_input(disp, X.KeyPress, keycode)
disp.sync()
time.sleep(KEY_DELAY)
time.sleep(KEY_DELAY / 1000)
ext.xtest.fake_input(disp, X.KeyRelease, keycode)
disp.sync()
time.sleep(KEY_DELAY)
time.sleep(KEY_DELAY / 1000)
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