From: Siraaj Khandkar Date: Mon, 30 Jul 2012 02:26:41 +0000 (-0400) Subject: Using erlang:send_after/3 for tick scheduling. X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=7319e378a77a6be8fc887433c1bb2de08e108fcf;p=cellular-automata.git Using erlang:send_after/3 for tick scheduling. --- diff --git a/001/src/life_time.erl b/001/src/life_time.erl index 089925f..1140c79 100644 --- a/001/src/life_time.erl +++ b/001/src/life_time.erl @@ -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 @@ -129,7 +132,7 @@ handle_cast({report_state, {CellID, GenID, CellState}}, ), 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=[]}}; @@ -150,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;