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
+118 -251
View File
@@ -27,6 +27,9 @@ var path = []
@export var obstacle_items: Array[int] = [12, 13, 14, 15] # Obstacle items in mesh library
@export var obstacle_directions: Dictionary = {} # Store direction for each placed obstacle: {Vector3i position: Direction}
# Dictionary to store obstacle information: {cell_pos: orientation}
# orientation: 0=North, 1=East, 2=South, 3=West
var obstacles = {}
# Direction and movement systems
enum Direction {
@@ -501,83 +504,108 @@ func get_obstacle_direction(pos: Vector3i) -> Direction:
return obstacle_directions[pos]
return Direction.CENTER
#func is_movement_blocked(from_pos: Vector2i, to_pos: Vector2i, floor_index: int = 3) -> bool:
## Must be adjacent cells
#if abs(from_pos.x - to_pos.x) + abs(from_pos.y - to_pos.y) != 1:
#return false
#
## Determine which direction we're moving
#var direction: Direction
#
#if to_pos.y < from_pos.y: # Moving NORTH
#direction = Direction.NORTH
#elif to_pos.x > from_pos.x: # Moving EAST
#direction = Direction.EAST
#elif to_pos.y > from_pos.y: # Moving SOUTH
#direction = Direction.SOUTH
#elif to_pos.x < from_pos.x: # Moving WEST
#direction = Direction.WEST
#
## Check if the current cell has an obstacle blocking the exit
#var from_obstacle_pos = Vector3i(from_pos.x, floor_index, from_pos.y)
#if has_obstacle_at(from_obstacle_pos):
#var obs_dir = get_obstacle_direction(from_obstacle_pos)
#if obs_dir == direction: # Obstacle blocks exit in this direction
#return true
#
## Check if the destination cell has an obstacle blocking the entrance
#var to_obstacle_pos = Vector3i(to_pos.x, floor_index, to_pos.y)
#if has_obstacle_at(to_obstacle_pos):
#var opposite_dir: Direction
#
## Calculate the opposite direction
#match direction:
#Direction.NORTH: opposite_dir = Direction.SOUTH
#Direction.EAST: opposite_dir = Direction.WEST
#Direction.SOUTH: opposite_dir = Direction.NORTH
#Direction.WEST: opposite_dir = Direction.EAST
#
#var obs_dir = get_obstacle_direction(to_obstacle_pos)
#if obs_dir == opposite_dir: # Obstacle blocks entrance from this direction
#return true
#
#return false
func get_obstacle_orientation(pos: Vector3i) -> int:
return get_cell_item_orientation(pos)
func is_movement_blocked(from_pos: Vector2i, to_pos: Vector2i, floor_index: int = 3) -> bool:
# Must be adjacent cells
# Must be adjacent cells for direct blocking check
if abs(from_pos.x - to_pos.x) + abs(from_pos.y - to_pos.y) != 1:
return false
# Determine movement direction
var direction: int
# Get 3D positions for the cells
var from_pos3d = Vector3i(from_pos.x, floor_index, from_pos.y)
var to_pos3d = Vector3i(to_pos.x, floor_index, to_pos.y)
if to_pos.y < from_pos.y: # Moving NORTH
direction = 0 # North
elif to_pos.x > from_pos.x: # Moving EAST
direction = 1 # East
elif to_pos.y > from_pos.y: # Moving SOUTH
direction = 2 # South
elif to_pos.x < from_pos.x: # Moving WEST
direction = 3 # West
# Check if the current cell has an obstacle blocking the exit
var from_obstacle_pos = Vector3i(from_pos.x, floor_index, from_pos.y)
if has_obstacle_at(from_obstacle_pos):
var orientation = get_cell_orientation(from_obstacle_pos)
if orientation == direction: # Obstacle blocks exit in this direction
# Check if the starting cell has an obstacle
if has_obstacle_at(from_pos3d):
var orientation = get_obstacle_orientation(from_pos3d)
# Check if the obstacle is blocking the requested movement direction
if from_pos.y > to_pos.y and orientation == 0: # Moving NORTH, obstacle faces NORTH
return true
elif from_pos.x < to_pos.x and orientation == 1: # Moving EAST, obstacle faces EAST
return true
elif from_pos.y < to_pos.y and orientation == 2: # Moving SOUTH, obstacle faces SOUTH
return true
elif from_pos.x > to_pos.x and orientation == 3: # Moving WEST, obstacle faces WEST
return true
# Check if the destination cell has an obstacle blocking the entrance
var to_obstacle_pos = Vector3i(to_pos.x, floor_index, to_pos.y)
if has_obstacle_at(to_obstacle_pos):
var orientation = get_cell_orientation(to_obstacle_pos)
var opposite_dir = (direction + 2) % 4 # Opposite direction (0→2, 1→3, 2→0, 3→1)
# Check if the destination cell has an obstacle blocking entry
if has_obstacle_at(to_pos3d):
var orientation = get_obstacle_orientation(to_pos3d)
if orientation == opposite_dir: # Obstacle blocks entrance from this direction
# Check if the obstacle is blocking entry from the requested direction
if to_pos.y < from_pos.y and orientation == 2: # Coming from SOUTH, obstacle faces SOUTH
return true
elif to_pos.x > from_pos.x and orientation == 3: # Coming from WEST, obstacle faces WEST
return true
elif to_pos.y > from_pos.y and orientation == 0: # Coming from NORTH, obstacle faces NORTH
return true
elif to_pos.x < from_pos.x and orientation == 1: # Coming from EAST, obstacle faces EAST
return true
return false
# Function to check if a cell is blocked by any obstacles in its vicinity
func is_cell_blocked_by_obstacles(pos: Vector2i, floor_index: int = 3) -> bool:
var pos3d = Vector3i(pos.x, floor_index, pos.y)
# Check if this cell itself has an obstacle
if has_obstacle_at(pos3d):
return true
# Check all adjacent cells for obstacles that might block this cell
var adjacent_positions = [
Vector2i(pos.x, pos.y - 1), # North
Vector2i(pos.x + 1, pos.y), # East
Vector2i(pos.x, pos.y + 1), # South
Vector2i(pos.x - 1, pos.y), # West
]
for adj_pos in adjacent_positions:
var adj_pos3d = Vector3i(adj_pos.x, floor_index, adj_pos.y)
# Check if position is valid
if is_position_valid(adj_pos) and has_obstacle_at(adj_pos3d):
var orientation = get_obstacle_orientation(adj_pos3d)
# Check if the obstacle is blocking this cell
if adj_pos.y < pos.y and orientation == 0: # Obstacle to NORTH facing NORTH
return true
elif adj_pos.x > pos.x and orientation == 1: # Obstacle to EAST facing EAST
return true
elif adj_pos.y > pos.y and orientation == 2: # Obstacle to SOUTH facing SOUTH
return true
elif adj_pos.x < pos.x and orientation == 3: # Obstacle to WEST facing WEST
return true
return false
# Function to get all cells blocked by an obstacle at a specific position
func get_cells_blocked_by_obstacle(obstacle_pos: Vector2i, orientation: int, floor_index: int = 3) -> Array:
var blocked_cells = []
# Determine which cells are blocked based on orientation
match orientation:
0: # NORTH - blocks the row above
for x in range(max(0, obstacle_pos.x - 1), min(columns, obstacle_pos.x + 2)):
blocked_cells.append(Vector2i(x, obstacle_pos.y - 1))
1: # EAST - blocks the column to the right
for y in range(max(0, obstacle_pos.y - 1), min(rows, obstacle_pos.y + 2)):
blocked_cells.append(Vector2i(obstacle_pos.x + 1, y))
2: # SOUTH - blocks the row below
for x in range(max(0, obstacle_pos.x - 1), min(columns, obstacle_pos.x + 2)):
blocked_cells.append(Vector2i(x, obstacle_pos.y + 1))
3: # WEST - blocks the column to the left
for y in range(max(0, obstacle_pos.y - 1), min(rows, obstacle_pos.y + 2)):
blocked_cells.append(Vector2i(obstacle_pos.x - 1, y))
# Filter out invalid positions
return blocked_cells.filter(func(pos): return is_position_valid(pos))
# Cell rotation handling
func get_cell_rotation(position: Vector3i) -> int:
return get_cell_item_orientation(position)
@@ -621,209 +649,45 @@ func set_diagonal_movement(enable: bool):
diagonal_movement = enable
initialize_astar()
# Add this function to check if a movement is blocked by an obstacle
#func is_blocked_by_obstacle(from_pos: Vector2i, to_pos: Vector2i, floor_index: int = 3) -> bool:
## Detect movement direction
#var diff_x = to_pos.x - from_pos.x
#var diff_y = to_pos.y - from_pos.y
#
## Case 1: Moving along X axis (horizontally)
#if diff_y == 0 and diff_x != 0:
## Check if there's a vertical obstacle blocking horizontal movement
#var min_x = min(from_pos.x, to_pos.x)
#var max_x = max(from_pos.x, to_pos.x)
#for x in range(min_x, max_x + 1):
#var cell_pos = Vector3i(x, floor_index, from_pos.y)
#var cell_index = get_cell_item(cell_pos)
#if cell_index in obstacle_items:
#var obstacle_idx = obstacle_items.find(cell_index)
#if obstacle_idx != -1 and obstacle_idx < obstacle_directions.size():
#var dir = obstacle_directions[obstacle_idx]
#if dir == Direction.BLOCKED_NORTH or dir == Direction.BLOCKED_SOUTH:
#return true
#
## Case 2: Moving along Y axis (vertically)
#if diff_x == 0 and diff_y != 0:
## Check if there's a horizontal obstacle blocking vertical movement
#var min_y = min(from_pos.y, to_pos.y)
#var max_y = max(from_pos.y, to_pos.y)
#for y in range(min_y, max_y + 1):
#var cell_pos = Vector3i(from_pos.x, floor_index, y)
#var cell_index = get_cell_item(cell_pos)
#if cell_index in obstacle_items:
#var obstacle_idx = obstacle_items.find(cell_index)
#if obstacle_idx != -1 and obstacle_idx < obstacle_directions.size():
#var dir = obstacle_directions[obstacle_idx]
#if dir == Direction.BLOCKED_EAST or dir == Direction.BLOCKED_WEST:
#return true
#
## Case 3: Diagonal movement - check if both direct paths are blocked
## This will force the player to take the longer route
#if diff_x != 0 and diff_y != 0:
## Check if moving horizontally first then vertically would be blocked
#var horiz_first = is_blocked_by_obstacle(from_pos, Vector2i(to_pos.x, from_pos.y), floor_index)
#var vert_second = is_blocked_by_obstacle(Vector2i(to_pos.x, from_pos.y), to_pos, floor_index)
#
## Check if moving vertically first then horizontally would be blocked
#var vert_first = is_blocked_by_obstacle(from_pos, Vector2i(from_pos.x, to_pos.y), floor_index)
#var horiz_second = is_blocked_by_obstacle(Vector2i(from_pos.x, to_pos.y), to_pos, floor_index)
#
## If both paths are blocked, then the diagonal movement is blocked
#if (horiz_first or vert_second) and (vert_first or horiz_second):
#return true
#
#return false
#func is_blocked_by_obstacle(from_pos: Vector2i, to_pos: Vector2i, floor_index: int = 3) -> bool:
## Determine movement direction (without using normalized for Vector2i)
#var diff_x = to_pos.x - from_pos.x
#var diff_y = to_pos.y - from_pos.y
#
## Convert to direction based on sign
#var dir_x = 0
#var dir_y = 0
#if diff_x != 0: dir_x = 1 if diff_x > 0 else -1
#if diff_y != 0: dir_y = 1 if diff_y > 0 else -1
#
## Check for obstacles at both cells
#var from_obstacle = get_cell_item(Vector3i(from_pos.x, floor_index, from_pos.y))
#var to_obstacle = get_cell_item(Vector3i(to_pos.x, floor_index, to_pos.y))
#
## Check obstacle at starting position
#if from_obstacle in obstacle_items:
#var from_pos_3d = Vector3i(from_pos.x, floor_index, from_pos.y)
#var from_dir = Direction.CENTER
#
## Use safe dictionary access
#if obstacle_directions.has(from_pos_3d):
#from_dir = obstacle_directions[from_pos_3d]
#
## Block movement based on obstacle direction
#match from_dir:
#Direction.NORTH: # Blocks south movement
#if dir_y > 0: return true
#Direction.EAST: # Blocks west movement
#if dir_x < 0: return true
#Direction.SOUTH: # Blocks north movement
#if dir_y < 0: return true
#Direction.WEST: # Blocks east movement
#if dir_x > 0: return true
#
## Check obstacle at destination position
#if to_obstacle in obstacle_items:
#var to_pos_3d = Vector3i(to_pos.x, floor_index, to_pos.y)
#var to_dir = Direction.CENTER
#
## Use safe dictionary access
#if obstacle_directions.has(to_pos_3d):
#to_dir = obstacle_directions[to_pos_3d]
#
## Block movement based on obstacle direction (from opposite side)
#match to_dir:
#Direction.NORTH: # Blocks south movement (coming from north)
#if dir_y < 0: return true
#Direction.EAST: # Blocks west movement (coming from east)
#if dir_x > 0: return true
#Direction.SOUTH: # Blocks north movement (coming from south)
#if dir_y > 0: return true
#Direction.WEST: # Blocks east movement (coming from west)
#if dir_x < 0: return true
#
## Check intermediate cell for vertical/horizontal movement
#if from_pos.x != to_pos.x and from_pos.y == to_pos.y: # Horizontal movement
#var x_step = 1 if to_pos.x > from_pos.x else -1
#var intermediate_x = from_pos.x + x_step
#while intermediate_x != to_pos.x:
#var inter_obstacle = get_cell_item(Vector3i(intermediate_x, floor_index, from_pos.y))
#if inter_obstacle in obstacle_items:
#var inter_pos_3d = Vector3i(intermediate_x, floor_index, from_pos.y)
#var inter_dir = Direction.CENTER
#
## Use safe dictionary access
#if obstacle_directions.has(inter_pos_3d):
#inter_dir = obstacle_directions[inter_pos_3d]
#
#if inter_dir == Direction.NORTH or inter_dir == Direction.SOUTH:
#return true
#intermediate_x += x_step
#elif from_pos.x == to_pos.x and from_pos.y != to_pos.y: # Vertical movement
#var y_step = 1 if to_pos.y > from_pos.y else -1
#var intermediate_y = from_pos.y + y_step
#while intermediate_y != to_pos.y:
#var inter_obstacle = get_cell_item(Vector3i(from_pos.x, floor_index, intermediate_y))
#if inter_obstacle in obstacle_items:
#var inter_pos_3d = Vector3i(from_pos.x, floor_index, intermediate_y)
#var inter_dir = Direction.CENTER
#
## Use safe dictionary access
#if obstacle_directions.has(inter_pos_3d):
#inter_dir = obstacle_directions[inter_pos_3d]
#
#if inter_dir == Direction.EAST or inter_dir == Direction.WEST:
#return true
#intermediate_y += y_step
#
## If none of the above conditions triggered, movement is allowed
#return false
# Updated is_blocked_by_obstacle to check for each step in the path
func is_blocked_by_obstacle(from_pos: Vector2i, to_pos: Vector2i, floor_index: int = 3) -> bool:
# For orthogonal movement (up, down, left, right)
# For direct orthogonal movement (up, down, left, right)
if (from_pos.x == to_pos.x and abs(from_pos.y - to_pos.y) == 1) or (from_pos.y == to_pos.y and abs(from_pos.x - to_pos.x) == 1):
return is_movement_blocked(from_pos, to_pos, floor_index)
# For diagonal or longer distances, build a path and check each step
var path = []
# Simple path planning for orthogonal movement
if from_pos.x == to_pos.x or from_pos.y == to_pos.y:
# Check each step along the path
var dx = sign(to_pos.x - from_pos.x)
var dy = sign(to_pos.y - from_pos.y)
var current = from_pos
while current != to_pos:
var next = Vector2i(current.x + dx, current.y + dy)
if is_movement_blocked(current, next, floor_index):
return true
path.append([current, next])
current = next
else:
# For diagonal movement, check if both orthogonal paths are blocked
# For diagonal movement, check both possible paths
# Path 1: Move horizontally first, then vertically
var mid1 = Vector2i(to_pos.x, from_pos.y)
var path1_blocked = is_blocked_by_obstacle(from_pos, mid1, floor_index) or is_blocked_by_obstacle(mid1, to_pos, floor_index)
# Path 2: Move vertically first, then horizontally
var mid2 = Vector2i(from_pos.x, to_pos.y)
var path2_blocked = is_blocked_by_obstacle(from_pos, mid2, floor_index) or is_blocked_by_obstacle(mid2, to_pos, floor_index)
var path1_blocked = is_blocked_by_obstacle(from_pos, mid1, floor_index)
var path2_blocked = is_blocked_by_obstacle(from_pos, mid2, floor_index)
# Movement is blocked if both paths are blocked
return path1_blocked and path2_blocked
# Check each step in the path
for step in path:
if is_movement_blocked(step[0], step[1], floor_index):
return true
return false
#func place_obstacle(pos: Vector3i, obstacle_item: int, direction: Direction) -> bool:
## Always place on floor 3
#pos.y = 3
#
#if get_cell_item(pos) != -1:
#return false # Cell is already occupied
#
#set_cell_item(pos, obstacle_item)
#
## Store the direction of the obstacle in the dictionary
#obstacle_directions[pos] = direction
#
## Update the cell's orientation based on direction
#var orientation = 0
#match direction:
#Direction.NORTH:
#orientation = 0 # Default orientation
#Direction.EAST:
#orientation = 1 # 90 degrees clockwise
#Direction.SOUTH:
#orientation = 2 # 180 degrees
#Direction.WEST:
#orientation = 3 # 270 degrees clockwise
#
#set_cell_item(pos, obstacle_item, orientation)
#
## Re-initialize A* pathfinding to account for the new obstacle
#initialize_astar()
#
#return true
# Place an obstacle at the specified position with a specific orientation
func place_obstacle(pos: Vector3i, obstacle_item: int, orientation: int) -> bool:
# Always place on floor 3
pos.y = 3
@@ -834,6 +698,9 @@ func place_obstacle(pos: Vector3i, obstacle_item: int, orientation: int) -> bool
# Set the obstacle item with the specified orientation
set_cell_item(pos, obstacle_item, orientation)
# Store the obstacle information
obstacles[pos] = orientation
# Re-initialize A* pathfinding to account for the new obstacle
initialize_astar()
+2 -1
View File
@@ -25,4 +25,5 @@ func _get_plugin_name():
func _edit(object):
if dock and object is EnhancedGridMap:
dock.set_enhanced_gridmap(object)
if is_instance_valid(dock) and dock.has_method("set_enhanced_gridmap"):
dock.set_enhanced_gridmap(object)