-
Notifications
You must be signed in to change notification settings - Fork 1
/
NET_Client_lib.pas
658 lines (599 loc) · 14.7 KB
/
NET_Client_lib.pas
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
unit NET_Client_lib;
interface
uses
Windows, SysUtils,
Engine_Reg,
Func_lib,
Type_Lib,
Graph_Lib,
Math_Lib,
Net_Lib;
type TClient_Machine =
class(TNet_Machine)
constructor Create(servip : string; servport: word);
destructor Destroy;override;
protected
serv_btimer: byte;
sp_count: integer;
sp: array [1..100] of TSpectator;
Server: TClient;
procedure ReadInfo(flags: byte);
procedure getinfo_Send(flags: byte);
procedure addplayer_recv;
procedure removeplayer_recv;
procedure gamemsg_recv;
procedure shotobj_recv;
procedure shotobjkill_recv;
procedure pings_recv;
procedure teamjoin_recv;
procedure shot_recv;
function Getspects(ind: integer): TSpectator;override;
public
serv_info: TNP_ServerInfo;
property Serv: TClient read Server;
procedure Update_Prev; override;
procedure Update_Next; override;
procedure say_Send(uid: byte; msg: string);override;
procedure join_send(ptype: byte; name, model: string);
procedure disjoin_send(ptype: byte);
procedure changename_Send(uid: byte; name, model: string); override;
procedure shot_send(uid: byte);override;
procedure resp_Send(uid: byte);
procedure TeamJoin(uid, team: byte);
function spects_Count: integer;override;
end;
var
net_client: TClient_Machine;
implementation
uses Constants_Lib, Map_Lib, Stat_Lib, Player_Lib, weapon_lib, real_lib, game_lib, phys_lib, HUD_Lib;
{ TClient_Machine }
procedure TClient_Machine.addplayer_recv;
var
pl_type, uid: byte;
s1, s2: string;
c: TRGB; b: byte;
pl: TPlayer;
begin
Read(@uid, 1);
Read(@pl_type, 1);
ReadString(s1);
ReadString(s2);
Read(@c, 3);
Read(@b, 1);
if pl_type<>C_PLAYER_NET then
begin
Map.pl_add(pl_type, s1, s2, false, uid);
CMDCheck;
end
else
begin
pl:=Map.pl_addNET(pl_type, server, s1, s2, false, true, uid);
pl.railcolor:=c;
pl.railtype:=b;
end;
end;
procedure TClient_Machine.changename_Send(uid: byte; name, model: string);
var
c: TRGB;
b: byte;
begin
Message_(NM_CHANGENAME);
NET_Write(@uid, 1);
WriteString(name);
WriteString(model);
if Map.pl_find(uid, C_PLAYER_ALL) then
begin
c:=Map.pl_current.railcolor;
b:=Map.pl_current.railtype;
end else
begin
c:=r_rail_color;
b:=r_rail_type;
end;
NET_Write(@c, 3);
NET_Write(@b, 1);
server.Send(false);
end;
constructor TClient_Machine.Create(servip: string; servport: word);
var
params: TNP_ConnectParams;
begin
inherited Create;
NET_Client:=Self;
fSocket:=true;
fType:=NT_CLIENT;
Server:=TClient.Create(servip, servport, '');
Server.SendPing;
Read(@Params, sizeof(Params));
fNet_id:=Params.NET_ID;
ReadInfo(INFO_SERVER);
if NET.Type_<>NT_CLIENT then Exit;
Server.SendPing;
Server.RecvPing;
sp_count:=0;
end;
destructor TClient_Machine.Destroy;
begin
if not net_debug_disconnect then
Server.SendDisconnect(NM_CLIENTLEAVE);
Server.Free;
inherited;
end;
procedure TClient_Machine.disjoin_send(ptype: byte);
begin
Message_(NM_DISJOIN);
NET_Write(@ptype, 1);
Server.Send(true);
end;
procedure TClient_Machine.gamemsg_recv;
var
msg: byte;
h: THit;
uid, num: byte;
numw: word;
ww: array [0..7] of word;
pl: TPlayer;
begin
Read(@msg, 1);
case msg of
NM_RESPAWN:
begin
Read(@uid, 1);
Read(@num, 1);
Read(@numw, 2);
pl:=Map.PlayerByUID(uid);
if (pl<>nil) then
begin
pl.resp:=true;
pl.respindex:=num;
pl.lasthitid:=numw;
end;
server.pTimer:=serv_btimer;
end;
NM_OBJECTS:
with Map do
begin
if (Read(@numw, 2)=0) or
(Read(@uid, 1)=0) then Exit;
if numw<Obj.Count then
begin
Demo.RecActivate(numw, uid);
Obj[numw].Activate(Map.PlayerByUID(uid));
if Read(@ww, Obj[numw].fNetSize*2)=0 then Exit;
Obj[numw].LoadNet(ww);
end else
if numw>=20000 then Map.ActivateTarget(numw-20000, true);
end;
NM_HITS:
begin
if Read(@h, sizeof(h))>0 then
HitApply(h);
end;
NM_SHOTOBJ:
shotobj_recv;
NM_SHOTOBJ_KILL:
shotobjkill_recv;
NM_SHOT:
shot_recv;
end;
end;
procedure TClient_Machine.getinfo_Send(flags: byte);
begin
Message_(NM_GameInfo);
NET_Write(@flags, 1);
Server.Send(true);
end;
function TClient_Machine.Getspects(ind: integer): TSpectator;
begin
if (ind>=1) and (ind<=sp_count) then
Result:=sp[ind];
end;
procedure TClient_Machine.join_send(ptype: byte; name, model: string);
var
c: TRGB;
b: byte;
begin
if ptype = C_PLAYER_P1 then
begin
c:=r_rail_color;
b:=r_rail_type;
end
else if ptype = C_PLAYER_P1 then
begin
c:=r_p2_rail_color;
b:=r_p2_rail_type;
end
else
begin
c:=r_enemy_rail_color;
b:=r_enemy_rail_type;
end;
Message_(NM_JOIN);
NET_Write(@ptype, 1);
WriteString(name);
WriteString(model);
NET_Write(@c, 3);
NET_Write(@b, 1);
Server.Send(true);
end;
procedure TClient_Machine.pings_recv;
var
tb, uid: byte;
w: word;
i: integer;
begin
read(@tb, 1);
for i:=0 to tb-1 do
begin
read(@uid, 1);
read(@w, 2);
if Map.pl_find(uid, C_PLAYER_ALL) then
Map.pl_current.current_ping:=w;
end;
read(@tb, 1);
sp_count:=tb;
for i:=1 to tb do
begin
ReadString(sp[i].name);
Read(@sp[i].ping, 2);
end;
end;
procedure TClient_Machine.ReadInfo(flags: byte);
var
s1, s2: string;
tb, tb2, tb3, uid: byte;
tw : word;
ww : array [0..7] of word;
buf : array of byte;
i: integer;
stat: TPlayerStat;
pl: TPlayer;
c: TRGB;
b: byte;
begin
if flags and INFO_SERVER>0 then
begin
Read(@serv_info, sizeof(serv_info));
ReadString(s1);
ReadString(s2);
Server.Name:=s1;
NET_ServerMap:=s2;
if (serv_info.session<>Map.session_number) or (NET_ServerMap<>Map.GetFileName) or
(serv_info.gametype<>gametype) then
begin
NET_ClearAPL;
Map.not_warmup_game:=not serv_info.warmup and Map.warmup;
gametype_c:=serv_info.gametype;
if (NET_ServerMap=Map.GetFileName) and (gametype_c=gametype) then
Map.Restart
else LoadMap(NET_ServerMap);
if NET.Type_<>NT_Client then Exit;
//äèñêîííåêò óæå ïðîèçîø¸ë, íàäî ñðî÷íî âûéòè èç ïðîöåäóðêè,
//âåäü ýòîãî îáúåêòà óæå íå ñóùåñòâóåò â ïðèðîäå!!!
getinfo_Send(INFO_LOAD);
Server.ClearPing;
end;
HUD_SetTime(serv_info.servertime);
Map.Demo.RecTime;
Map.session_number:=serv_info.session;
if serv_info.stopped then
begin
if not Map.stopped then
Map.StopGame;
end;
end;
if flags and INFO_PLAYERS>0 then
begin
Read(@tb, 1);
for i:=0 to tb-1 do
begin
Read(@uid, 1);
Read(@tb2, 1);
ReaD(@tb3, 1);
ReadString(s1);
ReadString(s2);
Read(@c, 3);
Read(@b, 1);
pl := Map.PlayerByUid(uid);
if pl<>nil then
begin
if pl.playertype<>tb2 then
begin
pl.playertype:=tb2;
end;
if pl.name<>s1 then
Map.SetPlayerName(uid, s1);
if pl.modelname<>s2 then
Map.SetPlayerModel(uid, s2);
end else
begin
if tb2=C_PLAYER_NET then
pl:=Map.pl_addNET(tb2, Server, s1, s2, true, true, uid)
else pl:=Map.pl_add(tb2, s1, s2, false, uid);
end;
pl.railcolor:=c;
pl.railtype:=b;
Map.TeamJoin(uid, tb3);
Read(@tb2, 1);
if tb2>0 then
pl.Restart;
pl.byte_Health:=tb2;
Read(@tb2, 1);
pl.Armor:=tb2;
Read(@tw, 2);
pl.word_pos_x:=tw;
Read(@tw, 2);
pl.word_pos_y:=tw;
end;
map.Demo.RecPlayers;
if not Map.pl_find(-1, C_PLAYER_p1) then
if not net_spectator then
join_send(C_PLAYER_p1, p1name, p1model);
end;
if flags and INFO_OBJS>0 then
begin
Read(@tw, 2);
with Map do
while tw<Obj.Count do
begin
Read(@ww, Obj[tw].fNetSize*2);
Obj[tw].LoadNet(ww);
Read(@tw, 2);
end;
end;
if flags and INFO_STATS>0 then
begin
Read(@tb, 1);
for i:=0 to tb-1 do
begin
Read(@stat, sizeof(stat));
Stat_Set(stat.UID, stat);
end;
end;
if flags and INFO_PHYS>0 then
begin
Read(@tw, 2);
if tb>0 then
begin
Read(@tw, 2);
SetLength(buf, tb2);
Read(@buf[0], tw);
if (tb=phys_getvarscount) and
(tb2=phys_getbufsize) then
phys_readbuf(buf, tw);
end;
end;
end;
procedure TClient_Machine.removeplayer_recv;
var
b: byte;
begin
Read(@b, 1);
if Map.pl_find(b, C_PLAYER_ALL) then
Map.pl_delete_current(true);
end;
procedure TClient_Machine.resp_Send(uid: byte);
begin
Message_(NM_RESPAWN);
NET_Write(@uid, 1);
server.Send(false);
end;
procedure TClient_Machine.say_Send(uid: byte; msg: string);
begin
Message_(NM_SAY);
NET_Write(@uid, 1);
WriteString(msg);
server.Send(false);
end;
procedure TClient_Machine.shotobjkill_recv;
var
w, x, y: word;
ro: TRealObj;
begin
Read(@w, 2);
ro:=RealObj_Find(w);
Read(@x, 2);
Read(@y, 2);
if (ro<>nil) then
begin
ro.x:=x;
ro.y:=y;
ro.Kill;
end;
end;
procedure TClient_Machine.shotobj_recv;
var
s: TRealObjStruct;
w: word;
ss: single;
obj: TRealObj;
begin
fillchar(s, sizeof(s), 0);
Read(@s.uid, 2);
Read(@s.playerUID, 1);
Read(@s.itemid, 1);
Read(@w, 2);
s.x:=w;
Read(@w, 2);
s.y:=w;
Read(@w, 2);
s.angle:=w;
s.angle:=s.angle*Pi/180;
ss:=WPN_SPEED[s.ItemID];
s.dx:=ss*cos(s.angle);
s.dy:=ss*sin(s.angle);
s.objtype:=otShot;
obj:=RealObj_Add(s);
for w:=1 to (Server.StatPing div 40) do
if not obj.dead then
obj.Update;
Map.Demo.RecShotObjCreate(obj);
end;
procedure TClient_Machine.shot_recv;
var
uid, weapon_: byte;
x_, y_, angle_: word;
begin
Read(@uid, 1);
Read(@weapon_, 1);
Read(@x_, 2);
Read(@y_, 2);
Read(@angle_, 2);
if Map.pl_find(uid, C_PLAYER_NET) then
with Map.pl_current do
begin
// cur_wpn:=weapon_;
// SwitchTicker:=2;
SetWeapon(weapon_);
fshot:=true;
shotpos:=Point2f(x_, y_);
absangle:=angle_;
end;
//îòñûëàòü ýòî âñå ïî ñåòêå ÷òîëè?? à íàõ, ñàìî ïîøëåòñÿ, êîãäà âðåìÿ íàñòàíåò
end;
procedure TClient_Machine.shot_send(uid: byte);
var
pl: TPlayer;
b: byte; w: word;
s: single;
begin
if Map.pl_find(uid, C_PLAYER_ALL) then
begin
pl:=Map.pl_current;
Message_(NM_SHOT);
NET_Write(@uid, 1);
b:=pl.cur_weapon;
NET_Write(@b, 1);
w:=round(pl.shotpos.x);
NET_Write(@w, 2);
w:=round(pl.shotpos.y);
NET_Write(@w, 2);
s:=pl.AbsAngle;
while s<0 do
s:=s+360;
w:=round(s);
NET_Write(@w, 2);
Server.Send(true);
end;
end;
function TClient_Machine.spects_Count: integer;
begin
Result:=sp_count;
end;
procedure TClient_Machine.TeamJoin(uid, team: byte);
begin
Message_(NM_TEAMJOIN);
NET_Write(@uid, 1);
NET_Write(@team, 1);
Server.Send(true);
end;
procedure TClient_Machine.teamjoin_recv;
var
uid, team: byte;
begin
Read(@uid, 1); //÷èòàåì òèï èãðîêà
Read(@team, 1);
if Map.pl_find(uid, C_PLAYER_ALL) then
Map.TeamJoin(uid, team);
end;
procedure TClient_Machine.Update_Next;
begin
inherited;
if timer mod net_sync=0 then
begin
Message_(NM_PLAYERS);
if players_write(Server) then
Server.Send(false);
players_sending_end;
end;
end;
procedure TClient_Machine.Update_Prev;
var
IP_ : PChar;
IP : string;
i : integer;
port : word;
Msg,tb : Byte;
begin
inherited;
IP_ := nil;
NET_Buf_seek:=0;
NET_Buf_len := NET_Recv(NET_Buf, NET_BufLen, IP_, i);
port:=i;
IP := StrPas(IP_);
i := NET_Buf_len;
while (NET_Buf_len>0) do
begin
Read(@Msg, 1);
case Msg of
NM_Msg: msg_Recv(IP, Port, i-1);
NM_RInfo: rinfo_recv(IP, Port);
NM_Invite: rinfo_recv(IP, Port, 1);
end;
if (IP=Server.IP) and (port=Server.Port) then
case Msg of
NM_PING:
server.SendPong;
NM_PONG:
server.RecvPing;
NM_PINGS:
pings_recv;
NM_DISCONNECT:
begin
Read(@tb, 1);
case tb of
NM_CLIENTLEAVE: Log('^1Disconnected');
NM_TIMEOUT: Log('^1You was dropped by timeout!');
NM_SERVERLEAVE: Log('^1Server leaves the game!');
NM_KICK: Log('^1You have kicked by server!');
end;
Map.ClearAll;
Exit;
end;
NM_GAMEINFO:
begin
Read(@tb, 1);
ReadInfo(tb);
end;
NM_PLAYERS:
begin
Read(@serv_btimer, 1);
if Server.Valid(serv_btimer) then
while NET_buf_SEEK<i do
players_recv;
end;
NM_GAMEMSG:
begin
Read(@serv_btimer, 1);
while NET_buf_SEEK<i-1 do
gamemsg_recv;
end;
NM_ADDPLAYER:
addplayer_recv;
NM_DELPLAYER:
removeplayer_recv;
NM_SAY:
say_Recv;
NM_CHANGENAME:
changename_Recv;
NM_TEAMJOIN:
teamjoin_recv;
end;
IP_ := nil;
NET_Buf_seek:=0;
NET_Buf_len := NET_Recv(NET_Buf, NET_BufLen, IP_, i);
port:=i;
IP := StrPas(IP_);
i := NET_Buf_len;
end;//while
//SERVER PING TIMEOUT
if GetTickCount - server.LastPing > 1000*Net_Timeout then
begin
log('^1Server Ping Timeout');
Map.ClearAll;
Exit;
end;
if timer mod 50=0 then
Server.SendPing;
end;
end.