DEADSOFTWARE

0dab350037a061849ca1d69a9c5552f8a7006a32
[d2df-sdl.git] / src / game / g_netmsg.pas
1 {$MODE DELPHI}
2 unit g_netmsg;
4 interface
6 uses g_net, g_triggers, Classes, SysUtils, md5;
8 const
9 NET_MSG_INFO = 100;
11 NET_MSG_CHAT = 101;
12 NET_MSG_SND = 102;
13 NET_MSG_GFX = 103;
14 NET_MSG_GEVENT = 104;
15 NET_MSG_SCORE = 105;
16 NET_MSG_COOP = 106;
17 NET_MSG_FLAG = 107;
18 NET_MSG_REQFST = 108;
19 NET_MSG_GSET = 109;
21 NET_MSG_PLR = 111;
22 NET_MSG_PLRPOS = 112;
23 NET_MSG_PLRSTA = 113;
24 NET_MSG_PLRDEL = 114;
25 NET_MSG_PLRDMG = 115;
26 NET_MSG_PLRDIE = 116;
27 NET_MSG_PLRFIRE= 117;
28 NET_MSG_PLRSET = 119;
29 NET_MSG_CHEAT = 120;
31 NET_MSG_ISPAWN = 121;
32 NET_MSG_IDEL = 122;
34 NET_MSG_MSPAWN = 131;
35 NET_MSG_MPOS = 132;
36 NET_MSG_MSTATE = 133;
37 NET_MSG_MSHOT = 134;
38 NET_MSG_MDEL = 135;
40 NET_MSG_PSTATE = 141;
41 NET_MSG_PTEX = 142;
43 NET_MSG_TSOUND = 151;
44 NET_MSG_TMUSIC = 152;
46 NET_MSG_SHDEL = 161;
47 NET_MSG_SHADD = 162;
48 NET_MSG_SHPOS = 163;
50 NET_MSG_RCON_AUTH = 191;
51 NET_MSG_RCON_CMD = 192;
52 NET_MSG_TIME_SYNC = 194;
53 NET_MSG_VOTE_EVENT = 195;
55 NET_MSG_MAP_REQUEST = 201;
56 NET_MSG_MAP_RESPONSE = 202;
57 NET_MSG_RES_REQUEST = 203;
58 NET_MSG_RES_RESPONSE = 204;
60 NET_CHAT_SYSTEM = 0;
61 NET_CHAT_PLAYER = 1;
62 NET_CHAT_TEAM = 2;
64 NET_RCON_NOAUTH = 0;
65 NET_RCON_PWGOOD = 1;
66 NET_RCON_PWBAD = 2;
68 NET_GFX_SPARK = 1;
69 NET_GFX_TELE = 2;
70 NET_GFX_RESPAWN = 3;
71 NET_GFX_FIRE = 4;
72 NET_GFX_EXPLODE = 5;
73 NET_GFX_BFGEXPL = 6;
74 NET_GFX_BFGHIT = 7;
75 NET_GFX_SHELL1 = 8;
76 NET_GFX_SHELL2 = 9;
77 NET_GFX_SHELL3 = 10;
79 NET_EV_MAPSTART = 1;
80 NET_EV_MAPEND = 2;
81 NET_EV_CHANGE_TEAM = 3;
82 NET_EV_PLAYER_KICK = 4;
83 NET_EV_PLAYER_BAN = 5;
84 NET_EV_LMS_WARMUP = 6;
85 NET_EV_LMS_SURVIVOR = 7;
86 NET_EV_RCON = 8;
87 NET_EV_BIGTEXT = 9;
88 NET_EV_SCORE = 10;
89 NET_EV_SCORE_MSG = 11;
90 NET_EV_LMS_START = 12;
91 NET_EV_LMS_WIN = 13;
92 NET_EV_TLMS_WIN = 14;
93 NET_EV_LMS_LOSE = 15;
94 NET_EV_LMS_DRAW = 16;
95 NET_EV_KILLCOMBO = 17;
96 NET_EV_PLAYER_TOUCH = 18;
98 NET_VE_STARTED = 1;
99 NET_VE_PASSED = 2;
100 NET_VE_FAILED = 3;
101 NET_VE_VOTE = 4;
102 NET_VE_REVOKE = 5;
103 NET_VE_INPROGRESS = 6;
105 NET_FLAG_GET = 1;
106 NET_FLAG_DROP = 2;
107 NET_FLAG_CAP = 3;
108 NET_FLAG_RETURN = 4;
110 NET_CHEAT_SUICIDE = 1;
111 NET_CHEAT_SPECTATE = 2;
113 NET_MAX_DIFFTIME = 5000 div 36;
115 // HOST MESSAGES
117 procedure MH_RECV_Info(C: pTNetClient; P: Pointer);
118 procedure MH_RECV_Chat(C: pTNetClient; P: Pointer);
119 procedure MH_RECV_FullStateRequest(C: pTNetClient; P: Pointer);
120 function MH_RECV_PlayerPos(C: pTNetClient; P: Pointer): Word;
121 procedure MH_RECV_PlayerSettings(C: pTNetClient; P: Pointer);
122 procedure MH_RECV_CheatRequest(C: pTNetClient; P: Pointer);
123 procedure MH_RECV_RCONPassword(C: pTNetClient; P: Pointer);
124 procedure MH_RECV_RCONCommand(C: pTNetClient; P: Pointer);
125 procedure MH_RECV_MapRequest(C: pTNetClient; P: Pointer);
126 procedure MH_RECV_ResRequest(C: pTNetClient; P: Pointer);
127 procedure MH_RECV_Vote(C: pTNetClient; P: Pointer);
129 // GAME
130 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
131 procedure MH_SEND_Info(ID: Byte);
132 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
133 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
134 procedure MH_SEND_Sound(X, Y: Integer; Name: string; ID: Integer = NET_EVERYONE);
135 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
136 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
137 procedure MH_SEND_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
138 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
139 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
140 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
141 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
142 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
143 // PLAYER
144 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
145 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
146 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
147 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
148 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
149 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
150 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
151 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
152 // ITEM
153 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
154 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
155 // PANEL
156 procedure MH_SEND_PanelTexture(PType: Word; PID: LongWord; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
157 procedure MH_SEND_PanelState(PType: Word; PID: LongWord; ID: Integer = NET_EVERYONE);
158 // MONSTER
159 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
160 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
161 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
162 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
163 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
164 // TRIGGER
165 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
166 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
167 // MISC
168 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
169 procedure MH_SEND_VoteEvent(EvType: Byte;
170 StrArg1: string = 'a'; StrArg2: string = 'b';
171 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
172 ID: Integer = NET_EVERYONE);
174 // CLIENT MESSAGES //
176 // GAME
177 procedure MC_RECV_Chat(P: Pointer);
178 procedure MC_RECV_Effect(P: Pointer);
179 procedure MC_RECV_Sound(P: Pointer);
180 procedure MC_RECV_GameStats(P: Pointer);
181 procedure MC_RECV_CoopStats(P: Pointer);
182 procedure MC_RECV_GameEvent(P: Pointer);
183 procedure MC_RECV_FlagEvent(P: Pointer);
184 procedure MC_RECV_GameSettings(P: Pointer);
185 // PLAYER
186 function MC_RECV_PlayerCreate(P: Pointer): Word;
187 function MC_RECV_PlayerPos(P: Pointer): Word;
188 function MC_RECV_PlayerStats(P: Pointer): Word;
189 function MC_RECV_PlayerDelete(P: Pointer): Word;
190 function MC_RECV_PlayerDamage(P: Pointer): Word;
191 function MC_RECV_PlayerDeath(P: Pointer): Word;
192 function MC_RECV_PlayerFire(P: Pointer): Word;
193 procedure MC_RECV_PlayerSettings(P: Pointer);
194 // ITEM
195 procedure MC_RECV_ItemSpawn(P: Pointer);
196 procedure MC_RECV_ItemDestroy(P: Pointer);
197 // PANEL
198 procedure MC_RECV_PanelTexture(P: Pointer);
199 procedure MC_RECV_PanelState(P: Pointer);
200 // MONSTER
201 procedure MC_RECV_MonsterSpawn(P: Pointer);
202 procedure MC_RECV_MonsterPos(P: Pointer);
203 procedure MC_RECV_MonsterState(P: Pointer);
204 procedure MC_RECV_MonsterShot(P: Pointer);
205 procedure MC_RECV_MonsterDelete(P: Pointer);
206 // SHOT
207 procedure MC_RECV_CreateShot(P: Pointer);
208 procedure MC_RECV_UpdateShot(P: Pointer);
209 procedure MC_RECV_DeleteShot(P: Pointer);
210 // TRIGGER
211 procedure MC_RECV_TriggerSound(P: Pointer);
212 procedure MC_RECV_TriggerMusic(P: Pointer);
213 // MISC
214 procedure MC_RECV_TimeSync(P: Pointer);
215 procedure MC_RECV_VoteEvent(P: Pointer);
216 // SERVICE
217 procedure MC_SEND_Info(Password: string);
218 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
219 procedure MC_SEND_PlayerPos();
220 procedure MC_SEND_FullStateRequest();
221 procedure MC_SEND_PlayerSettings();
222 procedure MC_SEND_CheatRequest(Kind: Byte);
223 procedure MC_SEND_RCONPassword(Password: string);
224 procedure MC_SEND_RCONCommand(Cmd: string);
225 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
226 // DOWNLOAD
227 procedure MC_SEND_MapRequest();
228 procedure MC_SEND_ResRequest(const resName: AnsiString);
230 type
231 TExternalResourceInfo = record
232 Name: string[255];
233 md5: TMD5Digest;
234 end;
236 TResDataMsg = record
237 MsgId: Byte;
238 FileSize: Integer;
239 FileData: AByte;
240 end;
242 TMapDataMsg = record
243 MsgId: Byte;
244 FileSize: Integer;
245 FileData: AByte;
246 ExternalResources: array of TExternalResourceInfo;
247 end;
249 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
250 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
252 implementation
254 uses
255 Math, ENet, e_input, e_fixedbuffer, e_graphics, e_log,
256 g_textures, g_gfx, g_sound, g_console, g_basic, g_options, g_main,
257 g_game, g_player, g_map, g_panel, g_items, g_weapons, g_phys, g_gui,
258 g_language, g_monsters, g_netmaster, utils, wadreader, 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 FillChar(EvHash, Sizeof(EvHash), 0);
1628 EvType := e_Raw_Read_Byte(P);
1629 EvNum := e_Raw_Read_LongInt(P);
1630 EvStr := e_Raw_Read_String(P);
1631 gLastMap := e_Raw_Read_Byte(P) <> 0;
1632 if gLastMap and (gGameSettings.GameMode = GM_COOP) then gStatsOff := True;
1633 gStatsPressed := True;
1634 EvTime := e_Raw_Read_LongWord(P);
1635 BHash := e_Raw_Read_Byte(P) <> 0;
1636 if BHash then
1637 EvHash := e_Raw_Read_MD5(P);
1639 gTime := EvTime;
1641 case EvType of
1642 NET_EV_MAPSTART:
1643 begin
1644 gGameOn := False;
1645 g_Game_ClearLoading();
1646 g_Game_StopAllSounds(True);
1648 gSwitchGameMode := Byte(EvNum);
1649 gGameSettings.GameMode := gSwitchGameMode;
1651 gWADHash := EvHash;
1652 if not g_Game_StartMap(EvStr, True) then
1653 begin
1654 if Pos(':\', EvStr) = 0 then
1655 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [gGameSettings.WAD + ':\' + EvStr]))
1656 else
1657 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [EvStr]));
1658 Exit;
1659 end;
1661 MC_SEND_FullStateRequest;
1662 end;
1664 NET_EV_MAPEND:
1665 begin
1666 gMissionFailed := EvNum <> 0;
1667 gExit := EXIT_ENDLEVELCUSTOM;
1668 end;
1670 NET_EV_RCON:
1671 begin
1672 case EvNum of
1673 NET_RCON_NOAUTH:
1674 g_Console_Add(_lc[I_NET_RCON_NOAUTH], True);
1675 NET_RCON_PWGOOD:
1676 g_Console_Add(_lc[I_NET_RCON_PWD_VALID], True);
1677 NET_RCON_PWBAD:
1678 g_Console_Add(_lc[I_NET_RCON_PWD_INVALID], True);
1679 end;
1680 end;
1682 NET_EV_CHANGE_TEAM:
1683 begin
1684 if EvNum = TEAM_RED then
1685 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [EvStr]), True);
1686 if EvNum = TEAM_BLUE then
1687 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [EvStr]), True);
1688 end;
1690 NET_EV_PLAYER_KICK:
1691 g_Console_Add(Format(_lc[I_PLAYER_KICK], [EvStr]), True);
1693 NET_EV_PLAYER_BAN:
1694 g_Console_Add(Format(_lc[I_PLAYER_BAN], [EvStr]), True);
1696 NET_EV_LMS_WARMUP:
1697 g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [EvNum]), True);
1699 NET_EV_LMS_SURVIVOR:
1700 g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True);
1702 NET_EV_BIGTEXT:
1703 g_Game_Message(AnsiUpperCase(EvStr), Word(EvNum));
1705 NET_EV_SCORE:
1706 begin
1707 pl := g_Player_Get(EvNum and $FFFF);
1708 if pl = nil then
1709 pln := '?'
1710 else
1711 pln := pl.Name;
1712 cnt := (EvNum shr 16) and $FF;
1713 if Pos('w', EvStr) = 0 then
1714 begin
1715 // Default score
1716 if Pos('t', EvStr) = 0 then
1717 begin
1718 // Player +/- score
1719 if Pos('-', EvStr) = 0 then
1720 begin
1721 if Pos('e', EvStr) = 0 then
1722 i1 := I_PLAYER_SCORE_ADD_OWN
1723 else
1724 i1 := I_PLAYER_SCORE_ADD_ENEMY;
1725 end else
1726 begin
1727 if Pos('e', EvStr) = 0 then
1728 i1 := I_PLAYER_SCORE_SUB_OWN
1729 else
1730 i1 := I_PLAYER_SCORE_SUB_ENEMY;
1731 end;
1732 // Which team
1733 if Pos('r', EvStr) > 0 then
1734 i2 := I_PLAYER_SCORE_TO_RED
1735 else
1736 i2 := I_PLAYER_SCORE_TO_BLUE;
1737 g_Console_Add(Format(_lc[i1], [pln, cnt, _lc[i2]]), True);
1738 end else
1739 begin
1740 // Team +/- score
1741 if Pos('-', EvStr) = 0 then
1742 i1 := I_PLAYER_SCORE_ADD_TEAM
1743 else
1744 i1 := I_PLAYER_SCORE_SUB_TEAM;
1745 // Which team
1746 if Pos('r', EvStr) > 0 then
1747 i2 := I_PLAYER_SCORE_RED
1748 else
1749 i2 := I_PLAYER_SCORE_BLUE;
1750 g_Console_Add(Format(_lc[i1], [_lc[i2], cnt]), True);
1751 end;
1752 end else
1753 begin
1754 // Game Win
1755 if Pos('e', EvStr) = 0 then
1756 i1 := I_PLAYER_SCORE_WIN_OWN
1757 else
1758 i1 := I_PLAYER_SCORE_WIN_ENEMY;
1759 // Which team
1760 if Pos('r', EvStr) > 0 then
1761 i2 := I_PLAYER_SCORE_TO_RED
1762 else
1763 i2 := I_PLAYER_SCORE_TO_BLUE;
1764 g_Console_Add(Format(_lc[i1], [pln, _lc[i2]]), True);
1765 end;
1766 end;
1768 NET_EV_SCORE_MSG:
1769 begin
1770 if EvNum = TEAM_RED then
1771 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1772 if EvNum = TEAM_BLUE then
1773 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1774 if EvNum = -TEAM_RED then
1775 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1776 if EvNum = -TEAM_BLUE then
1777 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1778 end;
1780 NET_EV_LMS_START:
1781 begin
1782 g_Player_RemoveAllCorpses;
1783 g_Game_Message(_lc[I_MESSAGE_LMS_START], 144);
1784 end;
1786 NET_EV_LMS_WIN:
1787 g_Game_Message(Format(_lc[I_MESSAGE_LMS_WIN], [AnsiUpperCase(EvStr)]), 144);
1789 NET_EV_TLMS_WIN:
1790 begin
1791 if EvNum = TEAM_RED then
1792 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 144);
1793 if EvNum = TEAM_BLUE then
1794 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 144);
1795 end;
1797 NET_EV_LMS_LOSE:
1798 g_Game_Message(_lc[I_MESSAGE_LMS_LOSE], 144);
1800 NET_EV_LMS_DRAW:
1801 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
1803 NET_EV_KILLCOMBO:
1804 g_Game_Announce_KillCombo(EvNum);
1806 NET_EV_PLAYER_TOUCH:
1807 begin
1808 pl := g_Player_Get(EvNum);
1809 if pl <> nil then
1810 pl.Touch();
1811 end;
1813 end;
1814 end;
1816 procedure MC_RECV_FlagEvent(P: Pointer);
1817 var
1818 PID: Word;
1819 Pl: TPlayer;
1820 EvType: Byte;
1821 Fl: Byte;
1822 Quiet: Boolean;
1823 s, ts: string;
1824 begin
1825 EvType := e_Raw_Read_Byte(P);
1826 Fl := e_Raw_Read_Byte(P);
1828 if Fl = FLAG_NONE then Exit;
1830 Quiet := (e_Raw_Read_Byte(P) <> 0);
1831 PID := e_Raw_Read_Word(P);
1833 gFlags[Fl].State := e_Raw_Read_Byte(P);
1834 gFlags[Fl].CaptureTime := e_Raw_Read_LongWord(P);
1835 gFlags[Fl].Obj.X := e_Raw_Read_LongInt(P);
1836 gFlags[Fl].Obj.Y := e_Raw_Read_LongInt(P);
1837 gFlags[Fl].Obj.Vel.X := e_Raw_Read_LongInt(P);
1838 gFlags[Fl].Obj.Vel.Y := e_Raw_Read_LongInt(P);
1840 Pl := g_Player_Get(PID);
1841 if (Pl = nil) and
1842 (EvType <> FLAG_STATE_NORMAL) and
1843 (EvType <> FLAG_STATE_DROPPED) and
1844 (EvType <> FLAG_STATE_RETURNED) then
1845 Exit;
1847 case EvType of
1848 FLAG_STATE_NORMAL:
1849 begin
1850 if Quiet or (Pl = nil) then Exit;
1852 if Fl = FLAG_RED then
1853 s := _lc[I_PLAYER_FLAG_RED]
1854 else
1855 s := _lc[I_PLAYER_FLAG_BLUE];
1857 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1858 end;
1860 FLAG_STATE_CAPTURED:
1861 begin
1862 if (Pl <> nil) then Pl.SetFlag(Fl);
1864 if Quiet then Exit;
1866 if Fl = FLAG_RED then
1867 s := _lc[I_PLAYER_FLAG_RED]
1868 else
1869 s := _lc[I_PLAYER_FLAG_BLUE];
1871 g_Console_Add(Format(_lc[I_PLAYER_FLAG_GET], [Pl.Name, s]), True);
1872 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_GET], [AnsiUpperCase(s)]), 144);
1873 end;
1875 FLAG_STATE_DROPPED:
1876 begin
1877 if (Pl <> nil) then Pl.SetFlag(FLAG_NONE);
1879 if Quiet or (Pl = nil) then Exit;
1881 if Fl = FLAG_RED then
1882 s := _lc[I_PLAYER_FLAG_RED]
1883 else
1884 s := _lc[I_PLAYER_FLAG_BLUE];
1886 g_Console_Add(Format(_lc[I_PLAYER_FLAG_DROP], [Pl.Name, s]), True);
1887 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_DROP], [AnsiUpperCase(s)]), 144);
1888 end;
1890 FLAG_STATE_SCORED:
1891 begin
1892 g_Map_ResetFlag(FLAG_RED);
1893 g_Map_ResetFlag(FLAG_BLUE);
1894 if Quiet or (Pl = nil) then Exit;
1895 Pl.SetFlag(FLAG_NONE);
1897 if Fl = FLAG_RED then
1898 s := _lc[I_PLAYER_FLAG_RED]
1899 else
1900 s := _lc[I_PLAYER_FLAG_BLUE];
1902 ts := Format('%.4d', [gFlags[Fl].CaptureTime]);
1903 Insert('.', ts, Length(ts) + 1 - 3);
1904 g_Console_Add(Format(_lc[I_PLAYER_FLAG_CAPTURE], [Pl.Name, s, ts]), True);
1905 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_CAPTURE], [AnsiUpperCase(s)]), 144);
1906 end;
1908 FLAG_STATE_RETURNED:
1909 begin
1910 g_Map_ResetFlag(Fl);
1911 if Quiet then Exit;
1913 if Fl = FLAG_RED then
1914 s := _lc[I_PLAYER_FLAG_RED]
1915 else
1916 s := _lc[I_PLAYER_FLAG_BLUE];
1918 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1919 end;
1920 end;
1921 end;
1923 procedure MC_RECV_GameSettings(P: Pointer);
1924 begin
1925 gGameSettings.GameMode := e_Raw_Read_Byte(P);
1926 gGameSettings.GoalLimit := e_Raw_Read_Word(P);
1927 gGameSettings.TimeLimit := e_Raw_Read_Word(P);
1928 gGameSettings.MaxLives := e_Raw_Read_Byte(P);
1929 gGameSettings.Options := e_Raw_Read_LongWord(P);
1930 end;
1932 // PLAYER
1934 function MC_RECV_PlayerCreate(P: Pointer): Word;
1935 var
1936 PID, DID: Word;
1937 PName, Model: string;
1938 Color: TRGB;
1939 T: Byte;
1940 Pl: TPlayer;
1941 begin
1942 PID := e_Raw_Read_Word(P);
1943 Pl := g_Player_Get(PID);
1945 PName := e_Raw_Read_String(P);
1946 Model := e_Raw_Read_String(P);
1947 Color.R := e_Raw_Read_Byte(P);
1948 Color.G := e_Raw_Read_Byte(P);
1949 Color.B := e_Raw_Read_Byte(P);
1950 T := e_Raw_Read_Byte(P);
1952 Result := 0;
1953 if (PID <> NetPlrUID1) and (PID <> NetPlrUID2) then
1954 begin
1955 if (Pl <> nil) then Exit;
1956 DID := g_Player_Create(Model, Color, T, False);
1957 with g_Player_Get(DID) do
1958 begin
1959 UID := PID;
1960 Name := PName;
1961 Reset(True);
1962 end;
1963 end
1964 else
1965 begin
1966 if (PID = NetPlrUID1) and (gPlayer1 <> nil) then begin
1967 gPlayer1.UID := PID;
1968 gPlayer1.Model.SetColor(Color.R, Color.G, Color.B);
1969 gPlayer1.ChangeTeam(T);
1970 end;
1971 if (PID = NetPlrUID2) and (gPlayer2 <> nil) then begin
1972 gPlayer2.UID := PID;
1973 gPlayer2.Model.SetColor(Color.R, Color.G, Color.B);
1974 gPlayer2.ChangeTeam(T);
1975 end;
1976 end;
1978 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
1979 e_WriteLog('NET: Player ' + PName + ' [' + IntToStr(PID) + '] added.', MSG_NOTIFY);
1980 Result := PID;
1981 end;
1983 function MC_RECV_PlayerPos(P: Pointer): Word;
1984 var
1985 GT: LongWord;
1986 PID: Word;
1987 kByte: Word;
1988 Pl: TPlayer;
1989 Dir: Byte;
1990 TmpX, TmpY: Integer;
1991 begin
1992 Result := 0;
1994 GT := e_Raw_Read_LongWord(P);
1995 if GT < gTime - NET_MAX_DIFFTIME then
1996 begin
1997 gTime := GT;
1998 Exit;
1999 end;
2000 gTime := GT;
2002 PID := e_Raw_Read_Word(P);
2003 Pl := g_Player_Get(PID);
2005 if Pl = nil then Exit;
2007 Result := PID;
2009 with Pl do
2010 begin
2011 FPing := e_Raw_Read_Word(P);
2012 FLoss := e_Raw_Read_Byte(P);
2013 kByte := e_Raw_Read_Word(P);
2014 Dir := e_Raw_Read_Byte(P);
2016 TmpX := e_Raw_Read_LongInt(P);
2017 TmpY := e_Raw_Read_LongInt(P);
2019 ReleaseKeys;
2021 if (kByte = NET_KEY_CHAT) then
2022 PressKey(KEY_CHAT, 10000)
2023 else
2024 begin
2025 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
2026 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
2027 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
2028 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
2029 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
2030 end;
2032 if ((Pl <> gPlayer1) and (Pl <> gPlayer2)) or LongBool(kByte and NET_KEY_FORCEDIR) then
2033 SetDirection(TDirection(Dir));
2035 GameVelX := e_Raw_Read_LongInt(P);
2036 GameVelY := e_Raw_Read_LongInt(P);
2037 GameAccelX := e_Raw_Read_LongInt(P);
2038 GameAccelY := e_Raw_Read_LongInt(P);
2039 SetLerp(TmpX, TmpY);
2040 if NetForcePlayerUpdate then Update();
2041 end;
2042 end;
2044 function MC_RECV_PlayerStats(P: Pointer): Word;
2045 var
2046 PID: Word;
2047 Pl: TPlayer;
2048 I: Integer;
2049 OldJet: Boolean;
2050 NewTeam: Byte;
2051 begin
2052 PID := e_Raw_Read_Word(P);
2053 Pl := g_Player_Get(PID);
2054 Result := 0;
2055 if Pl = nil then
2056 Exit;
2058 with Pl do
2059 begin
2060 Live := (e_Raw_Read_Byte(P) <> 0);
2061 GodMode := (e_Raw_Read_Byte(P) <> 0);
2062 Health := e_Raw_Read_LongInt(P);
2063 Armor := e_Raw_Read_LongInt(P);
2064 Air := e_Raw_Read_LongInt(P);
2065 JetFuel := e_Raw_Read_LongInt(P);
2066 Lives := e_Raw_Read_Byte(P);
2067 NewTeam := e_Raw_Read_Byte(P);
2069 for I := WEAPON_KASTET to WEAPON_SUPERPULEMET do
2070 FWeapon[I] := (e_Raw_Read_Byte(P) <> 0);
2072 for I := A_BULLETS to A_CELLS do
2073 FAmmo[I] := e_Raw_Read_Word(P);
2075 for I := A_BULLETS to A_CELLS do
2076 FMaxAmmo[I] := e_Raw_Read_Word(P);
2078 for I := MR_SUIT to MR_MAX do
2079 FMegaRulez[I] := e_Raw_Read_LongWord(P);
2081 FRulez := [];
2082 if (e_Raw_Read_Byte(P) <> 0) then
2083 FRulez := FRulez + [R_ITEM_BACKPACK];
2084 if (e_Raw_Read_Byte(P) <> 0) then
2085 FRulez := FRulez + [R_KEY_RED];
2086 if (e_Raw_Read_Byte(P) <> 0) then
2087 FRulez := FRulez + [R_KEY_GREEN];
2088 if (e_Raw_Read_Byte(P) <> 0) then
2089 FRulez := FRulez + [R_KEY_BLUE];
2090 if (e_Raw_Read_Byte(P) <> 0) then
2091 FRulez := FRulez + [R_BERSERK];
2093 Frags := e_Raw_Read_LongInt(P);
2094 Death := e_Raw_Read_LongInt(P);
2096 SetWeapon(e_Raw_Read_Byte(P));
2098 FSpectator := e_Raw_Read_Byte(P) <> 0;
2099 if FSpectator then
2100 begin
2101 if Pl = gPlayer1 then
2102 begin
2103 gLMSPID1 := UID;
2104 gPlayer1 := nil;
2105 end;
2106 if Pl = gPlayer2 then
2107 begin
2108 gLMSPID2 := UID;
2109 gPlayer2 := nil;
2110 end;
2111 end
2112 else
2113 begin
2114 if (gPlayer1 = nil) and (gLMSPID1 > 0) then
2115 gPlayer1 := g_Player_Get(gLMSPID1);
2116 if (gPlayer2 = nil) and (gLMSPID2 > 0) then
2117 gPlayer2 := g_Player_Get(gLMSPID2);
2118 end;
2119 FGhost := e_Raw_Read_Byte(P) <> 0;
2120 FPhysics := e_Raw_Read_Byte(P) <> 0;
2121 FNoRespawn := e_Raw_Read_Byte(P) <> 0;
2122 OldJet := FJetpack;
2123 FJetpack := e_Raw_Read_Byte(P) <> 0;
2124 if OldJet and not FJetpack then
2125 JetpackOff
2126 else if not OldJet and FJetpack then
2127 JetpackOn;
2128 if Team <> NewTeam then
2129 Pl.ChangeTeam(NewTeam);
2130 end;
2132 Result := PID;
2133 end;
2135 function MC_RECV_PlayerDamage(P: Pointer): Word;
2136 var
2137 PID: Word;
2138 Pl: TPlayer;
2139 Kind: Byte;
2140 Attacker, Value: Word;
2141 VX, VY: Integer;
2142 begin
2143 Result := 0;
2144 if not gGameOn then Exit;
2145 PID := e_Raw_Read_Word(P);
2146 Pl := g_Player_Get(PID);
2147 if Pl = nil then Exit;
2149 Kind := e_Raw_Read_Byte(P);
2150 Attacker := e_Raw_Read_Word(P);
2151 Value := e_Raw_Read_Word(P);
2152 VX := e_Raw_Read_Word(P);
2153 VY := e_Raw_Read_Word(P);
2155 with Pl do
2156 Damage(Value, Attacker, VX, VY, Kind);
2158 Result := PID;
2159 end;
2161 function MC_RECV_PlayerDeath(P: Pointer): Word;
2162 var
2163 PID: Word;
2164 Pl: TPlayer;
2165 KillType, DeathType: Byte;
2166 Attacker: Word;
2167 begin
2168 Result := 0;
2169 if not gGameOn then Exit;
2170 PID := e_Raw_Read_Word(P);
2171 Pl := g_Player_Get(PID);
2172 if Pl = nil then Exit;
2174 KillType := e_Raw_Read_Byte(P);
2175 DeathType := e_Raw_Read_Byte(P);
2176 Attacker := e_Raw_Read_Word(P);
2178 with Pl do
2179 begin
2180 Kill(KillType, Attacker, DeathType);
2181 SoftReset;
2182 end;
2183 end;
2185 function MC_RECV_PlayerDelete(P: Pointer): Word;
2186 var
2187 PID: Word;
2188 Pl: TPlayer;
2189 begin
2190 PID := e_Raw_Read_Word(P);
2191 Pl := g_Player_Get(PID);
2192 Result := 0;
2193 if Pl = nil then Exit;
2195 g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [Pl.Name]), True);
2196 e_WriteLog('NET: Player ' + Pl.Name + ' [' + IntToStr(PID) + '] removed.', MSG_NOTIFY);
2198 g_Player_Remove(PID);
2200 Result := PID;
2201 end;
2203 function MC_RECV_PlayerFire(P: Pointer): Word;
2204 var
2205 PID: Word;
2206 Weap: Byte;
2207 Pl: TPlayer;
2208 X, Y, AX, AY: Integer;
2209 SHID: Integer;
2210 begin
2211 Result := 0;
2212 if not gGameOn then Exit;
2213 PID := e_Raw_Read_Word(P);
2214 Pl := g_Player_Get(PID);
2215 if Pl = nil then Exit;
2217 Weap := e_Raw_Read_Byte(P);
2218 X := e_Raw_Read_LongInt(P);
2219 Y := e_Raw_Read_LongInt(P);
2220 AX := e_Raw_Read_LongInt(P);
2221 AY := e_Raw_Read_LongInt(P);
2222 SHID := e_Raw_Read_LongInt(P);
2224 with Pl do
2225 if Live then NetFire(Weap, X, Y, AX, AY, SHID);
2226 end;
2228 procedure MC_RECV_PlayerSettings(P: Pointer);
2229 var
2230 TmpName: string;
2231 TmpModel: string;
2232 TmpColor: TRGB;
2233 TmpTeam: Byte;
2234 Pl: TPlayer;
2235 PID: Word;
2236 begin
2237 PID := e_Raw_Read_Word(P);
2238 Pl := g_Player_Get(PID);
2239 if Pl = nil then Exit;
2241 TmpName := e_Raw_Read_String(P);
2242 TmpModel := e_Raw_Read_String(P);
2243 TmpColor.R := e_Raw_Read_Byte(P);
2244 TmpColor.G := e_Raw_Read_Byte(P);
2245 TmpColor.B := e_Raw_Read_Byte(P);
2246 TmpTeam := e_Raw_Read_Byte(P);
2248 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
2249 begin
2250 Pl.ChangeTeam(TmpTeam);
2251 if gPlayer1 = Pl then
2252 gPlayer1Settings.Team := TmpTeam;
2253 if gPlayer2 = Pl then
2254 gPlayer2Settings.Team := TmpTeam;
2255 end else
2256 Pl.SetColor(TmpColor);
2258 if Pl.Name <> TmpName then
2259 begin
2260 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
2261 Pl.Name := TmpName;
2262 end;
2264 if TmpModel <> Pl.Model.Name then
2265 Pl.SetModel(TmpModel);
2266 end;
2268 // ITEM
2270 procedure MC_RECV_ItemSpawn(P: Pointer);
2271 var
2272 ID: Word;
2273 AID: DWord;
2274 X, Y, VX, VY: Integer;
2275 T: Byte;
2276 Quiet, Fall{, Resp}: Boolean;
2277 Anim: TAnimation;
2278 begin
2279 if not gGameOn then Exit;
2280 ID := e_Raw_Read_Word(P);
2281 Quiet := e_Raw_Read_Byte(P) <> 0;
2282 T := e_Raw_Read_Byte(P);
2283 Fall := e_Raw_Read_Byte(P) <> 0;
2284 {Resp :=} e_Raw_Read_Byte(P);
2285 X := e_Raw_Read_LongInt(P);
2286 Y := e_Raw_Read_LongInt(P);
2287 VX := e_Raw_Read_LongInt(P);
2288 VY := e_Raw_Read_LongInt(P);
2290 g_Items_Create(X, Y, T, Fall, False, False, ID);
2291 gItems[ID].Obj.Vel.X := VX;
2292 gItems[ID].Obj.Vel.Y := VY;
2294 if not Quiet then
2295 begin
2296 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
2297 if g_Frames_Get(AID, 'FRAMES_ITEM_RESPAWN') then
2298 begin
2299 Anim := TAnimation.Create(AID, False, 4);
2300 g_GFX_OnceAnim(X+(gItems[ID].Obj.Rect.Width div 2)-16, Y+(gItems[ID].Obj.Rect.Height div 2)-16, Anim);
2301 Anim.Free();
2302 end;
2303 end;
2304 end;
2306 procedure MC_RECV_ItemDestroy(P: Pointer);
2307 var
2308 ID: Word;
2309 Quiet: Boolean;
2310 begin
2311 if not gGameOn then Exit;
2312 ID := e_Raw_Read_Word(P);
2313 Quiet := e_Raw_Read_Byte(P) <> 0;
2314 if gItems = nil then Exit;
2315 if (ID > High(gItems)) then Exit;
2317 if not Quiet then
2318 if gSoundEffectsDF then
2319 begin
2320 if gItems[ID].ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
2321 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
2322 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ',
2323 gItems[ID].Obj.X, gItems[ID].Obj.Y)
2324 else
2325 if gItems[ID].ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
2326 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
2327 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_AMMO_BACKPACK] then
2328 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON',
2329 gItems[ID].Obj.X, gItems[ID].Obj.Y)
2330 else
2331 g_Sound_PlayExAt('SOUND_ITEM_GETITEM',
2332 gItems[ID].Obj.X, gItems[ID].Obj.Y);
2333 end
2334 else
2335 begin
2336 if gItems[ID].ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
2337 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
2338 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ',
2339 gItems[ID].Obj.X, gItems[ID].Obj.Y)
2340 else
2341 if gItems[ID].ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
2342 ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
2343 ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET] then
2344 g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON',
2345 gItems[ID].Obj.X, gItems[ID].Obj.Y)
2346 else
2347 g_Sound_PlayExAt('SOUND_ITEM_GETITEM',
2348 gItems[ID].Obj.X, gItems[ID].Obj.Y);
2349 end;
2351 g_Items_Remove(ID);
2352 end;
2354 // PANEL
2356 procedure MC_RECV_PanelTexture(P: Pointer);
2357 var
2358 TP: TPanel;
2359 PType: Word;
2360 ID: LongWord;
2361 Tex, Fr: Integer;
2362 Loop, Cnt: Byte;
2363 begin
2364 if not gGameOn then Exit;
2365 PType := e_Raw_Read_Word(P);
2366 ID := e_Raw_Read_LongWord(P);
2367 Tex := e_Raw_Read_LongInt(P);
2368 Fr := e_Raw_Read_LongInt(P);
2369 Cnt := e_Raw_Read_Byte(P);
2370 Loop := e_Raw_Read_Byte(P);
2372 TP := nil;
2374 case PType of
2375 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2376 if gWalls <> nil then
2377 TP := gWalls[ID];
2378 PANEL_FORE:
2379 if gRenderForegrounds <> nil then
2380 TP := gRenderForegrounds[ID];
2381 PANEL_BACK:
2382 if gRenderBackgrounds <> nil then
2383 TP := gRenderBackgrounds[ID];
2384 PANEL_WATER:
2385 if gWater <> nil then
2386 TP := gWater[ID];
2387 PANEL_ACID1:
2388 if gAcid1 <> nil then
2389 TP := gAcid1[ID];
2390 PANEL_ACID2:
2391 if gAcid2 <> nil then
2392 TP := gAcid2[ID];
2393 PANEL_STEP:
2394 if gSteps <> nil then
2395 TP := gSteps[ID];
2396 else
2397 Exit;
2398 end;
2400 if TP <> nil then
2401 if Loop = 0 then
2402 begin // switch texture
2403 TP.SetTexture(Tex, Loop);
2404 TP.SetFrame(Fr, Cnt);
2405 end else // looped or non-looped animation
2406 TP.NextTexture(Loop);
2407 end;
2409 procedure MC_RECV_PanelState(P: Pointer);
2410 var
2411 ID: LongWord;
2412 E: Boolean;
2413 Lift: Byte;
2414 PType: Word;
2415 begin
2416 if not gGameOn then Exit;
2417 PType := e_Raw_Read_Word(P);
2418 ID := e_Raw_Read_LongWord(P);
2419 E := (e_Raw_Read_Byte(P) <> 0);
2420 Lift := e_Raw_Read_Byte(P);
2422 case PType of
2423 PANEL_WALL, PANEL_OPENDOOR, PANEL_CLOSEDOOR:
2424 if E then
2425 g_Map_EnableWall(ID)
2426 else
2427 g_Map_DisableWall(ID);
2429 PANEL_LIFTUP, PANEL_LIFTDOWN, PANEL_LIFTLEFT, PANEL_LIFTRIGHT:
2430 g_Map_SetLift(ID, Lift);
2431 end;
2432 end;
2434 // TRIGGERS
2436 procedure MC_RECV_TriggerSound(P: Pointer);
2437 var
2438 SPlaying: Boolean;
2439 SPos, SID: LongWord;
2440 SCount: LongInt;
2441 I: Integer;
2442 begin
2443 if not gGameOn then Exit;
2444 if gTriggers = nil then Exit;
2446 SID := e_Raw_Read_LongWord(P);
2447 SPlaying := e_Raw_Read_Byte(P) <> 0;
2448 SPos := e_Raw_Read_LongWord(P);
2449 SCount := e_Raw_Read_LongInt(P);
2451 for I := Low(gTriggers) to High(gTriggers) do
2452 if gTriggers[I].TriggerType = TRIGGER_SOUND then
2453 if gTriggers[I].ClientID = SID then
2454 with gTriggers[I] do
2455 begin
2456 if SPlaying then
2457 begin
2458 if Data.Local then
2459 Sound.PlayVolumeAt(X+(Width div 2), Y+(Height div 2), Data.Volume/255.0)
2460 else
2461 Sound.PlayPanVolume((Data.Pan-127.0)/128.0, Data.Volume/255.0);
2462 Sound.SetPosition(SPos);
2463 end
2464 else
2465 if Sound.IsPlaying then Sound.Stop;
2467 SoundPlayCount := SCount;
2468 end;
2469 end;
2471 procedure MC_RECV_TriggerMusic(P: Pointer);
2472 var
2473 MName: string;
2474 MPlaying: Boolean;
2475 MPos: LongWord;
2476 MPaused: Boolean;
2477 begin
2478 if not gGameOn then Exit;
2480 MName := e_Raw_Read_String(P);
2481 MPlaying := e_Raw_Read_Byte(P) <> 0;
2482 MPos := e_Raw_Read_LongWord(P);
2483 MPaused := e_Raw_Read_Byte(P) <> 0;
2485 if MPlaying then
2486 begin
2487 gMusic.SetByName(MName);
2488 gMusic.Play(True);
2489 gMusic.SetPosition(MPos);
2490 gMusic.SpecPause := MPaused;
2491 end
2492 else
2493 if gMusic.IsPlaying then gMusic.Stop;
2494 end;
2496 // MONSTERS
2498 procedure MC_RECV_MonsterSpawn(P: Pointer);
2499 var
2500 ID: Word;
2501 MType, MState, MDir, MAnim, MBehav: Byte;
2502 X, Y, VX, VY, MTargTime, MHealth, MAmmo, MSleep: Integer;
2503 MTarg: Word;
2504 M: TMonster;
2505 begin
2506 ID := e_Raw_Read_Word(P);
2507 M := g_Monsters_Get(ID);
2508 if M <> nil then
2509 Exit;
2511 MType := e_Raw_Read_Byte(P);
2512 MState := e_Raw_Read_Byte(P);
2513 MAnim := e_Raw_Read_Byte(P);
2514 MTarg := e_Raw_Read_Word(P);
2515 MTargTime := e_Raw_Read_LongInt(P);
2516 MBehav := e_Raw_Read_Byte(P);
2517 MSleep := e_Raw_Read_LongInt(P);
2518 MHealth := e_Raw_Read_LongInt(P);
2519 MAmmo := e_Raw_Read_LongInt(P);
2521 X := e_Raw_Read_LongInt(P);
2522 Y := e_Raw_Read_LongInt(P);
2523 VX := e_Raw_Read_LongInt(P);
2524 VY := e_Raw_Read_LongInt(P);
2525 MDir := e_Raw_Read_Byte(P);
2527 g_Monsters_Create(MType, X, Y, TDirection(MDir), False, ID);
2528 M := g_Monsters_Get(ID);
2529 if M = nil then
2530 Exit;
2532 with M do
2533 begin
2534 GameX := X;
2535 GameY := Y;
2536 GameVelX := VX;
2537 GameVelY := VY;
2539 MonsterAnim := MAnim;
2540 MonsterTargetUID := MTarg;
2541 MonsterTargetTime := MTargTime;
2542 MonsterBehaviour := MBehav;
2543 MonsterSleep := MSleep;
2544 MonsterAmmo := MAmmo;
2545 SetHealth(MHealth);
2547 SetState(MState);
2548 end;
2549 end;
2551 procedure MC_RECV_MonsterPos(P: Pointer);
2552 var
2553 M: TMonster;
2554 ID: Word;
2555 begin
2556 ID := e_Raw_Read_Word(P);
2557 M := g_Monsters_Get(ID);
2558 if M = nil then
2559 Exit;
2561 with M do
2562 begin
2563 GameX := e_Raw_Read_LongInt(P);
2564 GameY := e_Raw_Read_LongInt(P);
2565 GameVelX := e_Raw_Read_LongInt(P);
2566 GameVelY := e_Raw_Read_LongInt(P);
2567 GameDirection := TDirection(e_Raw_Read_Byte(P));
2568 end;
2569 end;
2571 procedure MC_RECV_MonsterState(P: Pointer);
2572 var
2573 ID: Integer;
2574 MState, MFAnm: Byte;
2575 M: TMonster;
2576 AnimRevert: Boolean;
2577 begin
2578 ID := e_Raw_Read_Word(P);
2579 M := g_Monsters_Get(ID);
2580 if M = nil then Exit;
2582 MState := e_Raw_Read_Byte(P);
2583 MFAnm := e_Raw_Read_Byte(P);
2585 with M do
2586 begin
2587 MonsterTargetUID := e_Raw_Read_Word(P);
2588 MonsterTargetTime := e_Raw_Read_LongInt(P);
2589 MonsterSleep := e_Raw_Read_LongInt(P);
2590 MonsterHealth := e_Raw_Read_LongInt(P);
2591 MonsterAmmo := e_Raw_Read_LongInt(P);
2592 MonsterPain := e_Raw_Read_LongInt(P);
2593 AnimRevert := e_Raw_Read_Byte(P) <> 0;
2594 RevertAnim(AnimRevert);
2596 if MonsterState <> MState then
2597 begin
2598 if (MState = MONSTATE_GO) and (MonsterState = MONSTATE_SLEEP) then
2599 WakeUpSound;
2600 if (MState = MONSTATE_DIE) then
2601 DieSound;
2602 if (MState = MONSTATE_PAIN) then
2603 MakeBloodSimple(Min(200, MonsterPain));
2604 if (MState = MONSTATE_ATTACK) then
2605 kick(nil);
2606 if (MState = MONSTATE_DEAD) then
2607 SetDeadAnim;
2609 SetState(MState, MFAnm);
2610 end;
2611 end;
2612 end;
2614 procedure MC_RECV_MonsterShot(P: Pointer);
2615 var
2616 ID: Integer;
2617 M: TMonster;
2618 X, Y, VX, VY: Integer;
2619 begin
2620 ID := e_Raw_Read_Word(P);
2622 M := g_Monsters_Get(ID);
2623 if M = nil then Exit;
2625 X := e_Raw_Read_LongInt(P);
2626 Y := e_Raw_Read_LongInt(P);
2627 VX := e_Raw_Read_LongInt(P);
2628 VY := e_Raw_Read_LongInt(P);
2630 M.ClientAttack(X, Y, VX, VY);
2631 end;
2633 procedure MC_RECV_MonsterDelete(P: Pointer);
2634 var
2635 ID: Integer;
2636 M: TMonster;
2637 begin
2638 ID := e_Raw_Read_Word(P);
2639 M := g_Monsters_Get(ID);
2640 if M = nil then Exit;
2642 gMonsters[ID].SetState(5);
2643 gMonsters[ID].MonsterRemoved := True;
2644 end;
2646 procedure MC_RECV_TimeSync(P: Pointer);
2647 var
2648 Time: LongWord;
2649 begin
2650 Time := e_Raw_Read_LongWord(P);
2652 if gState = STATE_INTERCUSTOM then
2653 gServInterTime := Min(Time, 255);
2654 end;
2656 procedure MC_RECV_VoteEvent(P: Pointer);
2657 var
2658 EvID: Byte;
2659 Str1, Str2: string;
2660 Int1, Int2: SmallInt;
2661 begin
2662 EvID := e_Raw_Read_Byte(P);
2663 Int1 := e_Raw_Read_SmallInt(P);
2664 Int2 := e_Raw_Read_SmallInt(P);
2665 Str1 := e_Raw_Read_String(P);
2666 Str2 := e_Raw_Read_String(P);
2668 case EvID of
2669 NET_VE_STARTED:
2670 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_STARTED], [Str1, Str2, Int1]), True);
2671 NET_VE_PASSED:
2672 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_PASSED], [Str1]), True);
2673 NET_VE_FAILED:
2674 g_Console_Add(_lc[I_MESSAGE_VOTE_FAILED], True);
2675 NET_VE_VOTE:
2676 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Str1, Int1, Int2]), True);
2677 NET_VE_INPROGRESS:
2678 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_INPROGRESS], [Str1]), True);
2679 end;
2680 end;
2682 // CLIENT SEND
2684 procedure MC_SEND_Info(Password: string);
2685 begin
2686 e_Buffer_Clear(@NetOut);
2688 e_Buffer_Write(@NetOut, Byte(NET_MSG_INFO));
2689 e_Buffer_Write(@NetOut, GAME_VERSION);
2690 e_Buffer_Write(@NetOut, Password);
2691 e_Buffer_Write(@NetOut, gPlayer1Settings.Name);
2692 e_Buffer_Write(@NetOut, gPlayer1Settings.Model);
2693 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.R);
2694 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.G);
2695 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.B);
2696 e_Buffer_Write(@NetOut, gPlayer1Settings.Team);
2698 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2699 end;
2701 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
2702 begin
2703 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHAT));
2704 e_Buffer_Write(@NetOut, Txt);
2705 e_Buffer_Write(@NetOut, Mode);
2707 g_Net_Client_Send(True, NET_CHAN_CHAT);
2708 end;
2710 procedure MC_SEND_PlayerPos();
2711 var
2712 kByte: Word;
2713 Predict: Boolean;
2714 begin
2715 if not gGameOn then Exit;
2716 if gPlayers = nil then Exit;
2717 if gPlayer1 = nil then Exit;
2719 kByte := 0;
2720 Predict := NetPredictSelf; // and (not NetGotKeys);
2722 if (not gConsoleShow) and (not gChatShow) and (g_ActiveWindow = nil) then
2723 with gGameControls.P1Control do
2724 begin
2725 if e_KeyPressed(KeyLeft) and (not e_KeyPressed(KeyRight)) then
2726 P1MoveButton := 1
2727 else
2728 if (not e_KeyPressed(KeyLeft)) and e_KeyPressed(KeyRight) then
2729 P1MoveButton := 2
2730 else
2731 if (not e_KeyPressed(KeyLeft)) and (not e_KeyPressed(KeyRight)) then
2732 P1MoveButton := 0;
2734 if (P1MoveButton = 2) and e_KeyPressed(KeyLeft) then
2735 gPlayer1.SetDirection(D_LEFT)
2736 else
2737 if (P1MoveButton = 1) and e_KeyPressed(KeyRight) then
2738 gPlayer1.SetDirection(D_RIGHT)
2739 else
2740 if P1MoveButton <> 0 then
2741 gPlayer1.SetDirection(TDirection(P1MoveButton-1));
2743 gPlayer1.ReleaseKeys;
2744 if P1MoveButton = 1 then
2745 begin
2746 kByte := kByte or NET_KEY_LEFT;
2747 if Predict then gPlayer1.PressKey(KEY_LEFT, 10000);
2748 end;
2749 if P1MoveButton = 2 then
2750 begin
2751 kByte := kByte or NET_KEY_RIGHT;
2752 if Predict then gPlayer1.PressKey(KEY_RIGHT, 10000);
2753 end;
2754 if e_KeyPressed(KeyUp) then
2755 begin
2756 kByte := kByte or NET_KEY_UP;
2757 gPlayer1.PressKey(KEY_UP, 10000);
2758 end;
2759 if e_KeyPressed(KeyDown) then
2760 begin
2761 kByte := kByte or NET_KEY_DOWN;
2762 gPlayer1.PressKey(KEY_DOWN, 10000);
2763 end;
2764 if e_KeyPressed(KeyJump) then
2765 begin
2766 kByte := kByte or NET_KEY_JUMP;
2767 // gPlayer1.PressKey(KEY_JUMP, 10000); // TODO: Make a prediction option
2768 end;
2769 if e_KeyPressed(KeyFire) then kByte := kByte or NET_KEY_FIRE;
2770 if e_KeyPressed(KeyOpen) then kByte := kByte or NET_KEY_OPEN;
2771 if e_KeyPressed(KeyNextWeapon) then kByte := kByte or NET_KEY_NW;
2772 if e_KeyPressed(KeyPrevWeapon) then kByte := kByte or NET_KEY_PW;
2773 end
2774 else
2775 kByte := NET_KEY_CHAT;
2777 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRPOS));
2778 e_Buffer_Write(@NetOut, gTime);
2779 e_Buffer_Write(@NetOut, kByte);
2780 e_Buffer_Write(@NetOut, Byte(gPlayer1.Direction));
2781 g_Net_Client_Send(True, NET_CHAN_PLAYERPOS);
2783 //kBytePrev := kByte;
2784 //kDirPrev := gPlayer1.Direction;
2785 end;
2787 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
2788 begin
2789 e_Buffer_Write(@NetOut, Byte(NET_MSG_VOTE_EVENT));
2790 e_Buffer_Write(@NetOut, Byte(Start));
2791 e_Buffer_Write(@NetOut, Command);
2792 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2793 end;
2795 procedure MC_SEND_PlayerSettings();
2796 begin
2797 e_Buffer_Write(@NetOut, Byte(NET_MSG_PLRSET));
2798 e_Buffer_Write(@NetOut, gPlayer1Settings.Name);
2799 e_Buffer_Write(@NetOut, gPlayer1Settings.Model);
2800 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.R);
2801 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.G);
2802 e_Buffer_Write(@NetOut, gPlayer1Settings.Color.B);
2803 e_Buffer_Write(@NetOut, gPlayer1Settings.Team);
2805 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2806 end;
2808 procedure MC_SEND_FullStateRequest();
2809 begin
2810 e_Buffer_Write(@NetOut, Byte(NET_MSG_REQFST));
2812 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2813 end;
2815 procedure MC_SEND_CheatRequest(Kind: Byte);
2816 begin
2817 e_Buffer_Write(@NetOut, Byte(NET_MSG_CHEAT));
2818 e_Buffer_Write(@NetOut, Kind);
2820 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2821 end;
2822 procedure MC_SEND_RCONPassword(Password: string);
2823 begin
2824 e_Buffer_Write(@NetOut, Byte(NET_MSG_RCON_AUTH));
2825 e_Buffer_Write(@NetOut, Password);
2827 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2828 end;
2829 procedure MC_SEND_RCONCommand(Cmd: string);
2830 begin
2831 e_Buffer_Write(@NetOut, Byte(NET_MSG_RCON_CMD));
2832 e_Buffer_Write(@NetOut, Cmd);
2834 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2835 end;
2837 // i have no idea why all this stuff is in here
2839 function ReadFile(const FileName: TFileName): AByte;
2840 var
2841 FileStream : TStream;
2842 fname: string;
2843 begin
2844 e_WriteLog(Format('NETWORK: looking for file "%s"', [FileName]), MSG_NOTIFY);
2845 fname := findDiskWad(FileName);
2846 if length(fname) = 0 then
2847 begin
2848 e_WriteLog(Format('NETWORK: file "%s" not found!', [FileName]), MSG_FATALERROR);
2849 SetLength(Result, 0);
2850 exit;
2851 end;
2852 e_WriteLog(Format('NETWORK: found file "%s"', [fname]), MSG_NOTIFY);
2853 Result := nil;
2854 FileStream := openDiskFileRO(fname);
2855 try
2856 if FileStream.Size > 0 then
2857 begin
2858 SetLength(Result, FileStream.Size);
2859 FileStream.Read(Result[0], FileStream.Size);
2860 end;
2861 finally
2862 FileStream.Free;
2863 end;
2864 end;
2866 function CreateMapDataMsg(const FileName: TFileName; ResList: TStringList): TMapDataMsg;
2867 var
2868 i: Integer;
2869 begin
2870 Result.MsgId := NET_MSG_MAP_RESPONSE;
2871 Result.FileData := ReadFile(FileName);
2872 Result.FileSize := Length(Result.FileData);
2874 SetLength(Result.ExternalResources, ResList.Count);
2875 for i:=0 to ResList.Count-1 do
2876 begin
2877 Result.ExternalResources[i].Name := ResList.Strings[i];
2878 Result.ExternalResources[i].md5 := MD5File(GameDir+'/wads/'+ResList.Strings[i]);
2879 end;
2880 end;
2882 procedure ResDataMsgToBytes(var bytes: AByte; const ResData: TResDataMsg);
2883 var
2884 ResultStream: TMemoryStream;
2885 begin
2886 ResultStream := TMemoryStream.Create;
2888 ResultStream.WriteBuffer(ResData.MsgId, SizeOf(ResData.MsgId)); //msgId
2889 ResultStream.WriteBuffer(ResData.FileSize, SizeOf(ResData.FileSize)); //file size
2890 ResultStream.WriteBuffer(ResData.FileData[0], ResData.FileSize); //file data
2892 SetLength(bytes, ResultStream.Size);
2893 ResultStream.Seek(0, soFromBeginning);
2894 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
2896 ResultStream.Free;
2897 end;
2899 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
2900 begin
2901 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
2902 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize));
2903 SetLength(Result.FileData, Result.FileSize);
2904 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize);
2905 end;
2907 procedure MapDataMsgToBytes(var bytes: AByte; const MapDataMsg: TMapDataMsg);
2908 var
2909 ResultStream: TMemoryStream;
2910 resCount: Integer;
2911 begin
2912 resCount := Length(MapDataMsg.ExternalResources);
2914 ResultStream := TMemoryStream.Create;
2916 ResultStream.WriteBuffer(MapDataMsg.MsgId, SizeOf(MapDataMsg.MsgId)); //msgId
2917 ResultStream.WriteBuffer(MapDataMsg.FileSize, SizeOf(MapDataMsg.FileSize)); //file size
2918 ResultStream.WriteBuffer(MapDataMsg.FileData[0], MapDataMsg.FileSize); //file data
2920 ResultStream.WriteBuffer(resCount, SizeOf(resCount)); //res count
2921 ResultStream.WriteBuffer(MapDataMsg.ExternalResources[0], resCount*SizeOf(TExternalResourceInfo)); //res data
2923 SetLength(bytes, ResultStream.Size);
2924 ResultStream.Seek(0, soFromBeginning);
2925 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
2927 ResultStream.Free;
2928 end;
2930 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
2931 var
2932 resCount: Integer;
2933 begin
2934 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
2935 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize)); //file size
2937 SetLength(Result.FileData, Result.FileSize);
2938 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize); //file data
2940 msgStream.ReadBuffer(resCount, SizeOf(resCount)); //res count
2941 SetLength(Result.ExternalResources, resCount);
2943 msgStream.ReadBuffer(Result.ExternalResources[0], resCount * SizeOf(TExternalResourceInfo)); //res data
2944 end;
2946 function IsValidFileName(const S: String): Boolean;
2947 const
2948 Forbidden: set of Char = ['<', '>', '|', '"', ':', '*', '?'];
2949 var
2950 I: Integer;
2951 begin
2952 Result := S <> '';
2953 for I := 1 to Length(S) do
2954 Result := Result and (not(S[I] in Forbidden));
2955 end;
2957 function IsValidFilePath(const S: String): Boolean;
2958 var
2959 I: Integer;
2960 begin
2961 Result := False;
2962 if not IsValidFileName(S) then exit;
2963 if FileExists(S) then exit;
2964 I := LastDelimiter('\/', S);
2965 if (I > 0) then
2966 if (not DirectoryExists(Copy(S, 1, I-1))) then
2967 exit;
2968 Result := True;
2969 end;
2971 procedure MC_SEND_MapRequest();
2972 begin
2973 e_Buffer_Write(@NetOut, Byte(NET_MSG_MAP_REQUEST));
2974 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2975 end;
2977 procedure MC_SEND_ResRequest(const resName: AnsiString);
2978 begin
2979 e_Buffer_Write(@NetOut, Byte(NET_MSG_RES_REQUEST));
2980 e_Buffer_Write(@NetOut, resName);
2981 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2982 end;
2984 procedure MH_RECV_MapRequest(C: pTNetClient; P: Pointer);
2985 var
2986 payload: AByte;
2987 peer: pENetPeer;
2988 mapDataMsg: TMapDataMsg;
2989 begin
2990 e_WriteLog('NET: Received map request from ' +
2991 DecodeIPV4(C.Peer.address.host), MSG_NOTIFY);
2993 mapDataMsg := CreateMapDataMsg(MapsDir + gGameSettings.WAD, gExternalResources);
2994 peer := NetClients[C.ID].Peer;
2996 MapDataMsgToBytes(payload, mapDataMsg);
2997 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
2999 payload := nil;
3000 mapDataMsg.FileData := nil;
3001 mapDataMsg.ExternalResources := nil;
3002 end;
3004 procedure MH_RECV_ResRequest(C: pTNetClient; P: Pointer);
3005 var
3006 payload: AByte;
3007 peer: pENetPeer;
3008 FileName: String;
3009 resDataMsg: TResDataMsg;
3010 begin
3011 FileName := ExtractFileName(e_Raw_Read_String(P));
3012 e_WriteLog('NET: Received res request: ' + FileName +
3013 ' from ' + DecodeIPV4(C.Peer.address.host), MSG_NOTIFY);
3015 if not IsValidFilePath(FileName) then
3016 begin
3017 e_WriteLog('Invalid filename: ' + FileName, MSG_WARNING);
3018 exit;
3019 end;
3021 peer := NetClients[C.ID].Peer;
3023 if gExternalResources.IndexOf(FileName) > -1 then
3024 begin
3025 resDataMsg.MsgId := NET_MSG_RES_RESPONSE;
3026 resDataMsg.FileData := ReadFile(GameDir+'/wads/'+FileName);
3027 resDataMsg.FileSize := Length(resDataMsg.FileData);
3029 ResDataMsgToBytes(payload, resDataMsg);
3030 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3031 end;
3032 end;
3034 end.