parser.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 = 8, 50
  7. DETECT_COLUMN_OFFSET_Y = -11
  8. MIN_COLUMN_SAT = 130
  9. MIN_COLUMN_VAL = 120
  10. COLUMN_VSHIFT = [-2, -2, -1, -1, 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 = 45
  20. MIN_BASIC_SAT = 180
  21. MIN_BOMB_SAT = 130
  22. DETECT_EXA_X = 30
  23. DETECT_EXA_Y = 547
  24. EXA_HUE = 129
  25. EXA_MIN_VAL = 240
  26. DETECT_HELD_Y = 579
  27. DETECT_EXA_LIGHT_X = 30
  28. DETECT_EXA_LIGHT_Y = 609
  29. EXA_LIGHT_HUE = 226
  30. MIN_EXA_LIGHT_VAL = 180
  31. def is_hue(h, hexpect):
  32. return abs(h - hexpect) <= HUE_TOLERANCE
  33. def is_basic(block):
  34. return RED <= block <= YELLOW
  35. def is_bomb(block):
  36. return block > NOBLOCK
  37. def bomb_to_basic(block):
  38. assert is_bomb(block)
  39. return block - BOMB_OFFSET
  40. def detect_columns(board):
  41. def saturated(x, y):
  42. h, s, v = board.getpixel((x, y))
  43. return s > MIN_COLUMN_SAT and v > MIN_COLUMN_VAL
  44. a, b = DETECT_COLUMN_OFFSET_X
  45. for y in range(MAX_COLUMN_HEIGHT, BLOCK_SIZE, -1):
  46. for col in range(COLUMNS):
  47. x = col * BLOCK_SIZE
  48. if saturated(x + a, y) and saturated(x + b, y):
  49. #print('found bottom in column', col)
  50. return y + 1 - DETECT_COLUMN_OFFSET_Y + COLUMN_VSHIFT[col]
  51. def detect_block_type(board, x, y):
  52. # check for basic blocks first, use saturation filter to avoid confusing
  53. # green blocks with background
  54. h, s, v = board.getpixel((x + DETECT_BASIC_X,
  55. y + DETECT_BASIC_Y))
  56. if s >= MIN_BASIC_SAT:
  57. for ty, hexpect in enumerate(BASIC_HUES):
  58. if is_hue(h, hexpect):
  59. return ty
  60. # if no basic block is detected, check another pixel for bomb contents
  61. h, s, v = board.getpixel((x + DETECT_BOMB_X,
  62. y + DETECT_BOMB_Y))
  63. if s >= MIN_BOMB_SAT:
  64. for ty, hexpect in enumerate(BOMB_HUES):
  65. if is_hue(h, hexpect):
  66. return ty + BOMB_OFFSET
  67. # no basic block or bomb -> empty slot
  68. return NOBLOCK
  69. def detect_blocks(board):
  70. maxy = detect_columns(board) - BLOCK_SIZE
  71. for y in range(maxy, 0, -BLOCK_SIZE):
  72. for col in range(COLUMNS):
  73. x = col * BLOCK_SIZE
  74. yield detect_block_type(board, x, y + COLUMN_VSHIFT[col])
  75. def detect_exa(board):
  76. for col in range(COLUMNS):
  77. x = col * BLOCK_SIZE + DETECT_EXA_X
  78. y = DETECT_EXA_Y + COLUMN_VSHIFT[col]
  79. h, s, v = board.getpixel((x, y))
  80. if is_hue(h, EXA_HUE) and v >= EXA_MIN_VAL:
  81. return col
  82. def detect_held(board, exa):
  83. if exa is not None:
  84. x = exa * BLOCK_SIZE + DETECT_EXA_LIGHT_X
  85. y = DETECT_EXA_LIGHT_Y + COLUMN_VSHIFT[exa]
  86. h, s, v = board.getpixel((x, y))
  87. if not is_hue(h, EXA_LIGHT_HUE) or v < MIN_EXA_LIGHT_VAL:
  88. return detect_block_type(board, exa * BLOCK_SIZE, DETECT_HELD_Y)
  89. return NOBLOCK
  90. def print_board(blocks, exa, held):
  91. rows = len(blocks) // COLUMNS
  92. for row in range(rows):
  93. row = rows - row
  94. row_blocks = blocks[(row - 1) * COLUMNS:row * COLUMNS]
  95. print(' ' + ''.join('rpgby.RPGBY'[ty] for ty in row_blocks))
  96. if exa is not None:
  97. print(' ' * exa + ' |')
  98. print(' ', '-' * (exa - 1), '(', 'rpgby RPGBY'[held], ')',
  99. '-' * (COLUMNS - exa - 2), sep='')
  100. if __name__ == '__main__':
  101. from interaction import get_exapunks_window, screenshot_board
  102. win = get_exapunks_window()
  103. win.raise_window()
  104. while True:
  105. board = screenshot_board(win)
  106. blocks = list(detect_blocks(board))
  107. exa = detect_exa(board)
  108. held = detect_held(board, exa)
  109. print('\033c', end='')
  110. print_board(blocks, exa, held)
  111. time.sleep(0.05)