PageRenderTime 55ms CodeModel.GetById 43ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/army/hwh/info.erl

http://erlbattle.googlecode.com/
Erlang | 57 lines | 45 code | 9 blank | 3 comment | 0 complexity | 89baf5331dacbdfd6679a13cd8a568e8 MD5 | raw file
 1-module(hwh.info).
 2-compile(export_all).
 3-include("hwh_schema.hrl").
 4-include("schema.hrl").
 5
 6
 7start(Master, Side) ->
 8	process_flag(trap_exit, true),
 9	Tb = .ets:new(grid, [set, protected, {keypos, #grid_info.id}]),
10	init(Tb),
11	Master ! {grid, Tb},
12	loop(Tb, Side, Master).
13
14%%?????3*3 ??????
15init(Tb) ->
16	Grids = [{0,0}, {0,1}, {0,2},
17		 {1,0}, {1,1}, {1,2},
18		 {2,0}, {2,1}, {2,2}],
19	.lists:foreach(fun(ID) -> .ets:insert(Tb, #grid_info{id=ID}) end, 
20		Grids).
21
22
23loop(Tb, Side, Master) -> 
24	reset(Tb),
25	Enemys = .battlefield:get_soldier_by_side(util:enemy(Side)),
26	update_grid(Tb, Enemys, #grid_info.enemy),
27	Friends = .battlefield:get_soldier_by_side(Side),
28	update_grid(Tb, Friends, #grid_info.friend),
29
30	receive
31		{'EXIT', Master, finish} -> ok;
32		_ -> loop(Tb, Side, Master)
33	after 1 -> loop(Tb, Side, Master)
34	end.
35
36%% ????????????
37update_grid(Tb, Soldiers, Key) ->
38	.lists:foreach(
39		fun(S) ->
40			{X, Y} = S#soldier.position,
41			ID = {X div 5, Y div 5},
42			[Info|_] = .ets:lookup(Tb, ID),
43			NewSoldiers = [S#soldier.id|element(Key, Info)],
44			.ets:update_element(Tb, ID, {Key, NewSoldiers})
45		end,
46		Soldiers).
47
48
49%% ??????		
50reset(Tb) ->
51	Grids = [{0,0}, {0,1}, {0,2},
52		 {1,0}, {1,1}, {1,2},
53		 {2,0}, {2,1}, {2,2}],
54	.lists:foreach(fun(ID) -> .ets:update_element(Tb, ID, {#grid_info.friend, []}) end,
55		Grids),
56	.lists:foreach(fun(ID) -> .ets:update_element(Tb, ID, {#grid_info.enemy, []}) end, 
57		Grids).