DEADSOFTWARE

Net: Buffer outgoing messages
[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;
112 NET_EV_SECRET = 19;
113 NET_EV_INTER_READY = 20;
115 NET_VE_STARTED = 1;
116 NET_VE_PASSED = 2;
117 NET_VE_FAILED = 3;
118 NET_VE_VOTE = 4;
119 NET_VE_REVOKE = 5;
120 NET_VE_INPROGRESS = 6;
122 NET_FLAG_GET = 1;
123 NET_FLAG_DROP = 2;
124 NET_FLAG_CAP = 3;
125 NET_FLAG_RETURN = 4;
127 NET_CHEAT_SUICIDE = 1;
128 NET_CHEAT_SPECTATE = 2;
129 NET_CHEAT_READY = 3;
131 NET_MAX_DIFFTIME = 5000 div 36;
133 // HOST MESSAGES
135 procedure MH_RECV_Info(C: pTNetClient; var M: TMsg);
136 procedure MH_RECV_Chat(C: pTNetClient; var M: TMsg);
137 procedure MH_RECV_FullStateRequest(C: pTNetClient; var M: TMsg);
138 function MH_RECV_PlayerPos(C: pTNetClient; var M: TMsg): Word;
139 procedure MH_RECV_PlayerSettings(C: pTNetClient; var M: TMsg);
140 procedure MH_RECV_CheatRequest(C: pTNetClient; var M: TMsg);
141 procedure MH_RECV_RCONPassword(C: pTNetClient; var M: TMsg);
142 procedure MH_RECV_RCONCommand(C: pTNetClient; var M: TMsg);
143 procedure MH_RECV_MapRequest(C: pTNetClient; var M: TMsg);
144 procedure MH_RECV_ResRequest(C: pTNetClient; var M: TMsg);
145 procedure MH_RECV_Vote(C: pTNetClient; var M: TMsg);
147 // GAME
148 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
149 procedure MH_SEND_Info(ID: Byte);
150 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
151 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
152 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
153 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
154 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
155 procedure MH_SEND_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
156 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
157 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
158 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
159 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
160 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
161 // PLAYER
162 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
163 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
164 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
165 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
166 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
167 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
168 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
169 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
170 // ITEM
171 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
172 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
173 // PANEL
174 procedure MH_SEND_PanelTexture(PGUID: Integer; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
175 procedure MH_SEND_PanelState(PGUID: Integer; ID: Integer = NET_EVERYONE);
176 // MONSTER
177 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
178 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
179 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
180 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
181 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
182 // TRIGGER
183 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
184 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
185 // MISC
186 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
187 procedure MH_SEND_VoteEvent(EvType: Byte;
188 StrArg1: string = 'a'; StrArg2: string = 'b';
189 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
190 ID: Integer = NET_EVERYONE);
192 // CLIENT MESSAGES //
194 // GAME
195 procedure MC_RECV_Chat(var M: TMsg);
196 procedure MC_RECV_Effect(var M: TMsg);
197 procedure MC_RECV_Sound(var M: TMsg);
198 procedure MC_RECV_GameStats(var M: TMsg);
199 procedure MC_RECV_CoopStats(var M: TMsg);
200 procedure MC_RECV_GameEvent(var M: TMsg);
201 procedure MC_RECV_FlagEvent(var M: TMsg);
202 procedure MC_RECV_GameSettings(var M: TMsg);
203 // PLAYER
204 function MC_RECV_PlayerCreate(var M: TMsg): Word;
205 function MC_RECV_PlayerPos(var M: TMsg): Word;
206 function MC_RECV_PlayerStats(var M: TMsg): Word;
207 function MC_RECV_PlayerDelete(var M: TMsg): Word;
208 function MC_RECV_PlayerDamage(var M: TMsg): Word;
209 function MC_RECV_PlayerDeath(var M: TMsg): Word;
210 function MC_RECV_PlayerFire(var M: TMsg): Word;
211 procedure MC_RECV_PlayerSettings(var M: TMsg);
212 // ITEM
213 procedure MC_RECV_ItemSpawn(var M: TMsg);
214 procedure MC_RECV_ItemDestroy(var M: TMsg);
215 // PANEL
216 procedure MC_RECV_PanelTexture(var M: TMsg);
217 procedure MC_RECV_PanelState(var M: TMsg);
218 // MONSTER
219 procedure MC_RECV_MonsterSpawn(var M: TMsg);
220 procedure MC_RECV_MonsterPos(var M: TMsg);
221 procedure MC_RECV_MonsterState(var M: TMsg);
222 procedure MC_RECV_MonsterShot(var M: TMsg);
223 procedure MC_RECV_MonsterDelete(var M: TMsg);
224 // SHOT
225 procedure MC_RECV_CreateShot(var M: TMsg);
226 procedure MC_RECV_UpdateShot(var M: TMsg);
227 procedure MC_RECV_DeleteShot(var M: TMsg);
228 // TRIGGER
229 procedure MC_RECV_TriggerSound(var M: TMsg);
230 procedure MC_RECV_TriggerMusic(var M: TMsg);
231 // MISC
232 procedure MC_RECV_TimeSync(var M: TMsg);
233 procedure MC_RECV_VoteEvent(var M: TMsg);
234 // SERVICE
235 procedure MC_SEND_Info(Password: string);
236 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
237 procedure MC_SEND_PlayerPos();
238 procedure MC_SEND_FullStateRequest();
239 procedure MC_SEND_PlayerSettings();
240 procedure MC_SEND_CheatRequest(Kind: Byte);
241 procedure MC_SEND_RCONPassword(Password: string);
242 procedure MC_SEND_RCONCommand(Cmd: string);
243 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
244 // DOWNLOAD
245 procedure MC_SEND_MapRequest();
246 procedure MC_SEND_ResRequest(const resName: AnsiString);
248 type
249 TExternalResourceInfo = record
250 Name: string[255];
251 md5: TMD5Digest;
252 end;
254 TResDataMsg = record
255 MsgId: Byte;
256 FileSize: Integer;
257 FileData: AByte;
258 end;
260 TMapDataMsg = record
261 MsgId: Byte;
262 FileSize: Integer;
263 FileData: AByte;
264 ExternalResources: array of TExternalResourceInfo;
265 end;
267 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
268 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
270 implementation
272 uses
273 Math, ENet, e_input, e_graphics, e_log,
274 g_textures, g_gfx, g_sound, g_console, g_basic, g_options, g_main,
275 g_game, g_player, g_map, g_panel, g_items, g_weapons, g_phys, g_gui,
276 g_language, g_monsters, g_netmaster, utils, wadreader, MAPDEF;
278 const
279 NET_KEY_LEFT = 1;
280 NET_KEY_RIGHT = 2;
281 NET_KEY_UP = 4;
282 NET_KEY_DOWN = 8;
283 NET_KEY_JUMP = 16;
284 NET_KEY_FIRE = 32;
285 NET_KEY_OPEN = 64;
286 NET_KEY_NW = 256;
287 NET_KEY_PW = 512;
288 NET_KEY_CHAT = 2048;
289 NET_KEY_FORCEDIR = 4096;
291 //var
292 //kBytePrev: Word = 0;
293 //kDirPrev: TDirection = D_LEFT;
294 //HostGameTime: Word = 0;
296 // HOST MESSAGES //
299 // GAME
301 procedure MH_RECV_Chat(C: pTNetClient; var M: TMsg);
302 var
303 Txt: string;
304 Mode: Byte;
305 PID: Word;
306 Pl: TPlayer;
307 begin
308 PID := C^.Player;
309 Pl := g_Player_Get(PID);
311 Txt := M.ReadString();
312 Mode := M.ReadByte();
313 if (Mode = NET_CHAT_SYSTEM) then
314 Mode := NET_CHAT_PLAYER; // prevent sending system messages from clients
315 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
316 Mode := NET_CHAT_PLAYER; // revert to player chat in non-team game
318 if Pl = nil then
319 MH_SEND_Chat(Txt, Mode)
320 else
321 MH_SEND_Chat(Pl.Name + ': ' + Txt, Mode, IfThen(Mode = NET_CHAT_TEAM, Pl.Team, NET_EVERYONE));
322 end;
324 procedure MH_RECV_Info(C: pTNetClient; var M: TMsg);
325 var
326 Ver, PName, Model, Pw: string;
327 R, G, B, T: Byte;
328 PID: Word;
329 Color: TRGB;
330 I: Integer;
331 begin
332 Ver := M.ReadString();
333 Pw := M.ReadString();
334 PName := M.ReadString();
335 Model := M.ReadString();
336 R := M.ReadByte();
337 G := M.ReadByte();
338 B := M.ReadByte();
339 T := M.ReadByte();
341 if Ver <> GAME_VERSION then
342 begin
343 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
344 _lc[I_NET_DISC_VERSION]);
345 enet_peer_disconnect(C^.Peer, NET_DISC_VERSION);
346 Exit;
347 end;
349 if g_Net_IsHostBanned(C^.Peer^.address.host) then
350 begin
351 if g_Net_IsHostBanned(C^.Peer^.address.host, True) then
352 begin
353 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
354 _lc[I_NET_DISC_BAN]);
355 enet_peer_disconnect(C^.Peer, NET_DISC_BAN);
356 end
357 else
358 begin
359 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
360 _lc[I_NET_DISC_BAN]);
361 enet_peer_disconnect(C^.Peer, NET_DISC_TEMPBAN);
362 end;
363 Exit;
364 end;
366 if NetPassword <> '' then
367 if AnsiLowerCase(NetPassword) <> AnsiLowerCase(Pw) then
368 begin
369 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
370 _lc[I_NET_DISC_PASSWORD]);
371 enet_peer_disconnect(C^.Peer, NET_DISC_PASSWORD);
372 Exit;
373 end;
375 Color.R := R;
376 Color.B := B;
377 Color.G := G;
379 PID := g_Player_Create(Model, Color, T, False);
380 with g_Player_Get(PID) do
381 begin
382 Name := PName;
383 Reset(True);
384 end;
386 C^.Player := PID;
388 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
389 e_WriteLog('NET: Client ' + PName + ' [' + IntToStr(C^.ID) +
390 '] connected. Assigned player #' + IntToStr(PID) + '.', TMsgType.Notify);
392 MH_SEND_Info(C^.ID);
394 with g_Player_Get(PID) do
395 begin
396 Name := PName;
397 FClientID := C^.ID;
398 // round in progress, don't spawn
399 if (gGameSettings.MaxLives > 0) and (gLMSRespawn = LMS_RESPAWN_NONE) then
400 begin
401 Lives := 0;
402 FNoRespawn := True;
403 Spectate;
404 FWantsInGame := True; // TODO: look into this later
405 end
406 else
407 Respawn(gGameSettings.GameType = GT_SINGLE);
408 end;
410 for I := Low(NetClients) to High(NetClients) do
411 begin
412 if NetClients[I].ID = C^.ID then Continue;
413 MH_SEND_PlayerCreate(PID, NetClients[I].ID);
414 MH_SEND_PlayerPos(True, PID, NetClients[I].ID);
415 MH_SEND_PlayerStats(PID, NetClients[I].ID);
416 end;
418 if gState in [STATE_INTERCUSTOM, STATE_FOLD] then
419 MH_SEND_GameEvent(NET_EV_MAPEND, 0, 'N', C^.ID);
421 if NetUseMaster then g_Net_Slist_Update;
422 end;
424 procedure MH_RECV_FullStateRequest(C: pTNetClient; var M: TMsg);
425 begin
426 if gGameOn then
427 MH_SEND_Everything((C^.State = NET_STATE_AUTH), C^.ID)
428 else
429 C^.RequestedFullUpdate := True;
430 end;
432 // PLAYER
434 function MH_RECV_PlayerPos(C: pTNetClient; var M: TMsg): Word;
435 var
436 Dir, i: Byte;
437 WeaponSelect: Word;
438 PID: Word;
439 kByte: Word;
440 Pl: TPlayer;
441 GT: LongWord;
442 begin
443 Result := 0;
444 if not gGameOn then Exit;
446 GT := M.ReadLongWord();
447 PID := C^.Player;
448 Pl := g_Player_Get(PID);
449 if Pl = nil then
450 Exit;
452 if (GT > gTime + NET_MAX_DIFFTIME) or (GT < Pl.NetTime) then Exit;
454 with Pl do
455 begin
456 NetTime := GT;
457 kByte := M.ReadWord();
458 Dir := M.ReadByte();
459 WeaponSelect := M.ReadWord();
460 //e_WriteLog(Format('R:ws=%d', [WeaponSelect]), MSG_WARNING);
461 if Direction <> TDirection(Dir) then
462 JustTeleported := False;
464 SetDirection(TDirection(Dir));
465 ReleaseKeys;
467 if kByte = NET_KEY_CHAT then
468 begin
469 PressKey(KEY_CHAT, 10000);
470 Exit;
471 end;
473 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
474 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
475 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
476 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
477 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
478 if LongBool(kByte and NET_KEY_FIRE) then PressKey(KEY_FIRE, 10000);
479 if LongBool(kByte and NET_KEY_OPEN) then PressKey(KEY_OPEN, 10000);
480 if LongBool(kByte and NET_KEY_NW) then PressKey(KEY_NEXTWEAPON, 10000);
481 if LongBool(kByte and NET_KEY_PW) then PressKey(KEY_PREVWEAPON, 10000);
483 for i := 0 to 15 do
484 begin
485 if (WeaponSelect and Word(1 shl i)) <> 0 then
486 begin
487 //e_WriteLog(Format(' R:wn=%d', [i]), MSG_WARNING);
488 QueueWeaponSwitch(i);
489 end;
490 end;
491 end;
493 // MH_SEND_PlayerPos(False, PID, C^.ID);
494 end;
496 procedure MH_RECV_CheatRequest(C: pTNetClient; var M: TMsg);
497 var
498 CheatKind: Byte;
499 Pl: TPlayer;
500 begin
501 Pl := g_Player_Get(C^.Player);
502 if Pl = nil then Exit;
504 CheatKind := M.ReadByte();
506 case CheatKind of
507 NET_CHEAT_SUICIDE:
508 Pl.Damage(SUICIDE_DAMAGE, Pl.UID, 0, 0, HIT_SELF);
509 NET_CHEAT_SPECTATE:
510 begin
511 if Pl.FSpectator then
512 Pl.Respawn(False)
513 else
514 Pl.Spectate;
515 end;
516 NET_CHEAT_READY:
517 begin
518 if gState <> STATE_INTERCUSTOM then Exit;
519 Pl.FReady := not Pl.FReady;
520 if Pl.FReady then
521 begin
522 MH_SEND_GameEvent(NET_EV_INTER_READY, Pl.UID, 'Y');
523 Inc(gInterReadyCount);
524 end
525 else
526 begin
527 MH_SEND_GameEvent(NET_EV_INTER_READY, Pl.UID, 'N');
528 Dec(gInterReadyCount);
529 end;
530 end;
531 end;
532 end;
534 procedure MH_RECV_PlayerSettings(C: pTNetClient; var M: TMsg);
535 var
536 TmpName: string;
537 TmpModel: string;
538 TmpColor: TRGB;
539 TmpTeam: Byte;
540 Pl: TPlayer;
541 begin
542 TmpName := M.ReadString();
543 TmpModel := M.ReadString();
544 TmpColor.R := M.ReadByte();
545 TmpColor.G := M.ReadByte();
546 TmpColor.B := M.ReadByte();
547 TmpTeam := M.ReadByte();
549 Pl := g_Player_Get(C^.Player);
550 if Pl = nil then Exit;
552 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
553 Pl.SwitchTeam
554 else
555 Pl.SetColor(TmpColor);
557 if Pl.Name <> TmpName then
558 begin
559 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
560 Pl.Name := TmpName;
561 end;
563 if TmpModel <> Pl.Model.Name then
564 Pl.SetModel(TmpModel);
566 MH_SEND_PlayerSettings(Pl.UID, TmpModel);
567 end;
569 // RCON
571 procedure MH_RECV_RCONPassword(C: pTNetClient; var M: TMsg);
572 var
573 Pwd: string;
574 begin
575 Pwd := M.ReadString();
576 if not NetAllowRCON then Exit;
577 if Pwd = NetRCONPassword then
578 begin
579 C^.RCONAuth := True;
580 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWGOOD, 'N', C^.ID);
581 end
582 else
583 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWBAD, 'N', C^.ID);
584 end;
586 procedure MH_RECV_RCONCommand(C: pTNetClient; var M: TMsg);
587 var
588 Cmd: string;
589 begin
590 Cmd := M.ReadString();
591 if not NetAllowRCON then Exit;
592 if not C^.RCONAuth then
593 begin
594 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_NOAUTH, 'N', C^.ID);
595 Exit;
596 end;
597 g_Console_Process(Cmd);
598 end;
600 // MISC
602 procedure MH_RECV_Vote(C: pTNetClient; var M: TMsg);
603 var
604 Start: Boolean;
605 Name, Command: string;
606 Need: Integer;
607 Pl: TPlayer;
608 begin
609 Start := M.ReadByte() <> 0;
610 Command := M.ReadString();
612 Pl := g_Player_Get(C^.Player);
613 if Pl = nil then Exit;
614 Name := Pl.Name;
616 if Start then
617 begin
618 if not g_Console_CommandBlacklisted(Command) then
619 g_Game_StartVote(Command, Name);
620 end
621 else if gVoteInProgress then
622 begin
623 if (gPlayer1 <> nil) or (gPlayer2 <> nil) then
624 Need := Floor((NetClientCount+1)/2.0) + 1
625 else
626 Need := Floor(NetClientCount/2.0) + 1;
627 if C^.Voted then
628 begin
629 Dec(gVoteCount);
630 C^.Voted := False;
631 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_REVOKED], [Name, gVoteCount, Need]), True);
632 MH_SEND_VoteEvent(NET_VE_REVOKE, Name, 'a', gVoteCount, Need);
633 end
634 else
635 begin
636 Inc(gVoteCount);
637 C^.Voted := True;
638 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Name, gVoteCount, Need]), True);
639 MH_SEND_VoteEvent(NET_VE_VOTE, Name, 'a', gVoteCount, Need);
640 g_Game_CheckVote;
641 end;
642 end;
643 end;
645 // GAME (SEND)
647 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
649 function sendItemRespawn (it: PItem): Boolean;
650 begin
651 result := false; // don't stop
652 MH_SEND_ItemSpawn(True, it.myid, ID);
653 end;
655 function sendMonSpawn (mon: TMonster): Boolean;
656 begin
657 result := false; // don't stop
658 MH_SEND_MonsterSpawn(mon.UID, ID);
659 end;
661 function sendPanelState (pan: TPanel): Boolean;
662 begin
663 result := false; // don't stop
664 MH_SEND_PanelState(pan.guid, ID); // anyway, to sync mplats
665 if (pan.CanChangeTexture) then MH_SEND_PanelTexture(pan.guid, pan.LastAnimLoop, ID);
666 end;
668 var
669 I: Integer;
670 begin
671 if gPlayers <> nil then
672 begin
673 for I := Low(gPlayers) to High(gPlayers) do
674 begin
675 if gPlayers[I] <> nil then
676 begin
677 if CreatePlayers then MH_SEND_PlayerCreate(gPlayers[I].UID, ID);
678 MH_SEND_PlayerPos(True, gPlayers[I].UID, ID);
679 MH_SEND_PlayerStats(gPlayers[I].UID, ID);
681 if (gPlayers[I].Flag <> FLAG_NONE) and (gGameSettings.GameMode = GM_CTF) then
682 begin
683 MH_SEND_FlagEvent(FLAG_STATE_CAPTURED, gPlayers[I].Flag, gPlayers[I].UID, True, ID);
684 end;
685 end;
686 end;
687 end;
689 g_Items_ForEachAlive(sendItemRespawn, true); // backwards
690 g_Mons_ForEach(sendMonSpawn);
691 g_Map_ForEachPanel(sendPanelState);
693 if gTriggers <> nil then
694 begin
695 for I := Low(gTriggers) to High(gTriggers) do
696 begin
697 if gTriggers[I].TriggerType = TRIGGER_SOUND then
698 begin
699 MH_SEND_TriggerSound(gTriggers[I], ID);
700 end;
701 end;
702 end;
704 if Shots <> nil then
705 begin
706 for I := Low(Shots) to High(Shots) do
707 begin
708 if Shots[i].ShotType in [6, 7, 8] then
709 begin
710 MH_SEND_CreateShot(i, ID);
711 end;
712 end;
713 end;
715 MH_SEND_TriggerMusic(ID);
717 MH_SEND_GameStats(ID);
718 MH_SEND_CoopStats(ID);
720 if gGameSettings.GameMode = GM_CTF then
721 begin
722 if gFlags[FLAG_RED].State <> FLAG_STATE_CAPTURED then MH_SEND_FlagEvent(gFlags[FLAG_RED].State, FLAG_RED, 0, True, ID);
723 if gFlags[FLAG_BLUE].State <> FLAG_STATE_CAPTURED then MH_SEND_FlagEvent(gFlags[FLAG_BLUE].State, FLAG_BLUE, 0, True, ID);
724 end;
726 if CreatePlayers and (ID >= 0) then NetClients[ID].State := NET_STATE_GAME;
728 if gLMSRespawn > LMS_RESPAWN_NONE then
729 begin
730 MH_SEND_GameEvent(NET_EV_LMS_WARMUP, (gLMSRespawnTime - gTime) div 1000, 'N', ID);
731 end;
733 g_Net_Flush();
734 end;
736 procedure MH_SEND_Info(ID: Byte);
737 var
738 Map: string;
739 begin
740 Map := g_ExtractFileName(gMapInfo.Map);
742 NetOut.Clear();
744 NetOut.Write(Byte(NET_MSG_INFO));
745 NetOut.Write(ID);
746 NetOut.Write(NetClients[ID].Player);
747 NetOut.Write(gGameSettings.WAD);
748 NetOut.Write(Map);
749 NetOut.Write(gWADHash);
750 NetOut.Write(gGameSettings.GameMode);
751 NetOut.Write(gGameSettings.GoalLimit);
752 NetOut.Write(gGameSettings.TimeLimit);
753 NetOut.Write(gGameSettings.MaxLives);
754 NetOut.Write(gGameSettings.Options);
755 NetOut.Write(gTime);
757 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
758 end;
760 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
761 var
762 Name: string;
763 i: Integer;
764 Team: Byte;
765 begin
766 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
767 Mode := NET_CHAT_PLAYER;
769 Team := 0;
770 if (Mode = NET_CHAT_TEAM) then
771 begin
772 for i := Low(gPlayers) to High(gPlayers) do
773 if (gPlayers[i] <> nil) and (gPlayers[i].FClientID >= 0) and
774 (gPlayers[i].Team = ID) then
775 begin
776 NetOut.Write(Byte(NET_MSG_CHAT));
777 NetOut.Write(Txt);
778 NetOut.Write(Mode);
779 g_Net_Host_Send(gPlayers[i].FClientID, True, NET_CHAN_CHAT);
780 end;
781 Team := ID;
782 ID := NET_EVERYONE;
783 end
784 else
785 begin
786 NetOut.Write(Byte(NET_MSG_CHAT));
787 NetOut.Write(Txt);
788 NetOut.Write(Mode);
789 g_Net_Host_Send(ID, True, NET_CHAN_CHAT);
790 end;
792 if Mode = NET_CHAT_SYSTEM then
793 Exit;
795 if ID = NET_EVERYONE then
796 begin
797 if Mode = NET_CHAT_PLAYER then
798 begin
799 g_Console_Add(Txt, True);
800 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
801 g_Game_ChatSound(b_Text_Unformat(Txt));
802 end
803 else
804 if Mode = NET_CHAT_TEAM then
805 if gPlayer1 <> nil then
806 begin
807 if (gPlayer1.Team = TEAM_RED) and (Team = TEAM_RED) then
808 begin
809 g_Console_Add(#18'[Team] '#2 + Txt, True);
810 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
811 g_Game_ChatSound(b_Text_Unformat(Txt));
812 end
813 else if (gPlayer1.Team = TEAM_BLUE) and (Team = TEAM_BLUE) then
814 begin
815 g_Console_Add(#20'[Team] '#2 + Txt, True);
816 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
817 g_Game_ChatSound(b_Text_Unformat(Txt));
818 end;
819 end;
820 end
821 else
822 begin
823 Name := g_Net_ClientName_ByID(ID);
824 g_Console_Add('-> ' + Name + ': ' + Txt, True);
825 e_WriteLog('[Tell ' + Name + '] ' + b_Text_Unformat(Txt), TMsgType.Notify);
826 g_Game_ChatSound(b_Text_Unformat(Txt), False);
827 end;
828 end;
830 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
831 begin
832 NetOut.Write(Byte(NET_MSG_GFX));
833 NetOut.Write(Kind);
834 NetOut.Write(X);
835 NetOut.Write(Y);
836 NetOut.Write(Ang);
838 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
839 end;
841 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
842 begin
843 NetOut.Write(Byte(NET_MSG_SND));
844 NetOut.Write(Name);
845 if Pos then
846 begin
847 NetOut.Write(Byte(1));
848 NetOut.Write(X);
849 NetOut.Write(Y);
850 end
851 else
852 NetOut.Write(Byte(0));
854 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
855 end;
857 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
858 begin
859 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
861 NetOut.Write(Byte(NET_MSG_SHADD));
862 NetOut.Write(Proj);
863 NetOut.Write(Shots[Proj].ShotType);
864 NetOut.Write(Shots[Proj].Target);
865 NetOut.Write(Shots[Proj].SpawnerUID);
866 NetOut.Write(Shots[Proj].Timeout);
867 NetOut.Write(Shots[Proj].Obj.X);
868 NetOut.Write(Shots[Proj].Obj.Y);
869 NetOut.Write(Shots[Proj].Obj.Vel.X);
870 NetOut.Write(Shots[Proj].Obj.Vel.Y);
872 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
873 end;
875 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
876 begin
877 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
879 NetOut.Write(Byte(NET_MSG_SHPOS));
880 NetOut.Write(Proj);
881 NetOut.Write(Shots[Proj].Obj.X);
882 NetOut.Write(Shots[Proj].Obj.Y);
883 NetOut.Write(Shots[Proj].Obj.Vel.X);
884 NetOut.Write(Shots[Proj].Obj.Vel.Y);
886 g_Net_Host_Send(ID, False, NET_CHAN_SHOTS);
887 end;
889 procedure MH_Send_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
890 begin
891 NetOut.Write(Byte(NET_MSG_SHDEL));
892 NetOut.Write(Proj);
893 NetOut.Write(Byte(Loud));
894 NetOut.Write(X);
895 NetOut.Write(Y);
897 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
898 end;
900 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
901 begin
902 NetOut.Write(Byte(NET_MSG_SCORE));
903 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
904 begin
905 NetOut.Write(gTeamStat[TEAM_RED].Goals);
906 NetOut.Write(gTeamStat[TEAM_BLUE].Goals);
907 end
908 else
909 if gGameSettings.GameMode = GM_COOP then
910 begin
911 NetOut.Write(gCoopMonstersKilled);
912 NetOut.Write(gCoopSecretsFound);
913 end;
915 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
916 end;
918 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
919 begin
920 NetOut.Write(Byte(NET_MSG_COOP));
921 NetOut.Write(gTotalMonsters);
922 NetOut.Write(gSecretsCount);
923 NetOut.Write(gCoopTotalMonstersKilled);
924 NetOut.Write(gCoopTotalSecretsFound);
925 NetOut.Write(gCoopTotalMonsters);
926 NetOut.Write(gCoopTotalSecrets);
927 end;
929 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
930 begin
931 NetOut.Write(Byte(NET_MSG_GEVENT));
932 NetOut.Write(EvType);
933 NetOut.Write(EvNum);
934 NetOut.Write(EvStr);
935 NetOut.Write(Byte(gLastMap));
936 NetOut.Write(gTime);
937 if (EvType = NET_EV_MAPSTART) and isWadPath(EvStr) then
938 begin
939 NetOut.Write(Byte(1));
940 NetOut.Write(gWADHash);
941 end else
942 NetOut.Write(Byte(0));
944 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
945 end;
947 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
948 begin
949 NetOut.Write(Byte(NET_MSG_FLAG));
950 NetOut.Write(EvType);
951 NetOut.Write(Flag);
952 NetOut.Write(Byte(Quiet));
953 NetOut.Write(PID);
954 NetOut.Write(gFlags[Flag].State);
955 NetOut.Write(gFlags[Flag].CaptureTime);
956 NetOut.Write(gFlags[Flag].Obj.X);
957 NetOut.Write(gFlags[Flag].Obj.Y);
958 NetOut.Write(gFlags[Flag].Obj.Vel.X);
959 NetOut.Write(gFlags[Flag].Obj.Vel.Y);
961 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
962 end;
964 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
965 begin
966 NetOut.Write(Byte(NET_MSG_GSET));
967 NetOut.Write(gGameSettings.GameMode);
968 NetOut.Write(gGameSettings.GoalLimit);
969 NetOut.Write(gGameSettings.TimeLimit);
970 NetOut.Write(gGameSettings.MaxLives);
971 NetOut.Write(gGameSettings.Options);
973 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
974 end;
976 // PLAYER (SEND)
978 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
979 var
980 P: TPlayer;
981 begin
982 P := g_Player_Get(PID);
983 if P = nil then Exit;
985 NetOut.Write(Byte(NET_MSG_PLR));
986 NetOut.Write(PID);
987 NetOut.Write(P.Name);
989 NetOut.Write(P.FActualModelName);
990 NetOut.Write(P.FColor.R);
991 NetOut.Write(P.FColor.G);
992 NetOut.Write(P.FColor.B);
993 NetOut.Write(P.Team);
995 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT)
996 end;
998 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
999 var
1000 kByte: Word;
1001 Pl: TPlayer;
1002 begin
1003 Pl := g_Player_Get(PID);
1004 if Pl = nil then Exit;
1005 if Pl.FDummy then Exit;
1007 NetOut.Write(Byte(NET_MSG_PLRPOS));
1008 NetOut.Write(gTime);
1009 NetOut.Write(PID);
1011 kByte := 0;
1013 with Pl do
1014 begin
1015 NetOut.Write(FPing);
1016 NetOut.Write(FLoss);
1017 if IsKeyPressed(KEY_CHAT) then
1018 kByte := NET_KEY_CHAT
1019 else
1020 begin
1021 if IsKeyPressed(KEY_LEFT) then kByte := kByte or NET_KEY_LEFT;
1022 if IsKeyPressed(KEY_RIGHT) then kByte := kByte or NET_KEY_RIGHT;
1023 if IsKeyPressed(KEY_UP) then kByte := kByte or NET_KEY_UP;
1024 if IsKeyPressed(KEY_DOWN) then kByte := kByte or NET_KEY_DOWN;
1025 if IsKeyPressed(KEY_JUMP) then kByte := kByte or NET_KEY_JUMP;
1026 if JustTeleported then kByte := kByte or NET_KEY_FORCEDIR;
1027 end;
1029 NetOut.Write(kByte);
1030 if Direction = TDirection.D_LEFT then NetOut.Write(Byte(0)) else NetOut.Write(Byte(1));
1031 NetOut.Write(GameX);
1032 NetOut.Write(GameY);
1033 NetOut.Write(GameVelX);
1034 NetOut.Write(GameVelY);
1035 NetOut.Write(GameAccelX);
1036 NetOut.Write(GameAccelY);
1037 end;
1039 g_Net_Host_Send(ID, Reliable, NET_CHAN_PLAYERPOS);
1040 end;
1042 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
1043 var
1044 P: TPlayer;
1045 I: Integer;
1046 begin
1047 P := g_Player_Get(PID);
1048 if P = nil then Exit;
1050 NetOut.Write(Byte(NET_MSG_PLRSTA));
1051 NetOut.Write(PID);
1053 with P do
1054 begin
1055 NetOut.Write(Byte(alive));
1056 NetOut.Write(Byte(GodMode));
1057 NetOut.Write(Health);
1058 NetOut.Write(Armor);
1059 NetOut.Write(Air);
1060 NetOut.Write(JetFuel);
1061 NetOut.Write(Lives);
1062 NetOut.Write(Team);
1064 for I := WP_FIRST to WP_LAST do
1065 NetOut.Write(Byte(FWeapon[I]));
1067 for I := A_BULLETS to A_HIGH do
1068 NetOut.Write(FAmmo[I]);
1070 for I := A_BULLETS to A_HIGH do
1071 NetOut.Write(FMaxAmmo[I]);
1073 for I := MR_SUIT to MR_MAX do
1074 NetOut.Write(LongWord(FMegaRulez[I]));
1076 NetOut.Write(Byte(R_ITEM_BACKPACK in FRulez));
1077 NetOut.Write(Byte(R_KEY_RED in FRulez));
1078 NetOut.Write(Byte(R_KEY_GREEN in FRulez));
1079 NetOut.Write(Byte(R_KEY_BLUE in FRulez));
1080 NetOut.Write(Byte(R_BERSERK in FRulez));
1082 NetOut.Write(Frags);
1083 NetOut.Write(Death);
1085 NetOut.Write(CurrWeap);
1087 NetOut.Write(Byte(FSpectator));
1088 NetOut.Write(Byte(FGhost));
1089 NetOut.Write(Byte(FPhysics));
1090 NetOut.Write(Byte(FNoRespawn));
1091 NetOut.Write(Byte(FJetpack));
1092 NetOut.Write(FFireTime);
1093 NetOut.Write(Byte(FFlaming));
1094 end;
1096 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1097 end;
1099 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
1100 begin
1101 NetOut.Write(Byte(NET_MSG_PLRDMG));
1102 NetOut.Write(PID);
1103 NetOut.Write(Kind);
1104 NetOut.Write(Attacker);
1105 NetOut.Write(Value);
1106 NetOut.Write(VX);
1107 NetOut.Write(VY);
1109 g_Net_Host_Send(ID, False, NET_CHAN_PLAYER);
1110 end;
1112 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
1113 begin
1114 NetOut.Write(Byte(NET_MSG_PLRDIE));
1115 NetOut.Write(PID);
1116 NetOut.Write(KillType);
1117 NetOut.Write(DeathType);
1118 NetOut.Write(Attacker);
1120 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1121 end;
1123 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
1124 begin
1125 NetOut.Write(Byte(NET_MSG_PLRFIRE));
1126 NetOut.Write(PID);
1127 NetOut.Write(Weapon);
1128 NetOut.Write(X);
1129 NetOut.Write(Y);
1130 NetOut.Write(AX);
1131 NetOut.Write(AY);
1132 NetOut.Write(ShotID);
1134 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
1135 end;
1137 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
1138 begin
1139 NetOut.Write(Byte(NET_MSG_PLRDEL));
1140 NetOut.Write(PID);
1142 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1143 end;
1145 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
1146 var
1147 Pl: TPlayer;
1148 begin
1149 Pl := g_Player_Get(PID);
1150 if Pl = nil then Exit;
1152 NetOut.Write(Byte(NET_MSG_PLRSET));
1153 NetOut.Write(PID);
1154 NetOut.Write(Pl.Name);
1155 if Mdl = '' then
1156 NetOut.Write(Pl.Model.Name)
1157 else
1158 NetOut.Write(Mdl);
1159 NetOut.Write(Pl.FColor.R);
1160 NetOut.Write(Pl.FColor.G);
1161 NetOut.Write(Pl.FColor.B);
1162 NetOut.Write(Pl.Team);
1164 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1165 end;
1167 // ITEM (SEND)
1169 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1170 var
1171 it: PItem;
1172 tt: Byte;
1173 begin
1174 it := g_Items_ByIdx(IID);
1176 NetOut.Write(Byte(NET_MSG_ISPAWN));
1177 NetOut.Write(IID);
1178 NetOut.Write(Byte(Quiet));
1179 tt := it.ItemType;
1180 if it.dropped then tt := tt or $80;
1181 NetOut.Write(tt);
1182 NetOut.Write(Byte(it.Fall));
1183 NetOut.Write(Byte(it.Respawnable));
1184 NetOut.Write(it.Obj.X);
1185 NetOut.Write(it.Obj.Y);
1186 NetOut.Write(it.Obj.Vel.X);
1187 NetOut.Write(it.Obj.Vel.Y);
1189 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1190 end;
1192 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1193 begin
1194 NetOut.Write(Byte(NET_MSG_IDEL));
1195 NetOut.Write(IID);
1196 NetOut.Write(Byte(Quiet));
1198 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1199 end;
1201 // PANEL
1203 procedure MH_SEND_PanelTexture(PGUID: Integer; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
1204 var
1205 TP: TPanel;
1206 begin
1207 TP := g_Map_PanelByGUID(PGUID);
1208 if (TP = nil) then exit;
1210 with TP do
1211 begin
1212 NetOut.Write(Byte(NET_MSG_PTEX));
1213 NetOut.Write(LongWord(PGUID));
1214 NetOut.Write(FCurTexture);
1215 NetOut.Write(FCurFrame);
1216 NetOut.Write(FCurFrameCount);
1217 NetOut.Write(AnimLoop);
1218 end;
1220 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1221 end;
1223 procedure MH_SEND_PanelState(PGUID: Integer; ID: Integer = NET_EVERYONE);
1224 var
1225 TP: TPanel;
1226 mpflags: Byte = 0;
1227 begin
1228 TP := g_Map_PanelByGUID(PGUID);
1229 if (TP = nil) then exit;
1231 NetOut.Write(Byte(NET_MSG_PSTATE));
1232 NetOut.Write(LongWord(PGUID));
1233 NetOut.Write(Byte(TP.Enabled));
1234 NetOut.Write(TP.LiftType);
1235 NetOut.Write(TP.X);
1236 NetOut.Write(TP.Y);
1237 NetOut.Write(Word(TP.Width));
1238 NetOut.Write(Word(TP.Height));
1239 // mplats
1240 NetOut.Write(LongInt(TP.movingSpeedX));
1241 NetOut.Write(LongInt(TP.movingSpeedY));
1242 NetOut.Write(LongInt(TP.movingStartX));
1243 NetOut.Write(LongInt(TP.movingStartY));
1244 NetOut.Write(LongInt(TP.movingEndX));
1245 NetOut.Write(LongInt(TP.movingEndY));
1246 NetOut.Write(LongInt(TP.sizeSpeedX));
1247 NetOut.Write(LongInt(TP.sizeSpeedY));
1248 NetOut.Write(LongInt(TP.sizeEndX));
1249 NetOut.Write(LongInt(TP.sizeEndY));
1250 if TP.movingActive then mpflags := mpflags or 1;
1251 if TP.moveOnce then mpflags := mpflags or 2;
1252 NetOut.Write(Byte(mpflags));
1254 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1255 end;
1257 // TRIGGER
1259 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
1260 begin
1261 if gTriggers = nil then Exit;
1262 if T.Sound = nil then Exit;
1264 NetOut.Write(Byte(NET_MSG_TSOUND));
1265 NetOut.Write(T.ClientID);
1266 NetOut.Write(Byte(T.Sound.IsPlaying));
1267 NetOut.Write(LongWord(T.Sound.GetPosition));
1268 NetOut.Write(T.SoundPlayCount);
1270 g_Net_Host_Send(ID, True);
1271 end;
1273 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
1274 begin
1275 NetOut.Write(Byte(NET_MSG_TMUSIC));
1276 NetOut.Write(gMusic.Name);
1277 NetOut.Write(Byte(gMusic.IsPlaying));
1278 NetOut.Write(LongWord(gMusic.GetPosition));
1279 NetOut.Write(Byte(gMusic.SpecPause or gMusic.IsPaused));
1281 g_Net_Host_Send(ID, True);
1282 end;
1284 // MONSTER
1286 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
1287 var
1288 M: TMonster;
1289 begin
1290 M := g_Monsters_ByUID(UID);
1291 if M = nil then
1292 Exit;
1294 with M do
1295 begin
1296 NetOut.Write(Byte(NET_MSG_MSPAWN));
1297 NetOut.Write(UID);
1298 NetOut.Write(MonsterType);
1299 NetOut.Write(MonsterState);
1300 NetOut.Write(MonsterAnim);
1301 NetOut.Write(MonsterTargetUID);
1302 NetOut.Write(MonsterTargetTime);
1303 NetOut.Write(MonsterBehaviour);
1304 NetOut.Write(MonsterSleep);
1305 NetOut.Write(MonsterHealth);
1306 NetOut.Write(MonsterAmmo);
1307 NetOut.Write(GameX);
1308 NetOut.Write(GameY);
1309 NetOut.Write(GameVelX);
1310 NetOut.Write(GameVelY);
1311 NetOut.Write(Byte(GameDirection));
1312 end;
1314 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1315 end;
1317 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
1318 var
1319 M: TMonster;
1320 begin
1321 M := g_Monsters_ByUID(UID);
1322 if M = nil then Exit;
1324 NetOut.Write(Byte(NET_MSG_MPOS));
1325 NetOut.Write(UID);
1327 with M do
1328 begin
1329 NetOut.Write(GameX);
1330 NetOut.Write(GameY);
1331 NetOut.Write(GameVelX);
1332 NetOut.Write(GameVelY);
1333 NetOut.Write(Byte(GameDirection));
1334 end;
1336 g_Net_Host_Send(ID, False, NET_CHAN_MONSTERPOS);
1337 end;
1339 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
1340 var
1341 M: TMonster;
1342 begin
1343 M := g_Monsters_ByUID(UID);
1344 if M = nil then Exit;
1346 NetOut.Write(Byte(NET_MSG_MSTATE));
1347 NetOut.Write(UID);
1349 with M do
1350 begin
1351 NetOut.Write(MonsterState);
1352 NetOut.Write(ForcedAnim);
1353 NetOut.Write(MonsterTargetUID);
1354 NetOut.Write(MonsterTargetTime);
1355 NetOut.Write(MonsterSleep);
1356 NetOut.Write(MonsterHealth);
1357 NetOut.Write(MonsterAmmo);
1358 NetOut.Write(MonsterPain);
1359 NetOut.Write(Byte(AnimIsReverse));
1360 NetOut.Write(FFireTime);
1361 end;
1363 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1364 end;
1366 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
1367 begin
1368 NetOut.Write(Byte(NET_MSG_MSHOT));
1369 NetOut.Write(UID);
1370 NetOut.Write(X);
1371 NetOut.Write(Y);
1372 NetOut.Write(VX);
1373 NetOut.Write(VY);
1375 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1376 end;
1378 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
1379 var
1380 M: TMonster;
1381 begin
1382 M := g_Monsters_ByUID(UID);
1383 if M = nil then Exit;
1385 NetOut.Write(Byte(NET_MSG_MDEL));
1386 NetOut.Write(UID);
1388 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1389 end;
1391 // MISC
1393 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
1394 begin
1395 NetOut.Write(Byte(NET_MSG_TIME_SYNC));
1396 NetOut.Write(Time);
1398 g_Net_Host_Send(ID, False, NET_CHAN_SERVICE);
1399 end;
1401 procedure MH_SEND_VoteEvent(EvType: Byte;
1402 StrArg1: string = 'a'; StrArg2: string = 'b';
1403 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
1404 ID: Integer = NET_EVERYONE);
1405 begin
1406 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
1407 NetOut.Write(EvType);
1408 NetOut.Write(IntArg1);
1409 NetOut.Write(IntArg2);
1410 NetOut.Write(StrArg1);
1411 NetOut.Write(StrArg2);
1413 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1414 end;
1416 // CLIENT MESSAGES //
1418 // GAME
1420 procedure MC_RECV_Chat(var M: TMsg);
1421 var
1422 Txt: string;
1423 Mode: Byte;
1424 begin
1425 Txt := M.ReadString();
1426 Mode := M.ReadByte();
1428 if Mode <> NET_CHAT_SYSTEM then
1429 begin
1430 if Mode = NET_CHAT_PLAYER then
1431 begin
1432 g_Console_Add(Txt, True);
1433 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1434 g_Game_ChatSound(b_Text_Unformat(Txt));
1435 end else
1436 if (Mode = NET_CHAT_TEAM) and (gPlayer1 <> nil) then
1437 begin
1438 if gPlayer1.Team = TEAM_RED then
1439 g_Console_Add(b_Text_Format('\r[Team] ') + Txt, True);
1440 if gPlayer1.Team = TEAM_BLUE then
1441 g_Console_Add(b_Text_Format('\b[Team] ') + Txt, True);
1442 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1443 g_Game_ChatSound(b_Text_Unformat(Txt));
1444 end;
1445 end else
1446 g_Console_Add(Txt, True);
1447 end;
1449 procedure MC_RECV_Effect(var M: TMsg);
1450 var
1451 Kind: Byte;
1452 X, Y: Integer;
1453 Ang: SmallInt;
1454 Anim: TAnimation;
1455 ID: LongWord;
1456 begin
1457 if not gGameOn then Exit;
1458 Kind := M.ReadByte();
1459 X := M.ReadLongInt();
1460 Y := M.ReadLongInt();
1461 Ang := M.ReadSmallInt();
1463 case Kind of
1464 NET_GFX_SPARK:
1465 g_GFX_Spark(X, Y, 2 + Random(2), Ang, 0, 0);
1467 NET_GFX_TELE:
1468 begin
1469 if g_Frames_Get(ID, 'FRAMES_TELEPORT') then
1470 begin
1471 Anim := TAnimation.Create(ID, False, 3);
1472 g_GFX_OnceAnim(X, Y, Anim);
1473 Anim.Free();
1474 end;
1475 if Ang = 1 then
1476 g_Sound_PlayExAt('SOUND_GAME_TELEPORT', X, Y);
1477 end;
1479 NET_GFX_EXPLODE:
1480 begin
1481 if g_Frames_Get(ID, 'FRAMES_EXPLODE_ROCKET') then
1482 begin
1483 Anim := TAnimation.Create(ID, False, 6);
1484 Anim.Blending := False;
1485 g_GFX_OnceAnim(X-64, Y-64, Anim);
1486 Anim.Free();
1487 end;
1488 if Ang = 1 then
1489 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEROCKET', X, Y);
1490 end;
1492 NET_GFX_BFGEXPL:
1493 begin
1494 if g_Frames_Get(ID, 'FRAMES_EXPLODE_BFG') then
1495 begin
1496 Anim := TAnimation.Create(ID, False, 6);
1497 Anim.Blending := False;
1498 g_GFX_OnceAnim(X-64, Y-64, Anim);
1499 Anim.Free();
1500 end;
1501 if Ang = 1 then
1502 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEBFG', X, Y);
1503 end;
1505 NET_GFX_BFGHIT:
1506 begin
1507 if g_Frames_Get(ID, 'FRAMES_BFGHIT') then
1508 begin
1509 Anim := TAnimation.Create(ID, False, 4);
1510 g_GFX_OnceAnim(X-32, Y-32, Anim);
1511 Anim.Free();
1512 end;
1513 end;
1515 NET_GFX_FIRE:
1516 begin
1517 if g_Frames_Get(ID, 'FRAMES_FIRE') then
1518 begin
1519 Anim := TAnimation.Create(ID, False, 4);
1520 g_GFX_OnceAnim(X, Y, Anim);
1521 Anim.Free();
1522 end;
1523 if Ang = 1 then
1524 g_Sound_PlayExAt('SOUND_FIRE', X, Y);
1525 end;
1527 NET_GFX_RESPAWN:
1528 begin
1529 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
1530 begin
1531 Anim := TAnimation.Create(ID, False, 4);
1532 g_GFX_OnceAnim(X, Y, Anim);
1533 Anim.Free();
1534 end;
1535 if Ang = 1 then
1536 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
1537 end;
1539 NET_GFX_SHELL1:
1540 g_Player_CreateShell(X, Y, 0, -2, SHELL_BULLET);
1542 NET_GFX_SHELL2:
1543 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1545 NET_GFX_SHELL3:
1546 begin
1547 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1548 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1549 end;
1550 end;
1551 end;
1553 procedure MC_RECV_Sound(var M: TMsg);
1554 var
1555 Name: string;
1556 X, Y: Integer;
1557 Pos: Boolean;
1558 begin
1559 Name := M.ReadString();
1560 Pos := M.ReadByte() <> 0;
1561 if Pos then
1562 begin
1563 X := M.ReadLongInt();
1564 Y := M.ReadLongInt();
1565 g_Sound_PlayExAt(Name, X, Y);
1566 end
1567 else
1568 g_Sound_PlayEx(Name);
1569 end;
1571 procedure MC_RECV_CreateShot(var M: TMsg);
1572 var
1573 I, X, Y, XV, YV: Integer;
1574 Timeout: LongWord;
1575 Target, Spawner: Word;
1576 ShType: Byte;
1577 begin
1578 I := M.ReadLongInt();
1579 ShType := M.ReadByte();
1580 Target := M.ReadWord();
1581 Spawner := M.ReadWord();
1582 Timeout := M.ReadLongWord();
1583 X := M.ReadLongInt();
1584 Y := M.ReadLongInt();
1585 XV := M.ReadLongInt();
1586 YV := M.ReadLongInt();
1588 I := g_Weapon_CreateShot(I, ShType, Spawner, Target, X, Y, XV, YV);
1589 if (Shots <> nil) and (I <= High(Shots)) then
1590 begin
1591 Shots[I].Timeout := Timeout;
1592 //Shots[I].Target := Target; // TODO: find a use for Target later
1593 end;
1594 end;
1596 procedure MC_RECV_UpdateShot(var M: TMsg);
1597 var
1598 I, TX, TY, TXV, TYV: Integer;
1599 begin
1600 I := M.ReadLongInt();
1601 TX := M.ReadLongInt();
1602 TY := M.ReadLongInt();
1603 TXV := M.ReadLongInt();
1604 TYV := M.ReadLongInt();
1606 if (Shots <> nil) and (I <= High(Shots)) then
1607 with (Shots[i]) do
1608 begin
1609 Obj.X := TX;
1610 Obj.Y := TY;
1611 Obj.Vel.X := TXV;
1612 Obj.Vel.Y := TYV;
1613 end;
1614 end;
1616 procedure MC_RECV_DeleteShot(var M: TMsg);
1617 var
1618 I, X, Y: Integer;
1619 L: Boolean;
1620 begin
1621 if not gGameOn then Exit;
1622 I := M.ReadLongInt();
1623 L := (M.ReadByte() <> 0);
1624 X := M.ReadLongInt();
1625 Y := M.ReadLongInt();
1627 g_Weapon_DestroyShot(I, X, Y, L);
1628 end;
1630 procedure MC_RECV_GameStats(var M: TMsg);
1631 begin
1632 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
1633 begin
1634 gTeamStat[TEAM_RED].Goals := M.ReadSmallInt();
1635 gTeamStat[TEAM_BLUE].Goals := M.ReadSmallInt();
1636 end
1637 else
1638 if gGameSettings.GameMode = GM_COOP then
1639 begin
1640 gCoopMonstersKilled := M.ReadWord();
1641 gCoopSecretsFound := M.ReadWord();
1642 end;
1643 end;
1645 procedure MC_RECV_CoopStats(var M: TMsg);
1646 begin
1647 gTotalMonsters := M.ReadLongInt();
1648 gSecretsCount := M.ReadLongInt();
1649 gCoopTotalMonstersKilled := M.ReadWord();
1650 gCoopTotalSecretsFound := M.ReadWord();
1651 gCoopTotalMonsters := M.ReadWord();
1652 gCoopTotalSecrets := M.ReadWord();
1653 end;
1655 procedure MC_RECV_GameEvent(var M: TMsg);
1656 var
1657 EvType: Byte;
1658 EvNum: Integer;
1659 EvStr: string;
1660 EvTime: LongWord;
1661 BHash: Boolean;
1662 EvHash: TMD5Digest;
1663 pl: TPlayer;
1664 i1, i2: TStrings_Locale;
1665 pln: String;
1666 cnt: Byte;
1667 begin
1668 FillChar(EvHash, Sizeof(EvHash), 0);
1669 EvType := M.ReadByte();
1670 EvNum := M.ReadLongInt();
1671 EvStr := M.ReadString();
1672 gLastMap := M.ReadByte() <> 0;
1673 if gLastMap and (gGameSettings.GameMode = GM_COOP) then gStatsOff := True;
1674 gStatsPressed := True;
1675 EvTime := M.ReadLongWord();
1676 BHash := M.ReadByte() <> 0;
1677 if BHash then
1678 EvHash := M.ReadMD5();
1680 gTime := EvTime;
1682 case EvType of
1683 NET_EV_MAPSTART:
1684 begin
1685 gGameOn := False;
1686 g_Game_ClearLoading();
1687 g_Game_StopAllSounds(True);
1689 gSwitchGameMode := Byte(EvNum);
1690 gGameSettings.GameMode := gSwitchGameMode;
1692 gWADHash := EvHash;
1693 if not g_Game_StartMap(EvStr, True) then
1694 begin
1695 if not isWadPath(EvStr) then
1696 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [gGameSettings.WAD + ':\' + EvStr]))
1697 else
1698 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [EvStr]));
1699 Exit;
1700 end;
1702 MC_SEND_FullStateRequest;
1703 end;
1705 NET_EV_MAPEND:
1706 begin
1707 gMissionFailed := EvNum <> 0;
1708 gExit := EXIT_ENDLEVELCUSTOM;
1709 end;
1711 NET_EV_RCON:
1712 begin
1713 case EvNum of
1714 NET_RCON_NOAUTH:
1715 g_Console_Add(_lc[I_NET_RCON_NOAUTH], True);
1716 NET_RCON_PWGOOD:
1717 g_Console_Add(_lc[I_NET_RCON_PWD_VALID], True);
1718 NET_RCON_PWBAD:
1719 g_Console_Add(_lc[I_NET_RCON_PWD_INVALID], True);
1720 end;
1721 end;
1723 NET_EV_CHANGE_TEAM:
1724 begin
1725 if EvNum = TEAM_RED then
1726 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [EvStr]), True);
1727 if EvNum = TEAM_BLUE then
1728 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [EvStr]), True);
1729 end;
1731 NET_EV_PLAYER_KICK:
1732 g_Console_Add(Format(_lc[I_PLAYER_KICK], [EvStr]), True);
1734 NET_EV_PLAYER_BAN:
1735 g_Console_Add(Format(_lc[I_PLAYER_BAN], [EvStr]), True);
1737 NET_EV_LMS_WARMUP:
1738 g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [EvNum]), True);
1740 NET_EV_LMS_SURVIVOR:
1741 g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True);
1743 NET_EV_BIGTEXT:
1744 g_Game_Message(AnsiUpperCase(EvStr), Word(EvNum));
1746 NET_EV_SCORE:
1747 begin
1748 pl := g_Player_Get(EvNum and $FFFF);
1749 if pl = nil then
1750 pln := '?'
1751 else
1752 pln := pl.Name;
1753 cnt := (EvNum shr 16) and $FF;
1754 if Pos('w', EvStr) = 0 then
1755 begin
1756 // Default score
1757 if Pos('t', EvStr) = 0 then
1758 begin
1759 // Player +/- score
1760 if Pos('-', EvStr) = 0 then
1761 begin
1762 if Pos('e', EvStr) = 0 then
1763 i1 := I_PLAYER_SCORE_ADD_OWN
1764 else
1765 i1 := I_PLAYER_SCORE_ADD_ENEMY;
1766 end else
1767 begin
1768 if Pos('e', EvStr) = 0 then
1769 i1 := I_PLAYER_SCORE_SUB_OWN
1770 else
1771 i1 := I_PLAYER_SCORE_SUB_ENEMY;
1772 end;
1773 // Which team
1774 if Pos('r', EvStr) > 0 then
1775 i2 := I_PLAYER_SCORE_TO_RED
1776 else
1777 i2 := I_PLAYER_SCORE_TO_BLUE;
1778 g_Console_Add(Format(_lc[i1], [pln, cnt, _lc[i2]]), True);
1779 end else
1780 begin
1781 // Team +/- score
1782 if Pos('-', EvStr) = 0 then
1783 i1 := I_PLAYER_SCORE_ADD_TEAM
1784 else
1785 i1 := I_PLAYER_SCORE_SUB_TEAM;
1786 // Which team
1787 if Pos('r', EvStr) > 0 then
1788 i2 := I_PLAYER_SCORE_RED
1789 else
1790 i2 := I_PLAYER_SCORE_BLUE;
1791 g_Console_Add(Format(_lc[i1], [_lc[i2], cnt]), True);
1792 end;
1793 end else
1794 begin
1795 // Game Win
1796 if Pos('e', EvStr) = 0 then
1797 i1 := I_PLAYER_SCORE_WIN_OWN
1798 else
1799 i1 := I_PLAYER_SCORE_WIN_ENEMY;
1800 // Which team
1801 if Pos('r', EvStr) > 0 then
1802 i2 := I_PLAYER_SCORE_TO_RED
1803 else
1804 i2 := I_PLAYER_SCORE_TO_BLUE;
1805 g_Console_Add(Format(_lc[i1], [pln, _lc[i2]]), True);
1806 end;
1807 end;
1809 NET_EV_SCORE_MSG:
1810 begin
1811 if EvNum = TEAM_RED then
1812 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1813 if EvNum = TEAM_BLUE then
1814 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1815 if EvNum = -TEAM_RED then
1816 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1817 if EvNum = -TEAM_BLUE then
1818 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1819 end;
1821 NET_EV_LMS_START:
1822 begin
1823 g_Player_RemoveAllCorpses;
1824 g_Game_Message(_lc[I_MESSAGE_LMS_START], 144);
1825 end;
1827 NET_EV_LMS_WIN:
1828 g_Game_Message(Format(_lc[I_MESSAGE_LMS_WIN], [AnsiUpperCase(EvStr)]), 144);
1830 NET_EV_TLMS_WIN:
1831 begin
1832 if EvNum = TEAM_RED then
1833 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 144);
1834 if EvNum = TEAM_BLUE then
1835 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 144);
1836 end;
1838 NET_EV_LMS_LOSE:
1839 g_Game_Message(_lc[I_MESSAGE_LMS_LOSE], 144);
1841 NET_EV_LMS_DRAW:
1842 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
1844 NET_EV_KILLCOMBO:
1845 g_Game_Announce_KillCombo(EvNum);
1847 NET_EV_PLAYER_TOUCH:
1848 begin
1849 pl := g_Player_Get(EvNum);
1850 if pl <> nil then
1851 pl.Touch();
1852 end;
1854 NET_EV_SECRET:
1855 begin
1856 pl := g_Player_Get(EvNum);
1857 if pl <> nil then
1858 begin
1859 g_Console_Add(Format(_lc[I_PLAYER_SECRET], [pl.Name]), True);
1860 g_Sound_PlayEx('SOUND_GAME_SECRET');
1861 end;
1862 end;
1864 NET_EV_INTER_READY:
1865 begin
1866 pl := g_Player_Get(EvNum);
1867 if pl <> nil then pl.FReady := (EvStr = 'Y');
1868 end;
1869 end;
1870 end;
1872 procedure MC_RECV_FlagEvent(var M: TMsg);
1873 var
1874 PID: Word;
1875 Pl: TPlayer;
1876 EvType: Byte;
1877 Fl, a: Byte;
1878 Quiet: Boolean;
1879 s, ts: string;
1880 begin
1881 EvType := M.ReadByte();
1882 Fl := M.ReadByte();
1884 if Fl = FLAG_NONE then Exit;
1886 Quiet := (M.ReadByte() <> 0);
1887 PID := M.ReadWord();
1889 gFlags[Fl].State := M.ReadByte();
1890 gFlags[Fl].CaptureTime := M.ReadLongWord();
1891 gFlags[Fl].Obj.X := M.ReadLongInt();
1892 gFlags[Fl].Obj.Y := M.ReadLongInt();
1893 gFlags[Fl].Obj.Vel.X := M.ReadLongInt();
1894 gFlags[Fl].Obj.Vel.Y := M.ReadLongInt();
1896 Pl := g_Player_Get(PID);
1897 if (Pl = nil) and
1898 (EvType <> FLAG_STATE_NORMAL) and
1899 (EvType <> FLAG_STATE_DROPPED) and
1900 (EvType <> FLAG_STATE_RETURNED) then
1901 Exit;
1903 case EvType of
1904 FLAG_STATE_NORMAL:
1905 begin
1906 if Quiet or (Pl = nil) then Exit;
1908 if Fl = FLAG_RED then
1909 s := _lc[I_PLAYER_FLAG_RED]
1910 else
1911 s := _lc[I_PLAYER_FLAG_BLUE];
1913 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1915 if ((Pl = gPlayer1) or (Pl = gPlayer2)
1916 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
1917 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
1918 a := 0
1919 else
1920 a := 1;
1922 if not sound_ret_flag[a].IsPlaying() then
1923 sound_ret_flag[a].Play();
1924 end;
1926 FLAG_STATE_CAPTURED:
1927 begin
1928 if (Pl <> nil) then Pl.SetFlag(Fl);
1930 if Quiet then Exit;
1932 if Fl = FLAG_RED then
1933 s := _lc[I_PLAYER_FLAG_RED]
1934 else
1935 s := _lc[I_PLAYER_FLAG_BLUE];
1937 g_Console_Add(Format(_lc[I_PLAYER_FLAG_GET], [Pl.Name, s]), True);
1938 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_GET], [AnsiUpperCase(s)]), 144);
1940 if ((Pl = gPlayer1) or (Pl = gPlayer2)
1941 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
1942 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
1943 a := 0
1944 else
1945 a := 1;
1947 if not sound_get_flag[a].IsPlaying() then
1948 sound_get_flag[a].Play();
1949 end;
1951 FLAG_STATE_DROPPED:
1952 begin
1953 if (Pl <> nil) then Pl.SetFlag(FLAG_NONE);
1955 if Quiet or (Pl = nil) then Exit;
1957 if Fl = FLAG_RED then
1958 s := _lc[I_PLAYER_FLAG_RED]
1959 else
1960 s := _lc[I_PLAYER_FLAG_BLUE];
1962 g_Console_Add(Format(_lc[I_PLAYER_FLAG_DROP], [Pl.Name, s]), True);
1963 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_DROP], [AnsiUpperCase(s)]), 144);
1965 if ((Pl = gPlayer1) or (Pl = gPlayer2)
1966 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
1967 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
1968 a := 0
1969 else
1970 a := 1;
1972 if not sound_lost_flag[a].IsPlaying() then
1973 sound_lost_flag[a].Play();
1974 end;
1976 FLAG_STATE_SCORED:
1977 begin
1978 g_Map_ResetFlag(FLAG_RED);
1979 g_Map_ResetFlag(FLAG_BLUE);
1980 if Quiet or (Pl = nil) then Exit;
1981 Pl.SetFlag(FLAG_NONE);
1983 if Fl = FLAG_RED then
1984 s := _lc[I_PLAYER_FLAG_RED]
1985 else
1986 s := _lc[I_PLAYER_FLAG_BLUE];
1988 ts := Format('%.4d', [gFlags[Fl].CaptureTime]);
1989 Insert('.', ts, Length(ts) + 1 - 3);
1990 g_Console_Add(Format(_lc[I_PLAYER_FLAG_CAPTURE], [Pl.Name, s, ts]), True);
1991 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_CAPTURE], [AnsiUpperCase(s)]), 144);
1993 if ((Pl = gPlayer1) or (Pl = gPlayer2)
1994 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
1995 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
1996 a := 0
1997 else
1998 a := 1;
2000 if not sound_cap_flag[a].IsPlaying() then
2001 sound_cap_flag[a].Play();
2002 end;
2004 FLAG_STATE_RETURNED:
2005 begin
2006 g_Map_ResetFlag(Fl);
2007 if Quiet then Exit;
2009 if Fl = FLAG_RED then
2010 s := _lc[I_PLAYER_FLAG_RED]
2011 else
2012 s := _lc[I_PLAYER_FLAG_BLUE];
2014 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2016 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2017 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2018 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2019 a := 0
2020 else
2021 a := 1;
2023 if not sound_ret_flag[a].IsPlaying() then
2024 sound_ret_flag[a].Play();
2025 end;
2026 end;
2027 end;
2029 procedure MC_RECV_GameSettings(var M: TMsg);
2030 begin
2031 gGameSettings.GameMode := M.ReadByte();
2032 gGameSettings.GoalLimit := M.ReadWord();
2033 gGameSettings.TimeLimit := M.ReadWord();
2034 gGameSettings.MaxLives := M.ReadByte();
2035 gGameSettings.Options := M.ReadLongWord();
2036 end;
2038 // PLAYER
2040 function MC_RECV_PlayerCreate(var M: TMsg): Word;
2041 var
2042 PID, DID: Word;
2043 PName, Model: string;
2044 Color: TRGB;
2045 T: Byte;
2046 Pl: TPlayer;
2047 begin
2048 PID := M.ReadWord();
2049 Pl := g_Player_Get(PID);
2051 PName := M.ReadString();
2052 Model := M.ReadString();
2053 Color.R := M.ReadByte();
2054 Color.G := M.ReadByte();
2055 Color.B := M.ReadByte();
2056 T := M.ReadByte();
2058 Result := 0;
2059 if (PID <> NetPlrUID1) and (PID <> NetPlrUID2) then
2060 begin
2061 if (Pl <> nil) then Exit;
2062 DID := g_Player_Create(Model, Color, T, False);
2063 with g_Player_Get(DID) do
2064 begin
2065 UID := PID;
2066 Name := PName;
2067 Reset(True);
2068 end;
2069 end
2070 else
2071 begin
2072 if (PID = NetPlrUID1) and (gPlayer1 <> nil) then begin
2073 gPlayer1.UID := PID;
2074 gPlayer1.Model.SetColor(Color.R, Color.G, Color.B);
2075 gPlayer1.ChangeTeam(T);
2076 end;
2077 if (PID = NetPlrUID2) and (gPlayer2 <> nil) then begin
2078 gPlayer2.UID := PID;
2079 gPlayer2.Model.SetColor(Color.R, Color.G, Color.B);
2080 gPlayer2.ChangeTeam(T);
2081 end;
2082 end;
2084 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
2085 e_WriteLog('NET: Player ' + PName + ' [' + IntToStr(PID) + '] added.', TMsgType.Notify);
2086 Result := PID;
2087 end;
2089 function MC_RECV_PlayerPos(var M: TMsg): Word;
2090 var
2091 GT: LongWord;
2092 PID: Word;
2093 kByte: Word;
2094 Pl: TPlayer;
2095 Dir: Byte;
2096 TmpX, TmpY: Integer;
2097 begin
2098 Result := 0;
2100 GT := M.ReadLongWord();
2101 if GT < gTime - NET_MAX_DIFFTIME then
2102 begin
2103 gTime := GT;
2104 Exit;
2105 end;
2106 gTime := GT;
2108 PID := M.ReadWord();
2109 Pl := g_Player_Get(PID);
2111 if Pl = nil then Exit;
2113 Result := PID;
2115 with Pl do
2116 begin
2117 FPing := M.ReadWord();
2118 FLoss := M.ReadByte();
2119 kByte := M.ReadWord();
2120 Dir := M.ReadByte();
2122 TmpX := M.ReadLongInt();
2123 TmpY := M.ReadLongInt();
2125 ReleaseKeys;
2127 if (kByte = NET_KEY_CHAT) then
2128 PressKey(KEY_CHAT, 10000)
2129 else
2130 begin
2131 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
2132 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
2133 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
2134 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
2135 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
2136 end;
2138 if ((Pl <> gPlayer1) and (Pl <> gPlayer2)) or LongBool(kByte and NET_KEY_FORCEDIR) then
2139 SetDirection(TDirection(Dir));
2141 GameVelX := M.ReadLongInt();
2142 GameVelY := M.ReadLongInt();
2143 GameAccelX := M.ReadLongInt();
2144 GameAccelY := M.ReadLongInt();
2145 SetLerp(TmpX, TmpY);
2146 if NetForcePlayerUpdate then Update();
2147 end;
2148 end;
2150 function MC_RECV_PlayerStats(var M: TMsg): Word;
2151 var
2152 PID: Word;
2153 Pl: TPlayer;
2154 I, OldFire: Integer;
2155 OldJet, Flam: Boolean;
2156 NewTeam: Byte;
2157 begin
2158 PID := M.ReadWord();
2159 Pl := g_Player_Get(PID);
2160 Result := 0;
2161 if Pl = nil then
2162 Exit;
2164 with Pl do
2165 begin
2166 alive := (M.ReadByte() <> 0);
2167 GodMode := (M.ReadByte() <> 0);
2168 Health := M.ReadLongInt();
2169 Armor := M.ReadLongInt();
2170 Air := M.ReadLongInt();
2171 JetFuel := M.ReadLongInt();
2172 Lives := M.ReadByte();
2173 NewTeam := M.ReadByte();
2175 for I := WP_FIRST to WP_LAST do
2176 FWeapon[I] := (M.ReadByte() <> 0);
2178 for I := A_BULLETS to A_HIGH do
2179 FAmmo[I] := M.ReadWord();
2181 for I := A_BULLETS to A_HIGH do
2182 FMaxAmmo[I] := M.ReadWord();
2184 for I := MR_SUIT to MR_MAX do
2185 FMegaRulez[I] := M.ReadLongWord();
2187 FRulez := [];
2188 if (M.ReadByte() <> 0) then
2189 FRulez := FRulez + [R_ITEM_BACKPACK];
2190 if (M.ReadByte() <> 0) then
2191 FRulez := FRulez + [R_KEY_RED];
2192 if (M.ReadByte() <> 0) then
2193 FRulez := FRulez + [R_KEY_GREEN];
2194 if (M.ReadByte() <> 0) then
2195 FRulez := FRulez + [R_KEY_BLUE];
2196 if (M.ReadByte() <> 0) then
2197 FRulez := FRulez + [R_BERSERK];
2199 Frags := M.ReadLongInt();
2200 Death := M.ReadLongInt();
2202 SetWeapon(M.ReadByte());
2204 FSpectator := M.ReadByte() <> 0;
2205 if FSpectator then
2206 begin
2207 if Pl = gPlayer1 then
2208 begin
2209 gLMSPID1 := UID;
2210 gPlayer1 := nil;
2211 end;
2212 if Pl = gPlayer2 then
2213 begin
2214 gLMSPID2 := UID;
2215 gPlayer2 := nil;
2216 end;
2217 end
2218 else
2219 begin
2220 if (gPlayer1 = nil) and (gLMSPID1 > 0) then
2221 gPlayer1 := g_Player_Get(gLMSPID1);
2222 if (gPlayer2 = nil) and (gLMSPID2 > 0) then
2223 gPlayer2 := g_Player_Get(gLMSPID2);
2224 end;
2225 FGhost := M.ReadByte() <> 0;
2226 FPhysics := M.ReadByte() <> 0;
2227 FNoRespawn := M.ReadByte() <> 0;
2228 OldJet := FJetpack;
2229 FJetpack := M.ReadByte() <> 0;
2230 OldFire := FFireTime;
2231 FFireTime := M.ReadLongInt();
2232 if (OldFire <= 0) and (FFireTime > 0) then
2233 g_Sound_PlayExAt('SOUND_IGNITE', Obj.X, Obj.Y);
2234 Flam := M.ReadByte() <> 0;
2235 if OldJet and not FJetpack then
2236 JetpackOff
2237 else if not OldJet and FJetpack then
2238 JetpackOn;
2239 if FFlaming and not Flam then
2240 FlamerOff;
2241 if Team <> NewTeam then
2242 Pl.ChangeTeam(NewTeam);
2243 end;
2245 Result := PID;
2246 end;
2248 function MC_RECV_PlayerDamage(var M: TMsg): Word;
2249 var
2250 PID: Word;
2251 Pl: TPlayer;
2252 Kind: Byte;
2253 Attacker, Value: Word;
2254 VX, VY: Integer;
2255 begin
2256 Result := 0;
2257 if not gGameOn then Exit;
2258 PID := M.ReadWord();
2259 Pl := g_Player_Get(PID);
2260 if Pl = nil then Exit;
2262 Kind := M.ReadByte();
2263 Attacker := M.ReadWord();
2264 Value := M.ReadWord();
2265 VX := M.ReadWord();
2266 VY := M.ReadWord();
2268 with Pl do
2269 Damage(Value, Attacker, VX, VY, Kind);
2271 Result := PID;
2272 end;
2274 function MC_RECV_PlayerDeath(var M: TMsg): Word;
2275 var
2276 PID: Word;
2277 Pl: TPlayer;
2278 KillType, DeathType: Byte;
2279 Attacker: Word;
2280 begin
2281 Result := 0;
2282 if not gGameOn then Exit;
2283 PID := M.ReadWord();
2284 Pl := g_Player_Get(PID);
2285 if Pl = nil then Exit;
2287 KillType := M.ReadByte();
2288 DeathType := M.ReadByte();
2289 Attacker := M.ReadWord();
2291 with Pl do
2292 begin
2293 Kill(KillType, Attacker, DeathType);
2294 SoftReset;
2295 end;
2296 end;
2298 function MC_RECV_PlayerDelete(var M: TMsg): Word;
2299 var
2300 PID: Word;
2301 Pl: TPlayer;
2302 begin
2303 PID := M.ReadWord();
2304 Pl := g_Player_Get(PID);
2305 Result := 0;
2306 if Pl = nil then Exit;
2308 g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [Pl.Name]), True);
2309 e_WriteLog('NET: Player ' + Pl.Name + ' [' + IntToStr(PID) + '] removed.', TMsgType.Notify);
2311 g_Player_Remove(PID);
2313 Result := PID;
2314 end;
2316 function MC_RECV_PlayerFire(var M: TMsg): Word;
2317 var
2318 PID: Word;
2319 Weap: Byte;
2320 Pl: TPlayer;
2321 X, Y, AX, AY: Integer;
2322 SHID: Integer;
2323 begin
2324 Result := 0;
2325 if not gGameOn then Exit;
2326 PID := M.ReadWord();
2327 Pl := g_Player_Get(PID);
2328 if Pl = nil then Exit;
2330 Weap := M.ReadByte();
2331 X := M.ReadLongInt();
2332 Y := M.ReadLongInt();
2333 AX := M.ReadLongInt();
2334 AY := M.ReadLongInt();
2335 SHID := M.ReadLongInt();
2337 with Pl do
2338 if alive then NetFire(Weap, X, Y, AX, AY, SHID);
2339 end;
2341 procedure MC_RECV_PlayerSettings(var M: TMsg);
2342 var
2343 TmpName: string;
2344 TmpModel: string;
2345 TmpColor: TRGB;
2346 TmpTeam: Byte;
2347 Pl: TPlayer;
2348 PID: Word;
2349 begin
2350 PID := M.ReadWord();
2351 Pl := g_Player_Get(PID);
2352 if Pl = nil then Exit;
2354 TmpName := M.ReadString();
2355 TmpModel := M.ReadString();
2356 TmpColor.R := M.ReadByte();
2357 TmpColor.G := M.ReadByte();
2358 TmpColor.B := M.ReadByte();
2359 TmpTeam := M.ReadByte();
2361 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
2362 begin
2363 Pl.ChangeTeam(TmpTeam);
2364 if gPlayer1 = Pl then
2365 gPlayer1Settings.Team := TmpTeam;
2366 if gPlayer2 = Pl then
2367 gPlayer2Settings.Team := TmpTeam;
2368 end else
2369 Pl.SetColor(TmpColor);
2371 if Pl.Name <> TmpName then
2372 begin
2373 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
2374 Pl.Name := TmpName;
2375 end;
2377 if TmpModel <> Pl.Model.Name then
2378 Pl.SetModel(TmpModel);
2379 end;
2381 // ITEM
2383 procedure MC_RECV_ItemSpawn(var M: TMsg);
2384 var
2385 ID: Word;
2386 AID: DWord;
2387 X, Y, VX, VY: Integer;
2388 T: Byte;
2389 Quiet, Fall{, Resp}: Boolean;
2390 Anim: TAnimation;
2391 it: PItem;
2392 begin
2393 if not gGameOn then Exit;
2394 ID := M.ReadWord();
2395 Quiet := M.ReadByte() <> 0;
2396 T := M.ReadByte();
2397 Fall := M.ReadByte() <> 0;
2398 {Resp :=} M.ReadByte();
2399 X := M.ReadLongInt();
2400 Y := M.ReadLongInt();
2401 VX := M.ReadLongInt();
2402 VY := M.ReadLongInt();
2404 g_Items_Create(X, Y, T and $7F, Fall, False, False, ID);
2405 if ((T and $80) <> 0) then g_Items_SetDrop(ID);
2407 it := g_Items_ByIdx(ID);
2408 it.Obj.Vel.X := VX;
2409 it.Obj.Vel.Y := VY;
2411 if not Quiet then
2412 begin
2413 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
2414 if g_Frames_Get(AID, 'FRAMES_ITEM_RESPAWN') then
2415 begin
2416 Anim := TAnimation.Create(AID, False, 4);
2417 g_GFX_OnceAnim(X+(it.Obj.Rect.Width div 2)-16, Y+(it.Obj.Rect.Height div 2)-16, Anim);
2418 Anim.Free();
2419 end;
2420 end;
2421 end;
2423 procedure MC_RECV_ItemDestroy(var M: TMsg);
2424 var
2425 ID: Word;
2426 Quiet: Boolean;
2427 begin
2428 if not gGameOn then Exit;
2429 ID := M.ReadWord();
2430 Quiet := M.ReadByte() <> 0;
2432 if not g_Items_ValidId(ID) then exit;
2434 if not Quiet then g_Items_EmitPickupSound(ID);
2436 g_Items_Remove(ID);
2437 end;
2439 // PANEL
2441 procedure MC_RECV_PanelTexture(var M: TMsg);
2442 var
2443 TP: TPanel;
2444 PGUID: Integer;
2445 Tex, Fr: Integer;
2446 Loop, Cnt: Byte;
2447 begin
2448 if not gGameOn then Exit;
2450 PGUID := Integer(M.ReadLongWord());
2451 Tex := M.ReadLongInt();
2452 Fr := M.ReadLongInt();
2453 Cnt := M.ReadByte();
2454 Loop := M.ReadByte();
2456 TP := g_Map_PanelByGUID(PGUID);
2457 if (TP <> nil) then
2458 begin
2459 // switch texture
2460 TP.SetTexture(Tex, Loop);
2461 TP.SetFrame(Fr, Cnt);
2462 end;
2463 end;
2465 procedure MC_RECV_PanelState(var M: TMsg);
2466 var
2467 PGUID: Integer;
2468 E: Boolean;
2469 Lift: Byte;
2470 X, Y, W, H: Integer;
2471 TP: TPanel;
2472 speedX, speedY, startX, startY, endX, endY: Integer;
2473 sizeSpX, sizeSpY, sizeEX, sizeEY: Integer;
2474 mpflags: Byte;
2475 begin
2476 if not gGameOn then Exit;
2478 PGUID := Integer(M.ReadLongWord());
2479 E := (M.ReadByte() <> 0);
2480 Lift := M.ReadByte();
2481 X := M.ReadLongInt();
2482 Y := M.ReadLongInt();
2483 W := M.ReadWord();
2484 H := M.ReadWord();
2485 // mplats
2486 speedX := M.ReadLongInt();
2487 speedY := M.ReadLongInt();
2488 startX := M.ReadLongInt();
2489 startY := M.ReadLongInt();
2490 endX := M.ReadLongInt();
2491 endY := M.ReadLongInt();
2492 sizeSpX := M.ReadLongInt();
2493 sizeSpY := M.ReadLongInt();
2494 sizeEX := M.ReadLongInt();
2495 sizeEY := M.ReadLongInt();
2496 mpflags := M.ReadByte(); // bit0: TP.movingActive; bit1: TP.moveOnce
2498 TP := g_Map_PanelByGUID(PGUID);
2499 if (TP = nil) then exit;
2501 // update lifts state
2502 if TP.isGLift then g_Map_SetLiftGUID(PGUID, Lift);
2504 // update enabled/disabled state for all panels
2505 if E then g_Map_EnableWallGUID(PGUID) else g_Map_DisableWallGUID(PGUID);
2507 // update panel position, as it can be moved (mplat)
2508 TP.X := X;
2509 TP.Y := Y;
2510 TP.Width := W;
2511 TP.Height := H;
2512 // update mplat state
2513 TP.movingSpeedX := speedX;
2514 TP.movingSpeedY := speedY;
2515 TP.movingStartX := startX;
2516 TP.movingStartY := startY;
2517 TP.movingEndX := endX;
2518 TP.movingEndY := endY;
2519 TP.sizeSpeedX := sizeSpX;
2520 TP.sizeSpeedY := sizeSpY;
2521 TP.sizeEndX := sizeEX;
2522 TP.sizeEndY := sizeEY;
2523 TP.movingActive := ((mpflags and 1) <> 0);
2524 TP.moveOnce := ((mpflags and 2) <> 0);
2525 // notify panel of it's position/size change, so it can fix other internal structures
2526 TP.positionChanged();
2527 end;
2529 // TRIGGERS
2531 procedure MC_RECV_TriggerSound(var M: TMsg);
2532 var
2533 SPlaying: Boolean;
2534 SPos, SID: LongWord;
2535 SCount: LongInt;
2536 I: Integer;
2537 begin
2538 if not gGameOn then Exit;
2539 if gTriggers = nil then Exit;
2541 SID := M.ReadLongWord();
2542 SPlaying := M.ReadByte() <> 0;
2543 SPos := M.ReadLongWord();
2544 SCount := M.ReadLongInt();
2546 for I := Low(gTriggers) to High(gTriggers) do
2547 if gTriggers[I].TriggerType = TRIGGER_SOUND then
2548 if gTriggers[I].ClientID = SID then
2549 with gTriggers[I] do
2550 begin
2551 if Sound <> nil then
2552 begin
2553 if SPlaying then
2554 begin
2555 if tgcLocal then
2556 Sound.PlayVolumeAt(X+(Width div 2), Y+(Height div 2), tgcVolume/255.0)
2557 else
2558 Sound.PlayPanVolume((tgcPan-127.0)/128.0, tgcVolume/255.0);
2559 Sound.SetPosition(SPos);
2560 end
2561 else
2562 if Sound.IsPlaying then Sound.Stop;
2563 end;
2565 SoundPlayCount := SCount;
2566 end;
2567 end;
2569 procedure MC_RECV_TriggerMusic(var M: TMsg);
2570 var
2571 MName: string;
2572 MPlaying: Boolean;
2573 MPos: LongWord;
2574 MPaused: Boolean;
2575 begin
2576 if not gGameOn then Exit;
2578 MName := M.ReadString();
2579 MPlaying := M.ReadByte() <> 0;
2580 MPos := M.ReadLongWord();
2581 MPaused := M.ReadByte() <> 0;
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.