DEADSOFTWARE

initial commit:
[d2df-sdl.git] / src / game / g_netmsg.pas
1 unit g_netmsg;
3 interface
5 uses g_net, g_triggers, Classes, SysUtils, md5;
7 const
8 NET_MSG_INFO = 100;
10 NET_MSG_CHAT = 101;
11 NET_MSG_SND = 102;
12 NET_MSG_GFX = 103;
13 NET_MSG_GEVENT = 104;
14 NET_MSG_SCORE = 105;
15 NET_MSG_COOP = 106;
16 NET_MSG_FLAG = 107;
17 NET_MSG_REQFST = 108;
18 NET_MSG_GSET = 109;
20 NET_MSG_PLR = 111;
21 NET_MSG_PLRPOS = 112;
22 NET_MSG_PLRSTA = 113;
23 NET_MSG_PLRDEL = 114;
24 NET_MSG_PLRDMG = 115;
25 NET_MSG_PLRDIE = 116;
26 NET_MSG_PLRFIRE= 117;
27 NET_MSG_PLRSET = 119;
28 NET_MSG_CHEAT = 120;
30 NET_MSG_ISPAWN = 121;
31 NET_MSG_IDEL = 122;
33 NET_MSG_MSPAWN = 131;
34 NET_MSG_MPOS = 132;
35 NET_MSG_MSTATE = 133;
36 NET_MSG_MSHOT = 134;
37 NET_MSG_MDEL = 135;
39 NET_MSG_PSTATE = 141;
40 NET_MSG_PTEX = 142;
42 NET_MSG_TSOUND = 151;
43 NET_MSG_TMUSIC = 152;
45 NET_MSG_SHDEL = 161;
46 NET_MSG_SHADD = 162;
47 NET_MSG_SHPOS = 163;
49 NET_MSG_RCON_AUTH = 191;
50 NET_MSG_RCON_CMD = 192;
51 NET_MSG_TIME_SYNC = 194;
52 NET_MSG_VOTE_EVENT = 195;
54 NET_MSG_MAP_REQUEST = 201;
55 NET_MSG_MAP_RESPONSE = 202;
56 NET_MSG_RES_REQUEST = 203;
57 NET_MSG_RES_RESPONSE = 204;
59 NET_CHAT_SYSTEM = 0;
60 NET_CHAT_PLAYER = 1;
61 NET_CHAT_TEAM = 2;
63 NET_RCON_NOAUTH = 0;
64 NET_RCON_PWGOOD = 1;
65 NET_RCON_PWBAD = 2;
67 NET_GFX_SPARK = 1;
68 NET_GFX_TELE = 2;
69 NET_GFX_RESPAWN = 3;
70 NET_GFX_FIRE = 4;
71 NET_GFX_EXPLODE = 5;
72 NET_GFX_BFGEXPL = 6;
73 NET_GFX_BFGHIT = 7;
74 NET_GFX_SHELL1 = 8;
75 NET_GFX_SHELL2 = 9;
76 NET_GFX_SHELL3 = 10;
78 NET_EV_MAPSTART = 1;
79 NET_EV_MAPEND = 2;
80 NET_EV_CHANGE_TEAM = 3;
81 NET_EV_PLAYER_KICK = 4;
82 NET_EV_PLAYER_BAN = 5;
83 NET_EV_LMS_WARMUP = 6;
84 NET_EV_LMS_SURVIVOR = 7;
85 NET_EV_RCON = 8;
86 NET_EV_BIGTEXT = 9;
87 NET_EV_SCORE = 10;
88 NET_EV_SCORE_MSG = 11;
89 NET_EV_LMS_START = 12;
90 NET_EV_LMS_WIN = 13;
91 NET_EV_TLMS_WIN = 14;
92 NET_EV_LMS_LOSE = 15;
93 NET_EV_LMS_DRAW = 16;
94 NET_EV_KILLCOMBO = 17;
95 NET_EV_PLAYER_TOUCH = 18;
97 NET_VE_STARTED = 1;
98 NET_VE_PASSED = 2;
99 NET_VE_FAILED = 3;
100 NET_VE_VOTE = 4;
101 NET_VE_REVOKE = 5;
102 NET_VE_INPROGRESS = 6;
104 NET_FLAG_GET = 1;
105 NET_FLAG_DROP = 2;
106 NET_FLAG_CAP = 3;
107 NET_FLAG_RETURN = 4;
109 NET_CHEAT_SUICIDE = 1;
110 NET_CHEAT_SPECTATE = 2;
112 NET_MAX_DIFFTIME = 5000 div 36;
114 // HOST MESSAGES
116 procedure MH_RECV_Info(C: pTNetClient; P: Pointer);
117 procedure MH_RECV_Chat(C: pTNetClient; P: Pointer);
118 procedure MH_RECV_FullStateRequest(C: pTNetClient; P: Pointer);
119 function MH_RECV_PlayerPos(C: pTNetClient; P: Pointer): Word;
120 procedure MH_RECV_PlayerSettings(C: pTNetClient; P: Pointer);
121 procedure MH_RECV_CheatRequest(C: pTNetClient; P: Pointer);
122 procedure MH_RECV_RCONPassword(C: pTNetClient; P: Pointer);
123 procedure MH_RECV_RCONCommand(C: pTNetClient; P: Pointer);
124 procedure MH_RECV_MapRequest(C: pTNetClient; P: Pointer);
125 procedure MH_RECV_ResRequest(C: pTNetClient; P: Pointer);
126 procedure MH_RECV_Vote(C: pTNetClient; P: Pointer);
128 // GAME
129 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
130 procedure MH_SEND_Info(ID: Byte);
131 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
132 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
133 procedure MH_SEND_Sound(X, Y: Integer; Name: string; ID: Integer = NET_EVERYONE);
134 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
135 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
136 procedure MH_SEND_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
137 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
138 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
139 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
140 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
141 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
142 // PLAYER
143 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
144 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
145 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
146 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
147 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
148 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
149 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
150 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
151 // ITEM
152 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
153 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
154 // PANEL
155 procedure MH_SEND_PanelTexture(PType: Word; PID: LongWord; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
156 procedure MH_SEND_PanelState(PType: Word; PID: LongWord; ID: Integer = NET_EVERYONE);
157 // MONSTER
158 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
159 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
160 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
161 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
162 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
163 // TRIGGER
164 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
165 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
166 // MISC
167 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
168 procedure MH_SEND_VoteEvent(EvType: Byte;
169 StrArg1: string = 'a'; StrArg2: string = 'b';
170 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
171 ID: Integer = NET_EVERYONE);
173 // CLIENT MESSAGES //
175 // GAME
176 procedure MC_RECV_Chat(P: Pointer);
177 procedure MC_RECV_Effect(P: Pointer);
178 procedure MC_RECV_Sound(P: Pointer);
179 procedure MC_RECV_GameStats(P: Pointer);
180 procedure MC_RECV_CoopStats(P: Pointer);
181 procedure MC_RECV_GameEvent(P: Pointer);
182 procedure MC_RECV_FlagEvent(P: Pointer);
183 procedure MC_RECV_GameSettings(P: Pointer);
184 // PLAYER
185 function MC_RECV_PlayerCreate(P: Pointer): Word;
186 function MC_RECV_PlayerPos(P: Pointer): Word;
187 function MC_RECV_PlayerStats(P: Pointer): Word;
188 function MC_RECV_PlayerDelete(P: Pointer): Word;
189 function MC_RECV_PlayerDamage(P: Pointer): Word;
190 function MC_RECV_PlayerDeath(P: Pointer): Word;
191 function MC_RECV_PlayerFire(P: Pointer): Word;
192 procedure MC_RECV_PlayerSettings(P: Pointer);
193 // ITEM
194 procedure MC_RECV_ItemSpawn(P: Pointer);
195 procedure MC_RECV_ItemDestroy(P: Pointer);
196 // PANEL
197 procedure MC_RECV_PanelTexture(P: Pointer);
198 procedure MC_RECV_PanelState(P: Pointer);
199 // MONSTER
200 procedure MC_RECV_MonsterSpawn(P: Pointer);
201 procedure MC_RECV_MonsterPos(P: Pointer);
202 procedure MC_RECV_MonsterState(P: Pointer);
203 procedure MC_RECV_MonsterShot(P: Pointer);
204 procedure MC_RECV_MonsterDelete(P: Pointer);
205 // SHOT
206 procedure MC_RECV_CreateShot(P: Pointer);
207 procedure MC_RECV_UpdateShot(P: Pointer);
208 procedure MC_RECV_DeleteShot(P: Pointer);
209 // TRIGGER
210 procedure MC_RECV_TriggerSound(P: Pointer);
211 procedure MC_RECV_TriggerMusic(P: Pointer);
212 // MISC
213 procedure MC_RECV_TimeSync(P: Pointer);
214 procedure MC_RECV_VoteEvent(P: Pointer);
215 // SERVICE
216 procedure MC_SEND_Info(Password: string);
217 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
218 procedure MC_SEND_PlayerPos();
219 procedure MC_SEND_FullStateRequest();
220 procedure MC_SEND_PlayerSettings();
221 procedure MC_SEND_CheatRequest(Kind: Byte);
222 procedure MC_SEND_RCONPassword(Password: string);
223 procedure MC_SEND_RCONCommand(Cmd: string);
224 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
225 // DOWNLOAD
226 procedure MC_SEND_MapRequest();
227 procedure MC_SEND_ResRequest(const resName: AnsiString);
229 type
230 TExternalResourceInfo = record
231 Name: string[255];
232 md5: TMD5Digest;
233 end;
235 TResDataMsg = record
236 MsgId: Byte;
237 FileSize: Integer;
238 FileData: AByte;
239 end;
241 TMapDataMsg = record
242 MsgId: Byte;
243 FileSize: Integer;
244 FileData: AByte;
245 ExternalResources: array of TExternalResourceInfo;
246 end;
248 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
249 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
251 implementation
253 uses
254 Math, ENet, e_input, e_fixedbuffer, e_graphics, e_log,
255 g_textures, g_gfx, g_sound, g_console, g_basic, g_options, g_main,
256 g_game, g_player, g_map, g_panel, g_items, g_weapons, g_phys, g_gui,
257 g_language, g_monsters, g_netmaster,
258 WADEDITOR, MAPDEF;
260 const
261 NET_KEY_LEFT = 1;
262 NET_KEY_RIGHT = 2;
263 NET_KEY_UP = 4;
264 NET_KEY_DOWN = 8;
265 NET_KEY_JUMP = 16;
266 NET_KEY_FIRE = 32;
267 NET_KEY_OPEN = 64;
268 NET_KEY_NW = 256;
269 NET_KEY_PW = 512;
270 NET_KEY_CHAT = 2048;
271 NET_KEY_FORCEDIR = 4096;
273 //var
274 //kBytePrev: Word = 0;
275 //kDirPrev: TDirection = D_LEFT;
276 //HostGameTime: Word = 0;
278 // HOST MESSAGES //
281 // GAME
283 procedure MH_RECV_Chat(C: pTNetClient; P: Pointer);
284 var
285 Txt: string;
286 Mode: Byte;
287 PID: Word;
288 Pl: TPlayer;
289 begin
290 PID := C^.Player;
291 Pl := g_Player_Get(PID);
293 Txt := e_Raw_Read_String(P);
294 Mode := e_Raw_Read_Byte(P);
295 if (Mode = NET_CHAT_SYSTEM) then
296 Mode := NET_CHAT_PLAYER; // prevent sending system messages from clients
297 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
298 Mode := NET_CHAT_PLAYER; // revert to player chat in non-team game
300 if Pl = nil then
301 MH_SEND_Chat(Txt, Mode)
302 else
303 MH_SEND_Chat(Pl.Name + ': ' + Txt, Mode, IfThen(Mode = NET_CHAT_TEAM, Pl.Team, NET_EVERYONE));
304 end;
306 procedure MH_RECV_Info(C: pTNetClient; P: Pointer);
307 var
308 Ver, PName, Model, Pw: string;
309 R, G, B, T: Byte;
310 PID: Word;
311 Color: TRGB;
312 I: Integer;
313 begin
314 Ver := e_Raw_Read_String(P);
315 Pw := e_Raw_Read_String(P);
316 PName := e_Raw_Read_String(P);
317 Model := e_Raw_Read_String(P);
318 R := e_Raw_Read_Byte(P);
319 G := e_Raw_Read_Byte(P);
320 B := e_Raw_Read_Byte(P);
321 T := e_Raw_Read_Byte(P);
323 if Ver <> GAME_VERSION then
324 begin
325 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
326 _lc[I_NET_DISC_VERSION]);
327 enet_peer_disconnect(C^.Peer, NET_DISC_VERSION);
328 Exit;
329 end;
331 if g_Net_IsHostBanned(C^.Peer^.address.host) then
332 begin
333 if g_Net_IsHostBanned(C^.Peer^.address.host, True) then
334 begin
335 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
336 _lc[I_NET_DISC_BAN]);
337 enet_peer_disconnect(C^.Peer, NET_DISC_BAN);
338 end
339 else
340 begin
341 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
342 _lc[I_NET_DISC_BAN]);
343 enet_peer_disconnect(C^.Peer, NET_DISC_TEMPBAN);
344 end;
345 Exit;
346 end;
348 if NetPassword <> '' then
349 if AnsiLowerCase(NetPassword) <> AnsiLowerCase(Pw) then
350 begin
351 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
352 _lc[I_NET_DISC_PASSWORD]);
353 enet_peer_disconnect(C^.Peer, NET_DISC_PASSWORD);
354 Exit;
355 end;
357 Color.R := R;
358 Color.B := B;
359 Color.G := G;
361 PID := g_Player_Create(Model, Color, T, False);
362 with g_Player_Get(PID) do
363 begin
364 Name := PName;
365 Reset(True);
366 end;
368 C^.Player := PID;
370 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
371 e_WriteLog('NET: Client ' + PName + ' [' + IntToStr(C^.ID) +
372 '] connected. Assigned player #' + IntToStr(PID) + '.', MSG_NOTIFY);
374 MH_SEND_Info(C^.ID);
376 with g_Player_Get(PID) do
377 begin
378 Name := PName;
379 FClientID := C^.ID;
380 // round in progress, don't spawn
381 if (gGameSettings.MaxLives > 0) and (gLMSRespawn = LMS_RESPAWN_NONE) then
382 begin
383 Lives := 0;
384 FNoRespawn := True;
385 Spectate;
386 FWantsInGame := True; // TODO: look into this later
387 end
388 else
389 Respawn(gGameSettings.GameType = GT_SINGLE);
390 end;
392 for I := Low(NetClients) to High(NetClients) do
393 begin
394 if NetClients[I].ID = C^.ID then Continue;
395 MH_SEND_PlayerCreate(PID, NetClients[I].ID);
396 MH_SEND_PlayerPos(True, PID, NetClients[I].ID);
397 MH_SEND_PlayerStats(PID, NetClients[I].ID);
398 end;
400 if gState in [STATE_INTERCUSTOM, STATE_FOLD] then
401 MH_SEND_GameEvent(NET_EV_MAPEND, 0, 'N', C^.ID);
403 if NetUseMaster then g_Net_Slist_Update;
404 end;
406 procedure MH_RECV_FullStateRequest(C: pTNetClient; P: Pointer);
407 begin
408 if gGameOn then
409 MH_SEND_Everything((C^.State = NET_STATE_AUTH), C^.ID)
410 else
411 C^.RequestedFullUpdate := True;
412 end;
414 // PLAYER
416 function MH_RECV_PlayerPos(C: pTNetClient; P: Pointer): Word;
417 var
418 Dir: Byte;
419 PID: Word;
420 kByte: Word;
421 Pl: TPlayer;
422 GT: LongWord;
423 begin
424 Result := 0;
425 if not gGameOn then Exit;
427 GT := e_Raw_Read_LongWord(P);
428 PID := C^.Player;
429 Pl := g_Player_Get(PID);
430 if Pl = nil then
431 Exit;
433 if (GT > gTime + NET_MAX_DIFFTIME) or (GT < Pl.NetTime) then Exit;
435 with Pl do
436 begin
437 NetTime := GT;
438 kByte := e_Raw_Read_Word(P);
439 Dir := e_Raw_Read_Byte(P);
440 if Direction <> TDirection(Dir) then
441 JustTeleported := False;
442 SetDirection(TDirection(Dir));
443 ReleaseKeys;
445 if kByte = NET_KEY_CHAT then
446 begin
447 PressKey(KEY_CHAT, 10000);
448 Exit;
449 end;
451 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
452 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
453 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
454 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
455 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
456 if LongBool(kByte and NET_KEY_FIRE) then PressKey(KEY_FIRE, 10000);
457 if LongBool(kByte and NET_KEY_OPEN) then PressKey(KEY_OPEN, 10000);
458 if LongBool(kByte and NET_KEY_NW) then PressKey(KEY_NEXTWEAPON, 10000);
459 if LongBool(kByte and NET_KEY_PW) then PressKey(KEY_PREVWEAPON, 10000);
460 end;
462 // MH_SEND_PlayerPos(False, PID, C^.ID);
463 end;
465 procedure MH_RECV_CheatRequest(C: pTNetClient; P: Pointer);
466 var
467 CheatKind: Byte;
468 Pl: TPlayer;
469 begin
470 Pl := g_Player_Get(C^.Player);
471 if Pl = nil then Exit;
473 CheatKind := e_Raw_Read_Byte(P);
475 case CheatKind of
476 NET_CHEAT_SUICIDE:
477 Pl.Damage(SUICIDE_DAMAGE, Pl.UID, 0, 0, HIT_SELF);
478 NET_CHEAT_SPECTATE:
479 begin
480 if Pl.FSpectator then
481 Pl.Respawn(False)
482 else
483 Pl.Spectate;
484 end;
485 end;
486 end;
488 procedure MH_RECV_PlayerSettings(C: pTNetClient; P: Pointer);
489 var
490 TmpName: string;
491 TmpModel: string;
492 TmpColor: TRGB;
493 TmpTeam: Byte;
494 Pl: TPlayer;
495 begin
496 TmpName := e_Raw_Read_String(P);
497 TmpModel := e_Raw_Read_String(P);
498 TmpColor.R := e_Raw_Read_Byte(P);
499 TmpColor.G := e_Raw_Read_Byte(P);
500 TmpColor.B := e_Raw_Read_Byte(P);
501 TmpTeam := e_Raw_Read_Byte(P);
503 Pl := g_Player_Get(C^.Player);
504 if Pl = nil then Exit;
506 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
507 Pl.SwitchTeam
508 else
509 Pl.SetColor(TmpColor);
511 if Pl.Name <> TmpName then
512 begin
513 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
514 Pl.Name := TmpName;
515 end;
517 if TmpModel <> Pl.Model.Name then
518 Pl.SetModel(TmpModel);
520 MH_SEND_PlayerSettings(Pl.UID, TmpModel);
521 end;
523 // RCON
525 procedure MH_RECV_RCONPassword(C: pTNetClient; P: Pointer);
526 var
527 Pwd: string;
528 begin
529 Pwd := e_Raw_Read_String(P);
530 if not NetAllowRCON then Exit;
531 if Pwd = NetRCONPassword then
532 begin
533 C^.RCONAuth := True;
534 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWGOOD, 'N', C^.ID);
535 end
536 else
537 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWBAD, 'N', C^.ID);
538 end;
540 procedure MH_RECV_RCONCommand(C: pTNetClient; P: Pointer);
541 var
542 Cmd: string;
543 begin
544 Cmd := e_Raw_Read_String(P);
545 if not NetAllowRCON then Exit;
546 if not C^.RCONAuth then
547 begin
548 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_NOAUTH, 'N', C^.ID);
549 Exit;
550 end;
551 g_Console_Process(Cmd);
552 end;
554 // MISC
556 procedure MH_RECV_Vote(C: pTNetClient; P: Pointer);
557 var
558 Start: Boolean;
559 Name, Command: string;
560 Need: Integer;
561 Pl: TPlayer;
562 begin
563 Start := e_Raw_Read_Byte(P) <> 0;
564 Command := e_Raw_Read_String(P);
566 Pl := g_Player_Get(C^.Player);
567 if Pl = nil then Exit;
568 Name := Pl.Name;
570 if Start then
571 begin
572 if not g_Console_CommandBlacklisted(Command) then
573 g_Game_StartVote(Command, Name);
574 end
575 else if gVoteInProgress then
576 begin
577 if (gPlayer1 <> nil) or (gPlayer2 <> nil) then
578 Need := Floor((NetClientCount+1)/2.0) + 1
579 else
580 Need := Floor(NetClientCount/2.0) + 1;
581 if C^.Voted then
582 begin
583 Dec(gVoteCount);
584 C^.Voted := False;
585 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_REVOKED], [Name, gVoteCount, Need]), True);
586 MH_SEND_VoteEvent(NET_VE_REVOKE, Name, 'a', gVoteCount, Need);
587 end
588 else
589 begin
590 Inc(gVoteCount);
591 C^.Voted := True;
592 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Name, gVoteCount, Need]), True);
593 MH_SEND_VoteEvent(NET_VE_VOTE, Name, 'a', gVoteCount, Need);
594 g_Game_CheckVote;
595 end;
596 end;
597 end;
599 // GAME (SEND)
601 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
602 var
603 I: Integer;
604 begin
605 if gPlayers <> nil then
606 for I := Low(gPlayers) to High(gPlayers) do
607 if gPlayers[I] <> nil then
608 begin
609 if CreatePlayers then MH_SEND_PlayerCreate(gPlayers[I].UID, ID);
610 MH_SEND_PlayerPos(True, gPlayers[I].UID, ID);
611 MH_SEND_PlayerStats(gPlayers[I].UID, ID);
613 if (gPlayers[I].Flag <> FLAG_NONE) and (gGameSettings.GameMode = GM_CTF) then
614 MH_SEND_FlagEvent(FLAG_STATE_CAPTURED, gPlayers[I].Flag, gPlayers[I].UID, True, ID);
615 end;
617 if gItems <> nil then
618 begin
619 for I := High(gItems) downto Low(gItems) do
620 if gItems[I].Live then
621 MH_SEND_ItemSpawn(True, I, ID);
622 end;
624 if gMonsters <> nil then
625 for I := 0 to High(gMonsters) do
626 if gMonsters[I] <> nil then
627 MH_SEND_MonsterSpawn(gMonsters[I].UID, ID);
629 if gWalls <> nil then
630 for I := Low(gWalls) to High(gWalls) do
631 if gWalls[I] <> nil then
632 with gWalls[I] do
633 begin
634 if Door then
635 MH_SEND_PanelState(PanelType, I, ID);
637 if GetTextureCount > 1 then
638 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
639 end;
641 if gLifts <> nil then
642 for I := Low(gLifts) to High(gLifts) do
643 if gLifts[I] <> nil then
644 with gLifts[I] do
645 MH_SEND_PanelState(PanelType, I, ID);
647 if gRenderForegrounds <> nil then
648 for I := Low(gRenderForegrounds) to High(gRenderForegrounds) do
649 if gRenderForegrounds[I] <> nil then
650 with gRenderForegrounds[I] do
651 if (GetTextureCount > 1) then
652 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
653 if gRenderBackgrounds <> nil then
654 for I := Low(gRenderBackgrounds) to High(gRenderBackgrounds) do
655 if gRenderBackgrounds[I] <> nil then
656 with gRenderBackgrounds[I] do
657 if GetTextureCount > 1 then
658 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
659 if gWater <> nil then
660 for I := Low(gWater) to High(gWater) do
661 if gWater[I] <> nil then
662 with gWater[I] do
663 if GetTextureCount > 1 then
664 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
665 if gAcid1 <> nil then
666 for I := Low(gAcid1) to High(gAcid1) do
667 if gAcid1[I] <> nil then
668 with gAcid1[I] do
669 if GetTextureCount > 1 then
670 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
671 if gAcid2 <> nil then
672 for I := Low(gAcid2) to High(gAcid2) do
673 if gAcid2[I] <> nil then
674 with gAcid2[I] do
675 if GetTextureCount > 1 then
676 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
677 if gSteps <> nil then
678 for I := Low(gSteps) to High(gSteps) do
679 if gSteps[I] <> nil then
680 with gSteps[I] do
681 if GetTextureCount > 1 then
682 MH_SEND_PanelTexture(PanelType, I, LastAnimLoop, ID);
684 if gTriggers <> nil then
685 for I := Low(gTriggers) to High(gTriggers) do
686 if gTriggers[I].TriggerType = TRIGGER_SOUND then
687 MH_SEND_TriggerSound(gTriggers[I], ID);
689 if Shots <> nil then
690 for I := Low(Shots) to High(Shots) do
691 if Shots[i].ShotType in [6, 7, 8] then
692 MH_SEND_CreateShot(i, ID);
694 MH_SEND_TriggerMusic(ID);
696 MH_SEND_GameStats(ID);
697 MH_SEND_CoopStats(ID);
699 if gGameSettings.GameMode = GM_CTF then
700 begin
701 if gFlags[FLAG_RED].State <> FLAG_STATE_CAPTURED then
702 MH_SEND_FlagEvent(gFlags[FLAG_RED].State, FLAG_RED, 0, True, ID);
703 if gFlags[FLAG_BLUE].State <> FLAG_STATE_CAPTURED then
704 MH_SEND_FlagEvent(gFlags[FLAG_BLUE].State, FLAG_BLUE, 0, True, ID);
705 end;
707 if CreatePlayers and (ID >= 0) then NetClients[ID].State := NET_STATE_GAME;
709 if gLMSRespawn > LMS_RESPAWN_NONE then
710 MH_SEND_GameEvent(NET_EV_LMS_WARMUP, (gLMSRespawnTime - gTime) div 1000, 'N', ID);
711 end;
713 procedure MH_SEND_Info(ID: Byte);
714 var
715 Map: string;
716 begin
717 g_ProcessResourceStr(gMapInfo.Map, nil, nil, @Map);
719 e_Buffer_Clear(@NetOut);
721 e_Buffer_Write(@NetOut, Byte(NET_MSG_INFO));
722 e_Buffer_Write(@NetOut, ID);
723 e_Buffer_Write(@NetOut, NetClients[ID].Player);
724 e_Buffer_Write(@NetOut, gGameSettings.WAD);
725 e_Buffer_Write(@NetOut, Map);
726 e_Buffer_Write(@NetOut, gWADHash);
727 e_Buffer_Write(@NetOut, gGameSettings.GameMode);
728 e_Buffer_Write(@NetOut, gGameSettings.GoalLimit);
729 e_Buffer_Write(@NetOut, gGameSettings.TimeLimit);
730 e_Buffer_Write(@NetOut, gGameSettings.MaxLives);
731 e_Buffer_Write(@NetOut, gGameSettings.Options);
732 e_Buffer_Write(@NetOut, gTime);
734 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
735 end;
737 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
738 var
739 Name: string;
740 i: Integer;
741 Team: Byte;
742 begin
743 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
744 Mode := NET_CHAT_PLAYER;
746 Team := 0;
747 if (Mode = NET_CHAT_TEAM) then
748 begin
749 for i := Low(gPlayers) to High(gPlayers) do
750 if (gPlayers[i] <> nil) and (gPlayers[i].FClientID >= 0) and
751 (gPlayers[i].Team = ID) then
752 begin
753 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHAT));
754 e_Buffer_Write(@NetOut, Txt);
755 e_Buffer_Write(@NetOut, Mode);
756 g_Net_Host_Send(gPlayers[i].FClientID, True, NET_CHAN_CHAT);
757 end;
758 Team := ID;
759 ID := NET_EVERYONE;
760 end
761 else
762 begin
763 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHAT));
764 e_Buffer_Write(@NetOut, Txt);
765 e_Buffer_Write(@NetOut, Mode);
766 g_Net_Host_Send(ID, True, NET_CHAN_CHAT);
767 end;
769 if Mode = NET_CHAT_SYSTEM then
770 Exit;
772 if ID = NET_EVERYONE then
773 begin
774 if Mode = NET_CHAT_PLAYER then
775 begin
776 g_Console_Add(Txt, True);
777 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
778 g_Sound_PlayEx('SOUND_GAME_RADIO');
779 end
780 else
781 if Mode = NET_CHAT_TEAM then
782 if gPlayer1 <> nil then
783 begin
784 if (gPlayer1.Team = TEAM_RED) and (Team = TEAM_RED) then
785 begin
786 g_Console_Add(#18'[Team] '#2 + Txt, True);
787 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
788 g_Sound_PlayEx('SOUND_GAME_RADIO');
789 end
790 else if (gPlayer1.Team = TEAM_BLUE) and (Team = TEAM_BLUE) then
791 begin
792 g_Console_Add(#20'[Team] '#2 + Txt, True);
793 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
794 g_Sound_PlayEx('SOUND_GAME_RADIO');
795 end;
796 end;
797 end
798 else
799 begin
800 Name := g_Net_ClientName_ByID(ID);
801 g_Console_Add('-> ' + Name + ': ' + Txt, True);
802 e_WriteLog('[Tell ' + Name + '] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
803 g_Sound_PlayEx('SOUND_GAME_RADIO');
804 end;
805 end;
807 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
808 begin
809 e_Buffer_Write(@NetOut, Byte(NET_MSG_GFX));
810 e_Buffer_Write(@NetOut, Kind);
811 e_Buffer_Write(@NetOut, X);
812 e_Buffer_Write(@NetOut, Y);
813 e_Buffer_Write(@NetOut, Ang);
815 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
816 end;
818 procedure MH_SEND_Sound(X, Y: Integer; Name: string; ID: Integer = NET_EVERYONE);
819 begin
820 e_Buffer_Write(@NetOut, Byte(NET_MSG_SND));
821 e_Buffer_Write(@NetOut, Name);
822 e_Buffer_Write(@NetOut, X);
823 e_Buffer_Write(@NetOut, Y);
825 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
826 end;
828 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
829 begin
830 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
832 e_Buffer_Write(@NetOut, Byte(NET_MSG_SHADD));
833 e_Buffer_Write(@NetOut, Proj);
834 e_Buffer_Write(@NetOut, Shots[Proj].ShotType);
835 e_Buffer_Write(@NetOut, Shots[Proj].Target);
836 e_Buffer_Write(@NetOut, Shots[Proj].SpawnerUID);
837 e_Buffer_Write(@NetOut, Shots[Proj].Timeout);
838 e_Buffer_Write(@NetOut, Shots[Proj].Obj.X);
839 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Y);
840 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.X);
841 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.Y);
843 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
844 end;
846 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
847 begin
848 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
850 e_Buffer_Write(@NetOut, Byte(NET_MSG_SHPOS));
851 e_Buffer_Write(@NetOut, Proj);
852 e_Buffer_Write(@NetOut, Shots[Proj].Obj.X);
853 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Y);
854 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.X);
855 e_Buffer_Write(@NetOut, Shots[Proj].Obj.Vel.Y);
857 g_Net_Host_Send(ID, False, NET_CHAN_SHOTS);
858 end;
860 procedure MH_Send_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
861 begin
862 e_Buffer_Write(@NetOut, Byte(NET_MSG_SHDEL));
863 e_Buffer_Write(@NetOut, Proj);
864 e_Buffer_Write(@NetOut, Byte(Loud));
865 e_Buffer_Write(@NetOut, X);
866 e_Buffer_Write(@NetOut, Y);
868 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
869 end;
871 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
872 begin
873 e_Buffer_Write(@NetOut, Byte(NET_MSG_SCORE));
874 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
875 begin
876 e_Buffer_Write(@NetOut, gTeamStat[TEAM_RED].Goals);
877 e_Buffer_Write(@NetOut, gTeamStat[TEAM_BLUE].Goals);
878 end
879 else
880 if gGameSettings.GameMode = GM_COOP then
881 begin
882 e_Buffer_Write(@NetOut, gCoopMonstersKilled);
883 e_Buffer_Write(@NetOut, gCoopSecretsFound);
884 end;
886 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
887 end;
889 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
890 begin
891 e_Buffer_Write(@NetOut, Byte(NET_MSG_COOP));
892 e_Buffer_Write(@NetOut, gTotalMonsters);
893 e_Buffer_Write(@NetOut, gSecretsCount);
894 e_Buffer_Write(@NetOut, gCoopTotalMonstersKilled);
895 e_Buffer_Write(@NetOut, gCoopTotalSecretsFound);
896 e_Buffer_Write(@NetOut, gCoopTotalMonsters);
897 e_Buffer_Write(@NetOut, gCoopTotalSecrets);
898 end;
900 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
901 begin
902 e_Buffer_Write(@NetOut, Byte(NET_MSG_GEVENT));
903 e_Buffer_Write(@NetOut, EvType);
904 e_Buffer_Write(@NetOut, EvNum);
905 e_Buffer_Write(@NetOut, EvStr);
906 e_Buffer_Write(@NetOut, Byte(gLastMap));
907 e_Buffer_Write(@NetOut, gTime);
908 if (EvType = NET_EV_MAPSTART) and (Pos(':\', EvStr) > 0) then
909 begin
910 e_Buffer_Write(@NetOut, Byte(1));
911 e_Buffer_Write(@NetOut, gWADHash);
912 end else
913 e_Buffer_Write(@NetOut, Byte(0));
915 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
916 end;
918 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
919 begin
920 e_Buffer_Write(@NetOut, Byte(NET_MSG_FLAG));
921 e_Buffer_Write(@NetOut, EvType);
922 e_Buffer_Write(@NetOut, Flag);
923 e_Buffer_Write(@NetOut, Byte(Quiet));
924 e_Buffer_Write(@NetOut, PID);
925 e_Buffer_Write(@NetOut, gFlags[Flag].State);
926 e_Buffer_Write(@NetOut, gFlags[Flag].CaptureTime);
927 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.X);
928 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.Y);
929 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.Vel.X);
930 e_Buffer_Write(@NetOut, gFlags[Flag].Obj.Vel.Y);
932 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
933 end;
935 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
936 begin
937 e_Buffer_Write(@NetOut, Byte(NET_MSG_GSET));
938 e_Buffer_Write(@NetOut, gGameSettings.GameMode);
939 e_Buffer_Write(@NetOut, gGameSettings.GoalLimit);
940 e_Buffer_Write(@NetOut, gGameSettings.TimeLimit);
941 e_Buffer_Write(@NetOut, gGameSettings.MaxLives);
942 e_Buffer_Write(@NetOut, gGameSettings.Options);
944 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
945 end;
947 // PLAYER (SEND)
949 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
950 var
951 P: TPlayer;
952 begin
953 P := g_Player_Get(PID);
954 if P = nil then Exit;
956 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLR));
957 e_Buffer_Write(@NetOut, PID);
958 e_Buffer_Write(@NetOut, P.Name);
960 e_Buffer_Write(@NetOut, P.FActualModelName);
961 e_Buffer_Write(@NetOut, P.FColor.R);
962 e_Buffer_Write(@NetOut, P.FColor.G);
963 e_Buffer_Write(@NetOut, P.FColor.B);
964 e_Buffer_Write(@NetOut, P.Team);
966 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT)
967 end;
969 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
970 var
971 kByte: Word;
972 Pl: TPlayer;
973 begin
974 Pl := g_Player_Get(PID);
975 if Pl = nil then Exit;
976 if Pl.FDummy then Exit;
978 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRPOS));
979 e_Buffer_Write(@NetOut, gTime);
980 e_Buffer_Write(@NetOut, PID);
982 kByte := 0;
984 with Pl do
985 begin
986 e_Buffer_Write(@NetOut, FPing);
987 e_Buffer_Write(@NetOut, FLoss);
988 if IsKeyPressed(KEY_CHAT) then
989 kByte := NET_KEY_CHAT
990 else
991 begin
992 if IsKeyPressed(KEY_LEFT) then kByte := kByte or NET_KEY_LEFT;
993 if IsKeyPressed(KEY_RIGHT) then kByte := kByte or NET_KEY_RIGHT;
994 if IsKeyPressed(KEY_UP) then kByte := kByte or NET_KEY_UP;
995 if IsKeyPressed(KEY_DOWN) then kByte := kByte or NET_KEY_DOWN;
996 if IsKeyPressed(KEY_JUMP) then kByte := kByte or NET_KEY_JUMP;
997 if JustTeleported then kByte := kByte or NET_KEY_FORCEDIR;
998 end;
1000 e_Buffer_Write(@NetOut, kByte);
1001 if Direction = D_LEFT then e_Buffer_Write(@NetOut, Byte(0)) else e_Buffer_Write(@NetOut, Byte(1));
1002 e_Buffer_Write(@NetOut, GameX);
1003 e_Buffer_Write(@NetOut, GameY);
1004 e_Buffer_Write(@NetOut, GameVelX);
1005 e_Buffer_Write(@NetOut, GameVelY);
1006 e_Buffer_Write(@NetOut, GameAccelX);
1007 e_Buffer_Write(@NetOut, GameAccelY);
1008 end;
1010 g_Net_Host_Send(ID, Reliable, NET_CHAN_PLAYERPOS);
1011 end;
1013 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
1014 var
1015 P: TPlayer;
1016 I: Integer;
1017 begin
1018 P := g_Player_Get(PID);
1019 if P = nil then Exit;
1021 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRSTA));
1022 e_Buffer_Write(@NetOut, PID);
1024 with P do
1025 begin
1026 e_Buffer_Write(@NetOut, Byte(Live));
1027 e_Buffer_Write(@NetOut, Byte(GodMode));
1028 e_Buffer_Write(@NetOut, Health);
1029 e_Buffer_Write(@NetOut, Armor);
1030 e_Buffer_Write(@NetOut, Air);
1031 e_Buffer_Write(@NetOut, JetFuel);
1032 e_Buffer_Write(@NetOut, Lives);
1033 e_Buffer_Write(@NetOut, Team);
1035 for I := WEAPON_KASTET to WEAPON_SUPERPULEMET do
1036 e_Buffer_Write(@NetOut, Byte(FWeapon[I]));
1038 for I := A_BULLETS to A_CELLS do
1039 e_Buffer_Write(@NetOut, FAmmo[I]);
1041 for I := A_BULLETS to A_CELLS do
1042 e_Buffer_Write(@NetOut, FMaxAmmo[I]);
1044 for I := MR_SUIT to MR_MAX do
1045 e_Buffer_Write(@NetOut, LongWord(FMegaRulez[I]));
1047 e_Buffer_Write(@NetOut, Byte(R_ITEM_BACKPACK in FRulez));
1048 e_Buffer_Write(@NetOut, Byte(R_KEY_RED in FRulez));
1049 e_Buffer_Write(@NetOut, Byte(R_KEY_GREEN in FRulez));
1050 e_Buffer_Write(@NetOut, Byte(R_KEY_BLUE in FRulez));
1051 e_Buffer_Write(@NetOut, Byte(R_BERSERK in FRulez));
1053 e_Buffer_Write(@NetOut, Frags);
1054 e_Buffer_Write(@NetOut, Death);
1056 e_Buffer_Write(@NetOut, CurrWeap);
1058 e_Buffer_Write(@NetOut, Byte(FSpectator));
1059 e_Buffer_Write(@NetOut, Byte(FGhost));
1060 e_Buffer_Write(@NetOut, Byte(FPhysics));
1061 e_Buffer_Write(@NetOut, Byte(FNoRespawn));
1062 e_Buffer_Write(@NetOut, Byte(FJetpack));
1063 end;
1065 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1066 end;
1068 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
1069 begin
1070 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRDMG));
1071 e_Buffer_Write(@NetOut, PID);
1072 e_Buffer_Write(@NetOut, Kind);
1073 e_Buffer_Write(@NetOut, Attacker);
1074 e_Buffer_Write(@NetOut, Value);
1075 e_Buffer_Write(@NetOut, VX);
1076 e_Buffer_Write(@NetOut, VY);
1078 g_Net_Host_Send(ID, False, NET_CHAN_PLAYER);
1079 end;
1081 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
1082 begin
1083 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRDIE));
1084 e_Buffer_Write(@NetOut, PID);
1085 e_Buffer_Write(@NetOut, KillType);
1086 e_Buffer_Write(@NetOut, DeathType);
1087 e_Buffer_Write(@NetOut, Attacker);
1089 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1090 end;
1092 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
1093 begin
1094 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRFIRE));
1095 e_Buffer_Write(@NetOut, PID);
1096 e_Buffer_Write(@NetOut, Weapon);
1097 e_Buffer_Write(@NetOut, X);
1098 e_Buffer_Write(@NetOut, Y);
1099 e_Buffer_Write(@NetOut, AX);
1100 e_Buffer_Write(@NetOut, AY);
1101 e_Buffer_Write(@NetOut, ShotID);
1103 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
1104 end;
1106 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
1107 begin
1108 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRDEL));
1109 e_Buffer_Write(@NetOut, PID);
1111 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1112 end;
1114 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
1115 var
1116 Pl: TPlayer;
1117 begin
1118 Pl := g_Player_Get(PID);
1119 if Pl = nil then Exit;
1121 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRSET));
1122 e_Buffer_Write(@NetOut, PID);
1123 e_Buffer_Write(@NetOut, Pl.Name);
1124 if Mdl = '' then
1125 e_Buffer_Write(@NetOut, Pl.Model.Name)
1126 else
1127 e_Buffer_Write(@NetOut, Mdl);
1128 e_Buffer_Write(@NetOut, Pl.FColor.R);
1129 e_Buffer_Write(@NetOut, Pl.FColor.G);
1130 e_Buffer_Write(@NetOut, Pl.FColor.B);
1131 e_Buffer_Write(@NetOut, Pl.Team);
1133 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1134 end;
1136 // ITEM (SEND)
1138 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1139 begin
1140 e_Buffer_Write(@NetOut, Byte(NET_MSG_ISPAWN));
1141 e_Buffer_Write(@NetOut, IID);
1142 e_Buffer_Write(@NetOut, Byte(Quiet));
1143 e_Buffer_Write(@NetOut, gItems[IID].ItemType);
1144 e_Buffer_Write(@NetOut, Byte(gItems[IID].Fall));
1145 e_Buffer_Write(@NetOut, Byte(gItems[IID].Respawnable));
1146 e_Buffer_Write(@NetOut, gItems[IID].Obj.X);
1147 e_Buffer_Write(@NetOut, gItems[IID].Obj.Y);
1148 e_Buffer_Write(@NetOut, gItems[IID].Obj.Vel.X);
1149 e_Buffer_Write(@NetOut, gItems[IID].Obj.Vel.Y);
1151 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1152 end;
1154 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1155 begin
1156 e_Buffer_Write(@NetOut, Byte(NET_MSG_IDEL));
1157 e_Buffer_Write(@NetOut, IID);
1158 e_Buffer_Write(@NetOut, Byte(Quiet));
1160 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1161 end;
1163 // PANEL
1165 procedure MH_SEND_PanelTexture(PType: Word; PID: LongWord; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
1166 var
1167 TP: TPanel;
1168 begin
1169 case PType of
1170 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
1171 TP := gWalls[PID];
1172 PANEL_FORE:
1173 TP := gRenderForegrounds[PID];
1174 PANEL_BACK:
1175 TP := gRenderBackgrounds[PID];
1176 PANEL_WATER:
1177 TP := gWater[PID];
1178 PANEL_ACID1:
1179 TP := gAcid1[PID];
1180 PANEL_ACID2:
1181 TP := gAcid2[PID];
1182 PANEL_STEP:
1183 TP := gSteps[PID];
1184 else
1185 Exit;
1186 end;
1188 with TP do
1189 begin
1190 e_Buffer_Write(@NetOut, Byte(NET_MSG_PTEX));
1191 e_Buffer_Write(@NetOut, PType);
1192 e_Buffer_Write(@NetOut, PID);
1193 e_Buffer_Write(@NetOut, FCurTexture);
1194 e_Buffer_Write(@NetOut, FCurFrame);
1195 e_Buffer_Write(@NetOut, FCurFrameCount);
1196 e_Buffer_Write(@NetOut, AnimLoop);
1197 end;
1199 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1200 end;
1202 procedure MH_SEND_PanelState(PType: Word; PID: LongWord; ID: Integer = NET_EVERYONE);
1203 var
1204 TP: TPanel;
1205 begin
1206 case PType of
1207 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
1208 TP := gWalls[PID];
1209 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
1210 TP := gLifts[PID];
1211 else
1212 Exit;
1213 end;
1215 e_Buffer_Write(@NetOut, Byte(NET_MSG_PSTATE));
1216 e_Buffer_Write(@NetOut, PType);
1217 e_Buffer_Write(@NetOut, PID);
1218 e_Buffer_Write(@NetOut, Byte(TP.Enabled));
1219 e_Buffer_Write(@NetOut, TP.LiftType);
1221 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1222 end;
1224 // TRIGGER
1226 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
1227 begin
1228 if gTriggers = nil then Exit;
1229 if T.Sound = nil then Exit;
1231 e_Buffer_Write(@NetOut, Byte(NET_MSG_TSOUND));
1232 e_Buffer_Write(@NetOut, T.ClientID);
1233 e_Buffer_Write(@NetOut, Byte(T.Sound.IsPlaying));
1234 e_Buffer_Write(@NetOut, LongWord(T.Sound.GetPosition));
1235 e_Buffer_Write(@NetOut, T.SoundPlayCount);
1237 g_Net_Host_Send(ID, True);
1238 end;
1240 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
1241 begin
1242 e_Buffer_Write(@NetOut, Byte(NET_MSG_TMUSIC));
1243 e_Buffer_Write(@NetOut, gMusic.Name);
1244 e_Buffer_Write(@NetOut, Byte(gMusic.IsPlaying));
1245 e_Buffer_Write(@NetOut, LongWord(gMusic.GetPosition));
1246 e_Buffer_Write(@NetOut, Byte(gMusic.SpecPause or gMusic.IsPaused));
1248 g_Net_Host_Send(ID, True);
1249 end;
1251 // MONSTER
1253 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
1254 var
1255 M: TMonster;
1256 begin
1257 M := g_Monsters_Get(UID);
1258 if M = nil then
1259 Exit;
1261 with M do
1262 begin
1263 e_Buffer_Write(@NetOut, Byte(NET_MSG_MSPAWN));
1264 e_Buffer_Write(@NetOut, UID);
1265 e_Buffer_Write(@NetOut, MonsterType);
1266 e_Buffer_Write(@NetOut, MonsterState);
1267 e_Buffer_Write(@NetOut, MonsterAnim);
1268 e_Buffer_Write(@NetOut, MonsterTargetUID);
1269 e_Buffer_Write(@NetOut, MonsterTargetTime);
1270 e_Buffer_Write(@NetOut, MonsterBehaviour);
1271 e_Buffer_Write(@NetOut, MonsterSleep);
1272 e_Buffer_Write(@NetOut, MonsterHealth);
1273 e_Buffer_Write(@NetOut, MonsterAmmo);
1274 e_Buffer_Write(@NetOut, GameX);
1275 e_Buffer_Write(@NetOut, GameY);
1276 e_Buffer_Write(@NetOut, GameVelX);
1277 e_Buffer_Write(@NetOut, GameVelY);
1278 e_Buffer_Write(@NetOut, Byte(GameDirection));
1279 end;
1281 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1282 end;
1284 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
1285 var
1286 M: TMonster;
1287 begin
1288 M := g_Monsters_Get(UID);
1289 if M = nil then Exit;
1291 e_Buffer_Write(@NetOut, Byte(NET_MSG_MPOS));
1292 e_Buffer_Write(@NetOut, UID);
1294 with M do
1295 begin
1296 e_Buffer_Write(@NetOut, GameX);
1297 e_Buffer_Write(@NetOut, GameY);
1298 e_Buffer_Write(@NetOut, GameVelX);
1299 e_Buffer_Write(@NetOut, GameVelY);
1300 e_Buffer_Write(@NetOut, Byte(GameDirection));
1301 end;
1303 g_Net_Host_Send(ID, False, NET_CHAN_MONSTERPOS);
1304 end;
1306 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
1307 var
1308 M: TMonster;
1309 begin
1310 M := g_Monsters_Get(UID);
1311 if M = nil then Exit;
1313 e_Buffer_Write(@NetOut, Byte(NET_MSG_MSTATE));
1314 e_Buffer_Write(@NetOut, UID);
1316 with M do
1317 begin
1318 e_Buffer_Write(@NetOut, MonsterState);
1319 e_Buffer_Write(@NetOut, ForcedAnim);
1320 e_Buffer_Write(@NetOut, MonsterTargetUID);
1321 e_Buffer_Write(@NetOut, MonsterTargetTime);
1322 e_Buffer_Write(@NetOut, MonsterSleep);
1323 e_Buffer_Write(@NetOut, MonsterHealth);
1324 e_Buffer_Write(@NetOut, MonsterAmmo);
1325 e_Buffer_Write(@NetOut, MonsterPain);
1326 e_Buffer_Write(@NetOut, Byte(AnimIsReverse));
1327 end;
1329 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1330 end;
1332 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
1333 begin
1334 e_Buffer_Write(@NetOut, Byte(NET_MSG_MSHOT));
1335 e_Buffer_Write(@NetOut, UID);
1336 e_Buffer_Write(@NetOut, X);
1337 e_Buffer_Write(@NetOut, Y);
1338 e_Buffer_Write(@NetOut, VX);
1339 e_Buffer_Write(@NetOut, VY);
1341 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1342 end;
1344 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
1345 var
1346 M: TMonster;
1347 begin
1348 M := g_Monsters_Get(UID);
1349 if M = nil then Exit;
1351 e_Buffer_Write(@NetOut, Byte(NET_MSG_MDEL));
1352 e_Buffer_Write(@NetOut, UID);
1354 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1355 end;
1357 // MISC
1359 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
1360 begin
1361 e_Buffer_Write(@NetOut, Byte(NET_MSG_TIME_SYNC));
1362 e_Buffer_Write(@NetOut, Time);
1364 g_Net_Host_Send(ID, False, NET_CHAN_SERVICE);
1365 end;
1367 procedure MH_SEND_VoteEvent(EvType: Byte;
1368 StrArg1: string = 'a'; StrArg2: string = 'b';
1369 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
1370 ID: Integer = NET_EVERYONE);
1371 begin
1372 e_Buffer_Write(@NetOut, Byte(NET_MSG_VOTE_EVENT));
1373 e_Buffer_Write(@NetOut, EvType);
1374 e_Buffer_Write(@NetOut, IntArg1);
1375 e_Buffer_Write(@NetOut, IntArg2);
1376 e_Buffer_Write(@NetOut, StrArg1);
1377 e_Buffer_Write(@NetOut, StrArg2);
1379 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1380 end;
1382 // CLIENT MESSAGES //
1384 // GAME
1386 procedure MC_RECV_Chat(P: Pointer);
1387 var
1388 Txt: string;
1389 Mode: Byte;
1390 begin
1391 Txt := e_Raw_Read_String(P);
1392 Mode := e_Raw_Read_Byte(P);
1394 if Mode <> NET_CHAT_SYSTEM then
1395 begin
1396 if Mode = NET_CHAT_PLAYER then
1397 begin
1398 g_Console_Add(Txt, True);
1399 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
1400 g_Sound_PlayEx('SOUND_GAME_RADIO');
1401 end else
1402 if (Mode = NET_CHAT_TEAM) and (gPlayer1 <> nil) then
1403 begin
1404 if gPlayer1.Team = TEAM_RED then
1405 g_Console_Add(b_Text_Format('\r[Team] ') + Txt, True);
1406 if gPlayer1.Team = TEAM_BLUE then
1407 g_Console_Add(b_Text_Format('\b[Team] ') + Txt, True);
1408 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), MSG_NOTIFY);
1409 g_Sound_PlayEx('SOUND_GAME_RADIO');
1410 end;
1411 end else
1412 g_Console_Add(Txt, True);
1413 end;
1415 procedure MC_RECV_Effect(P: Pointer);
1416 var
1417 Kind: Byte;
1418 X, Y: Integer;
1419 Ang: SmallInt;
1420 Anim: TAnimation;
1421 ID: LongWord;
1422 begin
1423 if not gGameOn then Exit;
1424 Kind := e_Raw_Read_Byte(P);
1425 X := e_Raw_Read_LongInt(P);
1426 Y := e_Raw_Read_LongInt(P);
1427 Ang := e_Raw_Read_SmallInt(P);
1429 case Kind of
1430 NET_GFX_SPARK:
1431 g_GFX_Spark(X, Y, 2 + Random(2), Ang, 0, 0);
1433 NET_GFX_TELE:
1434 begin
1435 if g_Frames_Get(ID, 'FRAMES_TELEPORT') then
1436 begin
1437 Anim := TAnimation.Create(ID, False, 3);
1438 g_GFX_OnceAnim(X, Y, Anim);
1439 Anim.Free();
1440 end;
1441 if Ang = 1 then
1442 g_Sound_PlayExAt('SOUND_GAME_TELEPORT', X, Y);
1443 end;
1445 NET_GFX_EXPLODE:
1446 begin
1447 if g_Frames_Get(ID, 'FRAMES_EXPLODE_ROCKET') then
1448 begin
1449 Anim := TAnimation.Create(ID, False, 6);
1450 Anim.Blending := False;
1451 g_GFX_OnceAnim(X-64, Y-64, Anim);
1452 Anim.Free();
1453 end;
1454 if Ang = 1 then
1455 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEROCKET', X, Y);
1456 end;
1458 NET_GFX_BFGEXPL:
1459 begin
1460 if g_Frames_Get(ID, 'FRAMES_EXPLODE_BFG') then
1461 begin
1462 Anim := TAnimation.Create(ID, False, 6);
1463 Anim.Blending := False;
1464 g_GFX_OnceAnim(X-64, Y-64, Anim);
1465 Anim.Free();
1466 end;
1467 if Ang = 1 then
1468 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEBFG', X, Y);
1469 end;
1471 NET_GFX_BFGHIT:
1472 begin
1473 if g_Frames_Get(ID, 'FRAMES_BFGHIT') then
1474 begin
1475 Anim := TAnimation.Create(ID, False, 4);
1476 g_GFX_OnceAnim(X-32, Y-32, Anim);
1477 Anim.Free();
1478 end;
1479 end;
1481 NET_GFX_FIRE:
1482 begin
1483 if g_Frames_Get(ID, 'FRAMES_FIRE') then
1484 begin
1485 Anim := TAnimation.Create(ID, False, 4);
1486 g_GFX_OnceAnim(X, Y, Anim);
1487 Anim.Free();
1488 end;
1489 if Ang = 1 then
1490 g_Sound_PlayExAt('SOUND_FIRE', X, Y);
1491 end;
1493 NET_GFX_RESPAWN:
1494 begin
1495 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
1496 begin
1497 Anim := TAnimation.Create(ID, False, 4);
1498 g_GFX_OnceAnim(X, Y, Anim);
1499 Anim.Free();
1500 end;
1501 if Ang = 1 then
1502 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
1503 end;
1505 NET_GFX_SHELL1:
1506 g_Player_CreateShell(X, Y, 0, -2, SHELL_BULLET);
1508 NET_GFX_SHELL2:
1509 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1511 NET_GFX_SHELL3:
1512 begin
1513 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1514 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1515 end;
1516 end;
1517 end;
1519 procedure MC_RECV_Sound(P: Pointer);
1520 var
1521 Name: string;
1522 X, Y: Integer;
1523 begin
1524 Name := e_Raw_Read_String(P);
1525 X := e_Raw_Read_LongInt(P);
1526 Y := e_Raw_Read_LongInt(P);
1527 g_Sound_PlayExAt(Name, X, Y);
1528 end;
1530 procedure MC_RECV_CreateShot(P: Pointer);
1531 var
1532 I, X, Y, XV, YV: Integer;
1533 Timeout: LongWord;
1534 Target, Spawner: Word;
1535 ShType: Byte;
1536 begin
1537 I := e_Raw_Read_LongInt(P);
1538 ShType := e_Raw_Read_Byte(P);
1539 Target := e_Raw_Read_Word(P);
1540 Spawner := e_Raw_Read_Word(P);
1541 Timeout := e_Raw_Read_LongWord(P);
1542 X := e_Raw_Read_LongInt(P);
1543 Y := e_Raw_Read_LongInt(P);
1544 XV := e_Raw_Read_LongInt(P);
1545 YV := e_Raw_Read_LongInt(P);
1547 I := g_Weapon_CreateShot(I, ShType, Spawner, Target, X, Y, XV, YV);
1548 if (Shots <> nil) and (I <= High(Shots)) then
1549 begin
1550 Shots[I].Timeout := Timeout;
1551 //Shots[I].Target := Target; // TODO: find a use for Target later
1552 end;
1553 end;
1555 procedure MC_RECV_UpdateShot(P: Pointer);
1556 var
1557 I, TX, TY, TXV, TYV: Integer;
1558 begin
1559 I := e_Raw_Read_LongInt(P);
1560 TX := e_Raw_Read_LongInt(P);
1561 TY := e_Raw_Read_LongInt(P);
1562 TXV := e_Raw_Read_LongInt(P);
1563 TYV := e_Raw_Read_LongInt(P);
1565 if (Shots <> nil) and (I <= High(Shots)) then
1566 with (Shots[i]) do
1567 begin
1568 Obj.X := TX;
1569 Obj.Y := TY;
1570 Obj.Vel.X := TXV;
1571 Obj.Vel.Y := TYV;
1572 end;
1573 end;
1575 procedure MC_RECV_DeleteShot(P: Pointer);
1576 var
1577 I, X, Y: Integer;
1578 L: Boolean;
1579 begin
1580 if not gGameOn then Exit;
1581 I := e_Raw_Read_LongInt(P);
1582 L := (e_Raw_Read_Byte(P) <> 0);
1583 X := e_Raw_Read_LongInt(P);
1584 Y := e_Raw_Read_LongInt(P);
1586 g_Weapon_DestroyShot(I, X, Y, L);
1587 end;
1589 procedure MC_RECV_GameStats(P: Pointer);
1590 begin
1591 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
1592 begin
1593 gTeamStat[TEAM_RED].Goals := e_Raw_Read_SmallInt(P);
1594 gTeamStat[TEAM_BLUE].Goals := e_Raw_Read_SmallInt(P);
1595 end
1596 else
1597 if gGameSettings.GameMode = GM_COOP then
1598 begin
1599 gCoopMonstersKilled := e_Raw_Read_Word(P);
1600 gCoopSecretsFound := e_Raw_Read_Word(P);
1601 end;
1602 end;
1604 procedure MC_RECV_CoopStats(P: Pointer);
1605 begin
1606 gTotalMonsters := e_Raw_Read_LongInt(P);
1607 gSecretsCount := e_Raw_Read_LongInt(P);
1608 gCoopTotalMonstersKilled := e_Raw_Read_Word(P);
1609 gCoopTotalSecretsFound := e_Raw_Read_Word(P);
1610 gCoopTotalMonsters := e_Raw_Read_Word(P);
1611 gCoopTotalSecrets := e_Raw_Read_Word(P);
1612 end;
1614 procedure MC_RECV_GameEvent(P: Pointer);
1615 var
1616 EvType: Byte;
1617 EvNum: Integer;
1618 EvStr: string;
1619 EvTime: LongWord;
1620 BHash: Boolean;
1621 EvHash: TMD5Digest;
1622 pl: TPlayer;
1623 i1, i2: TStrings_Locale;
1624 pln: String;
1625 cnt: Byte;
1626 begin
1627 EvType := e_Raw_Read_Byte(P);
1628 EvNum := e_Raw_Read_LongInt(P);
1629 EvStr := e_Raw_Read_String(P);
1630 gLastMap := e_Raw_Read_Byte(P) <> 0;
1631 if gLastMap and (gGameSettings.GameMode = GM_COOP) then gStatsOff := True;
1632 gStatsPressed := True;
1633 EvTime := e_Raw_Read_LongWord(P);
1634 BHash := e_Raw_Read_Byte(P) <> 0;
1635 if BHash then
1636 EvHash := e_Raw_Read_MD5(P);
1638 gTime := EvTime;
1640 case EvType of
1641 NET_EV_MAPSTART:
1642 begin
1643 gGameOn := False;
1644 g_Game_ClearLoading();
1645 g_Game_StopAllSounds(True);
1647 gSwitchGameMode := Byte(EvNum);
1648 gGameSettings.GameMode := gSwitchGameMode;
1650 gWADHash := EvHash;
1651 if not g_Game_StartMap(EvStr, True) then
1652 begin
1653 if Pos(':\', EvStr) = 0 then
1654 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [gGameSettings.WAD + ':\' + EvStr]))
1655 else
1656 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [EvStr]));
1657 Exit;
1658 end;
1660 MC_SEND_FullStateRequest;
1661 end;
1663 NET_EV_MAPEND:
1664 begin
1665 gMissionFailed := EvNum <> 0;
1666 gExit := EXIT_ENDLEVELCUSTOM;
1667 end;
1669 NET_EV_RCON:
1670 begin
1671 case EvNum of
1672 NET_RCON_NOAUTH:
1673 g_Console_Add(_lc[I_NET_RCON_NOAUTH], True);
1674 NET_RCON_PWGOOD:
1675 g_Console_Add(_lc[I_NET_RCON_PWD_VALID], True);
1676 NET_RCON_PWBAD:
1677 g_Console_Add(_lc[I_NET_RCON_PWD_INVALID], True);
1678 end;
1679 end;
1681 NET_EV_CHANGE_TEAM:
1682 begin
1683 if EvNum = TEAM_RED then
1684 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [EvStr]), True);
1685 if EvNum = TEAM_BLUE then
1686 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [EvStr]), True);
1687 end;
1689 NET_EV_PLAYER_KICK:
1690 g_Console_Add(Format(_lc[I_PLAYER_KICK], [EvStr]), True);
1692 NET_EV_PLAYER_BAN:
1693 g_Console_Add(Format(_lc[I_PLAYER_BAN], [EvStr]), True);
1695 NET_EV_LMS_WARMUP:
1696 g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [EvNum]), True);
1698 NET_EV_LMS_SURVIVOR:
1699 g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True);
1701 NET_EV_BIGTEXT:
1702 g_Game_Message(AnsiUpperCase(EvStr), Word(EvNum));
1704 NET_EV_SCORE:
1705 begin
1706 pl := g_Player_Get(EvNum and $FFFF);
1707 if pl = nil then
1708 pln := '?'
1709 else
1710 pln := pl.Name;
1711 cnt := (EvNum shr 16) and $FF;
1712 if Pos('w', EvStr) = 0 then
1713 begin
1714 // Default score
1715 if Pos('t', EvStr) = 0 then
1716 begin
1717 // Player +/- score
1718 if Pos('-', EvStr) = 0 then
1719 begin
1720 if Pos('e', EvStr) = 0 then
1721 i1 := I_PLAYER_SCORE_ADD_OWN
1722 else
1723 i1 := I_PLAYER_SCORE_ADD_ENEMY;
1724 end else
1725 begin
1726 if Pos('e', EvStr) = 0 then
1727 i1 := I_PLAYER_SCORE_SUB_OWN
1728 else
1729 i1 := I_PLAYER_SCORE_SUB_ENEMY;
1730 end;
1731 // Which team
1732 if Pos('r', EvStr) > 0 then
1733 i2 := I_PLAYER_SCORE_TO_RED
1734 else
1735 i2 := I_PLAYER_SCORE_TO_BLUE;
1736 g_Console_Add(Format(_lc[i1], [pln, cnt, _lc[i2]]), True);
1737 end else
1738 begin
1739 // Team +/- score
1740 if Pos('-', EvStr) = 0 then
1741 i1 := I_PLAYER_SCORE_ADD_TEAM
1742 else
1743 i1 := I_PLAYER_SCORE_SUB_TEAM;
1744 // Which team
1745 if Pos('r', EvStr) > 0 then
1746 i2 := I_PLAYER_SCORE_RED
1747 else
1748 i2 := I_PLAYER_SCORE_BLUE;
1749 g_Console_Add(Format(_lc[i1], [_lc[i2], cnt]), True);
1750 end;
1751 end else
1752 begin
1753 // Game Win
1754 if Pos('e', EvStr) = 0 then
1755 i1 := I_PLAYER_SCORE_WIN_OWN
1756 else
1757 i1 := I_PLAYER_SCORE_WIN_ENEMY;
1758 // Which team
1759 if Pos('r', EvStr) > 0 then
1760 i2 := I_PLAYER_SCORE_TO_RED
1761 else
1762 i2 := I_PLAYER_SCORE_TO_BLUE;
1763 g_Console_Add(Format(_lc[i1], [pln, _lc[i2]]), True);
1764 end;
1765 end;
1767 NET_EV_SCORE_MSG:
1768 begin
1769 if EvNum = TEAM_RED then
1770 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1771 if EvNum = TEAM_BLUE then
1772 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1773 if EvNum = -TEAM_RED then
1774 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1775 if EvNum = -TEAM_BLUE then
1776 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1777 end;
1779 NET_EV_LMS_START:
1780 begin
1781 g_Player_RemoveAllCorpses;
1782 g_Game_Message(_lc[I_MESSAGE_LMS_START], 144);
1783 end;
1785 NET_EV_LMS_WIN:
1786 g_Game_Message(Format(_lc[I_MESSAGE_LMS_WIN], [AnsiUpperCase(EvStr)]), 144);
1788 NET_EV_TLMS_WIN:
1789 begin
1790 if EvNum = TEAM_RED then
1791 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 144);
1792 if EvNum = TEAM_BLUE then
1793 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 144);
1794 end;
1796 NET_EV_LMS_LOSE:
1797 g_Game_Message(_lc[I_MESSAGE_LMS_LOSE], 144);
1799 NET_EV_LMS_DRAW:
1800 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
1802 NET_EV_KILLCOMBO:
1803 g_Game_Announce_KillCombo(EvNum);
1805 NET_EV_PLAYER_TOUCH:
1806 begin
1807 pl := g_Player_Get(EvNum);
1808 if pl <> nil then
1809 pl.Touch();
1810 end;
1812 end;
1813 end;
1815 procedure MC_RECV_FlagEvent(P: Pointer);
1816 var
1817 PID: Word;
1818 Pl: TPlayer;
1819 EvType: Byte;
1820 Fl: Byte;
1821 Quiet: Boolean;
1822 s, ts: string;
1823 begin
1824 EvType := e_Raw_Read_Byte(P);
1825 Fl := e_Raw_Read_Byte(P);
1827 if Fl = FLAG_NONE then Exit;
1829 Quiet := (e_Raw_Read_Byte(P) <> 0);
1830 PID := e_Raw_Read_Word(P);
1832 gFlags[Fl].State := e_Raw_Read_Byte(P);
1833 gFlags[Fl].CaptureTime := e_Raw_Read_LongWord(P);
1834 gFlags[Fl].Obj.X := e_Raw_Read_LongInt(P);
1835 gFlags[Fl].Obj.Y := e_Raw_Read_LongInt(P);
1836 gFlags[Fl].Obj.Vel.X := e_Raw_Read_LongInt(P);
1837 gFlags[Fl].Obj.Vel.Y := e_Raw_Read_LongInt(P);
1839 Pl := g_Player_Get(PID);
1840 if (Pl = nil) and
1841 (EvType <> FLAG_STATE_NORMAL) and
1842 (EvType <> FLAG_STATE_DROPPED) and
1843 (EvType <> FLAG_STATE_RETURNED) then
1844 Exit;
1846 case EvType of
1847 FLAG_STATE_NORMAL:
1848 begin
1849 if Quiet or (Pl = nil) then Exit;
1851 if Fl = FLAG_RED then
1852 s := _lc[I_PLAYER_FLAG_RED]
1853 else
1854 s := _lc[I_PLAYER_FLAG_BLUE];
1856 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1857 end;
1859 FLAG_STATE_CAPTURED:
1860 begin
1861 if (Pl <> nil) then Pl.SetFlag(Fl);
1863 if Quiet then Exit;
1865 if Fl = FLAG_RED then
1866 s := _lc[I_PLAYER_FLAG_RED]
1867 else
1868 s := _lc[I_PLAYER_FLAG_BLUE];
1870 g_Console_Add(Format(_lc[I_PLAYER_FLAG_GET], [Pl.Name, s]), True);
1871 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_GET], [AnsiUpperCase(s)]), 144);
1872 end;
1874 FLAG_STATE_DROPPED:
1875 begin
1876 if (Pl <> nil) then Pl.SetFlag(FLAG_NONE);
1878 if Quiet or (Pl = nil) then Exit;
1880 if Fl = FLAG_RED then
1881 s := _lc[I_PLAYER_FLAG_RED]
1882 else
1883 s := _lc[I_PLAYER_FLAG_BLUE];
1885 g_Console_Add(Format(_lc[I_PLAYER_FLAG_DROP], [Pl.Name, s]), True);
1886 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_DROP], [AnsiUpperCase(s)]), 144);
1887 end;
1889 FLAG_STATE_SCORED:
1890 begin
1891 g_Map_ResetFlag(FLAG_RED);
1892 g_Map_ResetFlag(FLAG_BLUE);
1893 if Quiet or (Pl = nil) then Exit;
1894 Pl.SetFlag(FLAG_NONE);
1896 if Fl = FLAG_RED then
1897 s := _lc[I_PLAYER_FLAG_RED]
1898 else
1899 s := _lc[I_PLAYER_FLAG_BLUE];
1901 ts := Format('%.4d', [gFlags[Fl].CaptureTime]);
1902 Insert('.', ts, Length(ts) + 1 - 3);
1903 g_Console_Add(Format(_lc[I_PLAYER_FLAG_CAPTURE], [Pl.Name, s, ts]), True);
1904 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_CAPTURE], [AnsiUpperCase(s)]), 144);
1905 end;
1907 FLAG_STATE_RETURNED:
1908 begin
1909 g_Map_ResetFlag(Fl);
1910 if Quiet then Exit;
1912 if Fl = FLAG_RED then
1913 s := _lc[I_PLAYER_FLAG_RED]
1914 else
1915 s := _lc[I_PLAYER_FLAG_BLUE];
1917 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1918 end;
1919 end;
1920 end;
1922 procedure MC_RECV_GameSettings(P: Pointer);
1923 begin
1924 gGameSettings.GameMode := e_Raw_Read_Byte(P);
1925 gGameSettings.GoalLimit := e_Raw_Read_Word(P);
1926 gGameSettings.TimeLimit := e_Raw_Read_Word(P);
1927 gGameSettings.MaxLives := e_Raw_Read_Byte(P);
1928 gGameSettings.Options := e_Raw_Read_LongWord(P);
1929 end;
1931 // PLAYER
1933 function MC_RECV_PlayerCreate(P: Pointer): Word;
1934 var
1935 PID, DID: Word;
1936 PName, Model: string;
1937 Color: TRGB;
1938 T: Byte;
1939 Pl: TPlayer;
1940 begin
1941 PID := e_Raw_Read_Word(P);
1942 Pl := g_Player_Get(PID);
1944 PName := e_Raw_Read_String(P);
1945 Model := e_Raw_Read_String(P);
1946 Color.R := e_Raw_Read_Byte(P);
1947 Color.G := e_Raw_Read_Byte(P);
1948 Color.B := e_Raw_Read_Byte(P);
1949 T := e_Raw_Read_Byte(P);
1951 Result := 0;
1952 if (PID <> NetPlrUID1) and (PID <> NetPlrUID2) then
1953 begin
1954 if (Pl <> nil) then Exit;
1955 DID := g_Player_Create(Model, Color, T, False);
1956 with g_Player_Get(DID) do
1957 begin
1958 UID := PID;
1959 Name := PName;
1960 Reset(True);
1961 end;
1962 end
1963 else
1964 begin
1965 if (PID = NetPlrUID1) and (gPlayer1 <> nil) then begin
1966 gPlayer1.UID := PID;
1967 gPlayer1.Model.SetColor(Color.R, Color.G, Color.B);
1968 gPlayer1.ChangeTeam(T);
1969 end;
1970 if (PID = NetPlrUID2) and (gPlayer2 <> nil) then begin
1971 gPlayer2.UID := PID;
1972 gPlayer2.Model.SetColor(Color.R, Color.G, Color.B);
1973 gPlayer2.ChangeTeam(T);
1974 end;
1975 end;
1977 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
1978 e_WriteLog('NET: Player ' + PName + ' [' + IntToStr(PID) + '] added.', MSG_NOTIFY);
1979 Result := PID;
1980 end;
1982 function MC_RECV_PlayerPos(P: Pointer): Word;
1983 var
1984 GT: LongWord;
1985 PID: Word;
1986 kByte: Word;
1987 Pl: TPlayer;
1988 Dir: Byte;
1989 TmpX, TmpY: Integer;
1990 begin
1991 Result := 0;
1993 GT := e_Raw_Read_LongWord(P);
1994 if GT < gTime - NET_MAX_DIFFTIME then
1995 begin
1996 gTime := GT;
1997 Exit;
1998 end;
1999 gTime := GT;
2001 PID := e_Raw_Read_Word(P);
2002 Pl := g_Player_Get(PID);
2004 if Pl = nil then Exit;
2006 Result := PID;
2008 with Pl do
2009 begin
2010 FPing := e_Raw_Read_Word(P);
2011 FLoss := e_Raw_Read_Byte(P);
2012 kByte := e_Raw_Read_Word(P);
2013 Dir := e_Raw_Read_Byte(P);
2015 TmpX := e_Raw_Read_LongInt(P);
2016 TmpY := e_Raw_Read_LongInt(P);
2018 ReleaseKeys;
2020 if (kByte = NET_KEY_CHAT) then
2021 PressKey(KEY_CHAT, 10000)
2022 else
2023 begin
2024 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
2025 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
2026 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
2027 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
2028 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
2029 end;
2031 if ((Pl <> gPlayer1) and (Pl <> gPlayer2)) or LongBool(kByte and NET_KEY_FORCEDIR) then
2032 SetDirection(TDirection(Dir));
2034 GameVelX := e_Raw_Read_LongInt(P);
2035 GameVelY := e_Raw_Read_LongInt(P);
2036 GameAccelX := e_Raw_Read_LongInt(P);
2037 GameAccelY := e_Raw_Read_LongInt(P);
2038 SetLerp(TmpX, TmpY);
2039 if NetForcePlayerUpdate then Update();
2040 end;
2041 end;
2043 function MC_RECV_PlayerStats(P: Pointer): Word;
2044 var
2045 PID: Word;
2046 Pl: TPlayer;
2047 I: Integer;
2048 OldJet: Boolean;
2049 NewTeam: Byte;
2050 begin
2051 PID := e_Raw_Read_Word(P);
2052 Pl := g_Player_Get(PID);
2053 Result := 0;
2054 if Pl = nil then
2055 Exit;
2057 with Pl do
2058 begin
2059 Live := (e_Raw_Read_Byte(P) <> 0);
2060 GodMode := (e_Raw_Read_Byte(P) <> 0);
2061 Health := e_Raw_Read_LongInt(P);
2062 Armor := e_Raw_Read_LongInt(P);
2063 Air := e_Raw_Read_LongInt(P);
2064 JetFuel := e_Raw_Read_LongInt(P);
2065 Lives := e_Raw_Read_Byte(P);
2066 NewTeam := e_Raw_Read_Byte(P);
2068 for I := WEAPON_KASTET to WEAPON_SUPERPULEMET do
2069 FWeapon[I] := (e_Raw_Read_Byte(P) <> 0);
2071 for I := A_BULLETS to A_CELLS do
2072 FAmmo[I] := e_Raw_Read_Word(P);
2074 for I := A_BULLETS to A_CELLS do
2075 FMaxAmmo[I] := e_Raw_Read_Word(P);
2077 for I := MR_SUIT to MR_MAX do
2078 FMegaRulez[I] := e_Raw_Read_LongWord(P);
2080 FRulez := [];
2081 if (e_Raw_Read_Byte(P) <> 0) then
2082 FRulez := FRulez + [R_ITEM_BACKPACK];
2083 if (e_Raw_Read_Byte(P) <> 0) then
2084 FRulez := FRulez + [R_KEY_RED];
2085 if (e_Raw_Read_Byte(P) <> 0) then
2086 FRulez := FRulez + [R_KEY_GREEN];
2087 if (e_Raw_Read_Byte(P) <> 0) then
2088 FRulez := FRulez + [R_KEY_BLUE];
2089 if (e_Raw_Read_Byte(P) <> 0) then
2090 FRulez := FRulez + [R_BERSERK];
2092 Frags := e_Raw_Read_LongInt(P);
2093 Death := e_Raw_Read_LongInt(P);
2095 SetWeapon(e_Raw_Read_Byte(P));
2097 FSpectator := e_Raw_Read_Byte(P) <> 0;
2098 if FSpectator then
2099 begin
2100 if Pl = gPlayer1 then
2101 begin
2102 gLMSPID1 := UID;
2103 gPlayer1 := nil;
2104 end;
2105 if Pl = gPlayer2 then
2106 begin
2107 gLMSPID2 := UID;
2108 gPlayer2 := nil;
2109 end;
2110 end
2111 else
2112 begin
2113 if (gPlayer1 = nil) and (gLMSPID1 > 0) then
2114 gPlayer1 := g_Player_Get(gLMSPID1);
2115 if (gPlayer2 = nil) and (gLMSPID2 > 0) then
2116 gPlayer2 := g_Player_Get(gLMSPID2);
2117 end;
2118 FGhost := e_Raw_Read_Byte(P) <> 0;
2119 FPhysics := e_Raw_Read_Byte(P) <> 0;
2120 FNoRespawn := e_Raw_Read_Byte(P) <> 0;
2121 OldJet := FJetpack;
2122 FJetpack := e_Raw_Read_Byte(P) <> 0;
2123 if OldJet and not FJetpack then
2124 JetpackOff
2125 else if not OldJet and FJetpack then
2126 JetpackOn;
2127 if Team <> NewTeam then
2128 Pl.ChangeTeam(NewTeam);
2129 end;
2131 Result := PID;
2132 end;
2134 function MC_RECV_PlayerDamage(P: Pointer): Word;
2135 var
2136 PID: Word;
2137 Pl: TPlayer;
2138 Kind: Byte;
2139 Attacker, Value: Word;
2140 VX, VY: Integer;
2141 begin
2142 Result := 0;
2143 if not gGameOn then Exit;
2144 PID := e_Raw_Read_Word(P);
2145 Pl := g_Player_Get(PID);
2146 if Pl = nil then Exit;
2148 Kind := e_Raw_Read_Byte(P);
2149 Attacker := e_Raw_Read_Word(P);
2150 Value := e_Raw_Read_Word(P);
2151 VX := e_Raw_Read_Word(P);
2152 VY := e_Raw_Read_Word(P);
2154 with Pl do
2155 Damage(Value, Attacker, VX, VY, Kind);
2157 Result := PID;
2158 end;
2160 function MC_RECV_PlayerDeath(P: Pointer): Word;
2161 var
2162 PID: Word;
2163 Pl: TPlayer;
2164 KillType, DeathType: Byte;
2165 Attacker: Word;
2166 begin
2167 Result := 0;
2168 if not gGameOn then Exit;
2169 PID := e_Raw_Read_Word(P);
2170 Pl := g_Player_Get(PID);
2171 if Pl = nil then Exit;
2173 KillType := e_Raw_Read_Byte(P);
2174 DeathType := e_Raw_Read_Byte(P);
2175 Attacker := e_Raw_Read_Word(P);
2177 with Pl do
2178 begin
2179 Kill(KillType, Attacker, DeathType);
2180 SoftReset;
2181 end;
2182 end;
2184 function MC_RECV_PlayerDelete(P: Pointer): Word;
2185 var
2186 PID: Word;
2187 Pl: TPlayer;
2188 begin
2189 PID := e_Raw_Read_Word(P);
2190 Pl := g_Player_Get(PID);
2191 Result := 0;
2192 if Pl = nil then Exit;
2194 g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [Pl.Name]), True);
2195 e_WriteLog('NET: Player ' + Pl.Name + ' [' + IntToStr(PID) + '] removed.', MSG_NOTIFY);
2197 g_Player_Remove(PID);
2199 Result := PID;
2200 end;
2202 function MC_RECV_PlayerFire(P: Pointer): Word;
2203 var
2204 PID: Word;
2205 Weap: Byte;
2206 Pl: TPlayer;
2207 X, Y, AX, AY: Integer;
2208 SHID: Integer;
2209 begin
2210 Result := 0;
2211 if not gGameOn then Exit;
2212 PID := e_Raw_Read_Word(P);
2213 Pl := g_Player_Get(PID);
2214 if Pl = nil then Exit;
2216 Weap := e_Raw_Read_Byte(P);
2217 X := e_Raw_Read_LongInt(P);
2218 Y := e_Raw_Read_LongInt(P);
2219 AX := e_Raw_Read_LongInt(P);
2220 AY := e_Raw_Read_LongInt(P);
2221 SHID := e_Raw_Read_LongInt(P);
2223 with Pl do
2224 if Live then NetFire(Weap, X, Y, AX, AY, SHID);
2225 end;
2227 procedure MC_RECV_PlayerSettings(P: Pointer);
2228 var
2229 TmpName: string;
2230 TmpModel: string;
2231 TmpColor: TRGB;
2232 TmpTeam: Byte;
2233 Pl: TPlayer;
2234 PID: Word;
2235 begin
2236 PID := e_Raw_Read_Word(P);
2237 Pl := g_Player_Get(PID);
2238 if Pl = nil then Exit;
2240 TmpName := e_Raw_Read_String(P);
2241 TmpModel := e_Raw_Read_String(P);
2242 TmpColor.R := e_Raw_Read_Byte(P);
2243 TmpColor.G := e_Raw_Read_Byte(P);
2244 TmpColor.B := e_Raw_Read_Byte(P);
2245 TmpTeam := e_Raw_Read_Byte(P);
2247 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
2248 begin
2249 Pl.ChangeTeam(TmpTeam);
2250 if gPlayer1 = Pl then
2251 gPlayer1Settings.Team := TmpTeam;
2252 if gPlayer2 = Pl then
2253 gPlayer2Settings.Team := TmpTeam;
2254 end else
2255 Pl.SetColor(TmpColor);
2257 if Pl.Name <> TmpName then
2258 begin
2259 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
2260 Pl.Name := TmpName;
2261 end;
2263 if TmpModel <> Pl.Model.Name then
2264 Pl.SetModel(TmpModel);
2265 end;
2267 // ITEM
2269 procedure MC_RECV_ItemSpawn(P: Pointer);
2270 var
2271 ID: Word;
2272 AID: DWord;
2273 X, Y, VX, VY: Integer;
2274 T: Byte;
2275 Quiet, Fall{, Resp}: Boolean;
2276 Anim: TAnimation;
2277 begin
2278 if not gGameOn then Exit;
2279 ID := e_Raw_Read_Word(P);
2280 Quiet := e_Raw_Read_Byte(P) <> 0;
2281 T := e_Raw_Read_Byte(P);
2282 Fall := e_Raw_Read_Byte(P) <> 0;
2283 {Resp :=} e_Raw_Read_Byte(P);
2284 X := e_Raw_Read_LongInt(P);
2285 Y := e_Raw_Read_LongInt(P);
2286 VX := e_Raw_Read_LongInt(P);
2287 VY := e_Raw_Read_LongInt(P);
2289 g_Items_Create(X, Y, T, Fall, False, False, ID);
2290 gItems[ID].Obj.Vel.X := VX;
2291 gItems[ID].Obj.Vel.Y := VY;
2293 if not Quiet then
2294 begin
2295 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
2296 if g_Frames_Get(AID, 'FRAMES_ITEM_RESPAWN') then
2297 begin
2298 Anim := TAnimation.Create(AID, False, 4);
2299 g_GFX_OnceAnim(X+(gItems[ID].Obj.Rect.Width div 2)-16, Y+(gItems[ID].Obj.Rect.Height div 2)-16, Anim);
2300 Anim.Free();
2301 end;
2302 end;
2303 end;
2305 procedure MC_RECV_ItemDestroy(P: Pointer);
2306 var
2307 ID: Word;
2308 Quiet: Boolean;
2309 begin
2310 if not gGameOn then Exit;
2311 ID := e_Raw_Read_Word(P);
2312 Quiet := e_Raw_Read_Byte(P) <> 0;
2313 if gItems = nil then Exit;
2314 if (ID > High(gItems)) then Exit;
2316 if not Quiet then
2317 if gSoundEffectsDF then
2318 begin
2319 if gItems[ID].ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
2320 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
2321 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ',
2322 gItems[ID].Obj.X, gItems[ID].Obj.Y)
2323 else
2324 if gItems[ID].ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
2325 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
2326 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_AMMO_BACKPACK] then
2327 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON',
2328 gItems[ID].Obj.X, gItems[ID].Obj.Y)
2329 else
2330 g_Sound_PlayExAt('SOUND_ITEM_GETITEM',
2331 gItems[ID].Obj.X, gItems[ID].Obj.Y);
2332 end
2333 else
2334 begin
2335 if gItems[ID].ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
2336 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
2337 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ',
2338 gItems[ID].Obj.X, gItems[ID].Obj.Y)
2339 else
2340 if gItems[ID].ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
2341 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
2342 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET] then
2343 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON',
2344 gItems[ID].Obj.X, gItems[ID].Obj.Y)
2345 else
2346 g_Sound_PlayExAt('SOUND_ITEM_GETITEM',
2347 gItems[ID].Obj.X, gItems[ID].Obj.Y);
2348 end;
2350 g_Items_Remove(ID);
2351 end;
2353 // PANEL
2355 procedure MC_RECV_PanelTexture(P: Pointer);
2356 var
2357 TP: TPanel;
2358 PType: Word;
2359 ID: LongWord;
2360 Tex, Fr: Integer;
2361 Loop, Cnt: Byte;
2362 begin
2363 if not gGameOn then Exit;
2364 PType := e_Raw_Read_Word(P);
2365 ID := e_Raw_Read_LongWord(P);
2366 Tex := e_Raw_Read_LongInt(P);
2367 Fr := e_Raw_Read_LongInt(P);
2368 Cnt := e_Raw_Read_Byte(P);
2369 Loop := e_Raw_Read_Byte(P);
2371 TP := nil;
2373 case PType of
2374 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2375 if gWalls <> nil then
2376 TP := gWalls[ID];
2377 PANEL_FORE:
2378 if gRenderForegrounds <> nil then
2379 TP := gRenderForegrounds[ID];
2380 PANEL_BACK:
2381 if gRenderBackgrounds <> nil then
2382 TP := gRenderBackgrounds[ID];
2383 PANEL_WATER:
2384 if gWater <> nil then
2385 TP := gWater[ID];
2386 PANEL_ACID1:
2387 if gAcid1 <> nil then
2388 TP := gAcid1[ID];
2389 PANEL_ACID2:
2390 if gAcid2 <> nil then
2391 TP := gAcid2[ID];
2392 PANEL_STEP:
2393 if gSteps <> nil then
2394 TP := gSteps[ID];
2395 else
2396 Exit;
2397 end;
2399 if TP <> nil then
2400 if Loop = 0 then
2401 begin // switch texture
2402 TP.SetTexture(Tex, Loop);
2403 TP.SetFrame(Fr, Cnt);
2404 end else // looped or non-looped animation
2405 TP.NextTexture(Loop);
2406 end;
2408 procedure MC_RECV_PanelState(P: Pointer);
2409 var
2410 ID: LongWord;
2411 E: Boolean;
2412 Lift: Byte;
2413 PType: Word;
2414 begin
2415 if not gGameOn then Exit;
2416 PType := e_Raw_Read_Word(P);
2417 ID := e_Raw_Read_LongWord(P);
2418 E := (e_Raw_Read_Byte(P) <> 0);
2419 Lift := e_Raw_Read_Byte(P);
2421 case PType of
2422 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2423 if E then
2424 g_Map_EnableWall(ID)
2425 else
2426 g_Map_DisableWall(ID);
2428 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
2429 g_Map_SetLift(ID, Lift);
2430 end;
2431 end;
2433 // TRIGGERS
2435 procedure MC_RECV_TriggerSound(P: Pointer);
2436 var
2437 SPlaying: Boolean;
2438 SPos, SID: LongWord;
2439 SCount: LongInt;
2440 I: Integer;
2441 begin
2442 if not gGameOn then Exit;
2443 if gTriggers = nil then Exit;
2445 SID := e_Raw_Read_LongWord(P);
2446 SPlaying := e_Raw_Read_Byte(P) <> 0;
2447 SPos := e_Raw_Read_LongWord(P);
2448 SCount := e_Raw_Read_LongInt(P);
2450 for I := Low(gTriggers) to High(gTriggers) do
2451 if gTriggers[I].TriggerType = TRIGGER_SOUND then
2452 if gTriggers[I].ClientID = SID then
2453 with gTriggers[I] do
2454 begin
2455 if SPlaying then
2456 begin
2457 if Data.Local then
2458 Sound.PlayVolumeAt(X+(Width div 2), Y+(Height div 2), Data.Volume/255.0)
2459 else
2460 Sound.PlayPanVolume((Data.Pan-127.0)/128.0, Data.Volume/255.0);
2461 Sound.SetPosition(SPos);
2462 end
2463 else
2464 if Sound.IsPlaying then Sound.Stop;
2466 SoundPlayCount := SCount;
2467 end;
2468 end;
2470 procedure MC_RECV_TriggerMusic(P: Pointer);
2471 var
2472 MName: string;
2473 MPlaying: Boolean;
2474 MPos: LongWord;
2475 MPaused: Boolean;
2476 begin
2477 if not gGameOn then Exit;
2479 MName := e_Raw_Read_String(P);
2480 MPlaying := e_Raw_Read_Byte(P) <> 0;
2481 MPos := e_Raw_Read_LongWord(P);
2482 MPaused := e_Raw_Read_Byte(P) <> 0;
2484 if MPlaying then
2485 begin
2486 gMusic.SetByName(MName);
2487 gMusic.Play(True);
2488 gMusic.SetPosition(MPos);
2489 gMusic.SpecPause := MPaused;
2490 end
2491 else
2492 if gMusic.IsPlaying then gMusic.Stop;
2493 end;
2495 // MONSTERS
2497 procedure MC_RECV_MonsterSpawn(P: Pointer);
2498 var
2499 ID: Word;
2500 MType, MState, MDir, MAnim, MBehav: Byte;
2501 X, Y, VX, VY, MTargTime, MHealth, MAmmo, MSleep: Integer;
2502 MTarg: Word;
2503 M: TMonster;
2504 begin
2505 ID := e_Raw_Read_Word(P);
2506 M := g_Monsters_Get(ID);
2507 if M <> nil then
2508 Exit;
2510 MType := e_Raw_Read_Byte(P);
2511 MState := e_Raw_Read_Byte(P);
2512 MAnim := e_Raw_Read_Byte(P);
2513 MTarg := e_Raw_Read_Word(P);
2514 MTargTime := e_Raw_Read_LongInt(P);
2515 MBehav := e_Raw_Read_Byte(P);
2516 MSleep := e_Raw_Read_LongInt(P);
2517 MHealth := e_Raw_Read_LongInt(P);
2518 MAmmo := e_Raw_Read_LongInt(P);
2520 X := e_Raw_Read_LongInt(P);
2521 Y := e_Raw_Read_LongInt(P);
2522 VX := e_Raw_Read_LongInt(P);
2523 VY := e_Raw_Read_LongInt(P);
2524 MDir := e_Raw_Read_Byte(P);
2526 g_Monsters_Create(MType, X, Y, TDirection(MDir), False, ID);
2527 M := g_Monsters_Get(ID);
2528 if M = nil then
2529 Exit;
2531 with M do
2532 begin
2533 GameX := X;
2534 GameY := Y;
2535 GameVelX := VX;
2536 GameVelY := VY;
2538 MonsterAnim := MAnim;
2539 MonsterTargetUID := MTarg;
2540 MonsterTargetTime := MTargTime;
2541 MonsterBehaviour := MBehav;
2542 MonsterSleep := MSleep;
2543 MonsterAmmo := MAmmo;
2544 SetHealth(MHealth);
2546 SetState(MState);
2547 end;
2548 end;
2550 procedure MC_RECV_MonsterPos(P: Pointer);
2551 var
2552 M: TMonster;
2553 ID: Word;
2554 begin
2555 ID := e_Raw_Read_Word(P);
2556 M := g_Monsters_Get(ID);
2557 if M = nil then
2558 Exit;
2560 with M do
2561 begin
2562 GameX := e_Raw_Read_LongInt(P);
2563 GameY := e_Raw_Read_LongInt(P);
2564 GameVelX := e_Raw_Read_LongInt(P);
2565 GameVelY := e_Raw_Read_LongInt(P);
2566 GameDirection := TDirection(e_Raw_Read_Byte(P));
2567 end;
2568 end;
2570 procedure MC_RECV_MonsterState(P: Pointer);
2571 var
2572 ID: Integer;
2573 MState, MFAnm: Byte;
2574 M: TMonster;
2575 AnimRevert: Boolean;
2576 begin
2577 ID := e_Raw_Read_Word(P);
2578 M := g_Monsters_Get(ID);
2579 if M = nil then Exit;
2581 MState := e_Raw_Read_Byte(P);
2582 MFAnm := e_Raw_Read_Byte(P);
2584 with M do
2585 begin
2586 MonsterTargetUID := e_Raw_Read_Word(P);
2587 MonsterTargetTime := e_Raw_Read_LongInt(P);
2588 MonsterSleep := e_Raw_Read_LongInt(P);
2589 MonsterHealth := e_Raw_Read_LongInt(P);
2590 MonsterAmmo := e_Raw_Read_LongInt(P);
2591 MonsterPain := e_Raw_Read_LongInt(P);
2592 AnimRevert := e_Raw_Read_Byte(P) <> 0;
2593 RevertAnim(AnimRevert);
2595 if MonsterState <> MState then
2596 begin
2597 if (MState = MONSTATE_GO) and (MonsterState = MONSTATE_SLEEP) then
2598 WakeUpSound;
2599 if (MState = MONSTATE_DIE) then
2600 DieSound;
2601 if (MState = MONSTATE_PAIN) then
2602 MakeBloodSimple(Min(200, MonsterPain));
2603 if (MState = MONSTATE_ATTACK) then
2604 kick(nil);
2605 if (MState = MONSTATE_DEAD) then
2606 SetDeadAnim;
2608 SetState(MState, MFAnm);
2609 end;
2610 end;
2611 end;
2613 procedure MC_RECV_MonsterShot(P: Pointer);
2614 var
2615 ID: Integer;
2616 M: TMonster;
2617 X, Y, VX, VY: Integer;
2618 begin
2619 ID := e_Raw_Read_Word(P);
2621 M := g_Monsters_Get(ID);
2622 if M = nil then Exit;
2624 X := e_Raw_Read_LongInt(P);
2625 Y := e_Raw_Read_LongInt(P);
2626 VX := e_Raw_Read_LongInt(P);
2627 VY := e_Raw_Read_LongInt(P);
2629 M.ClientAttack(X, Y, VX, VY);
2630 end;
2632 procedure MC_RECV_MonsterDelete(P: Pointer);
2633 var
2634 ID: Integer;
2635 M: TMonster;
2636 begin
2637 ID := e_Raw_Read_Word(P);
2638 M := g_Monsters_Get(ID);
2639 if M = nil then Exit;
2641 gMonsters[ID].SetState(5);
2642 gMonsters[ID].MonsterRemoved := True;
2643 end;
2645 procedure MC_RECV_TimeSync(P: Pointer);
2646 var
2647 Time: LongWord;
2648 begin
2649 Time := e_Raw_Read_LongWord(P);
2651 if gState = STATE_INTERCUSTOM then
2652 gServInterTime := Min(Time, 255);
2653 end;
2655 procedure MC_RECV_VoteEvent(P: Pointer);
2656 var
2657 EvID: Byte;
2658 Str1, Str2: string;
2659 Int1, Int2: SmallInt;
2660 begin
2661 EvID := e_Raw_Read_Byte(P);
2662 Int1 := e_Raw_Read_SmallInt(P);
2663 Int2 := e_Raw_Read_SmallInt(P);
2664 Str1 := e_Raw_Read_String(P);
2665 Str2 := e_Raw_Read_String(P);
2667 case EvID of
2668 NET_VE_STARTED:
2669 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_STARTED], [Str1, Str2, Int1]), True);
2670 NET_VE_PASSED:
2671 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_PASSED], [Str1]), True);
2672 NET_VE_FAILED:
2673 g_Console_Add(_lc[I_MESSAGE_VOTE_FAILED], True);
2674 NET_VE_VOTE:
2675 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Str1, Int1, Int2]), True);
2676 NET_VE_INPROGRESS:
2677 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_INPROGRESS], [Str1]), True);
2678 end;
2679 end;
2681 // CLIENT SEND
2683 procedure MC_SEND_Info(Password: string);
2684 begin
2685 e_Buffer_Clear(@NetOut);
2687 e_Buffer_Write(@NetOut, Byte(NET_MSG_INFO));
2688 e_Buffer_Write(@NetOut, GAME_VERSION);
2689 e_Buffer_Write(@NetOut, Password);
2690 e_Buffer_Write(@NetOut, gPlayer1Settings.Name);
2691 e_Buffer_Write(@NetOut, gPlayer1Settings.Model);
2692 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.R);
2693 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.G);
2694 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.B);
2695 e_Buffer_Write(@NetOut, gPlayer1Settings.Team);
2697 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2698 end;
2700 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
2701 begin
2702 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHAT));
2703 e_Buffer_Write(@NetOut, Txt);
2704 e_Buffer_Write(@NetOut, Mode);
2706 g_Net_Client_Send(True, NET_CHAN_CHAT);
2707 end;
2709 procedure MC_SEND_PlayerPos();
2710 var
2711 kByte: Word;
2712 Predict: Boolean;
2713 begin
2714 if not gGameOn then Exit;
2715 if gPlayers = nil then Exit;
2716 if gPlayer1 = nil then Exit;
2718 kByte := 0;
2719 Predict := NetPredictSelf; // and (not NetGotKeys);
2721 if (not gConsoleShow) and (not gChatShow) and (g_ActiveWindow = nil) then
2722 with gGameControls.P1Control do
2723 begin
2724 if e_KeyPressed(KeyLeft) and (not e_KeyPressed(KeyRight)) then
2725 P1MoveButton := 1
2726 else
2727 if (not e_KeyPressed(KeyLeft)) and e_KeyPressed(KeyRight) then
2728 P1MoveButton := 2
2729 else
2730 if (not e_KeyPressed(KeyLeft)) and (not e_KeyPressed(KeyRight)) then
2731 P1MoveButton := 0;
2733 if (P1MoveButton = 2) and e_KeyPressed(KeyLeft) then
2734 gPlayer1.SetDirection(D_LEFT)
2735 else
2736 if (P1MoveButton = 1) and e_KeyPressed(KeyRight) then
2737 gPlayer1.SetDirection(D_RIGHT)
2738 else
2739 if P1MoveButton <> 0 then
2740 gPlayer1.SetDirection(TDirection(P1MoveButton-1));
2742 gPlayer1.ReleaseKeys;
2743 if P1MoveButton = 1 then
2744 begin
2745 kByte := kByte or NET_KEY_LEFT;
2746 if Predict then gPlayer1.PressKey(KEY_LEFT, 10000);
2747 end;
2748 if P1MoveButton = 2 then
2749 begin
2750 kByte := kByte or NET_KEY_RIGHT;
2751 if Predict then gPlayer1.PressKey(KEY_RIGHT, 10000);
2752 end;
2753 if e_KeyPressed(KeyUp) then
2754 begin
2755 kByte := kByte or NET_KEY_UP;
2756 gPlayer1.PressKey(KEY_UP, 10000);
2757 end;
2758 if e_KeyPressed(KeyDown) then
2759 begin
2760 kByte := kByte or NET_KEY_DOWN;
2761 gPlayer1.PressKey(KEY_DOWN, 10000);
2762 end;
2763 if e_KeyPressed(KeyJump) then
2764 begin
2765 kByte := kByte or NET_KEY_JUMP;
2766 // gPlayer1.PressKey(KEY_JUMP, 10000); // TODO: Make a prediction option
2767 end;
2768 if e_KeyPressed(KeyFire) then kByte := kByte or NET_KEY_FIRE;
2769 if e_KeyPressed(KeyOpen) then kByte := kByte or NET_KEY_OPEN;
2770 if e_KeyPressed(KeyNextWeapon) then kByte := kByte or NET_KEY_NW;
2771 if e_KeyPressed(KeyPrevWeapon) then kByte := kByte or NET_KEY_PW;
2772 end
2773 else
2774 kByte := NET_KEY_CHAT;
2776 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRPOS));
2777 e_Buffer_Write(@NetOut, gTime);
2778 e_Buffer_Write(@NetOut, kByte);
2779 e_Buffer_Write(@NetOut, Byte(gPlayer1.Direction));
2780 g_Net_Client_Send(True, NET_CHAN_PLAYERPOS);
2782 //kBytePrev := kByte;
2783 //kDirPrev := gPlayer1.Direction;
2784 end;
2786 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
2787 begin
2788 e_Buffer_Write(@NetOut, Byte(NET_MSG_VOTE_EVENT));
2789 e_Buffer_Write(@NetOut, Byte(Start));
2790 e_Buffer_Write(@NetOut, Command);
2791 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2792 end;
2794 procedure MC_SEND_PlayerSettings();
2795 begin
2796 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRSET));
2797 e_Buffer_Write(@NetOut, gPlayer1Settings.Name);
2798 e_Buffer_Write(@NetOut, gPlayer1Settings.Model);
2799 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.R);
2800 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.G);
2801 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.B);
2802 e_Buffer_Write(@NetOut, gPlayer1Settings.Team);
2804 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2805 end;
2807 procedure MC_SEND_FullStateRequest();
2808 begin
2809 e_Buffer_Write(@NetOut, Byte(NET_MSG_REQFST));
2811 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2812 end;
2814 procedure MC_SEND_CheatRequest(Kind: Byte);
2815 begin
2816 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHEAT));
2817 e_Buffer_Write(@NetOut, Kind);
2819 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2820 end;
2821 procedure MC_SEND_RCONPassword(Password: string);
2822 begin
2823 e_Buffer_Write(@NetOut, Byte(NET_MSG_RCON_AUTH));
2824 e_Buffer_Write(@NetOut, Password);
2826 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2827 end;
2828 procedure MC_SEND_RCONCommand(Cmd: string);
2829 begin
2830 e_Buffer_Write(@NetOut, Byte(NET_MSG_RCON_CMD));
2831 e_Buffer_Write(@NetOut, Cmd);
2833 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2834 end;
2836 // i have no idea why all this stuff is in here
2838 function ReadFile(const FileName: TFileName): AByte;
2839 var
2840 FileStream : TFileStream;
2841 begin
2842 Result := nil;
2843 FileStream:= TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
2844 try
2845 if FileStream.Size>0 then
2846 begin
2847 SetLength(Result, FileStream.Size);
2848 FileStream.Read(Result[0], FileStream.Size);
2849 end;
2850 finally
2851 FileStream.Free;
2852 end;
2853 end;
2855 function CreateMapDataMsg(const FileName: TFileName; ResList: TStringList): TMapDataMsg;
2856 var
2857 i: Integer;
2858 begin
2859 Result.MsgId := NET_MSG_MAP_RESPONSE;
2860 Result.FileData := ReadFile(FileName);
2861 Result.FileSize := Length(Result.FileData);
2863 SetLength(Result.ExternalResources, ResList.Count);
2864 for i:=0 to ResList.Count-1 do
2865 begin
2866 Result.ExternalResources[i].Name := ResList.Strings[i];
2867 Result.ExternalResources[i].md5 := MD5File(GameDir+'/wads/'+ResList.Strings[i]);
2868 end;
2869 end;
2871 procedure ResDataMsgToBytes(var bytes: AByte; const ResData: TResDataMsg);
2872 var
2873 ResultStream: TMemoryStream;
2874 begin
2875 ResultStream := TMemoryStream.Create;
2877 ResultStream.WriteBuffer(ResData.MsgId, SizeOf(ResData.MsgId)); //msgId
2878 ResultStream.WriteBuffer(ResData.FileSize, SizeOf(ResData.FileSize)); //file size
2879 ResultStream.WriteBuffer(ResData.FileData[0], ResData.FileSize); //file data
2881 SetLength(bytes, ResultStream.Size);
2882 ResultStream.Seek(0, soFromBeginning);
2883 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
2885 ResultStream.Free;
2886 end;
2888 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
2889 begin
2890 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
2891 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize));
2892 SetLength(Result.FileData, Result.FileSize);
2893 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize);
2894 end;
2896 procedure MapDataMsgToBytes(var bytes: AByte; const MapDataMsg: TMapDataMsg);
2897 var
2898 ResultStream: TMemoryStream;
2899 resCount: Integer;
2900 begin
2901 resCount := Length(MapDataMsg.ExternalResources);
2903 ResultStream := TMemoryStream.Create;
2905 ResultStream.WriteBuffer(MapDataMsg.MsgId, SizeOf(MapDataMsg.MsgId)); //msgId
2906 ResultStream.WriteBuffer(MapDataMsg.FileSize, SizeOf(MapDataMsg.FileSize)); //file size
2907 ResultStream.WriteBuffer(MapDataMsg.FileData[0], MapDataMsg.FileSize); //file data
2909 ResultStream.WriteBuffer(resCount, SizeOf(resCount)); //res count
2910 ResultStream.WriteBuffer(MapDataMsg.ExternalResources[0], resCount*SizeOf(TExternalResourceInfo)); //res data
2912 SetLength(bytes, ResultStream.Size);
2913 ResultStream.Seek(0, soFromBeginning);
2914 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
2916 ResultStream.Free;
2917 end;
2919 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
2920 var
2921 resCount: Integer;
2922 begin
2923 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
2924 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize)); //file size
2926 SetLength(Result.FileData, Result.FileSize);
2927 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize); //file data
2929 msgStream.ReadBuffer(resCount, SizeOf(resCount)); //res count
2930 SetLength(Result.ExternalResources, resCount);
2932 msgStream.ReadBuffer(Result.ExternalResources[0], resCount * SizeOf(TExternalResourceInfo)); //res data
2933 end;
2935 function IsValidFileName(const S: String): Boolean;
2936 const
2937 Forbidden: set of Char = ['<', '>', '|', '"', ':', '*', '?'];
2938 var
2939 I: Integer;
2940 begin
2941 Result := S <> '';
2942 for I := 1 to Length(S) do
2943 Result := Result and (not(S[I] in Forbidden));
2944 end;
2946 function IsValidFilePath(const S: String): Boolean;
2947 var
2948 I: Integer;
2949 begin
2950 Result := False;
2951 if not IsValidFileName(S) then exit;
2952 if FileExists(S) then exit;
2953 I := LastDelimiter('\/', S);
2954 if (I > 0) then
2955 if (not DirectoryExists(Copy(S, 1, I-1))) then
2956 exit;
2957 Result := True;
2958 end;
2960 procedure MC_SEND_MapRequest();
2961 begin
2962 e_Buffer_Write(@NetOut, Byte(NET_MSG_MAP_REQUEST));
2963 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2964 end;
2966 procedure MC_SEND_ResRequest(const resName: AnsiString);
2967 begin
2968 e_Buffer_Write(@NetOut, Byte(NET_MSG_RES_REQUEST));
2969 e_Buffer_Write(@NetOut, resName);
2970 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2971 end;
2973 procedure MH_RECV_MapRequest(C: pTNetClient; P: Pointer);
2974 var
2975 payload: AByte;
2976 peer: pENetPeer;
2977 mapDataMsg: TMapDataMsg;
2978 begin
2979 e_WriteLog('NET: Received map request from ' +
2980 DecodeIPV4(C.Peer.address.host), MSG_NOTIFY);
2982 mapDataMsg := CreateMapDataMsg(MapsDir + gGameSettings.WAD, gExternalResources);
2983 peer := NetClients[C.ID].Peer;
2985 MapDataMsgToBytes(payload, mapDataMsg);
2986 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
2988 payload := nil;
2989 mapDataMsg.FileData := nil;
2990 mapDataMsg.ExternalResources := nil;
2991 end;
2993 procedure MH_RECV_ResRequest(C: pTNetClient; P: Pointer);
2994 var
2995 payload: AByte;
2996 peer: pENetPeer;
2997 FileName: String;
2998 resDataMsg: TResDataMsg;
2999 begin
3000 FileName := ExtractFileName(e_Raw_Read_String(P));
3001 e_WriteLog('NET: Received res request: ' + FileName +
3002 ' from ' + DecodeIPV4(C.Peer.address.host), MSG_NOTIFY);
3004 if not IsValidFilePath(FileName) then
3005 begin
3006 e_WriteLog('Invalid filename: ' + FileName, MSG_WARNING);
3007 exit;
3008 end;
3010 peer := NetClients[C.ID].Peer;
3012 if gExternalResources.IndexOf(FileName) > -1 then
3013 begin
3014 resDataMsg.MsgId := NET_MSG_RES_RESPONSE;
3015 resDataMsg.FileData := ReadFile(GameDir+'/wads/'+FileName);
3016 resDataMsg.FileSize := Length(resDataMsg.FileData);
3018 ResDataMsgToBytes(payload, resDataMsg);
3019 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3020 end;
3021 end;
3023 end.