DEADSOFTWARE

no more global `gItems[]` array; created DynTree for items (not used yet); also,...
[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 var
638 I: Integer;
639 begin
640 if gPlayers <> nil then
641 begin
642 for I := Low(gPlayers) to High(gPlayers) do
643 begin
644 if gPlayers[I] <> nil then
645 begin
646 if CreatePlayers then MH_SEND_PlayerCreate(gPlayers[I].UID, ID);
647 MH_SEND_PlayerPos(True, gPlayers[I].UID, ID);
648 MH_SEND_PlayerStats(gPlayers[I].UID, ID);
650 if (gPlayers[I].Flag <> FLAG_NONE) and (gGameSettings.GameMode = GM_CTF) then
651 MH_SEND_FlagEvent(FLAG_STATE_CAPTURED, gPlayers[I].Flag, gPlayers[I].UID, True, ID);
652 end;
653 end;
654 end;
656 g_Items_ForEachAlive(sendItemRespawn, true); // backwards
658 if gMonsters <> nil then
659 for I := 0 to High(gMonsters) do
660 if gMonsters[I] <> nil then
661 MH_SEND_MonsterSpawn(gMonsters[I].UID, ID);
663 if gWalls <> nil then
664 for I := Low(gWalls) to High(gWalls) do
665 if gWalls[I] <> nil then
666 with gWalls[I] do
667 begin
668 if Door then
669 MH_SEND_PanelState(PanelType, I, ID);
671 if GetTextureCount > 1 then
672 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
673 end;
675 if gLifts <> nil then
676 for I := Low(gLifts) to High(gLifts) do
677 if gLifts[I] <> nil then
678 with gLifts[I] do
679 MH_SEND_PanelState(PanelType, I, ID);
681 if gRenderForegrounds <> nil then
682 for I := Low(gRenderForegrounds) to High(gRenderForegrounds) do
683 if gRenderForegrounds[I] <> nil then
684 with gRenderForegrounds[I] do
685 begin
686 if (GetTextureCount > 1) then
687 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
688 if Moved then
689 MH_SEND_PanelState(PanelType, I, ID);
690 end;
691 if gRenderBackgrounds <> nil then
692 for I := Low(gRenderBackgrounds) to High(gRenderBackgrounds) do
693 if gRenderBackgrounds[I] <> nil then
694 with gRenderBackgrounds[I] do
695 begin
696 if (GetTextureCount > 1) then
697 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
698 if Moved then
699 MH_SEND_PanelState(PanelType, I, ID);
700 end;
701 if gWater <> nil then
702 for I := Low(gWater) to High(gWater) do
703 if gWater[I] <> nil then
704 with gWater[I] do
705 if GetTextureCount > 1 then
706 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
707 if gAcid1 <> nil then
708 for I := Low(gAcid1) to High(gAcid1) do
709 if gAcid1[I] <> nil then
710 with gAcid1[I] do
711 if GetTextureCount > 1 then
712 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
713 if gAcid2 <> nil then
714 for I := Low(gAcid2) to High(gAcid2) do
715 if gAcid2[I] <> nil then
716 with gAcid2[I] do
717 if GetTextureCount > 1 then
718 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
719 if gSteps <> nil then
720 for I := Low(gSteps) to High(gSteps) do
721 if gSteps[I] <> nil then
722 with gSteps[I] do
723 if GetTextureCount > 1 then
724 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
726 if gTriggers <> nil then
727 for I := Low(gTriggers) to High(gTriggers) do
728 if gTriggers[I].TriggerType = TRIGGER_SOUND then
729 MH_SEND_TriggerSound(gTriggers[I], ID);
731 if Shots <> nil then
732 for I := Low(Shots) to High(Shots) do
733 if Shots[i].ShotType in [6, 7, 8] then
734 MH_SEND_CreateShot(i, ID);
736 MH_SEND_TriggerMusic(ID);
738 MH_SEND_GameStats(ID);
739 MH_SEND_CoopStats(ID);
741 if gGameSettings.GameMode = GM_CTF then
742 begin
743 if gFlags[FLAG_RED].State <> FLAG_STATE_CAPTURED then
744 MH_SEND_FlagEvent(gFlags[FLAG_RED].State, FLAG_RED, 0, True, ID);
745 if gFlags[FLAG_BLUE].State <> FLAG_STATE_CAPTURED then
746 MH_SEND_FlagEvent(gFlags[FLAG_BLUE].State, FLAG_BLUE, 0, True, ID);
747 end;
749 if CreatePlayers and (ID >= 0) then NetClients[ID].State := NET_STATE_GAME;
751 if gLMSRespawn > LMS_RESPAWN_NONE then
752 MH_SEND_GameEvent(NET_EV_LMS_WARMUP, (gLMSRespawnTime - gTime) div 1000, 'N', ID);
753 end;
755 procedure MH_SEND_Info(ID: Byte);
756 var
757 Map: string;
758 begin
759 Map := g_ExtractFileName(gMapInfo.Map);
761 e_Buffer_Clear(@NetOut);
763 e_Buffer_Write(@NetOut, Byte(NET_MSG_INFO));
764 e_Buffer_Write(@NetOut, ID);
765 e_Buffer_Write(@NetOut, NetClients[ID].Player);
766 e_Buffer_Write(@NetOut, gGameSettings.WAD);
767 e_Buffer_Write(@NetOut, Map);
768 e_Buffer_Write(@NetOut, gWADHash);
769 e_Buffer_Write(@NetOut, gGameSettings.GameMode);
770 e_Buffer_Write(@NetOut, gGameSettings.GoalLimit);
771 e_Buffer_Write(@NetOut, gGameSettings.TimeLimit);
772 e_Buffer_Write(@NetOut, gGameSettings.MaxLives);
773 e_Buffer_Write(@NetOut, gGameSettings.Options);
774 e_Buffer_Write(@NetOut, gTime);
776 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
777 end;
779 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
780 var
781 Name: string;
782 i: Integer;
783 Team: Byte;
784 begin
785 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
786 Mode := NET_CHAT_PLAYER;
788 Team := 0;
789 if (Mode = NET_CHAT_TEAM) then
790 begin
791 for i := Low(gPlayers) to High(gPlayers) do
792 if (gPlayers[i] <> nil) and (gPlayers[i].FClientID >= 0) and
793 (gPlayers[i].Team = ID) then
794 begin
795 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHAT));
796 e_Buffer_Write(@NetOut, Txt);
797 e_Buffer_Write(@NetOut, Mode);
798 g_Net_Host_Send(gPlayers[i].FClientID, True, NET_CHAN_CHAT);
799 end;
800 Team := ID;
801 ID := NET_EVERYONE;
802 end
803 else
804 begin
805 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHAT));
806 e_Buffer_Write(@NetOut, Txt);
807 e_Buffer_Write(@NetOut, Mode);
808 g_Net_Host_Send(ID, True, NET_CHAN_CHAT);
809 end;
811 if Mode = NET_CHAT_SYSTEM then
812 Exit;
814 if ID = NET_EVERYONE then
815 begin
816 if Mode = NET_CHAT_PLAYER then
817 begin
818 g_Console_Add(Txt, True);
819 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
820 g_Sound_PlayEx('SOUND_GAME_RADIO');
821 end
822 else
823 if Mode = NET_CHAT_TEAM then
824 if gPlayer1 <> nil then
825 begin
826 if (gPlayer1.Team = TEAM_RED) and (Team = TEAM_RED) then
827 begin
828 g_Console_Add(#18'[Team] '#2 + Txt, True);
829 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
830 g_Sound_PlayEx('SOUND_GAME_RADIO');
831 end
832 else if (gPlayer1.Team = TEAM_BLUE) and (Team = TEAM_BLUE) then
833 begin
834 g_Console_Add(#20'[Team] '#2 + Txt, True);
835 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
836 g_Sound_PlayEx('SOUND_GAME_RADIO');
837 end;
838 end;
839 end
840 else
841 begin
842 Name := g_Net_ClientName_ByID(ID);
843 g_Console_Add('-> ' + Name + ': ' + Txt, True);
844 e_WriteLog('[Tell ' + Name + '] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
845 g_Sound_PlayEx('SOUND_GAME_RADIO');
846 end;
847 end;
849 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
850 begin
851 e_Buffer_Write(@NetOut, Byte(NET_MSG_GFX));
852 e_Buffer_Write(@NetOut, Kind);
853 e_Buffer_Write(@NetOut, X);
854 e_Buffer_Write(@NetOut, Y);
855 e_Buffer_Write(@NetOut, Ang);
857 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
858 end;
860 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
861 begin
862 e_Buffer_Write(@NetOut, Byte(NET_MSG_SND));
863 e_Buffer_Write(@NetOut, Name);
864 if Pos then
865 begin
866 e_Buffer_Write(@NetOut, Byte(1));
867 e_Buffer_Write(@NetOut, X);
868 e_Buffer_Write(@NetOut, Y);
869 end
870 else
871 e_Buffer_Write(@NetOut, Byte(0));
873 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
874 end;
876 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
877 begin
878 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
880 e_Buffer_Write(@NetOut, Byte(NET_MSG_SHADD));
881 e_Buffer_Write(@NetOut, Proj);
882 e_Buffer_Write(@NetOut, Shots[Proj].ShotType);
883 e_Buffer_Write(@NetOut, Shots[Proj].Target);
884 e_Buffer_Write(@NetOut, Shots[Proj].SpawnerUID);
885 e_Buffer_Write(@NetOut, Shots[Proj].Timeout);
886 e_Buffer_Write(@NetOut, Shots[Proj].Obj.X);
887 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Y);
888 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.X);
889 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.Y);
891 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
892 end;
894 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
895 begin
896 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
898 e_Buffer_Write(@NetOut, Byte(NET_MSG_SHPOS));
899 e_Buffer_Write(@NetOut, Proj);
900 e_Buffer_Write(@NetOut, Shots[Proj].Obj.X);
901 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Y);
902 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.X);
903 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.Y);
905 g_Net_Host_Send(ID, False, NET_CHAN_SHOTS);
906 end;
908 procedure MH_Send_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
909 begin
910 e_Buffer_Write(@NetOut, Byte(NET_MSG_SHDEL));
911 e_Buffer_Write(@NetOut, Proj);
912 e_Buffer_Write(@NetOut, Byte(Loud));
913 e_Buffer_Write(@NetOut, X);
914 e_Buffer_Write(@NetOut, Y);
916 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
917 end;
919 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
920 begin
921 e_Buffer_Write(@NetOut, Byte(NET_MSG_SCORE));
922 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
923 begin
924 e_Buffer_Write(@NetOut, gTeamStat[TEAM_RED].Goals);
925 e_Buffer_Write(@NetOut, gTeamStat[TEAM_BLUE].Goals);
926 end
927 else
928 if gGameSettings.GameMode = GM_COOP then
929 begin
930 e_Buffer_Write(@NetOut, gCoopMonstersKilled);
931 e_Buffer_Write(@NetOut, gCoopSecretsFound);
932 end;
934 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
935 end;
937 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
938 begin
939 e_Buffer_Write(@NetOut, Byte(NET_MSG_COOP));
940 e_Buffer_Write(@NetOut, gTotalMonsters);
941 e_Buffer_Write(@NetOut, gSecretsCount);
942 e_Buffer_Write(@NetOut, gCoopTotalMonstersKilled);
943 e_Buffer_Write(@NetOut, gCoopTotalSecretsFound);
944 e_Buffer_Write(@NetOut, gCoopTotalMonsters);
945 e_Buffer_Write(@NetOut, gCoopTotalSecrets);
946 end;
948 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
949 begin
950 e_Buffer_Write(@NetOut, Byte(NET_MSG_GEVENT));
951 e_Buffer_Write(@NetOut, EvType);
952 e_Buffer_Write(@NetOut, EvNum);
953 e_Buffer_Write(@NetOut, EvStr);
954 e_Buffer_Write(@NetOut, Byte(gLastMap));
955 e_Buffer_Write(@NetOut, gTime);
956 if (EvType = NET_EV_MAPSTART) and (Pos(':\', EvStr) > 0) then
957 begin
958 e_Buffer_Write(@NetOut, Byte(1));
959 e_Buffer_Write(@NetOut, gWADHash);
960 end else
961 e_Buffer_Write(@NetOut, Byte(0));
963 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
964 end;
966 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
967 begin
968 e_Buffer_Write(@NetOut, Byte(NET_MSG_FLAG));
969 e_Buffer_Write(@NetOut, EvType);
970 e_Buffer_Write(@NetOut, Flag);
971 e_Buffer_Write(@NetOut, Byte(Quiet));
972 e_Buffer_Write(@NetOut, PID);
973 e_Buffer_Write(@NetOut, gFlags[Flag].State);
974 e_Buffer_Write(@NetOut, gFlags[Flag].CaptureTime);
975 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.X);
976 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.Y);
977 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.Vel.X);
978 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.Vel.Y);
980 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
981 end;
983 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
984 begin
985 e_Buffer_Write(@NetOut, Byte(NET_MSG_GSET));
986 e_Buffer_Write(@NetOut, gGameSettings.GameMode);
987 e_Buffer_Write(@NetOut, gGameSettings.GoalLimit);
988 e_Buffer_Write(@NetOut, gGameSettings.TimeLimit);
989 e_Buffer_Write(@NetOut, gGameSettings.MaxLives);
990 e_Buffer_Write(@NetOut, gGameSettings.Options);
992 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
993 end;
995 // PLAYER (SEND)
997 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
998 var
999 P: TPlayer;
1000 begin
1001 P := g_Player_Get(PID);
1002 if P = nil then Exit;
1004 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLR));
1005 e_Buffer_Write(@NetOut, PID);
1006 e_Buffer_Write(@NetOut, P.Name);
1008 e_Buffer_Write(@NetOut, P.FActualModelName);
1009 e_Buffer_Write(@NetOut, P.FColor.R);
1010 e_Buffer_Write(@NetOut, P.FColor.G);
1011 e_Buffer_Write(@NetOut, P.FColor.B);
1012 e_Buffer_Write(@NetOut, P.Team);
1014 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT)
1015 end;
1017 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
1018 var
1019 kByte: Word;
1020 Pl: TPlayer;
1021 begin
1022 Pl := g_Player_Get(PID);
1023 if Pl = nil then Exit;
1024 if Pl.FDummy then Exit;
1026 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRPOS));
1027 e_Buffer_Write(@NetOut, gTime);
1028 e_Buffer_Write(@NetOut, PID);
1030 kByte := 0;
1032 with Pl do
1033 begin
1034 e_Buffer_Write(@NetOut, FPing);
1035 e_Buffer_Write(@NetOut, FLoss);
1036 if IsKeyPressed(KEY_CHAT) then
1037 kByte := NET_KEY_CHAT
1038 else
1039 begin
1040 if IsKeyPressed(KEY_LEFT) then kByte := kByte or NET_KEY_LEFT;
1041 if IsKeyPressed(KEY_RIGHT) then kByte := kByte or NET_KEY_RIGHT;
1042 if IsKeyPressed(KEY_UP) then kByte := kByte or NET_KEY_UP;
1043 if IsKeyPressed(KEY_DOWN) then kByte := kByte or NET_KEY_DOWN;
1044 if IsKeyPressed(KEY_JUMP) then kByte := kByte or NET_KEY_JUMP;
1045 if JustTeleported then kByte := kByte or NET_KEY_FORCEDIR;
1046 end;
1048 e_Buffer_Write(@NetOut, kByte);
1049 if Direction = D_LEFT then e_Buffer_Write(@NetOut, Byte(0)) else e_Buffer_Write(@NetOut, Byte(1));
1050 e_Buffer_Write(@NetOut, GameX);
1051 e_Buffer_Write(@NetOut, GameY);
1052 e_Buffer_Write(@NetOut, GameVelX);
1053 e_Buffer_Write(@NetOut, GameVelY);
1054 e_Buffer_Write(@NetOut, GameAccelX);
1055 e_Buffer_Write(@NetOut, GameAccelY);
1056 end;
1058 g_Net_Host_Send(ID, Reliable, NET_CHAN_PLAYERPOS);
1059 end;
1061 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
1062 var
1063 P: TPlayer;
1064 I: Integer;
1065 begin
1066 P := g_Player_Get(PID);
1067 if P = nil then Exit;
1069 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRSTA));
1070 e_Buffer_Write(@NetOut, PID);
1072 with P do
1073 begin
1074 e_Buffer_Write(@NetOut, Byte(Live));
1075 e_Buffer_Write(@NetOut, Byte(GodMode));
1076 e_Buffer_Write(@NetOut, Health);
1077 e_Buffer_Write(@NetOut, Armor);
1078 e_Buffer_Write(@NetOut, Air);
1079 e_Buffer_Write(@NetOut, JetFuel);
1080 e_Buffer_Write(@NetOut, Lives);
1081 e_Buffer_Write(@NetOut, Team);
1083 for I := WP_FIRST to WP_LAST do
1084 e_Buffer_Write(@NetOut, Byte(FWeapon[I]));
1086 for I := A_BULLETS to A_HIGH do
1087 e_Buffer_Write(@NetOut, FAmmo[I]);
1089 for I := A_BULLETS to A_HIGH do
1090 e_Buffer_Write(@NetOut, FMaxAmmo[I]);
1092 for I := MR_SUIT to MR_MAX do
1093 e_Buffer_Write(@NetOut, LongWord(FMegaRulez[I]));
1095 e_Buffer_Write(@NetOut, Byte(R_ITEM_BACKPACK in FRulez));
1096 e_Buffer_Write(@NetOut, Byte(R_KEY_RED in FRulez));
1097 e_Buffer_Write(@NetOut, Byte(R_KEY_GREEN in FRulez));
1098 e_Buffer_Write(@NetOut, Byte(R_KEY_BLUE in FRulez));
1099 e_Buffer_Write(@NetOut, Byte(R_BERSERK in FRulez));
1101 e_Buffer_Write(@NetOut, Frags);
1102 e_Buffer_Write(@NetOut, Death);
1104 e_Buffer_Write(@NetOut, CurrWeap);
1106 e_Buffer_Write(@NetOut, Byte(FSpectator));
1107 e_Buffer_Write(@NetOut, Byte(FGhost));
1108 e_Buffer_Write(@NetOut, Byte(FPhysics));
1109 e_Buffer_Write(@NetOut, Byte(FNoRespawn));
1110 e_Buffer_Write(@NetOut, Byte(FJetpack));
1111 e_Buffer_Write(@NetOut, FFireTime);
1112 end;
1114 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1115 end;
1117 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
1118 begin
1119 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRDMG));
1120 e_Buffer_Write(@NetOut, PID);
1121 e_Buffer_Write(@NetOut, Kind);
1122 e_Buffer_Write(@NetOut, Attacker);
1123 e_Buffer_Write(@NetOut, Value);
1124 e_Buffer_Write(@NetOut, VX);
1125 e_Buffer_Write(@NetOut, VY);
1127 g_Net_Host_Send(ID, False, NET_CHAN_PLAYER);
1128 end;
1130 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
1131 begin
1132 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRDIE));
1133 e_Buffer_Write(@NetOut, PID);
1134 e_Buffer_Write(@NetOut, KillType);
1135 e_Buffer_Write(@NetOut, DeathType);
1136 e_Buffer_Write(@NetOut, Attacker);
1138 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1139 end;
1141 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
1142 begin
1143 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRFIRE));
1144 e_Buffer_Write(@NetOut, PID);
1145 e_Buffer_Write(@NetOut, Weapon);
1146 e_Buffer_Write(@NetOut, X);
1147 e_Buffer_Write(@NetOut, Y);
1148 e_Buffer_Write(@NetOut, AX);
1149 e_Buffer_Write(@NetOut, AY);
1150 e_Buffer_Write(@NetOut, ShotID);
1152 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
1153 end;
1155 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
1156 begin
1157 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRDEL));
1158 e_Buffer_Write(@NetOut, PID);
1160 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1161 end;
1163 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
1164 var
1165 Pl: TPlayer;
1166 begin
1167 Pl := g_Player_Get(PID);
1168 if Pl = nil then Exit;
1170 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRSET));
1171 e_Buffer_Write(@NetOut, PID);
1172 e_Buffer_Write(@NetOut, Pl.Name);
1173 if Mdl = '' then
1174 e_Buffer_Write(@NetOut, Pl.Model.Name)
1175 else
1176 e_Buffer_Write(@NetOut, Mdl);
1177 e_Buffer_Write(@NetOut, Pl.FColor.R);
1178 e_Buffer_Write(@NetOut, Pl.FColor.G);
1179 e_Buffer_Write(@NetOut, Pl.FColor.B);
1180 e_Buffer_Write(@NetOut, Pl.Team);
1182 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1183 end;
1185 // ITEM (SEND)
1187 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1188 var
1189 it: PItem;
1190 begin
1191 it := g_ItemByIdx(IID);
1193 e_Buffer_Write(@NetOut, Byte(NET_MSG_ISPAWN));
1194 e_Buffer_Write(@NetOut, IID);
1195 e_Buffer_Write(@NetOut, Byte(Quiet));
1196 e_Buffer_Write(@NetOut, it.ItemType);
1197 e_Buffer_Write(@NetOut, Byte(it.Fall));
1198 e_Buffer_Write(@NetOut, Byte(it.Respawnable));
1199 e_Buffer_Write(@NetOut, it.Obj.X);
1200 e_Buffer_Write(@NetOut, it.Obj.Y);
1201 e_Buffer_Write(@NetOut, it.Obj.Vel.X);
1202 e_Buffer_Write(@NetOut, it.Obj.Vel.Y);
1204 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1205 end;
1207 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1208 begin
1209 e_Buffer_Write(@NetOut, Byte(NET_MSG_IDEL));
1210 e_Buffer_Write(@NetOut, IID);
1211 e_Buffer_Write(@NetOut, Byte(Quiet));
1213 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1214 end;
1216 // PANEL
1218 procedure MH_SEND_PanelTexture(PType: Word; PID: LongWord; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
1219 var
1220 TP: TPanel;
1221 begin
1222 case PType of
1223 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
1224 TP := gWalls[PID];
1225 PANEL_FORE:
1226 TP := gRenderForegrounds[PID];
1227 PANEL_BACK:
1228 TP := gRenderBackgrounds[PID];
1229 PANEL_WATER:
1230 TP := gWater[PID];
1231 PANEL_ACID1:
1232 TP := gAcid1[PID];
1233 PANEL_ACID2:
1234 TP := gAcid2[PID];
1235 PANEL_STEP:
1236 TP := gSteps[PID];
1237 else
1238 Exit;
1239 end;
1241 with TP do
1242 begin
1243 e_Buffer_Write(@NetOut, Byte(NET_MSG_PTEX));
1244 e_Buffer_Write(@NetOut, PType);
1245 e_Buffer_Write(@NetOut, PID);
1246 e_Buffer_Write(@NetOut, FCurTexture);
1247 e_Buffer_Write(@NetOut, FCurFrame);
1248 e_Buffer_Write(@NetOut, FCurFrameCount);
1249 e_Buffer_Write(@NetOut, AnimLoop);
1250 end;
1252 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1253 end;
1255 procedure MH_SEND_PanelState(PType: Word; PID: LongWord; ID: Integer = NET_EVERYONE);
1256 var
1257 TP: TPanel;
1258 begin
1259 case PType of
1260 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
1261 TP := gWalls[PID];
1262 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
1263 TP := gLifts[PID];
1264 PANEL_BACK:
1265 begin
1266 TP := gRenderBackgrounds[PID];
1267 TP.Moved := True;
1268 end;
1269 PANEL_FORE:
1270 begin
1271 TP := gRenderForegrounds[PID];
1272 TP.Moved := True;
1273 end;
1274 else
1275 Exit;
1276 end;
1278 e_Buffer_Write(@NetOut, Byte(NET_MSG_PSTATE));
1279 e_Buffer_Write(@NetOut, PType);
1280 e_Buffer_Write(@NetOut, PID);
1281 e_Buffer_Write(@NetOut, Byte(TP.Enabled));
1282 e_Buffer_Write(@NetOut, TP.LiftType);
1283 e_Buffer_Write(@NetOut, TP.X);
1284 e_Buffer_Write(@NetOut, TP.Y);
1286 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1287 end;
1289 // TRIGGER
1291 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
1292 begin
1293 if gTriggers = nil then Exit;
1294 if T.Sound = nil then Exit;
1296 e_Buffer_Write(@NetOut, Byte(NET_MSG_TSOUND));
1297 e_Buffer_Write(@NetOut, T.ClientID);
1298 e_Buffer_Write(@NetOut, Byte(T.Sound.IsPlaying));
1299 e_Buffer_Write(@NetOut, LongWord(T.Sound.GetPosition));
1300 e_Buffer_Write(@NetOut, T.SoundPlayCount);
1302 g_Net_Host_Send(ID, True);
1303 end;
1305 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
1306 begin
1307 e_Buffer_Write(@NetOut, Byte(NET_MSG_TMUSIC));
1308 e_Buffer_Write(@NetOut, gMusic.Name);
1309 e_Buffer_Write(@NetOut, Byte(gMusic.IsPlaying));
1310 e_Buffer_Write(@NetOut, LongWord(gMusic.GetPosition));
1311 e_Buffer_Write(@NetOut, Byte(gMusic.SpecPause or gMusic.IsPaused));
1313 g_Net_Host_Send(ID, True);
1314 end;
1316 // MONSTER
1318 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
1319 var
1320 M: TMonster;
1321 begin
1322 M := g_Monsters_Get(UID);
1323 if M = nil then
1324 Exit;
1326 with M do
1327 begin
1328 e_Buffer_Write(@NetOut, Byte(NET_MSG_MSPAWN));
1329 e_Buffer_Write(@NetOut, UID);
1330 e_Buffer_Write(@NetOut, MonsterType);
1331 e_Buffer_Write(@NetOut, MonsterState);
1332 e_Buffer_Write(@NetOut, MonsterAnim);
1333 e_Buffer_Write(@NetOut, MonsterTargetUID);
1334 e_Buffer_Write(@NetOut, MonsterTargetTime);
1335 e_Buffer_Write(@NetOut, MonsterBehaviour);
1336 e_Buffer_Write(@NetOut, MonsterSleep);
1337 e_Buffer_Write(@NetOut, MonsterHealth);
1338 e_Buffer_Write(@NetOut, MonsterAmmo);
1339 e_Buffer_Write(@NetOut, GameX);
1340 e_Buffer_Write(@NetOut, GameY);
1341 e_Buffer_Write(@NetOut, GameVelX);
1342 e_Buffer_Write(@NetOut, GameVelY);
1343 e_Buffer_Write(@NetOut, Byte(GameDirection));
1344 end;
1346 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1347 end;
1349 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
1350 var
1351 M: TMonster;
1352 begin
1353 M := g_Monsters_Get(UID);
1354 if M = nil then Exit;
1356 e_Buffer_Write(@NetOut, Byte(NET_MSG_MPOS));
1357 e_Buffer_Write(@NetOut, UID);
1359 with M do
1360 begin
1361 e_Buffer_Write(@NetOut, GameX);
1362 e_Buffer_Write(@NetOut, GameY);
1363 e_Buffer_Write(@NetOut, GameVelX);
1364 e_Buffer_Write(@NetOut, GameVelY);
1365 e_Buffer_Write(@NetOut, Byte(GameDirection));
1366 end;
1368 g_Net_Host_Send(ID, False, NET_CHAN_MONSTERPOS);
1369 end;
1371 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
1372 var
1373 M: TMonster;
1374 begin
1375 M := g_Monsters_Get(UID);
1376 if M = nil then Exit;
1378 e_Buffer_Write(@NetOut, Byte(NET_MSG_MSTATE));
1379 e_Buffer_Write(@NetOut, UID);
1381 with M do
1382 begin
1383 e_Buffer_Write(@NetOut, MonsterState);
1384 e_Buffer_Write(@NetOut, ForcedAnim);
1385 e_Buffer_Write(@NetOut, MonsterTargetUID);
1386 e_Buffer_Write(@NetOut, MonsterTargetTime);
1387 e_Buffer_Write(@NetOut, MonsterSleep);
1388 e_Buffer_Write(@NetOut, MonsterHealth);
1389 e_Buffer_Write(@NetOut, MonsterAmmo);
1390 e_Buffer_Write(@NetOut, MonsterPain);
1391 e_Buffer_Write(@NetOut, Byte(AnimIsReverse));
1392 e_Buffer_Write(@NetOut, FFireTime);
1393 end;
1395 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1396 end;
1398 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
1399 begin
1400 e_Buffer_Write(@NetOut, Byte(NET_MSG_MSHOT));
1401 e_Buffer_Write(@NetOut, UID);
1402 e_Buffer_Write(@NetOut, X);
1403 e_Buffer_Write(@NetOut, Y);
1404 e_Buffer_Write(@NetOut, VX);
1405 e_Buffer_Write(@NetOut, VY);
1407 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1408 end;
1410 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
1411 var
1412 M: TMonster;
1413 begin
1414 M := g_Monsters_Get(UID);
1415 if M = nil then Exit;
1417 e_Buffer_Write(@NetOut, Byte(NET_MSG_MDEL));
1418 e_Buffer_Write(@NetOut, UID);
1420 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1421 end;
1423 // MISC
1425 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
1426 begin
1427 e_Buffer_Write(@NetOut, Byte(NET_MSG_TIME_SYNC));
1428 e_Buffer_Write(@NetOut, Time);
1430 g_Net_Host_Send(ID, False, NET_CHAN_SERVICE);
1431 end;
1433 procedure MH_SEND_VoteEvent(EvType: Byte;
1434 StrArg1: string = 'a'; StrArg2: string = 'b';
1435 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
1436 ID: Integer = NET_EVERYONE);
1437 begin
1438 e_Buffer_Write(@NetOut, Byte(NET_MSG_VOTE_EVENT));
1439 e_Buffer_Write(@NetOut, EvType);
1440 e_Buffer_Write(@NetOut, IntArg1);
1441 e_Buffer_Write(@NetOut, IntArg2);
1442 e_Buffer_Write(@NetOut, StrArg1);
1443 e_Buffer_Write(@NetOut, StrArg2);
1445 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1446 end;
1448 // CLIENT MESSAGES //
1450 // GAME
1452 procedure MC_RECV_Chat(P: Pointer);
1453 var
1454 Txt: string;
1455 Mode: Byte;
1456 begin
1457 Txt := e_Raw_Read_String(P);
1458 Mode := e_Raw_Read_Byte(P);
1460 if Mode <> NET_CHAT_SYSTEM then
1461 begin
1462 if Mode = NET_CHAT_PLAYER then
1463 begin
1464 g_Console_Add(Txt, True);
1465 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
1466 g_Sound_PlayEx('SOUND_GAME_RADIO');
1467 end else
1468 if (Mode = NET_CHAT_TEAM) and (gPlayer1 <> nil) then
1469 begin
1470 if gPlayer1.Team = TEAM_RED then
1471 g_Console_Add(b_Text_Format('\r[Team] ') + Txt, True);
1472 if gPlayer1.Team = TEAM_BLUE then
1473 g_Console_Add(b_Text_Format('\b[Team] ') + Txt, True);
1474 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
1475 g_Sound_PlayEx('SOUND_GAME_RADIO');
1476 end;
1477 end else
1478 g_Console_Add(Txt, True);
1479 end;
1481 procedure MC_RECV_Effect(P: Pointer);
1482 var
1483 Kind: Byte;
1484 X, Y: Integer;
1485 Ang: SmallInt;
1486 Anim: TAnimation;
1487 ID: LongWord;
1488 begin
1489 if not gGameOn then Exit;
1490 Kind := e_Raw_Read_Byte(P);
1491 X := e_Raw_Read_LongInt(P);
1492 Y := e_Raw_Read_LongInt(P);
1493 Ang := e_Raw_Read_SmallInt(P);
1495 case Kind of
1496 NET_GFX_SPARK:
1497 g_GFX_Spark(X, Y, 2 + Random(2), Ang, 0, 0);
1499 NET_GFX_TELE:
1500 begin
1501 if g_Frames_Get(ID, 'FRAMES_TELEPORT') then
1502 begin
1503 Anim := TAnimation.Create(ID, False, 3);
1504 g_GFX_OnceAnim(X, Y, Anim);
1505 Anim.Free();
1506 end;
1507 if Ang = 1 then
1508 g_Sound_PlayExAt('SOUND_GAME_TELEPORT', X, Y);
1509 end;
1511 NET_GFX_EXPLODE:
1512 begin
1513 if g_Frames_Get(ID, 'FRAMES_EXPLODE_ROCKET') then
1514 begin
1515 Anim := TAnimation.Create(ID, False, 6);
1516 Anim.Blending := False;
1517 g_GFX_OnceAnim(X-64, Y-64, Anim);
1518 Anim.Free();
1519 end;
1520 if Ang = 1 then
1521 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEROCKET', X, Y);
1522 end;
1524 NET_GFX_BFGEXPL:
1525 begin
1526 if g_Frames_Get(ID, 'FRAMES_EXPLODE_BFG') then
1527 begin
1528 Anim := TAnimation.Create(ID, False, 6);
1529 Anim.Blending := False;
1530 g_GFX_OnceAnim(X-64, Y-64, Anim);
1531 Anim.Free();
1532 end;
1533 if Ang = 1 then
1534 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEBFG', X, Y);
1535 end;
1537 NET_GFX_BFGHIT:
1538 begin
1539 if g_Frames_Get(ID, 'FRAMES_BFGHIT') then
1540 begin
1541 Anim := TAnimation.Create(ID, False, 4);
1542 g_GFX_OnceAnim(X-32, Y-32, Anim);
1543 Anim.Free();
1544 end;
1545 end;
1547 NET_GFX_FIRE:
1548 begin
1549 if g_Frames_Get(ID, 'FRAMES_FIRE') then
1550 begin
1551 Anim := TAnimation.Create(ID, False, 4);
1552 g_GFX_OnceAnim(X, Y, Anim);
1553 Anim.Free();
1554 end;
1555 if Ang = 1 then
1556 g_Sound_PlayExAt('SOUND_FIRE', X, Y);
1557 end;
1559 NET_GFX_RESPAWN:
1560 begin
1561 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
1562 begin
1563 Anim := TAnimation.Create(ID, False, 4);
1564 g_GFX_OnceAnim(X, Y, Anim);
1565 Anim.Free();
1566 end;
1567 if Ang = 1 then
1568 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
1569 end;
1571 NET_GFX_SHELL1:
1572 g_Player_CreateShell(X, Y, 0, -2, SHELL_BULLET);
1574 NET_GFX_SHELL2:
1575 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1577 NET_GFX_SHELL3:
1578 begin
1579 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1580 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1581 end;
1582 end;
1583 end;
1585 procedure MC_RECV_Sound(P: Pointer);
1586 var
1587 Name: string;
1588 X, Y: Integer;
1589 Pos: Boolean;
1590 begin
1591 Name := e_Raw_Read_String(P);
1592 Pos := e_Raw_Read_Byte(P) <> 0;
1593 if Pos then
1594 begin
1595 X := e_Raw_Read_LongInt(P);
1596 Y := e_Raw_Read_LongInt(P);
1597 g_Sound_PlayExAt(Name, X, Y);
1598 end
1599 else
1600 g_Sound_PlayEx(Name);
1601 end;
1603 procedure MC_RECV_CreateShot(P: Pointer);
1604 var
1605 I, X, Y, XV, YV: Integer;
1606 Timeout: LongWord;
1607 Target, Spawner: Word;
1608 ShType: Byte;
1609 begin
1610 I := e_Raw_Read_LongInt(P);
1611 ShType := e_Raw_Read_Byte(P);
1612 Target := e_Raw_Read_Word(P);
1613 Spawner := e_Raw_Read_Word(P);
1614 Timeout := e_Raw_Read_LongWord(P);
1615 X := e_Raw_Read_LongInt(P);
1616 Y := e_Raw_Read_LongInt(P);
1617 XV := e_Raw_Read_LongInt(P);
1618 YV := e_Raw_Read_LongInt(P);
1620 I := g_Weapon_CreateShot(I, ShType, Spawner, Target, X, Y, XV, YV);
1621 if (Shots <> nil) and (I <= High(Shots)) then
1622 begin
1623 Shots[I].Timeout := Timeout;
1624 //Shots[I].Target := Target; // TODO: find a use for Target later
1625 end;
1626 end;
1628 procedure MC_RECV_UpdateShot(P: Pointer);
1629 var
1630 I, TX, TY, TXV, TYV: Integer;
1631 begin
1632 I := e_Raw_Read_LongInt(P);
1633 TX := e_Raw_Read_LongInt(P);
1634 TY := e_Raw_Read_LongInt(P);
1635 TXV := e_Raw_Read_LongInt(P);
1636 TYV := e_Raw_Read_LongInt(P);
1638 if (Shots <> nil) and (I <= High(Shots)) then
1639 with (Shots[i]) do
1640 begin
1641 Obj.X := TX;
1642 Obj.Y := TY;
1643 Obj.Vel.X := TXV;
1644 Obj.Vel.Y := TYV;
1645 end;
1646 end;
1648 procedure MC_RECV_DeleteShot(P: Pointer);
1649 var
1650 I, X, Y: Integer;
1651 L: Boolean;
1652 begin
1653 if not gGameOn then Exit;
1654 I := e_Raw_Read_LongInt(P);
1655 L := (e_Raw_Read_Byte(P) <> 0);
1656 X := e_Raw_Read_LongInt(P);
1657 Y := e_Raw_Read_LongInt(P);
1659 g_Weapon_DestroyShot(I, X, Y, L);
1660 end;
1662 procedure MC_RECV_GameStats(P: Pointer);
1663 begin
1664 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
1665 begin
1666 gTeamStat[TEAM_RED].Goals := e_Raw_Read_SmallInt(P);
1667 gTeamStat[TEAM_BLUE].Goals := e_Raw_Read_SmallInt(P);
1668 end
1669 else
1670 if gGameSettings.GameMode = GM_COOP then
1671 begin
1672 gCoopMonstersKilled := e_Raw_Read_Word(P);
1673 gCoopSecretsFound := e_Raw_Read_Word(P);
1674 end;
1675 end;
1677 procedure MC_RECV_CoopStats(P: Pointer);
1678 begin
1679 gTotalMonsters := e_Raw_Read_LongInt(P);
1680 gSecretsCount := e_Raw_Read_LongInt(P);
1681 gCoopTotalMonstersKilled := e_Raw_Read_Word(P);
1682 gCoopTotalSecretsFound := e_Raw_Read_Word(P);
1683 gCoopTotalMonsters := e_Raw_Read_Word(P);
1684 gCoopTotalSecrets := e_Raw_Read_Word(P);
1685 end;
1687 procedure MC_RECV_GameEvent(P: Pointer);
1688 var
1689 EvType: Byte;
1690 EvNum: Integer;
1691 EvStr: string;
1692 EvTime: LongWord;
1693 BHash: Boolean;
1694 EvHash: TMD5Digest;
1695 pl: TPlayer;
1696 i1, i2: TStrings_Locale;
1697 pln: String;
1698 cnt: Byte;
1699 begin
1700 FillChar(EvHash, Sizeof(EvHash), 0);
1701 EvType := e_Raw_Read_Byte(P);
1702 EvNum := e_Raw_Read_LongInt(P);
1703 EvStr := e_Raw_Read_String(P);
1704 gLastMap := e_Raw_Read_Byte(P) <> 0;
1705 if gLastMap and (gGameSettings.GameMode = GM_COOP) then gStatsOff := True;
1706 gStatsPressed := True;
1707 EvTime := e_Raw_Read_LongWord(P);
1708 BHash := e_Raw_Read_Byte(P) <> 0;
1709 if BHash then
1710 EvHash := e_Raw_Read_MD5(P);
1712 gTime := EvTime;
1714 case EvType of
1715 NET_EV_MAPSTART:
1716 begin
1717 gGameOn := False;
1718 g_Game_ClearLoading();
1719 g_Game_StopAllSounds(True);
1721 gSwitchGameMode := Byte(EvNum);
1722 gGameSettings.GameMode := gSwitchGameMode;
1724 gWADHash := EvHash;
1725 if not g_Game_StartMap(EvStr, True) then
1726 begin
1727 if Pos(':\', EvStr) = 0 then
1728 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [gGameSettings.WAD + ':\' + EvStr]))
1729 else
1730 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [EvStr]));
1731 Exit;
1732 end;
1734 MC_SEND_FullStateRequest;
1735 end;
1737 NET_EV_MAPEND:
1738 begin
1739 gMissionFailed := EvNum <> 0;
1740 gExit := EXIT_ENDLEVELCUSTOM;
1741 end;
1743 NET_EV_RCON:
1744 begin
1745 case EvNum of
1746 NET_RCON_NOAUTH:
1747 g_Console_Add(_lc[I_NET_RCON_NOAUTH], True);
1748 NET_RCON_PWGOOD:
1749 g_Console_Add(_lc[I_NET_RCON_PWD_VALID], True);
1750 NET_RCON_PWBAD:
1751 g_Console_Add(_lc[I_NET_RCON_PWD_INVALID], True);
1752 end;
1753 end;
1755 NET_EV_CHANGE_TEAM:
1756 begin
1757 if EvNum = TEAM_RED then
1758 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [EvStr]), True);
1759 if EvNum = TEAM_BLUE then
1760 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [EvStr]), True);
1761 end;
1763 NET_EV_PLAYER_KICK:
1764 g_Console_Add(Format(_lc[I_PLAYER_KICK], [EvStr]), True);
1766 NET_EV_PLAYER_BAN:
1767 g_Console_Add(Format(_lc[I_PLAYER_BAN], [EvStr]), True);
1769 NET_EV_LMS_WARMUP:
1770 g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [EvNum]), True);
1772 NET_EV_LMS_SURVIVOR:
1773 g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True);
1775 NET_EV_BIGTEXT:
1776 g_Game_Message(AnsiUpperCase(EvStr), Word(EvNum));
1778 NET_EV_SCORE:
1779 begin
1780 pl := g_Player_Get(EvNum and $FFFF);
1781 if pl = nil then
1782 pln := '?'
1783 else
1784 pln := pl.Name;
1785 cnt := (EvNum shr 16) and $FF;
1786 if Pos('w', EvStr) = 0 then
1787 begin
1788 // Default score
1789 if Pos('t', EvStr) = 0 then
1790 begin
1791 // Player +/- score
1792 if Pos('-', EvStr) = 0 then
1793 begin
1794 if Pos('e', EvStr) = 0 then
1795 i1 := I_PLAYER_SCORE_ADD_OWN
1796 else
1797 i1 := I_PLAYER_SCORE_ADD_ENEMY;
1798 end else
1799 begin
1800 if Pos('e', EvStr) = 0 then
1801 i1 := I_PLAYER_SCORE_SUB_OWN
1802 else
1803 i1 := I_PLAYER_SCORE_SUB_ENEMY;
1804 end;
1805 // Which team
1806 if Pos('r', EvStr) > 0 then
1807 i2 := I_PLAYER_SCORE_TO_RED
1808 else
1809 i2 := I_PLAYER_SCORE_TO_BLUE;
1810 g_Console_Add(Format(_lc[i1], [pln, cnt, _lc[i2]]), True);
1811 end else
1812 begin
1813 // Team +/- score
1814 if Pos('-', EvStr) = 0 then
1815 i1 := I_PLAYER_SCORE_ADD_TEAM
1816 else
1817 i1 := I_PLAYER_SCORE_SUB_TEAM;
1818 // Which team
1819 if Pos('r', EvStr) > 0 then
1820 i2 := I_PLAYER_SCORE_RED
1821 else
1822 i2 := I_PLAYER_SCORE_BLUE;
1823 g_Console_Add(Format(_lc[i1], [_lc[i2], cnt]), True);
1824 end;
1825 end else
1826 begin
1827 // Game Win
1828 if Pos('e', EvStr) = 0 then
1829 i1 := I_PLAYER_SCORE_WIN_OWN
1830 else
1831 i1 := I_PLAYER_SCORE_WIN_ENEMY;
1832 // Which team
1833 if Pos('r', EvStr) > 0 then
1834 i2 := I_PLAYER_SCORE_TO_RED
1835 else
1836 i2 := I_PLAYER_SCORE_TO_BLUE;
1837 g_Console_Add(Format(_lc[i1], [pln, _lc[i2]]), True);
1838 end;
1839 end;
1841 NET_EV_SCORE_MSG:
1842 begin
1843 if EvNum = TEAM_RED then
1844 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1845 if EvNum = TEAM_BLUE then
1846 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1847 if EvNum = -TEAM_RED then
1848 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1849 if EvNum = -TEAM_BLUE then
1850 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1851 end;
1853 NET_EV_LMS_START:
1854 begin
1855 g_Player_RemoveAllCorpses;
1856 g_Game_Message(_lc[I_MESSAGE_LMS_START], 144);
1857 end;
1859 NET_EV_LMS_WIN:
1860 g_Game_Message(Format(_lc[I_MESSAGE_LMS_WIN], [AnsiUpperCase(EvStr)]), 144);
1862 NET_EV_TLMS_WIN:
1863 begin
1864 if EvNum = TEAM_RED then
1865 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 144);
1866 if EvNum = TEAM_BLUE then
1867 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 144);
1868 end;
1870 NET_EV_LMS_LOSE:
1871 g_Game_Message(_lc[I_MESSAGE_LMS_LOSE], 144);
1873 NET_EV_LMS_DRAW:
1874 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
1876 NET_EV_KILLCOMBO:
1877 g_Game_Announce_KillCombo(EvNum);
1879 NET_EV_PLAYER_TOUCH:
1880 begin
1881 pl := g_Player_Get(EvNum);
1882 if pl <> nil then
1883 pl.Touch();
1884 end;
1886 end;
1887 end;
1889 procedure MC_RECV_FlagEvent(P: Pointer);
1890 var
1891 PID: Word;
1892 Pl: TPlayer;
1893 EvType: Byte;
1894 Fl: Byte;
1895 Quiet: Boolean;
1896 s, ts: string;
1897 begin
1898 EvType := e_Raw_Read_Byte(P);
1899 Fl := e_Raw_Read_Byte(P);
1901 if Fl = FLAG_NONE then Exit;
1903 Quiet := (e_Raw_Read_Byte(P) <> 0);
1904 PID := e_Raw_Read_Word(P);
1906 gFlags[Fl].State := e_Raw_Read_Byte(P);
1907 gFlags[Fl].CaptureTime := e_Raw_Read_LongWord(P);
1908 gFlags[Fl].Obj.X := e_Raw_Read_LongInt(P);
1909 gFlags[Fl].Obj.Y := e_Raw_Read_LongInt(P);
1910 gFlags[Fl].Obj.Vel.X := e_Raw_Read_LongInt(P);
1911 gFlags[Fl].Obj.Vel.Y := e_Raw_Read_LongInt(P);
1913 Pl := g_Player_Get(PID);
1914 if (Pl = nil) and
1915 (EvType <> FLAG_STATE_NORMAL) and
1916 (EvType <> FLAG_STATE_DROPPED) and
1917 (EvType <> FLAG_STATE_RETURNED) then
1918 Exit;
1920 case EvType of
1921 FLAG_STATE_NORMAL:
1922 begin
1923 if Quiet or (Pl = nil) then Exit;
1925 if Fl = FLAG_RED then
1926 s := _lc[I_PLAYER_FLAG_RED]
1927 else
1928 s := _lc[I_PLAYER_FLAG_BLUE];
1930 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1931 end;
1933 FLAG_STATE_CAPTURED:
1934 begin
1935 if (Pl <> nil) then Pl.SetFlag(Fl);
1937 if Quiet then Exit;
1939 if Fl = FLAG_RED then
1940 s := _lc[I_PLAYER_FLAG_RED]
1941 else
1942 s := _lc[I_PLAYER_FLAG_BLUE];
1944 g_Console_Add(Format(_lc[I_PLAYER_FLAG_GET], [Pl.Name, s]), True);
1945 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_GET], [AnsiUpperCase(s)]), 144);
1946 end;
1948 FLAG_STATE_DROPPED:
1949 begin
1950 if (Pl <> nil) then Pl.SetFlag(FLAG_NONE);
1952 if Quiet or (Pl = nil) then Exit;
1954 if Fl = FLAG_RED then
1955 s := _lc[I_PLAYER_FLAG_RED]
1956 else
1957 s := _lc[I_PLAYER_FLAG_BLUE];
1959 g_Console_Add(Format(_lc[I_PLAYER_FLAG_DROP], [Pl.Name, s]), True);
1960 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_DROP], [AnsiUpperCase(s)]), 144);
1961 end;
1963 FLAG_STATE_SCORED:
1964 begin
1965 g_Map_ResetFlag(FLAG_RED);
1966 g_Map_ResetFlag(FLAG_BLUE);
1967 if Quiet or (Pl = nil) then Exit;
1968 Pl.SetFlag(FLAG_NONE);
1970 if Fl = FLAG_RED then
1971 s := _lc[I_PLAYER_FLAG_RED]
1972 else
1973 s := _lc[I_PLAYER_FLAG_BLUE];
1975 ts := Format('%.4d', [gFlags[Fl].CaptureTime]);
1976 Insert('.', ts, Length(ts) + 1 - 3);
1977 g_Console_Add(Format(_lc[I_PLAYER_FLAG_CAPTURE], [Pl.Name, s, ts]), True);
1978 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_CAPTURE], [AnsiUpperCase(s)]), 144);
1979 end;
1981 FLAG_STATE_RETURNED:
1982 begin
1983 g_Map_ResetFlag(Fl);
1984 if Quiet then Exit;
1986 if Fl = FLAG_RED then
1987 s := _lc[I_PLAYER_FLAG_RED]
1988 else
1989 s := _lc[I_PLAYER_FLAG_BLUE];
1991 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1992 end;
1993 end;
1994 end;
1996 procedure MC_RECV_GameSettings(P: Pointer);
1997 begin
1998 gGameSettings.GameMode := e_Raw_Read_Byte(P);
1999 gGameSettings.GoalLimit := e_Raw_Read_Word(P);
2000 gGameSettings.TimeLimit := e_Raw_Read_Word(P);
2001 gGameSettings.MaxLives := e_Raw_Read_Byte(P);
2002 gGameSettings.Options := e_Raw_Read_LongWord(P);
2003 end;
2005 // PLAYER
2007 function MC_RECV_PlayerCreate(P: Pointer): Word;
2008 var
2009 PID, DID: Word;
2010 PName, Model: string;
2011 Color: TRGB;
2012 T: Byte;
2013 Pl: TPlayer;
2014 begin
2015 PID := e_Raw_Read_Word(P);
2016 Pl := g_Player_Get(PID);
2018 PName := e_Raw_Read_String(P);
2019 Model := e_Raw_Read_String(P);
2020 Color.R := e_Raw_Read_Byte(P);
2021 Color.G := e_Raw_Read_Byte(P);
2022 Color.B := e_Raw_Read_Byte(P);
2023 T := e_Raw_Read_Byte(P);
2025 Result := 0;
2026 if (PID <> NetPlrUID1) and (PID <> NetPlrUID2) then
2027 begin
2028 if (Pl <> nil) then Exit;
2029 DID := g_Player_Create(Model, Color, T, False);
2030 with g_Player_Get(DID) do
2031 begin
2032 UID := PID;
2033 Name := PName;
2034 Reset(True);
2035 end;
2036 end
2037 else
2038 begin
2039 if (PID = NetPlrUID1) and (gPlayer1 <> nil) then begin
2040 gPlayer1.UID := PID;
2041 gPlayer1.Model.SetColor(Color.R, Color.G, Color.B);
2042 gPlayer1.ChangeTeam(T);
2043 end;
2044 if (PID = NetPlrUID2) and (gPlayer2 <> nil) then begin
2045 gPlayer2.UID := PID;
2046 gPlayer2.Model.SetColor(Color.R, Color.G, Color.B);
2047 gPlayer2.ChangeTeam(T);
2048 end;
2049 end;
2051 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
2052 e_WriteLog('NET: Player ' + PName + ' [' + IntToStr(PID) + '] added.', MSG_NOTIFY);
2053 Result := PID;
2054 end;
2056 function MC_RECV_PlayerPos(P: Pointer): Word;
2057 var
2058 GT: LongWord;
2059 PID: Word;
2060 kByte: Word;
2061 Pl: TPlayer;
2062 Dir: Byte;
2063 TmpX, TmpY: Integer;
2064 begin
2065 Result := 0;
2067 GT := e_Raw_Read_LongWord(P);
2068 if GT < gTime - NET_MAX_DIFFTIME then
2069 begin
2070 gTime := GT;
2071 Exit;
2072 end;
2073 gTime := GT;
2075 PID := e_Raw_Read_Word(P);
2076 Pl := g_Player_Get(PID);
2078 if Pl = nil then Exit;
2080 Result := PID;
2082 with Pl do
2083 begin
2084 FPing := e_Raw_Read_Word(P);
2085 FLoss := e_Raw_Read_Byte(P);
2086 kByte := e_Raw_Read_Word(P);
2087 Dir := e_Raw_Read_Byte(P);
2089 TmpX := e_Raw_Read_LongInt(P);
2090 TmpY := e_Raw_Read_LongInt(P);
2092 ReleaseKeys;
2094 if (kByte = NET_KEY_CHAT) then
2095 PressKey(KEY_CHAT, 10000)
2096 else
2097 begin
2098 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
2099 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
2100 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
2101 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
2102 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
2103 end;
2105 if ((Pl <> gPlayer1) and (Pl <> gPlayer2)) or LongBool(kByte and NET_KEY_FORCEDIR) then
2106 SetDirection(TDirection(Dir));
2108 GameVelX := e_Raw_Read_LongInt(P);
2109 GameVelY := e_Raw_Read_LongInt(P);
2110 GameAccelX := e_Raw_Read_LongInt(P);
2111 GameAccelY := e_Raw_Read_LongInt(P);
2112 SetLerp(TmpX, TmpY);
2113 if NetForcePlayerUpdate then Update();
2114 end;
2115 end;
2117 function MC_RECV_PlayerStats(P: Pointer): Word;
2118 var
2119 PID: Word;
2120 Pl: TPlayer;
2121 I: Integer;
2122 OldJet: Boolean;
2123 NewTeam: Byte;
2124 begin
2125 PID := e_Raw_Read_Word(P);
2126 Pl := g_Player_Get(PID);
2127 Result := 0;
2128 if Pl = nil then
2129 Exit;
2131 with Pl do
2132 begin
2133 Live := (e_Raw_Read_Byte(P) <> 0);
2134 GodMode := (e_Raw_Read_Byte(P) <> 0);
2135 Health := e_Raw_Read_LongInt(P);
2136 Armor := e_Raw_Read_LongInt(P);
2137 Air := e_Raw_Read_LongInt(P);
2138 JetFuel := e_Raw_Read_LongInt(P);
2139 Lives := e_Raw_Read_Byte(P);
2140 NewTeam := e_Raw_Read_Byte(P);
2142 for I := WP_FIRST to WP_LAST do
2143 FWeapon[I] := (e_Raw_Read_Byte(P) <> 0);
2145 for I := A_BULLETS to A_HIGH do
2146 FAmmo[I] := e_Raw_Read_Word(P);
2148 for I := A_BULLETS to A_HIGH do
2149 FMaxAmmo[I] := e_Raw_Read_Word(P);
2151 for I := MR_SUIT to MR_MAX do
2152 FMegaRulez[I] := e_Raw_Read_LongWord(P);
2154 FRulez := [];
2155 if (e_Raw_Read_Byte(P) <> 0) then
2156 FRulez := FRulez + [R_ITEM_BACKPACK];
2157 if (e_Raw_Read_Byte(P) <> 0) then
2158 FRulez := FRulez + [R_KEY_RED];
2159 if (e_Raw_Read_Byte(P) <> 0) then
2160 FRulez := FRulez + [R_KEY_GREEN];
2161 if (e_Raw_Read_Byte(P) <> 0) then
2162 FRulez := FRulez + [R_KEY_BLUE];
2163 if (e_Raw_Read_Byte(P) <> 0) then
2164 FRulez := FRulez + [R_BERSERK];
2166 Frags := e_Raw_Read_LongInt(P);
2167 Death := e_Raw_Read_LongInt(P);
2169 SetWeapon(e_Raw_Read_Byte(P));
2171 FSpectator := e_Raw_Read_Byte(P) <> 0;
2172 if FSpectator then
2173 begin
2174 if Pl = gPlayer1 then
2175 begin
2176 gLMSPID1 := UID;
2177 gPlayer1 := nil;
2178 end;
2179 if Pl = gPlayer2 then
2180 begin
2181 gLMSPID2 := UID;
2182 gPlayer2 := nil;
2183 end;
2184 end
2185 else
2186 begin
2187 if (gPlayer1 = nil) and (gLMSPID1 > 0) then
2188 gPlayer1 := g_Player_Get(gLMSPID1);
2189 if (gPlayer2 = nil) and (gLMSPID2 > 0) then
2190 gPlayer2 := g_Player_Get(gLMSPID2);
2191 end;
2192 FGhost := e_Raw_Read_Byte(P) <> 0;
2193 FPhysics := e_Raw_Read_Byte(P) <> 0;
2194 FNoRespawn := e_Raw_Read_Byte(P) <> 0;
2195 OldJet := FJetpack;
2196 FJetpack := e_Raw_Read_Byte(P) <> 0;
2197 FFireTime := e_Raw_Read_LongInt(P);
2198 if OldJet and not FJetpack then
2199 JetpackOff
2200 else if not OldJet and FJetpack then
2201 JetpackOn;
2202 if Team <> NewTeam then
2203 Pl.ChangeTeam(NewTeam);
2204 end;
2206 Result := PID;
2207 end;
2209 function MC_RECV_PlayerDamage(P: Pointer): Word;
2210 var
2211 PID: Word;
2212 Pl: TPlayer;
2213 Kind: Byte;
2214 Attacker, Value: Word;
2215 VX, VY: Integer;
2216 begin
2217 Result := 0;
2218 if not gGameOn then Exit;
2219 PID := e_Raw_Read_Word(P);
2220 Pl := g_Player_Get(PID);
2221 if Pl = nil then Exit;
2223 Kind := e_Raw_Read_Byte(P);
2224 Attacker := e_Raw_Read_Word(P);
2225 Value := e_Raw_Read_Word(P);
2226 VX := e_Raw_Read_Word(P);
2227 VY := e_Raw_Read_Word(P);
2229 with Pl do
2230 Damage(Value, Attacker, VX, VY, Kind);
2232 Result := PID;
2233 end;
2235 function MC_RECV_PlayerDeath(P: Pointer): Word;
2236 var
2237 PID: Word;
2238 Pl: TPlayer;
2239 KillType, DeathType: Byte;
2240 Attacker: Word;
2241 begin
2242 Result := 0;
2243 if not gGameOn then Exit;
2244 PID := e_Raw_Read_Word(P);
2245 Pl := g_Player_Get(PID);
2246 if Pl = nil then Exit;
2248 KillType := e_Raw_Read_Byte(P);
2249 DeathType := e_Raw_Read_Byte(P);
2250 Attacker := e_Raw_Read_Word(P);
2252 with Pl do
2253 begin
2254 Kill(KillType, Attacker, DeathType);
2255 SoftReset;
2256 end;
2257 end;
2259 function MC_RECV_PlayerDelete(P: Pointer): Word;
2260 var
2261 PID: Word;
2262 Pl: TPlayer;
2263 begin
2264 PID := e_Raw_Read_Word(P);
2265 Pl := g_Player_Get(PID);
2266 Result := 0;
2267 if Pl = nil then Exit;
2269 g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [Pl.Name]), True);
2270 e_WriteLog('NET: Player ' + Pl.Name + ' [' + IntToStr(PID) + '] removed.', MSG_NOTIFY);
2272 g_Player_Remove(PID);
2274 Result := PID;
2275 end;
2277 function MC_RECV_PlayerFire(P: Pointer): Word;
2278 var
2279 PID: Word;
2280 Weap: Byte;
2281 Pl: TPlayer;
2282 X, Y, AX, AY: Integer;
2283 SHID: Integer;
2284 begin
2285 Result := 0;
2286 if not gGameOn then Exit;
2287 PID := e_Raw_Read_Word(P);
2288 Pl := g_Player_Get(PID);
2289 if Pl = nil then Exit;
2291 Weap := e_Raw_Read_Byte(P);
2292 X := e_Raw_Read_LongInt(P);
2293 Y := e_Raw_Read_LongInt(P);
2294 AX := e_Raw_Read_LongInt(P);
2295 AY := e_Raw_Read_LongInt(P);
2296 SHID := e_Raw_Read_LongInt(P);
2298 with Pl do
2299 if Live then NetFire(Weap, X, Y, AX, AY, SHID);
2300 end;
2302 procedure MC_RECV_PlayerSettings(P: Pointer);
2303 var
2304 TmpName: string;
2305 TmpModel: string;
2306 TmpColor: TRGB;
2307 TmpTeam: Byte;
2308 Pl: TPlayer;
2309 PID: Word;
2310 begin
2311 PID := e_Raw_Read_Word(P);
2312 Pl := g_Player_Get(PID);
2313 if Pl = nil then Exit;
2315 TmpName := e_Raw_Read_String(P);
2316 TmpModel := e_Raw_Read_String(P);
2317 TmpColor.R := e_Raw_Read_Byte(P);
2318 TmpColor.G := e_Raw_Read_Byte(P);
2319 TmpColor.B := e_Raw_Read_Byte(P);
2320 TmpTeam := e_Raw_Read_Byte(P);
2322 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
2323 begin
2324 Pl.ChangeTeam(TmpTeam);
2325 if gPlayer1 = Pl then
2326 gPlayer1Settings.Team := TmpTeam;
2327 if gPlayer2 = Pl then
2328 gPlayer2Settings.Team := TmpTeam;
2329 end else
2330 Pl.SetColor(TmpColor);
2332 if Pl.Name <> TmpName then
2333 begin
2334 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
2335 Pl.Name := TmpName;
2336 end;
2338 if TmpModel <> Pl.Model.Name then
2339 Pl.SetModel(TmpModel);
2340 end;
2342 // ITEM
2344 procedure MC_RECV_ItemSpawn(P: Pointer);
2345 var
2346 ID: Word;
2347 AID: DWord;
2348 X, Y, VX, VY: Integer;
2349 T: Byte;
2350 Quiet, Fall{, Resp}: Boolean;
2351 Anim: TAnimation;
2352 it: PItem;
2353 begin
2354 if not gGameOn then Exit;
2355 ID := e_Raw_Read_Word(P);
2356 Quiet := e_Raw_Read_Byte(P) <> 0;
2357 T := e_Raw_Read_Byte(P);
2358 Fall := e_Raw_Read_Byte(P) <> 0;
2359 {Resp :=} e_Raw_Read_Byte(P);
2360 X := e_Raw_Read_LongInt(P);
2361 Y := e_Raw_Read_LongInt(P);
2362 VX := e_Raw_Read_LongInt(P);
2363 VY := e_Raw_Read_LongInt(P);
2365 g_Items_Create(X, Y, T, Fall, False, False, ID);
2367 it := g_ItemByIdx(ID);
2368 it.Obj.Vel.X := VX;
2369 it.Obj.Vel.Y := VY;
2371 if not Quiet then
2372 begin
2373 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
2374 if g_Frames_Get(AID, 'FRAMES_ITEM_RESPAWN') then
2375 begin
2376 Anim := TAnimation.Create(AID, False, 4);
2377 g_GFX_OnceAnim(X+(it.Obj.Rect.Width div 2)-16, Y+(it.Obj.Rect.Height div 2)-16, Anim);
2378 Anim.Free();
2379 end;
2380 end;
2381 end;
2383 procedure MC_RECV_ItemDestroy(P: Pointer);
2384 var
2385 ID: Word;
2386 Quiet: Boolean;
2387 begin
2388 if not gGameOn then Exit;
2389 ID := e_Raw_Read_Word(P);
2390 Quiet := e_Raw_Read_Byte(P) <> 0;
2392 if not g_ItemValidId(ID) then exit;
2394 if not Quiet then g_Item_EmitPickupSound(ID);
2396 g_Items_Remove(ID);
2397 end;
2399 // PANEL
2401 procedure MC_RECV_PanelTexture(P: Pointer);
2402 var
2403 TP: TPanel;
2404 PType: Word;
2405 ID: LongWord;
2406 Tex, Fr: Integer;
2407 Loop, Cnt: Byte;
2408 begin
2409 if not gGameOn then Exit;
2410 PType := e_Raw_Read_Word(P);
2411 ID := e_Raw_Read_LongWord(P);
2412 Tex := e_Raw_Read_LongInt(P);
2413 Fr := e_Raw_Read_LongInt(P);
2414 Cnt := e_Raw_Read_Byte(P);
2415 Loop := e_Raw_Read_Byte(P);
2417 TP := nil;
2419 case PType of
2420 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2421 if gWalls <> nil then
2422 TP := gWalls[ID];
2423 PANEL_FORE:
2424 if gRenderForegrounds <> nil then
2425 TP := gRenderForegrounds[ID];
2426 PANEL_BACK:
2427 if gRenderBackgrounds <> nil then
2428 TP := gRenderBackgrounds[ID];
2429 PANEL_WATER:
2430 if gWater <> nil then
2431 TP := gWater[ID];
2432 PANEL_ACID1:
2433 if gAcid1 <> nil then
2434 TP := gAcid1[ID];
2435 PANEL_ACID2:
2436 if gAcid2 <> nil then
2437 TP := gAcid2[ID];
2438 PANEL_STEP:
2439 if gSteps <> nil then
2440 TP := gSteps[ID];
2441 else
2442 Exit;
2443 end;
2445 if TP <> nil then
2446 if Loop = 0 then
2447 begin // switch texture
2448 TP.SetTexture(Tex, Loop);
2449 TP.SetFrame(Fr, Cnt);
2450 end else // looped or non-looped animation
2451 TP.NextTexture(Loop);
2452 end;
2454 procedure MC_RECV_PanelState(P: Pointer);
2455 var
2456 ID: LongWord;
2457 E: Boolean;
2458 Lift: Byte;
2459 PType: Word;
2460 X, Y: Integer;
2461 begin
2462 if not gGameOn then Exit;
2463 PType := e_Raw_Read_Word(P);
2464 ID := e_Raw_Read_LongWord(P);
2465 E := (e_Raw_Read_Byte(P) <> 0);
2466 Lift := e_Raw_Read_Byte(P);
2467 X := e_Raw_Read_LongInt(P);
2468 Y := e_Raw_Read_LongInt(P);
2470 case PType of
2471 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2472 if E then
2473 g_Map_EnableWall(ID)
2474 else
2475 g_Map_DisableWall(ID);
2477 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
2478 g_Map_SetLift(ID, Lift);
2480 PANEL_BACK:
2481 begin
2482 gRenderBackgrounds[ID].X := X;
2483 gRenderBackgrounds[ID].Y := Y;
2484 end;
2486 PANEL_FORE:
2487 begin
2488 gRenderForegrounds[ID].X := X;
2489 gRenderForegrounds[ID].Y := Y;
2490 end;
2491 end;
2492 end;
2494 // TRIGGERS
2496 procedure MC_RECV_TriggerSound(P: Pointer);
2497 var
2498 SPlaying: Boolean;
2499 SPos, SID: LongWord;
2500 SCount: LongInt;
2501 I: Integer;
2502 begin
2503 if not gGameOn then Exit;
2504 if gTriggers = nil then Exit;
2506 SID := e_Raw_Read_LongWord(P);
2507 SPlaying := e_Raw_Read_Byte(P) <> 0;
2508 SPos := e_Raw_Read_LongWord(P);
2509 SCount := e_Raw_Read_LongInt(P);
2511 for I := Low(gTriggers) to High(gTriggers) do
2512 if gTriggers[I].TriggerType = TRIGGER_SOUND then
2513 if gTriggers[I].ClientID = SID then
2514 with gTriggers[I] do
2515 begin
2516 if SPlaying then
2517 begin
2518 if Data.Local then
2519 Sound.PlayVolumeAt(X+(Width div 2), Y+(Height div 2), Data.Volume/255.0)
2520 else
2521 Sound.PlayPanVolume((Data.Pan-127.0)/128.0, Data.Volume/255.0);
2522 Sound.SetPosition(SPos);
2523 end
2524 else
2525 if Sound.IsPlaying then Sound.Stop;
2527 SoundPlayCount := SCount;
2528 end;
2529 end;
2531 procedure MC_RECV_TriggerMusic(P: Pointer);
2532 var
2533 MName: string;
2534 MPlaying: Boolean;
2535 MPos: LongWord;
2536 MPaused: Boolean;
2537 begin
2538 if not gGameOn then Exit;
2540 MName := e_Raw_Read_String(P);
2541 MPlaying := e_Raw_Read_Byte(P) <> 0;
2542 MPos := e_Raw_Read_LongWord(P);
2543 MPaused := e_Raw_Read_Byte(P) <> 0;
2545 if MPlaying then
2546 begin
2547 gMusic.SetByName(MName);
2548 gMusic.Play(True);
2549 gMusic.SetPosition(MPos);
2550 gMusic.SpecPause := MPaused;
2551 end
2552 else
2553 if gMusic.IsPlaying then gMusic.Stop;
2554 end;
2556 // MONSTERS
2558 procedure MC_RECV_MonsterSpawn(P: Pointer);
2559 var
2560 ID: Word;
2561 MType, MState, MDir, MAnim, MBehav: Byte;
2562 X, Y, VX, VY, MTargTime, MHealth, MAmmo, MSleep: Integer;
2563 MTarg: Word;
2564 M: TMonster;
2565 begin
2566 ID := e_Raw_Read_Word(P);
2567 M := g_Monsters_Get(ID);
2568 if M <> nil then
2569 Exit;
2571 MType := e_Raw_Read_Byte(P);
2572 MState := e_Raw_Read_Byte(P);
2573 MAnim := e_Raw_Read_Byte(P);
2574 MTarg := e_Raw_Read_Word(P);
2575 MTargTime := e_Raw_Read_LongInt(P);
2576 MBehav := e_Raw_Read_Byte(P);
2577 MSleep := e_Raw_Read_LongInt(P);
2578 MHealth := e_Raw_Read_LongInt(P);
2579 MAmmo := e_Raw_Read_LongInt(P);
2581 X := e_Raw_Read_LongInt(P);
2582 Y := e_Raw_Read_LongInt(P);
2583 VX := e_Raw_Read_LongInt(P);
2584 VY := e_Raw_Read_LongInt(P);
2585 MDir := e_Raw_Read_Byte(P);
2587 g_Monsters_Create(MType, X, Y, TDirection(MDir), False, ID);
2588 M := g_Monsters_Get(ID);
2589 if M = nil then
2590 Exit;
2592 with M do
2593 begin
2594 GameX := X;
2595 GameY := Y;
2596 GameVelX := VX;
2597 GameVelY := VY;
2599 MonsterAnim := MAnim;
2600 MonsterTargetUID := MTarg;
2601 MonsterTargetTime := MTargTime;
2602 MonsterBehaviour := MBehav;
2603 MonsterSleep := MSleep;
2604 MonsterAmmo := MAmmo;
2605 SetHealth(MHealth);
2607 SetState(MState);
2609 positionChanged(); // this updates spatial accelerators
2610 end;
2611 end;
2613 procedure MC_RECV_MonsterPos(P: Pointer);
2614 var
2615 M: TMonster;
2616 ID: Word;
2617 begin
2618 ID := e_Raw_Read_Word(P);
2619 M := g_Monsters_Get(ID);
2620 if M = nil then
2621 Exit;
2623 with M do
2624 begin
2625 GameX := e_Raw_Read_LongInt(P);
2626 GameY := e_Raw_Read_LongInt(P);
2627 GameVelX := e_Raw_Read_LongInt(P);
2628 GameVelY := e_Raw_Read_LongInt(P);
2629 GameDirection := TDirection(e_Raw_Read_Byte(P));
2630 positionChanged(); // this updates spatial accelerators
2631 end;
2632 end;
2634 procedure MC_RECV_MonsterState(P: Pointer);
2635 var
2636 ID: Integer;
2637 MState, MFAnm: Byte;
2638 M: TMonster;
2639 AnimRevert: Boolean;
2640 begin
2641 ID := e_Raw_Read_Word(P);
2642 M := g_Monsters_Get(ID);
2643 if M = nil then Exit;
2645 MState := e_Raw_Read_Byte(P);
2646 MFAnm := e_Raw_Read_Byte(P);
2648 with M do
2649 begin
2650 MonsterTargetUID := e_Raw_Read_Word(P);
2651 MonsterTargetTime := e_Raw_Read_LongInt(P);
2652 MonsterSleep := e_Raw_Read_LongInt(P);
2653 MonsterHealth := e_Raw_Read_LongInt(P);
2654 MonsterAmmo := e_Raw_Read_LongInt(P);
2655 MonsterPain := e_Raw_Read_LongInt(P);
2656 AnimRevert := e_Raw_Read_Byte(P) <> 0;
2657 FFireTime := e_Raw_Read_LongInt(P);
2658 RevertAnim(AnimRevert);
2660 if MonsterState <> MState then
2661 begin
2662 if (MState = MONSTATE_GO) and (MonsterState = MONSTATE_SLEEP) then
2663 WakeUpSound;
2664 if (MState = MONSTATE_DIE) then
2665 DieSound;
2666 if (MState = MONSTATE_PAIN) then
2667 MakeBloodSimple(Min(200, MonsterPain));
2668 if (MState = MONSTATE_ATTACK) then
2669 kick(nil);
2670 if (MState = MONSTATE_DEAD) then
2671 SetDeadAnim;
2673 SetState(MState, MFAnm);
2674 end;
2675 end;
2676 end;
2678 procedure MC_RECV_MonsterShot(P: Pointer);
2679 var
2680 ID: Integer;
2681 M: TMonster;
2682 X, Y, VX, VY: Integer;
2683 begin
2684 ID := e_Raw_Read_Word(P);
2686 M := g_Monsters_Get(ID);
2687 if M = nil then Exit;
2689 X := e_Raw_Read_LongInt(P);
2690 Y := e_Raw_Read_LongInt(P);
2691 VX := e_Raw_Read_LongInt(P);
2692 VY := e_Raw_Read_LongInt(P);
2694 M.ClientAttack(X, Y, VX, VY);
2695 end;
2697 procedure MC_RECV_MonsterDelete(P: Pointer);
2698 var
2699 ID: Integer;
2700 M: TMonster;
2701 begin
2702 ID := e_Raw_Read_Word(P);
2703 M := g_Monsters_Get(ID);
2704 if M = nil then Exit;
2706 gMonsters[ID].SetState(5);
2707 gMonsters[ID].MonsterRemoved := True;
2708 end;
2710 procedure MC_RECV_TimeSync(P: Pointer);
2711 var
2712 Time: LongWord;
2713 begin
2714 Time := e_Raw_Read_LongWord(P);
2716 if gState = STATE_INTERCUSTOM then
2717 gServInterTime := Min(Time, 255);
2718 end;
2720 procedure MC_RECV_VoteEvent(P: Pointer);
2721 var
2722 EvID: Byte;
2723 Str1, Str2: string;
2724 Int1, Int2: SmallInt;
2725 begin
2726 EvID := e_Raw_Read_Byte(P);
2727 Int1 := e_Raw_Read_SmallInt(P);
2728 Int2 := e_Raw_Read_SmallInt(P);
2729 Str1 := e_Raw_Read_String(P);
2730 Str2 := e_Raw_Read_String(P);
2732 case EvID of
2733 NET_VE_STARTED:
2734 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_STARTED], [Str1, Str2, Int1]), True);
2735 NET_VE_PASSED:
2736 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_PASSED], [Str1]), True);
2737 NET_VE_FAILED:
2738 g_Console_Add(_lc[I_MESSAGE_VOTE_FAILED], True);
2739 NET_VE_VOTE:
2740 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Str1, Int1, Int2]), True);
2741 NET_VE_INPROGRESS:
2742 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_INPROGRESS], [Str1]), True);
2743 end;
2744 end;
2746 // CLIENT SEND
2748 procedure MC_SEND_Info(Password: string);
2749 begin
2750 e_Buffer_Clear(@NetOut);
2752 e_Buffer_Write(@NetOut, Byte(NET_MSG_INFO));
2753 e_Buffer_Write(@NetOut, GAME_VERSION);
2754 e_Buffer_Write(@NetOut, Password);
2755 e_Buffer_Write(@NetOut, gPlayer1Settings.Name);
2756 e_Buffer_Write(@NetOut, gPlayer1Settings.Model);
2757 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.R);
2758 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.G);
2759 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.B);
2760 e_Buffer_Write(@NetOut, gPlayer1Settings.Team);
2762 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2763 end;
2765 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
2766 begin
2767 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHAT));
2768 e_Buffer_Write(@NetOut, Txt);
2769 e_Buffer_Write(@NetOut, Mode);
2771 g_Net_Client_Send(True, NET_CHAN_CHAT);
2772 end;
2774 function isKeyPressed (key1: Word; key2: Word): Boolean;
2775 begin
2776 if (key1 <> 0) and e_KeyPressed(key1) then begin result := true; exit; end;
2777 if (key2 <> 0) and e_KeyPressed(key2) then begin result := true; exit; end;
2778 result := false;
2779 end;
2781 procedure MC_SEND_PlayerPos();
2782 var
2783 kByte: Word;
2784 Predict: Boolean;
2785 strafeDir: Byte;
2786 WeaponSelect: Word = 0;
2787 I: Integer;
2788 begin
2789 if not gGameOn then Exit;
2790 if gPlayers = nil then Exit;
2791 if gPlayer1 = nil then Exit;
2793 kByte := 0;
2794 Predict := NetPredictSelf; // and (not NetGotKeys);
2796 if (not gConsoleShow) and (not gChatShow) and (g_ActiveWindow = nil) then
2797 begin
2798 strafeDir := P1MoveButton shr 4;
2799 P1MoveButton := P1MoveButton and $0F;
2800 with gGameControls.P1Control do
2801 begin
2802 if isKeyPressed(KeyLeft, KeyLeft2) and (not isKeyPressed(KeyRight, KeyRight2)) then P1MoveButton := 1
2803 else if (not isKeyPressed(KeyLeft, KeyLeft2)) and isKeyPressed(KeyRight, KeyRight2) then P1MoveButton := 2
2804 else if (not isKeyPressed(KeyLeft, KeyLeft2)) and (not isKeyPressed(KeyRight, KeyRight2)) then P1MoveButton := 0;
2806 // strafing
2807 if isKeyPressed(KeyStrafe, KeyStrafe2) then
2808 begin
2809 // new strafe mechanics
2810 if (strafeDir = 0) then strafeDir := P1MoveButton; // start strafing
2811 // now set direction according to strafe (reversed)
2812 if (strafeDir = 2) then gPlayer1.SetDirection(D_LEFT)
2813 else if (strafeDir = 1) then gPlayer1.SetDirection(D_RIGHT);
2814 end
2815 else
2816 begin
2817 if (P1MoveButton = 2) and isKeyPressed(KeyLeft, KeyLeft2) then gPlayer1.SetDirection(D_LEFT)
2818 else if (P1MoveButton = 1) and isKeyPressed(KeyRight, KeyRight2) then gPlayer1.SetDirection(D_RIGHT)
2819 else if P1MoveButton <> 0 then gPlayer1.SetDirection(TDirection(P1MoveButton-1));
2820 end;
2822 gPlayer1.ReleaseKeys;
2823 if P1MoveButton = 1 then
2824 begin
2825 kByte := kByte or NET_KEY_LEFT;
2826 if Predict then gPlayer1.PressKey(KEY_LEFT, 10000);
2827 end;
2828 if P1MoveButton = 2 then
2829 begin
2830 kByte := kByte or NET_KEY_RIGHT;
2831 if Predict then gPlayer1.PressKey(KEY_RIGHT, 10000);
2832 end;
2833 if isKeyPressed(KeyUp, KeyUp2) then
2834 begin
2835 kByte := kByte or NET_KEY_UP;
2836 gPlayer1.PressKey(KEY_UP, 10000);
2837 end;
2838 if isKeyPressed(KeyDown, KeyDown2) then
2839 begin
2840 kByte := kByte or NET_KEY_DOWN;
2841 gPlayer1.PressKey(KEY_DOWN, 10000);
2842 end;
2843 if isKeyPressed(KeyJump, KeyJump2) then
2844 begin
2845 kByte := kByte or NET_KEY_JUMP;
2846 // gPlayer1.PressKey(KEY_JUMP, 10000); // TODO: Make a prediction option
2847 end;
2848 if isKeyPressed(KeyFire, KeyFire2) then kByte := kByte or NET_KEY_FIRE;
2849 if isKeyPressed(KeyOpen, KeyOpen2) then kByte := kByte or NET_KEY_OPEN;
2850 if isKeyPressed(KeyNextWeapon, KeyNextWeapon2) then kByte := kByte or NET_KEY_NW;
2851 if isKeyPressed(KeyPrevWeapon, KeyPrevWeapon2) then kByte := kByte or NET_KEY_PW;
2852 for I := 0 to High(KeyWeapon) do
2853 if isKeyPressed(KeyWeapon[I], KeyWeapon2[I]) then
2854 WeaponSelect := WeaponSelect or Word(1 shl I);
2855 end;
2856 // fix movebutton state
2857 P1MoveButton := P1MoveButton or (strafeDir shl 4);
2858 end
2859 else
2860 kByte := NET_KEY_CHAT;
2862 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRPOS));
2863 e_Buffer_Write(@NetOut, gTime);
2864 e_Buffer_Write(@NetOut, kByte);
2865 e_Buffer_Write(@NetOut, Byte(gPlayer1.Direction));
2866 e_Buffer_Write(@NetOut, WeaponSelect);
2867 //e_WriteLog(Format('S:ws=%d', [WeaponSelect]), MSG_WARNING);
2868 g_Net_Client_Send(True, NET_CHAN_PLAYERPOS);
2870 //kBytePrev := kByte;
2871 //kDirPrev := gPlayer1.Direction;
2872 end;
2874 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
2875 begin
2876 e_Buffer_Write(@NetOut, Byte(NET_MSG_VOTE_EVENT));
2877 e_Buffer_Write(@NetOut, Byte(Start));
2878 e_Buffer_Write(@NetOut, Command);
2879 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2880 end;
2882 procedure MC_SEND_PlayerSettings();
2883 begin
2884 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRSET));
2885 e_Buffer_Write(@NetOut, gPlayer1Settings.Name);
2886 e_Buffer_Write(@NetOut, gPlayer1Settings.Model);
2887 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.R);
2888 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.G);
2889 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.B);
2890 e_Buffer_Write(@NetOut, gPlayer1Settings.Team);
2892 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2893 end;
2895 procedure MC_SEND_FullStateRequest();
2896 begin
2897 e_Buffer_Write(@NetOut, Byte(NET_MSG_REQFST));
2899 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2900 end;
2902 procedure MC_SEND_CheatRequest(Kind: Byte);
2903 begin
2904 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHEAT));
2905 e_Buffer_Write(@NetOut, Kind);
2907 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2908 end;
2909 procedure MC_SEND_RCONPassword(Password: string);
2910 begin
2911 e_Buffer_Write(@NetOut, Byte(NET_MSG_RCON_AUTH));
2912 e_Buffer_Write(@NetOut, Password);
2914 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2915 end;
2916 procedure MC_SEND_RCONCommand(Cmd: string);
2917 begin
2918 e_Buffer_Write(@NetOut, Byte(NET_MSG_RCON_CMD));
2919 e_Buffer_Write(@NetOut, Cmd);
2921 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2922 end;
2924 // i have no idea why all this stuff is in here
2926 function ReadFile(const FileName: TFileName): AByte;
2927 var
2928 FileStream : TStream;
2929 fname: string;
2930 begin
2931 e_WriteLog(Format('NETWORK: looking for file "%s"', [FileName]), MSG_NOTIFY);
2932 fname := findDiskWad(FileName);
2933 if length(fname) = 0 then
2934 begin
2935 e_WriteLog(Format('NETWORK: file "%s" not found!', [FileName]), MSG_FATALERROR);
2936 SetLength(Result, 0);
2937 exit;
2938 end;
2939 e_WriteLog(Format('NETWORK: found file "%s"', [fname]), MSG_NOTIFY);
2940 Result := nil;
2941 FileStream := openDiskFileRO(fname);
2942 try
2943 if FileStream.Size > 0 then
2944 begin
2945 SetLength(Result, FileStream.Size);
2946 FileStream.Read(Result[0], FileStream.Size);
2947 end;
2948 finally
2949 FileStream.Free;
2950 end;
2951 end;
2953 function CreateMapDataMsg(const FileName: TFileName; ResList: TStringList): TMapDataMsg;
2954 var
2955 i: Integer;
2956 begin
2957 Result.MsgId := NET_MSG_MAP_RESPONSE;
2958 Result.FileData := ReadFile(FileName);
2959 Result.FileSize := Length(Result.FileData);
2961 SetLength(Result.ExternalResources, ResList.Count);
2962 for i:=0 to ResList.Count-1 do
2963 begin
2964 Result.ExternalResources[i].Name := ResList.Strings[i];
2965 Result.ExternalResources[i].md5 := MD5File(GameDir+'/wads/'+ResList.Strings[i]);
2966 end;
2967 end;
2969 procedure ResDataMsgToBytes(var bytes: AByte; const ResData: TResDataMsg);
2970 var
2971 ResultStream: TMemoryStream;
2972 begin
2973 ResultStream := TMemoryStream.Create;
2975 ResultStream.WriteBuffer(ResData.MsgId, SizeOf(ResData.MsgId)); //msgId
2976 ResultStream.WriteBuffer(ResData.FileSize, SizeOf(ResData.FileSize)); //file size
2977 ResultStream.WriteBuffer(ResData.FileData[0], ResData.FileSize); //file data
2979 SetLength(bytes, ResultStream.Size);
2980 ResultStream.Seek(0, soFromBeginning);
2981 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
2983 ResultStream.Free;
2984 end;
2986 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
2987 begin
2988 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
2989 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize));
2990 SetLength(Result.FileData, Result.FileSize);
2991 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize);
2992 end;
2994 procedure MapDataMsgToBytes(var bytes: AByte; const MapDataMsg: TMapDataMsg);
2995 var
2996 ResultStream: TMemoryStream;
2997 resCount: Integer;
2998 begin
2999 resCount := Length(MapDataMsg.ExternalResources);
3001 ResultStream := TMemoryStream.Create;
3003 ResultStream.WriteBuffer(MapDataMsg.MsgId, SizeOf(MapDataMsg.MsgId)); //msgId
3004 ResultStream.WriteBuffer(MapDataMsg.FileSize, SizeOf(MapDataMsg.FileSize)); //file size
3005 ResultStream.WriteBuffer(MapDataMsg.FileData[0], MapDataMsg.FileSize); //file data
3007 ResultStream.WriteBuffer(resCount, SizeOf(resCount)); //res count
3008 ResultStream.WriteBuffer(MapDataMsg.ExternalResources[0], resCount*SizeOf(TExternalResourceInfo)); //res data
3010 SetLength(bytes, ResultStream.Size);
3011 ResultStream.Seek(0, soFromBeginning);
3012 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
3014 ResultStream.Free;
3015 end;
3017 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
3018 var
3019 resCount: Integer;
3020 begin
3021 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
3022 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize)); //file size
3024 SetLength(Result.FileData, Result.FileSize);
3025 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize); //file data
3027 msgStream.ReadBuffer(resCount, SizeOf(resCount)); //res count
3028 SetLength(Result.ExternalResources, resCount);
3030 msgStream.ReadBuffer(Result.ExternalResources[0], resCount * SizeOf(TExternalResourceInfo)); //res data
3031 end;
3033 function IsValidFileName(const S: String): Boolean;
3034 const
3035 Forbidden: set of Char = ['<', '>', '|', '"', ':', '*', '?'];
3036 var
3037 I: Integer;
3038 begin
3039 Result := S <> '';
3040 for I := 1 to Length(S) do
3041 Result := Result and (not(S[I] in Forbidden));
3042 end;
3044 function IsValidFilePath(const S: String): Boolean;
3045 var
3046 I: Integer;
3047 begin
3048 Result := False;
3049 if not IsValidFileName(S) then exit;
3050 if FileExists(S) then exit;
3051 I := LastDelimiter('\/', S);
3052 if (I > 0) then
3053 if (not DirectoryExists(Copy(S, 1, I-1))) then
3054 exit;
3055 Result := True;
3056 end;
3058 procedure MC_SEND_MapRequest();
3059 begin
3060 e_Buffer_Write(@NetOut, Byte(NET_MSG_MAP_REQUEST));
3061 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3062 end;
3064 procedure MC_SEND_ResRequest(const resName: AnsiString);
3065 begin
3066 e_Buffer_Write(@NetOut, Byte(NET_MSG_RES_REQUEST));
3067 e_Buffer_Write(@NetOut, resName);
3068 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3069 end;
3071 procedure MH_RECV_MapRequest(C: pTNetClient; P: Pointer);
3072 var
3073 payload: AByte;
3074 peer: pENetPeer;
3075 mapDataMsg: TMapDataMsg;
3076 begin
3077 e_WriteLog('NET: Received map request from ' +
3078 DecodeIPV4(C.Peer.address.host), MSG_NOTIFY);
3080 mapDataMsg := CreateMapDataMsg(MapsDir + gGameSettings.WAD, gExternalResources);
3081 peer := NetClients[C.ID].Peer;
3083 MapDataMsgToBytes(payload, mapDataMsg);
3084 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3086 payload := nil;
3087 mapDataMsg.FileData := nil;
3088 mapDataMsg.ExternalResources := nil;
3089 end;
3091 procedure MH_RECV_ResRequest(C: pTNetClient; P: Pointer);
3092 var
3093 payload: AByte;
3094 peer: pENetPeer;
3095 FileName: String;
3096 resDataMsg: TResDataMsg;
3097 begin
3098 FileName := ExtractFileName(e_Raw_Read_String(P));
3099 e_WriteLog('NET: Received res request: ' + FileName +
3100 ' from ' + DecodeIPV4(C.Peer.address.host), MSG_NOTIFY);
3102 if not IsValidFilePath(FileName) then
3103 begin
3104 e_WriteLog('Invalid filename: ' + FileName, MSG_WARNING);
3105 exit;
3106 end;
3108 peer := NetClients[C.ID].Peer;
3110 if gExternalResources.IndexOf(FileName) > -1 then
3111 begin
3112 resDataMsg.MsgId := NET_MSG_RES_RESPONSE;
3113 resDataMsg.FileData := ReadFile(GameDir+'/wads/'+FileName);
3114 resDataMsg.FileSize := Length(resDataMsg.FileData);
3116 ResDataMsgToBytes(payload, resDataMsg);
3117 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3118 end;
3119 end;
3121 end.