DEADSOFTWARE

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