Refactored to use Vector4i. Added Spider/Grasshopper. TODO: Ant

This commit is contained in:
Sch1nken 2024-03-14 19:30:18 +01:00
parent ff4aa93845
commit 11f8a71f52
19 changed files with 482 additions and 326 deletions

View file

@ -1,2 +1,38 @@
extends MovementBehaviour
class_name MovementBehaviourAnt
# Ant uses a modified BFS? It's basically a supercharged spider with limitlesss movement range
# Naive approach: BFS from all neighbours, don't go to already visited cells, check if can reach
# Maybe better: Get al "placecable tiles" from the map, go over those. Might be faster but more complicated
func simulate_move_recursive(start: Vector4i, exclude: Array[Vector4i], map: HexGrid, visited_cells: Array[Vector4i] = []) -> Array[Vector4i]:
var visited = visited_cells
var possible: Array[Vector4i] = []
#if max_num < 1:
# return [start]
for neighbour in map.get_empty_neighbours(start):
if neighbour in visited:
continue
if not map.can_reach(start, neighbour):
continue
var same_neighbours = map.get_same_neighbours(start, neighbour)
for e in exclude:
same_neighbours.erase(e)
if same_neighbours.size() > 0:
visited.append(neighbour)
print("yay?")
possible.append_array(simulate_move_recursive(neighbour, exclude, map, visited))
return possible
func get_available_spaces(pos: Vector4i, map: HexGrid) -> Array[Vector4i]:
var possible_places: Array[Vector4i] = []
possible_places = simulate_move_recursive(pos, [pos], map)
return possible_places

View file

@ -1,38 +1,13 @@
extends MovementBehaviour
class_name MovementBehaviourBee
func can_reach(start: HexGrid.CubeCoordinates, target: HexGrid.CubeCoordinates, map: HexGrid) -> bool:
# if we have 5 potential spaces it can never be blocked
var cubepos: HexGrid.CubeCoordinates = start
var cubecoord: HexGrid.CubeCoordinates = target
var cubetest: HexGrid.CubeCoordinates = HexGrid.CubeCoordinates.new(0, 0, 0)
cubetest.q = cubecoord.q - cubepos.q
cubetest.r = cubecoord.r - cubepos.r
cubetest.s = cubecoord.s - cubepos.s
var left = get_left_neighbour(cubetest)
var right = get_right_neighbour(cubetest)
var left_coord = HexGrid.CubeCoordinates.new(left.q + cubepos.q, left.r + cubepos.r, left.s + cubepos.s)
var right_coord = HexGrid.CubeCoordinates.new(right.q + cubepos.q, right.r + cubepos.r, right.s + cubepos.s)
return map.is_cell_empty(left_coord) or map.is_cell_empty(right_coord)
func get_left_neighbour(pos: HexGrid.CubeCoordinates) -> HexGrid.CubeCoordinates:
return HexGrid.CubeCoordinates.new(-pos.s, -pos.q, -pos.r)
func get_right_neighbour(pos: HexGrid.CubeCoordinates) -> HexGrid.CubeCoordinates:
return HexGrid.CubeCoordinates.new(-pos.r, -pos.s, -pos.q)
func get_available_spaces(pos: HexGrid.CubeCoordinates, map: HexGrid) -> Array[HexGrid.CubeCoordinates]:
func get_available_spaces(pos: Vector4i, map: HexGrid) -> Array[Vector4i]:
var potential_spaces = map.get_empty_neighbours(pos)
var target_spaces: Array[HexGrid.CubeCoordinates] = []
var target_spaces: Array[Vector4i] = []
for neighbour in potential_spaces:
if can_reach(pos, neighbour, map):
for neighbour in potential_spaces:
if map.can_reach(pos, neighbour):
target_spaces.append(neighbour)
return target_spaces

View file

@ -1,2 +1,38 @@
extends MovementBehaviour
class_name MovementBehaviourGrasshopper
const DIRS = [
Vector4i(0, -1, +1, 0), #UP
Vector4i(-1, 0, +1, 0), #UP_LEFT
Vector4i(-1, +1, 0, 0), #DOWN_LEFT
Vector4i(0, +1, -1, 0), #DOWN
Vector4i(+1, 0, -1, 0), #DOWN_RIGHT
Vector4i(+1, -1, 0, 0), #UP_RIGHT
]
func simulate_move_dirs(start: Vector4i, map: HexGrid) -> Array[Vector4i]:
var possible: Array[Vector4i] = []
for dir in DIRS:
possible.push_back(simulate_move_recursive(start, dir, map))
return possible
func simulate_move_recursive(start: Vector4i, dir: Vector4i, map: HexGrid) -> Vector4i:
var possible: Array[Vector4i] = []
var target: Vector4i = start + dir
if map.is_cell_empty(target):
return target
return simulate_move_recursive(target, dir, map)
func get_available_spaces(pos: Vector4i, map: HexGrid) -> Array[Vector4i]:
var possible_places: Array[Vector4i] = []
possible_places = simulate_move_dirs(pos, map)
for neighbour in map.get_empty_neighbours(pos):
possible_places.erase(neighbour)
return possible_places

View file

@ -1,2 +1,38 @@
extends MovementBehaviour
class_name MovementBehaviourSpider
@export var max_movement_reach: int = 3
func simulate_move_recursive(start: Vector4i, max_num: int, exclude: Array[Vector4i], map: HexGrid, visited_cells: Array[Vector4i] = []) -> Array[Vector4i]:
var visited = visited_cells.duplicate()
var possible: Array[Vector4i] = []
visited.append(start)
if max_num < 1:
return [start]
for neighbour in map.get_empty_neighbours(start):
if neighbour in visited:
continue
if not map.can_reach(start, neighbour):
continue
var same_neighbours = map.get_same_neighbours(start, neighbour)
for e in exclude:
same_neighbours.erase(e)
if same_neighbours.size() > 0:
visited.append(neighbour)
print("yay?")
possible.append_array(simulate_move_recursive(neighbour, max_num - 1, exclude, map, visited))
return possible
func get_available_spaces(pos: Vector4i, map: HexGrid) -> Array[Vector4i]:
var possible_places: Array[Vector4i] = []
possible_places = simulate_move_recursive(pos, max_movement_reach, [pos], map)
return possible_places