Kyrluckechuck / TFT-Bot

Automate Teamfight Tactics (TFT) for token/mission farming. Updated for Set 8.5!
GNU Affero General Public License v3.0
50 stars 7 forks source link

Estimate Unit Positions #174

Closed akshualy closed 1 year ago

akshualy commented 1 year ago

Maths

Description

Adds a new screen helper method to get the approximate position of units on the board. This is the first step in adding item logic. I haven't done more yet since I think it's better to PR this step-by-step.

I've done my best to show what the steps in OpenCV are doing: showcase In text-form:

  1. Take a screenshot and convert the colors to an HSV range we can work with
  2. Remove everything except certain green colors in the image (as a mask, that's why black&white)
  3. Remove noise (small / random pixels)
  4. Expand the left-over pixels
  5. Blur the pixels so they get merged together
  6. Group them as a rectangle

Based on those groups, the actual Y position on the board is calculated. This is what the debug line should look like:

2023-05-27 02:36:22.160 | DEBUG    | __main__:main_game_loop:540 - Board positions: [Coordinates(position_x=1373, position_y=672), Coordinates(position_x=1236, position_y=672), Coordinates(position_x=873, position_y=596), Coordinates(position_x=969, position_y=515), Coordinates(position_x=846, position_y=515), Coordinates(position_x=1267, position_y=446), Coordinates(position_x=1149, position_y=446), Coordinates(position_x=1028, position_y=446)]

Visualized, the blue box is a 50x50 rectangle around each coordinate coordinates

I have the math ready to center the box on the x-axis of the actual "square" of the board as well, but it's good enough like that I think, so we can skip calculating it.

Notes

We shouldn't need to consider offsets here, since it's raw pixel coordinates in the screenshot. However, we do need to consider window offsets once we want to move the mouse to the coordinates.

Additional commits: