Ah it's so satisfying when you get something to work after working on it forever!! :)
I'm... really sorry you have to see this:
func find_alternative_routes(map_position_list:Array):
var all_connected = []
var first_map_point = map_position_list.front()
var end_map_point = map_position_list.back()
for map_position in map_position_list:
if not (map_position in all_connected):
all_connected.append(map_position)
var potential_connected = []
var connected = get_connected_positions(map_position)
# there are potentially multiple paths from the start
if map_position == first_map_point and connected.size() > 1:
set_map_position_disabled(map_position)
potential_connected = check_for_path(connected, map_position_list)
set_map_position_enabled(map_position)
# we have more than just the coming from and going to points
elif connected.size() > 2:
set_map_position_disabled(map_position)
potential_connected = check_for_path(connected, map_position_list)
set_map_position_enabled(map_position)
for point in potential_connected:
if not (point in all_connected):
all_connected.append(point)
return all_connected
func check_for_path(starting_points, known_path):
var potential_connections = []
for point in starting_points:
# already been down that road
if point in known_path:
pass
else:
var path_to_end = _get_path(point, known_path.back())
if not path_to_end.empty():
for p in path_to_end:
if not(p in potential_connections):
potential_connections.append(p)
return potential_connections
func get_connected_positions(map_position):
var index = _calculate_point_index(map_position)
var connected_indexes = aStarNode.get_point_connections(index)
var positions = []
for _index in connected_indexes:
var pos = aStarNode.get_point_position(_index)
positions.append(pos)
return positions
func set_map_position_enabled(pos):
var point_to_enable = aStarNode.get_closest_point(pos, true)
aStarNode.set_point_disabled(point_to_enable, false)
func set_map_position_disabled(pos):
var point_to_disable = aStarNode.get_closest_point(pos, true)
aStarNode.set_point_disabled(point_to_disable, true)
It could really use some refactoring but I think it's bug free. Call the top function with the results of get_path()
(while still in MAP coordinates!!).
Basically you loop through all the points, if you have more than 2 connections, meaning there's a potential branch that could lead on an alternative path to the end, disable that point (as to not back track) and try to find a path to the end from the alternative branch.
Lemme know if you need anymore help!