DEADSOFTWARE

7cd1a7cc8ecd18dec4363db5ad4e55d1682206e8
[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 {.$DEFINE K8_XXX_WEAPON_DEBUG}
21 interface
23 uses e_msg, g_net, g_triggers, Classes, SysUtils, md5;
25 const
26 NET_MSG_INFO = 100;
28 NET_MSG_CHAT = 101;
29 NET_MSG_SND = 102;
30 NET_MSG_GFX = 103;
31 NET_MSG_GEVENT = 104;
32 NET_MSG_SCORE = 105;
33 NET_MSG_COOP = 106;
34 NET_MSG_FLAG = 107;
35 NET_MSG_REQFST = 108;
36 NET_MSG_GSET = 109;
38 NET_MSG_PLR = 111;
39 NET_MSG_PLRPOS = 112;
40 NET_MSG_PLRSTA = 113;
41 NET_MSG_PLRDEL = 114;
42 NET_MSG_PLRDMG = 115;
43 NET_MSG_PLRDIE = 116;
44 NET_MSG_PLRFIRE= 117;
45 NET_MSG_PLRSET = 119;
46 NET_MSG_CHEAT = 120;
48 NET_MSG_ISPAWN = 121;
49 NET_MSG_IDEL = 122;
51 NET_MSG_MSPAWN = 131;
52 NET_MSG_MPOS = 132;
53 NET_MSG_MSTATE = 133;
54 NET_MSG_MSHOT = 134;
55 NET_MSG_MDEL = 135;
57 NET_MSG_PSTATE = 141;
58 NET_MSG_PTEX = 142;
60 NET_MSG_TSOUND = 151;
61 NET_MSG_TMUSIC = 152;
63 NET_MSG_SHDEL = 161;
64 NET_MSG_SHADD = 162;
65 NET_MSG_SHPOS = 163;
67 NET_MSG_RCON_AUTH = 191;
68 NET_MSG_RCON_CMD = 192;
69 NET_MSG_TIME_SYNC = 194;
70 NET_MSG_VOTE_EVENT = 195;
72 NET_MSG_MAP_REQUEST = 201;
73 NET_MSG_MAP_RESPONSE = 202;
74 NET_MSG_RES_REQUEST = 203;
75 NET_MSG_RES_RESPONSE = 204;
77 NET_CHAT_SYSTEM = 0;
78 NET_CHAT_PLAYER = 1;
79 NET_CHAT_TEAM = 2;
81 NET_RCON_NOAUTH = 0;
82 NET_RCON_PWGOOD = 1;
83 NET_RCON_PWBAD = 2;
85 NET_GFX_SPARK = 1;
86 NET_GFX_TELE = 2;
87 NET_GFX_RESPAWN = 3;
88 NET_GFX_FIRE = 4;
89 NET_GFX_EXPLODE = 5;
90 NET_GFX_BFGEXPL = 6;
91 NET_GFX_BFGHIT = 7;
92 NET_GFX_SHELL1 = 8;
93 NET_GFX_SHELL2 = 9;
94 NET_GFX_SHELL3 = 10;
96 NET_EV_MAPSTART = 1;
97 NET_EV_MAPEND = 2;
98 NET_EV_CHANGE_TEAM = 3;
99 NET_EV_PLAYER_KICK = 4;
100 NET_EV_PLAYER_BAN = 5;
101 NET_EV_LMS_WARMUP = 6;
102 NET_EV_LMS_SURVIVOR = 7;
103 NET_EV_RCON = 8;
104 NET_EV_BIGTEXT = 9;
105 NET_EV_SCORE = 10;
106 NET_EV_SCORE_MSG = 11;
107 NET_EV_LMS_START = 12;
108 NET_EV_LMS_WIN = 13;
109 NET_EV_TLMS_WIN = 14;
110 NET_EV_LMS_LOSE = 15;
111 NET_EV_LMS_DRAW = 16;
112 NET_EV_KILLCOMBO = 17;
113 NET_EV_PLAYER_TOUCH = 18;
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;
130 NET_MAX_DIFFTIME = 5000 div 36;
132 // HOST MESSAGES
134 procedure MH_RECV_Info(C: pTNetClient; var M: TMsg);
135 procedure MH_RECV_Chat(C: pTNetClient; var M: TMsg);
136 procedure MH_RECV_FullStateRequest(C: pTNetClient; var M: TMsg);
137 function MH_RECV_PlayerPos(C: pTNetClient; var M: TMsg): Word;
138 procedure MH_RECV_PlayerSettings(C: pTNetClient; var M: TMsg);
139 procedure MH_RECV_CheatRequest(C: pTNetClient; var M: TMsg);
140 procedure MH_RECV_RCONPassword(C: pTNetClient; var M: TMsg);
141 procedure MH_RECV_RCONCommand(C: pTNetClient; var M: TMsg);
142 procedure MH_RECV_MapRequest(C: pTNetClient; var M: TMsg);
143 procedure MH_RECV_ResRequest(C: pTNetClient; var M: TMsg);
144 procedure MH_RECV_Vote(C: pTNetClient; var M: TMsg);
146 // GAME
147 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
148 procedure MH_SEND_Info(ID: Byte);
149 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
150 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
151 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
152 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
153 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
154 procedure MH_SEND_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
155 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
156 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
157 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
158 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
159 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
160 // PLAYER
161 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
162 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
163 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
164 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
165 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
166 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
167 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
168 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
169 // ITEM
170 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
171 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
172 // PANEL
173 procedure MH_SEND_PanelTexture(PGUID: Integer; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
174 procedure MH_SEND_PanelState(PGUID: Integer; ID: Integer = NET_EVERYONE);
175 // MONSTER
176 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
177 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
178 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
179 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
180 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
181 // TRIGGER
182 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
183 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
184 // MISC
185 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
186 procedure MH_SEND_VoteEvent(EvType: Byte;
187 StrArg1: string = 'a'; StrArg2: string = 'b';
188 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
189 ID: Integer = NET_EVERYONE);
191 // CLIENT MESSAGES //
193 // GAME
194 procedure MC_RECV_Chat(var M: TMsg);
195 procedure MC_RECV_Effect(var M: TMsg);
196 procedure MC_RECV_Sound(var M: TMsg);
197 procedure MC_RECV_GameStats(var M: TMsg);
198 procedure MC_RECV_CoopStats(var M: TMsg);
199 procedure MC_RECV_GameEvent(var M: TMsg);
200 procedure MC_RECV_FlagEvent(var M: TMsg);
201 procedure MC_RECV_GameSettings(var M: TMsg);
202 // PLAYER
203 function MC_RECV_PlayerCreate(var M: TMsg): Word;
204 function MC_RECV_PlayerPos(var M: TMsg): Word;
205 function MC_RECV_PlayerStats(var M: TMsg): Word;
206 function MC_RECV_PlayerDelete(var M: TMsg): Word;
207 function MC_RECV_PlayerDamage(var M: TMsg): Word;
208 function MC_RECV_PlayerDeath(var M: TMsg): Word;
209 function MC_RECV_PlayerFire(var M: TMsg): Word;
210 procedure MC_RECV_PlayerSettings(var M: TMsg);
211 // ITEM
212 procedure MC_RECV_ItemSpawn(var M: TMsg);
213 procedure MC_RECV_ItemDestroy(var M: TMsg);
214 // PANEL
215 procedure MC_RECV_PanelTexture(var M: TMsg);
216 procedure MC_RECV_PanelState(var M: TMsg);
217 // MONSTER
218 procedure MC_RECV_MonsterSpawn(var M: TMsg);
219 procedure MC_RECV_MonsterPos(var M: TMsg);
220 procedure MC_RECV_MonsterState(var M: TMsg);
221 procedure MC_RECV_MonsterShot(var M: TMsg);
222 procedure MC_RECV_MonsterDelete(var M: TMsg);
223 // SHOT
224 procedure MC_RECV_CreateShot(var M: TMsg);
225 procedure MC_RECV_UpdateShot(var M: TMsg);
226 procedure MC_RECV_DeleteShot(var M: TMsg);
227 // TRIGGER
228 procedure MC_RECV_TriggerSound(var M: TMsg);
229 procedure MC_RECV_TriggerMusic(var M: TMsg);
230 // MISC
231 procedure MC_RECV_TimeSync(var M: TMsg);
232 procedure MC_RECV_VoteEvent(var M: TMsg);
233 // SERVICE
234 procedure MC_SEND_Info(Password: string);
235 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
236 procedure MC_SEND_PlayerPos();
237 procedure MC_SEND_FullStateRequest();
238 procedure MC_SEND_PlayerSettings();
239 procedure MC_SEND_CheatRequest(Kind: Byte);
240 procedure MC_SEND_RCONPassword(Password: string);
241 procedure MC_SEND_RCONCommand(Cmd: string);
242 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
243 // DOWNLOAD
244 procedure MC_SEND_MapRequest();
245 procedure MC_SEND_ResRequest(const resName: AnsiString);
247 type
248 TExternalResourceInfo = record
249 Name: string[255];
250 md5: TMD5Digest;
251 end;
253 TResDataMsg = record
254 MsgId: Byte;
255 FileSize: Integer;
256 FileData: AByte;
257 end;
259 TMapDataMsg = record
260 MsgId: Byte;
261 FileSize: Integer;
262 FileData: AByte;
263 ExternalResources: array of TExternalResourceInfo;
264 end;
266 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
267 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
269 implementation
271 uses
272 Math, ENet, e_input, e_graphics, e_log,
273 g_textures, g_gfx, g_sound, g_console, g_basic, g_options, g_main,
274 g_game, g_player, g_map, g_panel, g_items, g_weapons, g_phys, g_gui,
275 g_language, g_monsters, g_netmaster, utils, wadreader, MAPDEF;
277 const
278 NET_KEY_LEFT = 1;
279 NET_KEY_RIGHT = 2;
280 NET_KEY_UP = 4;
281 NET_KEY_DOWN = 8;
282 NET_KEY_JUMP = 16;
283 NET_KEY_FIRE = 32;
284 NET_KEY_OPEN = 64;
285 NET_KEY_NW = 256;
286 NET_KEY_PW = 512;
287 NET_KEY_CHAT = 2048;
288 NET_KEY_FORCEDIR = 4096;
290 //var
291 //kBytePrev: Word = 0;
292 //kDirPrev: TDirection = D_LEFT;
293 //HostGameTime: Word = 0;
295 // HOST MESSAGES //
298 // GAME
300 procedure MH_RECV_Chat(C: pTNetClient; var M: TMsg);
301 var
302 Txt: string;
303 Mode: Byte;
304 PID: Word;
305 Pl: TPlayer;
306 begin
307 PID := C^.Player;
308 Pl := g_Player_Get(PID);
310 Txt := M.ReadString();
311 Mode := M.ReadByte();
312 if (Mode = NET_CHAT_SYSTEM) then
313 Mode := NET_CHAT_PLAYER; // prevent sending system messages from clients
314 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
315 Mode := NET_CHAT_PLAYER; // revert to player chat in non-team game
317 if Pl = nil then
318 MH_SEND_Chat(Txt, Mode)
319 else
320 MH_SEND_Chat(Pl.Name + ': ' + Txt, Mode, IfThen(Mode = NET_CHAT_TEAM, Pl.Team, NET_EVERYONE));
321 end;
323 procedure MH_RECV_Info(C: pTNetClient; var M: TMsg);
324 var
325 Ver, PName, Model, Pw: string;
326 R, G, B, T: Byte;
327 PID: Word;
328 Color: TRGB;
329 I: Integer;
330 begin
331 Ver := M.ReadString();
332 Pw := M.ReadString();
333 PName := M.ReadString();
334 Model := M.ReadString();
335 R := M.ReadByte();
336 G := M.ReadByte();
337 B := M.ReadByte();
338 T := M.ReadByte();
340 if Ver <> GAME_VERSION then
341 begin
342 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
343 _lc[I_NET_DISC_VERSION]);
344 enet_peer_disconnect(C^.Peer, NET_DISC_VERSION);
345 Exit;
346 end;
348 if g_Net_IsHostBanned(C^.Peer^.address.host) then
349 begin
350 if g_Net_IsHostBanned(C^.Peer^.address.host, True) then
351 begin
352 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
353 _lc[I_NET_DISC_BAN]);
354 enet_peer_disconnect(C^.Peer, NET_DISC_BAN);
355 end
356 else
357 begin
358 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
359 _lc[I_NET_DISC_BAN]);
360 enet_peer_disconnect(C^.Peer, NET_DISC_TEMPBAN);
361 end;
362 Exit;
363 end;
365 if NetPassword <> '' then
366 if AnsiLowerCase(NetPassword) <> AnsiLowerCase(Pw) then
367 begin
368 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
369 _lc[I_NET_DISC_PASSWORD]);
370 enet_peer_disconnect(C^.Peer, NET_DISC_PASSWORD);
371 Exit;
372 end;
374 Color.R := R;
375 Color.B := B;
376 Color.G := G;
378 PID := g_Player_Create(Model, Color, T, False);
379 with g_Player_Get(PID) do
380 begin
381 Name := PName;
382 Reset(True);
383 end;
385 C^.Player := PID;
387 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
388 e_WriteLog('NET: Client ' + PName + ' [' + IntToStr(C^.ID) +
389 '] connected. Assigned player #' + IntToStr(PID) + '.', TMsgType.Notify);
391 MH_SEND_Info(C^.ID);
393 with g_Player_Get(PID) do
394 begin
395 Name := PName;
396 FClientID := C^.ID;
397 // round in progress, don't spawn
398 if (gGameSettings.MaxLives > 0) and (gLMSRespawn = LMS_RESPAWN_NONE) then
399 begin
400 Lives := 0;
401 FNoRespawn := True;
402 Spectate;
403 FWantsInGame := True; // TODO: look into this later
404 end
405 else
406 Respawn(gGameSettings.GameType = GT_SINGLE);
407 end;
409 for I := Low(NetClients) to High(NetClients) do
410 begin
411 if NetClients[I].ID = C^.ID then Continue;
412 MH_SEND_PlayerCreate(PID, NetClients[I].ID);
413 MH_SEND_PlayerPos(True, PID, NetClients[I].ID);
414 MH_SEND_PlayerStats(PID, NetClients[I].ID);
415 end;
417 if gState in [STATE_INTERCUSTOM, STATE_FOLD] then
418 MH_SEND_GameEvent(NET_EV_MAPEND, 0, 'N', C^.ID);
420 if NetUseMaster then g_Net_Slist_Update;
421 end;
423 procedure MH_RECV_FullStateRequest(C: pTNetClient; var M: TMsg);
424 begin
425 if gGameOn then
426 MH_SEND_Everything((C^.State = NET_STATE_AUTH), C^.ID)
427 else
428 C^.RequestedFullUpdate := True;
429 end;
431 // PLAYER
433 function MH_RECV_PlayerPos(C: pTNetClient; var M: TMsg): Word;
434 var
435 Dir{, i}: Byte;
436 //WeaponSelect: Word;
437 PID: Word;
438 kByte: Word;
439 Pl: TPlayer;
440 GT, wctr: LongWord;
441 newweapon: Byte;
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 wctr := M.ReadLongWord();
460 newweapon := M.ReadByte();
461 if (wctr > Pl.NetWeapCtr) then
462 begin
463 if (newweapon <> CurrWeap) then
464 begin
465 {$IFDEF K8_XXX_WEAPON_DEBUG}
466 writeln('HOST PLRPOS: got: currweap=', CurrWeap, '; curfrm=', gTime, '; netweap=', newweapon, '; oldweap=', CurrWeap);
467 {$ENDIF}
468 SetWeaponHost(newweapon);
469 end;
470 Pl.NetWeapCtr := wctr;
471 end;
472 //e_WriteLog(Format('R:ws=%d', [WeaponSelect]), MSG_WARNING);
473 if Direction <> TDirection(Dir) then
474 JustTeleported := False;
476 SetDirection(TDirection(Dir));
477 ReleaseKeysNoWeapon();
479 if kByte = NET_KEY_CHAT then
480 begin
481 PressKey(KEY_CHAT, 10000);
482 Exit;
483 end;
485 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
486 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
487 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
488 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
489 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
490 if LongBool(kByte and NET_KEY_FIRE) then PressKey(KEY_FIRE, 10000);
491 if LongBool(kByte and NET_KEY_OPEN) then PressKey(KEY_OPEN, 10000);
492 //if LongBool(kByte and NET_KEY_NW) then PressKey(KEY_NEXTWEAPON, 10000);
493 //if LongBool(kByte and NET_KEY_PW) then PressKey(KEY_PREVWEAPON, 10000);
495 (*
496 for i := 0 to 15 do
497 begin
498 if (WeaponSelect and Word(1 shl i)) <> 0 then
499 begin
500 //e_WriteLog(Format(' R:wn=%d', [i]), MSG_WARNING);
501 QueueWeaponSwitch(i);
502 end;
503 end;
504 *)
505 end;
507 // MH_SEND_PlayerPos(False, PID, C^.ID);
508 end;
510 procedure MH_RECV_CheatRequest(C: pTNetClient; var M: TMsg);
511 var
512 CheatKind: Byte;
513 Pl: TPlayer;
514 begin
515 Pl := g_Player_Get(C^.Player);
516 if Pl = nil then Exit;
518 CheatKind := M.ReadByte();
520 case CheatKind of
521 NET_CHEAT_SUICIDE:
522 Pl.Damage(SUICIDE_DAMAGE, Pl.UID, 0, 0, HIT_SELF);
523 NET_CHEAT_SPECTATE:
524 begin
525 if Pl.FSpectator then
526 Pl.Respawn(False)
527 else
528 Pl.Spectate;
529 end;
530 end;
531 end;
533 procedure MH_RECV_PlayerSettings(C: pTNetClient; var M: TMsg);
534 var
535 TmpName: string;
536 TmpModel: string;
537 TmpColor: TRGB;
538 TmpTeam: Byte;
539 Pl: TPlayer;
540 begin
541 TmpName := M.ReadString();
542 TmpModel := M.ReadString();
543 TmpColor.R := M.ReadByte();
544 TmpColor.G := M.ReadByte();
545 TmpColor.B := M.ReadByte();
546 TmpTeam := M.ReadByte();
548 Pl := g_Player_Get(C^.Player);
549 if Pl = nil then Exit;
551 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
552 Pl.SwitchTeam
553 else
554 Pl.SetColor(TmpColor);
556 if Pl.Name <> TmpName then
557 begin
558 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
559 Pl.Name := TmpName;
560 end;
562 if TmpModel <> Pl.Model.Name then
563 Pl.SetModel(TmpModel);
565 MH_SEND_PlayerSettings(Pl.UID, TmpModel);
566 end;
568 // RCON
570 procedure MH_RECV_RCONPassword(C: pTNetClient; var M: TMsg);
571 var
572 Pwd: string;
573 begin
574 Pwd := M.ReadString();
575 if not NetAllowRCON then Exit;
576 if Pwd = NetRCONPassword then
577 begin
578 C^.RCONAuth := True;
579 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWGOOD, 'N', C^.ID);
580 end
581 else
582 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWBAD, 'N', C^.ID);
583 end;
585 procedure MH_RECV_RCONCommand(C: pTNetClient; var M: TMsg);
586 var
587 Cmd: string;
588 begin
589 Cmd := M.ReadString();
590 if not NetAllowRCON then Exit;
591 if not C^.RCONAuth then
592 begin
593 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_NOAUTH, 'N', C^.ID);
594 Exit;
595 end;
596 g_Console_Process(Cmd);
597 end;
599 // MISC
601 procedure MH_RECV_Vote(C: pTNetClient; var M: TMsg);
602 var
603 Start: Boolean;
604 Name, Command: string;
605 Need: Integer;
606 Pl: TPlayer;
607 begin
608 Start := M.ReadByte() <> 0;
609 Command := M.ReadString();
611 Pl := g_Player_Get(C^.Player);
612 if Pl = nil then Exit;
613 Name := Pl.Name;
615 if Start then
616 begin
617 if not g_Console_CommandBlacklisted(Command) then
618 g_Game_StartVote(Command, Name);
619 end
620 else if gVoteInProgress then
621 begin
622 if (gPlayer1 <> nil) or (gPlayer2 <> nil) then
623 Need := Floor((NetClientCount+1)/2.0) + 1
624 else
625 Need := Floor(NetClientCount/2.0) + 1;
626 if C^.Voted then
627 begin
628 Dec(gVoteCount);
629 C^.Voted := False;
630 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_REVOKED], [Name, gVoteCount, Need]), True);
631 MH_SEND_VoteEvent(NET_VE_REVOKE, Name, 'a', gVoteCount, Need);
632 end
633 else
634 begin
635 Inc(gVoteCount);
636 C^.Voted := True;
637 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Name, gVoteCount, Need]), True);
638 MH_SEND_VoteEvent(NET_VE_VOTE, Name, 'a', gVoteCount, Need);
639 g_Game_CheckVote;
640 end;
641 end;
642 end;
644 // GAME (SEND)
646 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
648 function sendItemRespawn (it: PItem): Boolean;
649 begin
650 result := false; // don't stop
651 MH_SEND_ItemSpawn(True, it.myid, ID);
652 end;
654 function sendMonSpawn (mon: TMonster): Boolean;
655 begin
656 result := false; // don't stop
657 MH_SEND_MonsterSpawn(mon.UID, ID);
658 end;
660 function sendPanelState (pan: TPanel): Boolean;
661 begin
662 result := false; // don't stop
663 MH_SEND_PanelState(pan.guid, ID); // anyway, to sync mplats
664 if (pan.CanChangeTexture) then MH_SEND_PanelTexture(pan.guid, pan.LastAnimLoop, ID);
665 end;
667 var
668 I: Integer;
669 begin
670 if gPlayers <> nil then
671 begin
672 for I := Low(gPlayers) to High(gPlayers) do
673 begin
674 if gPlayers[I] <> nil then
675 begin
676 if CreatePlayers then MH_SEND_PlayerCreate(gPlayers[I].UID, ID);
677 MH_SEND_PlayerPos(True, gPlayers[I].UID, ID);
678 MH_SEND_PlayerStats(gPlayers[I].UID, ID);
680 if (gPlayers[I].Flag <> FLAG_NONE) and (gGameSettings.GameMode = GM_CTF) then
681 begin
682 MH_SEND_FlagEvent(FLAG_STATE_CAPTURED, gPlayers[I].Flag, gPlayers[I].UID, True, ID);
683 end;
684 end;
685 end;
686 end;
688 g_Items_ForEachAlive(sendItemRespawn, true); // backwards
689 g_Mons_ForEach(sendMonSpawn);
690 g_Map_ForEachPanel(sendPanelState);
692 if gTriggers <> nil then
693 begin
694 for I := Low(gTriggers) to High(gTriggers) do
695 begin
696 if gTriggers[I].TriggerType = TRIGGER_SOUND then
697 begin
698 MH_SEND_TriggerSound(gTriggers[I], ID);
699 end;
700 end;
701 end;
703 if Shots <> nil then
704 begin
705 for I := Low(Shots) to High(Shots) do
706 begin
707 if Shots[i].ShotType in [6, 7, 8] then
708 begin
709 MH_SEND_CreateShot(i, ID);
710 end;
711 end;
712 end;
714 MH_SEND_TriggerMusic(ID);
716 MH_SEND_GameStats(ID);
717 MH_SEND_CoopStats(ID);
719 if gGameSettings.GameMode = GM_CTF then
720 begin
721 if gFlags[FLAG_RED].State <> FLAG_STATE_CAPTURED then MH_SEND_FlagEvent(gFlags[FLAG_RED].State, FLAG_RED, 0, True, ID);
722 if gFlags[FLAG_BLUE].State <> FLAG_STATE_CAPTURED then MH_SEND_FlagEvent(gFlags[FLAG_BLUE].State, FLAG_BLUE, 0, True, ID);
723 end;
725 if CreatePlayers and (ID >= 0) then NetClients[ID].State := NET_STATE_GAME;
727 if gLMSRespawn > LMS_RESPAWN_NONE then
728 begin
729 MH_SEND_GameEvent(NET_EV_LMS_WARMUP, (gLMSRespawnTime - gTime) div 1000, 'N', ID);
730 end;
731 end;
733 procedure MH_SEND_Info(ID: Byte);
734 var
735 Map: string;
736 begin
737 Map := g_ExtractFileName(gMapInfo.Map);
739 NetOut.Clear();
741 NetOut.Write(Byte(NET_MSG_INFO));
742 NetOut.Write(ID);
743 NetOut.Write(NetClients[ID].Player);
744 NetOut.Write(gGameSettings.WAD);
745 NetOut.Write(Map);
746 NetOut.Write(gWADHash);
747 NetOut.Write(gGameSettings.GameMode);
748 NetOut.Write(gGameSettings.GoalLimit);
749 NetOut.Write(gGameSettings.TimeLimit);
750 NetOut.Write(gGameSettings.MaxLives);
751 NetOut.Write(gGameSettings.Options);
752 NetOut.Write(gTime);
754 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
755 end;
757 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
758 var
759 Name: string;
760 i: Integer;
761 Team: Byte;
762 begin
763 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
764 Mode := NET_CHAT_PLAYER;
766 Team := 0;
767 if (Mode = NET_CHAT_TEAM) then
768 begin
769 for i := Low(gPlayers) to High(gPlayers) do
770 if (gPlayers[i] <> nil) and (gPlayers[i].FClientID >= 0) and
771 (gPlayers[i].Team = ID) then
772 begin
773 NetOut.Write(Byte(NET_MSG_CHAT));
774 NetOut.Write(Txt);
775 NetOut.Write(Mode);
776 g_Net_Host_Send(gPlayers[i].FClientID, True, NET_CHAN_CHAT);
777 end;
778 Team := ID;
779 ID := NET_EVERYONE;
780 end
781 else
782 begin
783 NetOut.Write(Byte(NET_MSG_CHAT));
784 NetOut.Write(Txt);
785 NetOut.Write(Mode);
786 g_Net_Host_Send(ID, True, NET_CHAN_CHAT);
787 end;
789 if Mode = NET_CHAT_SYSTEM then
790 Exit;
792 if ID = NET_EVERYONE then
793 begin
794 if Mode = NET_CHAT_PLAYER then
795 begin
796 g_Console_Add(Txt, True);
797 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
798 g_Game_ChatSound(b_Text_Unformat(Txt));
799 end
800 else
801 if Mode = NET_CHAT_TEAM then
802 if gPlayer1 <> nil then
803 begin
804 if (gPlayer1.Team = TEAM_RED) and (Team = TEAM_RED) then
805 begin
806 g_Console_Add(#18'[Team] '#2 + Txt, True);
807 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
808 g_Game_ChatSound(b_Text_Unformat(Txt));
809 end
810 else if (gPlayer1.Team = TEAM_BLUE) and (Team = TEAM_BLUE) then
811 begin
812 g_Console_Add(#20'[Team] '#2 + Txt, True);
813 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
814 g_Game_ChatSound(b_Text_Unformat(Txt));
815 end;
816 end;
817 end
818 else
819 begin
820 Name := g_Net_ClientName_ByID(ID);
821 g_Console_Add('-> ' + Name + ': ' + Txt, True);
822 e_WriteLog('[Tell ' + Name + '] ' + b_Text_Unformat(Txt), TMsgType.Notify);
823 g_Game_ChatSound(b_Text_Unformat(Txt), False);
824 end;
825 end;
827 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
828 begin
829 NetOut.Write(Byte(NET_MSG_GFX));
830 NetOut.Write(Kind);
831 NetOut.Write(X);
832 NetOut.Write(Y);
833 NetOut.Write(Ang);
835 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
836 end;
838 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
839 begin
840 NetOut.Write(Byte(NET_MSG_SND));
841 NetOut.Write(Name);
842 if Pos then
843 begin
844 NetOut.Write(Byte(1));
845 NetOut.Write(X);
846 NetOut.Write(Y);
847 end
848 else
849 NetOut.Write(Byte(0));
851 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
852 end;
854 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
855 begin
856 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
858 NetOut.Write(Byte(NET_MSG_SHADD));
859 NetOut.Write(Proj);
860 NetOut.Write(Shots[Proj].ShotType);
861 NetOut.Write(Shots[Proj].Target);
862 NetOut.Write(Shots[Proj].SpawnerUID);
863 NetOut.Write(Shots[Proj].Timeout);
864 NetOut.Write(Shots[Proj].Obj.X);
865 NetOut.Write(Shots[Proj].Obj.Y);
866 NetOut.Write(Shots[Proj].Obj.Vel.X);
867 NetOut.Write(Shots[Proj].Obj.Vel.Y);
869 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
870 end;
872 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
873 begin
874 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
876 NetOut.Write(Byte(NET_MSG_SHPOS));
877 NetOut.Write(Proj);
878 NetOut.Write(Shots[Proj].Obj.X);
879 NetOut.Write(Shots[Proj].Obj.Y);
880 NetOut.Write(Shots[Proj].Obj.Vel.X);
881 NetOut.Write(Shots[Proj].Obj.Vel.Y);
883 g_Net_Host_Send(ID, False, NET_CHAN_SHOTS);
884 end;
886 procedure MH_Send_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
887 begin
888 NetOut.Write(Byte(NET_MSG_SHDEL));
889 NetOut.Write(Proj);
890 NetOut.Write(Byte(Loud));
891 NetOut.Write(X);
892 NetOut.Write(Y);
894 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
895 end;
897 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
898 begin
899 NetOut.Write(Byte(NET_MSG_SCORE));
900 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
901 begin
902 NetOut.Write(gTeamStat[TEAM_RED].Goals);
903 NetOut.Write(gTeamStat[TEAM_BLUE].Goals);
904 end
905 else
906 if gGameSettings.GameMode = GM_COOP then
907 begin
908 NetOut.Write(gCoopMonstersKilled);
909 NetOut.Write(gCoopSecretsFound);
910 end;
912 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
913 end;
915 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
916 begin
917 NetOut.Write(Byte(NET_MSG_COOP));
918 NetOut.Write(gTotalMonsters);
919 NetOut.Write(gSecretsCount);
920 NetOut.Write(gCoopTotalMonstersKilled);
921 NetOut.Write(gCoopTotalSecretsFound);
922 NetOut.Write(gCoopTotalMonsters);
923 NetOut.Write(gCoopTotalSecrets);
924 end;
926 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
927 begin
928 NetOut.Write(Byte(NET_MSG_GEVENT));
929 NetOut.Write(EvType);
930 NetOut.Write(EvNum);
931 NetOut.Write(EvStr);
932 NetOut.Write(Byte(gLastMap));
933 NetOut.Write(gTime);
934 if (EvType = NET_EV_MAPSTART) and isWadPath(EvStr) then
935 begin
936 NetOut.Write(Byte(1));
937 NetOut.Write(gWADHash);
938 end else
939 NetOut.Write(Byte(0));
941 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
942 end;
944 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
945 begin
946 NetOut.Write(Byte(NET_MSG_FLAG));
947 NetOut.Write(EvType);
948 NetOut.Write(Flag);
949 NetOut.Write(Byte(Quiet));
950 NetOut.Write(PID);
951 NetOut.Write(gFlags[Flag].State);
952 NetOut.Write(gFlags[Flag].CaptureTime);
953 NetOut.Write(gFlags[Flag].Obj.X);
954 NetOut.Write(gFlags[Flag].Obj.Y);
955 NetOut.Write(gFlags[Flag].Obj.Vel.X);
956 NetOut.Write(gFlags[Flag].Obj.Vel.Y);
958 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
959 end;
961 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
962 begin
963 NetOut.Write(Byte(NET_MSG_GSET));
964 NetOut.Write(gGameSettings.GameMode);
965 NetOut.Write(gGameSettings.GoalLimit);
966 NetOut.Write(gGameSettings.TimeLimit);
967 NetOut.Write(gGameSettings.MaxLives);
968 NetOut.Write(gGameSettings.Options);
970 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
971 end;
973 // PLAYER (SEND)
975 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
976 var
977 P: TPlayer;
978 begin
979 P := g_Player_Get(PID);
980 if P = nil then Exit;
982 NetOut.Write(Byte(NET_MSG_PLR));
983 NetOut.Write(PID);
984 NetOut.Write(P.Name);
986 NetOut.Write(P.FActualModelName);
987 NetOut.Write(P.FColor.R);
988 NetOut.Write(P.FColor.G);
989 NetOut.Write(P.FColor.B);
990 NetOut.Write(P.Team);
992 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT)
993 end;
995 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
996 var
997 kByte: Word;
998 Pl: TPlayer;
999 begin
1000 Pl := g_Player_Get(PID);
1001 if Pl = nil then Exit;
1002 if Pl.FDummy then Exit;
1004 NetOut.Write(Byte(NET_MSG_PLRPOS));
1005 NetOut.Write(gTime);
1006 NetOut.Write(PID);
1008 kByte := 0;
1010 with Pl do
1011 begin
1012 NetOut.Write(FPing);
1013 NetOut.Write(FLoss);
1014 if IsKeyPressed(KEY_CHAT) then
1015 begin
1016 kByte := NET_KEY_CHAT;
1017 end
1018 else
1019 begin
1020 if IsKeyPressed(KEY_LEFT) then kByte := kByte or NET_KEY_LEFT;
1021 if IsKeyPressed(KEY_RIGHT) then kByte := kByte or NET_KEY_RIGHT;
1022 if IsKeyPressed(KEY_UP) then kByte := kByte or NET_KEY_UP;
1023 if IsKeyPressed(KEY_DOWN) then kByte := kByte or NET_KEY_DOWN;
1024 if IsKeyPressed(KEY_JUMP) then kByte := kByte or NET_KEY_JUMP;
1025 if JustTeleported then kByte := kByte or NET_KEY_FORCEDIR;
1026 end;
1028 NetOut.Write(kByte);
1029 if Direction = TDirection.D_LEFT then NetOut.Write(Byte(0)) else NetOut.Write(Byte(1));
1031 NetOut.Write(LongWord(Pl.NetWeapCtr));
1032 NetOut.Write(Byte(Pl.CurrWeap));
1034 NetOut.Write(GameX);
1035 NetOut.Write(GameY);
1036 NetOut.Write(GameVelX);
1037 NetOut.Write(GameVelY);
1038 NetOut.Write(GameAccelX);
1039 NetOut.Write(GameAccelY);
1040 end;
1042 g_Net_Host_Send(ID, Reliable, NET_CHAN_PLAYERPOS);
1043 end;
1045 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
1046 var
1047 P: TPlayer;
1048 I: Integer;
1049 ww: Word;
1050 begin
1051 P := g_Player_Get(PID);
1052 if P = nil then Exit;
1054 NetOut.Write(Byte(NET_MSG_PLRSTA));
1055 NetOut.Write(PID);
1057 with P do
1058 begin
1059 NetOut.Write(Byte(alive));
1060 NetOut.Write(Byte(GodMode));
1061 NetOut.Write(Health);
1062 NetOut.Write(Armor);
1063 NetOut.Write(Air);
1064 NetOut.Write(JetFuel);
1065 NetOut.Write(Lives);
1066 NetOut.Write(Team);
1068 // collect all weapons in one word
1069 ww := 0;
1070 for I := WP_FIRST to WP_LAST do if (FWeapon[I]) then ww := ww or (1 shl (i-WP_FIRST));
1071 NetOut.Write(Word(ww));
1073 for I := A_BULLETS to A_HIGH do
1074 NetOut.Write(FAmmo[I]);
1076 for I := A_BULLETS to A_HIGH do
1077 NetOut.Write(FMaxAmmo[I]);
1079 for I := MR_SUIT to MR_MAX do
1080 NetOut.Write(LongWord(FMegaRulez[I]));
1082 // collect all special flags in one byte
1083 ww := 0;
1084 if (R_ITEM_BACKPACK in FRulez) then ww := ww or $01;
1085 if (R_KEY_RED in FRulez) then ww := ww or $02;
1086 if (R_KEY_GREEN in FRulez) then ww := ww or $04;
1087 if (R_KEY_BLUE in FRulez) then ww := ww or $08;
1088 if (R_BERSERK in FRulez) then ww := ww or $10;
1089 NetOut.Write(Byte(ww));
1091 NetOut.Write(Frags);
1092 NetOut.Write(Death);
1094 NetOut.Write(LongWord(NetWeapCtr));
1095 NetOut.Write(Byte(CurrWeap));
1097 // other flags
1098 ww := 0;
1099 if (FSpectator) then ww := ww or $01;
1100 if (FGhost) then ww := ww or $02;
1101 if (FPhysics) then ww := ww or $04;
1102 if (FNoRespawn) then ww := ww or $08;
1103 if (FJetpack) then ww := ww or $10;
1104 NetOut.Write(Byte(ww));
1106 NetOut.Write(FFireTime);
1107 end;
1109 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1110 end;
1112 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
1113 begin
1114 NetOut.Write(Byte(NET_MSG_PLRDMG));
1115 NetOut.Write(PID);
1116 NetOut.Write(Kind);
1117 NetOut.Write(Attacker);
1118 NetOut.Write(Value);
1119 NetOut.Write(VX);
1120 NetOut.Write(VY);
1122 g_Net_Host_Send(ID, False, NET_CHAN_PLAYER);
1123 end;
1125 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
1126 begin
1127 NetOut.Write(Byte(NET_MSG_PLRDIE));
1128 NetOut.Write(PID);
1129 NetOut.Write(KillType);
1130 NetOut.Write(DeathType);
1131 NetOut.Write(Attacker);
1133 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1134 end;
1136 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
1137 begin
1138 NetOut.Write(Byte(NET_MSG_PLRFIRE));
1139 NetOut.Write(PID);
1140 NetOut.Write(Weapon);
1141 NetOut.Write(X);
1142 NetOut.Write(Y);
1143 NetOut.Write(AX);
1144 NetOut.Write(AY);
1145 NetOut.Write(ShotID);
1147 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
1148 end;
1150 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
1151 begin
1152 NetOut.Write(Byte(NET_MSG_PLRDEL));
1153 NetOut.Write(PID);
1155 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1156 end;
1158 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
1159 var
1160 Pl: TPlayer;
1161 begin
1162 Pl := g_Player_Get(PID);
1163 if Pl = nil then Exit;
1165 NetOut.Write(Byte(NET_MSG_PLRSET));
1166 NetOut.Write(PID);
1167 NetOut.Write(Pl.Name);
1168 if Mdl = '' then
1169 NetOut.Write(Pl.Model.Name)
1170 else
1171 NetOut.Write(Mdl);
1172 NetOut.Write(Pl.FColor.R);
1173 NetOut.Write(Pl.FColor.G);
1174 NetOut.Write(Pl.FColor.B);
1175 NetOut.Write(Pl.Team);
1177 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1178 end;
1180 // ITEM (SEND)
1182 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1183 var
1184 it: PItem;
1185 tt: Byte;
1186 begin
1187 it := g_Items_ByIdx(IID);
1189 NetOut.Write(Byte(NET_MSG_ISPAWN));
1190 NetOut.Write(IID);
1191 NetOut.Write(Byte(Quiet));
1192 tt := it.ItemType;
1193 if it.dropped then tt := tt or $80;
1194 NetOut.Write(tt);
1195 NetOut.Write(Byte(it.Fall));
1196 NetOut.Write(Byte(it.Respawnable));
1197 NetOut.Write(it.Obj.X);
1198 NetOut.Write(it.Obj.Y);
1199 NetOut.Write(it.Obj.Vel.X);
1200 NetOut.Write(it.Obj.Vel.Y);
1202 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1203 end;
1205 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1206 begin
1207 NetOut.Write(Byte(NET_MSG_IDEL));
1208 NetOut.Write(IID);
1209 NetOut.Write(Byte(Quiet));
1211 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1212 end;
1214 // PANEL
1216 procedure MH_SEND_PanelTexture(PGUID: Integer; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
1217 var
1218 TP: TPanel;
1219 begin
1220 TP := g_Map_PanelByGUID(PGUID);
1221 if (TP = nil) then exit;
1223 with TP do
1224 begin
1225 NetOut.Write(Byte(NET_MSG_PTEX));
1226 NetOut.Write(LongWord(PGUID));
1227 NetOut.Write(FCurTexture);
1228 NetOut.Write(FCurFrame);
1229 NetOut.Write(FCurFrameCount);
1230 NetOut.Write(AnimLoop);
1231 end;
1233 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1234 end;
1236 procedure MH_SEND_PanelState(PGUID: Integer; ID: Integer = NET_EVERYONE);
1237 var
1238 TP: TPanel;
1239 mpflags: Byte = 0;
1240 begin
1241 TP := g_Map_PanelByGUID(PGUID);
1242 if (TP = nil) then exit;
1244 NetOut.Write(Byte(NET_MSG_PSTATE));
1245 NetOut.Write(LongWord(PGUID));
1246 NetOut.Write(Byte(TP.Enabled));
1247 NetOut.Write(TP.LiftType);
1248 NetOut.Write(TP.X);
1249 NetOut.Write(TP.Y);
1250 NetOut.Write(Word(TP.Width));
1251 NetOut.Write(Word(TP.Height));
1252 // mplats
1253 NetOut.Write(LongInt(TP.movingSpeedX));
1254 NetOut.Write(LongInt(TP.movingSpeedY));
1255 NetOut.Write(LongInt(TP.movingStartX));
1256 NetOut.Write(LongInt(TP.movingStartY));
1257 NetOut.Write(LongInt(TP.movingEndX));
1258 NetOut.Write(LongInt(TP.movingEndY));
1259 NetOut.Write(LongInt(TP.sizeSpeedX));
1260 NetOut.Write(LongInt(TP.sizeSpeedY));
1261 NetOut.Write(LongInt(TP.sizeEndX));
1262 NetOut.Write(LongInt(TP.sizeEndY));
1263 if TP.movingActive then mpflags := mpflags or 1;
1264 if TP.moveOnce then mpflags := mpflags or 2;
1265 NetOut.Write(Byte(mpflags));
1267 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1268 end;
1270 // TRIGGER
1272 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
1273 begin
1274 if gTriggers = nil then Exit;
1275 if T.Sound = nil then Exit;
1277 NetOut.Write(Byte(NET_MSG_TSOUND));
1278 NetOut.Write(T.ClientID);
1279 NetOut.Write(Byte(T.Sound.IsPlaying));
1280 NetOut.Write(LongWord(T.Sound.GetPosition));
1281 NetOut.Write(T.SoundPlayCount);
1283 g_Net_Host_Send(ID, True);
1284 end;
1286 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
1287 begin
1288 NetOut.Write(Byte(NET_MSG_TMUSIC));
1289 NetOut.Write(gMusic.Name);
1290 NetOut.Write(Byte(gMusic.IsPlaying));
1291 NetOut.Write(LongWord(gMusic.GetPosition));
1292 NetOut.Write(Byte(gMusic.SpecPause or gMusic.IsPaused));
1294 g_Net_Host_Send(ID, True);
1295 end;
1297 // MONSTER
1299 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
1300 var
1301 M: TMonster;
1302 begin
1303 M := g_Monsters_ByUID(UID);
1304 if M = nil then
1305 Exit;
1307 with M do
1308 begin
1309 NetOut.Write(Byte(NET_MSG_MSPAWN));
1310 NetOut.Write(UID);
1311 NetOut.Write(MonsterType);
1312 NetOut.Write(MonsterState);
1313 NetOut.Write(MonsterAnim);
1314 NetOut.Write(MonsterTargetUID);
1315 NetOut.Write(MonsterTargetTime);
1316 NetOut.Write(MonsterBehaviour);
1317 NetOut.Write(MonsterSleep);
1318 NetOut.Write(MonsterHealth);
1319 NetOut.Write(MonsterAmmo);
1320 NetOut.Write(GameX);
1321 NetOut.Write(GameY);
1322 NetOut.Write(GameVelX);
1323 NetOut.Write(GameVelY);
1324 NetOut.Write(Byte(GameDirection));
1325 end;
1327 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1328 end;
1330 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
1331 var
1332 M: TMonster;
1333 begin
1334 M := g_Monsters_ByUID(UID);
1335 if M = nil then Exit;
1337 NetOut.Write(Byte(NET_MSG_MPOS));
1338 NetOut.Write(UID);
1340 with M do
1341 begin
1342 NetOut.Write(GameX);
1343 NetOut.Write(GameY);
1344 NetOut.Write(GameVelX);
1345 NetOut.Write(GameVelY);
1346 NetOut.Write(Byte(GameDirection));
1347 end;
1349 g_Net_Host_Send(ID, False, NET_CHAN_MONSTERPOS);
1350 end;
1352 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
1353 var
1354 M: TMonster;
1355 begin
1356 M := g_Monsters_ByUID(UID);
1357 if M = nil then Exit;
1359 NetOut.Write(Byte(NET_MSG_MSTATE));
1360 NetOut.Write(UID);
1362 with M do
1363 begin
1364 NetOut.Write(MonsterState);
1365 NetOut.Write(ForcedAnim);
1366 NetOut.Write(MonsterTargetUID);
1367 NetOut.Write(MonsterTargetTime);
1368 NetOut.Write(MonsterSleep);
1369 NetOut.Write(MonsterHealth);
1370 NetOut.Write(MonsterAmmo);
1371 NetOut.Write(MonsterPain);
1372 NetOut.Write(Byte(AnimIsReverse));
1373 NetOut.Write(FFireTime);
1374 end;
1376 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1377 end;
1379 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
1380 begin
1381 NetOut.Write(Byte(NET_MSG_MSHOT));
1382 NetOut.Write(UID);
1383 NetOut.Write(X);
1384 NetOut.Write(Y);
1385 NetOut.Write(VX);
1386 NetOut.Write(VY);
1388 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1389 end;
1391 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
1392 var
1393 M: TMonster;
1394 begin
1395 M := g_Monsters_ByUID(UID);
1396 if M = nil then Exit;
1398 NetOut.Write(Byte(NET_MSG_MDEL));
1399 NetOut.Write(UID);
1401 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1402 end;
1404 // MISC
1406 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
1407 begin
1408 NetOut.Write(Byte(NET_MSG_TIME_SYNC));
1409 NetOut.Write(Time);
1411 g_Net_Host_Send(ID, False, NET_CHAN_SERVICE);
1412 end;
1414 procedure MH_SEND_VoteEvent(EvType: Byte;
1415 StrArg1: string = 'a'; StrArg2: string = 'b';
1416 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
1417 ID: Integer = NET_EVERYONE);
1418 begin
1419 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
1420 NetOut.Write(EvType);
1421 NetOut.Write(IntArg1);
1422 NetOut.Write(IntArg2);
1423 NetOut.Write(StrArg1);
1424 NetOut.Write(StrArg2);
1426 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1427 end;
1429 // CLIENT MESSAGES //
1431 // GAME
1433 procedure MC_RECV_Chat(var M: TMsg);
1434 var
1435 Txt: string;
1436 Mode: Byte;
1437 begin
1438 Txt := M.ReadString();
1439 Mode := M.ReadByte();
1441 if Mode <> NET_CHAT_SYSTEM then
1442 begin
1443 if Mode = NET_CHAT_PLAYER then
1444 begin
1445 g_Console_Add(Txt, True);
1446 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1447 g_Game_ChatSound(b_Text_Unformat(Txt));
1448 end else
1449 if (Mode = NET_CHAT_TEAM) and (gPlayer1 <> nil) then
1450 begin
1451 if gPlayer1.Team = TEAM_RED then
1452 g_Console_Add(b_Text_Format('\r[Team] ') + Txt, True);
1453 if gPlayer1.Team = TEAM_BLUE then
1454 g_Console_Add(b_Text_Format('\b[Team] ') + Txt, True);
1455 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1456 g_Game_ChatSound(b_Text_Unformat(Txt));
1457 end;
1458 end else
1459 g_Console_Add(Txt, True);
1460 end;
1462 procedure MC_RECV_Effect(var M: TMsg);
1463 var
1464 Kind: Byte;
1465 X, Y: Integer;
1466 Ang: SmallInt;
1467 Anim: TAnimation;
1468 ID: LongWord;
1469 begin
1470 if not gGameOn then Exit;
1471 Kind := M.ReadByte();
1472 X := M.ReadLongInt();
1473 Y := M.ReadLongInt();
1474 Ang := M.ReadSmallInt();
1476 case Kind of
1477 NET_GFX_SPARK:
1478 g_GFX_Spark(X, Y, 2 + Random(2), Ang, 0, 0);
1480 NET_GFX_TELE:
1481 begin
1482 if g_Frames_Get(ID, 'FRAMES_TELEPORT') then
1483 begin
1484 Anim := TAnimation.Create(ID, False, 3);
1485 g_GFX_OnceAnim(X, Y, Anim);
1486 Anim.Free();
1487 end;
1488 if Ang = 1 then
1489 g_Sound_PlayExAt('SOUND_GAME_TELEPORT', X, Y);
1490 end;
1492 NET_GFX_EXPLODE:
1493 begin
1494 if g_Frames_Get(ID, 'FRAMES_EXPLODE_ROCKET') 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_EXPLODEROCKET', X, Y);
1503 end;
1505 NET_GFX_BFGEXPL:
1506 begin
1507 if g_Frames_Get(ID, 'FRAMES_EXPLODE_BFG') then
1508 begin
1509 Anim := TAnimation.Create(ID, False, 6);
1510 Anim.Blending := False;
1511 g_GFX_OnceAnim(X-64, Y-64, Anim);
1512 Anim.Free();
1513 end;
1514 if Ang = 1 then
1515 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEBFG', X, Y);
1516 end;
1518 NET_GFX_BFGHIT:
1519 begin
1520 if g_Frames_Get(ID, 'FRAMES_BFGHIT') then
1521 begin
1522 Anim := TAnimation.Create(ID, False, 4);
1523 g_GFX_OnceAnim(X-32, Y-32, Anim);
1524 Anim.Free();
1525 end;
1526 end;
1528 NET_GFX_FIRE:
1529 begin
1530 if g_Frames_Get(ID, 'FRAMES_FIRE') then
1531 begin
1532 Anim := TAnimation.Create(ID, False, 4);
1533 g_GFX_OnceAnim(X, Y, Anim);
1534 Anim.Free();
1535 end;
1536 if Ang = 1 then
1537 g_Sound_PlayExAt('SOUND_FIRE', X, Y);
1538 end;
1540 NET_GFX_RESPAWN:
1541 begin
1542 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
1543 begin
1544 Anim := TAnimation.Create(ID, False, 4);
1545 g_GFX_OnceAnim(X, Y, Anim);
1546 Anim.Free();
1547 end;
1548 if Ang = 1 then
1549 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
1550 end;
1552 NET_GFX_SHELL1:
1553 g_Player_CreateShell(X, Y, 0, -2, SHELL_BULLET);
1555 NET_GFX_SHELL2:
1556 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1558 NET_GFX_SHELL3:
1559 begin
1560 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1561 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1562 end;
1563 end;
1564 end;
1566 procedure MC_RECV_Sound(var M: TMsg);
1567 var
1568 Name: string;
1569 X, Y: Integer;
1570 Pos: Boolean;
1571 begin
1572 Name := M.ReadString();
1573 Pos := M.ReadByte() <> 0;
1574 if Pos then
1575 begin
1576 X := M.ReadLongInt();
1577 Y := M.ReadLongInt();
1578 g_Sound_PlayExAt(Name, X, Y);
1579 end
1580 else
1581 g_Sound_PlayEx(Name);
1582 end;
1584 procedure MC_RECV_CreateShot(var M: TMsg);
1585 var
1586 I, X, Y, XV, YV: Integer;
1587 Timeout: LongWord;
1588 Target, Spawner: Word;
1589 ShType: Byte;
1590 begin
1591 I := M.ReadLongInt();
1592 ShType := M.ReadByte();
1593 Target := M.ReadWord();
1594 Spawner := M.ReadWord();
1595 Timeout := M.ReadLongWord();
1596 X := M.ReadLongInt();
1597 Y := M.ReadLongInt();
1598 XV := M.ReadLongInt();
1599 YV := M.ReadLongInt();
1601 I := g_Weapon_CreateShot(I, ShType, Spawner, Target, X, Y, XV, YV);
1602 if (Shots <> nil) and (I <= High(Shots)) then
1603 begin
1604 Shots[I].Timeout := Timeout;
1605 //Shots[I].Target := Target; // TODO: find a use for Target later
1606 end;
1607 end;
1609 procedure MC_RECV_UpdateShot(var M: TMsg);
1610 var
1611 I, TX, TY, TXV, TYV: Integer;
1612 begin
1613 I := M.ReadLongInt();
1614 TX := M.ReadLongInt();
1615 TY := M.ReadLongInt();
1616 TXV := M.ReadLongInt();
1617 TYV := M.ReadLongInt();
1619 if (Shots <> nil) and (I <= High(Shots)) then
1620 with (Shots[i]) do
1621 begin
1622 Obj.X := TX;
1623 Obj.Y := TY;
1624 Obj.Vel.X := TXV;
1625 Obj.Vel.Y := TYV;
1626 end;
1627 end;
1629 procedure MC_RECV_DeleteShot(var M: TMsg);
1630 var
1631 I, X, Y: Integer;
1632 L: Boolean;
1633 begin
1634 if not gGameOn then Exit;
1635 I := M.ReadLongInt();
1636 L := (M.ReadByte() <> 0);
1637 X := M.ReadLongInt();
1638 Y := M.ReadLongInt();
1640 g_Weapon_DestroyShot(I, X, Y, L);
1641 end;
1643 procedure MC_RECV_GameStats(var M: TMsg);
1644 begin
1645 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
1646 begin
1647 gTeamStat[TEAM_RED].Goals := M.ReadSmallInt();
1648 gTeamStat[TEAM_BLUE].Goals := M.ReadSmallInt();
1649 end
1650 else
1651 if gGameSettings.GameMode = GM_COOP then
1652 begin
1653 gCoopMonstersKilled := M.ReadWord();
1654 gCoopSecretsFound := M.ReadWord();
1655 end;
1656 end;
1658 procedure MC_RECV_CoopStats(var M: TMsg);
1659 begin
1660 gTotalMonsters := M.ReadLongInt();
1661 gSecretsCount := M.ReadLongInt();
1662 gCoopTotalMonstersKilled := M.ReadWord();
1663 gCoopTotalSecretsFound := M.ReadWord();
1664 gCoopTotalMonsters := M.ReadWord();
1665 gCoopTotalSecrets := M.ReadWord();
1666 end;
1668 procedure MC_RECV_GameEvent(var M: TMsg);
1669 var
1670 EvType: Byte;
1671 EvNum: Integer;
1672 EvStr: string;
1673 EvTime: LongWord;
1674 BHash: Boolean;
1675 EvHash: TMD5Digest;
1676 pl: TPlayer;
1677 i1, i2: TStrings_Locale;
1678 pln: String;
1679 cnt: Byte;
1680 begin
1681 FillChar(EvHash, Sizeof(EvHash), 0);
1682 EvType := M.ReadByte();
1683 EvNum := M.ReadLongInt();
1684 EvStr := M.ReadString();
1685 gLastMap := M.ReadByte() <> 0;
1686 if gLastMap and (gGameSettings.GameMode = GM_COOP) then gStatsOff := True;
1687 gStatsPressed := True;
1688 EvTime := M.ReadLongWord();
1689 BHash := M.ReadByte() <> 0;
1690 if BHash then
1691 EvHash := M.ReadMD5();
1693 gTime := EvTime;
1695 case EvType of
1696 NET_EV_MAPSTART:
1697 begin
1698 gGameOn := False;
1699 g_Game_ClearLoading();
1700 g_Game_StopAllSounds(True);
1702 gSwitchGameMode := Byte(EvNum);
1703 gGameSettings.GameMode := gSwitchGameMode;
1705 gWADHash := EvHash;
1706 if not g_Game_StartMap(EvStr, True) then
1707 begin
1708 if not isWadPath(EvStr) then
1709 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [gGameSettings.WAD + ':\' + EvStr]))
1710 else
1711 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [EvStr]));
1712 Exit;
1713 end;
1715 MC_SEND_FullStateRequest;
1716 end;
1718 NET_EV_MAPEND:
1719 begin
1720 gMissionFailed := EvNum <> 0;
1721 gExit := EXIT_ENDLEVELCUSTOM;
1722 end;
1724 NET_EV_RCON:
1725 begin
1726 case EvNum of
1727 NET_RCON_NOAUTH:
1728 g_Console_Add(_lc[I_NET_RCON_NOAUTH], True);
1729 NET_RCON_PWGOOD:
1730 g_Console_Add(_lc[I_NET_RCON_PWD_VALID], True);
1731 NET_RCON_PWBAD:
1732 g_Console_Add(_lc[I_NET_RCON_PWD_INVALID], True);
1733 end;
1734 end;
1736 NET_EV_CHANGE_TEAM:
1737 begin
1738 if EvNum = TEAM_RED then
1739 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [EvStr]), True);
1740 if EvNum = TEAM_BLUE then
1741 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [EvStr]), True);
1742 end;
1744 NET_EV_PLAYER_KICK:
1745 g_Console_Add(Format(_lc[I_PLAYER_KICK], [EvStr]), True);
1747 NET_EV_PLAYER_BAN:
1748 g_Console_Add(Format(_lc[I_PLAYER_BAN], [EvStr]), True);
1750 NET_EV_LMS_WARMUP:
1751 g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [EvNum]), True);
1753 NET_EV_LMS_SURVIVOR:
1754 g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True);
1756 NET_EV_BIGTEXT:
1757 g_Game_Message(AnsiUpperCase(EvStr), Word(EvNum));
1759 NET_EV_SCORE:
1760 begin
1761 pl := g_Player_Get(EvNum and $FFFF);
1762 if pl = nil then
1763 pln := '?'
1764 else
1765 pln := pl.Name;
1766 cnt := (EvNum shr 16) and $FF;
1767 if Pos('w', EvStr) = 0 then
1768 begin
1769 // Default score
1770 if Pos('t', EvStr) = 0 then
1771 begin
1772 // Player +/- score
1773 if Pos('-', EvStr) = 0 then
1774 begin
1775 if Pos('e', EvStr) = 0 then
1776 i1 := I_PLAYER_SCORE_ADD_OWN
1777 else
1778 i1 := I_PLAYER_SCORE_ADD_ENEMY;
1779 end else
1780 begin
1781 if Pos('e', EvStr) = 0 then
1782 i1 := I_PLAYER_SCORE_SUB_OWN
1783 else
1784 i1 := I_PLAYER_SCORE_SUB_ENEMY;
1785 end;
1786 // Which team
1787 if Pos('r', EvStr) > 0 then
1788 i2 := I_PLAYER_SCORE_TO_RED
1789 else
1790 i2 := I_PLAYER_SCORE_TO_BLUE;
1791 g_Console_Add(Format(_lc[i1], [pln, cnt, _lc[i2]]), True);
1792 end else
1793 begin
1794 // Team +/- score
1795 if Pos('-', EvStr) = 0 then
1796 i1 := I_PLAYER_SCORE_ADD_TEAM
1797 else
1798 i1 := I_PLAYER_SCORE_SUB_TEAM;
1799 // Which team
1800 if Pos('r', EvStr) > 0 then
1801 i2 := I_PLAYER_SCORE_RED
1802 else
1803 i2 := I_PLAYER_SCORE_BLUE;
1804 g_Console_Add(Format(_lc[i1], [_lc[i2], cnt]), True);
1805 end;
1806 end else
1807 begin
1808 // Game Win
1809 if Pos('e', EvStr) = 0 then
1810 i1 := I_PLAYER_SCORE_WIN_OWN
1811 else
1812 i1 := I_PLAYER_SCORE_WIN_ENEMY;
1813 // Which team
1814 if Pos('r', EvStr) > 0 then
1815 i2 := I_PLAYER_SCORE_TO_RED
1816 else
1817 i2 := I_PLAYER_SCORE_TO_BLUE;
1818 g_Console_Add(Format(_lc[i1], [pln, _lc[i2]]), True);
1819 end;
1820 end;
1822 NET_EV_SCORE_MSG:
1823 begin
1824 if EvNum = TEAM_RED then
1825 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1826 if EvNum = TEAM_BLUE then
1827 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1828 if EvNum = -TEAM_RED then
1829 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1830 if EvNum = -TEAM_BLUE then
1831 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1832 end;
1834 NET_EV_LMS_START:
1835 begin
1836 g_Player_RemoveAllCorpses;
1837 g_Game_Message(_lc[I_MESSAGE_LMS_START], 144);
1838 end;
1840 NET_EV_LMS_WIN:
1841 g_Game_Message(Format(_lc[I_MESSAGE_LMS_WIN], [AnsiUpperCase(EvStr)]), 144);
1843 NET_EV_TLMS_WIN:
1844 begin
1845 if EvNum = TEAM_RED then
1846 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 144);
1847 if EvNum = TEAM_BLUE then
1848 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 144);
1849 end;
1851 NET_EV_LMS_LOSE:
1852 g_Game_Message(_lc[I_MESSAGE_LMS_LOSE], 144);
1854 NET_EV_LMS_DRAW:
1855 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
1857 NET_EV_KILLCOMBO:
1858 g_Game_Announce_KillCombo(EvNum);
1860 NET_EV_PLAYER_TOUCH:
1861 begin
1862 pl := g_Player_Get(EvNum);
1863 if pl <> nil then
1864 pl.Touch();
1865 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: 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);
1912 end;
1914 FLAG_STATE_CAPTURED:
1915 begin
1916 if (Pl <> nil) then Pl.SetFlag(Fl);
1918 if Quiet then Exit;
1920 if Fl = FLAG_RED then
1921 s := _lc[I_PLAYER_FLAG_RED]
1922 else
1923 s := _lc[I_PLAYER_FLAG_BLUE];
1925 g_Console_Add(Format(_lc[I_PLAYER_FLAG_GET], [Pl.Name, s]), True);
1926 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_GET], [AnsiUpperCase(s)]), 144);
1927 end;
1929 FLAG_STATE_DROPPED:
1930 begin
1931 if (Pl <> nil) then Pl.SetFlag(FLAG_NONE);
1933 if Quiet or (Pl = nil) then Exit;
1935 if Fl = FLAG_RED then
1936 s := _lc[I_PLAYER_FLAG_RED]
1937 else
1938 s := _lc[I_PLAYER_FLAG_BLUE];
1940 g_Console_Add(Format(_lc[I_PLAYER_FLAG_DROP], [Pl.Name, s]), True);
1941 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_DROP], [AnsiUpperCase(s)]), 144);
1942 end;
1944 FLAG_STATE_SCORED:
1945 begin
1946 g_Map_ResetFlag(FLAG_RED);
1947 g_Map_ResetFlag(FLAG_BLUE);
1948 if Quiet or (Pl = nil) then Exit;
1949 Pl.SetFlag(FLAG_NONE);
1951 if Fl = FLAG_RED then
1952 s := _lc[I_PLAYER_FLAG_RED]
1953 else
1954 s := _lc[I_PLAYER_FLAG_BLUE];
1956 ts := Format('%.4d', [gFlags[Fl].CaptureTime]);
1957 Insert('.', ts, Length(ts) + 1 - 3);
1958 g_Console_Add(Format(_lc[I_PLAYER_FLAG_CAPTURE], [Pl.Name, s, ts]), True);
1959 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_CAPTURE], [AnsiUpperCase(s)]), 144);
1960 end;
1962 FLAG_STATE_RETURNED:
1963 begin
1964 g_Map_ResetFlag(Fl);
1965 if Quiet then Exit;
1967 if Fl = FLAG_RED then
1968 s := _lc[I_PLAYER_FLAG_RED]
1969 else
1970 s := _lc[I_PLAYER_FLAG_BLUE];
1972 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1973 end;
1974 end;
1975 end;
1977 procedure MC_RECV_GameSettings(var M: TMsg);
1978 begin
1979 gGameSettings.GameMode := M.ReadByte();
1980 gGameSettings.GoalLimit := M.ReadWord();
1981 gGameSettings.TimeLimit := M.ReadWord();
1982 gGameSettings.MaxLives := M.ReadByte();
1983 gGameSettings.Options := M.ReadLongWord();
1984 end;
1986 // PLAYER
1988 function MC_RECV_PlayerCreate(var M: TMsg): Word;
1989 var
1990 PID, DID: Word;
1991 PName, Model: string;
1992 Color: TRGB;
1993 T: Byte;
1994 Pl: TPlayer;
1995 begin
1996 PID := M.ReadWord();
1997 Pl := g_Player_Get(PID);
1999 PName := M.ReadString();
2000 Model := M.ReadString();
2001 Color.R := M.ReadByte();
2002 Color.G := M.ReadByte();
2003 Color.B := M.ReadByte();
2004 T := M.ReadByte();
2006 Result := 0;
2007 if (PID <> NetPlrUID1) and (PID <> NetPlrUID2) then
2008 begin
2009 if (Pl <> nil) then Exit;
2010 DID := g_Player_Create(Model, Color, T, False);
2011 with g_Player_Get(DID) do
2012 begin
2013 UID := PID;
2014 Name := PName;
2015 Reset(True);
2016 end;
2017 end
2018 else
2019 begin
2020 if (PID = NetPlrUID1) and (gPlayer1 <> nil) then begin
2021 gPlayer1.UID := PID;
2022 gPlayer1.Model.SetColor(Color.R, Color.G, Color.B);
2023 gPlayer1.ChangeTeam(T);
2024 end;
2025 if (PID = NetPlrUID2) and (gPlayer2 <> nil) then begin
2026 gPlayer2.UID := PID;
2027 gPlayer2.Model.SetColor(Color.R, Color.G, Color.B);
2028 gPlayer2.ChangeTeam(T);
2029 end;
2030 end;
2032 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
2033 e_WriteLog('NET: Player ' + PName + ' [' + IntToStr(PID) + '] added.', TMsgType.Notify);
2034 Result := PID;
2035 end;
2037 function MC_RECV_PlayerPos(var M: TMsg): Word;
2038 var
2039 GT, wctr: LongWord;
2040 PID: Word;
2041 kByte: Word;
2042 Pl: TPlayer;
2043 Dir, weapon: Byte;
2044 TmpX, TmpY: Integer;
2045 begin
2046 Result := 0;
2048 GT := M.ReadLongWord();
2049 if GT < gTime - NET_MAX_DIFFTIME then
2050 begin
2051 gTime := GT;
2052 Exit;
2053 end;
2054 gTime := GT;
2056 PID := M.ReadWord();
2057 Pl := g_Player_Get(PID);
2059 if Pl = nil then Exit;
2061 Result := PID;
2063 with Pl do
2064 begin
2065 FPing := M.ReadWord();
2066 FLoss := M.ReadByte();
2067 kByte := M.ReadWord();
2068 Dir := M.ReadByte();
2070 wctr := M.ReadLongWord();
2071 weapon := M.ReadByte();
2072 // `>=`, so server is still the authority
2073 if (wctr >= Pl.NetWeapCtr) then
2074 begin
2075 Pl.CurrWeap := weapon;
2076 Pl.NetWeapCtr := wctr;
2077 end;
2079 TmpX := M.ReadLongInt();
2080 TmpY := M.ReadLongInt();
2082 ReleaseKeysNoWeapon;
2084 if (kByte = NET_KEY_CHAT) then
2085 begin
2086 PressKey(KEY_CHAT, 10000);
2087 end
2088 else
2089 begin
2090 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
2091 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
2092 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
2093 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
2094 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
2095 end;
2097 if ((Pl <> gPlayer1) and (Pl <> gPlayer2)) or LongBool(kByte and NET_KEY_FORCEDIR) then
2098 SetDirection(TDirection(Dir));
2100 GameVelX := M.ReadLongInt();
2101 GameVelY := M.ReadLongInt();
2102 GameAccelX := M.ReadLongInt();
2103 GameAccelY := M.ReadLongInt();
2104 SetLerp(TmpX, TmpY);
2105 if NetForcePlayerUpdate then Update();
2106 end;
2107 end;
2109 function MC_RECV_PlayerStats(var M: TMsg): Word;
2110 var
2111 PID: Word;
2112 Pl: TPlayer;
2113 I: Integer;
2114 OldJet: Boolean;
2115 NewTeam: Byte;
2116 ww: Word;
2117 newweapon: Byte;
2118 wctr: LongWord;
2119 begin
2120 PID := M.ReadWord();
2121 Pl := g_Player_Get(PID);
2122 Result := 0;
2123 if Pl = nil then
2124 Exit;
2126 with Pl do
2127 begin
2128 alive := (M.ReadByte() <> 0);
2129 GodMode := (M.ReadByte() <> 0);
2130 Health := M.ReadLongInt();
2131 Armor := M.ReadLongInt();
2132 Air := M.ReadLongInt();
2133 JetFuel := M.ReadLongInt();
2134 Lives := M.ReadByte();
2135 NewTeam := M.ReadByte();
2137 ww := M.ReadWord();
2138 for I := WP_FIRST to WP_LAST do
2139 begin
2140 FWeapon[I] := ((ww and $01) <> 0);
2141 ww := ww shr 1;
2142 end;
2144 for I := A_BULLETS to A_HIGH do
2145 FAmmo[I] := M.ReadWord();
2147 for I := A_BULLETS to A_HIGH do
2148 FMaxAmmo[I] := M.ReadWord();
2150 for I := MR_SUIT to MR_MAX do
2151 FMegaRulez[I] := M.ReadLongWord();
2153 FRulez := [];
2154 // unpack special flags
2155 ww := M.ReadByte();
2156 if ((ww and $01) <> 0) then FRulez := FRulez+[R_ITEM_BACKPACK];
2157 if ((ww and $02) <> 0) then FRulez := FRulez+[R_KEY_RED];
2158 if ((ww and $04) <> 0) then FRulez := FRulez+[R_KEY_GREEN];
2159 if ((ww and $08) <> 0) then FRulez := FRulez+[R_KEY_BLUE];
2160 if ((ww and $10) <> 0) then FRulez := FRulez+[R_BERSERK];
2162 Frags := M.ReadLongInt();
2163 Death := M.ReadLongInt();
2165 wctr := M.ReadLongWord();
2166 newweapon := M.ReadByte();
2167 {$IFDEF K8_XXX_WEAPON_DEBUG}
2168 writeln('CLIENT PLRSTATS: got: currweap=', CurrWeap, '; curfrm=', gTime, '; netweap=', newweapon, '; lastnwfrm=', NetForceWeapFIdx);
2169 {$ENDIF}
2170 // `>=`, so server is still the authority
2171 if (wctr >= NetWeapCtr) then
2172 begin
2173 SetWeaponHost(newweapon);
2174 NetWeapCtr := wctr;
2175 end;
2177 // other flags
2178 ww := M.ReadByte();
2180 FSpectator := ((ww and $01) <> 0);
2181 if FSpectator then
2182 begin
2183 if Pl = gPlayer1 then
2184 begin
2185 gLMSPID1 := UID;
2186 gPlayer1 := nil;
2187 end;
2188 if Pl = gPlayer2 then
2189 begin
2190 gLMSPID2 := UID;
2191 gPlayer2 := nil;
2192 end;
2193 end
2194 else
2195 begin
2196 if (gPlayer1 = nil) and (gLMSPID1 > 0) then
2197 gPlayer1 := g_Player_Get(gLMSPID1);
2198 if (gPlayer2 = nil) and (gLMSPID2 > 0) then
2199 gPlayer2 := g_Player_Get(gLMSPID2);
2200 end;
2202 FGhost := ((ww and $02) <> 0);
2203 FPhysics := ((ww and $04) <> 0);
2204 FNoRespawn := ((ww and $08) <> 0);
2205 OldJet := FJetpack;
2206 FJetpack := ((ww and $10) <> 0);
2207 FFireTime := M.ReadLongInt();
2208 if OldJet and not FJetpack then
2209 JetpackOff
2210 else if not OldJet and FJetpack then
2211 JetpackOn;
2212 if Team <> NewTeam then
2213 Pl.ChangeTeam(NewTeam);
2214 end;
2216 Result := PID;
2217 end;
2219 function MC_RECV_PlayerDamage(var M: TMsg): Word;
2220 var
2221 PID: Word;
2222 Pl: TPlayer;
2223 Kind: Byte;
2224 Attacker, Value: Word;
2225 VX, VY: Integer;
2226 begin
2227 Result := 0;
2228 if not gGameOn then Exit;
2229 PID := M.ReadWord();
2230 Pl := g_Player_Get(PID);
2231 if Pl = nil then Exit;
2233 Kind := M.ReadByte();
2234 Attacker := M.ReadWord();
2235 Value := M.ReadWord();
2236 VX := M.ReadWord();
2237 VY := M.ReadWord();
2239 with Pl do
2240 Damage(Value, Attacker, VX, VY, Kind);
2242 Result := PID;
2243 end;
2245 function MC_RECV_PlayerDeath(var M: TMsg): Word;
2246 var
2247 PID: Word;
2248 Pl: TPlayer;
2249 KillType, DeathType: Byte;
2250 Attacker: Word;
2251 begin
2252 Result := 0;
2253 if not gGameOn then Exit;
2254 PID := M.ReadWord();
2255 Pl := g_Player_Get(PID);
2256 if Pl = nil then Exit;
2258 KillType := M.ReadByte();
2259 DeathType := M.ReadByte();
2260 Attacker := M.ReadWord();
2262 with Pl do
2263 begin
2264 Kill(KillType, Attacker, DeathType);
2265 SoftReset;
2266 end;
2267 end;
2269 function MC_RECV_PlayerDelete(var M: TMsg): Word;
2270 var
2271 PID: Word;
2272 Pl: TPlayer;
2273 begin
2274 PID := M.ReadWord();
2275 Pl := g_Player_Get(PID);
2276 Result := 0;
2277 if Pl = nil then Exit;
2279 g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [Pl.Name]), True);
2280 e_WriteLog('NET: Player ' + Pl.Name + ' [' + IntToStr(PID) + '] removed.', TMsgType.Notify);
2282 g_Player_Remove(PID);
2284 Result := PID;
2285 end;
2287 function MC_RECV_PlayerFire(var M: TMsg): Word;
2288 var
2289 PID: Word;
2290 Weap: Byte;
2291 Pl: TPlayer;
2292 X, Y, AX, AY: Integer;
2293 SHID: Integer;
2294 begin
2295 Result := 0;
2296 if not gGameOn then Exit;
2297 PID := M.ReadWord();
2298 Pl := g_Player_Get(PID);
2299 if Pl = nil then Exit;
2301 Weap := M.ReadByte();
2302 X := M.ReadLongInt();
2303 Y := M.ReadLongInt();
2304 AX := M.ReadLongInt();
2305 AY := M.ReadLongInt();
2306 SHID := M.ReadLongInt();
2308 with Pl do
2309 if alive then NetFire(Weap, X, Y, AX, AY, SHID);
2310 end;
2312 procedure MC_RECV_PlayerSettings(var M: TMsg);
2313 var
2314 TmpName: string;
2315 TmpModel: string;
2316 TmpColor: TRGB;
2317 TmpTeam: Byte;
2318 Pl: TPlayer;
2319 PID: Word;
2320 begin
2321 PID := M.ReadWord();
2322 Pl := g_Player_Get(PID);
2323 if Pl = nil then Exit;
2325 TmpName := M.ReadString();
2326 TmpModel := M.ReadString();
2327 TmpColor.R := M.ReadByte();
2328 TmpColor.G := M.ReadByte();
2329 TmpColor.B := M.ReadByte();
2330 TmpTeam := M.ReadByte();
2332 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
2333 begin
2334 Pl.ChangeTeam(TmpTeam);
2335 if gPlayer1 = Pl then
2336 gPlayer1Settings.Team := TmpTeam;
2337 if gPlayer2 = Pl then
2338 gPlayer2Settings.Team := TmpTeam;
2339 end else
2340 Pl.SetColor(TmpColor);
2342 if Pl.Name <> TmpName then
2343 begin
2344 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
2345 Pl.Name := TmpName;
2346 end;
2348 if TmpModel <> Pl.Model.Name then
2349 Pl.SetModel(TmpModel);
2350 end;
2352 // ITEM
2354 procedure MC_RECV_ItemSpawn(var M: TMsg);
2355 var
2356 ID: Word;
2357 AID: DWord;
2358 X, Y, VX, VY: Integer;
2359 T: Byte;
2360 Quiet, Fall{, Resp}: Boolean;
2361 Anim: TAnimation;
2362 it: PItem;
2363 begin
2364 if not gGameOn then Exit;
2365 ID := M.ReadWord();
2366 Quiet := M.ReadByte() <> 0;
2367 T := M.ReadByte();
2368 Fall := M.ReadByte() <> 0;
2369 {Resp :=} M.ReadByte();
2370 X := M.ReadLongInt();
2371 Y := M.ReadLongInt();
2372 VX := M.ReadLongInt();
2373 VY := M.ReadLongInt();
2375 g_Items_Create(X, Y, T and $7F, Fall, False, False, ID);
2376 if ((T and $80) <> 0) then g_Items_SetDrop(ID);
2378 it := g_Items_ByIdx(ID);
2379 it.Obj.Vel.X := VX;
2380 it.Obj.Vel.Y := VY;
2382 if not Quiet then
2383 begin
2384 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
2385 if g_Frames_Get(AID, 'FRAMES_ITEM_RESPAWN') then
2386 begin
2387 Anim := TAnimation.Create(AID, False, 4);
2388 g_GFX_OnceAnim(X+(it.Obj.Rect.Width div 2)-16, Y+(it.Obj.Rect.Height div 2)-16, Anim);
2389 Anim.Free();
2390 end;
2391 end;
2392 end;
2394 procedure MC_RECV_ItemDestroy(var M: TMsg);
2395 var
2396 ID: Word;
2397 Quiet: Boolean;
2398 begin
2399 if not gGameOn then Exit;
2400 ID := M.ReadWord();
2401 Quiet := M.ReadByte() <> 0;
2403 if not g_Items_ValidId(ID) then exit;
2405 if not Quiet then g_Items_EmitPickupSound(ID);
2407 g_Items_Remove(ID);
2408 end;
2410 // PANEL
2412 procedure MC_RECV_PanelTexture(var M: TMsg);
2413 var
2414 TP: TPanel;
2415 PGUID: Integer;
2416 Tex, Fr: Integer;
2417 Loop, Cnt: Byte;
2418 begin
2419 if not gGameOn then Exit;
2421 PGUID := Integer(M.ReadLongWord());
2422 Tex := M.ReadLongInt();
2423 Fr := M.ReadLongInt();
2424 Cnt := M.ReadByte();
2425 Loop := M.ReadByte();
2427 TP := g_Map_PanelByGUID(PGUID);
2428 if (TP <> nil) then
2429 begin
2430 // switch texture
2431 TP.SetTexture(Tex, Loop);
2432 TP.SetFrame(Fr, Cnt);
2433 end;
2434 end;
2436 procedure MC_RECV_PanelState(var M: TMsg);
2437 var
2438 PGUID: Integer;
2439 E: Boolean;
2440 Lift: Byte;
2441 X, Y, W, H: Integer;
2442 TP: TPanel;
2443 speedX, speedY, startX, startY, endX, endY: Integer;
2444 sizeSpX, sizeSpY, sizeEX, sizeEY: Integer;
2445 mpflags: Byte;
2446 begin
2447 if not gGameOn then Exit;
2449 PGUID := Integer(M.ReadLongWord());
2450 E := (M.ReadByte() <> 0);
2451 Lift := M.ReadByte();
2452 X := M.ReadLongInt();
2453 Y := M.ReadLongInt();
2454 W := M.ReadWord();
2455 H := M.ReadWord();
2456 // mplats
2457 speedX := M.ReadLongInt();
2458 speedY := M.ReadLongInt();
2459 startX := M.ReadLongInt();
2460 startY := M.ReadLongInt();
2461 endX := M.ReadLongInt();
2462 endY := M.ReadLongInt();
2463 sizeSpX := M.ReadLongInt();
2464 sizeSpY := M.ReadLongInt();
2465 sizeEX := M.ReadLongInt();
2466 sizeEY := M.ReadLongInt();
2467 mpflags := M.ReadByte(); // bit0: TP.movingActive; bit1: TP.moveOnce
2469 TP := g_Map_PanelByGUID(PGUID);
2470 if (TP = nil) then exit;
2472 // update lifts state
2473 if TP.isGLift then g_Map_SetLiftGUID(PGUID, Lift);
2475 // update enabled/disabled state for all panels
2476 if E then g_Map_EnableWallGUID(PGUID) else g_Map_DisableWallGUID(PGUID);
2478 // update panel position, as it can be moved (mplat)
2479 TP.X := X;
2480 TP.Y := Y;
2481 TP.Width := W;
2482 TP.Height := H;
2483 // update mplat state
2484 TP.movingSpeedX := speedX;
2485 TP.movingSpeedY := speedY;
2486 TP.movingStartX := startX;
2487 TP.movingStartY := startY;
2488 TP.movingEndX := endX;
2489 TP.movingEndY := endY;
2490 TP.sizeSpeedX := sizeSpX;
2491 TP.sizeSpeedY := sizeSpY;
2492 TP.sizeEndX := sizeEX;
2493 TP.sizeEndY := sizeEY;
2494 TP.movingActive := ((mpflags and 1) <> 0);
2495 TP.moveOnce := ((mpflags and 2) <> 0);
2496 // notify panel of it's position/size change, so it can fix other internal structures
2497 TP.positionChanged();
2498 end;
2500 // TRIGGERS
2502 procedure MC_RECV_TriggerSound(var M: TMsg);
2503 var
2504 SPlaying: Boolean;
2505 SPos, SID: LongWord;
2506 SCount: LongInt;
2507 I: Integer;
2508 begin
2509 if not gGameOn then Exit;
2510 if gTriggers = nil then Exit;
2512 SID := M.ReadLongWord();
2513 SPlaying := M.ReadByte() <> 0;
2514 SPos := M.ReadLongWord();
2515 SCount := M.ReadLongInt();
2517 for I := Low(gTriggers) to High(gTriggers) do
2518 if gTriggers[I].TriggerType = TRIGGER_SOUND then
2519 if gTriggers[I].ClientID = SID then
2520 with gTriggers[I] do
2521 begin
2522 if Sound <> nil then
2523 begin
2524 if SPlaying then
2525 begin
2526 if tgcLocal then
2527 Sound.PlayVolumeAt(X+(Width div 2), Y+(Height div 2), tgcVolume/255.0)
2528 else
2529 Sound.PlayPanVolume((tgcPan-127.0)/128.0, tgcVolume/255.0);
2530 Sound.SetPosition(SPos);
2531 end
2532 else
2533 if Sound.IsPlaying then Sound.Stop;
2534 end;
2536 SoundPlayCount := SCount;
2537 end;
2538 end;
2540 procedure MC_RECV_TriggerMusic(var M: TMsg);
2541 var
2542 MName: string;
2543 MPlaying: Boolean;
2544 MPos: LongWord;
2545 MPaused: Boolean;
2546 begin
2547 if not gGameOn then Exit;
2549 MName := M.ReadString();
2550 MPlaying := M.ReadByte() <> 0;
2551 MPos := M.ReadLongWord();
2552 MPaused := M.ReadByte() <> 0;
2554 if MPlaying then
2555 begin
2556 gMusic.SetByName(MName);
2557 gMusic.Play(True);
2558 gMusic.SetPosition(MPos);
2559 gMusic.SpecPause := MPaused;
2560 end
2561 else
2562 if gMusic.IsPlaying then gMusic.Stop;
2563 end;
2565 // MONSTERS
2567 procedure MC_RECV_MonsterSpawn(var M: TMsg);
2568 var
2569 ID: Word;
2570 MType, MState, MDir, MAnim, MBehav: Byte;
2571 X, Y, VX, VY, MTargTime, MHealth, MAmmo, MSleep: Integer;
2572 MTarg: Word;
2573 Mon: TMonster;
2574 begin
2575 ID := M.ReadWord();
2576 Mon := g_Monsters_ByUID(ID);
2577 if Mon <> nil then
2578 Exit;
2580 MType := M.ReadByte();
2581 MState := M.ReadByte();
2582 MAnim := M.ReadByte();
2583 MTarg := M.ReadWord();
2584 MTargTime := M.ReadLongInt();
2585 MBehav := M.ReadByte();
2586 MSleep := M.ReadLongInt();
2587 MHealth := M.ReadLongInt();
2588 MAmmo := M.ReadLongInt();
2590 X := M.ReadLongInt();
2591 Y := M.ReadLongInt();
2592 VX := M.ReadLongInt();
2593 VY := M.ReadLongInt();
2594 MDir := M.ReadByte();
2596 g_Monsters_Create(MType, X, Y, TDirection(MDir), False, ID);
2597 Mon := g_Monsters_ByUID(ID);
2598 if Mon = nil then
2599 Exit;
2601 with Mon do
2602 begin
2604 MonsterAnim := MAnim;
2605 MonsterTargetUID := MTarg;
2606 MonsterTargetTime := MTargTime;
2607 MonsterBehaviour := MBehav;
2608 MonsterSleep := MSleep;
2609 MonsterAmmo := MAmmo;
2610 SetHealth(MHealth);
2612 SetState(MState);
2614 setPosition(X, Y); // this will call positionChanged();
2615 GameVelX := VX;
2616 GameVelY := VY;
2617 end;
2618 end;
2620 procedure MC_RECV_MonsterPos(var M: TMsg);
2621 var
2622 Mon: TMonster;
2623 ID: Word;
2624 X, Y: Integer;
2625 begin
2626 ID := M.ReadWord();
2627 Mon := g_Monsters_ByUID(ID);
2628 if Mon = nil then
2629 Exit;
2631 with Mon do
2632 begin
2633 X := M.ReadLongInt();
2634 Y := M.ReadLongInt();
2635 Mon.setPosition(X, Y); // this will call `positionChanged()`
2636 GameVelX := M.ReadLongInt();
2637 GameVelY := M.ReadLongInt();
2638 GameDirection := TDirection(M.ReadByte());
2639 end;
2640 end;
2642 procedure MC_RECV_MonsterState(var M: TMsg);
2643 var
2644 ID: Integer;
2645 MState, MFAnm: Byte;
2646 Mon: TMonster;
2647 AnimRevert: Boolean;
2648 begin
2649 ID := M.ReadWord();
2650 Mon := g_Monsters_ByUID(ID);
2651 if Mon = nil then Exit;
2653 MState := M.ReadByte();
2654 MFAnm := M.ReadByte();
2656 with Mon do
2657 begin
2658 MonsterTargetUID := M.ReadWord();
2659 MonsterTargetTime := M.ReadLongInt();
2660 MonsterSleep := M.ReadLongInt();
2661 MonsterHealth := M.ReadLongInt();
2662 MonsterAmmo := M.ReadLongInt();
2663 MonsterPain := M.ReadLongInt();
2664 AnimRevert := M.ReadByte() <> 0;
2665 FFireTime := M.ReadLongInt();
2666 RevertAnim(AnimRevert);
2668 if MonsterState <> MState then
2669 begin
2670 if (MState = MONSTATE_GO) and (MonsterState = MONSTATE_SLEEP) then WakeUpSound();
2671 if (MState = MONSTATE_DIE) then DieSound();
2672 if (MState = MONSTATE_PAIN) then MakeBloodSimple(Min(200, MonsterPain));
2673 if (MState = MONSTATE_ATTACK) then kick(nil);
2674 if (MState = MONSTATE_DEAD) then SetDeadAnim();
2676 SetState(MState, MFAnm);
2677 end;
2678 end;
2679 end;
2681 procedure MC_RECV_MonsterShot(var M: TMsg);
2682 var
2683 ID: Integer;
2684 Mon: TMonster;
2685 X, Y, VX, VY: Integer;
2686 begin
2687 ID := M.ReadWord();
2689 Mon := g_Monsters_ByUID(ID);
2690 if Mon = nil then Exit;
2692 X := M.ReadLongInt();
2693 Y := M.ReadLongInt();
2694 VX := M.ReadLongInt();
2695 VY := M.ReadLongInt();
2697 Mon.ClientAttack(X, Y, VX, VY);
2698 end;
2700 procedure MC_RECV_MonsterDelete(var M: TMsg);
2701 var
2702 ID: Integer;
2703 Mon: TMonster;
2704 begin
2705 ID := M.ReadWord();
2706 Mon := g_Monsters_ByUID(ID);
2707 if Mon = nil then Exit;
2708 Mon.SetState(5);
2709 Mon.MonsterRemoved := True;
2710 end;
2712 procedure MC_RECV_TimeSync(var M: TMsg);
2713 var
2714 Time: LongWord;
2715 begin
2716 Time := M.ReadLongWord();
2718 if gState = STATE_INTERCUSTOM then
2719 gServInterTime := Min(Time, 255);
2720 end;
2722 procedure MC_RECV_VoteEvent(var M: TMsg);
2723 var
2724 EvID: Byte;
2725 Str1, Str2: string;
2726 Int1, Int2: SmallInt;
2727 begin
2728 EvID := M.ReadByte();
2729 Int1 := M.ReadSmallInt();
2730 Int2 := M.ReadSmallInt();
2731 Str1 := M.ReadString();
2732 Str2 := M.ReadString();
2734 case EvID of
2735 NET_VE_STARTED:
2736 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_STARTED], [Str1, Str2, Int1]), True);
2737 NET_VE_PASSED:
2738 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_PASSED], [Str1]), True);
2739 NET_VE_FAILED:
2740 g_Console_Add(_lc[I_MESSAGE_VOTE_FAILED], True);
2741 NET_VE_VOTE:
2742 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Str1, Int1, Int2]), True);
2743 NET_VE_INPROGRESS:
2744 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_INPROGRESS], [Str1]), True);
2745 end;
2746 end;
2748 // CLIENT SEND
2750 procedure MC_SEND_Info(Password: string);
2751 begin
2752 NetOut.Clear();
2754 NetOut.Write(Byte(NET_MSG_INFO));
2755 NetOut.Write(GAME_VERSION);
2756 NetOut.Write(Password);
2757 NetOut.Write(gPlayer1Settings.Name);
2758 NetOut.Write(gPlayer1Settings.Model);
2759 NetOut.Write(gPlayer1Settings.Color.R);
2760 NetOut.Write(gPlayer1Settings.Color.G);
2761 NetOut.Write(gPlayer1Settings.Color.B);
2762 NetOut.Write(gPlayer1Settings.Team);
2764 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2765 end;
2767 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
2768 begin
2769 NetOut.Write(Byte(NET_MSG_CHAT));
2770 NetOut.Write(Txt);
2771 NetOut.Write(Mode);
2773 g_Net_Client_Send(True, NET_CHAN_CHAT);
2774 end;
2776 function isKeyPressed (key1: Word; key2: Word): Boolean;
2777 begin
2778 if (key1 <> 0) and e_KeyPressed(key1) then begin result := true; exit; end;
2779 if (key2 <> 0) and e_KeyPressed(key2) then begin result := true; exit; end;
2780 result := false;
2781 end;
2783 procedure MC_SEND_PlayerPos();
2784 var
2785 kByte: Word;
2786 Predict: Boolean;
2787 strafeDir: Byte;
2788 //WeaponSelect: Word = 0;
2789 I: Integer;
2790 begin
2791 if not gGameOn then Exit;
2792 if gPlayers = nil then Exit;
2793 if gPlayer1 = nil then Exit;
2795 kByte := 0;
2796 Predict := NetPredictSelf; // and (not NetGotKeys);
2798 if (not gConsoleShow) and (not gChatShow) and (g_ActiveWindow = nil) then
2799 begin
2800 strafeDir := P1MoveButton shr 4;
2801 P1MoveButton := P1MoveButton and $0F;
2802 with gGameControls.P1Control do
2803 begin
2804 if isKeyPressed(KeyLeft, KeyLeft2) and (not isKeyPressed(KeyRight, KeyRight2)) then P1MoveButton := 1
2805 else if (not isKeyPressed(KeyLeft, KeyLeft2)) and isKeyPressed(KeyRight, KeyRight2) then P1MoveButton := 2
2806 else if (not isKeyPressed(KeyLeft, KeyLeft2)) and (not isKeyPressed(KeyRight, KeyRight2)) then P1MoveButton := 0;
2808 // strafing
2809 if isKeyPressed(KeyStrafe, KeyStrafe2) then
2810 begin
2811 // new strafe mechanics
2812 if (strafeDir = 0) then strafeDir := P1MoveButton; // start strafing
2813 // now set direction according to strafe (reversed)
2814 if (strafeDir = 2) then gPlayer1.SetDirection(TDirection.D_LEFT)
2815 else if (strafeDir = 1) then gPlayer1.SetDirection(TDirection.D_RIGHT);
2816 end
2817 else
2818 begin
2819 if (P1MoveButton = 2) and isKeyPressed(KeyLeft, KeyLeft2) then gPlayer1.SetDirection(TDirection.D_LEFT)
2820 else if (P1MoveButton = 1) and isKeyPressed(KeyRight, KeyRight2) then gPlayer1.SetDirection(TDirection.D_RIGHT)
2821 else if P1MoveButton <> 0 then gPlayer1.SetDirection(TDirection(P1MoveButton-1));
2822 end;
2824 gPlayer1.ReleaseKeys;
2825 gPlayer1.weaponSwitchKeysStateChange(-1, isKeyPressed(KeyNextWeapon, KeyNextWeapon2));
2826 gPlayer1.weaponSwitchKeysStateChange(-2, isKeyPressed(KeyPrevWeapon, KeyPrevWeapon2));
2828 if P1MoveButton = 1 then
2829 begin
2830 kByte := kByte or NET_KEY_LEFT;
2831 if Predict then gPlayer1.PressKey(KEY_LEFT, 10000);
2832 end;
2833 if P1MoveButton = 2 then
2834 begin
2835 kByte := kByte or NET_KEY_RIGHT;
2836 if Predict then gPlayer1.PressKey(KEY_RIGHT, 10000);
2837 end;
2838 if isKeyPressed(KeyUp, KeyUp2) then
2839 begin
2840 kByte := kByte or NET_KEY_UP;
2841 gPlayer1.PressKey(KEY_UP, 10000);
2842 end;
2843 if isKeyPressed(KeyDown, KeyDown2) then
2844 begin
2845 kByte := kByte or NET_KEY_DOWN;
2846 gPlayer1.PressKey(KEY_DOWN, 10000);
2847 end;
2848 if isKeyPressed(KeyJump, KeyJump2) then
2849 begin
2850 kByte := kByte or NET_KEY_JUMP;
2851 // gPlayer1.PressKey(KEY_JUMP, 10000); // TODO: Make a prediction option
2852 end;
2853 if isKeyPressed(KeyFire, KeyFire2) then kByte := kByte or NET_KEY_FIRE;
2854 if isKeyPressed(KeyOpen, KeyOpen2) then kByte := kByte or NET_KEY_OPEN;
2855 // do not send weapon switch keys, `MH_SEND_PlayerStats()` will send changed weapon anyway
2856 if isKeyPressed(KeyNextWeapon, KeyNextWeapon2) and gPlayer1.isWeaponSwitchKeyReleased(-1) then gPlayer1.PressKey(KEY_NEXTWEAPON); //kByte := kByte or NET_KEY_NW;
2857 if isKeyPressed(KeyPrevWeapon, KeyPrevWeapon2) and gPlayer1.isWeaponSwitchKeyReleased(-2) then gPlayer1.PressKey(KEY_PREVWEAPON); //kByte := kByte or NET_KEY_PW;
2858 for I := 0 to High(KeyWeapon) do
2859 begin
2860 if isKeyPressed(KeyWeapon[I], KeyWeapon2[I]) then
2861 begin
2862 //writeln('keyweapon #', i, ' is pressed; released=', Integer(gPlayer1.isWeaponSwitchKeyReleased(i)), '; frm=', gPlayer1.NetForceWeapFIdx, '; gTime=', gTime);
2863 gPlayer1.weaponSwitchKeysStateChange(i, true);
2864 if gPlayer1.isWeaponSwitchKeyReleased(i) then
2865 begin
2866 //writeln('keyweapon #', i, ' is pressed; released=', Integer(gPlayer1.isWeaponSwitchKeyReleased(i)), '; frm=', gPlayer1.NetForceWeapFIdx, '; gTime=', gTime);
2867 gPlayer1.QueueWeaponSwitch(i); // all choices are passed there, and god will take the best
2868 //WeaponSelect := WeaponSelect or Word(1 shl I);
2869 end;
2870 end
2871 else
2872 begin
2873 gPlayer1.weaponSwitchKeysStateChange(i, false);
2874 end;
2875 end;
2876 end;
2877 // fix movebutton state
2878 P1MoveButton := P1MoveButton or (strafeDir shl 4);
2879 end
2880 else
2881 kByte := NET_KEY_CHAT;
2883 gPlayer1.weaponSwitchKeysShiftNewStates();
2885 NetOut.Write(Byte(NET_MSG_PLRPOS));
2886 NetOut.Write(gTime);
2887 NetOut.Write(kByte);
2888 NetOut.Write(Byte(gPlayer1.Direction));
2889 NetOut.Write(LongWord(gPlayer1.NetWeapCtr));
2890 NetOut.Write(Byte(gPlayer1.CurrWeap));
2891 {$IFDEF K8_XXX_WEAPON_DEBUG}
2892 if (kByte and NET_KEY_FIRE) <> 0 then writeln('FIRE: CurrWeap=', gPlayer1.CurrWeap);
2893 {$ENDIF}
2894 //e_WriteLog(Format('S:nwp=%d; nwfidx=%d', [Integer(gPlayer1.NetForceWeap), Integer(gPlayer1.NetForceWeapFIdx)]), TMsgType.Warning);
2895 //NetOut.Write(WeaponSelect);
2896 //e_WriteLog(Format('S:ws=%d', [WeaponSelect]), MSG_WARNING);
2897 g_Net_Client_Send(True, NET_CHAN_PLAYERPOS);
2899 //kBytePrev := kByte;
2900 //kDirPrev := gPlayer1.Direction;
2901 end;
2903 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
2904 begin
2905 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
2906 NetOut.Write(Byte(Start));
2907 NetOut.Write(Command);
2908 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2909 end;
2911 procedure MC_SEND_PlayerSettings();
2912 begin
2913 NetOut.Write(Byte(NET_MSG_PLRSET));
2914 NetOut.Write(gPlayer1Settings.Name);
2915 NetOut.Write(gPlayer1Settings.Model);
2916 NetOut.Write(gPlayer1Settings.Color.R);
2917 NetOut.Write(gPlayer1Settings.Color.G);
2918 NetOut.Write(gPlayer1Settings.Color.B);
2919 NetOut.Write(gPlayer1Settings.Team);
2921 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2922 end;
2924 procedure MC_SEND_FullStateRequest();
2925 begin
2926 NetOut.Write(Byte(NET_MSG_REQFST));
2928 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2929 end;
2931 procedure MC_SEND_CheatRequest(Kind: Byte);
2932 begin
2933 NetOut.Write(Byte(NET_MSG_CHEAT));
2934 NetOut.Write(Kind);
2936 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2937 end;
2938 procedure MC_SEND_RCONPassword(Password: string);
2939 begin
2940 NetOut.Write(Byte(NET_MSG_RCON_AUTH));
2941 NetOut.Write(Password);
2943 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2944 end;
2945 procedure MC_SEND_RCONCommand(Cmd: string);
2946 begin
2947 NetOut.Write(Byte(NET_MSG_RCON_CMD));
2948 NetOut.Write(Cmd);
2950 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2951 end;
2953 // i have no idea why all this stuff is in here
2955 function ReadFile(const FileName: TFileName): AByte;
2956 var
2957 FileStream : TStream;
2958 fname: string;
2959 begin
2960 e_WriteLog(Format('NETWORK: looking for file "%s"', [FileName]), TMsgType.Notify);
2961 fname := findDiskWad(FileName);
2962 if length(fname) = 0 then
2963 begin
2964 e_WriteLog(Format('NETWORK: file "%s" not found!', [FileName]), TMsgType.Fatal);
2965 SetLength(Result, 0);
2966 exit;
2967 end;
2968 e_WriteLog(Format('NETWORK: found file "%s"', [fname]), TMsgType.Notify);
2969 Result := nil;
2970 FileStream := openDiskFileRO(fname);
2971 try
2972 if FileStream.Size > 0 then
2973 begin
2974 SetLength(Result, FileStream.Size);
2975 FileStream.Read(Result[0], FileStream.Size);
2976 end;
2977 finally
2978 FileStream.Free;
2979 end;
2980 end;
2982 function CreateMapDataMsg(const FileName: TFileName; ResList: TStringList): TMapDataMsg;
2983 var
2984 i: Integer;
2985 begin
2986 Result.MsgId := NET_MSG_MAP_RESPONSE;
2987 Result.FileData := ReadFile(FileName);
2988 Result.FileSize := Length(Result.FileData);
2990 SetLength(Result.ExternalResources, ResList.Count);
2991 for i:=0 to ResList.Count-1 do
2992 begin
2993 Result.ExternalResources[i].Name := ResList.Strings[i];
2994 Result.ExternalResources[i].md5 := MD5File(GameDir+'/wads/'+ResList.Strings[i]);
2995 end;
2996 end;
2998 procedure ResDataMsgToBytes(var bytes: AByte; const ResData: TResDataMsg);
2999 var
3000 ResultStream: TMemoryStream;
3001 begin
3002 ResultStream := TMemoryStream.Create;
3004 ResultStream.WriteBuffer(ResData.MsgId, SizeOf(ResData.MsgId)); //msgId
3005 ResultStream.WriteBuffer(ResData.FileSize, SizeOf(ResData.FileSize)); //file size
3006 ResultStream.WriteBuffer(ResData.FileData[0], ResData.FileSize); //file data
3008 SetLength(bytes, ResultStream.Size);
3009 ResultStream.Seek(0, soFromBeginning);
3010 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
3012 ResultStream.Free;
3013 end;
3015 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
3016 begin
3017 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
3018 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize));
3019 SetLength(Result.FileData, Result.FileSize);
3020 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize);
3021 end;
3023 procedure MapDataMsgToBytes(var bytes: AByte; const MapDataMsg: TMapDataMsg);
3024 var
3025 ResultStream: TMemoryStream;
3026 resCount: Integer;
3027 begin
3028 resCount := Length(MapDataMsg.ExternalResources);
3030 ResultStream := TMemoryStream.Create;
3032 ResultStream.WriteBuffer(MapDataMsg.MsgId, SizeOf(MapDataMsg.MsgId)); //msgId
3033 ResultStream.WriteBuffer(MapDataMsg.FileSize, SizeOf(MapDataMsg.FileSize)); //file size
3034 ResultStream.WriteBuffer(MapDataMsg.FileData[0], MapDataMsg.FileSize); //file data
3036 ResultStream.WriteBuffer(resCount, SizeOf(resCount)); //res count
3037 ResultStream.WriteBuffer(MapDataMsg.ExternalResources[0], resCount*SizeOf(TExternalResourceInfo)); //res data
3039 SetLength(bytes, ResultStream.Size);
3040 ResultStream.Seek(0, soFromBeginning);
3041 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
3043 ResultStream.Free;
3044 end;
3046 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
3047 var
3048 resCount: Integer;
3049 begin
3050 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
3051 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize)); //file size
3053 SetLength(Result.FileData, Result.FileSize);
3054 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize); //file data
3056 msgStream.ReadBuffer(resCount, SizeOf(resCount)); //res count
3057 SetLength(Result.ExternalResources, resCount);
3059 msgStream.ReadBuffer(Result.ExternalResources[0], resCount * SizeOf(TExternalResourceInfo)); //res data
3060 end;
3062 function IsValidFileName(const S: String): Boolean;
3063 const
3064 Forbidden: set of Char = ['<', '>', '|', '"', ':', '*', '?'];
3065 var
3066 I: Integer;
3067 begin
3068 Result := S <> '';
3069 for I := 1 to Length(S) do
3070 Result := Result and (not(S[I] in Forbidden));
3071 end;
3073 function IsValidFilePath(const S: String): Boolean;
3074 var
3075 I: Integer;
3076 begin
3077 Result := False;
3078 if not IsValidFileName(S) then exit;
3079 if FileExists(S) then exit;
3080 I := LastDelimiter('\/', S);
3081 if (I > 0) then
3082 if (not DirectoryExists(Copy(S, 1, I-1))) then
3083 exit;
3084 Result := True;
3085 end;
3087 procedure MC_SEND_MapRequest();
3088 begin
3089 NetOut.Write(Byte(NET_MSG_MAP_REQUEST));
3090 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3091 end;
3093 procedure MC_SEND_ResRequest(const resName: AnsiString);
3094 begin
3095 NetOut.Write(Byte(NET_MSG_RES_REQUEST));
3096 NetOut.Write(resName);
3097 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3098 end;
3100 procedure MH_RECV_MapRequest(C: pTNetClient; var M: TMsg);
3101 var
3102 payload: AByte;
3103 peer: pENetPeer;
3104 mapDataMsg: TMapDataMsg;
3105 begin
3106 e_WriteLog('NET: Received map request from ' +
3107 DecodeIPV4(C^.Peer.address.host), TMsgType.Notify);
3109 mapDataMsg := CreateMapDataMsg(MapsDir + gGameSettings.WAD, gExternalResources);
3110 peer := NetClients[C^.ID].Peer;
3112 MapDataMsgToBytes(payload, mapDataMsg);
3113 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3115 payload := nil;
3116 mapDataMsg.FileData := nil;
3117 mapDataMsg.ExternalResources := nil;
3118 end;
3120 procedure MH_RECV_ResRequest(C: pTNetClient; var M: TMsg);
3121 var
3122 payload: AByte;
3123 peer: pENetPeer;
3124 FileName: String;
3125 resDataMsg: TResDataMsg;
3126 begin
3127 FileName := ExtractFileName(M.ReadString());
3128 e_WriteLog('NET: Received res request: ' + FileName +
3129 ' from ' + DecodeIPV4(C^.Peer.address.host), TMsgType.Notify);
3131 if not IsValidFilePath(FileName) then
3132 begin
3133 e_WriteLog('Invalid filename: ' + FileName, TMsgType.Warning);
3134 exit;
3135 end;
3137 peer := NetClients[C^.ID].Peer;
3139 if gExternalResources.IndexOf(FileName) > -1 then
3140 begin
3141 resDataMsg.MsgId := NET_MSG_RES_RESPONSE;
3142 resDataMsg.FileData := ReadFile(GameDir+'/wads/'+FileName);
3143 resDataMsg.FileSize := Length(resDataMsg.FileData);
3145 ResDataMsgToBytes(payload, resDataMsg);
3146 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3147 end;
3148 end;
3150 end.