]).
--define(INTERVAL, 100). % In milliseconds
+-define(GEN_INTERVAL, 100). % In milliseconds
-define(CHAR_DEAD, 32). % " "
-define(CHAR_ALIVE, 111). % "o"
,state_pairs :: list(tuple(integer(), integer())) | []
,replies_pending :: integer()
,gen_id :: integer()
+ ,gen_began :: erlang:timestamp()
}).
,replies_pending = 0
,gen_id = 0
},
- ok = schedule_next_gen(),
+ NextGenDelay = 0,
+ ok = schedule_next_gen(NextGenDelay),
{ok, State}.
,gen_id=GenID
}=State) ->
+ GenBegan = os:timestamp(),
NewGenID = GenID + 1,
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
},
,state_pairs=StatePairs
,replies_pending=RepliesPending
,gen_id=GenID
+ ,gen_began=GenBegan
,num_cells=NumCells
}=State) ->
),
StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs],
- ok = life_observer:log_generation(GenID, NewNDead, NewNAlive),
+ GenDurationMic = timer:now_diff(os:timestamp(), GenBegan),
+ GenDurationMil = GenDurationMic / 1000,
+ GenDurationSec = GenDurationMic / 1000000,
+ NextGenDelay = round(?GEN_INTERVAL - GenDurationMil),
+
+ ok = life_observer:log_generation(GenID, GenDurationSec, 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, GenDurationSec]
),
ok = do_print_bar(X),
ok = do_print_state_chars(X, StateChars),
- ok = schedule_next_gen(),
+ ok = schedule_next_gen(NextGenDelay),
{noreply, NewState#state{state_pairs=[]}};
_N ->
increment_dead_or_alive(1, NDead, NAlive) -> {NDead, NAlive + 1}.
-schedule_next_gen() ->
- erlang:send_after(?INTERVAL, self(), next_gen),
+schedule_next_gen(Delay) when Delay > 0 ->
+ erlang:send_after(Delay, self(), next_gen),
+ ok;
+
+schedule_next_gen(_) ->
+ erlang:send(self(), next_gen),
ok.