DEADSOFTWARE

put "{$MODE ...}" directive in each source file; removed trailing spaces, and convert...
[d2df-sdl.git] / src / game / g_netmsg.pas
1 {$MODE DELPHI}
2 unit g_netmsg;
4 interface
6 uses g_net, g_triggers, Classes, SysUtils, md5;
8 const
9 NET_PROTO_VERSION = 1;
11 NET_MSG_INFO = 100;
13 NET_MSG_CHAT = 101;
14 NET_MSG_SND = 102;
15 NET_MSG_GFX = 103;
16 NET_MSG_GEVENT = 104;
17 NET_MSG_SCORE = 105;
18 NET_MSG_COOP = 106;
19 NET_MSG_FLAG = 107;
20 NET_MSG_REQFST = 108;
21 NET_MSG_GSET = 109;
23 NET_MSG_PLR = 111;
24 NET_MSG_PLRPOS = 112;
25 NET_MSG_PLRSTA = 113;
26 NET_MSG_PLRDEL = 114;
27 NET_MSG_PLRDMG = 115;
28 NET_MSG_PLRDIE = 116;
29 NET_MSG_PLRFIRE= 117;
30 NET_MSG_PLRSET = 119;
31 NET_MSG_CHEAT = 120;
33 NET_MSG_ISPAWN = 121;
34 NET_MSG_IDEL = 122;
36 NET_MSG_MSPAWN = 131;
37 NET_MSG_MPOS = 132;
38 NET_MSG_MSTATE = 133;
39 NET_MSG_MSHOT = 134;
40 NET_MSG_MDEL = 135;
42 NET_MSG_PSTATE = 141;
43 NET_MSG_PTEX = 142;
45 NET_MSG_TSOUND = 151;
46 NET_MSG_TMUSIC = 152;
48 NET_MSG_SHDEL = 161;
49 NET_MSG_SHADD = 162;
50 NET_MSG_SHPOS = 163;
52 NET_MSG_RCON_AUTH = 191;
53 NET_MSG_RCON_CMD = 192;
54 NET_MSG_TIME_SYNC = 194;
55 NET_MSG_VOTE_EVENT = 195;
57 NET_MSG_MAP_REQUEST = 201;
58 NET_MSG_MAP_RESPONSE = 202;
59 NET_MSG_RES_REQUEST = 203;
60 NET_MSG_RES_RESPONSE = 204;
62 NET_CHAT_SYSTEM = 0;
63 NET_CHAT_PLAYER = 1;
64 NET_CHAT_TEAM = 2;
66 NET_RCON_NOAUTH = 0;
67 NET_RCON_PWGOOD = 1;
68 NET_RCON_PWBAD = 2;
70 NET_GFX_SPARK = 1;
71 NET_GFX_TELE = 2;
72 NET_GFX_RESPAWN = 3;
73 NET_GFX_FIRE = 4;
74 NET_GFX_EXPLODE = 5;
75 NET_GFX_BFGEXPL = 6;
76 NET_GFX_BFGHIT = 7;
77 NET_GFX_SHELL1 = 8;
78 NET_GFX_SHELL2 = 9;
79 NET_GFX_SHELL3 = 10;
81 NET_EV_MAPSTART = 1;
82 NET_EV_MAPEND = 2;
83 NET_EV_CHANGE_TEAM = 3;
84 NET_EV_PLAYER_KICK = 4;
85 NET_EV_PLAYER_BAN = 5;
86 NET_EV_LMS_WARMUP = 6;
87 NET_EV_LMS_SURVIVOR = 7;
88 NET_EV_RCON = 8;
89 NET_EV_BIGTEXT = 9;
90 NET_EV_SCORE = 10;
91 NET_EV_SCORE_MSG = 11;
92 NET_EV_LMS_START = 12;
93 NET_EV_LMS_WIN = 13;
94 NET_EV_TLMS_WIN = 14;
95 NET_EV_LMS_LOSE = 15;
96 NET_EV_LMS_DRAW = 16;
97 NET_EV_KILLCOMBO = 17;
98 NET_EV_PLAYER_TOUCH = 18;
100 NET_VE_STARTED = 1;
101 NET_VE_PASSED = 2;
102 NET_VE_FAILED = 3;
103 NET_VE_VOTE = 4;
104 NET_VE_REVOKE = 5;
105 NET_VE_INPROGRESS = 6;
107 NET_FLAG_GET = 1;
108 NET_FLAG_DROP = 2;
109 NET_FLAG_CAP = 3;
110 NET_FLAG_RETURN = 4;
112 NET_CHEAT_SUICIDE = 1;
113 NET_CHEAT_SPECTATE = 2;
115 NET_MAX_DIFFTIME = 5000 div 36;
117 // HOST MESSAGES
119 procedure MH_RECV_Info(C: pTNetClient; P: Pointer);
120 procedure MH_RECV_Chat(C: pTNetClient; P: Pointer);
121 procedure MH_RECV_FullStateRequest(C: pTNetClient; P: Pointer);
122 function MH_RECV_PlayerPos(C: pTNetClient; P: Pointer): Word;
123 procedure MH_RECV_PlayerSettings(C: pTNetClient; P: Pointer);
124 procedure MH_RECV_CheatRequest(C: pTNetClient; P: Pointer);
125 procedure MH_RECV_RCONPassword(C: pTNetClient; P: Pointer);
126 procedure MH_RECV_RCONCommand(C: pTNetClient; P: Pointer);
127 procedure MH_RECV_MapRequest(C: pTNetClient; P: Pointer);
128 procedure MH_RECV_ResRequest(C: pTNetClient; P: Pointer);
129 procedure MH_RECV_Vote(C: pTNetClient; P: Pointer);
131 // GAME
132 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
133 procedure MH_SEND_Info(ID: Byte);
134 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
135 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
136 procedure MH_SEND_Sound(X, Y: Integer; Name: string; ID: Integer = NET_EVERYONE);
137 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
138 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
139 procedure MH_SEND_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
140 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
141 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
142 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
143 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
144 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
145 // PLAYER
146 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
147 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
148 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
149 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
150 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
151 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
152 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
153 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
154 // ITEM
155 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
156 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
157 // PANEL
158 procedure MH_SEND_PanelTexture(PType: Word; PID: LongWord; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
159 procedure MH_SEND_PanelState(PType: Word; PID: LongWord; ID: Integer = NET_EVERYONE);
160 // MONSTER
161 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
162 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
163 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
164 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
165 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
166 // TRIGGER
167 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
168 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
169 // MISC
170 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
171 procedure MH_SEND_VoteEvent(EvType: Byte;
172 StrArg1: string = 'a'; StrArg2: string = 'b';
173 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
174 ID: Integer = NET_EVERYONE);
176 // CLIENT MESSAGES //
178 // GAME
179 procedure MC_RECV_Chat(P: Pointer);
180 procedure MC_RECV_Effect(P: Pointer);
181 procedure MC_RECV_Sound(P: Pointer);
182 procedure MC_RECV_GameStats(P: Pointer);
183 procedure MC_RECV_CoopStats(P: Pointer);
184 procedure MC_RECV_GameEvent(P: Pointer);
185 procedure MC_RECV_FlagEvent(P: Pointer);
186 procedure MC_RECV_GameSettings(P: Pointer);
187 // PLAYER
188 function MC_RECV_PlayerCreate(P: Pointer): Word;
189 function MC_RECV_PlayerPos(P: Pointer): Word;
190 function MC_RECV_PlayerStats(P: Pointer): Word;
191 function MC_RECV_PlayerDelete(P: Pointer): Word;
192 function MC_RECV_PlayerDamage(P: Pointer): Word;
193 function MC_RECV_PlayerDeath(P: Pointer): Word;
194 function MC_RECV_PlayerFire(P: Pointer): Word;
195 procedure MC_RECV_PlayerSettings(P: Pointer);
196 // ITEM
197 procedure MC_RECV_ItemSpawn(P: Pointer);
198 procedure MC_RECV_ItemDestroy(P: Pointer);
199 // PANEL
200 procedure MC_RECV_PanelTexture(P: Pointer);
201 procedure MC_RECV_PanelState(P: Pointer);
202 // MONSTER
203 procedure MC_RECV_MonsterSpawn(P: Pointer);
204 procedure MC_RECV_MonsterPos(P: Pointer);
205 procedure MC_RECV_MonsterState(P: Pointer);
206 procedure MC_RECV_MonsterShot(P: Pointer);
207 procedure MC_RECV_MonsterDelete(P: Pointer);
208 // SHOT
209 procedure MC_RECV_CreateShot(P: Pointer);
210 procedure MC_RECV_UpdateShot(P: Pointer);
211 procedure MC_RECV_DeleteShot(P: Pointer);
212 // TRIGGER
213 procedure MC_RECV_TriggerSound(P: Pointer);
214 procedure MC_RECV_TriggerMusic(P: Pointer);
215 // MISC
216 procedure MC_RECV_TimeSync(P: Pointer);
217 procedure MC_RECV_VoteEvent(P: Pointer);
218 // SERVICE
219 procedure MC_SEND_Info(Password: string);
220 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
221 procedure MC_SEND_PlayerPos();
222 procedure MC_SEND_FullStateRequest();
223 procedure MC_SEND_PlayerSettings();
224 procedure MC_SEND_CheatRequest(Kind: Byte);
225 procedure MC_SEND_RCONPassword(Password: string);
226 procedure MC_SEND_RCONCommand(Cmd: string);
227 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
228 // DOWNLOAD
229 procedure MC_SEND_MapRequest();
230 procedure MC_SEND_ResRequest(const resName: AnsiString);
232 type
233 TExternalResourceInfo = record
234 Name: string[255];
235 md5: TMD5Digest;
236 end;
238 TResDataMsg = record
239 MsgId: Byte;
240 FileSize: Integer;
241 FileData: AByte;
242 end;
244 TMapDataMsg = record
245 MsgId: Byte;
246 FileSize: Integer;
247 FileData: AByte;
248 ExternalResources: array of TExternalResourceInfo;
249 end;
251 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
252 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
254 implementation
256 uses
257 Math, ENet, e_input, e_fixedbuffer, e_graphics, e_log,
258 g_textures, g_gfx, g_sound, g_console, g_basic, g_options, g_main,
259 g_game, g_player, g_map, g_panel, g_items, g_weapons, g_phys, g_gui,
260 g_language, g_monsters, g_netmaster, utils, wadreader, MAPDEF;
262 const
263 NET_KEY_LEFT = 1;
264 NET_KEY_RIGHT = 2;
265 NET_KEY_UP = 4;
266 NET_KEY_DOWN = 8;
267 NET_KEY_JUMP = 16;
268 NET_KEY_FIRE = 32;
269 NET_KEY_OPEN = 64;
270 NET_KEY_NW = 256;
271 NET_KEY_PW = 512;
272 NET_KEY_CHAT = 2048;
273 NET_KEY_FORCEDIR = 4096;
275 //var
276 //kBytePrev: Word = 0;
277 //kDirPrev: TDirection = D_LEFT;
278 //HostGameTime: Word = 0;
280 // HOST MESSAGES //
283 // GAME
285 procedure MH_RECV_Chat(C: pTNetClient; P: Pointer);
286 var
287 Txt: string;
288 Mode: Byte;
289 PID: Word;
290 Pl: TPlayer;
291 begin
292 PID := C^.Player;
293 Pl := g_Player_Get(PID);
295 Txt := e_Raw_Read_String(P);
296 Mode := e_Raw_Read_Byte(P);
297 if (Mode = NET_CHAT_SYSTEM) then
298 Mode := NET_CHAT_PLAYER; // prevent sending system messages from clients
299 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
300 Mode := NET_CHAT_PLAYER; // revert to player chat in non-team game
302 if Pl = nil then
303 MH_SEND_Chat(Txt, Mode)
304 else
305 MH_SEND_Chat(Pl.Name + ': ' + Txt, Mode, IfThen(Mode = NET_CHAT_TEAM, Pl.Team, NET_EVERYONE));
306 end;
308 procedure MH_RECV_Info(C: pTNetClient; P: Pointer);
309 var
310 Ver, PName, Model, Pw: string;
311 R, G, B, T: Byte;
312 PID: Word;
313 Color: TRGB;
314 I: Integer;
315 ProtoVer, Zero: Byte;
316 begin
317 Zero := e_Raw_Read_Byte(P);
318 ProtoVer := e_Raw_Read_Byte(P);
319 Ver := e_Raw_Read_String(P);
320 Pw := e_Raw_Read_String(P);
321 PName := e_Raw_Read_String(P);
322 Model := e_Raw_Read_String(P);
323 R := e_Raw_Read_Byte(P);
324 G := e_Raw_Read_Byte(P);
325 B := e_Raw_Read_Byte(P);
326 T := e_Raw_Read_Byte(P);
328 if (Ver <> GAME_VERSION) or (ProtoVer <> NET_PROTO_VERSION) or (Zero <> 0) then
329 begin
330 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
331 _lc[I_NET_DISC_VERSION]);
332 enet_peer_disconnect(C^.Peer, NET_DISC_VERSION);
333 Exit;
334 end;
336 if g_Net_IsHostBanned(C^.Peer^.address.host) then
337 begin
338 if g_Net_IsHostBanned(C^.Peer^.address.host, True) then
339 begin
340 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
341 _lc[I_NET_DISC_BAN]);
342 enet_peer_disconnect(C^.Peer, NET_DISC_BAN);
343 end
344 else
345 begin
346 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
347 _lc[I_NET_DISC_BAN]);
348 enet_peer_disconnect(C^.Peer, NET_DISC_TEMPBAN);
349 end;
350 Exit;
351 end;
353 if NetPassword <> '' then
354 if AnsiLowerCase(NetPassword) <> AnsiLowerCase(Pw) then
355 begin
356 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
357 _lc[I_NET_DISC_PASSWORD]);
358 enet_peer_disconnect(C^.Peer, NET_DISC_PASSWORD);
359 Exit;
360 end;
362 Color.R := R;
363 Color.B := B;
364 Color.G := G;
366 PID := g_Player_Create(Model, Color, T, False);
367 with g_Player_Get(PID) do
368 begin
369 Name := PName;
370 Reset(True);
371 end;
373 C^.Player := PID;
375 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
376 e_WriteLog('NET: Client ' + PName + ' [' + IntToStr(C^.ID) +
377 '] connected. Assigned player #' + IntToStr(PID) + '.', MSG_NOTIFY);
379 MH_SEND_Info(C^.ID);
381 with g_Player_Get(PID) do
382 begin
383 Name := PName;
384 FClientID := C^.ID;
385 // round in progress, don't spawn
386 if (gGameSettings.MaxLives > 0) and (gLMSRespawn = LMS_RESPAWN_NONE) then
387 begin
388 Lives := 0;
389 FNoRespawn := True;
390 Spectate;
391 FWantsInGame := True; // TODO: look into this later
392 end
393 else
394 Respawn(gGameSettings.GameType = GT_SINGLE);
395 end;
397 for I := Low(NetClients) to High(NetClients) do
398 begin
399 if NetClients[I].ID = C^.ID then Continue;
400 MH_SEND_PlayerCreate(PID, NetClients[I].ID);
401 MH_SEND_PlayerPos(True, PID, NetClients[I].ID);
402 MH_SEND_PlayerStats(PID, NetClients[I].ID);
403 end;
405 if gState in [STATE_INTERCUSTOM, STATE_FOLD] then
406 MH_SEND_GameEvent(NET_EV_MAPEND, 0, 'N', C^.ID);
408 if NetUseMaster then g_Net_Slist_Update;
409 end;
411 procedure MH_RECV_FullStateRequest(C: pTNetClient; P: Pointer);
412 begin
413 if gGameOn then
414 MH_SEND_Everything((C^.State = NET_STATE_AUTH), C^.ID)
415 else
416 C^.RequestedFullUpdate := True;
417 end;
419 // PLAYER
421 function MH_RECV_PlayerPos(C: pTNetClient; P: Pointer): Word;
422 var
423 Dir: Byte;
424 PID: Word;
425 kByte: Word;
426 Pl: TPlayer;
427 GT: LongWord;
428 begin
429 Result := 0;
430 if not gGameOn then Exit;
432 GT := e_Raw_Read_LongWord(P);
433 PID := C^.Player;
434 Pl := g_Player_Get(PID);
435 if Pl = nil then
436 Exit;
438 if (GT > gTime + NET_MAX_DIFFTIME) or (GT < Pl.NetTime) then Exit;
440 with Pl do
441 begin
442 NetTime := GT;
443 kByte := e_Raw_Read_Word(P);
444 Dir := e_Raw_Read_Byte(P);
445 if Direction <> TDirection(Dir) then
446 JustTeleported := False;
447 SetDirection(TDirection(Dir));
448 ReleaseKeys;
450 if kByte = NET_KEY_CHAT then
451 begin
452 PressKey(KEY_CHAT, 10000);
453 Exit;
454 end;
456 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
457 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
458 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
459 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
460 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
461 if LongBool(kByte and NET_KEY_FIRE) then PressKey(KEY_FIRE, 10000);
462 if LongBool(kByte and NET_KEY_OPEN) then PressKey(KEY_OPEN, 10000);
463 if LongBool(kByte and NET_KEY_NW) then PressKey(KEY_NEXTWEAPON, 10000);
464 if LongBool(kByte and NET_KEY_PW) then PressKey(KEY_PREVWEAPON, 10000);
465 end;
467 // MH_SEND_PlayerPos(False, PID, C^.ID);
468 end;
470 procedure MH_RECV_CheatRequest(C: pTNetClient; P: Pointer);
471 var
472 CheatKind: Byte;
473 Pl: TPlayer;
474 begin
475 Pl := g_Player_Get(C^.Player);
476 if Pl = nil then Exit;
478 CheatKind := e_Raw_Read_Byte(P);
480 case CheatKind of
481 NET_CHEAT_SUICIDE:
482 Pl.Damage(SUICIDE_DAMAGE, Pl.UID, 0, 0, HIT_SELF);
483 NET_CHEAT_SPECTATE:
484 begin
485 if Pl.FSpectator then
486 Pl.Respawn(False)
487 else
488 Pl.Spectate;
489 end;
490 end;
491 end;
493 procedure MH_RECV_PlayerSettings(C: pTNetClient; P: Pointer);
494 var
495 TmpName: string;
496 TmpModel: string;
497 TmpColor: TRGB;
498 TmpTeam: Byte;
499 Pl: TPlayer;
500 begin
501 TmpName := e_Raw_Read_String(P);
502 TmpModel := e_Raw_Read_String(P);
503 TmpColor.R := e_Raw_Read_Byte(P);
504 TmpColor.G := e_Raw_Read_Byte(P);
505 TmpColor.B := e_Raw_Read_Byte(P);
506 TmpTeam := e_Raw_Read_Byte(P);
508 Pl := g_Player_Get(C^.Player);
509 if Pl = nil then Exit;
511 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
512 Pl.SwitchTeam
513 else
514 Pl.SetColor(TmpColor);
516 if Pl.Name <> TmpName then
517 begin
518 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
519 Pl.Name := TmpName;
520 end;
522 if TmpModel <> Pl.Model.Name then
523 Pl.SetModel(TmpModel);
525 MH_SEND_PlayerSettings(Pl.UID, TmpModel);
526 end;
528 // RCON
530 procedure MH_RECV_RCONPassword(C: pTNetClient; P: Pointer);
531 var
532 Pwd: string;
533 begin
534 Pwd := e_Raw_Read_String(P);
535 if not NetAllowRCON then Exit;
536 if Pwd = NetRCONPassword then
537 begin
538 C^.RCONAuth := True;
539 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWGOOD, 'N', C^.ID);
540 end
541 else
542 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWBAD, 'N', C^.ID);
543 end;
545 procedure MH_RECV_RCONCommand(C: pTNetClient; P: Pointer);
546 var
547 Cmd: string;
548 begin
549 Cmd := e_Raw_Read_String(P);
550 if not NetAllowRCON then Exit;
551 if not C^.RCONAuth then
552 begin
553 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_NOAUTH, 'N', C^.ID);
554 Exit;
555 end;
556 g_Console_Process(Cmd);
557 end;
559 // MISC
561 procedure MH_RECV_Vote(C: pTNetClient; P: Pointer);
562 var
563 Start: Boolean;
564 Name, Command: string;
565 Need: Integer;
566 Pl: TPlayer;
567 begin
568 Start := e_Raw_Read_Byte(P) <> 0;
569 Command := e_Raw_Read_String(P);
571 Pl := g_Player_Get(C^.Player);
572 if Pl = nil then Exit;
573 Name := Pl.Name;
575 if Start then
576 begin
577 if not g_Console_CommandBlacklisted(Command) then
578 g_Game_StartVote(Command, Name);
579 end
580 else if gVoteInProgress then
581 begin
582 if (gPlayer1 <> nil) or (gPlayer2 <> nil) then
583 Need := Floor((NetClientCount+1)/2.0) + 1
584 else
585 Need := Floor(NetClientCount/2.0) + 1;
586 if C^.Voted then
587 begin
588 Dec(gVoteCount);
589 C^.Voted := False;
590 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_REVOKED], [Name, gVoteCount, Need]), True);
591 MH_SEND_VoteEvent(NET_VE_REVOKE, Name, 'a', gVoteCount, Need);
592 end
593 else
594 begin
595 Inc(gVoteCount);
596 C^.Voted := True;
597 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Name, gVoteCount, Need]), True);
598 MH_SEND_VoteEvent(NET_VE_VOTE, Name, 'a', gVoteCount, Need);
599 g_Game_CheckVote;
600 end;
601 end;
602 end;
604 // GAME (SEND)
606 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
607 var
608 I: Integer;
609 begin
610 if gPlayers <> nil then
611 for I := Low(gPlayers) to High(gPlayers) do
612 if gPlayers[I] <> nil then
613 begin
614 if CreatePlayers then MH_SEND_PlayerCreate(gPlayers[I].UID, ID);
615 MH_SEND_PlayerPos(True, gPlayers[I].UID, ID);
616 MH_SEND_PlayerStats(gPlayers[I].UID, ID);
618 if (gPlayers[I].Flag <> FLAG_NONE) and (gGameSettings.GameMode = GM_CTF) then
619 MH_SEND_FlagEvent(FLAG_STATE_CAPTURED, gPlayers[I].Flag, gPlayers[I].UID, True, ID);
620 end;
622 if gItems <> nil then
623 begin
624 for I := High(gItems) downto Low(gItems) do
625 if gItems[I].Live then
626 MH_SEND_ItemSpawn(True, I, ID);
627 end;
629 if gMonsters <> nil then
630 for I := 0 to High(gMonsters) do
631 if gMonsters[I] <> nil then
632 MH_SEND_MonsterSpawn(gMonsters[I].UID, ID);
634 if gWalls <> nil then
635 for I := Low(gWalls) to High(gWalls) do
636 if gWalls[I] <> nil then
637 with gWalls[I] do
638 begin
639 if Door then
640 MH_SEND_PanelState(PanelType, I, ID);
642 if GetTextureCount > 1 then
643 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
644 end;
646 if gLifts <> nil then
647 for I := Low(gLifts) to High(gLifts) do
648 if gLifts[I] <> nil then
649 with gLifts[I] do
650 MH_SEND_PanelState(PanelType, I, ID);
652 if gRenderForegrounds <> nil then
653 for I := Low(gRenderForegrounds) to High(gRenderForegrounds) do
654 if gRenderForegrounds[I] <> nil then
655 with gRenderForegrounds[I] do
656 if (GetTextureCount > 1) then
657 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
658 if gRenderBackgrounds <> nil then
659 for I := Low(gRenderBackgrounds) to High(gRenderBackgrounds) do
660 if gRenderBackgrounds[I] <> nil then
661 with gRenderBackgrounds[I] do
662 if GetTextureCount > 1 then
663 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
664 if gWater <> nil then
665 for I := Low(gWater) to High(gWater) do
666 if gWater[I] <> nil then
667 with gWater[I] do
668 if GetTextureCount > 1 then
669 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
670 if gAcid1 <> nil then
671 for I := Low(gAcid1) to High(gAcid1) do
672 if gAcid1[I] <> nil then
673 with gAcid1[I] do
674 if GetTextureCount > 1 then
675 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
676 if gAcid2 <> nil then
677 for I := Low(gAcid2) to High(gAcid2) do
678 if gAcid2[I] <> nil then
679 with gAcid2[I] do
680 if GetTextureCount > 1 then
681 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
682 if gSteps <> nil then
683 for I := Low(gSteps) to High(gSteps) do
684 if gSteps[I] <> nil then
685 with gSteps[I] do
686 if GetTextureCount > 1 then
687 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
689 if gTriggers <> nil then
690 for I := Low(gTriggers) to High(gTriggers) do
691 if gTriggers[I].TriggerType = TRIGGER_SOUND then
692 MH_SEND_TriggerSound(gTriggers[I], ID);
694 if Shots <> nil then
695 for I := Low(Shots) to High(Shots) do
696 if Shots[i].ShotType in [6, 7, 8] then
697 MH_SEND_CreateShot(i, ID);
699 MH_SEND_TriggerMusic(ID);
701 MH_SEND_GameStats(ID);
702 MH_SEND_CoopStats(ID);
704 if gGameSettings.GameMode = GM_CTF then
705 begin
706 if gFlags[FLAG_RED].State <> FLAG_STATE_CAPTURED then
707 MH_SEND_FlagEvent(gFlags[FLAG_RED].State, FLAG_RED, 0, True, ID);
708 if gFlags[FLAG_BLUE].State <> FLAG_STATE_CAPTURED then
709 MH_SEND_FlagEvent(gFlags[FLAG_BLUE].State, FLAG_BLUE, 0, True, ID);
710 end;
712 if CreatePlayers and (ID >= 0) then NetClients[ID].State := NET_STATE_GAME;
714 if gLMSRespawn > LMS_RESPAWN_NONE then
715 MH_SEND_GameEvent(NET_EV_LMS_WARMUP, (gLMSRespawnTime - gTime) div 1000, 'N', ID);
716 end;
718 procedure MH_SEND_Info(ID: Byte);
719 var
720 Map: string;
721 begin
722 g_ProcessResourceStr(gMapInfo.Map, nil, nil, @Map);
724 e_Buffer_Clear(@NetOut);
726 e_Buffer_Write(@NetOut, Byte(NET_MSG_INFO));
727 e_Buffer_Write(@NetOut, ID);
728 e_Buffer_Write(@NetOut, NetClients[ID].Player);
729 e_Buffer_Write(@NetOut, gGameSettings.WAD);
730 e_Buffer_Write(@NetOut, Map);
731 e_Buffer_Write(@NetOut, gWADHash);
732 e_Buffer_Write(@NetOut, gGameSettings.GameMode);
733 e_Buffer_Write(@NetOut, gGameSettings.GoalLimit);
734 e_Buffer_Write(@NetOut, gGameSettings.TimeLimit);
735 e_Buffer_Write(@NetOut, gGameSettings.MaxLives);
736 e_Buffer_Write(@NetOut, gGameSettings.Options);
737 e_Buffer_Write(@NetOut, gTime);
739 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
740 end;
742 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
743 var
744 Name: string;
745 i: Integer;
746 Team: Byte;
747 begin
748 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
749 Mode := NET_CHAT_PLAYER;
751 Team := 0;
752 if (Mode = NET_CHAT_TEAM) then
753 begin
754 for i := Low(gPlayers) to High(gPlayers) do
755 if (gPlayers[i] <> nil) and (gPlayers[i].FClientID >= 0) and
756 (gPlayers[i].Team = ID) then
757 begin
758 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHAT));
759 e_Buffer_Write(@NetOut, Txt);
760 e_Buffer_Write(@NetOut, Mode);
761 g_Net_Host_Send(gPlayers[i].FClientID, True, NET_CHAN_CHAT);
762 end;
763 Team := ID;
764 ID := NET_EVERYONE;
765 end
766 else
767 begin
768 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHAT));
769 e_Buffer_Write(@NetOut, Txt);
770 e_Buffer_Write(@NetOut, Mode);
771 g_Net_Host_Send(ID, True, NET_CHAN_CHAT);
772 end;
774 if Mode = NET_CHAT_SYSTEM then
775 Exit;
777 if ID = NET_EVERYONE then
778 begin
779 if Mode = NET_CHAT_PLAYER then
780 begin
781 g_Console_Add(Txt, True);
782 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
783 g_Sound_PlayEx('SOUND_GAME_RADIO');
784 end
785 else
786 if Mode = NET_CHAT_TEAM then
787 if gPlayer1 <> nil then
788 begin
789 if (gPlayer1.Team = TEAM_RED) and (Team = TEAM_RED) then
790 begin
791 g_Console_Add(#18'[Team] '#2 + Txt, True);
792 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
793 g_Sound_PlayEx('SOUND_GAME_RADIO');
794 end
795 else if (gPlayer1.Team = TEAM_BLUE) and (Team = TEAM_BLUE) then
796 begin
797 g_Console_Add(#20'[Team] '#2 + Txt, True);
798 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
799 g_Sound_PlayEx('SOUND_GAME_RADIO');
800 end;
801 end;
802 end
803 else
804 begin
805 Name := g_Net_ClientName_ByID(ID);
806 g_Console_Add('-> ' + Name + ': ' + Txt, True);
807 e_WriteLog('[Tell ' + Name + '] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
808 g_Sound_PlayEx('SOUND_GAME_RADIO');
809 end;
810 end;
812 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
813 begin
814 e_Buffer_Write(@NetOut, Byte(NET_MSG_GFX));
815 e_Buffer_Write(@NetOut, Kind);
816 e_Buffer_Write(@NetOut, X);
817 e_Buffer_Write(@NetOut, Y);
818 e_Buffer_Write(@NetOut, Ang);
820 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
821 end;
823 procedure MH_SEND_Sound(X, Y: Integer; Name: string; ID: Integer = NET_EVERYONE);
824 begin
825 e_Buffer_Write(@NetOut, Byte(NET_MSG_SND));
826 e_Buffer_Write(@NetOut, Name);
827 e_Buffer_Write(@NetOut, X);
828 e_Buffer_Write(@NetOut, Y);
830 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
831 end;
833 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
834 begin
835 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
837 e_Buffer_Write(@NetOut, Byte(NET_MSG_SHADD));
838 e_Buffer_Write(@NetOut, Proj);
839 e_Buffer_Write(@NetOut, Shots[Proj].ShotType);
840 e_Buffer_Write(@NetOut, Shots[Proj].Target);
841 e_Buffer_Write(@NetOut, Shots[Proj].SpawnerUID);
842 e_Buffer_Write(@NetOut, Shots[Proj].Timeout);
843 e_Buffer_Write(@NetOut, Shots[Proj].Obj.X);
844 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Y);
845 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.X);
846 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.Y);
848 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
849 end;
851 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
852 begin
853 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
855 e_Buffer_Write(@NetOut, Byte(NET_MSG_SHPOS));
856 e_Buffer_Write(@NetOut, Proj);
857 e_Buffer_Write(@NetOut, Shots[Proj].Obj.X);
858 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Y);
859 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.X);
860 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.Y);
862 g_Net_Host_Send(ID, False, NET_CHAN_SHOTS);
863 end;
865 procedure MH_Send_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
866 begin
867 e_Buffer_Write(@NetOut, Byte(NET_MSG_SHDEL));
868 e_Buffer_Write(@NetOut, Proj);
869 e_Buffer_Write(@NetOut, Byte(Loud));
870 e_Buffer_Write(@NetOut, X);
871 e_Buffer_Write(@NetOut, Y);
873 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
874 end;
876 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
877 begin
878 e_Buffer_Write(@NetOut, Byte(NET_MSG_SCORE));
879 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
880 begin
881 e_Buffer_Write(@NetOut, gTeamStat[TEAM_RED].Goals);
882 e_Buffer_Write(@NetOut, gTeamStat[TEAM_BLUE].Goals);
883 end
884 else
885 if gGameSettings.GameMode = GM_COOP then
886 begin
887 e_Buffer_Write(@NetOut, gCoopMonstersKilled);
888 e_Buffer_Write(@NetOut, gCoopSecretsFound);
889 end;
891 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
892 end;
894 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
895 begin
896 e_Buffer_Write(@NetOut, Byte(NET_MSG_COOP));
897 e_Buffer_Write(@NetOut, gTotalMonsters);
898 e_Buffer_Write(@NetOut, gSecretsCount);
899 e_Buffer_Write(@NetOut, gCoopTotalMonstersKilled);
900 e_Buffer_Write(@NetOut, gCoopTotalSecretsFound);
901 e_Buffer_Write(@NetOut, gCoopTotalMonsters);
902 e_Buffer_Write(@NetOut, gCoopTotalSecrets);
903 end;
905 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
906 begin
907 e_Buffer_Write(@NetOut, Byte(NET_MSG_GEVENT));
908 e_Buffer_Write(@NetOut, EvType);
909 e_Buffer_Write(@NetOut, EvNum);
910 e_Buffer_Write(@NetOut, EvStr);
911 e_Buffer_Write(@NetOut, Byte(gLastMap));
912 e_Buffer_Write(@NetOut, gTime);
913 if (EvType = NET_EV_MAPSTART) and (Pos(':\', EvStr) > 0) then
914 begin
915 e_Buffer_Write(@NetOut, Byte(1));
916 e_Buffer_Write(@NetOut, gWADHash);
917 end else
918 e_Buffer_Write(@NetOut, Byte(0));
920 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
921 end;
923 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
924 begin
925 e_Buffer_Write(@NetOut, Byte(NET_MSG_FLAG));
926 e_Buffer_Write(@NetOut, EvType);
927 e_Buffer_Write(@NetOut, Flag);
928 e_Buffer_Write(@NetOut, Byte(Quiet));
929 e_Buffer_Write(@NetOut, PID);
930 e_Buffer_Write(@NetOut, gFlags[Flag].State);
931 e_Buffer_Write(@NetOut, gFlags[Flag].CaptureTime);
932 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.X);
933 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.Y);
934 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.Vel.X);
935 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.Vel.Y);
937 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
938 end;
940 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
941 begin
942 e_Buffer_Write(@NetOut, Byte(NET_MSG_GSET));
943 e_Buffer_Write(@NetOut, gGameSettings.GameMode);
944 e_Buffer_Write(@NetOut, gGameSettings.GoalLimit);
945 e_Buffer_Write(@NetOut, gGameSettings.TimeLimit);
946 e_Buffer_Write(@NetOut, gGameSettings.MaxLives);
947 e_Buffer_Write(@NetOut, gGameSettings.Options);
949 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
950 end;
952 // PLAYER (SEND)
954 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
955 var
956 P: TPlayer;
957 begin
958 P := g_Player_Get(PID);
959 if P = nil then Exit;
961 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLR));
962 e_Buffer_Write(@NetOut, PID);
963 e_Buffer_Write(@NetOut, P.Name);
965 e_Buffer_Write(@NetOut, P.FActualModelName);
966 e_Buffer_Write(@NetOut, P.FColor.R);
967 e_Buffer_Write(@NetOut, P.FColor.G);
968 e_Buffer_Write(@NetOut, P.FColor.B);
969 e_Buffer_Write(@NetOut, P.Team);
971 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT)
972 end;
974 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
975 var
976 kByte: Word;
977 Pl: TPlayer;
978 begin
979 Pl := g_Player_Get(PID);
980 if Pl = nil then Exit;
981 if Pl.FDummy then Exit;
983 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRPOS));
984 e_Buffer_Write(@NetOut, gTime);
985 e_Buffer_Write(@NetOut, PID);
987 kByte := 0;
989 with Pl do
990 begin
991 e_Buffer_Write(@NetOut, FPing);
992 e_Buffer_Write(@NetOut, FLoss);
993 if IsKeyPressed(KEY_CHAT) then
994 kByte := NET_KEY_CHAT
995 else
996 begin
997 if IsKeyPressed(KEY_LEFT) then kByte := kByte or NET_KEY_LEFT;
998 if IsKeyPressed(KEY_RIGHT) then kByte := kByte or NET_KEY_RIGHT;
999 if IsKeyPressed(KEY_UP) then kByte := kByte or NET_KEY_UP;
1000 if IsKeyPressed(KEY_DOWN) then kByte := kByte or NET_KEY_DOWN;
1001 if IsKeyPressed(KEY_JUMP) then kByte := kByte or NET_KEY_JUMP;
1002 if JustTeleported then kByte := kByte or NET_KEY_FORCEDIR;
1003 end;
1005 e_Buffer_Write(@NetOut, kByte);
1006 if Direction = D_LEFT then e_Buffer_Write(@NetOut, Byte(0)) else e_Buffer_Write(@NetOut, Byte(1));
1007 e_Buffer_Write(@NetOut, GameX);
1008 e_Buffer_Write(@NetOut, GameY);
1009 e_Buffer_Write(@NetOut, GameVelX);
1010 e_Buffer_Write(@NetOut, GameVelY);
1011 e_Buffer_Write(@NetOut, GameAccelX);
1012 e_Buffer_Write(@NetOut, GameAccelY);
1013 end;
1015 g_Net_Host_Send(ID, Reliable, NET_CHAN_PLAYERPOS);
1016 end;
1018 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
1019 var
1020 P: TPlayer;
1021 I: Integer;
1022 begin
1023 P := g_Player_Get(PID);
1024 if P = nil then Exit;
1026 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRSTA));
1027 e_Buffer_Write(@NetOut, PID);
1029 with P do
1030 begin
1031 e_Buffer_Write(@NetOut, Byte(Live));
1032 e_Buffer_Write(@NetOut, Byte(GodMode));
1033 e_Buffer_Write(@NetOut, Health);
1034 e_Buffer_Write(@NetOut, Armor);
1035 e_Buffer_Write(@NetOut, Air);
1036 e_Buffer_Write(@NetOut, JetFuel);
1037 e_Buffer_Write(@NetOut, Lives);
1038 e_Buffer_Write(@NetOut, Team);
1040 for I := WEAPON_KASTET to WEAPON_SUPERPULEMET do
1041 e_Buffer_Write(@NetOut, Byte(FWeapon[I]));
1043 for I := A_BULLETS to A_CELLS do
1044 e_Buffer_Write(@NetOut, FAmmo[I]);
1046 for I := A_BULLETS to A_CELLS do
1047 e_Buffer_Write(@NetOut, FMaxAmmo[I]);
1049 for I := MR_SUIT to MR_MAX do
1050 e_Buffer_Write(@NetOut, LongWord(FMegaRulez[I]));
1052 e_Buffer_Write(@NetOut, Byte(R_ITEM_BACKPACK in FRulez));
1053 e_Buffer_Write(@NetOut, Byte(R_KEY_RED in FRulez));
1054 e_Buffer_Write(@NetOut, Byte(R_KEY_GREEN in FRulez));
1055 e_Buffer_Write(@NetOut, Byte(R_KEY_BLUE in FRulez));
1056 e_Buffer_Write(@NetOut, Byte(R_BERSERK in FRulez));
1058 e_Buffer_Write(@NetOut, Frags);
1059 e_Buffer_Write(@NetOut, Death);
1061 e_Buffer_Write(@NetOut, CurrWeap);
1063 e_Buffer_Write(@NetOut, Byte(FSpectator));
1064 e_Buffer_Write(@NetOut, Byte(FGhost));
1065 e_Buffer_Write(@NetOut, Byte(FPhysics));
1066 e_Buffer_Write(@NetOut, Byte(FNoRespawn));
1067 e_Buffer_Write(@NetOut, Byte(FJetpack));
1068 end;
1070 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1071 end;
1073 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
1074 begin
1075 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRDMG));
1076 e_Buffer_Write(@NetOut, PID);
1077 e_Buffer_Write(@NetOut, Kind);
1078 e_Buffer_Write(@NetOut, Attacker);
1079 e_Buffer_Write(@NetOut, Value);
1080 e_Buffer_Write(@NetOut, VX);
1081 e_Buffer_Write(@NetOut, VY);
1083 g_Net_Host_Send(ID, False, NET_CHAN_PLAYER);
1084 end;
1086 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
1087 begin
1088 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRDIE));
1089 e_Buffer_Write(@NetOut, PID);
1090 e_Buffer_Write(@NetOut, KillType);
1091 e_Buffer_Write(@NetOut, DeathType);
1092 e_Buffer_Write(@NetOut, Attacker);
1094 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1095 end;
1097 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
1098 begin
1099 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRFIRE));
1100 e_Buffer_Write(@NetOut, PID);
1101 e_Buffer_Write(@NetOut, Weapon);
1102 e_Buffer_Write(@NetOut, X);
1103 e_Buffer_Write(@NetOut, Y);
1104 e_Buffer_Write(@NetOut, AX);
1105 e_Buffer_Write(@NetOut, AY);
1106 e_Buffer_Write(@NetOut, ShotID);
1108 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
1109 end;
1111 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
1112 begin
1113 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRDEL));
1114 e_Buffer_Write(@NetOut, PID);
1116 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1117 end;
1119 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
1120 var
1121 Pl: TPlayer;
1122 begin
1123 Pl := g_Player_Get(PID);
1124 if Pl = nil then Exit;
1126 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRSET));
1127 e_Buffer_Write(@NetOut, PID);
1128 e_Buffer_Write(@NetOut, Pl.Name);
1129 if Mdl = '' then
1130 e_Buffer_Write(@NetOut, Pl.Model.Name)
1131 else
1132 e_Buffer_Write(@NetOut, Mdl);
1133 e_Buffer_Write(@NetOut, Pl.FColor.R);
1134 e_Buffer_Write(@NetOut, Pl.FColor.G);
1135 e_Buffer_Write(@NetOut, Pl.FColor.B);
1136 e_Buffer_Write(@NetOut, Pl.Team);
1138 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1139 end;
1141 // ITEM (SEND)
1143 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1144 begin
1145 e_Buffer_Write(@NetOut, Byte(NET_MSG_ISPAWN));
1146 e_Buffer_Write(@NetOut, IID);
1147 e_Buffer_Write(@NetOut, Byte(Quiet));
1148 e_Buffer_Write(@NetOut, gItems[IID].ItemType);
1149 e_Buffer_Write(@NetOut, Byte(gItems[IID].Fall));
1150 e_Buffer_Write(@NetOut, Byte(gItems[IID].Respawnable));
1151 e_Buffer_Write(@NetOut, gItems[IID].Obj.X);
1152 e_Buffer_Write(@NetOut, gItems[IID].Obj.Y);
1153 e_Buffer_Write(@NetOut, gItems[IID].Obj.Vel.X);
1154 e_Buffer_Write(@NetOut, gItems[IID].Obj.Vel.Y);
1156 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1157 end;
1159 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1160 begin
1161 e_Buffer_Write(@NetOut, Byte(NET_MSG_IDEL));
1162 e_Buffer_Write(@NetOut, IID);
1163 e_Buffer_Write(@NetOut, Byte(Quiet));
1165 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1166 end;
1168 // PANEL
1170 procedure MH_SEND_PanelTexture(PType: Word; PID: LongWord; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
1171 var
1172 TP: TPanel;
1173 begin
1174 case PType of
1175 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
1176 TP := gWalls[PID];
1177 PANEL_FORE:
1178 TP := gRenderForegrounds[PID];
1179 PANEL_BACK:
1180 TP := gRenderBackgrounds[PID];
1181 PANEL_WATER:
1182 TP := gWater[PID];
1183 PANEL_ACID1:
1184 TP := gAcid1[PID];
1185 PANEL_ACID2:
1186 TP := gAcid2[PID];
1187 PANEL_STEP:
1188 TP := gSteps[PID];
1189 else
1190 Exit;
1191 end;
1193 with TP do
1194 begin
1195 e_Buffer_Write(@NetOut, Byte(NET_MSG_PTEX));
1196 e_Buffer_Write(@NetOut, PType);
1197 e_Buffer_Write(@NetOut, PID);
1198 e_Buffer_Write(@NetOut, FCurTexture);
1199 e_Buffer_Write(@NetOut, FCurFrame);
1200 e_Buffer_Write(@NetOut, FCurFrameCount);
1201 e_Buffer_Write(@NetOut, AnimLoop);
1202 end;
1204 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1205 end;
1207 procedure MH_SEND_PanelState(PType: Word; PID: LongWord; ID: Integer = NET_EVERYONE);
1208 var
1209 TP: TPanel;
1210 begin
1211 case PType of
1212 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
1213 TP := gWalls[PID];
1214 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
1215 TP := gLifts[PID];
1216 else
1217 Exit;
1218 end;
1220 e_Buffer_Write(@NetOut, Byte(NET_MSG_PSTATE));
1221 e_Buffer_Write(@NetOut, PType);
1222 e_Buffer_Write(@NetOut, PID);
1223 e_Buffer_Write(@NetOut, Byte(TP.Enabled));
1224 e_Buffer_Write(@NetOut, TP.LiftType);
1226 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1227 end;
1229 // TRIGGER
1231 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
1232 begin
1233 if gTriggers = nil then Exit;
1234 if T.Sound = nil then Exit;
1236 e_Buffer_Write(@NetOut, Byte(NET_MSG_TSOUND));
1237 e_Buffer_Write(@NetOut, T.ClientID);
1238 e_Buffer_Write(@NetOut, Byte(T.Sound.IsPlaying));
1239 e_Buffer_Write(@NetOut, LongWord(T.Sound.GetPosition));
1240 e_Buffer_Write(@NetOut, T.SoundPlayCount);
1242 g_Net_Host_Send(ID, True);
1243 end;
1245 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
1246 begin
1247 e_Buffer_Write(@NetOut, Byte(NET_MSG_TMUSIC));
1248 e_Buffer_Write(@NetOut, gMusic.Name);
1249 e_Buffer_Write(@NetOut, Byte(gMusic.IsPlaying));
1250 e_Buffer_Write(@NetOut, LongWord(gMusic.GetPosition));
1251 e_Buffer_Write(@NetOut, Byte(gMusic.SpecPause or gMusic.IsPaused));
1253 g_Net_Host_Send(ID, True);
1254 end;
1256 // MONSTER
1258 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
1259 var
1260 M: TMonster;
1261 begin
1262 M := g_Monsters_Get(UID);
1263 if M = nil then
1264 Exit;
1266 with M do
1267 begin
1268 e_Buffer_Write(@NetOut, Byte(NET_MSG_MSPAWN));
1269 e_Buffer_Write(@NetOut, UID);
1270 e_Buffer_Write(@NetOut, MonsterType);
1271 e_Buffer_Write(@NetOut, MonsterState);
1272 e_Buffer_Write(@NetOut, MonsterAnim);
1273 e_Buffer_Write(@NetOut, MonsterTargetUID);
1274 e_Buffer_Write(@NetOut, MonsterTargetTime);
1275 e_Buffer_Write(@NetOut, MonsterBehaviour);
1276 e_Buffer_Write(@NetOut, MonsterSleep);
1277 e_Buffer_Write(@NetOut, MonsterHealth);
1278 e_Buffer_Write(@NetOut, MonsterAmmo);
1279 e_Buffer_Write(@NetOut, GameX);
1280 e_Buffer_Write(@NetOut, GameY);
1281 e_Buffer_Write(@NetOut, GameVelX);
1282 e_Buffer_Write(@NetOut, GameVelY);
1283 e_Buffer_Write(@NetOut, Byte(GameDirection));
1284 end;
1286 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1287 end;
1289 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
1290 var
1291 M: TMonster;
1292 begin
1293 M := g_Monsters_Get(UID);
1294 if M = nil then Exit;
1296 e_Buffer_Write(@NetOut, Byte(NET_MSG_MPOS));
1297 e_Buffer_Write(@NetOut, UID);
1299 with M do
1300 begin
1301 e_Buffer_Write(@NetOut, GameX);
1302 e_Buffer_Write(@NetOut, GameY);
1303 e_Buffer_Write(@NetOut, GameVelX);
1304 e_Buffer_Write(@NetOut, GameVelY);
1305 e_Buffer_Write(@NetOut, Byte(GameDirection));
1306 end;
1308 g_Net_Host_Send(ID, False, NET_CHAN_MONSTERPOS);
1309 end;
1311 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
1312 var
1313 M: TMonster;
1314 begin
1315 M := g_Monsters_Get(UID);
1316 if M = nil then Exit;
1318 e_Buffer_Write(@NetOut, Byte(NET_MSG_MSTATE));
1319 e_Buffer_Write(@NetOut, UID);
1321 with M do
1322 begin
1323 e_Buffer_Write(@NetOut, MonsterState);
1324 e_Buffer_Write(@NetOut, ForcedAnim);
1325 e_Buffer_Write(@NetOut, MonsterTargetUID);
1326 e_Buffer_Write(@NetOut, MonsterTargetTime);
1327 e_Buffer_Write(@NetOut, MonsterSleep);
1328 e_Buffer_Write(@NetOut, MonsterHealth);
1329 e_Buffer_Write(@NetOut, MonsterAmmo);
1330 e_Buffer_Write(@NetOut, MonsterPain);
1331 e_Buffer_Write(@NetOut, Byte(AnimIsReverse));
1332 end;
1334 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1335 end;
1337 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
1338 begin
1339 e_Buffer_Write(@NetOut, Byte(NET_MSG_MSHOT));
1340 e_Buffer_Write(@NetOut, UID);
1341 e_Buffer_Write(@NetOut, X);
1342 e_Buffer_Write(@NetOut, Y);
1343 e_Buffer_Write(@NetOut, VX);
1344 e_Buffer_Write(@NetOut, VY);
1346 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1347 end;
1349 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
1350 var
1351 M: TMonster;
1352 begin
1353 M := g_Monsters_Get(UID);
1354 if M = nil then Exit;
1356 e_Buffer_Write(@NetOut, Byte(NET_MSG_MDEL));
1357 e_Buffer_Write(@NetOut, UID);
1359 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1360 end;
1362 // MISC
1364 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
1365 begin
1366 e_Buffer_Write(@NetOut, Byte(NET_MSG_TIME_SYNC));
1367 e_Buffer_Write(@NetOut, Time);
1369 g_Net_Host_Send(ID, False, NET_CHAN_SERVICE);
1370 end;
1372 procedure MH_SEND_VoteEvent(EvType: Byte;
1373 StrArg1: string = 'a'; StrArg2: string = 'b';
1374 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
1375 ID: Integer = NET_EVERYONE);
1376 begin
1377 e_Buffer_Write(@NetOut, Byte(NET_MSG_VOTE_EVENT));
1378 e_Buffer_Write(@NetOut, EvType);
1379 e_Buffer_Write(@NetOut, IntArg1);
1380 e_Buffer_Write(@NetOut, IntArg2);
1381 e_Buffer_Write(@NetOut, StrArg1);
1382 e_Buffer_Write(@NetOut, StrArg2);
1384 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1385 end;
1387 // CLIENT MESSAGES //
1389 // GAME
1391 procedure MC_RECV_Chat(P: Pointer);
1392 var
1393 Txt: string;
1394 Mode: Byte;
1395 begin
1396 Txt := e_Raw_Read_String(P);
1397 Mode := e_Raw_Read_Byte(P);
1399 if Mode <> NET_CHAT_SYSTEM then
1400 begin
1401 if Mode = NET_CHAT_PLAYER then
1402 begin
1403 g_Console_Add(Txt, True);
1404 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
1405 g_Sound_PlayEx('SOUND_GAME_RADIO');
1406 end else
1407 if (Mode = NET_CHAT_TEAM) and (gPlayer1 <> nil) then
1408 begin
1409 if gPlayer1.Team = TEAM_RED then
1410 g_Console_Add(b_Text_Format('\r[Team] ') + Txt, True);
1411 if gPlayer1.Team = TEAM_BLUE then
1412 g_Console_Add(b_Text_Format('\b[Team] ') + Txt, True);
1413 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
1414 g_Sound_PlayEx('SOUND_GAME_RADIO');
1415 end;
1416 end else
1417 g_Console_Add(Txt, True);
1418 end;
1420 procedure MC_RECV_Effect(P: Pointer);
1421 var
1422 Kind: Byte;
1423 X, Y: Integer;
1424 Ang: SmallInt;
1425 Anim: TAnimation;
1426 ID: LongWord;
1427 begin
1428 if not gGameOn then Exit;
1429 Kind := e_Raw_Read_Byte(P);
1430 X := e_Raw_Read_LongInt(P);
1431 Y := e_Raw_Read_LongInt(P);
1432 Ang := e_Raw_Read_SmallInt(P);
1434 case Kind of
1435 NET_GFX_SPARK:
1436 g_GFX_Spark(X, Y, 2 + Random(2), Ang, 0, 0);
1438 NET_GFX_TELE:
1439 begin
1440 if g_Frames_Get(ID, 'FRAMES_TELEPORT') then
1441 begin
1442 Anim := TAnimation.Create(ID, False, 3);
1443 g_GFX_OnceAnim(X, Y, Anim);
1444 Anim.Free();
1445 end;
1446 if Ang = 1 then
1447 g_Sound_PlayExAt('SOUND_GAME_TELEPORT', X, Y);
1448 end;
1450 NET_GFX_EXPLODE:
1451 begin
1452 if g_Frames_Get(ID, 'FRAMES_EXPLODE_ROCKET') then
1453 begin
1454 Anim := TAnimation.Create(ID, False, 6);
1455 Anim.Blending := False;
1456 g_GFX_OnceAnim(X-64, Y-64, Anim);
1457 Anim.Free();
1458 end;
1459 if Ang = 1 then
1460 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEROCKET', X, Y);
1461 end;
1463 NET_GFX_BFGEXPL:
1464 begin
1465 if g_Frames_Get(ID, 'FRAMES_EXPLODE_BFG') then
1466 begin
1467 Anim := TAnimation.Create(ID, False, 6);
1468 Anim.Blending := False;
1469 g_GFX_OnceAnim(X-64, Y-64, Anim);
1470 Anim.Free();
1471 end;
1472 if Ang = 1 then
1473 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEBFG', X, Y);
1474 end;
1476 NET_GFX_BFGHIT:
1477 begin
1478 if g_Frames_Get(ID, 'FRAMES_BFGHIT') then
1479 begin
1480 Anim := TAnimation.Create(ID, False, 4);
1481 g_GFX_OnceAnim(X-32, Y-32, Anim);
1482 Anim.Free();
1483 end;
1484 end;
1486 NET_GFX_FIRE:
1487 begin
1488 if g_Frames_Get(ID, 'FRAMES_FIRE') then
1489 begin
1490 Anim := TAnimation.Create(ID, False, 4);
1491 g_GFX_OnceAnim(X, Y, Anim);
1492 Anim.Free();
1493 end;
1494 if Ang = 1 then
1495 g_Sound_PlayExAt('SOUND_FIRE', X, Y);
1496 end;
1498 NET_GFX_RESPAWN:
1499 begin
1500 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
1501 begin
1502 Anim := TAnimation.Create(ID, False, 4);
1503 g_GFX_OnceAnim(X, Y, Anim);
1504 Anim.Free();
1505 end;
1506 if Ang = 1 then
1507 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
1508 end;
1510 NET_GFX_SHELL1:
1511 g_Player_CreateShell(X, Y, 0, -2, SHELL_BULLET);
1513 NET_GFX_SHELL2:
1514 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1516 NET_GFX_SHELL3:
1517 begin
1518 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1519 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1520 end;
1521 end;
1522 end;
1524 procedure MC_RECV_Sound(P: Pointer);
1525 var
1526 Name: string;
1527 X, Y: Integer;
1528 begin
1529 Name := e_Raw_Read_String(P);
1530 X := e_Raw_Read_LongInt(P);
1531 Y := e_Raw_Read_LongInt(P);
1532 g_Sound_PlayExAt(Name, X, Y);
1533 end;
1535 procedure MC_RECV_CreateShot(P: Pointer);
1536 var
1537 I, X, Y, XV, YV: Integer;
1538 Timeout: LongWord;
1539 Target, Spawner: Word;
1540 ShType: Byte;
1541 begin
1542 I := e_Raw_Read_LongInt(P);
1543 ShType := e_Raw_Read_Byte(P);
1544 Target := e_Raw_Read_Word(P);
1545 Spawner := e_Raw_Read_Word(P);
1546 Timeout := e_Raw_Read_LongWord(P);
1547 X := e_Raw_Read_LongInt(P);
1548 Y := e_Raw_Read_LongInt(P);
1549 XV := e_Raw_Read_LongInt(P);
1550 YV := e_Raw_Read_LongInt(P);
1552 I := g_Weapon_CreateShot(I, ShType, Spawner, Target, X, Y, XV, YV);
1553 if (Shots <> nil) and (I <= High(Shots)) then
1554 begin
1555 Shots[I].Timeout := Timeout;
1556 //Shots[I].Target := Target; // TODO: find a use for Target later
1557 end;
1558 end;
1560 procedure MC_RECV_UpdateShot(P: Pointer);
1561 var
1562 I, TX, TY, TXV, TYV: Integer;
1563 begin
1564 I := e_Raw_Read_LongInt(P);
1565 TX := e_Raw_Read_LongInt(P);
1566 TY := e_Raw_Read_LongInt(P);
1567 TXV := e_Raw_Read_LongInt(P);
1568 TYV := e_Raw_Read_LongInt(P);
1570 if (Shots <> nil) and (I <= High(Shots)) then
1571 with (Shots[i]) do
1572 begin
1573 Obj.X := TX;
1574 Obj.Y := TY;
1575 Obj.Vel.X := TXV;
1576 Obj.Vel.Y := TYV;
1577 end;
1578 end;
1580 procedure MC_RECV_DeleteShot(P: Pointer);
1581 var
1582 I, X, Y: Integer;
1583 L: Boolean;
1584 begin
1585 if not gGameOn then Exit;
1586 I := e_Raw_Read_LongInt(P);
1587 L := (e_Raw_Read_Byte(P) <> 0);
1588 X := e_Raw_Read_LongInt(P);
1589 Y := e_Raw_Read_LongInt(P);
1591 g_Weapon_DestroyShot(I, X, Y, L);
1592 end;
1594 procedure MC_RECV_GameStats(P: Pointer);
1595 begin
1596 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
1597 begin
1598 gTeamStat[TEAM_RED].Goals := e_Raw_Read_SmallInt(P);
1599 gTeamStat[TEAM_BLUE].Goals := e_Raw_Read_SmallInt(P);
1600 end
1601 else
1602 if gGameSettings.GameMode = GM_COOP then
1603 begin
1604 gCoopMonstersKilled := e_Raw_Read_Word(P);
1605 gCoopSecretsFound := e_Raw_Read_Word(P);
1606 end;
1607 end;
1609 procedure MC_RECV_CoopStats(P: Pointer);
1610 begin
1611 gTotalMonsters := e_Raw_Read_LongInt(P);
1612 gSecretsCount := e_Raw_Read_LongInt(P);
1613 gCoopTotalMonstersKilled := e_Raw_Read_Word(P);
1614 gCoopTotalSecretsFound := e_Raw_Read_Word(P);
1615 gCoopTotalMonsters := e_Raw_Read_Word(P);
1616 gCoopTotalSecrets := e_Raw_Read_Word(P);
1617 end;
1619 procedure MC_RECV_GameEvent(P: Pointer);
1620 var
1621 EvType: Byte;
1622 EvNum: Integer;
1623 EvStr: string;
1624 EvTime: LongWord;
1625 BHash: Boolean;
1626 EvHash: TMD5Digest;
1627 pl: TPlayer;
1628 i1, i2: TStrings_Locale;
1629 pln: String;
1630 cnt: Byte;
1631 begin
1632 FillChar(EvHash, Sizeof(EvHash), 0);
1633 EvType := e_Raw_Read_Byte(P);
1634 EvNum := e_Raw_Read_LongInt(P);
1635 EvStr := e_Raw_Read_String(P);
1636 gLastMap := e_Raw_Read_Byte(P) <> 0;
1637 if gLastMap and (gGameSettings.GameMode = GM_COOP) then gStatsOff := True;
1638 gStatsPressed := True;
1639 EvTime := e_Raw_Read_LongWord(P);
1640 BHash := e_Raw_Read_Byte(P) <> 0;
1641 if BHash then
1642 EvHash := e_Raw_Read_MD5(P);
1644 gTime := EvTime;
1646 case EvType of
1647 NET_EV_MAPSTART:
1648 begin
1649 gGameOn := False;
1650 g_Game_ClearLoading();
1651 g_Game_StopAllSounds(True);
1653 gSwitchGameMode := Byte(EvNum);
1654 gGameSettings.GameMode := gSwitchGameMode;
1656 gWADHash := EvHash;
1657 if not g_Game_StartMap(EvStr, True) then
1658 begin
1659 if Pos(':\', EvStr) = 0 then
1660 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [gGameSettings.WAD + ':\' + EvStr]))
1661 else
1662 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [EvStr]));
1663 Exit;
1664 end;
1666 MC_SEND_FullStateRequest;
1667 end;
1669 NET_EV_MAPEND:
1670 begin
1671 gMissionFailed := EvNum <> 0;
1672 gExit := EXIT_ENDLEVELCUSTOM;
1673 end;
1675 NET_EV_RCON:
1676 begin
1677 case EvNum of
1678 NET_RCON_NOAUTH:
1679 g_Console_Add(_lc[I_NET_RCON_NOAUTH], True);
1680 NET_RCON_PWGOOD:
1681 g_Console_Add(_lc[I_NET_RCON_PWD_VALID], True);
1682 NET_RCON_PWBAD:
1683 g_Console_Add(_lc[I_NET_RCON_PWD_INVALID], True);
1684 end;
1685 end;
1687 NET_EV_CHANGE_TEAM:
1688 begin
1689 if EvNum = TEAM_RED then
1690 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [EvStr]), True);
1691 if EvNum = TEAM_BLUE then
1692 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [EvStr]), True);
1693 end;
1695 NET_EV_PLAYER_KICK:
1696 g_Console_Add(Format(_lc[I_PLAYER_KICK], [EvStr]), True);
1698 NET_EV_PLAYER_BAN:
1699 g_Console_Add(Format(_lc[I_PLAYER_BAN], [EvStr]), True);
1701 NET_EV_LMS_WARMUP:
1702 g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [EvNum]), True);
1704 NET_EV_LMS_SURVIVOR:
1705 g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True);
1707 NET_EV_BIGTEXT:
1708 g_Game_Message(AnsiUpperCase(EvStr), Word(EvNum));
1710 NET_EV_SCORE:
1711 begin
1712 pl := g_Player_Get(EvNum and $FFFF);
1713 if pl = nil then
1714 pln := '?'
1715 else
1716 pln := pl.Name;
1717 cnt := (EvNum shr 16) and $FF;
1718 if Pos('w', EvStr) = 0 then
1719 begin
1720 // Default score
1721 if Pos('t', EvStr) = 0 then
1722 begin
1723 // Player +/- score
1724 if Pos('-', EvStr) = 0 then
1725 begin
1726 if Pos('e', EvStr) = 0 then
1727 i1 := I_PLAYER_SCORE_ADD_OWN
1728 else
1729 i1 := I_PLAYER_SCORE_ADD_ENEMY;
1730 end else
1731 begin
1732 if Pos('e', EvStr) = 0 then
1733 i1 := I_PLAYER_SCORE_SUB_OWN
1734 else
1735 i1 := I_PLAYER_SCORE_SUB_ENEMY;
1736 end;
1737 // Which team
1738 if Pos('r', EvStr) > 0 then
1739 i2 := I_PLAYER_SCORE_TO_RED
1740 else
1741 i2 := I_PLAYER_SCORE_TO_BLUE;
1742 g_Console_Add(Format(_lc[i1], [pln, cnt, _lc[i2]]), True);
1743 end else
1744 begin
1745 // Team +/- score
1746 if Pos('-', EvStr) = 0 then
1747 i1 := I_PLAYER_SCORE_ADD_TEAM
1748 else
1749 i1 := I_PLAYER_SCORE_SUB_TEAM;
1750 // Which team
1751 if Pos('r', EvStr) > 0 then
1752 i2 := I_PLAYER_SCORE_RED
1753 else
1754 i2 := I_PLAYER_SCORE_BLUE;
1755 g_Console_Add(Format(_lc[i1], [_lc[i2], cnt]), True);
1756 end;
1757 end else
1758 begin
1759 // Game Win
1760 if Pos('e', EvStr) = 0 then
1761 i1 := I_PLAYER_SCORE_WIN_OWN
1762 else
1763 i1 := I_PLAYER_SCORE_WIN_ENEMY;
1764 // Which team
1765 if Pos('r', EvStr) > 0 then
1766 i2 := I_PLAYER_SCORE_TO_RED
1767 else
1768 i2 := I_PLAYER_SCORE_TO_BLUE;
1769 g_Console_Add(Format(_lc[i1], [pln, _lc[i2]]), True);
1770 end;
1771 end;
1773 NET_EV_SCORE_MSG:
1774 begin
1775 if EvNum = TEAM_RED then
1776 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1777 if EvNum = TEAM_BLUE then
1778 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1779 if EvNum = -TEAM_RED then
1780 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1781 if EvNum = -TEAM_BLUE then
1782 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1783 end;
1785 NET_EV_LMS_START:
1786 begin
1787 g_Player_RemoveAllCorpses;
1788 g_Game_Message(_lc[I_MESSAGE_LMS_START], 144);
1789 end;
1791 NET_EV_LMS_WIN:
1792 g_Game_Message(Format(_lc[I_MESSAGE_LMS_WIN], [AnsiUpperCase(EvStr)]), 144);
1794 NET_EV_TLMS_WIN:
1795 begin
1796 if EvNum = TEAM_RED then
1797 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 144);
1798 if EvNum = TEAM_BLUE then
1799 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 144);
1800 end;
1802 NET_EV_LMS_LOSE:
1803 g_Game_Message(_lc[I_MESSAGE_LMS_LOSE], 144);
1805 NET_EV_LMS_DRAW:
1806 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
1808 NET_EV_KILLCOMBO:
1809 g_Game_Announce_KillCombo(EvNum);
1811 NET_EV_PLAYER_TOUCH:
1812 begin
1813 pl := g_Player_Get(EvNum);
1814 if pl <> nil then
1815 pl.Touch();
1816 end;
1818 end;
1819 end;
1821 procedure MC_RECV_FlagEvent(P: Pointer);
1822 var
1823 PID: Word;
1824 Pl: TPlayer;
1825 EvType: Byte;
1826 Fl: Byte;
1827 Quiet: Boolean;
1828 s, ts: string;
1829 begin
1830 EvType := e_Raw_Read_Byte(P);
1831 Fl := e_Raw_Read_Byte(P);
1833 if Fl = FLAG_NONE then Exit;
1835 Quiet := (e_Raw_Read_Byte(P) <> 0);
1836 PID := e_Raw_Read_Word(P);
1838 gFlags[Fl].State := e_Raw_Read_Byte(P);
1839 gFlags[Fl].CaptureTime := e_Raw_Read_LongWord(P);
1840 gFlags[Fl].Obj.X := e_Raw_Read_LongInt(P);
1841 gFlags[Fl].Obj.Y := e_Raw_Read_LongInt(P);
1842 gFlags[Fl].Obj.Vel.X := e_Raw_Read_LongInt(P);
1843 gFlags[Fl].Obj.Vel.Y := e_Raw_Read_LongInt(P);
1845 Pl := g_Player_Get(PID);
1846 if (Pl = nil) and
1847 (EvType <> FLAG_STATE_NORMAL) and
1848 (EvType <> FLAG_STATE_DROPPED) and
1849 (EvType <> FLAG_STATE_RETURNED) then
1850 Exit;
1852 case EvType of
1853 FLAG_STATE_NORMAL:
1854 begin
1855 if Quiet or (Pl = nil) then Exit;
1857 if Fl = FLAG_RED then
1858 s := _lc[I_PLAYER_FLAG_RED]
1859 else
1860 s := _lc[I_PLAYER_FLAG_BLUE];
1862 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1863 end;
1865 FLAG_STATE_CAPTURED:
1866 begin
1867 if (Pl <> nil) then Pl.SetFlag(Fl);
1869 if Quiet then Exit;
1871 if Fl = FLAG_RED then
1872 s := _lc[I_PLAYER_FLAG_RED]
1873 else
1874 s := _lc[I_PLAYER_FLAG_BLUE];
1876 g_Console_Add(Format(_lc[I_PLAYER_FLAG_GET], [Pl.Name, s]), True);
1877 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_GET], [AnsiUpperCase(s)]), 144);
1878 end;
1880 FLAG_STATE_DROPPED:
1881 begin
1882 if (Pl <> nil) then Pl.SetFlag(FLAG_NONE);
1884 if Quiet or (Pl = nil) then Exit;
1886 if Fl = FLAG_RED then
1887 s := _lc[I_PLAYER_FLAG_RED]
1888 else
1889 s := _lc[I_PLAYER_FLAG_BLUE];
1891 g_Console_Add(Format(_lc[I_PLAYER_FLAG_DROP], [Pl.Name, s]), True);
1892 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_DROP], [AnsiUpperCase(s)]), 144);
1893 end;
1895 FLAG_STATE_SCORED:
1896 begin
1897 g_Map_ResetFlag(FLAG_RED);
1898 g_Map_ResetFlag(FLAG_BLUE);
1899 if Quiet or (Pl = nil) then Exit;
1900 Pl.SetFlag(FLAG_NONE);
1902 if Fl = FLAG_RED then
1903 s := _lc[I_PLAYER_FLAG_RED]
1904 else
1905 s := _lc[I_PLAYER_FLAG_BLUE];
1907 ts := Format('%.4d', [gFlags[Fl].CaptureTime]);
1908 Insert('.', ts, Length(ts) + 1 - 3);
1909 g_Console_Add(Format(_lc[I_PLAYER_FLAG_CAPTURE], [Pl.Name, s, ts]), True);
1910 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_CAPTURE], [AnsiUpperCase(s)]), 144);
1911 end;
1913 FLAG_STATE_RETURNED:
1914 begin
1915 g_Map_ResetFlag(Fl);
1916 if Quiet then Exit;
1918 if Fl = FLAG_RED then
1919 s := _lc[I_PLAYER_FLAG_RED]
1920 else
1921 s := _lc[I_PLAYER_FLAG_BLUE];
1923 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1924 end;
1925 end;
1926 end;
1928 procedure MC_RECV_GameSettings(P: Pointer);
1929 begin
1930 gGameSettings.GameMode := e_Raw_Read_Byte(P);
1931 gGameSettings.GoalLimit := e_Raw_Read_Word(P);
1932 gGameSettings.TimeLimit := e_Raw_Read_Word(P);
1933 gGameSettings.MaxLives := e_Raw_Read_Byte(P);
1934 gGameSettings.Options := e_Raw_Read_LongWord(P);
1935 end;
1937 // PLAYER
1939 function MC_RECV_PlayerCreate(P: Pointer): Word;
1940 var
1941 PID, DID: Word;
1942 PName, Model: string;
1943 Color: TRGB;
1944 T: Byte;
1945 Pl: TPlayer;
1946 begin
1947 PID := e_Raw_Read_Word(P);
1948 Pl := g_Player_Get(PID);
1950 PName := e_Raw_Read_String(P);
1951 Model := e_Raw_Read_String(P);
1952 Color.R := e_Raw_Read_Byte(P);
1953 Color.G := e_Raw_Read_Byte(P);
1954 Color.B := e_Raw_Read_Byte(P);
1955 T := e_Raw_Read_Byte(P);
1957 Result := 0;
1958 if (PID <> NetPlrUID1) and (PID <> NetPlrUID2) then
1959 begin
1960 if (Pl <> nil) then Exit;
1961 DID := g_Player_Create(Model, Color, T, False);
1962 with g_Player_Get(DID) do
1963 begin
1964 UID := PID;
1965 Name := PName;
1966 Reset(True);
1967 end;
1968 end
1969 else
1970 begin
1971 if (PID = NetPlrUID1) and (gPlayer1 <> nil) then begin
1972 gPlayer1.UID := PID;
1973 gPlayer1.Model.SetColor(Color.R, Color.G, Color.B);
1974 gPlayer1.ChangeTeam(T);
1975 end;
1976 if (PID = NetPlrUID2) and (gPlayer2 <> nil) then begin
1977 gPlayer2.UID := PID;
1978 gPlayer2.Model.SetColor(Color.R, Color.G, Color.B);
1979 gPlayer2.ChangeTeam(T);
1980 end;
1981 end;
1983 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
1984 e_WriteLog('NET: Player ' + PName + ' [' + IntToStr(PID) + '] added.', MSG_NOTIFY);
1985 Result := PID;
1986 end;
1988 function MC_RECV_PlayerPos(P: Pointer): Word;
1989 var
1990 GT: LongWord;
1991 PID: Word;
1992 kByte: Word;
1993 Pl: TPlayer;
1994 Dir: Byte;
1995 TmpX, TmpY: Integer;
1996 begin
1997 Result := 0;
1999 GT := e_Raw_Read_LongWord(P);
2000 if GT < gTime - NET_MAX_DIFFTIME then
2001 begin
2002 gTime := GT;
2003 Exit;
2004 end;
2005 gTime := GT;
2007 PID := e_Raw_Read_Word(P);
2008 Pl := g_Player_Get(PID);
2010 if Pl = nil then Exit;
2012 Result := PID;
2014 with Pl do
2015 begin
2016 FPing := e_Raw_Read_Word(P);
2017 FLoss := e_Raw_Read_Byte(P);
2018 kByte := e_Raw_Read_Word(P);
2019 Dir := e_Raw_Read_Byte(P);
2021 TmpX := e_Raw_Read_LongInt(P);
2022 TmpY := e_Raw_Read_LongInt(P);
2024 ReleaseKeys;
2026 if (kByte = NET_KEY_CHAT) then
2027 PressKey(KEY_CHAT, 10000)
2028 else
2029 begin
2030 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
2031 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
2032 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
2033 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
2034 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
2035 end;
2037 if ((Pl <> gPlayer1) and (Pl <> gPlayer2)) or LongBool(kByte and NET_KEY_FORCEDIR) then
2038 SetDirection(TDirection(Dir));
2040 GameVelX := e_Raw_Read_LongInt(P);
2041 GameVelY := e_Raw_Read_LongInt(P);
2042 GameAccelX := e_Raw_Read_LongInt(P);
2043 GameAccelY := e_Raw_Read_LongInt(P);
2044 SetLerp(TmpX, TmpY);
2045 if NetForcePlayerUpdate then Update();
2046 end;
2047 end;
2049 function MC_RECV_PlayerStats(P: Pointer): Word;
2050 var
2051 PID: Word;
2052 Pl: TPlayer;
2053 I: Integer;
2054 OldJet: Boolean;
2055 NewTeam: Byte;
2056 begin
2057 PID := e_Raw_Read_Word(P);
2058 Pl := g_Player_Get(PID);
2059 Result := 0;
2060 if Pl = nil then
2061 Exit;
2063 with Pl do
2064 begin
2065 Live := (e_Raw_Read_Byte(P) <> 0);
2066 GodMode := (e_Raw_Read_Byte(P) <> 0);
2067 Health := e_Raw_Read_LongInt(P);
2068 Armor := e_Raw_Read_LongInt(P);
2069 Air := e_Raw_Read_LongInt(P);
2070 JetFuel := e_Raw_Read_LongInt(P);
2071 Lives := e_Raw_Read_Byte(P);
2072 NewTeam := e_Raw_Read_Byte(P);
2074 for I := WEAPON_KASTET to WEAPON_SUPERPULEMET do
2075 FWeapon[I] := (e_Raw_Read_Byte(P) <> 0);
2077 for I := A_BULLETS to A_CELLS do
2078 FAmmo[I] := e_Raw_Read_Word(P);
2080 for I := A_BULLETS to A_CELLS do
2081 FMaxAmmo[I] := e_Raw_Read_Word(P);
2083 for I := MR_SUIT to MR_MAX do
2084 FMegaRulez[I] := e_Raw_Read_LongWord(P);
2086 FRulez := [];
2087 if (e_Raw_Read_Byte(P) <> 0) then
2088 FRulez := FRulez + [R_ITEM_BACKPACK];
2089 if (e_Raw_Read_Byte(P) <> 0) then
2090 FRulez := FRulez + [R_KEY_RED];
2091 if (e_Raw_Read_Byte(P) <> 0) then
2092 FRulez := FRulez + [R_KEY_GREEN];
2093 if (e_Raw_Read_Byte(P) <> 0) then
2094 FRulez := FRulez + [R_KEY_BLUE];
2095 if (e_Raw_Read_Byte(P) <> 0) then
2096 FRulez := FRulez + [R_BERSERK];
2098 Frags := e_Raw_Read_LongInt(P);
2099 Death := e_Raw_Read_LongInt(P);
2101 SetWeapon(e_Raw_Read_Byte(P));
2103 FSpectator := e_Raw_Read_Byte(P) <> 0;
2104 if FSpectator then
2105 begin
2106 if Pl = gPlayer1 then
2107 begin
2108 gLMSPID1 := UID;
2109 gPlayer1 := nil;
2110 end;
2111 if Pl = gPlayer2 then
2112 begin
2113 gLMSPID2 := UID;
2114 gPlayer2 := nil;
2115 end;
2116 end
2117 else
2118 begin
2119 if (gPlayer1 = nil) and (gLMSPID1 > 0) then
2120 gPlayer1 := g_Player_Get(gLMSPID1);
2121 if (gPlayer2 = nil) and (gLMSPID2 > 0) then
2122 gPlayer2 := g_Player_Get(gLMSPID2);
2123 end;
2124 FGhost := e_Raw_Read_Byte(P) <> 0;
2125 FPhysics := e_Raw_Read_Byte(P) <> 0;
2126 FNoRespawn := e_Raw_Read_Byte(P) <> 0;
2127 OldJet := FJetpack;
2128 FJetpack := e_Raw_Read_Byte(P) <> 0;
2129 if OldJet and not FJetpack then
2130 JetpackOff
2131 else if not OldJet and FJetpack then
2132 JetpackOn;
2133 if Team <> NewTeam then
2134 Pl.ChangeTeam(NewTeam);
2135 end;
2137 Result := PID;
2138 end;
2140 function MC_RECV_PlayerDamage(P: Pointer): Word;
2141 var
2142 PID: Word;
2143 Pl: TPlayer;
2144 Kind: Byte;
2145 Attacker, Value: Word;
2146 VX, VY: Integer;
2147 begin
2148 Result := 0;
2149 if not gGameOn then Exit;
2150 PID := e_Raw_Read_Word(P);
2151 Pl := g_Player_Get(PID);
2152 if Pl = nil then Exit;
2154 Kind := e_Raw_Read_Byte(P);
2155 Attacker := e_Raw_Read_Word(P);
2156 Value := e_Raw_Read_Word(P);
2157 VX := e_Raw_Read_Word(P);
2158 VY := e_Raw_Read_Word(P);
2160 with Pl do
2161 Damage(Value, Attacker, VX, VY, Kind);
2163 Result := PID;
2164 end;
2166 function MC_RECV_PlayerDeath(P: Pointer): Word;
2167 var
2168 PID: Word;
2169 Pl: TPlayer;
2170 KillType, DeathType: Byte;
2171 Attacker: Word;
2172 begin
2173 Result := 0;
2174 if not gGameOn then Exit;
2175 PID := e_Raw_Read_Word(P);
2176 Pl := g_Player_Get(PID);
2177 if Pl = nil then Exit;
2179 KillType := e_Raw_Read_Byte(P);
2180 DeathType := e_Raw_Read_Byte(P);
2181 Attacker := e_Raw_Read_Word(P);
2183 with Pl do
2184 begin
2185 Kill(KillType, Attacker, DeathType);
2186 SoftReset;
2187 end;
2188 end;
2190 function MC_RECV_PlayerDelete(P: Pointer): Word;
2191 var
2192 PID: Word;
2193 Pl: TPlayer;
2194 begin
2195 PID := e_Raw_Read_Word(P);
2196 Pl := g_Player_Get(PID);
2197 Result := 0;
2198 if Pl = nil then Exit;
2200 g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [Pl.Name]), True);
2201 e_WriteLog('NET: Player ' + Pl.Name + ' [' + IntToStr(PID) + '] removed.', MSG_NOTIFY);
2203 g_Player_Remove(PID);
2205 Result := PID;
2206 end;
2208 function MC_RECV_PlayerFire(P: Pointer): Word;
2209 var
2210 PID: Word;
2211 Weap: Byte;
2212 Pl: TPlayer;
2213 X, Y, AX, AY: Integer;
2214 SHID: Integer;
2215 begin
2216 Result := 0;
2217 if not gGameOn then Exit;
2218 PID := e_Raw_Read_Word(P);
2219 Pl := g_Player_Get(PID);
2220 if Pl = nil then Exit;
2222 Weap := e_Raw_Read_Byte(P);
2223 X := e_Raw_Read_LongInt(P);
2224 Y := e_Raw_Read_LongInt(P);
2225 AX := e_Raw_Read_LongInt(P);
2226 AY := e_Raw_Read_LongInt(P);
2227 SHID := e_Raw_Read_LongInt(P);
2229 with Pl do
2230 if Live then NetFire(Weap, X, Y, AX, AY, SHID);
2231 end;
2233 procedure MC_RECV_PlayerSettings(P: Pointer);
2234 var
2235 TmpName: string;
2236 TmpModel: string;
2237 TmpColor: TRGB;
2238 TmpTeam: Byte;
2239 Pl: TPlayer;
2240 PID: Word;
2241 begin
2242 PID := e_Raw_Read_Word(P);
2243 Pl := g_Player_Get(PID);
2244 if Pl = nil then Exit;
2246 TmpName := e_Raw_Read_String(P);
2247 TmpModel := e_Raw_Read_String(P);
2248 TmpColor.R := e_Raw_Read_Byte(P);
2249 TmpColor.G := e_Raw_Read_Byte(P);
2250 TmpColor.B := e_Raw_Read_Byte(P);
2251 TmpTeam := e_Raw_Read_Byte(P);
2253 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
2254 begin
2255 Pl.ChangeTeam(TmpTeam);
2256 if gPlayer1 = Pl then
2257 gPlayer1Settings.Team := TmpTeam;
2258 if gPlayer2 = Pl then
2259 gPlayer2Settings.Team := TmpTeam;
2260 end else
2261 Pl.SetColor(TmpColor);
2263 if Pl.Name <> TmpName then
2264 begin
2265 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
2266 Pl.Name := TmpName;
2267 end;
2269 if TmpModel <> Pl.Model.Name then
2270 Pl.SetModel(TmpModel);
2271 end;
2273 // ITEM
2275 procedure MC_RECV_ItemSpawn(P: Pointer);
2276 var
2277 ID: Word;
2278 AID: DWord;
2279 X, Y, VX, VY: Integer;
2280 T: Byte;
2281 Quiet, Fall{, Resp}: Boolean;
2282 Anim: TAnimation;
2283 begin
2284 if not gGameOn then Exit;
2285 ID := e_Raw_Read_Word(P);
2286 Quiet := e_Raw_Read_Byte(P) <> 0;
2287 T := e_Raw_Read_Byte(P);
2288 Fall := e_Raw_Read_Byte(P) <> 0;
2289 {Resp :=} e_Raw_Read_Byte(P);
2290 X := e_Raw_Read_LongInt(P);
2291 Y := e_Raw_Read_LongInt(P);
2292 VX := e_Raw_Read_LongInt(P);
2293 VY := e_Raw_Read_LongInt(P);
2295 g_Items_Create(X, Y, T, Fall, False, False, ID);
2296 gItems[ID].Obj.Vel.X := VX;
2297 gItems[ID].Obj.Vel.Y := VY;
2299 if not Quiet then
2300 begin
2301 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
2302 if g_Frames_Get(AID, 'FRAMES_ITEM_RESPAWN') then
2303 begin
2304 Anim := TAnimation.Create(AID, False, 4);
2305 g_GFX_OnceAnim(X+(gItems[ID].Obj.Rect.Width div 2)-16, Y+(gItems[ID].Obj.Rect.Height div 2)-16, Anim);
2306 Anim.Free();
2307 end;
2308 end;
2309 end;
2311 procedure MC_RECV_ItemDestroy(P: Pointer);
2312 var
2313 ID: Word;
2314 Quiet: Boolean;
2315 begin
2316 if not gGameOn then Exit;
2317 ID := e_Raw_Read_Word(P);
2318 Quiet := e_Raw_Read_Byte(P) <> 0;
2319 if gItems = nil then Exit;
2320 if (ID > High(gItems)) then Exit;
2322 if not Quiet then
2323 if gSoundEffectsDF then
2324 begin
2325 if gItems[ID].ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
2326 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
2327 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ',
2328 gItems[ID].Obj.X, gItems[ID].Obj.Y)
2329 else
2330 if gItems[ID].ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
2331 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
2332 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_AMMO_BACKPACK] then
2333 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON',
2334 gItems[ID].Obj.X, gItems[ID].Obj.Y)
2335 else
2336 g_Sound_PlayExAt('SOUND_ITEM_GETITEM',
2337 gItems[ID].Obj.X, gItems[ID].Obj.Y);
2338 end
2339 else
2340 begin
2341 if gItems[ID].ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
2342 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
2343 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ',
2344 gItems[ID].Obj.X, gItems[ID].Obj.Y)
2345 else
2346 if gItems[ID].ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
2347 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
2348 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET] then
2349 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON',
2350 gItems[ID].Obj.X, gItems[ID].Obj.Y)
2351 else
2352 g_Sound_PlayExAt('SOUND_ITEM_GETITEM',
2353 gItems[ID].Obj.X, gItems[ID].Obj.Y);
2354 end;
2356 g_Items_Remove(ID);
2357 end;
2359 // PANEL
2361 procedure MC_RECV_PanelTexture(P: Pointer);
2362 var
2363 TP: TPanel;
2364 PType: Word;
2365 ID: LongWord;
2366 Tex, Fr: Integer;
2367 Loop, Cnt: Byte;
2368 begin
2369 if not gGameOn then Exit;
2370 PType := e_Raw_Read_Word(P);
2371 ID := e_Raw_Read_LongWord(P);
2372 Tex := e_Raw_Read_LongInt(P);
2373 Fr := e_Raw_Read_LongInt(P);
2374 Cnt := e_Raw_Read_Byte(P);
2375 Loop := e_Raw_Read_Byte(P);
2377 TP := nil;
2379 case PType of
2380 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2381 if gWalls <> nil then
2382 TP := gWalls[ID];
2383 PANEL_FORE:
2384 if gRenderForegrounds <> nil then
2385 TP := gRenderForegrounds[ID];
2386 PANEL_BACK:
2387 if gRenderBackgrounds <> nil then
2388 TP := gRenderBackgrounds[ID];
2389 PANEL_WATER:
2390 if gWater <> nil then
2391 TP := gWater[ID];
2392 PANEL_ACID1:
2393 if gAcid1 <> nil then
2394 TP := gAcid1[ID];
2395 PANEL_ACID2:
2396 if gAcid2 <> nil then
2397 TP := gAcid2[ID];
2398 PANEL_STEP:
2399 if gSteps <> nil then
2400 TP := gSteps[ID];
2401 else
2402 Exit;
2403 end;
2405 if TP <> nil then
2406 if Loop = 0 then
2407 begin // switch texture
2408 TP.SetTexture(Tex, Loop);
2409 TP.SetFrame(Fr, Cnt);
2410 end else // looped or non-looped animation
2411 TP.NextTexture(Loop);
2412 end;
2414 procedure MC_RECV_PanelState(P: Pointer);
2415 var
2416 ID: LongWord;
2417 E: Boolean;
2418 Lift: Byte;
2419 PType: Word;
2420 begin
2421 if not gGameOn then Exit;
2422 PType := e_Raw_Read_Word(P);
2423 ID := e_Raw_Read_LongWord(P);
2424 E := (e_Raw_Read_Byte(P) <> 0);
2425 Lift := e_Raw_Read_Byte(P);
2427 case PType of
2428 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2429 if E then
2430 g_Map_EnableWall(ID)
2431 else
2432 g_Map_DisableWall(ID);
2434 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
2435 g_Map_SetLift(ID, Lift);
2436 end;
2437 end;
2439 // TRIGGERS
2441 procedure MC_RECV_TriggerSound(P: Pointer);
2442 var
2443 SPlaying: Boolean;
2444 SPos, SID: LongWord;
2445 SCount: LongInt;
2446 I: Integer;
2447 begin
2448 if not gGameOn then Exit;
2449 if gTriggers = nil then Exit;
2451 SID := e_Raw_Read_LongWord(P);
2452 SPlaying := e_Raw_Read_Byte(P) <> 0;
2453 SPos := e_Raw_Read_LongWord(P);
2454 SCount := e_Raw_Read_LongInt(P);
2456 for I := Low(gTriggers) to High(gTriggers) do
2457 if gTriggers[I].TriggerType = TRIGGER_SOUND then
2458 if gTriggers[I].ClientID = SID then
2459 with gTriggers[I] do
2460 begin
2461 if SPlaying then
2462 begin
2463 if Data.Local then
2464 Sound.PlayVolumeAt(X+(Width div 2), Y+(Height div 2), Data.Volume/255.0)
2465 else
2466 Sound.PlayPanVolume((Data.Pan-127.0)/128.0, Data.Volume/255.0);
2467 Sound.SetPosition(SPos);
2468 end
2469 else
2470 if Sound.IsPlaying then Sound.Stop;
2472 SoundPlayCount := SCount;
2473 end;
2474 end;
2476 procedure MC_RECV_TriggerMusic(P: Pointer);
2477 var
2478 MName: string;
2479 MPlaying: Boolean;
2480 MPos: LongWord;
2481 MPaused: Boolean;
2482 begin
2483 if not gGameOn then Exit;
2485 MName := e_Raw_Read_String(P);
2486 MPlaying := e_Raw_Read_Byte(P) <> 0;
2487 MPos := e_Raw_Read_LongWord(P);
2488 MPaused := e_Raw_Read_Byte(P) <> 0;
2490 if MPlaying then
2491 begin
2492 gMusic.SetByName(MName);
2493 gMusic.Play(True);
2494 gMusic.SetPosition(MPos);
2495 gMusic.SpecPause := MPaused;
2496 end
2497 else
2498 if gMusic.IsPlaying then gMusic.Stop;
2499 end;
2501 // MONSTERS
2503 procedure MC_RECV_MonsterSpawn(P: Pointer);
2504 var
2505 ID: Word;
2506 MType, MState, MDir, MAnim, MBehav: Byte;
2507 X, Y, VX, VY, MTargTime, MHealth, MAmmo, MSleep: Integer;
2508 MTarg: Word;
2509 M: TMonster;
2510 begin
2511 ID := e_Raw_Read_Word(P);
2512 M := g_Monsters_Get(ID);
2513 if M <> nil then
2514 Exit;
2516 MType := e_Raw_Read_Byte(P);
2517 MState := e_Raw_Read_Byte(P);
2518 MAnim := e_Raw_Read_Byte(P);
2519 MTarg := e_Raw_Read_Word(P);
2520 MTargTime := e_Raw_Read_LongInt(P);
2521 MBehav := e_Raw_Read_Byte(P);
2522 MSleep := e_Raw_Read_LongInt(P);
2523 MHealth := e_Raw_Read_LongInt(P);
2524 MAmmo := e_Raw_Read_LongInt(P);
2526 X := e_Raw_Read_LongInt(P);
2527 Y := e_Raw_Read_LongInt(P);
2528 VX := e_Raw_Read_LongInt(P);
2529 VY := e_Raw_Read_LongInt(P);
2530 MDir := e_Raw_Read_Byte(P);
2532 g_Monsters_Create(MType, X, Y, TDirection(MDir), False, ID);
2533 M := g_Monsters_Get(ID);
2534 if M = nil then
2535 Exit;
2537 with M do
2538 begin
2539 GameX := X;
2540 GameY := Y;
2541 GameVelX := VX;
2542 GameVelY := VY;
2544 MonsterAnim := MAnim;
2545 MonsterTargetUID := MTarg;
2546 MonsterTargetTime := MTargTime;
2547 MonsterBehaviour := MBehav;
2548 MonsterSleep := MSleep;
2549 MonsterAmmo := MAmmo;
2550 SetHealth(MHealth);
2552 SetState(MState);
2553 end;
2554 end;
2556 procedure MC_RECV_MonsterPos(P: Pointer);
2557 var
2558 M: TMonster;
2559 ID: Word;
2560 begin
2561 ID := e_Raw_Read_Word(P);
2562 M := g_Monsters_Get(ID);
2563 if M = nil then
2564 Exit;
2566 with M do
2567 begin
2568 GameX := e_Raw_Read_LongInt(P);
2569 GameY := e_Raw_Read_LongInt(P);
2570 GameVelX := e_Raw_Read_LongInt(P);
2571 GameVelY := e_Raw_Read_LongInt(P);
2572 GameDirection := TDirection(e_Raw_Read_Byte(P));
2573 end;
2574 end;
2576 procedure MC_RECV_MonsterState(P: Pointer);
2577 var
2578 ID: Integer;
2579 MState, MFAnm: Byte;
2580 M: TMonster;
2581 AnimRevert: Boolean;
2582 begin
2583 ID := e_Raw_Read_Word(P);
2584 M := g_Monsters_Get(ID);
2585 if M = nil then Exit;
2587 MState := e_Raw_Read_Byte(P);
2588 MFAnm := e_Raw_Read_Byte(P);
2590 with M do
2591 begin
2592 MonsterTargetUID := e_Raw_Read_Word(P);
2593 MonsterTargetTime := e_Raw_Read_LongInt(P);
2594 MonsterSleep := e_Raw_Read_LongInt(P);
2595 MonsterHealth := e_Raw_Read_LongInt(P);
2596 MonsterAmmo := e_Raw_Read_LongInt(P);
2597 MonsterPain := e_Raw_Read_LongInt(P);
2598 AnimRevert := e_Raw_Read_Byte(P) <> 0;
2599 RevertAnim(AnimRevert);
2601 if MonsterState <> MState then
2602 begin
2603 if (MState = MONSTATE_GO) and (MonsterState = MONSTATE_SLEEP) then
2604 WakeUpSound;
2605 if (MState = MONSTATE_DIE) then
2606 DieSound;
2607 if (MState = MONSTATE_PAIN) then
2608 MakeBloodSimple(Min(200, MonsterPain));
2609 if (MState = MONSTATE_ATTACK) then
2610 kick(nil);
2611 if (MState = MONSTATE_DEAD) then
2612 SetDeadAnim;
2614 SetState(MState, MFAnm);
2615 end;
2616 end;
2617 end;
2619 procedure MC_RECV_MonsterShot(P: Pointer);
2620 var
2621 ID: Integer;
2622 M: TMonster;
2623 X, Y, VX, VY: Integer;
2624 begin
2625 ID := e_Raw_Read_Word(P);
2627 M := g_Monsters_Get(ID);
2628 if M = nil then Exit;
2630 X := e_Raw_Read_LongInt(P);
2631 Y := e_Raw_Read_LongInt(P);
2632 VX := e_Raw_Read_LongInt(P);
2633 VY := e_Raw_Read_LongInt(P);
2635 M.ClientAttack(X, Y, VX, VY);
2636 end;
2638 procedure MC_RECV_MonsterDelete(P: Pointer);
2639 var
2640 ID: Integer;
2641 M: TMonster;
2642 begin
2643 ID := e_Raw_Read_Word(P);
2644 M := g_Monsters_Get(ID);
2645 if M = nil then Exit;
2647 gMonsters[ID].SetState(5);
2648 gMonsters[ID].MonsterRemoved := True;
2649 end;
2651 procedure MC_RECV_TimeSync(P: Pointer);
2652 var
2653 Time: LongWord;
2654 begin
2655 Time := e_Raw_Read_LongWord(P);
2657 if gState = STATE_INTERCUSTOM then
2658 gServInterTime := Min(Time, 255);
2659 end;
2661 procedure MC_RECV_VoteEvent(P: Pointer);
2662 var
2663 EvID: Byte;
2664 Str1, Str2: string;
2665 Int1, Int2: SmallInt;
2666 begin
2667 EvID := e_Raw_Read_Byte(P);
2668 Int1 := e_Raw_Read_SmallInt(P);
2669 Int2 := e_Raw_Read_SmallInt(P);
2670 Str1 := e_Raw_Read_String(P);
2671 Str2 := e_Raw_Read_String(P);
2673 case EvID of
2674 NET_VE_STARTED:
2675 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_STARTED], [Str1, Str2, Int1]), True);
2676 NET_VE_PASSED:
2677 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_PASSED], [Str1]), True);
2678 NET_VE_FAILED:
2679 g_Console_Add(_lc[I_MESSAGE_VOTE_FAILED], True);
2680 NET_VE_VOTE:
2681 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Str1, Int1, Int2]), True);
2682 NET_VE_INPROGRESS:
2683 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_INPROGRESS], [Str1]), True);
2684 end;
2685 end;
2687 // CLIENT SEND
2689 procedure MC_SEND_Info(Password: string);
2690 begin
2691 e_Buffer_Clear(@NetOut);
2693 e_Buffer_Write(@NetOut, Byte(NET_MSG_INFO));
2694 e_Buffer_Write(@NetOut, Byte(0)); // to kill old clients
2695 e_Buffer_Write(@NetOut, Byte(NET_PROTO_VERSION));
2696 e_Buffer_Write(@NetOut, GAME_VERSION);
2697 e_Buffer_Write(@NetOut, Password);
2698 e_Buffer_Write(@NetOut, gPlayer1Settings.Name);
2699 e_Buffer_Write(@NetOut, gPlayer1Settings.Model);
2700 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.R);
2701 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.G);
2702 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.B);
2703 e_Buffer_Write(@NetOut, gPlayer1Settings.Team);
2705 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2706 end;
2708 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
2709 begin
2710 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHAT));
2711 e_Buffer_Write(@NetOut, Txt);
2712 e_Buffer_Write(@NetOut, Mode);
2714 g_Net_Client_Send(True, NET_CHAN_CHAT);
2715 end;
2717 procedure MC_SEND_PlayerPos();
2718 var
2719 kByte: Word;
2720 Predict: Boolean;
2721 begin
2722 if not gGameOn then Exit;
2723 if gPlayers = nil then Exit;
2724 if gPlayer1 = nil then Exit;
2726 kByte := 0;
2727 Predict := NetPredictSelf; // and (not NetGotKeys);
2729 if (not gConsoleShow) and (not gChatShow) and (g_ActiveWindow = nil) then
2730 with gGameControls.P1Control do
2731 begin
2732 if e_KeyPressed(KeyLeft) and (not e_KeyPressed(KeyRight)) then
2733 P1MoveButton := 1
2734 else
2735 if (not e_KeyPressed(KeyLeft)) and e_KeyPressed(KeyRight) then
2736 P1MoveButton := 2
2737 else
2738 if (not e_KeyPressed(KeyLeft)) and (not e_KeyPressed(KeyRight)) then
2739 P1MoveButton := 0;
2741 if (P1MoveButton = 2) and e_KeyPressed(KeyLeft) then
2742 gPlayer1.SetDirection(D_LEFT)
2743 else
2744 if (P1MoveButton = 1) and e_KeyPressed(KeyRight) then
2745 gPlayer1.SetDirection(D_RIGHT)
2746 else
2747 if P1MoveButton <> 0 then
2748 gPlayer1.SetDirection(TDirection(P1MoveButton-1));
2750 gPlayer1.ReleaseKeys;
2751 if P1MoveButton = 1 then
2752 begin
2753 kByte := kByte or NET_KEY_LEFT;
2754 if Predict then gPlayer1.PressKey(KEY_LEFT, 10000);
2755 end;
2756 if P1MoveButton = 2 then
2757 begin
2758 kByte := kByte or NET_KEY_RIGHT;
2759 if Predict then gPlayer1.PressKey(KEY_RIGHT, 10000);
2760 end;
2761 if e_KeyPressed(KeyUp) then
2762 begin
2763 kByte := kByte or NET_KEY_UP;
2764 gPlayer1.PressKey(KEY_UP, 10000);
2765 end;
2766 if e_KeyPressed(KeyDown) then
2767 begin
2768 kByte := kByte or NET_KEY_DOWN;
2769 gPlayer1.PressKey(KEY_DOWN, 10000);
2770 end;
2771 if e_KeyPressed(KeyJump) then
2772 begin
2773 kByte := kByte or NET_KEY_JUMP;
2774 // gPlayer1.PressKey(KEY_JUMP, 10000); // TODO: Make a prediction option
2775 end;
2776 if e_KeyPressed(KeyFire) then kByte := kByte or NET_KEY_FIRE;
2777 if e_KeyPressed(KeyOpen) then kByte := kByte or NET_KEY_OPEN;
2778 if e_KeyPressed(KeyNextWeapon) then kByte := kByte or NET_KEY_NW;
2779 if e_KeyPressed(KeyPrevWeapon) then kByte := kByte or NET_KEY_PW;
2780 end
2781 else
2782 kByte := NET_KEY_CHAT;
2784 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRPOS));
2785 e_Buffer_Write(@NetOut, gTime);
2786 e_Buffer_Write(@NetOut, kByte);
2787 e_Buffer_Write(@NetOut, Byte(gPlayer1.Direction));
2788 g_Net_Client_Send(True, NET_CHAN_PLAYERPOS);
2790 //kBytePrev := kByte;
2791 //kDirPrev := gPlayer1.Direction;
2792 end;
2794 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
2795 begin
2796 e_Buffer_Write(@NetOut, Byte(NET_MSG_VOTE_EVENT));
2797 e_Buffer_Write(@NetOut, Byte(Start));
2798 e_Buffer_Write(@NetOut, Command);
2799 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2800 end;
2802 procedure MC_SEND_PlayerSettings();
2803 begin
2804 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRSET));
2805 e_Buffer_Write(@NetOut, gPlayer1Settings.Name);
2806 e_Buffer_Write(@NetOut, gPlayer1Settings.Model);
2807 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.R);
2808 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.G);
2809 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.B);
2810 e_Buffer_Write(@NetOut, gPlayer1Settings.Team);
2812 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2813 end;
2815 procedure MC_SEND_FullStateRequest();
2816 begin
2817 e_Buffer_Write(@NetOut, Byte(NET_MSG_REQFST));
2819 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2820 end;
2822 procedure MC_SEND_CheatRequest(Kind: Byte);
2823 begin
2824 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHEAT));
2825 e_Buffer_Write(@NetOut, Kind);
2827 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2828 end;
2829 procedure MC_SEND_RCONPassword(Password: string);
2830 begin
2831 e_Buffer_Write(@NetOut, Byte(NET_MSG_RCON_AUTH));
2832 e_Buffer_Write(@NetOut, Password);
2834 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2835 end;
2836 procedure MC_SEND_RCONCommand(Cmd: string);
2837 begin
2838 e_Buffer_Write(@NetOut, Byte(NET_MSG_RCON_CMD));
2839 e_Buffer_Write(@NetOut, Cmd);
2841 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2842 end;
2844 // i have no idea why all this stuff is in here
2846 function ReadFile(const FileName: TFileName): AByte;
2847 var
2848 FileStream : TStream;
2849 fname: string;
2850 begin
2851 e_WriteLog(Format('NETWORK: looking for file "%s"', [FileName]), MSG_NOTIFY);
2852 fname := findDiskWad(FileName);
2853 if length(fname) = 0 then
2854 begin
2855 e_WriteLog(Format('NETWORK: file "%s" not found!', [FileName]), MSG_FATALERROR);
2856 SetLength(Result, 0);
2857 exit;
2858 end;
2859 e_WriteLog(Format('NETWORK: found file "%s"', [fname]), MSG_NOTIFY);
2860 Result := nil;
2861 FileStream := openDiskFileRO(fname);
2862 try
2863 if FileStream.Size > 0 then
2864 begin
2865 SetLength(Result, FileStream.Size);
2866 FileStream.Read(Result[0], FileStream.Size);
2867 end;
2868 finally
2869 FileStream.Free;
2870 end;
2871 end;
2873 function CreateMapDataMsg(const FileName: TFileName; ResList: TStringList): TMapDataMsg;
2874 var
2875 i: Integer;
2876 begin
2877 Result.MsgId := NET_MSG_MAP_RESPONSE;
2878 Result.FileData := ReadFile(FileName);
2879 Result.FileSize := Length(Result.FileData);
2881 SetLength(Result.ExternalResources, ResList.Count);
2882 for i:=0 to ResList.Count-1 do
2883 begin
2884 Result.ExternalResources[i].Name := ResList.Strings[i];
2885 Result.ExternalResources[i].md5 := MD5File(GameDir+'/wads/'+ResList.Strings[i]);
2886 end;
2887 end;
2889 procedure ResDataMsgToBytes(var bytes: AByte; const ResData: TResDataMsg);
2890 var
2891 ResultStream: TMemoryStream;
2892 begin
2893 ResultStream := TMemoryStream.Create;
2895 ResultStream.WriteBuffer(ResData.MsgId, SizeOf(ResData.MsgId)); //msgId
2896 ResultStream.WriteBuffer(ResData.FileSize, SizeOf(ResData.FileSize)); //file size
2897 ResultStream.WriteBuffer(ResData.FileData[0], ResData.FileSize); //file data
2899 SetLength(bytes, ResultStream.Size);
2900 ResultStream.Seek(0, soFromBeginning);
2901 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
2903 ResultStream.Free;
2904 end;
2906 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
2907 begin
2908 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
2909 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize));
2910 SetLength(Result.FileData, Result.FileSize);
2911 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize);
2912 end;
2914 procedure MapDataMsgToBytes(var bytes: AByte; const MapDataMsg: TMapDataMsg);
2915 var
2916 ResultStream: TMemoryStream;
2917 resCount: Integer;
2918 begin
2919 resCount := Length(MapDataMsg.ExternalResources);
2921 ResultStream := TMemoryStream.Create;
2923 ResultStream.WriteBuffer(MapDataMsg.MsgId, SizeOf(MapDataMsg.MsgId)); //msgId
2924 ResultStream.WriteBuffer(MapDataMsg.FileSize, SizeOf(MapDataMsg.FileSize)); //file size
2925 ResultStream.WriteBuffer(MapDataMsg.FileData[0], MapDataMsg.FileSize); //file data
2927 ResultStream.WriteBuffer(resCount, SizeOf(resCount)); //res count
2928 ResultStream.WriteBuffer(MapDataMsg.ExternalResources[0], resCount*SizeOf(TExternalResourceInfo)); //res data
2930 SetLength(bytes, ResultStream.Size);
2931 ResultStream.Seek(0, soFromBeginning);
2932 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
2934 ResultStream.Free;
2935 end;
2937 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
2938 var
2939 resCount: Integer;
2940 begin
2941 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
2942 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize)); //file size
2944 SetLength(Result.FileData, Result.FileSize);
2945 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize); //file data
2947 msgStream.ReadBuffer(resCount, SizeOf(resCount)); //res count
2948 SetLength(Result.ExternalResources, resCount);
2950 msgStream.ReadBuffer(Result.ExternalResources[0], resCount * SizeOf(TExternalResourceInfo)); //res data
2951 end;
2953 function IsValidFileName(const S: String): Boolean;
2954 const
2955 Forbidden: set of Char = ['<', '>', '|', '"', ':', '*', '?'];
2956 var
2957 I: Integer;
2958 begin
2959 Result := S <> '';
2960 for I := 1 to Length(S) do
2961 Result := Result and (not(S[I] in Forbidden));
2962 end;
2964 function IsValidFilePath(const S: String): Boolean;
2965 var
2966 I: Integer;
2967 begin
2968 Result := False;
2969 if not IsValidFileName(S) then exit;
2970 if FileExists(S) then exit;
2971 I := LastDelimiter('\/', S);
2972 if (I > 0) then
2973 if (not DirectoryExists(Copy(S, 1, I-1))) then
2974 exit;
2975 Result := True;
2976 end;
2978 procedure MC_SEND_MapRequest();
2979 begin
2980 e_Buffer_Write(@NetOut, Byte(NET_MSG_MAP_REQUEST));
2981 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2982 end;
2984 procedure MC_SEND_ResRequest(const resName: AnsiString);
2985 begin
2986 e_Buffer_Write(@NetOut, Byte(NET_MSG_RES_REQUEST));
2987 e_Buffer_Write(@NetOut, resName);
2988 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2989 end;
2991 procedure MH_RECV_MapRequest(C: pTNetClient; P: Pointer);
2992 var
2993 payload: AByte;
2994 peer: pENetPeer;
2995 mapDataMsg: TMapDataMsg;
2996 begin
2997 e_WriteLog('NET: Received map request from ' +
2998 DecodeIPV4(C.Peer.address.host), MSG_NOTIFY);
3000 mapDataMsg := CreateMapDataMsg(MapsDir + gGameSettings.WAD, gExternalResources);
3001 peer := NetClients[C.ID].Peer;
3003 MapDataMsgToBytes(payload, mapDataMsg);
3004 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3006 payload := nil;
3007 mapDataMsg.FileData := nil;
3008 mapDataMsg.ExternalResources := nil;
3009 end;
3011 procedure MH_RECV_ResRequest(C: pTNetClient; P: Pointer);
3012 var
3013 payload: AByte;
3014 peer: pENetPeer;
3015 FileName: String;
3016 resDataMsg: TResDataMsg;
3017 begin
3018 FileName := ExtractFileName(e_Raw_Read_String(P));
3019 e_WriteLog('NET: Received res request: ' + FileName +
3020 ' from ' + DecodeIPV4(C.Peer.address.host), MSG_NOTIFY);
3022 if not IsValidFilePath(FileName) then
3023 begin
3024 e_WriteLog('Invalid filename: ' + FileName, MSG_WARNING);
3025 exit;
3026 end;
3028 peer := NetClients[C.ID].Peer;
3030 if gExternalResources.IndexOf(FileName) > -1 then
3031 begin
3032 resDataMsg.MsgId := NET_MSG_RES_RESPONSE;
3033 resDataMsg.FileData := ReadFile(GameDir+'/wads/'+FileName);
3034 resDataMsg.FileSize := Length(resDataMsg.FileData);
3036 ResDataMsgToBytes(payload, resDataMsg);
3037 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3038 end;
3039 end;
3041 end.