make the orientation of obstacle align with four side of cell on gridmap

This commit is contained in:
2025-03-13 17:32:41 +08:00
parent 58142e9d67
commit d64b685e6e
6 changed files with 377 additions and 195 deletions
+277 -147
View File
@@ -266,7 +266,7 @@ func randomize_floor_custom(randomize_states: Array, floor_index: int):
else:
set_cell_item(cell_pos, normal_items[0], current_orientation)
# Improved neighbor checking system
#func get_neighbors(current_pos: Vector2i, floor_index: int) -> Array[NeighborInfo]:
#var neighbors: Array[NeighborInfo] = []
#
@@ -288,29 +288,36 @@ func randomize_floor_custom(randomize_states: Array, floor_index: int):
#if is_position_valid(neighbor_pos):
#var is_walkable = is_cell_walkable(neighbor_pos, floor_index)
#
## Check for obstacles - specifically for orthogonal movement
#if not is_diagonal_direction(dir) and is_blocked_by_obstacle(current_pos, neighbor_pos, 3):
#is_walkable = false
#
## Special handling for diagonal movement
#if is_diagonal_direction(dir):
#var adjacent1: Vector2i
#var adjacent2: Vector2i
#
#match dir:
#Direction.NORTHWEST:
#adjacent1 = current_pos + Vector2i(-1, 0)
#adjacent2 = current_pos + Vector2i(0, -1)
#adjacent1 = current_pos + Vector2i(-1, 0) # West
#adjacent2 = current_pos + Vector2i(0, -1) # North
#Direction.NORTHEAST:
#adjacent1 = current_pos + Vector2i(1, 0)
#adjacent2 = current_pos + Vector2i(0, -1)
#adjacent1 = current_pos + Vector2i(1, 0) # East
#adjacent2 = current_pos + Vector2i(0, -1) # North
#Direction.SOUTHWEST:
#adjacent1 = current_pos + Vector2i(-1, 0)
#adjacent2 = current_pos + Vector2i(0, 1)
#adjacent1 = current_pos + Vector2i(-1, 0) # West
#adjacent2 = current_pos + Vector2i(0, 1) # South
#Direction.SOUTHEAST:
#adjacent1 = current_pos + Vector2i(1, 0)
#adjacent2 = current_pos + Vector2i(0, 1)
#adjacent1 = current_pos + Vector2i(1, 0) # East
#adjacent2 = current_pos + Vector2i(0, 1) # South
#
## For diagonal movement, both adjacent cells must be walkable
## AND the movements to those adjacent cells must not be blocked
#is_walkable = is_walkable and \
#is_position_valid(adjacent1) and is_cell_walkable(adjacent1, floor_index) and \
#is_position_valid(adjacent2) and is_cell_walkable(adjacent2, floor_index) and \
#not is_blocked_by_obstacle(current_pos, adjacent1, floor_index) and \
#not is_blocked_by_obstacle(current_pos, adjacent2, floor_index)
#is_position_valid(adjacent1) and is_cell_walkable(adjacent1, floor_index) and \
#is_position_valid(adjacent2) and is_cell_walkable(adjacent2, floor_index) and \
#not is_blocked_by_obstacle(current_pos, adjacent1, 3) and \
#not is_blocked_by_obstacle(current_pos, adjacent2, 3)
#
#if diagonal_movement or not is_diagonal_direction(dir):
#neighbors.append(NeighborInfo.new(neighbor_pos, dir, is_walkable))
@@ -320,17 +327,21 @@ func randomize_floor_custom(randomize_states: Array, floor_index: int):
func get_neighbors(current_pos: Vector2i, floor_index: int) -> Array[NeighborInfo]:
var neighbors: Array[NeighborInfo] = []
# Four orthogonal directions
var directions = {
Direction.NORTHWEST: Vector2i(-1, -1),
Direction.NORTH: Vector2i(0, -1),
Direction.NORTHEAST: Vector2i(1, -1),
Direction.WEST: Vector2i(-1, 0),
Direction.EAST: Vector2i(1, 0),
Direction.SOUTHWEST: Vector2i(-1, 1),
Direction.SOUTH: Vector2i(0, 1),
Direction.SOUTHEAST: Vector2i(1, 1)
Direction.WEST: Vector2i(-1, 0)
}
# Add diagonal directions if enabled
if diagonal_movement:
directions[Direction.NORTHWEST] = Vector2i(-1, -1)
directions[Direction.NORTHEAST] = Vector2i(1, -1)
directions[Direction.SOUTHWEST] = Vector2i(-1, 1)
directions[Direction.SOUTHEAST] = Vector2i(1, 1)
for dir in directions:
var offset = directions[dir]
var neighbor_pos = current_pos + offset
@@ -338,38 +349,22 @@ func get_neighbors(current_pos: Vector2i, floor_index: int) -> Array[NeighborInf
if is_position_valid(neighbor_pos):
var is_walkable = is_cell_walkable(neighbor_pos, floor_index)
# Check for obstacles - specifically for orthogonal movement
if not is_diagonal_direction(dir) and is_blocked_by_obstacle(current_pos, neighbor_pos, 3):
# Check if movement to this neighbor is blocked by obstacles
if not is_diagonal_direction(dir) and is_movement_blocked(current_pos, neighbor_pos, floor_index):
is_walkable = false
# Special handling for diagonal movement
if is_diagonal_direction(dir):
var adjacent1: Vector2i
var adjacent2: Vector2i
# For diagonal movement, check if both orthogonal paths are blocked
var mid1 = Vector2i(neighbor_pos.x, current_pos.y)
var mid2 = Vector2i(current_pos.x, neighbor_pos.y)
match dir:
Direction.NORTHWEST:
adjacent1 = current_pos + Vector2i(-1, 0) # West
adjacent2 = current_pos + Vector2i(0, -1) # North
Direction.NORTHEAST:
adjacent1 = current_pos + Vector2i(1, 0) # East
adjacent2 = current_pos + Vector2i(0, -1) # North
Direction.SOUTHWEST:
adjacent1 = current_pos + Vector2i(-1, 0) # West
adjacent2 = current_pos + Vector2i(0, 1) # South
Direction.SOUTHEAST:
adjacent1 = current_pos + Vector2i(1, 0) # East
adjacent2 = current_pos + Vector2i(0, 1) # South
var path1_blocked = is_movement_blocked(current_pos, mid1, floor_index)
var path2_blocked = is_movement_blocked(current_pos, mid2, floor_index)
# For diagonal movement, both adjacent cells must be walkable
# AND the movements to those adjacent cells must not be blocked
is_walkable = is_walkable and \
is_position_valid(adjacent1) and is_cell_walkable(adjacent1, floor_index) and \
is_position_valid(adjacent2) and is_cell_walkable(adjacent2, floor_index) and \
not is_blocked_by_obstacle(current_pos, adjacent1, 3) and \
not is_blocked_by_obstacle(current_pos, adjacent2, 3)
if path1_blocked and path2_blocked:
is_walkable = false
if diagonal_movement or not is_diagonal_direction(dir):
if is_walkable:
neighbors.append(NeighborInfo.new(neighbor_pos, dir, is_walkable))
return neighbors
@@ -490,6 +485,99 @@ func update_grid_data():
grid_data.append(floor_data)
emit_signal("grid_updated")
# Check the obstacle on a cell
func has_obstacle_at(pos: Vector3i) -> bool:
var item = get_cell_item(pos)
return item in obstacle_items
# Get orientation ( rotation )
func get_cell_orientation(pos: Vector3i) -> int:
return get_cell_item_orientation(pos)
# Get obstacle direction
# Get the direction of an obstacle at a specific position
func get_obstacle_direction(pos: Vector3i) -> Direction:
if obstacle_directions.has(pos):
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 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 movement direction
var direction: int
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
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)
if orientation == opposite_dir: # Obstacle blocks entrance from this direction
return true
return false
# Cell rotation handling
func get_cell_rotation(position: Vector3i) -> int:
return get_cell_item_orientation(position)
@@ -586,122 +674,164 @@ func set_diagonal_movement(enable: bool):
#
#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:
# 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
# For orthogonal movement (up, down, left, right)
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)
# Use safe dictionary access
if obstacle_directions.has(from_pos_3d):
from_dir = obstacle_directions[from_pos_3d]
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
current = next
else:
# For diagonal movement, check if both orthogonal paths are blocked
var mid1 = Vector2i(to_pos.x, from_pos.y)
var mid2 = Vector2i(from_pos.x, to_pos.y)
# 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
var path1_blocked = is_blocked_by_obstacle(from_pos, mid1, floor_index)
var path2_blocked = is_blocked_by_obstacle(from_pos, mid2, floor_index)
# 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
return path1_blocked and path2_blocked
# 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
func place_obstacle(pos: Vector3i, obstacle_item: int, direction: Direction) -> bool:
#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
func place_obstacle(pos: Vector3i, obstacle_item: int, orientation: int) -> 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 correctly 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 the obstacle item with the specified orientation
set_cell_item(pos, obstacle_item, orientation)
# Re-initialize A* pathfinding to account for the new obstacle
@@ -95,7 +95,7 @@ item/11/navigation_mesh_transform = Transform3D(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
item/11/navigation_layers = 1
item/12/name = "obstacle_block_h"
item/12/mesh = ExtResource("4_8v5xv")
item/12/mesh_transform = Transform3D(1.65, 0, 0, 0, 1, 0, 0, 0, 1, 0, -2, 0.5)
item/12/mesh_transform = Transform3D(1.65, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.4, 0.5)
item/12/mesh_cast_shadow = 1
item/12/shapes = []
item/12/navigation_mesh_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
+1 -1
View File
@@ -3,5 +3,5 @@
name="Enchaced Gridmap"
description=""
author="Bengski"
version="1.0"
version="1.4"
script="plugin.gd"