Commit 770a09bf authored by Taddeüs Kroes's avatar Taddeüs Kroes

Various cleanup

parent de6182ec
...@@ -26,7 +26,7 @@ def find_window(name): ...@@ -26,7 +26,7 @@ def find_window(name):
if win: if win:
return win return win
return traverse(display.Display().screen().root) return traverse(disp.screen().root)
def get_exapunks_window(): def get_exapunks_window():
...@@ -52,17 +52,18 @@ def screenshot_board(window): ...@@ -52,17 +52,18 @@ def screenshot_board(window):
return im.convert('HSV') return im.convert('HSV')
def press_key(window, key): def press_keys(window, keys):
keysym = XK.string_to_keysym(key) for key in keys:
keycode = disp.keysym_to_keycode(keysym) keysym = XK.string_to_keysym(key)
keycode = disp.keysym_to_keycode(keysym)
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)
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)
if __name__ == '__main__': if __name__ == '__main__':
...@@ -72,17 +73,13 @@ if __name__ == '__main__': ...@@ -72,17 +73,13 @@ if __name__ == '__main__':
old_focus = disp.get_input_focus() old_focus = disp.get_input_focus()
disp.set_input_focus(win, X.RevertToParent, X.CurrentTime) disp.set_input_focus(win, X.RevertToParent, X.CurrentTime)
def press_keys(keys): #press_keys(win, 'aaaj')
for key in keys:
press_key(win, key)
#press_keys('aaaj')
#while True: #while True:
# press_keys('djkj' * 7) # press_keys(win, 'djkj' * 7)
# press_keys('ajkj' * 7) # press_keys(win, 'ajkj' * 7)
while True: while True:
press_key(win, 'adjk'[randint(0, 3)]) press_keys(win, 'adjk'[randint(0, 3)])
disp.set_input_focus(old_focus.focus, X.RevertToParent, X.CurrentTime) disp.set_input_focus(old_focus.focus, X.RevertToParent, X.CurrentTime)
disp.sync() disp.sync()
...@@ -13,16 +13,17 @@ MIN_COLUMN_VAL = 120 ...@@ -13,16 +13,17 @@ MIN_COLUMN_VAL = 120
COLUMN_VSHIFT = [-2, -2, -1, -1, 0, 0, 0] COLUMN_VSHIFT = [-2, -2, -1, -1, 0, 0, 0]
RED, PINK, GREEN, BLUE, YELLOW, NONE = range(6) RED, PINK, GREEN, BLUE, YELLOW, NOBLOCK = range(6)
BOMB_OFFSET = NONE + 1 BOMB_OFFSET = NOBLOCK + 1
BASIC_HUES = [248, 224, 118, 158, 26] BASIC_HUES = [248, 224, 118, 158, 26]
BOMB_HUES = [250, 219, 131, 174, 38] BOMB_HUES = [250, 219, 132, 174, 38]
HUE_TOLERANCE = 5 HUE_TOLERANCE = 5
DETECT_BASIC_X = 9 DETECT_BASIC_X = 9
DETECT_BASIC_Y = 15 DETECT_BASIC_Y = 15
DETECT_BOMB_X = 25 DETECT_BOMB_X = 22
DETECT_BOMB_Y = 44 DETECT_BOMB_Y = 45
MIN_BASIC_SAT = 180 MIN_BASIC_SAT = 180
MIN_BOMB_SAT = 130
DETECT_EXA_X = 30 DETECT_EXA_X = 30
DETECT_EXA_Y = 547 DETECT_EXA_Y = 547
...@@ -39,6 +40,19 @@ def is_hue(h, hexpect): ...@@ -39,6 +40,19 @@ def is_hue(h, hexpect):
return abs(h - hexpect) <= HUE_TOLERANCE return abs(h - hexpect) <= HUE_TOLERANCE
def is_basic(block):
return RED <= block <= YELLOW
def is_bomb(block):
return block > NOBLOCK
def bomb_to_basic(block):
assert is_bomb(block)
return block - BOMB_OFFSET
def detect_columns(board): def detect_columns(board):
def saturated(x, y): def saturated(x, y):
h, s, v = board.getpixel((x, y)) h, s, v = board.getpixel((x, y))
...@@ -55,11 +69,10 @@ def detect_columns(board): ...@@ -55,11 +69,10 @@ def detect_columns(board):
def detect_block_type(board, x, y): def detect_block_type(board, x, y):
h, s, v = board.getpixel((x + DETECT_BASIC_X,
y + DETECT_BASIC_Y))
# check for basic blocks first, use saturation filter to avoid confusing # check for basic blocks first, use saturation filter to avoid confusing
# green blocks with background # green blocks with background
h, s, v = board.getpixel((x + DETECT_BASIC_X,
y + DETECT_BASIC_Y))
if s >= MIN_BASIC_SAT: if s >= MIN_BASIC_SAT:
for ty, hexpect in enumerate(BASIC_HUES): for ty, hexpect in enumerate(BASIC_HUES):
if is_hue(h, hexpect): if is_hue(h, hexpect):
...@@ -68,12 +81,13 @@ def detect_block_type(board, x, y): ...@@ -68,12 +81,13 @@ def detect_block_type(board, x, y):
# if no basic block is detected, check another pixel for bomb contents # if no basic block is detected, check another pixel for bomb contents
h, s, v = board.getpixel((x + DETECT_BOMB_X, h, s, v = board.getpixel((x + DETECT_BOMB_X,
y + DETECT_BOMB_Y)) y + DETECT_BOMB_Y))
for ty, hexpect in enumerate(BOMB_HUES): if s >= MIN_BOMB_SAT:
if is_hue(h, hexpect): for ty, hexpect in enumerate(BOMB_HUES):
return ty + BOMB_OFFSET if is_hue(h, hexpect):
return ty + BOMB_OFFSET
# no basic block or bomb -> empty slot # no basic block or bomb -> empty slot
return NONE return NOBLOCK
def detect_blocks(board): def detect_blocks(board):
...@@ -101,7 +115,7 @@ def detect_held(board, exa): ...@@ -101,7 +115,7 @@ def detect_held(board, exa):
if not is_hue(h, EXA_LIGHT_HUE) or v < MIN_EXA_LIGHT_VAL: if not is_hue(h, EXA_LIGHT_HUE) or v < MIN_EXA_LIGHT_VAL:
return detect_block_type(board, exa * BLOCK_SIZE, DETECT_HELD_Y) return detect_block_type(board, exa * BLOCK_SIZE, DETECT_HELD_Y)
return NONE return NOBLOCK
def print_board(blocks, exa, held): def print_board(blocks, exa, held):
...@@ -113,12 +127,12 @@ def print_board(blocks, exa, held): ...@@ -113,12 +127,12 @@ def print_board(blocks, exa, held):
if exa is not None: if exa is not None:
print(' ' * exa + ' |') print(' ' * exa + ' |')
print('-' * exa, '(', 'rpgby RPGBY'[held], ')', print(' ', '-' * (exa - 1), '(', 'rpgby RPGBY'[held], ')',
'-' * (COLUMNS - exa - 1), sep='') '-' * (COLUMNS - exa - 2), sep='')
if __name__ == '__main__': if __name__ == '__main__':
from iteraction import get_exapunks_window, screenshot_board from interaction import get_exapunks_window, screenshot_board
win = get_exapunks_window() win = get_exapunks_window()
win.raise_window() win.raise_window()
......
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