DEADSOFTWARE

added ready to interscreen; fixed client strafe (?); bumped protocol ver
[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;
732 end;
734 procedure MH_SEND_Info(ID: Byte);
735 var
736 Map: string;
737 begin
738 Map := g_ExtractFileName(gMapInfo.Map);
740 NetOut.Clear();
742 NetOut.Write(Byte(NET_MSG_INFO));
743 NetOut.Write(ID);
744 NetOut.Write(NetClients[ID].Player);
745 NetOut.Write(gGameSettings.WAD);
746 NetOut.Write(Map);
747 NetOut.Write(gWADHash);
748 NetOut.Write(gGameSettings.GameMode);
749 NetOut.Write(gGameSettings.GoalLimit);
750 NetOut.Write(gGameSettings.TimeLimit);
751 NetOut.Write(gGameSettings.MaxLives);
752 NetOut.Write(gGameSettings.Options);
753 NetOut.Write(gTime);
755 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
756 end;
758 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
759 var
760 Name: string;
761 i: Integer;
762 Team: Byte;
763 begin
764 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
765 Mode := NET_CHAT_PLAYER;
767 Team := 0;
768 if (Mode = NET_CHAT_TEAM) then
769 begin
770 for i := Low(gPlayers) to High(gPlayers) do
771 if (gPlayers[i] <> nil) and (gPlayers[i].FClientID >= 0) and
772 (gPlayers[i].Team = ID) then
773 begin
774 NetOut.Write(Byte(NET_MSG_CHAT));
775 NetOut.Write(Txt);
776 NetOut.Write(Mode);
777 g_Net_Host_Send(gPlayers[i].FClientID, True, NET_CHAN_CHAT);
778 end;
779 Team := ID;
780 ID := NET_EVERYONE;
781 end
782 else
783 begin
784 NetOut.Write(Byte(NET_MSG_CHAT));
785 NetOut.Write(Txt);
786 NetOut.Write(Mode);
787 g_Net_Host_Send(ID, True, NET_CHAN_CHAT);
788 end;
790 if Mode = NET_CHAT_SYSTEM then
791 Exit;
793 if ID = NET_EVERYONE then
794 begin
795 if Mode = NET_CHAT_PLAYER then
796 begin
797 g_Console_Add(Txt, True);
798 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
799 g_Game_ChatSound(b_Text_Unformat(Txt));
800 end
801 else
802 if Mode = NET_CHAT_TEAM then
803 if gPlayer1 <> nil then
804 begin
805 if (gPlayer1.Team = TEAM_RED) and (Team = TEAM_RED) then
806 begin
807 g_Console_Add(#18'[Team] '#2 + Txt, True);
808 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
809 g_Game_ChatSound(b_Text_Unformat(Txt));
810 end
811 else if (gPlayer1.Team = TEAM_BLUE) and (Team = TEAM_BLUE) then
812 begin
813 g_Console_Add(#20'[Team] '#2 + Txt, True);
814 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
815 g_Game_ChatSound(b_Text_Unformat(Txt));
816 end;
817 end;
818 end
819 else
820 begin
821 Name := g_Net_ClientName_ByID(ID);
822 g_Console_Add('-> ' + Name + ': ' + Txt, True);
823 e_WriteLog('[Tell ' + Name + '] ' + b_Text_Unformat(Txt), TMsgType.Notify);
824 g_Game_ChatSound(b_Text_Unformat(Txt), False);
825 end;
826 end;
828 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
829 begin
830 NetOut.Write(Byte(NET_MSG_GFX));
831 NetOut.Write(Kind);
832 NetOut.Write(X);
833 NetOut.Write(Y);
834 NetOut.Write(Ang);
836 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
837 end;
839 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
840 begin
841 NetOut.Write(Byte(NET_MSG_SND));
842 NetOut.Write(Name);
843 if Pos then
844 begin
845 NetOut.Write(Byte(1));
846 NetOut.Write(X);
847 NetOut.Write(Y);
848 end
849 else
850 NetOut.Write(Byte(0));
852 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
853 end;
855 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
856 begin
857 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
859 NetOut.Write(Byte(NET_MSG_SHADD));
860 NetOut.Write(Proj);
861 NetOut.Write(Shots[Proj].ShotType);
862 NetOut.Write(Shots[Proj].Target);
863 NetOut.Write(Shots[Proj].SpawnerUID);
864 NetOut.Write(Shots[Proj].Timeout);
865 NetOut.Write(Shots[Proj].Obj.X);
866 NetOut.Write(Shots[Proj].Obj.Y);
867 NetOut.Write(Shots[Proj].Obj.Vel.X);
868 NetOut.Write(Shots[Proj].Obj.Vel.Y);
870 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
871 end;
873 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
874 begin
875 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
877 NetOut.Write(Byte(NET_MSG_SHPOS));
878 NetOut.Write(Proj);
879 NetOut.Write(Shots[Proj].Obj.X);
880 NetOut.Write(Shots[Proj].Obj.Y);
881 NetOut.Write(Shots[Proj].Obj.Vel.X);
882 NetOut.Write(Shots[Proj].Obj.Vel.Y);
884 g_Net_Host_Send(ID, False, NET_CHAN_SHOTS);
885 end;
887 procedure MH_Send_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
888 begin
889 NetOut.Write(Byte(NET_MSG_SHDEL));
890 NetOut.Write(Proj);
891 NetOut.Write(Byte(Loud));
892 NetOut.Write(X);
893 NetOut.Write(Y);
895 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
896 end;
898 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
899 begin
900 NetOut.Write(Byte(NET_MSG_SCORE));
901 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
902 begin
903 NetOut.Write(gTeamStat[TEAM_RED].Goals);
904 NetOut.Write(gTeamStat[TEAM_BLUE].Goals);
905 end
906 else
907 if gGameSettings.GameMode = GM_COOP then
908 begin
909 NetOut.Write(gCoopMonstersKilled);
910 NetOut.Write(gCoopSecretsFound);
911 end;
913 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
914 end;
916 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
917 begin
918 NetOut.Write(Byte(NET_MSG_COOP));
919 NetOut.Write(gTotalMonsters);
920 NetOut.Write(gSecretsCount);
921 NetOut.Write(gCoopTotalMonstersKilled);
922 NetOut.Write(gCoopTotalSecretsFound);
923 NetOut.Write(gCoopTotalMonsters);
924 NetOut.Write(gCoopTotalSecrets);
925 end;
927 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
928 begin
929 NetOut.Write(Byte(NET_MSG_GEVENT));
930 NetOut.Write(EvType);
931 NetOut.Write(EvNum);
932 NetOut.Write(EvStr);
933 NetOut.Write(Byte(gLastMap));
934 NetOut.Write(gTime);
935 if (EvType = NET_EV_MAPSTART) and isWadPath(EvStr) then
936 begin
937 NetOut.Write(Byte(1));
938 NetOut.Write(gWADHash);
939 end else
940 NetOut.Write(Byte(0));
942 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
943 end;
945 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
946 begin
947 NetOut.Write(Byte(NET_MSG_FLAG));
948 NetOut.Write(EvType);
949 NetOut.Write(Flag);
950 NetOut.Write(Byte(Quiet));
951 NetOut.Write(PID);
952 NetOut.Write(gFlags[Flag].State);
953 NetOut.Write(gFlags[Flag].CaptureTime);
954 NetOut.Write(gFlags[Flag].Obj.X);
955 NetOut.Write(gFlags[Flag].Obj.Y);
956 NetOut.Write(gFlags[Flag].Obj.Vel.X);
957 NetOut.Write(gFlags[Flag].Obj.Vel.Y);
959 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
960 end;
962 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
963 begin
964 NetOut.Write(Byte(NET_MSG_GSET));
965 NetOut.Write(gGameSettings.GameMode);
966 NetOut.Write(gGameSettings.GoalLimit);
967 NetOut.Write(gGameSettings.TimeLimit);
968 NetOut.Write(gGameSettings.MaxLives);
969 NetOut.Write(gGameSettings.Options);
971 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
972 end;
974 // PLAYER (SEND)
976 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
977 var
978 P: TPlayer;
979 begin
980 P := g_Player_Get(PID);
981 if P = nil then Exit;
983 NetOut.Write(Byte(NET_MSG_PLR));
984 NetOut.Write(PID);
985 NetOut.Write(P.Name);
987 NetOut.Write(P.FActualModelName);
988 NetOut.Write(P.FColor.R);
989 NetOut.Write(P.FColor.G);
990 NetOut.Write(P.FColor.B);
991 NetOut.Write(P.Team);
993 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT)
994 end;
996 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
997 var
998 kByte: Word;
999 Pl: TPlayer;
1000 begin
1001 Pl := g_Player_Get(PID);
1002 if Pl = nil then Exit;
1003 if Pl.FDummy then Exit;
1005 NetOut.Write(Byte(NET_MSG_PLRPOS));
1006 NetOut.Write(gTime);
1007 NetOut.Write(PID);
1009 kByte := 0;
1011 with Pl do
1012 begin
1013 NetOut.Write(FPing);
1014 NetOut.Write(FLoss);
1015 if IsKeyPressed(KEY_CHAT) then
1016 kByte := NET_KEY_CHAT
1017 else
1018 begin
1019 if IsKeyPressed(KEY_LEFT) then kByte := kByte or NET_KEY_LEFT;
1020 if IsKeyPressed(KEY_RIGHT) then kByte := kByte or NET_KEY_RIGHT;
1021 if IsKeyPressed(KEY_UP) then kByte := kByte or NET_KEY_UP;
1022 if IsKeyPressed(KEY_DOWN) then kByte := kByte or NET_KEY_DOWN;
1023 if IsKeyPressed(KEY_JUMP) then kByte := kByte or NET_KEY_JUMP;
1024 if JustTeleported then kByte := kByte or NET_KEY_FORCEDIR;
1025 end;
1027 NetOut.Write(kByte);
1028 if Direction = TDirection.D_LEFT then NetOut.Write(Byte(0)) else NetOut.Write(Byte(1));
1029 NetOut.Write(GameX);
1030 NetOut.Write(GameY);
1031 NetOut.Write(GameVelX);
1032 NetOut.Write(GameVelY);
1033 NetOut.Write(GameAccelX);
1034 NetOut.Write(GameAccelY);
1035 end;
1037 g_Net_Host_Send(ID, Reliable, NET_CHAN_PLAYERPOS);
1038 end;
1040 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
1041 var
1042 P: TPlayer;
1043 I: Integer;
1044 begin
1045 P := g_Player_Get(PID);
1046 if P = nil then Exit;
1048 NetOut.Write(Byte(NET_MSG_PLRSTA));
1049 NetOut.Write(PID);
1051 with P do
1052 begin
1053 NetOut.Write(Byte(alive));
1054 NetOut.Write(Byte(GodMode));
1055 NetOut.Write(Health);
1056 NetOut.Write(Armor);
1057 NetOut.Write(Air);
1058 NetOut.Write(JetFuel);
1059 NetOut.Write(Lives);
1060 NetOut.Write(Team);
1062 for I := WP_FIRST to WP_LAST do
1063 NetOut.Write(Byte(FWeapon[I]));
1065 for I := A_BULLETS to A_HIGH do
1066 NetOut.Write(FAmmo[I]);
1068 for I := A_BULLETS to A_HIGH do
1069 NetOut.Write(FMaxAmmo[I]);
1071 for I := MR_SUIT to MR_MAX do
1072 NetOut.Write(LongWord(FMegaRulez[I]));
1074 NetOut.Write(Byte(R_ITEM_BACKPACK in FRulez));
1075 NetOut.Write(Byte(R_KEY_RED in FRulez));
1076 NetOut.Write(Byte(R_KEY_GREEN in FRulez));
1077 NetOut.Write(Byte(R_KEY_BLUE in FRulez));
1078 NetOut.Write(Byte(R_BERSERK in FRulez));
1080 NetOut.Write(Frags);
1081 NetOut.Write(Death);
1083 NetOut.Write(CurrWeap);
1085 NetOut.Write(Byte(FSpectator));
1086 NetOut.Write(Byte(FGhost));
1087 NetOut.Write(Byte(FPhysics));
1088 NetOut.Write(Byte(FNoRespawn));
1089 NetOut.Write(Byte(FJetpack));
1090 NetOut.Write(FFireTime);
1091 NetOut.Write(Byte(FFlaming));
1092 end;
1094 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1095 end;
1097 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
1098 begin
1099 NetOut.Write(Byte(NET_MSG_PLRDMG));
1100 NetOut.Write(PID);
1101 NetOut.Write(Kind);
1102 NetOut.Write(Attacker);
1103 NetOut.Write(Value);
1104 NetOut.Write(VX);
1105 NetOut.Write(VY);
1107 g_Net_Host_Send(ID, False, NET_CHAN_PLAYER);
1108 end;
1110 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
1111 begin
1112 NetOut.Write(Byte(NET_MSG_PLRDIE));
1113 NetOut.Write(PID);
1114 NetOut.Write(KillType);
1115 NetOut.Write(DeathType);
1116 NetOut.Write(Attacker);
1118 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1119 end;
1121 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
1122 begin
1123 NetOut.Write(Byte(NET_MSG_PLRFIRE));
1124 NetOut.Write(PID);
1125 NetOut.Write(Weapon);
1126 NetOut.Write(X);
1127 NetOut.Write(Y);
1128 NetOut.Write(AX);
1129 NetOut.Write(AY);
1130 NetOut.Write(ShotID);
1132 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
1133 end;
1135 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
1136 begin
1137 NetOut.Write(Byte(NET_MSG_PLRDEL));
1138 NetOut.Write(PID);
1140 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1141 end;
1143 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
1144 var
1145 Pl: TPlayer;
1146 begin
1147 Pl := g_Player_Get(PID);
1148 if Pl = nil then Exit;
1150 NetOut.Write(Byte(NET_MSG_PLRSET));
1151 NetOut.Write(PID);
1152 NetOut.Write(Pl.Name);
1153 if Mdl = '' then
1154 NetOut.Write(Pl.Model.Name)
1155 else
1156 NetOut.Write(Mdl);
1157 NetOut.Write(Pl.FColor.R);
1158 NetOut.Write(Pl.FColor.G);
1159 NetOut.Write(Pl.FColor.B);
1160 NetOut.Write(Pl.Team);
1162 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1163 end;
1165 // ITEM (SEND)
1167 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1168 var
1169 it: PItem;
1170 tt: Byte;
1171 begin
1172 it := g_Items_ByIdx(IID);
1174 NetOut.Write(Byte(NET_MSG_ISPAWN));
1175 NetOut.Write(IID);
1176 NetOut.Write(Byte(Quiet));
1177 tt := it.ItemType;
1178 if it.dropped then tt := tt or $80;
1179 NetOut.Write(tt);
1180 NetOut.Write(Byte(it.Fall));
1181 NetOut.Write(Byte(it.Respawnable));
1182 NetOut.Write(it.Obj.X);
1183 NetOut.Write(it.Obj.Y);
1184 NetOut.Write(it.Obj.Vel.X);
1185 NetOut.Write(it.Obj.Vel.Y);
1187 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1188 end;
1190 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1191 begin
1192 NetOut.Write(Byte(NET_MSG_IDEL));
1193 NetOut.Write(IID);
1194 NetOut.Write(Byte(Quiet));
1196 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1197 end;
1199 // PANEL
1201 procedure MH_SEND_PanelTexture(PGUID: Integer; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
1202 var
1203 TP: TPanel;
1204 begin
1205 TP := g_Map_PanelByGUID(PGUID);
1206 if (TP = nil) then exit;
1208 with TP do
1209 begin
1210 NetOut.Write(Byte(NET_MSG_PTEX));
1211 NetOut.Write(LongWord(PGUID));
1212 NetOut.Write(FCurTexture);
1213 NetOut.Write(FCurFrame);
1214 NetOut.Write(FCurFrameCount);
1215 NetOut.Write(AnimLoop);
1216 end;
1218 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1219 end;
1221 procedure MH_SEND_PanelState(PGUID: Integer; ID: Integer = NET_EVERYONE);
1222 var
1223 TP: TPanel;
1224 mpflags: Byte = 0;
1225 begin
1226 TP := g_Map_PanelByGUID(PGUID);
1227 if (TP = nil) then exit;
1229 NetOut.Write(Byte(NET_MSG_PSTATE));
1230 NetOut.Write(LongWord(PGUID));
1231 NetOut.Write(Byte(TP.Enabled));
1232 NetOut.Write(TP.LiftType);
1233 NetOut.Write(TP.X);
1234 NetOut.Write(TP.Y);
1235 NetOut.Write(Word(TP.Width));
1236 NetOut.Write(Word(TP.Height));
1237 // mplats
1238 NetOut.Write(LongInt(TP.movingSpeedX));
1239 NetOut.Write(LongInt(TP.movingSpeedY));
1240 NetOut.Write(LongInt(TP.movingStartX));
1241 NetOut.Write(LongInt(TP.movingStartY));
1242 NetOut.Write(LongInt(TP.movingEndX));
1243 NetOut.Write(LongInt(TP.movingEndY));
1244 NetOut.Write(LongInt(TP.sizeSpeedX));
1245 NetOut.Write(LongInt(TP.sizeSpeedY));
1246 NetOut.Write(LongInt(TP.sizeEndX));
1247 NetOut.Write(LongInt(TP.sizeEndY));
1248 if TP.movingActive then mpflags := mpflags or 1;
1249 if TP.moveOnce then mpflags := mpflags or 2;
1250 NetOut.Write(Byte(mpflags));
1252 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1253 end;
1255 // TRIGGER
1257 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
1258 begin
1259 if gTriggers = nil then Exit;
1260 if T.Sound = nil then Exit;
1262 NetOut.Write(Byte(NET_MSG_TSOUND));
1263 NetOut.Write(T.ClientID);
1264 NetOut.Write(Byte(T.Sound.IsPlaying));
1265 NetOut.Write(LongWord(T.Sound.GetPosition));
1266 NetOut.Write(T.SoundPlayCount);
1268 g_Net_Host_Send(ID, True);
1269 end;
1271 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
1272 begin
1273 NetOut.Write(Byte(NET_MSG_TMUSIC));
1274 NetOut.Write(gMusic.Name);
1275 NetOut.Write(Byte(gMusic.IsPlaying));
1276 NetOut.Write(LongWord(gMusic.GetPosition));
1277 NetOut.Write(Byte(gMusic.SpecPause or gMusic.IsPaused));
1279 g_Net_Host_Send(ID, True);
1280 end;
1282 // MONSTER
1284 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
1285 var
1286 M: TMonster;
1287 begin
1288 M := g_Monsters_ByUID(UID);
1289 if M = nil then
1290 Exit;
1292 with M do
1293 begin
1294 NetOut.Write(Byte(NET_MSG_MSPAWN));
1295 NetOut.Write(UID);
1296 NetOut.Write(MonsterType);
1297 NetOut.Write(MonsterState);
1298 NetOut.Write(MonsterAnim);
1299 NetOut.Write(MonsterTargetUID);
1300 NetOut.Write(MonsterTargetTime);
1301 NetOut.Write(MonsterBehaviour);
1302 NetOut.Write(MonsterSleep);
1303 NetOut.Write(MonsterHealth);
1304 NetOut.Write(MonsterAmmo);
1305 NetOut.Write(GameX);
1306 NetOut.Write(GameY);
1307 NetOut.Write(GameVelX);
1308 NetOut.Write(GameVelY);
1309 NetOut.Write(Byte(GameDirection));
1310 end;
1312 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1313 end;
1315 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
1316 var
1317 M: TMonster;
1318 begin
1319 M := g_Monsters_ByUID(UID);
1320 if M = nil then Exit;
1322 NetOut.Write(Byte(NET_MSG_MPOS));
1323 NetOut.Write(UID);
1325 with M do
1326 begin
1327 NetOut.Write(GameX);
1328 NetOut.Write(GameY);
1329 NetOut.Write(GameVelX);
1330 NetOut.Write(GameVelY);
1331 NetOut.Write(Byte(GameDirection));
1332 end;
1334 g_Net_Host_Send(ID, False, NET_CHAN_MONSTERPOS);
1335 end;
1337 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
1338 var
1339 M: TMonster;
1340 begin
1341 M := g_Monsters_ByUID(UID);
1342 if M = nil then Exit;
1344 NetOut.Write(Byte(NET_MSG_MSTATE));
1345 NetOut.Write(UID);
1347 with M do
1348 begin
1349 NetOut.Write(MonsterState);
1350 NetOut.Write(ForcedAnim);
1351 NetOut.Write(MonsterTargetUID);
1352 NetOut.Write(MonsterTargetTime);
1353 NetOut.Write(MonsterSleep);
1354 NetOut.Write(MonsterHealth);
1355 NetOut.Write(MonsterAmmo);
1356 NetOut.Write(MonsterPain);
1357 NetOut.Write(Byte(AnimIsReverse));
1358 NetOut.Write(FFireTime);
1359 end;
1361 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1362 end;
1364 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
1365 begin
1366 NetOut.Write(Byte(NET_MSG_MSHOT));
1367 NetOut.Write(UID);
1368 NetOut.Write(X);
1369 NetOut.Write(Y);
1370 NetOut.Write(VX);
1371 NetOut.Write(VY);
1373 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1374 end;
1376 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
1377 var
1378 M: TMonster;
1379 begin
1380 M := g_Monsters_ByUID(UID);
1381 if M = nil then Exit;
1383 NetOut.Write(Byte(NET_MSG_MDEL));
1384 NetOut.Write(UID);
1386 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1387 end;
1389 // MISC
1391 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
1392 begin
1393 NetOut.Write(Byte(NET_MSG_TIME_SYNC));
1394 NetOut.Write(Time);
1396 g_Net_Host_Send(ID, False, NET_CHAN_SERVICE);
1397 end;
1399 procedure MH_SEND_VoteEvent(EvType: Byte;
1400 StrArg1: string = 'a'; StrArg2: string = 'b';
1401 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
1402 ID: Integer = NET_EVERYONE);
1403 begin
1404 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
1405 NetOut.Write(EvType);
1406 NetOut.Write(IntArg1);
1407 NetOut.Write(IntArg2);
1408 NetOut.Write(StrArg1);
1409 NetOut.Write(StrArg2);
1411 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1412 end;
1414 // CLIENT MESSAGES //
1416 // GAME
1418 procedure MC_RECV_Chat(var M: TMsg);
1419 var
1420 Txt: string;
1421 Mode: Byte;
1422 begin
1423 Txt := M.ReadString();
1424 Mode := M.ReadByte();
1426 if Mode <> NET_CHAT_SYSTEM then
1427 begin
1428 if Mode = NET_CHAT_PLAYER then
1429 begin
1430 g_Console_Add(Txt, True);
1431 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1432 g_Game_ChatSound(b_Text_Unformat(Txt));
1433 end else
1434 if (Mode = NET_CHAT_TEAM) and (gPlayer1 <> nil) then
1435 begin
1436 if gPlayer1.Team = TEAM_RED then
1437 g_Console_Add(b_Text_Format('\r[Team] ') + Txt, True);
1438 if gPlayer1.Team = TEAM_BLUE then
1439 g_Console_Add(b_Text_Format('\b[Team] ') + Txt, True);
1440 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1441 g_Game_ChatSound(b_Text_Unformat(Txt));
1442 end;
1443 end else
1444 g_Console_Add(Txt, True);
1445 end;
1447 procedure MC_RECV_Effect(var M: TMsg);
1448 var
1449 Kind: Byte;
1450 X, Y: Integer;
1451 Ang: SmallInt;
1452 Anim: TAnimation;
1453 ID: LongWord;
1454 begin
1455 if not gGameOn then Exit;
1456 Kind := M.ReadByte();
1457 X := M.ReadLongInt();
1458 Y := M.ReadLongInt();
1459 Ang := M.ReadSmallInt();
1461 case Kind of
1462 NET_GFX_SPARK:
1463 g_GFX_Spark(X, Y, 2 + Random(2), Ang, 0, 0);
1465 NET_GFX_TELE:
1466 begin
1467 if g_Frames_Get(ID, 'FRAMES_TELEPORT') then
1468 begin
1469 Anim := TAnimation.Create(ID, False, 3);
1470 g_GFX_OnceAnim(X, Y, Anim);
1471 Anim.Free();
1472 end;
1473 if Ang = 1 then
1474 g_Sound_PlayExAt('SOUND_GAME_TELEPORT', X, Y);
1475 end;
1477 NET_GFX_EXPLODE:
1478 begin
1479 if g_Frames_Get(ID, 'FRAMES_EXPLODE_ROCKET') then
1480 begin
1481 Anim := TAnimation.Create(ID, False, 6);
1482 Anim.Blending := False;
1483 g_GFX_OnceAnim(X-64, Y-64, Anim);
1484 Anim.Free();
1485 end;
1486 if Ang = 1 then
1487 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEROCKET', X, Y);
1488 end;
1490 NET_GFX_BFGEXPL:
1491 begin
1492 if g_Frames_Get(ID, 'FRAMES_EXPLODE_BFG') then
1493 begin
1494 Anim := TAnimation.Create(ID, False, 6);
1495 Anim.Blending := False;
1496 g_GFX_OnceAnim(X-64, Y-64, Anim);
1497 Anim.Free();
1498 end;
1499 if Ang = 1 then
1500 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEBFG', X, Y);
1501 end;
1503 NET_GFX_BFGHIT:
1504 begin
1505 if g_Frames_Get(ID, 'FRAMES_BFGHIT') then
1506 begin
1507 Anim := TAnimation.Create(ID, False, 4);
1508 g_GFX_OnceAnim(X-32, Y-32, Anim);
1509 Anim.Free();
1510 end;
1511 end;
1513 NET_GFX_FIRE:
1514 begin
1515 if g_Frames_Get(ID, 'FRAMES_FIRE') then
1516 begin
1517 Anim := TAnimation.Create(ID, False, 4);
1518 g_GFX_OnceAnim(X, Y, Anim);
1519 Anim.Free();
1520 end;
1521 if Ang = 1 then
1522 g_Sound_PlayExAt('SOUND_FIRE', X, Y);
1523 end;
1525 NET_GFX_RESPAWN:
1526 begin
1527 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
1528 begin
1529 Anim := TAnimation.Create(ID, False, 4);
1530 g_GFX_OnceAnim(X, Y, Anim);
1531 Anim.Free();
1532 end;
1533 if Ang = 1 then
1534 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
1535 end;
1537 NET_GFX_SHELL1:
1538 g_Player_CreateShell(X, Y, 0, -2, SHELL_BULLET);
1540 NET_GFX_SHELL2:
1541 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1543 NET_GFX_SHELL3:
1544 begin
1545 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1546 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1547 end;
1548 end;
1549 end;
1551 procedure MC_RECV_Sound(var M: TMsg);
1552 var
1553 Name: string;
1554 X, Y: Integer;
1555 Pos: Boolean;
1556 begin
1557 Name := M.ReadString();
1558 Pos := M.ReadByte() <> 0;
1559 if Pos then
1560 begin
1561 X := M.ReadLongInt();
1562 Y := M.ReadLongInt();
1563 g_Sound_PlayExAt(Name, X, Y);
1564 end
1565 else
1566 g_Sound_PlayEx(Name);
1567 end;
1569 procedure MC_RECV_CreateShot(var M: TMsg);
1570 var
1571 I, X, Y, XV, YV: Integer;
1572 Timeout: LongWord;
1573 Target, Spawner: Word;
1574 ShType: Byte;
1575 begin
1576 I := M.ReadLongInt();
1577 ShType := M.ReadByte();
1578 Target := M.ReadWord();
1579 Spawner := M.ReadWord();
1580 Timeout := M.ReadLongWord();
1581 X := M.ReadLongInt();
1582 Y := M.ReadLongInt();
1583 XV := M.ReadLongInt();
1584 YV := M.ReadLongInt();
1586 I := g_Weapon_CreateShot(I, ShType, Spawner, Target, X, Y, XV, YV);
1587 if (Shots <> nil) and (I <= High(Shots)) then
1588 begin
1589 Shots[I].Timeout := Timeout;
1590 //Shots[I].Target := Target; // TODO: find a use for Target later
1591 end;
1592 end;
1594 procedure MC_RECV_UpdateShot(var M: TMsg);
1595 var
1596 I, TX, TY, TXV, TYV: Integer;
1597 begin
1598 I := M.ReadLongInt();
1599 TX := M.ReadLongInt();
1600 TY := M.ReadLongInt();
1601 TXV := M.ReadLongInt();
1602 TYV := M.ReadLongInt();
1604 if (Shots <> nil) and (I <= High(Shots)) then
1605 with (Shots[i]) do
1606 begin
1607 Obj.X := TX;
1608 Obj.Y := TY;
1609 Obj.Vel.X := TXV;
1610 Obj.Vel.Y := TYV;
1611 end;
1612 end;
1614 procedure MC_RECV_DeleteShot(var M: TMsg);
1615 var
1616 I, X, Y: Integer;
1617 L: Boolean;
1618 begin
1619 if not gGameOn then Exit;
1620 I := M.ReadLongInt();
1621 L := (M.ReadByte() <> 0);
1622 X := M.ReadLongInt();
1623 Y := M.ReadLongInt();
1625 g_Weapon_DestroyShot(I, X, Y, L);
1626 end;
1628 procedure MC_RECV_GameStats(var M: TMsg);
1629 begin
1630 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
1631 begin
1632 gTeamStat[TEAM_RED].Goals := M.ReadSmallInt();
1633 gTeamStat[TEAM_BLUE].Goals := M.ReadSmallInt();
1634 end
1635 else
1636 if gGameSettings.GameMode = GM_COOP then
1637 begin
1638 gCoopMonstersKilled := M.ReadWord();
1639 gCoopSecretsFound := M.ReadWord();
1640 end;
1641 end;
1643 procedure MC_RECV_CoopStats(var M: TMsg);
1644 begin
1645 gTotalMonsters := M.ReadLongInt();
1646 gSecretsCount := M.ReadLongInt();
1647 gCoopTotalMonstersKilled := M.ReadWord();
1648 gCoopTotalSecretsFound := M.ReadWord();
1649 gCoopTotalMonsters := M.ReadWord();
1650 gCoopTotalSecrets := M.ReadWord();
1651 end;
1653 procedure MC_RECV_GameEvent(var M: TMsg);
1654 var
1655 EvType: Byte;
1656 EvNum: Integer;
1657 EvStr: string;
1658 EvTime: LongWord;
1659 BHash: Boolean;
1660 EvHash: TMD5Digest;
1661 pl: TPlayer;
1662 i1, i2: TStrings_Locale;
1663 pln: String;
1664 cnt: Byte;
1665 begin
1666 FillChar(EvHash, Sizeof(EvHash), 0);
1667 EvType := M.ReadByte();
1668 EvNum := M.ReadLongInt();
1669 EvStr := M.ReadString();
1670 gLastMap := M.ReadByte() <> 0;
1671 if gLastMap and (gGameSettings.GameMode = GM_COOP) then gStatsOff := True;
1672 gStatsPressed := True;
1673 EvTime := M.ReadLongWord();
1674 BHash := M.ReadByte() <> 0;
1675 if BHash then
1676 EvHash := M.ReadMD5();
1678 gTime := EvTime;
1680 case EvType of
1681 NET_EV_MAPSTART:
1682 begin
1683 gGameOn := False;
1684 g_Game_ClearLoading();
1685 g_Game_StopAllSounds(True);
1687 gSwitchGameMode := Byte(EvNum);
1688 gGameSettings.GameMode := gSwitchGameMode;
1690 gWADHash := EvHash;
1691 if not g_Game_StartMap(EvStr, True) then
1692 begin
1693 if not isWadPath(EvStr) then
1694 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [gGameSettings.WAD + ':\' + EvStr]))
1695 else
1696 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [EvStr]));
1697 Exit;
1698 end;
1700 MC_SEND_FullStateRequest;
1701 end;
1703 NET_EV_MAPEND:
1704 begin
1705 gMissionFailed := EvNum <> 0;
1706 gExit := EXIT_ENDLEVELCUSTOM;
1707 end;
1709 NET_EV_RCON:
1710 begin
1711 case EvNum of
1712 NET_RCON_NOAUTH:
1713 g_Console_Add(_lc[I_NET_RCON_NOAUTH], True);
1714 NET_RCON_PWGOOD:
1715 g_Console_Add(_lc[I_NET_RCON_PWD_VALID], True);
1716 NET_RCON_PWBAD:
1717 g_Console_Add(_lc[I_NET_RCON_PWD_INVALID], True);
1718 end;
1719 end;
1721 NET_EV_CHANGE_TEAM:
1722 begin
1723 if EvNum = TEAM_RED then
1724 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [EvStr]), True);
1725 if EvNum = TEAM_BLUE then
1726 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [EvStr]), True);
1727 end;
1729 NET_EV_PLAYER_KICK:
1730 g_Console_Add(Format(_lc[I_PLAYER_KICK], [EvStr]), True);
1732 NET_EV_PLAYER_BAN:
1733 g_Console_Add(Format(_lc[I_PLAYER_BAN], [EvStr]), True);
1735 NET_EV_LMS_WARMUP:
1736 g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [EvNum]), True);
1738 NET_EV_LMS_SURVIVOR:
1739 g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True);
1741 NET_EV_BIGTEXT:
1742 g_Game_Message(AnsiUpperCase(EvStr), Word(EvNum));
1744 NET_EV_SCORE:
1745 begin
1746 pl := g_Player_Get(EvNum and $FFFF);
1747 if pl = nil then
1748 pln := '?'
1749 else
1750 pln := pl.Name;
1751 cnt := (EvNum shr 16) and $FF;
1752 if Pos('w', EvStr) = 0 then
1753 begin
1754 // Default score
1755 if Pos('t', EvStr) = 0 then
1756 begin
1757 // Player +/- score
1758 if Pos('-', EvStr) = 0 then
1759 begin
1760 if Pos('e', EvStr) = 0 then
1761 i1 := I_PLAYER_SCORE_ADD_OWN
1762 else
1763 i1 := I_PLAYER_SCORE_ADD_ENEMY;
1764 end else
1765 begin
1766 if Pos('e', EvStr) = 0 then
1767 i1 := I_PLAYER_SCORE_SUB_OWN
1768 else
1769 i1 := I_PLAYER_SCORE_SUB_ENEMY;
1770 end;
1771 // Which team
1772 if Pos('r', EvStr) > 0 then
1773 i2 := I_PLAYER_SCORE_TO_RED
1774 else
1775 i2 := I_PLAYER_SCORE_TO_BLUE;
1776 g_Console_Add(Format(_lc[i1], [pln, cnt, _lc[i2]]), True);
1777 end else
1778 begin
1779 // Team +/- score
1780 if Pos('-', EvStr) = 0 then
1781 i1 := I_PLAYER_SCORE_ADD_TEAM
1782 else
1783 i1 := I_PLAYER_SCORE_SUB_TEAM;
1784 // Which team
1785 if Pos('r', EvStr) > 0 then
1786 i2 := I_PLAYER_SCORE_RED
1787 else
1788 i2 := I_PLAYER_SCORE_BLUE;
1789 g_Console_Add(Format(_lc[i1], [_lc[i2], cnt]), True);
1790 end;
1791 end else
1792 begin
1793 // Game Win
1794 if Pos('e', EvStr) = 0 then
1795 i1 := I_PLAYER_SCORE_WIN_OWN
1796 else
1797 i1 := I_PLAYER_SCORE_WIN_ENEMY;
1798 // Which team
1799 if Pos('r', EvStr) > 0 then
1800 i2 := I_PLAYER_SCORE_TO_RED
1801 else
1802 i2 := I_PLAYER_SCORE_TO_BLUE;
1803 g_Console_Add(Format(_lc[i1], [pln, _lc[i2]]), True);
1804 end;
1805 end;
1807 NET_EV_SCORE_MSG:
1808 begin
1809 if EvNum = TEAM_RED then
1810 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1811 if EvNum = TEAM_BLUE then
1812 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1813 if EvNum = -TEAM_RED then
1814 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1815 if EvNum = -TEAM_BLUE then
1816 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1817 end;
1819 NET_EV_LMS_START:
1820 begin
1821 g_Player_RemoveAllCorpses;
1822 g_Game_Message(_lc[I_MESSAGE_LMS_START], 144);
1823 end;
1825 NET_EV_LMS_WIN:
1826 g_Game_Message(Format(_lc[I_MESSAGE_LMS_WIN], [AnsiUpperCase(EvStr)]), 144);
1828 NET_EV_TLMS_WIN:
1829 begin
1830 if EvNum = TEAM_RED then
1831 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 144);
1832 if EvNum = TEAM_BLUE then
1833 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 144);
1834 end;
1836 NET_EV_LMS_LOSE:
1837 g_Game_Message(_lc[I_MESSAGE_LMS_LOSE], 144);
1839 NET_EV_LMS_DRAW:
1840 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
1842 NET_EV_KILLCOMBO:
1843 g_Game_Announce_KillCombo(EvNum);
1845 NET_EV_PLAYER_TOUCH:
1846 begin
1847 pl := g_Player_Get(EvNum);
1848 if pl <> nil then
1849 pl.Touch();
1850 end;
1852 NET_EV_SECRET:
1853 begin
1854 pl := g_Player_Get(EvNum);
1855 if pl <> nil then
1856 begin
1857 g_Console_Add(Format(_lc[I_PLAYER_SECRET], [pl.Name]), True);
1858 g_Sound_PlayEx('SOUND_GAME_SECRET');
1859 end;
1860 end;
1862 NET_EV_INTER_READY:
1863 begin
1864 pl := g_Player_Get(EvNum);
1865 if pl <> nil then pl.FReady := (EvStr = 'Y');
1866 end;
1867 end;
1868 end;
1870 procedure MC_RECV_FlagEvent(var M: TMsg);
1871 var
1872 PID: Word;
1873 Pl: TPlayer;
1874 EvType: Byte;
1875 Fl, a: Byte;
1876 Quiet: Boolean;
1877 s, ts: string;
1878 begin
1879 EvType := M.ReadByte();
1880 Fl := M.ReadByte();
1882 if Fl = FLAG_NONE then Exit;
1884 Quiet := (M.ReadByte() <> 0);
1885 PID := M.ReadWord();
1887 gFlags[Fl].State := M.ReadByte();
1888 gFlags[Fl].CaptureTime := M.ReadLongWord();
1889 gFlags[Fl].Obj.X := M.ReadLongInt();
1890 gFlags[Fl].Obj.Y := M.ReadLongInt();
1891 gFlags[Fl].Obj.Vel.X := M.ReadLongInt();
1892 gFlags[Fl].Obj.Vel.Y := M.ReadLongInt();
1894 Pl := g_Player_Get(PID);
1895 if (Pl = nil) and
1896 (EvType <> FLAG_STATE_NORMAL) and
1897 (EvType <> FLAG_STATE_DROPPED) and
1898 (EvType <> FLAG_STATE_RETURNED) then
1899 Exit;
1901 case EvType of
1902 FLAG_STATE_NORMAL:
1903 begin
1904 if Quiet or (Pl = nil) then Exit;
1906 if Fl = FLAG_RED then
1907 s := _lc[I_PLAYER_FLAG_RED]
1908 else
1909 s := _lc[I_PLAYER_FLAG_BLUE];
1911 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1913 if ((Pl = gPlayer1) or (Pl = gPlayer2)
1914 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
1915 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
1916 a := 0
1917 else
1918 a := 1;
1920 if not sound_ret_flag[a].IsPlaying() then
1921 sound_ret_flag[a].Play();
1922 end;
1924 FLAG_STATE_CAPTURED:
1925 begin
1926 if (Pl <> nil) then Pl.SetFlag(Fl);
1928 if Quiet then Exit;
1930 if Fl = FLAG_RED then
1931 s := _lc[I_PLAYER_FLAG_RED]
1932 else
1933 s := _lc[I_PLAYER_FLAG_BLUE];
1935 g_Console_Add(Format(_lc[I_PLAYER_FLAG_GET], [Pl.Name, s]), True);
1936 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_GET], [AnsiUpperCase(s)]), 144);
1938 if ((Pl = gPlayer1) or (Pl = gPlayer2)
1939 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
1940 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
1941 a := 0
1942 else
1943 a := 1;
1945 if not sound_get_flag[a].IsPlaying() then
1946 sound_get_flag[a].Play();
1947 end;
1949 FLAG_STATE_DROPPED:
1950 begin
1951 if (Pl <> nil) then Pl.SetFlag(FLAG_NONE);
1953 if Quiet or (Pl = nil) then Exit;
1955 if Fl = FLAG_RED then
1956 s := _lc[I_PLAYER_FLAG_RED]
1957 else
1958 s := _lc[I_PLAYER_FLAG_BLUE];
1960 g_Console_Add(Format(_lc[I_PLAYER_FLAG_DROP], [Pl.Name, s]), True);
1961 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_DROP], [AnsiUpperCase(s)]), 144);
1963 if ((Pl = gPlayer1) or (Pl = gPlayer2)
1964 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
1965 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
1966 a := 0
1967 else
1968 a := 1;
1970 if not sound_lost_flag[a].IsPlaying() then
1971 sound_lost_flag[a].Play();
1972 end;
1974 FLAG_STATE_SCORED:
1975 begin
1976 g_Map_ResetFlag(FLAG_RED);
1977 g_Map_ResetFlag(FLAG_BLUE);
1978 if Quiet or (Pl = nil) then Exit;
1979 Pl.SetFlag(FLAG_NONE);
1981 if Fl = FLAG_RED then
1982 s := _lc[I_PLAYER_FLAG_RED]
1983 else
1984 s := _lc[I_PLAYER_FLAG_BLUE];
1986 ts := Format('%.4d', [gFlags[Fl].CaptureTime]);
1987 Insert('.', ts, Length(ts) + 1 - 3);
1988 g_Console_Add(Format(_lc[I_PLAYER_FLAG_CAPTURE], [Pl.Name, s, ts]), True);
1989 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_CAPTURE], [AnsiUpperCase(s)]), 144);
1991 if ((Pl = gPlayer1) or (Pl = gPlayer2)
1992 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
1993 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
1994 a := 0
1995 else
1996 a := 1;
1998 if not sound_cap_flag[a].IsPlaying() then
1999 sound_cap_flag[a].Play();
2000 end;
2002 FLAG_STATE_RETURNED:
2003 begin
2004 g_Map_ResetFlag(Fl);
2005 if Quiet then Exit;
2007 if Fl = FLAG_RED then
2008 s := _lc[I_PLAYER_FLAG_RED]
2009 else
2010 s := _lc[I_PLAYER_FLAG_BLUE];
2012 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2014 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2015 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2016 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2017 a := 0
2018 else
2019 a := 1;
2021 if not sound_ret_flag[a].IsPlaying() then
2022 sound_ret_flag[a].Play();
2023 end;
2024 end;
2025 end;
2027 procedure MC_RECV_GameSettings(var M: TMsg);
2028 begin
2029 gGameSettings.GameMode := M.ReadByte();
2030 gGameSettings.GoalLimit := M.ReadWord();
2031 gGameSettings.TimeLimit := M.ReadWord();
2032 gGameSettings.MaxLives := M.ReadByte();
2033 gGameSettings.Options := M.ReadLongWord();
2034 end;
2036 // PLAYER
2038 function MC_RECV_PlayerCreate(var M: TMsg): Word;
2039 var
2040 PID, DID: Word;
2041 PName, Model: string;
2042 Color: TRGB;
2043 T: Byte;
2044 Pl: TPlayer;
2045 begin
2046 PID := M.ReadWord();
2047 Pl := g_Player_Get(PID);
2049 PName := M.ReadString();
2050 Model := M.ReadString();
2051 Color.R := M.ReadByte();
2052 Color.G := M.ReadByte();
2053 Color.B := M.ReadByte();
2054 T := M.ReadByte();
2056 Result := 0;
2057 if (PID <> NetPlrUID1) and (PID <> NetPlrUID2) then
2058 begin
2059 if (Pl <> nil) then Exit;
2060 DID := g_Player_Create(Model, Color, T, False);
2061 with g_Player_Get(DID) do
2062 begin
2063 UID := PID;
2064 Name := PName;
2065 Reset(True);
2066 end;
2067 end
2068 else
2069 begin
2070 if (PID = NetPlrUID1) and (gPlayer1 <> nil) then begin
2071 gPlayer1.UID := PID;
2072 gPlayer1.Model.SetColor(Color.R, Color.G, Color.B);
2073 gPlayer1.ChangeTeam(T);
2074 end;
2075 if (PID = NetPlrUID2) and (gPlayer2 <> nil) then begin
2076 gPlayer2.UID := PID;
2077 gPlayer2.Model.SetColor(Color.R, Color.G, Color.B);
2078 gPlayer2.ChangeTeam(T);
2079 end;
2080 end;
2082 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
2083 e_WriteLog('NET: Player ' + PName + ' [' + IntToStr(PID) + '] added.', TMsgType.Notify);
2084 Result := PID;
2085 end;
2087 function MC_RECV_PlayerPos(var M: TMsg): Word;
2088 var
2089 GT: LongWord;
2090 PID: Word;
2091 kByte: Word;
2092 Pl: TPlayer;
2093 Dir: Byte;
2094 TmpX, TmpY: Integer;
2095 begin
2096 Result := 0;
2098 GT := M.ReadLongWord();
2099 if GT < gTime - NET_MAX_DIFFTIME then
2100 begin
2101 gTime := GT;
2102 Exit;
2103 end;
2104 gTime := GT;
2106 PID := M.ReadWord();
2107 Pl := g_Player_Get(PID);
2109 if Pl = nil then Exit;
2111 Result := PID;
2113 with Pl do
2114 begin
2115 FPing := M.ReadWord();
2116 FLoss := M.ReadByte();
2117 kByte := M.ReadWord();
2118 Dir := M.ReadByte();
2120 TmpX := M.ReadLongInt();
2121 TmpY := M.ReadLongInt();
2123 ReleaseKeys;
2125 if (kByte = NET_KEY_CHAT) then
2126 PressKey(KEY_CHAT, 10000)
2127 else
2128 begin
2129 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
2130 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
2131 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
2132 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
2133 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
2134 end;
2136 if ((Pl <> gPlayer1) and (Pl <> gPlayer2)) or LongBool(kByte and NET_KEY_FORCEDIR) then
2137 SetDirection(TDirection(Dir));
2139 GameVelX := M.ReadLongInt();
2140 GameVelY := M.ReadLongInt();
2141 GameAccelX := M.ReadLongInt();
2142 GameAccelY := M.ReadLongInt();
2143 SetLerp(TmpX, TmpY);
2144 if NetForcePlayerUpdate then Update();
2145 end;
2146 end;
2148 function MC_RECV_PlayerStats(var M: TMsg): Word;
2149 var
2150 PID: Word;
2151 Pl: TPlayer;
2152 I, OldFire: Integer;
2153 OldJet, Flam: Boolean;
2154 NewTeam: Byte;
2155 begin
2156 PID := M.ReadWord();
2157 Pl := g_Player_Get(PID);
2158 Result := 0;
2159 if Pl = nil then
2160 Exit;
2162 with Pl do
2163 begin
2164 alive := (M.ReadByte() <> 0);
2165 GodMode := (M.ReadByte() <> 0);
2166 Health := M.ReadLongInt();
2167 Armor := M.ReadLongInt();
2168 Air := M.ReadLongInt();
2169 JetFuel := M.ReadLongInt();
2170 Lives := M.ReadByte();
2171 NewTeam := M.ReadByte();
2173 for I := WP_FIRST to WP_LAST do
2174 FWeapon[I] := (M.ReadByte() <> 0);
2176 for I := A_BULLETS to A_HIGH do
2177 FAmmo[I] := M.ReadWord();
2179 for I := A_BULLETS to A_HIGH do
2180 FMaxAmmo[I] := M.ReadWord();
2182 for I := MR_SUIT to MR_MAX do
2183 FMegaRulez[I] := M.ReadLongWord();
2185 FRulez := [];
2186 if (M.ReadByte() <> 0) then
2187 FRulez := FRulez + [R_ITEM_BACKPACK];
2188 if (M.ReadByte() <> 0) then
2189 FRulez := FRulez + [R_KEY_RED];
2190 if (M.ReadByte() <> 0) then
2191 FRulez := FRulez + [R_KEY_GREEN];
2192 if (M.ReadByte() <> 0) then
2193 FRulez := FRulez + [R_KEY_BLUE];
2194 if (M.ReadByte() <> 0) then
2195 FRulez := FRulez + [R_BERSERK];
2197 Frags := M.ReadLongInt();
2198 Death := M.ReadLongInt();
2200 SetWeapon(M.ReadByte());
2202 FSpectator := M.ReadByte() <> 0;
2203 if FSpectator then
2204 begin
2205 if Pl = gPlayer1 then
2206 begin
2207 gLMSPID1 := UID;
2208 gPlayer1 := nil;
2209 end;
2210 if Pl = gPlayer2 then
2211 begin
2212 gLMSPID2 := UID;
2213 gPlayer2 := nil;
2214 end;
2215 end
2216 else
2217 begin
2218 if (gPlayer1 = nil) and (gLMSPID1 > 0) then
2219 gPlayer1 := g_Player_Get(gLMSPID1);
2220 if (gPlayer2 = nil) and (gLMSPID2 > 0) then
2221 gPlayer2 := g_Player_Get(gLMSPID2);
2222 end;
2223 FGhost := M.ReadByte() <> 0;
2224 FPhysics := M.ReadByte() <> 0;
2225 FNoRespawn := M.ReadByte() <> 0;
2226 OldJet := FJetpack;
2227 FJetpack := M.ReadByte() <> 0;
2228 OldFire := FFireTime;
2229 FFireTime := M.ReadLongInt();
2230 if (OldFire <= 0) and (FFireTime > 0) then
2231 g_Sound_PlayExAt('SOUND_IGNITE', Obj.X, Obj.Y);
2232 Flam := M.ReadByte() <> 0;
2233 if OldJet and not FJetpack then
2234 JetpackOff
2235 else if not OldJet and FJetpack then
2236 JetpackOn;
2237 if FFlaming and not Flam then
2238 FlamerOff;
2239 if Team <> NewTeam then
2240 Pl.ChangeTeam(NewTeam);
2241 end;
2243 Result := PID;
2244 end;
2246 function MC_RECV_PlayerDamage(var M: TMsg): Word;
2247 var
2248 PID: Word;
2249 Pl: TPlayer;
2250 Kind: Byte;
2251 Attacker, Value: Word;
2252 VX, VY: Integer;
2253 begin
2254 Result := 0;
2255 if not gGameOn then Exit;
2256 PID := M.ReadWord();
2257 Pl := g_Player_Get(PID);
2258 if Pl = nil then Exit;
2260 Kind := M.ReadByte();
2261 Attacker := M.ReadWord();
2262 Value := M.ReadWord();
2263 VX := M.ReadWord();
2264 VY := M.ReadWord();
2266 with Pl do
2267 Damage(Value, Attacker, VX, VY, Kind);
2269 Result := PID;
2270 end;
2272 function MC_RECV_PlayerDeath(var M: TMsg): Word;
2273 var
2274 PID: Word;
2275 Pl: TPlayer;
2276 KillType, DeathType: Byte;
2277 Attacker: Word;
2278 begin
2279 Result := 0;
2280 if not gGameOn then Exit;
2281 PID := M.ReadWord();
2282 Pl := g_Player_Get(PID);
2283 if Pl = nil then Exit;
2285 KillType := M.ReadByte();
2286 DeathType := M.ReadByte();
2287 Attacker := M.ReadWord();
2289 with Pl do
2290 begin
2291 Kill(KillType, Attacker, DeathType);
2292 SoftReset;
2293 end;
2294 end;
2296 function MC_RECV_PlayerDelete(var M: TMsg): Word;
2297 var
2298 PID: Word;
2299 Pl: TPlayer;
2300 begin
2301 PID := M.ReadWord();
2302 Pl := g_Player_Get(PID);
2303 Result := 0;
2304 if Pl = nil then Exit;
2306 g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [Pl.Name]), True);
2307 e_WriteLog('NET: Player ' + Pl.Name + ' [' + IntToStr(PID) + '] removed.', TMsgType.Notify);
2309 g_Player_Remove(PID);
2311 Result := PID;
2312 end;
2314 function MC_RECV_PlayerFire(var M: TMsg): Word;
2315 var
2316 PID: Word;
2317 Weap: Byte;
2318 Pl: TPlayer;
2319 X, Y, AX, AY: Integer;
2320 SHID: Integer;
2321 begin
2322 Result := 0;
2323 if not gGameOn then Exit;
2324 PID := M.ReadWord();
2325 Pl := g_Player_Get(PID);
2326 if Pl = nil then Exit;
2328 Weap := M.ReadByte();
2329 X := M.ReadLongInt();
2330 Y := M.ReadLongInt();
2331 AX := M.ReadLongInt();
2332 AY := M.ReadLongInt();
2333 SHID := M.ReadLongInt();
2335 with Pl do
2336 if alive then NetFire(Weap, X, Y, AX, AY, SHID);
2337 end;
2339 procedure MC_RECV_PlayerSettings(var M: TMsg);
2340 var
2341 TmpName: string;
2342 TmpModel: string;
2343 TmpColor: TRGB;
2344 TmpTeam: Byte;
2345 Pl: TPlayer;
2346 PID: Word;
2347 begin
2348 PID := M.ReadWord();
2349 Pl := g_Player_Get(PID);
2350 if Pl = nil then Exit;
2352 TmpName := M.ReadString();
2353 TmpModel := M.ReadString();
2354 TmpColor.R := M.ReadByte();
2355 TmpColor.G := M.ReadByte();
2356 TmpColor.B := M.ReadByte();
2357 TmpTeam := M.ReadByte();
2359 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
2360 begin
2361 Pl.ChangeTeam(TmpTeam);
2362 if gPlayer1 = Pl then
2363 gPlayer1Settings.Team := TmpTeam;
2364 if gPlayer2 = Pl then
2365 gPlayer2Settings.Team := TmpTeam;
2366 end else
2367 Pl.SetColor(TmpColor);
2369 if Pl.Name <> TmpName then
2370 begin
2371 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
2372 Pl.Name := TmpName;
2373 end;
2375 if TmpModel <> Pl.Model.Name then
2376 Pl.SetModel(TmpModel);
2377 end;
2379 // ITEM
2381 procedure MC_RECV_ItemSpawn(var M: TMsg);
2382 var
2383 ID: Word;
2384 AID: DWord;
2385 X, Y, VX, VY: Integer;
2386 T: Byte;
2387 Quiet, Fall{, Resp}: Boolean;
2388 Anim: TAnimation;
2389 it: PItem;
2390 begin
2391 if not gGameOn then Exit;
2392 ID := M.ReadWord();
2393 Quiet := M.ReadByte() <> 0;
2394 T := M.ReadByte();
2395 Fall := M.ReadByte() <> 0;
2396 {Resp :=} M.ReadByte();
2397 X := M.ReadLongInt();
2398 Y := M.ReadLongInt();
2399 VX := M.ReadLongInt();
2400 VY := M.ReadLongInt();
2402 g_Items_Create(X, Y, T and $7F, Fall, False, False, ID);
2403 if ((T and $80) <> 0) then g_Items_SetDrop(ID);
2405 it := g_Items_ByIdx(ID);
2406 it.Obj.Vel.X := VX;
2407 it.Obj.Vel.Y := VY;
2409 if not Quiet then
2410 begin
2411 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
2412 if g_Frames_Get(AID, 'FRAMES_ITEM_RESPAWN') then
2413 begin
2414 Anim := TAnimation.Create(AID, False, 4);
2415 g_GFX_OnceAnim(X+(it.Obj.Rect.Width div 2)-16, Y+(it.Obj.Rect.Height div 2)-16, Anim);
2416 Anim.Free();
2417 end;
2418 end;
2419 end;
2421 procedure MC_RECV_ItemDestroy(var M: TMsg);
2422 var
2423 ID: Word;
2424 Quiet: Boolean;
2425 begin
2426 if not gGameOn then Exit;
2427 ID := M.ReadWord();
2428 Quiet := M.ReadByte() <> 0;
2430 if not g_Items_ValidId(ID) then exit;
2432 if not Quiet then g_Items_EmitPickupSound(ID);
2434 g_Items_Remove(ID);
2435 end;
2437 // PANEL
2439 procedure MC_RECV_PanelTexture(var M: TMsg);
2440 var
2441 TP: TPanel;
2442 PGUID: Integer;
2443 Tex, Fr: Integer;
2444 Loop, Cnt: Byte;
2445 begin
2446 if not gGameOn then Exit;
2448 PGUID := Integer(M.ReadLongWord());
2449 Tex := M.ReadLongInt();
2450 Fr := M.ReadLongInt();
2451 Cnt := M.ReadByte();
2452 Loop := M.ReadByte();
2454 TP := g_Map_PanelByGUID(PGUID);
2455 if (TP <> nil) then
2456 begin
2457 // switch texture
2458 TP.SetTexture(Tex, Loop);
2459 TP.SetFrame(Fr, Cnt);
2460 end;
2461 end;
2463 procedure MC_RECV_PanelState(var M: TMsg);
2464 var
2465 PGUID: Integer;
2466 E: Boolean;
2467 Lift: Byte;
2468 X, Y, W, H: Integer;
2469 TP: TPanel;
2470 speedX, speedY, startX, startY, endX, endY: Integer;
2471 sizeSpX, sizeSpY, sizeEX, sizeEY: Integer;
2472 mpflags: Byte;
2473 begin
2474 if not gGameOn then Exit;
2476 PGUID := Integer(M.ReadLongWord());
2477 E := (M.ReadByte() <> 0);
2478 Lift := M.ReadByte();
2479 X := M.ReadLongInt();
2480 Y := M.ReadLongInt();
2481 W := M.ReadWord();
2482 H := M.ReadWord();
2483 // mplats
2484 speedX := M.ReadLongInt();
2485 speedY := M.ReadLongInt();
2486 startX := M.ReadLongInt();
2487 startY := M.ReadLongInt();
2488 endX := M.ReadLongInt();
2489 endY := M.ReadLongInt();
2490 sizeSpX := M.ReadLongInt();
2491 sizeSpY := M.ReadLongInt();
2492 sizeEX := M.ReadLongInt();
2493 sizeEY := M.ReadLongInt();
2494 mpflags := M.ReadByte(); // bit0: TP.movingActive; bit1: TP.moveOnce
2496 TP := g_Map_PanelByGUID(PGUID);
2497 if (TP = nil) then exit;
2499 // update lifts state
2500 if TP.isGLift then g_Map_SetLiftGUID(PGUID, Lift);
2502 // update enabled/disabled state for all panels
2503 if E then g_Map_EnableWallGUID(PGUID) else g_Map_DisableWallGUID(PGUID);
2505 // update panel position, as it can be moved (mplat)
2506 TP.X := X;
2507 TP.Y := Y;
2508 TP.Width := W;
2509 TP.Height := H;
2510 // update mplat state
2511 TP.movingSpeedX := speedX;
2512 TP.movingSpeedY := speedY;
2513 TP.movingStartX := startX;
2514 TP.movingStartY := startY;
2515 TP.movingEndX := endX;
2516 TP.movingEndY := endY;
2517 TP.sizeSpeedX := sizeSpX;
2518 TP.sizeSpeedY := sizeSpY;
2519 TP.sizeEndX := sizeEX;
2520 TP.sizeEndY := sizeEY;
2521 TP.movingActive := ((mpflags and 1) <> 0);
2522 TP.moveOnce := ((mpflags and 2) <> 0);
2523 // notify panel of it's position/size change, so it can fix other internal structures
2524 TP.positionChanged();
2525 end;
2527 // TRIGGERS
2529 procedure MC_RECV_TriggerSound(var M: TMsg);
2530 var
2531 SPlaying: Boolean;
2532 SPos, SID: LongWord;
2533 SCount: LongInt;
2534 I: Integer;
2535 begin
2536 if not gGameOn then Exit;
2537 if gTriggers = nil then Exit;
2539 SID := M.ReadLongWord();
2540 SPlaying := M.ReadByte() <> 0;
2541 SPos := M.ReadLongWord();
2542 SCount := M.ReadLongInt();
2544 for I := Low(gTriggers) to High(gTriggers) do
2545 if gTriggers[I].TriggerType = TRIGGER_SOUND then
2546 if gTriggers[I].ClientID = SID then
2547 with gTriggers[I] do
2548 begin
2549 if Sound <> nil then
2550 begin
2551 if SPlaying then
2552 begin
2553 if tgcLocal then
2554 Sound.PlayVolumeAt(X+(Width div 2), Y+(Height div 2), tgcVolume/255.0)
2555 else
2556 Sound.PlayPanVolume((tgcPan-127.0)/128.0, tgcVolume/255.0);
2557 Sound.SetPosition(SPos);
2558 end
2559 else
2560 if Sound.IsPlaying then Sound.Stop;
2561 end;
2563 SoundPlayCount := SCount;
2564 end;
2565 end;
2567 procedure MC_RECV_TriggerMusic(var M: TMsg);
2568 var
2569 MName: string;
2570 MPlaying: Boolean;
2571 MPos: LongWord;
2572 MPaused: Boolean;
2573 begin
2574 if not gGameOn then Exit;
2576 MName := M.ReadString();
2577 MPlaying := M.ReadByte() <> 0;
2578 MPos := M.ReadLongWord();
2579 MPaused := M.ReadByte() <> 0;
2581 if MPlaying then
2582 begin
2583 gMusic.SetByName(MName);
2584 gMusic.Play(True);
2585 gMusic.SetPosition(MPos);
2586 gMusic.SpecPause := MPaused;
2587 end
2588 else
2589 if gMusic.IsPlaying then gMusic.Stop;
2590 end;
2592 // MONSTERS
2594 procedure MC_RECV_MonsterSpawn(var M: TMsg);
2595 var
2596 ID: Word;
2597 MType, MState, MDir, MAnim, MBehav: Byte;
2598 X, Y, VX, VY, MTargTime, MHealth, MAmmo, MSleep: Integer;
2599 MTarg: Word;
2600 Mon: TMonster;
2601 begin
2602 ID := M.ReadWord();
2603 Mon := g_Monsters_ByUID(ID);
2604 if Mon <> nil then
2605 Exit;
2607 MType := M.ReadByte();
2608 MState := M.ReadByte();
2609 MAnim := M.ReadByte();
2610 MTarg := M.ReadWord();
2611 MTargTime := M.ReadLongInt();
2612 MBehav := M.ReadByte();
2613 MSleep := M.ReadLongInt();
2614 MHealth := M.ReadLongInt();
2615 MAmmo := M.ReadLongInt();
2617 X := M.ReadLongInt();
2618 Y := M.ReadLongInt();
2619 VX := M.ReadLongInt();
2620 VY := M.ReadLongInt();
2621 MDir := M.ReadByte();
2623 g_Monsters_Create(MType, X, Y, TDirection(MDir), False, ID);
2624 Mon := g_Monsters_ByUID(ID);
2625 if Mon = nil then
2626 Exit;
2628 with Mon do
2629 begin
2631 MonsterAnim := MAnim;
2632 MonsterTargetUID := MTarg;
2633 MonsterTargetTime := MTargTime;
2634 MonsterBehaviour := MBehav;
2635 MonsterSleep := MSleep;
2636 MonsterAmmo := MAmmo;
2637 SetHealth(MHealth);
2639 SetState(MState);
2641 setPosition(X, Y); // this will call positionChanged();
2642 GameVelX := VX;
2643 GameVelY := VY;
2644 end;
2645 end;
2647 procedure MC_RECV_MonsterPos(var M: TMsg);
2648 var
2649 Mon: TMonster;
2650 ID: Word;
2651 X, Y: Integer;
2652 begin
2653 ID := M.ReadWord();
2654 Mon := g_Monsters_ByUID(ID);
2655 if Mon = nil then
2656 Exit;
2658 with Mon do
2659 begin
2660 X := M.ReadLongInt();
2661 Y := M.ReadLongInt();
2662 Mon.setPosition(X, Y); // this will call `positionChanged()`
2663 GameVelX := M.ReadLongInt();
2664 GameVelY := M.ReadLongInt();
2665 GameDirection := TDirection(M.ReadByte());
2666 end;
2667 end;
2669 procedure MC_RECV_MonsterState(var M: TMsg);
2670 var
2671 ID, OldFire: Integer;
2672 MState, MFAnm: Byte;
2673 Mon: TMonster;
2674 AnimRevert: Boolean;
2675 begin
2676 ID := M.ReadWord();
2677 Mon := g_Monsters_ByUID(ID);
2678 if Mon = nil then Exit;
2680 MState := M.ReadByte();
2681 MFAnm := M.ReadByte();
2683 with Mon do
2684 begin
2685 MonsterTargetUID := M.ReadWord();
2686 MonsterTargetTime := M.ReadLongInt();
2687 MonsterSleep := M.ReadLongInt();
2688 MonsterHealth := M.ReadLongInt();
2689 MonsterAmmo := M.ReadLongInt();
2690 MonsterPain := M.ReadLongInt();
2691 AnimRevert := M.ReadByte() <> 0;
2692 OldFire := FFireTime;
2693 FFireTime := M.ReadLongInt();
2694 if (OldFire <= 0) and (FFireTime > 0) then
2695 g_Sound_PlayExAt('SOUND_IGNITE', Obj.X, Obj.Y);
2696 RevertAnim(AnimRevert);
2698 if MonsterState <> MState then
2699 begin
2700 if (MState = MONSTATE_GO) and (MonsterState = MONSTATE_SLEEP) then WakeUpSound();
2701 if (MState = MONSTATE_DIE) then DieSound();
2702 if (MState = MONSTATE_PAIN) then MakeBloodSimple(Min(200, MonsterPain));
2703 if (MState = MONSTATE_ATTACK) then kick(nil);
2704 if (MState = MONSTATE_DEAD) then SetDeadAnim();
2706 SetState(MState, MFAnm);
2707 end;
2708 end;
2709 end;
2711 procedure MC_RECV_MonsterShot(var M: TMsg);
2712 var
2713 ID: Integer;
2714 Mon: TMonster;
2715 X, Y, VX, VY: Integer;
2716 begin
2717 ID := M.ReadWord();
2719 Mon := g_Monsters_ByUID(ID);
2720 if Mon = nil then Exit;
2722 X := M.ReadLongInt();
2723 Y := M.ReadLongInt();
2724 VX := M.ReadLongInt();
2725 VY := M.ReadLongInt();
2727 Mon.ClientAttack(X, Y, VX, VY);
2728 end;
2730 procedure MC_RECV_MonsterDelete(var M: TMsg);
2731 var
2732 ID: Integer;
2733 Mon: TMonster;
2734 begin
2735 ID := M.ReadWord();
2736 Mon := g_Monsters_ByUID(ID);
2737 if Mon = nil then Exit;
2738 Mon.SetState(5);
2739 Mon.MonsterRemoved := True;
2740 end;
2742 procedure MC_RECV_TimeSync(var M: TMsg);
2743 var
2744 Time: LongWord;
2745 begin
2746 Time := M.ReadLongWord();
2748 if gState = STATE_INTERCUSTOM then
2749 gServInterTime := Min(Time, 255);
2750 end;
2752 procedure MC_RECV_VoteEvent(var M: TMsg);
2753 var
2754 EvID: Byte;
2755 Str1, Str2: string;
2756 Int1, Int2: SmallInt;
2757 begin
2758 EvID := M.ReadByte();
2759 Int1 := M.ReadSmallInt();
2760 Int2 := M.ReadSmallInt();
2761 Str1 := M.ReadString();
2762 Str2 := M.ReadString();
2764 case EvID of
2765 NET_VE_STARTED:
2766 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_STARTED], [Str1, Str2, Int1]), True);
2767 NET_VE_PASSED:
2768 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_PASSED], [Str1]), True);
2769 NET_VE_FAILED:
2770 g_Console_Add(_lc[I_MESSAGE_VOTE_FAILED], True);
2771 NET_VE_VOTE:
2772 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Str1, Int1, Int2]), True);
2773 NET_VE_INPROGRESS:
2774 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_INPROGRESS], [Str1]), True);
2775 end;
2776 end;
2778 // CLIENT SEND
2780 procedure MC_SEND_Info(Password: string);
2781 begin
2782 NetOut.Clear();
2784 NetOut.Write(Byte(NET_MSG_INFO));
2785 NetOut.Write(GAME_VERSION);
2786 NetOut.Write(Password);
2787 NetOut.Write(gPlayer1Settings.Name);
2788 NetOut.Write(gPlayer1Settings.Model);
2789 NetOut.Write(gPlayer1Settings.Color.R);
2790 NetOut.Write(gPlayer1Settings.Color.G);
2791 NetOut.Write(gPlayer1Settings.Color.B);
2792 NetOut.Write(gPlayer1Settings.Team);
2794 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2795 end;
2797 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
2798 begin
2799 NetOut.Write(Byte(NET_MSG_CHAT));
2800 NetOut.Write(Txt);
2801 NetOut.Write(Mode);
2803 g_Net_Client_Send(True, NET_CHAN_CHAT);
2804 end;
2806 procedure MC_SEND_PlayerPos();
2807 var
2808 kByte: Word;
2809 Predict: Boolean;
2810 strafeDir: Byte;
2811 WeaponSelect: Word = 0;
2812 i: Integer;
2813 begin
2814 if not gGameOn then Exit;
2815 if gPlayers = nil then Exit;
2816 if gPlayer1 = nil then Exit;
2818 kByte := 0;
2819 Predict := NetPredictSelf; // and (not NetGotKeys);
2821 if (not gConsoleShow) and (not gChatShow) and (g_ActiveWindow = nil) then
2822 begin
2823 strafeDir := P1MoveButton shr 4;
2824 P1MoveButton := P1MoveButton and $0F;
2826 if gPlayerAction[0, ACTION_MOVELEFT] and (not gPlayerAction[0, ACTION_MOVERIGHT]) then
2827 P1MoveButton := 1
2828 else if (not gPlayerAction[0, ACTION_MOVELEFT]) and gPlayerAction[0, ACTION_MOVERIGHT] then
2829 P1MoveButton := 2
2830 else if (not gPlayerAction[0, ACTION_MOVELEFT]) and (not gPlayerAction[0, ACTION_MOVERIGHT]) then
2831 P1MoveButton := 0;
2833 // strafing
2834 if gPlayerAction[0, ACTION_STRAFE] then
2835 begin
2836 // new strafe mechanics
2837 if (strafeDir = 0) then
2838 strafeDir := P1MoveButton; // start strafing
2839 // now set direction according to strafe (reversed)
2840 if (strafeDir = 2) then
2841 gPlayer1.SetDirection(TDirection.D_LEFT)
2842 else if (strafeDir = 1) then
2843 gPlayer1.SetDirection(TDirection.D_RIGHT)
2844 end
2845 else
2846 begin
2847 strafeDir := 0; // not strafing anymore
2848 if (P1MoveButton = 2) and gPlayerAction[0, ACTION_MOVELEFT] then
2849 gPlayer1.SetDirection(TDirection.D_LEFT)
2850 else if (P1MoveButton = 1) and gPlayerAction[0, ACTION_MOVERIGHT] then
2851 gPlayer1.SetDirection(TDirection.D_RIGHT)
2852 else if P1MoveButton <> 0 then
2853 gPlayer1.SetDirection(TDirection(P1MoveButton-1));
2854 end;
2856 gPlayer1.ReleaseKeys;
2857 if P1MoveButton = 1 then
2858 begin
2859 kByte := kByte or NET_KEY_LEFT;
2860 if Predict then gPlayer1.PressKey(KEY_LEFT, 10000);
2861 end;
2862 if P1MoveButton = 2 then
2863 begin
2864 kByte := kByte or NET_KEY_RIGHT;
2865 if Predict then gPlayer1.PressKey(KEY_RIGHT, 10000);
2866 end;
2867 if gPlayerAction[0, ACTION_LOOKUP] then
2868 begin
2869 kByte := kByte or NET_KEY_UP;
2870 gPlayer1.PressKey(KEY_UP, 10000);
2871 end;
2872 if gPlayerAction[0, ACTION_LOOKDOWN] then
2873 begin
2874 kByte := kByte or NET_KEY_DOWN;
2875 gPlayer1.PressKey(KEY_DOWN, 10000);
2876 end;
2877 if gPlayerAction[0, ACTION_JUMP] then
2878 begin
2879 kByte := kByte or NET_KEY_JUMP;
2880 // gPlayer1.PressKey(KEY_JUMP, 10000); // TODO: Make a prediction option
2881 end;
2882 if gPlayerAction[0, ACTION_ATTACK] then kByte := kByte or NET_KEY_FIRE;
2883 if gPlayerAction[0, ACTION_ACTIVATE] then kByte := kByte or NET_KEY_OPEN;
2884 if gPlayerAction[0, ACTION_WEAPNEXT] then kByte := kByte or NET_KEY_NW;
2885 if gPlayerAction[0, ACTION_WEAPPREV] then kByte := kByte or NET_KEY_PW;
2887 gPlayerAction[0, ACTION_WEAPNEXT] := False; // HACK, remove after readyweaon&pendinweapon implementation
2888 gPlayerAction[0, ACTION_WEAPPREV] := False; // HACK, remove after readyweaon&pendinweapon implementation
2890 for i := WP_FIRST to WP_LAST do
2891 begin
2892 if gSelectWeapon[0, i] then
2893 begin
2894 WeaponSelect := WeaponSelect or Word(1 shl i);
2895 gSelectWeapon[0, i] := False
2896 end
2897 end;
2899 // fix movebutton state
2900 P1MoveButton := P1MoveButton or (strafeDir shl 4);
2901 end
2902 else
2903 kByte := NET_KEY_CHAT;
2905 NetOut.Write(Byte(NET_MSG_PLRPOS));
2906 NetOut.Write(gTime);
2907 NetOut.Write(kByte);
2908 NetOut.Write(Byte(gPlayer1.Direction));
2909 NetOut.Write(WeaponSelect);
2910 //e_WriteLog(Format('S:ws=%d', [WeaponSelect]), MSG_WARNING);
2911 g_Net_Client_Send(True, NET_CHAN_PLAYERPOS);
2913 //kBytePrev := kByte;
2914 //kDirPrev := gPlayer1.Direction;
2915 end;
2917 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
2918 begin
2919 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
2920 NetOut.Write(Byte(Start));
2921 NetOut.Write(Command);
2922 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2923 end;
2925 procedure MC_SEND_PlayerSettings();
2926 begin
2927 NetOut.Write(Byte(NET_MSG_PLRSET));
2928 NetOut.Write(gPlayer1Settings.Name);
2929 NetOut.Write(gPlayer1Settings.Model);
2930 NetOut.Write(gPlayer1Settings.Color.R);
2931 NetOut.Write(gPlayer1Settings.Color.G);
2932 NetOut.Write(gPlayer1Settings.Color.B);
2933 NetOut.Write(gPlayer1Settings.Team);
2935 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2936 end;
2938 procedure MC_SEND_FullStateRequest();
2939 begin
2940 NetOut.Write(Byte(NET_MSG_REQFST));
2942 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2943 end;
2945 procedure MC_SEND_CheatRequest(Kind: Byte);
2946 begin
2947 NetOut.Write(Byte(NET_MSG_CHEAT));
2948 NetOut.Write(Kind);
2950 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2951 end;
2952 procedure MC_SEND_RCONPassword(Password: string);
2953 begin
2954 NetOut.Write(Byte(NET_MSG_RCON_AUTH));
2955 NetOut.Write(Password);
2957 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2958 end;
2959 procedure MC_SEND_RCONCommand(Cmd: string);
2960 begin
2961 NetOut.Write(Byte(NET_MSG_RCON_CMD));
2962 NetOut.Write(Cmd);
2964 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2965 end;
2967 // i have no idea why all this stuff is in here
2969 function ReadFile(const FileName: TFileName): AByte;
2970 var
2971 FileStream : TStream;
2972 fname: string;
2973 begin
2974 e_WriteLog(Format('NETWORK: looking for file "%s"', [FileName]), TMsgType.Notify);
2975 fname := findDiskWad(FileName);
2976 if length(fname) = 0 then
2977 begin
2978 e_WriteLog(Format('NETWORK: file "%s" not found!', [FileName]), TMsgType.Fatal);
2979 SetLength(Result, 0);
2980 exit;
2981 end;
2982 e_WriteLog(Format('NETWORK: found file "%s"', [fname]), TMsgType.Notify);
2983 Result := nil;
2984 FileStream := openDiskFileRO(fname);
2985 try
2986 if FileStream.Size > 0 then
2987 begin
2988 SetLength(Result, FileStream.Size);
2989 FileStream.Read(Result[0], FileStream.Size);
2990 end;
2991 finally
2992 FileStream.Free;
2993 end;
2994 end;
2996 function CreateMapDataMsg(const FileName: TFileName; ResList: TStringList): TMapDataMsg;
2997 var
2998 i: Integer;
2999 begin
3000 Result.MsgId := NET_MSG_MAP_RESPONSE;
3001 Result.FileData := ReadFile(FileName);
3002 Result.FileSize := Length(Result.FileData);
3004 SetLength(Result.ExternalResources, ResList.Count);
3005 for i:=0 to ResList.Count-1 do
3006 begin
3007 Result.ExternalResources[i].Name := ResList.Strings[i];
3008 Result.ExternalResources[i].md5 := MD5File(GameDir+'/wads/'+ResList.Strings[i]);
3009 end;
3010 end;
3012 procedure ResDataMsgToBytes(var bytes: AByte; const ResData: TResDataMsg);
3013 var
3014 ResultStream: TMemoryStream;
3015 begin
3016 ResultStream := TMemoryStream.Create;
3018 ResultStream.WriteBuffer(ResData.MsgId, SizeOf(ResData.MsgId)); //msgId
3019 ResultStream.WriteBuffer(ResData.FileSize, SizeOf(ResData.FileSize)); //file size
3020 ResultStream.WriteBuffer(ResData.FileData[0], ResData.FileSize); //file data
3022 SetLength(bytes, ResultStream.Size);
3023 ResultStream.Seek(0, soFromBeginning);
3024 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
3026 ResultStream.Free;
3027 end;
3029 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
3030 begin
3031 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
3032 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize));
3033 SetLength(Result.FileData, Result.FileSize);
3034 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize);
3035 end;
3037 procedure MapDataMsgToBytes(var bytes: AByte; const MapDataMsg: TMapDataMsg);
3038 var
3039 ResultStream: TMemoryStream;
3040 resCount: Integer;
3041 begin
3042 resCount := Length(MapDataMsg.ExternalResources);
3044 ResultStream := TMemoryStream.Create;
3046 ResultStream.WriteBuffer(MapDataMsg.MsgId, SizeOf(MapDataMsg.MsgId)); //msgId
3047 ResultStream.WriteBuffer(MapDataMsg.FileSize, SizeOf(MapDataMsg.FileSize)); //file size
3048 ResultStream.WriteBuffer(MapDataMsg.FileData[0], MapDataMsg.FileSize); //file data
3050 ResultStream.WriteBuffer(resCount, SizeOf(resCount)); //res count
3051 ResultStream.WriteBuffer(MapDataMsg.ExternalResources[0], resCount*SizeOf(TExternalResourceInfo)); //res data
3053 SetLength(bytes, ResultStream.Size);
3054 ResultStream.Seek(0, soFromBeginning);
3055 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
3057 ResultStream.Free;
3058 end;
3060 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
3061 var
3062 resCount: Integer;
3063 begin
3064 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
3065 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize)); //file size
3067 SetLength(Result.FileData, Result.FileSize);
3068 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize); //file data
3070 msgStream.ReadBuffer(resCount, SizeOf(resCount)); //res count
3071 SetLength(Result.ExternalResources, resCount);
3073 msgStream.ReadBuffer(Result.ExternalResources[0], resCount * SizeOf(TExternalResourceInfo)); //res data
3074 end;
3076 function IsValidFileName(const S: String): Boolean;
3077 const
3078 Forbidden: set of Char = ['<', '>', '|', '"', ':', '*', '?'];
3079 var
3080 I: Integer;
3081 begin
3082 Result := S <> '';
3083 for I := 1 to Length(S) do
3084 Result := Result and (not(S[I] in Forbidden));
3085 end;
3087 function IsValidFilePath(const S: String): Boolean;
3088 var
3089 I: Integer;
3090 begin
3091 Result := False;
3092 if not IsValidFileName(S) then exit;
3093 if FileExists(S) then exit;
3094 I := LastDelimiter('\/', S);
3095 if (I > 0) then
3096 if (not DirectoryExists(Copy(S, 1, I-1))) then
3097 exit;
3098 Result := True;
3099 end;
3101 procedure MC_SEND_MapRequest();
3102 begin
3103 NetOut.Write(Byte(NET_MSG_MAP_REQUEST));
3104 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3105 end;
3107 procedure MC_SEND_ResRequest(const resName: AnsiString);
3108 begin
3109 NetOut.Write(Byte(NET_MSG_RES_REQUEST));
3110 NetOut.Write(resName);
3111 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3112 end;
3114 procedure MH_RECV_MapRequest(C: pTNetClient; var M: TMsg);
3115 var
3116 payload: AByte;
3117 peer: pENetPeer;
3118 mapDataMsg: TMapDataMsg;
3119 begin
3120 e_WriteLog('NET: Received map request from ' +
3121 DecodeIPV4(C^.Peer.address.host), TMsgType.Notify);
3123 mapDataMsg := CreateMapDataMsg(MapsDir + gGameSettings.WAD, gExternalResources);
3124 peer := NetClients[C^.ID].Peer;
3126 MapDataMsgToBytes(payload, mapDataMsg);
3127 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3129 payload := nil;
3130 mapDataMsg.FileData := nil;
3131 mapDataMsg.ExternalResources := nil;
3132 end;
3134 procedure MH_RECV_ResRequest(C: pTNetClient; var M: TMsg);
3135 var
3136 payload: AByte;
3137 peer: pENetPeer;
3138 FileName: String;
3139 resDataMsg: TResDataMsg;
3140 begin
3141 FileName := ExtractFileName(M.ReadString());
3142 e_WriteLog('NET: Received res request: ' + FileName +
3143 ' from ' + DecodeIPV4(C^.Peer.address.host), TMsgType.Notify);
3145 if not IsValidFilePath(FileName) then
3146 begin
3147 e_WriteLog('Invalid filename: ' + FileName, TMsgType.Warning);
3148 exit;
3149 end;
3151 peer := NetClients[C^.ID].Peer;
3153 if gExternalResources.IndexOf(FileName) > -1 then
3154 begin
3155 resDataMsg.MsgId := NET_MSG_RES_RESPONSE;
3156 resDataMsg.FileData := ReadFile(GameDir+'/wads/'+FileName);
3157 resDataMsg.FileSize := Length(resDataMsg.FileData);
3159 ResDataMsgToBytes(payload, resDataMsg);
3160 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3161 end;
3162 end;
3164 end.