Skip to content

Commit ccee3ae

Browse files
committed
fix: handle edge case of path of length 1
1 parent 166394c commit ccee3ae

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

apps/bot_manager/lib/bot_state_machine.ex

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -327,24 +327,32 @@ defmodule BotManager.BotStateMachine do
327327
shortest_path = AStarNative.a_star_shortest_path(from, to, bot_state_machine.collision_grid)
328328

329329
# If we don't have a path, retry finding new position in map
330-
if Enum.empty?(shortest_path) do
331-
Map.put(bot_state_machine, :path_towards_position, nil)
332-
|> Map.put(:position_to_move_to, nil)
333-
else
334-
# Replacing first and last points with the actual start and end points
335-
shortest_path = ([from] ++ Enum.slice(shortest_path, 1, Enum.count(shortest_path) - 2) ++ [to])
336-
|> AStarNative.simplify_path(bot_state_machine.obstacles)
337-
|> SplinePath.smooth_path()
338-
339-
# The first point should only be necessary to simplify the path
340-
shortest_path = tl(shortest_path)
341-
342-
Map.put(bot_state_machine, :position_to_move_to, position_to_move_to)
343-
|> Map.put(
344-
:path_towards_position,
345-
shortest_path
346-
)
347-
|> Map.put(:last_time_position_changed, :os.system_time(:millisecond))
330+
cond do
331+
Enum.empty?(shortest_path) ->
332+
Map.put(bot_state_machine, :path_towards_position, nil)
333+
|> Map.put(:position_to_move_to, nil)
334+
length(shortest_path) == 1 ->
335+
Map.put(bot_state_machine, :position_to_move_to, position_to_move_to)
336+
|> Map.put(
337+
:path_towards_position,
338+
[to]
339+
)
340+
|> Map.put(:last_time_position_changed, :os.system_time(:millisecond))
341+
true ->
342+
# Replacing first and last points with the actual start and end points
343+
shortest_path = ([from] ++ Enum.slice(shortest_path, 1, Enum.count(shortest_path) - 2) ++ [to])
344+
|> AStarNative.simplify_path(bot_state_machine.obstacles)
345+
|> SplinePath.smooth_path()
346+
347+
# The first point should only be necessary to simplify the path
348+
shortest_path = tl(shortest_path)
349+
350+
Map.put(bot_state_machine, :position_to_move_to, position_to_move_to)
351+
|> Map.put(
352+
:path_towards_position,
353+
shortest_path
354+
)
355+
|> Map.put(:last_time_position_changed, :os.system_time(:millisecond))
348356
end
349357
end
350358
end

0 commit comments

Comments
 (0)