DEADSOFTWARE

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