DEADSOFTWARE

net: started master-server communication rewrite (phase 1: master i/o moved to separa...
[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 e_LogWritefln('*** client #%u (cid #%u) first spawn', [C.ID, C.Player]);
495 C.WaitForFirstSpawn := false;
496 plr.FNoRespawn := false;
497 plr.FWantsInGame := true; // TODO: look into this later
498 plr.Respawn(False);
499 end;
502 procedure MH_RECV_FullStateRequest(C: pTNetClient; var M: TMsg);
503 begin
504 //e_LogWritefln('*** client #%u (cid #%u) full state request', [C.ID, C.Player]);
505 if gGameOn then
506 begin
507 MH_SEND_Everything((C^.State = NET_STATE_AUTH), C^.ID)
508 end
509 else
510 begin
511 C^.RequestedFullUpdate := True;
512 end;
513 end;
515 // PLAYER
517 function MH_RECV_PlayerPos(C: pTNetClient; var M: TMsg): Word;
518 var
519 Dir, i: Byte;
520 WeaponSelect: Word;
521 PID: Word;
522 kByte: Word;
523 Pl: TPlayer;
524 GT: LongWord;
525 begin
526 Result := 0;
527 if not gGameOn then Exit;
529 GT := M.ReadLongWord();
530 PID := C^.Player;
531 Pl := g_Player_Get(PID);
532 if Pl = nil then
533 Exit;
535 if (GT > gTime + NET_MAX_DIFFTIME) or (GT < Pl.NetTime) then Exit;
537 with Pl do
538 begin
539 NetTime := GT;
540 kByte := M.ReadWord();
541 Dir := M.ReadByte();
542 WeaponSelect := M.ReadWord();
543 //e_WriteLog(Format('R:ws=%d', [WeaponSelect]), MSG_WARNING);
544 if Direction <> TDirection(Dir) then
545 JustTeleported := False;
547 SetDirection(TDirection(Dir));
548 ReleaseKeys;
550 if kByte = NET_KEY_CHAT then
551 begin
552 PressKey(KEY_CHAT, 10000);
553 Exit;
554 end;
556 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
557 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
558 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
559 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
560 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
561 if LongBool(kByte and NET_KEY_FIRE) then PressKey(KEY_FIRE, 10000);
562 if LongBool(kByte and NET_KEY_OPEN) then PressKey(KEY_OPEN, 10000);
563 if LongBool(kByte and NET_KEY_NW) then PressKey(KEY_NEXTWEAPON, 10000);
564 if LongBool(kByte and NET_KEY_PW) then PressKey(KEY_PREVWEAPON, 10000);
566 for i := 0 to 15 do
567 begin
568 if (WeaponSelect and Word(1 shl i)) <> 0 then
569 begin
570 //e_WriteLog(Format(' R:wn=%d', [i]), MSG_WARNING);
571 QueueWeaponSwitch(i);
572 end;
573 end;
574 end;
576 // MH_SEND_PlayerPos(False, PID, C^.ID);
577 end;
579 procedure MH_RECV_CheatRequest(C: pTNetClient; var M: TMsg);
580 var
581 CheatKind: Byte;
582 Pl: TPlayer;
583 begin
584 Pl := g_Player_Get(C^.Player);
585 if Pl = nil then Exit;
587 CheatKind := M.ReadByte();
589 case CheatKind of
590 NET_CHEAT_SUICIDE:
591 Pl.Damage(SUICIDE_DAMAGE, Pl.UID, 0, 0, HIT_SELF);
592 NET_CHEAT_SPECTATE:
593 begin
594 if Pl.FSpectator then
595 Pl.Respawn(False)
596 else
597 Pl.Spectate;
598 end;
599 NET_CHEAT_READY:
600 begin
601 if gState <> STATE_INTERCUSTOM then Exit;
602 Pl.FReady := not Pl.FReady;
603 if Pl.FReady then
604 begin
605 MH_SEND_GameEvent(NET_EV_INTER_READY, Pl.UID, 'Y');
606 Inc(gInterReadyCount);
607 end
608 else
609 begin
610 MH_SEND_GameEvent(NET_EV_INTER_READY, Pl.UID, 'N');
611 Dec(gInterReadyCount);
612 end;
613 end;
614 end;
615 end;
617 procedure MH_RECV_PlayerSettings(C: pTNetClient; var M: TMsg);
618 var
619 TmpName: string;
620 TmpModel: string;
621 TmpColor: TRGB;
622 TmpTeam: Byte;
623 Pl: TPlayer;
624 begin
625 TmpName := M.ReadString();
626 TmpModel := M.ReadString();
627 TmpColor.R := M.ReadByte();
628 TmpColor.G := M.ReadByte();
629 TmpColor.B := M.ReadByte();
630 TmpTeam := M.ReadByte();
632 Pl := g_Player_Get(C^.Player);
633 if Pl = nil then Exit;
635 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
636 Pl.SwitchTeam
637 else
638 Pl.SetColor(TmpColor);
640 if Pl.Name <> TmpName then
641 begin
642 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
643 Pl.Name := TmpName;
644 end;
646 if TmpModel <> Pl.Model.Name then
647 Pl.SetModel(TmpModel);
649 MH_SEND_PlayerSettings(Pl.UID, TmpModel);
650 end;
652 // RCON
654 procedure MH_RECV_RCONPassword(C: pTNetClient; var M: TMsg);
655 var
656 Pwd: string;
657 begin
658 Pwd := M.ReadString();
659 if not NetAllowRCON then Exit;
660 if Pwd = NetRCONPassword then
661 begin
662 C^.RCONAuth := True;
663 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWGOOD, 'N', C^.ID);
664 end
665 else
666 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWBAD, 'N', C^.ID);
667 end;
669 procedure MH_RECV_RCONCommand(C: pTNetClient; var M: TMsg);
670 var
671 Cmd: string;
672 begin
673 Cmd := M.ReadString();
674 if not NetAllowRCON then Exit;
675 if not C^.RCONAuth then
676 begin
677 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_NOAUTH, 'N', C^.ID);
678 Exit;
679 end;
680 g_Console_Process(Cmd);
681 end;
683 // MISC
685 procedure MH_RECV_Vote(C: pTNetClient; var M: TMsg);
686 var
687 Start: Boolean;
688 Name, Command: string;
689 Need: Integer;
690 Pl: TPlayer;
691 begin
692 Start := M.ReadByte() <> 0;
693 Command := M.ReadString();
695 Pl := g_Player_Get(C^.Player);
696 if Pl = nil then Exit;
697 Name := Pl.Name;
699 if Start then
700 begin
701 if not g_Console_CommandBlacklisted(Command) then
702 g_Game_StartVote(Command, Name);
703 end
704 else if gVoteInProgress then
705 begin
706 if (gPlayer1 <> nil) or (gPlayer2 <> nil) then
707 Need := Floor((NetClientCount+1)/2.0) + 1
708 else
709 Need := Floor(NetClientCount/2.0) + 1;
710 if C^.Voted then
711 begin
712 Dec(gVoteCount);
713 C^.Voted := False;
714 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_REVOKED], [Name, gVoteCount, Need]), True);
715 MH_SEND_VoteEvent(NET_VE_REVOKE, Name, 'a', gVoteCount, Need);
716 end
717 else
718 begin
719 Inc(gVoteCount);
720 C^.Voted := True;
721 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Name, gVoteCount, Need]), True);
722 MH_SEND_VoteEvent(NET_VE_VOTE, Name, 'a', gVoteCount, Need);
723 g_Game_CheckVote;
724 end;
725 end;
726 end;
728 // GAME (SEND)
730 procedure MH_SEND_Everything(CreatePlayers: Boolean {= False}; ID: Integer {= NET_EVERYONE});
732 function sendItemRespawn (it: PItem): Boolean;
733 begin
734 result := false; // don't stop
735 MH_SEND_ItemSpawn(True, it.myid, ID);
736 end;
738 function sendMonSpawn (mon: TMonster): Boolean;
739 begin
740 result := false; // don't stop
741 MH_SEND_MonsterSpawn(mon.UID, ID);
742 end;
744 function sendPanelState (pan: TPanel): Boolean;
745 begin
746 result := false; // don't stop
747 MH_SEND_PanelState(pan.guid, ID); // anyway, to sync mplats
748 if (pan.CanChangeTexture) then MH_SEND_PanelTexture(pan.guid, pan.LastAnimLoop, ID);
749 end;
751 var
752 I: Integer;
753 begin
754 if (ID >= 0) and (ID < length(NetClients)) then
755 begin
756 e_LogWritefln('*** client #%u (cid #%u) will get everything', [ID, NetClients[ID].Player]);
757 MH_ProcessFirstSpawn(@NetClients[ID]);
758 end;
760 if gPlayers <> nil then
761 begin
762 for I := Low(gPlayers) to High(gPlayers) do
763 begin
764 if gPlayers[I] <> nil then
765 begin
766 if CreatePlayers then MH_SEND_PlayerCreate(gPlayers[I].UID, ID);
767 MH_SEND_PlayerPos(True, gPlayers[I].UID, ID);
768 MH_SEND_PlayerStats(gPlayers[I].UID, ID);
770 if (gPlayers[I].Flag <> FLAG_NONE) and (gGameSettings.GameMode = GM_CTF) then
771 begin
772 MH_SEND_FlagEvent(FLAG_STATE_CAPTURED, gPlayers[I].Flag, gPlayers[I].UID, True, ID);
773 end;
774 end;
775 end;
776 end;
778 g_Items_ForEachAlive(sendItemRespawn, true); // backwards
779 g_Mons_ForEach(sendMonSpawn);
780 g_Map_ForEachPanel(sendPanelState);
782 if gTriggers <> nil then
783 begin
784 for I := Low(gTriggers) to High(gTriggers) do
785 begin
786 if gTriggers[I].TriggerType = TRIGGER_SOUND then
787 begin
788 MH_SEND_TriggerSound(gTriggers[I], ID);
789 end;
790 end;
791 end;
793 if Shots <> nil then
794 begin
795 for I := Low(Shots) to High(Shots) do
796 begin
797 if Shots[i].ShotType in [6, 7, 8] then
798 begin
799 MH_SEND_CreateShot(i, ID);
800 end;
801 end;
802 end;
804 MH_SEND_TriggerMusic(ID);
806 MH_SEND_GameStats(ID);
807 MH_SEND_CoopStats(ID);
809 if gGameSettings.GameMode = GM_CTF then
810 begin
811 if gFlags[FLAG_RED].State <> FLAG_STATE_CAPTURED then MH_SEND_FlagEvent(gFlags[FLAG_RED].State, FLAG_RED, 0, True, ID);
812 if gFlags[FLAG_BLUE].State <> FLAG_STATE_CAPTURED then MH_SEND_FlagEvent(gFlags[FLAG_BLUE].State, FLAG_BLUE, 0, True, ID);
813 end;
815 if CreatePlayers and (ID >= 0) then NetClients[ID].State := NET_STATE_GAME;
817 if gLMSRespawn > LMS_RESPAWN_NONE then
818 begin
819 e_LogWritefln('*** client #%u (cid #%u) WARMUP', [ID, NetClients[ID].Player]);
820 MH_SEND_GameEvent(NET_EV_LMS_WARMUP, (gLMSRespawnTime - gTime) div 1000, 'N', ID);
821 end;
823 g_Net_Flush();
824 end;
826 procedure MH_SEND_Info(ID: Byte);
827 var
828 Map: string;
829 begin
830 Map := g_ExtractFileName(gMapInfo.Map);
832 NetOut.Clear();
834 NetOut.Write(Byte(NET_MSG_INFO));
835 NetOut.Write(ID);
836 NetOut.Write(NetClients[ID].Player);
837 NetOut.Write(gGameSettings.WAD);
838 NetOut.Write(Map);
839 NetOut.Write(gWADHash);
840 NetOut.Write(gGameSettings.GameMode);
841 NetOut.Write(gGameSettings.GoalLimit);
842 NetOut.Write(gGameSettings.TimeLimit);
843 NetOut.Write(gGameSettings.MaxLives);
844 NetOut.Write(gGameSettings.Options);
845 NetOut.Write(gTime);
847 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
848 end;
850 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
851 var
852 Name: string;
853 i: Integer;
854 Team: Byte;
855 begin
856 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
857 Mode := NET_CHAT_PLAYER;
859 Team := 0;
860 if (Mode = NET_CHAT_TEAM) then
861 begin
862 for i := Low(gPlayers) to High(gPlayers) do
863 if (gPlayers[i] <> nil) and (gPlayers[i].FClientID >= 0) and
864 (gPlayers[i].Team = ID) then
865 begin
866 NetOut.Write(Byte(NET_MSG_CHAT));
867 NetOut.Write(Txt);
868 NetOut.Write(Mode);
869 g_Net_Host_Send(gPlayers[i].FClientID, True, NET_CHAN_CHAT);
870 end;
871 Team := ID;
872 ID := NET_EVERYONE;
873 end
874 else
875 begin
876 NetOut.Write(Byte(NET_MSG_CHAT));
877 NetOut.Write(Txt);
878 NetOut.Write(Mode);
879 g_Net_Host_Send(ID, True, NET_CHAN_CHAT);
880 end;
882 if Mode = NET_CHAT_SYSTEM then
883 Exit;
885 if ID = NET_EVERYONE then
886 begin
887 if Mode = NET_CHAT_PLAYER then
888 begin
889 g_Console_Add(Txt, True);
890 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
891 g_Game_ChatSound(b_Text_Unformat(Txt));
892 end
893 else
894 if Mode = NET_CHAT_TEAM then
895 if gPlayer1 <> nil then
896 begin
897 if (gPlayer1.Team = TEAM_RED) and (Team = TEAM_RED) then
898 begin
899 g_Console_Add(#18'[Team] '#2 + Txt, True);
900 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
901 g_Game_ChatSound(b_Text_Unformat(Txt));
902 end
903 else if (gPlayer1.Team = TEAM_BLUE) and (Team = TEAM_BLUE) then
904 begin
905 g_Console_Add(#20'[Team] '#2 + Txt, True);
906 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
907 g_Game_ChatSound(b_Text_Unformat(Txt));
908 end;
909 end;
910 end
911 else
912 begin
913 Name := g_Net_ClientName_ByID(ID);
914 g_Console_Add('-> ' + Name + ': ' + Txt, True);
915 e_WriteLog('[Tell ' + Name + '] ' + b_Text_Unformat(Txt), TMsgType.Notify);
916 g_Game_ChatSound(b_Text_Unformat(Txt), False);
917 end;
918 end;
920 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
921 begin
922 NetOut.Write(Byte(NET_MSG_GFX));
923 NetOut.Write(Kind);
924 NetOut.Write(X);
925 NetOut.Write(Y);
926 NetOut.Write(Ang);
928 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
929 end;
931 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
932 begin
933 NetOut.Write(Byte(NET_MSG_SND));
934 NetOut.Write(Name);
935 if Pos then
936 begin
937 NetOut.Write(Byte(1));
938 NetOut.Write(X);
939 NetOut.Write(Y);
940 end
941 else
942 NetOut.Write(Byte(0));
944 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
945 end;
947 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
948 begin
949 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
951 NetOut.Write(Byte(NET_MSG_SHADD));
952 NetOut.Write(Proj);
953 NetOut.Write(Shots[Proj].ShotType);
954 NetOut.Write(Shots[Proj].Target);
955 NetOut.Write(Shots[Proj].SpawnerUID);
956 NetOut.Write(Shots[Proj].Timeout);
957 NetOut.Write(Shots[Proj].Obj.X);
958 NetOut.Write(Shots[Proj].Obj.Y);
959 NetOut.Write(Shots[Proj].Obj.Vel.X);
960 NetOut.Write(Shots[Proj].Obj.Vel.Y);
962 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
963 end;
965 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
966 begin
967 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
969 NetOut.Write(Byte(NET_MSG_SHPOS));
970 NetOut.Write(Proj);
971 NetOut.Write(Shots[Proj].Obj.X);
972 NetOut.Write(Shots[Proj].Obj.Y);
973 NetOut.Write(Shots[Proj].Obj.Vel.X);
974 NetOut.Write(Shots[Proj].Obj.Vel.Y);
976 g_Net_Host_Send(ID, False, NET_CHAN_SHOTS);
977 end;
979 procedure MH_Send_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
980 begin
981 NetOut.Write(Byte(NET_MSG_SHDEL));
982 NetOut.Write(Proj);
983 NetOut.Write(Byte(Loud));
984 NetOut.Write(X);
985 NetOut.Write(Y);
987 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
988 end;
990 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
991 begin
992 NetOut.Write(Byte(NET_MSG_SCORE));
993 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
994 begin
995 NetOut.Write(gTeamStat[TEAM_RED].Goals);
996 NetOut.Write(gTeamStat[TEAM_BLUE].Goals);
997 end
998 else
999 if gGameSettings.GameMode = GM_COOP then
1000 begin
1001 NetOut.Write(gCoopMonstersKilled);
1002 NetOut.Write(gCoopSecretsFound);
1003 end;
1005 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1006 end;
1008 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
1009 begin
1010 NetOut.Write(Byte(NET_MSG_COOP));
1011 NetOut.Write(gTotalMonsters);
1012 NetOut.Write(gSecretsCount);
1013 NetOut.Write(gCoopTotalMonstersKilled);
1014 NetOut.Write(gCoopTotalSecretsFound);
1015 NetOut.Write(gCoopTotalMonsters);
1016 NetOut.Write(gCoopTotalSecrets);
1017 end;
1019 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
1020 begin
1021 NetOut.Write(Byte(NET_MSG_GEVENT));
1022 NetOut.Write(EvType);
1023 NetOut.Write(EvNum);
1024 NetOut.Write(EvStr);
1025 NetOut.Write(Byte(gLastMap));
1026 NetOut.Write(gTime);
1027 if (EvType = NET_EV_MAPSTART) and isWadPath(EvStr) then
1028 begin
1029 NetOut.Write(Byte(1));
1030 NetOut.Write(gWADHash);
1031 end else
1032 NetOut.Write(Byte(0));
1034 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
1035 end;
1037 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
1038 begin
1039 NetOut.Write(Byte(NET_MSG_FLAG));
1040 NetOut.Write(EvType);
1041 NetOut.Write(Flag);
1042 NetOut.Write(Byte(Quiet));
1043 NetOut.Write(PID);
1044 NetOut.Write(gFlags[Flag].State);
1045 NetOut.Write(gFlags[Flag].CaptureTime);
1046 NetOut.Write(gFlags[Flag].Obj.X);
1047 NetOut.Write(gFlags[Flag].Obj.Y);
1048 NetOut.Write(gFlags[Flag].Obj.Vel.X);
1049 NetOut.Write(gFlags[Flag].Obj.Vel.Y);
1051 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1052 end;
1054 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
1055 begin
1056 NetOut.Write(Byte(NET_MSG_GSET));
1057 NetOut.Write(gGameSettings.GameMode);
1058 NetOut.Write(gGameSettings.GoalLimit);
1059 NetOut.Write(gGameSettings.TimeLimit);
1060 NetOut.Write(gGameSettings.MaxLives);
1061 NetOut.Write(gGameSettings.Options);
1063 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1064 end;
1066 // PLAYER (SEND)
1068 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
1069 var
1070 P: TPlayer;
1071 begin
1072 P := g_Player_Get(PID);
1073 if P = nil then Exit;
1075 NetOut.Write(Byte(NET_MSG_PLR));
1076 NetOut.Write(PID);
1077 NetOut.Write(P.Name);
1079 NetOut.Write(P.FActualModelName);
1080 NetOut.Write(P.FColor.R);
1081 NetOut.Write(P.FColor.G);
1082 NetOut.Write(P.FColor.B);
1083 NetOut.Write(P.Team);
1085 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT)
1086 end;
1088 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
1089 var
1090 kByte: Word;
1091 Pl: TPlayer;
1092 begin
1093 Pl := g_Player_Get(PID);
1094 if Pl = nil then Exit;
1095 if Pl.FDummy then Exit;
1097 NetOut.Write(Byte(NET_MSG_PLRPOS));
1098 NetOut.Write(gTime);
1099 NetOut.Write(PID);
1101 kByte := 0;
1103 with Pl do
1104 begin
1105 NetOut.Write(FPing);
1106 NetOut.Write(FLoss);
1107 if IsKeyPressed(KEY_CHAT) then
1108 kByte := NET_KEY_CHAT
1109 else
1110 begin
1111 if IsKeyPressed(KEY_LEFT) then kByte := kByte or NET_KEY_LEFT;
1112 if IsKeyPressed(KEY_RIGHT) then kByte := kByte or NET_KEY_RIGHT;
1113 if IsKeyPressed(KEY_UP) then kByte := kByte or NET_KEY_UP;
1114 if IsKeyPressed(KEY_DOWN) then kByte := kByte or NET_KEY_DOWN;
1115 if IsKeyPressed(KEY_JUMP) then kByte := kByte or NET_KEY_JUMP;
1116 if JustTeleported then kByte := kByte or NET_KEY_FORCEDIR;
1117 end;
1119 NetOut.Write(kByte);
1120 if Direction = TDirection.D_LEFT then NetOut.Write(Byte(0)) else NetOut.Write(Byte(1));
1121 NetOut.Write(GameX);
1122 NetOut.Write(GameY);
1123 NetOut.Write(GameVelX);
1124 NetOut.Write(GameVelY);
1125 NetOut.Write(GameAccelX);
1126 NetOut.Write(GameAccelY);
1127 end;
1129 g_Net_Host_Send(ID, Reliable, NET_CHAN_PLAYERPOS);
1130 end;
1132 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
1133 var
1134 P: TPlayer;
1135 I: Integer;
1136 begin
1137 P := g_Player_Get(PID);
1138 if P = nil then Exit;
1140 NetOut.Write(Byte(NET_MSG_PLRSTA));
1141 NetOut.Write(PID);
1143 with P do
1144 begin
1145 NetOut.Write(Byte(alive));
1146 NetOut.Write(Byte(GodMode));
1147 NetOut.Write(Health);
1148 NetOut.Write(Armor);
1149 NetOut.Write(Air);
1150 NetOut.Write(JetFuel);
1151 NetOut.Write(Lives);
1152 NetOut.Write(Team);
1154 for I := WP_FIRST to WP_LAST do
1155 NetOut.Write(Byte(FWeapon[I]));
1157 for I := A_BULLETS to A_HIGH do
1158 NetOut.Write(FAmmo[I]);
1160 for I := A_BULLETS to A_HIGH do
1161 NetOut.Write(FMaxAmmo[I]);
1163 for I := MR_SUIT to MR_MAX do
1164 NetOut.Write(LongWord(FMegaRulez[I]));
1166 NetOut.Write(Byte(R_ITEM_BACKPACK in FRulez));
1167 NetOut.Write(Byte(R_KEY_RED in FRulez));
1168 NetOut.Write(Byte(R_KEY_GREEN in FRulez));
1169 NetOut.Write(Byte(R_KEY_BLUE in FRulez));
1170 NetOut.Write(Byte(R_BERSERK in FRulez));
1172 NetOut.Write(Frags);
1173 NetOut.Write(Death);
1175 NetOut.Write(CurrWeap);
1177 NetOut.Write(Byte(FSpectator));
1178 NetOut.Write(Byte(FGhost));
1179 NetOut.Write(Byte(FPhysics));
1180 NetOut.Write(Byte(FNoRespawn));
1181 NetOut.Write(Byte(FJetpack));
1182 NetOut.Write(FFireTime);
1183 NetOut.Write(Byte(FFlaming));
1184 end;
1186 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1187 end;
1189 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
1190 begin
1191 NetOut.Write(Byte(NET_MSG_PLRDMG));
1192 NetOut.Write(PID);
1193 NetOut.Write(Kind);
1194 NetOut.Write(Attacker);
1195 NetOut.Write(Value);
1196 NetOut.Write(VX);
1197 NetOut.Write(VY);
1199 g_Net_Host_Send(ID, False, NET_CHAN_PLAYER);
1200 end;
1202 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
1203 begin
1204 NetOut.Write(Byte(NET_MSG_PLRDIE));
1205 NetOut.Write(PID);
1206 NetOut.Write(KillType);
1207 NetOut.Write(DeathType);
1208 NetOut.Write(Attacker);
1210 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1211 end;
1213 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
1214 begin
1215 NetOut.Write(Byte(NET_MSG_PLRFIRE));
1216 NetOut.Write(PID);
1217 NetOut.Write(Weapon);
1218 NetOut.Write(X);
1219 NetOut.Write(Y);
1220 NetOut.Write(AX);
1221 NetOut.Write(AY);
1222 NetOut.Write(ShotID);
1224 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
1225 end;
1227 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
1228 begin
1229 NetOut.Write(Byte(NET_MSG_PLRDEL));
1230 NetOut.Write(PID);
1232 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1233 end;
1235 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
1236 var
1237 Pl: TPlayer;
1238 begin
1239 Pl := g_Player_Get(PID);
1240 if Pl = nil then Exit;
1242 NetOut.Write(Byte(NET_MSG_PLRSET));
1243 NetOut.Write(PID);
1244 NetOut.Write(Pl.Name);
1245 if Mdl = '' then
1246 NetOut.Write(Pl.Model.Name)
1247 else
1248 NetOut.Write(Mdl);
1249 NetOut.Write(Pl.FColor.R);
1250 NetOut.Write(Pl.FColor.G);
1251 NetOut.Write(Pl.FColor.B);
1252 NetOut.Write(Pl.Team);
1254 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1255 end;
1257 // ITEM (SEND)
1259 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1260 var
1261 it: PItem;
1262 tt: Byte;
1263 begin
1264 it := g_Items_ByIdx(IID);
1266 NetOut.Write(Byte(NET_MSG_ISPAWN));
1267 NetOut.Write(IID);
1268 NetOut.Write(Byte(Quiet));
1269 tt := it.ItemType;
1270 if it.dropped then tt := tt or $80;
1271 NetOut.Write(tt);
1272 NetOut.Write(Byte(it.Fall));
1273 NetOut.Write(Byte(it.Respawnable));
1274 NetOut.Write(it.Obj.X);
1275 NetOut.Write(it.Obj.Y);
1276 NetOut.Write(it.Obj.Vel.X);
1277 NetOut.Write(it.Obj.Vel.Y);
1279 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1280 end;
1282 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1283 begin
1284 NetOut.Write(Byte(NET_MSG_IDEL));
1285 NetOut.Write(IID);
1286 NetOut.Write(Byte(Quiet));
1288 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1289 end;
1291 // PANEL
1293 procedure MH_SEND_PanelTexture(PGUID: Integer; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
1294 var
1295 TP: TPanel;
1296 begin
1297 TP := g_Map_PanelByGUID(PGUID);
1298 if (TP = nil) then exit;
1300 with TP do
1301 begin
1302 NetOut.Write(Byte(NET_MSG_PTEX));
1303 NetOut.Write(LongWord(PGUID));
1304 NetOut.Write(FCurTexture);
1305 NetOut.Write(FCurFrame);
1306 NetOut.Write(FCurFrameCount);
1307 NetOut.Write(AnimLoop);
1308 end;
1310 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1311 end;
1313 procedure MH_SEND_PanelState(PGUID: Integer; ID: Integer = NET_EVERYONE);
1314 var
1315 TP: TPanel;
1316 mpflags: Byte = 0;
1317 begin
1318 TP := g_Map_PanelByGUID(PGUID);
1319 if (TP = nil) then exit;
1321 NetOut.Write(Byte(NET_MSG_PSTATE));
1322 NetOut.Write(LongWord(PGUID));
1323 NetOut.Write(Byte(TP.Enabled));
1324 NetOut.Write(TP.LiftType);
1325 NetOut.Write(TP.X);
1326 NetOut.Write(TP.Y);
1327 NetOut.Write(Word(TP.Width));
1328 NetOut.Write(Word(TP.Height));
1329 // mplats
1330 NetOut.Write(LongInt(TP.movingSpeedX));
1331 NetOut.Write(LongInt(TP.movingSpeedY));
1332 NetOut.Write(LongInt(TP.movingStartX));
1333 NetOut.Write(LongInt(TP.movingStartY));
1334 NetOut.Write(LongInt(TP.movingEndX));
1335 NetOut.Write(LongInt(TP.movingEndY));
1336 NetOut.Write(LongInt(TP.sizeSpeedX));
1337 NetOut.Write(LongInt(TP.sizeSpeedY));
1338 NetOut.Write(LongInt(TP.sizeEndX));
1339 NetOut.Write(LongInt(TP.sizeEndY));
1340 if TP.movingActive then mpflags := mpflags or 1;
1341 if TP.moveOnce then mpflags := mpflags or 2;
1342 NetOut.Write(Byte(mpflags));
1344 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1345 end;
1347 // TRIGGER
1349 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
1350 begin
1351 if gTriggers = nil then Exit;
1352 if T.Sound = nil then Exit;
1354 NetOut.Write(Byte(NET_MSG_TSOUND));
1355 NetOut.Write(T.ClientID);
1356 NetOut.Write(Byte(T.Sound.IsPlaying));
1357 NetOut.Write(LongWord(T.Sound.GetPosition));
1358 NetOut.Write(T.SoundPlayCount);
1360 g_Net_Host_Send(ID, True);
1361 end;
1363 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
1364 begin
1365 NetOut.Write(Byte(NET_MSG_TMUSIC));
1366 NetOut.Write(gMusic.Name);
1367 NetOut.Write(Byte(gMusic.IsPlaying));
1368 NetOut.Write(LongWord(gMusic.GetPosition));
1369 NetOut.Write(Byte(gMusic.SpecPause or gMusic.IsPaused));
1371 g_Net_Host_Send(ID, True);
1372 end;
1374 // MONSTER
1376 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
1377 var
1378 M: TMonster;
1379 begin
1380 M := g_Monsters_ByUID(UID);
1381 if M = nil then
1382 Exit;
1384 with M do
1385 begin
1386 NetOut.Write(Byte(NET_MSG_MSPAWN));
1387 NetOut.Write(UID);
1388 NetOut.Write(MonsterType);
1389 NetOut.Write(MonsterState);
1390 NetOut.Write(MonsterAnim);
1391 NetOut.Write(MonsterTargetUID);
1392 NetOut.Write(MonsterTargetTime);
1393 NetOut.Write(MonsterBehaviour);
1394 NetOut.Write(MonsterSleep);
1395 NetOut.Write(MonsterHealth);
1396 NetOut.Write(MonsterAmmo);
1397 NetOut.Write(GameX);
1398 NetOut.Write(GameY);
1399 NetOut.Write(GameVelX);
1400 NetOut.Write(GameVelY);
1401 NetOut.Write(Byte(GameDirection));
1402 end;
1404 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1405 end;
1407 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
1408 var
1409 M: TMonster;
1410 begin
1411 M := g_Monsters_ByUID(UID);
1412 if M = nil then Exit;
1414 NetOut.Write(Byte(NET_MSG_MPOS));
1415 NetOut.Write(UID);
1417 with M do
1418 begin
1419 NetOut.Write(GameX);
1420 NetOut.Write(GameY);
1421 NetOut.Write(GameVelX);
1422 NetOut.Write(GameVelY);
1423 NetOut.Write(Byte(GameDirection));
1424 end;
1426 g_Net_Host_Send(ID, False, NET_CHAN_MONSTERPOS);
1427 end;
1429 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
1430 var
1431 M: TMonster;
1432 begin
1433 M := g_Monsters_ByUID(UID);
1434 if M = nil then Exit;
1436 NetOut.Write(Byte(NET_MSG_MSTATE));
1437 NetOut.Write(UID);
1439 with M do
1440 begin
1441 NetOut.Write(MonsterState);
1442 NetOut.Write(ForcedAnim);
1443 NetOut.Write(MonsterTargetUID);
1444 NetOut.Write(MonsterTargetTime);
1445 NetOut.Write(MonsterSleep);
1446 NetOut.Write(MonsterHealth);
1447 NetOut.Write(MonsterAmmo);
1448 NetOut.Write(MonsterPain);
1449 NetOut.Write(Byte(AnimIsReverse));
1450 NetOut.Write(FFireTime);
1451 end;
1453 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1454 end;
1456 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
1457 begin
1458 NetOut.Write(Byte(NET_MSG_MSHOT));
1459 NetOut.Write(UID);
1460 NetOut.Write(X);
1461 NetOut.Write(Y);
1462 NetOut.Write(VX);
1463 NetOut.Write(VY);
1465 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1466 end;
1468 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
1469 var
1470 M: TMonster;
1471 begin
1472 M := g_Monsters_ByUID(UID);
1473 if M = nil then Exit;
1475 NetOut.Write(Byte(NET_MSG_MDEL));
1476 NetOut.Write(UID);
1478 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1479 end;
1481 // MISC
1483 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
1484 begin
1485 NetOut.Write(Byte(NET_MSG_TIME_SYNC));
1486 NetOut.Write(Time);
1488 g_Net_Host_Send(ID, False, NET_CHAN_SERVICE);
1489 end;
1491 procedure MH_SEND_VoteEvent(EvType: Byte;
1492 StrArg1: string = 'a'; StrArg2: string = 'b';
1493 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
1494 ID: Integer = NET_EVERYONE);
1495 begin
1496 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
1497 NetOut.Write(EvType);
1498 NetOut.Write(IntArg1);
1499 NetOut.Write(IntArg2);
1500 NetOut.Write(StrArg1);
1501 NetOut.Write(StrArg2);
1503 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1504 end;
1506 // CLIENT MESSAGES //
1508 // GAME
1510 procedure MC_RECV_Chat(var M: TMsg);
1511 var
1512 Txt: string;
1513 Mode: Byte;
1514 begin
1515 Txt := M.ReadString();
1516 Mode := M.ReadByte();
1518 if Mode <> NET_CHAT_SYSTEM then
1519 begin
1520 if Mode = NET_CHAT_PLAYER then
1521 begin
1522 g_Console_Add(Txt, True);
1523 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1524 g_Game_ChatSound(b_Text_Unformat(Txt));
1525 end else
1526 if (Mode = NET_CHAT_TEAM) and (gPlayer1 <> nil) then
1527 begin
1528 if gPlayer1.Team = TEAM_RED then
1529 g_Console_Add(b_Text_Format('\r[Team] ') + Txt, True);
1530 if gPlayer1.Team = TEAM_BLUE then
1531 g_Console_Add(b_Text_Format('\b[Team] ') + Txt, True);
1532 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1533 g_Game_ChatSound(b_Text_Unformat(Txt));
1534 end;
1535 end else
1536 g_Console_Add(Txt, True);
1537 end;
1539 procedure MC_RECV_Effect(var M: TMsg);
1540 var
1541 Kind: Byte;
1542 X, Y: Integer;
1543 Ang: SmallInt;
1544 Anim: TAnimation;
1545 ID: LongWord;
1546 begin
1547 if not gGameOn then Exit;
1548 Kind := M.ReadByte();
1549 X := M.ReadLongInt();
1550 Y := M.ReadLongInt();
1551 Ang := M.ReadSmallInt();
1553 case Kind of
1554 NET_GFX_SPARK:
1555 g_GFX_Spark(X, Y, 2 + Random(2), Ang, 0, 0);
1557 NET_GFX_TELE:
1558 begin
1559 if g_Frames_Get(ID, 'FRAMES_TELEPORT') then
1560 begin
1561 Anim := TAnimation.Create(ID, False, 3);
1562 g_GFX_OnceAnim(X, Y, Anim);
1563 Anim.Free();
1564 end;
1565 if Ang = 1 then
1566 g_Sound_PlayExAt('SOUND_GAME_TELEPORT', X, Y);
1567 end;
1569 NET_GFX_EXPLODE:
1570 begin
1571 if g_Frames_Get(ID, 'FRAMES_EXPLODE_ROCKET') then
1572 begin
1573 Anim := TAnimation.Create(ID, False, 6);
1574 Anim.Blending := False;
1575 g_GFX_OnceAnim(X-64, Y-64, Anim);
1576 Anim.Free();
1577 end;
1578 if Ang = 1 then
1579 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEROCKET', X, Y);
1580 end;
1582 NET_GFX_BFGEXPL:
1583 begin
1584 if g_Frames_Get(ID, 'FRAMES_EXPLODE_BFG') then
1585 begin
1586 Anim := TAnimation.Create(ID, False, 6);
1587 Anim.Blending := False;
1588 g_GFX_OnceAnim(X-64, Y-64, Anim);
1589 Anim.Free();
1590 end;
1591 if Ang = 1 then
1592 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEBFG', X, Y);
1593 end;
1595 NET_GFX_BFGHIT:
1596 begin
1597 if g_Frames_Get(ID, 'FRAMES_BFGHIT') then
1598 begin
1599 Anim := TAnimation.Create(ID, False, 4);
1600 g_GFX_OnceAnim(X-32, Y-32, Anim);
1601 Anim.Free();
1602 end;
1603 end;
1605 NET_GFX_FIRE:
1606 begin
1607 if g_Frames_Get(ID, 'FRAMES_FIRE') then
1608 begin
1609 Anim := TAnimation.Create(ID, False, 4);
1610 g_GFX_OnceAnim(X, Y, Anim);
1611 Anim.Free();
1612 end;
1613 if Ang = 1 then
1614 g_Sound_PlayExAt('SOUND_FIRE', X, Y);
1615 end;
1617 NET_GFX_RESPAWN:
1618 begin
1619 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
1620 begin
1621 Anim := TAnimation.Create(ID, False, 4);
1622 g_GFX_OnceAnim(X, Y, Anim);
1623 Anim.Free();
1624 end;
1625 if Ang = 1 then
1626 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
1627 end;
1629 NET_GFX_SHELL1:
1630 g_Player_CreateShell(X, Y, 0, -2, SHELL_BULLET);
1632 NET_GFX_SHELL2:
1633 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1635 NET_GFX_SHELL3:
1636 begin
1637 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1638 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1639 end;
1640 end;
1641 end;
1643 procedure MC_RECV_Sound(var M: TMsg);
1644 var
1645 Name: string;
1646 X, Y: Integer;
1647 Pos: Boolean;
1648 begin
1649 Name := M.ReadString();
1650 Pos := M.ReadByte() <> 0;
1651 if Pos then
1652 begin
1653 X := M.ReadLongInt();
1654 Y := M.ReadLongInt();
1655 g_Sound_PlayExAt(Name, X, Y);
1656 end
1657 else
1658 g_Sound_PlayEx(Name);
1659 end;
1661 procedure MC_RECV_CreateShot(var M: TMsg);
1662 var
1663 I, X, Y, XV, YV: Integer;
1664 Timeout: LongWord;
1665 Target, Spawner: Word;
1666 ShType: Byte;
1667 begin
1668 I := M.ReadLongInt();
1669 ShType := M.ReadByte();
1670 Target := M.ReadWord();
1671 Spawner := M.ReadWord();
1672 Timeout := M.ReadLongWord();
1673 X := M.ReadLongInt();
1674 Y := M.ReadLongInt();
1675 XV := M.ReadLongInt();
1676 YV := M.ReadLongInt();
1678 I := g_Weapon_CreateShot(I, ShType, Spawner, Target, X, Y, XV, YV);
1679 if (Shots <> nil) and (I <= High(Shots)) then
1680 begin
1681 Shots[I].Timeout := Timeout;
1682 //Shots[I].Target := Target; // TODO: find a use for Target later
1683 end;
1684 end;
1686 procedure MC_RECV_UpdateShot(var M: TMsg);
1687 var
1688 I, TX, TY, TXV, TYV: Integer;
1689 begin
1690 I := M.ReadLongInt();
1691 TX := M.ReadLongInt();
1692 TY := M.ReadLongInt();
1693 TXV := M.ReadLongInt();
1694 TYV := M.ReadLongInt();
1696 if (Shots <> nil) and (I <= High(Shots)) then
1697 with (Shots[i]) do
1698 begin
1699 Obj.X := TX;
1700 Obj.Y := TY;
1701 Obj.Vel.X := TXV;
1702 Obj.Vel.Y := TYV;
1703 end;
1704 end;
1706 procedure MC_RECV_DeleteShot(var M: TMsg);
1707 var
1708 I, X, Y: Integer;
1709 L: Boolean;
1710 begin
1711 if not gGameOn then Exit;
1712 I := M.ReadLongInt();
1713 L := (M.ReadByte() <> 0);
1714 X := M.ReadLongInt();
1715 Y := M.ReadLongInt();
1717 g_Weapon_DestroyShot(I, X, Y, L);
1718 end;
1720 procedure MC_RECV_GameStats(var M: TMsg);
1721 begin
1722 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
1723 begin
1724 gTeamStat[TEAM_RED].Goals := M.ReadSmallInt();
1725 gTeamStat[TEAM_BLUE].Goals := M.ReadSmallInt();
1726 end
1727 else
1728 if gGameSettings.GameMode = GM_COOP then
1729 begin
1730 gCoopMonstersKilled := M.ReadWord();
1731 gCoopSecretsFound := M.ReadWord();
1732 end;
1733 end;
1735 procedure MC_RECV_CoopStats(var M: TMsg);
1736 begin
1737 gTotalMonsters := M.ReadLongInt();
1738 gSecretsCount := M.ReadLongInt();
1739 gCoopTotalMonstersKilled := M.ReadWord();
1740 gCoopTotalSecretsFound := M.ReadWord();
1741 gCoopTotalMonsters := M.ReadWord();
1742 gCoopTotalSecrets := M.ReadWord();
1743 end;
1745 procedure MC_RECV_GameEvent(var M: TMsg);
1746 var
1747 EvType: Byte;
1748 EvNum: Integer;
1749 EvStr: string;
1750 EvTime: LongWord;
1751 BHash: Boolean;
1752 EvHash: TMD5Digest;
1753 pl: TPlayer;
1754 i1, i2: TStrings_Locale;
1755 pln: String;
1756 cnt: Byte;
1757 goodCmd: Boolean = true;
1758 begin
1759 FillChar(EvHash, Sizeof(EvHash), 0);
1760 EvType := M.ReadByte();
1761 EvNum := M.ReadLongInt();
1762 EvStr := M.ReadString();
1763 gLastMap := M.ReadByte() <> 0;
1764 if gLastMap and (gGameSettings.GameMode = GM_COOP) then gStatsOff := True;
1765 gStatsPressed := True;
1766 EvTime := M.ReadLongWord();
1767 BHash := M.ReadByte() <> 0;
1768 if BHash then
1769 EvHash := M.ReadMD5();
1771 gTime := EvTime;
1773 if (g_Res_received_map_start <> 0) then
1774 begin
1775 if (g_Res_received_map_start < 0) then exit;
1776 goodCmd := false;
1777 case EvType of
1778 NET_EV_MAPSTART: goodCmd := true;
1779 NET_EV_MAPEND: goodCmd := true;
1780 NET_EV_PLAYER_KICK: goodCmd := true;
1781 NET_EV_PLAYER_BAN: goodCmd := true;
1782 end;
1783 if not goodCmd then exit;
1784 end;
1786 case EvType of
1787 NET_EV_MAPSTART:
1788 begin
1789 if (g_Res_received_map_start <> 0) then
1790 begin
1791 g_Res_received_map_start := -1;
1792 end
1793 else
1794 begin
1795 gGameOn := False;
1796 g_Game_ClearLoading();
1797 g_Game_StopAllSounds(True);
1799 gSwitchGameMode := Byte(EvNum);
1800 gGameSettings.GameMode := gSwitchGameMode;
1802 gWADHash := EvHash;
1803 if not g_Game_StartMap(EvStr, True) then
1804 begin
1805 if not isWadPath(EvStr) then
1806 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [gGameSettings.WAD + ':\' + EvStr]))
1807 else
1808 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [EvStr]));
1809 Exit;
1810 end;
1812 MC_SEND_FullStateRequest;
1813 end;
1814 end;
1816 NET_EV_MAPEND:
1817 begin
1818 if (g_Res_received_map_start <> 0) then
1819 begin
1820 g_Res_received_map_start := -1;
1821 end
1822 else
1823 begin
1824 gMissionFailed := EvNum <> 0;
1825 gExit := EXIT_ENDLEVELCUSTOM;
1826 end;
1827 end;
1829 NET_EV_RCON:
1830 begin
1831 case EvNum of
1832 NET_RCON_NOAUTH:
1833 g_Console_Add(_lc[I_NET_RCON_NOAUTH], True);
1834 NET_RCON_PWGOOD:
1835 g_Console_Add(_lc[I_NET_RCON_PWD_VALID], True);
1836 NET_RCON_PWBAD:
1837 g_Console_Add(_lc[I_NET_RCON_PWD_INVALID], True);
1838 end;
1839 end;
1841 NET_EV_CHANGE_TEAM:
1842 begin
1843 if EvNum = TEAM_RED then
1844 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [EvStr]), True);
1845 if EvNum = TEAM_BLUE then
1846 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [EvStr]), True);
1847 end;
1849 NET_EV_PLAYER_KICK:
1850 begin
1851 g_Console_Add(Format(_lc[I_PLAYER_KICK], [EvStr]), True);
1852 if (g_Res_received_map_start <> 0) then g_Res_received_map_start := -1;
1853 end;
1855 NET_EV_PLAYER_BAN:
1856 begin
1857 g_Console_Add(Format(_lc[I_PLAYER_BAN], [EvStr]), True);
1858 if (g_Res_received_map_start <> 0) then g_Res_received_map_start := -1;
1859 end;
1861 NET_EV_LMS_WARMUP:
1862 g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [EvNum]), True);
1864 NET_EV_LMS_SURVIVOR:
1865 g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True);
1867 NET_EV_BIGTEXT:
1868 g_Game_Message(AnsiUpperCase(EvStr), Word(EvNum));
1870 NET_EV_SCORE:
1871 begin
1872 pl := g_Player_Get(EvNum and $FFFF);
1873 if pl = nil then
1874 pln := '?'
1875 else
1876 pln := pl.Name;
1877 cnt := (EvNum shr 16) and $FF;
1878 if Pos('w', EvStr) = 0 then
1879 begin
1880 // Default score
1881 if Pos('t', EvStr) = 0 then
1882 begin
1883 // Player +/- score
1884 if Pos('-', EvStr) = 0 then
1885 begin
1886 if Pos('e', EvStr) = 0 then
1887 i1 := I_PLAYER_SCORE_ADD_OWN
1888 else
1889 i1 := I_PLAYER_SCORE_ADD_ENEMY;
1890 end else
1891 begin
1892 if Pos('e', EvStr) = 0 then
1893 i1 := I_PLAYER_SCORE_SUB_OWN
1894 else
1895 i1 := I_PLAYER_SCORE_SUB_ENEMY;
1896 end;
1897 // Which team
1898 if Pos('r', EvStr) > 0 then
1899 i2 := I_PLAYER_SCORE_TO_RED
1900 else
1901 i2 := I_PLAYER_SCORE_TO_BLUE;
1902 g_Console_Add(Format(_lc[i1], [pln, cnt, _lc[i2]]), True);
1903 end else
1904 begin
1905 // Team +/- score
1906 if Pos('-', EvStr) = 0 then
1907 i1 := I_PLAYER_SCORE_ADD_TEAM
1908 else
1909 i1 := I_PLAYER_SCORE_SUB_TEAM;
1910 // Which team
1911 if Pos('r', EvStr) > 0 then
1912 i2 := I_PLAYER_SCORE_RED
1913 else
1914 i2 := I_PLAYER_SCORE_BLUE;
1915 g_Console_Add(Format(_lc[i1], [_lc[i2], cnt]), True);
1916 end;
1917 end else
1918 begin
1919 // Game Win
1920 if Pos('e', EvStr) = 0 then
1921 i1 := I_PLAYER_SCORE_WIN_OWN
1922 else
1923 i1 := I_PLAYER_SCORE_WIN_ENEMY;
1924 // Which team
1925 if Pos('r', EvStr) > 0 then
1926 i2 := I_PLAYER_SCORE_TO_RED
1927 else
1928 i2 := I_PLAYER_SCORE_TO_BLUE;
1929 g_Console_Add(Format(_lc[i1], [pln, _lc[i2]]), True);
1930 end;
1931 end;
1933 NET_EV_SCORE_MSG:
1934 begin
1935 if EvNum = TEAM_RED then
1936 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1937 if EvNum = TEAM_BLUE then
1938 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1939 if EvNum = -TEAM_RED then
1940 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1941 if EvNum = -TEAM_BLUE then
1942 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1943 end;
1945 NET_EV_LMS_START:
1946 begin
1947 g_Player_RemoveAllCorpses;
1948 g_Game_Message(_lc[I_MESSAGE_LMS_START], 144);
1949 end;
1951 NET_EV_LMS_WIN:
1952 g_Game_Message(Format(_lc[I_MESSAGE_LMS_WIN], [AnsiUpperCase(EvStr)]), 144);
1954 NET_EV_TLMS_WIN:
1955 begin
1956 if EvNum = TEAM_RED then
1957 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 144);
1958 if EvNum = TEAM_BLUE then
1959 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 144);
1960 end;
1962 NET_EV_LMS_LOSE:
1963 g_Game_Message(_lc[I_MESSAGE_LMS_LOSE], 144);
1965 NET_EV_LMS_DRAW:
1966 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
1968 NET_EV_KILLCOMBO:
1969 g_Game_Announce_KillCombo(EvNum);
1971 NET_EV_PLAYER_TOUCH:
1972 begin
1973 pl := g_Player_Get(EvNum);
1974 if pl <> nil then
1975 pl.Touch();
1976 end;
1978 NET_EV_SECRET:
1979 begin
1980 pl := g_Player_Get(EvNum);
1981 if pl <> nil then
1982 begin
1983 g_Console_Add(Format(_lc[I_PLAYER_SECRET], [pl.Name]), True);
1984 g_Sound_PlayEx('SOUND_GAME_SECRET');
1985 end;
1986 end;
1988 NET_EV_INTER_READY:
1989 begin
1990 pl := g_Player_Get(EvNum);
1991 if pl <> nil then pl.FReady := (EvStr = 'Y');
1992 end;
1993 end;
1994 end;
1996 procedure MC_RECV_FlagEvent(var M: TMsg);
1997 var
1998 PID: Word;
1999 Pl: TPlayer;
2000 EvType: Byte;
2001 Fl, a: Byte;
2002 Quiet: Boolean;
2003 s, ts: string;
2004 begin
2005 EvType := M.ReadByte();
2006 Fl := M.ReadByte();
2008 if Fl = FLAG_NONE then Exit;
2010 Quiet := (M.ReadByte() <> 0);
2011 PID := M.ReadWord();
2013 gFlags[Fl].State := M.ReadByte();
2014 gFlags[Fl].CaptureTime := M.ReadLongWord();
2015 gFlags[Fl].Obj.X := M.ReadLongInt();
2016 gFlags[Fl].Obj.Y := M.ReadLongInt();
2017 gFlags[Fl].Obj.Vel.X := M.ReadLongInt();
2018 gFlags[Fl].Obj.Vel.Y := M.ReadLongInt();
2020 Pl := g_Player_Get(PID);
2021 if (Pl = nil) and
2022 (EvType <> FLAG_STATE_NORMAL) and
2023 (EvType <> FLAG_STATE_DROPPED) and
2024 (EvType <> FLAG_STATE_RETURNED) then
2025 Exit;
2027 case EvType of
2028 FLAG_STATE_NORMAL:
2029 begin
2030 if Quiet or (Pl = nil) then Exit;
2032 if Fl = FLAG_RED then
2033 s := _lc[I_PLAYER_FLAG_RED]
2034 else
2035 s := _lc[I_PLAYER_FLAG_BLUE];
2037 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2039 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2040 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2041 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2042 a := 0
2043 else
2044 a := 1;
2046 if not sound_ret_flag[a].IsPlaying() then
2047 sound_ret_flag[a].Play();
2048 end;
2050 FLAG_STATE_CAPTURED:
2051 begin
2052 if (Pl <> nil) then Pl.SetFlag(Fl);
2054 if Quiet then Exit;
2056 if Fl = FLAG_RED then
2057 s := _lc[I_PLAYER_FLAG_RED]
2058 else
2059 s := _lc[I_PLAYER_FLAG_BLUE];
2061 g_Console_Add(Format(_lc[I_PLAYER_FLAG_GET], [Pl.Name, s]), True);
2062 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_GET], [AnsiUpperCase(s)]), 144);
2064 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2065 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2066 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2067 a := 0
2068 else
2069 a := 1;
2071 if not sound_get_flag[a].IsPlaying() then
2072 sound_get_flag[a].Play();
2073 end;
2075 FLAG_STATE_DROPPED:
2076 begin
2077 if (Pl <> nil) then Pl.SetFlag(FLAG_NONE);
2079 if Quiet or (Pl = nil) then Exit;
2081 if Fl = FLAG_RED then
2082 s := _lc[I_PLAYER_FLAG_RED]
2083 else
2084 s := _lc[I_PLAYER_FLAG_BLUE];
2086 g_Console_Add(Format(_lc[I_PLAYER_FLAG_DROP], [Pl.Name, s]), True);
2087 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_DROP], [AnsiUpperCase(s)]), 144);
2089 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2090 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2091 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2092 a := 0
2093 else
2094 a := 1;
2096 if not sound_lost_flag[a].IsPlaying() then
2097 sound_lost_flag[a].Play();
2098 end;
2100 FLAG_STATE_SCORED:
2101 begin
2102 g_Map_ResetFlag(FLAG_RED);
2103 g_Map_ResetFlag(FLAG_BLUE);
2104 if Quiet or (Pl = nil) then Exit;
2105 Pl.SetFlag(FLAG_NONE);
2107 if Fl = FLAG_RED then
2108 s := _lc[I_PLAYER_FLAG_RED]
2109 else
2110 s := _lc[I_PLAYER_FLAG_BLUE];
2112 ts := Format('%.4d', [gFlags[Fl].CaptureTime]);
2113 Insert('.', ts, Length(ts) + 1 - 3);
2114 g_Console_Add(Format(_lc[I_PLAYER_FLAG_CAPTURE], [Pl.Name, s, ts]), True);
2115 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_CAPTURE], [AnsiUpperCase(s)]), 144);
2117 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2118 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2119 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2120 a := 0
2121 else
2122 a := 1;
2124 if not sound_cap_flag[a].IsPlaying() then
2125 sound_cap_flag[a].Play();
2126 end;
2128 FLAG_STATE_RETURNED:
2129 begin
2130 g_Map_ResetFlag(Fl);
2131 if Quiet then Exit;
2133 if Fl = FLAG_RED then
2134 s := _lc[I_PLAYER_FLAG_RED]
2135 else
2136 s := _lc[I_PLAYER_FLAG_BLUE];
2138 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2140 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2141 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2142 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2143 a := 0
2144 else
2145 a := 1;
2147 if not sound_ret_flag[a].IsPlaying() then
2148 sound_ret_flag[a].Play();
2149 end;
2150 end;
2151 end;
2153 procedure MC_RECV_GameSettings(var M: TMsg);
2154 begin
2155 gGameSettings.GameMode := M.ReadByte();
2156 gGameSettings.GoalLimit := M.ReadWord();
2157 gGameSettings.TimeLimit := M.ReadWord();
2158 gGameSettings.MaxLives := M.ReadByte();
2159 gGameSettings.Options := M.ReadLongWord();
2160 end;
2162 // PLAYER
2164 function MC_RECV_PlayerCreate(var M: TMsg): Word;
2165 var
2166 PID, DID: Word;
2167 PName, Model: string;
2168 Color: TRGB;
2169 T: Byte;
2170 Pl: TPlayer;
2171 begin
2172 PID := M.ReadWord();
2173 Pl := g_Player_Get(PID);
2175 PName := M.ReadString();
2176 Model := M.ReadString();
2177 Color.R := M.ReadByte();
2178 Color.G := M.ReadByte();
2179 Color.B := M.ReadByte();
2180 T := M.ReadByte();
2182 Result := 0;
2183 if (PID <> NetPlrUID1) and (PID <> NetPlrUID2) then
2184 begin
2185 if (Pl <> nil) then Exit;
2186 DID := g_Player_Create(Model, Color, T, False);
2187 with g_Player_Get(DID) do
2188 begin
2189 UID := PID;
2190 Name := PName;
2191 Reset(True);
2192 end;
2193 end
2194 else
2195 begin
2196 if (PID = NetPlrUID1) and (gPlayer1 <> nil) then begin
2197 gPlayer1.UID := PID;
2198 gPlayer1.Model.SetColor(Color.R, Color.G, Color.B);
2199 gPlayer1.ChangeTeam(T);
2200 end;
2201 if (PID = NetPlrUID2) and (gPlayer2 <> nil) then begin
2202 gPlayer2.UID := PID;
2203 gPlayer2.Model.SetColor(Color.R, Color.G, Color.B);
2204 gPlayer2.ChangeTeam(T);
2205 end;
2206 end;
2208 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
2209 e_WriteLog('NET: Player ' + PName + ' [' + IntToStr(PID) + '] added.', TMsgType.Notify);
2210 Result := PID;
2211 end;
2213 function MC_RECV_PlayerPos(var M: TMsg): Word;
2214 var
2215 GT: LongWord;
2216 PID: Word;
2217 kByte: Word;
2218 Pl: TPlayer;
2219 Dir: Byte;
2220 TmpX, TmpY: Integer;
2221 begin
2222 Result := 0;
2224 GT := M.ReadLongWord();
2225 if GT < gTime - NET_MAX_DIFFTIME then
2226 begin
2227 gTime := GT;
2228 Exit;
2229 end;
2230 gTime := GT;
2232 PID := M.ReadWord();
2233 Pl := g_Player_Get(PID);
2235 if Pl = nil then Exit;
2237 Result := PID;
2239 with Pl do
2240 begin
2241 FPing := M.ReadWord();
2242 FLoss := M.ReadByte();
2243 kByte := M.ReadWord();
2244 Dir := M.ReadByte();
2246 TmpX := M.ReadLongInt();
2247 TmpY := M.ReadLongInt();
2249 ReleaseKeys;
2251 if (kByte = NET_KEY_CHAT) then
2252 PressKey(KEY_CHAT, 10000)
2253 else
2254 begin
2255 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
2256 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
2257 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
2258 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
2259 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
2260 end;
2262 if ((Pl <> gPlayer1) and (Pl <> gPlayer2)) or LongBool(kByte and NET_KEY_FORCEDIR) then
2263 SetDirection(TDirection(Dir));
2265 GameVelX := M.ReadLongInt();
2266 GameVelY := M.ReadLongInt();
2267 GameAccelX := M.ReadLongInt();
2268 GameAccelY := M.ReadLongInt();
2269 SetLerp(TmpX, TmpY);
2270 if NetForcePlayerUpdate then Update();
2271 end;
2272 end;
2274 function MC_RECV_PlayerStats(var M: TMsg): Word;
2275 var
2276 PID: Word;
2277 Pl: TPlayer;
2278 I, OldFire: Integer;
2279 OldJet, Flam: Boolean;
2280 NewTeam: Byte;
2281 begin
2282 PID := M.ReadWord();
2283 Pl := g_Player_Get(PID);
2284 Result := 0;
2285 if Pl = nil then
2286 Exit;
2288 with Pl do
2289 begin
2290 alive := (M.ReadByte() <> 0);
2291 GodMode := (M.ReadByte() <> 0);
2292 Health := M.ReadLongInt();
2293 Armor := M.ReadLongInt();
2294 Air := M.ReadLongInt();
2295 JetFuel := M.ReadLongInt();
2296 Lives := M.ReadByte();
2297 NewTeam := M.ReadByte();
2299 for I := WP_FIRST to WP_LAST do
2300 FWeapon[I] := (M.ReadByte() <> 0);
2302 for I := A_BULLETS to A_HIGH do
2303 FAmmo[I] := M.ReadWord();
2305 for I := A_BULLETS to A_HIGH do
2306 FMaxAmmo[I] := M.ReadWord();
2308 for I := MR_SUIT to MR_MAX do
2309 FMegaRulez[I] := M.ReadLongWord();
2311 FRulez := [];
2312 if (M.ReadByte() <> 0) then
2313 FRulez := FRulez + [R_ITEM_BACKPACK];
2314 if (M.ReadByte() <> 0) then
2315 FRulez := FRulez + [R_KEY_RED];
2316 if (M.ReadByte() <> 0) then
2317 FRulez := FRulez + [R_KEY_GREEN];
2318 if (M.ReadByte() <> 0) then
2319 FRulez := FRulez + [R_KEY_BLUE];
2320 if (M.ReadByte() <> 0) then
2321 FRulez := FRulez + [R_BERSERK];
2323 Frags := M.ReadLongInt();
2324 Death := M.ReadLongInt();
2326 SetWeapon(M.ReadByte());
2328 FSpectator := M.ReadByte() <> 0;
2329 if FSpectator then
2330 begin
2331 if Pl = gPlayer1 then
2332 begin
2333 gLMSPID1 := UID;
2334 gPlayer1 := nil;
2335 end;
2336 if Pl = gPlayer2 then
2337 begin
2338 gLMSPID2 := UID;
2339 gPlayer2 := nil;
2340 end;
2341 end
2342 else
2343 begin
2344 if (gPlayer1 = nil) and (gLMSPID1 > 0) then
2345 gPlayer1 := g_Player_Get(gLMSPID1);
2346 if (gPlayer2 = nil) and (gLMSPID2 > 0) then
2347 gPlayer2 := g_Player_Get(gLMSPID2);
2348 end;
2349 FGhost := M.ReadByte() <> 0;
2350 FPhysics := M.ReadByte() <> 0;
2351 FNoRespawn := M.ReadByte() <> 0;
2352 OldJet := FJetpack;
2353 FJetpack := M.ReadByte() <> 0;
2354 OldFire := FFireTime;
2355 FFireTime := M.ReadLongInt();
2356 if (OldFire <= 0) and (FFireTime > 0) then
2357 g_Sound_PlayExAt('SOUND_IGNITE', Obj.X, Obj.Y);
2358 Flam := M.ReadByte() <> 0;
2359 if OldJet and not FJetpack then
2360 JetpackOff
2361 else if not OldJet and FJetpack then
2362 JetpackOn;
2363 if FFlaming and not Flam then
2364 FlamerOff;
2365 if Team <> NewTeam then
2366 Pl.ChangeTeam(NewTeam);
2367 end;
2369 Result := PID;
2370 end;
2372 function MC_RECV_PlayerDamage(var M: TMsg): Word;
2373 var
2374 PID: Word;
2375 Pl: TPlayer;
2376 Kind: Byte;
2377 Attacker, Value: Word;
2378 VX, VY: Integer;
2379 begin
2380 Result := 0;
2381 if not gGameOn then Exit;
2382 PID := M.ReadWord();
2383 Pl := g_Player_Get(PID);
2384 if Pl = nil then Exit;
2386 Kind := M.ReadByte();
2387 Attacker := M.ReadWord();
2388 Value := M.ReadWord();
2389 VX := M.ReadWord();
2390 VY := M.ReadWord();
2392 with Pl do
2393 Damage(Value, Attacker, VX, VY, Kind);
2395 Result := PID;
2396 end;
2398 function MC_RECV_PlayerDeath(var M: TMsg): Word;
2399 var
2400 PID: Word;
2401 Pl: TPlayer;
2402 KillType, DeathType: Byte;
2403 Attacker: Word;
2404 begin
2405 Result := 0;
2406 if not gGameOn then Exit;
2407 PID := M.ReadWord();
2408 Pl := g_Player_Get(PID);
2409 if Pl = nil then Exit;
2411 KillType := M.ReadByte();
2412 DeathType := M.ReadByte();
2413 Attacker := M.ReadWord();
2415 with Pl do
2416 begin
2417 Kill(KillType, Attacker, DeathType);
2418 SoftReset;
2419 end;
2420 end;
2422 function MC_RECV_PlayerDelete(var M: TMsg): Word;
2423 var
2424 PID: Word;
2425 Pl: TPlayer;
2426 begin
2427 PID := M.ReadWord();
2428 Pl := g_Player_Get(PID);
2429 Result := 0;
2430 if Pl = nil then Exit;
2432 g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [Pl.Name]), True);
2433 e_WriteLog('NET: Player ' + Pl.Name + ' [' + IntToStr(PID) + '] removed.', TMsgType.Notify);
2435 g_Player_Remove(PID);
2437 Result := PID;
2438 end;
2440 function MC_RECV_PlayerFire(var M: TMsg): Word;
2441 var
2442 PID: Word;
2443 Weap: Byte;
2444 Pl: TPlayer;
2445 X, Y, AX, AY: Integer;
2446 SHID: Integer;
2447 begin
2448 Result := 0;
2449 if not gGameOn then Exit;
2450 PID := M.ReadWord();
2451 Pl := g_Player_Get(PID);
2452 if Pl = nil then Exit;
2454 Weap := M.ReadByte();
2455 X := M.ReadLongInt();
2456 Y := M.ReadLongInt();
2457 AX := M.ReadLongInt();
2458 AY := M.ReadLongInt();
2459 SHID := M.ReadLongInt();
2461 with Pl do
2462 if alive then NetFire(Weap, X, Y, AX, AY, SHID);
2463 end;
2465 procedure MC_RECV_PlayerSettings(var M: TMsg);
2466 var
2467 TmpName: string;
2468 TmpModel: string;
2469 TmpColor: TRGB;
2470 TmpTeam: Byte;
2471 Pl: TPlayer;
2472 PID: Word;
2473 begin
2474 PID := M.ReadWord();
2475 Pl := g_Player_Get(PID);
2476 if Pl = nil then Exit;
2478 TmpName := M.ReadString();
2479 TmpModel := M.ReadString();
2480 TmpColor.R := M.ReadByte();
2481 TmpColor.G := M.ReadByte();
2482 TmpColor.B := M.ReadByte();
2483 TmpTeam := M.ReadByte();
2485 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
2486 begin
2487 Pl.ChangeTeam(TmpTeam);
2488 if gPlayer1 = Pl then
2489 gPlayer1Settings.Team := TmpTeam;
2490 if gPlayer2 = Pl then
2491 gPlayer2Settings.Team := TmpTeam;
2492 end else
2493 Pl.SetColor(TmpColor);
2495 if Pl.Name <> TmpName then
2496 begin
2497 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
2498 Pl.Name := TmpName;
2499 end;
2501 if TmpModel <> Pl.Model.Name then
2502 Pl.SetModel(TmpModel);
2503 end;
2505 // ITEM
2507 procedure MC_RECV_ItemSpawn(var M: TMsg);
2508 var
2509 ID: Word;
2510 AID: DWord;
2511 X, Y, VX, VY: Integer;
2512 T: Byte;
2513 Quiet, Fall{, Resp}: Boolean;
2514 Anim: TAnimation;
2515 it: PItem;
2516 begin
2517 if not gGameOn then Exit;
2518 ID := M.ReadWord();
2519 Quiet := M.ReadByte() <> 0;
2520 T := M.ReadByte();
2521 Fall := M.ReadByte() <> 0;
2522 {Resp :=} M.ReadByte();
2523 X := M.ReadLongInt();
2524 Y := M.ReadLongInt();
2525 VX := M.ReadLongInt();
2526 VY := M.ReadLongInt();
2528 g_Items_Create(X, Y, T and $7F, Fall, False, False, ID);
2529 if ((T and $80) <> 0) then g_Items_SetDrop(ID);
2531 it := g_Items_ByIdx(ID);
2532 it.Obj.Vel.X := VX;
2533 it.Obj.Vel.Y := VY;
2535 if not Quiet then
2536 begin
2537 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
2538 if g_Frames_Get(AID, 'FRAMES_ITEM_RESPAWN') then
2539 begin
2540 Anim := TAnimation.Create(AID, False, 4);
2541 g_GFX_OnceAnim(X+(it.Obj.Rect.Width div 2)-16, Y+(it.Obj.Rect.Height div 2)-16, Anim);
2542 Anim.Free();
2543 end;
2544 end;
2545 end;
2547 procedure MC_RECV_ItemDestroy(var M: TMsg);
2548 var
2549 ID: Word;
2550 Quiet: Boolean;
2551 begin
2552 if not gGameOn then Exit;
2553 ID := M.ReadWord();
2554 Quiet := M.ReadByte() <> 0;
2556 if not g_Items_ValidId(ID) then exit;
2558 if not Quiet then g_Items_EmitPickupSound(ID);
2560 g_Items_Remove(ID);
2561 end;
2563 // PANEL
2565 procedure MC_RECV_PanelTexture(var M: TMsg);
2566 var
2567 TP: TPanel;
2568 PGUID: Integer;
2569 Tex, Fr: Integer;
2570 Loop, Cnt: Byte;
2571 begin
2572 if not gGameOn then Exit;
2574 PGUID := Integer(M.ReadLongWord());
2575 Tex := M.ReadLongInt();
2576 Fr := M.ReadLongInt();
2577 Cnt := M.ReadByte();
2578 Loop := M.ReadByte();
2580 TP := g_Map_PanelByGUID(PGUID);
2581 if (TP <> nil) then
2582 begin
2583 // switch texture
2584 TP.SetTexture(Tex, Loop);
2585 TP.SetFrame(Fr, Cnt);
2586 end;
2587 end;
2589 procedure MC_RECV_PanelState(var M: TMsg);
2590 var
2591 PGUID: Integer;
2592 E: Boolean;
2593 Lift: Byte;
2594 X, Y, W, H: Integer;
2595 TP: TPanel;
2596 speedX, speedY, startX, startY, endX, endY: Integer;
2597 sizeSpX, sizeSpY, sizeEX, sizeEY: Integer;
2598 mpflags: Byte;
2599 begin
2600 if not gGameOn then Exit;
2602 PGUID := Integer(M.ReadLongWord());
2603 E := (M.ReadByte() <> 0);
2604 Lift := M.ReadByte();
2605 X := M.ReadLongInt();
2606 Y := M.ReadLongInt();
2607 W := M.ReadWord();
2608 H := M.ReadWord();
2609 // mplats
2610 speedX := M.ReadLongInt();
2611 speedY := M.ReadLongInt();
2612 startX := M.ReadLongInt();
2613 startY := M.ReadLongInt();
2614 endX := M.ReadLongInt();
2615 endY := M.ReadLongInt();
2616 sizeSpX := M.ReadLongInt();
2617 sizeSpY := M.ReadLongInt();
2618 sizeEX := M.ReadLongInt();
2619 sizeEY := M.ReadLongInt();
2620 mpflags := M.ReadByte(); // bit0: TP.movingActive; bit1: TP.moveOnce
2622 TP := g_Map_PanelByGUID(PGUID);
2623 if (TP = nil) then exit;
2625 // update lifts state
2626 if TP.isGLift then g_Map_SetLiftGUID(PGUID, Lift);
2628 // update enabled/disabled state for all panels
2629 if E then g_Map_EnableWallGUID(PGUID) else g_Map_DisableWallGUID(PGUID);
2631 // update panel position, as it can be moved (mplat)
2632 TP.X := X;
2633 TP.Y := Y;
2634 TP.Width := W;
2635 TP.Height := H;
2636 // update mplat state
2637 TP.movingSpeedX := speedX;
2638 TP.movingSpeedY := speedY;
2639 TP.movingStartX := startX;
2640 TP.movingStartY := startY;
2641 TP.movingEndX := endX;
2642 TP.movingEndY := endY;
2643 TP.sizeSpeedX := sizeSpX;
2644 TP.sizeSpeedY := sizeSpY;
2645 TP.sizeEndX := sizeEX;
2646 TP.sizeEndY := sizeEY;
2647 TP.movingActive := ((mpflags and 1) <> 0);
2648 TP.moveOnce := ((mpflags and 2) <> 0);
2649 // notify panel of it's position/size change, so it can fix other internal structures
2650 TP.positionChanged();
2651 end;
2653 // TRIGGERS
2655 procedure MC_RECV_TriggerSound(var M: TMsg);
2656 var
2657 SPlaying: Boolean;
2658 SPos, SID: LongWord;
2659 SCount: LongInt;
2660 I: Integer;
2661 begin
2662 if not gGameOn then Exit;
2663 if gTriggers = nil then Exit;
2665 SID := M.ReadLongWord();
2666 SPlaying := M.ReadByte() <> 0;
2667 SPos := M.ReadLongWord();
2668 SCount := M.ReadLongInt();
2670 for I := Low(gTriggers) to High(gTriggers) do
2671 if gTriggers[I].TriggerType = TRIGGER_SOUND then
2672 if gTriggers[I].ClientID = SID then
2673 with gTriggers[I] do
2674 begin
2675 if Sound <> nil then
2676 begin
2677 if SPlaying then
2678 begin
2679 if tgcLocal then
2680 Sound.PlayVolumeAt(X+(Width div 2), Y+(Height div 2), tgcVolume/255.0)
2681 else
2682 Sound.PlayPanVolume((tgcPan-127.0)/128.0, tgcVolume/255.0);
2683 Sound.SetPosition(SPos);
2684 end
2685 else
2686 if Sound.IsPlaying then Sound.Stop;
2687 end;
2689 SoundPlayCount := SCount;
2690 end;
2691 end;
2693 procedure MC_RECV_TriggerMusic(var M: TMsg);
2694 var
2695 MName: string;
2696 MPlaying: Boolean;
2697 MPos: LongWord;
2698 MPaused: Boolean;
2699 begin
2700 if not gGameOn then Exit;
2702 MName := M.ReadString();
2703 MPlaying := M.ReadByte() <> 0;
2704 MPos := M.ReadLongWord();
2705 MPaused := M.ReadByte() <> 0;
2706 MPos := MPos+1; //k8: stfu, fpc!
2708 if MPlaying then
2709 begin
2710 gMusic.SetByName(MName);
2711 gMusic.Play(True);
2712 // gMusic.SetPosition(MPos);
2713 gMusic.SpecPause := MPaused;
2714 end
2715 else
2716 if gMusic.IsPlaying then gMusic.Stop;
2717 end;
2719 // MONSTERS
2721 procedure MC_RECV_MonsterSpawn(var M: TMsg);
2722 var
2723 ID: Word;
2724 MType, MState, MDir, MAnim, MBehav: Byte;
2725 X, Y, VX, VY, MTargTime, MHealth, MAmmo, MSleep: Integer;
2726 MTarg: Word;
2727 Mon: TMonster;
2728 begin
2729 ID := M.ReadWord();
2730 Mon := g_Monsters_ByUID(ID);
2731 if Mon <> nil then
2732 Exit;
2734 MType := M.ReadByte();
2735 MState := M.ReadByte();
2736 MAnim := M.ReadByte();
2737 MTarg := M.ReadWord();
2738 MTargTime := M.ReadLongInt();
2739 MBehav := M.ReadByte();
2740 MSleep := M.ReadLongInt();
2741 MHealth := M.ReadLongInt();
2742 MAmmo := M.ReadLongInt();
2744 X := M.ReadLongInt();
2745 Y := M.ReadLongInt();
2746 VX := M.ReadLongInt();
2747 VY := M.ReadLongInt();
2748 MDir := M.ReadByte();
2750 g_Monsters_Create(MType, X, Y, TDirection(MDir), False, ID);
2751 Mon := g_Monsters_ByUID(ID);
2752 if Mon = nil then
2753 Exit;
2755 with Mon do
2756 begin
2758 MonsterAnim := MAnim;
2759 MonsterTargetUID := MTarg;
2760 MonsterTargetTime := MTargTime;
2761 MonsterBehaviour := MBehav;
2762 MonsterSleep := MSleep;
2763 MonsterAmmo := MAmmo;
2764 SetHealth(MHealth);
2766 SetState(MState);
2768 setPosition(X, Y); // this will call positionChanged();
2769 GameVelX := VX;
2770 GameVelY := VY;
2771 end;
2772 end;
2774 procedure MC_RECV_MonsterPos(var M: TMsg);
2775 var
2776 Mon: TMonster;
2777 ID: Word;
2778 X, Y: Integer;
2779 begin
2780 ID := M.ReadWord();
2781 Mon := g_Monsters_ByUID(ID);
2782 if Mon = nil then
2783 Exit;
2785 with Mon do
2786 begin
2787 X := M.ReadLongInt();
2788 Y := M.ReadLongInt();
2789 Mon.setPosition(X, Y); // this will call `positionChanged()`
2790 GameVelX := M.ReadLongInt();
2791 GameVelY := M.ReadLongInt();
2792 GameDirection := TDirection(M.ReadByte());
2793 end;
2794 end;
2796 procedure MC_RECV_MonsterState(var M: TMsg);
2797 var
2798 ID, OldFire: Integer;
2799 MState, MFAnm: Byte;
2800 Mon: TMonster;
2801 AnimRevert: Boolean;
2802 begin
2803 ID := M.ReadWord();
2804 Mon := g_Monsters_ByUID(ID);
2805 if Mon = nil then Exit;
2807 MState := M.ReadByte();
2808 MFAnm := M.ReadByte();
2810 with Mon do
2811 begin
2812 MonsterTargetUID := M.ReadWord();
2813 MonsterTargetTime := M.ReadLongInt();
2814 MonsterSleep := M.ReadLongInt();
2815 MonsterHealth := M.ReadLongInt();
2816 MonsterAmmo := M.ReadLongInt();
2817 MonsterPain := M.ReadLongInt();
2818 AnimRevert := M.ReadByte() <> 0;
2819 OldFire := FFireTime;
2820 FFireTime := M.ReadLongInt();
2821 if (OldFire <= 0) and (FFireTime > 0) then
2822 g_Sound_PlayExAt('SOUND_IGNITE', Obj.X, Obj.Y);
2823 RevertAnim(AnimRevert);
2825 if MonsterState <> MState then
2826 begin
2827 if (MState = MONSTATE_GO) and (MonsterState = MONSTATE_SLEEP) then WakeUpSound();
2828 if (MState = MONSTATE_DIE) then DieSound();
2829 if (MState = MONSTATE_PAIN) then MakeBloodSimple(Min(200, MonsterPain));
2830 if (MState = MONSTATE_ATTACK) then kick(nil);
2831 if (MState = MONSTATE_DEAD) then SetDeadAnim();
2833 SetState(MState, MFAnm);
2834 end;
2835 end;
2836 end;
2838 procedure MC_RECV_MonsterShot(var M: TMsg);
2839 var
2840 ID: Integer;
2841 Mon: TMonster;
2842 X, Y, VX, VY: Integer;
2843 begin
2844 ID := M.ReadWord();
2846 Mon := g_Monsters_ByUID(ID);
2847 if Mon = nil then Exit;
2849 X := M.ReadLongInt();
2850 Y := M.ReadLongInt();
2851 VX := M.ReadLongInt();
2852 VY := M.ReadLongInt();
2854 Mon.ClientAttack(X, Y, VX, VY);
2855 end;
2857 procedure MC_RECV_MonsterDelete(var M: TMsg);
2858 var
2859 ID: Integer;
2860 Mon: TMonster;
2861 begin
2862 ID := M.ReadWord();
2863 Mon := g_Monsters_ByUID(ID);
2864 if Mon = nil then Exit;
2865 Mon.SetState(5);
2866 Mon.MonsterRemoved := True;
2867 end;
2869 procedure MC_RECV_TimeSync(var M: TMsg);
2870 var
2871 Time: LongWord;
2872 begin
2873 Time := M.ReadLongWord();
2875 if gState = STATE_INTERCUSTOM then
2876 gServInterTime := Min(Time, 255);
2877 end;
2879 procedure MC_RECV_VoteEvent(var M: TMsg);
2880 var
2881 EvID: Byte;
2882 Str1, Str2: string;
2883 Int1, Int2: SmallInt;
2884 begin
2885 EvID := M.ReadByte();
2886 Int1 := M.ReadSmallInt();
2887 Int2 := M.ReadSmallInt();
2888 Str1 := M.ReadString();
2889 Str2 := M.ReadString();
2891 case EvID of
2892 NET_VE_STARTED:
2893 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_STARTED], [Str1, Str2, Int1]), True);
2894 NET_VE_PASSED:
2895 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_PASSED], [Str1]), True);
2896 NET_VE_FAILED:
2897 g_Console_Add(_lc[I_MESSAGE_VOTE_FAILED], True);
2898 NET_VE_VOTE:
2899 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Str1, Int1, Int2]), True);
2900 NET_VE_INPROGRESS:
2901 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_INPROGRESS], [Str1]), True);
2902 end;
2903 end;
2905 // CLIENT SEND
2907 procedure MC_SEND_Info(Password: string);
2908 begin
2909 NetOut.Clear();
2911 NetOut.Write(Byte(NET_MSG_INFO));
2912 NetOut.Write(GAME_VERSION);
2913 NetOut.Write(Password);
2914 NetOut.Write(gPlayer1Settings.Name);
2915 NetOut.Write(gPlayer1Settings.Model);
2916 NetOut.Write(gPlayer1Settings.Color.R);
2917 NetOut.Write(gPlayer1Settings.Color.G);
2918 NetOut.Write(gPlayer1Settings.Color.B);
2919 NetOut.Write(gPlayer1Settings.Team);
2921 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2922 end;
2924 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
2925 begin
2926 NetOut.Write(Byte(NET_MSG_CHAT));
2927 NetOut.Write(Txt);
2928 NetOut.Write(Mode);
2930 g_Net_Client_Send(True, NET_CHAN_CHAT);
2931 end;
2933 procedure MC_SEND_PlayerPos();
2934 var
2935 kByte: Word;
2936 Predict: Boolean;
2937 strafeDir: Byte;
2938 WeaponSelect: Word = 0;
2939 i: Integer;
2940 begin
2941 if not gGameOn then Exit;
2942 if gPlayers = nil then Exit;
2943 if gPlayer1 = nil then Exit;
2945 kByte := 0;
2946 Predict := NetPredictSelf; // and (not NetGotKeys);
2948 if (not gConsoleShow) and (not gChatShow) and (g_ActiveWindow = nil) then
2949 begin
2950 strafeDir := P1MoveButton shr 4;
2951 P1MoveButton := P1MoveButton and $0F;
2953 if gPlayerAction[0, ACTION_MOVELEFT] and (not gPlayerAction[0, ACTION_MOVERIGHT]) then
2954 P1MoveButton := 1
2955 else if (not gPlayerAction[0, ACTION_MOVELEFT]) and gPlayerAction[0, ACTION_MOVERIGHT] then
2956 P1MoveButton := 2
2957 else if (not gPlayerAction[0, ACTION_MOVELEFT]) and (not gPlayerAction[0, ACTION_MOVERIGHT]) then
2958 P1MoveButton := 0;
2960 // strafing
2961 if gPlayerAction[0, ACTION_STRAFE] then
2962 begin
2963 // new strafe mechanics
2964 if (strafeDir = 0) then
2965 strafeDir := P1MoveButton; // start strafing
2966 // now set direction according to strafe (reversed)
2967 if (strafeDir = 2) then
2968 gPlayer1.SetDirection(TDirection.D_LEFT)
2969 else if (strafeDir = 1) then
2970 gPlayer1.SetDirection(TDirection.D_RIGHT)
2971 end
2972 else
2973 begin
2974 strafeDir := 0; // not strafing anymore
2975 if (P1MoveButton = 2) and gPlayerAction[0, ACTION_MOVELEFT] then
2976 gPlayer1.SetDirection(TDirection.D_LEFT)
2977 else if (P1MoveButton = 1) and gPlayerAction[0, ACTION_MOVERIGHT] then
2978 gPlayer1.SetDirection(TDirection.D_RIGHT)
2979 else if P1MoveButton <> 0 then
2980 gPlayer1.SetDirection(TDirection(P1MoveButton-1));
2981 end;
2983 gPlayer1.ReleaseKeys;
2984 if P1MoveButton = 1 then
2985 begin
2986 kByte := kByte or NET_KEY_LEFT;
2987 if Predict then gPlayer1.PressKey(KEY_LEFT, 10000);
2988 end;
2989 if P1MoveButton = 2 then
2990 begin
2991 kByte := kByte or NET_KEY_RIGHT;
2992 if Predict then gPlayer1.PressKey(KEY_RIGHT, 10000);
2993 end;
2994 if gPlayerAction[0, ACTION_LOOKUP] then
2995 begin
2996 kByte := kByte or NET_KEY_UP;
2997 gPlayer1.PressKey(KEY_UP, 10000);
2998 end;
2999 if gPlayerAction[0, ACTION_LOOKDOWN] then
3000 begin
3001 kByte := kByte or NET_KEY_DOWN;
3002 gPlayer1.PressKey(KEY_DOWN, 10000);
3003 end;
3004 if gPlayerAction[0, ACTION_JUMP] then
3005 begin
3006 kByte := kByte or NET_KEY_JUMP;
3007 // gPlayer1.PressKey(KEY_JUMP, 10000); // TODO: Make a prediction option
3008 end;
3009 if gPlayerAction[0, ACTION_ATTACK] then kByte := kByte or NET_KEY_FIRE;
3010 if gPlayerAction[0, ACTION_ACTIVATE] then kByte := kByte or NET_KEY_OPEN;
3011 if gPlayerAction[0, ACTION_WEAPNEXT] then kByte := kByte or NET_KEY_NW;
3012 if gPlayerAction[0, ACTION_WEAPPREV] then kByte := kByte or NET_KEY_PW;
3014 gPlayerAction[0, ACTION_WEAPNEXT] := False; // HACK, remove after readyweaon&pendinweapon implementation
3015 gPlayerAction[0, ACTION_WEAPPREV] := False; // HACK, remove after readyweaon&pendinweapon implementation
3017 for i := WP_FIRST to WP_LAST do
3018 begin
3019 if gSelectWeapon[0, i] then
3020 begin
3021 WeaponSelect := WeaponSelect or Word(1 shl i);
3022 gSelectWeapon[0, i] := False
3023 end
3024 end;
3026 // fix movebutton state
3027 P1MoveButton := P1MoveButton or (strafeDir shl 4);
3028 end
3029 else
3030 kByte := NET_KEY_CHAT;
3032 NetOut.Write(Byte(NET_MSG_PLRPOS));
3033 NetOut.Write(gTime);
3034 NetOut.Write(kByte);
3035 NetOut.Write(Byte(gPlayer1.Direction));
3036 NetOut.Write(WeaponSelect);
3037 //e_WriteLog(Format('S:ws=%d', [WeaponSelect]), MSG_WARNING);
3038 g_Net_Client_Send(True, NET_CHAN_PLAYERPOS);
3040 //kBytePrev := kByte;
3041 //kDirPrev := gPlayer1.Direction;
3042 end;
3044 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
3045 begin
3046 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
3047 NetOut.Write(Byte(Start));
3048 NetOut.Write(Command);
3049 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3050 end;
3052 procedure MC_SEND_PlayerSettings();
3053 begin
3054 NetOut.Write(Byte(NET_MSG_PLRSET));
3055 NetOut.Write(gPlayer1Settings.Name);
3056 NetOut.Write(gPlayer1Settings.Model);
3057 NetOut.Write(gPlayer1Settings.Color.R);
3058 NetOut.Write(gPlayer1Settings.Color.G);
3059 NetOut.Write(gPlayer1Settings.Color.B);
3060 NetOut.Write(gPlayer1Settings.Team);
3062 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3063 end;
3065 procedure MC_SEND_FullStateRequest();
3066 begin
3067 NetOut.Write(Byte(NET_MSG_REQFST));
3069 g_Net_Client_Send(True, NET_CHAN_SERVICE);
3070 end;
3072 procedure MC_SEND_CheatRequest(Kind: Byte);
3073 begin
3074 NetOut.Write(Byte(NET_MSG_CHEAT));
3075 NetOut.Write(Kind);
3077 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3078 end;
3079 procedure MC_SEND_RCONPassword(Password: string);
3080 begin
3081 NetOut.Write(Byte(NET_MSG_RCON_AUTH));
3082 NetOut.Write(Password);
3084 g_Net_Client_Send(True, NET_CHAN_SERVICE);
3085 end;
3086 procedure MC_SEND_RCONCommand(Cmd: string);
3087 begin
3088 NetOut.Write(Byte(NET_MSG_RCON_CMD));
3089 NetOut.Write(Cmd);
3091 g_Net_Client_Send(True, NET_CHAN_SERVICE);
3092 end;
3095 end.