-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player017Smarter.oz
275 lines (243 loc) · 6.31 KB
/
Player017Smarter.oz
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
functor
import
Input
OS
System
export
portPlayer:StartPlayer
define
% Vars
MapWidth = {List.length Input.map}
MapHeight = {List.length Input.map.1}
% Functions
StartPlayer
TreatStream
MatchHead
% Message functions
InitPosition
NewTurn
Move
IsDead
AskHealth
SayMoved
SayMineExplode
SayDeath
SayDamageTaken
SayFoodAppeared
SayFoodEaten
SayFlagTaken
SayFlagDropped
ChargeItem
SayCharge
FireItem
SayMinePlaced
SayShoot
TakeFlag
DropFlag
SetPosition
SetPlayers
% Helper functions
RandomInRange = fun {$ Min Max} Min+({OS.rand}mod(Max-Min+1)) end
MovePlayer
HasNoPlayers
in
fun {StartPlayer Color ID}
Stream
Port
in
{NewPort Stream Port}
thread
{TreatStream
Stream
state(
id:id(name:basic color:Color id:ID)
position:{List.nth Input.spawnPoints ID}
map:Input.map
hp:Input.startHealth
flag:null
mineReloads:0
gunReloads:0
startPosition:{List.nth Input.spawnPoints ID}
target:nil
players:nil)}
end
Port
end
proc{TreatStream Stream State}
case Stream
of H|T then {TreatStream T {MatchHead H State}}
end
end
fun {MatchHead Head State}
case Head
of initPosition(?ID ?Position) then {InitPosition State ID Position}
[] move(?ID ?Position) then {Move State ID Position}
[] sayMoved(ID Position) then {SayMoved State ID Position}
[] sayMineExplode(Mine) then {SayMineExplode State Mine}
[] sayFoodAppeared(Food) then {SayFoodAppeared State Food}
[] sayFoodEaten(ID Food) then {SayFoodEaten State ID Food}
[] chargeItem(?ID ?Kind) then {ChargeItem State ID Kind}
[] sayCharge(ID Kind) then {SayCharge State ID Kind}
[] fireItem(?ID ?Kind) then {FireItem State ID Kind}
[] sayMinePlaced(ID Mine) then {SayMinePlaced State ID Mine}
[] sayShoot(ID Position) then {SayShoot State ID Position}
[] isDead(?Answer) then {IsDead State Answer}
[] sayDeath(ID) then {SayDeath State ID}
[] sayDamageTaken(ID Damage LifeLeft) then {SayDamageTaken State ID Damage LifeLeft}
[] takeFlag(?ID ?Flag) then {TakeFlag State ID Flag}
[] dropFlag(?ID ?Flag) then {DropFlag State ID Flag}
[] sayFlagTaken(ID Flag) then {SayFlagTaken State ID Flag}
[] sayFlagDropped(ID Flag) then {SayFlagDropped State ID flag}
end
end
%% State Setters
fun {SetPosition State Position}
state(id:State.id position:Position
map:State.map hp:State.hp flag:State.flag
mineReloads:State.mineReloads gunReloads:State.gunReloads
startPosition:State.startPosition players:State.players)
end
fun {SetPlayers State Players}
state(id:State.id position:State.position
map:State.map hp:State.hp flag:State.flag
mineReloads:State.mineReloads gunReloads:State.gunReloads
startPosition:State.startPosition players:Players)
end
%%%% TODO Message functions
fun {InitPosition State ?ID ?Position}
ID = State.id
Position = State.startPosition
State
end
fun {HasNoPlayers Players X Y}
case Players
of player(id:_ position:pt(x:X1 y:Y1))|T then
if X==X1 andthen Y==Y1 then
false
else
{HasNoPlayers T X Y}
end
[] nil then true end
end
fun {Move State ?ID ?Position}
proc {ChooseDirection XDiff YDiff} V in
V = {OS.rand} mod 4
if V > 1 then
XDiff = 0
YDiff = (V mod 2) * 2 - 1
else
XDiff = (V mod 2) * 2 - 1
YDiff = 0
end
end
fun {IsValidPosition X Y}
if X >= 1 andthen Y >= 1 andthen Y =< MapHeight andthen X =< MapWidth
andthen {List.nth {List.nth Input.map X} Y} \= 3
andthen {HasNoPlayers State.players X Y} then
true
else
false
end
end
X Y XDiff YDiff
in
{ChooseDirection XDiff YDiff}
State.position = pt(x:X y:Y)
if {IsValidPosition X+XDiff Y+YDiff} then
Position = pt(x:X+XDiff y:Y+YDiff)
else
Position = pt(x:X y:Y)
end
{SetPosition State Position}
end
fun {MovePlayer Players ID NewPosition}
case Players
of player(id:PlayerID position:PlayerPosition)|T then
if ID==PlayerID then
player(id:ID position:NewPosition)|T
else
player(id:PlayerID position:PlayerPosition)|{MovePlayer T ID NewPosition}
end
[] nil then
[player(id:ID position:NewPosition)]
end
end
fun {SayMoved State ID Position}
{SetPlayers State {MovePlayer State.players ID Position}}
end
fun {SayMineExplode State Mine}
State
end
fun {SayFoodAppeared State Food}
State
end
fun {SayFoodEaten State ID Food}
State
end
fun {ChargeItem State ?ID ?Kind}
R
in
%%%% Check if all weapons are full %%%%
if State.mineReloads == Input.mineCharge andthen State.gunReloads == Input.gunCharge then
ID = State.id
Kind = null
State
else
if State.mineReloads == Input.mineCharge then
Kind = gunReloads
ID = State.id
R = State.Kind + 1
State
else
Kind = mineReloads
ID = State.id
R = State.Kind + 1
State
end
end
end
fun {SayCharge State ID Kind}
State
end
fun {FireItem State ?ID ?Kind}
ID = State.id
Kind = null
State
end
fun {SayMinePlaced State ID Mine}
State
end
fun {SayShoot State ID Position}
State
end
fun {IsDead State ?Answer}
Answer = State.hp < 1
State
end
fun {SayDeath State ID}
State
end
fun {SayDamageTaken State ID Damage LifeLeft}
State
end
fun {TakeFlag State ?ID ?Flag}
ID = State.id
Flag = flag(pos:_ color:_)
State
end
fun {DropFlag State ?ID ?Flag}
ID = State.id
if State.hp < 1 then
Flag = null
else
Flag = flag(pos:_ color:_)
end
State
end
fun {SayFlagTaken State ID Flag}
State
end
fun {SayFlagDropped State ID Flag}
State
end
end