DEADSOFTWARE

oops; gTime fwp sync timeout was too small
[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: 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 //WeaponSelect := M.ReadWord();
460 newweapon := M.ReadByte();
461 if (newweapon <> CurrWeap) then
462 begin
463 {$IFDEF K8_XXX_WEAPON_DEBUG}
464 writeln('HOST PLRPOS: got: currweap=', CurrWeap, '; curfrm=', gTime, '; netweap=', newweapon, '; oldweap=', CurrWeap);
465 {$ENDIF}
466 //NetForceWeap := newweapon;
467 SetWeapon(newweapon);
468 end;
469 //e_WriteLog(Format('R:ws=%d', [WeaponSelect]), MSG_WARNING);
470 if Direction <> TDirection(Dir) then
471 JustTeleported := False;
473 SetDirection(TDirection(Dir));
474 ReleaseKeysNoWeapon();
476 if kByte = NET_KEY_CHAT then
477 begin
478 PressKey(KEY_CHAT, 10000);
479 Exit;
480 end;
482 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
483 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
484 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
485 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
486 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
487 if LongBool(kByte and NET_KEY_FIRE) then PressKey(KEY_FIRE, 10000);
488 if LongBool(kByte and NET_KEY_OPEN) then PressKey(KEY_OPEN, 10000);
489 //if LongBool(kByte and NET_KEY_NW) then PressKey(KEY_NEXTWEAPON, 10000);
490 //if LongBool(kByte and NET_KEY_PW) then PressKey(KEY_PREVWEAPON, 10000);
492 (*
493 for i := 0 to 15 do
494 begin
495 if (WeaponSelect and Word(1 shl i)) <> 0 then
496 begin
497 //e_WriteLog(Format(' R:wn=%d', [i]), MSG_WARNING);
498 QueueWeaponSwitch(i);
499 end;
500 end;
501 *)
502 end;
504 // MH_SEND_PlayerPos(False, PID, C^.ID);
505 end;
507 procedure MH_RECV_CheatRequest(C: pTNetClient; var M: TMsg);
508 var
509 CheatKind: Byte;
510 Pl: TPlayer;
511 begin
512 Pl := g_Player_Get(C^.Player);
513 if Pl = nil then Exit;
515 CheatKind := M.ReadByte();
517 case CheatKind of
518 NET_CHEAT_SUICIDE:
519 Pl.Damage(SUICIDE_DAMAGE, Pl.UID, 0, 0, HIT_SELF);
520 NET_CHEAT_SPECTATE:
521 begin
522 if Pl.FSpectator then
523 Pl.Respawn(False)
524 else
525 Pl.Spectate;
526 end;
527 end;
528 end;
530 procedure MH_RECV_PlayerSettings(C: pTNetClient; var M: TMsg);
531 var
532 TmpName: string;
533 TmpModel: string;
534 TmpColor: TRGB;
535 TmpTeam: Byte;
536 Pl: TPlayer;
537 begin
538 TmpName := M.ReadString();
539 TmpModel := M.ReadString();
540 TmpColor.R := M.ReadByte();
541 TmpColor.G := M.ReadByte();
542 TmpColor.B := M.ReadByte();
543 TmpTeam := M.ReadByte();
545 Pl := g_Player_Get(C^.Player);
546 if Pl = nil then Exit;
548 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
549 Pl.SwitchTeam
550 else
551 Pl.SetColor(TmpColor);
553 if Pl.Name <> TmpName then
554 begin
555 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
556 Pl.Name := TmpName;
557 end;
559 if TmpModel <> Pl.Model.Name then
560 Pl.SetModel(TmpModel);
562 MH_SEND_PlayerSettings(Pl.UID, TmpModel);
563 end;
565 // RCON
567 procedure MH_RECV_RCONPassword(C: pTNetClient; var M: TMsg);
568 var
569 Pwd: string;
570 begin
571 Pwd := M.ReadString();
572 if not NetAllowRCON then Exit;
573 if Pwd = NetRCONPassword then
574 begin
575 C^.RCONAuth := True;
576 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWGOOD, 'N', C^.ID);
577 end
578 else
579 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWBAD, 'N', C^.ID);
580 end;
582 procedure MH_RECV_RCONCommand(C: pTNetClient; var M: TMsg);
583 var
584 Cmd: string;
585 begin
586 Cmd := M.ReadString();
587 if not NetAllowRCON then Exit;
588 if not C^.RCONAuth then
589 begin
590 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_NOAUTH, 'N', C^.ID);
591 Exit;
592 end;
593 g_Console_Process(Cmd);
594 end;
596 // MISC
598 procedure MH_RECV_Vote(C: pTNetClient; var M: TMsg);
599 var
600 Start: Boolean;
601 Name, Command: string;
602 Need: Integer;
603 Pl: TPlayer;
604 begin
605 Start := M.ReadByte() <> 0;
606 Command := M.ReadString();
608 Pl := g_Player_Get(C^.Player);
609 if Pl = nil then Exit;
610 Name := Pl.Name;
612 if Start then
613 begin
614 if not g_Console_CommandBlacklisted(Command) then
615 g_Game_StartVote(Command, Name);
616 end
617 else if gVoteInProgress then
618 begin
619 if (gPlayer1 <> nil) or (gPlayer2 <> nil) then
620 Need := Floor((NetClientCount+1)/2.0) + 1
621 else
622 Need := Floor(NetClientCount/2.0) + 1;
623 if C^.Voted then
624 begin
625 Dec(gVoteCount);
626 C^.Voted := False;
627 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_REVOKED], [Name, gVoteCount, Need]), True);
628 MH_SEND_VoteEvent(NET_VE_REVOKE, Name, 'a', gVoteCount, Need);
629 end
630 else
631 begin
632 Inc(gVoteCount);
633 C^.Voted := True;
634 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Name, gVoteCount, Need]), True);
635 MH_SEND_VoteEvent(NET_VE_VOTE, Name, 'a', gVoteCount, Need);
636 g_Game_CheckVote;
637 end;
638 end;
639 end;
641 // GAME (SEND)
643 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
645 function sendItemRespawn (it: PItem): Boolean;
646 begin
647 result := false; // don't stop
648 MH_SEND_ItemSpawn(True, it.myid, ID);
649 end;
651 function sendMonSpawn (mon: TMonster): Boolean;
652 begin
653 result := false; // don't stop
654 MH_SEND_MonsterSpawn(mon.UID, ID);
655 end;
657 function sendPanelState (pan: TPanel): Boolean;
658 begin
659 result := false; // don't stop
660 MH_SEND_PanelState(pan.guid, ID); // anyway, to sync mplats
661 if (pan.CanChangeTexture) then MH_SEND_PanelTexture(pan.guid, pan.LastAnimLoop, ID);
662 end;
664 var
665 I: Integer;
666 begin
667 if gPlayers <> nil then
668 begin
669 for I := Low(gPlayers) to High(gPlayers) do
670 begin
671 if gPlayers[I] <> nil then
672 begin
673 if CreatePlayers then MH_SEND_PlayerCreate(gPlayers[I].UID, ID);
674 MH_SEND_PlayerPos(True, gPlayers[I].UID, ID);
675 MH_SEND_PlayerStats(gPlayers[I].UID, ID);
677 if (gPlayers[I].Flag <> FLAG_NONE) and (gGameSettings.GameMode = GM_CTF) then
678 begin
679 MH_SEND_FlagEvent(FLAG_STATE_CAPTURED, gPlayers[I].Flag, gPlayers[I].UID, True, ID);
680 end;
681 end;
682 end;
683 end;
685 g_Items_ForEachAlive(sendItemRespawn, true); // backwards
686 g_Mons_ForEach(sendMonSpawn);
687 g_Map_ForEachPanel(sendPanelState);
689 if gTriggers <> nil then
690 begin
691 for I := Low(gTriggers) to High(gTriggers) do
692 begin
693 if gTriggers[I].TriggerType = TRIGGER_SOUND then
694 begin
695 MH_SEND_TriggerSound(gTriggers[I], ID);
696 end;
697 end;
698 end;
700 if Shots <> nil then
701 begin
702 for I := Low(Shots) to High(Shots) do
703 begin
704 if Shots[i].ShotType in [6, 7, 8] then
705 begin
706 MH_SEND_CreateShot(i, ID);
707 end;
708 end;
709 end;
711 MH_SEND_TriggerMusic(ID);
713 MH_SEND_GameStats(ID);
714 MH_SEND_CoopStats(ID);
716 if gGameSettings.GameMode = GM_CTF then
717 begin
718 if gFlags[FLAG_RED].State <> FLAG_STATE_CAPTURED then MH_SEND_FlagEvent(gFlags[FLAG_RED].State, FLAG_RED, 0, True, ID);
719 if gFlags[FLAG_BLUE].State <> FLAG_STATE_CAPTURED then MH_SEND_FlagEvent(gFlags[FLAG_BLUE].State, FLAG_BLUE, 0, True, ID);
720 end;
722 if CreatePlayers and (ID >= 0) then NetClients[ID].State := NET_STATE_GAME;
724 if gLMSRespawn > LMS_RESPAWN_NONE then
725 begin
726 MH_SEND_GameEvent(NET_EV_LMS_WARMUP, (gLMSRespawnTime - gTime) div 1000, 'N', ID);
727 end;
728 end;
730 procedure MH_SEND_Info(ID: Byte);
731 var
732 Map: string;
733 begin
734 Map := g_ExtractFileName(gMapInfo.Map);
736 NetOut.Clear();
738 NetOut.Write(Byte(NET_MSG_INFO));
739 NetOut.Write(ID);
740 NetOut.Write(NetClients[ID].Player);
741 NetOut.Write(gGameSettings.WAD);
742 NetOut.Write(Map);
743 NetOut.Write(gWADHash);
744 NetOut.Write(gGameSettings.GameMode);
745 NetOut.Write(gGameSettings.GoalLimit);
746 NetOut.Write(gGameSettings.TimeLimit);
747 NetOut.Write(gGameSettings.MaxLives);
748 NetOut.Write(gGameSettings.Options);
749 NetOut.Write(gTime);
751 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
752 end;
754 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
755 var
756 Name: string;
757 i: Integer;
758 Team: Byte;
759 begin
760 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
761 Mode := NET_CHAT_PLAYER;
763 Team := 0;
764 if (Mode = NET_CHAT_TEAM) then
765 begin
766 for i := Low(gPlayers) to High(gPlayers) do
767 if (gPlayers[i] <> nil) and (gPlayers[i].FClientID >= 0) and
768 (gPlayers[i].Team = ID) then
769 begin
770 NetOut.Write(Byte(NET_MSG_CHAT));
771 NetOut.Write(Txt);
772 NetOut.Write(Mode);
773 g_Net_Host_Send(gPlayers[i].FClientID, True, NET_CHAN_CHAT);
774 end;
775 Team := ID;
776 ID := NET_EVERYONE;
777 end
778 else
779 begin
780 NetOut.Write(Byte(NET_MSG_CHAT));
781 NetOut.Write(Txt);
782 NetOut.Write(Mode);
783 g_Net_Host_Send(ID, True, NET_CHAN_CHAT);
784 end;
786 if Mode = NET_CHAT_SYSTEM then
787 Exit;
789 if ID = NET_EVERYONE then
790 begin
791 if Mode = NET_CHAT_PLAYER then
792 begin
793 g_Console_Add(Txt, True);
794 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
795 g_Game_ChatSound(b_Text_Unformat(Txt));
796 end
797 else
798 if Mode = NET_CHAT_TEAM then
799 if gPlayer1 <> nil then
800 begin
801 if (gPlayer1.Team = TEAM_RED) and (Team = TEAM_RED) then
802 begin
803 g_Console_Add(#18'[Team] '#2 + Txt, True);
804 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
805 g_Game_ChatSound(b_Text_Unformat(Txt));
806 end
807 else if (gPlayer1.Team = TEAM_BLUE) and (Team = TEAM_BLUE) then
808 begin
809 g_Console_Add(#20'[Team] '#2 + Txt, True);
810 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
811 g_Game_ChatSound(b_Text_Unformat(Txt));
812 end;
813 end;
814 end
815 else
816 begin
817 Name := g_Net_ClientName_ByID(ID);
818 g_Console_Add('-> ' + Name + ': ' + Txt, True);
819 e_WriteLog('[Tell ' + Name + '] ' + b_Text_Unformat(Txt), TMsgType.Notify);
820 g_Game_ChatSound(b_Text_Unformat(Txt), False);
821 end;
822 end;
824 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
825 begin
826 NetOut.Write(Byte(NET_MSG_GFX));
827 NetOut.Write(Kind);
828 NetOut.Write(X);
829 NetOut.Write(Y);
830 NetOut.Write(Ang);
832 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
833 end;
835 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
836 begin
837 NetOut.Write(Byte(NET_MSG_SND));
838 NetOut.Write(Name);
839 if Pos then
840 begin
841 NetOut.Write(Byte(1));
842 NetOut.Write(X);
843 NetOut.Write(Y);
844 end
845 else
846 NetOut.Write(Byte(0));
848 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
849 end;
851 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
852 begin
853 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
855 NetOut.Write(Byte(NET_MSG_SHADD));
856 NetOut.Write(Proj);
857 NetOut.Write(Shots[Proj].ShotType);
858 NetOut.Write(Shots[Proj].Target);
859 NetOut.Write(Shots[Proj].SpawnerUID);
860 NetOut.Write(Shots[Proj].Timeout);
861 NetOut.Write(Shots[Proj].Obj.X);
862 NetOut.Write(Shots[Proj].Obj.Y);
863 NetOut.Write(Shots[Proj].Obj.Vel.X);
864 NetOut.Write(Shots[Proj].Obj.Vel.Y);
866 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
867 end;
869 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
870 begin
871 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
873 NetOut.Write(Byte(NET_MSG_SHPOS));
874 NetOut.Write(Proj);
875 NetOut.Write(Shots[Proj].Obj.X);
876 NetOut.Write(Shots[Proj].Obj.Y);
877 NetOut.Write(Shots[Proj].Obj.Vel.X);
878 NetOut.Write(Shots[Proj].Obj.Vel.Y);
880 g_Net_Host_Send(ID, False, NET_CHAN_SHOTS);
881 end;
883 procedure MH_Send_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
884 begin
885 NetOut.Write(Byte(NET_MSG_SHDEL));
886 NetOut.Write(Proj);
887 NetOut.Write(Byte(Loud));
888 NetOut.Write(X);
889 NetOut.Write(Y);
891 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
892 end;
894 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
895 begin
896 NetOut.Write(Byte(NET_MSG_SCORE));
897 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
898 begin
899 NetOut.Write(gTeamStat[TEAM_RED].Goals);
900 NetOut.Write(gTeamStat[TEAM_BLUE].Goals);
901 end
902 else
903 if gGameSettings.GameMode = GM_COOP then
904 begin
905 NetOut.Write(gCoopMonstersKilled);
906 NetOut.Write(gCoopSecretsFound);
907 end;
909 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
910 end;
912 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
913 begin
914 NetOut.Write(Byte(NET_MSG_COOP));
915 NetOut.Write(gTotalMonsters);
916 NetOut.Write(gSecretsCount);
917 NetOut.Write(gCoopTotalMonstersKilled);
918 NetOut.Write(gCoopTotalSecretsFound);
919 NetOut.Write(gCoopTotalMonsters);
920 NetOut.Write(gCoopTotalSecrets);
921 end;
923 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
924 begin
925 NetOut.Write(Byte(NET_MSG_GEVENT));
926 NetOut.Write(EvType);
927 NetOut.Write(EvNum);
928 NetOut.Write(EvStr);
929 NetOut.Write(Byte(gLastMap));
930 NetOut.Write(gTime);
931 if (EvType = NET_EV_MAPSTART) and isWadPath(EvStr) then
932 begin
933 NetOut.Write(Byte(1));
934 NetOut.Write(gWADHash);
935 end else
936 NetOut.Write(Byte(0));
938 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
939 end;
941 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
942 begin
943 NetOut.Write(Byte(NET_MSG_FLAG));
944 NetOut.Write(EvType);
945 NetOut.Write(Flag);
946 NetOut.Write(Byte(Quiet));
947 NetOut.Write(PID);
948 NetOut.Write(gFlags[Flag].State);
949 NetOut.Write(gFlags[Flag].CaptureTime);
950 NetOut.Write(gFlags[Flag].Obj.X);
951 NetOut.Write(gFlags[Flag].Obj.Y);
952 NetOut.Write(gFlags[Flag].Obj.Vel.X);
953 NetOut.Write(gFlags[Flag].Obj.Vel.Y);
955 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
956 end;
958 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
959 begin
960 NetOut.Write(Byte(NET_MSG_GSET));
961 NetOut.Write(gGameSettings.GameMode);
962 NetOut.Write(gGameSettings.GoalLimit);
963 NetOut.Write(gGameSettings.TimeLimit);
964 NetOut.Write(gGameSettings.MaxLives);
965 NetOut.Write(gGameSettings.Options);
967 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
968 end;
970 // PLAYER (SEND)
972 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
973 var
974 P: TPlayer;
975 begin
976 P := g_Player_Get(PID);
977 if P = nil then Exit;
979 NetOut.Write(Byte(NET_MSG_PLR));
980 NetOut.Write(PID);
981 NetOut.Write(P.Name);
983 NetOut.Write(P.FActualModelName);
984 NetOut.Write(P.FColor.R);
985 NetOut.Write(P.FColor.G);
986 NetOut.Write(P.FColor.B);
987 NetOut.Write(P.Team);
989 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT)
990 end;
992 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
993 var
994 kByte: Word;
995 Pl: TPlayer;
996 begin
997 Pl := g_Player_Get(PID);
998 if Pl = nil then Exit;
999 if Pl.FDummy then Exit;
1001 NetOut.Write(Byte(NET_MSG_PLRPOS));
1002 NetOut.Write(gTime);
1003 NetOut.Write(PID);
1005 kByte := 0;
1007 with Pl do
1008 begin
1009 NetOut.Write(FPing);
1010 NetOut.Write(FLoss);
1011 if IsKeyPressed(KEY_CHAT) then
1012 kByte := NET_KEY_CHAT
1013 else
1014 begin
1015 if IsKeyPressed(KEY_LEFT) then kByte := kByte or NET_KEY_LEFT;
1016 if IsKeyPressed(KEY_RIGHT) then kByte := kByte or NET_KEY_RIGHT;
1017 if IsKeyPressed(KEY_UP) then kByte := kByte or NET_KEY_UP;
1018 if IsKeyPressed(KEY_DOWN) then kByte := kByte or NET_KEY_DOWN;
1019 if IsKeyPressed(KEY_JUMP) then kByte := kByte or NET_KEY_JUMP;
1020 if JustTeleported then kByte := kByte or NET_KEY_FORCEDIR;
1021 end;
1023 NetOut.Write(kByte);
1024 if Direction = TDirection.D_LEFT then NetOut.Write(Byte(0)) else NetOut.Write(Byte(1));
1025 NetOut.Write(GameX);
1026 NetOut.Write(GameY);
1027 NetOut.Write(GameVelX);
1028 NetOut.Write(GameVelY);
1029 NetOut.Write(GameAccelX);
1030 NetOut.Write(GameAccelY);
1031 end;
1033 g_Net_Host_Send(ID, Reliable, NET_CHAN_PLAYERPOS);
1034 end;
1036 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
1037 var
1038 P: TPlayer;
1039 I: Integer;
1040 ww: Word;
1041 begin
1042 P := g_Player_Get(PID);
1043 if P = nil then Exit;
1045 NetOut.Write(Byte(NET_MSG_PLRSTA));
1046 NetOut.Write(PID);
1048 with P do
1049 begin
1050 NetOut.Write(Byte(alive));
1051 NetOut.Write(Byte(GodMode));
1052 NetOut.Write(Health);
1053 NetOut.Write(Armor);
1054 NetOut.Write(Air);
1055 NetOut.Write(JetFuel);
1056 NetOut.Write(Lives);
1057 NetOut.Write(Team);
1059 // collect all weapons in one word
1060 ww := 0;
1061 for I := WP_FIRST to WP_LAST do if (FWeapon[I]) then ww := ww or (1 shl (i-WP_FIRST));
1062 NetOut.Write(Word(ww));
1064 for I := A_BULLETS to A_HIGH do
1065 NetOut.Write(FAmmo[I]);
1067 for I := A_BULLETS to A_HIGH do
1068 NetOut.Write(FMaxAmmo[I]);
1070 for I := MR_SUIT to MR_MAX do
1071 NetOut.Write(LongWord(FMegaRulez[I]));
1073 // collect all special flags in one byte
1074 ww := 0;
1075 if (R_ITEM_BACKPACK in FRulez) then ww := ww or $01;
1076 if (R_KEY_RED in FRulez) then ww := ww or $02;
1077 if (R_KEY_GREEN in FRulez) then ww := ww or $04;
1078 if (R_KEY_BLUE in FRulez) then ww := ww or $08;
1079 if (R_BERSERK in FRulez) then ww := ww or $10;
1080 NetOut.Write(Byte(ww));
1082 NetOut.Write(Frags);
1083 NetOut.Write(Death);
1085 //NetOut.Write(CurrWeap);
1086 NetOut.Write(Byte(CurrWeap));
1088 // other flags
1089 ww := 0;
1090 if (FSpectator) then ww := ww or $01;
1091 if (FGhost) then ww := ww or $02;
1092 if (FPhysics) then ww := ww or $04;
1093 if (FNoRespawn) then ww := ww or $08;
1094 if (FJetpack) then ww := ww or $10;
1095 NetOut.Write(Byte(ww));
1097 NetOut.Write(FFireTime);
1098 end;
1100 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1101 end;
1103 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
1104 begin
1105 NetOut.Write(Byte(NET_MSG_PLRDMG));
1106 NetOut.Write(PID);
1107 NetOut.Write(Kind);
1108 NetOut.Write(Attacker);
1109 NetOut.Write(Value);
1110 NetOut.Write(VX);
1111 NetOut.Write(VY);
1113 g_Net_Host_Send(ID, False, NET_CHAN_PLAYER);
1114 end;
1116 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
1117 begin
1118 NetOut.Write(Byte(NET_MSG_PLRDIE));
1119 NetOut.Write(PID);
1120 NetOut.Write(KillType);
1121 NetOut.Write(DeathType);
1122 NetOut.Write(Attacker);
1124 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1125 end;
1127 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
1128 begin
1129 NetOut.Write(Byte(NET_MSG_PLRFIRE));
1130 NetOut.Write(PID);
1131 NetOut.Write(Weapon);
1132 NetOut.Write(X);
1133 NetOut.Write(Y);
1134 NetOut.Write(AX);
1135 NetOut.Write(AY);
1136 NetOut.Write(ShotID);
1138 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
1139 end;
1141 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
1142 begin
1143 NetOut.Write(Byte(NET_MSG_PLRDEL));
1144 NetOut.Write(PID);
1146 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1147 end;
1149 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
1150 var
1151 Pl: TPlayer;
1152 begin
1153 Pl := g_Player_Get(PID);
1154 if Pl = nil then Exit;
1156 NetOut.Write(Byte(NET_MSG_PLRSET));
1157 NetOut.Write(PID);
1158 NetOut.Write(Pl.Name);
1159 if Mdl = '' then
1160 NetOut.Write(Pl.Model.Name)
1161 else
1162 NetOut.Write(Mdl);
1163 NetOut.Write(Pl.FColor.R);
1164 NetOut.Write(Pl.FColor.G);
1165 NetOut.Write(Pl.FColor.B);
1166 NetOut.Write(Pl.Team);
1168 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1169 end;
1171 // ITEM (SEND)
1173 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1174 var
1175 it: PItem;
1176 tt: Byte;
1177 begin
1178 it := g_Items_ByIdx(IID);
1180 NetOut.Write(Byte(NET_MSG_ISPAWN));
1181 NetOut.Write(IID);
1182 NetOut.Write(Byte(Quiet));
1183 tt := it.ItemType;
1184 if it.dropped then tt := tt or $80;
1185 NetOut.Write(tt);
1186 NetOut.Write(Byte(it.Fall));
1187 NetOut.Write(Byte(it.Respawnable));
1188 NetOut.Write(it.Obj.X);
1189 NetOut.Write(it.Obj.Y);
1190 NetOut.Write(it.Obj.Vel.X);
1191 NetOut.Write(it.Obj.Vel.Y);
1193 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1194 end;
1196 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1197 begin
1198 NetOut.Write(Byte(NET_MSG_IDEL));
1199 NetOut.Write(IID);
1200 NetOut.Write(Byte(Quiet));
1202 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1203 end;
1205 // PANEL
1207 procedure MH_SEND_PanelTexture(PGUID: Integer; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
1208 var
1209 TP: TPanel;
1210 begin
1211 TP := g_Map_PanelByGUID(PGUID);
1212 if (TP = nil) then exit;
1214 with TP do
1215 begin
1216 NetOut.Write(Byte(NET_MSG_PTEX));
1217 NetOut.Write(LongWord(PGUID));
1218 NetOut.Write(FCurTexture);
1219 NetOut.Write(FCurFrame);
1220 NetOut.Write(FCurFrameCount);
1221 NetOut.Write(AnimLoop);
1222 end;
1224 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1225 end;
1227 procedure MH_SEND_PanelState(PGUID: Integer; ID: Integer = NET_EVERYONE);
1228 var
1229 TP: TPanel;
1230 mpflags: Byte = 0;
1231 begin
1232 TP := g_Map_PanelByGUID(PGUID);
1233 if (TP = nil) then exit;
1235 NetOut.Write(Byte(NET_MSG_PSTATE));
1236 NetOut.Write(LongWord(PGUID));
1237 NetOut.Write(Byte(TP.Enabled));
1238 NetOut.Write(TP.LiftType);
1239 NetOut.Write(TP.X);
1240 NetOut.Write(TP.Y);
1241 NetOut.Write(Word(TP.Width));
1242 NetOut.Write(Word(TP.Height));
1243 // mplats
1244 NetOut.Write(LongInt(TP.movingSpeedX));
1245 NetOut.Write(LongInt(TP.movingSpeedY));
1246 NetOut.Write(LongInt(TP.movingStartX));
1247 NetOut.Write(LongInt(TP.movingStartY));
1248 NetOut.Write(LongInt(TP.movingEndX));
1249 NetOut.Write(LongInt(TP.movingEndY));
1250 NetOut.Write(LongInt(TP.sizeSpeedX));
1251 NetOut.Write(LongInt(TP.sizeSpeedY));
1252 NetOut.Write(LongInt(TP.sizeEndX));
1253 NetOut.Write(LongInt(TP.sizeEndY));
1254 if TP.movingActive then mpflags := mpflags or 1;
1255 if TP.moveOnce then mpflags := mpflags or 2;
1256 NetOut.Write(Byte(mpflags));
1258 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1259 end;
1261 // TRIGGER
1263 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
1264 begin
1265 if gTriggers = nil then Exit;
1266 if T.Sound = nil then Exit;
1268 NetOut.Write(Byte(NET_MSG_TSOUND));
1269 NetOut.Write(T.ClientID);
1270 NetOut.Write(Byte(T.Sound.IsPlaying));
1271 NetOut.Write(LongWord(T.Sound.GetPosition));
1272 NetOut.Write(T.SoundPlayCount);
1274 g_Net_Host_Send(ID, True);
1275 end;
1277 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
1278 begin
1279 NetOut.Write(Byte(NET_MSG_TMUSIC));
1280 NetOut.Write(gMusic.Name);
1281 NetOut.Write(Byte(gMusic.IsPlaying));
1282 NetOut.Write(LongWord(gMusic.GetPosition));
1283 NetOut.Write(Byte(gMusic.SpecPause or gMusic.IsPaused));
1285 g_Net_Host_Send(ID, True);
1286 end;
1288 // MONSTER
1290 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
1291 var
1292 M: TMonster;
1293 begin
1294 M := g_Monsters_ByUID(UID);
1295 if M = nil then
1296 Exit;
1298 with M do
1299 begin
1300 NetOut.Write(Byte(NET_MSG_MSPAWN));
1301 NetOut.Write(UID);
1302 NetOut.Write(MonsterType);
1303 NetOut.Write(MonsterState);
1304 NetOut.Write(MonsterAnim);
1305 NetOut.Write(MonsterTargetUID);
1306 NetOut.Write(MonsterTargetTime);
1307 NetOut.Write(MonsterBehaviour);
1308 NetOut.Write(MonsterSleep);
1309 NetOut.Write(MonsterHealth);
1310 NetOut.Write(MonsterAmmo);
1311 NetOut.Write(GameX);
1312 NetOut.Write(GameY);
1313 NetOut.Write(GameVelX);
1314 NetOut.Write(GameVelY);
1315 NetOut.Write(Byte(GameDirection));
1316 end;
1318 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1319 end;
1321 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
1322 var
1323 M: TMonster;
1324 begin
1325 M := g_Monsters_ByUID(UID);
1326 if M = nil then Exit;
1328 NetOut.Write(Byte(NET_MSG_MPOS));
1329 NetOut.Write(UID);
1331 with M do
1332 begin
1333 NetOut.Write(GameX);
1334 NetOut.Write(GameY);
1335 NetOut.Write(GameVelX);
1336 NetOut.Write(GameVelY);
1337 NetOut.Write(Byte(GameDirection));
1338 end;
1340 g_Net_Host_Send(ID, False, NET_CHAN_MONSTERPOS);
1341 end;
1343 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
1344 var
1345 M: TMonster;
1346 begin
1347 M := g_Monsters_ByUID(UID);
1348 if M = nil then Exit;
1350 NetOut.Write(Byte(NET_MSG_MSTATE));
1351 NetOut.Write(UID);
1353 with M do
1354 begin
1355 NetOut.Write(MonsterState);
1356 NetOut.Write(ForcedAnim);
1357 NetOut.Write(MonsterTargetUID);
1358 NetOut.Write(MonsterTargetTime);
1359 NetOut.Write(MonsterSleep);
1360 NetOut.Write(MonsterHealth);
1361 NetOut.Write(MonsterAmmo);
1362 NetOut.Write(MonsterPain);
1363 NetOut.Write(Byte(AnimIsReverse));
1364 NetOut.Write(FFireTime);
1365 end;
1367 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1368 end;
1370 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
1371 begin
1372 NetOut.Write(Byte(NET_MSG_MSHOT));
1373 NetOut.Write(UID);
1374 NetOut.Write(X);
1375 NetOut.Write(Y);
1376 NetOut.Write(VX);
1377 NetOut.Write(VY);
1379 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1380 end;
1382 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
1383 var
1384 M: TMonster;
1385 begin
1386 M := g_Monsters_ByUID(UID);
1387 if M = nil then Exit;
1389 NetOut.Write(Byte(NET_MSG_MDEL));
1390 NetOut.Write(UID);
1392 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1393 end;
1395 // MISC
1397 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
1398 begin
1399 NetOut.Write(Byte(NET_MSG_TIME_SYNC));
1400 NetOut.Write(Time);
1402 g_Net_Host_Send(ID, False, NET_CHAN_SERVICE);
1403 end;
1405 procedure MH_SEND_VoteEvent(EvType: Byte;
1406 StrArg1: string = 'a'; StrArg2: string = 'b';
1407 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
1408 ID: Integer = NET_EVERYONE);
1409 begin
1410 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
1411 NetOut.Write(EvType);
1412 NetOut.Write(IntArg1);
1413 NetOut.Write(IntArg2);
1414 NetOut.Write(StrArg1);
1415 NetOut.Write(StrArg2);
1417 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1418 end;
1420 // CLIENT MESSAGES //
1422 // GAME
1424 procedure MC_RECV_Chat(var M: TMsg);
1425 var
1426 Txt: string;
1427 Mode: Byte;
1428 begin
1429 Txt := M.ReadString();
1430 Mode := M.ReadByte();
1432 if Mode <> NET_CHAT_SYSTEM then
1433 begin
1434 if Mode = NET_CHAT_PLAYER then
1435 begin
1436 g_Console_Add(Txt, True);
1437 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1438 g_Game_ChatSound(b_Text_Unformat(Txt));
1439 end else
1440 if (Mode = NET_CHAT_TEAM) and (gPlayer1 <> nil) then
1441 begin
1442 if gPlayer1.Team = TEAM_RED then
1443 g_Console_Add(b_Text_Format('\r[Team] ') + Txt, True);
1444 if gPlayer1.Team = TEAM_BLUE then
1445 g_Console_Add(b_Text_Format('\b[Team] ') + Txt, True);
1446 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1447 g_Game_ChatSound(b_Text_Unformat(Txt));
1448 end;
1449 end else
1450 g_Console_Add(Txt, True);
1451 end;
1453 procedure MC_RECV_Effect(var M: TMsg);
1454 var
1455 Kind: Byte;
1456 X, Y: Integer;
1457 Ang: SmallInt;
1458 Anim: TAnimation;
1459 ID: LongWord;
1460 begin
1461 if not gGameOn then Exit;
1462 Kind := M.ReadByte();
1463 X := M.ReadLongInt();
1464 Y := M.ReadLongInt();
1465 Ang := M.ReadSmallInt();
1467 case Kind of
1468 NET_GFX_SPARK:
1469 g_GFX_Spark(X, Y, 2 + Random(2), Ang, 0, 0);
1471 NET_GFX_TELE:
1472 begin
1473 if g_Frames_Get(ID, 'FRAMES_TELEPORT') then
1474 begin
1475 Anim := TAnimation.Create(ID, False, 3);
1476 g_GFX_OnceAnim(X, Y, Anim);
1477 Anim.Free();
1478 end;
1479 if Ang = 1 then
1480 g_Sound_PlayExAt('SOUND_GAME_TELEPORT', X, Y);
1481 end;
1483 NET_GFX_EXPLODE:
1484 begin
1485 if g_Frames_Get(ID, 'FRAMES_EXPLODE_ROCKET') then
1486 begin
1487 Anim := TAnimation.Create(ID, False, 6);
1488 Anim.Blending := False;
1489 g_GFX_OnceAnim(X-64, Y-64, Anim);
1490 Anim.Free();
1491 end;
1492 if Ang = 1 then
1493 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEROCKET', X, Y);
1494 end;
1496 NET_GFX_BFGEXPL:
1497 begin
1498 if g_Frames_Get(ID, 'FRAMES_EXPLODE_BFG') then
1499 begin
1500 Anim := TAnimation.Create(ID, False, 6);
1501 Anim.Blending := False;
1502 g_GFX_OnceAnim(X-64, Y-64, Anim);
1503 Anim.Free();
1504 end;
1505 if Ang = 1 then
1506 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEBFG', X, Y);
1507 end;
1509 NET_GFX_BFGHIT:
1510 begin
1511 if g_Frames_Get(ID, 'FRAMES_BFGHIT') then
1512 begin
1513 Anim := TAnimation.Create(ID, False, 4);
1514 g_GFX_OnceAnim(X-32, Y-32, Anim);
1515 Anim.Free();
1516 end;
1517 end;
1519 NET_GFX_FIRE:
1520 begin
1521 if g_Frames_Get(ID, 'FRAMES_FIRE') then
1522 begin
1523 Anim := TAnimation.Create(ID, False, 4);
1524 g_GFX_OnceAnim(X, Y, Anim);
1525 Anim.Free();
1526 end;
1527 if Ang = 1 then
1528 g_Sound_PlayExAt('SOUND_FIRE', X, Y);
1529 end;
1531 NET_GFX_RESPAWN:
1532 begin
1533 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
1534 begin
1535 Anim := TAnimation.Create(ID, False, 4);
1536 g_GFX_OnceAnim(X, Y, Anim);
1537 Anim.Free();
1538 end;
1539 if Ang = 1 then
1540 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
1541 end;
1543 NET_GFX_SHELL1:
1544 g_Player_CreateShell(X, Y, 0, -2, SHELL_BULLET);
1546 NET_GFX_SHELL2:
1547 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1549 NET_GFX_SHELL3:
1550 begin
1551 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1552 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1553 end;
1554 end;
1555 end;
1557 procedure MC_RECV_Sound(var M: TMsg);
1558 var
1559 Name: string;
1560 X, Y: Integer;
1561 Pos: Boolean;
1562 begin
1563 Name := M.ReadString();
1564 Pos := M.ReadByte() <> 0;
1565 if Pos then
1566 begin
1567 X := M.ReadLongInt();
1568 Y := M.ReadLongInt();
1569 g_Sound_PlayExAt(Name, X, Y);
1570 end
1571 else
1572 g_Sound_PlayEx(Name);
1573 end;
1575 procedure MC_RECV_CreateShot(var M: TMsg);
1576 var
1577 I, X, Y, XV, YV: Integer;
1578 Timeout: LongWord;
1579 Target, Spawner: Word;
1580 ShType: Byte;
1581 begin
1582 I := M.ReadLongInt();
1583 ShType := M.ReadByte();
1584 Target := M.ReadWord();
1585 Spawner := M.ReadWord();
1586 Timeout := M.ReadLongWord();
1587 X := M.ReadLongInt();
1588 Y := M.ReadLongInt();
1589 XV := M.ReadLongInt();
1590 YV := M.ReadLongInt();
1592 I := g_Weapon_CreateShot(I, ShType, Spawner, Target, X, Y, XV, YV);
1593 if (Shots <> nil) and (I <= High(Shots)) then
1594 begin
1595 Shots[I].Timeout := Timeout;
1596 //Shots[I].Target := Target; // TODO: find a use for Target later
1597 end;
1598 end;
1600 procedure MC_RECV_UpdateShot(var M: TMsg);
1601 var
1602 I, TX, TY, TXV, TYV: Integer;
1603 begin
1604 I := M.ReadLongInt();
1605 TX := M.ReadLongInt();
1606 TY := M.ReadLongInt();
1607 TXV := M.ReadLongInt();
1608 TYV := M.ReadLongInt();
1610 if (Shots <> nil) and (I <= High(Shots)) then
1611 with (Shots[i]) do
1612 begin
1613 Obj.X := TX;
1614 Obj.Y := TY;
1615 Obj.Vel.X := TXV;
1616 Obj.Vel.Y := TYV;
1617 end;
1618 end;
1620 procedure MC_RECV_DeleteShot(var M: TMsg);
1621 var
1622 I, X, Y: Integer;
1623 L: Boolean;
1624 begin
1625 if not gGameOn then Exit;
1626 I := M.ReadLongInt();
1627 L := (M.ReadByte() <> 0);
1628 X := M.ReadLongInt();
1629 Y := M.ReadLongInt();
1631 g_Weapon_DestroyShot(I, X, Y, L);
1632 end;
1634 procedure MC_RECV_GameStats(var M: TMsg);
1635 begin
1636 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
1637 begin
1638 gTeamStat[TEAM_RED].Goals := M.ReadSmallInt();
1639 gTeamStat[TEAM_BLUE].Goals := M.ReadSmallInt();
1640 end
1641 else
1642 if gGameSettings.GameMode = GM_COOP then
1643 begin
1644 gCoopMonstersKilled := M.ReadWord();
1645 gCoopSecretsFound := M.ReadWord();
1646 end;
1647 end;
1649 procedure MC_RECV_CoopStats(var M: TMsg);
1650 begin
1651 gTotalMonsters := M.ReadLongInt();
1652 gSecretsCount := M.ReadLongInt();
1653 gCoopTotalMonstersKilled := M.ReadWord();
1654 gCoopTotalSecretsFound := M.ReadWord();
1655 gCoopTotalMonsters := M.ReadWord();
1656 gCoopTotalSecrets := M.ReadWord();
1657 end;
1659 procedure MC_RECV_GameEvent(var M: TMsg);
1660 var
1661 EvType: Byte;
1662 EvNum: Integer;
1663 EvStr: string;
1664 EvTime: LongWord;
1665 BHash: Boolean;
1666 EvHash: TMD5Digest;
1667 pl: TPlayer;
1668 i1, i2: TStrings_Locale;
1669 pln: String;
1670 cnt: Byte;
1671 begin
1672 FillChar(EvHash, Sizeof(EvHash), 0);
1673 EvType := M.ReadByte();
1674 EvNum := M.ReadLongInt();
1675 EvStr := M.ReadString();
1676 gLastMap := M.ReadByte() <> 0;
1677 if gLastMap and (gGameSettings.GameMode = GM_COOP) then gStatsOff := True;
1678 gStatsPressed := True;
1679 EvTime := M.ReadLongWord();
1680 BHash := M.ReadByte() <> 0;
1681 if BHash then
1682 EvHash := M.ReadMD5();
1684 gTime := EvTime;
1686 case EvType of
1687 NET_EV_MAPSTART:
1688 begin
1689 gGameOn := False;
1690 g_Game_ClearLoading();
1691 g_Game_StopAllSounds(True);
1693 gSwitchGameMode := Byte(EvNum);
1694 gGameSettings.GameMode := gSwitchGameMode;
1696 gWADHash := EvHash;
1697 if not g_Game_StartMap(EvStr, True) then
1698 begin
1699 if not isWadPath(EvStr) then
1700 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [gGameSettings.WAD + ':\' + EvStr]))
1701 else
1702 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [EvStr]));
1703 Exit;
1704 end;
1706 MC_SEND_FullStateRequest;
1707 end;
1709 NET_EV_MAPEND:
1710 begin
1711 gMissionFailed := EvNum <> 0;
1712 gExit := EXIT_ENDLEVELCUSTOM;
1713 end;
1715 NET_EV_RCON:
1716 begin
1717 case EvNum of
1718 NET_RCON_NOAUTH:
1719 g_Console_Add(_lc[I_NET_RCON_NOAUTH], True);
1720 NET_RCON_PWGOOD:
1721 g_Console_Add(_lc[I_NET_RCON_PWD_VALID], True);
1722 NET_RCON_PWBAD:
1723 g_Console_Add(_lc[I_NET_RCON_PWD_INVALID], True);
1724 end;
1725 end;
1727 NET_EV_CHANGE_TEAM:
1728 begin
1729 if EvNum = TEAM_RED then
1730 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [EvStr]), True);
1731 if EvNum = TEAM_BLUE then
1732 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [EvStr]), True);
1733 end;
1735 NET_EV_PLAYER_KICK:
1736 g_Console_Add(Format(_lc[I_PLAYER_KICK], [EvStr]), True);
1738 NET_EV_PLAYER_BAN:
1739 g_Console_Add(Format(_lc[I_PLAYER_BAN], [EvStr]), True);
1741 NET_EV_LMS_WARMUP:
1742 g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [EvNum]), True);
1744 NET_EV_LMS_SURVIVOR:
1745 g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True);
1747 NET_EV_BIGTEXT:
1748 g_Game_Message(AnsiUpperCase(EvStr), Word(EvNum));
1750 NET_EV_SCORE:
1751 begin
1752 pl := g_Player_Get(EvNum and $FFFF);
1753 if pl = nil then
1754 pln := '?'
1755 else
1756 pln := pl.Name;
1757 cnt := (EvNum shr 16) and $FF;
1758 if Pos('w', EvStr) = 0 then
1759 begin
1760 // Default score
1761 if Pos('t', EvStr) = 0 then
1762 begin
1763 // Player +/- score
1764 if Pos('-', EvStr) = 0 then
1765 begin
1766 if Pos('e', EvStr) = 0 then
1767 i1 := I_PLAYER_SCORE_ADD_OWN
1768 else
1769 i1 := I_PLAYER_SCORE_ADD_ENEMY;
1770 end else
1771 begin
1772 if Pos('e', EvStr) = 0 then
1773 i1 := I_PLAYER_SCORE_SUB_OWN
1774 else
1775 i1 := I_PLAYER_SCORE_SUB_ENEMY;
1776 end;
1777 // Which team
1778 if Pos('r', EvStr) > 0 then
1779 i2 := I_PLAYER_SCORE_TO_RED
1780 else
1781 i2 := I_PLAYER_SCORE_TO_BLUE;
1782 g_Console_Add(Format(_lc[i1], [pln, cnt, _lc[i2]]), True);
1783 end else
1784 begin
1785 // Team +/- score
1786 if Pos('-', EvStr) = 0 then
1787 i1 := I_PLAYER_SCORE_ADD_TEAM
1788 else
1789 i1 := I_PLAYER_SCORE_SUB_TEAM;
1790 // Which team
1791 if Pos('r', EvStr) > 0 then
1792 i2 := I_PLAYER_SCORE_RED
1793 else
1794 i2 := I_PLAYER_SCORE_BLUE;
1795 g_Console_Add(Format(_lc[i1], [_lc[i2], cnt]), True);
1796 end;
1797 end else
1798 begin
1799 // Game Win
1800 if Pos('e', EvStr) = 0 then
1801 i1 := I_PLAYER_SCORE_WIN_OWN
1802 else
1803 i1 := I_PLAYER_SCORE_WIN_ENEMY;
1804 // Which team
1805 if Pos('r', EvStr) > 0 then
1806 i2 := I_PLAYER_SCORE_TO_RED
1807 else
1808 i2 := I_PLAYER_SCORE_TO_BLUE;
1809 g_Console_Add(Format(_lc[i1], [pln, _lc[i2]]), True);
1810 end;
1811 end;
1813 NET_EV_SCORE_MSG:
1814 begin
1815 if EvNum = TEAM_RED then
1816 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1817 if EvNum = TEAM_BLUE then
1818 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1819 if EvNum = -TEAM_RED then
1820 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1821 if EvNum = -TEAM_BLUE then
1822 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1823 end;
1825 NET_EV_LMS_START:
1826 begin
1827 g_Player_RemoveAllCorpses;
1828 g_Game_Message(_lc[I_MESSAGE_LMS_START], 144);
1829 end;
1831 NET_EV_LMS_WIN:
1832 g_Game_Message(Format(_lc[I_MESSAGE_LMS_WIN], [AnsiUpperCase(EvStr)]), 144);
1834 NET_EV_TLMS_WIN:
1835 begin
1836 if EvNum = TEAM_RED then
1837 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 144);
1838 if EvNum = TEAM_BLUE then
1839 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 144);
1840 end;
1842 NET_EV_LMS_LOSE:
1843 g_Game_Message(_lc[I_MESSAGE_LMS_LOSE], 144);
1845 NET_EV_LMS_DRAW:
1846 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
1848 NET_EV_KILLCOMBO:
1849 g_Game_Announce_KillCombo(EvNum);
1851 NET_EV_PLAYER_TOUCH:
1852 begin
1853 pl := g_Player_Get(EvNum);
1854 if pl <> nil then
1855 pl.Touch();
1856 end;
1858 end;
1859 end;
1861 procedure MC_RECV_FlagEvent(var M: TMsg);
1862 var
1863 PID: Word;
1864 Pl: TPlayer;
1865 EvType: Byte;
1866 Fl: Byte;
1867 Quiet: Boolean;
1868 s, ts: string;
1869 begin
1870 EvType := M.ReadByte();
1871 Fl := M.ReadByte();
1873 if Fl = FLAG_NONE then Exit;
1875 Quiet := (M.ReadByte() <> 0);
1876 PID := M.ReadWord();
1878 gFlags[Fl].State := M.ReadByte();
1879 gFlags[Fl].CaptureTime := M.ReadLongWord();
1880 gFlags[Fl].Obj.X := M.ReadLongInt();
1881 gFlags[Fl].Obj.Y := M.ReadLongInt();
1882 gFlags[Fl].Obj.Vel.X := M.ReadLongInt();
1883 gFlags[Fl].Obj.Vel.Y := M.ReadLongInt();
1885 Pl := g_Player_Get(PID);
1886 if (Pl = nil) and
1887 (EvType <> FLAG_STATE_NORMAL) and
1888 (EvType <> FLAG_STATE_DROPPED) and
1889 (EvType <> FLAG_STATE_RETURNED) then
1890 Exit;
1892 case EvType of
1893 FLAG_STATE_NORMAL:
1894 begin
1895 if Quiet or (Pl = nil) then Exit;
1897 if Fl = FLAG_RED then
1898 s := _lc[I_PLAYER_FLAG_RED]
1899 else
1900 s := _lc[I_PLAYER_FLAG_BLUE];
1902 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1903 end;
1905 FLAG_STATE_CAPTURED:
1906 begin
1907 if (Pl <> nil) then Pl.SetFlag(Fl);
1909 if Quiet then Exit;
1911 if Fl = FLAG_RED then
1912 s := _lc[I_PLAYER_FLAG_RED]
1913 else
1914 s := _lc[I_PLAYER_FLAG_BLUE];
1916 g_Console_Add(Format(_lc[I_PLAYER_FLAG_GET], [Pl.Name, s]), True);
1917 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_GET], [AnsiUpperCase(s)]), 144);
1918 end;
1920 FLAG_STATE_DROPPED:
1921 begin
1922 if (Pl <> nil) then Pl.SetFlag(FLAG_NONE);
1924 if Quiet or (Pl = nil) then Exit;
1926 if Fl = FLAG_RED then
1927 s := _lc[I_PLAYER_FLAG_RED]
1928 else
1929 s := _lc[I_PLAYER_FLAG_BLUE];
1931 g_Console_Add(Format(_lc[I_PLAYER_FLAG_DROP], [Pl.Name, s]), True);
1932 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_DROP], [AnsiUpperCase(s)]), 144);
1933 end;
1935 FLAG_STATE_SCORED:
1936 begin
1937 g_Map_ResetFlag(FLAG_RED);
1938 g_Map_ResetFlag(FLAG_BLUE);
1939 if Quiet or (Pl = nil) then Exit;
1940 Pl.SetFlag(FLAG_NONE);
1942 if Fl = FLAG_RED then
1943 s := _lc[I_PLAYER_FLAG_RED]
1944 else
1945 s := _lc[I_PLAYER_FLAG_BLUE];
1947 ts := Format('%.4d', [gFlags[Fl].CaptureTime]);
1948 Insert('.', ts, Length(ts) + 1 - 3);
1949 g_Console_Add(Format(_lc[I_PLAYER_FLAG_CAPTURE], [Pl.Name, s, ts]), True);
1950 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_CAPTURE], [AnsiUpperCase(s)]), 144);
1951 end;
1953 FLAG_STATE_RETURNED:
1954 begin
1955 g_Map_ResetFlag(Fl);
1956 if Quiet then Exit;
1958 if Fl = FLAG_RED then
1959 s := _lc[I_PLAYER_FLAG_RED]
1960 else
1961 s := _lc[I_PLAYER_FLAG_BLUE];
1963 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1964 end;
1965 end;
1966 end;
1968 procedure MC_RECV_GameSettings(var M: TMsg);
1969 begin
1970 gGameSettings.GameMode := M.ReadByte();
1971 gGameSettings.GoalLimit := M.ReadWord();
1972 gGameSettings.TimeLimit := M.ReadWord();
1973 gGameSettings.MaxLives := M.ReadByte();
1974 gGameSettings.Options := M.ReadLongWord();
1975 end;
1977 // PLAYER
1979 function MC_RECV_PlayerCreate(var M: TMsg): Word;
1980 var
1981 PID, DID: Word;
1982 PName, Model: string;
1983 Color: TRGB;
1984 T: Byte;
1985 Pl: TPlayer;
1986 begin
1987 PID := M.ReadWord();
1988 Pl := g_Player_Get(PID);
1990 PName := M.ReadString();
1991 Model := M.ReadString();
1992 Color.R := M.ReadByte();
1993 Color.G := M.ReadByte();
1994 Color.B := M.ReadByte();
1995 T := M.ReadByte();
1997 Result := 0;
1998 if (PID <> NetPlrUID1) and (PID <> NetPlrUID2) then
1999 begin
2000 if (Pl <> nil) then Exit;
2001 DID := g_Player_Create(Model, Color, T, False);
2002 with g_Player_Get(DID) do
2003 begin
2004 UID := PID;
2005 Name := PName;
2006 Reset(True);
2007 end;
2008 end
2009 else
2010 begin
2011 if (PID = NetPlrUID1) and (gPlayer1 <> nil) then begin
2012 gPlayer1.UID := PID;
2013 gPlayer1.Model.SetColor(Color.R, Color.G, Color.B);
2014 gPlayer1.ChangeTeam(T);
2015 end;
2016 if (PID = NetPlrUID2) and (gPlayer2 <> nil) then begin
2017 gPlayer2.UID := PID;
2018 gPlayer2.Model.SetColor(Color.R, Color.G, Color.B);
2019 gPlayer2.ChangeTeam(T);
2020 end;
2021 end;
2023 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
2024 e_WriteLog('NET: Player ' + PName + ' [' + IntToStr(PID) + '] added.', TMsgType.Notify);
2025 Result := PID;
2026 end;
2028 function MC_RECV_PlayerPos(var M: TMsg): Word;
2029 var
2030 GT: LongWord;
2031 PID: Word;
2032 kByte: Word;
2033 Pl: TPlayer;
2034 Dir: Byte;
2035 TmpX, TmpY: Integer;
2036 begin
2037 Result := 0;
2039 GT := M.ReadLongWord();
2040 if GT < gTime - NET_MAX_DIFFTIME then
2041 begin
2042 gTime := GT;
2043 Exit;
2044 end;
2045 gTime := GT;
2046 if g_Game_IsClient and (gPlayer1 <> nil) and (gPlayer1.NetForceWeapFIdx >= gTime+15) then gPlayer1.NetForceWeapFIdx := 0;
2047 if g_Game_IsClient and (gPlayer2 <> nil) and (gPlayer2.NetForceWeapFIdx >= gTime+15) then gPlayer2.NetForceWeapFIdx := 0;
2049 PID := M.ReadWord();
2050 Pl := g_Player_Get(PID);
2052 if Pl = nil then Exit;
2054 Result := PID;
2056 with Pl do
2057 begin
2058 FPing := M.ReadWord();
2059 FLoss := M.ReadByte();
2060 kByte := M.ReadWord();
2061 Dir := M.ReadByte();
2063 TmpX := M.ReadLongInt();
2064 TmpY := M.ReadLongInt();
2066 ReleaseKeysNoWeapon;
2068 if (kByte = NET_KEY_CHAT) then
2069 PressKey(KEY_CHAT, 10000)
2070 else
2071 begin
2072 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
2073 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
2074 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
2075 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
2076 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
2077 end;
2079 if ((Pl <> gPlayer1) and (Pl <> gPlayer2)) or LongBool(kByte and NET_KEY_FORCEDIR) then
2080 SetDirection(TDirection(Dir));
2082 GameVelX := M.ReadLongInt();
2083 GameVelY := M.ReadLongInt();
2084 GameAccelX := M.ReadLongInt();
2085 GameAccelY := M.ReadLongInt();
2086 SetLerp(TmpX, TmpY);
2087 if NetForcePlayerUpdate then Update();
2088 end;
2089 end;
2091 function MC_RECV_PlayerStats(var M: TMsg): Word;
2092 var
2093 PID: Word;
2094 Pl: TPlayer;
2095 I: Integer;
2096 OldJet: Boolean;
2097 NewTeam: Byte;
2098 ww: Word;
2099 newweapon: Byte;
2100 begin
2101 PID := M.ReadWord();
2102 Pl := g_Player_Get(PID);
2103 Result := 0;
2104 if Pl = nil then
2105 Exit;
2107 with Pl do
2108 begin
2109 alive := (M.ReadByte() <> 0);
2110 GodMode := (M.ReadByte() <> 0);
2111 Health := M.ReadLongInt();
2112 Armor := M.ReadLongInt();
2113 Air := M.ReadLongInt();
2114 JetFuel := M.ReadLongInt();
2115 Lives := M.ReadByte();
2116 NewTeam := M.ReadByte();
2118 ww := M.ReadWord();
2119 for I := WP_FIRST to WP_LAST do
2120 begin
2121 FWeapon[I] := ((ww and $01) <> 0);
2122 ww := ww shr 1;
2123 end;
2125 for I := A_BULLETS to A_HIGH do
2126 FAmmo[I] := M.ReadWord();
2128 for I := A_BULLETS to A_HIGH do
2129 FMaxAmmo[I] := M.ReadWord();
2131 for I := MR_SUIT to MR_MAX do
2132 FMegaRulez[I] := M.ReadLongWord();
2134 FRulez := [];
2135 // unpack special flags
2136 ww := M.ReadByte();
2137 if ((ww and $01) <> 0) then FRulez := FRulez+[R_ITEM_BACKPACK];
2138 if ((ww and $02) <> 0) then FRulez := FRulez+[R_KEY_RED];
2139 if ((ww and $04) <> 0) then FRulez := FRulez+[R_KEY_GREEN];
2140 if ((ww and $08) <> 0) then FRulez := FRulez+[R_KEY_BLUE];
2141 if ((ww and $10) <> 0) then FRulez := FRulez+[R_BERSERK];
2143 Frags := M.ReadLongInt();
2144 Death := M.ReadLongInt();
2146 newweapon := M.ReadByte();
2147 {$IFDEF K8_XXX_WEAPON_DEBUG}
2148 writeln('CLIENT PLRSTATS: got: currweap=', CurrWeap, '; curfrm=', gTime, '; netweap=', newweapon, '; lastnwfrm=', NetForceWeapFIdx);
2149 {$ENDIF}
2150 if (gTime >= NetForceWeapFIdx) then
2151 begin
2152 //NetForceWeap := newweapon;
2153 SetWeapon(newweapon);
2154 end;
2155 //SetWeapon(M.ReadByte());
2157 // other flags
2158 ww := M.ReadByte();
2160 FSpectator := ((ww and $01) <> 0);
2161 if FSpectator then
2162 begin
2163 if Pl = gPlayer1 then
2164 begin
2165 gLMSPID1 := UID;
2166 gPlayer1 := nil;
2167 end;
2168 if Pl = gPlayer2 then
2169 begin
2170 gLMSPID2 := UID;
2171 gPlayer2 := nil;
2172 end;
2173 end
2174 else
2175 begin
2176 if (gPlayer1 = nil) and (gLMSPID1 > 0) then
2177 gPlayer1 := g_Player_Get(gLMSPID1);
2178 if (gPlayer2 = nil) and (gLMSPID2 > 0) then
2179 gPlayer2 := g_Player_Get(gLMSPID2);
2180 end;
2182 FGhost := ((ww and $02) <> 0);
2183 FPhysics := ((ww and $04) <> 0);
2184 FNoRespawn := ((ww and $08) <> 0);
2185 OldJet := FJetpack;
2186 FJetpack := ((ww and $10) <> 0);
2187 FFireTime := M.ReadLongInt();
2188 if OldJet and not FJetpack then
2189 JetpackOff
2190 else if not OldJet and FJetpack then
2191 JetpackOn;
2192 if Team <> NewTeam then
2193 Pl.ChangeTeam(NewTeam);
2194 end;
2196 Result := PID;
2197 end;
2199 function MC_RECV_PlayerDamage(var M: TMsg): Word;
2200 var
2201 PID: Word;
2202 Pl: TPlayer;
2203 Kind: Byte;
2204 Attacker, Value: Word;
2205 VX, VY: Integer;
2206 begin
2207 Result := 0;
2208 if not gGameOn then Exit;
2209 PID := M.ReadWord();
2210 Pl := g_Player_Get(PID);
2211 if Pl = nil then Exit;
2213 Kind := M.ReadByte();
2214 Attacker := M.ReadWord();
2215 Value := M.ReadWord();
2216 VX := M.ReadWord();
2217 VY := M.ReadWord();
2219 with Pl do
2220 Damage(Value, Attacker, VX, VY, Kind);
2222 Result := PID;
2223 end;
2225 function MC_RECV_PlayerDeath(var M: TMsg): Word;
2226 var
2227 PID: Word;
2228 Pl: TPlayer;
2229 KillType, DeathType: Byte;
2230 Attacker: Word;
2231 begin
2232 Result := 0;
2233 if not gGameOn then Exit;
2234 PID := M.ReadWord();
2235 Pl := g_Player_Get(PID);
2236 if Pl = nil then Exit;
2238 KillType := M.ReadByte();
2239 DeathType := M.ReadByte();
2240 Attacker := M.ReadWord();
2242 with Pl do
2243 begin
2244 Kill(KillType, Attacker, DeathType);
2245 SoftReset;
2246 end;
2247 end;
2249 function MC_RECV_PlayerDelete(var M: TMsg): Word;
2250 var
2251 PID: Word;
2252 Pl: TPlayer;
2253 begin
2254 PID := M.ReadWord();
2255 Pl := g_Player_Get(PID);
2256 Result := 0;
2257 if Pl = nil then Exit;
2259 g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [Pl.Name]), True);
2260 e_WriteLog('NET: Player ' + Pl.Name + ' [' + IntToStr(PID) + '] removed.', TMsgType.Notify);
2262 g_Player_Remove(PID);
2264 Result := PID;
2265 end;
2267 function MC_RECV_PlayerFire(var M: TMsg): Word;
2268 var
2269 PID: Word;
2270 Weap: Byte;
2271 Pl: TPlayer;
2272 X, Y, AX, AY: Integer;
2273 SHID: Integer;
2274 begin
2275 Result := 0;
2276 if not gGameOn then Exit;
2277 PID := M.ReadWord();
2278 Pl := g_Player_Get(PID);
2279 if Pl = nil then Exit;
2281 Weap := M.ReadByte();
2282 X := M.ReadLongInt();
2283 Y := M.ReadLongInt();
2284 AX := M.ReadLongInt();
2285 AY := M.ReadLongInt();
2286 SHID := M.ReadLongInt();
2288 with Pl do
2289 if alive then NetFire(Weap, X, Y, AX, AY, SHID);
2290 end;
2292 procedure MC_RECV_PlayerSettings(var M: TMsg);
2293 var
2294 TmpName: string;
2295 TmpModel: string;
2296 TmpColor: TRGB;
2297 TmpTeam: Byte;
2298 Pl: TPlayer;
2299 PID: Word;
2300 begin
2301 PID := M.ReadWord();
2302 Pl := g_Player_Get(PID);
2303 if Pl = nil then Exit;
2305 TmpName := M.ReadString();
2306 TmpModel := M.ReadString();
2307 TmpColor.R := M.ReadByte();
2308 TmpColor.G := M.ReadByte();
2309 TmpColor.B := M.ReadByte();
2310 TmpTeam := M.ReadByte();
2312 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
2313 begin
2314 Pl.ChangeTeam(TmpTeam);
2315 if gPlayer1 = Pl then
2316 gPlayer1Settings.Team := TmpTeam;
2317 if gPlayer2 = Pl then
2318 gPlayer2Settings.Team := TmpTeam;
2319 end else
2320 Pl.SetColor(TmpColor);
2322 if Pl.Name <> TmpName then
2323 begin
2324 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
2325 Pl.Name := TmpName;
2326 end;
2328 if TmpModel <> Pl.Model.Name then
2329 Pl.SetModel(TmpModel);
2330 end;
2332 // ITEM
2334 procedure MC_RECV_ItemSpawn(var M: TMsg);
2335 var
2336 ID: Word;
2337 AID: DWord;
2338 X, Y, VX, VY: Integer;
2339 T: Byte;
2340 Quiet, Fall{, Resp}: Boolean;
2341 Anim: TAnimation;
2342 it: PItem;
2343 begin
2344 if not gGameOn then Exit;
2345 ID := M.ReadWord();
2346 Quiet := M.ReadByte() <> 0;
2347 T := M.ReadByte();
2348 Fall := M.ReadByte() <> 0;
2349 {Resp :=} M.ReadByte();
2350 X := M.ReadLongInt();
2351 Y := M.ReadLongInt();
2352 VX := M.ReadLongInt();
2353 VY := M.ReadLongInt();
2355 g_Items_Create(X, Y, T and $7F, Fall, False, False, ID);
2356 if ((T and $80) <> 0) then g_Items_SetDrop(ID);
2358 it := g_Items_ByIdx(ID);
2359 it.Obj.Vel.X := VX;
2360 it.Obj.Vel.Y := VY;
2362 if not Quiet then
2363 begin
2364 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
2365 if g_Frames_Get(AID, 'FRAMES_ITEM_RESPAWN') then
2366 begin
2367 Anim := TAnimation.Create(AID, False, 4);
2368 g_GFX_OnceAnim(X+(it.Obj.Rect.Width div 2)-16, Y+(it.Obj.Rect.Height div 2)-16, Anim);
2369 Anim.Free();
2370 end;
2371 end;
2372 end;
2374 procedure MC_RECV_ItemDestroy(var M: TMsg);
2375 var
2376 ID: Word;
2377 Quiet: Boolean;
2378 begin
2379 if not gGameOn then Exit;
2380 ID := M.ReadWord();
2381 Quiet := M.ReadByte() <> 0;
2383 if not g_Items_ValidId(ID) then exit;
2385 if not Quiet then g_Items_EmitPickupSound(ID);
2387 g_Items_Remove(ID);
2388 end;
2390 // PANEL
2392 procedure MC_RECV_PanelTexture(var M: TMsg);
2393 var
2394 TP: TPanel;
2395 PGUID: Integer;
2396 Tex, Fr: Integer;
2397 Loop, Cnt: Byte;
2398 begin
2399 if not gGameOn then Exit;
2401 PGUID := Integer(M.ReadLongWord());
2402 Tex := M.ReadLongInt();
2403 Fr := M.ReadLongInt();
2404 Cnt := M.ReadByte();
2405 Loop := M.ReadByte();
2407 TP := g_Map_PanelByGUID(PGUID);
2408 if (TP <> nil) then
2409 begin
2410 // switch texture
2411 TP.SetTexture(Tex, Loop);
2412 TP.SetFrame(Fr, Cnt);
2413 end;
2414 end;
2416 procedure MC_RECV_PanelState(var M: TMsg);
2417 var
2418 PGUID: Integer;
2419 E: Boolean;
2420 Lift: Byte;
2421 X, Y, W, H: Integer;
2422 TP: TPanel;
2423 speedX, speedY, startX, startY, endX, endY: Integer;
2424 sizeSpX, sizeSpY, sizeEX, sizeEY: Integer;
2425 mpflags: Byte;
2426 begin
2427 if not gGameOn then Exit;
2429 PGUID := Integer(M.ReadLongWord());
2430 E := (M.ReadByte() <> 0);
2431 Lift := M.ReadByte();
2432 X := M.ReadLongInt();
2433 Y := M.ReadLongInt();
2434 W := M.ReadWord();
2435 H := M.ReadWord();
2436 // mplats
2437 speedX := M.ReadLongInt();
2438 speedY := M.ReadLongInt();
2439 startX := M.ReadLongInt();
2440 startY := M.ReadLongInt();
2441 endX := M.ReadLongInt();
2442 endY := M.ReadLongInt();
2443 sizeSpX := M.ReadLongInt();
2444 sizeSpY := M.ReadLongInt();
2445 sizeEX := M.ReadLongInt();
2446 sizeEY := M.ReadLongInt();
2447 mpflags := M.ReadByte(); // bit0: TP.movingActive; bit1: TP.moveOnce
2449 TP := g_Map_PanelByGUID(PGUID);
2450 if (TP = nil) then exit;
2452 // update lifts state
2453 if TP.isGLift then g_Map_SetLiftGUID(PGUID, Lift);
2455 // update enabled/disabled state for all panels
2456 if E then g_Map_EnableWallGUID(PGUID) else g_Map_DisableWallGUID(PGUID);
2458 // update panel position, as it can be moved (mplat)
2459 TP.X := X;
2460 TP.Y := Y;
2461 TP.Width := W;
2462 TP.Height := H;
2463 // update mplat state
2464 TP.movingSpeedX := speedX;
2465 TP.movingSpeedY := speedY;
2466 TP.movingStartX := startX;
2467 TP.movingStartY := startY;
2468 TP.movingEndX := endX;
2469 TP.movingEndY := endY;
2470 TP.sizeSpeedX := sizeSpX;
2471 TP.sizeSpeedY := sizeSpY;
2472 TP.sizeEndX := sizeEX;
2473 TP.sizeEndY := sizeEY;
2474 TP.movingActive := ((mpflags and 1) <> 0);
2475 TP.moveOnce := ((mpflags and 2) <> 0);
2476 // notify panel of it's position/size change, so it can fix other internal structures
2477 TP.positionChanged();
2478 end;
2480 // TRIGGERS
2482 procedure MC_RECV_TriggerSound(var M: TMsg);
2483 var
2484 SPlaying: Boolean;
2485 SPos, SID: LongWord;
2486 SCount: LongInt;
2487 I: Integer;
2488 begin
2489 if not gGameOn then Exit;
2490 if gTriggers = nil then Exit;
2492 SID := M.ReadLongWord();
2493 SPlaying := M.ReadByte() <> 0;
2494 SPos := M.ReadLongWord();
2495 SCount := M.ReadLongInt();
2497 for I := Low(gTriggers) to High(gTriggers) do
2498 if gTriggers[I].TriggerType = TRIGGER_SOUND then
2499 if gTriggers[I].ClientID = SID then
2500 with gTriggers[I] do
2501 begin
2502 if Sound <> nil then
2503 begin
2504 if SPlaying then
2505 begin
2506 if tgcLocal then
2507 Sound.PlayVolumeAt(X+(Width div 2), Y+(Height div 2), tgcVolume/255.0)
2508 else
2509 Sound.PlayPanVolume((tgcPan-127.0)/128.0, tgcVolume/255.0);
2510 Sound.SetPosition(SPos);
2511 end
2512 else
2513 if Sound.IsPlaying then Sound.Stop;
2514 end;
2516 SoundPlayCount := SCount;
2517 end;
2518 end;
2520 procedure MC_RECV_TriggerMusic(var M: TMsg);
2521 var
2522 MName: string;
2523 MPlaying: Boolean;
2524 MPos: LongWord;
2525 MPaused: Boolean;
2526 begin
2527 if not gGameOn then Exit;
2529 MName := M.ReadString();
2530 MPlaying := M.ReadByte() <> 0;
2531 MPos := M.ReadLongWord();
2532 MPaused := M.ReadByte() <> 0;
2534 if MPlaying then
2535 begin
2536 gMusic.SetByName(MName);
2537 gMusic.Play(True);
2538 gMusic.SetPosition(MPos);
2539 gMusic.SpecPause := MPaused;
2540 end
2541 else
2542 if gMusic.IsPlaying then gMusic.Stop;
2543 end;
2545 // MONSTERS
2547 procedure MC_RECV_MonsterSpawn(var M: TMsg);
2548 var
2549 ID: Word;
2550 MType, MState, MDir, MAnim, MBehav: Byte;
2551 X, Y, VX, VY, MTargTime, MHealth, MAmmo, MSleep: Integer;
2552 MTarg: Word;
2553 Mon: TMonster;
2554 begin
2555 ID := M.ReadWord();
2556 Mon := g_Monsters_ByUID(ID);
2557 if Mon <> nil then
2558 Exit;
2560 MType := M.ReadByte();
2561 MState := M.ReadByte();
2562 MAnim := M.ReadByte();
2563 MTarg := M.ReadWord();
2564 MTargTime := M.ReadLongInt();
2565 MBehav := M.ReadByte();
2566 MSleep := M.ReadLongInt();
2567 MHealth := M.ReadLongInt();
2568 MAmmo := M.ReadLongInt();
2570 X := M.ReadLongInt();
2571 Y := M.ReadLongInt();
2572 VX := M.ReadLongInt();
2573 VY := M.ReadLongInt();
2574 MDir := M.ReadByte();
2576 g_Monsters_Create(MType, X, Y, TDirection(MDir), False, ID);
2577 Mon := g_Monsters_ByUID(ID);
2578 if Mon = nil then
2579 Exit;
2581 with Mon do
2582 begin
2584 MonsterAnim := MAnim;
2585 MonsterTargetUID := MTarg;
2586 MonsterTargetTime := MTargTime;
2587 MonsterBehaviour := MBehav;
2588 MonsterSleep := MSleep;
2589 MonsterAmmo := MAmmo;
2590 SetHealth(MHealth);
2592 SetState(MState);
2594 setPosition(X, Y); // this will call positionChanged();
2595 GameVelX := VX;
2596 GameVelY := VY;
2597 end;
2598 end;
2600 procedure MC_RECV_MonsterPos(var M: TMsg);
2601 var
2602 Mon: TMonster;
2603 ID: Word;
2604 X, Y: Integer;
2605 begin
2606 ID := M.ReadWord();
2607 Mon := g_Monsters_ByUID(ID);
2608 if Mon = nil then
2609 Exit;
2611 with Mon do
2612 begin
2613 X := M.ReadLongInt();
2614 Y := M.ReadLongInt();
2615 Mon.setPosition(X, Y); // this will call `positionChanged()`
2616 GameVelX := M.ReadLongInt();
2617 GameVelY := M.ReadLongInt();
2618 GameDirection := TDirection(M.ReadByte());
2619 end;
2620 end;
2622 procedure MC_RECV_MonsterState(var M: TMsg);
2623 var
2624 ID: Integer;
2625 MState, MFAnm: Byte;
2626 Mon: TMonster;
2627 AnimRevert: Boolean;
2628 begin
2629 ID := M.ReadWord();
2630 Mon := g_Monsters_ByUID(ID);
2631 if Mon = nil then Exit;
2633 MState := M.ReadByte();
2634 MFAnm := M.ReadByte();
2636 with Mon do
2637 begin
2638 MonsterTargetUID := M.ReadWord();
2639 MonsterTargetTime := M.ReadLongInt();
2640 MonsterSleep := M.ReadLongInt();
2641 MonsterHealth := M.ReadLongInt();
2642 MonsterAmmo := M.ReadLongInt();
2643 MonsterPain := M.ReadLongInt();
2644 AnimRevert := M.ReadByte() <> 0;
2645 FFireTime := M.ReadLongInt();
2646 RevertAnim(AnimRevert);
2648 if MonsterState <> MState then
2649 begin
2650 if (MState = MONSTATE_GO) and (MonsterState = MONSTATE_SLEEP) then WakeUpSound();
2651 if (MState = MONSTATE_DIE) then DieSound();
2652 if (MState = MONSTATE_PAIN) then MakeBloodSimple(Min(200, MonsterPain));
2653 if (MState = MONSTATE_ATTACK) then kick(nil);
2654 if (MState = MONSTATE_DEAD) then SetDeadAnim();
2656 SetState(MState, MFAnm);
2657 end;
2658 end;
2659 end;
2661 procedure MC_RECV_MonsterShot(var M: TMsg);
2662 var
2663 ID: Integer;
2664 Mon: TMonster;
2665 X, Y, VX, VY: Integer;
2666 begin
2667 ID := M.ReadWord();
2669 Mon := g_Monsters_ByUID(ID);
2670 if Mon = nil then Exit;
2672 X := M.ReadLongInt();
2673 Y := M.ReadLongInt();
2674 VX := M.ReadLongInt();
2675 VY := M.ReadLongInt();
2677 Mon.ClientAttack(X, Y, VX, VY);
2678 end;
2680 procedure MC_RECV_MonsterDelete(var M: TMsg);
2681 var
2682 ID: Integer;
2683 Mon: TMonster;
2684 begin
2685 ID := M.ReadWord();
2686 Mon := g_Monsters_ByUID(ID);
2687 if Mon = nil then Exit;
2688 Mon.SetState(5);
2689 Mon.MonsterRemoved := True;
2690 end;
2692 procedure MC_RECV_TimeSync(var M: TMsg);
2693 var
2694 Time: LongWord;
2695 begin
2696 Time := M.ReadLongWord();
2698 if gState = STATE_INTERCUSTOM then
2699 gServInterTime := Min(Time, 255);
2700 end;
2702 procedure MC_RECV_VoteEvent(var M: TMsg);
2703 var
2704 EvID: Byte;
2705 Str1, Str2: string;
2706 Int1, Int2: SmallInt;
2707 begin
2708 EvID := M.ReadByte();
2709 Int1 := M.ReadSmallInt();
2710 Int2 := M.ReadSmallInt();
2711 Str1 := M.ReadString();
2712 Str2 := M.ReadString();
2714 case EvID of
2715 NET_VE_STARTED:
2716 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_STARTED], [Str1, Str2, Int1]), True);
2717 NET_VE_PASSED:
2718 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_PASSED], [Str1]), True);
2719 NET_VE_FAILED:
2720 g_Console_Add(_lc[I_MESSAGE_VOTE_FAILED], True);
2721 NET_VE_VOTE:
2722 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Str1, Int1, Int2]), True);
2723 NET_VE_INPROGRESS:
2724 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_INPROGRESS], [Str1]), True);
2725 end;
2726 end;
2728 // CLIENT SEND
2730 procedure MC_SEND_Info(Password: string);
2731 begin
2732 NetOut.Clear();
2734 NetOut.Write(Byte(NET_MSG_INFO));
2735 NetOut.Write(GAME_VERSION);
2736 NetOut.Write(Password);
2737 NetOut.Write(gPlayer1Settings.Name);
2738 NetOut.Write(gPlayer1Settings.Model);
2739 NetOut.Write(gPlayer1Settings.Color.R);
2740 NetOut.Write(gPlayer1Settings.Color.G);
2741 NetOut.Write(gPlayer1Settings.Color.B);
2742 NetOut.Write(gPlayer1Settings.Team);
2744 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2745 end;
2747 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
2748 begin
2749 NetOut.Write(Byte(NET_MSG_CHAT));
2750 NetOut.Write(Txt);
2751 NetOut.Write(Mode);
2753 g_Net_Client_Send(True, NET_CHAN_CHAT);
2754 end;
2756 function isKeyPressed (key1: Word; key2: Word): Boolean;
2757 begin
2758 if (key1 <> 0) and e_KeyPressed(key1) then begin result := true; exit; end;
2759 if (key2 <> 0) and e_KeyPressed(key2) then begin result := true; exit; end;
2760 result := false;
2761 end;
2763 procedure MC_SEND_PlayerPos();
2764 var
2765 kByte: Word;
2766 Predict: Boolean;
2767 strafeDir: Byte;
2768 //WeaponSelect: Word = 0;
2769 I: Integer;
2770 begin
2771 if not gGameOn then Exit;
2772 if gPlayers = nil then Exit;
2773 if gPlayer1 = nil then Exit;
2775 kByte := 0;
2776 Predict := NetPredictSelf; // and (not NetGotKeys);
2778 if (not gConsoleShow) and (not gChatShow) and (g_ActiveWindow = nil) then
2779 begin
2780 strafeDir := P1MoveButton shr 4;
2781 P1MoveButton := P1MoveButton and $0F;
2782 with gGameControls.P1Control do
2783 begin
2784 if isKeyPressed(KeyLeft, KeyLeft2) and (not isKeyPressed(KeyRight, KeyRight2)) then P1MoveButton := 1
2785 else if (not isKeyPressed(KeyLeft, KeyLeft2)) and isKeyPressed(KeyRight, KeyRight2) then P1MoveButton := 2
2786 else if (not isKeyPressed(KeyLeft, KeyLeft2)) and (not isKeyPressed(KeyRight, KeyRight2)) then P1MoveButton := 0;
2788 // strafing
2789 if isKeyPressed(KeyStrafe, KeyStrafe2) then
2790 begin
2791 // new strafe mechanics
2792 if (strafeDir = 0) then strafeDir := P1MoveButton; // start strafing
2793 // now set direction according to strafe (reversed)
2794 if (strafeDir = 2) then gPlayer1.SetDirection(TDirection.D_LEFT)
2795 else if (strafeDir = 1) then gPlayer1.SetDirection(TDirection.D_RIGHT);
2796 end
2797 else
2798 begin
2799 if (P1MoveButton = 2) and isKeyPressed(KeyLeft, KeyLeft2) then gPlayer1.SetDirection(TDirection.D_LEFT)
2800 else if (P1MoveButton = 1) and isKeyPressed(KeyRight, KeyRight2) then gPlayer1.SetDirection(TDirection.D_RIGHT)
2801 else if P1MoveButton <> 0 then gPlayer1.SetDirection(TDirection(P1MoveButton-1));
2802 end;
2804 gPlayer1.ReleaseKeys;
2805 gPlayer1.weaponSwitchKeysStateChange(-1, isKeyPressed(KeyNextWeapon, KeyNextWeapon2));
2806 gPlayer1.weaponSwitchKeysStateChange(-2, isKeyPressed(KeyPrevWeapon, KeyPrevWeapon2));
2808 if P1MoveButton = 1 then
2809 begin
2810 kByte := kByte or NET_KEY_LEFT;
2811 if Predict then gPlayer1.PressKey(KEY_LEFT, 10000);
2812 end;
2813 if P1MoveButton = 2 then
2814 begin
2815 kByte := kByte or NET_KEY_RIGHT;
2816 if Predict then gPlayer1.PressKey(KEY_RIGHT, 10000);
2817 end;
2818 if isKeyPressed(KeyUp, KeyUp2) then
2819 begin
2820 kByte := kByte or NET_KEY_UP;
2821 gPlayer1.PressKey(KEY_UP, 10000);
2822 end;
2823 if isKeyPressed(KeyDown, KeyDown2) then
2824 begin
2825 kByte := kByte or NET_KEY_DOWN;
2826 gPlayer1.PressKey(KEY_DOWN, 10000);
2827 end;
2828 if isKeyPressed(KeyJump, KeyJump2) then
2829 begin
2830 kByte := kByte or NET_KEY_JUMP;
2831 // gPlayer1.PressKey(KEY_JUMP, 10000); // TODO: Make a prediction option
2832 end;
2833 if isKeyPressed(KeyFire, KeyFire2) then kByte := kByte or NET_KEY_FIRE;
2834 if isKeyPressed(KeyOpen, KeyOpen2) then kByte := kByte or NET_KEY_OPEN;
2835 // do not send weapon switch keys, `MH_SEND_PlayerStats()` will send changed weapon anyway
2836 if isKeyPressed(KeyNextWeapon, KeyNextWeapon2) and gPlayer1.isWeaponSwitchKeyReleased(-1) then gPlayer1.PressKey(KEY_NEXTWEAPON); //kByte := kByte or NET_KEY_NW;
2837 if isKeyPressed(KeyPrevWeapon, KeyPrevWeapon2) and gPlayer1.isWeaponSwitchKeyReleased(-2) then gPlayer1.PressKey(KEY_PREVWEAPON); //kByte := kByte or NET_KEY_PW;
2838 for I := 0 to High(KeyWeapon) do
2839 begin
2840 if isKeyPressed(KeyWeapon[I], KeyWeapon2[I]) then
2841 begin
2842 gPlayer1.weaponSwitchKeysStateChange(i, true);
2843 if gPlayer1.isWeaponSwitchKeyReleased(i) then
2844 begin
2845 gPlayer1.QueueWeaponSwitch(i); // all choices are passed there, and god will take the best
2846 //WeaponSelect := WeaponSelect or Word(1 shl I);
2847 end;
2848 end
2849 else
2850 begin
2851 gPlayer1.weaponSwitchKeysStateChange(i, false);
2852 end;
2853 end;
2854 end;
2855 // fix movebutton state
2856 P1MoveButton := P1MoveButton or (strafeDir shl 4);
2857 end
2858 else
2859 kByte := NET_KEY_CHAT;
2861 gPlayer1.weaponSwitchKeysShiftNewStates();
2863 NetOut.Write(Byte(NET_MSG_PLRPOS));
2864 NetOut.Write(gTime);
2865 NetOut.Write(kByte);
2866 NetOut.Write(Byte(gPlayer1.Direction));
2867 NetOut.Write(Byte(gPlayer1.CurrWeap));
2868 {$IFDEF K8_XXX_WEAPON_DEBUG}
2869 if (kByte and NET_KEY_FIRE) <> 0 then writeln('FIRE: CurrWeap=', gPlayer1.CurrWeap);
2870 {$ENDIF}
2871 //e_WriteLog(Format('S:nwp=%d; nwfidx=%d', [Integer(gPlayer1.NetForceWeap), Integer(gPlayer1.NetForceWeapFIdx)]), TMsgType.Warning);
2872 //NetOut.Write(WeaponSelect);
2873 //e_WriteLog(Format('S:ws=%d', [WeaponSelect]), MSG_WARNING);
2874 g_Net_Client_Send(True, NET_CHAN_PLAYERPOS);
2876 // force player weapon
2877 //{if (gPlayer1.NetForceWeapFIdx < gTime) then} gPlayer1.SetWeapon(gPlayer1.NetForceWeap);
2879 //kBytePrev := kByte;
2880 //kDirPrev := gPlayer1.Direction;
2881 end;
2883 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
2884 begin
2885 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
2886 NetOut.Write(Byte(Start));
2887 NetOut.Write(Command);
2888 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2889 end;
2891 procedure MC_SEND_PlayerSettings();
2892 begin
2893 NetOut.Write(Byte(NET_MSG_PLRSET));
2894 NetOut.Write(gPlayer1Settings.Name);
2895 NetOut.Write(gPlayer1Settings.Model);
2896 NetOut.Write(gPlayer1Settings.Color.R);
2897 NetOut.Write(gPlayer1Settings.Color.G);
2898 NetOut.Write(gPlayer1Settings.Color.B);
2899 NetOut.Write(gPlayer1Settings.Team);
2901 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2902 end;
2904 procedure MC_SEND_FullStateRequest();
2905 begin
2906 NetOut.Write(Byte(NET_MSG_REQFST));
2908 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2909 end;
2911 procedure MC_SEND_CheatRequest(Kind: Byte);
2912 begin
2913 NetOut.Write(Byte(NET_MSG_CHEAT));
2914 NetOut.Write(Kind);
2916 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2917 end;
2918 procedure MC_SEND_RCONPassword(Password: string);
2919 begin
2920 NetOut.Write(Byte(NET_MSG_RCON_AUTH));
2921 NetOut.Write(Password);
2923 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2924 end;
2925 procedure MC_SEND_RCONCommand(Cmd: string);
2926 begin
2927 NetOut.Write(Byte(NET_MSG_RCON_CMD));
2928 NetOut.Write(Cmd);
2930 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2931 end;
2933 // i have no idea why all this stuff is in here
2935 function ReadFile(const FileName: TFileName): AByte;
2936 var
2937 FileStream : TStream;
2938 fname: string;
2939 begin
2940 e_WriteLog(Format('NETWORK: looking for file "%s"', [FileName]), TMsgType.Notify);
2941 fname := findDiskWad(FileName);
2942 if length(fname) = 0 then
2943 begin
2944 e_WriteLog(Format('NETWORK: file "%s" not found!', [FileName]), TMsgType.Fatal);
2945 SetLength(Result, 0);
2946 exit;
2947 end;
2948 e_WriteLog(Format('NETWORK: found file "%s"', [fname]), TMsgType.Notify);
2949 Result := nil;
2950 FileStream := openDiskFileRO(fname);
2951 try
2952 if FileStream.Size > 0 then
2953 begin
2954 SetLength(Result, FileStream.Size);
2955 FileStream.Read(Result[0], FileStream.Size);
2956 end;
2957 finally
2958 FileStream.Free;
2959 end;
2960 end;
2962 function CreateMapDataMsg(const FileName: TFileName; ResList: TStringList): TMapDataMsg;
2963 var
2964 i: Integer;
2965 begin
2966 Result.MsgId := NET_MSG_MAP_RESPONSE;
2967 Result.FileData := ReadFile(FileName);
2968 Result.FileSize := Length(Result.FileData);
2970 SetLength(Result.ExternalResources, ResList.Count);
2971 for i:=0 to ResList.Count-1 do
2972 begin
2973 Result.ExternalResources[i].Name := ResList.Strings[i];
2974 Result.ExternalResources[i].md5 := MD5File(GameDir+'/wads/'+ResList.Strings[i]);
2975 end;
2976 end;
2978 procedure ResDataMsgToBytes(var bytes: AByte; const ResData: TResDataMsg);
2979 var
2980 ResultStream: TMemoryStream;
2981 begin
2982 ResultStream := TMemoryStream.Create;
2984 ResultStream.WriteBuffer(ResData.MsgId, SizeOf(ResData.MsgId)); //msgId
2985 ResultStream.WriteBuffer(ResData.FileSize, SizeOf(ResData.FileSize)); //file size
2986 ResultStream.WriteBuffer(ResData.FileData[0], ResData.FileSize); //file data
2988 SetLength(bytes, ResultStream.Size);
2989 ResultStream.Seek(0, soFromBeginning);
2990 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
2992 ResultStream.Free;
2993 end;
2995 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
2996 begin
2997 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
2998 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize));
2999 SetLength(Result.FileData, Result.FileSize);
3000 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize);
3001 end;
3003 procedure MapDataMsgToBytes(var bytes: AByte; const MapDataMsg: TMapDataMsg);
3004 var
3005 ResultStream: TMemoryStream;
3006 resCount: Integer;
3007 begin
3008 resCount := Length(MapDataMsg.ExternalResources);
3010 ResultStream := TMemoryStream.Create;
3012 ResultStream.WriteBuffer(MapDataMsg.MsgId, SizeOf(MapDataMsg.MsgId)); //msgId
3013 ResultStream.WriteBuffer(MapDataMsg.FileSize, SizeOf(MapDataMsg.FileSize)); //file size
3014 ResultStream.WriteBuffer(MapDataMsg.FileData[0], MapDataMsg.FileSize); //file data
3016 ResultStream.WriteBuffer(resCount, SizeOf(resCount)); //res count
3017 ResultStream.WriteBuffer(MapDataMsg.ExternalResources[0], resCount*SizeOf(TExternalResourceInfo)); //res data
3019 SetLength(bytes, ResultStream.Size);
3020 ResultStream.Seek(0, soFromBeginning);
3021 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
3023 ResultStream.Free;
3024 end;
3026 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
3027 var
3028 resCount: Integer;
3029 begin
3030 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
3031 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize)); //file size
3033 SetLength(Result.FileData, Result.FileSize);
3034 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize); //file data
3036 msgStream.ReadBuffer(resCount, SizeOf(resCount)); //res count
3037 SetLength(Result.ExternalResources, resCount);
3039 msgStream.ReadBuffer(Result.ExternalResources[0], resCount * SizeOf(TExternalResourceInfo)); //res data
3040 end;
3042 function IsValidFileName(const S: String): Boolean;
3043 const
3044 Forbidden: set of Char = ['<', '>', '|', '"', ':', '*', '?'];
3045 var
3046 I: Integer;
3047 begin
3048 Result := S <> '';
3049 for I := 1 to Length(S) do
3050 Result := Result and (not(S[I] in Forbidden));
3051 end;
3053 function IsValidFilePath(const S: String): Boolean;
3054 var
3055 I: Integer;
3056 begin
3057 Result := False;
3058 if not IsValidFileName(S) then exit;
3059 if FileExists(S) then exit;
3060 I := LastDelimiter('\/', S);
3061 if (I > 0) then
3062 if (not DirectoryExists(Copy(S, 1, I-1))) then
3063 exit;
3064 Result := True;
3065 end;
3067 procedure MC_SEND_MapRequest();
3068 begin
3069 NetOut.Write(Byte(NET_MSG_MAP_REQUEST));
3070 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3071 end;
3073 procedure MC_SEND_ResRequest(const resName: AnsiString);
3074 begin
3075 NetOut.Write(Byte(NET_MSG_RES_REQUEST));
3076 NetOut.Write(resName);
3077 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3078 end;
3080 procedure MH_RECV_MapRequest(C: pTNetClient; var M: TMsg);
3081 var
3082 payload: AByte;
3083 peer: pENetPeer;
3084 mapDataMsg: TMapDataMsg;
3085 begin
3086 e_WriteLog('NET: Received map request from ' +
3087 DecodeIPV4(C^.Peer.address.host), TMsgType.Notify);
3089 mapDataMsg := CreateMapDataMsg(MapsDir + gGameSettings.WAD, gExternalResources);
3090 peer := NetClients[C^.ID].Peer;
3092 MapDataMsgToBytes(payload, mapDataMsg);
3093 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3095 payload := nil;
3096 mapDataMsg.FileData := nil;
3097 mapDataMsg.ExternalResources := nil;
3098 end;
3100 procedure MH_RECV_ResRequest(C: pTNetClient; var M: TMsg);
3101 var
3102 payload: AByte;
3103 peer: pENetPeer;
3104 FileName: String;
3105 resDataMsg: TResDataMsg;
3106 begin
3107 FileName := ExtractFileName(M.ReadString());
3108 e_WriteLog('NET: Received res request: ' + FileName +
3109 ' from ' + DecodeIPV4(C^.Peer.address.host), TMsgType.Notify);
3111 if not IsValidFilePath(FileName) then
3112 begin
3113 e_WriteLog('Invalid filename: ' + FileName, TMsgType.Warning);
3114 exit;
3115 end;
3117 peer := NetClients[C^.ID].Peer;
3119 if gExternalResources.IndexOf(FileName) > -1 then
3120 begin
3121 resDataMsg.MsgId := NET_MSG_RES_RESPONSE;
3122 resDataMsg.FileData := ReadFile(GameDir+'/wads/'+FileName);
3123 resDataMsg.FileSize := Length(resDataMsg.FileData);
3125 ResDataMsgToBytes(payload, resDataMsg);
3126 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3127 end;
3128 end;
3130 end.