parse.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env python3
  2. import time
  3. COLUMNS = 7
  4. BLOCK_SIZE = 60
  5. MAX_COLUMN_HEIGHT = 546
  6. DETECT_COLUMN_OFFSET_X = 9, 49
  7. DETECT_COLUMN_OFFSET_Y = 11
  8. MIN_COLUMN_SAT = 130
  9. MIN_COLUMN_VAL = 120
  10. COLUMN_VSHIFT = [1, 1, 1, 0, 0, 0, 0]
  11. RED, PINK, GREEN, BLUE, YELLOW, NOBLOCK = range(6)
  12. BOMB_OFFSET = NOBLOCK + 1
  13. BASIC_HUES = [248, 224, 118, 158, 26]
  14. BOMB_HUES = [250, 219, 132, 174, 38]
  15. HUE_TOLERANCE = 5
  16. DETECT_BASIC_X = 9
  17. DETECT_BASIC_Y = 15
  18. DETECT_BOMB_X = 22
  19. DETECT_BOMB_Y = 43
  20. DETECT_BOMB_Y_TOLERANCE = 7
  21. DETECT_BOMB_CHECK = 8, 20, 137, 185, 9 # x, y, hue, min_value, y_tolerance
  22. MIN_BASIC_SAT = 180
  23. MIN_BOMB_SAT = 130
  24. DETECT_EXA_X = 30
  25. DETECT_EXA_Y = 547
  26. EXA_HUE = 129
  27. EXA_MIN_VAL = 194
  28. DETECT_HELD_Y = 579
  29. DETECT_EXA_LIGHT_X = 30
  30. DETECT_EXA_LIGHT_Y = 609
  31. EXA_LIGHT_HUE = 226
  32. MIN_EXA_LIGHT_VAL = 172
  33. def is_hue(h, hexpect):
  34. return abs(h - hexpect) <= HUE_TOLERANCE
  35. def is_basic(block):
  36. return RED <= block <= YELLOW
  37. def is_bomb(block):
  38. return block > NOBLOCK
  39. def bomb_to_basic(block):
  40. assert is_bomb(block)
  41. return block - BOMB_OFFSET
  42. def detect_columns(board):
  43. def saturated(x, y):
  44. h, s, v = board.getpixel((x, y))
  45. return s > MIN_COLUMN_SAT and v > MIN_COLUMN_VAL
  46. miny = -min(DETECT_BASIC_Y, DETECT_BOMB_Y)
  47. for y in range(MAX_COLUMN_HEIGHT, miny + BLOCK_SIZE, -1):
  48. for col in range(COLUMNS):
  49. x = col * BLOCK_SIZE
  50. if all(saturated(x + cx, y) for cx in DETECT_COLUMN_OFFSET_X):
  51. #print('found bottom in column', col)
  52. return y + 1 + DETECT_COLUMN_OFFSET_Y - COLUMN_VSHIFT[col]
  53. def detect_block_type(board, x, y):
  54. # check for basic blocks first, use saturation filter to avoid confusing
  55. # green blocks with background
  56. h, s, v = board.getpixel((x + DETECT_BASIC_X,
  57. y + DETECT_BASIC_Y))
  58. if s >= MIN_BASIC_SAT:
  59. for ty, hexpect in enumerate(BASIC_HUES):
  60. if is_hue(h, hexpect):
  61. return ty
  62. # if no basic block is detected, check another range of pixels for bomb
  63. # contents
  64. def check_bomb():
  65. dx, dy, hexpect, minval, tolerance = DETECT_BOMB_CHECK
  66. for offset in range(tolerance):
  67. h, s, v = board.getpixel((x + dx, y + dy + offset))
  68. if is_hue(h, hexpect) and v >= minval:
  69. return True
  70. return False
  71. if check_bomb():
  72. for offset in range(DETECT_BOMB_Y_TOLERANCE):
  73. h, s, v = board.getpixel((x + DETECT_BOMB_X,
  74. y + DETECT_BOMB_Y + offset))
  75. if s >= MIN_BOMB_SAT:
  76. for basic_ty, hexpect in enumerate(BOMB_HUES):
  77. if is_hue(h, hexpect):
  78. return basic_ty + BOMB_OFFSET
  79. # no basic block or bomb -> empty slot
  80. return NOBLOCK
  81. def detect_blocks(board):
  82. maxy = detect_columns(board) - BLOCK_SIZE
  83. miny = -min(DETECT_BASIC_Y, DETECT_BOMB_Y)
  84. for y in range(maxy, miny, -BLOCK_SIZE):
  85. for col in range(COLUMNS):
  86. x = col * BLOCK_SIZE
  87. yield detect_block_type(board, x, y + COLUMN_VSHIFT[col])
  88. def detect_exa(board):
  89. for col in range(COLUMNS):
  90. x = col * BLOCK_SIZE + DETECT_EXA_X
  91. y = DETECT_EXA_Y + COLUMN_VSHIFT[col]
  92. h, s, v = board.getpixel((x, y))
  93. if is_hue(h, EXA_HUE) and v >= EXA_MIN_VAL:
  94. return col
  95. def detect_held(board, exa):
  96. if exa is not None:
  97. x = exa * BLOCK_SIZE + DETECT_EXA_LIGHT_X
  98. y = DETECT_EXA_LIGHT_Y + COLUMN_VSHIFT[exa]
  99. h, s, v = board.getpixel((x, y))
  100. if not is_hue(h, EXA_LIGHT_HUE) or v < MIN_EXA_LIGHT_VAL:
  101. return detect_block_type(board, exa * BLOCK_SIZE, DETECT_HELD_Y)
  102. return NOBLOCK
  103. def print_board(blocks, exa, held):
  104. rows = len(blocks) // COLUMNS
  105. for row in range(rows):
  106. row = rows - row
  107. row_blocks = blocks[(row - 1) * COLUMNS:row * COLUMNS]
  108. print(' ' + ''.join('rpgby.RPGBY'[ty] for ty in row_blocks))
  109. if exa is not None:
  110. print(' ' * exa + ' |')
  111. print(' ' * int(exa > 0), '-' * (exa - 1),
  112. '(', 'rpgby RPGBY'[held], ')',
  113. '-' * (COLUMNS - exa - 2), sep='')
  114. if __name__ == '__main__':
  115. from interaction import get_exapunks_window, screenshot_board
  116. win = get_exapunks_window()
  117. win.raise_window()
  118. while True:
  119. board = screenshot_board(win)
  120. blocks = list(detect_blocks(board))
  121. exa = detect_exa(board)
  122. held = detect_held(board, exa)
  123. print('\033c', end='')
  124. print_board(blocks, exa, held)
  125. time.sleep(0.05)