PageRenderTime 53ms CodeModel.GetById 37ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

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