DEADSOFTWARE

869da64bf5c6556f5f7ef047c8d0380d40183a18
[d2df-sdl.git] / src / game / g_netmsg.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$MODE DELPHI}
17 unit g_netmsg;
19 interface
21 uses g_net, g_triggers, Classes, SysUtils, md5;
23 const
24 NET_MSG_INFO = 100;
26 NET_MSG_CHAT = 101;
27 NET_MSG_SND = 102;
28 NET_MSG_GFX = 103;
29 NET_MSG_GEVENT = 104;
30 NET_MSG_SCORE = 105;
31 NET_MSG_COOP = 106;
32 NET_MSG_FLAG = 107;
33 NET_MSG_REQFST = 108;
34 NET_MSG_GSET = 109;
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;
49 NET_MSG_MSPAWN = 131;
50 NET_MSG_MPOS = 132;
51 NET_MSG_MSTATE = 133;
52 NET_MSG_MSHOT = 134;
53 NET_MSG_MDEL = 135;
55 NET_MSG_PSTATE = 141;
56 NET_MSG_PTEX = 142;
58 NET_MSG_TSOUND = 151;
59 NET_MSG_TMUSIC = 152;
61 NET_MSG_SHDEL = 161;
62 NET_MSG_SHADD = 162;
63 NET_MSG_SHPOS = 163;
65 NET_MSG_RCON_AUTH = 191;
66 NET_MSG_RCON_CMD = 192;
67 NET_MSG_TIME_SYNC = 194;
68 NET_MSG_VOTE_EVENT = 195;
70 NET_MSG_MAP_REQUEST = 201;
71 NET_MSG_MAP_RESPONSE = 202;
72 NET_MSG_RES_REQUEST = 203;
73 NET_MSG_RES_RESPONSE = 204;
75 NET_CHAT_SYSTEM = 0;
76 NET_CHAT_PLAYER = 1;
77 NET_CHAT_TEAM = 2;
79 NET_RCON_NOAUTH = 0;
80 NET_RCON_PWGOOD = 1;
81 NET_RCON_PWBAD = 2;
83 NET_GFX_SPARK = 1;
84 NET_GFX_TELE = 2;
85 NET_GFX_RESPAWN = 3;
86 NET_GFX_FIRE = 4;
87 NET_GFX_EXPLODE = 5;
88 NET_GFX_BFGEXPL = 6;
89 NET_GFX_BFGHIT = 7;
90 NET_GFX_SHELL1 = 8;
91 NET_GFX_SHELL2 = 9;
92 NET_GFX_SHELL3 = 10;
94 NET_EV_MAPSTART = 1;
95 NET_EV_MAPEND = 2;
96 NET_EV_CHANGE_TEAM = 3;
97 NET_EV_PLAYER_KICK = 4;
98 NET_EV_PLAYER_BAN = 5;
99 NET_EV_LMS_WARMUP = 6;
100 NET_EV_LMS_SURVIVOR = 7;
101 NET_EV_RCON = 8;
102 NET_EV_BIGTEXT = 9;
103 NET_EV_SCORE = 10;
104 NET_EV_SCORE_MSG = 11;
105 NET_EV_LMS_START = 12;
106 NET_EV_LMS_WIN = 13;
107 NET_EV_TLMS_WIN = 14;
108 NET_EV_LMS_LOSE = 15;
109 NET_EV_LMS_DRAW = 16;
110 NET_EV_KILLCOMBO = 17;
111 NET_EV_PLAYER_TOUCH = 18;
113 NET_VE_STARTED = 1;
114 NET_VE_PASSED = 2;
115 NET_VE_FAILED = 3;
116 NET_VE_VOTE = 4;
117 NET_VE_REVOKE = 5;
118 NET_VE_INPROGRESS = 6;
120 NET_FLAG_GET = 1;
121 NET_FLAG_DROP = 2;
122 NET_FLAG_CAP = 3;
123 NET_FLAG_RETURN = 4;
125 NET_CHEAT_SUICIDE = 1;
126 NET_CHEAT_SPECTATE = 2;
128 NET_MAX_DIFFTIME = 5000 div 36;
130 // HOST MESSAGES
132 procedure MH_RECV_Info(C: pTNetClient; P: Pointer);
133 procedure MH_RECV_Chat(C: pTNetClient; P: Pointer);
134 procedure MH_RECV_FullStateRequest(C: pTNetClient; P: Pointer);
135 function MH_RECV_PlayerPos(C: pTNetClient; P: Pointer): Word;
136 procedure MH_RECV_PlayerSettings(C: pTNetClient; P: Pointer);
137 procedure MH_RECV_CheatRequest(C: pTNetClient; P: Pointer);
138 procedure MH_RECV_RCONPassword(C: pTNetClient; P: Pointer);
139 procedure MH_RECV_RCONCommand(C: pTNetClient; P: Pointer);
140 procedure MH_RECV_MapRequest(C: pTNetClient; P: Pointer);
141 procedure MH_RECV_ResRequest(C: pTNetClient; P: Pointer);
142 procedure MH_RECV_Vote(C: pTNetClient; P: Pointer);
144 // GAME
145 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
146 procedure MH_SEND_Info(ID: Byte);
147 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
148 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
149 procedure MH_SEND_Sound(X, Y: Integer; Name: string; ID: Integer = NET_EVERYONE);
150 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
151 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
152 procedure MH_SEND_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
153 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
154 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
155 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
156 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
157 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
158 // PLAYER
159 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
160 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
161 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
162 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
163 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
164 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
165 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
166 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
167 // ITEM
168 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
169 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
170 // PANEL
171 procedure MH_SEND_PanelTexture(PType: Word; PID: LongWord; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
172 procedure MH_SEND_PanelState(PType: Word; PID: LongWord; ID: Integer = NET_EVERYONE);
173 // MONSTER
174 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
175 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
176 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
177 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
178 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
179 // TRIGGER
180 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
181 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
182 // MISC
183 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
184 procedure MH_SEND_VoteEvent(EvType: Byte;
185 StrArg1: string = 'a'; StrArg2: string = 'b';
186 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
187 ID: Integer = NET_EVERYONE);
189 // CLIENT MESSAGES //
191 // GAME
192 procedure MC_RECV_Chat(P: Pointer);
193 procedure MC_RECV_Effect(P: Pointer);
194 procedure MC_RECV_Sound(P: Pointer);
195 procedure MC_RECV_GameStats(P: Pointer);
196 procedure MC_RECV_CoopStats(P: Pointer);
197 procedure MC_RECV_GameEvent(P: Pointer);
198 procedure MC_RECV_FlagEvent(P: Pointer);
199 procedure MC_RECV_GameSettings(P: Pointer);
200 // PLAYER
201 function MC_RECV_PlayerCreate(P: Pointer): Word;
202 function MC_RECV_PlayerPos(P: Pointer): Word;
203 function MC_RECV_PlayerStats(P: Pointer): Word;
204 function MC_RECV_PlayerDelete(P: Pointer): Word;
205 function MC_RECV_PlayerDamage(P: Pointer): Word;
206 function MC_RECV_PlayerDeath(P: Pointer): Word;
207 function MC_RECV_PlayerFire(P: Pointer): Word;
208 procedure MC_RECV_PlayerSettings(P: Pointer);
209 // ITEM
210 procedure MC_RECV_ItemSpawn(P: Pointer);
211 procedure MC_RECV_ItemDestroy(P: Pointer);
212 // PANEL
213 procedure MC_RECV_PanelTexture(P: Pointer);
214 procedure MC_RECV_PanelState(P: Pointer);
215 // MONSTER
216 procedure MC_RECV_MonsterSpawn(P: Pointer);
217 procedure MC_RECV_MonsterPos(P: Pointer);
218 procedure MC_RECV_MonsterState(P: Pointer);
219 procedure MC_RECV_MonsterShot(P: Pointer);
220 procedure MC_RECV_MonsterDelete(P: Pointer);
221 // SHOT
222 procedure MC_RECV_CreateShot(P: Pointer);
223 procedure MC_RECV_UpdateShot(P: Pointer);
224 procedure MC_RECV_DeleteShot(P: Pointer);
225 // TRIGGER
226 procedure MC_RECV_TriggerSound(P: Pointer);
227 procedure MC_RECV_TriggerMusic(P: Pointer);
228 // MISC
229 procedure MC_RECV_TimeSync(P: Pointer);
230 procedure MC_RECV_VoteEvent(P: Pointer);
231 // SERVICE
232 procedure MC_SEND_Info(Password: string);
233 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
234 procedure MC_SEND_PlayerPos();
235 procedure MC_SEND_FullStateRequest();
236 procedure MC_SEND_PlayerSettings();
237 procedure MC_SEND_CheatRequest(Kind: Byte);
238 procedure MC_SEND_RCONPassword(Password: string);
239 procedure MC_SEND_RCONCommand(Cmd: string);
240 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
241 // DOWNLOAD
242 procedure MC_SEND_MapRequest();
243 procedure MC_SEND_ResRequest(const resName: AnsiString);
245 type
246 TExternalResourceInfo = record
247 Name: string[255];
248 md5: TMD5Digest;
249 end;
251 TResDataMsg = record
252 MsgId: Byte;
253 FileSize: Integer;
254 FileData: AByte;
255 end;
257 TMapDataMsg = record
258 MsgId: Byte;
259 FileSize: Integer;
260 FileData: AByte;
261 ExternalResources: array of TExternalResourceInfo;
262 end;
264 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
265 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
267 implementation
269 uses
270 Math, ENet, e_input, e_fixedbuffer, e_graphics, e_log,
271 g_textures, g_gfx, g_sound, g_console, g_basic, g_options, g_main,
272 g_game, g_player, g_map, g_panel, g_items, g_weapons, g_phys, g_gui,
273 g_language, g_monsters, g_netmaster, utils, wadreader, MAPDEF;
275 const
276 NET_KEY_LEFT = 1;
277 NET_KEY_RIGHT = 2;
278 NET_KEY_UP = 4;
279 NET_KEY_DOWN = 8;
280 NET_KEY_JUMP = 16;
281 NET_KEY_FIRE = 32;
282 NET_KEY_OPEN = 64;
283 NET_KEY_NW = 256;
284 NET_KEY_PW = 512;
285 NET_KEY_CHAT = 2048;
286 NET_KEY_FORCEDIR = 4096;
288 //var
289 //kBytePrev: Word = 0;
290 //kDirPrev: TDirection = D_LEFT;
291 //HostGameTime: Word = 0;
293 // HOST MESSAGES //
296 // GAME
298 procedure MH_RECV_Chat(C: pTNetClient; P: Pointer);
299 var
300 Txt: string;
301 Mode: Byte;
302 PID: Word;
303 Pl: TPlayer;
304 begin
305 PID := C^.Player;
306 Pl := g_Player_Get(PID);
308 Txt := e_Raw_Read_String(P);
309 Mode := e_Raw_Read_Byte(P);
310 if (Mode = NET_CHAT_SYSTEM) then
311 Mode := NET_CHAT_PLAYER; // prevent sending system messages from clients
312 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
313 Mode := NET_CHAT_PLAYER; // revert to player chat in non-team game
315 if Pl = nil then
316 MH_SEND_Chat(Txt, Mode)
317 else
318 MH_SEND_Chat(Pl.Name + ': ' + Txt, Mode, IfThen(Mode = NET_CHAT_TEAM, Pl.Team, NET_EVERYONE));
319 end;
321 procedure MH_RECV_Info(C: pTNetClient; P: Pointer);
322 var
323 Ver, PName, Model, Pw: string;
324 R, G, B, T: Byte;
325 PID: Word;
326 Color: TRGB;
327 I: Integer;
328 begin
329 Ver := e_Raw_Read_String(P);
330 Pw := e_Raw_Read_String(P);
331 PName := e_Raw_Read_String(P);
332 Model := e_Raw_Read_String(P);
333 R := e_Raw_Read_Byte(P);
334 G := e_Raw_Read_Byte(P);
335 B := e_Raw_Read_Byte(P);
336 T := e_Raw_Read_Byte(P);
338 if Ver <> GAME_VERSION then
339 begin
340 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
341 _lc[I_NET_DISC_VERSION]);
342 enet_peer_disconnect(C^.Peer, NET_DISC_VERSION);
343 Exit;
344 end;
346 if g_Net_IsHostBanned(C^.Peer^.address.host) then
347 begin
348 if g_Net_IsHostBanned(C^.Peer^.address.host, True) then
349 begin
350 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
351 _lc[I_NET_DISC_BAN]);
352 enet_peer_disconnect(C^.Peer, NET_DISC_BAN);
353 end
354 else
355 begin
356 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
357 _lc[I_NET_DISC_BAN]);
358 enet_peer_disconnect(C^.Peer, NET_DISC_TEMPBAN);
359 end;
360 Exit;
361 end;
363 if NetPassword <> '' then
364 if AnsiLowerCase(NetPassword) <> AnsiLowerCase(Pw) then
365 begin
366 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
367 _lc[I_NET_DISC_PASSWORD]);
368 enet_peer_disconnect(C^.Peer, NET_DISC_PASSWORD);
369 Exit;
370 end;
372 Color.R := R;
373 Color.B := B;
374 Color.G := G;
376 PID := g_Player_Create(Model, Color, T, False);
377 with g_Player_Get(PID) do
378 begin
379 Name := PName;
380 Reset(True);
381 end;
383 C^.Player := PID;
385 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
386 e_WriteLog('NET: Client ' + PName + ' [' + IntToStr(C^.ID) +
387 '] connected. Assigned player #' + IntToStr(PID) + '.', MSG_NOTIFY);
389 MH_SEND_Info(C^.ID);
391 with g_Player_Get(PID) do
392 begin
393 Name := PName;
394 FClientID := C^.ID;
395 // round in progress, don't spawn
396 if (gGameSettings.MaxLives > 0) and (gLMSRespawn = LMS_RESPAWN_NONE) then
397 begin
398 Lives := 0;
399 FNoRespawn := True;
400 Spectate;
401 FWantsInGame := True; // TODO: look into this later
402 end
403 else
404 Respawn(gGameSettings.GameType = GT_SINGLE);
405 end;
407 for I := Low(NetClients) to High(NetClients) do
408 begin
409 if NetClients[I].ID = C^.ID then Continue;
410 MH_SEND_PlayerCreate(PID, NetClients[I].ID);
411 MH_SEND_PlayerPos(True, PID, NetClients[I].ID);
412 MH_SEND_PlayerStats(PID, NetClients[I].ID);
413 end;
415 if gState in [STATE_INTERCUSTOM, STATE_FOLD] then
416 MH_SEND_GameEvent(NET_EV_MAPEND, 0, 'N', C^.ID);
418 if NetUseMaster then g_Net_Slist_Update;
419 end;
421 procedure MH_RECV_FullStateRequest(C: pTNetClient; P: Pointer);
422 begin
423 if gGameOn then
424 MH_SEND_Everything((C^.State = NET_STATE_AUTH), C^.ID)
425 else
426 C^.RequestedFullUpdate := True;
427 end;
429 // PLAYER
431 function MH_RECV_PlayerPos(C: pTNetClient; P: Pointer): Word;
432 var
433 Dir: Byte;
434 PID: Word;
435 kByte: Word;
436 Pl: TPlayer;
437 GT: LongWord;
438 begin
439 Result := 0;
440 if not gGameOn then Exit;
442 GT := e_Raw_Read_LongWord(P);
443 PID := C^.Player;
444 Pl := g_Player_Get(PID);
445 if Pl = nil then
446 Exit;
448 if (GT > gTime + NET_MAX_DIFFTIME) or (GT < Pl.NetTime) then Exit;
450 with Pl do
451 begin
452 NetTime := GT;
453 kByte := e_Raw_Read_Word(P);
454 Dir := e_Raw_Read_Byte(P);
455 if Direction <> TDirection(Dir) then
456 JustTeleported := False;
457 SetDirection(TDirection(Dir));
458 ReleaseKeys;
460 if kByte = NET_KEY_CHAT then
461 begin
462 PressKey(KEY_CHAT, 10000);
463 Exit;
464 end;
466 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
467 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
468 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
469 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
470 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
471 if LongBool(kByte and NET_KEY_FIRE) then PressKey(KEY_FIRE, 10000);
472 if LongBool(kByte and NET_KEY_OPEN) then PressKey(KEY_OPEN, 10000);
473 if LongBool(kByte and NET_KEY_NW) then PressKey(KEY_NEXTWEAPON, 10000);
474 if LongBool(kByte and NET_KEY_PW) then PressKey(KEY_PREVWEAPON, 10000);
475 end;
477 // MH_SEND_PlayerPos(False, PID, C^.ID);
478 end;
480 procedure MH_RECV_CheatRequest(C: pTNetClient; P: Pointer);
481 var
482 CheatKind: Byte;
483 Pl: TPlayer;
484 begin
485 Pl := g_Player_Get(C^.Player);
486 if Pl = nil then Exit;
488 CheatKind := e_Raw_Read_Byte(P);
490 case CheatKind of
491 NET_CHEAT_SUICIDE:
492 Pl.Damage(SUICIDE_DAMAGE, Pl.UID, 0, 0, HIT_SELF);
493 NET_CHEAT_SPECTATE:
494 begin
495 if Pl.FSpectator then
496 Pl.Respawn(False)
497 else
498 Pl.Spectate;
499 end;
500 end;
501 end;
503 procedure MH_RECV_PlayerSettings(C: pTNetClient; P: Pointer);
504 var
505 TmpName: string;
506 TmpModel: string;
507 TmpColor: TRGB;
508 TmpTeam: Byte;
509 Pl: TPlayer;
510 begin
511 TmpName := e_Raw_Read_String(P);
512 TmpModel := e_Raw_Read_String(P);
513 TmpColor.R := e_Raw_Read_Byte(P);
514 TmpColor.G := e_Raw_Read_Byte(P);
515 TmpColor.B := e_Raw_Read_Byte(P);
516 TmpTeam := e_Raw_Read_Byte(P);
518 Pl := g_Player_Get(C^.Player);
519 if Pl = nil then Exit;
521 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
522 Pl.SwitchTeam
523 else
524 Pl.SetColor(TmpColor);
526 if Pl.Name <> TmpName then
527 begin
528 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
529 Pl.Name := TmpName;
530 end;
532 if TmpModel <> Pl.Model.Name then
533 Pl.SetModel(TmpModel);
535 MH_SEND_PlayerSettings(Pl.UID, TmpModel);
536 end;
538 // RCON
540 procedure MH_RECV_RCONPassword(C: pTNetClient; P: Pointer);
541 var
542 Pwd: string;
543 begin
544 Pwd := e_Raw_Read_String(P);
545 if not NetAllowRCON then Exit;
546 if Pwd = NetRCONPassword then
547 begin
548 C^.RCONAuth := True;
549 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWGOOD, 'N', C^.ID);
550 end
551 else
552 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWBAD, 'N', C^.ID);
553 end;
555 procedure MH_RECV_RCONCommand(C: pTNetClient; P: Pointer);
556 var
557 Cmd: string;
558 begin
559 Cmd := e_Raw_Read_String(P);
560 if not NetAllowRCON then Exit;
561 if not C^.RCONAuth then
562 begin
563 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_NOAUTH, 'N', C^.ID);
564 Exit;
565 end;
566 g_Console_Process(Cmd);
567 end;
569 // MISC
571 procedure MH_RECV_Vote(C: pTNetClient; P: Pointer);
572 var
573 Start: Boolean;
574 Name, Command: string;
575 Need: Integer;
576 Pl: TPlayer;
577 begin
578 Start := e_Raw_Read_Byte(P) <> 0;
579 Command := e_Raw_Read_String(P);
581 Pl := g_Player_Get(C^.Player);
582 if Pl = nil then Exit;
583 Name := Pl.Name;
585 if Start then
586 begin
587 if not g_Console_CommandBlacklisted(Command) then
588 g_Game_StartVote(Command, Name);
589 end
590 else if gVoteInProgress then
591 begin
592 if (gPlayer1 <> nil) or (gPlayer2 <> nil) then
593 Need := Floor((NetClientCount+1)/2.0) + 1
594 else
595 Need := Floor(NetClientCount/2.0) + 1;
596 if C^.Voted then
597 begin
598 Dec(gVoteCount);
599 C^.Voted := False;
600 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_REVOKED], [Name, gVoteCount, Need]), True);
601 MH_SEND_VoteEvent(NET_VE_REVOKE, Name, 'a', gVoteCount, Need);
602 end
603 else
604 begin
605 Inc(gVoteCount);
606 C^.Voted := True;
607 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Name, gVoteCount, Need]), True);
608 MH_SEND_VoteEvent(NET_VE_VOTE, Name, 'a', gVoteCount, Need);
609 g_Game_CheckVote;
610 end;
611 end;
612 end;
614 // GAME (SEND)
616 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
617 var
618 I: Integer;
619 begin
620 if gPlayers <> nil then
621 for I := Low(gPlayers) to High(gPlayers) do
622 if gPlayers[I] <> nil then
623 begin
624 if CreatePlayers then MH_SEND_PlayerCreate(gPlayers[I].UID, ID);
625 MH_SEND_PlayerPos(True, gPlayers[I].UID, ID);
626 MH_SEND_PlayerStats(gPlayers[I].UID, ID);
628 if (gPlayers[I].Flag <> FLAG_NONE) and (gGameSettings.GameMode = GM_CTF) then
629 MH_SEND_FlagEvent(FLAG_STATE_CAPTURED, gPlayers[I].Flag, gPlayers[I].UID, True, ID);
630 end;
632 if gItems <> nil then
633 begin
634 for I := High(gItems) downto Low(gItems) do
635 if gItems[I].Live then
636 MH_SEND_ItemSpawn(True, I, ID);
637 end;
639 if gMonsters <> nil then
640 for I := 0 to High(gMonsters) do
641 if gMonsters[I] <> nil then
642 MH_SEND_MonsterSpawn(gMonsters[I].UID, ID);
644 if gWalls <> nil then
645 for I := Low(gWalls) to High(gWalls) do
646 if gWalls[I] <> nil then
647 with gWalls[I] do
648 begin
649 if Door then
650 MH_SEND_PanelState(PanelType, I, ID);
652 if GetTextureCount > 1 then
653 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
654 end;
656 if gLifts <> nil then
657 for I := Low(gLifts) to High(gLifts) do
658 if gLifts[I] <> nil then
659 with gLifts[I] do
660 MH_SEND_PanelState(PanelType, I, ID);
662 if gRenderForegrounds <> nil then
663 for I := Low(gRenderForegrounds) to High(gRenderForegrounds) do
664 if gRenderForegrounds[I] <> nil then
665 with gRenderForegrounds[I] do
666 begin
667 if (GetTextureCount > 1) then
668 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
669 if Moved then
670 MH_SEND_PanelState(PanelType, I, ID);
671 end;
672 if gRenderBackgrounds <> nil then
673 for I := Low(gRenderBackgrounds) to High(gRenderBackgrounds) do
674 if gRenderBackgrounds[I] <> nil then
675 with gRenderBackgrounds[I] do
676 begin
677 if (GetTextureCount > 1) then
678 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
679 if Moved then
680 MH_SEND_PanelState(PanelType, I, ID);
681 end;
682 if gWater <> nil then
683 for I := Low(gWater) to High(gWater) do
684 if gWater[I] <> nil then
685 with gWater[I] do
686 if GetTextureCount > 1 then
687 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
688 if gAcid1 <> nil then
689 for I := Low(gAcid1) to High(gAcid1) do
690 if gAcid1[I] <> nil then
691 with gAcid1[I] do
692 if GetTextureCount > 1 then
693 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
694 if gAcid2 <> nil then
695 for I := Low(gAcid2) to High(gAcid2) do
696 if gAcid2[I] <> nil then
697 with gAcid2[I] do
698 if GetTextureCount > 1 then
699 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
700 if gSteps <> nil then
701 for I := Low(gSteps) to High(gSteps) do
702 if gSteps[I] <> nil then
703 with gSteps[I] do
704 if GetTextureCount > 1 then
705 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
707 if gTriggers <> nil then
708 for I := Low(gTriggers) to High(gTriggers) do
709 if gTriggers[I].TriggerType = TRIGGER_SOUND then
710 MH_SEND_TriggerSound(gTriggers[I], ID);
712 if Shots <> nil then
713 for I := Low(Shots) to High(Shots) do
714 if Shots[i].ShotType in [6, 7, 8] then
715 MH_SEND_CreateShot(i, ID);
717 MH_SEND_TriggerMusic(ID);
719 MH_SEND_GameStats(ID);
720 MH_SEND_CoopStats(ID);
722 if gGameSettings.GameMode = GM_CTF then
723 begin
724 if gFlags[FLAG_RED].State <> FLAG_STATE_CAPTURED then
725 MH_SEND_FlagEvent(gFlags[FLAG_RED].State, FLAG_RED, 0, True, ID);
726 if gFlags[FLAG_BLUE].State <> FLAG_STATE_CAPTURED then
727 MH_SEND_FlagEvent(gFlags[FLAG_BLUE].State, FLAG_BLUE, 0, True, ID);
728 end;
730 if CreatePlayers and (ID >= 0) then NetClients[ID].State := NET_STATE_GAME;
732 if gLMSRespawn > LMS_RESPAWN_NONE then
733 MH_SEND_GameEvent(NET_EV_LMS_WARMUP, (gLMSRespawnTime - gTime) div 1000, 'N', ID);
734 end;
736 procedure MH_SEND_Info(ID: Byte);
737 var
738 Map: string;
739 begin
740 Map := g_ExtractFileName(gMapInfo.Map);
742 e_Buffer_Clear(@NetOut);
744 e_Buffer_Write(@NetOut, Byte(NET_MSG_INFO));
745 e_Buffer_Write(@NetOut, ID);
746 e_Buffer_Write(@NetOut, NetClients[ID].Player);
747 e_Buffer_Write(@NetOut, gGameSettings.WAD);
748 e_Buffer_Write(@NetOut, Map);
749 e_Buffer_Write(@NetOut, gWADHash);
750 e_Buffer_Write(@NetOut, gGameSettings.GameMode);
751 e_Buffer_Write(@NetOut, gGameSettings.GoalLimit);
752 e_Buffer_Write(@NetOut, gGameSettings.TimeLimit);
753 e_Buffer_Write(@NetOut, gGameSettings.MaxLives);
754 e_Buffer_Write(@NetOut, gGameSettings.Options);
755 e_Buffer_Write(@NetOut, gTime);
757 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
758 end;
760 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
761 var
762 Name: string;
763 i: Integer;
764 Team: Byte;
765 begin
766 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
767 Mode := NET_CHAT_PLAYER;
769 Team := 0;
770 if (Mode = NET_CHAT_TEAM) then
771 begin
772 for i := Low(gPlayers) to High(gPlayers) do
773 if (gPlayers[i] <> nil) and (gPlayers[i].FClientID >= 0) and
774 (gPlayers[i].Team = ID) then
775 begin
776 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHAT));
777 e_Buffer_Write(@NetOut, Txt);
778 e_Buffer_Write(@NetOut, Mode);
779 g_Net_Host_Send(gPlayers[i].FClientID, True, NET_CHAN_CHAT);
780 end;
781 Team := ID;
782 ID := NET_EVERYONE;
783 end
784 else
785 begin
786 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHAT));
787 e_Buffer_Write(@NetOut, Txt);
788 e_Buffer_Write(@NetOut, Mode);
789 g_Net_Host_Send(ID, True, NET_CHAN_CHAT);
790 end;
792 if Mode = NET_CHAT_SYSTEM then
793 Exit;
795 if ID = NET_EVERYONE then
796 begin
797 if Mode = NET_CHAT_PLAYER then
798 begin
799 g_Console_Add(Txt, True);
800 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
801 g_Sound_PlayEx('SOUND_GAME_RADIO');
802 end
803 else
804 if Mode = NET_CHAT_TEAM then
805 if gPlayer1 <> nil then
806 begin
807 if (gPlayer1.Team = TEAM_RED) and (Team = TEAM_RED) then
808 begin
809 g_Console_Add(#18'[Team] '#2 + Txt, True);
810 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
811 g_Sound_PlayEx('SOUND_GAME_RADIO');
812 end
813 else if (gPlayer1.Team = TEAM_BLUE) and (Team = TEAM_BLUE) then
814 begin
815 g_Console_Add(#20'[Team] '#2 + Txt, True);
816 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
817 g_Sound_PlayEx('SOUND_GAME_RADIO');
818 end;
819 end;
820 end
821 else
822 begin
823 Name := g_Net_ClientName_ByID(ID);
824 g_Console_Add('-> ' + Name + ': ' + Txt, True);
825 e_WriteLog('[Tell ' + Name + '] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
826 g_Sound_PlayEx('SOUND_GAME_RADIO');
827 end;
828 end;
830 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
831 begin
832 e_Buffer_Write(@NetOut, Byte(NET_MSG_GFX));
833 e_Buffer_Write(@NetOut, Kind);
834 e_Buffer_Write(@NetOut, X);
835 e_Buffer_Write(@NetOut, Y);
836 e_Buffer_Write(@NetOut, Ang);
838 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
839 end;
841 procedure MH_SEND_Sound(X, Y: Integer; Name: string; ID: Integer = NET_EVERYONE);
842 begin
843 e_Buffer_Write(@NetOut, Byte(NET_MSG_SND));
844 e_Buffer_Write(@NetOut, Name);
845 e_Buffer_Write(@NetOut, X);
846 e_Buffer_Write(@NetOut, Y);
848 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
849 end;
851 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
852 begin
853 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
855 e_Buffer_Write(@NetOut, Byte(NET_MSG_SHADD));
856 e_Buffer_Write(@NetOut, Proj);
857 e_Buffer_Write(@NetOut, Shots[Proj].ShotType);
858 e_Buffer_Write(@NetOut, Shots[Proj].Target);
859 e_Buffer_Write(@NetOut, Shots[Proj].SpawnerUID);
860 e_Buffer_Write(@NetOut, Shots[Proj].Timeout);
861 e_Buffer_Write(@NetOut, Shots[Proj].Obj.X);
862 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Y);
863 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.X);
864 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.Y);
866 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
867 end;
869 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
870 begin
871 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
873 e_Buffer_Write(@NetOut, Byte(NET_MSG_SHPOS));
874 e_Buffer_Write(@NetOut, Proj);
875 e_Buffer_Write(@NetOut, Shots[Proj].Obj.X);
876 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Y);
877 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.X);
878 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.Y);
880 g_Net_Host_Send(ID, False, NET_CHAN_SHOTS);
881 end;
883 procedure MH_Send_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
884 begin
885 e_Buffer_Write(@NetOut, Byte(NET_MSG_SHDEL));
886 e_Buffer_Write(@NetOut, Proj);
887 e_Buffer_Write(@NetOut, Byte(Loud));
888 e_Buffer_Write(@NetOut, X);
889 e_Buffer_Write(@NetOut, Y);
891 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
892 end;
894 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
895 begin
896 e_Buffer_Write(@NetOut, Byte(NET_MSG_SCORE));
897 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
898 begin
899 e_Buffer_Write(@NetOut, gTeamStat[TEAM_RED].Goals);
900 e_Buffer_Write(@NetOut, gTeamStat[TEAM_BLUE].Goals);
901 end
902 else
903 if gGameSettings.GameMode = GM_COOP then
904 begin
905 e_Buffer_Write(@NetOut, gCoopMonstersKilled);
906 e_Buffer_Write(@NetOut, gCoopSecretsFound);
907 end;
909 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
910 end;
912 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
913 begin
914 e_Buffer_Write(@NetOut, Byte(NET_MSG_COOP));
915 e_Buffer_Write(@NetOut, gTotalMonsters);
916 e_Buffer_Write(@NetOut, gSecretsCount);
917 e_Buffer_Write(@NetOut, gCoopTotalMonstersKilled);
918 e_Buffer_Write(@NetOut, gCoopTotalSecretsFound);
919 e_Buffer_Write(@NetOut, gCoopTotalMonsters);
920 e_Buffer_Write(@NetOut, gCoopTotalSecrets);
921 end;
923 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
924 begin
925 e_Buffer_Write(@NetOut, Byte(NET_MSG_GEVENT));
926 e_Buffer_Write(@NetOut, EvType);
927 e_Buffer_Write(@NetOut, EvNum);
928 e_Buffer_Write(@NetOut, EvStr);
929 e_Buffer_Write(@NetOut, Byte(gLastMap));
930 e_Buffer_Write(@NetOut, gTime);
931 if (EvType = NET_EV_MAPSTART) and (Pos(':\', EvStr) > 0) then
932 begin
933 e_Buffer_Write(@NetOut, Byte(1));
934 e_Buffer_Write(@NetOut, gWADHash);
935 end else
936 e_Buffer_Write(@NetOut, Byte(0));
938 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
939 end;
941 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
942 begin
943 e_Buffer_Write(@NetOut, Byte(NET_MSG_FLAG));
944 e_Buffer_Write(@NetOut, EvType);
945 e_Buffer_Write(@NetOut, Flag);
946 e_Buffer_Write(@NetOut, Byte(Quiet));
947 e_Buffer_Write(@NetOut, PID);
948 e_Buffer_Write(@NetOut, gFlags[Flag].State);
949 e_Buffer_Write(@NetOut, gFlags[Flag].CaptureTime);
950 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.X);
951 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.Y);
952 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.Vel.X);
953 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.Vel.Y);
955 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
956 end;
958 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
959 begin
960 e_Buffer_Write(@NetOut, Byte(NET_MSG_GSET));
961 e_Buffer_Write(@NetOut, gGameSettings.GameMode);
962 e_Buffer_Write(@NetOut, gGameSettings.GoalLimit);
963 e_Buffer_Write(@NetOut, gGameSettings.TimeLimit);
964 e_Buffer_Write(@NetOut, gGameSettings.MaxLives);
965 e_Buffer_Write(@NetOut, gGameSettings.Options);
967 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
968 end;
970 // PLAYER (SEND)
972 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
973 var
974 P: TPlayer;
975 begin
976 P := g_Player_Get(PID);
977 if P = nil then Exit;
979 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLR));
980 e_Buffer_Write(@NetOut, PID);
981 e_Buffer_Write(@NetOut, P.Name);
983 e_Buffer_Write(@NetOut, P.FActualModelName);
984 e_Buffer_Write(@NetOut, P.FColor.R);
985 e_Buffer_Write(@NetOut, P.FColor.G);
986 e_Buffer_Write(@NetOut, P.FColor.B);
987 e_Buffer_Write(@NetOut, P.Team);
989 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT)
990 end;
992 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
993 var
994 kByte: Word;
995 Pl: TPlayer;
996 begin
997 Pl := g_Player_Get(PID);
998 if Pl = nil then Exit;
999 if Pl.FDummy then Exit;
1001 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRPOS));
1002 e_Buffer_Write(@NetOut, gTime);
1003 e_Buffer_Write(@NetOut, PID);
1005 kByte := 0;
1007 with Pl do
1008 begin
1009 e_Buffer_Write(@NetOut, FPing);
1010 e_Buffer_Write(@NetOut, FLoss);
1011 if IsKeyPressed(KEY_CHAT) then
1012 kByte := NET_KEY_CHAT
1013 else
1014 begin
1015 if IsKeyPressed(KEY_LEFT) then kByte := kByte or NET_KEY_LEFT;
1016 if IsKeyPressed(KEY_RIGHT) then kByte := kByte or NET_KEY_RIGHT;
1017 if IsKeyPressed(KEY_UP) then kByte := kByte or NET_KEY_UP;
1018 if IsKeyPressed(KEY_DOWN) then kByte := kByte or NET_KEY_DOWN;
1019 if IsKeyPressed(KEY_JUMP) then kByte := kByte or NET_KEY_JUMP;
1020 if JustTeleported then kByte := kByte or NET_KEY_FORCEDIR;
1021 end;
1023 e_Buffer_Write(@NetOut, kByte);
1024 if Direction = D_LEFT then e_Buffer_Write(@NetOut, Byte(0)) else e_Buffer_Write(@NetOut, Byte(1));
1025 e_Buffer_Write(@NetOut, GameX);
1026 e_Buffer_Write(@NetOut, GameY);
1027 e_Buffer_Write(@NetOut, GameVelX);
1028 e_Buffer_Write(@NetOut, GameVelY);
1029 e_Buffer_Write(@NetOut, GameAccelX);
1030 e_Buffer_Write(@NetOut, GameAccelY);
1031 end;
1033 g_Net_Host_Send(ID, Reliable, NET_CHAN_PLAYERPOS);
1034 end;
1036 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
1037 var
1038 P: TPlayer;
1039 I: Integer;
1040 begin
1041 P := g_Player_Get(PID);
1042 if P = nil then Exit;
1044 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRSTA));
1045 e_Buffer_Write(@NetOut, PID);
1047 with P do
1048 begin
1049 e_Buffer_Write(@NetOut, Byte(Live));
1050 e_Buffer_Write(@NetOut, Byte(GodMode));
1051 e_Buffer_Write(@NetOut, Health);
1052 e_Buffer_Write(@NetOut, Armor);
1053 e_Buffer_Write(@NetOut, Air);
1054 e_Buffer_Write(@NetOut, JetFuel);
1055 e_Buffer_Write(@NetOut, Lives);
1056 e_Buffer_Write(@NetOut, Team);
1058 for I := WEAPON_KASTET to WEAPON_SUPERPULEMET do
1059 e_Buffer_Write(@NetOut, Byte(FWeapon[I]));
1061 for I := A_BULLETS to A_CELLS do
1062 e_Buffer_Write(@NetOut, FAmmo[I]);
1064 for I := A_BULLETS to A_CELLS do
1065 e_Buffer_Write(@NetOut, FMaxAmmo[I]);
1067 for I := MR_SUIT to MR_MAX do
1068 e_Buffer_Write(@NetOut, LongWord(FMegaRulez[I]));
1070 e_Buffer_Write(@NetOut, Byte(R_ITEM_BACKPACK in FRulez));
1071 e_Buffer_Write(@NetOut, Byte(R_KEY_RED in FRulez));
1072 e_Buffer_Write(@NetOut, Byte(R_KEY_GREEN in FRulez));
1073 e_Buffer_Write(@NetOut, Byte(R_KEY_BLUE in FRulez));
1074 e_Buffer_Write(@NetOut, Byte(R_BERSERK in FRulez));
1076 e_Buffer_Write(@NetOut, Frags);
1077 e_Buffer_Write(@NetOut, Death);
1079 e_Buffer_Write(@NetOut, CurrWeap);
1081 e_Buffer_Write(@NetOut, Byte(FSpectator));
1082 e_Buffer_Write(@NetOut, Byte(FGhost));
1083 e_Buffer_Write(@NetOut, Byte(FPhysics));
1084 e_Buffer_Write(@NetOut, Byte(FNoRespawn));
1085 e_Buffer_Write(@NetOut, Byte(FJetpack));
1086 end;
1088 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1089 end;
1091 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
1092 begin
1093 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRDMG));
1094 e_Buffer_Write(@NetOut, PID);
1095 e_Buffer_Write(@NetOut, Kind);
1096 e_Buffer_Write(@NetOut, Attacker);
1097 e_Buffer_Write(@NetOut, Value);
1098 e_Buffer_Write(@NetOut, VX);
1099 e_Buffer_Write(@NetOut, VY);
1101 g_Net_Host_Send(ID, False, NET_CHAN_PLAYER);
1102 end;
1104 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
1105 begin
1106 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRDIE));
1107 e_Buffer_Write(@NetOut, PID);
1108 e_Buffer_Write(@NetOut, KillType);
1109 e_Buffer_Write(@NetOut, DeathType);
1110 e_Buffer_Write(@NetOut, Attacker);
1112 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1113 end;
1115 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
1116 begin
1117 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRFIRE));
1118 e_Buffer_Write(@NetOut, PID);
1119 e_Buffer_Write(@NetOut, Weapon);
1120 e_Buffer_Write(@NetOut, X);
1121 e_Buffer_Write(@NetOut, Y);
1122 e_Buffer_Write(@NetOut, AX);
1123 e_Buffer_Write(@NetOut, AY);
1124 e_Buffer_Write(@NetOut, ShotID);
1126 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
1127 end;
1129 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
1130 begin
1131 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRDEL));
1132 e_Buffer_Write(@NetOut, PID);
1134 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1135 end;
1137 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
1138 var
1139 Pl: TPlayer;
1140 begin
1141 Pl := g_Player_Get(PID);
1142 if Pl = nil then Exit;
1144 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRSET));
1145 e_Buffer_Write(@NetOut, PID);
1146 e_Buffer_Write(@NetOut, Pl.Name);
1147 if Mdl = '' then
1148 e_Buffer_Write(@NetOut, Pl.Model.Name)
1149 else
1150 e_Buffer_Write(@NetOut, Mdl);
1151 e_Buffer_Write(@NetOut, Pl.FColor.R);
1152 e_Buffer_Write(@NetOut, Pl.FColor.G);
1153 e_Buffer_Write(@NetOut, Pl.FColor.B);
1154 e_Buffer_Write(@NetOut, Pl.Team);
1156 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1157 end;
1159 // ITEM (SEND)
1161 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1162 begin
1163 e_Buffer_Write(@NetOut, Byte(NET_MSG_ISPAWN));
1164 e_Buffer_Write(@NetOut, IID);
1165 e_Buffer_Write(@NetOut, Byte(Quiet));
1166 e_Buffer_Write(@NetOut, gItems[IID].ItemType);
1167 e_Buffer_Write(@NetOut, Byte(gItems[IID].Fall));
1168 e_Buffer_Write(@NetOut, Byte(gItems[IID].Respawnable));
1169 e_Buffer_Write(@NetOut, gItems[IID].Obj.X);
1170 e_Buffer_Write(@NetOut, gItems[IID].Obj.Y);
1171 e_Buffer_Write(@NetOut, gItems[IID].Obj.Vel.X);
1172 e_Buffer_Write(@NetOut, gItems[IID].Obj.Vel.Y);
1174 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1175 end;
1177 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1178 begin
1179 e_Buffer_Write(@NetOut, Byte(NET_MSG_IDEL));
1180 e_Buffer_Write(@NetOut, IID);
1181 e_Buffer_Write(@NetOut, Byte(Quiet));
1183 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1184 end;
1186 // PANEL
1188 procedure MH_SEND_PanelTexture(PType: Word; PID: LongWord; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
1189 var
1190 TP: TPanel;
1191 begin
1192 case PType of
1193 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
1194 TP := gWalls[PID];
1195 PANEL_FORE:
1196 TP := gRenderForegrounds[PID];
1197 PANEL_BACK:
1198 TP := gRenderBackgrounds[PID];
1199 PANEL_WATER:
1200 TP := gWater[PID];
1201 PANEL_ACID1:
1202 TP := gAcid1[PID];
1203 PANEL_ACID2:
1204 TP := gAcid2[PID];
1205 PANEL_STEP:
1206 TP := gSteps[PID];
1207 else
1208 Exit;
1209 end;
1211 with TP do
1212 begin
1213 e_Buffer_Write(@NetOut, Byte(NET_MSG_PTEX));
1214 e_Buffer_Write(@NetOut, PType);
1215 e_Buffer_Write(@NetOut, PID);
1216 e_Buffer_Write(@NetOut, FCurTexture);
1217 e_Buffer_Write(@NetOut, FCurFrame);
1218 e_Buffer_Write(@NetOut, FCurFrameCount);
1219 e_Buffer_Write(@NetOut, AnimLoop);
1220 end;
1222 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1223 end;
1225 procedure MH_SEND_PanelState(PType: Word; PID: LongWord; ID: Integer = NET_EVERYONE);
1226 var
1227 TP: TPanel;
1228 begin
1229 case PType of
1230 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
1231 TP := gWalls[PID];
1232 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
1233 TP := gLifts[PID];
1234 PANEL_BACK:
1235 begin
1236 TP := gRenderBackgrounds[PID];
1237 TP.Moved := True;
1238 end;
1239 PANEL_FORE:
1240 begin
1241 TP := gRenderForegrounds[PID];
1242 TP.Moved := True;
1243 end;
1244 else
1245 Exit;
1246 end;
1248 e_Buffer_Write(@NetOut, Byte(NET_MSG_PSTATE));
1249 e_Buffer_Write(@NetOut, PType);
1250 e_Buffer_Write(@NetOut, PID);
1251 e_Buffer_Write(@NetOut, Byte(TP.Enabled));
1252 e_Buffer_Write(@NetOut, TP.LiftType);
1253 e_Buffer_Write(@NetOut, TP.X);
1254 e_Buffer_Write(@NetOut, TP.Y);
1256 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1257 end;
1259 // TRIGGER
1261 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
1262 begin
1263 if gTriggers = nil then Exit;
1264 if T.Sound = nil then Exit;
1266 e_Buffer_Write(@NetOut, Byte(NET_MSG_TSOUND));
1267 e_Buffer_Write(@NetOut, T.ClientID);
1268 e_Buffer_Write(@NetOut, Byte(T.Sound.IsPlaying));
1269 e_Buffer_Write(@NetOut, LongWord(T.Sound.GetPosition));
1270 e_Buffer_Write(@NetOut, T.SoundPlayCount);
1272 g_Net_Host_Send(ID, True);
1273 end;
1275 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
1276 begin
1277 e_Buffer_Write(@NetOut, Byte(NET_MSG_TMUSIC));
1278 e_Buffer_Write(@NetOut, gMusic.Name);
1279 e_Buffer_Write(@NetOut, Byte(gMusic.IsPlaying));
1280 e_Buffer_Write(@NetOut, LongWord(gMusic.GetPosition));
1281 e_Buffer_Write(@NetOut, Byte(gMusic.SpecPause or gMusic.IsPaused));
1283 g_Net_Host_Send(ID, True);
1284 end;
1286 // MONSTER
1288 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
1289 var
1290 M: TMonster;
1291 begin
1292 M := g_Monsters_Get(UID);
1293 if M = nil then
1294 Exit;
1296 with M do
1297 begin
1298 e_Buffer_Write(@NetOut, Byte(NET_MSG_MSPAWN));
1299 e_Buffer_Write(@NetOut, UID);
1300 e_Buffer_Write(@NetOut, MonsterType);
1301 e_Buffer_Write(@NetOut, MonsterState);
1302 e_Buffer_Write(@NetOut, MonsterAnim);
1303 e_Buffer_Write(@NetOut, MonsterTargetUID);
1304 e_Buffer_Write(@NetOut, MonsterTargetTime);
1305 e_Buffer_Write(@NetOut, MonsterBehaviour);
1306 e_Buffer_Write(@NetOut, MonsterSleep);
1307 e_Buffer_Write(@NetOut, MonsterHealth);
1308 e_Buffer_Write(@NetOut, MonsterAmmo);
1309 e_Buffer_Write(@NetOut, GameX);
1310 e_Buffer_Write(@NetOut, GameY);
1311 e_Buffer_Write(@NetOut, GameVelX);
1312 e_Buffer_Write(@NetOut, GameVelY);
1313 e_Buffer_Write(@NetOut, Byte(GameDirection));
1314 end;
1316 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1317 end;
1319 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
1320 var
1321 M: TMonster;
1322 begin
1323 M := g_Monsters_Get(UID);
1324 if M = nil then Exit;
1326 e_Buffer_Write(@NetOut, Byte(NET_MSG_MPOS));
1327 e_Buffer_Write(@NetOut, UID);
1329 with M do
1330 begin
1331 e_Buffer_Write(@NetOut, GameX);
1332 e_Buffer_Write(@NetOut, GameY);
1333 e_Buffer_Write(@NetOut, GameVelX);
1334 e_Buffer_Write(@NetOut, GameVelY);
1335 e_Buffer_Write(@NetOut, Byte(GameDirection));
1336 end;
1338 g_Net_Host_Send(ID, False, NET_CHAN_MONSTERPOS);
1339 end;
1341 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
1342 var
1343 M: TMonster;
1344 begin
1345 M := g_Monsters_Get(UID);
1346 if M = nil then Exit;
1348 e_Buffer_Write(@NetOut, Byte(NET_MSG_MSTATE));
1349 e_Buffer_Write(@NetOut, UID);
1351 with M do
1352 begin
1353 e_Buffer_Write(@NetOut, MonsterState);
1354 e_Buffer_Write(@NetOut, ForcedAnim);
1355 e_Buffer_Write(@NetOut, MonsterTargetUID);
1356 e_Buffer_Write(@NetOut, MonsterTargetTime);
1357 e_Buffer_Write(@NetOut, MonsterSleep);
1358 e_Buffer_Write(@NetOut, MonsterHealth);
1359 e_Buffer_Write(@NetOut, MonsterAmmo);
1360 e_Buffer_Write(@NetOut, MonsterPain);
1361 e_Buffer_Write(@NetOut, Byte(AnimIsReverse));
1362 end;
1364 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1365 end;
1367 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
1368 begin
1369 e_Buffer_Write(@NetOut, Byte(NET_MSG_MSHOT));
1370 e_Buffer_Write(@NetOut, UID);
1371 e_Buffer_Write(@NetOut, X);
1372 e_Buffer_Write(@NetOut, Y);
1373 e_Buffer_Write(@NetOut, VX);
1374 e_Buffer_Write(@NetOut, VY);
1376 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1377 end;
1379 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
1380 var
1381 M: TMonster;
1382 begin
1383 M := g_Monsters_Get(UID);
1384 if M = nil then Exit;
1386 e_Buffer_Write(@NetOut, Byte(NET_MSG_MDEL));
1387 e_Buffer_Write(@NetOut, UID);
1389 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1390 end;
1392 // MISC
1394 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
1395 begin
1396 e_Buffer_Write(@NetOut, Byte(NET_MSG_TIME_SYNC));
1397 e_Buffer_Write(@NetOut, Time);
1399 g_Net_Host_Send(ID, False, NET_CHAN_SERVICE);
1400 end;
1402 procedure MH_SEND_VoteEvent(EvType: Byte;
1403 StrArg1: string = 'a'; StrArg2: string = 'b';
1404 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
1405 ID: Integer = NET_EVERYONE);
1406 begin
1407 e_Buffer_Write(@NetOut, Byte(NET_MSG_VOTE_EVENT));
1408 e_Buffer_Write(@NetOut, EvType);
1409 e_Buffer_Write(@NetOut, IntArg1);
1410 e_Buffer_Write(@NetOut, IntArg2);
1411 e_Buffer_Write(@NetOut, StrArg1);
1412 e_Buffer_Write(@NetOut, StrArg2);
1414 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1415 end;
1417 // CLIENT MESSAGES //
1419 // GAME
1421 procedure MC_RECV_Chat(P: Pointer);
1422 var
1423 Txt: string;
1424 Mode: Byte;
1425 begin
1426 Txt := e_Raw_Read_String(P);
1427 Mode := e_Raw_Read_Byte(P);
1429 if Mode <> NET_CHAT_SYSTEM then
1430 begin
1431 if Mode = NET_CHAT_PLAYER then
1432 begin
1433 g_Console_Add(Txt, True);
1434 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
1435 g_Sound_PlayEx('SOUND_GAME_RADIO');
1436 end else
1437 if (Mode = NET_CHAT_TEAM) and (gPlayer1 <> nil) then
1438 begin
1439 if gPlayer1.Team = TEAM_RED then
1440 g_Console_Add(b_Text_Format('\r[Team] ') + Txt, True);
1441 if gPlayer1.Team = TEAM_BLUE then
1442 g_Console_Add(b_Text_Format('\b[Team] ') + Txt, True);
1443 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
1444 g_Sound_PlayEx('SOUND_GAME_RADIO');
1445 end;
1446 end else
1447 g_Console_Add(Txt, True);
1448 end;
1450 procedure MC_RECV_Effect(P: Pointer);
1451 var
1452 Kind: Byte;
1453 X, Y: Integer;
1454 Ang: SmallInt;
1455 Anim: TAnimation;
1456 ID: LongWord;
1457 begin
1458 if not gGameOn then Exit;
1459 Kind := e_Raw_Read_Byte(P);
1460 X := e_Raw_Read_LongInt(P);
1461 Y := e_Raw_Read_LongInt(P);
1462 Ang := e_Raw_Read_SmallInt(P);
1464 case Kind of
1465 NET_GFX_SPARK:
1466 g_GFX_Spark(X, Y, 2 + Random(2), Ang, 0, 0);
1468 NET_GFX_TELE:
1469 begin
1470 if g_Frames_Get(ID, 'FRAMES_TELEPORT') then
1471 begin
1472 Anim := TAnimation.Create(ID, False, 3);
1473 g_GFX_OnceAnim(X, Y, Anim);
1474 Anim.Free();
1475 end;
1476 if Ang = 1 then
1477 g_Sound_PlayExAt('SOUND_GAME_TELEPORT', X, Y);
1478 end;
1480 NET_GFX_EXPLODE:
1481 begin
1482 if g_Frames_Get(ID, 'FRAMES_EXPLODE_ROCKET') then
1483 begin
1484 Anim := TAnimation.Create(ID, False, 6);
1485 Anim.Blending := False;
1486 g_GFX_OnceAnim(X-64, Y-64, Anim);
1487 Anim.Free();
1488 end;
1489 if Ang = 1 then
1490 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEROCKET', X, Y);
1491 end;
1493 NET_GFX_BFGEXPL:
1494 begin
1495 if g_Frames_Get(ID, 'FRAMES_EXPLODE_BFG') then
1496 begin
1497 Anim := TAnimation.Create(ID, False, 6);
1498 Anim.Blending := False;
1499 g_GFX_OnceAnim(X-64, Y-64, Anim);
1500 Anim.Free();
1501 end;
1502 if Ang = 1 then
1503 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEBFG', X, Y);
1504 end;
1506 NET_GFX_BFGHIT:
1507 begin
1508 if g_Frames_Get(ID, 'FRAMES_BFGHIT') then
1509 begin
1510 Anim := TAnimation.Create(ID, False, 4);
1511 g_GFX_OnceAnim(X-32, Y-32, Anim);
1512 Anim.Free();
1513 end;
1514 end;
1516 NET_GFX_FIRE:
1517 begin
1518 if g_Frames_Get(ID, 'FRAMES_FIRE') then
1519 begin
1520 Anim := TAnimation.Create(ID, False, 4);
1521 g_GFX_OnceAnim(X, Y, Anim);
1522 Anim.Free();
1523 end;
1524 if Ang = 1 then
1525 g_Sound_PlayExAt('SOUND_FIRE', X, Y);
1526 end;
1528 NET_GFX_RESPAWN:
1529 begin
1530 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
1531 begin
1532 Anim := TAnimation.Create(ID, False, 4);
1533 g_GFX_OnceAnim(X, Y, Anim);
1534 Anim.Free();
1535 end;
1536 if Ang = 1 then
1537 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
1538 end;
1540 NET_GFX_SHELL1:
1541 g_Player_CreateShell(X, Y, 0, -2, SHELL_BULLET);
1543 NET_GFX_SHELL2:
1544 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1546 NET_GFX_SHELL3:
1547 begin
1548 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1549 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1550 end;
1551 end;
1552 end;
1554 procedure MC_RECV_Sound(P: Pointer);
1555 var
1556 Name: string;
1557 X, Y: Integer;
1558 begin
1559 Name := e_Raw_Read_String(P);
1560 X := e_Raw_Read_LongInt(P);
1561 Y := e_Raw_Read_LongInt(P);
1562 g_Sound_PlayExAt(Name, X, Y);
1563 end;
1565 procedure MC_RECV_CreateShot(P: Pointer);
1566 var
1567 I, X, Y, XV, YV: Integer;
1568 Timeout: LongWord;
1569 Target, Spawner: Word;
1570 ShType: Byte;
1571 begin
1572 I := e_Raw_Read_LongInt(P);
1573 ShType := e_Raw_Read_Byte(P);
1574 Target := e_Raw_Read_Word(P);
1575 Spawner := e_Raw_Read_Word(P);
1576 Timeout := e_Raw_Read_LongWord(P);
1577 X := e_Raw_Read_LongInt(P);
1578 Y := e_Raw_Read_LongInt(P);
1579 XV := e_Raw_Read_LongInt(P);
1580 YV := e_Raw_Read_LongInt(P);
1582 I := g_Weapon_CreateShot(I, ShType, Spawner, Target, X, Y, XV, YV);
1583 if (Shots <> nil) and (I <= High(Shots)) then
1584 begin
1585 Shots[I].Timeout := Timeout;
1586 //Shots[I].Target := Target; // TODO: find a use for Target later
1587 end;
1588 end;
1590 procedure MC_RECV_UpdateShot(P: Pointer);
1591 var
1592 I, TX, TY, TXV, TYV: Integer;
1593 begin
1594 I := e_Raw_Read_LongInt(P);
1595 TX := e_Raw_Read_LongInt(P);
1596 TY := e_Raw_Read_LongInt(P);
1597 TXV := e_Raw_Read_LongInt(P);
1598 TYV := e_Raw_Read_LongInt(P);
1600 if (Shots <> nil) and (I <= High(Shots)) then
1601 with (Shots[i]) do
1602 begin
1603 Obj.X := TX;
1604 Obj.Y := TY;
1605 Obj.Vel.X := TXV;
1606 Obj.Vel.Y := TYV;
1607 end;
1608 end;
1610 procedure MC_RECV_DeleteShot(P: Pointer);
1611 var
1612 I, X, Y: Integer;
1613 L: Boolean;
1614 begin
1615 if not gGameOn then Exit;
1616 I := e_Raw_Read_LongInt(P);
1617 L := (e_Raw_Read_Byte(P) <> 0);
1618 X := e_Raw_Read_LongInt(P);
1619 Y := e_Raw_Read_LongInt(P);
1621 g_Weapon_DestroyShot(I, X, Y, L);
1622 end;
1624 procedure MC_RECV_GameStats(P: Pointer);
1625 begin
1626 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
1627 begin
1628 gTeamStat[TEAM_RED].Goals := e_Raw_Read_SmallInt(P);
1629 gTeamStat[TEAM_BLUE].Goals := e_Raw_Read_SmallInt(P);
1630 end
1631 else
1632 if gGameSettings.GameMode = GM_COOP then
1633 begin
1634 gCoopMonstersKilled := e_Raw_Read_Word(P);
1635 gCoopSecretsFound := e_Raw_Read_Word(P);
1636 end;
1637 end;
1639 procedure MC_RECV_CoopStats(P: Pointer);
1640 begin
1641 gTotalMonsters := e_Raw_Read_LongInt(P);
1642 gSecretsCount := e_Raw_Read_LongInt(P);
1643 gCoopTotalMonstersKilled := e_Raw_Read_Word(P);
1644 gCoopTotalSecretsFound := e_Raw_Read_Word(P);
1645 gCoopTotalMonsters := e_Raw_Read_Word(P);
1646 gCoopTotalSecrets := e_Raw_Read_Word(P);
1647 end;
1649 procedure MC_RECV_GameEvent(P: Pointer);
1650 var
1651 EvType: Byte;
1652 EvNum: Integer;
1653 EvStr: string;
1654 EvTime: LongWord;
1655 BHash: Boolean;
1656 EvHash: TMD5Digest;
1657 pl: TPlayer;
1658 i1, i2: TStrings_Locale;
1659 pln: String;
1660 cnt: Byte;
1661 begin
1662 FillChar(EvHash, Sizeof(EvHash), 0);
1663 EvType := e_Raw_Read_Byte(P);
1664 EvNum := e_Raw_Read_LongInt(P);
1665 EvStr := e_Raw_Read_String(P);
1666 gLastMap := e_Raw_Read_Byte(P) <> 0;
1667 if gLastMap and (gGameSettings.GameMode = GM_COOP) then gStatsOff := True;
1668 gStatsPressed := True;
1669 EvTime := e_Raw_Read_LongWord(P);
1670 BHash := e_Raw_Read_Byte(P) <> 0;
1671 if BHash then
1672 EvHash := e_Raw_Read_MD5(P);
1674 gTime := EvTime;
1676 case EvType of
1677 NET_EV_MAPSTART:
1678 begin
1679 gGameOn := False;
1680 g_Game_ClearLoading();
1681 g_Game_StopAllSounds(True);
1683 gSwitchGameMode := Byte(EvNum);
1684 gGameSettings.GameMode := gSwitchGameMode;
1686 gWADHash := EvHash;
1687 if not g_Game_StartMap(EvStr, True) then
1688 begin
1689 if Pos(':\', EvStr) = 0 then
1690 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [gGameSettings.WAD + ':\' + EvStr]))
1691 else
1692 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [EvStr]));
1693 Exit;
1694 end;
1696 MC_SEND_FullStateRequest;
1697 end;
1699 NET_EV_MAPEND:
1700 begin
1701 gMissionFailed := EvNum <> 0;
1702 gExit := EXIT_ENDLEVELCUSTOM;
1703 end;
1705 NET_EV_RCON:
1706 begin
1707 case EvNum of
1708 NET_RCON_NOAUTH:
1709 g_Console_Add(_lc[I_NET_RCON_NOAUTH], True);
1710 NET_RCON_PWGOOD:
1711 g_Console_Add(_lc[I_NET_RCON_PWD_VALID], True);
1712 NET_RCON_PWBAD:
1713 g_Console_Add(_lc[I_NET_RCON_PWD_INVALID], True);
1714 end;
1715 end;
1717 NET_EV_CHANGE_TEAM:
1718 begin
1719 if EvNum = TEAM_RED then
1720 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [EvStr]), True);
1721 if EvNum = TEAM_BLUE then
1722 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [EvStr]), True);
1723 end;
1725 NET_EV_PLAYER_KICK:
1726 g_Console_Add(Format(_lc[I_PLAYER_KICK], [EvStr]), True);
1728 NET_EV_PLAYER_BAN:
1729 g_Console_Add(Format(_lc[I_PLAYER_BAN], [EvStr]), True);
1731 NET_EV_LMS_WARMUP:
1732 g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [EvNum]), True);
1734 NET_EV_LMS_SURVIVOR:
1735 g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True);
1737 NET_EV_BIGTEXT:
1738 g_Game_Message(AnsiUpperCase(EvStr), Word(EvNum));
1740 NET_EV_SCORE:
1741 begin
1742 pl := g_Player_Get(EvNum and $FFFF);
1743 if pl = nil then
1744 pln := '?'
1745 else
1746 pln := pl.Name;
1747 cnt := (EvNum shr 16) and $FF;
1748 if Pos('w', EvStr) = 0 then
1749 begin
1750 // Default score
1751 if Pos('t', EvStr) = 0 then
1752 begin
1753 // Player +/- score
1754 if Pos('-', EvStr) = 0 then
1755 begin
1756 if Pos('e', EvStr) = 0 then
1757 i1 := I_PLAYER_SCORE_ADD_OWN
1758 else
1759 i1 := I_PLAYER_SCORE_ADD_ENEMY;
1760 end else
1761 begin
1762 if Pos('e', EvStr) = 0 then
1763 i1 := I_PLAYER_SCORE_SUB_OWN
1764 else
1765 i1 := I_PLAYER_SCORE_SUB_ENEMY;
1766 end;
1767 // Which team
1768 if Pos('r', EvStr) > 0 then
1769 i2 := I_PLAYER_SCORE_TO_RED
1770 else
1771 i2 := I_PLAYER_SCORE_TO_BLUE;
1772 g_Console_Add(Format(_lc[i1], [pln, cnt, _lc[i2]]), True);
1773 end else
1774 begin
1775 // Team +/- score
1776 if Pos('-', EvStr) = 0 then
1777 i1 := I_PLAYER_SCORE_ADD_TEAM
1778 else
1779 i1 := I_PLAYER_SCORE_SUB_TEAM;
1780 // Which team
1781 if Pos('r', EvStr) > 0 then
1782 i2 := I_PLAYER_SCORE_RED
1783 else
1784 i2 := I_PLAYER_SCORE_BLUE;
1785 g_Console_Add(Format(_lc[i1], [_lc[i2], cnt]), True);
1786 end;
1787 end else
1788 begin
1789 // Game Win
1790 if Pos('e', EvStr) = 0 then
1791 i1 := I_PLAYER_SCORE_WIN_OWN
1792 else
1793 i1 := I_PLAYER_SCORE_WIN_ENEMY;
1794 // Which team
1795 if Pos('r', EvStr) > 0 then
1796 i2 := I_PLAYER_SCORE_TO_RED
1797 else
1798 i2 := I_PLAYER_SCORE_TO_BLUE;
1799 g_Console_Add(Format(_lc[i1], [pln, _lc[i2]]), True);
1800 end;
1801 end;
1803 NET_EV_SCORE_MSG:
1804 begin
1805 if EvNum = TEAM_RED then
1806 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1807 if EvNum = TEAM_BLUE then
1808 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1809 if EvNum = -TEAM_RED then
1810 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1811 if EvNum = -TEAM_BLUE then
1812 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1813 end;
1815 NET_EV_LMS_START:
1816 begin
1817 g_Player_RemoveAllCorpses;
1818 g_Game_Message(_lc[I_MESSAGE_LMS_START], 144);
1819 end;
1821 NET_EV_LMS_WIN:
1822 g_Game_Message(Format(_lc[I_MESSAGE_LMS_WIN], [AnsiUpperCase(EvStr)]), 144);
1824 NET_EV_TLMS_WIN:
1825 begin
1826 if EvNum = TEAM_RED then
1827 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 144);
1828 if EvNum = TEAM_BLUE then
1829 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 144);
1830 end;
1832 NET_EV_LMS_LOSE:
1833 g_Game_Message(_lc[I_MESSAGE_LMS_LOSE], 144);
1835 NET_EV_LMS_DRAW:
1836 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
1838 NET_EV_KILLCOMBO:
1839 g_Game_Announce_KillCombo(EvNum);
1841 NET_EV_PLAYER_TOUCH:
1842 begin
1843 pl := g_Player_Get(EvNum);
1844 if pl <> nil then
1845 pl.Touch();
1846 end;
1848 end;
1849 end;
1851 procedure MC_RECV_FlagEvent(P: Pointer);
1852 var
1853 PID: Word;
1854 Pl: TPlayer;
1855 EvType: Byte;
1856 Fl: Byte;
1857 Quiet: Boolean;
1858 s, ts: string;
1859 begin
1860 EvType := e_Raw_Read_Byte(P);
1861 Fl := e_Raw_Read_Byte(P);
1863 if Fl = FLAG_NONE then Exit;
1865 Quiet := (e_Raw_Read_Byte(P) <> 0);
1866 PID := e_Raw_Read_Word(P);
1868 gFlags[Fl].State := e_Raw_Read_Byte(P);
1869 gFlags[Fl].CaptureTime := e_Raw_Read_LongWord(P);
1870 gFlags[Fl].Obj.X := e_Raw_Read_LongInt(P);
1871 gFlags[Fl].Obj.Y := e_Raw_Read_LongInt(P);
1872 gFlags[Fl].Obj.Vel.X := e_Raw_Read_LongInt(P);
1873 gFlags[Fl].Obj.Vel.Y := e_Raw_Read_LongInt(P);
1875 Pl := g_Player_Get(PID);
1876 if (Pl = nil) and
1877 (EvType <> FLAG_STATE_NORMAL) and
1878 (EvType <> FLAG_STATE_DROPPED) and
1879 (EvType <> FLAG_STATE_RETURNED) then
1880 Exit;
1882 case EvType of
1883 FLAG_STATE_NORMAL:
1884 begin
1885 if Quiet or (Pl = nil) then Exit;
1887 if Fl = FLAG_RED then
1888 s := _lc[I_PLAYER_FLAG_RED]
1889 else
1890 s := _lc[I_PLAYER_FLAG_BLUE];
1892 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1893 end;
1895 FLAG_STATE_CAPTURED:
1896 begin
1897 if (Pl <> nil) then Pl.SetFlag(Fl);
1899 if Quiet then Exit;
1901 if Fl = FLAG_RED then
1902 s := _lc[I_PLAYER_FLAG_RED]
1903 else
1904 s := _lc[I_PLAYER_FLAG_BLUE];
1906 g_Console_Add(Format(_lc[I_PLAYER_FLAG_GET], [Pl.Name, s]), True);
1907 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_GET], [AnsiUpperCase(s)]), 144);
1908 end;
1910 FLAG_STATE_DROPPED:
1911 begin
1912 if (Pl <> nil) then Pl.SetFlag(FLAG_NONE);
1914 if Quiet or (Pl = nil) then Exit;
1916 if Fl = FLAG_RED then
1917 s := _lc[I_PLAYER_FLAG_RED]
1918 else
1919 s := _lc[I_PLAYER_FLAG_BLUE];
1921 g_Console_Add(Format(_lc[I_PLAYER_FLAG_DROP], [Pl.Name, s]), True);
1922 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_DROP], [AnsiUpperCase(s)]), 144);
1923 end;
1925 FLAG_STATE_SCORED:
1926 begin
1927 g_Map_ResetFlag(FLAG_RED);
1928 g_Map_ResetFlag(FLAG_BLUE);
1929 if Quiet or (Pl = nil) then Exit;
1930 Pl.SetFlag(FLAG_NONE);
1932 if Fl = FLAG_RED then
1933 s := _lc[I_PLAYER_FLAG_RED]
1934 else
1935 s := _lc[I_PLAYER_FLAG_BLUE];
1937 ts := Format('%.4d', [gFlags[Fl].CaptureTime]);
1938 Insert('.', ts, Length(ts) + 1 - 3);
1939 g_Console_Add(Format(_lc[I_PLAYER_FLAG_CAPTURE], [Pl.Name, s, ts]), True);
1940 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_CAPTURE], [AnsiUpperCase(s)]), 144);
1941 end;
1943 FLAG_STATE_RETURNED:
1944 begin
1945 g_Map_ResetFlag(Fl);
1946 if Quiet then Exit;
1948 if Fl = FLAG_RED then
1949 s := _lc[I_PLAYER_FLAG_RED]
1950 else
1951 s := _lc[I_PLAYER_FLAG_BLUE];
1953 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1954 end;
1955 end;
1956 end;
1958 procedure MC_RECV_GameSettings(P: Pointer);
1959 begin
1960 gGameSettings.GameMode := e_Raw_Read_Byte(P);
1961 gGameSettings.GoalLimit := e_Raw_Read_Word(P);
1962 gGameSettings.TimeLimit := e_Raw_Read_Word(P);
1963 gGameSettings.MaxLives := e_Raw_Read_Byte(P);
1964 gGameSettings.Options := e_Raw_Read_LongWord(P);
1965 end;
1967 // PLAYER
1969 function MC_RECV_PlayerCreate(P: Pointer): Word;
1970 var
1971 PID, DID: Word;
1972 PName, Model: string;
1973 Color: TRGB;
1974 T: Byte;
1975 Pl: TPlayer;
1976 begin
1977 PID := e_Raw_Read_Word(P);
1978 Pl := g_Player_Get(PID);
1980 PName := e_Raw_Read_String(P);
1981 Model := e_Raw_Read_String(P);
1982 Color.R := e_Raw_Read_Byte(P);
1983 Color.G := e_Raw_Read_Byte(P);
1984 Color.B := e_Raw_Read_Byte(P);
1985 T := e_Raw_Read_Byte(P);
1987 Result := 0;
1988 if (PID <> NetPlrUID1) and (PID <> NetPlrUID2) then
1989 begin
1990 if (Pl <> nil) then Exit;
1991 DID := g_Player_Create(Model, Color, T, False);
1992 with g_Player_Get(DID) do
1993 begin
1994 UID := PID;
1995 Name := PName;
1996 Reset(True);
1997 end;
1998 end
1999 else
2000 begin
2001 if (PID = NetPlrUID1) and (gPlayer1 <> nil) then begin
2002 gPlayer1.UID := PID;
2003 gPlayer1.Model.SetColor(Color.R, Color.G, Color.B);
2004 gPlayer1.ChangeTeam(T);
2005 end;
2006 if (PID = NetPlrUID2) and (gPlayer2 <> nil) then begin
2007 gPlayer2.UID := PID;
2008 gPlayer2.Model.SetColor(Color.R, Color.G, Color.B);
2009 gPlayer2.ChangeTeam(T);
2010 end;
2011 end;
2013 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
2014 e_WriteLog('NET: Player ' + PName + ' [' + IntToStr(PID) + '] added.', MSG_NOTIFY);
2015 Result := PID;
2016 end;
2018 function MC_RECV_PlayerPos(P: Pointer): Word;
2019 var
2020 GT: LongWord;
2021 PID: Word;
2022 kByte: Word;
2023 Pl: TPlayer;
2024 Dir: Byte;
2025 TmpX, TmpY: Integer;
2026 begin
2027 Result := 0;
2029 GT := e_Raw_Read_LongWord(P);
2030 if GT < gTime - NET_MAX_DIFFTIME then
2031 begin
2032 gTime := GT;
2033 Exit;
2034 end;
2035 gTime := GT;
2037 PID := e_Raw_Read_Word(P);
2038 Pl := g_Player_Get(PID);
2040 if Pl = nil then Exit;
2042 Result := PID;
2044 with Pl do
2045 begin
2046 FPing := e_Raw_Read_Word(P);
2047 FLoss := e_Raw_Read_Byte(P);
2048 kByte := e_Raw_Read_Word(P);
2049 Dir := e_Raw_Read_Byte(P);
2051 TmpX := e_Raw_Read_LongInt(P);
2052 TmpY := e_Raw_Read_LongInt(P);
2054 ReleaseKeys;
2056 if (kByte = NET_KEY_CHAT) then
2057 PressKey(KEY_CHAT, 10000)
2058 else
2059 begin
2060 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
2061 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
2062 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
2063 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
2064 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
2065 end;
2067 if ((Pl <> gPlayer1) and (Pl <> gPlayer2)) or LongBool(kByte and NET_KEY_FORCEDIR) then
2068 SetDirection(TDirection(Dir));
2070 GameVelX := e_Raw_Read_LongInt(P);
2071 GameVelY := e_Raw_Read_LongInt(P);
2072 GameAccelX := e_Raw_Read_LongInt(P);
2073 GameAccelY := e_Raw_Read_LongInt(P);
2074 SetLerp(TmpX, TmpY);
2075 if NetForcePlayerUpdate then Update();
2076 end;
2077 end;
2079 function MC_RECV_PlayerStats(P: Pointer): Word;
2080 var
2081 PID: Word;
2082 Pl: TPlayer;
2083 I: Integer;
2084 OldJet: Boolean;
2085 NewTeam: Byte;
2086 begin
2087 PID := e_Raw_Read_Word(P);
2088 Pl := g_Player_Get(PID);
2089 Result := 0;
2090 if Pl = nil then
2091 Exit;
2093 with Pl do
2094 begin
2095 Live := (e_Raw_Read_Byte(P) <> 0);
2096 GodMode := (e_Raw_Read_Byte(P) <> 0);
2097 Health := e_Raw_Read_LongInt(P);
2098 Armor := e_Raw_Read_LongInt(P);
2099 Air := e_Raw_Read_LongInt(P);
2100 JetFuel := e_Raw_Read_LongInt(P);
2101 Lives := e_Raw_Read_Byte(P);
2102 NewTeam := e_Raw_Read_Byte(P);
2104 for I := WEAPON_KASTET to WEAPON_SUPERPULEMET do
2105 FWeapon[I] := (e_Raw_Read_Byte(P) <> 0);
2107 for I := A_BULLETS to A_CELLS do
2108 FAmmo[I] := e_Raw_Read_Word(P);
2110 for I := A_BULLETS to A_CELLS do
2111 FMaxAmmo[I] := e_Raw_Read_Word(P);
2113 for I := MR_SUIT to MR_MAX do
2114 FMegaRulez[I] := e_Raw_Read_LongWord(P);
2116 FRulez := [];
2117 if (e_Raw_Read_Byte(P) <> 0) then
2118 FRulez := FRulez + [R_ITEM_BACKPACK];
2119 if (e_Raw_Read_Byte(P) <> 0) then
2120 FRulez := FRulez + [R_KEY_RED];
2121 if (e_Raw_Read_Byte(P) <> 0) then
2122 FRulez := FRulez + [R_KEY_GREEN];
2123 if (e_Raw_Read_Byte(P) <> 0) then
2124 FRulez := FRulez + [R_KEY_BLUE];
2125 if (e_Raw_Read_Byte(P) <> 0) then
2126 FRulez := FRulez + [R_BERSERK];
2128 Frags := e_Raw_Read_LongInt(P);
2129 Death := e_Raw_Read_LongInt(P);
2131 SetWeapon(e_Raw_Read_Byte(P));
2133 FSpectator := e_Raw_Read_Byte(P) <> 0;
2134 if FSpectator then
2135 begin
2136 if Pl = gPlayer1 then
2137 begin
2138 gLMSPID1 := UID;
2139 gPlayer1 := nil;
2140 end;
2141 if Pl = gPlayer2 then
2142 begin
2143 gLMSPID2 := UID;
2144 gPlayer2 := nil;
2145 end;
2146 end
2147 else
2148 begin
2149 if (gPlayer1 = nil) and (gLMSPID1 > 0) then
2150 gPlayer1 := g_Player_Get(gLMSPID1);
2151 if (gPlayer2 = nil) and (gLMSPID2 > 0) then
2152 gPlayer2 := g_Player_Get(gLMSPID2);
2153 end;
2154 FGhost := e_Raw_Read_Byte(P) <> 0;
2155 FPhysics := e_Raw_Read_Byte(P) <> 0;
2156 FNoRespawn := e_Raw_Read_Byte(P) <> 0;
2157 OldJet := FJetpack;
2158 FJetpack := e_Raw_Read_Byte(P) <> 0;
2159 if OldJet and not FJetpack then
2160 JetpackOff
2161 else if not OldJet and FJetpack then
2162 JetpackOn;
2163 if Team <> NewTeam then
2164 Pl.ChangeTeam(NewTeam);
2165 end;
2167 Result := PID;
2168 end;
2170 function MC_RECV_PlayerDamage(P: Pointer): Word;
2171 var
2172 PID: Word;
2173 Pl: TPlayer;
2174 Kind: Byte;
2175 Attacker, Value: Word;
2176 VX, VY: Integer;
2177 begin
2178 Result := 0;
2179 if not gGameOn then Exit;
2180 PID := e_Raw_Read_Word(P);
2181 Pl := g_Player_Get(PID);
2182 if Pl = nil then Exit;
2184 Kind := e_Raw_Read_Byte(P);
2185 Attacker := e_Raw_Read_Word(P);
2186 Value := e_Raw_Read_Word(P);
2187 VX := e_Raw_Read_Word(P);
2188 VY := e_Raw_Read_Word(P);
2190 with Pl do
2191 Damage(Value, Attacker, VX, VY, Kind);
2193 Result := PID;
2194 end;
2196 function MC_RECV_PlayerDeath(P: Pointer): Word;
2197 var
2198 PID: Word;
2199 Pl: TPlayer;
2200 KillType, DeathType: Byte;
2201 Attacker: Word;
2202 begin
2203 Result := 0;
2204 if not gGameOn then Exit;
2205 PID := e_Raw_Read_Word(P);
2206 Pl := g_Player_Get(PID);
2207 if Pl = nil then Exit;
2209 KillType := e_Raw_Read_Byte(P);
2210 DeathType := e_Raw_Read_Byte(P);
2211 Attacker := e_Raw_Read_Word(P);
2213 with Pl do
2214 begin
2215 Kill(KillType, Attacker, DeathType);
2216 SoftReset;
2217 end;
2218 end;
2220 function MC_RECV_PlayerDelete(P: Pointer): Word;
2221 var
2222 PID: Word;
2223 Pl: TPlayer;
2224 begin
2225 PID := e_Raw_Read_Word(P);
2226 Pl := g_Player_Get(PID);
2227 Result := 0;
2228 if Pl = nil then Exit;
2230 g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [Pl.Name]), True);
2231 e_WriteLog('NET: Player ' + Pl.Name + ' [' + IntToStr(PID) + '] removed.', MSG_NOTIFY);
2233 g_Player_Remove(PID);
2235 Result := PID;
2236 end;
2238 function MC_RECV_PlayerFire(P: Pointer): Word;
2239 var
2240 PID: Word;
2241 Weap: Byte;
2242 Pl: TPlayer;
2243 X, Y, AX, AY: Integer;
2244 SHID: Integer;
2245 begin
2246 Result := 0;
2247 if not gGameOn then Exit;
2248 PID := e_Raw_Read_Word(P);
2249 Pl := g_Player_Get(PID);
2250 if Pl = nil then Exit;
2252 Weap := e_Raw_Read_Byte(P);
2253 X := e_Raw_Read_LongInt(P);
2254 Y := e_Raw_Read_LongInt(P);
2255 AX := e_Raw_Read_LongInt(P);
2256 AY := e_Raw_Read_LongInt(P);
2257 SHID := e_Raw_Read_LongInt(P);
2259 with Pl do
2260 if Live then NetFire(Weap, X, Y, AX, AY, SHID);
2261 end;
2263 procedure MC_RECV_PlayerSettings(P: Pointer);
2264 var
2265 TmpName: string;
2266 TmpModel: string;
2267 TmpColor: TRGB;
2268 TmpTeam: Byte;
2269 Pl: TPlayer;
2270 PID: Word;
2271 begin
2272 PID := e_Raw_Read_Word(P);
2273 Pl := g_Player_Get(PID);
2274 if Pl = nil then Exit;
2276 TmpName := e_Raw_Read_String(P);
2277 TmpModel := e_Raw_Read_String(P);
2278 TmpColor.R := e_Raw_Read_Byte(P);
2279 TmpColor.G := e_Raw_Read_Byte(P);
2280 TmpColor.B := e_Raw_Read_Byte(P);
2281 TmpTeam := e_Raw_Read_Byte(P);
2283 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
2284 begin
2285 Pl.ChangeTeam(TmpTeam);
2286 if gPlayer1 = Pl then
2287 gPlayer1Settings.Team := TmpTeam;
2288 if gPlayer2 = Pl then
2289 gPlayer2Settings.Team := TmpTeam;
2290 end else
2291 Pl.SetColor(TmpColor);
2293 if Pl.Name <> TmpName then
2294 begin
2295 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
2296 Pl.Name := TmpName;
2297 end;
2299 if TmpModel <> Pl.Model.Name then
2300 Pl.SetModel(TmpModel);
2301 end;
2303 // ITEM
2305 procedure MC_RECV_ItemSpawn(P: Pointer);
2306 var
2307 ID: Word;
2308 AID: DWord;
2309 X, Y, VX, VY: Integer;
2310 T: Byte;
2311 Quiet, Fall{, Resp}: Boolean;
2312 Anim: TAnimation;
2313 begin
2314 if not gGameOn then Exit;
2315 ID := e_Raw_Read_Word(P);
2316 Quiet := e_Raw_Read_Byte(P) <> 0;
2317 T := e_Raw_Read_Byte(P);
2318 Fall := e_Raw_Read_Byte(P) <> 0;
2319 {Resp :=} e_Raw_Read_Byte(P);
2320 X := e_Raw_Read_LongInt(P);
2321 Y := e_Raw_Read_LongInt(P);
2322 VX := e_Raw_Read_LongInt(P);
2323 VY := e_Raw_Read_LongInt(P);
2325 g_Items_Create(X, Y, T, Fall, False, False, ID);
2326 gItems[ID].Obj.Vel.X := VX;
2327 gItems[ID].Obj.Vel.Y := VY;
2329 if not Quiet then
2330 begin
2331 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
2332 if g_Frames_Get(AID, 'FRAMES_ITEM_RESPAWN') then
2333 begin
2334 Anim := TAnimation.Create(AID, False, 4);
2335 g_GFX_OnceAnim(X+(gItems[ID].Obj.Rect.Width div 2)-16, Y+(gItems[ID].Obj.Rect.Height div 2)-16, Anim);
2336 Anim.Free();
2337 end;
2338 end;
2339 end;
2341 procedure MC_RECV_ItemDestroy(P: Pointer);
2342 var
2343 ID: Word;
2344 Quiet: Boolean;
2345 begin
2346 if not gGameOn then Exit;
2347 ID := e_Raw_Read_Word(P);
2348 Quiet := e_Raw_Read_Byte(P) <> 0;
2349 if gItems = nil then Exit;
2350 if (ID > High(gItems)) then Exit;
2352 if not Quiet then
2353 if gSoundEffectsDF then
2354 begin
2355 if gItems[ID].ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
2356 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
2357 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ',
2358 gItems[ID].Obj.X, gItems[ID].Obj.Y)
2359 else
2360 if gItems[ID].ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
2361 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
2362 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_AMMO_BACKPACK] then
2363 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON',
2364 gItems[ID].Obj.X, gItems[ID].Obj.Y)
2365 else
2366 g_Sound_PlayExAt('SOUND_ITEM_GETITEM',
2367 gItems[ID].Obj.X, gItems[ID].Obj.Y);
2368 end
2369 else
2370 begin
2371 if gItems[ID].ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
2372 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
2373 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ',
2374 gItems[ID].Obj.X, gItems[ID].Obj.Y)
2375 else
2376 if gItems[ID].ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
2377 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
2378 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET] then
2379 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON',
2380 gItems[ID].Obj.X, gItems[ID].Obj.Y)
2381 else
2382 g_Sound_PlayExAt('SOUND_ITEM_GETITEM',
2383 gItems[ID].Obj.X, gItems[ID].Obj.Y);
2384 end;
2386 g_Items_Remove(ID);
2387 end;
2389 // PANEL
2391 procedure MC_RECV_PanelTexture(P: Pointer);
2392 var
2393 TP: TPanel;
2394 PType: Word;
2395 ID: LongWord;
2396 Tex, Fr: Integer;
2397 Loop, Cnt: Byte;
2398 begin
2399 if not gGameOn then Exit;
2400 PType := e_Raw_Read_Word(P);
2401 ID := e_Raw_Read_LongWord(P);
2402 Tex := e_Raw_Read_LongInt(P);
2403 Fr := e_Raw_Read_LongInt(P);
2404 Cnt := e_Raw_Read_Byte(P);
2405 Loop := e_Raw_Read_Byte(P);
2407 TP := nil;
2409 case PType of
2410 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2411 if gWalls <> nil then
2412 TP := gWalls[ID];
2413 PANEL_FORE:
2414 if gRenderForegrounds <> nil then
2415 TP := gRenderForegrounds[ID];
2416 PANEL_BACK:
2417 if gRenderBackgrounds <> nil then
2418 TP := gRenderBackgrounds[ID];
2419 PANEL_WATER:
2420 if gWater <> nil then
2421 TP := gWater[ID];
2422 PANEL_ACID1:
2423 if gAcid1 <> nil then
2424 TP := gAcid1[ID];
2425 PANEL_ACID2:
2426 if gAcid2 <> nil then
2427 TP := gAcid2[ID];
2428 PANEL_STEP:
2429 if gSteps <> nil then
2430 TP := gSteps[ID];
2431 else
2432 Exit;
2433 end;
2435 if TP <> nil then
2436 if Loop = 0 then
2437 begin // switch texture
2438 TP.SetTexture(Tex, Loop);
2439 TP.SetFrame(Fr, Cnt);
2440 end else // looped or non-looped animation
2441 TP.NextTexture(Loop);
2442 end;
2444 procedure MC_RECV_PanelState(P: Pointer);
2445 var
2446 ID: LongWord;
2447 E: Boolean;
2448 Lift: Byte;
2449 PType: Word;
2450 X, Y: Integer;
2451 begin
2452 if not gGameOn then Exit;
2453 PType := e_Raw_Read_Word(P);
2454 ID := e_Raw_Read_LongWord(P);
2455 E := (e_Raw_Read_Byte(P) <> 0);
2456 Lift := e_Raw_Read_Byte(P);
2457 X := e_Raw_Read_LongInt(P);
2458 Y := e_Raw_Read_LongInt(P);
2460 case PType of
2461 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2462 if E then
2463 g_Map_EnableWall(ID)
2464 else
2465 g_Map_DisableWall(ID);
2467 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
2468 g_Map_SetLift(ID, Lift);
2470 PANEL_BACK:
2471 begin
2472 gRenderBackgrounds[ID].X := X;
2473 gRenderBackgrounds[ID].Y := Y;
2474 end;
2476 PANEL_FORE:
2477 begin
2478 gRenderForegrounds[ID].X := X;
2479 gRenderForegrounds[ID].Y := Y;
2480 end;
2481 end;
2482 end;
2484 // TRIGGERS
2486 procedure MC_RECV_TriggerSound(P: Pointer);
2487 var
2488 SPlaying: Boolean;
2489 SPos, SID: LongWord;
2490 SCount: LongInt;
2491 I: Integer;
2492 begin
2493 if not gGameOn then Exit;
2494 if gTriggers = nil then Exit;
2496 SID := e_Raw_Read_LongWord(P);
2497 SPlaying := e_Raw_Read_Byte(P) <> 0;
2498 SPos := e_Raw_Read_LongWord(P);
2499 SCount := e_Raw_Read_LongInt(P);
2501 for I := Low(gTriggers) to High(gTriggers) do
2502 if gTriggers[I].TriggerType = TRIGGER_SOUND then
2503 if gTriggers[I].ClientID = SID then
2504 with gTriggers[I] do
2505 begin
2506 if SPlaying then
2507 begin
2508 if Data.Local then
2509 Sound.PlayVolumeAt(X+(Width div 2), Y+(Height div 2), Data.Volume/255.0)
2510 else
2511 Sound.PlayPanVolume((Data.Pan-127.0)/128.0, Data.Volume/255.0);
2512 Sound.SetPosition(SPos);
2513 end
2514 else
2515 if Sound.IsPlaying then Sound.Stop;
2517 SoundPlayCount := SCount;
2518 end;
2519 end;
2521 procedure MC_RECV_TriggerMusic(P: Pointer);
2522 var
2523 MName: string;
2524 MPlaying: Boolean;
2525 MPos: LongWord;
2526 MPaused: Boolean;
2527 begin
2528 if not gGameOn then Exit;
2530 MName := e_Raw_Read_String(P);
2531 MPlaying := e_Raw_Read_Byte(P) <> 0;
2532 MPos := e_Raw_Read_LongWord(P);
2533 MPaused := e_Raw_Read_Byte(P) <> 0;
2535 if MPlaying then
2536 begin
2537 gMusic.SetByName(MName);
2538 gMusic.Play(True);
2539 gMusic.SetPosition(MPos);
2540 gMusic.SpecPause := MPaused;
2541 end
2542 else
2543 if gMusic.IsPlaying then gMusic.Stop;
2544 end;
2546 // MONSTERS
2548 procedure MC_RECV_MonsterSpawn(P: Pointer);
2549 var
2550 ID: Word;
2551 MType, MState, MDir, MAnim, MBehav: Byte;
2552 X, Y, VX, VY, MTargTime, MHealth, MAmmo, MSleep: Integer;
2553 MTarg: Word;
2554 M: TMonster;
2555 begin
2556 ID := e_Raw_Read_Word(P);
2557 M := g_Monsters_Get(ID);
2558 if M <> nil then
2559 Exit;
2561 MType := e_Raw_Read_Byte(P);
2562 MState := e_Raw_Read_Byte(P);
2563 MAnim := e_Raw_Read_Byte(P);
2564 MTarg := e_Raw_Read_Word(P);
2565 MTargTime := e_Raw_Read_LongInt(P);
2566 MBehav := e_Raw_Read_Byte(P);
2567 MSleep := e_Raw_Read_LongInt(P);
2568 MHealth := e_Raw_Read_LongInt(P);
2569 MAmmo := e_Raw_Read_LongInt(P);
2571 X := e_Raw_Read_LongInt(P);
2572 Y := e_Raw_Read_LongInt(P);
2573 VX := e_Raw_Read_LongInt(P);
2574 VY := e_Raw_Read_LongInt(P);
2575 MDir := e_Raw_Read_Byte(P);
2577 g_Monsters_Create(MType, X, Y, TDirection(MDir), False, ID);
2578 M := g_Monsters_Get(ID);
2579 if M = nil then
2580 Exit;
2582 with M do
2583 begin
2584 GameX := X;
2585 GameY := Y;
2586 GameVelX := VX;
2587 GameVelY := VY;
2589 MonsterAnim := MAnim;
2590 MonsterTargetUID := MTarg;
2591 MonsterTargetTime := MTargTime;
2592 MonsterBehaviour := MBehav;
2593 MonsterSleep := MSleep;
2594 MonsterAmmo := MAmmo;
2595 SetHealth(MHealth);
2597 SetState(MState);
2598 end;
2599 end;
2601 procedure MC_RECV_MonsterPos(P: Pointer);
2602 var
2603 M: TMonster;
2604 ID: Word;
2605 begin
2606 ID := e_Raw_Read_Word(P);
2607 M := g_Monsters_Get(ID);
2608 if M = nil then
2609 Exit;
2611 with M do
2612 begin
2613 GameX := e_Raw_Read_LongInt(P);
2614 GameY := e_Raw_Read_LongInt(P);
2615 GameVelX := e_Raw_Read_LongInt(P);
2616 GameVelY := e_Raw_Read_LongInt(P);
2617 GameDirection := TDirection(e_Raw_Read_Byte(P));
2618 end;
2619 end;
2621 procedure MC_RECV_MonsterState(P: Pointer);
2622 var
2623 ID: Integer;
2624 MState, MFAnm: Byte;
2625 M: TMonster;
2626 AnimRevert: Boolean;
2627 begin
2628 ID := e_Raw_Read_Word(P);
2629 M := g_Monsters_Get(ID);
2630 if M = nil then Exit;
2632 MState := e_Raw_Read_Byte(P);
2633 MFAnm := e_Raw_Read_Byte(P);
2635 with M do
2636 begin
2637 MonsterTargetUID := e_Raw_Read_Word(P);
2638 MonsterTargetTime := e_Raw_Read_LongInt(P);
2639 MonsterSleep := e_Raw_Read_LongInt(P);
2640 MonsterHealth := e_Raw_Read_LongInt(P);
2641 MonsterAmmo := e_Raw_Read_LongInt(P);
2642 MonsterPain := e_Raw_Read_LongInt(P);
2643 AnimRevert := e_Raw_Read_Byte(P) <> 0;
2644 RevertAnim(AnimRevert);
2646 if MonsterState <> MState then
2647 begin
2648 if (MState = MONSTATE_GO) and (MonsterState = MONSTATE_SLEEP) then
2649 WakeUpSound;
2650 if (MState = MONSTATE_DIE) then
2651 DieSound;
2652 if (MState = MONSTATE_PAIN) then
2653 MakeBloodSimple(Min(200, MonsterPain));
2654 if (MState = MONSTATE_ATTACK) then
2655 kick(nil);
2656 if (MState = MONSTATE_DEAD) then
2657 SetDeadAnim;
2659 SetState(MState, MFAnm);
2660 end;
2661 end;
2662 end;
2664 procedure MC_RECV_MonsterShot(P: Pointer);
2665 var
2666 ID: Integer;
2667 M: TMonster;
2668 X, Y, VX, VY: Integer;
2669 begin
2670 ID := e_Raw_Read_Word(P);
2672 M := g_Monsters_Get(ID);
2673 if M = nil then Exit;
2675 X := e_Raw_Read_LongInt(P);
2676 Y := e_Raw_Read_LongInt(P);
2677 VX := e_Raw_Read_LongInt(P);
2678 VY := e_Raw_Read_LongInt(P);
2680 M.ClientAttack(X, Y, VX, VY);
2681 end;
2683 procedure MC_RECV_MonsterDelete(P: Pointer);
2684 var
2685 ID: Integer;
2686 M: TMonster;
2687 begin
2688 ID := e_Raw_Read_Word(P);
2689 M := g_Monsters_Get(ID);
2690 if M = nil then Exit;
2692 gMonsters[ID].SetState(5);
2693 gMonsters[ID].MonsterRemoved := True;
2694 end;
2696 procedure MC_RECV_TimeSync(P: Pointer);
2697 var
2698 Time: LongWord;
2699 begin
2700 Time := e_Raw_Read_LongWord(P);
2702 if gState = STATE_INTERCUSTOM then
2703 gServInterTime := Min(Time, 255);
2704 end;
2706 procedure MC_RECV_VoteEvent(P: Pointer);
2707 var
2708 EvID: Byte;
2709 Str1, Str2: string;
2710 Int1, Int2: SmallInt;
2711 begin
2712 EvID := e_Raw_Read_Byte(P);
2713 Int1 := e_Raw_Read_SmallInt(P);
2714 Int2 := e_Raw_Read_SmallInt(P);
2715 Str1 := e_Raw_Read_String(P);
2716 Str2 := e_Raw_Read_String(P);
2718 case EvID of
2719 NET_VE_STARTED:
2720 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_STARTED], [Str1, Str2, Int1]), True);
2721 NET_VE_PASSED:
2722 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_PASSED], [Str1]), True);
2723 NET_VE_FAILED:
2724 g_Console_Add(_lc[I_MESSAGE_VOTE_FAILED], True);
2725 NET_VE_VOTE:
2726 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Str1, Int1, Int2]), True);
2727 NET_VE_INPROGRESS:
2728 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_INPROGRESS], [Str1]), True);
2729 end;
2730 end;
2732 // CLIENT SEND
2734 procedure MC_SEND_Info(Password: string);
2735 begin
2736 e_Buffer_Clear(@NetOut);
2738 e_Buffer_Write(@NetOut, Byte(NET_MSG_INFO));
2739 e_Buffer_Write(@NetOut, GAME_VERSION);
2740 e_Buffer_Write(@NetOut, Password);
2741 e_Buffer_Write(@NetOut, gPlayer1Settings.Name);
2742 e_Buffer_Write(@NetOut, gPlayer1Settings.Model);
2743 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.R);
2744 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.G);
2745 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.B);
2746 e_Buffer_Write(@NetOut, gPlayer1Settings.Team);
2748 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2749 end;
2751 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
2752 begin
2753 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHAT));
2754 e_Buffer_Write(@NetOut, Txt);
2755 e_Buffer_Write(@NetOut, Mode);
2757 g_Net_Client_Send(True, NET_CHAN_CHAT);
2758 end;
2760 procedure MC_SEND_PlayerPos();
2761 var
2762 kByte: Word;
2763 Predict: Boolean;
2764 begin
2765 if not gGameOn then Exit;
2766 if gPlayers = nil then Exit;
2767 if gPlayer1 = nil then Exit;
2769 kByte := 0;
2770 Predict := NetPredictSelf; // and (not NetGotKeys);
2772 if (not gConsoleShow) and (not gChatShow) and (g_ActiveWindow = nil) then
2773 with gGameControls.P1Control do
2774 begin
2775 if e_KeyPressed(KeyLeft) and (not e_KeyPressed(KeyRight)) then
2776 P1MoveButton := 1
2777 else
2778 if (not e_KeyPressed(KeyLeft)) and e_KeyPressed(KeyRight) then
2779 P1MoveButton := 2
2780 else
2781 if (not e_KeyPressed(KeyLeft)) and (not e_KeyPressed(KeyRight)) then
2782 P1MoveButton := 0;
2784 if (P1MoveButton = 2) and e_KeyPressed(KeyLeft) then
2785 gPlayer1.SetDirection(D_LEFT)
2786 else
2787 if (P1MoveButton = 1) and e_KeyPressed(KeyRight) then
2788 gPlayer1.SetDirection(D_RIGHT)
2789 else
2790 if P1MoveButton <> 0 then
2791 gPlayer1.SetDirection(TDirection(P1MoveButton-1));
2793 gPlayer1.ReleaseKeys;
2794 if P1MoveButton = 1 then
2795 begin
2796 kByte := kByte or NET_KEY_LEFT;
2797 if Predict then gPlayer1.PressKey(KEY_LEFT, 10000);
2798 end;
2799 if P1MoveButton = 2 then
2800 begin
2801 kByte := kByte or NET_KEY_RIGHT;
2802 if Predict then gPlayer1.PressKey(KEY_RIGHT, 10000);
2803 end;
2804 if e_KeyPressed(KeyUp) then
2805 begin
2806 kByte := kByte or NET_KEY_UP;
2807 gPlayer1.PressKey(KEY_UP, 10000);
2808 end;
2809 if e_KeyPressed(KeyDown) then
2810 begin
2811 kByte := kByte or NET_KEY_DOWN;
2812 gPlayer1.PressKey(KEY_DOWN, 10000);
2813 end;
2814 if e_KeyPressed(KeyJump) then
2815 begin
2816 kByte := kByte or NET_KEY_JUMP;
2817 // gPlayer1.PressKey(KEY_JUMP, 10000); // TODO: Make a prediction option
2818 end;
2819 if e_KeyPressed(KeyFire) then kByte := kByte or NET_KEY_FIRE;
2820 if e_KeyPressed(KeyOpen) then kByte := kByte or NET_KEY_OPEN;
2821 if e_KeyPressed(KeyNextWeapon) then kByte := kByte or NET_KEY_NW;
2822 if e_KeyPressed(KeyPrevWeapon) then kByte := kByte or NET_KEY_PW;
2823 end
2824 else
2825 kByte := NET_KEY_CHAT;
2827 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRPOS));
2828 e_Buffer_Write(@NetOut, gTime);
2829 e_Buffer_Write(@NetOut, kByte);
2830 e_Buffer_Write(@NetOut, Byte(gPlayer1.Direction));
2831 g_Net_Client_Send(True, NET_CHAN_PLAYERPOS);
2833 //kBytePrev := kByte;
2834 //kDirPrev := gPlayer1.Direction;
2835 end;
2837 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
2838 begin
2839 e_Buffer_Write(@NetOut, Byte(NET_MSG_VOTE_EVENT));
2840 e_Buffer_Write(@NetOut, Byte(Start));
2841 e_Buffer_Write(@NetOut, Command);
2842 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2843 end;
2845 procedure MC_SEND_PlayerSettings();
2846 begin
2847 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRSET));
2848 e_Buffer_Write(@NetOut, gPlayer1Settings.Name);
2849 e_Buffer_Write(@NetOut, gPlayer1Settings.Model);
2850 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.R);
2851 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.G);
2852 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.B);
2853 e_Buffer_Write(@NetOut, gPlayer1Settings.Team);
2855 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2856 end;
2858 procedure MC_SEND_FullStateRequest();
2859 begin
2860 e_Buffer_Write(@NetOut, Byte(NET_MSG_REQFST));
2862 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2863 end;
2865 procedure MC_SEND_CheatRequest(Kind: Byte);
2866 begin
2867 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHEAT));
2868 e_Buffer_Write(@NetOut, Kind);
2870 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2871 end;
2872 procedure MC_SEND_RCONPassword(Password: string);
2873 begin
2874 e_Buffer_Write(@NetOut, Byte(NET_MSG_RCON_AUTH));
2875 e_Buffer_Write(@NetOut, Password);
2877 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2878 end;
2879 procedure MC_SEND_RCONCommand(Cmd: string);
2880 begin
2881 e_Buffer_Write(@NetOut, Byte(NET_MSG_RCON_CMD));
2882 e_Buffer_Write(@NetOut, Cmd);
2884 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2885 end;
2887 // i have no idea why all this stuff is in here
2889 function ReadFile(const FileName: TFileName): AByte;
2890 var
2891 FileStream : TStream;
2892 fname: string;
2893 begin
2894 e_WriteLog(Format('NETWORK: looking for file "%s"', [FileName]), MSG_NOTIFY);
2895 fname := findDiskWad(FileName);
2896 if length(fname) = 0 then
2897 begin
2898 e_WriteLog(Format('NETWORK: file "%s" not found!', [FileName]), MSG_FATALERROR);
2899 SetLength(Result, 0);
2900 exit;
2901 end;
2902 e_WriteLog(Format('NETWORK: found file "%s"', [fname]), MSG_NOTIFY);
2903 Result := nil;
2904 FileStream := openDiskFileRO(fname);
2905 try
2906 if FileStream.Size > 0 then
2907 begin
2908 SetLength(Result, FileStream.Size);
2909 FileStream.Read(Result[0], FileStream.Size);
2910 end;
2911 finally
2912 FileStream.Free;
2913 end;
2914 end;
2916 function CreateMapDataMsg(const FileName: TFileName; ResList: TStringList): TMapDataMsg;
2917 var
2918 i: Integer;
2919 begin
2920 Result.MsgId := NET_MSG_MAP_RESPONSE;
2921 Result.FileData := ReadFile(FileName);
2922 Result.FileSize := Length(Result.FileData);
2924 SetLength(Result.ExternalResources, ResList.Count);
2925 for i:=0 to ResList.Count-1 do
2926 begin
2927 Result.ExternalResources[i].Name := ResList.Strings[i];
2928 Result.ExternalResources[i].md5 := MD5File(GameDir+'/wads/'+ResList.Strings[i]);
2929 end;
2930 end;
2932 procedure ResDataMsgToBytes(var bytes: AByte; const ResData: TResDataMsg);
2933 var
2934 ResultStream: TMemoryStream;
2935 begin
2936 ResultStream := TMemoryStream.Create;
2938 ResultStream.WriteBuffer(ResData.MsgId, SizeOf(ResData.MsgId)); //msgId
2939 ResultStream.WriteBuffer(ResData.FileSize, SizeOf(ResData.FileSize)); //file size
2940 ResultStream.WriteBuffer(ResData.FileData[0], ResData.FileSize); //file data
2942 SetLength(bytes, ResultStream.Size);
2943 ResultStream.Seek(0, soFromBeginning);
2944 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
2946 ResultStream.Free;
2947 end;
2949 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
2950 begin
2951 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
2952 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize));
2953 SetLength(Result.FileData, Result.FileSize);
2954 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize);
2955 end;
2957 procedure MapDataMsgToBytes(var bytes: AByte; const MapDataMsg: TMapDataMsg);
2958 var
2959 ResultStream: TMemoryStream;
2960 resCount: Integer;
2961 begin
2962 resCount := Length(MapDataMsg.ExternalResources);
2964 ResultStream := TMemoryStream.Create;
2966 ResultStream.WriteBuffer(MapDataMsg.MsgId, SizeOf(MapDataMsg.MsgId)); //msgId
2967 ResultStream.WriteBuffer(MapDataMsg.FileSize, SizeOf(MapDataMsg.FileSize)); //file size
2968 ResultStream.WriteBuffer(MapDataMsg.FileData[0], MapDataMsg.FileSize); //file data
2970 ResultStream.WriteBuffer(resCount, SizeOf(resCount)); //res count
2971 ResultStream.WriteBuffer(MapDataMsg.ExternalResources[0], resCount*SizeOf(TExternalResourceInfo)); //res data
2973 SetLength(bytes, ResultStream.Size);
2974 ResultStream.Seek(0, soFromBeginning);
2975 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
2977 ResultStream.Free;
2978 end;
2980 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
2981 var
2982 resCount: Integer;
2983 begin
2984 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
2985 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize)); //file size
2987 SetLength(Result.FileData, Result.FileSize);
2988 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize); //file data
2990 msgStream.ReadBuffer(resCount, SizeOf(resCount)); //res count
2991 SetLength(Result.ExternalResources, resCount);
2993 msgStream.ReadBuffer(Result.ExternalResources[0], resCount * SizeOf(TExternalResourceInfo)); //res data
2994 end;
2996 function IsValidFileName(const S: String): Boolean;
2997 const
2998 Forbidden: set of Char = ['<', '>', '|', '"', ':', '*', '?'];
2999 var
3000 I: Integer;
3001 begin
3002 Result := S <> '';
3003 for I := 1 to Length(S) do
3004 Result := Result and (not(S[I] in Forbidden));
3005 end;
3007 function IsValidFilePath(const S: String): Boolean;
3008 var
3009 I: Integer;
3010 begin
3011 Result := False;
3012 if not IsValidFileName(S) then exit;
3013 if FileExists(S) then exit;
3014 I := LastDelimiter('\/', S);
3015 if (I > 0) then
3016 if (not DirectoryExists(Copy(S, 1, I-1))) then
3017 exit;
3018 Result := True;
3019 end;
3021 procedure MC_SEND_MapRequest();
3022 begin
3023 e_Buffer_Write(@NetOut, Byte(NET_MSG_MAP_REQUEST));
3024 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3025 end;
3027 procedure MC_SEND_ResRequest(const resName: AnsiString);
3028 begin
3029 e_Buffer_Write(@NetOut, Byte(NET_MSG_RES_REQUEST));
3030 e_Buffer_Write(@NetOut, resName);
3031 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3032 end;
3034 procedure MH_RECV_MapRequest(C: pTNetClient; P: Pointer);
3035 var
3036 payload: AByte;
3037 peer: pENetPeer;
3038 mapDataMsg: TMapDataMsg;
3039 begin
3040 e_WriteLog('NET: Received map request from ' +
3041 DecodeIPV4(C.Peer.address.host), MSG_NOTIFY);
3043 mapDataMsg := CreateMapDataMsg(MapsDir + gGameSettings.WAD, gExternalResources);
3044 peer := NetClients[C.ID].Peer;
3046 MapDataMsgToBytes(payload, mapDataMsg);
3047 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3049 payload := nil;
3050 mapDataMsg.FileData := nil;
3051 mapDataMsg.ExternalResources := nil;
3052 end;
3054 procedure MH_RECV_ResRequest(C: pTNetClient; P: Pointer);
3055 var
3056 payload: AByte;
3057 peer: pENetPeer;
3058 FileName: String;
3059 resDataMsg: TResDataMsg;
3060 begin
3061 FileName := ExtractFileName(e_Raw_Read_String(P));
3062 e_WriteLog('NET: Received res request: ' + FileName +
3063 ' from ' + DecodeIPV4(C.Peer.address.host), MSG_NOTIFY);
3065 if not IsValidFilePath(FileName) then
3066 begin
3067 e_WriteLog('Invalid filename: ' + FileName, MSG_WARNING);
3068 exit;
3069 end;
3071 peer := NetClients[C.ID].Peer;
3073 if gExternalResources.IndexOf(FileName) > -1 then
3074 begin
3075 resDataMsg.MsgId := NET_MSG_RES_RESPONSE;
3076 resDataMsg.FileData := ReadFile(GameDir+'/wads/'+FileName);
3077 resDataMsg.FileSize := Length(resDataMsg.FileData);
3079 ResDataMsgToBytes(payload, resDataMsg);
3080 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3081 end;
3082 end;
3084 end.