Commit | Line | Data |
---|---|---|
982ec720 | 1 | -module(life_time). |
d2a0e2f9 SK |
2 | -behaviour(gen_server). |
3 | ||
4 | ||
5 | %% API | |
1aa9333c | 6 | -export([start_link/3 |
29199e7e | 7 | ,report_state/3 |
d2a0e2f9 SK |
8 | ]). |
9 | ||
10 | %% Callbacks | |
11 | -export([init/1 | |
12 | ,handle_call/3 | |
13 | ,handle_cast/2 | |
14 | ,handle_info/2 | |
15 | ,terminate/2 | |
16 | ,code_change/3 | |
17 | ]). | |
18 | ||
19 | ||
a43ebd2e | 20 | -define(INTERVAL, 100). % In milliseconds |
d2a0e2f9 | 21 | |
1d652075 SK |
22 | -define(CHAR_DEAD, 32). % " " |
23 | -define(CHAR_ALIVE, 111). % "o" | |
24 | -define(CHAR_BAR, 45). % "-" | |
d2a0e2f9 SK |
25 | |
26 | ||
27 | -record(state, {x :: integer() | |
1aa9333c | 28 | ,y :: integer() |
d2a0e2f9 SK |
29 | ,cells :: list(atom()) |
30 | ,num_cells :: integer() | |
1ffcb224 SK |
31 | ,num_dead :: integer() |
32 | ,num_alive :: integer() | |
d2a0e2f9 SK |
33 | ,state_pairs :: list(tuple(integer(), integer())) | [] |
34 | ,replies_pending :: integer() | |
0a9b8c17 | 35 | ,gen_id :: integer() |
c04c5832 | 36 | ,gen_began :: erlang:timestamp() |
d2a0e2f9 SK |
37 | }). |
38 | ||
39 | ||
40 | %% ============================================================================ | |
41 | %% API | |
42 | %% ============================================================================ | |
43 | ||
1aa9333c | 44 | start_link(X, Y, Cells) -> |
d2a0e2f9 | 45 | ServerName = {local, ?MODULE}, |
1aa9333c | 46 | Args = [X, Y, Cells], |
d2a0e2f9 SK |
47 | Opts = [], |
48 | gen_server:start_link(ServerName, ?MODULE, Args, Opts). | |
49 | ||
50 | ||
29199e7e SK |
51 | report_state(CellID, GenID, CellState) -> |
52 | gen_server:cast(?MODULE, {report_state, {CellID, GenID, CellState}}). | |
d2a0e2f9 SK |
53 | |
54 | ||
90456d08 SK |
55 | %% ============================================================================ |
56 | %% Callbacks (unused) | |
57 | %% ============================================================================ | |
58 | ||
59 | handle_call(_Msg, _From, State) -> {reply, ok, State}. | |
90456d08 SK |
60 | code_change(_Old, State, _Other) -> {ok, State}. |
61 | terminate(_Reason, State) -> {ok, State}. | |
62 | ||
63 | ||
d2a0e2f9 SK |
64 | %% ============================================================================ |
65 | %% Callbacks | |
66 | %% ============================================================================ | |
67 | ||
1aa9333c | 68 | init([X, Y, Cells]) -> |
0a9b8c17 SK |
69 | State = #state{x = X |
70 | ,y = Y | |
71 | ,cells = Cells | |
72 | ,num_cells = length(Cells) | |
73 | ,state_pairs = [] | |
74 | ,replies_pending = 0 | |
75 | ,gen_id = 0 | |
d2a0e2f9 | 76 | }, |
1d121bcf | 77 | ok = schedule_next_gen(), |
d2a0e2f9 SK |
78 | {ok, State}. |
79 | ||
80 | ||
7319e378 | 81 | handle_info(next_gen, |
172421cb SK |
82 | #state{cells=Cells |
83 | ,num_cells=NumCells | |
84 | ,state_pairs=[] | |
0a9b8c17 | 85 | ,gen_id=GenID |
172421cb SK |
86 | }=State) -> |
87 | ||
c04c5832 | 88 | GenBegan = os:timestamp(), |
0a9b8c17 | 89 | NewGenID = GenID + 1, |
79a2bc14 | 90 | ok = life_lib:cast_one2all(Cells, {next_gen, NewGenID}), |
1ffcb224 SK |
91 | NewState = State#state{replies_pending=NumCells |
92 | ,gen_id=NewGenID | |
c04c5832 | 93 | ,gen_began=GenBegan |
1ffcb224 SK |
94 | ,num_dead=0 |
95 | ,num_alive=0 | |
96 | }, | |
97 | {noreply, NewState}; | |
d2a0e2f9 | 98 | |
7319e378 SK |
99 | handle_info(_Msg, State) -> |
100 | {noreply, State}. | |
101 | ||
102 | ||
29199e7e | 103 | handle_cast({report_state, {CellID, GenID, CellState}}, |
d2a0e2f9 | 104 | #state{x=X |
1aa9333c | 105 | ,y=Y |
1ffcb224 SK |
106 | ,num_dead=NDead |
107 | ,num_alive=NAlive | |
d2a0e2f9 SK |
108 | ,state_pairs=StatePairs |
109 | ,replies_pending=RepliesPending | |
0a9b8c17 | 110 | ,gen_id=GenID |
c04c5832 | 111 | ,gen_began=GenBegan |
812a14ea | 112 | ,num_cells=NumCells |
d2a0e2f9 SK |
113 | }=State) -> |
114 | ||
29199e7e | 115 | NewStatePairs = [{CellID, CellState} | StatePairs], |
d2a0e2f9 | 116 | NewRepliesPending = RepliesPending - 1, |
1ffcb224 SK |
117 | {NewNDead, NewNAlive} = increment_dead_or_alive(CellState, NDead, NAlive), |
118 | NewState = State#state{replies_pending=NewRepliesPending | |
119 | ,num_dead=NewNDead | |
120 | ,num_alive=NewNAlive | |
121 | }, | |
d2a0e2f9 SK |
122 | |
123 | case NewRepliesPending of | |
124 | 0 -> | |
3803d7be SK |
125 | SortedStatePairs = lists:sort( |
126 | fun({A, _}, {B, _}) -> A < B end, | |
127 | NewStatePairs | |
128 | ), | |
d2a0e2f9 | 129 | StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs], |
eec4cd1f | 130 | |
c04c5832 SK |
131 | GenDuration = timer:now_diff(os:timestamp(), GenBegan) / 1000000, |
132 | ||
133 | ok = life_observer:log_generation(GenID, GenDuration, NewNDead, NewNAlive), | |
c8c75648 | 134 | |
812a14ea | 135 | ok = io:format( |
c04c5832 SK |
136 | "X: ~b Y: ~b CELLS: ~b DEAD: ~b ALIVE: ~b GENERATION: ~b DURATION: ~f~n", |
137 | [X, Y, NumCells, NewNDead, NewNAlive, GenID, GenDuration] | |
812a14ea | 138 | ), |
f7349482 | 139 | ok = do_print_bar(X), |
d2a0e2f9 | 140 | ok = do_print_state_chars(X, StateChars), |
7319e378 | 141 | |
1d121bcf | 142 | ok = schedule_next_gen(), |
a88b2916 | 143 | {noreply, NewState#state{state_pairs=[]}}; |
d2a0e2f9 SK |
144 | |
145 | _N -> | |
146 | {noreply, NewState#state{state_pairs=NewStatePairs}} | |
147 | end; | |
148 | ||
149 | handle_cast(_Msg, State) -> | |
150 | {noreply, State}. | |
151 | ||
152 | ||
d2a0e2f9 SK |
153 | %% ============================================================================ |
154 | %% Internal | |
155 | %% ============================================================================ | |
156 | ||
1ffcb224 SK |
157 | increment_dead_or_alive(0, NDead, NAlive) -> {NDead + 1, NAlive}; |
158 | increment_dead_or_alive(1, NDead, NAlive) -> {NDead, NAlive + 1}. | |
159 | ||
160 | ||
0a9b8c17 | 161 | schedule_next_gen() -> |
7319e378 SK |
162 | erlang:send_after(?INTERVAL, self(), next_gen), |
163 | ok. | |
172421cb SK |
164 | |
165 | ||
d2a0e2f9 SK |
166 | state_to_char(0) -> ?CHAR_DEAD; |
167 | state_to_char(1) -> ?CHAR_ALIVE. | |
168 | ||
169 | ||
170 | do_print_state_chars(_, []) -> ok; | |
171 | do_print_state_chars(X, Chars) -> | |
172 | {XChars, RestChars} = lists:split(X, Chars), | |
173 | ok = io:format([XChars, $\n]), | |
174 | do_print_state_chars(X, RestChars). | |
175 | ||
176 | ||
177 | do_print_bar(X) -> | |
178 | io:format("~s~n", [[?CHAR_BAR || _ <- lists:seq(1, X - 1)]]). |