behavior3行为树
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

59 wiersze
1.9 KiB

  1. %%%-------------------------------------------------------------------
  2. %%% @author DY
  3. %%% @copyright (C) 2020, <COMPANY>
  4. %%% @doc
  5. %%% 收集终点路径
  6. %%% 返回:SUCCESS
  7. %%% @end
  8. %%% Created : 07. 10月 2020 15:13
  9. %%%-------------------------------------------------------------------
  10. -module(action_collect_dest_path).
  11. -include("behavior3.hrl").
  12. -include("example.hrl").
  13. %% API
  14. -export([tick/2]).
  15. tick(_BTree, #{cur_grid := CurGrid, grid_list := GirdList} = State) ->
  16. case GirdList of
  17. [] ->
  18. EndGrid = get_end_grid(CurGrid),
  19. State1 = State#{grid_list := get_grid_path(CurGrid, EndGrid)},
  20. ?INFO("~ts:找到穿传说中的“大蒜”的位置,要尽快到达那里!", [maps:get(uid, State1)]),
  21. {?BT_SUCCESS, State1};
  22. _ ->
  23. ?INFO("~ts:天要黑了,要尽快到达那里!", [maps:get(uid, State)]),
  24. {?BT_SUCCESS, State}
  25. end.
  26. get_end_grid(CurGrid) ->
  27. EndGridList = [{0, 0}, {0, ?MAX_X}, {?MAX_Y, ?MAX_X}, {?MAX_Y, 0}],
  28. {_, EndGrid} = hd(lists:sort([{calc_distance(CurGrid, Grid), Grid} || Grid <- EndGridList])),
  29. EndGrid.
  30. calc_distance({X1, Y1}, {X2, Y2}) ->
  31. math:sqrt(math:pow(X1 - X2, 2) + math:pow(Y1 - Y2, 2)).
  32. get_grid_path(CurGrid, EndGrid) ->
  33. case EndGrid of
  34. {0, 0} ->
  35. get_grid_path(CurGrid, [EndGrid], 1, 1);
  36. {0, ?MAX_X} ->
  37. get_grid_path(CurGrid, [EndGrid], 1, -1);
  38. {?MAX_Y, ?MAX_X} ->
  39. get_grid_path(CurGrid, [EndGrid], -1, -1);
  40. {?MAX_Y, 0} ->
  41. get_grid_path(CurGrid, [EndGrid], -1, 1)
  42. end.
  43. get_grid_path(CurGrid, GridPath, XInc, YInc) ->
  44. case GridPath of
  45. [CurGrid | _] ->
  46. GridPath;
  47. [{X, Y} | _] ->
  48. case Y + YInc of
  49. Y1 when Y1 == 0 orelse Y1 == ?MAX_X ->
  50. get_grid_path(CurGrid, [{X + XInc, Y1}, {X, Y1} | GridPath], XInc, bnot YInc + 1);
  51. Y1 ->
  52. get_grid_path(CurGrid, [{X, Y1} | GridPath], XInc, YInc)
  53. end
  54. end.