Extended usage instructions.
[cellular-automata.git] / 001 / src / life_time.erl
index e2ee30e..143573d 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()
@@ -33,6 +33,7 @@
                ,state_pairs     :: list(tuple(integer(), integer())) | []
                ,replies_pending :: integer()
                ,gen_id          :: integer()
+               ,gen_began       :: erlang:timestamp()
                }).
 
 
@@ -51,6 +52,15 @@ 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}.
+
+
 %% ============================================================================
 %% Callbacks
 %% ============================================================================
@@ -68,34 +78,28 @@ init([X, Y, Cells]) ->
     {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=[]
           ,gen_id=GenID
           }=State) ->
 
+    GenBegan = os:timestamp(),
     NewGenID = GenID + 1,
-    ok = cast_all(Cells, {next_gen, NewGenID}),
+    ok = life_lib:cast_one2all(Cells, {next_gen, NewGenID}),
     NewState = State#state{replies_pending=NumCells
                           ,gen_id=NewGenID
+                          ,gen_began=GenBegan
                           ,num_dead=0
                           ,num_alive=0
                           },
     {noreply, NewState};
 
+handle_info(_Msg, State) ->
+    {noreply, State}.
+
+
 handle_cast({report_state, {CellID, GenID, CellState}},
     #state{x=X
           ,y=Y
@@ -104,6 +108,7 @@ handle_cast({report_state, {CellID, GenID, CellState}},
           ,state_pairs=StatePairs
           ,replies_pending=RepliesPending
           ,gen_id=GenID
+          ,gen_began=GenBegan
           ,num_cells=NumCells
           }=State) ->
 
@@ -123,13 +128,17 @@ handle_cast({report_state, {CellID, GenID, CellState}},
             ),
             StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
 
+            GenDuration = timer:now_diff(os:timestamp(), GenBegan) / 1000000,
+
+            ok = life_observer:log_generation(GenID, GenDuration, NewNDead, NewNAlive),
+
             ok = io:format(
-                "X: ~b Y: ~b CELLS: ~b DEAD: ~b ALIVE: ~b GENERATION: ~b~n",
-                [X, Y, NumCells, NewNDead, NewNAlive, GenID]
+                "X: ~b Y: ~b CELLS: ~b DEAD: ~b ALIVE: ~b GENERATION: ~b DURATION: ~f~n",
+                [X, Y, NumCells, NewNDead, NewNAlive, GenID, GenDuration]
             ),
             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=[]}};
 
@@ -141,10 +150,6 @@ handle_cast(_Msg, State) ->
     {noreply, State}.
 
 
-handle_info(_Msg, State) ->
-    {noreply, State}.
-
-
 %% ============================================================================
 %% Internal
 %% ============================================================================
@@ -154,13 +159,8 @@ increment_dead_or_alive(1, NDead, NAlive) -> {NDead, NAlive + 1}.
 
 
 schedule_next_gen() ->
-    ok = gen_server:cast(?MODULE, next_gen).
-
-
-cast_all([], _) -> ok;
-cast_all([Server | Servers], Msg) ->
-    ok = gen_server:cast(Server, Msg),
-    cast_all(Servers, Msg).
+    erlang:send_after(?INTERVAL, self(), next_gen),
+    ok.
 
 
 state_to_char(0) -> ?CHAR_DEAD;
This page took 0.031814 seconds and 4 git commands to generate.