X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=001%2Fsrc%2Flife_time.erl;h=204b67f5b17ba7122ef32c3a1dbe77de159927e7;hb=3a20c476f299ddbe993ac28b5dec75a407edf338;hp=49ab6466ad4b6e1feec317df416e89057fcb3d4d;hpb=29199e7e6e93e242c9d9fecc7db67bb3e170d6d6;p=cellular-automata.git diff --git a/001/src/life_time.erl b/001/src/life_time.erl index 49ab646..204b67f 100644 --- a/001/src/life_time.erl +++ b/001/src/life_time.erl @@ -17,20 +17,23 @@ ]). --define(INTERVAL, 100). % In milliseconds +-define(GEN_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() + ,gen_began :: erlang:timestamp() }). @@ -49,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 %% ============================================================================ @@ -62,45 +74,52 @@ init([X, Y, Cells]) -> ,replies_pending = 0 ,gen_id = 0 }, - schedule_next_gen(), + NextGenDelay = 0, + ok = schedule_next_gen(NextGenDelay), {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}), - {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 + ,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 + ,num_dead=NDead + ,num_alive=NAlive ,state_pairs=StatePairs ,replies_pending=RepliesPending ,gen_id=GenID + ,gen_began=GenBegan ,num_cells=NumCells }=State) -> 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,14 +129,21 @@ handle_cast({report_state, {CellID, GenID, CellState}}, ), StateChars = [state_to_char(S) || {_, S} <- SortedStatePairs], + 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 GENERATION: ~b~n", - [X, Y, NumCells, 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 = timer:sleep(?INTERVAL), - schedule_next_gen(), + + ok = schedule_next_gen(NextGenDelay), {noreply, NewState#state{state_pairs=[]}}; _N -> @@ -128,22 +154,21 @@ 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}. + +schedule_next_gen(Delay) when Delay > 0 -> + erlang:send_after(Delay, self(), next_gen), + ok; -cast_all([], _) -> ok; -cast_all([Server | Servers], Msg) -> - ok = gen_server:cast(Server, Msg), - cast_all(Servers, Msg). +schedule_next_gen(_) -> + erlang:send(self(), next_gen), + ok. state_to_char(0) -> ?CHAR_DEAD;