Skip to content

Commit efcaed4

Browse files
food-pleaseNathanLovato
authored andcommitted
Add flags to Pathfinder.GetPathToCell()
1 parent 96f8c5c commit efcaed4

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

src/field/gameboard/pathfinder.gd

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ class_name Pathfinder
55
extends AStar2D
66

77

8+
const FLAG_ALLOW_SOURCE_OCCUPANT = 1 << 0
9+
const FLAG_ALLOW_TARGET_OCCUPANT = 1 << 1
10+
const FLAG_ALLOW_ALL_OCCUPANTS = 1 << 2
11+
12+
813
func _init() -> void:
914
# Disable/re-enable occupied cells whenever a gamepiece moves.
1015
GamepieceRegistry.gamepiece_moved.connect(
@@ -33,36 +38,44 @@ func can_move_to(coord: Vector2i) -> bool:
3338
## Find a path between two cells. Returns an empty array if no path is available.
3439
## If allow_blocked_source or allow_blocked_target are false, the pathinder wlil fail if a gamepiece
3540
## occupies the source or target cells, respectively.
36-
func get_path_to_cell(source_coord: Vector2i, target_coord: Vector2i,
37-
allow_disabled_source: = true, allow_disabled_target: = false) -> Array[Vector2i]:
41+
func get_path_to_cell(source_coord: Vector2i, target_coord: Vector2i,
42+
occupancy_flags: int = 1) -> Array[Vector2i]:
3843
# Store the return value in a variable.
3944
var move_path: Array[Vector2i] = []
4045

4146
# Find the source/target IDs and keep track of whether or not the cells are occupied.
4247
var source_id: = Gameboard.cell_to_index(source_coord)
4348
var target_id: = Gameboard.cell_to_index(target_coord)
4449

50+
# The pathfinder has several flags to ignore cell occupancy. We'll need to track which occupants
51+
# are temporarily ignored and then re-disable their pathfinder points once a path is found.
52+
# Key is point id, value is whether or not the point is disabled.
53+
var ignored_points: Dictionary[int, bool] = {}
54+
if (occupancy_flags & FLAG_ALLOW_ALL_OCCUPANTS) != 0:
55+
print("Allow all occupants!")
56+
for id in get_point_ids():
57+
if is_point_disabled(id):
58+
ignored_points[id] = true
59+
set_point_disabled(id, false)
60+
4561
if has_point(source_id) and has_point(target_id):
4662
# Check to see if we want to un-disable the source/target cells.
47-
var is_source_disabled: = is_point_disabled(source_id)
48-
var is_target_disabled: = is_point_disabled(target_id)
49-
if allow_disabled_source:
63+
if (occupancy_flags & FLAG_ALLOW_SOURCE_OCCUPANT) != 0:
64+
print("Allow source occupant!")
65+
ignored_points[source_id] = is_point_disabled(source_id)
5066
set_point_disabled(source_id, false)
51-
if allow_disabled_target:
67+
if (occupancy_flags & FLAG_ALLOW_TARGET_OCCUPANT) != 0:
68+
print("Allow target occupant!")
69+
ignored_points[target_id] = is_point_disabled(target_id)
5270
set_point_disabled(target_id, false)
5371

5472
for path_coord: Vector2i in get_point_path(source_id, target_id):
5573
if path_coord != source_coord: # Don't include the source as the first path element.
5674
move_path.append(path_coord)
5775

58-
# If the source/target cells had originally been disabled, re-disable them here.
59-
if allow_disabled_source:
60-
set_point_disabled(source_id, is_source_disabled)
61-
if allow_disabled_target:
62-
set_point_disabled(target_id, is_target_disabled)
63-
64-
65-
#set_point_disabled(source_id, is_source_disabled)
76+
# Change any enabled cells back to their previous state.
77+
for id in ignored_points:
78+
set_point_disabled(id, ignored_points[id])
6679

6780
return move_path
6881

src/field/gamepieces/controllers/path_loop_ai_controller.gd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func _find_move_path() -> bool:
100100
var source: = Gameboard.pixel_to_cell(path_to_follow.get_point_position(i-1) + _path_origin)
101101
var target: = Gameboard.pixel_to_cell(path_to_follow.get_point_position(i) + _path_origin)
102102

103-
var path_segment: = Gameboard.pathfinder.get_path_to_cell(source, target, true, true)
103+
var path_segment: = Gameboard.pathfinder.get_path_to_cell(source, target)
104104
if path_segment.is_empty():
105105
#push_error("'%s' PathLoopAiController::_find_waypoints_from_line2D() error - " % name +
106106
#"Failed to find a path between cells %s and %s." % [source, target])
@@ -114,7 +114,8 @@ func _find_move_path() -> bool:
114114
var first_cell: = Gameboard.pixel_to_cell(path_to_follow.get_point_position(0) + _path_origin)
115115

116116
# If we've made it this far there must be a path between the first and last cell.
117-
move_path.append_array(Gameboard.pathfinder.get_path_to_cell(last_cell, first_cell, true, true))
117+
move_path.append_array(Gameboard.pathfinder.get_path_to_cell(last_cell, first_cell,
118+
Pathfinder.FLAG_ALLOW_SOURCE_OCCUPANT | Pathfinder.FLAG_ALLOW_TARGET_OCCUPANT))
118119
return true
119120

120121

src/field/gamepieces/player_controller.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func _process(_delta: float) -> void:
4848
else:
4949
GamepieceRegistry.move_gamepiece(_gamepiece, target_cell)
5050
_gamepiece.move_to(Gameboard.cell_to_pixel(target_cell))
51-
print(new_move_path)
51+
5252
#print(Gameboard.pathfinder.get_path_to_cell())
5353
# If path is valid, move.
5454

0 commit comments

Comments
 (0)