DEADSOFTWARE

Game: Weapon autoswitch by travi$
[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;
34 NET_MSG_FLAGPOS= 110;
36 NET_MSG_PLR = 111;
37 NET_MSG_PLRPOS = 112;
38 NET_MSG_PLRSTA = 113;
39 NET_MSG_PLRDEL = 114;
40 NET_MSG_PLRDMG = 115;
41 NET_MSG_PLRDIE = 116;
42 NET_MSG_PLRFIRE= 117;
43 NET_MSG_PLRSET = 119;
44 NET_MSG_CHEAT = 120;
46 NET_MSG_ISPAWN = 121;
47 NET_MSG_IDEL = 122;
48 NET_MSG_IPOS = 123;
50 NET_MSG_MSPAWN = 131;
51 NET_MSG_MPOS = 132;
52 NET_MSG_MSTATE = 133;
53 NET_MSG_MSHOT = 134;
54 NET_MSG_MDEL = 135;
56 NET_MSG_PSTATE = 141;
57 NET_MSG_PTEX = 142;
59 NET_MSG_TSOUND = 151;
60 NET_MSG_TMUSIC = 152;
62 NET_MSG_SHDEL = 161;
63 NET_MSG_SHADD = 162;
64 NET_MSG_SHPOS = 163;
66 NET_MSG_RCON_AUTH = 191;
67 NET_MSG_RCON_CMD = 192;
68 NET_MSG_TIME_SYNC = 194;
69 NET_MSG_VOTE_EVENT = 195;
71 {
72 NET_MSG_MAP_REQUEST = 201;
73 NET_MSG_MAP_RESPONSE = 202;
74 NET_MSG_RES_REQUEST = 203;
75 NET_MSG_RES_RESPONSE = 204;
76 }
78 NET_CHAT_SYSTEM = 0;
79 NET_CHAT_PLAYER = 1;
80 NET_CHAT_TEAM = 2;
82 NET_RCON_NOAUTH = 0;
83 NET_RCON_PWGOOD = 1;
84 NET_RCON_PWBAD = 2;
86 NET_GFX_SPARK = 1;
87 NET_GFX_TELE = 2;
88 NET_GFX_RESPAWN = 3;
89 NET_GFX_FIRE = 4;
90 NET_GFX_EXPLODE = 5;
91 NET_GFX_BFGEXPL = 6;
92 NET_GFX_BFGHIT = 7;
93 NET_GFX_SHELL1 = 8;
94 NET_GFX_SHELL2 = 9;
95 NET_GFX_SHELL3 = 10;
97 NET_EV_MAPSTART = 1;
98 NET_EV_MAPEND = 2;
99 NET_EV_CHANGE_TEAM = 3;
100 NET_EV_PLAYER_KICK = 4;
101 NET_EV_PLAYER_BAN = 5;
102 NET_EV_LMS_WARMUP = 6;
103 NET_EV_LMS_SURVIVOR = 7;
104 NET_EV_RCON = 8;
105 NET_EV_BIGTEXT = 9;
106 NET_EV_SCORE = 10;
107 NET_EV_SCORE_MSG = 11;
108 NET_EV_LMS_START = 12;
109 NET_EV_LMS_WIN = 13;
110 NET_EV_TLMS_WIN = 14;
111 NET_EV_LMS_LOSE = 15;
112 NET_EV_LMS_DRAW = 16;
113 NET_EV_KILLCOMBO = 17;
114 NET_EV_PLAYER_TOUCH = 18;
115 NET_EV_SECRET = 19;
116 NET_EV_INTER_READY = 20;
117 NET_EV_LMS_NOSPAWN = 21;
119 NET_VE_STARTED = 1;
120 NET_VE_PASSED = 2;
121 NET_VE_FAILED = 3;
122 NET_VE_VOTE = 4;
123 NET_VE_REVOKE = 5;
124 NET_VE_INPROGRESS = 6;
126 NET_FLAG_GET = 1;
127 NET_FLAG_DROP = 2;
128 NET_FLAG_CAP = 3;
129 NET_FLAG_RETURN = 4;
131 NET_CHEAT_SUICIDE = 1;
132 NET_CHEAT_SPECTATE = 2;
133 NET_CHEAT_READY = 3;
134 NET_CHEAT_DROPFLAG = 4;
136 NET_MAX_DIFFTIME = 5000 div 36;
138 // HOST MESSAGES
140 procedure MH_MalformedPacket(C: pTNetClient);
141 procedure MH_ProcessFirstSpawn (C: pTNetClient);
143 procedure MH_RECV_Info(C: pTNetClient; var M: TMsg);
144 procedure MH_RECV_Chat(C: pTNetClient; var M: TMsg);
145 procedure MH_RECV_FullStateRequest(C: pTNetClient; var M: TMsg);
146 function MH_RECV_PlayerPos(C: pTNetClient; var M: TMsg): Word;
147 procedure MH_RECV_PlayerSettings(C: pTNetClient; var M: TMsg);
148 procedure MH_RECV_CheatRequest(C: pTNetClient; var M: TMsg);
149 procedure MH_RECV_RCONPassword(C: pTNetClient; var M: TMsg);
150 procedure MH_RECV_RCONCommand(C: pTNetClient; var M: TMsg);
151 //procedure MH_RECV_MapRequest(C: pTNetClient; var M: TMsg);
152 //procedure MH_RECV_ResRequest(C: pTNetClient; var M: TMsg);
153 procedure MH_RECV_Vote(C: pTNetClient; var M: TMsg);
155 // GAME
156 procedure MH_SEND_Everything(CreatePlayers: Boolean {= False}; ID: Integer {= NET_EVERYONE});
157 procedure MH_SEND_Info(ID: Byte);
158 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
159 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
160 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
161 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
162 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
163 procedure MH_SEND_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
164 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
165 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
166 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
167 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
168 procedure MH_SEND_FlagPos(Flag: Byte; ID: Integer = NET_EVERYONE);
169 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
170 // PLAYER
171 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
172 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
173 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
174 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
175 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
176 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
177 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
178 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
179 // ITEM
180 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
181 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
182 procedure MH_SEND_ItemPos(IID: Word; ID: Integer = NET_EVERYONE);
183 // PANEL
184 procedure MH_SEND_PanelTexture(PGUID: Integer; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
185 procedure MH_SEND_PanelState(PGUID: Integer; ID: Integer = NET_EVERYONE);
186 // MONSTER
187 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
188 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
189 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
190 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
191 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
192 // TRIGGER
193 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
194 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
195 // MISC
196 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
197 procedure MH_SEND_VoteEvent(EvType: Byte;
198 StrArg1: string = 'a'; StrArg2: string = 'b';
199 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
200 ID: Integer = NET_EVERYONE);
202 // CLIENT MESSAGES //
204 // GAME
205 procedure MC_RECV_Chat(var M: TMsg);
206 procedure MC_RECV_Effect(var M: TMsg);
207 procedure MC_RECV_Sound(var M: TMsg);
208 procedure MC_RECV_GameStats(var M: TMsg);
209 procedure MC_RECV_CoopStats(var M: TMsg);
210 procedure MC_RECV_GameEvent(var M: TMsg);
211 procedure MC_RECV_FlagEvent(var M: TMsg);
212 procedure MC_RECV_FlagPos(var M: TMsg);
213 procedure MC_RECV_GameSettings(var M: TMsg);
214 // PLAYER
215 function MC_RECV_PlayerCreate(var M: TMsg): Word;
216 function MC_RECV_PlayerPos(var M: TMsg): Word;
217 function MC_RECV_PlayerStats(var M: TMsg): Word;
218 function MC_RECV_PlayerDelete(var M: TMsg): Word;
219 function MC_RECV_PlayerDamage(var M: TMsg): Word;
220 function MC_RECV_PlayerDeath(var M: TMsg): Word;
221 function MC_RECV_PlayerFire(var M: TMsg): Word;
222 procedure MC_RECV_PlayerSettings(var M: TMsg);
223 // ITEM
224 procedure MC_RECV_ItemSpawn(var M: TMsg);
225 procedure MC_RECV_ItemDestroy(var M: TMsg);
226 procedure MC_RECV_ItemPos(var M: TMsg);
227 // PANEL
228 procedure MC_RECV_PanelTexture(var M: TMsg);
229 procedure MC_RECV_PanelState(var M: TMsg);
230 // MONSTER
231 procedure MC_RECV_MonsterSpawn(var M: TMsg);
232 procedure MC_RECV_MonsterPos(var M: TMsg);
233 procedure MC_RECV_MonsterState(var M: TMsg);
234 procedure MC_RECV_MonsterShot(var M: TMsg);
235 procedure MC_RECV_MonsterDelete(var M: TMsg);
236 // SHOT
237 procedure MC_RECV_CreateShot(var M: TMsg);
238 procedure MC_RECV_UpdateShot(var M: TMsg);
239 procedure MC_RECV_DeleteShot(var M: TMsg);
240 // TRIGGER
241 procedure MC_RECV_TriggerSound(var M: TMsg);
242 procedure MC_RECV_TriggerMusic(var M: TMsg);
243 // MISC
244 procedure MC_RECV_TimeSync(var M: TMsg);
245 procedure MC_RECV_VoteEvent(var M: TMsg);
246 // SERVICE
247 procedure MC_SEND_Info(Password: string);
248 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
249 procedure MC_SEND_PlayerPos();
250 procedure MC_SEND_FullStateRequest();
251 procedure MC_SEND_PlayerSettings();
252 procedure MC_SEND_CheatRequest(Kind: Byte);
253 procedure MC_SEND_RCONPassword(Password: string);
254 procedure MC_SEND_RCONCommand(Cmd: string);
255 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
256 // DOWNLOAD
257 //procedure MC_SEND_MapRequest();
258 //procedure MC_SEND_ResRequest(const resName: AnsiString);
261 type
262 TExternalResourceInfo = record
263 Name: string[255];
264 md5: TMD5Digest;
265 end;
267 TResDataMsg = record
268 MsgId: Byte;
269 FileSize: Integer;
270 FileData: AByte;
271 end;
273 TMapDataMsg = record
274 MsgId: Byte;
275 FileSize: Integer;
276 FileData: AByte;
277 ExternalResources: array of TExternalResourceInfo;
278 end;
280 function IsValidFileName(const S: String): Boolean;
281 function IsValidFilePath(const S: String): Boolean;
284 implementation
286 uses
287 Math, ENet, e_input, e_graphics, e_log,
288 g_textures, g_gfx, g_sound, g_console, g_basic, g_options, g_main,
289 g_game, g_player, g_map, g_panel, g_items, g_weapons, g_phys, g_gui,
290 g_language, g_monsters, g_netmaster, utils, wadreader, MAPDEF;
292 const
293 NET_KEY_LEFT = 1 shl 0;
294 NET_KEY_RIGHT = 1 shl 1;
295 NET_KEY_UP = 1 shl 2;
296 NET_KEY_DOWN = 1 shl 3;
297 NET_KEY_JUMP = 1 shl 4;
298 NET_KEY_FIRE = 1 shl 5;
299 NET_KEY_OPEN = 1 shl 6;
300 NET_KEY_CHAT = 1 shl 7;
301 NET_KEY_FORCEDIR = 1 shl 8;
303 //var
304 //kBytePrev: Word = 0;
305 //kDirPrev: TDirection = D_LEFT;
306 //HostGameTime: Word = 0;
309 function IsValidFileName(const S: String): Boolean;
310 const
311 Forbidden: set of Char = ['<', '>', '|', '"', ':', '*', '?'];
312 var
313 I: Integer;
314 begin
315 Result := S <> '';
316 for I := 1 to Length(S) do
317 Result := Result and (not(S[I] in Forbidden));
318 end;
320 function IsValidFilePath(const S: String): Boolean;
321 var
322 I: Integer;
323 begin
324 Result := False;
325 if not IsValidFileName(S) then exit;
326 if FileExists(S) then exit;
327 I := LastDelimiter('\/', S);
328 if (I > 0) then
329 if (not DirectoryExists(Copy(S, 1, I-1))) then
330 exit;
331 Result := True;
332 end;
335 // HOST MESSAGES //
338 // GAME
340 procedure MH_MalformedPacket(C: pTNetClient);
341 begin
342 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
343 _lc[I_NET_DISC_PROTOCOL]);
344 enet_peer_disconnect(C^.Peer, NET_DISC_PROTOCOL);
345 end;
347 procedure MH_RECV_Chat(C: pTNetClient; var M: TMsg);
348 var
349 Txt: string;
350 Mode: Byte;
351 PID: Word;
352 Pl: TPlayer;
353 Err: Boolean;
354 begin
355 PID := C^.Player;
356 Pl := g_Player_Get(PID);
358 Err := False;
359 try
360 Txt := M.ReadString();
361 Mode := M.ReadByte();
362 except
363 Err := True;
364 end;
366 if Err then begin MH_MalformedPacket(C); Exit; end;
368 if (Mode = NET_CHAT_SYSTEM) then
369 Mode := NET_CHAT_PLAYER; // prevent sending system messages from clients
370 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
371 Mode := NET_CHAT_PLAYER; // revert to player chat in non-team game
373 if Pl = nil then
374 MH_SEND_Chat(Txt, Mode)
375 else
376 MH_SEND_Chat(Pl.Name + ': ' + Txt, Mode, IfThen(Mode = NET_CHAT_TEAM, Pl.Team, NET_EVERYONE));
377 end;
379 procedure MH_RECV_Info(C: pTNetClient; var M: TMsg);
380 var
381 Ver, PName, Model, Pw: string;
382 R, G, B, T: Byte;
383 PID: Word;
384 Color: TRGB;
385 I: Integer;
386 Err: Boolean;
387 begin
388 Err := False;
389 try
390 Ver := M.ReadString();
391 Pw := M.ReadString();
392 PName := M.ReadString();
393 Model := M.ReadString();
394 R := M.ReadByte();
395 G := M.ReadByte();
396 B := M.ReadByte();
397 T := M.ReadByte();
398 except
399 Err := True;
400 end;
402 if Err then begin MH_MalformedPacket(C); Exit; end;
404 if Ver <> GAME_VERSION then
405 begin
406 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
407 _lc[I_NET_DISC_VERSION]);
408 enet_peer_disconnect(C^.Peer, NET_DISC_VERSION);
409 Exit;
410 end;
412 if g_Net_IsHostBanned(C^.Peer^.address.host) then
413 begin
414 if g_Net_IsHostBanned(C^.Peer^.address.host, True) then
415 begin
416 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
417 _lc[I_NET_DISC_BAN]);
418 enet_peer_disconnect(C^.Peer, NET_DISC_BAN);
419 end
420 else
421 begin
422 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
423 _lc[I_NET_DISC_BAN]);
424 enet_peer_disconnect(C^.Peer, NET_DISC_TEMPBAN);
425 end;
426 Exit;
427 end;
429 if NetPassword <> '' then
430 if AnsiLowerCase(NetPassword) <> AnsiLowerCase(Pw) then
431 begin
432 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
433 _lc[I_NET_DISC_PASSWORD]);
434 enet_peer_disconnect(C^.Peer, NET_DISC_PASSWORD);
435 Exit;
436 end;
438 if (C^.Player <> 0) then
439 begin
440 // already received info
441 g_Net_Penalize(C, 'client info spam');
442 Exit;
443 end;
445 Color.R := R;
446 Color.B := B;
447 Color.G := G;
449 PID := g_Player_Create(Model, Color, T, False);
450 with g_Player_Get(PID) do
451 begin
452 Name := PName;
453 Reset(True);
454 end;
456 C^.Player := PID;
457 C^.WaitForFirstSpawn := false;
458 C^.AuthTime := 0;
460 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
461 e_WriteLog('NET: Client ' + PName + ' [' + IntToStr(C^.ID) +
462 '] connected. Assigned player #' + IntToStr(PID) + '.', TMsgType.Notify);
464 MH_SEND_Info(C^.ID);
466 with g_Player_Get(PID) do
467 begin
468 Name := PName;
469 FClientID := C^.ID;
470 // round in progress, don't spawn
471 e_LogWritefln('*** client #%u (cid #%u) authenticated...', [C.ID, C.Player]);
472 //e_LogWritefln('spawning player with pid #%u...', [PID]);
473 //Respawn(gGameSettings.GameType = GT_SINGLE);
474 //k8: no, do not spawn a player yet, wait for "request full state" packet
475 Lives := 0;
476 Spectate;
477 FNoRespawn := True;
478 // `FWantsInGame` seems to mean "spawn the player on the next occasion".
479 // that is, if we'll set it to `true`, the player can be spawned after
480 // warmup time ran out, for example, regardless of the real player state.
481 // also, this seems to work only for the initial connection. further
482 // map changes could initiate resource downloading, but the player will
483 // be spawned immediately.
484 // the proper solution will require another player state, "ephemeral".
485 // the player should start any map in "ephemeral" state, and turned into
486 // real mobj only when they sent a special "i am ready" packet. this packet
487 // must be sent after receiving the full state, so the player will get a full
488 // map view before going into game.
489 FWantsInGame := false;
490 C^.WaitForFirstSpawn := true;
491 end;
493 //if not C^.WaitForFirstSpawn then
494 begin
495 for I := Low(NetClients) to High(NetClients) do
496 begin
497 if NetClients[I].ID = C^.ID then Continue;
498 MH_SEND_PlayerCreate(PID, NetClients[I].ID);
499 MH_SEND_PlayerPos(True, PID, NetClients[I].ID);
500 MH_SEND_PlayerStats(PID, NetClients[I].ID);
501 end;
502 end;
504 if gState in [STATE_INTERCUSTOM, STATE_FOLD] then
505 MH_SEND_GameEvent(NET_EV_MAPEND, 0, 'N', C^.ID);
507 if NetUseMaster then
508 begin
509 //g_Net_Slist_Update;
510 g_Net_Slist_Pulse();
511 end;
512 end;
515 procedure MH_ProcessFirstSpawn (C: pTNetClient);
516 var
517 plr: TPlayer;
518 begin
519 if not C.WaitForFirstSpawn then exit;
520 plr := g_Player_Get(C^.Player);
521 if not assigned(plr) then exit;
522 g_Net_Slist_ServerPlayerComes();
523 e_LogWritefln('*** client #%u (cid #%u) first spawn', [C.ID, C.Player]);
524 C.WaitForFirstSpawn := false;
525 plr.FNoRespawn := false;
526 plr.FWantsInGame := true; // TODO: look into this later
528 if (gGameSettings.MaxLives > 0) and (gLMSRespawn = LMS_RESPAWN_NONE) then
529 begin
530 plr.Spectate;
531 MH_SEND_GameEvent(NET_EV_LMS_NOSPAWN, 0, 'N', C.ID);
532 end
533 else
534 begin
535 plr.Respawn(False);
536 if gLMSRespawn > LMS_RESPAWN_NONE then
537 MH_SEND_GameEvent(NET_EV_LMS_WARMUP, gLMSRespawnTime - gTime, 'N', C.ID);
538 end;
539 end;
542 procedure MH_RECV_FullStateRequest(C: pTNetClient; var M: TMsg);
543 begin
544 //e_LogWritefln('*** client #%u (cid #%u) full state request', [C.ID, C.Player]);
545 if gGameOn then
546 begin
547 MH_SEND_Everything((C^.State = NET_STATE_AUTH), C^.ID)
548 end
549 else
550 begin
551 C^.RequestedFullUpdate := True;
552 end;
553 end;
555 // PLAYER
557 function MH_RECV_PlayerPos(C: pTNetClient; var M: TMsg): Word;
558 var
559 Dir, i: Byte;
560 WeaponAct: Byte;
561 WeaponSelect: Word;
562 PID: Word;
563 kByte: Word;
564 Pl: TPlayer;
565 GT: LongWord;
566 Err: Boolean;
567 begin
568 Result := 0;
569 Err := False;
570 if not gGameOn then Exit;
572 try
573 GT := M.ReadLongWord();
574 except
575 Err := True;
576 end;
578 if Err then begin MH_MalformedPacket(C); Exit; end;
580 PID := C^.Player;
581 Pl := g_Player_Get(PID);
582 if Pl = nil then
583 Exit;
585 if (GT > gTime + NET_MAX_DIFFTIME) or (GT < Pl.NetTime) then Exit;
587 with Pl do
588 begin
589 NetTime := GT;
590 try
591 kByte := M.ReadWord();
592 Dir := M.ReadByte();
593 WeaponAct := M.ReadByte();
594 WeaponSelect := M.ReadWord();
595 except
596 Err := True;
597 end;
599 if Err then begin MH_MalformedPacket(C); Exit; end;
601 //e_WriteLog(Format('R:ws=%d', [WeaponSelect]), MSG_WARNING);
602 if Direction <> TDirection(Dir) then
603 JustTeleported := False;
605 SetDirection(TDirection(Dir));
606 ReleaseKeys;
608 if kByte = NET_KEY_CHAT then
609 begin
610 PressKey(KEY_CHAT, 10000);
611 Exit;
612 end;
614 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
615 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
616 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
617 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
618 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
619 if LongBool(kByte and NET_KEY_FIRE) then PressKey(KEY_FIRE, 10000);
620 if LongBool(kByte and NET_KEY_OPEN) then PressKey(KEY_OPEN, 10000);
622 for i := 0 to 7 do
623 begin
624 if (WeaponAct and Byte(1 shl i)) <> 0 then
625 begin
626 //e_WriteLog(Format(' R:wn=%d', [i]), MSG_WARNING);
627 ProcessWeaponAction(i);
628 end;
629 end;
631 for i := 0 to 15 do
632 begin
633 if (WeaponSelect and Word(1 shl i)) <> 0 then
634 begin
635 //e_WriteLog(Format(' R:wn=%d', [i]), MSG_WARNING);
636 QueueWeaponSwitch(i);
637 end;
638 end;
639 end;
641 // MH_SEND_PlayerPos(False, PID, C^.ID);
642 end;
644 procedure MH_RECV_CheatRequest(C: pTNetClient; var M: TMsg);
645 var
646 CheatKind: Byte;
647 Pl: TPlayer;
648 Err: Boolean;
649 begin
650 Err := False;
651 Pl := g_Player_Get(C^.Player);
652 if Pl = nil then Exit;
654 try
655 CheatKind := M.ReadByte();
656 except
657 Err := True;
658 end;
660 if Err then begin MH_MalformedPacket(C); Exit; end;
662 case CheatKind of
663 NET_CHEAT_SUICIDE:
664 Pl.Damage(SUICIDE_DAMAGE, Pl.UID, 0, 0, HIT_SELF);
665 NET_CHEAT_SPECTATE:
666 begin
667 if Pl.FSpectator then
668 begin
669 if (gGameSettings.MaxLives = 0) or (gLMSRespawn > LMS_RESPAWN_NONE) then
670 Pl.Respawn(False)
671 else
672 MH_SEND_GameEvent(NET_EV_LMS_NOSPAWN, Pl.UID);
673 end
674 else
675 Pl.Spectate;
676 end;
677 NET_CHEAT_READY:
678 begin
679 if gState <> STATE_INTERCUSTOM then Exit;
680 Pl.FReady := not Pl.FReady;
681 if Pl.FReady then
682 begin
683 MH_SEND_GameEvent(NET_EV_INTER_READY, Pl.UID, 'Y');
684 Inc(gInterReadyCount);
685 end
686 else
687 begin
688 MH_SEND_GameEvent(NET_EV_INTER_READY, Pl.UID, 'N');
689 Dec(gInterReadyCount);
690 end;
691 end;
692 NET_CHEAT_DROPFLAG:
693 Pl.TryDropFlag();
694 end;
695 end;
697 procedure MH_RECV_PlayerSettings(C: pTNetClient; var M: TMsg);
698 var
699 TmpName: string;
700 TmpModel: string;
701 TmpColor: TRGB;
702 TmpTeam: Byte;
703 Pl: TPlayer;
704 Err: Boolean;
705 begin
706 Err := False;
707 try
708 TmpName := M.ReadString();
709 TmpModel := M.ReadString();
710 TmpColor.R := M.ReadByte();
711 TmpColor.G := M.ReadByte();
712 TmpColor.B := M.ReadByte();
713 TmpTeam := M.ReadByte();
714 except
715 Err := True;
716 end;
718 if Err then begin MH_MalformedPacket(C); Exit; end;
720 Pl := g_Player_Get(C^.Player);
721 if Pl = nil then Exit;
723 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
724 Pl.SwitchTeam
725 else
726 Pl.SetColor(TmpColor);
728 if Pl.Name <> TmpName then
729 begin
730 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
731 Pl.Name := TmpName;
732 end;
734 if TmpModel <> Pl.Model.Name then
735 Pl.SetModel(TmpModel);
737 MH_SEND_PlayerSettings(Pl.UID, TmpModel);
738 end;
740 // RCON
742 procedure MH_RECV_RCONPassword(C: pTNetClient; var M: TMsg);
743 var
744 Pwd: string;
745 Err: Boolean;
746 begin
747 Err := False;
748 try
749 Pwd := M.ReadString();
750 except
751 Err := True;
752 end;
753 if Err then begin MH_MalformedPacket(C); Exit; end;
754 if not NetAllowRCON then Exit;
755 if Pwd = NetRCONPassword then
756 begin
757 C^.RCONAuth := True;
758 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWGOOD, 'N', C^.ID);
759 end
760 else
761 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWBAD, 'N', C^.ID);
762 end;
764 procedure MH_RECV_RCONCommand(C: pTNetClient; var M: TMsg);
765 var
766 Cmd: string;
767 Err: Boolean;
768 begin
769 Err := False;
770 try
771 Cmd := M.ReadString();
772 except
773 Err := True;
774 end;
775 if Err then begin MH_MalformedPacket(C); Exit; end;
776 if not NetAllowRCON then Exit;
777 if not C^.RCONAuth then
778 begin
779 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_NOAUTH, 'N', C^.ID);
780 Exit;
781 end;
782 g_Console_Process(Cmd);
783 end;
785 // MISC
787 procedure MH_RECV_Vote(C: pTNetClient; var M: TMsg);
788 var
789 Start: Boolean;
790 Name, Command: string;
791 Need: Integer;
792 Pl: TPlayer;
793 Err: Boolean;
794 begin
795 Err := False;
796 try
797 Start := M.ReadByte() <> 0;
798 Command := M.ReadString();
799 except
800 Err := True;
801 end;
803 if Err then begin MH_MalformedPacket(C); Exit; end;
805 Pl := g_Player_Get(C^.Player);
806 if Pl = nil then Exit;
807 Name := Pl.Name;
809 if Start then
810 begin
811 if not g_Console_CommandBlacklisted(Command) then
812 g_Game_StartVote(Command, Name);
813 end
814 else if gVoteInProgress then
815 begin
816 if (gPlayer1 <> nil) or (gPlayer2 <> nil) then
817 Need := Floor((NetClientCount+1)/2.0) + 1
818 else
819 Need := Floor(NetClientCount/2.0) + 1;
820 if C^.Voted then
821 begin
822 Dec(gVoteCount);
823 C^.Voted := False;
824 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_REVOKED], [Name, gVoteCount, Need]), True);
825 MH_SEND_VoteEvent(NET_VE_REVOKE, Name, 'a', gVoteCount, Need);
826 end
827 else
828 begin
829 Inc(gVoteCount);
830 C^.Voted := True;
831 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Name, gVoteCount, Need]), True);
832 MH_SEND_VoteEvent(NET_VE_VOTE, Name, 'a', gVoteCount, Need);
833 g_Game_CheckVote;
834 end;
835 end;
836 end;
838 // GAME (SEND)
840 procedure MH_SEND_Everything(CreatePlayers: Boolean {= False}; ID: Integer {= NET_EVERYONE});
842 function sendItemRespawn (it: PItem): Boolean;
843 begin
844 result := false; // don't stop
845 MH_SEND_ItemSpawn(True, it.myid, ID);
846 end;
848 function sendMonSpawn (mon: TMonster): Boolean;
849 begin
850 result := false; // don't stop
851 MH_SEND_MonsterSpawn(mon.UID, ID);
852 end;
854 function sendPanelState (pan: TPanel): Boolean;
855 begin
856 result := false; // don't stop
857 MH_SEND_PanelState(pan.guid, ID); // anyway, to sync mplats
858 if (pan.CanChangeTexture) then MH_SEND_PanelTexture(pan.guid, pan.LastAnimLoop, ID);
859 end;
861 var
862 I: Integer;
863 begin
864 if (ID >= 0) and (ID < length(NetClients)) then
865 begin
866 e_LogWritefln('*** client #%u (cid #%u) will get everything', [ID, NetClients[ID].Player]);
867 MH_ProcessFirstSpawn(@NetClients[ID]);
868 end;
870 if gPlayers <> nil then
871 begin
872 for I := Low(gPlayers) to High(gPlayers) do
873 begin
874 if gPlayers[I] <> nil then
875 begin
876 if CreatePlayers then MH_SEND_PlayerCreate(gPlayers[I].UID, ID);
877 MH_SEND_PlayerPos(True, gPlayers[I].UID, ID);
878 MH_SEND_PlayerStats(gPlayers[I].UID, ID);
880 if (gPlayers[I].Flag <> FLAG_NONE) and (gGameSettings.GameMode = GM_CTF) then
881 begin
882 MH_SEND_FlagEvent(FLAG_STATE_CAPTURED, gPlayers[I].Flag, gPlayers[I].UID, True, ID);
883 end;
884 end;
885 end;
886 end;
888 g_Items_ForEachAlive(sendItemRespawn, true); // backwards
889 g_Mons_ForEach(sendMonSpawn);
890 g_Map_ForEachPanel(sendPanelState);
892 if gTriggers <> nil then
893 begin
894 for I := Low(gTriggers) to High(gTriggers) do
895 begin
896 if gTriggers[I].TriggerType = TRIGGER_SOUND then
897 begin
898 MH_SEND_TriggerSound(gTriggers[I], ID);
899 end;
900 end;
901 end;
903 if Shots <> nil then
904 begin
905 for I := Low(Shots) to High(Shots) do
906 begin
907 if Shots[i].ShotType in [6, 7, 8] then
908 begin
909 MH_SEND_CreateShot(i, ID);
910 end;
911 end;
912 end;
914 MH_SEND_TriggerMusic(ID);
916 MH_SEND_GameStats(ID);
917 MH_SEND_CoopStats(ID);
919 if gGameSettings.GameMode = GM_CTF then
920 begin
921 if gFlags[FLAG_RED].State <> FLAG_STATE_CAPTURED then MH_SEND_FlagEvent(gFlags[FLAG_RED].State, FLAG_RED, 0, True, ID);
922 if gFlags[FLAG_BLUE].State <> FLAG_STATE_CAPTURED then MH_SEND_FlagEvent(gFlags[FLAG_BLUE].State, FLAG_BLUE, 0, True, ID);
923 end;
925 if CreatePlayers and (ID >= 0) then NetClients[ID].State := NET_STATE_GAME;
927 g_Net_Flush();
928 end;
930 procedure MH_SEND_Info(ID: Byte);
931 begin
932 NetOut.Clear();
934 NetOut.Write(Byte(NET_MSG_INFO));
935 NetOut.Write(ID);
936 NetOut.Write(NetClients[ID].Player);
937 NetOut.Write(ExtractFileName(gGameSettings.WAD));
938 NetOut.Write(g_ExtractFileName(gMapInfo.Map));
939 NetOut.Write(gWADHash);
940 NetOut.Write(gGameSettings.GameMode);
941 NetOut.Write(gGameSettings.GoalLimit);
942 NetOut.Write(gGameSettings.TimeLimit);
943 NetOut.Write(gGameSettings.MaxLives);
944 NetOut.Write(gGameSettings.Options);
945 NetOut.Write(gTime);
947 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
948 end;
950 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
951 var
952 Name: string;
953 i: Integer;
954 Team: Byte;
955 begin
956 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
957 Mode := NET_CHAT_PLAYER;
959 Team := 0;
960 if (Mode = NET_CHAT_TEAM) then
961 begin
962 for i := Low(gPlayers) to High(gPlayers) do
963 if (gPlayers[i] <> nil) and (gPlayers[i].FClientID >= 0) and
964 (gPlayers[i].Team = ID) then
965 begin
966 NetOut.Write(Byte(NET_MSG_CHAT));
967 NetOut.Write(Txt);
968 NetOut.Write(Mode);
969 g_Net_Host_Send(gPlayers[i].FClientID, True, NET_CHAN_CHAT);
970 end;
971 Team := ID;
972 ID := NET_EVERYONE;
973 end
974 else
975 begin
976 NetOut.Write(Byte(NET_MSG_CHAT));
977 NetOut.Write(Txt);
978 NetOut.Write(Mode);
979 g_Net_Host_Send(ID, True, NET_CHAN_CHAT);
980 end;
982 if Mode = NET_CHAT_SYSTEM then
983 Exit;
985 if ID = NET_EVERYONE then
986 begin
987 if Mode = NET_CHAT_PLAYER then
988 begin
989 g_Console_Add(Txt, True);
990 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
991 g_Game_ChatSound(b_Text_Unformat(Txt));
992 end
993 else
994 if Mode = NET_CHAT_TEAM then
995 if gPlayer1 <> nil then
996 begin
997 if (gPlayer1.Team = TEAM_RED) and (Team = TEAM_RED) then
998 begin
999 g_Console_Add(#18'[Team] '#2 + Txt, True);
1000 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1001 g_Game_ChatSound(b_Text_Unformat(Txt));
1002 end
1003 else if (gPlayer1.Team = TEAM_BLUE) and (Team = TEAM_BLUE) then
1004 begin
1005 g_Console_Add(#20'[Team] '#2 + Txt, True);
1006 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1007 g_Game_ChatSound(b_Text_Unformat(Txt));
1008 end;
1009 end;
1010 end
1011 else
1012 begin
1013 Name := g_Net_ClientName_ByID(ID);
1014 g_Console_Add('-> ' + Name + ': ' + Txt, True);
1015 e_WriteLog('[Tell ' + Name + '] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1016 g_Game_ChatSound(b_Text_Unformat(Txt), False);
1017 end;
1018 end;
1020 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
1021 begin
1022 NetOut.Write(Byte(NET_MSG_GFX));
1023 NetOut.Write(Kind);
1024 NetOut.Write(X);
1025 NetOut.Write(Y);
1026 NetOut.Write(Ang);
1028 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
1029 end;
1031 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
1032 begin
1033 NetOut.Write(Byte(NET_MSG_SND));
1034 NetOut.Write(Name);
1035 if Pos then
1036 begin
1037 NetOut.Write(Byte(1));
1038 NetOut.Write(X);
1039 NetOut.Write(Y);
1040 end
1041 else
1042 NetOut.Write(Byte(0));
1044 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
1045 end;
1047 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
1048 begin
1049 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
1051 NetOut.Write(Byte(NET_MSG_SHADD));
1052 NetOut.Write(Proj);
1053 NetOut.Write(Shots[Proj].ShotType);
1054 NetOut.Write(Shots[Proj].Target);
1055 NetOut.Write(Shots[Proj].SpawnerUID);
1056 NetOut.Write(Shots[Proj].Timeout);
1057 NetOut.Write(Shots[Proj].Obj.X);
1058 NetOut.Write(Shots[Proj].Obj.Y);
1059 NetOut.Write(Shots[Proj].Obj.Vel.X);
1060 NetOut.Write(Shots[Proj].Obj.Vel.Y);
1062 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
1063 end;
1065 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
1066 begin
1067 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
1069 NetOut.Write(Byte(NET_MSG_SHPOS));
1070 NetOut.Write(Proj);
1071 NetOut.Write(Shots[Proj].Obj.X);
1072 NetOut.Write(Shots[Proj].Obj.Y);
1073 NetOut.Write(Shots[Proj].Obj.Vel.X);
1074 NetOut.Write(Shots[Proj].Obj.Vel.Y);
1076 g_Net_Host_Send(ID, False, NET_CHAN_SHOTS);
1077 end;
1079 procedure MH_Send_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
1080 begin
1081 NetOut.Write(Byte(NET_MSG_SHDEL));
1082 NetOut.Write(Proj);
1083 NetOut.Write(Byte(Loud));
1084 NetOut.Write(X);
1085 NetOut.Write(Y);
1087 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
1088 end;
1090 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
1091 begin
1092 NetOut.Write(Byte(NET_MSG_SCORE));
1093 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
1094 begin
1095 NetOut.Write(gTeamStat[TEAM_RED].Goals);
1096 NetOut.Write(gTeamStat[TEAM_BLUE].Goals);
1097 end
1098 else
1099 if gGameSettings.GameMode = GM_COOP then
1100 begin
1101 NetOut.Write(gCoopMonstersKilled);
1102 NetOut.Write(gCoopSecretsFound);
1103 end;
1105 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1106 end;
1108 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
1109 begin
1110 NetOut.Write(Byte(NET_MSG_COOP));
1111 NetOut.Write(gTotalMonsters);
1112 NetOut.Write(gSecretsCount);
1113 NetOut.Write(gCoopTotalMonstersKilled);
1114 NetOut.Write(gCoopTotalSecretsFound);
1115 NetOut.Write(gCoopTotalMonsters);
1116 NetOut.Write(gCoopTotalSecrets);
1117 end;
1119 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
1120 begin
1121 NetOut.Write(Byte(NET_MSG_GEVENT));
1122 NetOut.Write(EvType);
1123 NetOut.Write(EvNum);
1124 NetOut.Write(EvStr);
1125 NetOut.Write(Byte(gLastMap));
1126 NetOut.Write(gTime);
1127 if (EvType = NET_EV_MAPSTART) and isWadPath(EvStr) then
1128 begin
1129 NetOut.Write(Byte(1));
1130 NetOut.Write(gWADHash);
1131 end else
1132 NetOut.Write(Byte(0));
1134 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
1135 end;
1137 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
1138 begin
1139 NetOut.Write(Byte(NET_MSG_FLAG));
1140 NetOut.Write(EvType);
1141 NetOut.Write(Flag);
1142 NetOut.Write(Byte(Quiet));
1143 NetOut.Write(PID);
1144 NetOut.Write(gFlags[Flag].State);
1145 NetOut.Write(gFlags[Flag].CaptureTime);
1146 NetOut.Write(gFlags[Flag].Obj.X);
1147 NetOut.Write(gFlags[Flag].Obj.Y);
1148 NetOut.Write(gFlags[Flag].Obj.Vel.X);
1149 NetOut.Write(gFlags[Flag].Obj.Vel.Y);
1150 NetOut.Write(Byte(gFlags[Flag].Direction));
1152 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1153 end;
1155 procedure MH_SEND_FlagPos(Flag: Byte; ID: Integer = NET_EVERYONE);
1156 begin
1157 NetOut.Write(Byte(NET_MSG_FLAGPOS));
1158 NetOut.Write(Flag);
1159 NetOut.Write(gFlags[Flag].Obj.X);
1160 NetOut.Write(gFlags[Flag].Obj.Y);
1161 NetOut.Write(gFlags[Flag].Obj.Vel.X);
1162 NetOut.Write(gFlags[Flag].Obj.Vel.Y);
1164 g_Net_Host_Send(ID, False, NET_CHAN_IMPORTANT);
1165 end;
1167 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
1168 begin
1169 NetOut.Write(Byte(NET_MSG_GSET));
1170 NetOut.Write(gGameSettings.GameMode);
1171 NetOut.Write(gGameSettings.GoalLimit);
1172 NetOut.Write(gGameSettings.TimeLimit);
1173 NetOut.Write(gGameSettings.MaxLives);
1174 NetOut.Write(gGameSettings.Options);
1176 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1177 end;
1179 // PLAYER (SEND)
1181 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
1182 var
1183 P: TPlayer;
1184 begin
1185 P := g_Player_Get(PID);
1186 if P = nil then Exit;
1188 NetOut.Write(Byte(NET_MSG_PLR));
1189 NetOut.Write(PID);
1190 NetOut.Write(P.Name);
1192 NetOut.Write(P.FActualModelName);
1193 NetOut.Write(P.FColor.R);
1194 NetOut.Write(P.FColor.G);
1195 NetOut.Write(P.FColor.B);
1196 NetOut.Write(P.Team);
1198 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT)
1199 end;
1201 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
1202 var
1203 kByte: Word;
1204 Pl: TPlayer;
1205 begin
1206 Pl := g_Player_Get(PID);
1207 if Pl = nil then Exit;
1208 if Pl.FDummy then Exit;
1210 NetOut.Write(Byte(NET_MSG_PLRPOS));
1211 NetOut.Write(gTime);
1212 NetOut.Write(PID);
1214 kByte := 0;
1216 with Pl do
1217 begin
1218 NetOut.Write(FPing);
1219 NetOut.Write(FLoss);
1220 if IsKeyPressed(KEY_CHAT) then
1221 kByte := NET_KEY_CHAT
1222 else
1223 begin
1224 if IsKeyPressed(KEY_LEFT) then kByte := kByte or NET_KEY_LEFT;
1225 if IsKeyPressed(KEY_RIGHT) then kByte := kByte or NET_KEY_RIGHT;
1226 if IsKeyPressed(KEY_UP) then kByte := kByte or NET_KEY_UP;
1227 if IsKeyPressed(KEY_DOWN) then kByte := kByte or NET_KEY_DOWN;
1228 if IsKeyPressed(KEY_JUMP) then kByte := kByte or NET_KEY_JUMP;
1229 end;
1231 if JustTeleported then kByte := kByte or NET_KEY_FORCEDIR;
1233 NetOut.Write(kByte);
1234 if Direction = TDirection.D_LEFT then NetOut.Write(Byte(0)) else NetOut.Write(Byte(1));
1235 NetOut.Write(GameX);
1236 NetOut.Write(GameY);
1237 NetOut.Write(GameVelX);
1238 NetOut.Write(GameVelY);
1239 NetOut.Write(GameAccelX);
1240 NetOut.Write(GameAccelY);
1241 end;
1243 g_Net_Host_Send(ID, Reliable, NET_CHAN_PLAYERPOS);
1244 end;
1246 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
1247 var
1248 P: TPlayer;
1249 I: Integer;
1250 begin
1251 P := g_Player_Get(PID);
1252 if P = nil then Exit;
1254 NetOut.Write(Byte(NET_MSG_PLRSTA));
1255 NetOut.Write(PID);
1257 with P do
1258 begin
1259 NetOut.Write(Byte(alive));
1260 NetOut.Write(Byte(GodMode));
1261 NetOut.Write(Health);
1262 NetOut.Write(Armor);
1263 NetOut.Write(Air);
1264 NetOut.Write(JetFuel);
1265 NetOut.Write(Lives);
1266 NetOut.Write(Team);
1268 for I := WP_FIRST to WP_LAST do
1269 NetOut.Write(Byte(FWeapon[I]));
1271 for I := A_BULLETS to A_HIGH do
1272 NetOut.Write(FAmmo[I]);
1274 for I := A_BULLETS to A_HIGH do
1275 NetOut.Write(FMaxAmmo[I]);
1277 for I := MR_SUIT to MR_MAX do
1278 NetOut.Write(LongWord(FMegaRulez[I]));
1280 NetOut.Write(Byte(R_ITEM_BACKPACK in FRulez));
1281 NetOut.Write(Byte(R_KEY_RED in FRulez));
1282 NetOut.Write(Byte(R_KEY_GREEN in FRulez));
1283 NetOut.Write(Byte(R_KEY_BLUE in FRulez));
1284 NetOut.Write(Byte(R_BERSERK in FRulez));
1286 NetOut.Write(Frags);
1287 NetOut.Write(Death);
1289 NetOut.Write(CurrWeap);
1291 NetOut.Write(Byte(FSpectator));
1292 NetOut.Write(Byte(FGhost));
1293 NetOut.Write(Byte(FPhysics));
1294 NetOut.Write(Byte(FNoRespawn));
1295 NetOut.Write(Byte(FJetpack));
1296 NetOut.Write(FFireTime);
1297 NetOut.Write(Byte(FFlaming));
1298 NetOut.Write(FSpawnInvul);
1299 end;
1301 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1302 end;
1304 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
1305 begin
1306 NetOut.Write(Byte(NET_MSG_PLRDMG));
1307 NetOut.Write(PID);
1308 NetOut.Write(Kind);
1309 NetOut.Write(Attacker);
1310 NetOut.Write(Value);
1311 NetOut.Write(VX);
1312 NetOut.Write(VY);
1314 g_Net_Host_Send(ID, False, NET_CHAN_PLAYER);
1315 end;
1317 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
1318 begin
1319 NetOut.Write(Byte(NET_MSG_PLRDIE));
1320 NetOut.Write(PID);
1321 NetOut.Write(KillType);
1322 NetOut.Write(DeathType);
1323 NetOut.Write(Attacker);
1325 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1326 end;
1328 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
1329 begin
1330 NetOut.Write(Byte(NET_MSG_PLRFIRE));
1331 NetOut.Write(PID);
1332 NetOut.Write(Weapon);
1333 NetOut.Write(X);
1334 NetOut.Write(Y);
1335 NetOut.Write(AX);
1336 NetOut.Write(AY);
1337 NetOut.Write(ShotID);
1339 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
1340 end;
1342 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
1343 begin
1344 NetOut.Write(Byte(NET_MSG_PLRDEL));
1345 NetOut.Write(PID);
1347 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1348 end;
1350 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
1351 var
1352 Pl: TPlayer;
1353 begin
1354 Pl := g_Player_Get(PID);
1355 if Pl = nil then Exit;
1357 NetOut.Write(Byte(NET_MSG_PLRSET));
1358 NetOut.Write(PID);
1359 NetOut.Write(Pl.Name);
1360 if Mdl = '' then
1361 NetOut.Write(Pl.Model.Name)
1362 else
1363 NetOut.Write(Mdl);
1364 NetOut.Write(Pl.FColor.R);
1365 NetOut.Write(Pl.FColor.G);
1366 NetOut.Write(Pl.FColor.B);
1367 NetOut.Write(Pl.Team);
1369 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1370 end;
1372 // ITEM (SEND)
1374 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1375 var
1376 it: PItem;
1377 tt: Byte;
1378 begin
1379 it := g_Items_ByIdx(IID);
1381 NetOut.Write(Byte(NET_MSG_ISPAWN));
1382 NetOut.Write(IID);
1383 NetOut.Write(Byte(Quiet));
1384 tt := it.ItemType;
1385 if it.dropped then tt := tt or $80;
1386 NetOut.Write(tt);
1387 NetOut.Write(Byte(it.Fall));
1388 NetOut.Write(Byte(it.Respawnable));
1389 NetOut.Write(it.Obj.X);
1390 NetOut.Write(it.Obj.Y);
1391 NetOut.Write(it.Obj.Vel.X);
1392 NetOut.Write(it.Obj.Vel.Y);
1394 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1395 end;
1397 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1398 begin
1399 NetOut.Write(Byte(NET_MSG_IDEL));
1400 NetOut.Write(IID);
1401 NetOut.Write(Byte(Quiet));
1403 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1404 end;
1406 procedure MH_SEND_ItemPos(IID: Word; ID: Integer = NET_EVERYONE);
1407 var
1408 it: PItem;
1409 begin
1410 it := g_Items_ByIdx(IID);
1412 NetOut.Write(Byte(NET_MSG_IPOS));
1413 NetOut.Write(IID);
1414 NetOut.Write(it.Obj.X);
1415 NetOut.Write(it.Obj.Y);
1416 NetOut.Write(it.Obj.Vel.X);
1417 NetOut.Write(it.Obj.Vel.Y);
1419 g_Net_Host_Send(ID, False, NET_CHAN_LARGEDATA);
1420 end;
1422 // PANEL
1424 procedure MH_SEND_PanelTexture(PGUID: Integer; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
1425 var
1426 TP: TPanel;
1427 begin
1428 TP := g_Map_PanelByGUID(PGUID);
1429 if (TP = nil) then exit;
1431 with TP do
1432 begin
1433 NetOut.Write(Byte(NET_MSG_PTEX));
1434 NetOut.Write(LongWord(PGUID));
1435 NetOut.Write(FCurTexture);
1436 NetOut.Write(FCurFrame);
1437 NetOut.Write(FCurFrameCount);
1438 NetOut.Write(AnimLoop);
1439 end;
1441 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1442 end;
1444 procedure MH_SEND_PanelState(PGUID: Integer; ID: Integer = NET_EVERYONE);
1445 var
1446 TP: TPanel;
1447 mpflags: Byte = 0;
1448 begin
1449 TP := g_Map_PanelByGUID(PGUID);
1450 if (TP = nil) then exit;
1452 NetOut.Write(Byte(NET_MSG_PSTATE));
1453 NetOut.Write(LongWord(PGUID));
1454 NetOut.Write(Byte(TP.Enabled));
1455 NetOut.Write(TP.LiftType);
1456 NetOut.Write(TP.X);
1457 NetOut.Write(TP.Y);
1458 NetOut.Write(Word(TP.Width));
1459 NetOut.Write(Word(TP.Height));
1460 // mplats
1461 NetOut.Write(LongInt(TP.movingSpeedX));
1462 NetOut.Write(LongInt(TP.movingSpeedY));
1463 NetOut.Write(LongInt(TP.movingStartX));
1464 NetOut.Write(LongInt(TP.movingStartY));
1465 NetOut.Write(LongInt(TP.movingEndX));
1466 NetOut.Write(LongInt(TP.movingEndY));
1467 NetOut.Write(LongInt(TP.sizeSpeedX));
1468 NetOut.Write(LongInt(TP.sizeSpeedY));
1469 NetOut.Write(LongInt(TP.sizeEndX));
1470 NetOut.Write(LongInt(TP.sizeEndY));
1471 if TP.movingActive then mpflags := mpflags or 1;
1472 if TP.moveOnce then mpflags := mpflags or 2;
1473 NetOut.Write(Byte(mpflags));
1475 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1476 end;
1478 // TRIGGER
1480 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
1481 begin
1482 if gTriggers = nil then Exit;
1483 if T.Sound = nil then Exit;
1485 NetOut.Write(Byte(NET_MSG_TSOUND));
1486 NetOut.Write(T.ClientID);
1487 NetOut.Write(Byte(T.Sound.IsPlaying));
1488 NetOut.Write(LongWord(T.Sound.GetPosition));
1489 NetOut.Write(T.SoundPlayCount);
1491 g_Net_Host_Send(ID, True);
1492 end;
1494 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
1495 begin
1496 NetOut.Write(Byte(NET_MSG_TMUSIC));
1497 NetOut.Write(gMusic.Name);
1498 NetOut.Write(Byte(gMusic.IsPlaying));
1499 NetOut.Write(LongWord(gMusic.GetPosition));
1500 NetOut.Write(Byte(gMusic.SpecPause or gMusic.IsPaused));
1502 g_Net_Host_Send(ID, True);
1503 end;
1505 // MONSTER
1507 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
1508 var
1509 M: TMonster;
1510 begin
1511 M := g_Monsters_ByUID(UID);
1512 if M = nil then
1513 Exit;
1515 with M do
1516 begin
1517 NetOut.Write(Byte(NET_MSG_MSPAWN));
1518 NetOut.Write(UID);
1519 NetOut.Write(MonsterType);
1520 NetOut.Write(MonsterState);
1521 NetOut.Write(MonsterAnim);
1522 NetOut.Write(MonsterTargetUID);
1523 NetOut.Write(MonsterTargetTime);
1524 NetOut.Write(MonsterBehaviour);
1525 NetOut.Write(MonsterSleep);
1526 NetOut.Write(MonsterHealth);
1527 NetOut.Write(MonsterAmmo);
1528 NetOut.Write(GameX);
1529 NetOut.Write(GameY);
1530 NetOut.Write(GameVelX);
1531 NetOut.Write(GameVelY);
1532 NetOut.Write(Byte(GameDirection));
1533 end;
1535 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1536 end;
1538 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
1539 var
1540 M: TMonster;
1541 begin
1542 M := g_Monsters_ByUID(UID);
1543 if M = nil then Exit;
1545 NetOut.Write(Byte(NET_MSG_MPOS));
1546 NetOut.Write(UID);
1548 with M do
1549 begin
1550 NetOut.Write(GameX);
1551 NetOut.Write(GameY);
1552 NetOut.Write(GameVelX);
1553 NetOut.Write(GameVelY);
1554 NetOut.Write(Byte(GameDirection));
1555 end;
1557 g_Net_Host_Send(ID, False, NET_CHAN_MONSTERPOS);
1558 end;
1560 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
1561 var
1562 M: TMonster;
1563 begin
1564 M := g_Monsters_ByUID(UID);
1565 if M = nil then Exit;
1567 NetOut.Write(Byte(NET_MSG_MSTATE));
1568 NetOut.Write(UID);
1570 with M do
1571 begin
1572 NetOut.Write(MonsterState);
1573 NetOut.Write(ForcedAnim);
1574 NetOut.Write(MonsterTargetUID);
1575 NetOut.Write(MonsterTargetTime);
1576 NetOut.Write(MonsterSleep);
1577 NetOut.Write(MonsterHealth);
1578 NetOut.Write(MonsterAmmo);
1579 NetOut.Write(MonsterPain);
1580 NetOut.Write(Byte(AnimIsReverse));
1581 NetOut.Write(FFireTime);
1582 end;
1584 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1585 end;
1587 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
1588 begin
1589 NetOut.Write(Byte(NET_MSG_MSHOT));
1590 NetOut.Write(UID);
1591 NetOut.Write(X);
1592 NetOut.Write(Y);
1593 NetOut.Write(VX);
1594 NetOut.Write(VY);
1596 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1597 end;
1599 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
1600 var
1601 M: TMonster;
1602 begin
1603 M := g_Monsters_ByUID(UID);
1604 if M = nil then Exit;
1606 NetOut.Write(Byte(NET_MSG_MDEL));
1607 NetOut.Write(UID);
1609 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1610 end;
1612 // MISC
1614 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
1615 begin
1616 NetOut.Write(Byte(NET_MSG_TIME_SYNC));
1617 NetOut.Write(Time);
1619 g_Net_Host_Send(ID, False, NET_CHAN_SERVICE);
1620 end;
1622 procedure MH_SEND_VoteEvent(EvType: Byte;
1623 StrArg1: string = 'a'; StrArg2: string = 'b';
1624 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
1625 ID: Integer = NET_EVERYONE);
1626 begin
1627 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
1628 NetOut.Write(EvType);
1629 NetOut.Write(IntArg1);
1630 NetOut.Write(IntArg2);
1631 NetOut.Write(StrArg1);
1632 NetOut.Write(StrArg2);
1634 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1635 end;
1637 // CLIENT MESSAGES //
1639 // GAME
1641 procedure MC_RECV_Chat(var M: TMsg);
1642 var
1643 Txt: string;
1644 Mode: Byte;
1645 begin
1646 Txt := M.ReadString();
1647 Mode := M.ReadByte();
1649 if Mode <> NET_CHAT_SYSTEM then
1650 begin
1651 if NetDeafLevel = 0 then
1652 begin
1653 if Mode = NET_CHAT_PLAYER then
1654 begin
1655 g_Console_Add(Txt, True);
1656 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1657 g_Game_ChatSound(b_Text_Unformat(Txt));
1658 end else
1659 if (Mode = NET_CHAT_TEAM) and (gPlayer1 <> nil) then
1660 begin
1661 if gPlayer1.Team = TEAM_RED then
1662 g_Console_Add(b_Text_Format('\r[Team] ') + Txt, True);
1663 if gPlayer1.Team = TEAM_BLUE then
1664 g_Console_Add(b_Text_Format('\b[Team] ') + Txt, True);
1665 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1666 g_Game_ChatSound(b_Text_Unformat(Txt));
1667 end;
1668 end;
1669 end else if (NetDeafLevel < 2) then
1670 g_Console_Add(Txt, True);
1671 end;
1673 procedure MC_RECV_Effect(var M: TMsg);
1674 var
1675 Kind: Byte;
1676 X, Y: Integer;
1677 Ang: SmallInt;
1678 Anim: TAnimation;
1679 ID: LongWord;
1680 begin
1681 if not gGameOn then Exit;
1682 Kind := M.ReadByte();
1683 X := M.ReadLongInt();
1684 Y := M.ReadLongInt();
1685 Ang := M.ReadSmallInt();
1687 case Kind of
1688 NET_GFX_SPARK:
1689 g_GFX_Spark(X, Y, 2 + Random(2), Ang, 0, 0);
1691 NET_GFX_TELE:
1692 begin
1693 if g_Frames_Get(ID, 'FRAMES_TELEPORT') then
1694 begin
1695 Anim := TAnimation.Create(ID, False, 3);
1696 g_GFX_OnceAnim(X, Y, Anim);
1697 Anim.Free();
1698 end;
1699 if Ang = 1 then
1700 g_Sound_PlayExAt('SOUND_GAME_TELEPORT', X, Y);
1701 end;
1703 NET_GFX_EXPLODE:
1704 begin
1705 if g_Frames_Get(ID, 'FRAMES_EXPLODE_ROCKET') then
1706 begin
1707 Anim := TAnimation.Create(ID, False, 6);
1708 Anim.Blending := False;
1709 g_GFX_OnceAnim(X-64, Y-64, Anim);
1710 Anim.Free();
1711 end;
1712 if Ang = 1 then
1713 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEROCKET', X, Y);
1714 end;
1716 NET_GFX_BFGEXPL:
1717 begin
1718 if g_Frames_Get(ID, 'FRAMES_EXPLODE_BFG') then
1719 begin
1720 Anim := TAnimation.Create(ID, False, 6);
1721 Anim.Blending := False;
1722 g_GFX_OnceAnim(X-64, Y-64, Anim);
1723 Anim.Free();
1724 end;
1725 if Ang = 1 then
1726 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEBFG', X, Y);
1727 end;
1729 NET_GFX_BFGHIT:
1730 begin
1731 if g_Frames_Get(ID, 'FRAMES_BFGHIT') then
1732 begin
1733 Anim := TAnimation.Create(ID, False, 4);
1734 g_GFX_OnceAnim(X-32, Y-32, Anim);
1735 Anim.Free();
1736 end;
1737 end;
1739 NET_GFX_FIRE:
1740 begin
1741 if g_Frames_Get(ID, 'FRAMES_FIRE') then
1742 begin
1743 Anim := TAnimation.Create(ID, False, 4);
1744 g_GFX_OnceAnim(X, Y, Anim);
1745 Anim.Free();
1746 end;
1747 if Ang = 1 then
1748 g_Sound_PlayExAt('SOUND_FIRE', X, Y);
1749 end;
1751 NET_GFX_RESPAWN:
1752 begin
1753 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
1754 begin
1755 Anim := TAnimation.Create(ID, False, 4);
1756 g_GFX_OnceAnim(X, Y, Anim);
1757 Anim.Free();
1758 end;
1759 if Ang = 1 then
1760 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
1761 end;
1763 NET_GFX_SHELL1:
1764 g_Player_CreateShell(X, Y, 0, -2, SHELL_BULLET);
1766 NET_GFX_SHELL2:
1767 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1769 NET_GFX_SHELL3:
1770 begin
1771 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1772 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1773 end;
1774 end;
1775 end;
1777 procedure MC_RECV_Sound(var M: TMsg);
1778 var
1779 Name: string;
1780 X, Y: Integer;
1781 Pos: Boolean;
1782 begin
1783 Name := M.ReadString();
1784 Pos := M.ReadByte() <> 0;
1785 if Pos then
1786 begin
1787 X := M.ReadLongInt();
1788 Y := M.ReadLongInt();
1789 g_Sound_PlayExAt(Name, X, Y);
1790 end
1791 else
1792 g_Sound_PlayEx(Name);
1793 end;
1795 procedure MC_RECV_CreateShot(var M: TMsg);
1796 var
1797 I, X, Y, XV, YV: Integer;
1798 Timeout: LongWord;
1799 Target, Spawner: Word;
1800 ShType: Byte;
1801 begin
1802 I := M.ReadLongInt();
1803 ShType := M.ReadByte();
1804 Target := M.ReadWord();
1805 Spawner := M.ReadWord();
1806 Timeout := M.ReadLongWord();
1807 X := M.ReadLongInt();
1808 Y := M.ReadLongInt();
1809 XV := M.ReadLongInt();
1810 YV := M.ReadLongInt();
1812 I := g_Weapon_CreateShot(I, ShType, Spawner, Target, X, Y, XV, YV);
1813 if (Shots <> nil) and (I <= High(Shots)) then
1814 begin
1815 Shots[I].Timeout := Timeout;
1816 //Shots[I].Target := Target; // TODO: find a use for Target later
1817 end;
1818 end;
1820 procedure MC_RECV_UpdateShot(var M: TMsg);
1821 var
1822 I, TX, TY, TXV, TYV: Integer;
1823 begin
1824 I := M.ReadLongInt();
1825 TX := M.ReadLongInt();
1826 TY := M.ReadLongInt();
1827 TXV := M.ReadLongInt();
1828 TYV := M.ReadLongInt();
1830 if (Shots <> nil) and (I <= High(Shots)) then
1831 with (Shots[i]) do
1832 begin
1833 Obj.X := TX;
1834 Obj.Y := TY;
1835 Obj.Vel.X := TXV;
1836 Obj.Vel.Y := TYV;
1837 end;
1838 end;
1840 procedure MC_RECV_DeleteShot(var M: TMsg);
1841 var
1842 I, X, Y: Integer;
1843 L: Boolean;
1844 begin
1845 if not gGameOn then Exit;
1846 I := M.ReadLongInt();
1847 L := (M.ReadByte() <> 0);
1848 X := M.ReadLongInt();
1849 Y := M.ReadLongInt();
1851 g_Weapon_DestroyShot(I, X, Y, L);
1852 end;
1854 procedure MC_RECV_GameStats(var M: TMsg);
1855 begin
1856 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
1857 begin
1858 gTeamStat[TEAM_RED].Goals := M.ReadSmallInt();
1859 gTeamStat[TEAM_BLUE].Goals := M.ReadSmallInt();
1860 end
1861 else
1862 if gGameSettings.GameMode = GM_COOP then
1863 begin
1864 gCoopMonstersKilled := M.ReadWord();
1865 gCoopSecretsFound := M.ReadWord();
1866 end;
1867 end;
1869 procedure MC_RECV_CoopStats(var M: TMsg);
1870 begin
1871 gTotalMonsters := M.ReadLongInt();
1872 gSecretsCount := M.ReadLongInt();
1873 gCoopTotalMonstersKilled := M.ReadWord();
1874 gCoopTotalSecretsFound := M.ReadWord();
1875 gCoopTotalMonsters := M.ReadWord();
1876 gCoopTotalSecrets := M.ReadWord();
1877 end;
1879 procedure MC_RECV_GameEvent(var M: TMsg);
1880 var
1881 EvType: Byte;
1882 EvNum: Integer;
1883 EvStr: string;
1884 EvTime: LongWord;
1885 BHash: Boolean;
1886 EvHash: TMD5Digest;
1887 pl: TPlayer;
1888 i1, i2: TStrings_Locale;
1889 pln: String;
1890 cnt: Byte;
1891 goodCmd: Boolean = true;
1892 begin
1893 FillChar(EvHash, Sizeof(EvHash), 0);
1894 EvType := M.ReadByte();
1895 EvNum := M.ReadLongInt();
1896 EvStr := M.ReadString();
1897 gLastMap := M.ReadByte() <> 0;
1898 if gLastMap and (gGameSettings.GameMode = GM_COOP) then gStatsOff := True;
1899 gStatsPressed := True;
1900 EvTime := M.ReadLongWord();
1901 BHash := M.ReadByte() <> 0;
1902 if BHash then
1903 EvHash := M.ReadMD5();
1905 gTime := EvTime;
1907 if (g_Res_received_map_start <> 0) then
1908 begin
1909 if (g_Res_received_map_start < 0) then exit;
1910 goodCmd := false;
1911 case EvType of
1912 NET_EV_MAPSTART: goodCmd := true;
1913 NET_EV_MAPEND: goodCmd := true;
1914 NET_EV_PLAYER_KICK: goodCmd := true;
1915 NET_EV_PLAYER_BAN: goodCmd := true;
1916 NET_EV_LMS_WARMUP: goodCmd := true;
1917 end;
1918 if not goodCmd then exit;
1919 end;
1921 case EvType of
1922 NET_EV_MAPSTART:
1923 begin
1924 if (g_Res_received_map_start <> 0) then
1925 begin
1926 g_Res_received_map_start := -1;
1927 end
1928 else
1929 begin
1930 gGameOn := False;
1931 g_Game_ClearLoading();
1932 g_Game_StopAllSounds(True);
1934 gSwitchGameMode := Byte(EvNum);
1935 gGameSettings.GameMode := gSwitchGameMode;
1937 gWADHash := EvHash;
1938 if not g_Game_StartMap(false{asMegawad}, EvStr, True) then
1939 begin
1940 if not isWadPath(EvStr) then
1941 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [gGameSettings.WAD + ':\' + EvStr]))
1942 else
1943 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [EvStr]));
1944 Exit;
1945 end;
1947 MC_SEND_FullStateRequest;
1948 end;
1949 end;
1951 NET_EV_MAPEND:
1952 begin
1953 gLMSRespawn := LMS_RESPAWN_NONE;
1954 gLMSRespawnTime := 0;
1955 if (g_Res_received_map_start <> 0) then
1956 begin
1957 g_Res_received_map_start := -1;
1958 end
1959 else
1960 begin
1961 gMissionFailed := EvNum <> 0;
1962 gExit := EXIT_ENDLEVELCUSTOM;
1963 end;
1964 end;
1966 NET_EV_RCON:
1967 begin
1968 case EvNum of
1969 NET_RCON_NOAUTH:
1970 g_Console_Add(_lc[I_NET_RCON_NOAUTH], True);
1971 NET_RCON_PWGOOD:
1972 g_Console_Add(_lc[I_NET_RCON_PWD_VALID], True);
1973 NET_RCON_PWBAD:
1974 g_Console_Add(_lc[I_NET_RCON_PWD_INVALID], True);
1975 end;
1976 end;
1978 NET_EV_CHANGE_TEAM:
1979 begin
1980 if NetDeafLevel < 2 then
1981 begin
1982 if EvNum = TEAM_RED then
1983 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [EvStr]), True);
1984 if EvNum = TEAM_BLUE then
1985 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [EvStr]), True);
1986 end;
1987 end;
1989 NET_EV_PLAYER_KICK:
1990 begin
1991 g_Console_Add(Format(_lc[I_PLAYER_KICK], [EvStr]), True);
1992 if (g_Res_received_map_start <> 0) then g_Res_received_map_start := -1;
1993 end;
1995 NET_EV_PLAYER_BAN:
1996 begin
1997 g_Console_Add(Format(_lc[I_PLAYER_BAN], [EvStr]), True);
1998 if (g_Res_received_map_start <> 0) then g_Res_received_map_start := -1;
1999 end;
2001 NET_EV_LMS_WARMUP:
2002 begin
2003 if EvNum > 0 then
2004 begin
2005 gLMSRespawn := LMS_RESPAWN_WARMUP;
2006 gLMSRespawnTime := gTime + EvNum;
2007 g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [EvNum div 1000]), True);
2008 end
2009 else if gPlayer1 = nil then
2010 begin
2011 g_Console_Add(_lc[I_PLAYER_SPECT4], True);
2012 end;
2013 end;
2015 NET_EV_LMS_SURVIVOR:
2016 g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True);
2018 NET_EV_BIGTEXT:
2019 if NetDeafLevel < 2 then g_Game_Message(AnsiUpperCase(EvStr), Word(EvNum));
2021 NET_EV_SCORE:
2022 begin
2023 pl := g_Player_Get(EvNum and $FFFF);
2024 if pl = nil then
2025 pln := '?'
2026 else
2027 pln := pl.Name;
2028 cnt := (EvNum shr 16) and $FF;
2029 if Pos('w', EvStr) = 0 then
2030 begin
2031 // Default score
2032 if Pos('t', EvStr) = 0 then
2033 begin
2034 // Player +/- score
2035 if Pos('-', EvStr) = 0 then
2036 begin
2037 if Pos('e', EvStr) = 0 then
2038 i1 := I_PLAYER_SCORE_ADD_OWN
2039 else
2040 i1 := I_PLAYER_SCORE_ADD_ENEMY;
2041 end else
2042 begin
2043 if Pos('e', EvStr) = 0 then
2044 i1 := I_PLAYER_SCORE_SUB_OWN
2045 else
2046 i1 := I_PLAYER_SCORE_SUB_ENEMY;
2047 end;
2048 // Which team
2049 if Pos('r', EvStr) > 0 then
2050 i2 := I_PLAYER_SCORE_TO_RED
2051 else
2052 i2 := I_PLAYER_SCORE_TO_BLUE;
2053 g_Console_Add(Format(_lc[i1], [pln, cnt, _lc[i2]]), True);
2054 end else
2055 begin
2056 // Team +/- score
2057 if Pos('-', EvStr) = 0 then
2058 i1 := I_PLAYER_SCORE_ADD_TEAM
2059 else
2060 i1 := I_PLAYER_SCORE_SUB_TEAM;
2061 // Which team
2062 if Pos('r', EvStr) > 0 then
2063 i2 := I_PLAYER_SCORE_RED
2064 else
2065 i2 := I_PLAYER_SCORE_BLUE;
2066 g_Console_Add(Format(_lc[i1], [_lc[i2], cnt]), True);
2067 end;
2068 end else
2069 begin
2070 // Game Win
2071 if Pos('e', EvStr) = 0 then
2072 i1 := I_PLAYER_SCORE_WIN_OWN
2073 else
2074 i1 := I_PLAYER_SCORE_WIN_ENEMY;
2075 // Which team
2076 if Pos('r', EvStr) > 0 then
2077 i2 := I_PLAYER_SCORE_TO_RED
2078 else
2079 i2 := I_PLAYER_SCORE_TO_BLUE;
2080 g_Console_Add(Format(_lc[i1], [pln, _lc[i2]]), True);
2081 end;
2082 end;
2084 NET_EV_SCORE_MSG:
2085 begin
2086 if EvNum = TEAM_RED then
2087 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
2088 if EvNum = TEAM_BLUE then
2089 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
2090 if EvNum = -TEAM_RED then
2091 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
2092 if EvNum = -TEAM_BLUE then
2093 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
2094 end;
2096 NET_EV_LMS_START:
2097 begin
2098 g_Player_RemoveAllCorpses;
2099 gLMSRespawn := LMS_RESPAWN_NONE;
2100 g_Game_Message(_lc[I_MESSAGE_LMS_START], 144);
2101 end;
2103 NET_EV_LMS_WIN:
2104 g_Game_Message(Format(_lc[I_MESSAGE_LMS_WIN], [AnsiUpperCase(EvStr)]), 144);
2106 NET_EV_TLMS_WIN:
2107 begin
2108 if EvNum = TEAM_RED then
2109 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 144);
2110 if EvNum = TEAM_BLUE then
2111 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 144);
2112 end;
2114 NET_EV_LMS_LOSE:
2115 g_Game_Message(_lc[I_MESSAGE_LMS_LOSE], 144);
2117 NET_EV_LMS_DRAW:
2118 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
2120 NET_EV_LMS_NOSPAWN:
2121 g_Console_Add(_lc[I_PLAYER_SPECT4], True);
2123 NET_EV_KILLCOMBO:
2124 g_Game_Announce_KillCombo(EvNum);
2126 NET_EV_PLAYER_TOUCH:
2127 begin
2128 pl := g_Player_Get(EvNum);
2129 if pl <> nil then
2130 pl.Touch();
2131 end;
2133 NET_EV_SECRET:
2134 begin
2135 pl := g_Player_Get(EvNum);
2136 if pl <> nil then
2137 begin
2138 g_Console_Add(Format(_lc[I_PLAYER_SECRET], [pl.Name]), True);
2139 g_Sound_PlayEx('SOUND_GAME_SECRET');
2140 end;
2141 end;
2143 NET_EV_INTER_READY:
2144 begin
2145 pl := g_Player_Get(EvNum);
2146 if pl <> nil then pl.FReady := (EvStr = 'Y');
2147 end;
2148 end;
2149 end;
2151 procedure MC_RECV_FlagPos(var M: TMsg);
2152 var
2153 Fl: Byte;
2154 begin
2155 Fl := M.ReadByte();
2156 if Fl = FLAG_NONE then Exit;
2157 gFlags[Fl].Obj.X := M.ReadLongInt();
2158 gFlags[Fl].Obj.Y := M.ReadLongInt();
2159 gFlags[Fl].Obj.Vel.X := M.ReadLongInt();
2160 gFlags[Fl].Obj.Vel.Y := M.ReadLongInt();
2161 end;
2163 procedure MC_RECV_FlagEvent(var M: TMsg);
2164 var
2165 PID: Word;
2166 Pl: TPlayer;
2167 EvType: Byte;
2168 Fl, a: Byte;
2169 Quiet: Boolean;
2170 s, ts: string;
2171 begin
2172 EvType := M.ReadByte();
2173 Fl := M.ReadByte();
2175 if Fl = FLAG_NONE then Exit;
2177 Quiet := (M.ReadByte() <> 0);
2178 PID := M.ReadWord();
2180 gFlags[Fl].State := M.ReadByte();
2181 gFlags[Fl].CaptureTime := M.ReadLongWord();
2182 gFlags[Fl].Obj.X := M.ReadLongInt();
2183 gFlags[Fl].Obj.Y := M.ReadLongInt();
2184 gFlags[Fl].Obj.Vel.X := M.ReadLongInt();
2185 gFlags[Fl].Obj.Vel.Y := M.ReadLongInt();
2186 gFlags[Fl].Direction := TDirection(M.ReadByte());
2188 Pl := g_Player_Get(PID);
2189 if (Pl = nil) and
2190 (EvType <> FLAG_STATE_NORMAL) and
2191 (EvType <> FLAG_STATE_DROPPED) and
2192 (EvType <> FLAG_STATE_RETURNED) then
2193 Exit;
2195 case EvType of
2196 FLAG_STATE_NORMAL:
2197 begin
2198 if Quiet or (Pl = nil) then Exit;
2200 if Fl = FLAG_RED then
2201 s := _lc[I_PLAYER_FLAG_RED]
2202 else
2203 s := _lc[I_PLAYER_FLAG_BLUE];
2205 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2207 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2208 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2209 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2210 a := 0
2211 else
2212 a := 1;
2214 if not sound_ret_flag[a].IsPlaying() then
2215 sound_ret_flag[a].Play();
2216 end;
2218 FLAG_STATE_CAPTURED:
2219 begin
2220 if (Pl <> nil) then Pl.SetFlag(Fl);
2222 if Quiet then Exit;
2224 if Fl = FLAG_RED then
2225 s := _lc[I_PLAYER_FLAG_RED]
2226 else
2227 s := _lc[I_PLAYER_FLAG_BLUE];
2229 g_Console_Add(Format(_lc[I_PLAYER_FLAG_GET], [Pl.Name, s]), True);
2230 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_GET], [AnsiUpperCase(s)]), 144);
2232 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2233 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2234 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2235 a := 0
2236 else
2237 a := 1;
2239 if not sound_get_flag[a].IsPlaying() then
2240 sound_get_flag[a].Play();
2241 end;
2243 FLAG_STATE_DROPPED:
2244 begin
2245 if (Pl <> nil) then Pl.SetFlag(FLAG_NONE);
2247 if Quiet or (Pl = nil) then Exit;
2249 if Fl = FLAG_RED then
2250 s := _lc[I_PLAYER_FLAG_RED]
2251 else
2252 s := _lc[I_PLAYER_FLAG_BLUE];
2254 g_Console_Add(Format(_lc[I_PLAYER_FLAG_DROP], [Pl.Name, s]), True);
2255 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_DROP], [AnsiUpperCase(s)]), 144);
2257 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2258 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2259 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2260 a := 0
2261 else
2262 a := 1;
2264 if not sound_lost_flag[a].IsPlaying() then
2265 sound_lost_flag[a].Play();
2266 end;
2268 FLAG_STATE_SCORED:
2269 begin
2270 g_Map_ResetFlag(FLAG_RED);
2271 g_Map_ResetFlag(FLAG_BLUE);
2272 if Quiet or (Pl = nil) then Exit;
2273 Pl.SetFlag(FLAG_NONE);
2275 if Fl = FLAG_RED then
2276 s := _lc[I_PLAYER_FLAG_RED]
2277 else
2278 s := _lc[I_PLAYER_FLAG_BLUE];
2280 ts := Format('%.4d', [gFlags[Fl].CaptureTime]);
2281 Insert('.', ts, Length(ts) + 1 - 3);
2282 g_Console_Add(Format(_lc[I_PLAYER_FLAG_CAPTURE], [Pl.Name, s, ts]), True);
2283 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_CAPTURE], [AnsiUpperCase(s)]), 144);
2285 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2286 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2287 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2288 a := 0
2289 else
2290 a := 1;
2292 if not sound_cap_flag[a].IsPlaying() then
2293 sound_cap_flag[a].Play();
2294 end;
2296 FLAG_STATE_RETURNED:
2297 begin
2298 g_Map_ResetFlag(Fl);
2299 if Quiet then Exit;
2301 if Fl = FLAG_RED then
2302 s := _lc[I_PLAYER_FLAG_RED]
2303 else
2304 s := _lc[I_PLAYER_FLAG_BLUE];
2306 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2308 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2309 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2310 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2311 a := 0
2312 else
2313 a := 1;
2315 if not sound_ret_flag[a].IsPlaying() then
2316 sound_ret_flag[a].Play();
2317 end;
2318 end;
2319 end;
2321 procedure MC_RECV_GameSettings(var M: TMsg);
2322 begin
2323 gGameSettings.GameMode := M.ReadByte();
2324 gGameSettings.GoalLimit := M.ReadWord();
2325 gGameSettings.TimeLimit := M.ReadWord();
2326 gGameSettings.MaxLives := M.ReadByte();
2327 gGameSettings.Options := M.ReadLongWord();
2328 end;
2330 // PLAYER
2332 function MC_RECV_PlayerCreate(var M: TMsg): Word;
2333 var
2334 PID, DID: Word;
2335 PName, Model: string;
2336 Color: TRGB;
2337 T: Byte;
2338 Pl: TPlayer;
2339 begin
2340 PID := M.ReadWord();
2341 Pl := g_Player_Get(PID);
2343 PName := M.ReadString();
2344 Model := M.ReadString();
2345 Color.R := M.ReadByte();
2346 Color.G := M.ReadByte();
2347 Color.B := M.ReadByte();
2348 T := M.ReadByte();
2350 Result := 0;
2351 if (PID <> NetPlrUID1) and (PID <> NetPlrUID2) then
2352 begin
2353 if (Pl <> nil) then Exit;
2354 DID := g_Player_Create(Model, Color, T, False);
2355 with g_Player_Get(DID) do
2356 begin
2357 UID := PID;
2358 Name := PName;
2359 Reset(True);
2360 end;
2361 end
2362 else
2363 begin
2364 if (PID = NetPlrUID1) and (gPlayer1 <> nil) then begin
2365 gPlayer1.UID := PID;
2366 gPlayer1.Model.SetColor(Color.R, Color.G, Color.B);
2367 gPlayer1.ChangeTeam(T);
2368 end;
2369 if (PID = NetPlrUID2) and (gPlayer2 <> nil) then begin
2370 gPlayer2.UID := PID;
2371 gPlayer2.Model.SetColor(Color.R, Color.G, Color.B);
2372 gPlayer2.ChangeTeam(T);
2373 end;
2374 end;
2376 if NetDeafLevel < 3 then
2377 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
2378 e_WriteLog('NET: Player ' + PName + ' [' + IntToStr(PID) + '] added.', TMsgType.Notify);
2379 Result := PID;
2380 end;
2382 function MC_RECV_PlayerPos(var M: TMsg): Word;
2383 var
2384 GT: LongWord;
2385 PID: Word;
2386 kByte: Word;
2387 Pl: TPlayer;
2388 Dir: Byte;
2389 TmpX, TmpY: Integer;
2390 begin
2391 Result := 0;
2393 GT := M.ReadLongWord();
2394 if GT < gTime - NET_MAX_DIFFTIME then
2395 begin
2396 gTime := GT;
2397 Exit;
2398 end;
2399 gTime := GT;
2401 PID := M.ReadWord();
2402 Pl := g_Player_Get(PID);
2404 if Pl = nil then Exit;
2406 Result := PID;
2408 with Pl do
2409 begin
2410 FPing := M.ReadWord();
2411 FLoss := M.ReadByte();
2412 kByte := M.ReadWord();
2413 Dir := M.ReadByte();
2415 TmpX := M.ReadLongInt();
2416 TmpY := M.ReadLongInt();
2418 ReleaseKeys;
2420 if LongBool(kByte and NET_KEY_CHAT) then
2421 PressKey(KEY_CHAT, 10000)
2422 else
2423 begin
2424 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
2425 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
2426 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
2427 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
2428 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
2429 end;
2431 JustTeleported := LongBool(kByte and NET_KEY_FORCEDIR);
2433 if ((Pl <> gPlayer1) and (Pl <> gPlayer2)) or JustTeleported then
2434 SetDirection(TDirection(Dir));
2436 GameVelX := M.ReadLongInt();
2437 GameVelY := M.ReadLongInt();
2438 GameAccelX := M.ReadLongInt();
2439 GameAccelY := M.ReadLongInt();
2440 SetLerp(TmpX, TmpY);
2441 if NetForcePlayerUpdate then Update();
2442 end;
2443 end;
2445 function MC_RECV_PlayerStats(var M: TMsg): Word;
2446 var
2447 PID: Word;
2448 Pl: TPlayer;
2449 I, OldFire: Integer;
2450 checkWeapon: Boolean;
2451 OldJet, Flam: Boolean;
2452 NewTeam: Byte;
2453 begin
2454 PID := M.ReadWord();
2455 Pl := g_Player_Get(PID);
2456 Result := 0;
2457 if Pl = nil then
2458 Exit;
2460 with Pl do
2461 begin
2462 alive := (M.ReadByte() <> 0);
2463 GodMode := (M.ReadByte() <> 0);
2464 Health := M.ReadLongInt();
2465 Armor := M.ReadLongInt();
2466 Air := M.ReadLongInt();
2467 JetFuel := M.ReadLongInt();
2468 Lives := M.ReadByte();
2469 NewTeam := M.ReadByte();
2471 for I := WP_FIRST to WP_LAST do
2472 begin
2473 checkWeapon := (M.ReadByte() <> 0);
2474 if ( ((PID = gPlayer1.UID) or ( (gPlayer2 <> nil) and (PID = gPlayer2.UID))) and (I <> WEAPON_PISTOL) and (I <> WEAPON_KASTET ) and (gWeaponAutoswitch = True)) then
2475 begin
2476 if ( (checkWeapon = True) and (FWeapon[I] = False) ) then
2477 begin
2478 FWeapon[I] := True;
2479 if (PID = gPlayer1.UID) then gSelectWeapon[0, I] := True
2480 else gSelectWeapon[1, I] := True;
2481 end;
2482 end;
2483 FWeapon[I] := checkWeapon;
2484 end;
2485 for I := A_BULLETS to A_HIGH do
2486 FAmmo[I] := M.ReadWord();
2488 for I := A_BULLETS to A_HIGH do
2489 FMaxAmmo[I] := M.ReadWord();
2491 for I := MR_SUIT to MR_MAX do
2492 FMegaRulez[I] := M.ReadLongWord();
2494 FRulez := [];
2495 if (M.ReadByte() <> 0) then
2496 FRulez := FRulez + [R_ITEM_BACKPACK];
2497 if (M.ReadByte() <> 0) then
2498 FRulez := FRulez + [R_KEY_RED];
2499 if (M.ReadByte() <> 0) then
2500 FRulez := FRulez + [R_KEY_GREEN];
2501 if (M.ReadByte() <> 0) then
2502 FRulez := FRulez + [R_KEY_BLUE];
2503 if (M.ReadByte() <> 0) then
2504 FRulez := FRulez + [R_BERSERK];
2506 Frags := M.ReadLongInt();
2507 Death := M.ReadLongInt();
2509 SetWeapon(M.ReadByte());
2511 FSpectator := M.ReadByte() <> 0;
2512 if FSpectator then
2513 begin
2514 if UID = NetPlrUID1 then
2515 begin
2516 gSpectLatchPID1 := UID;
2517 gPlayer1 := nil;
2518 end;
2519 if UID = NetPlrUID2 then
2520 begin
2521 gSpectLatchPID2 := UID;
2522 gPlayer2 := nil;
2523 end;
2524 end
2525 else
2526 begin
2527 if (gPlayer1 = nil) and (gSpectLatchPID1 > 0) and (UID = gSpectLatchPID1) then
2528 begin
2529 gPlayer1 := Pl;
2530 gSpectLatchPID1 := 0;
2531 end;
2532 if (gPlayer2 = nil) and (gSpectLatchPID2 > 0) and (UID = gSpectLatchPID2) then
2533 begin
2534 gPlayer2 := Pl;
2535 gSpectLatchPID2 := 0;
2536 end;
2537 end;
2538 FGhost := M.ReadByte() <> 0;
2539 FPhysics := M.ReadByte() <> 0;
2540 FNoRespawn := M.ReadByte() <> 0;
2541 OldJet := FJetpack;
2542 FJetpack := M.ReadByte() <> 0;
2543 OldFire := FFireTime;
2544 FFireTime := M.ReadLongInt();
2545 if (OldFire <= 0) and (FFireTime > 0) then
2546 g_Sound_PlayExAt('SOUND_IGNITE', Obj.X, Obj.Y);
2547 Flam := M.ReadByte() <> 0;
2548 FSpawnInvul := M.ReadLongInt();
2549 if OldJet and not FJetpack then
2550 JetpackOff
2551 else if not OldJet and FJetpack then
2552 JetpackOn;
2553 if FFlaming and not Flam then
2554 FlamerOff;
2555 if Team <> NewTeam then
2556 Pl.ChangeTeam(NewTeam);
2557 end;
2559 Result := PID;
2560 end;
2562 function MC_RECV_PlayerDamage(var M: TMsg): Word;
2563 var
2564 PID: Word;
2565 Pl: TPlayer;
2566 Kind: Byte;
2567 Attacker, Value: Word;
2568 VX, VY: Integer;
2569 begin
2570 Result := 0;
2571 if not gGameOn then Exit;
2572 PID := M.ReadWord();
2573 Pl := g_Player_Get(PID);
2574 if Pl = nil then Exit;
2576 Kind := M.ReadByte();
2577 Attacker := M.ReadWord();
2578 Value := M.ReadWord();
2579 VX := M.ReadWord();
2580 VY := M.ReadWord();
2582 with Pl do
2583 Damage(Value, Attacker, VX, VY, Kind);
2585 Result := PID;
2586 end;
2588 function MC_RECV_PlayerDeath(var M: TMsg): Word;
2589 var
2590 PID: Word;
2591 Pl: TPlayer;
2592 KillType, DeathType: Byte;
2593 Attacker: Word;
2594 begin
2595 Result := 0;
2596 if not gGameOn then Exit;
2597 PID := M.ReadWord();
2598 Pl := g_Player_Get(PID);
2599 if Pl = nil then Exit;
2601 KillType := M.ReadByte();
2602 DeathType := M.ReadByte();
2603 Attacker := M.ReadWord();
2605 with Pl do
2606 begin
2607 Kill(KillType, Attacker, DeathType);
2608 SoftReset;
2609 end;
2610 end;
2612 function MC_RECV_PlayerDelete(var M: TMsg): Word;
2613 var
2614 PID: Word;
2615 Pl: TPlayer;
2616 begin
2617 PID := M.ReadWord();
2618 Pl := g_Player_Get(PID);
2619 Result := 0;
2620 if Pl = nil then Exit;
2622 if NetDeafLevel < 3 then
2623 g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [Pl.Name]), True);
2624 e_WriteLog('NET: Player ' + Pl.Name + ' [' + IntToStr(PID) + '] removed.', TMsgType.Notify);
2626 g_Player_Remove(PID);
2628 Result := PID;
2629 end;
2631 function MC_RECV_PlayerFire(var M: TMsg): Word;
2632 var
2633 PID: Word;
2634 Weap: Byte;
2635 Pl: TPlayer;
2636 X, Y, AX, AY: Integer;
2637 SHID: Integer;
2638 begin
2639 Result := 0;
2640 if not gGameOn then Exit;
2641 PID := M.ReadWord();
2642 Pl := g_Player_Get(PID);
2643 if Pl = nil then Exit;
2645 Weap := M.ReadByte();
2646 X := M.ReadLongInt();
2647 Y := M.ReadLongInt();
2648 AX := M.ReadLongInt();
2649 AY := M.ReadLongInt();
2650 SHID := M.ReadLongInt();
2652 with Pl do
2653 if alive then NetFire(Weap, X, Y, AX, AY, SHID);
2654 end;
2656 procedure MC_RECV_PlayerSettings(var M: TMsg);
2657 var
2658 TmpName: string;
2659 TmpModel: string;
2660 TmpColor: TRGB;
2661 TmpTeam: Byte;
2662 Pl: TPlayer;
2663 PID: Word;
2664 begin
2665 PID := M.ReadWord();
2666 Pl := g_Player_Get(PID);
2667 if Pl = nil then Exit;
2669 TmpName := M.ReadString();
2670 TmpModel := M.ReadString();
2671 TmpColor.R := M.ReadByte();
2672 TmpColor.G := M.ReadByte();
2673 TmpColor.B := M.ReadByte();
2674 TmpTeam := M.ReadByte();
2676 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
2677 begin
2678 Pl.ChangeTeam(TmpTeam);
2679 if gPlayer1 = Pl then
2680 gPlayer1Settings.Team := TmpTeam;
2681 if gPlayer2 = Pl then
2682 gPlayer2Settings.Team := TmpTeam;
2683 end else
2684 Pl.SetColor(TmpColor);
2686 if Pl.Name <> TmpName then
2687 begin
2688 if NetDeafLevel < 3 then
2689 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
2690 Pl.Name := TmpName;
2691 end;
2693 if TmpModel <> Pl.Model.Name then
2694 Pl.SetModel(TmpModel);
2695 end;
2697 // ITEM
2699 procedure MC_RECV_ItemSpawn(var M: TMsg);
2700 var
2701 ID: Word;
2702 AID: DWord;
2703 X, Y, VX, VY: Integer;
2704 T: Byte;
2705 Quiet, Fall{, Resp}: Boolean;
2706 Anim: TAnimation;
2707 it: PItem;
2708 begin
2709 if not gGameOn then Exit;
2710 ID := M.ReadWord();
2711 Quiet := M.ReadByte() <> 0;
2712 T := M.ReadByte();
2713 Fall := M.ReadByte() <> 0;
2714 {Resp :=} M.ReadByte();
2715 X := M.ReadLongInt();
2716 Y := M.ReadLongInt();
2717 VX := M.ReadLongInt();
2718 VY := M.ReadLongInt();
2720 g_Items_Create(X, Y, T and $7F, Fall, False, False, ID);
2721 if ((T and $80) <> 0) then g_Items_SetDrop(ID);
2723 it := g_Items_ByIdx(ID);
2724 it.Obj.Vel.X := VX;
2725 it.Obj.Vel.Y := VY;
2727 if not Quiet then
2728 begin
2729 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
2730 if g_Frames_Get(AID, 'FRAMES_ITEM_RESPAWN') then
2731 begin
2732 Anim := TAnimation.Create(AID, False, 4);
2733 g_GFX_OnceAnim(X+(it.Obj.Rect.Width div 2)-16, Y+(it.Obj.Rect.Height div 2)-16, Anim);
2734 Anim.Free();
2735 end;
2736 end;
2737 end;
2739 procedure MC_RECV_ItemDestroy(var M: TMsg);
2740 var
2741 ID: Word;
2742 Quiet: Boolean;
2743 begin
2744 if not gGameOn then Exit;
2745 ID := M.ReadWord();
2746 Quiet := M.ReadByte() <> 0;
2748 if not g_Items_ValidId(ID) then exit;
2750 if not Quiet then g_Items_EmitPickupSound(ID);
2752 g_Items_Remove(ID);
2753 end;
2755 procedure MC_RECV_ItemPos(var M: TMsg);
2756 var
2757 ID: Word;
2758 X, Y, VX, VY: Integer;
2759 it: PItem;
2760 begin
2761 if not gGameOn then Exit;
2763 ID := M.ReadWord();
2764 X := M.ReadLongInt();
2765 Y := M.ReadLongInt();
2766 VX := M.ReadLongInt();
2767 VY := M.ReadLongInt();
2769 if g_Items_ValidId(ID) then
2770 begin
2771 it := g_Items_ByIdx(ID);
2772 it.Obj.X := X;
2773 it.Obj.Y := Y;
2774 it.Obj.Vel.X := VX;
2775 it.Obj.Vel.Y := VY;
2776 it.positionChanged();
2777 end;
2778 end;
2780 // PANEL
2782 procedure MC_RECV_PanelTexture(var M: TMsg);
2783 var
2784 TP: TPanel;
2785 PGUID: Integer;
2786 Tex, Fr: Integer;
2787 Loop, Cnt: Byte;
2788 begin
2789 if not gGameOn then Exit;
2791 PGUID := Integer(M.ReadLongWord());
2792 Tex := M.ReadLongInt();
2793 Fr := M.ReadLongInt();
2794 Cnt := M.ReadByte();
2795 Loop := M.ReadByte();
2797 TP := g_Map_PanelByGUID(PGUID);
2798 if (TP <> nil) then
2799 begin
2800 // switch texture
2801 TP.SetTexture(Tex, Loop);
2802 TP.SetFrame(Fr, Cnt);
2803 end;
2804 end;
2806 procedure MC_RECV_PanelState(var M: TMsg);
2807 var
2808 PGUID: Integer;
2809 E: Boolean;
2810 Lift: Byte;
2811 X, Y, W, H: Integer;
2812 TP: TPanel;
2813 speedX, speedY, startX, startY, endX, endY: Integer;
2814 sizeSpX, sizeSpY, sizeEX, sizeEY: Integer;
2815 mpflags: Byte;
2816 begin
2817 if not gGameOn then Exit;
2819 PGUID := Integer(M.ReadLongWord());
2820 E := (M.ReadByte() <> 0);
2821 Lift := M.ReadByte();
2822 X := M.ReadLongInt();
2823 Y := M.ReadLongInt();
2824 W := M.ReadWord();
2825 H := M.ReadWord();
2826 // mplats
2827 speedX := M.ReadLongInt();
2828 speedY := M.ReadLongInt();
2829 startX := M.ReadLongInt();
2830 startY := M.ReadLongInt();
2831 endX := M.ReadLongInt();
2832 endY := M.ReadLongInt();
2833 sizeSpX := M.ReadLongInt();
2834 sizeSpY := M.ReadLongInt();
2835 sizeEX := M.ReadLongInt();
2836 sizeEY := M.ReadLongInt();
2837 mpflags := M.ReadByte(); // bit0: TP.movingActive; bit1: TP.moveOnce
2839 TP := g_Map_PanelByGUID(PGUID);
2840 if (TP = nil) then exit;
2842 // update lifts state
2843 if TP.isGLift then g_Map_SetLiftGUID(PGUID, Lift);
2845 // update enabled/disabled state for all panels
2846 if E then g_Map_EnableWallGUID(PGUID) else g_Map_DisableWallGUID(PGUID);
2848 // update panel position, as it can be moved (mplat)
2849 TP.X := X;
2850 TP.Y := Y;
2851 TP.Width := W;
2852 TP.Height := H;
2853 // update mplat state
2854 TP.movingSpeedX := speedX;
2855 TP.movingSpeedY := speedY;
2856 TP.movingStartX := startX;
2857 TP.movingStartY := startY;
2858 TP.movingEndX := endX;
2859 TP.movingEndY := endY;
2860 TP.sizeSpeedX := sizeSpX;
2861 TP.sizeSpeedY := sizeSpY;
2862 TP.sizeEndX := sizeEX;
2863 TP.sizeEndY := sizeEY;
2864 TP.movingActive := ((mpflags and 1) <> 0);
2865 TP.moveOnce := ((mpflags and 2) <> 0);
2866 // notify panel of it's position/size change, so it can fix other internal structures
2867 TP.positionChanged();
2868 end;
2870 // TRIGGERS
2872 procedure MC_RECV_TriggerSound(var M: TMsg);
2873 var
2874 SPlaying: Boolean;
2875 SPos, SID: LongWord;
2876 SCount: LongInt;
2877 I: Integer;
2878 begin
2879 if not gGameOn then Exit;
2880 if gTriggers = nil then Exit;
2882 SID := M.ReadLongWord();
2883 SPlaying := M.ReadByte() <> 0;
2884 SPos := M.ReadLongWord();
2885 SCount := M.ReadLongInt();
2887 for I := Low(gTriggers) to High(gTriggers) do
2888 if gTriggers[I].TriggerType = TRIGGER_SOUND then
2889 if gTriggers[I].ClientID = SID then
2890 with gTriggers[I] do
2891 begin
2892 if Sound <> nil then
2893 begin
2894 if SPlaying then
2895 begin
2896 if tgcLocal then
2897 Sound.PlayVolumeAt(X+(Width div 2), Y+(Height div 2), tgcVolume/255.0)
2898 else
2899 Sound.PlayPanVolume((tgcPan-127.0)/128.0, tgcVolume/255.0);
2900 Sound.SetPosition(SPos);
2901 end
2902 else
2903 if Sound.IsPlaying then Sound.Stop;
2904 end;
2906 SoundPlayCount := SCount;
2907 end;
2908 end;
2910 procedure MC_RECV_TriggerMusic(var M: TMsg);
2911 var
2912 MName: string;
2913 MPlaying: Boolean;
2914 MPos: LongWord;
2915 MPaused: Boolean;
2916 begin
2917 if not gGameOn then Exit;
2919 MName := M.ReadString();
2920 MPlaying := M.ReadByte() <> 0;
2921 MPos := M.ReadLongWord();
2922 MPaused := M.ReadByte() <> 0;
2923 MPos := MPos+1; //k8: stfu, fpc!
2925 if MPlaying then
2926 begin
2927 gMusic.SetByName(MName);
2928 gMusic.Play(True);
2929 // gMusic.SetPosition(MPos);
2930 gMusic.SpecPause := MPaused;
2931 end
2932 else
2933 if gMusic.IsPlaying then gMusic.Stop;
2934 end;
2936 // MONSTERS
2938 procedure MC_RECV_MonsterSpawn(var M: TMsg);
2939 var
2940 ID: Word;
2941 MType, MState, MDir, MAnim, MBehav: Byte;
2942 X, Y, VX, VY, MTargTime, MHealth, MAmmo, MSleep: Integer;
2943 MTarg: Word;
2944 Mon: TMonster;
2945 begin
2946 ID := M.ReadWord();
2947 Mon := g_Monsters_ByUID(ID);
2948 if Mon <> nil then
2949 Exit;
2951 MType := M.ReadByte();
2952 MState := M.ReadByte();
2953 MAnim := M.ReadByte();
2954 MTarg := M.ReadWord();
2955 MTargTime := M.ReadLongInt();
2956 MBehav := M.ReadByte();
2957 MSleep := M.ReadLongInt();
2958 MHealth := M.ReadLongInt();
2959 MAmmo := M.ReadLongInt();
2961 X := M.ReadLongInt();
2962 Y := M.ReadLongInt();
2963 VX := M.ReadLongInt();
2964 VY := M.ReadLongInt();
2965 MDir := M.ReadByte();
2967 g_Monsters_Create(MType, X, Y, TDirection(MDir), False, ID);
2968 Mon := g_Monsters_ByUID(ID);
2969 if Mon = nil then
2970 Exit;
2972 with Mon do
2973 begin
2975 MonsterAnim := MAnim;
2976 MonsterTargetUID := MTarg;
2977 MonsterTargetTime := MTargTime;
2978 MonsterBehaviour := MBehav;
2979 MonsterSleep := MSleep;
2980 MonsterAmmo := MAmmo;
2981 SetHealth(MHealth);
2983 SetState(MState);
2985 setPosition(X, Y); // this will call positionChanged();
2986 GameVelX := VX;
2987 GameVelY := VY;
2988 end;
2989 end;
2991 procedure MC_RECV_MonsterPos(var M: TMsg);
2992 var
2993 Mon: TMonster;
2994 ID: Word;
2995 X, Y: Integer;
2996 begin
2997 ID := M.ReadWord();
2998 Mon := g_Monsters_ByUID(ID);
2999 if Mon = nil then
3000 Exit;
3002 with Mon do
3003 begin
3004 X := M.ReadLongInt();
3005 Y := M.ReadLongInt();
3006 Mon.setPosition(X, Y); // this will call `positionChanged()`
3007 GameVelX := M.ReadLongInt();
3008 GameVelY := M.ReadLongInt();
3009 GameDirection := TDirection(M.ReadByte());
3010 end;
3011 end;
3013 procedure MC_RECV_MonsterState(var M: TMsg);
3014 var
3015 ID, OldFire: Integer;
3016 MState, MFAnm: Byte;
3017 Mon: TMonster;
3018 AnimRevert: Boolean;
3019 begin
3020 ID := M.ReadWord();
3021 Mon := g_Monsters_ByUID(ID);
3022 if Mon = nil then Exit;
3024 MState := M.ReadByte();
3025 MFAnm := M.ReadByte();
3027 with Mon do
3028 begin
3029 MonsterTargetUID := M.ReadWord();
3030 MonsterTargetTime := M.ReadLongInt();
3031 MonsterSleep := M.ReadLongInt();
3032 MonsterHealth := M.ReadLongInt();
3033 MonsterAmmo := M.ReadLongInt();
3034 MonsterPain := M.ReadLongInt();
3035 AnimRevert := M.ReadByte() <> 0;
3036 OldFire := FFireTime;
3037 FFireTime := M.ReadLongInt();
3038 if (OldFire <= 0) and (FFireTime > 0) then
3039 g_Sound_PlayExAt('SOUND_IGNITE', Obj.X, Obj.Y);
3040 RevertAnim(AnimRevert);
3042 if MonsterState <> MState then
3043 begin
3044 if (MState = MONSTATE_GO) and (MonsterState = MONSTATE_SLEEP) then WakeUpSound();
3045 if (MState = MONSTATE_DIE) then DieSound();
3046 if (MState = MONSTATE_PAIN) then MakeBloodSimple(Min(200, MonsterPain));
3047 if (MState = MONSTATE_ATTACK) then kick(nil);
3048 if (MState = MONSTATE_DEAD) then SetDeadAnim();
3050 SetState(MState, MFAnm);
3051 end;
3052 end;
3053 end;
3055 procedure MC_RECV_MonsterShot(var M: TMsg);
3056 var
3057 ID: Integer;
3058 Mon: TMonster;
3059 X, Y, VX, VY: Integer;
3060 begin
3061 ID := M.ReadWord();
3063 Mon := g_Monsters_ByUID(ID);
3064 if Mon = nil then Exit;
3066 X := M.ReadLongInt();
3067 Y := M.ReadLongInt();
3068 VX := M.ReadLongInt();
3069 VY := M.ReadLongInt();
3071 Mon.ClientAttack(X, Y, VX, VY);
3072 end;
3074 procedure MC_RECV_MonsterDelete(var M: TMsg);
3075 var
3076 ID: Integer;
3077 Mon: TMonster;
3078 begin
3079 ID := M.ReadWord();
3080 Mon := g_Monsters_ByUID(ID);
3081 if Mon = nil then Exit;
3082 Mon.SetState(5);
3083 Mon.MonsterRemoved := True;
3084 end;
3086 procedure MC_RECV_TimeSync(var M: TMsg);
3087 var
3088 Time: LongWord;
3089 begin
3090 Time := M.ReadLongWord();
3092 if gState = STATE_INTERCUSTOM then
3093 gServInterTime := Min(Time, 255);
3094 end;
3096 procedure MC_RECV_VoteEvent(var M: TMsg);
3097 var
3098 EvID: Byte;
3099 Str1, Str2: string;
3100 Int1, Int2: SmallInt;
3101 begin
3102 EvID := M.ReadByte();
3103 Int1 := M.ReadSmallInt();
3104 Int2 := M.ReadSmallInt();
3105 Str1 := M.ReadString();
3106 Str2 := M.ReadString();
3108 if NetDeafLevel < 2 then
3109 case EvID of
3110 NET_VE_STARTED:
3111 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_STARTED], [Str1, Str2, Int1]), True);
3112 NET_VE_PASSED:
3113 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_PASSED], [Str1]), True);
3114 NET_VE_FAILED:
3115 g_Console_Add(_lc[I_MESSAGE_VOTE_FAILED], True);
3116 NET_VE_VOTE:
3117 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Str1, Int1, Int2]), True);
3118 NET_VE_INPROGRESS:
3119 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_INPROGRESS], [Str1]), True);
3120 end;
3121 end;
3123 // CLIENT SEND
3125 procedure MC_SEND_Info(Password: string);
3126 begin
3127 NetOut.Clear();
3129 NetOut.Write(Byte(NET_MSG_INFO));
3130 NetOut.Write(GAME_VERSION);
3131 NetOut.Write(Password);
3132 NetOut.Write(gPlayer1Settings.Name);
3133 NetOut.Write(gPlayer1Settings.Model);
3134 NetOut.Write(gPlayer1Settings.Color.R);
3135 NetOut.Write(gPlayer1Settings.Color.G);
3136 NetOut.Write(gPlayer1Settings.Color.B);
3137 NetOut.Write(gPlayer1Settings.Team);
3139 g_Net_Client_Send(True, NET_CHAN_SERVICE);
3140 end;
3142 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
3143 begin
3144 NetOut.Write(Byte(NET_MSG_CHAT));
3145 NetOut.Write(Txt);
3146 NetOut.Write(Mode);
3148 g_Net_Client_Send(True, NET_CHAN_CHAT);
3149 end;
3151 procedure MC_SEND_PlayerPos();
3152 var
3153 kByte: Word;
3154 Predict: Boolean;
3155 strafeDir: Byte;
3156 WeaponAct: Byte = 0;
3157 WeaponSelect: Word = 0;
3158 i: Integer;
3159 begin
3160 if not gGameOn then Exit;
3161 if gPlayers = nil then Exit;
3162 if gPlayer1 = nil then Exit;
3164 kByte := 0;
3165 Predict := NetPredictSelf; // and (not NetGotKeys);
3167 if (not gConsoleShow) and (not gChatShow) and (g_ActiveWindow = nil) then
3168 begin
3169 strafeDir := P1MoveButton shr 4;
3170 P1MoveButton := P1MoveButton and $0F;
3172 if gPlayerAction[0, ACTION_MOVELEFT] and (not gPlayerAction[0, ACTION_MOVERIGHT]) then
3173 P1MoveButton := 1
3174 else if (not gPlayerAction[0, ACTION_MOVELEFT]) and gPlayerAction[0, ACTION_MOVERIGHT] then
3175 P1MoveButton := 2
3176 else if (not gPlayerAction[0, ACTION_MOVELEFT]) and (not gPlayerAction[0, ACTION_MOVERIGHT]) then
3177 P1MoveButton := 0;
3179 // strafing
3180 if gPlayerAction[0, ACTION_STRAFE] then
3181 begin
3182 // new strafe mechanics
3183 if (strafeDir = 0) then
3184 strafeDir := P1MoveButton; // start strafing
3185 // now set direction according to strafe (reversed)
3186 if (strafeDir = 2) then
3187 gPlayer1.SetDirection(TDirection.D_LEFT)
3188 else if (strafeDir = 1) then
3189 gPlayer1.SetDirection(TDirection.D_RIGHT)
3190 end
3191 else
3192 begin
3193 strafeDir := 0; // not strafing anymore
3194 if (P1MoveButton = 2) and gPlayerAction[0, ACTION_MOVELEFT] then
3195 gPlayer1.SetDirection(TDirection.D_LEFT)
3196 else if (P1MoveButton = 1) and gPlayerAction[0, ACTION_MOVERIGHT] then
3197 gPlayer1.SetDirection(TDirection.D_RIGHT)
3198 else if P1MoveButton <> 0 then
3199 gPlayer1.SetDirection(TDirection(P1MoveButton-1));
3200 end;
3202 gPlayer1.ReleaseKeys;
3203 if P1MoveButton = 1 then
3204 begin
3205 kByte := kByte or NET_KEY_LEFT;
3206 if Predict then gPlayer1.PressKey(KEY_LEFT, 10000);
3207 end;
3208 if P1MoveButton = 2 then
3209 begin
3210 kByte := kByte or NET_KEY_RIGHT;
3211 if Predict then gPlayer1.PressKey(KEY_RIGHT, 10000);
3212 end;
3213 if gPlayerAction[0, ACTION_LOOKUP] then
3214 begin
3215 kByte := kByte or NET_KEY_UP;
3216 gPlayer1.PressKey(KEY_UP, 10000);
3217 end;
3218 if gPlayerAction[0, ACTION_LOOKDOWN] then
3219 begin
3220 kByte := kByte or NET_KEY_DOWN;
3221 gPlayer1.PressKey(KEY_DOWN, 10000);
3222 end;
3223 if gPlayerAction[0, ACTION_JUMP] then
3224 begin
3225 kByte := kByte or NET_KEY_JUMP;
3226 // gPlayer1.PressKey(KEY_JUMP, 10000); // TODO: Make a prediction option
3227 end;
3228 if gPlayerAction[0, ACTION_ATTACK] then kByte := kByte or NET_KEY_FIRE;
3229 if gPlayerAction[0, ACTION_ACTIVATE] then kByte := kByte or NET_KEY_OPEN;
3231 for i := WP_FACT to WP_LACT do
3232 begin
3233 if gWeaponAction[0, i] then
3234 begin
3235 WeaponAct := WeaponAct or Byte(1 shl i);
3236 gWeaponAction[0, i] := False
3237 end
3238 end;
3240 for i := WP_FIRST to WP_LAST do
3241 begin
3242 if gSelectWeapon[0, i] then
3243 begin
3244 WeaponSelect := WeaponSelect or Word(1 shl i);
3245 gSelectWeapon[0, i] := False
3246 end
3247 end;
3249 // fix movebutton state
3250 P1MoveButton := P1MoveButton or (strafeDir shl 4);
3251 end
3252 else
3253 kByte := NET_KEY_CHAT;
3255 NetOut.Write(Byte(NET_MSG_PLRPOS));
3256 NetOut.Write(gTime);
3257 NetOut.Write(kByte);
3258 NetOut.Write(Byte(gPlayer1.Direction));
3259 NetOut.Write(WeaponAct);
3260 NetOut.Write(WeaponSelect);
3261 //e_WriteLog(Format('S:ws=%d', [WeaponSelect]), MSG_WARNING);
3262 g_Net_Client_Send(True, NET_CHAN_PLAYERPOS);
3264 //kBytePrev := kByte;
3265 //kDirPrev := gPlayer1.Direction;
3266 end;
3268 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
3269 begin
3270 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
3271 NetOut.Write(Byte(Start));
3272 NetOut.Write(Command);
3273 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3274 end;
3276 procedure MC_SEND_PlayerSettings();
3277 begin
3278 NetOut.Write(Byte(NET_MSG_PLRSET));
3279 NetOut.Write(gPlayer1Settings.Name);
3280 NetOut.Write(gPlayer1Settings.Model);
3281 NetOut.Write(gPlayer1Settings.Color.R);
3282 NetOut.Write(gPlayer1Settings.Color.G);
3283 NetOut.Write(gPlayer1Settings.Color.B);
3284 NetOut.Write(gPlayer1Settings.Team);
3286 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3287 end;
3289 procedure MC_SEND_FullStateRequest();
3290 begin
3291 NetOut.Write(Byte(NET_MSG_REQFST));
3293 g_Net_Client_Send(True, NET_CHAN_SERVICE);
3294 end;
3296 procedure MC_SEND_CheatRequest(Kind: Byte);
3297 begin
3298 NetOut.Write(Byte(NET_MSG_CHEAT));
3299 NetOut.Write(Kind);
3301 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3302 end;
3303 procedure MC_SEND_RCONPassword(Password: string);
3304 begin
3305 NetOut.Write(Byte(NET_MSG_RCON_AUTH));
3306 NetOut.Write(Password);
3308 g_Net_Client_Send(True, NET_CHAN_SERVICE);
3309 end;
3310 procedure MC_SEND_RCONCommand(Cmd: string);
3311 begin
3312 NetOut.Write(Byte(NET_MSG_RCON_CMD));
3313 NetOut.Write(Cmd);
3315 g_Net_Client_Send(True, NET_CHAN_SERVICE);
3316 end;
3319 end.