Skip to content

Commit 611c28a

Browse files
committed
fix: handle edge case of path of length 1
1 parent 9f98078 commit 611c28a

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
@@ -293,24 +293,32 @@ defmodule BotManager.BotStateMachine do
293293
shortest_path = AStarNative.a_star_shortest_path(from, to, bot_state_machine.collision_grid)
294294

295295
# If we don't have a path, retry finding new position in map
296-
if Enum.empty?(shortest_path) do
297-
Map.put(bot_state_machine, :path_towards_position, nil)
298-
|> Map.put(:position_to_move_to, nil)
299-
else
300-
# Replacing first and last points with the actual start and end points
301-
shortest_path = ([from] ++ Enum.slice(shortest_path, 1, Enum.count(shortest_path) - 2) ++ [to])
302-
|> AStarNative.simplify_path(bot_state_machine.obstacles)
303-
|> SplinePath.smooth_path()
304-
305-
# The first point should only be necessary to simplify the path
306-
shortest_path = tl(shortest_path)
307-
308-
Map.put(bot_state_machine, :position_to_move_to, position_to_move_to)
309-
|> Map.put(
310-
:path_towards_position,
311-
shortest_path
312-
)
313-
|> Map.put(:last_time_position_changed, :os.system_time(:millisecond))
296+
cond do
297+
Enum.empty?(shortest_path) ->
298+
Map.put(bot_state_machine, :path_towards_position, nil)
299+
|> Map.put(:position_to_move_to, nil)
300+
length(shortest_path) == 1 ->
301+
Map.put(bot_state_machine, :position_to_move_to, position_to_move_to)
302+
|> Map.put(
303+
:path_towards_position,
304+
[to]
305+
)
306+
|> Map.put(:last_time_position_changed, :os.system_time(:millisecond))
307+
true ->
308+
# Replacing first and last points with the actual start and end points
309+
shortest_path = ([from] ++ Enum.slice(shortest_path, 1, Enum.count(shortest_path) - 2) ++ [to])
310+
|> AStarNative.simplify_path(bot_state_machine.obstacles)
311+
|> SplinePath.smooth_path()
312+
313+
# The first point should only be necessary to simplify the path
314+
shortest_path = tl(shortest_path)
315+
316+
Map.put(bot_state_machine, :position_to_move_to, position_to_move_to)
317+
|> Map.put(
318+
:path_towards_position,
319+
shortest_path
320+
)
321+
|> Map.put(:last_time_position_changed, :os.system_time(:millisecond))
314322
end
315323
end
316324
end

0 commit comments

Comments
 (0)