DEADSOFTWARE

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