DEADSOFTWARE

network code for mplats: looks like it works; see commit comments for some more info
[d2df-sdl.git] / src / game / g_netmsg.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_netmsg;
19 interface
21 uses e_msg, g_net, g_triggers, Classes, SysUtils, md5;
23 const
24 NET_MSG_INFO = 100;
26 NET_MSG_CHAT = 101;
27 NET_MSG_SND = 102;
28 NET_MSG_GFX = 103;
29 NET_MSG_GEVENT = 104;
30 NET_MSG_SCORE = 105;
31 NET_MSG_COOP = 106;
32 NET_MSG_FLAG = 107;
33 NET_MSG_REQFST = 108;
34 NET_MSG_GSET = 109;
36 NET_MSG_PLR = 111;
37 NET_MSG_PLRPOS = 112;
38 NET_MSG_PLRSTA = 113;
39 NET_MSG_PLRDEL = 114;
40 NET_MSG_PLRDMG = 115;
41 NET_MSG_PLRDIE = 116;
42 NET_MSG_PLRFIRE= 117;
43 NET_MSG_PLRSET = 119;
44 NET_MSG_CHEAT = 120;
46 NET_MSG_ISPAWN = 121;
47 NET_MSG_IDEL = 122;
49 NET_MSG_MSPAWN = 131;
50 NET_MSG_MPOS = 132;
51 NET_MSG_MSTATE = 133;
52 NET_MSG_MSHOT = 134;
53 NET_MSG_MDEL = 135;
55 NET_MSG_PSTATE = 141;
56 NET_MSG_PTEX = 142;
58 NET_MSG_TSOUND = 151;
59 NET_MSG_TMUSIC = 152;
61 NET_MSG_SHDEL = 161;
62 NET_MSG_SHADD = 162;
63 NET_MSG_SHPOS = 163;
65 NET_MSG_RCON_AUTH = 191;
66 NET_MSG_RCON_CMD = 192;
67 NET_MSG_TIME_SYNC = 194;
68 NET_MSG_VOTE_EVENT = 195;
70 NET_MSG_MAP_REQUEST = 201;
71 NET_MSG_MAP_RESPONSE = 202;
72 NET_MSG_RES_REQUEST = 203;
73 NET_MSG_RES_RESPONSE = 204;
75 NET_CHAT_SYSTEM = 0;
76 NET_CHAT_PLAYER = 1;
77 NET_CHAT_TEAM = 2;
79 NET_RCON_NOAUTH = 0;
80 NET_RCON_PWGOOD = 1;
81 NET_RCON_PWBAD = 2;
83 NET_GFX_SPARK = 1;
84 NET_GFX_TELE = 2;
85 NET_GFX_RESPAWN = 3;
86 NET_GFX_FIRE = 4;
87 NET_GFX_EXPLODE = 5;
88 NET_GFX_BFGEXPL = 6;
89 NET_GFX_BFGHIT = 7;
90 NET_GFX_SHELL1 = 8;
91 NET_GFX_SHELL2 = 9;
92 NET_GFX_SHELL3 = 10;
94 NET_EV_MAPSTART = 1;
95 NET_EV_MAPEND = 2;
96 NET_EV_CHANGE_TEAM = 3;
97 NET_EV_PLAYER_KICK = 4;
98 NET_EV_PLAYER_BAN = 5;
99 NET_EV_LMS_WARMUP = 6;
100 NET_EV_LMS_SURVIVOR = 7;
101 NET_EV_RCON = 8;
102 NET_EV_BIGTEXT = 9;
103 NET_EV_SCORE = 10;
104 NET_EV_SCORE_MSG = 11;
105 NET_EV_LMS_START = 12;
106 NET_EV_LMS_WIN = 13;
107 NET_EV_TLMS_WIN = 14;
108 NET_EV_LMS_LOSE = 15;
109 NET_EV_LMS_DRAW = 16;
110 NET_EV_KILLCOMBO = 17;
111 NET_EV_PLAYER_TOUCH = 18;
113 NET_VE_STARTED = 1;
114 NET_VE_PASSED = 2;
115 NET_VE_FAILED = 3;
116 NET_VE_VOTE = 4;
117 NET_VE_REVOKE = 5;
118 NET_VE_INPROGRESS = 6;
120 NET_FLAG_GET = 1;
121 NET_FLAG_DROP = 2;
122 NET_FLAG_CAP = 3;
123 NET_FLAG_RETURN = 4;
125 NET_CHEAT_SUICIDE = 1;
126 NET_CHEAT_SPECTATE = 2;
128 NET_MAX_DIFFTIME = 5000 div 36;
130 // HOST MESSAGES
132 procedure MH_RECV_Info(C: pTNetClient; var M: TMsg);
133 procedure MH_RECV_Chat(C: pTNetClient; var M: TMsg);
134 procedure MH_RECV_FullStateRequest(C: pTNetClient; var M: TMsg);
135 function MH_RECV_PlayerPos(C: pTNetClient; var M: TMsg): Word;
136 procedure MH_RECV_PlayerSettings(C: pTNetClient; var M: TMsg);
137 procedure MH_RECV_CheatRequest(C: pTNetClient; var M: TMsg);
138 procedure MH_RECV_RCONPassword(C: pTNetClient; var M: TMsg);
139 procedure MH_RECV_RCONCommand(C: pTNetClient; var M: TMsg);
140 procedure MH_RECV_MapRequest(C: pTNetClient; var M: TMsg);
141 procedure MH_RECV_ResRequest(C: pTNetClient; var M: TMsg);
142 procedure MH_RECV_Vote(C: pTNetClient; var M: TMsg);
144 // GAME
145 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
146 procedure MH_SEND_Info(ID: Byte);
147 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
148 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
149 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
150 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
151 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
152 procedure MH_SEND_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
153 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
154 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
155 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
156 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
157 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
158 // PLAYER
159 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
160 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
161 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
162 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
163 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
164 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
165 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
166 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
167 // ITEM
168 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
169 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
170 // PANEL
171 procedure MH_SEND_PanelTexture(PType: Word; PGUID: Integer; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
172 procedure MH_SEND_PanelState(PType: Word; PGUID: Integer; ID: Integer = NET_EVERYONE);
173 // MONSTER
174 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
175 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
176 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
177 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
178 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
179 // TRIGGER
180 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
181 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
182 // MISC
183 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
184 procedure MH_SEND_VoteEvent(EvType: Byte;
185 StrArg1: string = 'a'; StrArg2: string = 'b';
186 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
187 ID: Integer = NET_EVERYONE);
189 // CLIENT MESSAGES //
191 // GAME
192 procedure MC_RECV_Chat(var M: TMsg);
193 procedure MC_RECV_Effect(var M: TMsg);
194 procedure MC_RECV_Sound(var M: TMsg);
195 procedure MC_RECV_GameStats(var M: TMsg);
196 procedure MC_RECV_CoopStats(var M: TMsg);
197 procedure MC_RECV_GameEvent(var M: TMsg);
198 procedure MC_RECV_FlagEvent(var M: TMsg);
199 procedure MC_RECV_GameSettings(var M: TMsg);
200 // PLAYER
201 function MC_RECV_PlayerCreate(var M: TMsg): Word;
202 function MC_RECV_PlayerPos(var M: TMsg): Word;
203 function MC_RECV_PlayerStats(var M: TMsg): Word;
204 function MC_RECV_PlayerDelete(var M: TMsg): Word;
205 function MC_RECV_PlayerDamage(var M: TMsg): Word;
206 function MC_RECV_PlayerDeath(var M: TMsg): Word;
207 function MC_RECV_PlayerFire(var M: TMsg): Word;
208 procedure MC_RECV_PlayerSettings(var M: TMsg);
209 // ITEM
210 procedure MC_RECV_ItemSpawn(var M: TMsg);
211 procedure MC_RECV_ItemDestroy(var M: TMsg);
212 // PANEL
213 procedure MC_RECV_PanelTexture(var M: TMsg);
214 procedure MC_RECV_PanelState(var M: TMsg);
215 // MONSTER
216 procedure MC_RECV_MonsterSpawn(var M: TMsg);
217 procedure MC_RECV_MonsterPos(var M: TMsg);
218 procedure MC_RECV_MonsterState(var M: TMsg);
219 procedure MC_RECV_MonsterShot(var M: TMsg);
220 procedure MC_RECV_MonsterDelete(var M: TMsg);
221 // SHOT
222 procedure MC_RECV_CreateShot(var M: TMsg);
223 procedure MC_RECV_UpdateShot(var M: TMsg);
224 procedure MC_RECV_DeleteShot(var M: TMsg);
225 // TRIGGER
226 procedure MC_RECV_TriggerSound(var M: TMsg);
227 procedure MC_RECV_TriggerMusic(var M: TMsg);
228 // MISC
229 procedure MC_RECV_TimeSync(var M: TMsg);
230 procedure MC_RECV_VoteEvent(var M: TMsg);
231 // SERVICE
232 procedure MC_SEND_Info(Password: string);
233 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
234 procedure MC_SEND_PlayerPos();
235 procedure MC_SEND_FullStateRequest();
236 procedure MC_SEND_PlayerSettings();
237 procedure MC_SEND_CheatRequest(Kind: Byte);
238 procedure MC_SEND_RCONPassword(Password: string);
239 procedure MC_SEND_RCONCommand(Cmd: string);
240 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
241 // DOWNLOAD
242 procedure MC_SEND_MapRequest();
243 procedure MC_SEND_ResRequest(const resName: AnsiString);
245 type
246 TExternalResourceInfo = record
247 Name: string[255];
248 md5: TMD5Digest;
249 end;
251 TResDataMsg = record
252 MsgId: Byte;
253 FileSize: Integer;
254 FileData: AByte;
255 end;
257 TMapDataMsg = record
258 MsgId: Byte;
259 FileSize: Integer;
260 FileData: AByte;
261 ExternalResources: array of TExternalResourceInfo;
262 end;
264 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
265 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
267 implementation
269 uses
270 Math, ENet, e_input, e_graphics, e_log,
271 g_textures, g_gfx, g_sound, g_console, g_basic, g_options, g_main,
272 g_game, g_player, g_map, g_panel, g_items, g_weapons, g_phys, g_gui,
273 g_language, g_monsters, g_netmaster, utils, wadreader, MAPDEF;
275 const
276 NET_KEY_LEFT = 1;
277 NET_KEY_RIGHT = 2;
278 NET_KEY_UP = 4;
279 NET_KEY_DOWN = 8;
280 NET_KEY_JUMP = 16;
281 NET_KEY_FIRE = 32;
282 NET_KEY_OPEN = 64;
283 NET_KEY_NW = 256;
284 NET_KEY_PW = 512;
285 NET_KEY_CHAT = 2048;
286 NET_KEY_FORCEDIR = 4096;
288 //var
289 //kBytePrev: Word = 0;
290 //kDirPrev: TDirection = D_LEFT;
291 //HostGameTime: Word = 0;
293 // HOST MESSAGES //
296 // GAME
298 procedure MH_RECV_Chat(C: pTNetClient; var M: TMsg);
299 var
300 Txt: string;
301 Mode: Byte;
302 PID: Word;
303 Pl: TPlayer;
304 begin
305 PID := C^.Player;
306 Pl := g_Player_Get(PID);
308 Txt := M.ReadString();
309 Mode := M.ReadByte();
310 if (Mode = NET_CHAT_SYSTEM) then
311 Mode := NET_CHAT_PLAYER; // prevent sending system messages from clients
312 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
313 Mode := NET_CHAT_PLAYER; // revert to player chat in non-team game
315 if Pl = nil then
316 MH_SEND_Chat(Txt, Mode)
317 else
318 MH_SEND_Chat(Pl.Name + ': ' + Txt, Mode, IfThen(Mode = NET_CHAT_TEAM, Pl.Team, NET_EVERYONE));
319 end;
321 procedure MH_RECV_Info(C: pTNetClient; var M: TMsg);
322 var
323 Ver, PName, Model, Pw: string;
324 R, G, B, T: Byte;
325 PID: Word;
326 Color: TRGB;
327 I: Integer;
328 begin
329 Ver := M.ReadString();
330 Pw := M.ReadString();
331 PName := M.ReadString();
332 Model := M.ReadString();
333 R := M.ReadByte();
334 G := M.ReadByte();
335 B := M.ReadByte();
336 T := M.ReadByte();
338 if Ver <> GAME_VERSION then
339 begin
340 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
341 _lc[I_NET_DISC_VERSION]);
342 enet_peer_disconnect(C^.Peer, NET_DISC_VERSION);
343 Exit;
344 end;
346 if g_Net_IsHostBanned(C^.Peer^.address.host) then
347 begin
348 if g_Net_IsHostBanned(C^.Peer^.address.host, True) then
349 begin
350 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
351 _lc[I_NET_DISC_BAN]);
352 enet_peer_disconnect(C^.Peer, NET_DISC_BAN);
353 end
354 else
355 begin
356 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
357 _lc[I_NET_DISC_BAN]);
358 enet_peer_disconnect(C^.Peer, NET_DISC_TEMPBAN);
359 end;
360 Exit;
361 end;
363 if NetPassword <> '' then
364 if AnsiLowerCase(NetPassword) <> AnsiLowerCase(Pw) then
365 begin
366 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
367 _lc[I_NET_DISC_PASSWORD]);
368 enet_peer_disconnect(C^.Peer, NET_DISC_PASSWORD);
369 Exit;
370 end;
372 Color.R := R;
373 Color.B := B;
374 Color.G := G;
376 PID := g_Player_Create(Model, Color, T, False);
377 with g_Player_Get(PID) do
378 begin
379 Name := PName;
380 Reset(True);
381 end;
383 C^.Player := PID;
385 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
386 e_WriteLog('NET: Client ' + PName + ' [' + IntToStr(C^.ID) +
387 '] connected. Assigned player #' + IntToStr(PID) + '.', MSG_NOTIFY);
389 MH_SEND_Info(C^.ID);
391 with g_Player_Get(PID) do
392 begin
393 Name := PName;
394 FClientID := C^.ID;
395 // round in progress, don't spawn
396 if (gGameSettings.MaxLives > 0) and (gLMSRespawn = LMS_RESPAWN_NONE) then
397 begin
398 Lives := 0;
399 FNoRespawn := True;
400 Spectate;
401 FWantsInGame := True; // TODO: look into this later
402 end
403 else
404 Respawn(gGameSettings.GameType = GT_SINGLE);
405 end;
407 for I := Low(NetClients) to High(NetClients) do
408 begin
409 if NetClients[I].ID = C^.ID then Continue;
410 MH_SEND_PlayerCreate(PID, NetClients[I].ID);
411 MH_SEND_PlayerPos(True, PID, NetClients[I].ID);
412 MH_SEND_PlayerStats(PID, NetClients[I].ID);
413 end;
415 if gState in [STATE_INTERCUSTOM, STATE_FOLD] then
416 MH_SEND_GameEvent(NET_EV_MAPEND, 0, 'N', C^.ID);
418 if NetUseMaster then g_Net_Slist_Update;
419 end;
421 procedure MH_RECV_FullStateRequest(C: pTNetClient; var M: TMsg);
422 begin
423 if gGameOn then
424 MH_SEND_Everything((C^.State = NET_STATE_AUTH), C^.ID)
425 else
426 C^.RequestedFullUpdate := True;
427 end;
429 // PLAYER
431 function MH_RECV_PlayerPos(C: pTNetClient; var M: TMsg): Word;
432 var
433 Dir, i: Byte;
434 WeaponSelect: Word;
435 PID: Word;
436 kByte: Word;
437 Pl: TPlayer;
438 GT: LongWord;
439 begin
440 Result := 0;
441 if not gGameOn then Exit;
443 GT := M.ReadLongWord();
444 PID := C^.Player;
445 Pl := g_Player_Get(PID);
446 if Pl = nil then
447 Exit;
449 if (GT > gTime + NET_MAX_DIFFTIME) or (GT < Pl.NetTime) then Exit;
451 with Pl do
452 begin
453 NetTime := GT;
454 kByte := M.ReadWord();
455 Dir := M.ReadByte();
456 WeaponSelect := M.ReadWord();
457 //e_WriteLog(Format('R:ws=%d', [WeaponSelect]), MSG_WARNING);
458 if Direction <> TDirection(Dir) then
459 JustTeleported := False;
461 SetDirection(TDirection(Dir));
462 ReleaseKeys;
464 if kByte = NET_KEY_CHAT then
465 begin
466 PressKey(KEY_CHAT, 10000);
467 Exit;
468 end;
470 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
471 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
472 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
473 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
474 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
475 if LongBool(kByte and NET_KEY_FIRE) then PressKey(KEY_FIRE, 10000);
476 if LongBool(kByte and NET_KEY_OPEN) then PressKey(KEY_OPEN, 10000);
477 if LongBool(kByte and NET_KEY_NW) then PressKey(KEY_NEXTWEAPON, 10000);
478 if LongBool(kByte and NET_KEY_PW) then PressKey(KEY_PREVWEAPON, 10000);
480 for i := 0 to 15 do
481 begin
482 if (WeaponSelect and Word(1 shl i)) <> 0 then
483 begin
484 //e_WriteLog(Format(' R:wn=%d', [i]), MSG_WARNING);
485 QueueWeaponSwitch(i);
486 end;
487 end;
488 end;
490 // MH_SEND_PlayerPos(False, PID, C^.ID);
491 end;
493 procedure MH_RECV_CheatRequest(C: pTNetClient; var M: TMsg);
494 var
495 CheatKind: Byte;
496 Pl: TPlayer;
497 begin
498 Pl := g_Player_Get(C^.Player);
499 if Pl = nil then Exit;
501 CheatKind := M.ReadByte();
503 case CheatKind of
504 NET_CHEAT_SUICIDE:
505 Pl.Damage(SUICIDE_DAMAGE, Pl.UID, 0, 0, HIT_SELF);
506 NET_CHEAT_SPECTATE:
507 begin
508 if Pl.FSpectator then
509 Pl.Respawn(False)
510 else
511 Pl.Spectate;
512 end;
513 end;
514 end;
516 procedure MH_RECV_PlayerSettings(C: pTNetClient; var M: TMsg);
517 var
518 TmpName: string;
519 TmpModel: string;
520 TmpColor: TRGB;
521 TmpTeam: Byte;
522 Pl: TPlayer;
523 begin
524 TmpName := M.ReadString();
525 TmpModel := M.ReadString();
526 TmpColor.R := M.ReadByte();
527 TmpColor.G := M.ReadByte();
528 TmpColor.B := M.ReadByte();
529 TmpTeam := M.ReadByte();
531 Pl := g_Player_Get(C^.Player);
532 if Pl = nil then Exit;
534 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
535 Pl.SwitchTeam
536 else
537 Pl.SetColor(TmpColor);
539 if Pl.Name <> TmpName then
540 begin
541 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
542 Pl.Name := TmpName;
543 end;
545 if TmpModel <> Pl.Model.Name then
546 Pl.SetModel(TmpModel);
548 MH_SEND_PlayerSettings(Pl.UID, TmpModel);
549 end;
551 // RCON
553 procedure MH_RECV_RCONPassword(C: pTNetClient; var M: TMsg);
554 var
555 Pwd: string;
556 begin
557 Pwd := M.ReadString();
558 if not NetAllowRCON then Exit;
559 if Pwd = NetRCONPassword then
560 begin
561 C^.RCONAuth := True;
562 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWGOOD, 'N', C^.ID);
563 end
564 else
565 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWBAD, 'N', C^.ID);
566 end;
568 procedure MH_RECV_RCONCommand(C: pTNetClient; var M: TMsg);
569 var
570 Cmd: string;
571 begin
572 Cmd := M.ReadString();
573 if not NetAllowRCON then Exit;
574 if not C^.RCONAuth then
575 begin
576 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_NOAUTH, 'N', C^.ID);
577 Exit;
578 end;
579 g_Console_Process(Cmd);
580 end;
582 // MISC
584 procedure MH_RECV_Vote(C: pTNetClient; var M: TMsg);
585 var
586 Start: Boolean;
587 Name, Command: string;
588 Need: Integer;
589 Pl: TPlayer;
590 begin
591 Start := M.ReadByte() <> 0;
592 Command := M.ReadString();
594 Pl := g_Player_Get(C^.Player);
595 if Pl = nil then Exit;
596 Name := Pl.Name;
598 if Start then
599 begin
600 if not g_Console_CommandBlacklisted(Command) then
601 g_Game_StartVote(Command, Name);
602 end
603 else if gVoteInProgress then
604 begin
605 if (gPlayer1 <> nil) or (gPlayer2 <> nil) then
606 Need := Floor((NetClientCount+1)/2.0) + 1
607 else
608 Need := Floor(NetClientCount/2.0) + 1;
609 if C^.Voted then
610 begin
611 Dec(gVoteCount);
612 C^.Voted := False;
613 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_REVOKED], [Name, gVoteCount, Need]), True);
614 MH_SEND_VoteEvent(NET_VE_REVOKE, Name, 'a', gVoteCount, Need);
615 end
616 else
617 begin
618 Inc(gVoteCount);
619 C^.Voted := True;
620 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Name, gVoteCount, Need]), True);
621 MH_SEND_VoteEvent(NET_VE_VOTE, Name, 'a', gVoteCount, Need);
622 g_Game_CheckVote;
623 end;
624 end;
625 end;
627 // GAME (SEND)
629 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
631 function sendItemRespawn (it: PItem): Boolean;
632 begin
633 result := false; // don't stop
634 MH_SEND_ItemSpawn(True, it.myid, ID);
635 end;
637 function sendMonSpawn (mon: TMonster): Boolean;
638 begin
639 result := false; // don't stop
640 MH_SEND_MonsterSpawn(mon.UID, ID);
641 end;
643 function sendPanelState (pan: TPanel): Boolean;
644 begin
645 result := false; // don't stop
646 MH_SEND_PanelState(pan.PanelType, pan.guid, ID); // anyway, to sync mplats
647 if (pan.GetTextureCount > 1) then MH_SEND_PanelTexture(pan.PanelType, pan.guid, pan.LastAnimLoop, ID);
648 end;
650 var
651 I: Integer;
652 begin
653 if gPlayers <> nil then
654 begin
655 for I := Low(gPlayers) to High(gPlayers) do
656 begin
657 if gPlayers[I] <> nil then
658 begin
659 if CreatePlayers then MH_SEND_PlayerCreate(gPlayers[I].UID, ID);
660 MH_SEND_PlayerPos(True, gPlayers[I].UID, ID);
661 MH_SEND_PlayerStats(gPlayers[I].UID, ID);
663 if (gPlayers[I].Flag <> FLAG_NONE) and (gGameSettings.GameMode = GM_CTF) then
664 begin
665 MH_SEND_FlagEvent(FLAG_STATE_CAPTURED, gPlayers[I].Flag, gPlayers[I].UID, True, ID);
666 end;
667 end;
668 end;
669 end;
671 g_Items_ForEachAlive(sendItemRespawn, true); // backwards
672 g_Mons_ForEach(sendMonSpawn);
674 g_Map_ForEachPanel(sendPanelState);
676 (* replaced with the `g_Map_ForEachPanel()` call above
677 if gWalls <> nil then
678 for I := Low(gWalls) to High(gWalls) do
679 if gWalls[I] <> nil then
680 with gWalls[I] do
681 begin
682 {if Door then} // anyway, to sync mplats
683 MH_SEND_PanelState(PanelType, I, ID);
685 if GetTextureCount > 1 then
686 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
687 end;
689 if gLifts <> nil then
690 for I := Low(gLifts) to High(gLifts) do
691 if gLifts[I] <> nil then
692 with gLifts[I] do
693 MH_SEND_PanelState(PanelType, I, ID);
695 if gRenderForegrounds <> nil then
696 for I := Low(gRenderForegrounds) to High(gRenderForegrounds) do
697 if gRenderForegrounds[I] <> nil then
698 with gRenderForegrounds[I] do
699 begin
700 if (GetTextureCount > 1) then
701 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
702 {if Moved then} // anyway, to sync mplats
703 MH_SEND_PanelState(PanelType, I, ID);
704 end;
705 if gRenderBackgrounds <> nil then
706 for I := Low(gRenderBackgrounds) to High(gRenderBackgrounds) do
707 if gRenderBackgrounds[I] <> nil then
708 with gRenderBackgrounds[I] do
709 begin
710 if (GetTextureCount > 1) then
711 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
712 {if Moved then} // anyway, to sync mplats
713 MH_SEND_PanelState(PanelType, I, ID);
714 end;
715 if gWater <> nil then
716 for I := Low(gWater) to High(gWater) do
717 if gWater[I] <> nil then
718 with gWater[I] do
719 begin
720 if GetTextureCount > 1 then
721 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
722 {if Moved then} // anyway, to sync mplats
723 MH_SEND_PanelState(PanelType, I, ID);
724 end;
725 if gAcid1 <> nil then
726 for I := Low(gAcid1) to High(gAcid1) do
727 if gAcid1[I] <> nil then
728 with gAcid1[I] do
729 begin
730 if GetTextureCount > 1 then
731 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
732 {if Moved then} // anyway, to sync mplats
733 MH_SEND_PanelState(PanelType, I, ID);
734 end;
735 if gAcid2 <> nil then
736 for I := Low(gAcid2) to High(gAcid2) do
737 if gAcid2[I] <> nil then
738 with gAcid2[I] do
739 begin
740 if GetTextureCount > 1 then
741 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
742 {if Moved then} // anyway, to sync mplats
743 MH_SEND_PanelState(PanelType, I, ID);
744 end;
745 if gSteps <> nil then
746 for I := Low(gSteps) to High(gSteps) do
747 if gSteps[I] <> nil then
748 with gSteps[I] do
749 begin
750 if GetTextureCount > 1 then
751 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
752 {if Moved then} // anyway, to sync mplats
753 MH_SEND_PanelState(PanelType, I, ID);
754 end;
755 *)
757 if gTriggers <> nil then
758 for I := Low(gTriggers) to High(gTriggers) do
759 if gTriggers[I].TriggerType = TRIGGER_SOUND then
760 MH_SEND_TriggerSound(gTriggers[I], ID);
762 if Shots <> nil then
763 for I := Low(Shots) to High(Shots) do
764 if Shots[i].ShotType in [6, 7, 8] then
765 MH_SEND_CreateShot(i, ID);
767 MH_SEND_TriggerMusic(ID);
769 MH_SEND_GameStats(ID);
770 MH_SEND_CoopStats(ID);
772 if gGameSettings.GameMode = GM_CTF then
773 begin
774 if gFlags[FLAG_RED].State <> FLAG_STATE_CAPTURED then
775 MH_SEND_FlagEvent(gFlags[FLAG_RED].State, FLAG_RED, 0, True, ID);
776 if gFlags[FLAG_BLUE].State <> FLAG_STATE_CAPTURED then
777 MH_SEND_FlagEvent(gFlags[FLAG_BLUE].State, FLAG_BLUE, 0, True, ID);
778 end;
780 if CreatePlayers and (ID >= 0) then NetClients[ID].State := NET_STATE_GAME;
782 if gLMSRespawn > LMS_RESPAWN_NONE then
783 MH_SEND_GameEvent(NET_EV_LMS_WARMUP, (gLMSRespawnTime - gTime) div 1000, 'N', ID);
784 end;
786 procedure MH_SEND_Info(ID: Byte);
787 var
788 Map: string;
789 begin
790 Map := g_ExtractFileName(gMapInfo.Map);
792 NetOut.Clear();
794 NetOut.Write(Byte(NET_MSG_INFO));
795 NetOut.Write(ID);
796 NetOut.Write(NetClients[ID].Player);
797 NetOut.Write(gGameSettings.WAD);
798 NetOut.Write(Map);
799 NetOut.Write(gWADHash);
800 NetOut.Write(gGameSettings.GameMode);
801 NetOut.Write(gGameSettings.GoalLimit);
802 NetOut.Write(gGameSettings.TimeLimit);
803 NetOut.Write(gGameSettings.MaxLives);
804 NetOut.Write(gGameSettings.Options);
805 NetOut.Write(gTime);
807 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
808 end;
810 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
811 var
812 Name: string;
813 i: Integer;
814 Team: Byte;
815 begin
816 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
817 Mode := NET_CHAT_PLAYER;
819 Team := 0;
820 if (Mode = NET_CHAT_TEAM) then
821 begin
822 for i := Low(gPlayers) to High(gPlayers) do
823 if (gPlayers[i] <> nil) and (gPlayers[i].FClientID >= 0) and
824 (gPlayers[i].Team = ID) then
825 begin
826 NetOut.Write(Byte(NET_MSG_CHAT));
827 NetOut.Write(Txt);
828 NetOut.Write(Mode);
829 g_Net_Host_Send(gPlayers[i].FClientID, True, NET_CHAN_CHAT);
830 end;
831 Team := ID;
832 ID := NET_EVERYONE;
833 end
834 else
835 begin
836 NetOut.Write(Byte(NET_MSG_CHAT));
837 NetOut.Write(Txt);
838 NetOut.Write(Mode);
839 g_Net_Host_Send(ID, True, NET_CHAN_CHAT);
840 end;
842 if Mode = NET_CHAT_SYSTEM then
843 Exit;
845 if ID = NET_EVERYONE then
846 begin
847 if Mode = NET_CHAT_PLAYER then
848 begin
849 g_Console_Add(Txt, True);
850 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
851 g_Sound_PlayEx('SOUND_GAME_RADIO');
852 end
853 else
854 if Mode = NET_CHAT_TEAM then
855 if gPlayer1 <> nil then
856 begin
857 if (gPlayer1.Team = TEAM_RED) and (Team = TEAM_RED) then
858 begin
859 g_Console_Add(#18'[Team] '#2 + Txt, True);
860 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
861 g_Sound_PlayEx('SOUND_GAME_RADIO');
862 end
863 else if (gPlayer1.Team = TEAM_BLUE) and (Team = TEAM_BLUE) then
864 begin
865 g_Console_Add(#20'[Team] '#2 + Txt, True);
866 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
867 g_Sound_PlayEx('SOUND_GAME_RADIO');
868 end;
869 end;
870 end
871 else
872 begin
873 Name := g_Net_ClientName_ByID(ID);
874 g_Console_Add('-> ' + Name + ': ' + Txt, True);
875 e_WriteLog('[Tell ' + Name + '] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
876 g_Sound_PlayEx('SOUND_GAME_RADIO');
877 end;
878 end;
880 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
881 begin
882 NetOut.Write(Byte(NET_MSG_GFX));
883 NetOut.Write(Kind);
884 NetOut.Write(X);
885 NetOut.Write(Y);
886 NetOut.Write(Ang);
888 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
889 end;
891 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
892 begin
893 NetOut.Write(Byte(NET_MSG_SND));
894 NetOut.Write(Name);
895 if Pos then
896 begin
897 NetOut.Write(Byte(1));
898 NetOut.Write(X);
899 NetOut.Write(Y);
900 end
901 else
902 NetOut.Write(Byte(0));
904 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
905 end;
907 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
908 begin
909 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
911 NetOut.Write(Byte(NET_MSG_SHADD));
912 NetOut.Write(Proj);
913 NetOut.Write(Shots[Proj].ShotType);
914 NetOut.Write(Shots[Proj].Target);
915 NetOut.Write(Shots[Proj].SpawnerUID);
916 NetOut.Write(Shots[Proj].Timeout);
917 NetOut.Write(Shots[Proj].Obj.X);
918 NetOut.Write(Shots[Proj].Obj.Y);
919 NetOut.Write(Shots[Proj].Obj.Vel.X);
920 NetOut.Write(Shots[Proj].Obj.Vel.Y);
922 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
923 end;
925 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
926 begin
927 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
929 NetOut.Write(Byte(NET_MSG_SHPOS));
930 NetOut.Write(Proj);
931 NetOut.Write(Shots[Proj].Obj.X);
932 NetOut.Write(Shots[Proj].Obj.Y);
933 NetOut.Write(Shots[Proj].Obj.Vel.X);
934 NetOut.Write(Shots[Proj].Obj.Vel.Y);
936 g_Net_Host_Send(ID, False, NET_CHAN_SHOTS);
937 end;
939 procedure MH_Send_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
940 begin
941 NetOut.Write(Byte(NET_MSG_SHDEL));
942 NetOut.Write(Proj);
943 NetOut.Write(Byte(Loud));
944 NetOut.Write(X);
945 NetOut.Write(Y);
947 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
948 end;
950 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
951 begin
952 NetOut.Write(Byte(NET_MSG_SCORE));
953 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
954 begin
955 NetOut.Write(gTeamStat[TEAM_RED].Goals);
956 NetOut.Write(gTeamStat[TEAM_BLUE].Goals);
957 end
958 else
959 if gGameSettings.GameMode = GM_COOP then
960 begin
961 NetOut.Write(gCoopMonstersKilled);
962 NetOut.Write(gCoopSecretsFound);
963 end;
965 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
966 end;
968 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
969 begin
970 NetOut.Write(Byte(NET_MSG_COOP));
971 NetOut.Write(gTotalMonsters);
972 NetOut.Write(gSecretsCount);
973 NetOut.Write(gCoopTotalMonstersKilled);
974 NetOut.Write(gCoopTotalSecretsFound);
975 NetOut.Write(gCoopTotalMonsters);
976 NetOut.Write(gCoopTotalSecrets);
977 end;
979 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
980 begin
981 NetOut.Write(Byte(NET_MSG_GEVENT));
982 NetOut.Write(EvType);
983 NetOut.Write(EvNum);
984 NetOut.Write(EvStr);
985 NetOut.Write(Byte(gLastMap));
986 NetOut.Write(gTime);
987 if (EvType = NET_EV_MAPSTART) and (Pos(':\', EvStr) > 0) then
988 begin
989 NetOut.Write(Byte(1));
990 NetOut.Write(gWADHash);
991 end else
992 NetOut.Write(Byte(0));
994 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
995 end;
997 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
998 begin
999 NetOut.Write(Byte(NET_MSG_FLAG));
1000 NetOut.Write(EvType);
1001 NetOut.Write(Flag);
1002 NetOut.Write(Byte(Quiet));
1003 NetOut.Write(PID);
1004 NetOut.Write(gFlags[Flag].State);
1005 NetOut.Write(gFlags[Flag].CaptureTime);
1006 NetOut.Write(gFlags[Flag].Obj.X);
1007 NetOut.Write(gFlags[Flag].Obj.Y);
1008 NetOut.Write(gFlags[Flag].Obj.Vel.X);
1009 NetOut.Write(gFlags[Flag].Obj.Vel.Y);
1011 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1012 end;
1014 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
1015 begin
1016 NetOut.Write(Byte(NET_MSG_GSET));
1017 NetOut.Write(gGameSettings.GameMode);
1018 NetOut.Write(gGameSettings.GoalLimit);
1019 NetOut.Write(gGameSettings.TimeLimit);
1020 NetOut.Write(gGameSettings.MaxLives);
1021 NetOut.Write(gGameSettings.Options);
1023 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1024 end;
1026 // PLAYER (SEND)
1028 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
1029 var
1030 P: TPlayer;
1031 begin
1032 P := g_Player_Get(PID);
1033 if P = nil then Exit;
1035 NetOut.Write(Byte(NET_MSG_PLR));
1036 NetOut.Write(PID);
1037 NetOut.Write(P.Name);
1039 NetOut.Write(P.FActualModelName);
1040 NetOut.Write(P.FColor.R);
1041 NetOut.Write(P.FColor.G);
1042 NetOut.Write(P.FColor.B);
1043 NetOut.Write(P.Team);
1045 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT)
1046 end;
1048 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
1049 var
1050 kByte: Word;
1051 Pl: TPlayer;
1052 begin
1053 Pl := g_Player_Get(PID);
1054 if Pl = nil then Exit;
1055 if Pl.FDummy then Exit;
1057 NetOut.Write(Byte(NET_MSG_PLRPOS));
1058 NetOut.Write(gTime);
1059 NetOut.Write(PID);
1061 kByte := 0;
1063 with Pl do
1064 begin
1065 NetOut.Write(FPing);
1066 NetOut.Write(FLoss);
1067 if IsKeyPressed(KEY_CHAT) then
1068 kByte := NET_KEY_CHAT
1069 else
1070 begin
1071 if IsKeyPressed(KEY_LEFT) then kByte := kByte or NET_KEY_LEFT;
1072 if IsKeyPressed(KEY_RIGHT) then kByte := kByte or NET_KEY_RIGHT;
1073 if IsKeyPressed(KEY_UP) then kByte := kByte or NET_KEY_UP;
1074 if IsKeyPressed(KEY_DOWN) then kByte := kByte or NET_KEY_DOWN;
1075 if IsKeyPressed(KEY_JUMP) then kByte := kByte or NET_KEY_JUMP;
1076 if JustTeleported then kByte := kByte or NET_KEY_FORCEDIR;
1077 end;
1079 NetOut.Write(kByte);
1080 if Direction = D_LEFT then NetOut.Write(Byte(0)) else NetOut.Write(Byte(1));
1081 NetOut.Write(GameX);
1082 NetOut.Write(GameY);
1083 NetOut.Write(GameVelX);
1084 NetOut.Write(GameVelY);
1085 NetOut.Write(GameAccelX);
1086 NetOut.Write(GameAccelY);
1087 end;
1089 g_Net_Host_Send(ID, Reliable, NET_CHAN_PLAYERPOS);
1090 end;
1092 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
1093 var
1094 P: TPlayer;
1095 I: Integer;
1096 begin
1097 P := g_Player_Get(PID);
1098 if P = nil then Exit;
1100 NetOut.Write(Byte(NET_MSG_PLRSTA));
1101 NetOut.Write(PID);
1103 with P do
1104 begin
1105 NetOut.Write(Byte(alive));
1106 NetOut.Write(Byte(GodMode));
1107 NetOut.Write(Health);
1108 NetOut.Write(Armor);
1109 NetOut.Write(Air);
1110 NetOut.Write(JetFuel);
1111 NetOut.Write(Lives);
1112 NetOut.Write(Team);
1114 for I := WP_FIRST to WP_LAST do
1115 NetOut.Write(Byte(FWeapon[I]));
1117 for I := A_BULLETS to A_HIGH do
1118 NetOut.Write(FAmmo[I]);
1120 for I := A_BULLETS to A_HIGH do
1121 NetOut.Write(FMaxAmmo[I]);
1123 for I := MR_SUIT to MR_MAX do
1124 NetOut.Write(LongWord(FMegaRulez[I]));
1126 NetOut.Write(Byte(R_ITEM_BACKPACK in FRulez));
1127 NetOut.Write(Byte(R_KEY_RED in FRulez));
1128 NetOut.Write(Byte(R_KEY_GREEN in FRulez));
1129 NetOut.Write(Byte(R_KEY_BLUE in FRulez));
1130 NetOut.Write(Byte(R_BERSERK in FRulez));
1132 NetOut.Write(Frags);
1133 NetOut.Write(Death);
1135 NetOut.Write(CurrWeap);
1137 NetOut.Write(Byte(FSpectator));
1138 NetOut.Write(Byte(FGhost));
1139 NetOut.Write(Byte(FPhysics));
1140 NetOut.Write(Byte(FNoRespawn));
1141 NetOut.Write(Byte(FJetpack));
1142 NetOut.Write(FFireTime);
1143 end;
1145 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1146 end;
1148 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
1149 begin
1150 NetOut.Write(Byte(NET_MSG_PLRDMG));
1151 NetOut.Write(PID);
1152 NetOut.Write(Kind);
1153 NetOut.Write(Attacker);
1154 NetOut.Write(Value);
1155 NetOut.Write(VX);
1156 NetOut.Write(VY);
1158 g_Net_Host_Send(ID, False, NET_CHAN_PLAYER);
1159 end;
1161 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
1162 begin
1163 NetOut.Write(Byte(NET_MSG_PLRDIE));
1164 NetOut.Write(PID);
1165 NetOut.Write(KillType);
1166 NetOut.Write(DeathType);
1167 NetOut.Write(Attacker);
1169 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1170 end;
1172 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
1173 begin
1174 NetOut.Write(Byte(NET_MSG_PLRFIRE));
1175 NetOut.Write(PID);
1176 NetOut.Write(Weapon);
1177 NetOut.Write(X);
1178 NetOut.Write(Y);
1179 NetOut.Write(AX);
1180 NetOut.Write(AY);
1181 NetOut.Write(ShotID);
1183 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
1184 end;
1186 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
1187 begin
1188 NetOut.Write(Byte(NET_MSG_PLRDEL));
1189 NetOut.Write(PID);
1191 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1192 end;
1194 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
1195 var
1196 Pl: TPlayer;
1197 begin
1198 Pl := g_Player_Get(PID);
1199 if Pl = nil then Exit;
1201 NetOut.Write(Byte(NET_MSG_PLRSET));
1202 NetOut.Write(PID);
1203 NetOut.Write(Pl.Name);
1204 if Mdl = '' then
1205 NetOut.Write(Pl.Model.Name)
1206 else
1207 NetOut.Write(Mdl);
1208 NetOut.Write(Pl.FColor.R);
1209 NetOut.Write(Pl.FColor.G);
1210 NetOut.Write(Pl.FColor.B);
1211 NetOut.Write(Pl.Team);
1213 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1214 end;
1216 // ITEM (SEND)
1218 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1219 var
1220 it: PItem;
1221 begin
1222 it := g_Items_ByIdx(IID);
1224 NetOut.Write(Byte(NET_MSG_ISPAWN));
1225 NetOut.Write(IID);
1226 NetOut.Write(Byte(Quiet));
1227 NetOut.Write(it.ItemType);
1228 NetOut.Write(Byte(it.Fall));
1229 NetOut.Write(Byte(it.Respawnable));
1230 NetOut.Write(it.Obj.X);
1231 NetOut.Write(it.Obj.Y);
1232 NetOut.Write(it.Obj.Vel.X);
1233 NetOut.Write(it.Obj.Vel.Y);
1235 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1236 end;
1238 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1239 begin
1240 NetOut.Write(Byte(NET_MSG_IDEL));
1241 NetOut.Write(IID);
1242 NetOut.Write(Byte(Quiet));
1244 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1245 end;
1247 // PANEL
1249 procedure MH_SEND_PanelTexture(PType: Word; PGUID: Integer; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
1250 var
1251 TP: TPanel;
1252 begin
1253 TP := g_Map_PanelByGUID(PGUID);
1254 if (TP = nil) then exit;
1256 case PType of
1257 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
1258 TP := gWalls[PID];
1259 PANEL_FORE:
1260 TP := gRenderForegrounds[PID];
1261 PANEL_BACK:
1262 TP := gRenderBackgrounds[PID];
1263 PANEL_WATER:
1264 TP := gWater[PID];
1265 PANEL_ACID1:
1266 TP := gAcid1[PID];
1267 PANEL_ACID2:
1268 TP := gAcid2[PID];
1269 PANEL_STEP:
1270 TP := gSteps[PID];
1271 else
1272 Exit;
1273 end;
1276 with TP do
1277 begin
1278 NetOut.Write(Byte(NET_MSG_PTEX));
1279 NetOut.Write(PType);
1280 NetOut.Write(LongWord(PGUID));
1281 NetOut.Write(FCurTexture);
1282 NetOut.Write(FCurFrame);
1283 NetOut.Write(FCurFrameCount);
1284 NetOut.Write(AnimLoop);
1285 end;
1287 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1288 end;
1290 procedure MH_SEND_PanelState(PType: Word; PGUID: Integer; ID: Integer = NET_EVERYONE);
1291 var
1292 TP: TPanel;
1293 mpflags: Byte = 0;
1294 begin
1295 TP := g_Map_PanelByGUID(PGUID);
1296 if (TP = nil) then exit;
1297 case PType of
1299 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR: TP := gWalls[PID];
1300 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT: TP := gLifts[PID];
1302 PANEL_BACK:
1303 begin
1304 //TP := gRenderBackgrounds[PID];
1305 TP.Moved := True;
1306 end;
1307 PANEL_FORE:
1308 begin
1309 //TP := gRenderForegrounds[PID];
1310 TP.Moved := True;
1311 end;
1313 else
1314 Exit;
1316 end;
1318 NetOut.Write(Byte(NET_MSG_PSTATE));
1319 NetOut.Write(PType);
1320 NetOut.Write(LongWord(PGUID));
1321 NetOut.Write(Byte(TP.Enabled));
1322 NetOut.Write(TP.LiftType);
1323 NetOut.Write(TP.X);
1324 NetOut.Write(TP.Y);
1325 NetOut.Write(Word(TP.Width));
1326 NetOut.Write(Word(TP.Height));
1327 // mplats
1328 NetOut.Write(LongInt(TP.movingSpeedX));
1329 NetOut.Write(LongInt(TP.movingSpeedY));
1330 NetOut.Write(LongInt(TP.movingStartX));
1331 NetOut.Write(LongInt(TP.movingStartY));
1332 NetOut.Write(LongInt(TP.movingEndX));
1333 NetOut.Write(LongInt(TP.movingEndY));
1334 NetOut.Write(LongInt(TP.sizeSpeedX));
1335 NetOut.Write(LongInt(TP.sizeSpeedY));
1336 NetOut.Write(LongInt(TP.sizeEndX));
1337 NetOut.Write(LongInt(TP.sizeEndY));
1338 if TP.movingActive then mpflags := mpflags or 1;
1339 if TP.moveOnce then mpflags := mpflags or 2;
1340 NetOut.Write(Byte(mpflags));
1342 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1343 end;
1345 // TRIGGER
1347 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
1348 begin
1349 if gTriggers = nil then Exit;
1350 if T.Sound = nil then Exit;
1352 NetOut.Write(Byte(NET_MSG_TSOUND));
1353 NetOut.Write(T.ClientID);
1354 NetOut.Write(Byte(T.Sound.IsPlaying));
1355 NetOut.Write(LongWord(T.Sound.GetPosition));
1356 NetOut.Write(T.SoundPlayCount);
1358 g_Net_Host_Send(ID, True);
1359 end;
1361 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
1362 begin
1363 NetOut.Write(Byte(NET_MSG_TMUSIC));
1364 NetOut.Write(gMusic.Name);
1365 NetOut.Write(Byte(gMusic.IsPlaying));
1366 NetOut.Write(LongWord(gMusic.GetPosition));
1367 NetOut.Write(Byte(gMusic.SpecPause or gMusic.IsPaused));
1369 g_Net_Host_Send(ID, True);
1370 end;
1372 // MONSTER
1374 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
1375 var
1376 M: TMonster;
1377 begin
1378 M := g_Monsters_ByUID(UID);
1379 if M = nil then
1380 Exit;
1382 with M do
1383 begin
1384 NetOut.Write(Byte(NET_MSG_MSPAWN));
1385 NetOut.Write(UID);
1386 NetOut.Write(MonsterType);
1387 NetOut.Write(MonsterState);
1388 NetOut.Write(MonsterAnim);
1389 NetOut.Write(MonsterTargetUID);
1390 NetOut.Write(MonsterTargetTime);
1391 NetOut.Write(MonsterBehaviour);
1392 NetOut.Write(MonsterSleep);
1393 NetOut.Write(MonsterHealth);
1394 NetOut.Write(MonsterAmmo);
1395 NetOut.Write(GameX);
1396 NetOut.Write(GameY);
1397 NetOut.Write(GameVelX);
1398 NetOut.Write(GameVelY);
1399 NetOut.Write(Byte(GameDirection));
1400 end;
1402 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1403 end;
1405 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
1406 var
1407 M: TMonster;
1408 begin
1409 M := g_Monsters_ByUID(UID);
1410 if M = nil then Exit;
1412 NetOut.Write(Byte(NET_MSG_MPOS));
1413 NetOut.Write(UID);
1415 with M do
1416 begin
1417 NetOut.Write(GameX);
1418 NetOut.Write(GameY);
1419 NetOut.Write(GameVelX);
1420 NetOut.Write(GameVelY);
1421 NetOut.Write(Byte(GameDirection));
1422 end;
1424 g_Net_Host_Send(ID, False, NET_CHAN_MONSTERPOS);
1425 end;
1427 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
1428 var
1429 M: TMonster;
1430 begin
1431 M := g_Monsters_ByUID(UID);
1432 if M = nil then Exit;
1434 NetOut.Write(Byte(NET_MSG_MSTATE));
1435 NetOut.Write(UID);
1437 with M do
1438 begin
1439 NetOut.Write(MonsterState);
1440 NetOut.Write(ForcedAnim);
1441 NetOut.Write(MonsterTargetUID);
1442 NetOut.Write(MonsterTargetTime);
1443 NetOut.Write(MonsterSleep);
1444 NetOut.Write(MonsterHealth);
1445 NetOut.Write(MonsterAmmo);
1446 NetOut.Write(MonsterPain);
1447 NetOut.Write(Byte(AnimIsReverse));
1448 NetOut.Write(FFireTime);
1449 end;
1451 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1452 end;
1454 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
1455 begin
1456 NetOut.Write(Byte(NET_MSG_MSHOT));
1457 NetOut.Write(UID);
1458 NetOut.Write(X);
1459 NetOut.Write(Y);
1460 NetOut.Write(VX);
1461 NetOut.Write(VY);
1463 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1464 end;
1466 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
1467 var
1468 M: TMonster;
1469 begin
1470 M := g_Monsters_ByUID(UID);
1471 if M = nil then Exit;
1473 NetOut.Write(Byte(NET_MSG_MDEL));
1474 NetOut.Write(UID);
1476 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1477 end;
1479 // MISC
1481 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
1482 begin
1483 NetOut.Write(Byte(NET_MSG_TIME_SYNC));
1484 NetOut.Write(Time);
1486 g_Net_Host_Send(ID, False, NET_CHAN_SERVICE);
1487 end;
1489 procedure MH_SEND_VoteEvent(EvType: Byte;
1490 StrArg1: string = 'a'; StrArg2: string = 'b';
1491 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
1492 ID: Integer = NET_EVERYONE);
1493 begin
1494 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
1495 NetOut.Write(EvType);
1496 NetOut.Write(IntArg1);
1497 NetOut.Write(IntArg2);
1498 NetOut.Write(StrArg1);
1499 NetOut.Write(StrArg2);
1501 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1502 end;
1504 // CLIENT MESSAGES //
1506 // GAME
1508 procedure MC_RECV_Chat(var M: TMsg);
1509 var
1510 Txt: string;
1511 Mode: Byte;
1512 begin
1513 Txt := M.ReadString();
1514 Mode := M.ReadByte();
1516 if Mode <> NET_CHAT_SYSTEM then
1517 begin
1518 if Mode = NET_CHAT_PLAYER then
1519 begin
1520 g_Console_Add(Txt, True);
1521 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
1522 g_Sound_PlayEx('SOUND_GAME_RADIO');
1523 end else
1524 if (Mode = NET_CHAT_TEAM) and (gPlayer1 <> nil) then
1525 begin
1526 if gPlayer1.Team = TEAM_RED then
1527 g_Console_Add(b_Text_Format('\r[Team] ') + Txt, True);
1528 if gPlayer1.Team = TEAM_BLUE then
1529 g_Console_Add(b_Text_Format('\b[Team] ') + Txt, True);
1530 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
1531 g_Sound_PlayEx('SOUND_GAME_RADIO');
1532 end;
1533 end else
1534 g_Console_Add(Txt, True);
1535 end;
1537 procedure MC_RECV_Effect(var M: TMsg);
1538 var
1539 Kind: Byte;
1540 X, Y: Integer;
1541 Ang: SmallInt;
1542 Anim: TAnimation;
1543 ID: LongWord;
1544 begin
1545 if not gGameOn then Exit;
1546 Kind := M.ReadByte();
1547 X := M.ReadLongInt();
1548 Y := M.ReadLongInt();
1549 Ang := M.ReadSmallInt();
1551 case Kind of
1552 NET_GFX_SPARK:
1553 g_GFX_Spark(X, Y, 2 + Random(2), Ang, 0, 0);
1555 NET_GFX_TELE:
1556 begin
1557 if g_Frames_Get(ID, 'FRAMES_TELEPORT') then
1558 begin
1559 Anim := TAnimation.Create(ID, False, 3);
1560 g_GFX_OnceAnim(X, Y, Anim);
1561 Anim.Free();
1562 end;
1563 if Ang = 1 then
1564 g_Sound_PlayExAt('SOUND_GAME_TELEPORT', X, Y);
1565 end;
1567 NET_GFX_EXPLODE:
1568 begin
1569 if g_Frames_Get(ID, 'FRAMES_EXPLODE_ROCKET') then
1570 begin
1571 Anim := TAnimation.Create(ID, False, 6);
1572 Anim.Blending := False;
1573 g_GFX_OnceAnim(X-64, Y-64, Anim);
1574 Anim.Free();
1575 end;
1576 if Ang = 1 then
1577 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEROCKET', X, Y);
1578 end;
1580 NET_GFX_BFGEXPL:
1581 begin
1582 if g_Frames_Get(ID, 'FRAMES_EXPLODE_BFG') then
1583 begin
1584 Anim := TAnimation.Create(ID, False, 6);
1585 Anim.Blending := False;
1586 g_GFX_OnceAnim(X-64, Y-64, Anim);
1587 Anim.Free();
1588 end;
1589 if Ang = 1 then
1590 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEBFG', X, Y);
1591 end;
1593 NET_GFX_BFGHIT:
1594 begin
1595 if g_Frames_Get(ID, 'FRAMES_BFGHIT') then
1596 begin
1597 Anim := TAnimation.Create(ID, False, 4);
1598 g_GFX_OnceAnim(X-32, Y-32, Anim);
1599 Anim.Free();
1600 end;
1601 end;
1603 NET_GFX_FIRE:
1604 begin
1605 if g_Frames_Get(ID, 'FRAMES_FIRE') then
1606 begin
1607 Anim := TAnimation.Create(ID, False, 4);
1608 g_GFX_OnceAnim(X, Y, Anim);
1609 Anim.Free();
1610 end;
1611 if Ang = 1 then
1612 g_Sound_PlayExAt('SOUND_FIRE', X, Y);
1613 end;
1615 NET_GFX_RESPAWN:
1616 begin
1617 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
1618 begin
1619 Anim := TAnimation.Create(ID, False, 4);
1620 g_GFX_OnceAnim(X, Y, Anim);
1621 Anim.Free();
1622 end;
1623 if Ang = 1 then
1624 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
1625 end;
1627 NET_GFX_SHELL1:
1628 g_Player_CreateShell(X, Y, 0, -2, SHELL_BULLET);
1630 NET_GFX_SHELL2:
1631 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1633 NET_GFX_SHELL3:
1634 begin
1635 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1636 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1637 end;
1638 end;
1639 end;
1641 procedure MC_RECV_Sound(var M: TMsg);
1642 var
1643 Name: string;
1644 X, Y: Integer;
1645 Pos: Boolean;
1646 begin
1647 Name := M.ReadString();
1648 Pos := M.ReadByte() <> 0;
1649 if Pos then
1650 begin
1651 X := M.ReadLongInt();
1652 Y := M.ReadLongInt();
1653 g_Sound_PlayExAt(Name, X, Y);
1654 end
1655 else
1656 g_Sound_PlayEx(Name);
1657 end;
1659 procedure MC_RECV_CreateShot(var M: TMsg);
1660 var
1661 I, X, Y, XV, YV: Integer;
1662 Timeout: LongWord;
1663 Target, Spawner: Word;
1664 ShType: Byte;
1665 begin
1666 I := M.ReadLongInt();
1667 ShType := M.ReadByte();
1668 Target := M.ReadWord();
1669 Spawner := M.ReadWord();
1670 Timeout := M.ReadLongWord();
1671 X := M.ReadLongInt();
1672 Y := M.ReadLongInt();
1673 XV := M.ReadLongInt();
1674 YV := M.ReadLongInt();
1676 I := g_Weapon_CreateShot(I, ShType, Spawner, Target, X, Y, XV, YV);
1677 if (Shots <> nil) and (I <= High(Shots)) then
1678 begin
1679 Shots[I].Timeout := Timeout;
1680 //Shots[I].Target := Target; // TODO: find a use for Target later
1681 end;
1682 end;
1684 procedure MC_RECV_UpdateShot(var M: TMsg);
1685 var
1686 I, TX, TY, TXV, TYV: Integer;
1687 begin
1688 I := M.ReadLongInt();
1689 TX := M.ReadLongInt();
1690 TY := M.ReadLongInt();
1691 TXV := M.ReadLongInt();
1692 TYV := M.ReadLongInt();
1694 if (Shots <> nil) and (I <= High(Shots)) then
1695 with (Shots[i]) do
1696 begin
1697 Obj.X := TX;
1698 Obj.Y := TY;
1699 Obj.Vel.X := TXV;
1700 Obj.Vel.Y := TYV;
1701 end;
1702 end;
1704 procedure MC_RECV_DeleteShot(var M: TMsg);
1705 var
1706 I, X, Y: Integer;
1707 L: Boolean;
1708 begin
1709 if not gGameOn then Exit;
1710 I := M.ReadLongInt();
1711 L := (M.ReadByte() <> 0);
1712 X := M.ReadLongInt();
1713 Y := M.ReadLongInt();
1715 g_Weapon_DestroyShot(I, X, Y, L);
1716 end;
1718 procedure MC_RECV_GameStats(var M: TMsg);
1719 begin
1720 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
1721 begin
1722 gTeamStat[TEAM_RED].Goals := M.ReadSmallInt();
1723 gTeamStat[TEAM_BLUE].Goals := M.ReadSmallInt();
1724 end
1725 else
1726 if gGameSettings.GameMode = GM_COOP then
1727 begin
1728 gCoopMonstersKilled := M.ReadWord();
1729 gCoopSecretsFound := M.ReadWord();
1730 end;
1731 end;
1733 procedure MC_RECV_CoopStats(var M: TMsg);
1734 begin
1735 gTotalMonsters := M.ReadLongInt();
1736 gSecretsCount := M.ReadLongInt();
1737 gCoopTotalMonstersKilled := M.ReadWord();
1738 gCoopTotalSecretsFound := M.ReadWord();
1739 gCoopTotalMonsters := M.ReadWord();
1740 gCoopTotalSecrets := M.ReadWord();
1741 end;
1743 procedure MC_RECV_GameEvent(var M: TMsg);
1744 var
1745 EvType: Byte;
1746 EvNum: Integer;
1747 EvStr: string;
1748 EvTime: LongWord;
1749 BHash: Boolean;
1750 EvHash: TMD5Digest;
1751 pl: TPlayer;
1752 i1, i2: TStrings_Locale;
1753 pln: String;
1754 cnt: Byte;
1755 begin
1756 FillChar(EvHash, Sizeof(EvHash), 0);
1757 EvType := M.ReadByte();
1758 EvNum := M.ReadLongInt();
1759 EvStr := M.ReadString();
1760 gLastMap := M.ReadByte() <> 0;
1761 if gLastMap and (gGameSettings.GameMode = GM_COOP) then gStatsOff := True;
1762 gStatsPressed := True;
1763 EvTime := M.ReadLongWord();
1764 BHash := M.ReadByte() <> 0;
1765 if BHash then
1766 EvHash := M.ReadMD5();
1768 gTime := EvTime;
1770 case EvType of
1771 NET_EV_MAPSTART:
1772 begin
1773 gGameOn := False;
1774 g_Game_ClearLoading();
1775 g_Game_StopAllSounds(True);
1777 gSwitchGameMode := Byte(EvNum);
1778 gGameSettings.GameMode := gSwitchGameMode;
1780 gWADHash := EvHash;
1781 if not g_Game_StartMap(EvStr, True) then
1782 begin
1783 if Pos(':\', EvStr) = 0 then
1784 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [gGameSettings.WAD + ':\' + EvStr]))
1785 else
1786 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [EvStr]));
1787 Exit;
1788 end;
1790 MC_SEND_FullStateRequest;
1791 end;
1793 NET_EV_MAPEND:
1794 begin
1795 gMissionFailed := EvNum <> 0;
1796 gExit := EXIT_ENDLEVELCUSTOM;
1797 end;
1799 NET_EV_RCON:
1800 begin
1801 case EvNum of
1802 NET_RCON_NOAUTH:
1803 g_Console_Add(_lc[I_NET_RCON_NOAUTH], True);
1804 NET_RCON_PWGOOD:
1805 g_Console_Add(_lc[I_NET_RCON_PWD_VALID], True);
1806 NET_RCON_PWBAD:
1807 g_Console_Add(_lc[I_NET_RCON_PWD_INVALID], True);
1808 end;
1809 end;
1811 NET_EV_CHANGE_TEAM:
1812 begin
1813 if EvNum = TEAM_RED then
1814 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [EvStr]), True);
1815 if EvNum = TEAM_BLUE then
1816 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [EvStr]), True);
1817 end;
1819 NET_EV_PLAYER_KICK:
1820 g_Console_Add(Format(_lc[I_PLAYER_KICK], [EvStr]), True);
1822 NET_EV_PLAYER_BAN:
1823 g_Console_Add(Format(_lc[I_PLAYER_BAN], [EvStr]), True);
1825 NET_EV_LMS_WARMUP:
1826 g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [EvNum]), True);
1828 NET_EV_LMS_SURVIVOR:
1829 g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True);
1831 NET_EV_BIGTEXT:
1832 g_Game_Message(AnsiUpperCase(EvStr), Word(EvNum));
1834 NET_EV_SCORE:
1835 begin
1836 pl := g_Player_Get(EvNum and $FFFF);
1837 if pl = nil then
1838 pln := '?'
1839 else
1840 pln := pl.Name;
1841 cnt := (EvNum shr 16) and $FF;
1842 if Pos('w', EvStr) = 0 then
1843 begin
1844 // Default score
1845 if Pos('t', EvStr) = 0 then
1846 begin
1847 // Player +/- score
1848 if Pos('-', EvStr) = 0 then
1849 begin
1850 if Pos('e', EvStr) = 0 then
1851 i1 := I_PLAYER_SCORE_ADD_OWN
1852 else
1853 i1 := I_PLAYER_SCORE_ADD_ENEMY;
1854 end else
1855 begin
1856 if Pos('e', EvStr) = 0 then
1857 i1 := I_PLAYER_SCORE_SUB_OWN
1858 else
1859 i1 := I_PLAYER_SCORE_SUB_ENEMY;
1860 end;
1861 // Which team
1862 if Pos('r', EvStr) > 0 then
1863 i2 := I_PLAYER_SCORE_TO_RED
1864 else
1865 i2 := I_PLAYER_SCORE_TO_BLUE;
1866 g_Console_Add(Format(_lc[i1], [pln, cnt, _lc[i2]]), True);
1867 end else
1868 begin
1869 // Team +/- score
1870 if Pos('-', EvStr) = 0 then
1871 i1 := I_PLAYER_SCORE_ADD_TEAM
1872 else
1873 i1 := I_PLAYER_SCORE_SUB_TEAM;
1874 // Which team
1875 if Pos('r', EvStr) > 0 then
1876 i2 := I_PLAYER_SCORE_RED
1877 else
1878 i2 := I_PLAYER_SCORE_BLUE;
1879 g_Console_Add(Format(_lc[i1], [_lc[i2], cnt]), True);
1880 end;
1881 end else
1882 begin
1883 // Game Win
1884 if Pos('e', EvStr) = 0 then
1885 i1 := I_PLAYER_SCORE_WIN_OWN
1886 else
1887 i1 := I_PLAYER_SCORE_WIN_ENEMY;
1888 // Which team
1889 if Pos('r', EvStr) > 0 then
1890 i2 := I_PLAYER_SCORE_TO_RED
1891 else
1892 i2 := I_PLAYER_SCORE_TO_BLUE;
1893 g_Console_Add(Format(_lc[i1], [pln, _lc[i2]]), True);
1894 end;
1895 end;
1897 NET_EV_SCORE_MSG:
1898 begin
1899 if EvNum = TEAM_RED then
1900 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1901 if EvNum = TEAM_BLUE then
1902 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1903 if EvNum = -TEAM_RED then
1904 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1905 if EvNum = -TEAM_BLUE then
1906 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1907 end;
1909 NET_EV_LMS_START:
1910 begin
1911 g_Player_RemoveAllCorpses;
1912 g_Game_Message(_lc[I_MESSAGE_LMS_START], 144);
1913 end;
1915 NET_EV_LMS_WIN:
1916 g_Game_Message(Format(_lc[I_MESSAGE_LMS_WIN], [AnsiUpperCase(EvStr)]), 144);
1918 NET_EV_TLMS_WIN:
1919 begin
1920 if EvNum = TEAM_RED then
1921 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 144);
1922 if EvNum = TEAM_BLUE then
1923 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 144);
1924 end;
1926 NET_EV_LMS_LOSE:
1927 g_Game_Message(_lc[I_MESSAGE_LMS_LOSE], 144);
1929 NET_EV_LMS_DRAW:
1930 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
1932 NET_EV_KILLCOMBO:
1933 g_Game_Announce_KillCombo(EvNum);
1935 NET_EV_PLAYER_TOUCH:
1936 begin
1937 pl := g_Player_Get(EvNum);
1938 if pl <> nil then
1939 pl.Touch();
1940 end;
1942 end;
1943 end;
1945 procedure MC_RECV_FlagEvent(var M: TMsg);
1946 var
1947 PID: Word;
1948 Pl: TPlayer;
1949 EvType: Byte;
1950 Fl: Byte;
1951 Quiet: Boolean;
1952 s, ts: string;
1953 begin
1954 EvType := M.ReadByte();
1955 Fl := M.ReadByte();
1957 if Fl = FLAG_NONE then Exit;
1959 Quiet := (M.ReadByte() <> 0);
1960 PID := M.ReadWord();
1962 gFlags[Fl].State := M.ReadByte();
1963 gFlags[Fl].CaptureTime := M.ReadLongWord();
1964 gFlags[Fl].Obj.X := M.ReadLongInt();
1965 gFlags[Fl].Obj.Y := M.ReadLongInt();
1966 gFlags[Fl].Obj.Vel.X := M.ReadLongInt();
1967 gFlags[Fl].Obj.Vel.Y := M.ReadLongInt();
1969 Pl := g_Player_Get(PID);
1970 if (Pl = nil) and
1971 (EvType <> FLAG_STATE_NORMAL) and
1972 (EvType <> FLAG_STATE_DROPPED) and
1973 (EvType <> FLAG_STATE_RETURNED) then
1974 Exit;
1976 case EvType of
1977 FLAG_STATE_NORMAL:
1978 begin
1979 if Quiet or (Pl = nil) then Exit;
1981 if Fl = FLAG_RED then
1982 s := _lc[I_PLAYER_FLAG_RED]
1983 else
1984 s := _lc[I_PLAYER_FLAG_BLUE];
1986 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1987 end;
1989 FLAG_STATE_CAPTURED:
1990 begin
1991 if (Pl <> nil) then Pl.SetFlag(Fl);
1993 if Quiet then Exit;
1995 if Fl = FLAG_RED then
1996 s := _lc[I_PLAYER_FLAG_RED]
1997 else
1998 s := _lc[I_PLAYER_FLAG_BLUE];
2000 g_Console_Add(Format(_lc[I_PLAYER_FLAG_GET], [Pl.Name, s]), True);
2001 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_GET], [AnsiUpperCase(s)]), 144);
2002 end;
2004 FLAG_STATE_DROPPED:
2005 begin
2006 if (Pl <> nil) then Pl.SetFlag(FLAG_NONE);
2008 if Quiet or (Pl = nil) then Exit;
2010 if Fl = FLAG_RED then
2011 s := _lc[I_PLAYER_FLAG_RED]
2012 else
2013 s := _lc[I_PLAYER_FLAG_BLUE];
2015 g_Console_Add(Format(_lc[I_PLAYER_FLAG_DROP], [Pl.Name, s]), True);
2016 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_DROP], [AnsiUpperCase(s)]), 144);
2017 end;
2019 FLAG_STATE_SCORED:
2020 begin
2021 g_Map_ResetFlag(FLAG_RED);
2022 g_Map_ResetFlag(FLAG_BLUE);
2023 if Quiet or (Pl = nil) then Exit;
2024 Pl.SetFlag(FLAG_NONE);
2026 if Fl = FLAG_RED then
2027 s := _lc[I_PLAYER_FLAG_RED]
2028 else
2029 s := _lc[I_PLAYER_FLAG_BLUE];
2031 ts := Format('%.4d', [gFlags[Fl].CaptureTime]);
2032 Insert('.', ts, Length(ts) + 1 - 3);
2033 g_Console_Add(Format(_lc[I_PLAYER_FLAG_CAPTURE], [Pl.Name, s, ts]), True);
2034 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_CAPTURE], [AnsiUpperCase(s)]), 144);
2035 end;
2037 FLAG_STATE_RETURNED:
2038 begin
2039 g_Map_ResetFlag(Fl);
2040 if Quiet then Exit;
2042 if Fl = FLAG_RED then
2043 s := _lc[I_PLAYER_FLAG_RED]
2044 else
2045 s := _lc[I_PLAYER_FLAG_BLUE];
2047 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2048 end;
2049 end;
2050 end;
2052 procedure MC_RECV_GameSettings(var M: TMsg);
2053 begin
2054 gGameSettings.GameMode := M.ReadByte();
2055 gGameSettings.GoalLimit := M.ReadWord();
2056 gGameSettings.TimeLimit := M.ReadWord();
2057 gGameSettings.MaxLives := M.ReadByte();
2058 gGameSettings.Options := M.ReadLongWord();
2059 end;
2061 // PLAYER
2063 function MC_RECV_PlayerCreate(var M: TMsg): Word;
2064 var
2065 PID, DID: Word;
2066 PName, Model: string;
2067 Color: TRGB;
2068 T: Byte;
2069 Pl: TPlayer;
2070 begin
2071 PID := M.ReadWord();
2072 Pl := g_Player_Get(PID);
2074 PName := M.ReadString();
2075 Model := M.ReadString();
2076 Color.R := M.ReadByte();
2077 Color.G := M.ReadByte();
2078 Color.B := M.ReadByte();
2079 T := M.ReadByte();
2081 Result := 0;
2082 if (PID <> NetPlrUID1) and (PID <> NetPlrUID2) then
2083 begin
2084 if (Pl <> nil) then Exit;
2085 DID := g_Player_Create(Model, Color, T, False);
2086 with g_Player_Get(DID) do
2087 begin
2088 UID := PID;
2089 Name := PName;
2090 Reset(True);
2091 end;
2092 end
2093 else
2094 begin
2095 if (PID = NetPlrUID1) and (gPlayer1 <> nil) then begin
2096 gPlayer1.UID := PID;
2097 gPlayer1.Model.SetColor(Color.R, Color.G, Color.B);
2098 gPlayer1.ChangeTeam(T);
2099 end;
2100 if (PID = NetPlrUID2) and (gPlayer2 <> nil) then begin
2101 gPlayer2.UID := PID;
2102 gPlayer2.Model.SetColor(Color.R, Color.G, Color.B);
2103 gPlayer2.ChangeTeam(T);
2104 end;
2105 end;
2107 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
2108 e_WriteLog('NET: Player ' + PName + ' [' + IntToStr(PID) + '] added.', MSG_NOTIFY);
2109 Result := PID;
2110 end;
2112 function MC_RECV_PlayerPos(var M: TMsg): Word;
2113 var
2114 GT: LongWord;
2115 PID: Word;
2116 kByte: Word;
2117 Pl: TPlayer;
2118 Dir: Byte;
2119 TmpX, TmpY: Integer;
2120 begin
2121 Result := 0;
2123 GT := M.ReadLongWord();
2124 if GT < gTime - NET_MAX_DIFFTIME then
2125 begin
2126 gTime := GT;
2127 Exit;
2128 end;
2129 gTime := GT;
2131 PID := M.ReadWord();
2132 Pl := g_Player_Get(PID);
2134 if Pl = nil then Exit;
2136 Result := PID;
2138 with Pl do
2139 begin
2140 FPing := M.ReadWord();
2141 FLoss := M.ReadByte();
2142 kByte := M.ReadWord();
2143 Dir := M.ReadByte();
2145 TmpX := M.ReadLongInt();
2146 TmpY := M.ReadLongInt();
2148 ReleaseKeys;
2150 if (kByte = NET_KEY_CHAT) then
2151 PressKey(KEY_CHAT, 10000)
2152 else
2153 begin
2154 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
2155 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
2156 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
2157 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
2158 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
2159 end;
2161 if ((Pl <> gPlayer1) and (Pl <> gPlayer2)) or LongBool(kByte and NET_KEY_FORCEDIR) then
2162 SetDirection(TDirection(Dir));
2164 GameVelX := M.ReadLongInt();
2165 GameVelY := M.ReadLongInt();
2166 GameAccelX := M.ReadLongInt();
2167 GameAccelY := M.ReadLongInt();
2168 SetLerp(TmpX, TmpY);
2169 if NetForcePlayerUpdate then Update();
2170 end;
2171 end;
2173 function MC_RECV_PlayerStats(var M: TMsg): Word;
2174 var
2175 PID: Word;
2176 Pl: TPlayer;
2177 I: Integer;
2178 OldJet: Boolean;
2179 NewTeam: Byte;
2180 begin
2181 PID := M.ReadWord();
2182 Pl := g_Player_Get(PID);
2183 Result := 0;
2184 if Pl = nil then
2185 Exit;
2187 with Pl do
2188 begin
2189 alive := (M.ReadByte() <> 0);
2190 GodMode := (M.ReadByte() <> 0);
2191 Health := M.ReadLongInt();
2192 Armor := M.ReadLongInt();
2193 Air := M.ReadLongInt();
2194 JetFuel := M.ReadLongInt();
2195 Lives := M.ReadByte();
2196 NewTeam := M.ReadByte();
2198 for I := WP_FIRST to WP_LAST do
2199 FWeapon[I] := (M.ReadByte() <> 0);
2201 for I := A_BULLETS to A_HIGH do
2202 FAmmo[I] := M.ReadWord();
2204 for I := A_BULLETS to A_HIGH do
2205 FMaxAmmo[I] := M.ReadWord();
2207 for I := MR_SUIT to MR_MAX do
2208 FMegaRulez[I] := M.ReadLongWord();
2210 FRulez := [];
2211 if (M.ReadByte() <> 0) then
2212 FRulez := FRulez + [R_ITEM_BACKPACK];
2213 if (M.ReadByte() <> 0) then
2214 FRulez := FRulez + [R_KEY_RED];
2215 if (M.ReadByte() <> 0) then
2216 FRulez := FRulez + [R_KEY_GREEN];
2217 if (M.ReadByte() <> 0) then
2218 FRulez := FRulez + [R_KEY_BLUE];
2219 if (M.ReadByte() <> 0) then
2220 FRulez := FRulez + [R_BERSERK];
2222 Frags := M.ReadLongInt();
2223 Death := M.ReadLongInt();
2225 SetWeapon(M.ReadByte());
2227 FSpectator := M.ReadByte() <> 0;
2228 if FSpectator then
2229 begin
2230 if Pl = gPlayer1 then
2231 begin
2232 gLMSPID1 := UID;
2233 gPlayer1 := nil;
2234 end;
2235 if Pl = gPlayer2 then
2236 begin
2237 gLMSPID2 := UID;
2238 gPlayer2 := nil;
2239 end;
2240 end
2241 else
2242 begin
2243 if (gPlayer1 = nil) and (gLMSPID1 > 0) then
2244 gPlayer1 := g_Player_Get(gLMSPID1);
2245 if (gPlayer2 = nil) and (gLMSPID2 > 0) then
2246 gPlayer2 := g_Player_Get(gLMSPID2);
2247 end;
2248 FGhost := M.ReadByte() <> 0;
2249 FPhysics := M.ReadByte() <> 0;
2250 FNoRespawn := M.ReadByte() <> 0;
2251 OldJet := FJetpack;
2252 FJetpack := M.ReadByte() <> 0;
2253 FFireTime := M.ReadLongInt();
2254 if OldJet and not FJetpack then
2255 JetpackOff
2256 else if not OldJet and FJetpack then
2257 JetpackOn;
2258 if Team <> NewTeam then
2259 Pl.ChangeTeam(NewTeam);
2260 end;
2262 Result := PID;
2263 end;
2265 function MC_RECV_PlayerDamage(var M: TMsg): Word;
2266 var
2267 PID: Word;
2268 Pl: TPlayer;
2269 Kind: Byte;
2270 Attacker, Value: Word;
2271 VX, VY: Integer;
2272 begin
2273 Result := 0;
2274 if not gGameOn then Exit;
2275 PID := M.ReadWord();
2276 Pl := g_Player_Get(PID);
2277 if Pl = nil then Exit;
2279 Kind := M.ReadByte();
2280 Attacker := M.ReadWord();
2281 Value := M.ReadWord();
2282 VX := M.ReadWord();
2283 VY := M.ReadWord();
2285 with Pl do
2286 Damage(Value, Attacker, VX, VY, Kind);
2288 Result := PID;
2289 end;
2291 function MC_RECV_PlayerDeath(var M: TMsg): Word;
2292 var
2293 PID: Word;
2294 Pl: TPlayer;
2295 KillType, DeathType: Byte;
2296 Attacker: Word;
2297 begin
2298 Result := 0;
2299 if not gGameOn then Exit;
2300 PID := M.ReadWord();
2301 Pl := g_Player_Get(PID);
2302 if Pl = nil then Exit;
2304 KillType := M.ReadByte();
2305 DeathType := M.ReadByte();
2306 Attacker := M.ReadWord();
2308 with Pl do
2309 begin
2310 Kill(KillType, Attacker, DeathType);
2311 SoftReset;
2312 end;
2313 end;
2315 function MC_RECV_PlayerDelete(var M: TMsg): Word;
2316 var
2317 PID: Word;
2318 Pl: TPlayer;
2319 begin
2320 PID := M.ReadWord();
2321 Pl := g_Player_Get(PID);
2322 Result := 0;
2323 if Pl = nil then Exit;
2325 g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [Pl.Name]), True);
2326 e_WriteLog('NET: Player ' + Pl.Name + ' [' + IntToStr(PID) + '] removed.', MSG_NOTIFY);
2328 g_Player_Remove(PID);
2330 Result := PID;
2331 end;
2333 function MC_RECV_PlayerFire(var M: TMsg): Word;
2334 var
2335 PID: Word;
2336 Weap: Byte;
2337 Pl: TPlayer;
2338 X, Y, AX, AY: Integer;
2339 SHID: Integer;
2340 begin
2341 Result := 0;
2342 if not gGameOn then Exit;
2343 PID := M.ReadWord();
2344 Pl := g_Player_Get(PID);
2345 if Pl = nil then Exit;
2347 Weap := M.ReadByte();
2348 X := M.ReadLongInt();
2349 Y := M.ReadLongInt();
2350 AX := M.ReadLongInt();
2351 AY := M.ReadLongInt();
2352 SHID := M.ReadLongInt();
2354 with Pl do
2355 if alive then NetFire(Weap, X, Y, AX, AY, SHID);
2356 end;
2358 procedure MC_RECV_PlayerSettings(var M: TMsg);
2359 var
2360 TmpName: string;
2361 TmpModel: string;
2362 TmpColor: TRGB;
2363 TmpTeam: Byte;
2364 Pl: TPlayer;
2365 PID: Word;
2366 begin
2367 PID := M.ReadWord();
2368 Pl := g_Player_Get(PID);
2369 if Pl = nil then Exit;
2371 TmpName := M.ReadString();
2372 TmpModel := M.ReadString();
2373 TmpColor.R := M.ReadByte();
2374 TmpColor.G := M.ReadByte();
2375 TmpColor.B := M.ReadByte();
2376 TmpTeam := M.ReadByte();
2378 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
2379 begin
2380 Pl.ChangeTeam(TmpTeam);
2381 if gPlayer1 = Pl then
2382 gPlayer1Settings.Team := TmpTeam;
2383 if gPlayer2 = Pl then
2384 gPlayer2Settings.Team := TmpTeam;
2385 end else
2386 Pl.SetColor(TmpColor);
2388 if Pl.Name <> TmpName then
2389 begin
2390 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
2391 Pl.Name := TmpName;
2392 end;
2394 if TmpModel <> Pl.Model.Name then
2395 Pl.SetModel(TmpModel);
2396 end;
2398 // ITEM
2400 procedure MC_RECV_ItemSpawn(var M: TMsg);
2401 var
2402 ID: Word;
2403 AID: DWord;
2404 X, Y, VX, VY: Integer;
2405 T: Byte;
2406 Quiet, Fall{, Resp}: Boolean;
2407 Anim: TAnimation;
2408 it: PItem;
2409 begin
2410 if not gGameOn then Exit;
2411 ID := M.ReadWord();
2412 Quiet := M.ReadByte() <> 0;
2413 T := M.ReadByte();
2414 Fall := M.ReadByte() <> 0;
2415 {Resp :=} M.ReadByte();
2416 X := M.ReadLongInt();
2417 Y := M.ReadLongInt();
2418 VX := M.ReadLongInt();
2419 VY := M.ReadLongInt();
2421 g_Items_Create(X, Y, T, Fall, False, False, ID);
2423 it := g_Items_ByIdx(ID);
2424 it.Obj.Vel.X := VX;
2425 it.Obj.Vel.Y := VY;
2427 if not Quiet then
2428 begin
2429 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
2430 if g_Frames_Get(AID, 'FRAMES_ITEM_RESPAWN') then
2431 begin
2432 Anim := TAnimation.Create(AID, False, 4);
2433 g_GFX_OnceAnim(X+(it.Obj.Rect.Width div 2)-16, Y+(it.Obj.Rect.Height div 2)-16, Anim);
2434 Anim.Free();
2435 end;
2436 end;
2437 end;
2439 procedure MC_RECV_ItemDestroy(var M: TMsg);
2440 var
2441 ID: Word;
2442 Quiet: Boolean;
2443 begin
2444 if not gGameOn then Exit;
2445 ID := M.ReadWord();
2446 Quiet := M.ReadByte() <> 0;
2448 if not g_Items_ValidId(ID) then exit;
2450 if not Quiet then g_Items_EmitPickupSound(ID);
2452 g_Items_Remove(ID);
2453 end;
2455 // PANEL
2457 procedure MC_RECV_PanelTexture(var M: TMsg);
2458 var
2459 TP: TPanel;
2460 //PType: Word;
2461 PGUID: Integer;
2462 Tex, Fr: Integer;
2463 Loop, Cnt: Byte;
2464 begin
2465 if not gGameOn then Exit;
2466 {PType :=} M.ReadWord();
2467 PGUID := Integer(M.ReadLongWord());
2468 Tex := M.ReadLongInt();
2469 Fr := M.ReadLongInt();
2470 Cnt := M.ReadByte();
2471 Loop := M.ReadByte();
2473 TP := g_Map_PanelByGUID(PGUID);
2476 case PType of
2477 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR: if gWalls <> nil then TP := gWalls[ID];
2478 PANEL_FORE: if gRenderForegrounds <> nil then TP := gRenderForegrounds[ID];
2479 PANEL_BACK: if gRenderBackgrounds <> nil then TP := gRenderBackgrounds[ID];
2480 PANEL_WATER: if gWater <> nil then TP := gWater[ID];
2481 PANEL_ACID1: if gAcid1 <> nil then TP := gAcid1[ID];
2482 PANEL_ACID2: if gAcid2 <> nil then TP := gAcid2[ID];
2483 PANEL_STEP: if gSteps <> nil then TP := gSteps[ID];
2484 else Exit;
2485 end;
2488 if (TP <> nil) then
2489 begin
2490 if Loop = 0 then
2491 begin
2492 // switch texture
2493 TP.SetTexture(Tex, Loop);
2494 TP.SetFrame(Fr, Cnt);
2495 end
2496 else
2497 begin
2498 // looped or non-looped animation
2499 TP.NextTexture(Loop);
2500 end;
2501 end;
2502 end;
2504 procedure MC_RECV_PanelState(var M: TMsg);
2505 var
2506 PGUID: Integer;
2507 E: Boolean;
2508 Lift: Byte;
2509 PType: Word;
2510 X, Y, W, H: Integer;
2511 TP: TPanel;
2512 speedX, speedY, startX, startY, endX, endY: Integer;
2513 sizeSpX, sizeSpY, sizeEX, sizeEY: Integer;
2514 mpflags: Byte;
2515 begin
2516 if not gGameOn then Exit;
2517 PType := M.ReadWord();
2518 PGUID := Integer(M.ReadLongWord());
2519 E := (M.ReadByte() <> 0);
2520 Lift := M.ReadByte();
2521 X := M.ReadLongInt();
2522 Y := M.ReadLongInt();
2523 W := M.ReadWord();
2524 H := M.ReadWord();
2525 // mplats
2526 speedX := M.ReadLongInt();
2527 speedY := M.ReadLongInt();
2528 startX := M.ReadLongInt();
2529 startY := M.ReadLongInt();
2530 endX := M.ReadLongInt();
2531 endY := M.ReadLongInt();
2532 sizeSpX := M.ReadLongInt();
2533 sizeSpY := M.ReadLongInt();
2534 sizeEX := M.ReadLongInt();
2535 sizeEY := M.ReadLongInt();
2536 mpflags := M.ReadByte(); // bit0: TP.movingActive; bit1: TP.moveOnce
2538 case PType of
2539 {PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2540 if E then g_Map_EnableWallGUID(PGUID) else g_Map_DisableWallGUID(PGUID);}
2542 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
2543 g_Map_SetLiftGUID(PGUID, Lift);
2545 {PANEL_BACK,
2546 PANEL_FORE:}
2548 else
2549 begin
2550 TP := g_Map_PanelByGUID(PGUID);
2551 if (TP <> nil) then
2552 begin
2553 TP.X := X;
2554 TP.Y := Y;
2555 TP.positionChanged();
2556 end;
2557 //gRenderBackgrounds[ID].X := X;
2558 //gRenderBackgrounds[ID].Y := Y;
2559 end;
2563 PANEL_FORE:
2564 begin
2565 gRenderForegrounds[ID].X := X;
2566 gRenderForegrounds[ID].Y := Y;
2567 end;
2569 end;
2571 // update enabled/disabled state for all panels
2572 if E then g_Map_EnableWallGUID(PGUID) else g_Map_DisableWallGUID(PGUID);
2574 // update panel position, as it can be moved
2575 TP := g_Map_PanelByGUID(PGUID);
2576 if (TP <> nil) then
2577 begin
2578 // mplat
2579 TP.movingSpeedX := speedX;
2580 TP.movingSpeedY := speedY;
2581 TP.movingStartX := startX;
2582 TP.movingStartY := startY;
2583 TP.movingEndX := endX;
2584 TP.movingEndY := endY;
2585 TP.sizeSpeedX := sizeSpX;
2586 TP.sizeSpeedY := sizeSpY;
2587 TP.sizeEndX := sizeEX;
2588 TP.sizeEndY := sizeEY;
2589 TP.movingActive := ((mpflags and 1) <> 0);
2590 TP.moveOnce := ((mpflags and 2) <> 0);
2591 // position
2592 TP.X := X;
2593 TP.Y := Y;
2594 TP.Width := W;
2595 TP.Height := H;
2596 TP.positionChanged();
2597 end;
2598 end;
2600 // TRIGGERS
2602 procedure MC_RECV_TriggerSound(var M: TMsg);
2603 var
2604 SPlaying: Boolean;
2605 SPos, SID: LongWord;
2606 SCount: LongInt;
2607 I: Integer;
2608 begin
2609 if not gGameOn then Exit;
2610 if gTriggers = nil then Exit;
2612 SID := M.ReadLongWord();
2613 SPlaying := M.ReadByte() <> 0;
2614 SPos := M.ReadLongWord();
2615 SCount := M.ReadLongInt();
2617 for I := Low(gTriggers) to High(gTriggers) do
2618 if gTriggers[I].TriggerType = TRIGGER_SOUND then
2619 if gTriggers[I].ClientID = SID then
2620 with gTriggers[I] do
2621 begin
2622 if SPlaying then
2623 begin
2624 if trigData.trigLocal then
2625 Sound.PlayVolumeAt(X+(Width div 2), Y+(Height div 2), trigData.trigVolume/255.0)
2626 else
2627 Sound.PlayPanVolume((trigData.trigPan-127.0)/128.0, trigData.trigVolume/255.0);
2628 Sound.SetPosition(SPos);
2629 end
2630 else
2631 if Sound.IsPlaying then Sound.Stop;
2633 SoundPlayCount := SCount;
2634 end;
2635 end;
2637 procedure MC_RECV_TriggerMusic(var M: TMsg);
2638 var
2639 MName: string;
2640 MPlaying: Boolean;
2641 MPos: LongWord;
2642 MPaused: Boolean;
2643 begin
2644 if not gGameOn then Exit;
2646 MName := M.ReadString();
2647 MPlaying := M.ReadByte() <> 0;
2648 MPos := M.ReadLongWord();
2649 MPaused := M.ReadByte() <> 0;
2651 if MPlaying then
2652 begin
2653 gMusic.SetByName(MName);
2654 gMusic.Play(True);
2655 gMusic.SetPosition(MPos);
2656 gMusic.SpecPause := MPaused;
2657 end
2658 else
2659 if gMusic.IsPlaying then gMusic.Stop;
2660 end;
2662 // MONSTERS
2664 procedure MC_RECV_MonsterSpawn(var M: TMsg);
2665 var
2666 ID: Word;
2667 MType, MState, MDir, MAnim, MBehav: Byte;
2668 X, Y, VX, VY, MTargTime, MHealth, MAmmo, MSleep: Integer;
2669 MTarg: Word;
2670 Mon: TMonster;
2671 begin
2672 ID := M.ReadWord();
2673 Mon := g_Monsters_ByUID(ID);
2674 if Mon <> nil then
2675 Exit;
2677 MType := M.ReadByte();
2678 MState := M.ReadByte();
2679 MAnim := M.ReadByte();
2680 MTarg := M.ReadWord();
2681 MTargTime := M.ReadLongInt();
2682 MBehav := M.ReadByte();
2683 MSleep := M.ReadLongInt();
2684 MHealth := M.ReadLongInt();
2685 MAmmo := M.ReadLongInt();
2687 X := M.ReadLongInt();
2688 Y := M.ReadLongInt();
2689 VX := M.ReadLongInt();
2690 VY := M.ReadLongInt();
2691 MDir := M.ReadByte();
2693 g_Monsters_Create(MType, X, Y, TDirection(MDir), False, ID);
2694 Mon := g_Monsters_ByUID(ID);
2695 if Mon = nil then
2696 Exit;
2698 with Mon do
2699 begin
2701 MonsterAnim := MAnim;
2702 MonsterTargetUID := MTarg;
2703 MonsterTargetTime := MTargTime;
2704 MonsterBehaviour := MBehav;
2705 MonsterSleep := MSleep;
2706 MonsterAmmo := MAmmo;
2707 SetHealth(MHealth);
2709 SetState(MState);
2711 setPosition(X, Y); // this will call positionChanged();
2712 GameVelX := VX;
2713 GameVelY := VY;
2714 //positionChanged(); // this updates spatial accelerators
2715 end;
2716 end;
2718 procedure MC_RECV_MonsterPos(var M: TMsg);
2719 var
2720 Mon: TMonster;
2721 ID: Word;
2722 X, Y: Integer;
2723 begin
2724 ID := M.ReadWord();
2725 Mon := g_Monsters_ByUID(ID);
2726 if Mon = nil then
2727 Exit;
2729 with Mon do
2730 begin
2731 X := M.ReadLongInt();
2732 Y := M.ReadLongInt();
2733 Mon.setPosition(X, Y); // this will call `positionChanged()`
2734 GameVelX := M.ReadLongInt();
2735 GameVelY := M.ReadLongInt();
2736 GameDirection := TDirection(M.ReadByte());
2737 //positionChanged(); // this updates spatial accelerators
2738 end;
2739 end;
2741 procedure MC_RECV_MonsterState(var M: TMsg);
2742 var
2743 ID: Integer;
2744 MState, MFAnm: Byte;
2745 Mon: TMonster;
2746 AnimRevert: Boolean;
2747 begin
2748 ID := M.ReadWord();
2749 Mon := g_Monsters_ByUID(ID);
2750 if Mon = nil then Exit;
2752 MState := M.ReadByte();
2753 MFAnm := M.ReadByte();
2755 with Mon do
2756 begin
2757 MonsterTargetUID := M.ReadWord();
2758 MonsterTargetTime := M.ReadLongInt();
2759 MonsterSleep := M.ReadLongInt();
2760 MonsterHealth := M.ReadLongInt();
2761 MonsterAmmo := M.ReadLongInt();
2762 MonsterPain := M.ReadLongInt();
2763 AnimRevert := M.ReadByte() <> 0;
2764 FFireTime := M.ReadLongInt();
2765 RevertAnim(AnimRevert);
2767 if MonsterState <> MState then
2768 begin
2769 if (MState = MONSTATE_GO) and (MonsterState = MONSTATE_SLEEP) then
2770 WakeUpSound;
2771 if (MState = MONSTATE_DIE) then
2772 DieSound;
2773 if (MState = MONSTATE_PAIN) then
2774 MakeBloodSimple(Min(200, MonsterPain));
2775 if (MState = MONSTATE_ATTACK) then
2776 kick(nil);
2777 if (MState = MONSTATE_DEAD) then
2778 SetDeadAnim;
2780 SetState(MState, MFAnm);
2781 end;
2782 end;
2783 end;
2785 procedure MC_RECV_MonsterShot(var M: TMsg);
2786 var
2787 ID: Integer;
2788 Mon: TMonster;
2789 X, Y, VX, VY: Integer;
2790 begin
2791 ID := M.ReadWord();
2793 Mon := g_Monsters_ByUID(ID);
2794 if Mon = nil then Exit;
2796 X := M.ReadLongInt();
2797 Y := M.ReadLongInt();
2798 VX := M.ReadLongInt();
2799 VY := M.ReadLongInt();
2801 Mon.ClientAttack(X, Y, VX, VY);
2802 end;
2804 procedure MC_RECV_MonsterDelete(var M: TMsg);
2805 var
2806 ID: Integer;
2807 Mon: TMonster;
2808 begin
2809 ID := M.ReadWord();
2810 Mon := g_Monsters_ByUID(ID);
2811 if Mon = nil then Exit;
2812 Mon.SetState(5);
2813 Mon.MonsterRemoved := True;
2814 end;
2816 procedure MC_RECV_TimeSync(var M: TMsg);
2817 var
2818 Time: LongWord;
2819 begin
2820 Time := M.ReadLongWord();
2822 if gState = STATE_INTERCUSTOM then
2823 gServInterTime := Min(Time, 255);
2824 end;
2826 procedure MC_RECV_VoteEvent(var M: TMsg);
2827 var
2828 EvID: Byte;
2829 Str1, Str2: string;
2830 Int1, Int2: SmallInt;
2831 begin
2832 EvID := M.ReadByte();
2833 Int1 := M.ReadSmallInt();
2834 Int2 := M.ReadSmallInt();
2835 Str1 := M.ReadString();
2836 Str2 := M.ReadString();
2838 case EvID of
2839 NET_VE_STARTED:
2840 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_STARTED], [Str1, Str2, Int1]), True);
2841 NET_VE_PASSED:
2842 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_PASSED], [Str1]), True);
2843 NET_VE_FAILED:
2844 g_Console_Add(_lc[I_MESSAGE_VOTE_FAILED], True);
2845 NET_VE_VOTE:
2846 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Str1, Int1, Int2]), True);
2847 NET_VE_INPROGRESS:
2848 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_INPROGRESS], [Str1]), True);
2849 end;
2850 end;
2852 // CLIENT SEND
2854 procedure MC_SEND_Info(Password: string);
2855 begin
2856 NetOut.Clear();
2858 NetOut.Write(Byte(NET_MSG_INFO));
2859 NetOut.Write(GAME_VERSION);
2860 NetOut.Write(Password);
2861 NetOut.Write(gPlayer1Settings.Name);
2862 NetOut.Write(gPlayer1Settings.Model);
2863 NetOut.Write(gPlayer1Settings.Color.R);
2864 NetOut.Write(gPlayer1Settings.Color.G);
2865 NetOut.Write(gPlayer1Settings.Color.B);
2866 NetOut.Write(gPlayer1Settings.Team);
2868 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2869 end;
2871 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
2872 begin
2873 NetOut.Write(Byte(NET_MSG_CHAT));
2874 NetOut.Write(Txt);
2875 NetOut.Write(Mode);
2877 g_Net_Client_Send(True, NET_CHAN_CHAT);
2878 end;
2880 function isKeyPressed (key1: Word; key2: Word): Boolean;
2881 begin
2882 if (key1 <> 0) and e_KeyPressed(key1) then begin result := true; exit; end;
2883 if (key2 <> 0) and e_KeyPressed(key2) then begin result := true; exit; end;
2884 result := false;
2885 end;
2887 procedure MC_SEND_PlayerPos();
2888 var
2889 kByte: Word;
2890 Predict: Boolean;
2891 strafeDir: Byte;
2892 WeaponSelect: Word = 0;
2893 I: Integer;
2894 begin
2895 if not gGameOn then Exit;
2896 if gPlayers = nil then Exit;
2897 if gPlayer1 = nil then Exit;
2899 kByte := 0;
2900 Predict := NetPredictSelf; // and (not NetGotKeys);
2902 if (not gConsoleShow) and (not gChatShow) and (g_ActiveWindow = nil) then
2903 begin
2904 strafeDir := P1MoveButton shr 4;
2905 P1MoveButton := P1MoveButton and $0F;
2906 with gGameControls.P1Control do
2907 begin
2908 if isKeyPressed(KeyLeft, KeyLeft2) and (not isKeyPressed(KeyRight, KeyRight2)) then P1MoveButton := 1
2909 else if (not isKeyPressed(KeyLeft, KeyLeft2)) and isKeyPressed(KeyRight, KeyRight2) then P1MoveButton := 2
2910 else if (not isKeyPressed(KeyLeft, KeyLeft2)) and (not isKeyPressed(KeyRight, KeyRight2)) then P1MoveButton := 0;
2912 // strafing
2913 if isKeyPressed(KeyStrafe, KeyStrafe2) then
2914 begin
2915 // new strafe mechanics
2916 if (strafeDir = 0) then strafeDir := P1MoveButton; // start strafing
2917 // now set direction according to strafe (reversed)
2918 if (strafeDir = 2) then gPlayer1.SetDirection(D_LEFT)
2919 else if (strafeDir = 1) then gPlayer1.SetDirection(D_RIGHT);
2920 end
2921 else
2922 begin
2923 if (P1MoveButton = 2) and isKeyPressed(KeyLeft, KeyLeft2) then gPlayer1.SetDirection(D_LEFT)
2924 else if (P1MoveButton = 1) and isKeyPressed(KeyRight, KeyRight2) then gPlayer1.SetDirection(D_RIGHT)
2925 else if P1MoveButton <> 0 then gPlayer1.SetDirection(TDirection(P1MoveButton-1));
2926 end;
2928 gPlayer1.ReleaseKeys;
2929 if P1MoveButton = 1 then
2930 begin
2931 kByte := kByte or NET_KEY_LEFT;
2932 if Predict then gPlayer1.PressKey(KEY_LEFT, 10000);
2933 end;
2934 if P1MoveButton = 2 then
2935 begin
2936 kByte := kByte or NET_KEY_RIGHT;
2937 if Predict then gPlayer1.PressKey(KEY_RIGHT, 10000);
2938 end;
2939 if isKeyPressed(KeyUp, KeyUp2) then
2940 begin
2941 kByte := kByte or NET_KEY_UP;
2942 gPlayer1.PressKey(KEY_UP, 10000);
2943 end;
2944 if isKeyPressed(KeyDown, KeyDown2) then
2945 begin
2946 kByte := kByte or NET_KEY_DOWN;
2947 gPlayer1.PressKey(KEY_DOWN, 10000);
2948 end;
2949 if isKeyPressed(KeyJump, KeyJump2) then
2950 begin
2951 kByte := kByte or NET_KEY_JUMP;
2952 // gPlayer1.PressKey(KEY_JUMP, 10000); // TODO: Make a prediction option
2953 end;
2954 if isKeyPressed(KeyFire, KeyFire2) then kByte := kByte or NET_KEY_FIRE;
2955 if isKeyPressed(KeyOpen, KeyOpen2) then kByte := kByte or NET_KEY_OPEN;
2956 if isKeyPressed(KeyNextWeapon, KeyNextWeapon2) then kByte := kByte or NET_KEY_NW;
2957 if isKeyPressed(KeyPrevWeapon, KeyPrevWeapon2) then kByte := kByte or NET_KEY_PW;
2958 for I := 0 to High(KeyWeapon) do
2959 if isKeyPressed(KeyWeapon[I], KeyWeapon2[I]) then
2960 WeaponSelect := WeaponSelect or Word(1 shl I);
2961 end;
2962 // fix movebutton state
2963 P1MoveButton := P1MoveButton or (strafeDir shl 4);
2964 end
2965 else
2966 kByte := NET_KEY_CHAT;
2968 NetOut.Write(Byte(NET_MSG_PLRPOS));
2969 NetOut.Write(gTime);
2970 NetOut.Write(kByte);
2971 NetOut.Write(Byte(gPlayer1.Direction));
2972 NetOut.Write(WeaponSelect);
2973 //e_WriteLog(Format('S:ws=%d', [WeaponSelect]), MSG_WARNING);
2974 g_Net_Client_Send(True, NET_CHAN_PLAYERPOS);
2976 //kBytePrev := kByte;
2977 //kDirPrev := gPlayer1.Direction;
2978 end;
2980 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
2981 begin
2982 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
2983 NetOut.Write(Byte(Start));
2984 NetOut.Write(Command);
2985 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2986 end;
2988 procedure MC_SEND_PlayerSettings();
2989 begin
2990 NetOut.Write(Byte(NET_MSG_PLRSET));
2991 NetOut.Write(gPlayer1Settings.Name);
2992 NetOut.Write(gPlayer1Settings.Model);
2993 NetOut.Write(gPlayer1Settings.Color.R);
2994 NetOut.Write(gPlayer1Settings.Color.G);
2995 NetOut.Write(gPlayer1Settings.Color.B);
2996 NetOut.Write(gPlayer1Settings.Team);
2998 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2999 end;
3001 procedure MC_SEND_FullStateRequest();
3002 begin
3003 NetOut.Write(Byte(NET_MSG_REQFST));
3005 g_Net_Client_Send(True, NET_CHAN_SERVICE);
3006 end;
3008 procedure MC_SEND_CheatRequest(Kind: Byte);
3009 begin
3010 NetOut.Write(Byte(NET_MSG_CHEAT));
3011 NetOut.Write(Kind);
3013 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3014 end;
3015 procedure MC_SEND_RCONPassword(Password: string);
3016 begin
3017 NetOut.Write(Byte(NET_MSG_RCON_AUTH));
3018 NetOut.Write(Password);
3020 g_Net_Client_Send(True, NET_CHAN_SERVICE);
3021 end;
3022 procedure MC_SEND_RCONCommand(Cmd: string);
3023 begin
3024 NetOut.Write(Byte(NET_MSG_RCON_CMD));
3025 NetOut.Write(Cmd);
3027 g_Net_Client_Send(True, NET_CHAN_SERVICE);
3028 end;
3030 // i have no idea why all this stuff is in here
3032 function ReadFile(const FileName: TFileName): AByte;
3033 var
3034 FileStream : TStream;
3035 fname: string;
3036 begin
3037 e_WriteLog(Format('NETWORK: looking for file "%s"', [FileName]), MSG_NOTIFY);
3038 fname := findDiskWad(FileName);
3039 if length(fname) = 0 then
3040 begin
3041 e_WriteLog(Format('NETWORK: file "%s" not found!', [FileName]), MSG_FATALERROR);
3042 SetLength(Result, 0);
3043 exit;
3044 end;
3045 e_WriteLog(Format('NETWORK: found file "%s"', [fname]), MSG_NOTIFY);
3046 Result := nil;
3047 FileStream := openDiskFileRO(fname);
3048 try
3049 if FileStream.Size > 0 then
3050 begin
3051 SetLength(Result, FileStream.Size);
3052 FileStream.Read(Result[0], FileStream.Size);
3053 end;
3054 finally
3055 FileStream.Free;
3056 end;
3057 end;
3059 function CreateMapDataMsg(const FileName: TFileName; ResList: TStringList): TMapDataMsg;
3060 var
3061 i: Integer;
3062 begin
3063 Result.MsgId := NET_MSG_MAP_RESPONSE;
3064 Result.FileData := ReadFile(FileName);
3065 Result.FileSize := Length(Result.FileData);
3067 SetLength(Result.ExternalResources, ResList.Count);
3068 for i:=0 to ResList.Count-1 do
3069 begin
3070 Result.ExternalResources[i].Name := ResList.Strings[i];
3071 Result.ExternalResources[i].md5 := MD5File(GameDir+'/wads/'+ResList.Strings[i]);
3072 end;
3073 end;
3075 procedure ResDataMsgToBytes(var bytes: AByte; const ResData: TResDataMsg);
3076 var
3077 ResultStream: TMemoryStream;
3078 begin
3079 ResultStream := TMemoryStream.Create;
3081 ResultStream.WriteBuffer(ResData.MsgId, SizeOf(ResData.MsgId)); //msgId
3082 ResultStream.WriteBuffer(ResData.FileSize, SizeOf(ResData.FileSize)); //file size
3083 ResultStream.WriteBuffer(ResData.FileData[0], ResData.FileSize); //file data
3085 SetLength(bytes, ResultStream.Size);
3086 ResultStream.Seek(0, soFromBeginning);
3087 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
3089 ResultStream.Free;
3090 end;
3092 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
3093 begin
3094 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
3095 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize));
3096 SetLength(Result.FileData, Result.FileSize);
3097 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize);
3098 end;
3100 procedure MapDataMsgToBytes(var bytes: AByte; const MapDataMsg: TMapDataMsg);
3101 var
3102 ResultStream: TMemoryStream;
3103 resCount: Integer;
3104 begin
3105 resCount := Length(MapDataMsg.ExternalResources);
3107 ResultStream := TMemoryStream.Create;
3109 ResultStream.WriteBuffer(MapDataMsg.MsgId, SizeOf(MapDataMsg.MsgId)); //msgId
3110 ResultStream.WriteBuffer(MapDataMsg.FileSize, SizeOf(MapDataMsg.FileSize)); //file size
3111 ResultStream.WriteBuffer(MapDataMsg.FileData[0], MapDataMsg.FileSize); //file data
3113 ResultStream.WriteBuffer(resCount, SizeOf(resCount)); //res count
3114 ResultStream.WriteBuffer(MapDataMsg.ExternalResources[0], resCount*SizeOf(TExternalResourceInfo)); //res data
3116 SetLength(bytes, ResultStream.Size);
3117 ResultStream.Seek(0, soFromBeginning);
3118 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
3120 ResultStream.Free;
3121 end;
3123 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
3124 var
3125 resCount: Integer;
3126 begin
3127 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
3128 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize)); //file size
3130 SetLength(Result.FileData, Result.FileSize);
3131 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize); //file data
3133 msgStream.ReadBuffer(resCount, SizeOf(resCount)); //res count
3134 SetLength(Result.ExternalResources, resCount);
3136 msgStream.ReadBuffer(Result.ExternalResources[0], resCount * SizeOf(TExternalResourceInfo)); //res data
3137 end;
3139 function IsValidFileName(const S: String): Boolean;
3140 const
3141 Forbidden: set of Char = ['<', '>', '|', '"', ':', '*', '?'];
3142 var
3143 I: Integer;
3144 begin
3145 Result := S <> '';
3146 for I := 1 to Length(S) do
3147 Result := Result and (not(S[I] in Forbidden));
3148 end;
3150 function IsValidFilePath(const S: String): Boolean;
3151 var
3152 I: Integer;
3153 begin
3154 Result := False;
3155 if not IsValidFileName(S) then exit;
3156 if FileExists(S) then exit;
3157 I := LastDelimiter('\/', S);
3158 if (I > 0) then
3159 if (not DirectoryExists(Copy(S, 1, I-1))) then
3160 exit;
3161 Result := True;
3162 end;
3164 procedure MC_SEND_MapRequest();
3165 begin
3166 NetOut.Write(Byte(NET_MSG_MAP_REQUEST));
3167 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3168 end;
3170 procedure MC_SEND_ResRequest(const resName: AnsiString);
3171 begin
3172 NetOut.Write(Byte(NET_MSG_RES_REQUEST));
3173 NetOut.Write(resName);
3174 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3175 end;
3177 procedure MH_RECV_MapRequest(C: pTNetClient; var M: TMsg);
3178 var
3179 payload: AByte;
3180 peer: pENetPeer;
3181 mapDataMsg: TMapDataMsg;
3182 begin
3183 e_WriteLog('NET: Received map request from ' +
3184 DecodeIPV4(C.Peer.address.host), MSG_NOTIFY);
3186 mapDataMsg := CreateMapDataMsg(MapsDir + gGameSettings.WAD, gExternalResources);
3187 peer := NetClients[C.ID].Peer;
3189 MapDataMsgToBytes(payload, mapDataMsg);
3190 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3192 payload := nil;
3193 mapDataMsg.FileData := nil;
3194 mapDataMsg.ExternalResources := nil;
3195 end;
3197 procedure MH_RECV_ResRequest(C: pTNetClient; var M: TMsg);
3198 var
3199 payload: AByte;
3200 peer: pENetPeer;
3201 FileName: String;
3202 resDataMsg: TResDataMsg;
3203 begin
3204 FileName := ExtractFileName(M.ReadString());
3205 e_WriteLog('NET: Received res request: ' + FileName +
3206 ' from ' + DecodeIPV4(C.Peer.address.host), MSG_NOTIFY);
3208 if not IsValidFilePath(FileName) then
3209 begin
3210 e_WriteLog('Invalid filename: ' + FileName, MSG_WARNING);
3211 exit;
3212 end;
3214 peer := NetClients[C.ID].Peer;
3216 if gExternalResources.IndexOf(FileName) > -1 then
3217 begin
3218 resDataMsg.MsgId := NET_MSG_RES_RESPONSE;
3219 resDataMsg.FileData := ReadFile(GameDir+'/wads/'+FileName);
3220 resDataMsg.FileSize := Length(resDataMsg.FileData);
3222 ResDataMsgToBytes(payload, resDataMsg);
3223 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3224 end;
3225 end;
3227 end.