Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

73 рядки
1.9 KiB

8 роки тому
  1. %% @author zouv
  2. %% @doc use bte for ai
  3. -module(bte_ai).
  4. -include_lib("erlbte/include/common.hrl").
  5. -export([
  6. init/1,
  7. tick/1
  8. ]).
  9. init(Data) ->
  10. {ok, Json, _} = rfc4627:decode(Data), % depend on rfc4627_jsonrpc
  11. RootId = get_field_string(Json, "root"),
  12. {ok, NodesJson} = rfc4627:get_field(Json, "nodes"),
  13. NodeList = init_nodes([RootId], NodesJson, []),
  14. CustomNodesModule = bte_node_behaviour:init_nodes_moudle(?MODULE), % load self-defined bte node
  15. BteConfigData =
  16. #r_bte_config_data{
  17. root_id = RootId,
  18. nodes = NodeList,
  19. custom_nodes_moudle = CustomNodesModule
  20. },
  21. BteStatus = bte_behavior:init(BteConfigData),
  22. {ok, BteStatus}.
  23. init_nodes([], _NodesJson, NodeList) ->
  24. NodeList;
  25. init_nodes([Id | LeftIdList], NodesJson, NodeList) ->
  26. {ok, NodeJson} = rfc4627:get_field(NodesJson, Id),
  27. Name = get_field_string(NodeJson, "name"),
  28. {ok, Parameters} = rfc4627:get_field(NodeJson, "parameters"),
  29. ChildId = binary_to_list(rfc4627:get_field(NodeJson, "child", <<>>)),
  30. Children = lists:map(fun(E) -> binary_to_list(E) end, rfc4627:get_field(NodeJson, "children", [])),
  31. BteNode =
  32. #r_bte_config_node{
  33. id = Id,
  34. name = Name,
  35. parameters = get_all(Parameters),
  36. child = ChildId,
  37. children = Children
  38. },
  39. NewNodeList = [BteNode | NodeList],
  40. if
  41. ChildId /= "" ->
  42. init_nodes([ChildId | LeftIdList], NodesJson, NewNodeList);
  43. Children /= [] ->
  44. init_nodes(Children ++ LeftIdList, NodesJson, NewNodeList);
  45. true ->
  46. init_nodes(LeftIdList, NodesJson, NewNodeList)
  47. end.
  48. get_field_string(Json, Key) ->
  49. {ok, Value} = rfc4627:get_field(Json, Key),
  50. binary_to_list(Value).
  51. get_all(Parameters) ->
  52. {obj, PrapList} = Parameters,
  53. lists:map(fun({EKey, EValue}) ->
  54. if
  55. is_binary(EValue) ->
  56. ENewValue = binary_to_list(EValue);
  57. true ->
  58. ENewValue = EValue
  59. end,
  60. {EKey, ENewValue}
  61. end,
  62. PrapList).
  63. tick(BteStatus) ->
  64. bte_behavior:tick(BteStatus).