DEADSOFTWARE

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