DEADSOFTWARE

SDL2: set window position properly; always center when going fullscreen->windowed
[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, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 unit g_netmsg;
18 interface
20 uses e_msg, g_net, g_triggers, Classes, SysUtils, md5;
22 const
23 NET_MSG_INFO = 100;
25 NET_MSG_CHAT = 101;
26 NET_MSG_SND = 102;
27 NET_MSG_GFX = 103;
28 NET_MSG_GEVENT = 104;
29 NET_MSG_SCORE = 105;
30 NET_MSG_COOP = 106;
31 NET_MSG_FLAG = 107;
32 NET_MSG_REQFST = 108;
33 NET_MSG_GSET = 109;
35 NET_MSG_PLR = 111;
36 NET_MSG_PLRPOS = 112;
37 NET_MSG_PLRSTA = 113;
38 NET_MSG_PLRDEL = 114;
39 NET_MSG_PLRDMG = 115;
40 NET_MSG_PLRDIE = 116;
41 NET_MSG_PLRFIRE= 117;
42 NET_MSG_PLRSET = 119;
43 NET_MSG_CHEAT = 120;
45 NET_MSG_ISPAWN = 121;
46 NET_MSG_IDEL = 122;
48 NET_MSG_MSPAWN = 131;
49 NET_MSG_MPOS = 132;
50 NET_MSG_MSTATE = 133;
51 NET_MSG_MSHOT = 134;
52 NET_MSG_MDEL = 135;
54 NET_MSG_PSTATE = 141;
55 NET_MSG_PTEX = 142;
57 NET_MSG_TSOUND = 151;
58 NET_MSG_TMUSIC = 152;
60 NET_MSG_SHDEL = 161;
61 NET_MSG_SHADD = 162;
62 NET_MSG_SHPOS = 163;
64 NET_MSG_RCON_AUTH = 191;
65 NET_MSG_RCON_CMD = 192;
66 NET_MSG_TIME_SYNC = 194;
67 NET_MSG_VOTE_EVENT = 195;
69 {
70 NET_MSG_MAP_REQUEST = 201;
71 NET_MSG_MAP_RESPONSE = 202;
72 NET_MSG_RES_REQUEST = 203;
73 NET_MSG_RES_RESPONSE = 204;
74 }
76 NET_CHAT_SYSTEM = 0;
77 NET_CHAT_PLAYER = 1;
78 NET_CHAT_TEAM = 2;
80 NET_RCON_NOAUTH = 0;
81 NET_RCON_PWGOOD = 1;
82 NET_RCON_PWBAD = 2;
84 NET_GFX_SPARK = 1;
85 NET_GFX_TELE = 2;
86 NET_GFX_RESPAWN = 3;
87 NET_GFX_FIRE = 4;
88 NET_GFX_EXPLODE = 5;
89 NET_GFX_BFGEXPL = 6;
90 NET_GFX_BFGHIT = 7;
91 NET_GFX_SHELL1 = 8;
92 NET_GFX_SHELL2 = 9;
93 NET_GFX_SHELL3 = 10;
95 NET_EV_MAPSTART = 1;
96 NET_EV_MAPEND = 2;
97 NET_EV_CHANGE_TEAM = 3;
98 NET_EV_PLAYER_KICK = 4;
99 NET_EV_PLAYER_BAN = 5;
100 NET_EV_LMS_WARMUP = 6;
101 NET_EV_LMS_SURVIVOR = 7;
102 NET_EV_RCON = 8;
103 NET_EV_BIGTEXT = 9;
104 NET_EV_SCORE = 10;
105 NET_EV_SCORE_MSG = 11;
106 NET_EV_LMS_START = 12;
107 NET_EV_LMS_WIN = 13;
108 NET_EV_TLMS_WIN = 14;
109 NET_EV_LMS_LOSE = 15;
110 NET_EV_LMS_DRAW = 16;
111 NET_EV_KILLCOMBO = 17;
112 NET_EV_PLAYER_TOUCH = 18;
113 NET_EV_SECRET = 19;
114 NET_EV_INTER_READY = 20;
116 NET_VE_STARTED = 1;
117 NET_VE_PASSED = 2;
118 NET_VE_FAILED = 3;
119 NET_VE_VOTE = 4;
120 NET_VE_REVOKE = 5;
121 NET_VE_INPROGRESS = 6;
123 NET_FLAG_GET = 1;
124 NET_FLAG_DROP = 2;
125 NET_FLAG_CAP = 3;
126 NET_FLAG_RETURN = 4;
128 NET_CHEAT_SUICIDE = 1;
129 NET_CHEAT_SPECTATE = 2;
130 NET_CHEAT_READY = 3;
132 NET_MAX_DIFFTIME = 5000 div 36;
134 // HOST MESSAGES
136 procedure MH_ProcessFirstSpawn (C: pTNetClient);
138 procedure MH_RECV_Info(C: pTNetClient; var M: TMsg);
139 procedure MH_RECV_Chat(C: pTNetClient; var M: TMsg);
140 procedure MH_RECV_FullStateRequest(C: pTNetClient; var M: TMsg);
141 function MH_RECV_PlayerPos(C: pTNetClient; var M: TMsg): Word;
142 procedure MH_RECV_PlayerSettings(C: pTNetClient; var M: TMsg);
143 procedure MH_RECV_CheatRequest(C: pTNetClient; var M: TMsg);
144 procedure MH_RECV_RCONPassword(C: pTNetClient; var M: TMsg);
145 procedure MH_RECV_RCONCommand(C: pTNetClient; var M: TMsg);
146 //procedure MH_RECV_MapRequest(C: pTNetClient; var M: TMsg);
147 //procedure MH_RECV_ResRequest(C: pTNetClient; var M: TMsg);
148 procedure MH_RECV_Vote(C: pTNetClient; var M: TMsg);
150 // GAME
151 procedure MH_SEND_Everything(CreatePlayers: Boolean {= False}; ID: Integer {= NET_EVERYONE});
152 procedure MH_SEND_Info(ID: Byte);
153 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
154 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
155 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
156 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
157 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
158 procedure MH_SEND_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
159 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
160 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
161 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
162 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
163 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
164 // PLAYER
165 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
166 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
167 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
168 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
169 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
170 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
171 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
172 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
173 // ITEM
174 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
175 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
176 // PANEL
177 procedure MH_SEND_PanelTexture(PGUID: Integer; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
178 procedure MH_SEND_PanelState(PGUID: Integer; ID: Integer = NET_EVERYONE);
179 // MONSTER
180 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
181 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
182 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
183 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
184 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
185 // TRIGGER
186 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
187 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
188 // MISC
189 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
190 procedure MH_SEND_VoteEvent(EvType: Byte;
191 StrArg1: string = 'a'; StrArg2: string = 'b';
192 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
193 ID: Integer = NET_EVERYONE);
195 // CLIENT MESSAGES //
197 // GAME
198 procedure MC_RECV_Chat(var M: TMsg);
199 procedure MC_RECV_Effect(var M: TMsg);
200 procedure MC_RECV_Sound(var M: TMsg);
201 procedure MC_RECV_GameStats(var M: TMsg);
202 procedure MC_RECV_CoopStats(var M: TMsg);
203 procedure MC_RECV_GameEvent(var M: TMsg);
204 procedure MC_RECV_FlagEvent(var M: TMsg);
205 procedure MC_RECV_GameSettings(var M: TMsg);
206 // PLAYER
207 function MC_RECV_PlayerCreate(var M: TMsg): Word;
208 function MC_RECV_PlayerPos(var M: TMsg): Word;
209 function MC_RECV_PlayerStats(var M: TMsg): Word;
210 function MC_RECV_PlayerDelete(var M: TMsg): Word;
211 function MC_RECV_PlayerDamage(var M: TMsg): Word;
212 function MC_RECV_PlayerDeath(var M: TMsg): Word;
213 function MC_RECV_PlayerFire(var M: TMsg): Word;
214 procedure MC_RECV_PlayerSettings(var M: TMsg);
215 // ITEM
216 procedure MC_RECV_ItemSpawn(var M: TMsg);
217 procedure MC_RECV_ItemDestroy(var M: TMsg);
218 // PANEL
219 procedure MC_RECV_PanelTexture(var M: TMsg);
220 procedure MC_RECV_PanelState(var M: TMsg);
221 // MONSTER
222 procedure MC_RECV_MonsterSpawn(var M: TMsg);
223 procedure MC_RECV_MonsterPos(var M: TMsg);
224 procedure MC_RECV_MonsterState(var M: TMsg);
225 procedure MC_RECV_MonsterShot(var M: TMsg);
226 procedure MC_RECV_MonsterDelete(var M: TMsg);
227 // SHOT
228 procedure MC_RECV_CreateShot(var M: TMsg);
229 procedure MC_RECV_UpdateShot(var M: TMsg);
230 procedure MC_RECV_DeleteShot(var M: TMsg);
231 // TRIGGER
232 procedure MC_RECV_TriggerSound(var M: TMsg);
233 procedure MC_RECV_TriggerMusic(var M: TMsg);
234 // MISC
235 procedure MC_RECV_TimeSync(var M: TMsg);
236 procedure MC_RECV_VoteEvent(var M: TMsg);
237 // SERVICE
238 procedure MC_SEND_Info(Password: string);
239 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
240 procedure MC_SEND_PlayerPos();
241 procedure MC_SEND_FullStateRequest();
242 procedure MC_SEND_PlayerSettings();
243 procedure MC_SEND_CheatRequest(Kind: Byte);
244 procedure MC_SEND_RCONPassword(Password: string);
245 procedure MC_SEND_RCONCommand(Cmd: string);
246 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
247 // DOWNLOAD
248 //procedure MC_SEND_MapRequest();
249 //procedure MC_SEND_ResRequest(const resName: AnsiString);
252 type
253 TExternalResourceInfo = record
254 Name: string[255];
255 md5: TMD5Digest;
256 end;
258 TResDataMsg = record
259 MsgId: Byte;
260 FileSize: Integer;
261 FileData: AByte;
262 end;
264 TMapDataMsg = record
265 MsgId: Byte;
266 FileSize: Integer;
267 FileData: AByte;
268 ExternalResources: array of TExternalResourceInfo;
269 end;
271 function IsValidFileName(const S: String): Boolean;
272 function IsValidFilePath(const S: String): Boolean;
275 implementation
277 uses
278 Math, ENet, e_input, e_graphics, e_log,
279 g_textures, g_gfx, g_sound, g_console, g_basic, g_options, g_main,
280 g_game, g_player, g_map, g_panel, g_items, g_weapons, g_phys, g_gui,
281 g_language, g_monsters, g_netmaster, utils, wadreader, MAPDEF;
283 const
284 NET_KEY_LEFT = 1;
285 NET_KEY_RIGHT = 2;
286 NET_KEY_UP = 4;
287 NET_KEY_DOWN = 8;
288 NET_KEY_JUMP = 16;
289 NET_KEY_FIRE = 32;
290 NET_KEY_OPEN = 64;
291 NET_KEY_NW = 256;
292 NET_KEY_PW = 512;
293 NET_KEY_CHAT = 2048;
294 NET_KEY_FORCEDIR = 4096;
296 //var
297 //kBytePrev: Word = 0;
298 //kDirPrev: TDirection = D_LEFT;
299 //HostGameTime: Word = 0;
302 function IsValidFileName(const S: String): Boolean;
303 const
304 Forbidden: set of Char = ['<', '>', '|', '"', ':', '*', '?'];
305 var
306 I: Integer;
307 begin
308 Result := S <> '';
309 for I := 1 to Length(S) do
310 Result := Result and (not(S[I] in Forbidden));
311 end;
313 function IsValidFilePath(const S: String): Boolean;
314 var
315 I: Integer;
316 begin
317 Result := False;
318 if not IsValidFileName(S) then exit;
319 if FileExists(S) then exit;
320 I := LastDelimiter('\/', S);
321 if (I > 0) then
322 if (not DirectoryExists(Copy(S, 1, I-1))) then
323 exit;
324 Result := True;
325 end;
328 // HOST MESSAGES //
331 // GAME
333 procedure MH_RECV_Chat(C: pTNetClient; var M: TMsg);
334 var
335 Txt: string;
336 Mode: Byte;
337 PID: Word;
338 Pl: TPlayer;
339 begin
340 PID := C^.Player;
341 Pl := g_Player_Get(PID);
343 Txt := M.ReadString();
344 Mode := M.ReadByte();
345 if (Mode = NET_CHAT_SYSTEM) then
346 Mode := NET_CHAT_PLAYER; // prevent sending system messages from clients
347 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
348 Mode := NET_CHAT_PLAYER; // revert to player chat in non-team game
350 if Pl = nil then
351 MH_SEND_Chat(Txt, Mode)
352 else
353 MH_SEND_Chat(Pl.Name + ': ' + Txt, Mode, IfThen(Mode = NET_CHAT_TEAM, Pl.Team, NET_EVERYONE));
354 end;
356 procedure MH_RECV_Info(C: pTNetClient; var M: TMsg);
357 var
358 Ver, PName, Model, Pw: string;
359 R, G, B, T: Byte;
360 PID: Word;
361 Color: TRGB;
362 I: Integer;
363 begin
364 Ver := M.ReadString();
365 Pw := M.ReadString();
366 PName := M.ReadString();
367 Model := M.ReadString();
368 R := M.ReadByte();
369 G := M.ReadByte();
370 B := M.ReadByte();
371 T := M.ReadByte();
373 if Ver <> GAME_VERSION then
374 begin
375 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
376 _lc[I_NET_DISC_VERSION]);
377 enet_peer_disconnect(C^.Peer, NET_DISC_VERSION);
378 Exit;
379 end;
381 if g_Net_IsHostBanned(C^.Peer^.address.host) then
382 begin
383 if g_Net_IsHostBanned(C^.Peer^.address.host, True) then
384 begin
385 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
386 _lc[I_NET_DISC_BAN]);
387 enet_peer_disconnect(C^.Peer, NET_DISC_BAN);
388 end
389 else
390 begin
391 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
392 _lc[I_NET_DISC_BAN]);
393 enet_peer_disconnect(C^.Peer, NET_DISC_TEMPBAN);
394 end;
395 Exit;
396 end;
398 if NetPassword <> '' then
399 if AnsiLowerCase(NetPassword) <> AnsiLowerCase(Pw) then
400 begin
401 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
402 _lc[I_NET_DISC_PASSWORD]);
403 enet_peer_disconnect(C^.Peer, NET_DISC_PASSWORD);
404 Exit;
405 end;
407 Color.R := R;
408 Color.B := B;
409 Color.G := G;
411 PID := g_Player_Create(Model, Color, T, False);
412 with g_Player_Get(PID) do
413 begin
414 Name := PName;
415 Reset(True);
416 end;
418 C^.Player := PID;
419 C^.WaitForFirstSpawn := false;
421 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
422 e_WriteLog('NET: Client ' + PName + ' [' + IntToStr(C^.ID) +
423 '] connected. Assigned player #' + IntToStr(PID) + '.', TMsgType.Notify);
425 MH_SEND_Info(C^.ID);
427 with g_Player_Get(PID) do
428 begin
429 Name := PName;
430 FClientID := C^.ID;
431 // round in progress, don't spawn
432 if (gGameSettings.MaxLives > 0) and (gLMSRespawn = LMS_RESPAWN_NONE) then
433 begin
434 Lives := 0;
435 FNoRespawn := True;
436 Spectate;
437 FWantsInGame := True; // TODO: look into this later
438 C^.WaitForFirstSpawn := true;
439 end
440 else
441 begin
442 e_LogWritefln('*** client #%u (cid #%u) authenticated...', [C.ID, C.Player]);
443 //e_LogWritefln('spawning player with pid #%u...', [PID]);
444 //Respawn(gGameSettings.GameType = GT_SINGLE);
445 //k8: no, do not spawn a player yet, wait for "request full state" packet
446 Lives := 0;
447 Spectate;
448 FNoRespawn := True;
449 // `FWantsInGame` seems to mean "spawn the player on the next occasion".
450 // that is, if we'll set it to `true`, the player can be spawned after
451 // warmup time ran out, for example, regardless of the real player state.
452 // also, this seems to work only for the initial connection. further
453 // map changes could initiate resource downloading, but the player will
454 // be spawned immediately.
455 // the proper solution will require another player state, "ephemeral".
456 // the player should start any map in "ephemeral" state, and turned into
457 // real mobj only when they sent a special "i am ready" packet. this packet
458 // must be sent after receiving the full state, so the player will get a full
459 // map view before going into game.
460 FWantsInGame := false;
461 C^.WaitForFirstSpawn := true;
462 end;
463 end;
465 //if not C^.WaitForFirstSpawn then
466 begin
467 for I := Low(NetClients) to High(NetClients) do
468 begin
469 if NetClients[I].ID = C^.ID then Continue;
470 MH_SEND_PlayerCreate(PID, NetClients[I].ID);
471 MH_SEND_PlayerPos(True, PID, NetClients[I].ID);
472 MH_SEND_PlayerStats(PID, NetClients[I].ID);
473 end;
474 end;
476 if gState in [STATE_INTERCUSTOM, STATE_FOLD] then
477 MH_SEND_GameEvent(NET_EV_MAPEND, 0, 'N', C^.ID);
479 if NetUseMaster then
480 begin
481 //g_Net_Slist_Update;
482 g_Net_Slist_Pulse();
483 end;
484 end;
487 procedure MH_ProcessFirstSpawn (C: pTNetClient);
488 var
489 plr: TPlayer;
490 begin
491 if not C.WaitForFirstSpawn then exit;
492 plr := g_Player_Get(C^.Player);
493 if not assigned(plr) then exit;
494 g_Net_Slist_ServerPlayerComes();
495 e_LogWritefln('*** client #%u (cid #%u) first spawn', [C.ID, C.Player]);
496 C.WaitForFirstSpawn := false;
497 plr.FNoRespawn := false;
498 plr.FWantsInGame := true; // TODO: look into this later
499 plr.Respawn(False);
500 end;
503 procedure MH_RECV_FullStateRequest(C: pTNetClient; var M: TMsg);
504 begin
505 //e_LogWritefln('*** client #%u (cid #%u) full state request', [C.ID, C.Player]);
506 if gGameOn then
507 begin
508 MH_SEND_Everything((C^.State = NET_STATE_AUTH), C^.ID)
509 end
510 else
511 begin
512 C^.RequestedFullUpdate := True;
513 end;
514 end;
516 // PLAYER
518 function MH_RECV_PlayerPos(C: pTNetClient; var M: TMsg): Word;
519 var
520 Dir, i: Byte;
521 WeaponSelect: Word;
522 PID: Word;
523 kByte: Word;
524 Pl: TPlayer;
525 GT: LongWord;
526 begin
527 Result := 0;
528 if not gGameOn then Exit;
530 GT := M.ReadLongWord();
531 PID := C^.Player;
532 Pl := g_Player_Get(PID);
533 if Pl = nil then
534 Exit;
536 if (GT > gTime + NET_MAX_DIFFTIME) or (GT < Pl.NetTime) then Exit;
538 with Pl do
539 begin
540 NetTime := GT;
541 kByte := M.ReadWord();
542 Dir := M.ReadByte();
543 WeaponSelect := M.ReadWord();
544 //e_WriteLog(Format('R:ws=%d', [WeaponSelect]), MSG_WARNING);
545 if Direction <> TDirection(Dir) then
546 JustTeleported := False;
548 SetDirection(TDirection(Dir));
549 ReleaseKeys;
551 if kByte = NET_KEY_CHAT then
552 begin
553 PressKey(KEY_CHAT, 10000);
554 Exit;
555 end;
557 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
558 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
559 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
560 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
561 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
562 if LongBool(kByte and NET_KEY_FIRE) then PressKey(KEY_FIRE, 10000);
563 if LongBool(kByte and NET_KEY_OPEN) then PressKey(KEY_OPEN, 10000);
564 if LongBool(kByte and NET_KEY_NW) then PressKey(KEY_NEXTWEAPON, 10000);
565 if LongBool(kByte and NET_KEY_PW) then PressKey(KEY_PREVWEAPON, 10000);
567 for i := 0 to 15 do
568 begin
569 if (WeaponSelect and Word(1 shl i)) <> 0 then
570 begin
571 //e_WriteLog(Format(' R:wn=%d', [i]), MSG_WARNING);
572 QueueWeaponSwitch(i);
573 end;
574 end;
575 end;
577 // MH_SEND_PlayerPos(False, PID, C^.ID);
578 end;
580 procedure MH_RECV_CheatRequest(C: pTNetClient; var M: TMsg);
581 var
582 CheatKind: Byte;
583 Pl: TPlayer;
584 begin
585 Pl := g_Player_Get(C^.Player);
586 if Pl = nil then Exit;
588 CheatKind := M.ReadByte();
590 case CheatKind of
591 NET_CHEAT_SUICIDE:
592 Pl.Damage(SUICIDE_DAMAGE, Pl.UID, 0, 0, HIT_SELF);
593 NET_CHEAT_SPECTATE:
594 begin
595 if Pl.FSpectator then
596 Pl.Respawn(False)
597 else
598 Pl.Spectate;
599 end;
600 NET_CHEAT_READY:
601 begin
602 if gState <> STATE_INTERCUSTOM then Exit;
603 Pl.FReady := not Pl.FReady;
604 if Pl.FReady then
605 begin
606 MH_SEND_GameEvent(NET_EV_INTER_READY, Pl.UID, 'Y');
607 Inc(gInterReadyCount);
608 end
609 else
610 begin
611 MH_SEND_GameEvent(NET_EV_INTER_READY, Pl.UID, 'N');
612 Dec(gInterReadyCount);
613 end;
614 end;
615 end;
616 end;
618 procedure MH_RECV_PlayerSettings(C: pTNetClient; var M: TMsg);
619 var
620 TmpName: string;
621 TmpModel: string;
622 TmpColor: TRGB;
623 TmpTeam: Byte;
624 Pl: TPlayer;
625 begin
626 TmpName := M.ReadString();
627 TmpModel := M.ReadString();
628 TmpColor.R := M.ReadByte();
629 TmpColor.G := M.ReadByte();
630 TmpColor.B := M.ReadByte();
631 TmpTeam := M.ReadByte();
633 Pl := g_Player_Get(C^.Player);
634 if Pl = nil then Exit;
636 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
637 Pl.SwitchTeam
638 else
639 Pl.SetColor(TmpColor);
641 if Pl.Name <> TmpName then
642 begin
643 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
644 Pl.Name := TmpName;
645 end;
647 if TmpModel <> Pl.Model.Name then
648 Pl.SetModel(TmpModel);
650 MH_SEND_PlayerSettings(Pl.UID, TmpModel);
651 end;
653 // RCON
655 procedure MH_RECV_RCONPassword(C: pTNetClient; var M: TMsg);
656 var
657 Pwd: string;
658 begin
659 Pwd := M.ReadString();
660 if not NetAllowRCON then Exit;
661 if Pwd = NetRCONPassword then
662 begin
663 C^.RCONAuth := True;
664 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWGOOD, 'N', C^.ID);
665 end
666 else
667 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWBAD, 'N', C^.ID);
668 end;
670 procedure MH_RECV_RCONCommand(C: pTNetClient; var M: TMsg);
671 var
672 Cmd: string;
673 begin
674 Cmd := M.ReadString();
675 if not NetAllowRCON then Exit;
676 if not C^.RCONAuth then
677 begin
678 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_NOAUTH, 'N', C^.ID);
679 Exit;
680 end;
681 g_Console_Process(Cmd);
682 end;
684 // MISC
686 procedure MH_RECV_Vote(C: pTNetClient; var M: TMsg);
687 var
688 Start: Boolean;
689 Name, Command: string;
690 Need: Integer;
691 Pl: TPlayer;
692 begin
693 Start := M.ReadByte() <> 0;
694 Command := M.ReadString();
696 Pl := g_Player_Get(C^.Player);
697 if Pl = nil then Exit;
698 Name := Pl.Name;
700 if Start then
701 begin
702 if not g_Console_CommandBlacklisted(Command) then
703 g_Game_StartVote(Command, Name);
704 end
705 else if gVoteInProgress then
706 begin
707 if (gPlayer1 <> nil) or (gPlayer2 <> nil) then
708 Need := Floor((NetClientCount+1)/2.0) + 1
709 else
710 Need := Floor(NetClientCount/2.0) + 1;
711 if C^.Voted then
712 begin
713 Dec(gVoteCount);
714 C^.Voted := False;
715 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_REVOKED], [Name, gVoteCount, Need]), True);
716 MH_SEND_VoteEvent(NET_VE_REVOKE, Name, 'a', gVoteCount, Need);
717 end
718 else
719 begin
720 Inc(gVoteCount);
721 C^.Voted := True;
722 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Name, gVoteCount, Need]), True);
723 MH_SEND_VoteEvent(NET_VE_VOTE, Name, 'a', gVoteCount, Need);
724 g_Game_CheckVote;
725 end;
726 end;
727 end;
729 // GAME (SEND)
731 procedure MH_SEND_Everything(CreatePlayers: Boolean {= False}; ID: Integer {= NET_EVERYONE});
733 function sendItemRespawn (it: PItem): Boolean;
734 begin
735 result := false; // don't stop
736 MH_SEND_ItemSpawn(True, it.myid, ID);
737 end;
739 function sendMonSpawn (mon: TMonster): Boolean;
740 begin
741 result := false; // don't stop
742 MH_SEND_MonsterSpawn(mon.UID, ID);
743 end;
745 function sendPanelState (pan: TPanel): Boolean;
746 begin
747 result := false; // don't stop
748 MH_SEND_PanelState(pan.guid, ID); // anyway, to sync mplats
749 if (pan.CanChangeTexture) then MH_SEND_PanelTexture(pan.guid, pan.LastAnimLoop, ID);
750 end;
752 var
753 I: Integer;
754 begin
755 if (ID >= 0) and (ID < length(NetClients)) then
756 begin
757 e_LogWritefln('*** client #%u (cid #%u) will get everything', [ID, NetClients[ID].Player]);
758 MH_ProcessFirstSpawn(@NetClients[ID]);
759 end;
761 if gPlayers <> nil then
762 begin
763 for I := Low(gPlayers) to High(gPlayers) do
764 begin
765 if gPlayers[I] <> nil then
766 begin
767 if CreatePlayers then MH_SEND_PlayerCreate(gPlayers[I].UID, ID);
768 MH_SEND_PlayerPos(True, gPlayers[I].UID, ID);
769 MH_SEND_PlayerStats(gPlayers[I].UID, ID);
771 if (gPlayers[I].Flag <> FLAG_NONE) and (gGameSettings.GameMode = GM_CTF) then
772 begin
773 MH_SEND_FlagEvent(FLAG_STATE_CAPTURED, gPlayers[I].Flag, gPlayers[I].UID, True, ID);
774 end;
775 end;
776 end;
777 end;
779 g_Items_ForEachAlive(sendItemRespawn, true); // backwards
780 g_Mons_ForEach(sendMonSpawn);
781 g_Map_ForEachPanel(sendPanelState);
783 if gTriggers <> nil then
784 begin
785 for I := Low(gTriggers) to High(gTriggers) do
786 begin
787 if gTriggers[I].TriggerType = TRIGGER_SOUND then
788 begin
789 MH_SEND_TriggerSound(gTriggers[I], ID);
790 end;
791 end;
792 end;
794 if Shots <> nil then
795 begin
796 for I := Low(Shots) to High(Shots) do
797 begin
798 if Shots[i].ShotType in [6, 7, 8] then
799 begin
800 MH_SEND_CreateShot(i, ID);
801 end;
802 end;
803 end;
805 MH_SEND_TriggerMusic(ID);
807 MH_SEND_GameStats(ID);
808 MH_SEND_CoopStats(ID);
810 if gGameSettings.GameMode = GM_CTF then
811 begin
812 if gFlags[FLAG_RED].State <> FLAG_STATE_CAPTURED then MH_SEND_FlagEvent(gFlags[FLAG_RED].State, FLAG_RED, 0, True, ID);
813 if gFlags[FLAG_BLUE].State <> FLAG_STATE_CAPTURED then MH_SEND_FlagEvent(gFlags[FLAG_BLUE].State, FLAG_BLUE, 0, True, ID);
814 end;
816 if CreatePlayers and (ID >= 0) then NetClients[ID].State := NET_STATE_GAME;
818 if gLMSRespawn > LMS_RESPAWN_NONE then
819 begin
820 e_LogWritefln('*** client #%u (cid #%u) WARMUP', [ID, NetClients[ID].Player]);
821 MH_SEND_GameEvent(NET_EV_LMS_WARMUP, (gLMSRespawnTime - gTime) div 1000, 'N', ID);
822 end;
824 g_Net_Flush();
825 end;
827 procedure MH_SEND_Info(ID: Byte);
828 begin
829 NetOut.Clear();
831 NetOut.Write(Byte(NET_MSG_INFO));
832 NetOut.Write(ID);
833 NetOut.Write(NetClients[ID].Player);
834 NetOut.Write(ExtractFileName(gGameSettings.WAD));
835 NetOut.Write(g_ExtractFileName(gMapInfo.Map));
836 NetOut.Write(gWADHash);
837 NetOut.Write(gGameSettings.GameMode);
838 NetOut.Write(gGameSettings.GoalLimit);
839 NetOut.Write(gGameSettings.TimeLimit);
840 NetOut.Write(gGameSettings.MaxLives);
841 NetOut.Write(gGameSettings.Options);
842 NetOut.Write(gTime);
844 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
845 end;
847 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
848 var
849 Name: string;
850 i: Integer;
851 Team: Byte;
852 begin
853 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
854 Mode := NET_CHAT_PLAYER;
856 Team := 0;
857 if (Mode = NET_CHAT_TEAM) then
858 begin
859 for i := Low(gPlayers) to High(gPlayers) do
860 if (gPlayers[i] <> nil) and (gPlayers[i].FClientID >= 0) and
861 (gPlayers[i].Team = ID) then
862 begin
863 NetOut.Write(Byte(NET_MSG_CHAT));
864 NetOut.Write(Txt);
865 NetOut.Write(Mode);
866 g_Net_Host_Send(gPlayers[i].FClientID, True, NET_CHAN_CHAT);
867 end;
868 Team := ID;
869 ID := NET_EVERYONE;
870 end
871 else
872 begin
873 NetOut.Write(Byte(NET_MSG_CHAT));
874 NetOut.Write(Txt);
875 NetOut.Write(Mode);
876 g_Net_Host_Send(ID, True, NET_CHAN_CHAT);
877 end;
879 if Mode = NET_CHAT_SYSTEM then
880 Exit;
882 if ID = NET_EVERYONE then
883 begin
884 if Mode = NET_CHAT_PLAYER then
885 begin
886 g_Console_Add(Txt, True);
887 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
888 g_Game_ChatSound(b_Text_Unformat(Txt));
889 end
890 else
891 if Mode = NET_CHAT_TEAM then
892 if gPlayer1 <> nil then
893 begin
894 if (gPlayer1.Team = TEAM_RED) and (Team = TEAM_RED) then
895 begin
896 g_Console_Add(#18'[Team] '#2 + Txt, True);
897 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
898 g_Game_ChatSound(b_Text_Unformat(Txt));
899 end
900 else if (gPlayer1.Team = TEAM_BLUE) and (Team = TEAM_BLUE) then
901 begin
902 g_Console_Add(#20'[Team] '#2 + Txt, True);
903 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
904 g_Game_ChatSound(b_Text_Unformat(Txt));
905 end;
906 end;
907 end
908 else
909 begin
910 Name := g_Net_ClientName_ByID(ID);
911 g_Console_Add('-> ' + Name + ': ' + Txt, True);
912 e_WriteLog('[Tell ' + Name + '] ' + b_Text_Unformat(Txt), TMsgType.Notify);
913 g_Game_ChatSound(b_Text_Unformat(Txt), False);
914 end;
915 end;
917 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
918 begin
919 NetOut.Write(Byte(NET_MSG_GFX));
920 NetOut.Write(Kind);
921 NetOut.Write(X);
922 NetOut.Write(Y);
923 NetOut.Write(Ang);
925 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
926 end;
928 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
929 begin
930 NetOut.Write(Byte(NET_MSG_SND));
931 NetOut.Write(Name);
932 if Pos then
933 begin
934 NetOut.Write(Byte(1));
935 NetOut.Write(X);
936 NetOut.Write(Y);
937 end
938 else
939 NetOut.Write(Byte(0));
941 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
942 end;
944 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
945 begin
946 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
948 NetOut.Write(Byte(NET_MSG_SHADD));
949 NetOut.Write(Proj);
950 NetOut.Write(Shots[Proj].ShotType);
951 NetOut.Write(Shots[Proj].Target);
952 NetOut.Write(Shots[Proj].SpawnerUID);
953 NetOut.Write(Shots[Proj].Timeout);
954 NetOut.Write(Shots[Proj].Obj.X);
955 NetOut.Write(Shots[Proj].Obj.Y);
956 NetOut.Write(Shots[Proj].Obj.Vel.X);
957 NetOut.Write(Shots[Proj].Obj.Vel.Y);
959 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
960 end;
962 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
963 begin
964 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
966 NetOut.Write(Byte(NET_MSG_SHPOS));
967 NetOut.Write(Proj);
968 NetOut.Write(Shots[Proj].Obj.X);
969 NetOut.Write(Shots[Proj].Obj.Y);
970 NetOut.Write(Shots[Proj].Obj.Vel.X);
971 NetOut.Write(Shots[Proj].Obj.Vel.Y);
973 g_Net_Host_Send(ID, False, NET_CHAN_SHOTS);
974 end;
976 procedure MH_Send_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
977 begin
978 NetOut.Write(Byte(NET_MSG_SHDEL));
979 NetOut.Write(Proj);
980 NetOut.Write(Byte(Loud));
981 NetOut.Write(X);
982 NetOut.Write(Y);
984 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
985 end;
987 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
988 begin
989 NetOut.Write(Byte(NET_MSG_SCORE));
990 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
991 begin
992 NetOut.Write(gTeamStat[TEAM_RED].Goals);
993 NetOut.Write(gTeamStat[TEAM_BLUE].Goals);
994 end
995 else
996 if gGameSettings.GameMode = GM_COOP then
997 begin
998 NetOut.Write(gCoopMonstersKilled);
999 NetOut.Write(gCoopSecretsFound);
1000 end;
1002 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1003 end;
1005 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
1006 begin
1007 NetOut.Write(Byte(NET_MSG_COOP));
1008 NetOut.Write(gTotalMonsters);
1009 NetOut.Write(gSecretsCount);
1010 NetOut.Write(gCoopTotalMonstersKilled);
1011 NetOut.Write(gCoopTotalSecretsFound);
1012 NetOut.Write(gCoopTotalMonsters);
1013 NetOut.Write(gCoopTotalSecrets);
1014 end;
1016 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
1017 begin
1018 NetOut.Write(Byte(NET_MSG_GEVENT));
1019 NetOut.Write(EvType);
1020 NetOut.Write(EvNum);
1021 NetOut.Write(EvStr);
1022 NetOut.Write(Byte(gLastMap));
1023 NetOut.Write(gTime);
1024 if (EvType = NET_EV_MAPSTART) and isWadPath(EvStr) then
1025 begin
1026 NetOut.Write(Byte(1));
1027 NetOut.Write(gWADHash);
1028 end else
1029 NetOut.Write(Byte(0));
1031 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
1032 end;
1034 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
1035 begin
1036 NetOut.Write(Byte(NET_MSG_FLAG));
1037 NetOut.Write(EvType);
1038 NetOut.Write(Flag);
1039 NetOut.Write(Byte(Quiet));
1040 NetOut.Write(PID);
1041 NetOut.Write(gFlags[Flag].State);
1042 NetOut.Write(gFlags[Flag].CaptureTime);
1043 NetOut.Write(gFlags[Flag].Obj.X);
1044 NetOut.Write(gFlags[Flag].Obj.Y);
1045 NetOut.Write(gFlags[Flag].Obj.Vel.X);
1046 NetOut.Write(gFlags[Flag].Obj.Vel.Y);
1048 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1049 end;
1051 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
1052 begin
1053 NetOut.Write(Byte(NET_MSG_GSET));
1054 NetOut.Write(gGameSettings.GameMode);
1055 NetOut.Write(gGameSettings.GoalLimit);
1056 NetOut.Write(gGameSettings.TimeLimit);
1057 NetOut.Write(gGameSettings.MaxLives);
1058 NetOut.Write(gGameSettings.Options);
1060 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1061 end;
1063 // PLAYER (SEND)
1065 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
1066 var
1067 P: TPlayer;
1068 begin
1069 P := g_Player_Get(PID);
1070 if P = nil then Exit;
1072 NetOut.Write(Byte(NET_MSG_PLR));
1073 NetOut.Write(PID);
1074 NetOut.Write(P.Name);
1076 NetOut.Write(P.FActualModelName);
1077 NetOut.Write(P.FColor.R);
1078 NetOut.Write(P.FColor.G);
1079 NetOut.Write(P.FColor.B);
1080 NetOut.Write(P.Team);
1082 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT)
1083 end;
1085 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
1086 var
1087 kByte: Word;
1088 Pl: TPlayer;
1089 begin
1090 Pl := g_Player_Get(PID);
1091 if Pl = nil then Exit;
1092 if Pl.FDummy then Exit;
1094 NetOut.Write(Byte(NET_MSG_PLRPOS));
1095 NetOut.Write(gTime);
1096 NetOut.Write(PID);
1098 kByte := 0;
1100 with Pl do
1101 begin
1102 NetOut.Write(FPing);
1103 NetOut.Write(FLoss);
1104 if IsKeyPressed(KEY_CHAT) then
1105 kByte := NET_KEY_CHAT
1106 else
1107 begin
1108 if IsKeyPressed(KEY_LEFT) then kByte := kByte or NET_KEY_LEFT;
1109 if IsKeyPressed(KEY_RIGHT) then kByte := kByte or NET_KEY_RIGHT;
1110 if IsKeyPressed(KEY_UP) then kByte := kByte or NET_KEY_UP;
1111 if IsKeyPressed(KEY_DOWN) then kByte := kByte or NET_KEY_DOWN;
1112 if IsKeyPressed(KEY_JUMP) then kByte := kByte or NET_KEY_JUMP;
1113 if JustTeleported then kByte := kByte or NET_KEY_FORCEDIR;
1114 end;
1116 NetOut.Write(kByte);
1117 if Direction = TDirection.D_LEFT then NetOut.Write(Byte(0)) else NetOut.Write(Byte(1));
1118 NetOut.Write(GameX);
1119 NetOut.Write(GameY);
1120 NetOut.Write(GameVelX);
1121 NetOut.Write(GameVelY);
1122 NetOut.Write(GameAccelX);
1123 NetOut.Write(GameAccelY);
1124 end;
1126 g_Net_Host_Send(ID, Reliable, NET_CHAN_PLAYERPOS);
1127 end;
1129 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
1130 var
1131 P: TPlayer;
1132 I: Integer;
1133 begin
1134 P := g_Player_Get(PID);
1135 if P = nil then Exit;
1137 NetOut.Write(Byte(NET_MSG_PLRSTA));
1138 NetOut.Write(PID);
1140 with P do
1141 begin
1142 NetOut.Write(Byte(alive));
1143 NetOut.Write(Byte(GodMode));
1144 NetOut.Write(Health);
1145 NetOut.Write(Armor);
1146 NetOut.Write(Air);
1147 NetOut.Write(JetFuel);
1148 NetOut.Write(Lives);
1149 NetOut.Write(Team);
1151 for I := WP_FIRST to WP_LAST do
1152 NetOut.Write(Byte(FWeapon[I]));
1154 for I := A_BULLETS to A_HIGH do
1155 NetOut.Write(FAmmo[I]);
1157 for I := A_BULLETS to A_HIGH do
1158 NetOut.Write(FMaxAmmo[I]);
1160 for I := MR_SUIT to MR_MAX do
1161 NetOut.Write(LongWord(FMegaRulez[I]));
1163 NetOut.Write(Byte(R_ITEM_BACKPACK in FRulez));
1164 NetOut.Write(Byte(R_KEY_RED in FRulez));
1165 NetOut.Write(Byte(R_KEY_GREEN in FRulez));
1166 NetOut.Write(Byte(R_KEY_BLUE in FRulez));
1167 NetOut.Write(Byte(R_BERSERK in FRulez));
1169 NetOut.Write(Frags);
1170 NetOut.Write(Death);
1172 NetOut.Write(CurrWeap);
1174 NetOut.Write(Byte(FSpectator));
1175 NetOut.Write(Byte(FGhost));
1176 NetOut.Write(Byte(FPhysics));
1177 NetOut.Write(Byte(FNoRespawn));
1178 NetOut.Write(Byte(FJetpack));
1179 NetOut.Write(FFireTime);
1180 NetOut.Write(Byte(FFlaming));
1181 NetOut.Write(FSpawnInvul);
1182 end;
1184 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1185 end;
1187 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
1188 begin
1189 NetOut.Write(Byte(NET_MSG_PLRDMG));
1190 NetOut.Write(PID);
1191 NetOut.Write(Kind);
1192 NetOut.Write(Attacker);
1193 NetOut.Write(Value);
1194 NetOut.Write(VX);
1195 NetOut.Write(VY);
1197 g_Net_Host_Send(ID, False, NET_CHAN_PLAYER);
1198 end;
1200 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
1201 begin
1202 NetOut.Write(Byte(NET_MSG_PLRDIE));
1203 NetOut.Write(PID);
1204 NetOut.Write(KillType);
1205 NetOut.Write(DeathType);
1206 NetOut.Write(Attacker);
1208 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1209 end;
1211 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
1212 begin
1213 NetOut.Write(Byte(NET_MSG_PLRFIRE));
1214 NetOut.Write(PID);
1215 NetOut.Write(Weapon);
1216 NetOut.Write(X);
1217 NetOut.Write(Y);
1218 NetOut.Write(AX);
1219 NetOut.Write(AY);
1220 NetOut.Write(ShotID);
1222 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
1223 end;
1225 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
1226 begin
1227 NetOut.Write(Byte(NET_MSG_PLRDEL));
1228 NetOut.Write(PID);
1230 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1231 end;
1233 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
1234 var
1235 Pl: TPlayer;
1236 begin
1237 Pl := g_Player_Get(PID);
1238 if Pl = nil then Exit;
1240 NetOut.Write(Byte(NET_MSG_PLRSET));
1241 NetOut.Write(PID);
1242 NetOut.Write(Pl.Name);
1243 if Mdl = '' then
1244 NetOut.Write(Pl.Model.Name)
1245 else
1246 NetOut.Write(Mdl);
1247 NetOut.Write(Pl.FColor.R);
1248 NetOut.Write(Pl.FColor.G);
1249 NetOut.Write(Pl.FColor.B);
1250 NetOut.Write(Pl.Team);
1252 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1253 end;
1255 // ITEM (SEND)
1257 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1258 var
1259 it: PItem;
1260 tt: Byte;
1261 begin
1262 it := g_Items_ByIdx(IID);
1264 NetOut.Write(Byte(NET_MSG_ISPAWN));
1265 NetOut.Write(IID);
1266 NetOut.Write(Byte(Quiet));
1267 tt := it.ItemType;
1268 if it.dropped then tt := tt or $80;
1269 NetOut.Write(tt);
1270 NetOut.Write(Byte(it.Fall));
1271 NetOut.Write(Byte(it.Respawnable));
1272 NetOut.Write(it.Obj.X);
1273 NetOut.Write(it.Obj.Y);
1274 NetOut.Write(it.Obj.Vel.X);
1275 NetOut.Write(it.Obj.Vel.Y);
1277 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1278 end;
1280 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1281 begin
1282 NetOut.Write(Byte(NET_MSG_IDEL));
1283 NetOut.Write(IID);
1284 NetOut.Write(Byte(Quiet));
1286 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1287 end;
1289 // PANEL
1291 procedure MH_SEND_PanelTexture(PGUID: Integer; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
1292 var
1293 TP: TPanel;
1294 begin
1295 TP := g_Map_PanelByGUID(PGUID);
1296 if (TP = nil) then exit;
1298 with TP do
1299 begin
1300 NetOut.Write(Byte(NET_MSG_PTEX));
1301 NetOut.Write(LongWord(PGUID));
1302 NetOut.Write(FCurTexture);
1303 NetOut.Write(FCurFrame);
1304 NetOut.Write(FCurFrameCount);
1305 NetOut.Write(AnimLoop);
1306 end;
1308 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1309 end;
1311 procedure MH_SEND_PanelState(PGUID: Integer; ID: Integer = NET_EVERYONE);
1312 var
1313 TP: TPanel;
1314 mpflags: Byte = 0;
1315 begin
1316 TP := g_Map_PanelByGUID(PGUID);
1317 if (TP = nil) then exit;
1319 NetOut.Write(Byte(NET_MSG_PSTATE));
1320 NetOut.Write(LongWord(PGUID));
1321 NetOut.Write(Byte(TP.Enabled));
1322 NetOut.Write(TP.LiftType);
1323 NetOut.Write(TP.X);
1324 NetOut.Write(TP.Y);
1325 NetOut.Write(Word(TP.Width));
1326 NetOut.Write(Word(TP.Height));
1327 // mplats
1328 NetOut.Write(LongInt(TP.movingSpeedX));
1329 NetOut.Write(LongInt(TP.movingSpeedY));
1330 NetOut.Write(LongInt(TP.movingStartX));
1331 NetOut.Write(LongInt(TP.movingStartY));
1332 NetOut.Write(LongInt(TP.movingEndX));
1333 NetOut.Write(LongInt(TP.movingEndY));
1334 NetOut.Write(LongInt(TP.sizeSpeedX));
1335 NetOut.Write(LongInt(TP.sizeSpeedY));
1336 NetOut.Write(LongInt(TP.sizeEndX));
1337 NetOut.Write(LongInt(TP.sizeEndY));
1338 if TP.movingActive then mpflags := mpflags or 1;
1339 if TP.moveOnce then mpflags := mpflags or 2;
1340 NetOut.Write(Byte(mpflags));
1342 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1343 end;
1345 // TRIGGER
1347 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
1348 begin
1349 if gTriggers = nil then Exit;
1350 if T.Sound = nil then Exit;
1352 NetOut.Write(Byte(NET_MSG_TSOUND));
1353 NetOut.Write(T.ClientID);
1354 NetOut.Write(Byte(T.Sound.IsPlaying));
1355 NetOut.Write(LongWord(T.Sound.GetPosition));
1356 NetOut.Write(T.SoundPlayCount);
1358 g_Net_Host_Send(ID, True);
1359 end;
1361 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
1362 begin
1363 NetOut.Write(Byte(NET_MSG_TMUSIC));
1364 NetOut.Write(gMusic.Name);
1365 NetOut.Write(Byte(gMusic.IsPlaying));
1366 NetOut.Write(LongWord(gMusic.GetPosition));
1367 NetOut.Write(Byte(gMusic.SpecPause or gMusic.IsPaused));
1369 g_Net_Host_Send(ID, True);
1370 end;
1372 // MONSTER
1374 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
1375 var
1376 M: TMonster;
1377 begin
1378 M := g_Monsters_ByUID(UID);
1379 if M = nil then
1380 Exit;
1382 with M do
1383 begin
1384 NetOut.Write(Byte(NET_MSG_MSPAWN));
1385 NetOut.Write(UID);
1386 NetOut.Write(MonsterType);
1387 NetOut.Write(MonsterState);
1388 NetOut.Write(MonsterAnim);
1389 NetOut.Write(MonsterTargetUID);
1390 NetOut.Write(MonsterTargetTime);
1391 NetOut.Write(MonsterBehaviour);
1392 NetOut.Write(MonsterSleep);
1393 NetOut.Write(MonsterHealth);
1394 NetOut.Write(MonsterAmmo);
1395 NetOut.Write(GameX);
1396 NetOut.Write(GameY);
1397 NetOut.Write(GameVelX);
1398 NetOut.Write(GameVelY);
1399 NetOut.Write(Byte(GameDirection));
1400 end;
1402 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1403 end;
1405 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
1406 var
1407 M: TMonster;
1408 begin
1409 M := g_Monsters_ByUID(UID);
1410 if M = nil then Exit;
1412 NetOut.Write(Byte(NET_MSG_MPOS));
1413 NetOut.Write(UID);
1415 with M do
1416 begin
1417 NetOut.Write(GameX);
1418 NetOut.Write(GameY);
1419 NetOut.Write(GameVelX);
1420 NetOut.Write(GameVelY);
1421 NetOut.Write(Byte(GameDirection));
1422 end;
1424 g_Net_Host_Send(ID, False, NET_CHAN_MONSTERPOS);
1425 end;
1427 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
1428 var
1429 M: TMonster;
1430 begin
1431 M := g_Monsters_ByUID(UID);
1432 if M = nil then Exit;
1434 NetOut.Write(Byte(NET_MSG_MSTATE));
1435 NetOut.Write(UID);
1437 with M do
1438 begin
1439 NetOut.Write(MonsterState);
1440 NetOut.Write(ForcedAnim);
1441 NetOut.Write(MonsterTargetUID);
1442 NetOut.Write(MonsterTargetTime);
1443 NetOut.Write(MonsterSleep);
1444 NetOut.Write(MonsterHealth);
1445 NetOut.Write(MonsterAmmo);
1446 NetOut.Write(MonsterPain);
1447 NetOut.Write(Byte(AnimIsReverse));
1448 NetOut.Write(FFireTime);
1449 end;
1451 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1452 end;
1454 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
1455 begin
1456 NetOut.Write(Byte(NET_MSG_MSHOT));
1457 NetOut.Write(UID);
1458 NetOut.Write(X);
1459 NetOut.Write(Y);
1460 NetOut.Write(VX);
1461 NetOut.Write(VY);
1463 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1464 end;
1466 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
1467 var
1468 M: TMonster;
1469 begin
1470 M := g_Monsters_ByUID(UID);
1471 if M = nil then Exit;
1473 NetOut.Write(Byte(NET_MSG_MDEL));
1474 NetOut.Write(UID);
1476 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1477 end;
1479 // MISC
1481 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
1482 begin
1483 NetOut.Write(Byte(NET_MSG_TIME_SYNC));
1484 NetOut.Write(Time);
1486 g_Net_Host_Send(ID, False, NET_CHAN_SERVICE);
1487 end;
1489 procedure MH_SEND_VoteEvent(EvType: Byte;
1490 StrArg1: string = 'a'; StrArg2: string = 'b';
1491 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
1492 ID: Integer = NET_EVERYONE);
1493 begin
1494 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
1495 NetOut.Write(EvType);
1496 NetOut.Write(IntArg1);
1497 NetOut.Write(IntArg2);
1498 NetOut.Write(StrArg1);
1499 NetOut.Write(StrArg2);
1501 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1502 end;
1504 // CLIENT MESSAGES //
1506 // GAME
1508 procedure MC_RECV_Chat(var M: TMsg);
1509 var
1510 Txt: string;
1511 Mode: Byte;
1512 begin
1513 Txt := M.ReadString();
1514 Mode := M.ReadByte();
1516 if Mode <> NET_CHAT_SYSTEM then
1517 begin
1518 if Mode = NET_CHAT_PLAYER then
1519 begin
1520 g_Console_Add(Txt, True);
1521 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1522 g_Game_ChatSound(b_Text_Unformat(Txt));
1523 end else
1524 if (Mode = NET_CHAT_TEAM) and (gPlayer1 <> nil) then
1525 begin
1526 if gPlayer1.Team = TEAM_RED then
1527 g_Console_Add(b_Text_Format('\r[Team] ') + Txt, True);
1528 if gPlayer1.Team = TEAM_BLUE then
1529 g_Console_Add(b_Text_Format('\b[Team] ') + Txt, True);
1530 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1531 g_Game_ChatSound(b_Text_Unformat(Txt));
1532 end;
1533 end else
1534 g_Console_Add(Txt, True);
1535 end;
1537 procedure MC_RECV_Effect(var M: TMsg);
1538 var
1539 Kind: Byte;
1540 X, Y: Integer;
1541 Ang: SmallInt;
1542 Anim: TAnimation;
1543 ID: LongWord;
1544 begin
1545 if not gGameOn then Exit;
1546 Kind := M.ReadByte();
1547 X := M.ReadLongInt();
1548 Y := M.ReadLongInt();
1549 Ang := M.ReadSmallInt();
1551 case Kind of
1552 NET_GFX_SPARK:
1553 g_GFX_Spark(X, Y, 2 + Random(2), Ang, 0, 0);
1555 NET_GFX_TELE:
1556 begin
1557 if g_Frames_Get(ID, 'FRAMES_TELEPORT') then
1558 begin
1559 Anim := TAnimation.Create(ID, False, 3);
1560 g_GFX_OnceAnim(X, Y, Anim);
1561 Anim.Free();
1562 end;
1563 if Ang = 1 then
1564 g_Sound_PlayExAt('SOUND_GAME_TELEPORT', X, Y);
1565 end;
1567 NET_GFX_EXPLODE:
1568 begin
1569 if g_Frames_Get(ID, 'FRAMES_EXPLODE_ROCKET') then
1570 begin
1571 Anim := TAnimation.Create(ID, False, 6);
1572 Anim.Blending := False;
1573 g_GFX_OnceAnim(X-64, Y-64, Anim);
1574 Anim.Free();
1575 end;
1576 if Ang = 1 then
1577 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEROCKET', X, Y);
1578 end;
1580 NET_GFX_BFGEXPL:
1581 begin
1582 if g_Frames_Get(ID, 'FRAMES_EXPLODE_BFG') then
1583 begin
1584 Anim := TAnimation.Create(ID, False, 6);
1585 Anim.Blending := False;
1586 g_GFX_OnceAnim(X-64, Y-64, Anim);
1587 Anim.Free();
1588 end;
1589 if Ang = 1 then
1590 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEBFG', X, Y);
1591 end;
1593 NET_GFX_BFGHIT:
1594 begin
1595 if g_Frames_Get(ID, 'FRAMES_BFGHIT') then
1596 begin
1597 Anim := TAnimation.Create(ID, False, 4);
1598 g_GFX_OnceAnim(X-32, Y-32, Anim);
1599 Anim.Free();
1600 end;
1601 end;
1603 NET_GFX_FIRE:
1604 begin
1605 if g_Frames_Get(ID, 'FRAMES_FIRE') then
1606 begin
1607 Anim := TAnimation.Create(ID, False, 4);
1608 g_GFX_OnceAnim(X, Y, Anim);
1609 Anim.Free();
1610 end;
1611 if Ang = 1 then
1612 g_Sound_PlayExAt('SOUND_FIRE', X, Y);
1613 end;
1615 NET_GFX_RESPAWN:
1616 begin
1617 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
1618 begin
1619 Anim := TAnimation.Create(ID, False, 4);
1620 g_GFX_OnceAnim(X, Y, Anim);
1621 Anim.Free();
1622 end;
1623 if Ang = 1 then
1624 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
1625 end;
1627 NET_GFX_SHELL1:
1628 g_Player_CreateShell(X, Y, 0, -2, SHELL_BULLET);
1630 NET_GFX_SHELL2:
1631 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1633 NET_GFX_SHELL3:
1634 begin
1635 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1636 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1637 end;
1638 end;
1639 end;
1641 procedure MC_RECV_Sound(var M: TMsg);
1642 var
1643 Name: string;
1644 X, Y: Integer;
1645 Pos: Boolean;
1646 begin
1647 Name := M.ReadString();
1648 Pos := M.ReadByte() <> 0;
1649 if Pos then
1650 begin
1651 X := M.ReadLongInt();
1652 Y := M.ReadLongInt();
1653 g_Sound_PlayExAt(Name, X, Y);
1654 end
1655 else
1656 g_Sound_PlayEx(Name);
1657 end;
1659 procedure MC_RECV_CreateShot(var M: TMsg);
1660 var
1661 I, X, Y, XV, YV: Integer;
1662 Timeout: LongWord;
1663 Target, Spawner: Word;
1664 ShType: Byte;
1665 begin
1666 I := M.ReadLongInt();
1667 ShType := M.ReadByte();
1668 Target := M.ReadWord();
1669 Spawner := M.ReadWord();
1670 Timeout := M.ReadLongWord();
1671 X := M.ReadLongInt();
1672 Y := M.ReadLongInt();
1673 XV := M.ReadLongInt();
1674 YV := M.ReadLongInt();
1676 I := g_Weapon_CreateShot(I, ShType, Spawner, Target, X, Y, XV, YV);
1677 if (Shots <> nil) and (I <= High(Shots)) then
1678 begin
1679 Shots[I].Timeout := Timeout;
1680 //Shots[I].Target := Target; // TODO: find a use for Target later
1681 end;
1682 end;
1684 procedure MC_RECV_UpdateShot(var M: TMsg);
1685 var
1686 I, TX, TY, TXV, TYV: Integer;
1687 begin
1688 I := M.ReadLongInt();
1689 TX := M.ReadLongInt();
1690 TY := M.ReadLongInt();
1691 TXV := M.ReadLongInt();
1692 TYV := M.ReadLongInt();
1694 if (Shots <> nil) and (I <= High(Shots)) then
1695 with (Shots[i]) do
1696 begin
1697 Obj.X := TX;
1698 Obj.Y := TY;
1699 Obj.Vel.X := TXV;
1700 Obj.Vel.Y := TYV;
1701 end;
1702 end;
1704 procedure MC_RECV_DeleteShot(var M: TMsg);
1705 var
1706 I, X, Y: Integer;
1707 L: Boolean;
1708 begin
1709 if not gGameOn then Exit;
1710 I := M.ReadLongInt();
1711 L := (M.ReadByte() <> 0);
1712 X := M.ReadLongInt();
1713 Y := M.ReadLongInt();
1715 g_Weapon_DestroyShot(I, X, Y, L);
1716 end;
1718 procedure MC_RECV_GameStats(var M: TMsg);
1719 begin
1720 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
1721 begin
1722 gTeamStat[TEAM_RED].Goals := M.ReadSmallInt();
1723 gTeamStat[TEAM_BLUE].Goals := M.ReadSmallInt();
1724 end
1725 else
1726 if gGameSettings.GameMode = GM_COOP then
1727 begin
1728 gCoopMonstersKilled := M.ReadWord();
1729 gCoopSecretsFound := M.ReadWord();
1730 end;
1731 end;
1733 procedure MC_RECV_CoopStats(var M: TMsg);
1734 begin
1735 gTotalMonsters := M.ReadLongInt();
1736 gSecretsCount := M.ReadLongInt();
1737 gCoopTotalMonstersKilled := M.ReadWord();
1738 gCoopTotalSecretsFound := M.ReadWord();
1739 gCoopTotalMonsters := M.ReadWord();
1740 gCoopTotalSecrets := M.ReadWord();
1741 end;
1743 procedure MC_RECV_GameEvent(var M: TMsg);
1744 var
1745 EvType: Byte;
1746 EvNum: Integer;
1747 EvStr: string;
1748 EvTime: LongWord;
1749 BHash: Boolean;
1750 EvHash: TMD5Digest;
1751 pl: TPlayer;
1752 i1, i2: TStrings_Locale;
1753 pln: String;
1754 cnt: Byte;
1755 goodCmd: Boolean = true;
1756 begin
1757 FillChar(EvHash, Sizeof(EvHash), 0);
1758 EvType := M.ReadByte();
1759 EvNum := M.ReadLongInt();
1760 EvStr := M.ReadString();
1761 gLastMap := M.ReadByte() <> 0;
1762 if gLastMap and (gGameSettings.GameMode = GM_COOP) then gStatsOff := True;
1763 gStatsPressed := True;
1764 EvTime := M.ReadLongWord();
1765 BHash := M.ReadByte() <> 0;
1766 if BHash then
1767 EvHash := M.ReadMD5();
1769 gTime := EvTime;
1771 if (g_Res_received_map_start <> 0) then
1772 begin
1773 if (g_Res_received_map_start < 0) then exit;
1774 goodCmd := false;
1775 case EvType of
1776 NET_EV_MAPSTART: goodCmd := true;
1777 NET_EV_MAPEND: goodCmd := true;
1778 NET_EV_PLAYER_KICK: goodCmd := true;
1779 NET_EV_PLAYER_BAN: goodCmd := true;
1780 end;
1781 if not goodCmd then exit;
1782 end;
1784 case EvType of
1785 NET_EV_MAPSTART:
1786 begin
1787 if (g_Res_received_map_start <> 0) then
1788 begin
1789 g_Res_received_map_start := -1;
1790 end
1791 else
1792 begin
1793 gGameOn := False;
1794 g_Game_ClearLoading();
1795 g_Game_StopAllSounds(True);
1797 gSwitchGameMode := Byte(EvNum);
1798 gGameSettings.GameMode := gSwitchGameMode;
1800 gWADHash := EvHash;
1801 if not g_Game_StartMap(false{asMegawad}, EvStr, True) then
1802 begin
1803 if not isWadPath(EvStr) then
1804 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [gGameSettings.WAD + ':\' + EvStr]))
1805 else
1806 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [EvStr]));
1807 Exit;
1808 end;
1810 MC_SEND_FullStateRequest;
1811 end;
1812 end;
1814 NET_EV_MAPEND:
1815 begin
1816 if (g_Res_received_map_start <> 0) then
1817 begin
1818 g_Res_received_map_start := -1;
1819 end
1820 else
1821 begin
1822 gMissionFailed := EvNum <> 0;
1823 gExit := EXIT_ENDLEVELCUSTOM;
1824 end;
1825 end;
1827 NET_EV_RCON:
1828 begin
1829 case EvNum of
1830 NET_RCON_NOAUTH:
1831 g_Console_Add(_lc[I_NET_RCON_NOAUTH], True);
1832 NET_RCON_PWGOOD:
1833 g_Console_Add(_lc[I_NET_RCON_PWD_VALID], True);
1834 NET_RCON_PWBAD:
1835 g_Console_Add(_lc[I_NET_RCON_PWD_INVALID], True);
1836 end;
1837 end;
1839 NET_EV_CHANGE_TEAM:
1840 begin
1841 if EvNum = TEAM_RED then
1842 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [EvStr]), True);
1843 if EvNum = TEAM_BLUE then
1844 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [EvStr]), True);
1845 end;
1847 NET_EV_PLAYER_KICK:
1848 begin
1849 g_Console_Add(Format(_lc[I_PLAYER_KICK], [EvStr]), True);
1850 if (g_Res_received_map_start <> 0) then g_Res_received_map_start := -1;
1851 end;
1853 NET_EV_PLAYER_BAN:
1854 begin
1855 g_Console_Add(Format(_lc[I_PLAYER_BAN], [EvStr]), True);
1856 if (g_Res_received_map_start <> 0) then g_Res_received_map_start := -1;
1857 end;
1859 NET_EV_LMS_WARMUP:
1860 g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [EvNum]), True);
1862 NET_EV_LMS_SURVIVOR:
1863 g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True);
1865 NET_EV_BIGTEXT:
1866 g_Game_Message(AnsiUpperCase(EvStr), Word(EvNum));
1868 NET_EV_SCORE:
1869 begin
1870 pl := g_Player_Get(EvNum and $FFFF);
1871 if pl = nil then
1872 pln := '?'
1873 else
1874 pln := pl.Name;
1875 cnt := (EvNum shr 16) and $FF;
1876 if Pos('w', EvStr) = 0 then
1877 begin
1878 // Default score
1879 if Pos('t', EvStr) = 0 then
1880 begin
1881 // Player +/- score
1882 if Pos('-', EvStr) = 0 then
1883 begin
1884 if Pos('e', EvStr) = 0 then
1885 i1 := I_PLAYER_SCORE_ADD_OWN
1886 else
1887 i1 := I_PLAYER_SCORE_ADD_ENEMY;
1888 end else
1889 begin
1890 if Pos('e', EvStr) = 0 then
1891 i1 := I_PLAYER_SCORE_SUB_OWN
1892 else
1893 i1 := I_PLAYER_SCORE_SUB_ENEMY;
1894 end;
1895 // Which team
1896 if Pos('r', EvStr) > 0 then
1897 i2 := I_PLAYER_SCORE_TO_RED
1898 else
1899 i2 := I_PLAYER_SCORE_TO_BLUE;
1900 g_Console_Add(Format(_lc[i1], [pln, cnt, _lc[i2]]), True);
1901 end else
1902 begin
1903 // Team +/- score
1904 if Pos('-', EvStr) = 0 then
1905 i1 := I_PLAYER_SCORE_ADD_TEAM
1906 else
1907 i1 := I_PLAYER_SCORE_SUB_TEAM;
1908 // Which team
1909 if Pos('r', EvStr) > 0 then
1910 i2 := I_PLAYER_SCORE_RED
1911 else
1912 i2 := I_PLAYER_SCORE_BLUE;
1913 g_Console_Add(Format(_lc[i1], [_lc[i2], cnt]), True);
1914 end;
1915 end else
1916 begin
1917 // Game Win
1918 if Pos('e', EvStr) = 0 then
1919 i1 := I_PLAYER_SCORE_WIN_OWN
1920 else
1921 i1 := I_PLAYER_SCORE_WIN_ENEMY;
1922 // Which team
1923 if Pos('r', EvStr) > 0 then
1924 i2 := I_PLAYER_SCORE_TO_RED
1925 else
1926 i2 := I_PLAYER_SCORE_TO_BLUE;
1927 g_Console_Add(Format(_lc[i1], [pln, _lc[i2]]), True);
1928 end;
1929 end;
1931 NET_EV_SCORE_MSG:
1932 begin
1933 if EvNum = TEAM_RED then
1934 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1935 if EvNum = TEAM_BLUE then
1936 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1937 if EvNum = -TEAM_RED then
1938 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1939 if EvNum = -TEAM_BLUE then
1940 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1941 end;
1943 NET_EV_LMS_START:
1944 begin
1945 g_Player_RemoveAllCorpses;
1946 g_Game_Message(_lc[I_MESSAGE_LMS_START], 144);
1947 end;
1949 NET_EV_LMS_WIN:
1950 g_Game_Message(Format(_lc[I_MESSAGE_LMS_WIN], [AnsiUpperCase(EvStr)]), 144);
1952 NET_EV_TLMS_WIN:
1953 begin
1954 if EvNum = TEAM_RED then
1955 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 144);
1956 if EvNum = TEAM_BLUE then
1957 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 144);
1958 end;
1960 NET_EV_LMS_LOSE:
1961 g_Game_Message(_lc[I_MESSAGE_LMS_LOSE], 144);
1963 NET_EV_LMS_DRAW:
1964 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
1966 NET_EV_KILLCOMBO:
1967 g_Game_Announce_KillCombo(EvNum);
1969 NET_EV_PLAYER_TOUCH:
1970 begin
1971 pl := g_Player_Get(EvNum);
1972 if pl <> nil then
1973 pl.Touch();
1974 end;
1976 NET_EV_SECRET:
1977 begin
1978 pl := g_Player_Get(EvNum);
1979 if pl <> nil then
1980 begin
1981 g_Console_Add(Format(_lc[I_PLAYER_SECRET], [pl.Name]), True);
1982 g_Sound_PlayEx('SOUND_GAME_SECRET');
1983 end;
1984 end;
1986 NET_EV_INTER_READY:
1987 begin
1988 pl := g_Player_Get(EvNum);
1989 if pl <> nil then pl.FReady := (EvStr = 'Y');
1990 end;
1991 end;
1992 end;
1994 procedure MC_RECV_FlagEvent(var M: TMsg);
1995 var
1996 PID: Word;
1997 Pl: TPlayer;
1998 EvType: Byte;
1999 Fl, a: Byte;
2000 Quiet: Boolean;
2001 s, ts: string;
2002 begin
2003 EvType := M.ReadByte();
2004 Fl := M.ReadByte();
2006 if Fl = FLAG_NONE then Exit;
2008 Quiet := (M.ReadByte() <> 0);
2009 PID := M.ReadWord();
2011 gFlags[Fl].State := M.ReadByte();
2012 gFlags[Fl].CaptureTime := M.ReadLongWord();
2013 gFlags[Fl].Obj.X := M.ReadLongInt();
2014 gFlags[Fl].Obj.Y := M.ReadLongInt();
2015 gFlags[Fl].Obj.Vel.X := M.ReadLongInt();
2016 gFlags[Fl].Obj.Vel.Y := M.ReadLongInt();
2018 Pl := g_Player_Get(PID);
2019 if (Pl = nil) and
2020 (EvType <> FLAG_STATE_NORMAL) and
2021 (EvType <> FLAG_STATE_DROPPED) and
2022 (EvType <> FLAG_STATE_RETURNED) then
2023 Exit;
2025 case EvType of
2026 FLAG_STATE_NORMAL:
2027 begin
2028 if Quiet or (Pl = nil) then Exit;
2030 if Fl = FLAG_RED then
2031 s := _lc[I_PLAYER_FLAG_RED]
2032 else
2033 s := _lc[I_PLAYER_FLAG_BLUE];
2035 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2037 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2038 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2039 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2040 a := 0
2041 else
2042 a := 1;
2044 if not sound_ret_flag[a].IsPlaying() then
2045 sound_ret_flag[a].Play();
2046 end;
2048 FLAG_STATE_CAPTURED:
2049 begin
2050 if (Pl <> nil) then Pl.SetFlag(Fl);
2052 if Quiet then Exit;
2054 if Fl = FLAG_RED then
2055 s := _lc[I_PLAYER_FLAG_RED]
2056 else
2057 s := _lc[I_PLAYER_FLAG_BLUE];
2059 g_Console_Add(Format(_lc[I_PLAYER_FLAG_GET], [Pl.Name, s]), True);
2060 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_GET], [AnsiUpperCase(s)]), 144);
2062 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2063 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2064 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2065 a := 0
2066 else
2067 a := 1;
2069 if not sound_get_flag[a].IsPlaying() then
2070 sound_get_flag[a].Play();
2071 end;
2073 FLAG_STATE_DROPPED:
2074 begin
2075 if (Pl <> nil) then Pl.SetFlag(FLAG_NONE);
2077 if Quiet or (Pl = nil) then Exit;
2079 if Fl = FLAG_RED then
2080 s := _lc[I_PLAYER_FLAG_RED]
2081 else
2082 s := _lc[I_PLAYER_FLAG_BLUE];
2084 g_Console_Add(Format(_lc[I_PLAYER_FLAG_DROP], [Pl.Name, s]), True);
2085 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_DROP], [AnsiUpperCase(s)]), 144);
2087 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2088 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2089 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2090 a := 0
2091 else
2092 a := 1;
2094 if not sound_lost_flag[a].IsPlaying() then
2095 sound_lost_flag[a].Play();
2096 end;
2098 FLAG_STATE_SCORED:
2099 begin
2100 g_Map_ResetFlag(FLAG_RED);
2101 g_Map_ResetFlag(FLAG_BLUE);
2102 if Quiet or (Pl = nil) then Exit;
2103 Pl.SetFlag(FLAG_NONE);
2105 if Fl = FLAG_RED then
2106 s := _lc[I_PLAYER_FLAG_RED]
2107 else
2108 s := _lc[I_PLAYER_FLAG_BLUE];
2110 ts := Format('%.4d', [gFlags[Fl].CaptureTime]);
2111 Insert('.', ts, Length(ts) + 1 - 3);
2112 g_Console_Add(Format(_lc[I_PLAYER_FLAG_CAPTURE], [Pl.Name, s, ts]), True);
2113 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_CAPTURE], [AnsiUpperCase(s)]), 144);
2115 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2116 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2117 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2118 a := 0
2119 else
2120 a := 1;
2122 if not sound_cap_flag[a].IsPlaying() then
2123 sound_cap_flag[a].Play();
2124 end;
2126 FLAG_STATE_RETURNED:
2127 begin
2128 g_Map_ResetFlag(Fl);
2129 if Quiet then Exit;
2131 if Fl = FLAG_RED then
2132 s := _lc[I_PLAYER_FLAG_RED]
2133 else
2134 s := _lc[I_PLAYER_FLAG_BLUE];
2136 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2138 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2139 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2140 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2141 a := 0
2142 else
2143 a := 1;
2145 if not sound_ret_flag[a].IsPlaying() then
2146 sound_ret_flag[a].Play();
2147 end;
2148 end;
2149 end;
2151 procedure MC_RECV_GameSettings(var M: TMsg);
2152 begin
2153 gGameSettings.GameMode := M.ReadByte();
2154 gGameSettings.GoalLimit := M.ReadWord();
2155 gGameSettings.TimeLimit := M.ReadWord();
2156 gGameSettings.MaxLives := M.ReadByte();
2157 gGameSettings.Options := M.ReadLongWord();
2158 end;
2160 // PLAYER
2162 function MC_RECV_PlayerCreate(var M: TMsg): Word;
2163 var
2164 PID, DID: Word;
2165 PName, Model: string;
2166 Color: TRGB;
2167 T: Byte;
2168 Pl: TPlayer;
2169 begin
2170 PID := M.ReadWord();
2171 Pl := g_Player_Get(PID);
2173 PName := M.ReadString();
2174 Model := M.ReadString();
2175 Color.R := M.ReadByte();
2176 Color.G := M.ReadByte();
2177 Color.B := M.ReadByte();
2178 T := M.ReadByte();
2180 Result := 0;
2181 if (PID <> NetPlrUID1) and (PID <> NetPlrUID2) then
2182 begin
2183 if (Pl <> nil) then Exit;
2184 DID := g_Player_Create(Model, Color, T, False);
2185 with g_Player_Get(DID) do
2186 begin
2187 UID := PID;
2188 Name := PName;
2189 Reset(True);
2190 end;
2191 end
2192 else
2193 begin
2194 if (PID = NetPlrUID1) and (gPlayer1 <> nil) then begin
2195 gPlayer1.UID := PID;
2196 gPlayer1.Model.SetColor(Color.R, Color.G, Color.B);
2197 gPlayer1.ChangeTeam(T);
2198 end;
2199 if (PID = NetPlrUID2) and (gPlayer2 <> nil) then begin
2200 gPlayer2.UID := PID;
2201 gPlayer2.Model.SetColor(Color.R, Color.G, Color.B);
2202 gPlayer2.ChangeTeam(T);
2203 end;
2204 end;
2206 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
2207 e_WriteLog('NET: Player ' + PName + ' [' + IntToStr(PID) + '] added.', TMsgType.Notify);
2208 Result := PID;
2209 end;
2211 function MC_RECV_PlayerPos(var M: TMsg): Word;
2212 var
2213 GT: LongWord;
2214 PID: Word;
2215 kByte: Word;
2216 Pl: TPlayer;
2217 Dir: Byte;
2218 TmpX, TmpY: Integer;
2219 begin
2220 Result := 0;
2222 GT := M.ReadLongWord();
2223 if GT < gTime - NET_MAX_DIFFTIME then
2224 begin
2225 gTime := GT;
2226 Exit;
2227 end;
2228 gTime := GT;
2230 PID := M.ReadWord();
2231 Pl := g_Player_Get(PID);
2233 if Pl = nil then Exit;
2235 Result := PID;
2237 with Pl do
2238 begin
2239 FPing := M.ReadWord();
2240 FLoss := M.ReadByte();
2241 kByte := M.ReadWord();
2242 Dir := M.ReadByte();
2244 TmpX := M.ReadLongInt();
2245 TmpY := M.ReadLongInt();
2247 ReleaseKeys;
2249 if (kByte = NET_KEY_CHAT) then
2250 PressKey(KEY_CHAT, 10000)
2251 else
2252 begin
2253 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
2254 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
2255 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
2256 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
2257 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
2258 end;
2260 if ((Pl <> gPlayer1) and (Pl <> gPlayer2)) or LongBool(kByte and NET_KEY_FORCEDIR) then
2261 SetDirection(TDirection(Dir));
2263 GameVelX := M.ReadLongInt();
2264 GameVelY := M.ReadLongInt();
2265 GameAccelX := M.ReadLongInt();
2266 GameAccelY := M.ReadLongInt();
2267 SetLerp(TmpX, TmpY);
2268 if NetForcePlayerUpdate then Update();
2269 end;
2270 end;
2272 function MC_RECV_PlayerStats(var M: TMsg): Word;
2273 var
2274 PID: Word;
2275 Pl: TPlayer;
2276 I, OldFire: Integer;
2277 OldJet, Flam: Boolean;
2278 NewTeam: Byte;
2279 begin
2280 PID := M.ReadWord();
2281 Pl := g_Player_Get(PID);
2282 Result := 0;
2283 if Pl = nil then
2284 Exit;
2286 with Pl do
2287 begin
2288 alive := (M.ReadByte() <> 0);
2289 GodMode := (M.ReadByte() <> 0);
2290 Health := M.ReadLongInt();
2291 Armor := M.ReadLongInt();
2292 Air := M.ReadLongInt();
2293 JetFuel := M.ReadLongInt();
2294 Lives := M.ReadByte();
2295 NewTeam := M.ReadByte();
2297 for I := WP_FIRST to WP_LAST do
2298 FWeapon[I] := (M.ReadByte() <> 0);
2300 for I := A_BULLETS to A_HIGH do
2301 FAmmo[I] := M.ReadWord();
2303 for I := A_BULLETS to A_HIGH do
2304 FMaxAmmo[I] := M.ReadWord();
2306 for I := MR_SUIT to MR_MAX do
2307 FMegaRulez[I] := M.ReadLongWord();
2309 FRulez := [];
2310 if (M.ReadByte() <> 0) then
2311 FRulez := FRulez + [R_ITEM_BACKPACK];
2312 if (M.ReadByte() <> 0) then
2313 FRulez := FRulez + [R_KEY_RED];
2314 if (M.ReadByte() <> 0) then
2315 FRulez := FRulez + [R_KEY_GREEN];
2316 if (M.ReadByte() <> 0) then
2317 FRulez := FRulez + [R_KEY_BLUE];
2318 if (M.ReadByte() <> 0) then
2319 FRulez := FRulez + [R_BERSERK];
2321 Frags := M.ReadLongInt();
2322 Death := M.ReadLongInt();
2324 SetWeapon(M.ReadByte());
2326 FSpectator := M.ReadByte() <> 0;
2327 if FSpectator then
2328 begin
2329 if Pl = gPlayer1 then
2330 begin
2331 gLMSPID1 := UID;
2332 gPlayer1 := nil;
2333 end;
2334 if Pl = gPlayer2 then
2335 begin
2336 gLMSPID2 := UID;
2337 gPlayer2 := nil;
2338 end;
2339 end
2340 else
2341 begin
2342 if (gPlayer1 = nil) and (gLMSPID1 > 0) then
2343 gPlayer1 := g_Player_Get(gLMSPID1);
2344 if (gPlayer2 = nil) and (gLMSPID2 > 0) then
2345 gPlayer2 := g_Player_Get(gLMSPID2);
2346 end;
2347 FGhost := M.ReadByte() <> 0;
2348 FPhysics := M.ReadByte() <> 0;
2349 FNoRespawn := M.ReadByte() <> 0;
2350 OldJet := FJetpack;
2351 FJetpack := M.ReadByte() <> 0;
2352 OldFire := FFireTime;
2353 FFireTime := M.ReadLongInt();
2354 if (OldFire <= 0) and (FFireTime > 0) then
2355 g_Sound_PlayExAt('SOUND_IGNITE', Obj.X, Obj.Y);
2356 Flam := M.ReadByte() <> 0;
2357 FSpawnInvul := M.ReadLongInt();
2358 if OldJet and not FJetpack then
2359 JetpackOff
2360 else if not OldJet and FJetpack then
2361 JetpackOn;
2362 if FFlaming and not Flam then
2363 FlamerOff;
2364 if Team <> NewTeam then
2365 Pl.ChangeTeam(NewTeam);
2366 end;
2368 Result := PID;
2369 end;
2371 function MC_RECV_PlayerDamage(var M: TMsg): Word;
2372 var
2373 PID: Word;
2374 Pl: TPlayer;
2375 Kind: Byte;
2376 Attacker, Value: Word;
2377 VX, VY: Integer;
2378 begin
2379 Result := 0;
2380 if not gGameOn then Exit;
2381 PID := M.ReadWord();
2382 Pl := g_Player_Get(PID);
2383 if Pl = nil then Exit;
2385 Kind := M.ReadByte();
2386 Attacker := M.ReadWord();
2387 Value := M.ReadWord();
2388 VX := M.ReadWord();
2389 VY := M.ReadWord();
2391 with Pl do
2392 Damage(Value, Attacker, VX, VY, Kind);
2394 Result := PID;
2395 end;
2397 function MC_RECV_PlayerDeath(var M: TMsg): Word;
2398 var
2399 PID: Word;
2400 Pl: TPlayer;
2401 KillType, DeathType: Byte;
2402 Attacker: Word;
2403 begin
2404 Result := 0;
2405 if not gGameOn then Exit;
2406 PID := M.ReadWord();
2407 Pl := g_Player_Get(PID);
2408 if Pl = nil then Exit;
2410 KillType := M.ReadByte();
2411 DeathType := M.ReadByte();
2412 Attacker := M.ReadWord();
2414 with Pl do
2415 begin
2416 Kill(KillType, Attacker, DeathType);
2417 SoftReset;
2418 end;
2419 end;
2421 function MC_RECV_PlayerDelete(var M: TMsg): Word;
2422 var
2423 PID: Word;
2424 Pl: TPlayer;
2425 begin
2426 PID := M.ReadWord();
2427 Pl := g_Player_Get(PID);
2428 Result := 0;
2429 if Pl = nil then Exit;
2431 g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [Pl.Name]), True);
2432 e_WriteLog('NET: Player ' + Pl.Name + ' [' + IntToStr(PID) + '] removed.', TMsgType.Notify);
2434 g_Player_Remove(PID);
2436 Result := PID;
2437 end;
2439 function MC_RECV_PlayerFire(var M: TMsg): Word;
2440 var
2441 PID: Word;
2442 Weap: Byte;
2443 Pl: TPlayer;
2444 X, Y, AX, AY: Integer;
2445 SHID: Integer;
2446 begin
2447 Result := 0;
2448 if not gGameOn then Exit;
2449 PID := M.ReadWord();
2450 Pl := g_Player_Get(PID);
2451 if Pl = nil then Exit;
2453 Weap := M.ReadByte();
2454 X := M.ReadLongInt();
2455 Y := M.ReadLongInt();
2456 AX := M.ReadLongInt();
2457 AY := M.ReadLongInt();
2458 SHID := M.ReadLongInt();
2460 with Pl do
2461 if alive then NetFire(Weap, X, Y, AX, AY, SHID);
2462 end;
2464 procedure MC_RECV_PlayerSettings(var M: TMsg);
2465 var
2466 TmpName: string;
2467 TmpModel: string;
2468 TmpColor: TRGB;
2469 TmpTeam: Byte;
2470 Pl: TPlayer;
2471 PID: Word;
2472 begin
2473 PID := M.ReadWord();
2474 Pl := g_Player_Get(PID);
2475 if Pl = nil then Exit;
2477 TmpName := M.ReadString();
2478 TmpModel := M.ReadString();
2479 TmpColor.R := M.ReadByte();
2480 TmpColor.G := M.ReadByte();
2481 TmpColor.B := M.ReadByte();
2482 TmpTeam := M.ReadByte();
2484 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
2485 begin
2486 Pl.ChangeTeam(TmpTeam);
2487 if gPlayer1 = Pl then
2488 gPlayer1Settings.Team := TmpTeam;
2489 if gPlayer2 = Pl then
2490 gPlayer2Settings.Team := TmpTeam;
2491 end else
2492 Pl.SetColor(TmpColor);
2494 if Pl.Name <> TmpName then
2495 begin
2496 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
2497 Pl.Name := TmpName;
2498 end;
2500 if TmpModel <> Pl.Model.Name then
2501 Pl.SetModel(TmpModel);
2502 end;
2504 // ITEM
2506 procedure MC_RECV_ItemSpawn(var M: TMsg);
2507 var
2508 ID: Word;
2509 AID: DWord;
2510 X, Y, VX, VY: Integer;
2511 T: Byte;
2512 Quiet, Fall{, Resp}: Boolean;
2513 Anim: TAnimation;
2514 it: PItem;
2515 begin
2516 if not gGameOn then Exit;
2517 ID := M.ReadWord();
2518 Quiet := M.ReadByte() <> 0;
2519 T := M.ReadByte();
2520 Fall := M.ReadByte() <> 0;
2521 {Resp :=} M.ReadByte();
2522 X := M.ReadLongInt();
2523 Y := M.ReadLongInt();
2524 VX := M.ReadLongInt();
2525 VY := M.ReadLongInt();
2527 g_Items_Create(X, Y, T and $7F, Fall, False, False, ID);
2528 if ((T and $80) <> 0) then g_Items_SetDrop(ID);
2530 it := g_Items_ByIdx(ID);
2531 it.Obj.Vel.X := VX;
2532 it.Obj.Vel.Y := VY;
2534 if not Quiet then
2535 begin
2536 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
2537 if g_Frames_Get(AID, 'FRAMES_ITEM_RESPAWN') then
2538 begin
2539 Anim := TAnimation.Create(AID, False, 4);
2540 g_GFX_OnceAnim(X+(it.Obj.Rect.Width div 2)-16, Y+(it.Obj.Rect.Height div 2)-16, Anim);
2541 Anim.Free();
2542 end;
2543 end;
2544 end;
2546 procedure MC_RECV_ItemDestroy(var M: TMsg);
2547 var
2548 ID: Word;
2549 Quiet: Boolean;
2550 begin
2551 if not gGameOn then Exit;
2552 ID := M.ReadWord();
2553 Quiet := M.ReadByte() <> 0;
2555 if not g_Items_ValidId(ID) then exit;
2557 if not Quiet then g_Items_EmitPickupSound(ID);
2559 g_Items_Remove(ID);
2560 end;
2562 // PANEL
2564 procedure MC_RECV_PanelTexture(var M: TMsg);
2565 var
2566 TP: TPanel;
2567 PGUID: Integer;
2568 Tex, Fr: Integer;
2569 Loop, Cnt: Byte;
2570 begin
2571 if not gGameOn then Exit;
2573 PGUID := Integer(M.ReadLongWord());
2574 Tex := M.ReadLongInt();
2575 Fr := M.ReadLongInt();
2576 Cnt := M.ReadByte();
2577 Loop := M.ReadByte();
2579 TP := g_Map_PanelByGUID(PGUID);
2580 if (TP <> nil) then
2581 begin
2582 // switch texture
2583 TP.SetTexture(Tex, Loop);
2584 TP.SetFrame(Fr, Cnt);
2585 end;
2586 end;
2588 procedure MC_RECV_PanelState(var M: TMsg);
2589 var
2590 PGUID: Integer;
2591 E: Boolean;
2592 Lift: Byte;
2593 X, Y, W, H: Integer;
2594 TP: TPanel;
2595 speedX, speedY, startX, startY, endX, endY: Integer;
2596 sizeSpX, sizeSpY, sizeEX, sizeEY: Integer;
2597 mpflags: Byte;
2598 begin
2599 if not gGameOn then Exit;
2601 PGUID := Integer(M.ReadLongWord());
2602 E := (M.ReadByte() <> 0);
2603 Lift := M.ReadByte();
2604 X := M.ReadLongInt();
2605 Y := M.ReadLongInt();
2606 W := M.ReadWord();
2607 H := M.ReadWord();
2608 // mplats
2609 speedX := M.ReadLongInt();
2610 speedY := M.ReadLongInt();
2611 startX := M.ReadLongInt();
2612 startY := M.ReadLongInt();
2613 endX := M.ReadLongInt();
2614 endY := M.ReadLongInt();
2615 sizeSpX := M.ReadLongInt();
2616 sizeSpY := M.ReadLongInt();
2617 sizeEX := M.ReadLongInt();
2618 sizeEY := M.ReadLongInt();
2619 mpflags := M.ReadByte(); // bit0: TP.movingActive; bit1: TP.moveOnce
2621 TP := g_Map_PanelByGUID(PGUID);
2622 if (TP = nil) then exit;
2624 // update lifts state
2625 if TP.isGLift then g_Map_SetLiftGUID(PGUID, Lift);
2627 // update enabled/disabled state for all panels
2628 if E then g_Map_EnableWallGUID(PGUID) else g_Map_DisableWallGUID(PGUID);
2630 // update panel position, as it can be moved (mplat)
2631 TP.X := X;
2632 TP.Y := Y;
2633 TP.Width := W;
2634 TP.Height := H;
2635 // update mplat state
2636 TP.movingSpeedX := speedX;
2637 TP.movingSpeedY := speedY;
2638 TP.movingStartX := startX;
2639 TP.movingStartY := startY;
2640 TP.movingEndX := endX;
2641 TP.movingEndY := endY;
2642 TP.sizeSpeedX := sizeSpX;
2643 TP.sizeSpeedY := sizeSpY;
2644 TP.sizeEndX := sizeEX;
2645 TP.sizeEndY := sizeEY;
2646 TP.movingActive := ((mpflags and 1) <> 0);
2647 TP.moveOnce := ((mpflags and 2) <> 0);
2648 // notify panel of it's position/size change, so it can fix other internal structures
2649 TP.positionChanged();
2650 end;
2652 // TRIGGERS
2654 procedure MC_RECV_TriggerSound(var M: TMsg);
2655 var
2656 SPlaying: Boolean;
2657 SPos, SID: LongWord;
2658 SCount: LongInt;
2659 I: Integer;
2660 begin
2661 if not gGameOn then Exit;
2662 if gTriggers = nil then Exit;
2664 SID := M.ReadLongWord();
2665 SPlaying := M.ReadByte() <> 0;
2666 SPos := M.ReadLongWord();
2667 SCount := M.ReadLongInt();
2669 for I := Low(gTriggers) to High(gTriggers) do
2670 if gTriggers[I].TriggerType = TRIGGER_SOUND then
2671 if gTriggers[I].ClientID = SID then
2672 with gTriggers[I] do
2673 begin
2674 if Sound <> nil then
2675 begin
2676 if SPlaying then
2677 begin
2678 if tgcLocal then
2679 Sound.PlayVolumeAt(X+(Width div 2), Y+(Height div 2), tgcVolume/255.0)
2680 else
2681 Sound.PlayPanVolume((tgcPan-127.0)/128.0, tgcVolume/255.0);
2682 Sound.SetPosition(SPos);
2683 end
2684 else
2685 if Sound.IsPlaying then Sound.Stop;
2686 end;
2688 SoundPlayCount := SCount;
2689 end;
2690 end;
2692 procedure MC_RECV_TriggerMusic(var M: TMsg);
2693 var
2694 MName: string;
2695 MPlaying: Boolean;
2696 MPos: LongWord;
2697 MPaused: Boolean;
2698 begin
2699 if not gGameOn then Exit;
2701 MName := M.ReadString();
2702 MPlaying := M.ReadByte() <> 0;
2703 MPos := M.ReadLongWord();
2704 MPaused := M.ReadByte() <> 0;
2705 MPos := MPos+1; //k8: stfu, fpc!
2707 if MPlaying then
2708 begin
2709 gMusic.SetByName(MName);
2710 gMusic.Play(True);
2711 // gMusic.SetPosition(MPos);
2712 gMusic.SpecPause := MPaused;
2713 end
2714 else
2715 if gMusic.IsPlaying then gMusic.Stop;
2716 end;
2718 // MONSTERS
2720 procedure MC_RECV_MonsterSpawn(var M: TMsg);
2721 var
2722 ID: Word;
2723 MType, MState, MDir, MAnim, MBehav: Byte;
2724 X, Y, VX, VY, MTargTime, MHealth, MAmmo, MSleep: Integer;
2725 MTarg: Word;
2726 Mon: TMonster;
2727 begin
2728 ID := M.ReadWord();
2729 Mon := g_Monsters_ByUID(ID);
2730 if Mon <> nil then
2731 Exit;
2733 MType := M.ReadByte();
2734 MState := M.ReadByte();
2735 MAnim := M.ReadByte();
2736 MTarg := M.ReadWord();
2737 MTargTime := M.ReadLongInt();
2738 MBehav := M.ReadByte();
2739 MSleep := M.ReadLongInt();
2740 MHealth := M.ReadLongInt();
2741 MAmmo := M.ReadLongInt();
2743 X := M.ReadLongInt();
2744 Y := M.ReadLongInt();
2745 VX := M.ReadLongInt();
2746 VY := M.ReadLongInt();
2747 MDir := M.ReadByte();
2749 g_Monsters_Create(MType, X, Y, TDirection(MDir), False, ID);
2750 Mon := g_Monsters_ByUID(ID);
2751 if Mon = nil then
2752 Exit;
2754 with Mon do
2755 begin
2757 MonsterAnim := MAnim;
2758 MonsterTargetUID := MTarg;
2759 MonsterTargetTime := MTargTime;
2760 MonsterBehaviour := MBehav;
2761 MonsterSleep := MSleep;
2762 MonsterAmmo := MAmmo;
2763 SetHealth(MHealth);
2765 SetState(MState);
2767 setPosition(X, Y); // this will call positionChanged();
2768 GameVelX := VX;
2769 GameVelY := VY;
2770 end;
2771 end;
2773 procedure MC_RECV_MonsterPos(var M: TMsg);
2774 var
2775 Mon: TMonster;
2776 ID: Word;
2777 X, Y: Integer;
2778 begin
2779 ID := M.ReadWord();
2780 Mon := g_Monsters_ByUID(ID);
2781 if Mon = nil then
2782 Exit;
2784 with Mon do
2785 begin
2786 X := M.ReadLongInt();
2787 Y := M.ReadLongInt();
2788 Mon.setPosition(X, Y); // this will call `positionChanged()`
2789 GameVelX := M.ReadLongInt();
2790 GameVelY := M.ReadLongInt();
2791 GameDirection := TDirection(M.ReadByte());
2792 end;
2793 end;
2795 procedure MC_RECV_MonsterState(var M: TMsg);
2796 var
2797 ID, OldFire: Integer;
2798 MState, MFAnm: Byte;
2799 Mon: TMonster;
2800 AnimRevert: Boolean;
2801 begin
2802 ID := M.ReadWord();
2803 Mon := g_Monsters_ByUID(ID);
2804 if Mon = nil then Exit;
2806 MState := M.ReadByte();
2807 MFAnm := M.ReadByte();
2809 with Mon do
2810 begin
2811 MonsterTargetUID := M.ReadWord();
2812 MonsterTargetTime := M.ReadLongInt();
2813 MonsterSleep := M.ReadLongInt();
2814 MonsterHealth := M.ReadLongInt();
2815 MonsterAmmo := M.ReadLongInt();
2816 MonsterPain := M.ReadLongInt();
2817 AnimRevert := M.ReadByte() <> 0;
2818 OldFire := FFireTime;
2819 FFireTime := M.ReadLongInt();
2820 if (OldFire <= 0) and (FFireTime > 0) then
2821 g_Sound_PlayExAt('SOUND_IGNITE', Obj.X, Obj.Y);
2822 RevertAnim(AnimRevert);
2824 if MonsterState <> MState then
2825 begin
2826 if (MState = MONSTATE_GO) and (MonsterState = MONSTATE_SLEEP) then WakeUpSound();
2827 if (MState = MONSTATE_DIE) then DieSound();
2828 if (MState = MONSTATE_PAIN) then MakeBloodSimple(Min(200, MonsterPain));
2829 if (MState = MONSTATE_ATTACK) then kick(nil);
2830 if (MState = MONSTATE_DEAD) then SetDeadAnim();
2832 SetState(MState, MFAnm);
2833 end;
2834 end;
2835 end;
2837 procedure MC_RECV_MonsterShot(var M: TMsg);
2838 var
2839 ID: Integer;
2840 Mon: TMonster;
2841 X, Y, VX, VY: Integer;
2842 begin
2843 ID := M.ReadWord();
2845 Mon := g_Monsters_ByUID(ID);
2846 if Mon = nil then Exit;
2848 X := M.ReadLongInt();
2849 Y := M.ReadLongInt();
2850 VX := M.ReadLongInt();
2851 VY := M.ReadLongInt();
2853 Mon.ClientAttack(X, Y, VX, VY);
2854 end;
2856 procedure MC_RECV_MonsterDelete(var M: TMsg);
2857 var
2858 ID: Integer;
2859 Mon: TMonster;
2860 begin
2861 ID := M.ReadWord();
2862 Mon := g_Monsters_ByUID(ID);
2863 if Mon = nil then Exit;
2864 Mon.SetState(5);
2865 Mon.MonsterRemoved := True;
2866 end;
2868 procedure MC_RECV_TimeSync(var M: TMsg);
2869 var
2870 Time: LongWord;
2871 begin
2872 Time := M.ReadLongWord();
2874 if gState = STATE_INTERCUSTOM then
2875 gServInterTime := Min(Time, 255);
2876 end;
2878 procedure MC_RECV_VoteEvent(var M: TMsg);
2879 var
2880 EvID: Byte;
2881 Str1, Str2: string;
2882 Int1, Int2: SmallInt;
2883 begin
2884 EvID := M.ReadByte();
2885 Int1 := M.ReadSmallInt();
2886 Int2 := M.ReadSmallInt();
2887 Str1 := M.ReadString();
2888 Str2 := M.ReadString();
2890 case EvID of
2891 NET_VE_STARTED:
2892 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_STARTED], [Str1, Str2, Int1]), True);
2893 NET_VE_PASSED:
2894 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_PASSED], [Str1]), True);
2895 NET_VE_FAILED:
2896 g_Console_Add(_lc[I_MESSAGE_VOTE_FAILED], True);
2897 NET_VE_VOTE:
2898 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Str1, Int1, Int2]), True);
2899 NET_VE_INPROGRESS:
2900 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_INPROGRESS], [Str1]), True);
2901 end;
2902 end;
2904 // CLIENT SEND
2906 procedure MC_SEND_Info(Password: string);
2907 begin
2908 NetOut.Clear();
2910 NetOut.Write(Byte(NET_MSG_INFO));
2911 NetOut.Write(GAME_VERSION);
2912 NetOut.Write(Password);
2913 NetOut.Write(gPlayer1Settings.Name);
2914 NetOut.Write(gPlayer1Settings.Model);
2915 NetOut.Write(gPlayer1Settings.Color.R);
2916 NetOut.Write(gPlayer1Settings.Color.G);
2917 NetOut.Write(gPlayer1Settings.Color.B);
2918 NetOut.Write(gPlayer1Settings.Team);
2920 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2921 end;
2923 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
2924 begin
2925 NetOut.Write(Byte(NET_MSG_CHAT));
2926 NetOut.Write(Txt);
2927 NetOut.Write(Mode);
2929 g_Net_Client_Send(True, NET_CHAN_CHAT);
2930 end;
2932 procedure MC_SEND_PlayerPos();
2933 var
2934 kByte: Word;
2935 Predict: Boolean;
2936 strafeDir: Byte;
2937 WeaponSelect: Word = 0;
2938 i: Integer;
2939 begin
2940 if not gGameOn then Exit;
2941 if gPlayers = nil then Exit;
2942 if gPlayer1 = nil then Exit;
2944 kByte := 0;
2945 Predict := NetPredictSelf; // and (not NetGotKeys);
2947 if (not gConsoleShow) and (not gChatShow) and (g_ActiveWindow = nil) then
2948 begin
2949 strafeDir := P1MoveButton shr 4;
2950 P1MoveButton := P1MoveButton and $0F;
2952 if gPlayerAction[0, ACTION_MOVELEFT] and (not gPlayerAction[0, ACTION_MOVERIGHT]) then
2953 P1MoveButton := 1
2954 else if (not gPlayerAction[0, ACTION_MOVELEFT]) and gPlayerAction[0, ACTION_MOVERIGHT] then
2955 P1MoveButton := 2
2956 else if (not gPlayerAction[0, ACTION_MOVELEFT]) and (not gPlayerAction[0, ACTION_MOVERIGHT]) then
2957 P1MoveButton := 0;
2959 // strafing
2960 if gPlayerAction[0, ACTION_STRAFE] then
2961 begin
2962 // new strafe mechanics
2963 if (strafeDir = 0) then
2964 strafeDir := P1MoveButton; // start strafing
2965 // now set direction according to strafe (reversed)
2966 if (strafeDir = 2) then
2967 gPlayer1.SetDirection(TDirection.D_LEFT)
2968 else if (strafeDir = 1) then
2969 gPlayer1.SetDirection(TDirection.D_RIGHT)
2970 end
2971 else
2972 begin
2973 strafeDir := 0; // not strafing anymore
2974 if (P1MoveButton = 2) and gPlayerAction[0, ACTION_MOVELEFT] then
2975 gPlayer1.SetDirection(TDirection.D_LEFT)
2976 else if (P1MoveButton = 1) and gPlayerAction[0, ACTION_MOVERIGHT] then
2977 gPlayer1.SetDirection(TDirection.D_RIGHT)
2978 else if P1MoveButton <> 0 then
2979 gPlayer1.SetDirection(TDirection(P1MoveButton-1));
2980 end;
2982 gPlayer1.ReleaseKeys;
2983 if P1MoveButton = 1 then
2984 begin
2985 kByte := kByte or NET_KEY_LEFT;
2986 if Predict then gPlayer1.PressKey(KEY_LEFT, 10000);
2987 end;
2988 if P1MoveButton = 2 then
2989 begin
2990 kByte := kByte or NET_KEY_RIGHT;
2991 if Predict then gPlayer1.PressKey(KEY_RIGHT, 10000);
2992 end;
2993 if gPlayerAction[0, ACTION_LOOKUP] then
2994 begin
2995 kByte := kByte or NET_KEY_UP;
2996 gPlayer1.PressKey(KEY_UP, 10000);
2997 end;
2998 if gPlayerAction[0, ACTION_LOOKDOWN] then
2999 begin
3000 kByte := kByte or NET_KEY_DOWN;
3001 gPlayer1.PressKey(KEY_DOWN, 10000);
3002 end;
3003 if gPlayerAction[0, ACTION_JUMP] then
3004 begin
3005 kByte := kByte or NET_KEY_JUMP;
3006 // gPlayer1.PressKey(KEY_JUMP, 10000); // TODO: Make a prediction option
3007 end;
3008 if gPlayerAction[0, ACTION_ATTACK] then kByte := kByte or NET_KEY_FIRE;
3009 if gPlayerAction[0, ACTION_ACTIVATE] then kByte := kByte or NET_KEY_OPEN;
3010 if gPlayerAction[0, ACTION_WEAPNEXT] then kByte := kByte or NET_KEY_NW;
3011 if gPlayerAction[0, ACTION_WEAPPREV] then kByte := kByte or NET_KEY_PW;
3013 gPlayerAction[0, ACTION_WEAPNEXT] := False; // HACK, remove after readyweaon&pendinweapon implementation
3014 gPlayerAction[0, ACTION_WEAPPREV] := False; // HACK, remove after readyweaon&pendinweapon implementation
3016 for i := WP_FIRST to WP_LAST do
3017 begin
3018 if gSelectWeapon[0, i] then
3019 begin
3020 WeaponSelect := WeaponSelect or Word(1 shl i);
3021 gSelectWeapon[0, i] := False
3022 end
3023 end;
3025 // fix movebutton state
3026 P1MoveButton := P1MoveButton or (strafeDir shl 4);
3027 end
3028 else
3029 kByte := NET_KEY_CHAT;
3031 NetOut.Write(Byte(NET_MSG_PLRPOS));
3032 NetOut.Write(gTime);
3033 NetOut.Write(kByte);
3034 NetOut.Write(Byte(gPlayer1.Direction));
3035 NetOut.Write(WeaponSelect);
3036 //e_WriteLog(Format('S:ws=%d', [WeaponSelect]), MSG_WARNING);
3037 g_Net_Client_Send(True, NET_CHAN_PLAYERPOS);
3039 //kBytePrev := kByte;
3040 //kDirPrev := gPlayer1.Direction;
3041 end;
3043 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
3044 begin
3045 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
3046 NetOut.Write(Byte(Start));
3047 NetOut.Write(Command);
3048 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3049 end;
3051 procedure MC_SEND_PlayerSettings();
3052 begin
3053 NetOut.Write(Byte(NET_MSG_PLRSET));
3054 NetOut.Write(gPlayer1Settings.Name);
3055 NetOut.Write(gPlayer1Settings.Model);
3056 NetOut.Write(gPlayer1Settings.Color.R);
3057 NetOut.Write(gPlayer1Settings.Color.G);
3058 NetOut.Write(gPlayer1Settings.Color.B);
3059 NetOut.Write(gPlayer1Settings.Team);
3061 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3062 end;
3064 procedure MC_SEND_FullStateRequest();
3065 begin
3066 NetOut.Write(Byte(NET_MSG_REQFST));
3068 g_Net_Client_Send(True, NET_CHAN_SERVICE);
3069 end;
3071 procedure MC_SEND_CheatRequest(Kind: Byte);
3072 begin
3073 NetOut.Write(Byte(NET_MSG_CHEAT));
3074 NetOut.Write(Kind);
3076 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3077 end;
3078 procedure MC_SEND_RCONPassword(Password: string);
3079 begin
3080 NetOut.Write(Byte(NET_MSG_RCON_AUTH));
3081 NetOut.Write(Password);
3083 g_Net_Client_Send(True, NET_CHAN_SERVICE);
3084 end;
3085 procedure MC_SEND_RCONCommand(Cmd: string);
3086 begin
3087 NetOut.Write(Byte(NET_MSG_RCON_CMD));
3088 NetOut.Write(Cmd);
3090 g_Net_Client_Send(True, NET_CHAN_SERVICE);
3091 end;
3094 end.