parse.py 4.0 KB

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