DEADSOFTWARE

bd32f68095a8d15b13a788babc2188afc70dee20
[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, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 unit g_netmsg;
18 interface
20 uses e_msg, g_net, g_triggers, Classes, SysUtils, md5;
22 const
23 NET_MSG_INFO = 100;
25 NET_MSG_CHAT = 101;
26 NET_MSG_SND = 102;
27 NET_MSG_GFX = 103;
28 NET_MSG_GEVENT = 104;
29 NET_MSG_SCORE = 105;
30 NET_MSG_COOP = 106;
31 NET_MSG_FLAG = 107;
32 NET_MSG_REQFST = 108;
33 NET_MSG_GSET = 109;
35 NET_MSG_PLR = 111;
36 NET_MSG_PLRPOS = 112;
37 NET_MSG_PLRSTA = 113;
38 NET_MSG_PLRDEL = 114;
39 NET_MSG_PLRDMG = 115;
40 NET_MSG_PLRDIE = 116;
41 NET_MSG_PLRFIRE= 117;
42 NET_MSG_PLRSET = 119;
43 NET_MSG_CHEAT = 120;
45 NET_MSG_ISPAWN = 121;
46 NET_MSG_IDEL = 122;
48 NET_MSG_MSPAWN = 131;
49 NET_MSG_MPOS = 132;
50 NET_MSG_MSTATE = 133;
51 NET_MSG_MSHOT = 134;
52 NET_MSG_MDEL = 135;
54 NET_MSG_PSTATE = 141;
55 NET_MSG_PTEX = 142;
57 NET_MSG_TSOUND = 151;
58 NET_MSG_TMUSIC = 152;
60 NET_MSG_SHDEL = 161;
61 NET_MSG_SHADD = 162;
62 NET_MSG_SHPOS = 163;
64 NET_MSG_RCON_AUTH = 191;
65 NET_MSG_RCON_CMD = 192;
66 NET_MSG_TIME_SYNC = 194;
67 NET_MSG_VOTE_EVENT = 195;
69 {
70 NET_MSG_MAP_REQUEST = 201;
71 NET_MSG_MAP_RESPONSE = 202;
72 NET_MSG_RES_REQUEST = 203;
73 NET_MSG_RES_RESPONSE = 204;
74 }
76 NET_CHAT_SYSTEM = 0;
77 NET_CHAT_PLAYER = 1;
78 NET_CHAT_TEAM = 2;
80 NET_RCON_NOAUTH = 0;
81 NET_RCON_PWGOOD = 1;
82 NET_RCON_PWBAD = 2;
84 NET_GFX_SPARK = 1;
85 NET_GFX_TELE = 2;
86 NET_GFX_RESPAWN = 3;
87 NET_GFX_FIRE = 4;
88 NET_GFX_EXPLODE = 5;
89 NET_GFX_BFGEXPL = 6;
90 NET_GFX_BFGHIT = 7;
91 NET_GFX_SHELL1 = 8;
92 NET_GFX_SHELL2 = 9;
93 NET_GFX_SHELL3 = 10;
95 NET_EV_MAPSTART = 1;
96 NET_EV_MAPEND = 2;
97 NET_EV_CHANGE_TEAM = 3;
98 NET_EV_PLAYER_KICK = 4;
99 NET_EV_PLAYER_BAN = 5;
100 NET_EV_LMS_WARMUP = 6;
101 NET_EV_LMS_SURVIVOR = 7;
102 NET_EV_RCON = 8;
103 NET_EV_BIGTEXT = 9;
104 NET_EV_SCORE = 10;
105 NET_EV_SCORE_MSG = 11;
106 NET_EV_LMS_START = 12;
107 NET_EV_LMS_WIN = 13;
108 NET_EV_TLMS_WIN = 14;
109 NET_EV_LMS_LOSE = 15;
110 NET_EV_LMS_DRAW = 16;
111 NET_EV_KILLCOMBO = 17;
112 NET_EV_PLAYER_TOUCH = 18;
113 NET_EV_SECRET = 19;
114 NET_EV_INTER_READY = 20;
116 NET_VE_STARTED = 1;
117 NET_VE_PASSED = 2;
118 NET_VE_FAILED = 3;
119 NET_VE_VOTE = 4;
120 NET_VE_REVOKE = 5;
121 NET_VE_INPROGRESS = 6;
123 NET_FLAG_GET = 1;
124 NET_FLAG_DROP = 2;
125 NET_FLAG_CAP = 3;
126 NET_FLAG_RETURN = 4;
128 NET_CHEAT_SUICIDE = 1;
129 NET_CHEAT_SPECTATE = 2;
130 NET_CHEAT_READY = 3;
132 NET_MAX_DIFFTIME = 5000 div 36;
134 // HOST MESSAGES
136 procedure MH_RECV_Info(C: pTNetClient; var M: TMsg);
137 procedure MH_RECV_Chat(C: pTNetClient; var M: TMsg);
138 procedure MH_RECV_FullStateRequest(C: pTNetClient; var M: TMsg);
139 function MH_RECV_PlayerPos(C: pTNetClient; var M: TMsg): Word;
140 procedure MH_RECV_PlayerSettings(C: pTNetClient; var M: TMsg);
141 procedure MH_RECV_CheatRequest(C: pTNetClient; var M: TMsg);
142 procedure MH_RECV_RCONPassword(C: pTNetClient; var M: TMsg);
143 procedure MH_RECV_RCONCommand(C: pTNetClient; var M: TMsg);
144 //procedure MH_RECV_MapRequest(C: pTNetClient; var M: TMsg);
145 //procedure MH_RECV_ResRequest(C: pTNetClient; var M: TMsg);
146 procedure MH_RECV_Vote(C: pTNetClient; var M: TMsg);
148 // GAME
149 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
150 procedure MH_SEND_Info(ID: Byte);
151 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
152 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
153 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
154 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
155 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
156 procedure MH_SEND_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
157 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
158 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
159 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
160 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
161 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
162 // PLAYER
163 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
164 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
165 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
166 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
167 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
168 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
169 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
170 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
171 // ITEM
172 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
173 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
174 // PANEL
175 procedure MH_SEND_PanelTexture(PGUID: Integer; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
176 procedure MH_SEND_PanelState(PGUID: Integer; ID: Integer = NET_EVERYONE);
177 // MONSTER
178 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
179 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
180 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
181 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
182 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
183 // TRIGGER
184 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
185 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
186 // MISC
187 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
188 procedure MH_SEND_VoteEvent(EvType: Byte;
189 StrArg1: string = 'a'; StrArg2: string = 'b';
190 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
191 ID: Integer = NET_EVERYONE);
193 // CLIENT MESSAGES //
195 // GAME
196 procedure MC_RECV_Chat(var M: TMsg);
197 procedure MC_RECV_Effect(var M: TMsg);
198 procedure MC_RECV_Sound(var M: TMsg);
199 procedure MC_RECV_GameStats(var M: TMsg);
200 procedure MC_RECV_CoopStats(var M: TMsg);
201 procedure MC_RECV_GameEvent(var M: TMsg);
202 procedure MC_RECV_FlagEvent(var M: TMsg);
203 procedure MC_RECV_GameSettings(var M: TMsg);
204 // PLAYER
205 function MC_RECV_PlayerCreate(var M: TMsg): Word;
206 function MC_RECV_PlayerPos(var M: TMsg): Word;
207 function MC_RECV_PlayerStats(var M: TMsg): Word;
208 function MC_RECV_PlayerDelete(var M: TMsg): Word;
209 function MC_RECV_PlayerDamage(var M: TMsg): Word;
210 function MC_RECV_PlayerDeath(var M: TMsg): Word;
211 function MC_RECV_PlayerFire(var M: TMsg): Word;
212 procedure MC_RECV_PlayerSettings(var M: TMsg);
213 // ITEM
214 procedure MC_RECV_ItemSpawn(var M: TMsg);
215 procedure MC_RECV_ItemDestroy(var M: TMsg);
216 // PANEL
217 procedure MC_RECV_PanelTexture(var M: TMsg);
218 procedure MC_RECV_PanelState(var M: TMsg);
219 // MONSTER
220 procedure MC_RECV_MonsterSpawn(var M: TMsg);
221 procedure MC_RECV_MonsterPos(var M: TMsg);
222 procedure MC_RECV_MonsterState(var M: TMsg);
223 procedure MC_RECV_MonsterShot(var M: TMsg);
224 procedure MC_RECV_MonsterDelete(var M: TMsg);
225 // SHOT
226 procedure MC_RECV_CreateShot(var M: TMsg);
227 procedure MC_RECV_UpdateShot(var M: TMsg);
228 procedure MC_RECV_DeleteShot(var M: TMsg);
229 // TRIGGER
230 procedure MC_RECV_TriggerSound(var M: TMsg);
231 procedure MC_RECV_TriggerMusic(var M: TMsg);
232 // MISC
233 procedure MC_RECV_TimeSync(var M: TMsg);
234 procedure MC_RECV_VoteEvent(var M: TMsg);
235 // SERVICE
236 procedure MC_SEND_Info(Password: string);
237 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
238 procedure MC_SEND_PlayerPos();
239 procedure MC_SEND_FullStateRequest();
240 procedure MC_SEND_PlayerSettings();
241 procedure MC_SEND_CheatRequest(Kind: Byte);
242 procedure MC_SEND_RCONPassword(Password: string);
243 procedure MC_SEND_RCONCommand(Cmd: string);
244 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
245 // DOWNLOAD
246 //procedure MC_SEND_MapRequest();
247 //procedure MC_SEND_ResRequest(const resName: AnsiString);
250 type
251 TExternalResourceInfo = record
252 Name: string[255];
253 md5: TMD5Digest;
254 end;
256 TResDataMsg = record
257 MsgId: Byte;
258 FileSize: Integer;
259 FileData: AByte;
260 end;
262 TMapDataMsg = record
263 MsgId: Byte;
264 FileSize: Integer;
265 FileData: AByte;
266 ExternalResources: array of TExternalResourceInfo;
267 end;
269 function IsValidFileName(const S: String): Boolean;
270 function IsValidFilePath(const S: String): Boolean;
272 implementation
274 uses
275 Math, ENet, e_input, e_graphics, e_log,
276 g_textures, g_gfx, g_sound, g_console, g_basic, g_options, g_main,
277 g_game, g_player, g_map, g_panel, g_items, g_weapons, g_phys, g_gui,
278 g_language, g_monsters, g_netmaster, utils, wadreader, MAPDEF;
280 const
281 NET_KEY_LEFT = 1;
282 NET_KEY_RIGHT = 2;
283 NET_KEY_UP = 4;
284 NET_KEY_DOWN = 8;
285 NET_KEY_JUMP = 16;
286 NET_KEY_FIRE = 32;
287 NET_KEY_OPEN = 64;
288 NET_KEY_NW = 256;
289 NET_KEY_PW = 512;
290 NET_KEY_CHAT = 2048;
291 NET_KEY_FORCEDIR = 4096;
293 //var
294 //kBytePrev: Word = 0;
295 //kDirPrev: TDirection = D_LEFT;
296 //HostGameTime: Word = 0;
298 // HOST MESSAGES //
301 // GAME
303 procedure MH_RECV_Chat(C: pTNetClient; var M: TMsg);
304 var
305 Txt: string;
306 Mode: Byte;
307 PID: Word;
308 Pl: TPlayer;
309 begin
310 PID := C^.Player;
311 Pl := g_Player_Get(PID);
313 Txt := M.ReadString();
314 Mode := M.ReadByte();
315 if (Mode = NET_CHAT_SYSTEM) then
316 Mode := NET_CHAT_PLAYER; // prevent sending system messages from clients
317 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
318 Mode := NET_CHAT_PLAYER; // revert to player chat in non-team game
320 if Pl = nil then
321 MH_SEND_Chat(Txt, Mode)
322 else
323 MH_SEND_Chat(Pl.Name + ': ' + Txt, Mode, IfThen(Mode = NET_CHAT_TEAM, Pl.Team, NET_EVERYONE));
324 end;
326 procedure MH_RECV_Info(C: pTNetClient; var M: TMsg);
327 var
328 Ver, PName, Model, Pw: string;
329 R, G, B, T: Byte;
330 PID: Word;
331 Color: TRGB;
332 I: Integer;
333 begin
334 Ver := M.ReadString();
335 Pw := M.ReadString();
336 PName := M.ReadString();
337 Model := M.ReadString();
338 R := M.ReadByte();
339 G := M.ReadByte();
340 B := M.ReadByte();
341 T := M.ReadByte();
343 if Ver <> GAME_VERSION then
344 begin
345 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
346 _lc[I_NET_DISC_VERSION]);
347 enet_peer_disconnect(C^.Peer, NET_DISC_VERSION);
348 Exit;
349 end;
351 if g_Net_IsHostBanned(C^.Peer^.address.host) then
352 begin
353 if g_Net_IsHostBanned(C^.Peer^.address.host, True) then
354 begin
355 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
356 _lc[I_NET_DISC_BAN]);
357 enet_peer_disconnect(C^.Peer, NET_DISC_BAN);
358 end
359 else
360 begin
361 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
362 _lc[I_NET_DISC_BAN]);
363 enet_peer_disconnect(C^.Peer, NET_DISC_TEMPBAN);
364 end;
365 Exit;
366 end;
368 if NetPassword <> '' then
369 if AnsiLowerCase(NetPassword) <> AnsiLowerCase(Pw) then
370 begin
371 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
372 _lc[I_NET_DISC_PASSWORD]);
373 enet_peer_disconnect(C^.Peer, NET_DISC_PASSWORD);
374 Exit;
375 end;
377 Color.R := R;
378 Color.B := B;
379 Color.G := G;
381 PID := g_Player_Create(Model, Color, T, False);
382 with g_Player_Get(PID) do
383 begin
384 Name := PName;
385 Reset(True);
386 end;
388 C^.Player := PID;
390 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
391 e_WriteLog('NET: Client ' + PName + ' [' + IntToStr(C^.ID) +
392 '] connected. Assigned player #' + IntToStr(PID) + '.', TMsgType.Notify);
394 MH_SEND_Info(C^.ID);
396 with g_Player_Get(PID) do
397 begin
398 Name := PName;
399 FClientID := C^.ID;
400 // round in progress, don't spawn
401 if (gGameSettings.MaxLives > 0) and (gLMSRespawn = LMS_RESPAWN_NONE) then
402 begin
403 Lives := 0;
404 FNoRespawn := True;
405 Spectate;
406 FWantsInGame := True; // TODO: look into this later
407 end
408 else
409 Respawn(gGameSettings.GameType = GT_SINGLE);
410 end;
412 for I := Low(NetClients) to High(NetClients) do
413 begin
414 if NetClients[I].ID = C^.ID then Continue;
415 MH_SEND_PlayerCreate(PID, NetClients[I].ID);
416 MH_SEND_PlayerPos(True, PID, NetClients[I].ID);
417 MH_SEND_PlayerStats(PID, NetClients[I].ID);
418 end;
420 if gState in [STATE_INTERCUSTOM, STATE_FOLD] then
421 MH_SEND_GameEvent(NET_EV_MAPEND, 0, 'N', C^.ID);
423 if NetUseMaster then g_Net_Slist_Update;
424 end;
426 procedure MH_RECV_FullStateRequest(C: pTNetClient; var M: TMsg);
427 begin
428 if gGameOn then
429 MH_SEND_Everything((C^.State = NET_STATE_AUTH), C^.ID)
430 else
431 C^.RequestedFullUpdate := True;
432 end;
434 // PLAYER
436 function MH_RECV_PlayerPos(C: pTNetClient; var M: TMsg): Word;
437 var
438 Dir, i: Byte;
439 WeaponSelect: Word;
440 PID: Word;
441 kByte: Word;
442 Pl: TPlayer;
443 GT: LongWord;
444 begin
445 Result := 0;
446 if not gGameOn then Exit;
448 GT := M.ReadLongWord();
449 PID := C^.Player;
450 Pl := g_Player_Get(PID);
451 if Pl = nil then
452 Exit;
454 if (GT > gTime + NET_MAX_DIFFTIME) or (GT < Pl.NetTime) then Exit;
456 with Pl do
457 begin
458 NetTime := GT;
459 kByte := M.ReadWord();
460 Dir := M.ReadByte();
461 WeaponSelect := M.ReadWord();
462 //e_WriteLog(Format('R:ws=%d', [WeaponSelect]), MSG_WARNING);
463 if Direction <> TDirection(Dir) then
464 JustTeleported := False;
466 SetDirection(TDirection(Dir));
467 ReleaseKeys;
469 if kByte = NET_KEY_CHAT then
470 begin
471 PressKey(KEY_CHAT, 10000);
472 Exit;
473 end;
475 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
476 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
477 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
478 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
479 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
480 if LongBool(kByte and NET_KEY_FIRE) then PressKey(KEY_FIRE, 10000);
481 if LongBool(kByte and NET_KEY_OPEN) then PressKey(KEY_OPEN, 10000);
482 if LongBool(kByte and NET_KEY_NW) then PressKey(KEY_NEXTWEAPON, 10000);
483 if LongBool(kByte and NET_KEY_PW) then PressKey(KEY_PREVWEAPON, 10000);
485 for i := 0 to 15 do
486 begin
487 if (WeaponSelect and Word(1 shl i)) <> 0 then
488 begin
489 //e_WriteLog(Format(' R:wn=%d', [i]), MSG_WARNING);
490 QueueWeaponSwitch(i);
491 end;
492 end;
493 end;
495 // MH_SEND_PlayerPos(False, PID, C^.ID);
496 end;
498 procedure MH_RECV_CheatRequest(C: pTNetClient; var M: TMsg);
499 var
500 CheatKind: Byte;
501 Pl: TPlayer;
502 begin
503 Pl := g_Player_Get(C^.Player);
504 if Pl = nil then Exit;
506 CheatKind := M.ReadByte();
508 case CheatKind of
509 NET_CHEAT_SUICIDE:
510 Pl.Damage(SUICIDE_DAMAGE, Pl.UID, 0, 0, HIT_SELF);
511 NET_CHEAT_SPECTATE:
512 begin
513 if Pl.FSpectator then
514 Pl.Respawn(False)
515 else
516 Pl.Spectate;
517 end;
518 NET_CHEAT_READY:
519 begin
520 if gState <> STATE_INTERCUSTOM then Exit;
521 Pl.FReady := not Pl.FReady;
522 if Pl.FReady then
523 begin
524 MH_SEND_GameEvent(NET_EV_INTER_READY, Pl.UID, 'Y');
525 Inc(gInterReadyCount);
526 end
527 else
528 begin
529 MH_SEND_GameEvent(NET_EV_INTER_READY, Pl.UID, 'N');
530 Dec(gInterReadyCount);
531 end;
532 end;
533 end;
534 end;
536 procedure MH_RECV_PlayerSettings(C: pTNetClient; var M: TMsg);
537 var
538 TmpName: string;
539 TmpModel: string;
540 TmpColor: TRGB;
541 TmpTeam: Byte;
542 Pl: TPlayer;
543 begin
544 TmpName := M.ReadString();
545 TmpModel := M.ReadString();
546 TmpColor.R := M.ReadByte();
547 TmpColor.G := M.ReadByte();
548 TmpColor.B := M.ReadByte();
549 TmpTeam := M.ReadByte();
551 Pl := g_Player_Get(C^.Player);
552 if Pl = nil then Exit;
554 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
555 Pl.SwitchTeam
556 else
557 Pl.SetColor(TmpColor);
559 if Pl.Name <> TmpName then
560 begin
561 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
562 Pl.Name := TmpName;
563 end;
565 if TmpModel <> Pl.Model.Name then
566 Pl.SetModel(TmpModel);
568 MH_SEND_PlayerSettings(Pl.UID, TmpModel);
569 end;
571 // RCON
573 procedure MH_RECV_RCONPassword(C: pTNetClient; var M: TMsg);
574 var
575 Pwd: string;
576 begin
577 Pwd := M.ReadString();
578 if not NetAllowRCON then Exit;
579 if Pwd = NetRCONPassword then
580 begin
581 C^.RCONAuth := True;
582 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWGOOD, 'N', C^.ID);
583 end
584 else
585 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWBAD, 'N', C^.ID);
586 end;
588 procedure MH_RECV_RCONCommand(C: pTNetClient; var M: TMsg);
589 var
590 Cmd: string;
591 begin
592 Cmd := M.ReadString();
593 if not NetAllowRCON then Exit;
594 if not C^.RCONAuth then
595 begin
596 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_NOAUTH, 'N', C^.ID);
597 Exit;
598 end;
599 g_Console_Process(Cmd);
600 end;
602 // MISC
604 procedure MH_RECV_Vote(C: pTNetClient; var M: TMsg);
605 var
606 Start: Boolean;
607 Name, Command: string;
608 Need: Integer;
609 Pl: TPlayer;
610 begin
611 Start := M.ReadByte() <> 0;
612 Command := M.ReadString();
614 Pl := g_Player_Get(C^.Player);
615 if Pl = nil then Exit;
616 Name := Pl.Name;
618 if Start then
619 begin
620 if not g_Console_CommandBlacklisted(Command) then
621 g_Game_StartVote(Command, Name);
622 end
623 else if gVoteInProgress then
624 begin
625 if (gPlayer1 <> nil) or (gPlayer2 <> nil) then
626 Need := Floor((NetClientCount+1)/2.0) + 1
627 else
628 Need := Floor(NetClientCount/2.0) + 1;
629 if C^.Voted then
630 begin
631 Dec(gVoteCount);
632 C^.Voted := False;
633 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_REVOKED], [Name, gVoteCount, Need]), True);
634 MH_SEND_VoteEvent(NET_VE_REVOKE, Name, 'a', gVoteCount, Need);
635 end
636 else
637 begin
638 Inc(gVoteCount);
639 C^.Voted := True;
640 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Name, gVoteCount, Need]), True);
641 MH_SEND_VoteEvent(NET_VE_VOTE, Name, 'a', gVoteCount, Need);
642 g_Game_CheckVote;
643 end;
644 end;
645 end;
647 // GAME (SEND)
649 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
651 function sendItemRespawn (it: PItem): Boolean;
652 begin
653 result := false; // don't stop
654 MH_SEND_ItemSpawn(True, it.myid, ID);
655 end;
657 function sendMonSpawn (mon: TMonster): Boolean;
658 begin
659 result := false; // don't stop
660 MH_SEND_MonsterSpawn(mon.UID, ID);
661 end;
663 function sendPanelState (pan: TPanel): Boolean;
664 begin
665 result := false; // don't stop
666 MH_SEND_PanelState(pan.guid, ID); // anyway, to sync mplats
667 if (pan.CanChangeTexture) then MH_SEND_PanelTexture(pan.guid, pan.LastAnimLoop, ID);
668 end;
670 var
671 I: Integer;
672 begin
673 if gPlayers <> nil then
674 begin
675 for I := Low(gPlayers) to High(gPlayers) do
676 begin
677 if gPlayers[I] <> nil then
678 begin
679 if CreatePlayers then MH_SEND_PlayerCreate(gPlayers[I].UID, ID);
680 MH_SEND_PlayerPos(True, gPlayers[I].UID, ID);
681 MH_SEND_PlayerStats(gPlayers[I].UID, ID);
683 if (gPlayers[I].Flag <> FLAG_NONE) and (gGameSettings.GameMode = GM_CTF) then
684 begin
685 MH_SEND_FlagEvent(FLAG_STATE_CAPTURED, gPlayers[I].Flag, gPlayers[I].UID, True, ID);
686 end;
687 end;
688 end;
689 end;
691 g_Items_ForEachAlive(sendItemRespawn, true); // backwards
692 g_Mons_ForEach(sendMonSpawn);
693 g_Map_ForEachPanel(sendPanelState);
695 if gTriggers <> nil then
696 begin
697 for I := Low(gTriggers) to High(gTriggers) do
698 begin
699 if gTriggers[I].TriggerType = TRIGGER_SOUND then
700 begin
701 MH_SEND_TriggerSound(gTriggers[I], ID);
702 end;
703 end;
704 end;
706 if Shots <> nil then
707 begin
708 for I := Low(Shots) to High(Shots) do
709 begin
710 if Shots[i].ShotType in [6, 7, 8] then
711 begin
712 MH_SEND_CreateShot(i, ID);
713 end;
714 end;
715 end;
717 MH_SEND_TriggerMusic(ID);
719 MH_SEND_GameStats(ID);
720 MH_SEND_CoopStats(ID);
722 if gGameSettings.GameMode = GM_CTF then
723 begin
724 if gFlags[FLAG_RED].State <> FLAG_STATE_CAPTURED then MH_SEND_FlagEvent(gFlags[FLAG_RED].State, FLAG_RED, 0, True, ID);
725 if gFlags[FLAG_BLUE].State <> FLAG_STATE_CAPTURED then MH_SEND_FlagEvent(gFlags[FLAG_BLUE].State, FLAG_BLUE, 0, True, ID);
726 end;
728 if CreatePlayers and (ID >= 0) then NetClients[ID].State := NET_STATE_GAME;
730 if gLMSRespawn > LMS_RESPAWN_NONE then
731 begin
732 MH_SEND_GameEvent(NET_EV_LMS_WARMUP, (gLMSRespawnTime - gTime) div 1000, 'N', ID);
733 end;
735 g_Net_Flush();
736 end;
738 procedure MH_SEND_Info(ID: Byte);
739 var
740 Map: string;
741 begin
742 Map := g_ExtractFileName(gMapInfo.Map);
744 NetOut.Clear();
746 NetOut.Write(Byte(NET_MSG_INFO));
747 NetOut.Write(ID);
748 NetOut.Write(NetClients[ID].Player);
749 NetOut.Write(gGameSettings.WAD);
750 NetOut.Write(Map);
751 NetOut.Write(gWADHash);
752 NetOut.Write(gGameSettings.GameMode);
753 NetOut.Write(gGameSettings.GoalLimit);
754 NetOut.Write(gGameSettings.TimeLimit);
755 NetOut.Write(gGameSettings.MaxLives);
756 NetOut.Write(gGameSettings.Options);
757 NetOut.Write(gTime);
759 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
760 end;
762 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
763 var
764 Name: string;
765 i: Integer;
766 Team: Byte;
767 begin
768 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
769 Mode := NET_CHAT_PLAYER;
771 Team := 0;
772 if (Mode = NET_CHAT_TEAM) then
773 begin
774 for i := Low(gPlayers) to High(gPlayers) do
775 if (gPlayers[i] <> nil) and (gPlayers[i].FClientID >= 0) and
776 (gPlayers[i].Team = ID) then
777 begin
778 NetOut.Write(Byte(NET_MSG_CHAT));
779 NetOut.Write(Txt);
780 NetOut.Write(Mode);
781 g_Net_Host_Send(gPlayers[i].FClientID, True, NET_CHAN_CHAT);
782 end;
783 Team := ID;
784 ID := NET_EVERYONE;
785 end
786 else
787 begin
788 NetOut.Write(Byte(NET_MSG_CHAT));
789 NetOut.Write(Txt);
790 NetOut.Write(Mode);
791 g_Net_Host_Send(ID, True, NET_CHAN_CHAT);
792 end;
794 if Mode = NET_CHAT_SYSTEM then
795 Exit;
797 if ID = NET_EVERYONE then
798 begin
799 if Mode = NET_CHAT_PLAYER then
800 begin
801 g_Console_Add(Txt, True);
802 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
803 g_Game_ChatSound(b_Text_Unformat(Txt));
804 end
805 else
806 if Mode = NET_CHAT_TEAM then
807 if gPlayer1 <> nil then
808 begin
809 if (gPlayer1.Team = TEAM_RED) and (Team = TEAM_RED) then
810 begin
811 g_Console_Add(#18'[Team] '#2 + Txt, True);
812 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
813 g_Game_ChatSound(b_Text_Unformat(Txt));
814 end
815 else if (gPlayer1.Team = TEAM_BLUE) and (Team = TEAM_BLUE) then
816 begin
817 g_Console_Add(#20'[Team] '#2 + Txt, True);
818 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
819 g_Game_ChatSound(b_Text_Unformat(Txt));
820 end;
821 end;
822 end
823 else
824 begin
825 Name := g_Net_ClientName_ByID(ID);
826 g_Console_Add('-> ' + Name + ': ' + Txt, True);
827 e_WriteLog('[Tell ' + Name + '] ' + b_Text_Unformat(Txt), TMsgType.Notify);
828 g_Game_ChatSound(b_Text_Unformat(Txt), False);
829 end;
830 end;
832 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
833 begin
834 NetOut.Write(Byte(NET_MSG_GFX));
835 NetOut.Write(Kind);
836 NetOut.Write(X);
837 NetOut.Write(Y);
838 NetOut.Write(Ang);
840 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
841 end;
843 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
844 begin
845 NetOut.Write(Byte(NET_MSG_SND));
846 NetOut.Write(Name);
847 if Pos then
848 begin
849 NetOut.Write(Byte(1));
850 NetOut.Write(X);
851 NetOut.Write(Y);
852 end
853 else
854 NetOut.Write(Byte(0));
856 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
857 end;
859 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
860 begin
861 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
863 NetOut.Write(Byte(NET_MSG_SHADD));
864 NetOut.Write(Proj);
865 NetOut.Write(Shots[Proj].ShotType);
866 NetOut.Write(Shots[Proj].Target);
867 NetOut.Write(Shots[Proj].SpawnerUID);
868 NetOut.Write(Shots[Proj].Timeout);
869 NetOut.Write(Shots[Proj].Obj.X);
870 NetOut.Write(Shots[Proj].Obj.Y);
871 NetOut.Write(Shots[Proj].Obj.Vel.X);
872 NetOut.Write(Shots[Proj].Obj.Vel.Y);
874 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
875 end;
877 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
878 begin
879 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
881 NetOut.Write(Byte(NET_MSG_SHPOS));
882 NetOut.Write(Proj);
883 NetOut.Write(Shots[Proj].Obj.X);
884 NetOut.Write(Shots[Proj].Obj.Y);
885 NetOut.Write(Shots[Proj].Obj.Vel.X);
886 NetOut.Write(Shots[Proj].Obj.Vel.Y);
888 g_Net_Host_Send(ID, False, NET_CHAN_SHOTS);
889 end;
891 procedure MH_Send_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
892 begin
893 NetOut.Write(Byte(NET_MSG_SHDEL));
894 NetOut.Write(Proj);
895 NetOut.Write(Byte(Loud));
896 NetOut.Write(X);
897 NetOut.Write(Y);
899 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
900 end;
902 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
903 begin
904 NetOut.Write(Byte(NET_MSG_SCORE));
905 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
906 begin
907 NetOut.Write(gTeamStat[TEAM_RED].Goals);
908 NetOut.Write(gTeamStat[TEAM_BLUE].Goals);
909 end
910 else
911 if gGameSettings.GameMode = GM_COOP then
912 begin
913 NetOut.Write(gCoopMonstersKilled);
914 NetOut.Write(gCoopSecretsFound);
915 end;
917 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
918 end;
920 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
921 begin
922 NetOut.Write(Byte(NET_MSG_COOP));
923 NetOut.Write(gTotalMonsters);
924 NetOut.Write(gSecretsCount);
925 NetOut.Write(gCoopTotalMonstersKilled);
926 NetOut.Write(gCoopTotalSecretsFound);
927 NetOut.Write(gCoopTotalMonsters);
928 NetOut.Write(gCoopTotalSecrets);
929 end;
931 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
932 begin
933 NetOut.Write(Byte(NET_MSG_GEVENT));
934 NetOut.Write(EvType);
935 NetOut.Write(EvNum);
936 NetOut.Write(EvStr);
937 NetOut.Write(Byte(gLastMap));
938 NetOut.Write(gTime);
939 if (EvType = NET_EV_MAPSTART) and isWadPath(EvStr) then
940 begin
941 NetOut.Write(Byte(1));
942 NetOut.Write(gWADHash);
943 end else
944 NetOut.Write(Byte(0));
946 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
947 end;
949 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
950 begin
951 NetOut.Write(Byte(NET_MSG_FLAG));
952 NetOut.Write(EvType);
953 NetOut.Write(Flag);
954 NetOut.Write(Byte(Quiet));
955 NetOut.Write(PID);
956 NetOut.Write(gFlags[Flag].State);
957 NetOut.Write(gFlags[Flag].CaptureTime);
958 NetOut.Write(gFlags[Flag].Obj.X);
959 NetOut.Write(gFlags[Flag].Obj.Y);
960 NetOut.Write(gFlags[Flag].Obj.Vel.X);
961 NetOut.Write(gFlags[Flag].Obj.Vel.Y);
963 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
964 end;
966 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
967 begin
968 NetOut.Write(Byte(NET_MSG_GSET));
969 NetOut.Write(gGameSettings.GameMode);
970 NetOut.Write(gGameSettings.GoalLimit);
971 NetOut.Write(gGameSettings.TimeLimit);
972 NetOut.Write(gGameSettings.MaxLives);
973 NetOut.Write(gGameSettings.Options);
975 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
976 end;
978 // PLAYER (SEND)
980 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
981 var
982 P: TPlayer;
983 begin
984 P := g_Player_Get(PID);
985 if P = nil then Exit;
987 NetOut.Write(Byte(NET_MSG_PLR));
988 NetOut.Write(PID);
989 NetOut.Write(P.Name);
991 NetOut.Write(P.FActualModelName);
992 NetOut.Write(P.FColor.R);
993 NetOut.Write(P.FColor.G);
994 NetOut.Write(P.FColor.B);
995 NetOut.Write(P.Team);
997 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT)
998 end;
1000 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
1001 var
1002 kByte: Word;
1003 Pl: TPlayer;
1004 begin
1005 Pl := g_Player_Get(PID);
1006 if Pl = nil then Exit;
1007 if Pl.FDummy then Exit;
1009 NetOut.Write(Byte(NET_MSG_PLRPOS));
1010 NetOut.Write(gTime);
1011 NetOut.Write(PID);
1013 kByte := 0;
1015 with Pl do
1016 begin
1017 NetOut.Write(FPing);
1018 NetOut.Write(FLoss);
1019 if IsKeyPressed(KEY_CHAT) then
1020 kByte := NET_KEY_CHAT
1021 else
1022 begin
1023 if IsKeyPressed(KEY_LEFT) then kByte := kByte or NET_KEY_LEFT;
1024 if IsKeyPressed(KEY_RIGHT) then kByte := kByte or NET_KEY_RIGHT;
1025 if IsKeyPressed(KEY_UP) then kByte := kByte or NET_KEY_UP;
1026 if IsKeyPressed(KEY_DOWN) then kByte := kByte or NET_KEY_DOWN;
1027 if IsKeyPressed(KEY_JUMP) then kByte := kByte or NET_KEY_JUMP;
1028 if JustTeleported then kByte := kByte or NET_KEY_FORCEDIR;
1029 end;
1031 NetOut.Write(kByte);
1032 if Direction = TDirection.D_LEFT then NetOut.Write(Byte(0)) else NetOut.Write(Byte(1));
1033 NetOut.Write(GameX);
1034 NetOut.Write(GameY);
1035 NetOut.Write(GameVelX);
1036 NetOut.Write(GameVelY);
1037 NetOut.Write(GameAccelX);
1038 NetOut.Write(GameAccelY);
1039 end;
1041 g_Net_Host_Send(ID, Reliable, NET_CHAN_PLAYERPOS);
1042 end;
1044 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
1045 var
1046 P: TPlayer;
1047 I: Integer;
1048 begin
1049 P := g_Player_Get(PID);
1050 if P = nil then Exit;
1052 NetOut.Write(Byte(NET_MSG_PLRSTA));
1053 NetOut.Write(PID);
1055 with P do
1056 begin
1057 NetOut.Write(Byte(alive));
1058 NetOut.Write(Byte(GodMode));
1059 NetOut.Write(Health);
1060 NetOut.Write(Armor);
1061 NetOut.Write(Air);
1062 NetOut.Write(JetFuel);
1063 NetOut.Write(Lives);
1064 NetOut.Write(Team);
1066 for I := WP_FIRST to WP_LAST do
1067 NetOut.Write(Byte(FWeapon[I]));
1069 for I := A_BULLETS to A_HIGH do
1070 NetOut.Write(FAmmo[I]);
1072 for I := A_BULLETS to A_HIGH do
1073 NetOut.Write(FMaxAmmo[I]);
1075 for I := MR_SUIT to MR_MAX do
1076 NetOut.Write(LongWord(FMegaRulez[I]));
1078 NetOut.Write(Byte(R_ITEM_BACKPACK in FRulez));
1079 NetOut.Write(Byte(R_KEY_RED in FRulez));
1080 NetOut.Write(Byte(R_KEY_GREEN in FRulez));
1081 NetOut.Write(Byte(R_KEY_BLUE in FRulez));
1082 NetOut.Write(Byte(R_BERSERK in FRulez));
1084 NetOut.Write(Frags);
1085 NetOut.Write(Death);
1087 NetOut.Write(CurrWeap);
1089 NetOut.Write(Byte(FSpectator));
1090 NetOut.Write(Byte(FGhost));
1091 NetOut.Write(Byte(FPhysics));
1092 NetOut.Write(Byte(FNoRespawn));
1093 NetOut.Write(Byte(FJetpack));
1094 NetOut.Write(FFireTime);
1095 NetOut.Write(Byte(FFlaming));
1096 end;
1098 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1099 end;
1101 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
1102 begin
1103 NetOut.Write(Byte(NET_MSG_PLRDMG));
1104 NetOut.Write(PID);
1105 NetOut.Write(Kind);
1106 NetOut.Write(Attacker);
1107 NetOut.Write(Value);
1108 NetOut.Write(VX);
1109 NetOut.Write(VY);
1111 g_Net_Host_Send(ID, False, NET_CHAN_PLAYER);
1112 end;
1114 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
1115 begin
1116 NetOut.Write(Byte(NET_MSG_PLRDIE));
1117 NetOut.Write(PID);
1118 NetOut.Write(KillType);
1119 NetOut.Write(DeathType);
1120 NetOut.Write(Attacker);
1122 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1123 end;
1125 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
1126 begin
1127 NetOut.Write(Byte(NET_MSG_PLRFIRE));
1128 NetOut.Write(PID);
1129 NetOut.Write(Weapon);
1130 NetOut.Write(X);
1131 NetOut.Write(Y);
1132 NetOut.Write(AX);
1133 NetOut.Write(AY);
1134 NetOut.Write(ShotID);
1136 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
1137 end;
1139 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
1140 begin
1141 NetOut.Write(Byte(NET_MSG_PLRDEL));
1142 NetOut.Write(PID);
1144 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1145 end;
1147 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
1148 var
1149 Pl: TPlayer;
1150 begin
1151 Pl := g_Player_Get(PID);
1152 if Pl = nil then Exit;
1154 NetOut.Write(Byte(NET_MSG_PLRSET));
1155 NetOut.Write(PID);
1156 NetOut.Write(Pl.Name);
1157 if Mdl = '' then
1158 NetOut.Write(Pl.Model.Name)
1159 else
1160 NetOut.Write(Mdl);
1161 NetOut.Write(Pl.FColor.R);
1162 NetOut.Write(Pl.FColor.G);
1163 NetOut.Write(Pl.FColor.B);
1164 NetOut.Write(Pl.Team);
1166 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1167 end;
1169 // ITEM (SEND)
1171 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1172 var
1173 it: PItem;
1174 tt: Byte;
1175 begin
1176 it := g_Items_ByIdx(IID);
1178 NetOut.Write(Byte(NET_MSG_ISPAWN));
1179 NetOut.Write(IID);
1180 NetOut.Write(Byte(Quiet));
1181 tt := it.ItemType;
1182 if it.dropped then tt := tt or $80;
1183 NetOut.Write(tt);
1184 NetOut.Write(Byte(it.Fall));
1185 NetOut.Write(Byte(it.Respawnable));
1186 NetOut.Write(it.Obj.X);
1187 NetOut.Write(it.Obj.Y);
1188 NetOut.Write(it.Obj.Vel.X);
1189 NetOut.Write(it.Obj.Vel.Y);
1191 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1192 end;
1194 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1195 begin
1196 NetOut.Write(Byte(NET_MSG_IDEL));
1197 NetOut.Write(IID);
1198 NetOut.Write(Byte(Quiet));
1200 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1201 end;
1203 // PANEL
1205 procedure MH_SEND_PanelTexture(PGUID: Integer; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
1206 var
1207 TP: TPanel;
1208 begin
1209 TP := g_Map_PanelByGUID(PGUID);
1210 if (TP = nil) then exit;
1212 with TP do
1213 begin
1214 NetOut.Write(Byte(NET_MSG_PTEX));
1215 NetOut.Write(LongWord(PGUID));
1216 NetOut.Write(FCurTexture);
1217 NetOut.Write(FCurFrame);
1218 NetOut.Write(FCurFrameCount);
1219 NetOut.Write(AnimLoop);
1220 end;
1222 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1223 end;
1225 procedure MH_SEND_PanelState(PGUID: Integer; ID: Integer = NET_EVERYONE);
1226 var
1227 TP: TPanel;
1228 mpflags: Byte = 0;
1229 begin
1230 TP := g_Map_PanelByGUID(PGUID);
1231 if (TP = nil) then exit;
1233 NetOut.Write(Byte(NET_MSG_PSTATE));
1234 NetOut.Write(LongWord(PGUID));
1235 NetOut.Write(Byte(TP.Enabled));
1236 NetOut.Write(TP.LiftType);
1237 NetOut.Write(TP.X);
1238 NetOut.Write(TP.Y);
1239 NetOut.Write(Word(TP.Width));
1240 NetOut.Write(Word(TP.Height));
1241 // mplats
1242 NetOut.Write(LongInt(TP.movingSpeedX));
1243 NetOut.Write(LongInt(TP.movingSpeedY));
1244 NetOut.Write(LongInt(TP.movingStartX));
1245 NetOut.Write(LongInt(TP.movingStartY));
1246 NetOut.Write(LongInt(TP.movingEndX));
1247 NetOut.Write(LongInt(TP.movingEndY));
1248 NetOut.Write(LongInt(TP.sizeSpeedX));
1249 NetOut.Write(LongInt(TP.sizeSpeedY));
1250 NetOut.Write(LongInt(TP.sizeEndX));
1251 NetOut.Write(LongInt(TP.sizeEndY));
1252 if TP.movingActive then mpflags := mpflags or 1;
1253 if TP.moveOnce then mpflags := mpflags or 2;
1254 NetOut.Write(Byte(mpflags));
1256 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1257 end;
1259 // TRIGGER
1261 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
1262 begin
1263 if gTriggers = nil then Exit;
1264 if T.Sound = nil then Exit;
1266 NetOut.Write(Byte(NET_MSG_TSOUND));
1267 NetOut.Write(T.ClientID);
1268 NetOut.Write(Byte(T.Sound.IsPlaying));
1269 NetOut.Write(LongWord(T.Sound.GetPosition));
1270 NetOut.Write(T.SoundPlayCount);
1272 g_Net_Host_Send(ID, True);
1273 end;
1275 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
1276 begin
1277 NetOut.Write(Byte(NET_MSG_TMUSIC));
1278 NetOut.Write(gMusic.Name);
1279 NetOut.Write(Byte(gMusic.IsPlaying));
1280 NetOut.Write(LongWord(gMusic.GetPosition));
1281 NetOut.Write(Byte(gMusic.SpecPause or gMusic.IsPaused));
1283 g_Net_Host_Send(ID, True);
1284 end;
1286 // MONSTER
1288 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
1289 var
1290 M: TMonster;
1291 begin
1292 M := g_Monsters_ByUID(UID);
1293 if M = nil then
1294 Exit;
1296 with M do
1297 begin
1298 NetOut.Write(Byte(NET_MSG_MSPAWN));
1299 NetOut.Write(UID);
1300 NetOut.Write(MonsterType);
1301 NetOut.Write(MonsterState);
1302 NetOut.Write(MonsterAnim);
1303 NetOut.Write(MonsterTargetUID);
1304 NetOut.Write(MonsterTargetTime);
1305 NetOut.Write(MonsterBehaviour);
1306 NetOut.Write(MonsterSleep);
1307 NetOut.Write(MonsterHealth);
1308 NetOut.Write(MonsterAmmo);
1309 NetOut.Write(GameX);
1310 NetOut.Write(GameY);
1311 NetOut.Write(GameVelX);
1312 NetOut.Write(GameVelY);
1313 NetOut.Write(Byte(GameDirection));
1314 end;
1316 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1317 end;
1319 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
1320 var
1321 M: TMonster;
1322 begin
1323 M := g_Monsters_ByUID(UID);
1324 if M = nil then Exit;
1326 NetOut.Write(Byte(NET_MSG_MPOS));
1327 NetOut.Write(UID);
1329 with M do
1330 begin
1331 NetOut.Write(GameX);
1332 NetOut.Write(GameY);
1333 NetOut.Write(GameVelX);
1334 NetOut.Write(GameVelY);
1335 NetOut.Write(Byte(GameDirection));
1336 end;
1338 g_Net_Host_Send(ID, False, NET_CHAN_MONSTERPOS);
1339 end;
1341 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
1342 var
1343 M: TMonster;
1344 begin
1345 M := g_Monsters_ByUID(UID);
1346 if M = nil then Exit;
1348 NetOut.Write(Byte(NET_MSG_MSTATE));
1349 NetOut.Write(UID);
1351 with M do
1352 begin
1353 NetOut.Write(MonsterState);
1354 NetOut.Write(ForcedAnim);
1355 NetOut.Write(MonsterTargetUID);
1356 NetOut.Write(MonsterTargetTime);
1357 NetOut.Write(MonsterSleep);
1358 NetOut.Write(MonsterHealth);
1359 NetOut.Write(MonsterAmmo);
1360 NetOut.Write(MonsterPain);
1361 NetOut.Write(Byte(AnimIsReverse));
1362 NetOut.Write(FFireTime);
1363 end;
1365 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1366 end;
1368 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
1369 begin
1370 NetOut.Write(Byte(NET_MSG_MSHOT));
1371 NetOut.Write(UID);
1372 NetOut.Write(X);
1373 NetOut.Write(Y);
1374 NetOut.Write(VX);
1375 NetOut.Write(VY);
1377 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1378 end;
1380 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
1381 var
1382 M: TMonster;
1383 begin
1384 M := g_Monsters_ByUID(UID);
1385 if M = nil then Exit;
1387 NetOut.Write(Byte(NET_MSG_MDEL));
1388 NetOut.Write(UID);
1390 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1391 end;
1393 // MISC
1395 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
1396 begin
1397 NetOut.Write(Byte(NET_MSG_TIME_SYNC));
1398 NetOut.Write(Time);
1400 g_Net_Host_Send(ID, False, NET_CHAN_SERVICE);
1401 end;
1403 procedure MH_SEND_VoteEvent(EvType: Byte;
1404 StrArg1: string = 'a'; StrArg2: string = 'b';
1405 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
1406 ID: Integer = NET_EVERYONE);
1407 begin
1408 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
1409 NetOut.Write(EvType);
1410 NetOut.Write(IntArg1);
1411 NetOut.Write(IntArg2);
1412 NetOut.Write(StrArg1);
1413 NetOut.Write(StrArg2);
1415 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1416 end;
1418 // CLIENT MESSAGES //
1420 // GAME
1422 procedure MC_RECV_Chat(var M: TMsg);
1423 var
1424 Txt: string;
1425 Mode: Byte;
1426 begin
1427 Txt := M.ReadString();
1428 Mode := M.ReadByte();
1430 if Mode <> NET_CHAT_SYSTEM then
1431 begin
1432 if Mode = NET_CHAT_PLAYER then
1433 begin
1434 g_Console_Add(Txt, True);
1435 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1436 g_Game_ChatSound(b_Text_Unformat(Txt));
1437 end else
1438 if (Mode = NET_CHAT_TEAM) and (gPlayer1 <> nil) then
1439 begin
1440 if gPlayer1.Team = TEAM_RED then
1441 g_Console_Add(b_Text_Format('\r[Team] ') + Txt, True);
1442 if gPlayer1.Team = TEAM_BLUE then
1443 g_Console_Add(b_Text_Format('\b[Team] ') + Txt, True);
1444 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1445 g_Game_ChatSound(b_Text_Unformat(Txt));
1446 end;
1447 end else
1448 g_Console_Add(Txt, True);
1449 end;
1451 procedure MC_RECV_Effect(var M: TMsg);
1452 var
1453 Kind: Byte;
1454 X, Y: Integer;
1455 Ang: SmallInt;
1456 Anim: TAnimation;
1457 ID: LongWord;
1458 begin
1459 if not gGameOn then Exit;
1460 Kind := M.ReadByte();
1461 X := M.ReadLongInt();
1462 Y := M.ReadLongInt();
1463 Ang := M.ReadSmallInt();
1465 case Kind of
1466 NET_GFX_SPARK:
1467 g_GFX_Spark(X, Y, 2 + Random(2), Ang, 0, 0);
1469 NET_GFX_TELE:
1470 begin
1471 if g_Frames_Get(ID, 'FRAMES_TELEPORT') then
1472 begin
1473 Anim := TAnimation.Create(ID, False, 3);
1474 g_GFX_OnceAnim(X, Y, Anim);
1475 Anim.Free();
1476 end;
1477 if Ang = 1 then
1478 g_Sound_PlayExAt('SOUND_GAME_TELEPORT', X, Y);
1479 end;
1481 NET_GFX_EXPLODE:
1482 begin
1483 if g_Frames_Get(ID, 'FRAMES_EXPLODE_ROCKET') then
1484 begin
1485 Anim := TAnimation.Create(ID, False, 6);
1486 Anim.Blending := False;
1487 g_GFX_OnceAnim(X-64, Y-64, Anim);
1488 Anim.Free();
1489 end;
1490 if Ang = 1 then
1491 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEROCKET', X, Y);
1492 end;
1494 NET_GFX_BFGEXPL:
1495 begin
1496 if g_Frames_Get(ID, 'FRAMES_EXPLODE_BFG') then
1497 begin
1498 Anim := TAnimation.Create(ID, False, 6);
1499 Anim.Blending := False;
1500 g_GFX_OnceAnim(X-64, Y-64, Anim);
1501 Anim.Free();
1502 end;
1503 if Ang = 1 then
1504 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEBFG', X, Y);
1505 end;
1507 NET_GFX_BFGHIT:
1508 begin
1509 if g_Frames_Get(ID, 'FRAMES_BFGHIT') then
1510 begin
1511 Anim := TAnimation.Create(ID, False, 4);
1512 g_GFX_OnceAnim(X-32, Y-32, Anim);
1513 Anim.Free();
1514 end;
1515 end;
1517 NET_GFX_FIRE:
1518 begin
1519 if g_Frames_Get(ID, 'FRAMES_FIRE') then
1520 begin
1521 Anim := TAnimation.Create(ID, False, 4);
1522 g_GFX_OnceAnim(X, Y, Anim);
1523 Anim.Free();
1524 end;
1525 if Ang = 1 then
1526 g_Sound_PlayExAt('SOUND_FIRE', X, Y);
1527 end;
1529 NET_GFX_RESPAWN:
1530 begin
1531 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
1532 begin
1533 Anim := TAnimation.Create(ID, False, 4);
1534 g_GFX_OnceAnim(X, Y, Anim);
1535 Anim.Free();
1536 end;
1537 if Ang = 1 then
1538 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
1539 end;
1541 NET_GFX_SHELL1:
1542 g_Player_CreateShell(X, Y, 0, -2, SHELL_BULLET);
1544 NET_GFX_SHELL2:
1545 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1547 NET_GFX_SHELL3:
1548 begin
1549 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1550 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1551 end;
1552 end;
1553 end;
1555 procedure MC_RECV_Sound(var M: TMsg);
1556 var
1557 Name: string;
1558 X, Y: Integer;
1559 Pos: Boolean;
1560 begin
1561 Name := M.ReadString();
1562 Pos := M.ReadByte() <> 0;
1563 if Pos then
1564 begin
1565 X := M.ReadLongInt();
1566 Y := M.ReadLongInt();
1567 g_Sound_PlayExAt(Name, X, Y);
1568 end
1569 else
1570 g_Sound_PlayEx(Name);
1571 end;
1573 procedure MC_RECV_CreateShot(var M: TMsg);
1574 var
1575 I, X, Y, XV, YV: Integer;
1576 Timeout: LongWord;
1577 Target, Spawner: Word;
1578 ShType: Byte;
1579 begin
1580 I := M.ReadLongInt();
1581 ShType := M.ReadByte();
1582 Target := M.ReadWord();
1583 Spawner := M.ReadWord();
1584 Timeout := M.ReadLongWord();
1585 X := M.ReadLongInt();
1586 Y := M.ReadLongInt();
1587 XV := M.ReadLongInt();
1588 YV := M.ReadLongInt();
1590 I := g_Weapon_CreateShot(I, ShType, Spawner, Target, X, Y, XV, YV);
1591 if (Shots <> nil) and (I <= High(Shots)) then
1592 begin
1593 Shots[I].Timeout := Timeout;
1594 //Shots[I].Target := Target; // TODO: find a use for Target later
1595 end;
1596 end;
1598 procedure MC_RECV_UpdateShot(var M: TMsg);
1599 var
1600 I, TX, TY, TXV, TYV: Integer;
1601 begin
1602 I := M.ReadLongInt();
1603 TX := M.ReadLongInt();
1604 TY := M.ReadLongInt();
1605 TXV := M.ReadLongInt();
1606 TYV := M.ReadLongInt();
1608 if (Shots <> nil) and (I <= High(Shots)) then
1609 with (Shots[i]) do
1610 begin
1611 Obj.X := TX;
1612 Obj.Y := TY;
1613 Obj.Vel.X := TXV;
1614 Obj.Vel.Y := TYV;
1615 end;
1616 end;
1618 procedure MC_RECV_DeleteShot(var M: TMsg);
1619 var
1620 I, X, Y: Integer;
1621 L: Boolean;
1622 begin
1623 if not gGameOn then Exit;
1624 I := M.ReadLongInt();
1625 L := (M.ReadByte() <> 0);
1626 X := M.ReadLongInt();
1627 Y := M.ReadLongInt();
1629 g_Weapon_DestroyShot(I, X, Y, L);
1630 end;
1632 procedure MC_RECV_GameStats(var M: TMsg);
1633 begin
1634 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
1635 begin
1636 gTeamStat[TEAM_RED].Goals := M.ReadSmallInt();
1637 gTeamStat[TEAM_BLUE].Goals := M.ReadSmallInt();
1638 end
1639 else
1640 if gGameSettings.GameMode = GM_COOP then
1641 begin
1642 gCoopMonstersKilled := M.ReadWord();
1643 gCoopSecretsFound := M.ReadWord();
1644 end;
1645 end;
1647 procedure MC_RECV_CoopStats(var M: TMsg);
1648 begin
1649 gTotalMonsters := M.ReadLongInt();
1650 gSecretsCount := M.ReadLongInt();
1651 gCoopTotalMonstersKilled := M.ReadWord();
1652 gCoopTotalSecretsFound := M.ReadWord();
1653 gCoopTotalMonsters := M.ReadWord();
1654 gCoopTotalSecrets := M.ReadWord();
1655 end;
1657 procedure MC_RECV_GameEvent(var M: TMsg);
1658 var
1659 EvType: Byte;
1660 EvNum: Integer;
1661 EvStr: string;
1662 EvTime: LongWord;
1663 BHash: Boolean;
1664 EvHash: TMD5Digest;
1665 pl: TPlayer;
1666 i1, i2: TStrings_Locale;
1667 pln: String;
1668 cnt: Byte;
1669 goodCmd: Boolean = true;
1670 begin
1671 FillChar(EvHash, Sizeof(EvHash), 0);
1672 EvType := M.ReadByte();
1673 EvNum := M.ReadLongInt();
1674 EvStr := M.ReadString();
1675 gLastMap := M.ReadByte() <> 0;
1676 if gLastMap and (gGameSettings.GameMode = GM_COOP) then gStatsOff := True;
1677 gStatsPressed := True;
1678 EvTime := M.ReadLongWord();
1679 BHash := M.ReadByte() <> 0;
1680 if BHash then
1681 EvHash := M.ReadMD5();
1683 gTime := EvTime;
1685 if (g_Res_received_map_start <> 0) then
1686 begin
1687 if (g_Res_received_map_start < 0) then exit;
1688 goodCmd := false;
1689 case EvType of
1690 NET_EV_MAPSTART: goodCmd := true;
1691 NET_EV_MAPEND: goodCmd := true;
1692 NET_EV_PLAYER_KICK: goodCmd := true;
1693 NET_EV_PLAYER_BAN: goodCmd := true;
1694 end;
1695 if not goodCmd then exit;
1696 end;
1698 case EvType of
1699 NET_EV_MAPSTART:
1700 begin
1701 if (g_Res_received_map_start <> 0) then
1702 begin
1703 g_Res_received_map_start := -1;
1704 end
1705 else
1706 begin
1707 gGameOn := False;
1708 g_Game_ClearLoading();
1709 g_Game_StopAllSounds(True);
1711 gSwitchGameMode := Byte(EvNum);
1712 gGameSettings.GameMode := gSwitchGameMode;
1714 gWADHash := EvHash;
1715 if not g_Game_StartMap(EvStr, True) then
1716 begin
1717 if not isWadPath(EvStr) then
1718 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [gGameSettings.WAD + ':\' + EvStr]))
1719 else
1720 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [EvStr]));
1721 Exit;
1722 end;
1724 MC_SEND_FullStateRequest;
1725 end;
1726 end;
1728 NET_EV_MAPEND:
1729 begin
1730 if (g_Res_received_map_start <> 0) then
1731 begin
1732 g_Res_received_map_start := -1;
1733 end
1734 else
1735 begin
1736 gMissionFailed := EvNum <> 0;
1737 gExit := EXIT_ENDLEVELCUSTOM;
1738 end;
1739 end;
1741 NET_EV_RCON:
1742 begin
1743 case EvNum of
1744 NET_RCON_NOAUTH:
1745 g_Console_Add(_lc[I_NET_RCON_NOAUTH], True);
1746 NET_RCON_PWGOOD:
1747 g_Console_Add(_lc[I_NET_RCON_PWD_VALID], True);
1748 NET_RCON_PWBAD:
1749 g_Console_Add(_lc[I_NET_RCON_PWD_INVALID], True);
1750 end;
1751 end;
1753 NET_EV_CHANGE_TEAM:
1754 begin
1755 if EvNum = TEAM_RED then
1756 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [EvStr]), True);
1757 if EvNum = TEAM_BLUE then
1758 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [EvStr]), True);
1759 end;
1761 NET_EV_PLAYER_KICK:
1762 begin
1763 g_Console_Add(Format(_lc[I_PLAYER_KICK], [EvStr]), True);
1764 if (g_Res_received_map_start <> 0) then g_Res_received_map_start := -1;
1765 end;
1767 NET_EV_PLAYER_BAN:
1768 begin
1769 g_Console_Add(Format(_lc[I_PLAYER_BAN], [EvStr]), True);
1770 if (g_Res_received_map_start <> 0) then g_Res_received_map_start := -1;
1771 end;
1773 NET_EV_LMS_WARMUP:
1774 g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [EvNum]), True);
1776 NET_EV_LMS_SURVIVOR:
1777 g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True);
1779 NET_EV_BIGTEXT:
1780 g_Game_Message(AnsiUpperCase(EvStr), Word(EvNum));
1782 NET_EV_SCORE:
1783 begin
1784 pl := g_Player_Get(EvNum and $FFFF);
1785 if pl = nil then
1786 pln := '?'
1787 else
1788 pln := pl.Name;
1789 cnt := (EvNum shr 16) and $FF;
1790 if Pos('w', EvStr) = 0 then
1791 begin
1792 // Default score
1793 if Pos('t', EvStr) = 0 then
1794 begin
1795 // Player +/- score
1796 if Pos('-', EvStr) = 0 then
1797 begin
1798 if Pos('e', EvStr) = 0 then
1799 i1 := I_PLAYER_SCORE_ADD_OWN
1800 else
1801 i1 := I_PLAYER_SCORE_ADD_ENEMY;
1802 end else
1803 begin
1804 if Pos('e', EvStr) = 0 then
1805 i1 := I_PLAYER_SCORE_SUB_OWN
1806 else
1807 i1 := I_PLAYER_SCORE_SUB_ENEMY;
1808 end;
1809 // Which team
1810 if Pos('r', EvStr) > 0 then
1811 i2 := I_PLAYER_SCORE_TO_RED
1812 else
1813 i2 := I_PLAYER_SCORE_TO_BLUE;
1814 g_Console_Add(Format(_lc[i1], [pln, cnt, _lc[i2]]), True);
1815 end else
1816 begin
1817 // Team +/- score
1818 if Pos('-', EvStr) = 0 then
1819 i1 := I_PLAYER_SCORE_ADD_TEAM
1820 else
1821 i1 := I_PLAYER_SCORE_SUB_TEAM;
1822 // Which team
1823 if Pos('r', EvStr) > 0 then
1824 i2 := I_PLAYER_SCORE_RED
1825 else
1826 i2 := I_PLAYER_SCORE_BLUE;
1827 g_Console_Add(Format(_lc[i1], [_lc[i2], cnt]), True);
1828 end;
1829 end else
1830 begin
1831 // Game Win
1832 if Pos('e', EvStr) = 0 then
1833 i1 := I_PLAYER_SCORE_WIN_OWN
1834 else
1835 i1 := I_PLAYER_SCORE_WIN_ENEMY;
1836 // Which team
1837 if Pos('r', EvStr) > 0 then
1838 i2 := I_PLAYER_SCORE_TO_RED
1839 else
1840 i2 := I_PLAYER_SCORE_TO_BLUE;
1841 g_Console_Add(Format(_lc[i1], [pln, _lc[i2]]), True);
1842 end;
1843 end;
1845 NET_EV_SCORE_MSG:
1846 begin
1847 if EvNum = TEAM_RED then
1848 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1849 if EvNum = TEAM_BLUE then
1850 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1851 if EvNum = -TEAM_RED then
1852 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1853 if EvNum = -TEAM_BLUE then
1854 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1855 end;
1857 NET_EV_LMS_START:
1858 begin
1859 g_Player_RemoveAllCorpses;
1860 g_Game_Message(_lc[I_MESSAGE_LMS_START], 144);
1861 end;
1863 NET_EV_LMS_WIN:
1864 g_Game_Message(Format(_lc[I_MESSAGE_LMS_WIN], [AnsiUpperCase(EvStr)]), 144);
1866 NET_EV_TLMS_WIN:
1867 begin
1868 if EvNum = TEAM_RED then
1869 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 144);
1870 if EvNum = TEAM_BLUE then
1871 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 144);
1872 end;
1874 NET_EV_LMS_LOSE:
1875 g_Game_Message(_lc[I_MESSAGE_LMS_LOSE], 144);
1877 NET_EV_LMS_DRAW:
1878 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
1880 NET_EV_KILLCOMBO:
1881 g_Game_Announce_KillCombo(EvNum);
1883 NET_EV_PLAYER_TOUCH:
1884 begin
1885 pl := g_Player_Get(EvNum);
1886 if pl <> nil then
1887 pl.Touch();
1888 end;
1890 NET_EV_SECRET:
1891 begin
1892 pl := g_Player_Get(EvNum);
1893 if pl <> nil then
1894 begin
1895 g_Console_Add(Format(_lc[I_PLAYER_SECRET], [pl.Name]), True);
1896 g_Sound_PlayEx('SOUND_GAME_SECRET');
1897 end;
1898 end;
1900 NET_EV_INTER_READY:
1901 begin
1902 pl := g_Player_Get(EvNum);
1903 if pl <> nil then pl.FReady := (EvStr = 'Y');
1904 end;
1905 end;
1906 end;
1908 procedure MC_RECV_FlagEvent(var M: TMsg);
1909 var
1910 PID: Word;
1911 Pl: TPlayer;
1912 EvType: Byte;
1913 Fl, a: Byte;
1914 Quiet: Boolean;
1915 s, ts: string;
1916 begin
1917 EvType := M.ReadByte();
1918 Fl := M.ReadByte();
1920 if Fl = FLAG_NONE then Exit;
1922 Quiet := (M.ReadByte() <> 0);
1923 PID := M.ReadWord();
1925 gFlags[Fl].State := M.ReadByte();
1926 gFlags[Fl].CaptureTime := M.ReadLongWord();
1927 gFlags[Fl].Obj.X := M.ReadLongInt();
1928 gFlags[Fl].Obj.Y := M.ReadLongInt();
1929 gFlags[Fl].Obj.Vel.X := M.ReadLongInt();
1930 gFlags[Fl].Obj.Vel.Y := M.ReadLongInt();
1932 Pl := g_Player_Get(PID);
1933 if (Pl = nil) and
1934 (EvType <> FLAG_STATE_NORMAL) and
1935 (EvType <> FLAG_STATE_DROPPED) and
1936 (EvType <> FLAG_STATE_RETURNED) then
1937 Exit;
1939 case EvType of
1940 FLAG_STATE_NORMAL:
1941 begin
1942 if Quiet or (Pl = nil) then Exit;
1944 if Fl = FLAG_RED then
1945 s := _lc[I_PLAYER_FLAG_RED]
1946 else
1947 s := _lc[I_PLAYER_FLAG_BLUE];
1949 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1951 if ((Pl = gPlayer1) or (Pl = gPlayer2)
1952 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
1953 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
1954 a := 0
1955 else
1956 a := 1;
1958 if not sound_ret_flag[a].IsPlaying() then
1959 sound_ret_flag[a].Play();
1960 end;
1962 FLAG_STATE_CAPTURED:
1963 begin
1964 if (Pl <> nil) then Pl.SetFlag(Fl);
1966 if Quiet then Exit;
1968 if Fl = FLAG_RED then
1969 s := _lc[I_PLAYER_FLAG_RED]
1970 else
1971 s := _lc[I_PLAYER_FLAG_BLUE];
1973 g_Console_Add(Format(_lc[I_PLAYER_FLAG_GET], [Pl.Name, s]), True);
1974 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_GET], [AnsiUpperCase(s)]), 144);
1976 if ((Pl = gPlayer1) or (Pl = gPlayer2)
1977 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
1978 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
1979 a := 0
1980 else
1981 a := 1;
1983 if not sound_get_flag[a].IsPlaying() then
1984 sound_get_flag[a].Play();
1985 end;
1987 FLAG_STATE_DROPPED:
1988 begin
1989 if (Pl <> nil) then Pl.SetFlag(FLAG_NONE);
1991 if Quiet or (Pl = nil) then Exit;
1993 if Fl = FLAG_RED then
1994 s := _lc[I_PLAYER_FLAG_RED]
1995 else
1996 s := _lc[I_PLAYER_FLAG_BLUE];
1998 g_Console_Add(Format(_lc[I_PLAYER_FLAG_DROP], [Pl.Name, s]), True);
1999 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_DROP], [AnsiUpperCase(s)]), 144);
2001 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2002 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2003 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2004 a := 0
2005 else
2006 a := 1;
2008 if not sound_lost_flag[a].IsPlaying() then
2009 sound_lost_flag[a].Play();
2010 end;
2012 FLAG_STATE_SCORED:
2013 begin
2014 g_Map_ResetFlag(FLAG_RED);
2015 g_Map_ResetFlag(FLAG_BLUE);
2016 if Quiet or (Pl = nil) then Exit;
2017 Pl.SetFlag(FLAG_NONE);
2019 if Fl = FLAG_RED then
2020 s := _lc[I_PLAYER_FLAG_RED]
2021 else
2022 s := _lc[I_PLAYER_FLAG_BLUE];
2024 ts := Format('%.4d', [gFlags[Fl].CaptureTime]);
2025 Insert('.', ts, Length(ts) + 1 - 3);
2026 g_Console_Add(Format(_lc[I_PLAYER_FLAG_CAPTURE], [Pl.Name, s, ts]), True);
2027 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_CAPTURE], [AnsiUpperCase(s)]), 144);
2029 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2030 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2031 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2032 a := 0
2033 else
2034 a := 1;
2036 if not sound_cap_flag[a].IsPlaying() then
2037 sound_cap_flag[a].Play();
2038 end;
2040 FLAG_STATE_RETURNED:
2041 begin
2042 g_Map_ResetFlag(Fl);
2043 if Quiet then Exit;
2045 if Fl = FLAG_RED then
2046 s := _lc[I_PLAYER_FLAG_RED]
2047 else
2048 s := _lc[I_PLAYER_FLAG_BLUE];
2050 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2052 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2053 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2054 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2055 a := 0
2056 else
2057 a := 1;
2059 if not sound_ret_flag[a].IsPlaying() then
2060 sound_ret_flag[a].Play();
2061 end;
2062 end;
2063 end;
2065 procedure MC_RECV_GameSettings(var M: TMsg);
2066 begin
2067 gGameSettings.GameMode := M.ReadByte();
2068 gGameSettings.GoalLimit := M.ReadWord();
2069 gGameSettings.TimeLimit := M.ReadWord();
2070 gGameSettings.MaxLives := M.ReadByte();
2071 gGameSettings.Options := M.ReadLongWord();
2072 end;
2074 // PLAYER
2076 function MC_RECV_PlayerCreate(var M: TMsg): Word;
2077 var
2078 PID, DID: Word;
2079 PName, Model: string;
2080 Color: TRGB;
2081 T: Byte;
2082 Pl: TPlayer;
2083 begin
2084 PID := M.ReadWord();
2085 Pl := g_Player_Get(PID);
2087 PName := M.ReadString();
2088 Model := M.ReadString();
2089 Color.R := M.ReadByte();
2090 Color.G := M.ReadByte();
2091 Color.B := M.ReadByte();
2092 T := M.ReadByte();
2094 Result := 0;
2095 if (PID <> NetPlrUID1) and (PID <> NetPlrUID2) then
2096 begin
2097 if (Pl <> nil) then Exit;
2098 DID := g_Player_Create(Model, Color, T, False);
2099 with g_Player_Get(DID) do
2100 begin
2101 UID := PID;
2102 Name := PName;
2103 Reset(True);
2104 end;
2105 end
2106 else
2107 begin
2108 if (PID = NetPlrUID1) and (gPlayer1 <> nil) then begin
2109 gPlayer1.UID := PID;
2110 gPlayer1.Model.SetColor(Color.R, Color.G, Color.B);
2111 gPlayer1.ChangeTeam(T);
2112 end;
2113 if (PID = NetPlrUID2) and (gPlayer2 <> nil) then begin
2114 gPlayer2.UID := PID;
2115 gPlayer2.Model.SetColor(Color.R, Color.G, Color.B);
2116 gPlayer2.ChangeTeam(T);
2117 end;
2118 end;
2120 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
2121 e_WriteLog('NET: Player ' + PName + ' [' + IntToStr(PID) + '] added.', TMsgType.Notify);
2122 Result := PID;
2123 end;
2125 function MC_RECV_PlayerPos(var M: TMsg): Word;
2126 var
2127 GT: LongWord;
2128 PID: Word;
2129 kByte: Word;
2130 Pl: TPlayer;
2131 Dir: Byte;
2132 TmpX, TmpY: Integer;
2133 begin
2134 Result := 0;
2136 GT := M.ReadLongWord();
2137 if GT < gTime - NET_MAX_DIFFTIME then
2138 begin
2139 gTime := GT;
2140 Exit;
2141 end;
2142 gTime := GT;
2144 PID := M.ReadWord();
2145 Pl := g_Player_Get(PID);
2147 if Pl = nil then Exit;
2149 Result := PID;
2151 with Pl do
2152 begin
2153 FPing := M.ReadWord();
2154 FLoss := M.ReadByte();
2155 kByte := M.ReadWord();
2156 Dir := M.ReadByte();
2158 TmpX := M.ReadLongInt();
2159 TmpY := M.ReadLongInt();
2161 ReleaseKeys;
2163 if (kByte = NET_KEY_CHAT) then
2164 PressKey(KEY_CHAT, 10000)
2165 else
2166 begin
2167 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
2168 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
2169 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
2170 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
2171 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
2172 end;
2174 if ((Pl <> gPlayer1) and (Pl <> gPlayer2)) or LongBool(kByte and NET_KEY_FORCEDIR) then
2175 SetDirection(TDirection(Dir));
2177 GameVelX := M.ReadLongInt();
2178 GameVelY := M.ReadLongInt();
2179 GameAccelX := M.ReadLongInt();
2180 GameAccelY := M.ReadLongInt();
2181 SetLerp(TmpX, TmpY);
2182 if NetForcePlayerUpdate then Update();
2183 end;
2184 end;
2186 function MC_RECV_PlayerStats(var M: TMsg): Word;
2187 var
2188 PID: Word;
2189 Pl: TPlayer;
2190 I, OldFire: Integer;
2191 OldJet, Flam: Boolean;
2192 NewTeam: Byte;
2193 begin
2194 PID := M.ReadWord();
2195 Pl := g_Player_Get(PID);
2196 Result := 0;
2197 if Pl = nil then
2198 Exit;
2200 with Pl do
2201 begin
2202 alive := (M.ReadByte() <> 0);
2203 GodMode := (M.ReadByte() <> 0);
2204 Health := M.ReadLongInt();
2205 Armor := M.ReadLongInt();
2206 Air := M.ReadLongInt();
2207 JetFuel := M.ReadLongInt();
2208 Lives := M.ReadByte();
2209 NewTeam := M.ReadByte();
2211 for I := WP_FIRST to WP_LAST do
2212 FWeapon[I] := (M.ReadByte() <> 0);
2214 for I := A_BULLETS to A_HIGH do
2215 FAmmo[I] := M.ReadWord();
2217 for I := A_BULLETS to A_HIGH do
2218 FMaxAmmo[I] := M.ReadWord();
2220 for I := MR_SUIT to MR_MAX do
2221 FMegaRulez[I] := M.ReadLongWord();
2223 FRulez := [];
2224 if (M.ReadByte() <> 0) then
2225 FRulez := FRulez + [R_ITEM_BACKPACK];
2226 if (M.ReadByte() <> 0) then
2227 FRulez := FRulez + [R_KEY_RED];
2228 if (M.ReadByte() <> 0) then
2229 FRulez := FRulez + [R_KEY_GREEN];
2230 if (M.ReadByte() <> 0) then
2231 FRulez := FRulez + [R_KEY_BLUE];
2232 if (M.ReadByte() <> 0) then
2233 FRulez := FRulez + [R_BERSERK];
2235 Frags := M.ReadLongInt();
2236 Death := M.ReadLongInt();
2238 SetWeapon(M.ReadByte());
2240 FSpectator := M.ReadByte() <> 0;
2241 if FSpectator then
2242 begin
2243 if Pl = gPlayer1 then
2244 begin
2245 gLMSPID1 := UID;
2246 gPlayer1 := nil;
2247 end;
2248 if Pl = gPlayer2 then
2249 begin
2250 gLMSPID2 := UID;
2251 gPlayer2 := nil;
2252 end;
2253 end
2254 else
2255 begin
2256 if (gPlayer1 = nil) and (gLMSPID1 > 0) then
2257 gPlayer1 := g_Player_Get(gLMSPID1);
2258 if (gPlayer2 = nil) and (gLMSPID2 > 0) then
2259 gPlayer2 := g_Player_Get(gLMSPID2);
2260 end;
2261 FGhost := M.ReadByte() <> 0;
2262 FPhysics := M.ReadByte() <> 0;
2263 FNoRespawn := M.ReadByte() <> 0;
2264 OldJet := FJetpack;
2265 FJetpack := M.ReadByte() <> 0;
2266 OldFire := FFireTime;
2267 FFireTime := M.ReadLongInt();
2268 if (OldFire <= 0) and (FFireTime > 0) then
2269 g_Sound_PlayExAt('SOUND_IGNITE', Obj.X, Obj.Y);
2270 Flam := M.ReadByte() <> 0;
2271 if OldJet and not FJetpack then
2272 JetpackOff
2273 else if not OldJet and FJetpack then
2274 JetpackOn;
2275 if FFlaming and not Flam then
2276 FlamerOff;
2277 if Team <> NewTeam then
2278 Pl.ChangeTeam(NewTeam);
2279 end;
2281 Result := PID;
2282 end;
2284 function MC_RECV_PlayerDamage(var M: TMsg): Word;
2285 var
2286 PID: Word;
2287 Pl: TPlayer;
2288 Kind: Byte;
2289 Attacker, Value: Word;
2290 VX, VY: Integer;
2291 begin
2292 Result := 0;
2293 if not gGameOn then Exit;
2294 PID := M.ReadWord();
2295 Pl := g_Player_Get(PID);
2296 if Pl = nil then Exit;
2298 Kind := M.ReadByte();
2299 Attacker := M.ReadWord();
2300 Value := M.ReadWord();
2301 VX := M.ReadWord();
2302 VY := M.ReadWord();
2304 with Pl do
2305 Damage(Value, Attacker, VX, VY, Kind);
2307 Result := PID;
2308 end;
2310 function MC_RECV_PlayerDeath(var M: TMsg): Word;
2311 var
2312 PID: Word;
2313 Pl: TPlayer;
2314 KillType, DeathType: Byte;
2315 Attacker: Word;
2316 begin
2317 Result := 0;
2318 if not gGameOn then Exit;
2319 PID := M.ReadWord();
2320 Pl := g_Player_Get(PID);
2321 if Pl = nil then Exit;
2323 KillType := M.ReadByte();
2324 DeathType := M.ReadByte();
2325 Attacker := M.ReadWord();
2327 with Pl do
2328 begin
2329 Kill(KillType, Attacker, DeathType);
2330 SoftReset;
2331 end;
2332 end;
2334 function MC_RECV_PlayerDelete(var M: TMsg): Word;
2335 var
2336 PID: Word;
2337 Pl: TPlayer;
2338 begin
2339 PID := M.ReadWord();
2340 Pl := g_Player_Get(PID);
2341 Result := 0;
2342 if Pl = nil then Exit;
2344 g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [Pl.Name]), True);
2345 e_WriteLog('NET: Player ' + Pl.Name + ' [' + IntToStr(PID) + '] removed.', TMsgType.Notify);
2347 g_Player_Remove(PID);
2349 Result := PID;
2350 end;
2352 function MC_RECV_PlayerFire(var M: TMsg): Word;
2353 var
2354 PID: Word;
2355 Weap: Byte;
2356 Pl: TPlayer;
2357 X, Y, AX, AY: Integer;
2358 SHID: Integer;
2359 begin
2360 Result := 0;
2361 if not gGameOn then Exit;
2362 PID := M.ReadWord();
2363 Pl := g_Player_Get(PID);
2364 if Pl = nil then Exit;
2366 Weap := M.ReadByte();
2367 X := M.ReadLongInt();
2368 Y := M.ReadLongInt();
2369 AX := M.ReadLongInt();
2370 AY := M.ReadLongInt();
2371 SHID := M.ReadLongInt();
2373 with Pl do
2374 if alive then NetFire(Weap, X, Y, AX, AY, SHID);
2375 end;
2377 procedure MC_RECV_PlayerSettings(var M: TMsg);
2378 var
2379 TmpName: string;
2380 TmpModel: string;
2381 TmpColor: TRGB;
2382 TmpTeam: Byte;
2383 Pl: TPlayer;
2384 PID: Word;
2385 begin
2386 PID := M.ReadWord();
2387 Pl := g_Player_Get(PID);
2388 if Pl = nil then Exit;
2390 TmpName := M.ReadString();
2391 TmpModel := M.ReadString();
2392 TmpColor.R := M.ReadByte();
2393 TmpColor.G := M.ReadByte();
2394 TmpColor.B := M.ReadByte();
2395 TmpTeam := M.ReadByte();
2397 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
2398 begin
2399 Pl.ChangeTeam(TmpTeam);
2400 if gPlayer1 = Pl then
2401 gPlayer1Settings.Team := TmpTeam;
2402 if gPlayer2 = Pl then
2403 gPlayer2Settings.Team := TmpTeam;
2404 end else
2405 Pl.SetColor(TmpColor);
2407 if Pl.Name <> TmpName then
2408 begin
2409 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
2410 Pl.Name := TmpName;
2411 end;
2413 if TmpModel <> Pl.Model.Name then
2414 Pl.SetModel(TmpModel);
2415 end;
2417 // ITEM
2419 procedure MC_RECV_ItemSpawn(var M: TMsg);
2420 var
2421 ID: Word;
2422 AID: DWord;
2423 X, Y, VX, VY: Integer;
2424 T: Byte;
2425 Quiet, Fall{, Resp}: Boolean;
2426 Anim: TAnimation;
2427 it: PItem;
2428 begin
2429 if not gGameOn then Exit;
2430 ID := M.ReadWord();
2431 Quiet := M.ReadByte() <> 0;
2432 T := M.ReadByte();
2433 Fall := M.ReadByte() <> 0;
2434 {Resp :=} M.ReadByte();
2435 X := M.ReadLongInt();
2436 Y := M.ReadLongInt();
2437 VX := M.ReadLongInt();
2438 VY := M.ReadLongInt();
2440 g_Items_Create(X, Y, T and $7F, Fall, False, False, ID);
2441 if ((T and $80) <> 0) then g_Items_SetDrop(ID);
2443 it := g_Items_ByIdx(ID);
2444 it.Obj.Vel.X := VX;
2445 it.Obj.Vel.Y := VY;
2447 if not Quiet then
2448 begin
2449 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
2450 if g_Frames_Get(AID, 'FRAMES_ITEM_RESPAWN') then
2451 begin
2452 Anim := TAnimation.Create(AID, False, 4);
2453 g_GFX_OnceAnim(X+(it.Obj.Rect.Width div 2)-16, Y+(it.Obj.Rect.Height div 2)-16, Anim);
2454 Anim.Free();
2455 end;
2456 end;
2457 end;
2459 procedure MC_RECV_ItemDestroy(var M: TMsg);
2460 var
2461 ID: Word;
2462 Quiet: Boolean;
2463 begin
2464 if not gGameOn then Exit;
2465 ID := M.ReadWord();
2466 Quiet := M.ReadByte() <> 0;
2468 if not g_Items_ValidId(ID) then exit;
2470 if not Quiet then g_Items_EmitPickupSound(ID);
2472 g_Items_Remove(ID);
2473 end;
2475 // PANEL
2477 procedure MC_RECV_PanelTexture(var M: TMsg);
2478 var
2479 TP: TPanel;
2480 PGUID: Integer;
2481 Tex, Fr: Integer;
2482 Loop, Cnt: Byte;
2483 begin
2484 if not gGameOn then Exit;
2486 PGUID := Integer(M.ReadLongWord());
2487 Tex := M.ReadLongInt();
2488 Fr := M.ReadLongInt();
2489 Cnt := M.ReadByte();
2490 Loop := M.ReadByte();
2492 TP := g_Map_PanelByGUID(PGUID);
2493 if (TP <> nil) then
2494 begin
2495 // switch texture
2496 TP.SetTexture(Tex, Loop);
2497 TP.SetFrame(Fr, Cnt);
2498 end;
2499 end;
2501 procedure MC_RECV_PanelState(var M: TMsg);
2502 var
2503 PGUID: Integer;
2504 E: Boolean;
2505 Lift: Byte;
2506 X, Y, W, H: Integer;
2507 TP: TPanel;
2508 speedX, speedY, startX, startY, endX, endY: Integer;
2509 sizeSpX, sizeSpY, sizeEX, sizeEY: Integer;
2510 mpflags: Byte;
2511 begin
2512 if not gGameOn then Exit;
2514 PGUID := Integer(M.ReadLongWord());
2515 E := (M.ReadByte() <> 0);
2516 Lift := M.ReadByte();
2517 X := M.ReadLongInt();
2518 Y := M.ReadLongInt();
2519 W := M.ReadWord();
2520 H := M.ReadWord();
2521 // mplats
2522 speedX := M.ReadLongInt();
2523 speedY := M.ReadLongInt();
2524 startX := M.ReadLongInt();
2525 startY := M.ReadLongInt();
2526 endX := M.ReadLongInt();
2527 endY := M.ReadLongInt();
2528 sizeSpX := M.ReadLongInt();
2529 sizeSpY := M.ReadLongInt();
2530 sizeEX := M.ReadLongInt();
2531 sizeEY := M.ReadLongInt();
2532 mpflags := M.ReadByte(); // bit0: TP.movingActive; bit1: TP.moveOnce
2534 TP := g_Map_PanelByGUID(PGUID);
2535 if (TP = nil) then exit;
2537 // update lifts state
2538 if TP.isGLift then g_Map_SetLiftGUID(PGUID, Lift);
2540 // update enabled/disabled state for all panels
2541 if E then g_Map_EnableWallGUID(PGUID) else g_Map_DisableWallGUID(PGUID);
2543 // update panel position, as it can be moved (mplat)
2544 TP.X := X;
2545 TP.Y := Y;
2546 TP.Width := W;
2547 TP.Height := H;
2548 // update mplat state
2549 TP.movingSpeedX := speedX;
2550 TP.movingSpeedY := speedY;
2551 TP.movingStartX := startX;
2552 TP.movingStartY := startY;
2553 TP.movingEndX := endX;
2554 TP.movingEndY := endY;
2555 TP.sizeSpeedX := sizeSpX;
2556 TP.sizeSpeedY := sizeSpY;
2557 TP.sizeEndX := sizeEX;
2558 TP.sizeEndY := sizeEY;
2559 TP.movingActive := ((mpflags and 1) <> 0);
2560 TP.moveOnce := ((mpflags and 2) <> 0);
2561 // notify panel of it's position/size change, so it can fix other internal structures
2562 TP.positionChanged();
2563 end;
2565 // TRIGGERS
2567 procedure MC_RECV_TriggerSound(var M: TMsg);
2568 var
2569 SPlaying: Boolean;
2570 SPos, SID: LongWord;
2571 SCount: LongInt;
2572 I: Integer;
2573 begin
2574 if not gGameOn then Exit;
2575 if gTriggers = nil then Exit;
2577 SID := M.ReadLongWord();
2578 SPlaying := M.ReadByte() <> 0;
2579 SPos := M.ReadLongWord();
2580 SCount := M.ReadLongInt();
2582 for I := Low(gTriggers) to High(gTriggers) do
2583 if gTriggers[I].TriggerType = TRIGGER_SOUND then
2584 if gTriggers[I].ClientID = SID then
2585 with gTriggers[I] do
2586 begin
2587 if Sound <> nil then
2588 begin
2589 if SPlaying then
2590 begin
2591 if tgcLocal then
2592 Sound.PlayVolumeAt(X+(Width div 2), Y+(Height div 2), tgcVolume/255.0)
2593 else
2594 Sound.PlayPanVolume((tgcPan-127.0)/128.0, tgcVolume/255.0);
2595 Sound.SetPosition(SPos);
2596 end
2597 else
2598 if Sound.IsPlaying then Sound.Stop;
2599 end;
2601 SoundPlayCount := SCount;
2602 end;
2603 end;
2605 procedure MC_RECV_TriggerMusic(var M: TMsg);
2606 var
2607 MName: string;
2608 MPlaying: Boolean;
2609 MPos: LongWord;
2610 MPaused: Boolean;
2611 begin
2612 if not gGameOn then Exit;
2614 MName := M.ReadString();
2615 MPlaying := M.ReadByte() <> 0;
2616 MPos := M.ReadLongWord();
2617 MPaused := M.ReadByte() <> 0;
2618 MPos := MPos+1; //k8: stfu, fpc!
2620 if MPlaying then
2621 begin
2622 gMusic.SetByName(MName);
2623 gMusic.Play(True);
2624 // gMusic.SetPosition(MPos);
2625 gMusic.SpecPause := MPaused;
2626 end
2627 else
2628 if gMusic.IsPlaying then gMusic.Stop;
2629 end;
2631 // MONSTERS
2633 procedure MC_RECV_MonsterSpawn(var M: TMsg);
2634 var
2635 ID: Word;
2636 MType, MState, MDir, MAnim, MBehav: Byte;
2637 X, Y, VX, VY, MTargTime, MHealth, MAmmo, MSleep: Integer;
2638 MTarg: Word;
2639 Mon: TMonster;
2640 begin
2641 ID := M.ReadWord();
2642 Mon := g_Monsters_ByUID(ID);
2643 if Mon <> nil then
2644 Exit;
2646 MType := M.ReadByte();
2647 MState := M.ReadByte();
2648 MAnim := M.ReadByte();
2649 MTarg := M.ReadWord();
2650 MTargTime := M.ReadLongInt();
2651 MBehav := M.ReadByte();
2652 MSleep := M.ReadLongInt();
2653 MHealth := M.ReadLongInt();
2654 MAmmo := M.ReadLongInt();
2656 X := M.ReadLongInt();
2657 Y := M.ReadLongInt();
2658 VX := M.ReadLongInt();
2659 VY := M.ReadLongInt();
2660 MDir := M.ReadByte();
2662 g_Monsters_Create(MType, X, Y, TDirection(MDir), False, ID);
2663 Mon := g_Monsters_ByUID(ID);
2664 if Mon = nil then
2665 Exit;
2667 with Mon do
2668 begin
2670 MonsterAnim := MAnim;
2671 MonsterTargetUID := MTarg;
2672 MonsterTargetTime := MTargTime;
2673 MonsterBehaviour := MBehav;
2674 MonsterSleep := MSleep;
2675 MonsterAmmo := MAmmo;
2676 SetHealth(MHealth);
2678 SetState(MState);
2680 setPosition(X, Y); // this will call positionChanged();
2681 GameVelX := VX;
2682 GameVelY := VY;
2683 end;
2684 end;
2686 procedure MC_RECV_MonsterPos(var M: TMsg);
2687 var
2688 Mon: TMonster;
2689 ID: Word;
2690 X, Y: Integer;
2691 begin
2692 ID := M.ReadWord();
2693 Mon := g_Monsters_ByUID(ID);
2694 if Mon = nil then
2695 Exit;
2697 with Mon do
2698 begin
2699 X := M.ReadLongInt();
2700 Y := M.ReadLongInt();
2701 Mon.setPosition(X, Y); // this will call `positionChanged()`
2702 GameVelX := M.ReadLongInt();
2703 GameVelY := M.ReadLongInt();
2704 GameDirection := TDirection(M.ReadByte());
2705 end;
2706 end;
2708 procedure MC_RECV_MonsterState(var M: TMsg);
2709 var
2710 ID, OldFire: Integer;
2711 MState, MFAnm: Byte;
2712 Mon: TMonster;
2713 AnimRevert: Boolean;
2714 begin
2715 ID := M.ReadWord();
2716 Mon := g_Monsters_ByUID(ID);
2717 if Mon = nil then Exit;
2719 MState := M.ReadByte();
2720 MFAnm := M.ReadByte();
2722 with Mon do
2723 begin
2724 MonsterTargetUID := M.ReadWord();
2725 MonsterTargetTime := M.ReadLongInt();
2726 MonsterSleep := M.ReadLongInt();
2727 MonsterHealth := M.ReadLongInt();
2728 MonsterAmmo := M.ReadLongInt();
2729 MonsterPain := M.ReadLongInt();
2730 AnimRevert := M.ReadByte() <> 0;
2731 OldFire := FFireTime;
2732 FFireTime := M.ReadLongInt();
2733 if (OldFire <= 0) and (FFireTime > 0) then
2734 g_Sound_PlayExAt('SOUND_IGNITE', Obj.X, Obj.Y);
2735 RevertAnim(AnimRevert);
2737 if MonsterState <> MState then
2738 begin
2739 if (MState = MONSTATE_GO) and (MonsterState = MONSTATE_SLEEP) then WakeUpSound();
2740 if (MState = MONSTATE_DIE) then DieSound();
2741 if (MState = MONSTATE_PAIN) then MakeBloodSimple(Min(200, MonsterPain));
2742 if (MState = MONSTATE_ATTACK) then kick(nil);
2743 if (MState = MONSTATE_DEAD) then SetDeadAnim();
2745 SetState(MState, MFAnm);
2746 end;
2747 end;
2748 end;
2750 procedure MC_RECV_MonsterShot(var M: TMsg);
2751 var
2752 ID: Integer;
2753 Mon: TMonster;
2754 X, Y, VX, VY: Integer;
2755 begin
2756 ID := M.ReadWord();
2758 Mon := g_Monsters_ByUID(ID);
2759 if Mon = nil then Exit;
2761 X := M.ReadLongInt();
2762 Y := M.ReadLongInt();
2763 VX := M.ReadLongInt();
2764 VY := M.ReadLongInt();
2766 Mon.ClientAttack(X, Y, VX, VY);
2767 end;
2769 procedure MC_RECV_MonsterDelete(var M: TMsg);
2770 var
2771 ID: Integer;
2772 Mon: TMonster;
2773 begin
2774 ID := M.ReadWord();
2775 Mon := g_Monsters_ByUID(ID);
2776 if Mon = nil then Exit;
2777 Mon.SetState(5);
2778 Mon.MonsterRemoved := True;
2779 end;
2781 procedure MC_RECV_TimeSync(var M: TMsg);
2782 var
2783 Time: LongWord;
2784 begin
2785 Time := M.ReadLongWord();
2787 if gState = STATE_INTERCUSTOM then
2788 gServInterTime := Min(Time, 255);
2789 end;
2791 procedure MC_RECV_VoteEvent(var M: TMsg);
2792 var
2793 EvID: Byte;
2794 Str1, Str2: string;
2795 Int1, Int2: SmallInt;
2796 begin
2797 EvID := M.ReadByte();
2798 Int1 := M.ReadSmallInt();
2799 Int2 := M.ReadSmallInt();
2800 Str1 := M.ReadString();
2801 Str2 := M.ReadString();
2803 case EvID of
2804 NET_VE_STARTED:
2805 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_STARTED], [Str1, Str2, Int1]), True);
2806 NET_VE_PASSED:
2807 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_PASSED], [Str1]), True);
2808 NET_VE_FAILED:
2809 g_Console_Add(_lc[I_MESSAGE_VOTE_FAILED], True);
2810 NET_VE_VOTE:
2811 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Str1, Int1, Int2]), True);
2812 NET_VE_INPROGRESS:
2813 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_INPROGRESS], [Str1]), True);
2814 end;
2815 end;
2817 // CLIENT SEND
2819 procedure MC_SEND_Info(Password: string);
2820 begin
2821 NetOut.Clear();
2823 NetOut.Write(Byte(NET_MSG_INFO));
2824 NetOut.Write(GAME_VERSION);
2825 NetOut.Write(Password);
2826 NetOut.Write(gPlayer1Settings.Name);
2827 NetOut.Write(gPlayer1Settings.Model);
2828 NetOut.Write(gPlayer1Settings.Color.R);
2829 NetOut.Write(gPlayer1Settings.Color.G);
2830 NetOut.Write(gPlayer1Settings.Color.B);
2831 NetOut.Write(gPlayer1Settings.Team);
2833 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2834 end;
2836 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
2837 begin
2838 NetOut.Write(Byte(NET_MSG_CHAT));
2839 NetOut.Write(Txt);
2840 NetOut.Write(Mode);
2842 g_Net_Client_Send(True, NET_CHAN_CHAT);
2843 end;
2845 procedure MC_SEND_PlayerPos();
2846 var
2847 kByte: Word;
2848 Predict: Boolean;
2849 strafeDir: Byte;
2850 WeaponSelect: Word = 0;
2851 i: Integer;
2852 begin
2853 if not gGameOn then Exit;
2854 if gPlayers = nil then Exit;
2855 if gPlayer1 = nil then Exit;
2857 kByte := 0;
2858 Predict := NetPredictSelf; // and (not NetGotKeys);
2860 if (not gConsoleShow) and (not gChatShow) and (g_ActiveWindow = nil) then
2861 begin
2862 strafeDir := P1MoveButton shr 4;
2863 P1MoveButton := P1MoveButton and $0F;
2865 if gPlayerAction[0, ACTION_MOVELEFT] and (not gPlayerAction[0, ACTION_MOVERIGHT]) then
2866 P1MoveButton := 1
2867 else if (not gPlayerAction[0, ACTION_MOVELEFT]) and gPlayerAction[0, ACTION_MOVERIGHT] then
2868 P1MoveButton := 2
2869 else if (not gPlayerAction[0, ACTION_MOVELEFT]) and (not gPlayerAction[0, ACTION_MOVERIGHT]) then
2870 P1MoveButton := 0;
2872 // strafing
2873 if gPlayerAction[0, ACTION_STRAFE] then
2874 begin
2875 // new strafe mechanics
2876 if (strafeDir = 0) then
2877 strafeDir := P1MoveButton; // start strafing
2878 // now set direction according to strafe (reversed)
2879 if (strafeDir = 2) then
2880 gPlayer1.SetDirection(TDirection.D_LEFT)
2881 else if (strafeDir = 1) then
2882 gPlayer1.SetDirection(TDirection.D_RIGHT)
2883 end
2884 else
2885 begin
2886 strafeDir := 0; // not strafing anymore
2887 if (P1MoveButton = 2) and gPlayerAction[0, ACTION_MOVELEFT] then
2888 gPlayer1.SetDirection(TDirection.D_LEFT)
2889 else if (P1MoveButton = 1) and gPlayerAction[0, ACTION_MOVERIGHT] then
2890 gPlayer1.SetDirection(TDirection.D_RIGHT)
2891 else if P1MoveButton <> 0 then
2892 gPlayer1.SetDirection(TDirection(P1MoveButton-1));
2893 end;
2895 gPlayer1.ReleaseKeys;
2896 if P1MoveButton = 1 then
2897 begin
2898 kByte := kByte or NET_KEY_LEFT;
2899 if Predict then gPlayer1.PressKey(KEY_LEFT, 10000);
2900 end;
2901 if P1MoveButton = 2 then
2902 begin
2903 kByte := kByte or NET_KEY_RIGHT;
2904 if Predict then gPlayer1.PressKey(KEY_RIGHT, 10000);
2905 end;
2906 if gPlayerAction[0, ACTION_LOOKUP] then
2907 begin
2908 kByte := kByte or NET_KEY_UP;
2909 gPlayer1.PressKey(KEY_UP, 10000);
2910 end;
2911 if gPlayerAction[0, ACTION_LOOKDOWN] then
2912 begin
2913 kByte := kByte or NET_KEY_DOWN;
2914 gPlayer1.PressKey(KEY_DOWN, 10000);
2915 end;
2916 if gPlayerAction[0, ACTION_JUMP] then
2917 begin
2918 kByte := kByte or NET_KEY_JUMP;
2919 // gPlayer1.PressKey(KEY_JUMP, 10000); // TODO: Make a prediction option
2920 end;
2921 if gPlayerAction[0, ACTION_ATTACK] then kByte := kByte or NET_KEY_FIRE;
2922 if gPlayerAction[0, ACTION_ACTIVATE] then kByte := kByte or NET_KEY_OPEN;
2923 if gPlayerAction[0, ACTION_WEAPNEXT] then kByte := kByte or NET_KEY_NW;
2924 if gPlayerAction[0, ACTION_WEAPPREV] then kByte := kByte or NET_KEY_PW;
2926 gPlayerAction[0, ACTION_WEAPNEXT] := False; // HACK, remove after readyweaon&pendinweapon implementation
2927 gPlayerAction[0, ACTION_WEAPPREV] := False; // HACK, remove after readyweaon&pendinweapon implementation
2929 for i := WP_FIRST to WP_LAST do
2930 begin
2931 if gSelectWeapon[0, i] then
2932 begin
2933 WeaponSelect := WeaponSelect or Word(1 shl i);
2934 gSelectWeapon[0, i] := False
2935 end
2936 end;
2938 // fix movebutton state
2939 P1MoveButton := P1MoveButton or (strafeDir shl 4);
2940 end
2941 else
2942 kByte := NET_KEY_CHAT;
2944 NetOut.Write(Byte(NET_MSG_PLRPOS));
2945 NetOut.Write(gTime);
2946 NetOut.Write(kByte);
2947 NetOut.Write(Byte(gPlayer1.Direction));
2948 NetOut.Write(WeaponSelect);
2949 //e_WriteLog(Format('S:ws=%d', [WeaponSelect]), MSG_WARNING);
2950 g_Net_Client_Send(True, NET_CHAN_PLAYERPOS);
2952 //kBytePrev := kByte;
2953 //kDirPrev := gPlayer1.Direction;
2954 end;
2956 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
2957 begin
2958 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
2959 NetOut.Write(Byte(Start));
2960 NetOut.Write(Command);
2961 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2962 end;
2964 procedure MC_SEND_PlayerSettings();
2965 begin
2966 NetOut.Write(Byte(NET_MSG_PLRSET));
2967 NetOut.Write(gPlayer1Settings.Name);
2968 NetOut.Write(gPlayer1Settings.Model);
2969 NetOut.Write(gPlayer1Settings.Color.R);
2970 NetOut.Write(gPlayer1Settings.Color.G);
2971 NetOut.Write(gPlayer1Settings.Color.B);
2972 NetOut.Write(gPlayer1Settings.Team);
2974 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2975 end;
2977 procedure MC_SEND_FullStateRequest();
2978 begin
2979 NetOut.Write(Byte(NET_MSG_REQFST));
2981 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2982 end;
2984 procedure MC_SEND_CheatRequest(Kind: Byte);
2985 begin
2986 NetOut.Write(Byte(NET_MSG_CHEAT));
2987 NetOut.Write(Kind);
2989 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2990 end;
2991 procedure MC_SEND_RCONPassword(Password: string);
2992 begin
2993 NetOut.Write(Byte(NET_MSG_RCON_AUTH));
2994 NetOut.Write(Password);
2996 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2997 end;
2998 procedure MC_SEND_RCONCommand(Cmd: string);
2999 begin
3000 NetOut.Write(Byte(NET_MSG_RCON_CMD));
3001 NetOut.Write(Cmd);
3003 g_Net_Client_Send(True, NET_CHAN_SERVICE);
3004 end;
3006 // i have no idea why all this stuff is in here
3008 function ReadFile(const FileName: TFileName): AByte;
3009 var
3010 FileStream : TStream;
3011 fname: string;
3012 begin
3013 e_WriteLog(Format('NETWORK: looking for file "%s"', [FileName]), TMsgType.Notify);
3014 fname := findDiskWad(FileName);
3015 if length(fname) = 0 then
3016 begin
3017 e_WriteLog(Format('NETWORK: file "%s" not found!', [FileName]), TMsgType.Fatal);
3018 SetLength(Result, 0);
3019 exit;
3020 end;
3021 e_WriteLog(Format('NETWORK: found file "%s"', [fname]), TMsgType.Notify);
3022 Result := nil;
3023 FileStream := openDiskFileRO(fname);
3024 try
3025 if FileStream.Size > 0 then
3026 begin
3027 SetLength(Result, FileStream.Size);
3028 FileStream.Read(Result[0], FileStream.Size);
3029 end;
3030 finally
3031 FileStream.Free;
3032 end;
3033 end;
3036 function CreateMapDataMsg(const FileName: TFileName; ResList: TStringList): TMapDataMsg;
3037 var
3038 i: Integer;
3039 begin
3040 Result.MsgId := NET_MSG_MAP_RESPONSE;
3041 Result.FileData := ReadFile(FileName);
3042 Result.FileSize := Length(Result.FileData);
3044 SetLength(Result.ExternalResources, ResList.Count);
3045 for i:=0 to ResList.Count-1 do
3046 begin
3047 Result.ExternalResources[i].Name := ResList.Strings[i];
3048 Result.ExternalResources[i].md5 := MD5File(GameDir+'/wads/'+ResList.Strings[i]);
3049 end;
3050 end;
3052 procedure ResDataMsgToBytes(var bytes: AByte; const ResData: TResDataMsg);
3053 var
3054 ResultStream: TMemoryStream;
3055 begin
3056 ResultStream := TMemoryStream.Create;
3058 ResultStream.WriteBuffer(ResData.MsgId, SizeOf(ResData.MsgId)); //msgId
3059 ResultStream.WriteBuffer(ResData.FileSize, SizeOf(ResData.FileSize)); //file size
3060 ResultStream.WriteBuffer(ResData.FileData[0], ResData.FileSize); //file data
3062 SetLength(bytes, ResultStream.Size);
3063 ResultStream.Seek(0, soFromBeginning);
3064 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
3066 ResultStream.Free;
3067 end;
3069 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
3070 begin
3071 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
3072 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize));
3073 SetLength(Result.FileData, Result.FileSize);
3074 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize);
3075 end;
3077 procedure MapDataMsgToBytes(var bytes: AByte; const MapDataMsg: TMapDataMsg);
3078 var
3079 ResultStream: TMemoryStream;
3080 resCount: Integer;
3081 begin
3082 resCount := Length(MapDataMsg.ExternalResources);
3084 ResultStream := TMemoryStream.Create;
3086 ResultStream.WriteBuffer(MapDataMsg.MsgId, SizeOf(MapDataMsg.MsgId)); //msgId
3087 ResultStream.WriteBuffer(MapDataMsg.FileSize, SizeOf(MapDataMsg.FileSize)); //file size
3088 ResultStream.WriteBuffer(MapDataMsg.FileData[0], MapDataMsg.FileSize); //file data
3090 ResultStream.WriteBuffer(resCount, SizeOf(resCount)); //res count
3091 ResultStream.WriteBuffer(MapDataMsg.ExternalResources[0], resCount*SizeOf(TExternalResourceInfo)); //res data
3093 SetLength(bytes, ResultStream.Size);
3094 ResultStream.Seek(0, soFromBeginning);
3095 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
3097 ResultStream.Free;
3098 end;
3100 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
3101 var
3102 resCount: Integer;
3103 begin
3104 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
3105 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize)); //file size
3107 SetLength(Result.FileData, Result.FileSize);
3108 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize); //file data
3110 msgStream.ReadBuffer(resCount, SizeOf(resCount)); //res count
3111 SetLength(Result.ExternalResources, resCount);
3113 msgStream.ReadBuffer(Result.ExternalResources[0], resCount * SizeOf(TExternalResourceInfo)); //res data
3114 end;
3117 function IsValidFileName(const S: String): Boolean;
3118 const
3119 Forbidden: set of Char = ['<', '>', '|', '"', ':', '*', '?'];
3120 var
3121 I: Integer;
3122 begin
3123 Result := S <> '';
3124 for I := 1 to Length(S) do
3125 Result := Result and (not(S[I] in Forbidden));
3126 end;
3128 function IsValidFilePath(const S: String): Boolean;
3129 var
3130 I: Integer;
3131 begin
3132 Result := False;
3133 if not IsValidFileName(S) then exit;
3134 if FileExists(S) then exit;
3135 I := LastDelimiter('\/', S);
3136 if (I > 0) then
3137 if (not DirectoryExists(Copy(S, 1, I-1))) then
3138 exit;
3139 Result := True;
3140 end;
3143 procedure MC_SEND_MapRequest();
3144 begin
3145 NetOut.Write(Byte(NET_MSG_MAP_REQUEST));
3146 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3147 end;
3149 procedure MC_SEND_ResRequest(const resName: AnsiString);
3150 begin
3151 NetOut.Write(Byte(NET_MSG_RES_REQUEST));
3152 NetOut.Write(resName);
3153 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3154 end;
3156 procedure MH_RECV_MapRequest(C: pTNetClient; var M: TMsg);
3157 var
3158 peer: pENetPeer;
3159 payload: AByte;
3160 mapDataMsg: TMapDataMsg;
3161 begin
3162 e_WriteLog('NET: Received map request from ' +
3163 DecodeIPV4(C^.Peer.address.host), TMsgType.Notify);
3165 mapDataMsg := CreateMapDataMsg(MapsDir + gGameSettings.WAD, gExternalResources);
3166 peer := NetClients[C^.ID].Peer;
3168 MapDataMsgToBytes(payload, mapDataMsg);
3169 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3171 payload := nil;
3172 mapDataMsg.FileData := nil;
3173 mapDataMsg.ExternalResources := nil;
3174 end;
3176 procedure MH_RECV_ResRequest(C: pTNetClient; var M: TMsg);
3177 var
3178 payload: AByte;
3179 peer: pENetPeer;
3180 FileName: String;
3181 resDataMsg: TResDataMsg;
3182 begin
3183 FileName := ExtractFileName(M.ReadString());
3184 e_WriteLog('NET: Received res request: ' + FileName +
3185 ' from ' + DecodeIPV4(C^.Peer.address.host), TMsgType.Notify);
3187 if not IsValidFilePath(FileName) then
3188 begin
3189 e_WriteLog('Invalid filename: ' + FileName, TMsgType.Warning);
3190 exit;
3191 end;
3193 peer := NetClients[C^.ID].Peer;
3195 if gExternalResources.IndexOf(FileName) > -1 then
3196 begin
3197 resDataMsg.MsgId := NET_MSG_RES_RESPONSE;
3198 resDataMsg.FileData := ReadFile(GameDir+'/wads/'+FileName);
3199 resDataMsg.FileSize := Length(resDataMsg.FileData);
3201 ResDataMsgToBytes(payload, resDataMsg);
3202 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3203 end;
3204 end;
3208 end.