Using erlang:send_after/3 for tick scheduling.
[cellular-automata.git] / 001 / src / life_time.erl
index b47073c..1140c79 100644 (file)
@@ -4,7 +4,7 @@
 
 %% API
 -export([start_link/3
-        ,report_state/2
+        ,report_state/3
         ]).
 
 %% Callbacks
 
 -define(INTERVAL, 100).  % In milliseconds
 
--define(CHAR_DEAD,   32).  % Space
--define(CHAR_ALIVE, 111).  % o
--define(CHAR_BAR,    45).  % -
+-define(CHAR_DEAD,   32).  % " "
+-define(CHAR_ALIVE, 111).  % "o"
+-define(CHAR_BAR,    45).  % "-"
 
 
 -record(state, {x               :: integer()
                ,y               :: integer()
                ,cells           :: list(atom())
                ,num_cells       :: integer()
+               ,num_dead        :: integer()
+               ,num_alive       :: integer()
                ,state_pairs     :: list(tuple(integer(), integer())) | []
                ,replies_pending :: integer()
                ,gen_id          :: integer()
@@ -45,8 +47,17 @@ start_link(X, Y, Cells) ->
     gen_server:start_link(ServerName, ?MODULE, Args, Opts).
 
 
-report_state(CellID, CellState) ->
-    gen_server:cast(?MODULE, {report_state, {CellID, CellState}}).
+report_state(CellID, GenID, CellState) ->
+    gen_server:cast(?MODULE, {report_state, {CellID, GenID, CellState}}).
+
+
+%% ============================================================================
+%% Callbacks (unused)
+%% ============================================================================
+
+handle_call(_Msg, _From, State)  -> {reply, ok, State}.
+code_change(_Old, State, _Other) -> {ok, State}.
+terminate(_Reason, State)        -> {ok, State}.
 
 
 %% ============================================================================
@@ -62,23 +73,11 @@ init([X, Y, Cells]) ->
                   ,replies_pending = 0
                   ,gen_id          = 0
                   },
-    schedule_next_gen(),
+    ok = schedule_next_gen(),
     {ok, State}.
 
 
-terminate(_Reason, State) ->
-    {ok, State}.
-
-
-code_change(_Old, State, _Other) ->
-    {ok, State}.
-
-
-handle_call(_Msg, _From, State) ->
-    {reply, ok, State}.
-
-
-handle_cast(next_gen,
+handle_info(next_gen,
     #state{cells=Cells
           ,num_cells=NumCells
           ,state_pairs=[]
@@ -86,21 +85,36 @@ handle_cast(next_gen,
           }=State) ->
 
     NewGenID = GenID + 1,
-    ok = cast_all(Cells, {next_gen, NewGenID}),
-    {noreply, State#state{replies_pending=NumCells, gen_id=NewGenID}};
+    ok = life_lib:cast_one2all(Cells, {next_gen, NewGenID}),
+    NewState = State#state{replies_pending=NumCells
+                          ,gen_id=NewGenID
+                          ,num_dead=0
+                          ,num_alive=0
+                          },
+    {noreply, NewState};
 
-handle_cast({report_state, {ID, CellState}},
+handle_info(_Msg, State) ->
+    {noreply, State}.
+
+
+handle_cast({report_state, {CellID, GenID, CellState}},
     #state{x=X
           ,y=Y
+          ,num_dead=NDead
+          ,num_alive=NAlive
           ,state_pairs=StatePairs
           ,replies_pending=RepliesPending
           ,gen_id=GenID
           ,num_cells=NumCells
           }=State) ->
 
-    NewStatePairs = [{ID, CellState} | StatePairs],
+    NewStatePairs = [{CellID, CellState} | StatePairs],
     NewRepliesPending = RepliesPending - 1,
-    NewState = State#state{replies_pending=NewRepliesPending},
+    {NewNDead, NewNAlive} = increment_dead_or_alive(CellState, NDead, NAlive),
+    NewState = State#state{replies_pending=NewRepliesPending
+                          ,num_dead=NewNDead
+                          ,num_alive=NewNAlive
+                          },
 
     case NewRepliesPending of
         0 ->
@@ -110,16 +124,16 @@ handle_cast({report_state, {ID, CellState}},
             ),
             StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
 
+            ok = life_observer:log_generation(GenID, NewNDead, NewNAlive),
+
             ok = io:format(
-                "X: ~b Y: ~b CELLS: ~b GENERATION: ~b~n",
-                [X, Y, NumCells, GenID]
+                "X: ~b Y: ~b CELLS: ~b DEAD: ~b ALIVE: ~b GENERATION: ~b~n",
+                [X, Y, NumCells, NewNDead, NewNAlive, GenID]
             ),
             ok = do_print_bar(X),
-
             ok = do_print_state_chars(X, StateChars),
 
-            ok = timer:sleep(?INTERVAL),
-            schedule_next_gen(),
+            ok = schedule_next_gen(),
             {noreply, NewState#state{state_pairs=[]}};
 
         _N ->
@@ -130,22 +144,17 @@ handle_cast(_Msg, State) ->
     {noreply, State}.
 
 
-handle_info(_Msg, State) ->
-    {noreply, State}.
-
-
 %% ============================================================================
 %% Internal
 %% ============================================================================
 
-schedule_next_gen() ->
-    gen_server:cast(?MODULE, next_gen).
+increment_dead_or_alive(0, NDead, NAlive) -> {NDead + 1, NAlive};
+increment_dead_or_alive(1, NDead, NAlive) -> {NDead, NAlive + 1}.
 
 
-cast_all([], _) -> ok;
-cast_all([Server | Servers], Msg) ->
-    ok = gen_server:cast(Server, Msg),
-    cast_all(Servers, Msg).
+schedule_next_gen() ->
+    erlang:send_after(?INTERVAL, self(), next_gen),
+    ok.
 
 
 state_to_char(0) -> ?CHAR_DEAD;
This page took 0.035759 seconds and 4 git commands to generate.