update obstacles

This commit is contained in:
2025-03-21 11:44:12 +08:00
parent 50e5298009
commit 679961aa77
6 changed files with 323 additions and 327 deletions
+187 -28
View File
@@ -844,50 +844,209 @@ func highlight_cells_if_authorized(cells_to_highlight: Array):
enhanced_gridmap.hover_item
)
# Modify highlight functions to use the new authorized highlight method
## Update highlight_movement_range to respect obstacle blocking
#func highlight_movement_range():
#if not is_multiplayer_authority() or is_bot or is_in_group("Bots"):
#return
#
#clear_highlights()
#var cells_to_highlight = []
#
## Get neighboring cells that are directly accessible (not blocked by obstacles)
#var direct_neighbors = []
#var directions = [
#Vector2i(0, -1), # North
#Vector2i(1, 0), # East
#Vector2i(0, 1), # South
#Vector2i(-1, 0), # West
#]
#
## Add diagonal directions if enabled
#if enhanced_gridmap.diagonal_movement:
#directions.append(Vector2i(-1, -1)) # Northwest
#directions.append(Vector2i(1, -1)) # Northeast
#directions.append(Vector2i(-1, 1)) # Southwest
#directions.append(Vector2i(1, 1)) # Southeast
#
## First, get direct neighbors that aren't blocked
#for dir in directions:
#var neighbor_pos = current_position + dir
#
## Check if position is valid and walkable
#if enhanced_gridmap.is_position_valid(neighbor_pos) and \
#enhanced_gridmap.is_cell_walkable(neighbor_pos, 0) and \
#not is_position_occupied(neighbor_pos):
#
## Check if movement is blocked by obstacle
#if not enhanced_gridmap.is_movement_blocked(current_position, neighbor_pos, 3):
#direct_neighbors.append(neighbor_pos)
#
## Now, add all cells that are within movement range
## and can be reached via the direct neighbors
#for x in range(max(0, current_position.x - movement_range),
#min(enhanced_gridmap.columns, current_position.x + movement_range + 1)):
#for z in range(max(0, current_position.y - movement_range),
#min(enhanced_gridmap.rows, current_position.y + movement_range + 1)):
#var test_pos = Vector2i(x, z)
#
## Skip the current position
#if test_pos == current_position:
#continue
#
## Check if within movement range
#if is_within_movement_range(test_pos):
#var cell_item = enhanced_gridmap.get_cell_item(Vector3i(x, 0, z))
#
## Basic walkability check
#if cell_item != -1 and not (cell_item in enhanced_gridmap.non_walkable_items) and \
#not is_position_occupied(test_pos):
#
## Check if we can reach this position through one of our direct neighbors
#var can_reach = false
#
## Direct neighbors are always reachable
#if test_pos in direct_neighbors:
#can_reach = true
#else:
## For other cells, check if there's a valid path through direct neighbors
#for neighbor in direct_neighbors:
## Check if we can move from the neighbor to the target position
## without being blocked by an obstacle
#if not enhanced_gridmap.is_blocked_by_obstacle(neighbor, test_pos, 3):
#var manhattan_dist = abs(neighbor.x - test_pos.x) + abs(neighbor.y - test_pos.y)
#var remaining_range = movement_range - 1 # -1 for the first step
#
## Check if the remaining distance is within our movement range
#if manhattan_dist <= remaining_range:
#can_reach = true
#break
#
#if can_reach:
#cells_to_highlight.append(test_pos)
#
#highlight_cells_if_authorized(cells_to_highlight)
# Update highlight_movement_range to respect the expanded obstacle blocking
func highlight_movement_range():
if not is_multiplayer_authority() or is_bot or is_in_group("Bots"):
return
clear_highlights()
var cells_to_highlight = []
# For each position within movement range
for x in range(max(0, current_position.x - movement_range), min(enhanced_gridmap.columns, current_position.x + movement_range + 1)):
for z in range(max(0, current_position.y - movement_range), min(enhanced_gridmap.rows, current_position.y + movement_range + 1)):
# First, identify all cells that are blocked by obstacles
var blocked_cells = []
# Check all cells for obstacles and get their blocked cells
for x in range(enhanced_gridmap.columns):
for z in range(enhanced_gridmap.rows):
var cell_pos = Vector2i(x, z)
var cell_pos3d = Vector3i(x, 3, z)
if enhanced_gridmap.has_obstacle_at(cell_pos3d):
var orientation = enhanced_gridmap.get_obstacle_orientation(cell_pos3d)
blocked_cells.append_array(enhanced_gridmap.get_cells_blocked_by_obstacle(cell_pos, orientation, 3))
# Now highlight all cells within movement range that aren't blocked
for x in range(max(0, current_position.x - movement_range),
min(enhanced_gridmap.columns, current_position.x + movement_range + 1)):
for z in range(max(0, current_position.y - movement_range),
min(enhanced_gridmap.rows, current_position.y + movement_range + 1)):
var test_pos = Vector2i(x, z)
# Skip the current position
# Skip current position
if test_pos == current_position:
continue
# Check if within movement range
if is_within_movement_range(test_pos):
var cell_item = enhanced_gridmap.get_cell_item(Vector3i(x, 0, z))
# Skip if blocked by obstacle
if test_pos in blocked_cells:
continue
# Basic walkability check
if cell_item != -1 and not (cell_item in enhanced_gridmap.non_walkable_items) and not is_position_occupied(test_pos):
# Check if there's a direct path without obstacles
var path_blocked = false
# For orthogonal movement
if test_pos.x == current_position.x or test_pos.y == current_position.y:
path_blocked = enhanced_gridmap.is_blocked_by_obstacle(current_position, test_pos, 3)
else:
# For diagonal movement, check if both orthogonal paths are blocked
var mid1 = Vector2i(test_pos.x, current_position.y)
var mid2 = Vector2i(current_position.x, test_pos.y)
var path1_blocked = enhanced_gridmap.is_blocked_by_obstacle(current_position, mid1, 3)
var path2_blocked = enhanced_gridmap.is_blocked_by_obstacle(current_position, mid2, 3)
# Only completely block if both paths are blocked
path_blocked = path1_blocked and path2_blocked
if not path_blocked:
cells_to_highlight.append(test_pos)
# Check basic walkability
var cell_item = enhanced_gridmap.get_cell_item(Vector3i(x, 0, z))
if cell_item == -1 or cell_item in enhanced_gridmap.non_walkable_items or is_position_occupied(test_pos):
continue
# Check if there's a valid path to this cell
if can_reach_cell(test_pos, blocked_cells):
cells_to_highlight.append(test_pos)
highlight_cells_if_authorized(cells_to_highlight)
# Helper function to check if a cell can be reached given the blocked cells
func can_reach_cell(target_pos: Vector2i, blocked_cells: Array) -> bool:
# Simple BFS to find if there's a path
var queue = [current_position]
var visited = {current_position: true}
var steps = {current_position: 0}
while not queue.is_empty():
var current = queue.pop_front()
# If we've found the target, check if it's within movement range
if current == target_pos:
return steps[current] <= movement_range
# If we've used all movement, don't explore further
if steps[current] >= movement_range:
continue
# Try all adjacent cells
var directions = [
Vector2i(0, -1), # North
Vector2i(1, 0), # East
Vector2i(0, 1), # South
Vector2i(-1, 0), # West
]
# Add diagonal directions if enabled
if enhanced_gridmap.diagonal_movement:
directions.append(Vector2i(-1, -1)) # Northwest
directions.append(Vector2i(1, -1)) # Northeast
directions.append(Vector2i(-1, 1)) # Southwest
directions.append(Vector2i(1, 1)) # Southeast
for dir in directions:
var next_pos = current + dir
# Skip if already visited, blocked, or not valid
if visited.has(next_pos) or next_pos in blocked_cells:
continue
if not enhanced_gridmap.is_position_valid(next_pos) or not enhanced_gridmap.is_cell_walkable(next_pos, 0):
continue
if is_position_occupied(next_pos) and next_pos != target_pos:
continue
# Check if movement between cells is blocked by an obstacle
if not is_diagonal_direction(dir) and enhanced_gridmap.is_movement_blocked(current, next_pos, 3):
continue
# For diagonal movement, check if both orthogonal paths are blocked
if is_diagonal_direction(dir):
var mid1 = Vector2i(next_pos.x, current.y)
var mid2 = Vector2i(current.x, next_pos.y)
var path1_blocked = mid1 in blocked_cells or enhanced_gridmap.is_movement_blocked(current, mid1, 3)
var path2_blocked = mid2 in blocked_cells or enhanced_gridmap.is_movement_blocked(current, mid2, 3)
if path1_blocked and path2_blocked:
continue
# Add to queue
queue.append(next_pos)
visited[next_pos] = true
steps[next_pos] = steps[current] + 1
return false
# Helper function to check if a direction is diagonal
func is_diagonal_direction(direction: Vector2i) -> bool:
return direction.x != 0 and direction.y != 0
func highlight_adjacent_cells():
if not is_multiplayer_authority() or is_bot or is_in_group("Bots"):
return