behavior3行为树
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

45 行
1.2 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_path).
  11. -include("behavior3.hrl").
  12. -include("example.hrl").
  13. %% API
  14. -export([tick/2, get_grid_path/1]).
  15. tick(_BTree, #{cur_grid := CurGrid} = State) ->
  16. State1 = State#{grid_list := get_grid_path(CurGrid)},
  17. {?BT_SUCCESS, State1}.
  18. get_grid_path(CurGrid) ->
  19. List = [{1, 0}, {-1, 0}, {0, 1}, {0, -1}],
  20. Max = length(List),
  21. List1 = [E || {_, E} <- lists:sort([{?RAND(1, Max), E} || E <- List])],
  22. case get_grid_path_1(List1, CurGrid, []) of
  23. [] ->
  24. get_grid_path(CurGrid);
  25. Result ->
  26. Result
  27. end.
  28. get_grid_path_1([{X, Y} | T], {CurX, CurY}, Result) ->
  29. X1 = CurX + X,
  30. Y1 = CurY + Y,
  31. Grid = {X1, Y1},
  32. case X1 >= 0 andalso X1 =< ?MAX_X andalso Y1 >= 0 andalso Y1 =< ?MAX_Y of
  33. true ->
  34. get_grid_path_1(T, Grid, [Grid | Result]);
  35. false ->
  36. []
  37. end;
  38. get_grid_path_1([], _, Result) ->
  39. Result.