# tekton-enet/scripts/tile_grabber.gd extends Node class_name TileGrabber signal tile_grabbed(tile_id: int, position: Vector3i) signal tiles_arranged(arranged_tiles: Array) @export var enhanced_gridmap_path: NodePath @export var player_board_path: NodePath var enhanced_gridmap: EnhancedGridMap var player_board: Node var grabbed_tile: int = -1 var grabbed_position: Vector3i # Goal combinations - define what patterns score points var goal_combinations = [ { "name": "Line of 3", "pattern": [ [1, 1, 1], [0, 0, 0], [0, 0, 0] ], "score": 10 }, { "name": "L-Shape", "pattern": [ [1, 0, 0], [1, 0, 0], [1, 1, 0] ], "score": 15 }, { "name": "Square", "pattern": [ [1, 1, 0], [1, 1, 0], [0, 0, 0] ], "score": 20 } ] func _ready(): if enhanced_gridmap_path: enhanced_gridmap = get_node(enhanced_gridmap_path) if player_board_path: player_board = get_node(player_board_path) func grab_tile(grid_position: Vector3i) -> bool: if not enhanced_gridmap: print("Error: EnhancedGridMap not found") return false # Get the tile at the specified position var tile_id = enhanced_gridmap.get_cell_item(grid_position) if tile_id == -1: print("No tile at position: ", grid_position) return false # Store the grabbed tile and its position grabbed_tile = tile_id grabbed_position = grid_position # Remove the tile from the grid enhanced_gridmap.set_cell_item(grid_position, -1) # Emit signal that a tile was grabbed tile_grabbed.emit(tile_id, grid_position) print("Grabbed tile ", tile_id, " from position ", grid_position) return true func auto_arrange_tile() -> bool: if grabbed_tile == -1: print("No tile to arrange") return false # Find the best position for the grabbed tile based on goal combinations var best_position = find_best_position_for_tile(grabbed_tile) if best_position == Vector3i(-1, -1, -1): print("No valid position found for tile") return false # Place the tile at the best position enhanced_gridmap.set_cell_item(best_position, grabbed_tile) # Reset grabbed tile grabbed_tile = -1 grabbed_position = Vector3i(-1, -1, -1) # Emit signal that tiles were arranged tiles_arranged.emit([best_position]) print("Auto-arranged tile at position: ", best_position) return true func find_best_position_for_tile(tile_id: int) -> Vector3i: var best_position = Vector3i(-1, -1, -1) var best_score = -1 # Check all possible positions on the grid for x in range(enhanced_gridmap.columns): for z in range(enhanced_gridmap.rows): var position = Vector3i(x, 0, z) # Skip if position is already occupied if enhanced_gridmap.get_cell_item(position) != -1: continue # Temporarily place the tile to check for goal combinations enhanced_gridmap.set_cell_item(position, tile_id) # Calculate score for this position var score = calculate_position_score(position) # Remove the temporary tile enhanced_gridmap.set_cell_item(position, -1) # Update best position if this one scores higher if score > best_score: best_score = score best_position = position return best_position func calculate_position_score(position: Vector3i) -> int: var total_score = 0 # Check each goal combination for goal in goal_combinations: var matches = check_goal_combination(position, goal.pattern) if matches: total_score += goal.score return total_score func check_goal_combination(center_position: Vector3i, pattern: Array) -> bool: # Get the pattern dimensions var pattern_width = pattern[0].size() var pattern_height = pattern.size() # Calculate the top-left position of the pattern var start_x = center_position.x - (pattern_width / 2) var start_z = center_position.z - (pattern_height / 2) # Check if the pattern fits within the grid if start_x < 0 or start_z < 0 or start_x + pattern_width > enhanced_gridmap.columns or start_z + pattern_height > enhanced_gridmap.rows: return false # Check each cell in the pattern for z in range(pattern_height): for x in range(pattern_width): var pattern_value = pattern[z][x] var grid_position = Vector3i(start_x + x, 0, start_z + z) var grid_value = enhanced_gridmap.get_cell_item(grid_position) # If pattern expects a tile (1) but grid is empty (-1), no match if pattern_value == 1 and grid_value == -1: return false # If pattern expects empty (0) but grid has a tile, no match if pattern_value == 0 and grid_value != -1: return false # All cells match the pattern return true