X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=001%2Fsrc%2Flife_time.erl;h=143573df3514cf3f736be1bfa85c82a2248a102b;hb=bdfda001fb7cb77f29552bca0071ff3d389b5fd7;hp=25524212b5ce206ae56be83aa045d8dac1051e3a;hpb=1d121bcf9312f8c869537e5d28f2c2964fe35169;p=cellular-automata.git diff --git a/001/src/life_time.erl b/001/src/life_time.erl index 2552421..143573d 100644 --- a/001/src/life_time.erl +++ b/001/src/life_time.erl @@ -19,18 +19,21 @@ -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() + ,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 %% ============================================================================ @@ -66,41 +78,47 @@ 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}), - {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,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 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, 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=[]}}; @@ -128,22 +150,17 @@ handle_cast(_Msg, State) -> {noreply, State}. -handle_info(_Msg, State) -> - {noreply, State}. - - %% ============================================================================ %% Internal %% ============================================================================ -schedule_next_gen() -> - ok = 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;