Using erlang:send_after/3 for tick scheduling.
[cellular-automata.git] / 001 / src / life_time.erl
index c3b7a45..1140c79 100644 (file)
@@ -19,9 +19,9 @@
 
 -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()
@@ -56,7 +56,6 @@ report_state(CellID, GenID, CellState) ->
 %% ============================================================================
 
 handle_call(_Msg, _From, State)  -> {reply, ok, State}.
-handle_info(_Msg, State)         -> {noreply, State}.
 code_change(_Old, State, _Other) -> {ok, State}.
 terminate(_Reason, State)        -> {ok, State}.
 
@@ -78,7 +77,7 @@ init([X, Y, Cells]) ->
     {ok, State}.
 
 
-handle_cast(next_gen,
+handle_info(next_gen,
     #state{cells=Cells
           ,num_cells=NumCells
           ,state_pairs=[]
@@ -94,6 +93,10 @@ handle_cast(next_gen,
                           },
     {noreply, NewState};
 
+handle_info(_Msg, State) ->
+    {noreply, State}.
+
+
 handle_cast({report_state, {CellID, GenID, CellState}},
     #state{x=X
           ,y=Y
@@ -121,13 +124,15 @@ handle_cast({report_state, {CellID, GenID, 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 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),
+
             ok = schedule_next_gen(),
             {noreply, NewState#state{state_pairs=[]}};
 
@@ -148,7 +153,8 @@ increment_dead_or_alive(1, NDead, NAlive) -> {NDead, NAlive + 1}.
 
 
 schedule_next_gen() ->
-    ok = gen_server:cast(?MODULE, next_gen).
+    erlang:send_after(?INTERVAL, self(), next_gen),
+    ok.
 
 
 state_to_char(0) -> ?CHAR_DEAD;
This page took 0.034562 seconds and 4 git commands to generate.