-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerBasic.oz
175 lines (152 loc) · 3.3 KB
/
PlayerBasic.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
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
% Helper functions
RandomInRange = fun {$ Min Max} Min+({OS.rand}mod(Max-Min+1)) end
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}
% TODO You can add more elements if you need it
)
}
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
%%%% TODO Message functions
fun {InitPosition State ?ID ?Position}
ID = State.id
Position = State.startPosition
State
end
fun {Move State ?ID ?Position}
State
end
fun {SayMoved State ID Position}
State
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}
ID = State.id
Kind = null
State
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 {SayDeath State ID}
State
end
fun {SayDamageTaken State ID Damage LifeLeft}
State
end
fun {TakeFlag State ?ID ?Flag}
ID = State.id
Flag = null
State
end
fun {DropFlag State ?ID ?Flag}
ID = State.id
Flag = null
State
end
fun {SayFlagTaken State ID Flag}
State
end
fun {SayFlagDropped State ID Flag}
State
end
end