@@ -327,24 +327,32 @@ defmodule BotManager.BotStateMachine do
327
327
shortest_path = AStarNative . a_star_shortest_path ( from , to , bot_state_machine . collision_grid )
328
328
329
329
# 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 ) )
348
356
end
349
357
end
350
358
end
0 commit comments