-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.lua
297 lines (277 loc) · 5.47 KB
/
server.lua
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
-- vim:et
local cmd={}
cmd["MSG"]=function(pl,a,ts) -- MSG:~msg
if a.n<2 then
return
end
cput("MSG:%s:~%s",pl.name,a[2])
end
cmd["INFO"]=function(pl,a,ts) -- INFO:~msg
if a.n<2 then
return
end
cput("INFO:~%s",a[2])
end
cmd["OK"]=function(pl,a,ts)
pl.gotok=true
end
cmd["ALLY"]=function(pl,a,ts)
if a.n<4 then
return
end
local p1=players[tonumber(a[2])]
local p2=players[tonumber(a[3])]
local b=tonumber(a[4])==1
if not p1 or not p2 then
return
end
if not b and not ally[p1][p2] then
return
end
if b and ally[p1][p2] then
return
end
ally[p1][p2]=b
b=b and 1 or 0
cput("ALLY:%d:%d:%d",p1.idx,p2.idx,b)
end
cmd["B"]=function(pl,a,ts)
if a.n<4 then
return
end
local x,y=tonumber(a[3]),tonumber(a[4])
local isdev=true
local cl=d_cl[a[2]]
if not cl then
cl=u_cl[a[2]]
isdev=false
end
if not cl then
return
end
local price=cl.price
if not price then
return
end
if pl.cash<price then
return
end
if a[2]=="B" and pl.started then
return
end
if isdev then
local o=cl:new(pl,x,y)
if o:chk_border(x,y) then
pl.cash=pl.cash-price
o.idx=devices:add(o)
dhash:add(o)
cput("PC:%d:%d:%d",pl.idx,pl.cash,pl.maxcash)
cput("Dn:%d:%s:%d:%d:%d",pl.idx,o.cl,o.idx,o.x,o.y)
if a[2]=="B" then
pl.started=true
end
end
return
end
local o=cl:new(pl,x,y)
if o:chk_supply(x,y) then
pl.cash=pl.cash-price
o.idx=units:add(o)
uhash:add(o)
rq_u:add(o,ts,TCK)
cput("PC:%d:%d:%d",pl.idx,pl.cash,pl.maxcash)
cput("Un:%d:%s:%d:%d:%d",pl.idx,o.cl,o.idx,o.x,o.y)
end
end
cmd["S"]=function(pl,a,ts) -- Switch:idx:online
if a.n<3 then
return
end
local idx=tonumber(a[2])
local b=tonumber(a[3])==1
local o=devices[idx]
if o and o.initok and o.gotpwr and o.pl==pl then
o.online=b
if b then
rq_d:add(o,ts,TCK)
end
b=b and 1 or 0
cput("Ds:%d:%d",idx,b)
end
end
cmd["U"]=function(pl,a,ts) -- Upgrade:idx
if a.n<2 then
return
end
local idx=tonumber(a[2])
local o=devices[idx]
if o and o.initok and o.gotpwr and o.pl==pl then
if pl.cash<o.price then
return
end
if o.ec>=o.em then
return
end
o.ec=o.ec+1
pl.cash=pl.cash-o.price
cput("PC:%d:%d:%d",pl.idx,pl.cash,pl.maxcash)
cput("Du:%d:%d",idx,o.ec)
end
end
cmd["D"]=function(pl,a,ts) -- Del:idx
if a.n<2 then
return
end
local idx=tonumber(a[2])
local o=devices[idx]
if o and o.pl==pl then
o:delete()
cput("Dd:%d",idx)
devices:del(o)
end
end
cmd["M"]=function(pl,a,ts) -- Move:idx:x:y
if a.n<4 then
return
end
local idx=tonumber(a[2])
local x,y=tonumber(a[3]),tonumber(a[4])
local o=devices[idx]
if o and o.pl==pl and not o.online and o.pt<=0 then
if o:move(x,y) then
cput("Dm:%d:%d:%d",idx,o.x,o.y)
end
end
end
cmd["Lc"]=function(pl,a,ts) -- Link:dev1:dev2
if a.n<3 then
return
end
local d1=devices[tonumber(a[2])]
local d2=devices[tonumber(a[3])]
if d1 and d2 then
if d1.pl==pl or (d1.cl=="G" and d2.pl==pl) then
local l=d1:connect(d2)
if l then
links:add(l)
cput("Lc:%d:%d",d1.idx,d2.idx)
end
end
end
end
cmd["Lu"]=function(pl,a,ts) -- Unlink:dev1:dev2
if a.n<3 then
return
end
local d1=devices[tonumber(a[2])]
local d2=devices[tonumber(a[3])]
if d1 and d2 then
if d1.pl==pl or (d1.cl=="G" and d2.pl==pl) then
local l=d1:unlink(d2)
if l then
links:del(l)
cput("Lu:%d:%d",d1.idx,d2.idx)
end
end
end
end
cmd["LU"]=function(pl,a,ts) -- Unlink:dev
if a.n<2 then
return
end
local o=devices[tonumber(a[2])]
if o and o.pl==pl then
o:unlink_all()
cput("LU:%d",o.idx)
end
end
cmd["Ts"]=function(pl,a,ts) -- Shot:d1:u2
if a.n<3 then
return
end
local d1=devices[tonumber(a[2])]
local u2=devices[tonumber(a[3])]
if d1 and u2 then
d1:shot(u2)
end
end
cmd["TS"]=function(pl,a,ts) -- Shot:d1:d2
if a.n<3 then
return
end
local d1=devices[tonumber(a[2])]
local d2=devices[tonumber(a[3])]
if d1 and d2 then
d1:shot(d2)
end
end
cmd["Um"]=function(pl,a,ts) -- Move:idx:x:y:x:y
if a.n<4 then
return
end
local o=units[tonumber(a[2])]
local x,y=tonumber(a[3]),tonumber(a[4])
if o and o.pl==pl then
if o:move(x,y) then
rq_um:add(o,ts,0.02)
cput("Um:%d:%s:%d:%d:%d:%d",o.idx,pl.ping,o.x,o.y,o.mx,o.my)
else
cput("Up:%d:%d:%d",o.idx,o.x,o.y)
end
end
end
cmd["Sh"]=function(pl,a,ts) -- Shot:u1:u2
if a.n<3 then
return
end
local u1=devices[tonumber(a[2])]
local u2=devices[tonumber(a[3])]
if u1 and u2 then
u1:shot(u2)
end
end
cmd["SH"]=function(pl,a,ts) -- Shot:u1:d2
if a.n<3 then
return
end
local u1=devices[tonumber(a[2])]
local d2=devices[tonumber(a[3])]
if u1 and d2 then
u1:shot(d2)
end
end
function parse_client(msg,pl,ts)
local a=str_split(msg,":")
local chunk=cmd[a[1]]
if chunk then
chunk(pl,a,ts)
end
end
function scheduler(ts,dt)
for _,o in pairs(devices) do
o.pt=o.pt-dt
o:check()
end
for o,d in rq_um:iter(ts,0.1) do
if o.deleted then
rq_um:del()
elseif o:step(d) then
rq_um:del()
cput("Up:%d:%d:%d",o.idx,o.x,o.y)
end
end
for o in rq_d:iter(ts,TCK) do
if not o.deleted and o.online and o.logic then
o:logic()
else
rq_d:del()
end
end
for o in rq_u:iter(ts,TCK) do
if not o.deleted then
o:logic()
else
rq_u:del()
end
end
end