-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathball.erl
197 lines (169 loc) · 4.95 KB
/
ball.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
-module(ball).
-import(crypto, [rand_uniform/2, start/0]).
-include("state.hrl").
-compile(export_all).
create_balls(_, 0) ->
[];
create_balls( Canvas, Nballs) ->
[create_ball(Canvas) | create_balls(Canvas, Nballs-1)].
create_ball(Canvas) ->
Pos = { rand_uniform(200,400), rand_uniform(200, 400) },
create_ball(Canvas, Pos).
%% each ball consists of three processes:
%% BallProcess: the process that makes a new generation every xx ms.
%% ball_communicator: the process that communicates with others and the Canvas
create_ball(Canvas, {X,Y}) ->
State=#state{pos={X,Y}},
Size = State#state.size, %% uses default value from recrd
Color= State#state.color,
Ball=gs:oval(Canvas,[{coords,[{X-Size, Y-Size}, {X+Size,Y+Size}]},{fill,Color}]),
BallCommunicator=spawn_link( fun() -> ball_communicator(State, init) end),
BallProcess=spawn( fun() -> ball(Ball,
State#state{comm_pid=BallCommunicator}, init) end ),
BallCommunicator ! { set_ball_process, BallProcess},
BallCommunicator.
ball(Ball, State, init ) ->
process_flag(trap_exit, true),
spawn_link( fun() -> death_handler(Ball) end ),
ball(Ball, State, false ) ;
ball(Ball, State, OldState ) ->
{X,Y}=State#state.pos,
Size = State#state.size + State#state.dsize,
if
OldState =/= false,
OldState#state.pos =/= State#state.pos;
OldState#state.size =/= State#state.size;
OldState#state.color =/= State#state.color ->
try
gs:config(Ball,[{coords,[{X-Size, Y-Size}, {X+Size, Y+Size}]},
{fill, State#state.color}])
catch
_:_ -> true
end ;
true ->
true
end,
NewState =
receive
{'EXIT', Pid, Why} when Pid =:= State#state.comm_pid ->
%% io:format("ball ~p died because ~p~n", [self(), Why]),
exit(Why);
die_normal ->
exit(told_to_die);
{grid_info, Grid } ->
handle_grid_info(Grid, State);
Message ->
io:format("Ball ~p received unexpected message ~p~n", [self(), Message])
after State#state.generation_interval ->
State
end,
%% NewState is now set
%% check if the size has reached the maximum value
NewState1=
if
Size > 20 -> NewState#state{dsize=0, color=purple};
true -> NewState
end,
%% increment generation counter
NewState2=NewState1#state{
size=Size,
generation=State#state.generation+1},
%% tell BallCommunicator our new state
NewState2#state.comm_pid ! {update_state, NewState2},
%% death ?
R2=rand_uniform(0,100) ,
if
%% keep living if not too old
%% otherwise 5% chance of dying
NewState2#state.generation < NewState2#state.generation_die ;
R2 < 95 ->
ball(Ball, NewState2, State);
true-> %% ball dies
exit(ball_old_age_death)
end.
death_handler(Ball) ->
process_flag(trap_exit, true),
receive
{'EXIT', _Pid, {eaten, _State}} ->
gs:config(Ball, [ {fill, 'yellow'}]);
{'EXIT', _Pid, ball_old_age_death} ->
gs:config(Ball, [ {fill, 'none'}]);
{'EXIT', _Pid, die_normal} ->
true;
{'EXIT', _Pid, Why} ->
io:format("Death handler received unexpected EXIT ~p~n", [Why]);
Message ->
io:format("Death handler received unexpected ~p~n", [Message])
end,
receive after 1000 -> true end,
gs:destroy(Ball).
handle_grid_info(Grid, State) ->
{X,Y} = State#state.pos,
I=?gridindex(X,Y),
{V, _Pids} =array:get(I, Grid),
R=rand_uniform(0,100) ,
if
V > 100 ->
State#state{dsize=0, color=green};
State#state.generation > State#state.generation_split ,
X > ?GRIDSIZE,
Y > ?GRIDSIZE,
X < ?WORLDSIZE - ?GRIDSIZE,
Y < ?WORLDSIZE - ?GRIDSIZE,
V < 100,
R > 75 -> %% 25% chance of cloning
world ! { self(), split, State },
State;
true ->
State
end.
clone_ball(Canvas, OldState) ->
F=fun() -> rand_uniform(20, ?GRIDSIZE) end,
I=rand_uniform(0,4),
{DX,DY}=
case I of
0 -> {-F(), -F() } ;
1 -> {-F(), F() } ;
2 -> { F(), -F() } ;
3 -> { F(), F() } ;
true -> { 0, 0}
end ,
{X,Y}=OldState#state.pos,
create_ball(Canvas, {X+DX, Y+DY}).
ball_communicator(State, init) ->
process_flag(trap_exit, true),
ball_communicator(State).
ball_communicator(OldState) ->
NewState=
receive
%% our BallProcess died of old age
{'EXIT', _Pid, ball_old_age_death} ->
exit(ball_old_age_death);
%% someone eats us
{Pid, i_eat_you} ->
Pid ! {self(), you_ate_me, OldState},
exit({eaten, OldState});
%% regular death, uses exit(die_normal) to kill connected BallProcess
die_normal ->
exit(die_normal);
%% called once to connect to BallProcess
{set_ball_process, Pid} ->
link(Pid),
OldState#state{ball_process=Pid};
%% someone ('world', 'grazer', ...) wants State
{Pid, get_state} ->
Pid ! {self(), info, OldState},
OldState;
%% received new state from BallProcess
{update_state, State} ->
State#state{ball_process=OldState#state.ball_process};
%% pass grid info to BallProcess
{_PidStatProcess, grid_info, Grid } ->
OldState#state.ball_process ! {grid_info, Grid},
OldState;
Message ->
io:format("ball_communicator ~p received unexpected ~p, ~nState=~p~n",
[self(), Message, OldState])
end,
ball_communicator(NewState).
%% vim:tw=0