PageRenderTime 62ms CodeModel.GetById 53ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/core/engine/battleRecorder.erl

http://erlbattle.googlecode.com/
Erlang | 129 lines | 87 code | 27 blank | 15 comment | 16 complexity | 3259f3d4ca90ef0bda4dbe5cf0421b75 MD5 | raw file
  1-module(battleRecorder).
  2-include("schema.hrl").
  3-export([start/1]).
  4
  5start(Pid) ->
  6	
  7	process_flag(trap_exit, true),
  8	
  9	io:format("Battle Recorder Begin to Run ~n",[]),
 10	
 11	%% ????????????
 12	ets:new(battle_record,[ordered_set,named_table,private]),
 13
 14	run(Pid,1).
 15
 16%% ????????	
 17run(Pid, Seq) ->
 18
 19	receive
 20		%% ???????????
 21		{'EXIT', Pid , _Msg} -> 
 22			recordBattle();	
 23		{Pid,Record} -> 
 24			ets:insert(battle_record, {Seq, Record} ),
 25			run(Pid, Seq + 1);
 26		_ -> 
 27			run(Pid,Seq)   %% ????????
 28	end.
 29
 30%% ???????????????
 31recordBattle() ->
 32	
 33	%%????????
 34	Records = ets:tab2list(battle_record),
 35	ets:delete(battle_record),
 36	
 37	%% open file
 38	{_Ok, Io} = file:open(?EbBattleLogFile,[write]),		
 39	
 40	%% ?????
 41	initBattleField(Io),
 42	
 43	%% ????????????????
 44	lists:foreach(
 45		fun(RawRecord) ->
 46			{_Seq, Record} = RawRecord,
 47			case Record of 
 48				{?LogCmdAction, Time, Id, Action, Position, Facing, Hp}->
 49					{X,Y} = Position,
 50
 51					io:fwrite(Io,"~p,~p,~p,~p,~p,~p,~p,0~n" , [Time, changeAction(Action), X, Y, uniqueId(Id), simpleDirection(Facing), Hp]);
 52				{?LogCmdPlan, Id, Action, ActionEffectTime} ->
 53					if 
 54						Action == ?ActionWait  ->
 55							io:fwrite(Io,"plan,~p,[]~n" , [uniqueId(Id)]);
 56						true ->
 57							io:fwrite(Io,"plan,~p,~p@~p~n" , [uniqueId(Id), changeAction(Action), ActionEffectTime])
 58					end;
 59				{?LogCmdStatus,Time, Id, Position, Facing, Hp, HpLost} ->
 60					{X,Y} = Position,
 61					io:fwrite(Io,"~p,~p,~p,~p,~p,~p,~p,~p~n" , [Time,'status', X,Y, uniqueId(Id), simpleDirection(Facing), Hp, HpLost]);
 62				{?LogCmdResult, Result}->
 63					io:fwrite(Io,"result,~p~n" , [list_to_atom(Result)]);
 64				_ ->
 65					none
 66				end
 67			end,
 68			Records),
 69
 70	%% close file
 71	file:close(Io).	
 72
 73%% ??????????????	
 74initBattleField(Io) ->
 75
 76	Army = ?PreDef_army,
 77
 78    %% ??????
 79	lists:foreach(
 80		fun(Id) ->
 81			io:fwrite(Io,"~p,~p,~p,~p,~p,~p,~p,~p~n" , [0,'stand', 0,1+Id, Id, 'e', 100, 0])
 82		end,
 83		Army),
 84
 85	%% ??????
 86	lists:foreach(
 87		fun(Id) ->
 88			io:fwrite(Io,"~p,~p,~p,~p,~p,~p,~p,~p~n" , [0,'stand', 14,1+Id, Id+10, 'w', 100, 0])
 89		end,
 90		Army).
 91		
 92			
 93%% record ?????????? id =10 ??
 94uniqueId(Id) ->
 95
 96	{Sid, Side} = Id,
 97	
 98	if 
 99		Side == ?BlueSide ->
100			Sid + 10;
101		true ->
102			Sid
103	end.
104
105%% ????	
106changeAction(Action) ->	
107	if
108		Action == ?ActionMove -> 'walk';
109		Action == ?ActionForward -> 'walk';
110		Action == ?ActionAttack -> 'fight';
111		Action == ?ActionBack -> 'back';
112		Action == ?ActionTurnEast -> 'turnEast';
113		Action == ?ActionTurnWest -> 'turnWest';
114		Action == ?ActionTurnSouth -> 'turnSouth';
115		Action == ?ActionTurnNorth -> 'turnNorth';
116		true -> list_to_atom(Action)
117	end.
118	
119	
120%% ????
121simpleDirection(Facing) ->
122
123	if
124		Facing == ?DirWest ->'w';
125		Facing == ?DirEast ->'e';
126		Facing == ?DirNorth ->'n';
127		Facing == ?DirSouth ->'s'
128	end.
129