-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
359 lines (284 loc) · 12.4 KB
/
main.py
File metadata and controls
359 lines (284 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import time
from PIL import ImageGrab, Image
import cv2
import numpy as np
import keyboard
# Add this import at the top after building the Rust module
try:
import tetris_bot_rust
USE_RUST = True
print("Rust module available, using Rust implementation")
except ImportError:
USE_RUST = False
print("Rust module not available, using Python implementation")
class TetrisController:
def __init__(self):
# Define key mappings
self.keys = {
'left': 'a',
'right': 'd',
'rotate_cw': ';',
'rotate_ccw': 'k',
'rotate_180': 'o',
'soft_drop': 's',
'hard_drop': 'space',
'hold': 'w',
}
# Timing settings
self.move_delay = 0.04
self.rotation_delay = 0.08
self.drop_delay = 0.15
def execute_commands(self, commands):
"""Execute a list of input commands from Rust"""
if not commands:
return False
try:
for command in commands:
action_map = {
'left': 'left',
'right': 'right',
'rotate_cw': 'rotate_cw',
'rotate_ccw': 'rotate_ccw',
'rotate_180': 'rotate_180',
'drop': 'hard_drop',
'hold': 'hold'
}
if command.action in action_map:
key_name = action_map[command.action]
key = self.keys[key_name]
for _ in range(command.count):
keyboard.press_and_release(key)
if command.action in ['rotate_cw', 'rotate_ccw', 'rotate_180']:
time.sleep(self.rotation_delay)
elif command.action in ['left', 'right']:
time.sleep(self.move_delay)
elif command.action == 'drop':
time.sleep(self.drop_delay)
return True
except Exception as e:
print(f"Error executing commands: {e}")
return False
def hold_piece(self):
"""Hold the current piece"""
try:
keyboard.press_and_release(self.keys['hold'])
time.sleep(self.drop_delay)
return True
except Exception as e:
print(f"Error holding piece: {e}")
return False
def find_next_optimized(search_area, template_path, threshold=0.7):
"""Optimized template matching with 50% resolution for speed"""
x, y, width, height = search_area
screenshot = ImageGrab.grab(bbox=(x, y, x + width, y + height))
# Resize to 50% for faster processing
small_screenshot = screenshot.resize((width//2, height//2))
screenshot_cv = cv2.cvtColor(np.array(small_screenshot), cv2.COLOR_RGB2BGR)
try:
template = cv2.imread(template_path, cv2.IMREAD_COLOR)
if template is None:
return None
# Also resize template
template_height, template_width = template.shape[:2]
small_template = cv2.resize(template, (template_width//2, template_height//2))
except Exception:
return None
result = cv2.matchTemplate(screenshot_cv, small_template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
if max_val >= threshold:
# Scale back up to original coordinates
match_x = x + (max_loc[0] * 2)
match_y = y + (max_loc[1] * 2)
return (match_x, match_y, template_width, template_height)
else:
return None
def identify_piece_by_color_fast(pixel_color, tetris_colors):
"""Fast piece identification"""
if hasattr(pixel_color[0], 'item'):
pixel_color = tuple(int(c.item()) for c in pixel_color)
else:
pixel_color = tuple(int(c) for c in pixel_color)
best_match = None
min_distance = float('inf')
for piece_name, piece_color in tetris_colors.items():
distance = sum((a - b) ** 2 for a, b in zip(pixel_color, piece_color))
if distance < min_distance:
min_distance = distance
best_match = piece_name
return best_match
def identify_piece_by_color_robust(pixel_color, tetris_colors, min_confidence=50):
"""More robust piece identification with confidence threshold"""
if hasattr(pixel_color[0], 'item'):
pixel_color = tuple(int(c.item()) for c in pixel_color)
else:
pixel_color = tuple(int(c) for c in pixel_color)
best_match = None
min_distance = float('inf')
second_best_distance = float('inf')
for piece_name, piece_color in tetris_colors.items():
# Use Euclidean distance in RGB space
distance = ((pixel_color[0] - piece_color[0])**2 +
(pixel_color[1] - piece_color[1])**2 +
(pixel_color[2] - piece_color[2])**2) ** 0.5
if distance < min_distance:
second_best_distance = min_distance
min_distance = distance
best_match = piece_name
elif distance < second_best_distance:
second_best_distance = distance
# Check confidence - if two colors are too similar, return None
confidence = second_best_distance - min_distance
if confidence < min_confidence:
print(f"Low confidence detection: {pixel_color} -> {best_match} (conf: {confidence:.1f})")
return None
return best_match
def get_game_pieces_ultra_optimized(search_area, template_path, piece_pixel_offsets, current_piece_offset):
"""Ultra-optimized: single screenshot for all piece detection"""
next_coords = find_next_optimized(search_area, template_path, threshold=0.7)
if not next_coords:
return None, []
text_x, text_y, text_width, text_height = next_coords
# Calculate all pixel positions
current_pixel_x = text_x + current_piece_offset[0]
current_pixel_y = text_y + current_piece_offset[1]
piece_positions = []
for x_offset, y_offset in piece_pixel_offsets:
pixel_x = text_x + x_offset
pixel_y = text_y + text_height + y_offset
piece_positions.append((pixel_x, pixel_y))
# Find bounding box for all pixels
all_x_coords = [current_pixel_x] + [pos[0] for pos in piece_positions]
all_y_coords = [current_pixel_y] + [pos[1] for pos in piece_positions]
min_x = min(all_x_coords)
max_x = max(all_x_coords)
min_y = min(all_y_coords)
max_y = max(all_y_coords)
# Take ONE screenshot covering all needed pixels
screenshot = ImageGrab.grab(bbox=(min_x, min_y, max_x + 1, max_y + 1))
# Color definitions
tetris_colors = {
'I': (49, 178, 130),
'O': (179, 153, 49),
'T': (207, 60, 193),
'S': (131, 179, 50),
'Z': (179, 52, 59),
'J': (78, 61, 164),
'L': (180, 99, 50)
}
# Extract current piece color
current_rel_x = current_pixel_x - min_x
current_rel_y = current_pixel_y - min_y
current_color = screenshot.getpixel((current_rel_x, current_rel_y))
current_piece = identify_piece_by_color_fast(current_color, tetris_colors)
# Extract next pieces colors
next_pieces = []
for pixel_x, pixel_y in piece_positions:
rel_x = pixel_x - min_x
rel_y = pixel_y - min_y
color = screenshot.getpixel((rel_x, rel_y))
piece = identify_piece_by_color_fast(color, tetris_colors)
next_pieces.append(piece)
return current_piece, next_pieces
def scan_single_piece(template_path, search_area, piece_pixel_offset):
"""Scan just one piece position"""
next_coords = find_next_optimized(search_area, template_path, threshold=0.7)
if not next_coords:
return None
text_x, text_y, text_width, text_height = next_coords
# Calculate pixel position for the piece
pixel_x = text_x + piece_pixel_offset[0]
pixel_y = text_y + text_height + piece_pixel_offset[1]
# Take screenshot of just this one pixel
screenshot = ImageGrab.grab(bbox=(pixel_x, pixel_y, pixel_x + 1, pixel_y + 1))
# Color definitions
tetris_colors = {
'I': (49, 178, 130),
'O': (179, 153, 49),
'T': (207, 60, 193),
'S': (131, 179, 50),
'Z': (179, 52, 59),
'J': (78, 61, 164),
'L': (180, 99, 50)
}
color = screenshot.getpixel((0, 0))
piece = identify_piece_by_color_fast(color, tetris_colors)
return piece
def main_game_loop():
"""Main game loop with optimized piece tracking"""
# Configuration
SEARCH_AREA = (0, 0, 1920, 1080)
TEMPLATE_PATH = "assets/next_template.png"
CURRENT_PIECE_OFFSET = (-225, -60)
PIECE_PIXEL_OFFSETS = [
(104, 60), (104, 165), (104, 270), (104, 375), (104, 480)
]
# Last piece in queue (5th position) - this is what we'll scan each turn
LAST_QUEUE_POSITION = PIECE_PIXEL_OFFSETS[4] # (104, 480)
if not USE_RUST:
print("Rust module not available - cannot run game loop")
return
# Initialize components
controller = TetrisController()
tetris_bot_rust.initialize_game_board()
print("TETRIS BOT ACTIVE - Starting in 3 seconds...")
time.sleep(3)
try:
# INITIAL SCAN: Get current piece + full queue
print("Scanning initial pieces...")
current_piece, next_pieces = get_game_pieces_ultra_optimized(
SEARCH_AREA, TEMPLATE_PATH, PIECE_PIXEL_OFFSETS, CURRENT_PIECE_OFFSET
)
if not current_piece or not next_pieces:
print("Could not detect initial pieces!")
return
print(f"Initial current piece: {current_piece}")
print(f"Initial queue: {next_pieces}")
# Initialize the game board with all detected pieces
tetris_bot_rust.update_game_pieces(current_piece, None, next_pieces)
print("Starting game loop...")
moves_executed = 0
current_playing_piece = current_piece
while True:
try:
if current_playing_piece:
print(f"Playing piece: {current_playing_piece}")
# Get optimal move
result = tetris_bot_rust.get_optimal_move_with_lookahead(current_playing_piece)
if result:
best_move, commands = result
# Execute commands
if controller.execute_commands(commands):
tetris_bot_rust.execute_move_on_board(best_move)
moves_executed += 1
# ADVANCE QUEUE: Get next piece from queue
current_playing_piece = tetris_bot_rust.advance_piece_queue()
# SCAN NEW PIECE: Only scan the last queue position
print("Scanning for new queue piece...")
new_piece = scan_single_piece(
TEMPLATE_PATH, SEARCH_AREA, LAST_QUEUE_POSITION
)
if new_piece:
tetris_bot_rust.add_piece_to_queue(new_piece)
print(f"Added new piece to queue: {new_piece}")
else:
print("Warning: Could not detect new piece for queue")
time.sleep(0.1)
else:
print("No current piece available!")
break
time.sleep(0.1)
except KeyboardInterrupt:
print(f"\nStopping bot... Total moves executed: {moves_executed}")
break
except Exception as e:
print(f"Error in game loop: {e}")
time.sleep(1)
except Exception as e:
print(f"Fatal error: {e}")
if __name__ == "__main__":
import sys
if len(sys.argv) > 1 and sys.argv[1] == "play":
main_game_loop()
else:
print("=== TETRIS BOT ===")
print("Run with 'python main.py play' to start playing")