/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
- -module(randomArmy).
- -include("schema.hrl").
- -export([run/3]).
-
-
- run(Channel, Side, Queue) ->
- process_flag(trap_exit, true),
- loop(Channel, Side, Queue).
-
- loop(Channel, Side, Queue) ->
-
- Army = ?PreDef_army,
- lists:foreach(
- fun(SoldierID) ->
- %% ????????????????
- case ask_commander(SoldierID, Side) of
- none ->
- none;
- Action ->
- Channel ! {command, Action, SoldierID, 0, random:uniform(10)}
- end
- end,
-
- Army),
-
- %% ??????
- receive
- %% ????
- {'EXIT',_FROM, _Reason} ->
- io:format("RandomArmy Stop Attack! ~n",[]);
-
- _ ->
- loop(Channel, Side, Queue)
-
- after 100 ->
- loop(Channel, Side, Queue)
-
- end.
-
- ask_commander(SoldierID, Side) ->
- %% TODO: ??????????
- case battlefield:get_soldier(SoldierID, Side) of
- %% ????
- none -> none;
- Soldier ->
- {X, Y} = Soldier#soldier.position,
- %% ????
- Around = [{?DirEast, {X+1, Y}},
- {?DirWest, {X-1, Y}},
- {?DirSouth, {X, Y+1}},
- {?DirNorth, {X, Y-1}}],
- %% ??????
- Status = find_around(Around, Side, []),
- %% ??????????
- command_decision(Soldier#soldier.facing, Status)
- end.
-
- %% ???????
- %% ?????: invalid -- ?????
- %% nobody -- ??
- %% comrade -- ??
- %% enemy -- ??
- find_around([H|T], Side, Ret) ->
- {Direction, Position={X, Y}} = H,
- if
- X >= 0 andalso X =< 14 andalso Y >= 0 andalso Y =< 14 ->
- find_around(T, Side, [{Direction, anybody_there(Position, Side)}|Ret]);
- true -> find_around(T, Side, [{Direction, invalid}|Ret])
- end;
- find_around([], _Side, Ret) ->
- Ret.
-
-
- %% ?????????,????
- %% ????? nobody -- ??
- %% enemy -- ??
- %% comrade -- ???
- anybody_there(Position, Side) ->
- case battlefield:get_soldier_by_position(Position) of
- %% ?????
- none -> nobody;
- Soldier ->
- {_, MySide} = Soldier#soldier.id,
-
- if
- %% ??????
- MySide =/= Side -> enemy;
- %% ???
- true -> comrade
- end
- end.
-
-
- %% ??????????
- %% ?????????
- command_decision(Facing, Status) ->
- %% ??????? ?????
- case lists:member({Facing, enemy}, Status) of
- true -> ?ActionAttack;
- false ->
- %% ??????? ????
- case lists:filter(fun({_D, P}) -> P=:=enemy end, Status) of
- [H|_] ->
- {Direction, _} = H,
- turn_around(Direction);
- %% ??????
- [] ->
- %% ????????
- case lists:filter(fun({_D, P}) -> P=:=nobody end, Status) of
- %% ???,???????
- [] -> ?ActionWait;
- Any -> random_action(Facing, Any)
- end
- end
- end.
-
- %% ??????
- %%
- random_action(Facing, Status) ->
- Len = length(Status),
- Ran = random:uniform(Len),
- {Direction, _} = lists:nth(Ran, Status),
- if Direction =:= Facing ->
- ?ActionForward;
- true ->
- turn_around(Direction)
- end.
-
-
-
- %% ????
- turn_around(?DirEast) -> ?ActionTurnEast;
- turn_around(?DirWest) -> ?ActionTurnWest;
- turn_around(?DirNorth) -> ?ActionTurnNorth;
- turn_around(?DirSouth) -> ?ActionTurnSouth.