Using erlang:send_after/3 for tick scheduling.
[cellular-automata.git] / 001 / src / life_time.erl
CommitLineData
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()
d2a0e2f9
SK
36 }).
37
38
39%% ============================================================================
40%% API
41%% ============================================================================
42
1aa9333c 43start_link(X, Y, Cells) ->
d2a0e2f9 44 ServerName = {local, ?MODULE},
1aa9333c 45 Args = [X, Y, Cells],
d2a0e2f9
SK
46 Opts = [],
47 gen_server:start_link(ServerName, ?MODULE, Args, Opts).
48
49
29199e7e
SK
50report_state(CellID, GenID, CellState) ->
51 gen_server:cast(?MODULE, {report_state, {CellID, GenID, CellState}}).
d2a0e2f9
SK
52
53
90456d08
SK
54%% ============================================================================
55%% Callbacks (unused)
56%% ============================================================================
57
58handle_call(_Msg, _From, State) -> {reply, ok, State}.
90456d08
SK
59code_change(_Old, State, _Other) -> {ok, State}.
60terminate(_Reason, State) -> {ok, State}.
61
62
d2a0e2f9
SK
63%% ============================================================================
64%% Callbacks
65%% ============================================================================
66
1aa9333c 67init([X, Y, Cells]) ->
0a9b8c17
SK
68 State = #state{x = X
69 ,y = Y
70 ,cells = Cells
71 ,num_cells = length(Cells)
72 ,state_pairs = []
73 ,replies_pending = 0
74 ,gen_id = 0
d2a0e2f9 75 },
1d121bcf 76 ok = schedule_next_gen(),
d2a0e2f9
SK
77 {ok, State}.
78
79
7319e378 80handle_info(next_gen,
172421cb
SK
81 #state{cells=Cells
82 ,num_cells=NumCells
83 ,state_pairs=[]
0a9b8c17 84 ,gen_id=GenID
172421cb
SK
85 }=State) ->
86
0a9b8c17 87 NewGenID = GenID + 1,
79a2bc14 88 ok = life_lib:cast_one2all(Cells, {next_gen, NewGenID}),
1ffcb224
SK
89 NewState = State#state{replies_pending=NumCells
90 ,gen_id=NewGenID
91 ,num_dead=0
92 ,num_alive=0
93 },
94 {noreply, NewState};
d2a0e2f9 95
7319e378
SK
96handle_info(_Msg, State) ->
97 {noreply, State}.
98
99
29199e7e 100handle_cast({report_state, {CellID, GenID, CellState}},
d2a0e2f9 101 #state{x=X
1aa9333c 102 ,y=Y
1ffcb224
SK
103 ,num_dead=NDead
104 ,num_alive=NAlive
d2a0e2f9
SK
105 ,state_pairs=StatePairs
106 ,replies_pending=RepliesPending
0a9b8c17 107 ,gen_id=GenID
812a14ea 108 ,num_cells=NumCells
d2a0e2f9
SK
109 }=State) ->
110
29199e7e 111 NewStatePairs = [{CellID, CellState} | StatePairs],
d2a0e2f9 112 NewRepliesPending = RepliesPending - 1,
1ffcb224
SK
113 {NewNDead, NewNAlive} = increment_dead_or_alive(CellState, NDead, NAlive),
114 NewState = State#state{replies_pending=NewRepliesPending
115 ,num_dead=NewNDead
116 ,num_alive=NewNAlive
117 },
d2a0e2f9
SK
118
119 case NewRepliesPending of
120 0 ->
3803d7be
SK
121 SortedStatePairs = lists:sort(
122 fun({A, _}, {B, _}) -> A < B end,
123 NewStatePairs
124 ),
d2a0e2f9 125 StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
eec4cd1f 126
c8c75648
SK
127 ok = life_observer:log_generation(GenID, NewNDead, NewNAlive),
128
812a14ea 129 ok = io:format(
1ffcb224
SK
130 "X: ~b Y: ~b CELLS: ~b DEAD: ~b ALIVE: ~b GENERATION: ~b~n",
131 [X, Y, NumCells, NewNDead, NewNAlive, GenID]
812a14ea 132 ),
f7349482 133 ok = do_print_bar(X),
d2a0e2f9 134 ok = do_print_state_chars(X, StateChars),
7319e378 135
1d121bcf 136 ok = schedule_next_gen(),
a88b2916 137 {noreply, NewState#state{state_pairs=[]}};
d2a0e2f9
SK
138
139 _N ->
140 {noreply, NewState#state{state_pairs=NewStatePairs}}
141 end;
142
143handle_cast(_Msg, State) ->
144 {noreply, State}.
145
146
d2a0e2f9
SK
147%% ============================================================================
148%% Internal
149%% ============================================================================
150
1ffcb224
SK
151increment_dead_or_alive(0, NDead, NAlive) -> {NDead + 1, NAlive};
152increment_dead_or_alive(1, NDead, NAlive) -> {NDead, NAlive + 1}.
153
154
0a9b8c17 155schedule_next_gen() ->
7319e378
SK
156 erlang:send_after(?INTERVAL, self(), next_gen),
157 ok.
172421cb
SK
158
159
d2a0e2f9
SK
160state_to_char(0) -> ?CHAR_DEAD;
161state_to_char(1) -> ?CHAR_ALIVE.
162
163
164do_print_state_chars(_, []) -> ok;
165do_print_state_chars(X, Chars) ->
166 {XChars, RestChars} = lists:split(X, Chars),
167 ok = io:format([XChars, $\n]),
168 do_print_state_chars(X, RestChars).
169
170
171do_print_bar(X) ->
172 io:format("~s~n", [[?CHAR_BAR || _ <- lists:seq(1, X - 1)]]).
This page took 0.049554 seconds and 4 git commands to generate.