/army/karlcpp.random_army/randomArmy.erl

http://erlbattle.googlecode.com/ · Erlang · 136 lines · 85 code · 21 blank · 30 comment · 3 complexity · 6d8541e862b099e18915ce9e91658d7f MD5 · raw file

  1. -module(randomArmy).
  2. -include("schema.hrl").
  3. -export([run/3]).
  4. run(Channel, Side, Queue) ->
  5. process_flag(trap_exit, true),
  6. loop(Channel, Side, Queue).
  7. loop(Channel, Side, Queue) ->
  8. Army = ?PreDef_army,
  9. lists:foreach(
  10. fun(SoldierID) ->
  11. %% ????????????????
  12. case ask_commander(SoldierID, Side) of
  13. none ->
  14. none;
  15. Action ->
  16. Channel ! {command, Action, SoldierID, 0, random:uniform(10)}
  17. end
  18. end,
  19. Army),
  20. %% ??????
  21. receive
  22. %% ????
  23. {'EXIT',_FROM, _Reason} ->
  24. io:format("RandomArmy Stop Attack! ~n",[]);
  25. _ ->
  26. loop(Channel, Side, Queue)
  27. after 100 ->
  28. loop(Channel, Side, Queue)
  29. end.
  30. ask_commander(SoldierID, Side) ->
  31. %% TODO: ??????????
  32. case battlefield:get_soldier(SoldierID, Side) of
  33. %% ????
  34. none -> none;
  35. Soldier ->
  36. {X, Y} = Soldier#soldier.position,
  37. %% ????
  38. Around = [{?DirEast, {X+1, Y}},
  39. {?DirWest, {X-1, Y}},
  40. {?DirSouth, {X, Y+1}},
  41. {?DirNorth, {X, Y-1}}],
  42. %% ??????
  43. Status = find_around(Around, Side, []),
  44. %% ??????????
  45. command_decision(Soldier#soldier.facing, Status)
  46. end.
  47. %% ???????
  48. %% ?????: invalid -- ?????
  49. %% nobody -- ??
  50. %% comrade -- ??
  51. %% enemy -- ??
  52. find_around([H|T], Side, Ret) ->
  53. {Direction, Position={X, Y}} = H,
  54. if
  55. X >= 0 andalso X =< 14 andalso Y >= 0 andalso Y =< 14 ->
  56. find_around(T, Side, [{Direction, anybody_there(Position, Side)}|Ret]);
  57. true -> find_around(T, Side, [{Direction, invalid}|Ret])
  58. end;
  59. find_around([], _Side, Ret) ->
  60. Ret.
  61. %% ?????????,????
  62. %% ????? nobody -- ??
  63. %% enemy -- ??
  64. %% comrade -- ???
  65. anybody_there(Position, Side) ->
  66. case battlefield:get_soldier_by_position(Position) of
  67. %% ?????
  68. none -> nobody;
  69. Soldier ->
  70. {_, MySide} = Soldier#soldier.id,
  71. if
  72. %% ??????
  73. MySide =/= Side -> enemy;
  74. %% ???
  75. true -> comrade
  76. end
  77. end.
  78. %% ??????????
  79. %% ?????????
  80. command_decision(Facing, Status) ->
  81. %% ??????? ?????
  82. case lists:member({Facing, enemy}, Status) of
  83. true -> ?ActionAttack;
  84. false ->
  85. %% ??????? ????
  86. case lists:filter(fun({_D, P}) -> P=:=enemy end, Status) of
  87. [H|_] ->
  88. {Direction, _} = H,
  89. turn_around(Direction);
  90. %% ??????
  91. [] ->
  92. %% ????????
  93. case lists:filter(fun({_D, P}) -> P=:=nobody end, Status) of
  94. %% ???,???????
  95. [] -> ?ActionWait;
  96. Any -> random_action(Facing, Any)
  97. end
  98. end
  99. end.
  100. %% ??????
  101. %%
  102. random_action(Facing, Status) ->
  103. Len = length(Status),
  104. Ran = random:uniform(Len),
  105. {Direction, _} = lists:nth(Ran, Status),
  106. if Direction =:= Facing ->
  107. ?ActionForward;
  108. true ->
  109. turn_around(Direction)
  110. end.
  111. %% ????
  112. turn_around(?DirEast) -> ?ActionTurnEast;
  113. turn_around(?DirWest) -> ?ActionTurnWest;
  114. turn_around(?DirNorth) -> ?ActionTurnNorth;
  115. turn_around(?DirSouth) -> ?ActionTurnSouth.