DEADSOFTWARE

no more public `gMonsters`
[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 {$INCLUDE ../shared/a_modes.inc}
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; Pos: Boolean = True; 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, i: Byte;
434 WeaponSelect: Word;
435 PID: Word;
436 kByte: Word;
437 Pl: TPlayer;
438 GT: LongWord;
439 begin
440 Result := 0;
441 if not gGameOn then Exit;
443 GT := e_Raw_Read_LongWord(P);
444 PID := C^.Player;
445 Pl := g_Player_Get(PID);
446 if Pl = nil then
447 Exit;
449 if (GT > gTime + NET_MAX_DIFFTIME) or (GT < Pl.NetTime) then Exit;
451 with Pl do
452 begin
453 NetTime := GT;
454 kByte := e_Raw_Read_Word(P);
455 Dir := e_Raw_Read_Byte(P);
456 WeaponSelect := e_Raw_Read_Word(P);
457 //e_WriteLog(Format('R:ws=%d', [WeaponSelect]), MSG_WARNING);
458 if Direction <> TDirection(Dir) then
459 JustTeleported := False;
461 SetDirection(TDirection(Dir));
462 ReleaseKeys;
464 if kByte = NET_KEY_CHAT then
465 begin
466 PressKey(KEY_CHAT, 10000);
467 Exit;
468 end;
470 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
471 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
472 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
473 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
474 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
475 if LongBool(kByte and NET_KEY_FIRE) then PressKey(KEY_FIRE, 10000);
476 if LongBool(kByte and NET_KEY_OPEN) then PressKey(KEY_OPEN, 10000);
477 if LongBool(kByte and NET_KEY_NW) then PressKey(KEY_NEXTWEAPON, 10000);
478 if LongBool(kByte and NET_KEY_PW) then PressKey(KEY_PREVWEAPON, 10000);
480 for i := 0 to 15 do
481 begin
482 if (WeaponSelect and Word(1 shl i)) <> 0 then
483 begin
484 //e_WriteLog(Format(' R:wn=%d', [i]), MSG_WARNING);
485 QueueWeaponSwitch(i);
486 end;
487 end;
488 end;
490 // MH_SEND_PlayerPos(False, PID, C^.ID);
491 end;
493 procedure MH_RECV_CheatRequest(C: pTNetClient; P: Pointer);
494 var
495 CheatKind: Byte;
496 Pl: TPlayer;
497 begin
498 Pl := g_Player_Get(C^.Player);
499 if Pl = nil then Exit;
501 CheatKind := e_Raw_Read_Byte(P);
503 case CheatKind of
504 NET_CHEAT_SUICIDE:
505 Pl.Damage(SUICIDE_DAMAGE, Pl.UID, 0, 0, HIT_SELF);
506 NET_CHEAT_SPECTATE:
507 begin
508 if Pl.FSpectator then
509 Pl.Respawn(False)
510 else
511 Pl.Spectate;
512 end;
513 end;
514 end;
516 procedure MH_RECV_PlayerSettings(C: pTNetClient; P: Pointer);
517 var
518 TmpName: string;
519 TmpModel: string;
520 TmpColor: TRGB;
521 TmpTeam: Byte;
522 Pl: TPlayer;
523 begin
524 TmpName := e_Raw_Read_String(P);
525 TmpModel := e_Raw_Read_String(P);
526 TmpColor.R := e_Raw_Read_Byte(P);
527 TmpColor.G := e_Raw_Read_Byte(P);
528 TmpColor.B := e_Raw_Read_Byte(P);
529 TmpTeam := e_Raw_Read_Byte(P);
531 Pl := g_Player_Get(C^.Player);
532 if Pl = nil then Exit;
534 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
535 Pl.SwitchTeam
536 else
537 Pl.SetColor(TmpColor);
539 if Pl.Name <> TmpName then
540 begin
541 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
542 Pl.Name := TmpName;
543 end;
545 if TmpModel <> Pl.Model.Name then
546 Pl.SetModel(TmpModel);
548 MH_SEND_PlayerSettings(Pl.UID, TmpModel);
549 end;
551 // RCON
553 procedure MH_RECV_RCONPassword(C: pTNetClient; P: Pointer);
554 var
555 Pwd: string;
556 begin
557 Pwd := e_Raw_Read_String(P);
558 if not NetAllowRCON then Exit;
559 if Pwd = NetRCONPassword then
560 begin
561 C^.RCONAuth := True;
562 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWGOOD, 'N', C^.ID);
563 end
564 else
565 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWBAD, 'N', C^.ID);
566 end;
568 procedure MH_RECV_RCONCommand(C: pTNetClient; P: Pointer);
569 var
570 Cmd: string;
571 begin
572 Cmd := e_Raw_Read_String(P);
573 if not NetAllowRCON then Exit;
574 if not C^.RCONAuth then
575 begin
576 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_NOAUTH, 'N', C^.ID);
577 Exit;
578 end;
579 g_Console_Process(Cmd);
580 end;
582 // MISC
584 procedure MH_RECV_Vote(C: pTNetClient; P: Pointer);
585 var
586 Start: Boolean;
587 Name, Command: string;
588 Need: Integer;
589 Pl: TPlayer;
590 begin
591 Start := e_Raw_Read_Byte(P) <> 0;
592 Command := e_Raw_Read_String(P);
594 Pl := g_Player_Get(C^.Player);
595 if Pl = nil then Exit;
596 Name := Pl.Name;
598 if Start then
599 begin
600 if not g_Console_CommandBlacklisted(Command) then
601 g_Game_StartVote(Command, Name);
602 end
603 else if gVoteInProgress then
604 begin
605 if (gPlayer1 <> nil) or (gPlayer2 <> nil) then
606 Need := Floor((NetClientCount+1)/2.0) + 1
607 else
608 Need := Floor(NetClientCount/2.0) + 1;
609 if C^.Voted then
610 begin
611 Dec(gVoteCount);
612 C^.Voted := False;
613 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_REVOKED], [Name, gVoteCount, Need]), True);
614 MH_SEND_VoteEvent(NET_VE_REVOKE, Name, 'a', gVoteCount, Need);
615 end
616 else
617 begin
618 Inc(gVoteCount);
619 C^.Voted := True;
620 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Name, gVoteCount, Need]), True);
621 MH_SEND_VoteEvent(NET_VE_VOTE, Name, 'a', gVoteCount, Need);
622 g_Game_CheckVote;
623 end;
624 end;
625 end;
627 // GAME (SEND)
629 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
631 function sendItemRespawn (it: PItem): Boolean;
632 begin
633 result := false; // don't stop
634 MH_SEND_ItemSpawn(True, it.myid, ID);
635 end;
637 function sendMonSpawn (monidx: Integer; mon: TMonster): Boolean;
638 begin
639 result := false; // don't stop
640 MH_SEND_MonsterSpawn(mon.UID, ID);
641 end;
643 var
644 I: Integer;
645 begin
646 if gPlayers <> nil then
647 begin
648 for I := Low(gPlayers) to High(gPlayers) do
649 begin
650 if gPlayers[I] <> nil then
651 begin
652 if CreatePlayers then MH_SEND_PlayerCreate(gPlayers[I].UID, ID);
653 MH_SEND_PlayerPos(True, gPlayers[I].UID, ID);
654 MH_SEND_PlayerStats(gPlayers[I].UID, ID);
656 if (gPlayers[I].Flag <> FLAG_NONE) and (gGameSettings.GameMode = GM_CTF) then
657 MH_SEND_FlagEvent(FLAG_STATE_CAPTURED, gPlayers[I].Flag, gPlayers[I].UID, True, ID);
658 end;
659 end;
660 end;
662 g_Items_ForEachAlive(sendItemRespawn, true); // backwards
663 g_Mons_ForEach(sendMonSpawn);
665 if gWalls <> nil then
666 for I := Low(gWalls) to High(gWalls) do
667 if gWalls[I] <> nil then
668 with gWalls[I] do
669 begin
670 if Door then
671 MH_SEND_PanelState(PanelType, I, ID);
673 if GetTextureCount > 1 then
674 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
675 end;
677 if gLifts <> nil then
678 for I := Low(gLifts) to High(gLifts) do
679 if gLifts[I] <> nil then
680 with gLifts[I] do
681 MH_SEND_PanelState(PanelType, I, ID);
683 if gRenderForegrounds <> nil then
684 for I := Low(gRenderForegrounds) to High(gRenderForegrounds) do
685 if gRenderForegrounds[I] <> nil then
686 with gRenderForegrounds[I] do
687 begin
688 if (GetTextureCount > 1) then
689 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
690 if Moved then
691 MH_SEND_PanelState(PanelType, I, ID);
692 end;
693 if gRenderBackgrounds <> nil then
694 for I := Low(gRenderBackgrounds) to High(gRenderBackgrounds) do
695 if gRenderBackgrounds[I] <> nil then
696 with gRenderBackgrounds[I] do
697 begin
698 if (GetTextureCount > 1) then
699 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
700 if Moved then
701 MH_SEND_PanelState(PanelType, I, ID);
702 end;
703 if gWater <> nil then
704 for I := Low(gWater) to High(gWater) do
705 if gWater[I] <> nil then
706 with gWater[I] do
707 if GetTextureCount > 1 then
708 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
709 if gAcid1 <> nil then
710 for I := Low(gAcid1) to High(gAcid1) do
711 if gAcid1[I] <> nil then
712 with gAcid1[I] do
713 if GetTextureCount > 1 then
714 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
715 if gAcid2 <> nil then
716 for I := Low(gAcid2) to High(gAcid2) do
717 if gAcid2[I] <> nil then
718 with gAcid2[I] do
719 if GetTextureCount > 1 then
720 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
721 if gSteps <> nil then
722 for I := Low(gSteps) to High(gSteps) do
723 if gSteps[I] <> nil then
724 with gSteps[I] do
725 if GetTextureCount > 1 then
726 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
728 if gTriggers <> nil then
729 for I := Low(gTriggers) to High(gTriggers) do
730 if gTriggers[I].TriggerType = TRIGGER_SOUND then
731 MH_SEND_TriggerSound(gTriggers[I], ID);
733 if Shots <> nil then
734 for I := Low(Shots) to High(Shots) do
735 if Shots[i].ShotType in [6, 7, 8] then
736 MH_SEND_CreateShot(i, ID);
738 MH_SEND_TriggerMusic(ID);
740 MH_SEND_GameStats(ID);
741 MH_SEND_CoopStats(ID);
743 if gGameSettings.GameMode = GM_CTF then
744 begin
745 if gFlags[FLAG_RED].State <> FLAG_STATE_CAPTURED then
746 MH_SEND_FlagEvent(gFlags[FLAG_RED].State, FLAG_RED, 0, True, ID);
747 if gFlags[FLAG_BLUE].State <> FLAG_STATE_CAPTURED then
748 MH_SEND_FlagEvent(gFlags[FLAG_BLUE].State, FLAG_BLUE, 0, True, ID);
749 end;
751 if CreatePlayers and (ID >= 0) then NetClients[ID].State := NET_STATE_GAME;
753 if gLMSRespawn > LMS_RESPAWN_NONE then
754 MH_SEND_GameEvent(NET_EV_LMS_WARMUP, (gLMSRespawnTime - gTime) div 1000, 'N', ID);
755 end;
757 procedure MH_SEND_Info(ID: Byte);
758 var
759 Map: string;
760 begin
761 Map := g_ExtractFileName(gMapInfo.Map);
763 e_Buffer_Clear(@NetOut);
765 e_Buffer_Write(@NetOut, Byte(NET_MSG_INFO));
766 e_Buffer_Write(@NetOut, ID);
767 e_Buffer_Write(@NetOut, NetClients[ID].Player);
768 e_Buffer_Write(@NetOut, gGameSettings.WAD);
769 e_Buffer_Write(@NetOut, Map);
770 e_Buffer_Write(@NetOut, gWADHash);
771 e_Buffer_Write(@NetOut, gGameSettings.GameMode);
772 e_Buffer_Write(@NetOut, gGameSettings.GoalLimit);
773 e_Buffer_Write(@NetOut, gGameSettings.TimeLimit);
774 e_Buffer_Write(@NetOut, gGameSettings.MaxLives);
775 e_Buffer_Write(@NetOut, gGameSettings.Options);
776 e_Buffer_Write(@NetOut, gTime);
778 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
779 end;
781 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
782 var
783 Name: string;
784 i: Integer;
785 Team: Byte;
786 begin
787 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
788 Mode := NET_CHAT_PLAYER;
790 Team := 0;
791 if (Mode = NET_CHAT_TEAM) then
792 begin
793 for i := Low(gPlayers) to High(gPlayers) do
794 if (gPlayers[i] <> nil) and (gPlayers[i].FClientID >= 0) and
795 (gPlayers[i].Team = ID) then
796 begin
797 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHAT));
798 e_Buffer_Write(@NetOut, Txt);
799 e_Buffer_Write(@NetOut, Mode);
800 g_Net_Host_Send(gPlayers[i].FClientID, True, NET_CHAN_CHAT);
801 end;
802 Team := ID;
803 ID := NET_EVERYONE;
804 end
805 else
806 begin
807 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHAT));
808 e_Buffer_Write(@NetOut, Txt);
809 e_Buffer_Write(@NetOut, Mode);
810 g_Net_Host_Send(ID, True, NET_CHAN_CHAT);
811 end;
813 if Mode = NET_CHAT_SYSTEM then
814 Exit;
816 if ID = NET_EVERYONE then
817 begin
818 if Mode = NET_CHAT_PLAYER then
819 begin
820 g_Console_Add(Txt, True);
821 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
822 g_Sound_PlayEx('SOUND_GAME_RADIO');
823 end
824 else
825 if Mode = NET_CHAT_TEAM then
826 if gPlayer1 <> nil then
827 begin
828 if (gPlayer1.Team = TEAM_RED) and (Team = TEAM_RED) then
829 begin
830 g_Console_Add(#18'[Team] '#2 + Txt, True);
831 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
832 g_Sound_PlayEx('SOUND_GAME_RADIO');
833 end
834 else if (gPlayer1.Team = TEAM_BLUE) and (Team = TEAM_BLUE) then
835 begin
836 g_Console_Add(#20'[Team] '#2 + Txt, True);
837 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
838 g_Sound_PlayEx('SOUND_GAME_RADIO');
839 end;
840 end;
841 end
842 else
843 begin
844 Name := g_Net_ClientName_ByID(ID);
845 g_Console_Add('-> ' + Name + ': ' + Txt, True);
846 e_WriteLog('[Tell ' + Name + '] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
847 g_Sound_PlayEx('SOUND_GAME_RADIO');
848 end;
849 end;
851 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
852 begin
853 e_Buffer_Write(@NetOut, Byte(NET_MSG_GFX));
854 e_Buffer_Write(@NetOut, Kind);
855 e_Buffer_Write(@NetOut, X);
856 e_Buffer_Write(@NetOut, Y);
857 e_Buffer_Write(@NetOut, Ang);
859 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
860 end;
862 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
863 begin
864 e_Buffer_Write(@NetOut, Byte(NET_MSG_SND));
865 e_Buffer_Write(@NetOut, Name);
866 if Pos then
867 begin
868 e_Buffer_Write(@NetOut, Byte(1));
869 e_Buffer_Write(@NetOut, X);
870 e_Buffer_Write(@NetOut, Y);
871 end
872 else
873 e_Buffer_Write(@NetOut, Byte(0));
875 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
876 end;
878 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
879 begin
880 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
882 e_Buffer_Write(@NetOut, Byte(NET_MSG_SHADD));
883 e_Buffer_Write(@NetOut, Proj);
884 e_Buffer_Write(@NetOut, Shots[Proj].ShotType);
885 e_Buffer_Write(@NetOut, Shots[Proj].Target);
886 e_Buffer_Write(@NetOut, Shots[Proj].SpawnerUID);
887 e_Buffer_Write(@NetOut, Shots[Proj].Timeout);
888 e_Buffer_Write(@NetOut, Shots[Proj].Obj.X);
889 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Y);
890 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.X);
891 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.Y);
893 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
894 end;
896 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
897 begin
898 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
900 e_Buffer_Write(@NetOut, Byte(NET_MSG_SHPOS));
901 e_Buffer_Write(@NetOut, Proj);
902 e_Buffer_Write(@NetOut, Shots[Proj].Obj.X);
903 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Y);
904 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.X);
905 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.Y);
907 g_Net_Host_Send(ID, False, NET_CHAN_SHOTS);
908 end;
910 procedure MH_Send_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
911 begin
912 e_Buffer_Write(@NetOut, Byte(NET_MSG_SHDEL));
913 e_Buffer_Write(@NetOut, Proj);
914 e_Buffer_Write(@NetOut, Byte(Loud));
915 e_Buffer_Write(@NetOut, X);
916 e_Buffer_Write(@NetOut, Y);
918 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
919 end;
921 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
922 begin
923 e_Buffer_Write(@NetOut, Byte(NET_MSG_SCORE));
924 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
925 begin
926 e_Buffer_Write(@NetOut, gTeamStat[TEAM_RED].Goals);
927 e_Buffer_Write(@NetOut, gTeamStat[TEAM_BLUE].Goals);
928 end
929 else
930 if gGameSettings.GameMode = GM_COOP then
931 begin
932 e_Buffer_Write(@NetOut, gCoopMonstersKilled);
933 e_Buffer_Write(@NetOut, gCoopSecretsFound);
934 end;
936 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
937 end;
939 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
940 begin
941 e_Buffer_Write(@NetOut, Byte(NET_MSG_COOP));
942 e_Buffer_Write(@NetOut, gTotalMonsters);
943 e_Buffer_Write(@NetOut, gSecretsCount);
944 e_Buffer_Write(@NetOut, gCoopTotalMonstersKilled);
945 e_Buffer_Write(@NetOut, gCoopTotalSecretsFound);
946 e_Buffer_Write(@NetOut, gCoopTotalMonsters);
947 e_Buffer_Write(@NetOut, gCoopTotalSecrets);
948 end;
950 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
951 begin
952 e_Buffer_Write(@NetOut, Byte(NET_MSG_GEVENT));
953 e_Buffer_Write(@NetOut, EvType);
954 e_Buffer_Write(@NetOut, EvNum);
955 e_Buffer_Write(@NetOut, EvStr);
956 e_Buffer_Write(@NetOut, Byte(gLastMap));
957 e_Buffer_Write(@NetOut, gTime);
958 if (EvType = NET_EV_MAPSTART) and (Pos(':\', EvStr) > 0) then
959 begin
960 e_Buffer_Write(@NetOut, Byte(1));
961 e_Buffer_Write(@NetOut, gWADHash);
962 end else
963 e_Buffer_Write(@NetOut, Byte(0));
965 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
966 end;
968 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
969 begin
970 e_Buffer_Write(@NetOut, Byte(NET_MSG_FLAG));
971 e_Buffer_Write(@NetOut, EvType);
972 e_Buffer_Write(@NetOut, Flag);
973 e_Buffer_Write(@NetOut, Byte(Quiet));
974 e_Buffer_Write(@NetOut, PID);
975 e_Buffer_Write(@NetOut, gFlags[Flag].State);
976 e_Buffer_Write(@NetOut, gFlags[Flag].CaptureTime);
977 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.X);
978 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.Y);
979 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.Vel.X);
980 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.Vel.Y);
982 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
983 end;
985 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
986 begin
987 e_Buffer_Write(@NetOut, Byte(NET_MSG_GSET));
988 e_Buffer_Write(@NetOut, gGameSettings.GameMode);
989 e_Buffer_Write(@NetOut, gGameSettings.GoalLimit);
990 e_Buffer_Write(@NetOut, gGameSettings.TimeLimit);
991 e_Buffer_Write(@NetOut, gGameSettings.MaxLives);
992 e_Buffer_Write(@NetOut, gGameSettings.Options);
994 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
995 end;
997 // PLAYER (SEND)
999 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
1000 var
1001 P: TPlayer;
1002 begin
1003 P := g_Player_Get(PID);
1004 if P = nil then Exit;
1006 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLR));
1007 e_Buffer_Write(@NetOut, PID);
1008 e_Buffer_Write(@NetOut, P.Name);
1010 e_Buffer_Write(@NetOut, P.FActualModelName);
1011 e_Buffer_Write(@NetOut, P.FColor.R);
1012 e_Buffer_Write(@NetOut, P.FColor.G);
1013 e_Buffer_Write(@NetOut, P.FColor.B);
1014 e_Buffer_Write(@NetOut, P.Team);
1016 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT)
1017 end;
1019 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
1020 var
1021 kByte: Word;
1022 Pl: TPlayer;
1023 begin
1024 Pl := g_Player_Get(PID);
1025 if Pl = nil then Exit;
1026 if Pl.FDummy then Exit;
1028 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRPOS));
1029 e_Buffer_Write(@NetOut, gTime);
1030 e_Buffer_Write(@NetOut, PID);
1032 kByte := 0;
1034 with Pl do
1035 begin
1036 e_Buffer_Write(@NetOut, FPing);
1037 e_Buffer_Write(@NetOut, FLoss);
1038 if IsKeyPressed(KEY_CHAT) then
1039 kByte := NET_KEY_CHAT
1040 else
1041 begin
1042 if IsKeyPressed(KEY_LEFT) then kByte := kByte or NET_KEY_LEFT;
1043 if IsKeyPressed(KEY_RIGHT) then kByte := kByte or NET_KEY_RIGHT;
1044 if IsKeyPressed(KEY_UP) then kByte := kByte or NET_KEY_UP;
1045 if IsKeyPressed(KEY_DOWN) then kByte := kByte or NET_KEY_DOWN;
1046 if IsKeyPressed(KEY_JUMP) then kByte := kByte or NET_KEY_JUMP;
1047 if JustTeleported then kByte := kByte or NET_KEY_FORCEDIR;
1048 end;
1050 e_Buffer_Write(@NetOut, kByte);
1051 if Direction = D_LEFT then e_Buffer_Write(@NetOut, Byte(0)) else e_Buffer_Write(@NetOut, Byte(1));
1052 e_Buffer_Write(@NetOut, GameX);
1053 e_Buffer_Write(@NetOut, GameY);
1054 e_Buffer_Write(@NetOut, GameVelX);
1055 e_Buffer_Write(@NetOut, GameVelY);
1056 e_Buffer_Write(@NetOut, GameAccelX);
1057 e_Buffer_Write(@NetOut, GameAccelY);
1058 end;
1060 g_Net_Host_Send(ID, Reliable, NET_CHAN_PLAYERPOS);
1061 end;
1063 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
1064 var
1065 P: TPlayer;
1066 I: Integer;
1067 begin
1068 P := g_Player_Get(PID);
1069 if P = nil then Exit;
1071 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRSTA));
1072 e_Buffer_Write(@NetOut, PID);
1074 with P do
1075 begin
1076 e_Buffer_Write(@NetOut, Byte(Live));
1077 e_Buffer_Write(@NetOut, Byte(GodMode));
1078 e_Buffer_Write(@NetOut, Health);
1079 e_Buffer_Write(@NetOut, Armor);
1080 e_Buffer_Write(@NetOut, Air);
1081 e_Buffer_Write(@NetOut, JetFuel);
1082 e_Buffer_Write(@NetOut, Lives);
1083 e_Buffer_Write(@NetOut, Team);
1085 for I := WP_FIRST to WP_LAST do
1086 e_Buffer_Write(@NetOut, Byte(FWeapon[I]));
1088 for I := A_BULLETS to A_HIGH do
1089 e_Buffer_Write(@NetOut, FAmmo[I]);
1091 for I := A_BULLETS to A_HIGH do
1092 e_Buffer_Write(@NetOut, FMaxAmmo[I]);
1094 for I := MR_SUIT to MR_MAX do
1095 e_Buffer_Write(@NetOut, LongWord(FMegaRulez[I]));
1097 e_Buffer_Write(@NetOut, Byte(R_ITEM_BACKPACK in FRulez));
1098 e_Buffer_Write(@NetOut, Byte(R_KEY_RED in FRulez));
1099 e_Buffer_Write(@NetOut, Byte(R_KEY_GREEN in FRulez));
1100 e_Buffer_Write(@NetOut, Byte(R_KEY_BLUE in FRulez));
1101 e_Buffer_Write(@NetOut, Byte(R_BERSERK in FRulez));
1103 e_Buffer_Write(@NetOut, Frags);
1104 e_Buffer_Write(@NetOut, Death);
1106 e_Buffer_Write(@NetOut, CurrWeap);
1108 e_Buffer_Write(@NetOut, Byte(FSpectator));
1109 e_Buffer_Write(@NetOut, Byte(FGhost));
1110 e_Buffer_Write(@NetOut, Byte(FPhysics));
1111 e_Buffer_Write(@NetOut, Byte(FNoRespawn));
1112 e_Buffer_Write(@NetOut, Byte(FJetpack));
1113 e_Buffer_Write(@NetOut, FFireTime);
1114 end;
1116 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1117 end;
1119 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
1120 begin
1121 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRDMG));
1122 e_Buffer_Write(@NetOut, PID);
1123 e_Buffer_Write(@NetOut, Kind);
1124 e_Buffer_Write(@NetOut, Attacker);
1125 e_Buffer_Write(@NetOut, Value);
1126 e_Buffer_Write(@NetOut, VX);
1127 e_Buffer_Write(@NetOut, VY);
1129 g_Net_Host_Send(ID, False, NET_CHAN_PLAYER);
1130 end;
1132 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
1133 begin
1134 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRDIE));
1135 e_Buffer_Write(@NetOut, PID);
1136 e_Buffer_Write(@NetOut, KillType);
1137 e_Buffer_Write(@NetOut, DeathType);
1138 e_Buffer_Write(@NetOut, Attacker);
1140 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1141 end;
1143 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
1144 begin
1145 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRFIRE));
1146 e_Buffer_Write(@NetOut, PID);
1147 e_Buffer_Write(@NetOut, Weapon);
1148 e_Buffer_Write(@NetOut, X);
1149 e_Buffer_Write(@NetOut, Y);
1150 e_Buffer_Write(@NetOut, AX);
1151 e_Buffer_Write(@NetOut, AY);
1152 e_Buffer_Write(@NetOut, ShotID);
1154 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
1155 end;
1157 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
1158 begin
1159 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRDEL));
1160 e_Buffer_Write(@NetOut, PID);
1162 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1163 end;
1165 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
1166 var
1167 Pl: TPlayer;
1168 begin
1169 Pl := g_Player_Get(PID);
1170 if Pl = nil then Exit;
1172 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRSET));
1173 e_Buffer_Write(@NetOut, PID);
1174 e_Buffer_Write(@NetOut, Pl.Name);
1175 if Mdl = '' then
1176 e_Buffer_Write(@NetOut, Pl.Model.Name)
1177 else
1178 e_Buffer_Write(@NetOut, Mdl);
1179 e_Buffer_Write(@NetOut, Pl.FColor.R);
1180 e_Buffer_Write(@NetOut, Pl.FColor.G);
1181 e_Buffer_Write(@NetOut, Pl.FColor.B);
1182 e_Buffer_Write(@NetOut, Pl.Team);
1184 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1185 end;
1187 // ITEM (SEND)
1189 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1190 var
1191 it: PItem;
1192 begin
1193 it := g_ItemByIdx(IID);
1195 e_Buffer_Write(@NetOut, Byte(NET_MSG_ISPAWN));
1196 e_Buffer_Write(@NetOut, IID);
1197 e_Buffer_Write(@NetOut, Byte(Quiet));
1198 e_Buffer_Write(@NetOut, it.ItemType);
1199 e_Buffer_Write(@NetOut, Byte(it.Fall));
1200 e_Buffer_Write(@NetOut, Byte(it.Respawnable));
1201 e_Buffer_Write(@NetOut, it.Obj.X);
1202 e_Buffer_Write(@NetOut, it.Obj.Y);
1203 e_Buffer_Write(@NetOut, it.Obj.Vel.X);
1204 e_Buffer_Write(@NetOut, it.Obj.Vel.Y);
1206 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1207 end;
1209 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1210 begin
1211 e_Buffer_Write(@NetOut, Byte(NET_MSG_IDEL));
1212 e_Buffer_Write(@NetOut, IID);
1213 e_Buffer_Write(@NetOut, Byte(Quiet));
1215 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1216 end;
1218 // PANEL
1220 procedure MH_SEND_PanelTexture(PType: Word; PID: LongWord; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
1221 var
1222 TP: TPanel;
1223 begin
1224 case PType of
1225 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
1226 TP := gWalls[PID];
1227 PANEL_FORE:
1228 TP := gRenderForegrounds[PID];
1229 PANEL_BACK:
1230 TP := gRenderBackgrounds[PID];
1231 PANEL_WATER:
1232 TP := gWater[PID];
1233 PANEL_ACID1:
1234 TP := gAcid1[PID];
1235 PANEL_ACID2:
1236 TP := gAcid2[PID];
1237 PANEL_STEP:
1238 TP := gSteps[PID];
1239 else
1240 Exit;
1241 end;
1243 with TP do
1244 begin
1245 e_Buffer_Write(@NetOut, Byte(NET_MSG_PTEX));
1246 e_Buffer_Write(@NetOut, PType);
1247 e_Buffer_Write(@NetOut, PID);
1248 e_Buffer_Write(@NetOut, FCurTexture);
1249 e_Buffer_Write(@NetOut, FCurFrame);
1250 e_Buffer_Write(@NetOut, FCurFrameCount);
1251 e_Buffer_Write(@NetOut, AnimLoop);
1252 end;
1254 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1255 end;
1257 procedure MH_SEND_PanelState(PType: Word; PID: LongWord; ID: Integer = NET_EVERYONE);
1258 var
1259 TP: TPanel;
1260 begin
1261 case PType of
1262 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
1263 TP := gWalls[PID];
1264 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
1265 TP := gLifts[PID];
1266 PANEL_BACK:
1267 begin
1268 TP := gRenderBackgrounds[PID];
1269 TP.Moved := True;
1270 end;
1271 PANEL_FORE:
1272 begin
1273 TP := gRenderForegrounds[PID];
1274 TP.Moved := True;
1275 end;
1276 else
1277 Exit;
1278 end;
1280 e_Buffer_Write(@NetOut, Byte(NET_MSG_PSTATE));
1281 e_Buffer_Write(@NetOut, PType);
1282 e_Buffer_Write(@NetOut, PID);
1283 e_Buffer_Write(@NetOut, Byte(TP.Enabled));
1284 e_Buffer_Write(@NetOut, TP.LiftType);
1285 e_Buffer_Write(@NetOut, TP.X);
1286 e_Buffer_Write(@NetOut, TP.Y);
1288 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1289 end;
1291 // TRIGGER
1293 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
1294 begin
1295 if gTriggers = nil then Exit;
1296 if T.Sound = nil then Exit;
1298 e_Buffer_Write(@NetOut, Byte(NET_MSG_TSOUND));
1299 e_Buffer_Write(@NetOut, T.ClientID);
1300 e_Buffer_Write(@NetOut, Byte(T.Sound.IsPlaying));
1301 e_Buffer_Write(@NetOut, LongWord(T.Sound.GetPosition));
1302 e_Buffer_Write(@NetOut, T.SoundPlayCount);
1304 g_Net_Host_Send(ID, True);
1305 end;
1307 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
1308 begin
1309 e_Buffer_Write(@NetOut, Byte(NET_MSG_TMUSIC));
1310 e_Buffer_Write(@NetOut, gMusic.Name);
1311 e_Buffer_Write(@NetOut, Byte(gMusic.IsPlaying));
1312 e_Buffer_Write(@NetOut, LongWord(gMusic.GetPosition));
1313 e_Buffer_Write(@NetOut, Byte(gMusic.SpecPause or gMusic.IsPaused));
1315 g_Net_Host_Send(ID, True);
1316 end;
1318 // MONSTER
1320 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
1321 var
1322 M: TMonster;
1323 begin
1324 M := g_Monsters_Get(UID);
1325 if M = nil then
1326 Exit;
1328 with M do
1329 begin
1330 e_Buffer_Write(@NetOut, Byte(NET_MSG_MSPAWN));
1331 e_Buffer_Write(@NetOut, UID);
1332 e_Buffer_Write(@NetOut, MonsterType);
1333 e_Buffer_Write(@NetOut, MonsterState);
1334 e_Buffer_Write(@NetOut, MonsterAnim);
1335 e_Buffer_Write(@NetOut, MonsterTargetUID);
1336 e_Buffer_Write(@NetOut, MonsterTargetTime);
1337 e_Buffer_Write(@NetOut, MonsterBehaviour);
1338 e_Buffer_Write(@NetOut, MonsterSleep);
1339 e_Buffer_Write(@NetOut, MonsterHealth);
1340 e_Buffer_Write(@NetOut, MonsterAmmo);
1341 e_Buffer_Write(@NetOut, GameX);
1342 e_Buffer_Write(@NetOut, GameY);
1343 e_Buffer_Write(@NetOut, GameVelX);
1344 e_Buffer_Write(@NetOut, GameVelY);
1345 e_Buffer_Write(@NetOut, Byte(GameDirection));
1346 end;
1348 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1349 end;
1351 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
1352 var
1353 M: TMonster;
1354 begin
1355 M := g_Monsters_Get(UID);
1356 if M = nil then Exit;
1358 e_Buffer_Write(@NetOut, Byte(NET_MSG_MPOS));
1359 e_Buffer_Write(@NetOut, UID);
1361 with M do
1362 begin
1363 e_Buffer_Write(@NetOut, GameX);
1364 e_Buffer_Write(@NetOut, GameY);
1365 e_Buffer_Write(@NetOut, GameVelX);
1366 e_Buffer_Write(@NetOut, GameVelY);
1367 e_Buffer_Write(@NetOut, Byte(GameDirection));
1368 end;
1370 g_Net_Host_Send(ID, False, NET_CHAN_MONSTERPOS);
1371 end;
1373 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
1374 var
1375 M: TMonster;
1376 begin
1377 M := g_Monsters_Get(UID);
1378 if M = nil then Exit;
1380 e_Buffer_Write(@NetOut, Byte(NET_MSG_MSTATE));
1381 e_Buffer_Write(@NetOut, UID);
1383 with M do
1384 begin
1385 e_Buffer_Write(@NetOut, MonsterState);
1386 e_Buffer_Write(@NetOut, ForcedAnim);
1387 e_Buffer_Write(@NetOut, MonsterTargetUID);
1388 e_Buffer_Write(@NetOut, MonsterTargetTime);
1389 e_Buffer_Write(@NetOut, MonsterSleep);
1390 e_Buffer_Write(@NetOut, MonsterHealth);
1391 e_Buffer_Write(@NetOut, MonsterAmmo);
1392 e_Buffer_Write(@NetOut, MonsterPain);
1393 e_Buffer_Write(@NetOut, Byte(AnimIsReverse));
1394 e_Buffer_Write(@NetOut, FFireTime);
1395 end;
1397 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1398 end;
1400 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
1401 begin
1402 e_Buffer_Write(@NetOut, Byte(NET_MSG_MSHOT));
1403 e_Buffer_Write(@NetOut, UID);
1404 e_Buffer_Write(@NetOut, X);
1405 e_Buffer_Write(@NetOut, Y);
1406 e_Buffer_Write(@NetOut, VX);
1407 e_Buffer_Write(@NetOut, VY);
1409 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1410 end;
1412 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
1413 var
1414 M: TMonster;
1415 begin
1416 M := g_Monsters_Get(UID);
1417 if M = nil then Exit;
1419 e_Buffer_Write(@NetOut, Byte(NET_MSG_MDEL));
1420 e_Buffer_Write(@NetOut, UID);
1422 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1423 end;
1425 // MISC
1427 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
1428 begin
1429 e_Buffer_Write(@NetOut, Byte(NET_MSG_TIME_SYNC));
1430 e_Buffer_Write(@NetOut, Time);
1432 g_Net_Host_Send(ID, False, NET_CHAN_SERVICE);
1433 end;
1435 procedure MH_SEND_VoteEvent(EvType: Byte;
1436 StrArg1: string = 'a'; StrArg2: string = 'b';
1437 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
1438 ID: Integer = NET_EVERYONE);
1439 begin
1440 e_Buffer_Write(@NetOut, Byte(NET_MSG_VOTE_EVENT));
1441 e_Buffer_Write(@NetOut, EvType);
1442 e_Buffer_Write(@NetOut, IntArg1);
1443 e_Buffer_Write(@NetOut, IntArg2);
1444 e_Buffer_Write(@NetOut, StrArg1);
1445 e_Buffer_Write(@NetOut, StrArg2);
1447 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1448 end;
1450 // CLIENT MESSAGES //
1452 // GAME
1454 procedure MC_RECV_Chat(P: Pointer);
1455 var
1456 Txt: string;
1457 Mode: Byte;
1458 begin
1459 Txt := e_Raw_Read_String(P);
1460 Mode := e_Raw_Read_Byte(P);
1462 if Mode <> NET_CHAT_SYSTEM then
1463 begin
1464 if Mode = NET_CHAT_PLAYER then
1465 begin
1466 g_Console_Add(Txt, True);
1467 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
1468 g_Sound_PlayEx('SOUND_GAME_RADIO');
1469 end else
1470 if (Mode = NET_CHAT_TEAM) and (gPlayer1 <> nil) then
1471 begin
1472 if gPlayer1.Team = TEAM_RED then
1473 g_Console_Add(b_Text_Format('\r[Team] ') + Txt, True);
1474 if gPlayer1.Team = TEAM_BLUE then
1475 g_Console_Add(b_Text_Format('\b[Team] ') + Txt, True);
1476 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
1477 g_Sound_PlayEx('SOUND_GAME_RADIO');
1478 end;
1479 end else
1480 g_Console_Add(Txt, True);
1481 end;
1483 procedure MC_RECV_Effect(P: Pointer);
1484 var
1485 Kind: Byte;
1486 X, Y: Integer;
1487 Ang: SmallInt;
1488 Anim: TAnimation;
1489 ID: LongWord;
1490 begin
1491 if not gGameOn then Exit;
1492 Kind := e_Raw_Read_Byte(P);
1493 X := e_Raw_Read_LongInt(P);
1494 Y := e_Raw_Read_LongInt(P);
1495 Ang := e_Raw_Read_SmallInt(P);
1497 case Kind of
1498 NET_GFX_SPARK:
1499 g_GFX_Spark(X, Y, 2 + Random(2), Ang, 0, 0);
1501 NET_GFX_TELE:
1502 begin
1503 if g_Frames_Get(ID, 'FRAMES_TELEPORT') then
1504 begin
1505 Anim := TAnimation.Create(ID, False, 3);
1506 g_GFX_OnceAnim(X, Y, Anim);
1507 Anim.Free();
1508 end;
1509 if Ang = 1 then
1510 g_Sound_PlayExAt('SOUND_GAME_TELEPORT', X, Y);
1511 end;
1513 NET_GFX_EXPLODE:
1514 begin
1515 if g_Frames_Get(ID, 'FRAMES_EXPLODE_ROCKET') then
1516 begin
1517 Anim := TAnimation.Create(ID, False, 6);
1518 Anim.Blending := False;
1519 g_GFX_OnceAnim(X-64, Y-64, Anim);
1520 Anim.Free();
1521 end;
1522 if Ang = 1 then
1523 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEROCKET', X, Y);
1524 end;
1526 NET_GFX_BFGEXPL:
1527 begin
1528 if g_Frames_Get(ID, 'FRAMES_EXPLODE_BFG') then
1529 begin
1530 Anim := TAnimation.Create(ID, False, 6);
1531 Anim.Blending := False;
1532 g_GFX_OnceAnim(X-64, Y-64, Anim);
1533 Anim.Free();
1534 end;
1535 if Ang = 1 then
1536 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEBFG', X, Y);
1537 end;
1539 NET_GFX_BFGHIT:
1540 begin
1541 if g_Frames_Get(ID, 'FRAMES_BFGHIT') then
1542 begin
1543 Anim := TAnimation.Create(ID, False, 4);
1544 g_GFX_OnceAnim(X-32, Y-32, Anim);
1545 Anim.Free();
1546 end;
1547 end;
1549 NET_GFX_FIRE:
1550 begin
1551 if g_Frames_Get(ID, 'FRAMES_FIRE') then
1552 begin
1553 Anim := TAnimation.Create(ID, False, 4);
1554 g_GFX_OnceAnim(X, Y, Anim);
1555 Anim.Free();
1556 end;
1557 if Ang = 1 then
1558 g_Sound_PlayExAt('SOUND_FIRE', X, Y);
1559 end;
1561 NET_GFX_RESPAWN:
1562 begin
1563 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
1564 begin
1565 Anim := TAnimation.Create(ID, False, 4);
1566 g_GFX_OnceAnim(X, Y, Anim);
1567 Anim.Free();
1568 end;
1569 if Ang = 1 then
1570 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
1571 end;
1573 NET_GFX_SHELL1:
1574 g_Player_CreateShell(X, Y, 0, -2, SHELL_BULLET);
1576 NET_GFX_SHELL2:
1577 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1579 NET_GFX_SHELL3:
1580 begin
1581 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1582 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1583 end;
1584 end;
1585 end;
1587 procedure MC_RECV_Sound(P: Pointer);
1588 var
1589 Name: string;
1590 X, Y: Integer;
1591 Pos: Boolean;
1592 begin
1593 Name := e_Raw_Read_String(P);
1594 Pos := e_Raw_Read_Byte(P) <> 0;
1595 if Pos then
1596 begin
1597 X := e_Raw_Read_LongInt(P);
1598 Y := e_Raw_Read_LongInt(P);
1599 g_Sound_PlayExAt(Name, X, Y);
1600 end
1601 else
1602 g_Sound_PlayEx(Name);
1603 end;
1605 procedure MC_RECV_CreateShot(P: Pointer);
1606 var
1607 I, X, Y, XV, YV: Integer;
1608 Timeout: LongWord;
1609 Target, Spawner: Word;
1610 ShType: Byte;
1611 begin
1612 I := e_Raw_Read_LongInt(P);
1613 ShType := e_Raw_Read_Byte(P);
1614 Target := e_Raw_Read_Word(P);
1615 Spawner := e_Raw_Read_Word(P);
1616 Timeout := e_Raw_Read_LongWord(P);
1617 X := e_Raw_Read_LongInt(P);
1618 Y := e_Raw_Read_LongInt(P);
1619 XV := e_Raw_Read_LongInt(P);
1620 YV := e_Raw_Read_LongInt(P);
1622 I := g_Weapon_CreateShot(I, ShType, Spawner, Target, X, Y, XV, YV);
1623 if (Shots <> nil) and (I <= High(Shots)) then
1624 begin
1625 Shots[I].Timeout := Timeout;
1626 //Shots[I].Target := Target; // TODO: find a use for Target later
1627 end;
1628 end;
1630 procedure MC_RECV_UpdateShot(P: Pointer);
1631 var
1632 I, TX, TY, TXV, TYV: Integer;
1633 begin
1634 I := e_Raw_Read_LongInt(P);
1635 TX := e_Raw_Read_LongInt(P);
1636 TY := e_Raw_Read_LongInt(P);
1637 TXV := e_Raw_Read_LongInt(P);
1638 TYV := e_Raw_Read_LongInt(P);
1640 if (Shots <> nil) and (I <= High(Shots)) then
1641 with (Shots[i]) do
1642 begin
1643 Obj.X := TX;
1644 Obj.Y := TY;
1645 Obj.Vel.X := TXV;
1646 Obj.Vel.Y := TYV;
1647 end;
1648 end;
1650 procedure MC_RECV_DeleteShot(P: Pointer);
1651 var
1652 I, X, Y: Integer;
1653 L: Boolean;
1654 begin
1655 if not gGameOn then Exit;
1656 I := e_Raw_Read_LongInt(P);
1657 L := (e_Raw_Read_Byte(P) <> 0);
1658 X := e_Raw_Read_LongInt(P);
1659 Y := e_Raw_Read_LongInt(P);
1661 g_Weapon_DestroyShot(I, X, Y, L);
1662 end;
1664 procedure MC_RECV_GameStats(P: Pointer);
1665 begin
1666 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
1667 begin
1668 gTeamStat[TEAM_RED].Goals := e_Raw_Read_SmallInt(P);
1669 gTeamStat[TEAM_BLUE].Goals := e_Raw_Read_SmallInt(P);
1670 end
1671 else
1672 if gGameSettings.GameMode = GM_COOP then
1673 begin
1674 gCoopMonstersKilled := e_Raw_Read_Word(P);
1675 gCoopSecretsFound := e_Raw_Read_Word(P);
1676 end;
1677 end;
1679 procedure MC_RECV_CoopStats(P: Pointer);
1680 begin
1681 gTotalMonsters := e_Raw_Read_LongInt(P);
1682 gSecretsCount := e_Raw_Read_LongInt(P);
1683 gCoopTotalMonstersKilled := e_Raw_Read_Word(P);
1684 gCoopTotalSecretsFound := e_Raw_Read_Word(P);
1685 gCoopTotalMonsters := e_Raw_Read_Word(P);
1686 gCoopTotalSecrets := e_Raw_Read_Word(P);
1687 end;
1689 procedure MC_RECV_GameEvent(P: Pointer);
1690 var
1691 EvType: Byte;
1692 EvNum: Integer;
1693 EvStr: string;
1694 EvTime: LongWord;
1695 BHash: Boolean;
1696 EvHash: TMD5Digest;
1697 pl: TPlayer;
1698 i1, i2: TStrings_Locale;
1699 pln: String;
1700 cnt: Byte;
1701 begin
1702 FillChar(EvHash, Sizeof(EvHash), 0);
1703 EvType := e_Raw_Read_Byte(P);
1704 EvNum := e_Raw_Read_LongInt(P);
1705 EvStr := e_Raw_Read_String(P);
1706 gLastMap := e_Raw_Read_Byte(P) <> 0;
1707 if gLastMap and (gGameSettings.GameMode = GM_COOP) then gStatsOff := True;
1708 gStatsPressed := True;
1709 EvTime := e_Raw_Read_LongWord(P);
1710 BHash := e_Raw_Read_Byte(P) <> 0;
1711 if BHash then
1712 EvHash := e_Raw_Read_MD5(P);
1714 gTime := EvTime;
1716 case EvType of
1717 NET_EV_MAPSTART:
1718 begin
1719 gGameOn := False;
1720 g_Game_ClearLoading();
1721 g_Game_StopAllSounds(True);
1723 gSwitchGameMode := Byte(EvNum);
1724 gGameSettings.GameMode := gSwitchGameMode;
1726 gWADHash := EvHash;
1727 if not g_Game_StartMap(EvStr, True) then
1728 begin
1729 if Pos(':\', EvStr) = 0 then
1730 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [gGameSettings.WAD + ':\' + EvStr]))
1731 else
1732 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [EvStr]));
1733 Exit;
1734 end;
1736 MC_SEND_FullStateRequest;
1737 end;
1739 NET_EV_MAPEND:
1740 begin
1741 gMissionFailed := EvNum <> 0;
1742 gExit := EXIT_ENDLEVELCUSTOM;
1743 end;
1745 NET_EV_RCON:
1746 begin
1747 case EvNum of
1748 NET_RCON_NOAUTH:
1749 g_Console_Add(_lc[I_NET_RCON_NOAUTH], True);
1750 NET_RCON_PWGOOD:
1751 g_Console_Add(_lc[I_NET_RCON_PWD_VALID], True);
1752 NET_RCON_PWBAD:
1753 g_Console_Add(_lc[I_NET_RCON_PWD_INVALID], True);
1754 end;
1755 end;
1757 NET_EV_CHANGE_TEAM:
1758 begin
1759 if EvNum = TEAM_RED then
1760 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [EvStr]), True);
1761 if EvNum = TEAM_BLUE then
1762 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [EvStr]), True);
1763 end;
1765 NET_EV_PLAYER_KICK:
1766 g_Console_Add(Format(_lc[I_PLAYER_KICK], [EvStr]), True);
1768 NET_EV_PLAYER_BAN:
1769 g_Console_Add(Format(_lc[I_PLAYER_BAN], [EvStr]), True);
1771 NET_EV_LMS_WARMUP:
1772 g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [EvNum]), True);
1774 NET_EV_LMS_SURVIVOR:
1775 g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True);
1777 NET_EV_BIGTEXT:
1778 g_Game_Message(AnsiUpperCase(EvStr), Word(EvNum));
1780 NET_EV_SCORE:
1781 begin
1782 pl := g_Player_Get(EvNum and $FFFF);
1783 if pl = nil then
1784 pln := '?'
1785 else
1786 pln := pl.Name;
1787 cnt := (EvNum shr 16) and $FF;
1788 if Pos('w', EvStr) = 0 then
1789 begin
1790 // Default score
1791 if Pos('t', EvStr) = 0 then
1792 begin
1793 // Player +/- score
1794 if Pos('-', EvStr) = 0 then
1795 begin
1796 if Pos('e', EvStr) = 0 then
1797 i1 := I_PLAYER_SCORE_ADD_OWN
1798 else
1799 i1 := I_PLAYER_SCORE_ADD_ENEMY;
1800 end else
1801 begin
1802 if Pos('e', EvStr) = 0 then
1803 i1 := I_PLAYER_SCORE_SUB_OWN
1804 else
1805 i1 := I_PLAYER_SCORE_SUB_ENEMY;
1806 end;
1807 // Which team
1808 if Pos('r', EvStr) > 0 then
1809 i2 := I_PLAYER_SCORE_TO_RED
1810 else
1811 i2 := I_PLAYER_SCORE_TO_BLUE;
1812 g_Console_Add(Format(_lc[i1], [pln, cnt, _lc[i2]]), True);
1813 end else
1814 begin
1815 // Team +/- score
1816 if Pos('-', EvStr) = 0 then
1817 i1 := I_PLAYER_SCORE_ADD_TEAM
1818 else
1819 i1 := I_PLAYER_SCORE_SUB_TEAM;
1820 // Which team
1821 if Pos('r', EvStr) > 0 then
1822 i2 := I_PLAYER_SCORE_RED
1823 else
1824 i2 := I_PLAYER_SCORE_BLUE;
1825 g_Console_Add(Format(_lc[i1], [_lc[i2], cnt]), True);
1826 end;
1827 end else
1828 begin
1829 // Game Win
1830 if Pos('e', EvStr) = 0 then
1831 i1 := I_PLAYER_SCORE_WIN_OWN
1832 else
1833 i1 := I_PLAYER_SCORE_WIN_ENEMY;
1834 // Which team
1835 if Pos('r', EvStr) > 0 then
1836 i2 := I_PLAYER_SCORE_TO_RED
1837 else
1838 i2 := I_PLAYER_SCORE_TO_BLUE;
1839 g_Console_Add(Format(_lc[i1], [pln, _lc[i2]]), True);
1840 end;
1841 end;
1843 NET_EV_SCORE_MSG:
1844 begin
1845 if EvNum = TEAM_RED then
1846 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1847 if EvNum = TEAM_BLUE then
1848 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1849 if EvNum = -TEAM_RED then
1850 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1851 if EvNum = -TEAM_BLUE then
1852 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1853 end;
1855 NET_EV_LMS_START:
1856 begin
1857 g_Player_RemoveAllCorpses;
1858 g_Game_Message(_lc[I_MESSAGE_LMS_START], 144);
1859 end;
1861 NET_EV_LMS_WIN:
1862 g_Game_Message(Format(_lc[I_MESSAGE_LMS_WIN], [AnsiUpperCase(EvStr)]), 144);
1864 NET_EV_TLMS_WIN:
1865 begin
1866 if EvNum = TEAM_RED then
1867 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 144);
1868 if EvNum = TEAM_BLUE then
1869 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 144);
1870 end;
1872 NET_EV_LMS_LOSE:
1873 g_Game_Message(_lc[I_MESSAGE_LMS_LOSE], 144);
1875 NET_EV_LMS_DRAW:
1876 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
1878 NET_EV_KILLCOMBO:
1879 g_Game_Announce_KillCombo(EvNum);
1881 NET_EV_PLAYER_TOUCH:
1882 begin
1883 pl := g_Player_Get(EvNum);
1884 if pl <> nil then
1885 pl.Touch();
1886 end;
1888 end;
1889 end;
1891 procedure MC_RECV_FlagEvent(P: Pointer);
1892 var
1893 PID: Word;
1894 Pl: TPlayer;
1895 EvType: Byte;
1896 Fl: Byte;
1897 Quiet: Boolean;
1898 s, ts: string;
1899 begin
1900 EvType := e_Raw_Read_Byte(P);
1901 Fl := e_Raw_Read_Byte(P);
1903 if Fl = FLAG_NONE then Exit;
1905 Quiet := (e_Raw_Read_Byte(P) <> 0);
1906 PID := e_Raw_Read_Word(P);
1908 gFlags[Fl].State := e_Raw_Read_Byte(P);
1909 gFlags[Fl].CaptureTime := e_Raw_Read_LongWord(P);
1910 gFlags[Fl].Obj.X := e_Raw_Read_LongInt(P);
1911 gFlags[Fl].Obj.Y := e_Raw_Read_LongInt(P);
1912 gFlags[Fl].Obj.Vel.X := e_Raw_Read_LongInt(P);
1913 gFlags[Fl].Obj.Vel.Y := e_Raw_Read_LongInt(P);
1915 Pl := g_Player_Get(PID);
1916 if (Pl = nil) and
1917 (EvType <> FLAG_STATE_NORMAL) and
1918 (EvType <> FLAG_STATE_DROPPED) and
1919 (EvType <> FLAG_STATE_RETURNED) then
1920 Exit;
1922 case EvType of
1923 FLAG_STATE_NORMAL:
1924 begin
1925 if Quiet or (Pl = nil) then Exit;
1927 if Fl = FLAG_RED then
1928 s := _lc[I_PLAYER_FLAG_RED]
1929 else
1930 s := _lc[I_PLAYER_FLAG_BLUE];
1932 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1933 end;
1935 FLAG_STATE_CAPTURED:
1936 begin
1937 if (Pl <> nil) then Pl.SetFlag(Fl);
1939 if Quiet then Exit;
1941 if Fl = FLAG_RED then
1942 s := _lc[I_PLAYER_FLAG_RED]
1943 else
1944 s := _lc[I_PLAYER_FLAG_BLUE];
1946 g_Console_Add(Format(_lc[I_PLAYER_FLAG_GET], [Pl.Name, s]), True);
1947 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_GET], [AnsiUpperCase(s)]), 144);
1948 end;
1950 FLAG_STATE_DROPPED:
1951 begin
1952 if (Pl <> nil) then Pl.SetFlag(FLAG_NONE);
1954 if Quiet or (Pl = nil) then Exit;
1956 if Fl = FLAG_RED then
1957 s := _lc[I_PLAYER_FLAG_RED]
1958 else
1959 s := _lc[I_PLAYER_FLAG_BLUE];
1961 g_Console_Add(Format(_lc[I_PLAYER_FLAG_DROP], [Pl.Name, s]), True);
1962 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_DROP], [AnsiUpperCase(s)]), 144);
1963 end;
1965 FLAG_STATE_SCORED:
1966 begin
1967 g_Map_ResetFlag(FLAG_RED);
1968 g_Map_ResetFlag(FLAG_BLUE);
1969 if Quiet or (Pl = nil) then Exit;
1970 Pl.SetFlag(FLAG_NONE);
1972 if Fl = FLAG_RED then
1973 s := _lc[I_PLAYER_FLAG_RED]
1974 else
1975 s := _lc[I_PLAYER_FLAG_BLUE];
1977 ts := Format('%.4d', [gFlags[Fl].CaptureTime]);
1978 Insert('.', ts, Length(ts) + 1 - 3);
1979 g_Console_Add(Format(_lc[I_PLAYER_FLAG_CAPTURE], [Pl.Name, s, ts]), True);
1980 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_CAPTURE], [AnsiUpperCase(s)]), 144);
1981 end;
1983 FLAG_STATE_RETURNED:
1984 begin
1985 g_Map_ResetFlag(Fl);
1986 if Quiet then Exit;
1988 if Fl = FLAG_RED then
1989 s := _lc[I_PLAYER_FLAG_RED]
1990 else
1991 s := _lc[I_PLAYER_FLAG_BLUE];
1993 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1994 end;
1995 end;
1996 end;
1998 procedure MC_RECV_GameSettings(P: Pointer);
1999 begin
2000 gGameSettings.GameMode := e_Raw_Read_Byte(P);
2001 gGameSettings.GoalLimit := e_Raw_Read_Word(P);
2002 gGameSettings.TimeLimit := e_Raw_Read_Word(P);
2003 gGameSettings.MaxLives := e_Raw_Read_Byte(P);
2004 gGameSettings.Options := e_Raw_Read_LongWord(P);
2005 end;
2007 // PLAYER
2009 function MC_RECV_PlayerCreate(P: Pointer): Word;
2010 var
2011 PID, DID: Word;
2012 PName, Model: string;
2013 Color: TRGB;
2014 T: Byte;
2015 Pl: TPlayer;
2016 begin
2017 PID := e_Raw_Read_Word(P);
2018 Pl := g_Player_Get(PID);
2020 PName := e_Raw_Read_String(P);
2021 Model := e_Raw_Read_String(P);
2022 Color.R := e_Raw_Read_Byte(P);
2023 Color.G := e_Raw_Read_Byte(P);
2024 Color.B := e_Raw_Read_Byte(P);
2025 T := e_Raw_Read_Byte(P);
2027 Result := 0;
2028 if (PID <> NetPlrUID1) and (PID <> NetPlrUID2) then
2029 begin
2030 if (Pl <> nil) then Exit;
2031 DID := g_Player_Create(Model, Color, T, False);
2032 with g_Player_Get(DID) do
2033 begin
2034 UID := PID;
2035 Name := PName;
2036 Reset(True);
2037 end;
2038 end
2039 else
2040 begin
2041 if (PID = NetPlrUID1) and (gPlayer1 <> nil) then begin
2042 gPlayer1.UID := PID;
2043 gPlayer1.Model.SetColor(Color.R, Color.G, Color.B);
2044 gPlayer1.ChangeTeam(T);
2045 end;
2046 if (PID = NetPlrUID2) and (gPlayer2 <> nil) then begin
2047 gPlayer2.UID := PID;
2048 gPlayer2.Model.SetColor(Color.R, Color.G, Color.B);
2049 gPlayer2.ChangeTeam(T);
2050 end;
2051 end;
2053 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
2054 e_WriteLog('NET: Player ' + PName + ' [' + IntToStr(PID) + '] added.', MSG_NOTIFY);
2055 Result := PID;
2056 end;
2058 function MC_RECV_PlayerPos(P: Pointer): Word;
2059 var
2060 GT: LongWord;
2061 PID: Word;
2062 kByte: Word;
2063 Pl: TPlayer;
2064 Dir: Byte;
2065 TmpX, TmpY: Integer;
2066 begin
2067 Result := 0;
2069 GT := e_Raw_Read_LongWord(P);
2070 if GT < gTime - NET_MAX_DIFFTIME then
2071 begin
2072 gTime := GT;
2073 Exit;
2074 end;
2075 gTime := GT;
2077 PID := e_Raw_Read_Word(P);
2078 Pl := g_Player_Get(PID);
2080 if Pl = nil then Exit;
2082 Result := PID;
2084 with Pl do
2085 begin
2086 FPing := e_Raw_Read_Word(P);
2087 FLoss := e_Raw_Read_Byte(P);
2088 kByte := e_Raw_Read_Word(P);
2089 Dir := e_Raw_Read_Byte(P);
2091 TmpX := e_Raw_Read_LongInt(P);
2092 TmpY := e_Raw_Read_LongInt(P);
2094 ReleaseKeys;
2096 if (kByte = NET_KEY_CHAT) then
2097 PressKey(KEY_CHAT, 10000)
2098 else
2099 begin
2100 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
2101 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
2102 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
2103 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
2104 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
2105 end;
2107 if ((Pl <> gPlayer1) and (Pl <> gPlayer2)) or LongBool(kByte and NET_KEY_FORCEDIR) then
2108 SetDirection(TDirection(Dir));
2110 GameVelX := e_Raw_Read_LongInt(P);
2111 GameVelY := e_Raw_Read_LongInt(P);
2112 GameAccelX := e_Raw_Read_LongInt(P);
2113 GameAccelY := e_Raw_Read_LongInt(P);
2114 SetLerp(TmpX, TmpY);
2115 if NetForcePlayerUpdate then Update();
2116 end;
2117 end;
2119 function MC_RECV_PlayerStats(P: Pointer): Word;
2120 var
2121 PID: Word;
2122 Pl: TPlayer;
2123 I: Integer;
2124 OldJet: Boolean;
2125 NewTeam: Byte;
2126 begin
2127 PID := e_Raw_Read_Word(P);
2128 Pl := g_Player_Get(PID);
2129 Result := 0;
2130 if Pl = nil then
2131 Exit;
2133 with Pl do
2134 begin
2135 Live := (e_Raw_Read_Byte(P) <> 0);
2136 GodMode := (e_Raw_Read_Byte(P) <> 0);
2137 Health := e_Raw_Read_LongInt(P);
2138 Armor := e_Raw_Read_LongInt(P);
2139 Air := e_Raw_Read_LongInt(P);
2140 JetFuel := e_Raw_Read_LongInt(P);
2141 Lives := e_Raw_Read_Byte(P);
2142 NewTeam := e_Raw_Read_Byte(P);
2144 for I := WP_FIRST to WP_LAST do
2145 FWeapon[I] := (e_Raw_Read_Byte(P) <> 0);
2147 for I := A_BULLETS to A_HIGH do
2148 FAmmo[I] := e_Raw_Read_Word(P);
2150 for I := A_BULLETS to A_HIGH do
2151 FMaxAmmo[I] := e_Raw_Read_Word(P);
2153 for I := MR_SUIT to MR_MAX do
2154 FMegaRulez[I] := e_Raw_Read_LongWord(P);
2156 FRulez := [];
2157 if (e_Raw_Read_Byte(P) <> 0) then
2158 FRulez := FRulez + [R_ITEM_BACKPACK];
2159 if (e_Raw_Read_Byte(P) <> 0) then
2160 FRulez := FRulez + [R_KEY_RED];
2161 if (e_Raw_Read_Byte(P) <> 0) then
2162 FRulez := FRulez + [R_KEY_GREEN];
2163 if (e_Raw_Read_Byte(P) <> 0) then
2164 FRulez := FRulez + [R_KEY_BLUE];
2165 if (e_Raw_Read_Byte(P) <> 0) then
2166 FRulez := FRulez + [R_BERSERK];
2168 Frags := e_Raw_Read_LongInt(P);
2169 Death := e_Raw_Read_LongInt(P);
2171 SetWeapon(e_Raw_Read_Byte(P));
2173 FSpectator := e_Raw_Read_Byte(P) <> 0;
2174 if FSpectator then
2175 begin
2176 if Pl = gPlayer1 then
2177 begin
2178 gLMSPID1 := UID;
2179 gPlayer1 := nil;
2180 end;
2181 if Pl = gPlayer2 then
2182 begin
2183 gLMSPID2 := UID;
2184 gPlayer2 := nil;
2185 end;
2186 end
2187 else
2188 begin
2189 if (gPlayer1 = nil) and (gLMSPID1 > 0) then
2190 gPlayer1 := g_Player_Get(gLMSPID1);
2191 if (gPlayer2 = nil) and (gLMSPID2 > 0) then
2192 gPlayer2 := g_Player_Get(gLMSPID2);
2193 end;
2194 FGhost := e_Raw_Read_Byte(P) <> 0;
2195 FPhysics := e_Raw_Read_Byte(P) <> 0;
2196 FNoRespawn := e_Raw_Read_Byte(P) <> 0;
2197 OldJet := FJetpack;
2198 FJetpack := e_Raw_Read_Byte(P) <> 0;
2199 FFireTime := e_Raw_Read_LongInt(P);
2200 if OldJet and not FJetpack then
2201 JetpackOff
2202 else if not OldJet and FJetpack then
2203 JetpackOn;
2204 if Team <> NewTeam then
2205 Pl.ChangeTeam(NewTeam);
2206 end;
2208 Result := PID;
2209 end;
2211 function MC_RECV_PlayerDamage(P: Pointer): Word;
2212 var
2213 PID: Word;
2214 Pl: TPlayer;
2215 Kind: Byte;
2216 Attacker, Value: Word;
2217 VX, VY: Integer;
2218 begin
2219 Result := 0;
2220 if not gGameOn then Exit;
2221 PID := e_Raw_Read_Word(P);
2222 Pl := g_Player_Get(PID);
2223 if Pl = nil then Exit;
2225 Kind := e_Raw_Read_Byte(P);
2226 Attacker := e_Raw_Read_Word(P);
2227 Value := e_Raw_Read_Word(P);
2228 VX := e_Raw_Read_Word(P);
2229 VY := e_Raw_Read_Word(P);
2231 with Pl do
2232 Damage(Value, Attacker, VX, VY, Kind);
2234 Result := PID;
2235 end;
2237 function MC_RECV_PlayerDeath(P: Pointer): Word;
2238 var
2239 PID: Word;
2240 Pl: TPlayer;
2241 KillType, DeathType: Byte;
2242 Attacker: Word;
2243 begin
2244 Result := 0;
2245 if not gGameOn then Exit;
2246 PID := e_Raw_Read_Word(P);
2247 Pl := g_Player_Get(PID);
2248 if Pl = nil then Exit;
2250 KillType := e_Raw_Read_Byte(P);
2251 DeathType := e_Raw_Read_Byte(P);
2252 Attacker := e_Raw_Read_Word(P);
2254 with Pl do
2255 begin
2256 Kill(KillType, Attacker, DeathType);
2257 SoftReset;
2258 end;
2259 end;
2261 function MC_RECV_PlayerDelete(P: Pointer): Word;
2262 var
2263 PID: Word;
2264 Pl: TPlayer;
2265 begin
2266 PID := e_Raw_Read_Word(P);
2267 Pl := g_Player_Get(PID);
2268 Result := 0;
2269 if Pl = nil then Exit;
2271 g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [Pl.Name]), True);
2272 e_WriteLog('NET: Player ' + Pl.Name + ' [' + IntToStr(PID) + '] removed.', MSG_NOTIFY);
2274 g_Player_Remove(PID);
2276 Result := PID;
2277 end;
2279 function MC_RECV_PlayerFire(P: Pointer): Word;
2280 var
2281 PID: Word;
2282 Weap: Byte;
2283 Pl: TPlayer;
2284 X, Y, AX, AY: Integer;
2285 SHID: Integer;
2286 begin
2287 Result := 0;
2288 if not gGameOn then Exit;
2289 PID := e_Raw_Read_Word(P);
2290 Pl := g_Player_Get(PID);
2291 if Pl = nil then Exit;
2293 Weap := e_Raw_Read_Byte(P);
2294 X := e_Raw_Read_LongInt(P);
2295 Y := e_Raw_Read_LongInt(P);
2296 AX := e_Raw_Read_LongInt(P);
2297 AY := e_Raw_Read_LongInt(P);
2298 SHID := e_Raw_Read_LongInt(P);
2300 with Pl do
2301 if Live then NetFire(Weap, X, Y, AX, AY, SHID);
2302 end;
2304 procedure MC_RECV_PlayerSettings(P: Pointer);
2305 var
2306 TmpName: string;
2307 TmpModel: string;
2308 TmpColor: TRGB;
2309 TmpTeam: Byte;
2310 Pl: TPlayer;
2311 PID: Word;
2312 begin
2313 PID := e_Raw_Read_Word(P);
2314 Pl := g_Player_Get(PID);
2315 if Pl = nil then Exit;
2317 TmpName := e_Raw_Read_String(P);
2318 TmpModel := e_Raw_Read_String(P);
2319 TmpColor.R := e_Raw_Read_Byte(P);
2320 TmpColor.G := e_Raw_Read_Byte(P);
2321 TmpColor.B := e_Raw_Read_Byte(P);
2322 TmpTeam := e_Raw_Read_Byte(P);
2324 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
2325 begin
2326 Pl.ChangeTeam(TmpTeam);
2327 if gPlayer1 = Pl then
2328 gPlayer1Settings.Team := TmpTeam;
2329 if gPlayer2 = Pl then
2330 gPlayer2Settings.Team := TmpTeam;
2331 end else
2332 Pl.SetColor(TmpColor);
2334 if Pl.Name <> TmpName then
2335 begin
2336 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
2337 Pl.Name := TmpName;
2338 end;
2340 if TmpModel <> Pl.Model.Name then
2341 Pl.SetModel(TmpModel);
2342 end;
2344 // ITEM
2346 procedure MC_RECV_ItemSpawn(P: Pointer);
2347 var
2348 ID: Word;
2349 AID: DWord;
2350 X, Y, VX, VY: Integer;
2351 T: Byte;
2352 Quiet, Fall{, Resp}: Boolean;
2353 Anim: TAnimation;
2354 it: PItem;
2355 begin
2356 if not gGameOn then Exit;
2357 ID := e_Raw_Read_Word(P);
2358 Quiet := e_Raw_Read_Byte(P) <> 0;
2359 T := e_Raw_Read_Byte(P);
2360 Fall := e_Raw_Read_Byte(P) <> 0;
2361 {Resp :=} e_Raw_Read_Byte(P);
2362 X := e_Raw_Read_LongInt(P);
2363 Y := e_Raw_Read_LongInt(P);
2364 VX := e_Raw_Read_LongInt(P);
2365 VY := e_Raw_Read_LongInt(P);
2367 g_Items_Create(X, Y, T, Fall, False, False, ID);
2369 it := g_ItemByIdx(ID);
2370 it.Obj.Vel.X := VX;
2371 it.Obj.Vel.Y := VY;
2373 if not Quiet then
2374 begin
2375 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
2376 if g_Frames_Get(AID, 'FRAMES_ITEM_RESPAWN') then
2377 begin
2378 Anim := TAnimation.Create(AID, False, 4);
2379 g_GFX_OnceAnim(X+(it.Obj.Rect.Width div 2)-16, Y+(it.Obj.Rect.Height div 2)-16, Anim);
2380 Anim.Free();
2381 end;
2382 end;
2383 end;
2385 procedure MC_RECV_ItemDestroy(P: Pointer);
2386 var
2387 ID: Word;
2388 Quiet: Boolean;
2389 begin
2390 if not gGameOn then Exit;
2391 ID := e_Raw_Read_Word(P);
2392 Quiet := e_Raw_Read_Byte(P) <> 0;
2394 if not g_ItemValidId(ID) then exit;
2396 if not Quiet then g_Item_EmitPickupSound(ID);
2398 g_Items_Remove(ID);
2399 end;
2401 // PANEL
2403 procedure MC_RECV_PanelTexture(P: Pointer);
2404 var
2405 TP: TPanel;
2406 PType: Word;
2407 ID: LongWord;
2408 Tex, Fr: Integer;
2409 Loop, Cnt: Byte;
2410 begin
2411 if not gGameOn then Exit;
2412 PType := e_Raw_Read_Word(P);
2413 ID := e_Raw_Read_LongWord(P);
2414 Tex := e_Raw_Read_LongInt(P);
2415 Fr := e_Raw_Read_LongInt(P);
2416 Cnt := e_Raw_Read_Byte(P);
2417 Loop := e_Raw_Read_Byte(P);
2419 TP := nil;
2421 case PType of
2422 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2423 if gWalls <> nil then
2424 TP := gWalls[ID];
2425 PANEL_FORE:
2426 if gRenderForegrounds <> nil then
2427 TP := gRenderForegrounds[ID];
2428 PANEL_BACK:
2429 if gRenderBackgrounds <> nil then
2430 TP := gRenderBackgrounds[ID];
2431 PANEL_WATER:
2432 if gWater <> nil then
2433 TP := gWater[ID];
2434 PANEL_ACID1:
2435 if gAcid1 <> nil then
2436 TP := gAcid1[ID];
2437 PANEL_ACID2:
2438 if gAcid2 <> nil then
2439 TP := gAcid2[ID];
2440 PANEL_STEP:
2441 if gSteps <> nil then
2442 TP := gSteps[ID];
2443 else
2444 Exit;
2445 end;
2447 if TP <> nil then
2448 if Loop = 0 then
2449 begin // switch texture
2450 TP.SetTexture(Tex, Loop);
2451 TP.SetFrame(Fr, Cnt);
2452 end else // looped or non-looped animation
2453 TP.NextTexture(Loop);
2454 end;
2456 procedure MC_RECV_PanelState(P: Pointer);
2457 var
2458 ID: LongWord;
2459 E: Boolean;
2460 Lift: Byte;
2461 PType: Word;
2462 X, Y: Integer;
2463 begin
2464 if not gGameOn then Exit;
2465 PType := e_Raw_Read_Word(P);
2466 ID := e_Raw_Read_LongWord(P);
2467 E := (e_Raw_Read_Byte(P) <> 0);
2468 Lift := e_Raw_Read_Byte(P);
2469 X := e_Raw_Read_LongInt(P);
2470 Y := e_Raw_Read_LongInt(P);
2472 case PType of
2473 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2474 if E then
2475 g_Map_EnableWall(ID)
2476 else
2477 g_Map_DisableWall(ID);
2479 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
2480 g_Map_SetLift(ID, Lift);
2482 PANEL_BACK:
2483 begin
2484 gRenderBackgrounds[ID].X := X;
2485 gRenderBackgrounds[ID].Y := Y;
2486 end;
2488 PANEL_FORE:
2489 begin
2490 gRenderForegrounds[ID].X := X;
2491 gRenderForegrounds[ID].Y := Y;
2492 end;
2493 end;
2494 end;
2496 // TRIGGERS
2498 procedure MC_RECV_TriggerSound(P: Pointer);
2499 var
2500 SPlaying: Boolean;
2501 SPos, SID: LongWord;
2502 SCount: LongInt;
2503 I: Integer;
2504 begin
2505 if not gGameOn then Exit;
2506 if gTriggers = nil then Exit;
2508 SID := e_Raw_Read_LongWord(P);
2509 SPlaying := e_Raw_Read_Byte(P) <> 0;
2510 SPos := e_Raw_Read_LongWord(P);
2511 SCount := e_Raw_Read_LongInt(P);
2513 for I := Low(gTriggers) to High(gTriggers) do
2514 if gTriggers[I].TriggerType = TRIGGER_SOUND then
2515 if gTriggers[I].ClientID = SID then
2516 with gTriggers[I] do
2517 begin
2518 if SPlaying then
2519 begin
2520 if Data.Local then
2521 Sound.PlayVolumeAt(X+(Width div 2), Y+(Height div 2), Data.Volume/255.0)
2522 else
2523 Sound.PlayPanVolume((Data.Pan-127.0)/128.0, Data.Volume/255.0);
2524 Sound.SetPosition(SPos);
2525 end
2526 else
2527 if Sound.IsPlaying then Sound.Stop;
2529 SoundPlayCount := SCount;
2530 end;
2531 end;
2533 procedure MC_RECV_TriggerMusic(P: Pointer);
2534 var
2535 MName: string;
2536 MPlaying: Boolean;
2537 MPos: LongWord;
2538 MPaused: Boolean;
2539 begin
2540 if not gGameOn then Exit;
2542 MName := e_Raw_Read_String(P);
2543 MPlaying := e_Raw_Read_Byte(P) <> 0;
2544 MPos := e_Raw_Read_LongWord(P);
2545 MPaused := e_Raw_Read_Byte(P) <> 0;
2547 if MPlaying then
2548 begin
2549 gMusic.SetByName(MName);
2550 gMusic.Play(True);
2551 gMusic.SetPosition(MPos);
2552 gMusic.SpecPause := MPaused;
2553 end
2554 else
2555 if gMusic.IsPlaying then gMusic.Stop;
2556 end;
2558 // MONSTERS
2560 procedure MC_RECV_MonsterSpawn(P: Pointer);
2561 var
2562 ID: Word;
2563 MType, MState, MDir, MAnim, MBehav: Byte;
2564 X, Y, VX, VY, MTargTime, MHealth, MAmmo, MSleep: Integer;
2565 MTarg: Word;
2566 M: TMonster;
2567 begin
2568 ID := e_Raw_Read_Word(P);
2569 M := g_Monsters_Get(ID);
2570 if M <> nil then
2571 Exit;
2573 MType := e_Raw_Read_Byte(P);
2574 MState := e_Raw_Read_Byte(P);
2575 MAnim := e_Raw_Read_Byte(P);
2576 MTarg := e_Raw_Read_Word(P);
2577 MTargTime := e_Raw_Read_LongInt(P);
2578 MBehav := e_Raw_Read_Byte(P);
2579 MSleep := e_Raw_Read_LongInt(P);
2580 MHealth := e_Raw_Read_LongInt(P);
2581 MAmmo := e_Raw_Read_LongInt(P);
2583 X := e_Raw_Read_LongInt(P);
2584 Y := e_Raw_Read_LongInt(P);
2585 VX := e_Raw_Read_LongInt(P);
2586 VY := e_Raw_Read_LongInt(P);
2587 MDir := e_Raw_Read_Byte(P);
2589 g_Monsters_Create(MType, X, Y, TDirection(MDir), False, ID);
2590 M := g_Monsters_Get(ID);
2591 if M = nil then
2592 Exit;
2594 with M do
2595 begin
2596 GameX := X;
2597 GameY := Y;
2598 GameVelX := VX;
2599 GameVelY := VY;
2601 MonsterAnim := MAnim;
2602 MonsterTargetUID := MTarg;
2603 MonsterTargetTime := MTargTime;
2604 MonsterBehaviour := MBehav;
2605 MonsterSleep := MSleep;
2606 MonsterAmmo := MAmmo;
2607 SetHealth(MHealth);
2609 SetState(MState);
2611 positionChanged(); // this updates spatial accelerators
2612 end;
2613 end;
2615 procedure MC_RECV_MonsterPos(P: Pointer);
2616 var
2617 M: TMonster;
2618 ID: Word;
2619 begin
2620 ID := e_Raw_Read_Word(P);
2621 M := g_Monsters_Get(ID);
2622 if M = nil then
2623 Exit;
2625 with M do
2626 begin
2627 GameX := e_Raw_Read_LongInt(P);
2628 GameY := e_Raw_Read_LongInt(P);
2629 GameVelX := e_Raw_Read_LongInt(P);
2630 GameVelY := e_Raw_Read_LongInt(P);
2631 GameDirection := TDirection(e_Raw_Read_Byte(P));
2632 positionChanged(); // this updates spatial accelerators
2633 end;
2634 end;
2636 procedure MC_RECV_MonsterState(P: Pointer);
2637 var
2638 ID: Integer;
2639 MState, MFAnm: Byte;
2640 M: TMonster;
2641 AnimRevert: Boolean;
2642 begin
2643 ID := e_Raw_Read_Word(P);
2644 M := g_Monsters_Get(ID);
2645 if M = nil then Exit;
2647 MState := e_Raw_Read_Byte(P);
2648 MFAnm := e_Raw_Read_Byte(P);
2650 with M do
2651 begin
2652 MonsterTargetUID := e_Raw_Read_Word(P);
2653 MonsterTargetTime := e_Raw_Read_LongInt(P);
2654 MonsterSleep := e_Raw_Read_LongInt(P);
2655 MonsterHealth := e_Raw_Read_LongInt(P);
2656 MonsterAmmo := e_Raw_Read_LongInt(P);
2657 MonsterPain := e_Raw_Read_LongInt(P);
2658 AnimRevert := e_Raw_Read_Byte(P) <> 0;
2659 FFireTime := e_Raw_Read_LongInt(P);
2660 RevertAnim(AnimRevert);
2662 if MonsterState <> MState then
2663 begin
2664 if (MState = MONSTATE_GO) and (MonsterState = MONSTATE_SLEEP) then
2665 WakeUpSound;
2666 if (MState = MONSTATE_DIE) then
2667 DieSound;
2668 if (MState = MONSTATE_PAIN) then
2669 MakeBloodSimple(Min(200, MonsterPain));
2670 if (MState = MONSTATE_ATTACK) then
2671 kick(nil);
2672 if (MState = MONSTATE_DEAD) then
2673 SetDeadAnim;
2675 SetState(MState, MFAnm);
2676 end;
2677 end;
2678 end;
2680 procedure MC_RECV_MonsterShot(P: Pointer);
2681 var
2682 ID: Integer;
2683 M: TMonster;
2684 X, Y, VX, VY: Integer;
2685 begin
2686 ID := e_Raw_Read_Word(P);
2688 M := g_Monsters_Get(ID);
2689 if M = nil then Exit;
2691 X := e_Raw_Read_LongInt(P);
2692 Y := e_Raw_Read_LongInt(P);
2693 VX := e_Raw_Read_LongInt(P);
2694 VY := e_Raw_Read_LongInt(P);
2696 M.ClientAttack(X, Y, VX, VY);
2697 end;
2699 procedure MC_RECV_MonsterDelete(P: Pointer);
2700 var
2701 ID: Integer;
2702 M, mon: TMonster;
2703 begin
2704 ID := e_Raw_Read_Word(P);
2705 M := g_Monsters_Get(ID);
2706 if M = nil then Exit;
2708 mon := g_Mons_ByIdx(ID);
2709 mon.SetState(5);
2710 mon.MonsterRemoved := True;
2711 end;
2713 procedure MC_RECV_TimeSync(P: Pointer);
2714 var
2715 Time: LongWord;
2716 begin
2717 Time := e_Raw_Read_LongWord(P);
2719 if gState = STATE_INTERCUSTOM then
2720 gServInterTime := Min(Time, 255);
2721 end;
2723 procedure MC_RECV_VoteEvent(P: Pointer);
2724 var
2725 EvID: Byte;
2726 Str1, Str2: string;
2727 Int1, Int2: SmallInt;
2728 begin
2729 EvID := e_Raw_Read_Byte(P);
2730 Int1 := e_Raw_Read_SmallInt(P);
2731 Int2 := e_Raw_Read_SmallInt(P);
2732 Str1 := e_Raw_Read_String(P);
2733 Str2 := e_Raw_Read_String(P);
2735 case EvID of
2736 NET_VE_STARTED:
2737 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_STARTED], [Str1, Str2, Int1]), True);
2738 NET_VE_PASSED:
2739 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_PASSED], [Str1]), True);
2740 NET_VE_FAILED:
2741 g_Console_Add(_lc[I_MESSAGE_VOTE_FAILED], True);
2742 NET_VE_VOTE:
2743 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Str1, Int1, Int2]), True);
2744 NET_VE_INPROGRESS:
2745 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_INPROGRESS], [Str1]), True);
2746 end;
2747 end;
2749 // CLIENT SEND
2751 procedure MC_SEND_Info(Password: string);
2752 begin
2753 e_Buffer_Clear(@NetOut);
2755 e_Buffer_Write(@NetOut, Byte(NET_MSG_INFO));
2756 e_Buffer_Write(@NetOut, GAME_VERSION);
2757 e_Buffer_Write(@NetOut, Password);
2758 e_Buffer_Write(@NetOut, gPlayer1Settings.Name);
2759 e_Buffer_Write(@NetOut, gPlayer1Settings.Model);
2760 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.R);
2761 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.G);
2762 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.B);
2763 e_Buffer_Write(@NetOut, gPlayer1Settings.Team);
2765 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2766 end;
2768 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
2769 begin
2770 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHAT));
2771 e_Buffer_Write(@NetOut, Txt);
2772 e_Buffer_Write(@NetOut, Mode);
2774 g_Net_Client_Send(True, NET_CHAN_CHAT);
2775 end;
2777 function isKeyPressed (key1: Word; key2: Word): Boolean;
2778 begin
2779 if (key1 <> 0) and e_KeyPressed(key1) then begin result := true; exit; end;
2780 if (key2 <> 0) and e_KeyPressed(key2) then begin result := true; exit; end;
2781 result := false;
2782 end;
2784 procedure MC_SEND_PlayerPos();
2785 var
2786 kByte: Word;
2787 Predict: Boolean;
2788 strafeDir: Byte;
2789 WeaponSelect: Word = 0;
2790 I: Integer;
2791 begin
2792 if not gGameOn then Exit;
2793 if gPlayers = nil then Exit;
2794 if gPlayer1 = nil then Exit;
2796 kByte := 0;
2797 Predict := NetPredictSelf; // and (not NetGotKeys);
2799 if (not gConsoleShow) and (not gChatShow) and (g_ActiveWindow = nil) then
2800 begin
2801 strafeDir := P1MoveButton shr 4;
2802 P1MoveButton := P1MoveButton and $0F;
2803 with gGameControls.P1Control do
2804 begin
2805 if isKeyPressed(KeyLeft, KeyLeft2) and (not isKeyPressed(KeyRight, KeyRight2)) then P1MoveButton := 1
2806 else if (not isKeyPressed(KeyLeft, KeyLeft2)) and isKeyPressed(KeyRight, KeyRight2) then P1MoveButton := 2
2807 else if (not isKeyPressed(KeyLeft, KeyLeft2)) and (not isKeyPressed(KeyRight, KeyRight2)) then P1MoveButton := 0;
2809 // strafing
2810 if isKeyPressed(KeyStrafe, KeyStrafe2) then
2811 begin
2812 // new strafe mechanics
2813 if (strafeDir = 0) then strafeDir := P1MoveButton; // start strafing
2814 // now set direction according to strafe (reversed)
2815 if (strafeDir = 2) then gPlayer1.SetDirection(D_LEFT)
2816 else if (strafeDir = 1) then gPlayer1.SetDirection(D_RIGHT);
2817 end
2818 else
2819 begin
2820 if (P1MoveButton = 2) and isKeyPressed(KeyLeft, KeyLeft2) then gPlayer1.SetDirection(D_LEFT)
2821 else if (P1MoveButton = 1) and isKeyPressed(KeyRight, KeyRight2) then gPlayer1.SetDirection(D_RIGHT)
2822 else if P1MoveButton <> 0 then gPlayer1.SetDirection(TDirection(P1MoveButton-1));
2823 end;
2825 gPlayer1.ReleaseKeys;
2826 if P1MoveButton = 1 then
2827 begin
2828 kByte := kByte or NET_KEY_LEFT;
2829 if Predict then gPlayer1.PressKey(KEY_LEFT, 10000);
2830 end;
2831 if P1MoveButton = 2 then
2832 begin
2833 kByte := kByte or NET_KEY_RIGHT;
2834 if Predict then gPlayer1.PressKey(KEY_RIGHT, 10000);
2835 end;
2836 if isKeyPressed(KeyUp, KeyUp2) then
2837 begin
2838 kByte := kByte or NET_KEY_UP;
2839 gPlayer1.PressKey(KEY_UP, 10000);
2840 end;
2841 if isKeyPressed(KeyDown, KeyDown2) then
2842 begin
2843 kByte := kByte or NET_KEY_DOWN;
2844 gPlayer1.PressKey(KEY_DOWN, 10000);
2845 end;
2846 if isKeyPressed(KeyJump, KeyJump2) then
2847 begin
2848 kByte := kByte or NET_KEY_JUMP;
2849 // gPlayer1.PressKey(KEY_JUMP, 10000); // TODO: Make a prediction option
2850 end;
2851 if isKeyPressed(KeyFire, KeyFire2) then kByte := kByte or NET_KEY_FIRE;
2852 if isKeyPressed(KeyOpen, KeyOpen2) then kByte := kByte or NET_KEY_OPEN;
2853 if isKeyPressed(KeyNextWeapon, KeyNextWeapon2) then kByte := kByte or NET_KEY_NW;
2854 if isKeyPressed(KeyPrevWeapon, KeyPrevWeapon2) then kByte := kByte or NET_KEY_PW;
2855 for I := 0 to High(KeyWeapon) do
2856 if isKeyPressed(KeyWeapon[I], KeyWeapon2[I]) then
2857 WeaponSelect := WeaponSelect or Word(1 shl I);
2858 end;
2859 // fix movebutton state
2860 P1MoveButton := P1MoveButton or (strafeDir shl 4);
2861 end
2862 else
2863 kByte := NET_KEY_CHAT;
2865 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRPOS));
2866 e_Buffer_Write(@NetOut, gTime);
2867 e_Buffer_Write(@NetOut, kByte);
2868 e_Buffer_Write(@NetOut, Byte(gPlayer1.Direction));
2869 e_Buffer_Write(@NetOut, WeaponSelect);
2870 //e_WriteLog(Format('S:ws=%d', [WeaponSelect]), MSG_WARNING);
2871 g_Net_Client_Send(True, NET_CHAN_PLAYERPOS);
2873 //kBytePrev := kByte;
2874 //kDirPrev := gPlayer1.Direction;
2875 end;
2877 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
2878 begin
2879 e_Buffer_Write(@NetOut, Byte(NET_MSG_VOTE_EVENT));
2880 e_Buffer_Write(@NetOut, Byte(Start));
2881 e_Buffer_Write(@NetOut, Command);
2882 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2883 end;
2885 procedure MC_SEND_PlayerSettings();
2886 begin
2887 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRSET));
2888 e_Buffer_Write(@NetOut, gPlayer1Settings.Name);
2889 e_Buffer_Write(@NetOut, gPlayer1Settings.Model);
2890 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.R);
2891 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.G);
2892 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.B);
2893 e_Buffer_Write(@NetOut, gPlayer1Settings.Team);
2895 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2896 end;
2898 procedure MC_SEND_FullStateRequest();
2899 begin
2900 e_Buffer_Write(@NetOut, Byte(NET_MSG_REQFST));
2902 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2903 end;
2905 procedure MC_SEND_CheatRequest(Kind: Byte);
2906 begin
2907 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHEAT));
2908 e_Buffer_Write(@NetOut, Kind);
2910 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2911 end;
2912 procedure MC_SEND_RCONPassword(Password: string);
2913 begin
2914 e_Buffer_Write(@NetOut, Byte(NET_MSG_RCON_AUTH));
2915 e_Buffer_Write(@NetOut, Password);
2917 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2918 end;
2919 procedure MC_SEND_RCONCommand(Cmd: string);
2920 begin
2921 e_Buffer_Write(@NetOut, Byte(NET_MSG_RCON_CMD));
2922 e_Buffer_Write(@NetOut, Cmd);
2924 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2925 end;
2927 // i have no idea why all this stuff is in here
2929 function ReadFile(const FileName: TFileName): AByte;
2930 var
2931 FileStream : TStream;
2932 fname: string;
2933 begin
2934 e_WriteLog(Format('NETWORK: looking for file "%s"', [FileName]), MSG_NOTIFY);
2935 fname := findDiskWad(FileName);
2936 if length(fname) = 0 then
2937 begin
2938 e_WriteLog(Format('NETWORK: file "%s" not found!', [FileName]), MSG_FATALERROR);
2939 SetLength(Result, 0);
2940 exit;
2941 end;
2942 e_WriteLog(Format('NETWORK: found file "%s"', [fname]), MSG_NOTIFY);
2943 Result := nil;
2944 FileStream := openDiskFileRO(fname);
2945 try
2946 if FileStream.Size > 0 then
2947 begin
2948 SetLength(Result, FileStream.Size);
2949 FileStream.Read(Result[0], FileStream.Size);
2950 end;
2951 finally
2952 FileStream.Free;
2953 end;
2954 end;
2956 function CreateMapDataMsg(const FileName: TFileName; ResList: TStringList): TMapDataMsg;
2957 var
2958 i: Integer;
2959 begin
2960 Result.MsgId := NET_MSG_MAP_RESPONSE;
2961 Result.FileData := ReadFile(FileName);
2962 Result.FileSize := Length(Result.FileData);
2964 SetLength(Result.ExternalResources, ResList.Count);
2965 for i:=0 to ResList.Count-1 do
2966 begin
2967 Result.ExternalResources[i].Name := ResList.Strings[i];
2968 Result.ExternalResources[i].md5 := MD5File(GameDir+'/wads/'+ResList.Strings[i]);
2969 end;
2970 end;
2972 procedure ResDataMsgToBytes(var bytes: AByte; const ResData: TResDataMsg);
2973 var
2974 ResultStream: TMemoryStream;
2975 begin
2976 ResultStream := TMemoryStream.Create;
2978 ResultStream.WriteBuffer(ResData.MsgId, SizeOf(ResData.MsgId)); //msgId
2979 ResultStream.WriteBuffer(ResData.FileSize, SizeOf(ResData.FileSize)); //file size
2980 ResultStream.WriteBuffer(ResData.FileData[0], ResData.FileSize); //file data
2982 SetLength(bytes, ResultStream.Size);
2983 ResultStream.Seek(0, soFromBeginning);
2984 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
2986 ResultStream.Free;
2987 end;
2989 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
2990 begin
2991 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
2992 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize));
2993 SetLength(Result.FileData, Result.FileSize);
2994 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize);
2995 end;
2997 procedure MapDataMsgToBytes(var bytes: AByte; const MapDataMsg: TMapDataMsg);
2998 var
2999 ResultStream: TMemoryStream;
3000 resCount: Integer;
3001 begin
3002 resCount := Length(MapDataMsg.ExternalResources);
3004 ResultStream := TMemoryStream.Create;
3006 ResultStream.WriteBuffer(MapDataMsg.MsgId, SizeOf(MapDataMsg.MsgId)); //msgId
3007 ResultStream.WriteBuffer(MapDataMsg.FileSize, SizeOf(MapDataMsg.FileSize)); //file size
3008 ResultStream.WriteBuffer(MapDataMsg.FileData[0], MapDataMsg.FileSize); //file data
3010 ResultStream.WriteBuffer(resCount, SizeOf(resCount)); //res count
3011 ResultStream.WriteBuffer(MapDataMsg.ExternalResources[0], resCount*SizeOf(TExternalResourceInfo)); //res data
3013 SetLength(bytes, ResultStream.Size);
3014 ResultStream.Seek(0, soFromBeginning);
3015 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
3017 ResultStream.Free;
3018 end;
3020 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
3021 var
3022 resCount: Integer;
3023 begin
3024 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
3025 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize)); //file size
3027 SetLength(Result.FileData, Result.FileSize);
3028 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize); //file data
3030 msgStream.ReadBuffer(resCount, SizeOf(resCount)); //res count
3031 SetLength(Result.ExternalResources, resCount);
3033 msgStream.ReadBuffer(Result.ExternalResources[0], resCount * SizeOf(TExternalResourceInfo)); //res data
3034 end;
3036 function IsValidFileName(const S: String): Boolean;
3037 const
3038 Forbidden: set of Char = ['<', '>', '|', '"', ':', '*', '?'];
3039 var
3040 I: Integer;
3041 begin
3042 Result := S <> '';
3043 for I := 1 to Length(S) do
3044 Result := Result and (not(S[I] in Forbidden));
3045 end;
3047 function IsValidFilePath(const S: String): Boolean;
3048 var
3049 I: Integer;
3050 begin
3051 Result := False;
3052 if not IsValidFileName(S) then exit;
3053 if FileExists(S) then exit;
3054 I := LastDelimiter('\/', S);
3055 if (I > 0) then
3056 if (not DirectoryExists(Copy(S, 1, I-1))) then
3057 exit;
3058 Result := True;
3059 end;
3061 procedure MC_SEND_MapRequest();
3062 begin
3063 e_Buffer_Write(@NetOut, Byte(NET_MSG_MAP_REQUEST));
3064 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3065 end;
3067 procedure MC_SEND_ResRequest(const resName: AnsiString);
3068 begin
3069 e_Buffer_Write(@NetOut, Byte(NET_MSG_RES_REQUEST));
3070 e_Buffer_Write(@NetOut, resName);
3071 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3072 end;
3074 procedure MH_RECV_MapRequest(C: pTNetClient; P: Pointer);
3075 var
3076 payload: AByte;
3077 peer: pENetPeer;
3078 mapDataMsg: TMapDataMsg;
3079 begin
3080 e_WriteLog('NET: Received map request from ' +
3081 DecodeIPV4(C.Peer.address.host), MSG_NOTIFY);
3083 mapDataMsg := CreateMapDataMsg(MapsDir + gGameSettings.WAD, gExternalResources);
3084 peer := NetClients[C.ID].Peer;
3086 MapDataMsgToBytes(payload, mapDataMsg);
3087 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3089 payload := nil;
3090 mapDataMsg.FileData := nil;
3091 mapDataMsg.ExternalResources := nil;
3092 end;
3094 procedure MH_RECV_ResRequest(C: pTNetClient; P: Pointer);
3095 var
3096 payload: AByte;
3097 peer: pENetPeer;
3098 FileName: String;
3099 resDataMsg: TResDataMsg;
3100 begin
3101 FileName := ExtractFileName(e_Raw_Read_String(P));
3102 e_WriteLog('NET: Received res request: ' + FileName +
3103 ' from ' + DecodeIPV4(C.Peer.address.host), MSG_NOTIFY);
3105 if not IsValidFilePath(FileName) then
3106 begin
3107 e_WriteLog('Invalid filename: ' + FileName, MSG_WARNING);
3108 exit;
3109 end;
3111 peer := NetClients[C.ID].Peer;
3113 if gExternalResources.IndexOf(FileName) > -1 then
3114 begin
3115 resDataMsg.MsgId := NET_MSG_RES_RESPONSE;
3116 resDataMsg.FileData := ReadFile(GameDir+'/wads/'+FileName);
3117 resDataMsg.FileSize := Length(resDataMsg.FileData);
3119 ResDataMsgToBytes(payload, resDataMsg);
3120 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3121 end;
3122 end;
3124 end.