DEADSOFTWARE

net: some Preliminary (but half-working) code for new map/resource download scheme
[d2df-sdl.git] / src / game / g_netmsg.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 unit g_netmsg;
18 interface
20 uses e_msg, g_net, g_triggers, Classes, SysUtils, md5;
22 const
23 NET_MSG_INFO = 100;
25 NET_MSG_CHAT = 101;
26 NET_MSG_SND = 102;
27 NET_MSG_GFX = 103;
28 NET_MSG_GEVENT = 104;
29 NET_MSG_SCORE = 105;
30 NET_MSG_COOP = 106;
31 NET_MSG_FLAG = 107;
32 NET_MSG_REQFST = 108;
33 NET_MSG_GSET = 109;
35 NET_MSG_PLR = 111;
36 NET_MSG_PLRPOS = 112;
37 NET_MSG_PLRSTA = 113;
38 NET_MSG_PLRDEL = 114;
39 NET_MSG_PLRDMG = 115;
40 NET_MSG_PLRDIE = 116;
41 NET_MSG_PLRFIRE= 117;
42 NET_MSG_PLRSET = 119;
43 NET_MSG_CHEAT = 120;
45 NET_MSG_ISPAWN = 121;
46 NET_MSG_IDEL = 122;
48 NET_MSG_MSPAWN = 131;
49 NET_MSG_MPOS = 132;
50 NET_MSG_MSTATE = 133;
51 NET_MSG_MSHOT = 134;
52 NET_MSG_MDEL = 135;
54 NET_MSG_PSTATE = 141;
55 NET_MSG_PTEX = 142;
57 NET_MSG_TSOUND = 151;
58 NET_MSG_TMUSIC = 152;
60 NET_MSG_SHDEL = 161;
61 NET_MSG_SHADD = 162;
62 NET_MSG_SHPOS = 163;
64 NET_MSG_RCON_AUTH = 191;
65 NET_MSG_RCON_CMD = 192;
66 NET_MSG_TIME_SYNC = 194;
67 NET_MSG_VOTE_EVENT = 195;
69 NET_MSG_MAP_REQUEST = 201;
70 NET_MSG_MAP_RESPONSE = 202;
71 NET_MSG_RES_REQUEST = 203;
72 NET_MSG_RES_RESPONSE = 204;
74 // chunked file transfers
75 // it goes this way:
76 // client requests file (FILE_REQUEST)
77 // server sends file header info (FILE_HEADER)
78 // client acks chunk -1 (CHUNK_ACK) to initiate transfer, or cancels (FILE_CANCEL)
79 // server start sending data chunks (one at a time, waiting for an ACK for each one)
80 // when client acks the last chunk, transfer is complete
81 // this scheme sux, of course; we can do better by spamming with unreliable unsequenced packets,
82 // and use client acks to drive server sends, but meh... let's do it this way first, and
83 // we can improve it later.
85 // client: request a file
86 NET_MSG_FILE_REQUEST = 210;
87 // server: file info response
88 NET_MSG_FILE_HEADER = 211;
89 // client: request transfer cancellation
90 // server: something went wrong, transfer cancelled, bomb out
91 NET_MSG_FILE_CANCEL = 212;
92 // server: file chunk data
93 NET_MSG_FILE_CHUNK_DATA = 213;
94 // client: file chunk ack
95 NET_MSG_FILE_CHUNK_ACK = 214;
98 NET_CHAT_SYSTEM = 0;
99 NET_CHAT_PLAYER = 1;
100 NET_CHAT_TEAM = 2;
102 NET_RCON_NOAUTH = 0;
103 NET_RCON_PWGOOD = 1;
104 NET_RCON_PWBAD = 2;
106 NET_GFX_SPARK = 1;
107 NET_GFX_TELE = 2;
108 NET_GFX_RESPAWN = 3;
109 NET_GFX_FIRE = 4;
110 NET_GFX_EXPLODE = 5;
111 NET_GFX_BFGEXPL = 6;
112 NET_GFX_BFGHIT = 7;
113 NET_GFX_SHELL1 = 8;
114 NET_GFX_SHELL2 = 9;
115 NET_GFX_SHELL3 = 10;
117 NET_EV_MAPSTART = 1;
118 NET_EV_MAPEND = 2;
119 NET_EV_CHANGE_TEAM = 3;
120 NET_EV_PLAYER_KICK = 4;
121 NET_EV_PLAYER_BAN = 5;
122 NET_EV_LMS_WARMUP = 6;
123 NET_EV_LMS_SURVIVOR = 7;
124 NET_EV_RCON = 8;
125 NET_EV_BIGTEXT = 9;
126 NET_EV_SCORE = 10;
127 NET_EV_SCORE_MSG = 11;
128 NET_EV_LMS_START = 12;
129 NET_EV_LMS_WIN = 13;
130 NET_EV_TLMS_WIN = 14;
131 NET_EV_LMS_LOSE = 15;
132 NET_EV_LMS_DRAW = 16;
133 NET_EV_KILLCOMBO = 17;
134 NET_EV_PLAYER_TOUCH = 18;
135 NET_EV_SECRET = 19;
136 NET_EV_INTER_READY = 20;
138 NET_VE_STARTED = 1;
139 NET_VE_PASSED = 2;
140 NET_VE_FAILED = 3;
141 NET_VE_VOTE = 4;
142 NET_VE_REVOKE = 5;
143 NET_VE_INPROGRESS = 6;
145 NET_FLAG_GET = 1;
146 NET_FLAG_DROP = 2;
147 NET_FLAG_CAP = 3;
148 NET_FLAG_RETURN = 4;
150 NET_CHEAT_SUICIDE = 1;
151 NET_CHEAT_SPECTATE = 2;
152 NET_CHEAT_READY = 3;
154 NET_MAX_DIFFTIME = 5000 div 36;
156 // HOST MESSAGES
158 procedure MH_RECV_Info(C: pTNetClient; var M: TMsg);
159 procedure MH_RECV_Chat(C: pTNetClient; var M: TMsg);
160 procedure MH_RECV_FullStateRequest(C: pTNetClient; var M: TMsg);
161 function MH_RECV_PlayerPos(C: pTNetClient; var M: TMsg): Word;
162 procedure MH_RECV_PlayerSettings(C: pTNetClient; var M: TMsg);
163 procedure MH_RECV_CheatRequest(C: pTNetClient; var M: TMsg);
164 procedure MH_RECV_RCONPassword(C: pTNetClient; var M: TMsg);
165 procedure MH_RECV_RCONCommand(C: pTNetClient; var M: TMsg);
166 procedure MH_RECV_MapRequest(C: pTNetClient; var M: TMsg);
167 procedure MH_RECV_ResRequest(C: pTNetClient; var M: TMsg);
168 procedure MH_RECV_Vote(C: pTNetClient; var M: TMsg);
170 // GAME
171 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
172 procedure MH_SEND_Info(ID: Byte);
173 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
174 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
175 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
176 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
177 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
178 procedure MH_SEND_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
179 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
180 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
181 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
182 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
183 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
184 // PLAYER
185 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
186 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
187 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
188 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
189 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
190 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
191 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
192 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
193 // ITEM
194 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
195 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
196 // PANEL
197 procedure MH_SEND_PanelTexture(PGUID: Integer; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
198 procedure MH_SEND_PanelState(PGUID: Integer; ID: Integer = NET_EVERYONE);
199 // MONSTER
200 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
201 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
202 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
203 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
204 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
205 // TRIGGER
206 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
207 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
208 // MISC
209 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
210 procedure MH_SEND_VoteEvent(EvType: Byte;
211 StrArg1: string = 'a'; StrArg2: string = 'b';
212 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
213 ID: Integer = NET_EVERYONE);
215 // CLIENT MESSAGES //
217 // GAME
218 procedure MC_RECV_Chat(var M: TMsg);
219 procedure MC_RECV_Effect(var M: TMsg);
220 procedure MC_RECV_Sound(var M: TMsg);
221 procedure MC_RECV_GameStats(var M: TMsg);
222 procedure MC_RECV_CoopStats(var M: TMsg);
223 procedure MC_RECV_GameEvent(var M: TMsg);
224 procedure MC_RECV_FlagEvent(var M: TMsg);
225 procedure MC_RECV_GameSettings(var M: TMsg);
226 // PLAYER
227 function MC_RECV_PlayerCreate(var M: TMsg): Word;
228 function MC_RECV_PlayerPos(var M: TMsg): Word;
229 function MC_RECV_PlayerStats(var M: TMsg): Word;
230 function MC_RECV_PlayerDelete(var M: TMsg): Word;
231 function MC_RECV_PlayerDamage(var M: TMsg): Word;
232 function MC_RECV_PlayerDeath(var M: TMsg): Word;
233 function MC_RECV_PlayerFire(var M: TMsg): Word;
234 procedure MC_RECV_PlayerSettings(var M: TMsg);
235 // ITEM
236 procedure MC_RECV_ItemSpawn(var M: TMsg);
237 procedure MC_RECV_ItemDestroy(var M: TMsg);
238 // PANEL
239 procedure MC_RECV_PanelTexture(var M: TMsg);
240 procedure MC_RECV_PanelState(var M: TMsg);
241 // MONSTER
242 procedure MC_RECV_MonsterSpawn(var M: TMsg);
243 procedure MC_RECV_MonsterPos(var M: TMsg);
244 procedure MC_RECV_MonsterState(var M: TMsg);
245 procedure MC_RECV_MonsterShot(var M: TMsg);
246 procedure MC_RECV_MonsterDelete(var M: TMsg);
247 // SHOT
248 procedure MC_RECV_CreateShot(var M: TMsg);
249 procedure MC_RECV_UpdateShot(var M: TMsg);
250 procedure MC_RECV_DeleteShot(var M: TMsg);
251 // TRIGGER
252 procedure MC_RECV_TriggerSound(var M: TMsg);
253 procedure MC_RECV_TriggerMusic(var M: TMsg);
254 // MISC
255 procedure MC_RECV_TimeSync(var M: TMsg);
256 procedure MC_RECV_VoteEvent(var M: TMsg);
257 // SERVICE
258 procedure MC_SEND_Info(Password: string);
259 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
260 procedure MC_SEND_PlayerPos();
261 procedure MC_SEND_FullStateRequest();
262 procedure MC_SEND_PlayerSettings();
263 procedure MC_SEND_CheatRequest(Kind: Byte);
264 procedure MC_SEND_RCONPassword(Password: string);
265 procedure MC_SEND_RCONCommand(Cmd: string);
266 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
267 // DOWNLOAD
268 procedure MC_SEND_MapRequest();
269 procedure MC_SEND_ResRequest(const resName: AnsiString);
272 type
273 TExternalResourceInfo = record
274 Name: string[255];
275 md5: TMD5Digest;
276 end;
278 TResDataMsg = record
279 MsgId: Byte;
280 FileSize: Integer;
281 FileData: AByte;
282 end;
284 TMapDataMsg = record
285 MsgId: Byte;
286 FileSize: Integer;
287 FileData: AByte;
288 ExternalResources: array of TExternalResourceInfo;
289 end;
291 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
292 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
294 function IsValidFileName(const S: String): Boolean;
295 function IsValidFilePath(const S: String): Boolean;
297 implementation
299 uses
300 Math, ENet, e_input, e_graphics, e_log,
301 g_textures, g_gfx, g_sound, g_console, g_basic, g_options, g_main,
302 g_game, g_player, g_map, g_panel, g_items, g_weapons, g_phys, g_gui,
303 g_language, g_monsters, g_netmaster, utils, wadreader, MAPDEF;
305 const
306 NET_KEY_LEFT = 1;
307 NET_KEY_RIGHT = 2;
308 NET_KEY_UP = 4;
309 NET_KEY_DOWN = 8;
310 NET_KEY_JUMP = 16;
311 NET_KEY_FIRE = 32;
312 NET_KEY_OPEN = 64;
313 NET_KEY_NW = 256;
314 NET_KEY_PW = 512;
315 NET_KEY_CHAT = 2048;
316 NET_KEY_FORCEDIR = 4096;
318 //var
319 //kBytePrev: Word = 0;
320 //kDirPrev: TDirection = D_LEFT;
321 //HostGameTime: Word = 0;
323 // HOST MESSAGES //
326 // GAME
328 procedure MH_RECV_Chat(C: pTNetClient; var M: TMsg);
329 var
330 Txt: string;
331 Mode: Byte;
332 PID: Word;
333 Pl: TPlayer;
334 begin
335 PID := C^.Player;
336 Pl := g_Player_Get(PID);
338 Txt := M.ReadString();
339 Mode := M.ReadByte();
340 if (Mode = NET_CHAT_SYSTEM) then
341 Mode := NET_CHAT_PLAYER; // prevent sending system messages from clients
342 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
343 Mode := NET_CHAT_PLAYER; // revert to player chat in non-team game
345 if Pl = nil then
346 MH_SEND_Chat(Txt, Mode)
347 else
348 MH_SEND_Chat(Pl.Name + ': ' + Txt, Mode, IfThen(Mode = NET_CHAT_TEAM, Pl.Team, NET_EVERYONE));
349 end;
351 procedure MH_RECV_Info(C: pTNetClient; var M: TMsg);
352 var
353 Ver, PName, Model, Pw: string;
354 R, G, B, T: Byte;
355 PID: Word;
356 Color: TRGB;
357 I: Integer;
358 begin
359 Ver := M.ReadString();
360 Pw := M.ReadString();
361 PName := M.ReadString();
362 Model := M.ReadString();
363 R := M.ReadByte();
364 G := M.ReadByte();
365 B := M.ReadByte();
366 T := M.ReadByte();
368 if Ver <> GAME_VERSION then
369 begin
370 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
371 _lc[I_NET_DISC_VERSION]);
372 enet_peer_disconnect(C^.Peer, NET_DISC_VERSION);
373 Exit;
374 end;
376 if g_Net_IsHostBanned(C^.Peer^.address.host) then
377 begin
378 if g_Net_IsHostBanned(C^.Peer^.address.host, True) then
379 begin
380 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
381 _lc[I_NET_DISC_BAN]);
382 enet_peer_disconnect(C^.Peer, NET_DISC_BAN);
383 end
384 else
385 begin
386 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
387 _lc[I_NET_DISC_BAN]);
388 enet_peer_disconnect(C^.Peer, NET_DISC_TEMPBAN);
389 end;
390 Exit;
391 end;
393 if NetPassword <> '' then
394 if AnsiLowerCase(NetPassword) <> AnsiLowerCase(Pw) then
395 begin
396 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_MSG_HOST_REJECT] +
397 _lc[I_NET_DISC_PASSWORD]);
398 enet_peer_disconnect(C^.Peer, NET_DISC_PASSWORD);
399 Exit;
400 end;
402 Color.R := R;
403 Color.B := B;
404 Color.G := G;
406 PID := g_Player_Create(Model, Color, T, False);
407 with g_Player_Get(PID) do
408 begin
409 Name := PName;
410 Reset(True);
411 end;
413 C^.Player := PID;
415 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
416 e_WriteLog('NET: Client ' + PName + ' [' + IntToStr(C^.ID) +
417 '] connected. Assigned player #' + IntToStr(PID) + '.', TMsgType.Notify);
419 MH_SEND_Info(C^.ID);
421 with g_Player_Get(PID) do
422 begin
423 Name := PName;
424 FClientID := C^.ID;
425 // round in progress, don't spawn
426 if (gGameSettings.MaxLives > 0) and (gLMSRespawn = LMS_RESPAWN_NONE) then
427 begin
428 Lives := 0;
429 FNoRespawn := True;
430 Spectate;
431 FWantsInGame := True; // TODO: look into this later
432 end
433 else
434 Respawn(gGameSettings.GameType = GT_SINGLE);
435 end;
437 for I := Low(NetClients) to High(NetClients) do
438 begin
439 if NetClients[I].ID = C^.ID then Continue;
440 MH_SEND_PlayerCreate(PID, NetClients[I].ID);
441 MH_SEND_PlayerPos(True, PID, NetClients[I].ID);
442 MH_SEND_PlayerStats(PID, NetClients[I].ID);
443 end;
445 if gState in [STATE_INTERCUSTOM, STATE_FOLD] then
446 MH_SEND_GameEvent(NET_EV_MAPEND, 0, 'N', C^.ID);
448 if NetUseMaster then g_Net_Slist_Update;
449 end;
451 procedure MH_RECV_FullStateRequest(C: pTNetClient; var M: TMsg);
452 begin
453 if gGameOn then
454 MH_SEND_Everything((C^.State = NET_STATE_AUTH), C^.ID)
455 else
456 C^.RequestedFullUpdate := True;
457 end;
459 // PLAYER
461 function MH_RECV_PlayerPos(C: pTNetClient; var M: TMsg): Word;
462 var
463 Dir, i: Byte;
464 WeaponSelect: Word;
465 PID: Word;
466 kByte: Word;
467 Pl: TPlayer;
468 GT: LongWord;
469 begin
470 Result := 0;
471 if not gGameOn then Exit;
473 GT := M.ReadLongWord();
474 PID := C^.Player;
475 Pl := g_Player_Get(PID);
476 if Pl = nil then
477 Exit;
479 if (GT > gTime + NET_MAX_DIFFTIME) or (GT < Pl.NetTime) then Exit;
481 with Pl do
482 begin
483 NetTime := GT;
484 kByte := M.ReadWord();
485 Dir := M.ReadByte();
486 WeaponSelect := M.ReadWord();
487 //e_WriteLog(Format('R:ws=%d', [WeaponSelect]), MSG_WARNING);
488 if Direction <> TDirection(Dir) then
489 JustTeleported := False;
491 SetDirection(TDirection(Dir));
492 ReleaseKeys;
494 if kByte = NET_KEY_CHAT then
495 begin
496 PressKey(KEY_CHAT, 10000);
497 Exit;
498 end;
500 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
501 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
502 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
503 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
504 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
505 if LongBool(kByte and NET_KEY_FIRE) then PressKey(KEY_FIRE, 10000);
506 if LongBool(kByte and NET_KEY_OPEN) then PressKey(KEY_OPEN, 10000);
507 if LongBool(kByte and NET_KEY_NW) then PressKey(KEY_NEXTWEAPON, 10000);
508 if LongBool(kByte and NET_KEY_PW) then PressKey(KEY_PREVWEAPON, 10000);
510 for i := 0 to 15 do
511 begin
512 if (WeaponSelect and Word(1 shl i)) <> 0 then
513 begin
514 //e_WriteLog(Format(' R:wn=%d', [i]), MSG_WARNING);
515 QueueWeaponSwitch(i);
516 end;
517 end;
518 end;
520 // MH_SEND_PlayerPos(False, PID, C^.ID);
521 end;
523 procedure MH_RECV_CheatRequest(C: pTNetClient; var M: TMsg);
524 var
525 CheatKind: Byte;
526 Pl: TPlayer;
527 begin
528 Pl := g_Player_Get(C^.Player);
529 if Pl = nil then Exit;
531 CheatKind := M.ReadByte();
533 case CheatKind of
534 NET_CHEAT_SUICIDE:
535 Pl.Damage(SUICIDE_DAMAGE, Pl.UID, 0, 0, HIT_SELF);
536 NET_CHEAT_SPECTATE:
537 begin
538 if Pl.FSpectator then
539 Pl.Respawn(False)
540 else
541 Pl.Spectate;
542 end;
543 NET_CHEAT_READY:
544 begin
545 if gState <> STATE_INTERCUSTOM then Exit;
546 Pl.FReady := not Pl.FReady;
547 if Pl.FReady then
548 begin
549 MH_SEND_GameEvent(NET_EV_INTER_READY, Pl.UID, 'Y');
550 Inc(gInterReadyCount);
551 end
552 else
553 begin
554 MH_SEND_GameEvent(NET_EV_INTER_READY, Pl.UID, 'N');
555 Dec(gInterReadyCount);
556 end;
557 end;
558 end;
559 end;
561 procedure MH_RECV_PlayerSettings(C: pTNetClient; var M: TMsg);
562 var
563 TmpName: string;
564 TmpModel: string;
565 TmpColor: TRGB;
566 TmpTeam: Byte;
567 Pl: TPlayer;
568 begin
569 TmpName := M.ReadString();
570 TmpModel := M.ReadString();
571 TmpColor.R := M.ReadByte();
572 TmpColor.G := M.ReadByte();
573 TmpColor.B := M.ReadByte();
574 TmpTeam := M.ReadByte();
576 Pl := g_Player_Get(C^.Player);
577 if Pl = nil then Exit;
579 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
580 Pl.SwitchTeam
581 else
582 Pl.SetColor(TmpColor);
584 if Pl.Name <> TmpName then
585 begin
586 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
587 Pl.Name := TmpName;
588 end;
590 if TmpModel <> Pl.Model.Name then
591 Pl.SetModel(TmpModel);
593 MH_SEND_PlayerSettings(Pl.UID, TmpModel);
594 end;
596 // RCON
598 procedure MH_RECV_RCONPassword(C: pTNetClient; var M: TMsg);
599 var
600 Pwd: string;
601 begin
602 Pwd := M.ReadString();
603 if not NetAllowRCON then Exit;
604 if Pwd = NetRCONPassword then
605 begin
606 C^.RCONAuth := True;
607 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWGOOD, 'N', C^.ID);
608 end
609 else
610 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_PWBAD, 'N', C^.ID);
611 end;
613 procedure MH_RECV_RCONCommand(C: pTNetClient; var M: TMsg);
614 var
615 Cmd: string;
616 begin
617 Cmd := M.ReadString();
618 if not NetAllowRCON then Exit;
619 if not C^.RCONAuth then
620 begin
621 MH_SEND_GameEvent(NET_EV_RCON, NET_RCON_NOAUTH, 'N', C^.ID);
622 Exit;
623 end;
624 g_Console_Process(Cmd);
625 end;
627 // MISC
629 procedure MH_RECV_Vote(C: pTNetClient; var M: TMsg);
630 var
631 Start: Boolean;
632 Name, Command: string;
633 Need: Integer;
634 Pl: TPlayer;
635 begin
636 Start := M.ReadByte() <> 0;
637 Command := M.ReadString();
639 Pl := g_Player_Get(C^.Player);
640 if Pl = nil then Exit;
641 Name := Pl.Name;
643 if Start then
644 begin
645 if not g_Console_CommandBlacklisted(Command) then
646 g_Game_StartVote(Command, Name);
647 end
648 else if gVoteInProgress then
649 begin
650 if (gPlayer1 <> nil) or (gPlayer2 <> nil) then
651 Need := Floor((NetClientCount+1)/2.0) + 1
652 else
653 Need := Floor(NetClientCount/2.0) + 1;
654 if C^.Voted then
655 begin
656 Dec(gVoteCount);
657 C^.Voted := False;
658 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_REVOKED], [Name, gVoteCount, Need]), True);
659 MH_SEND_VoteEvent(NET_VE_REVOKE, Name, 'a', gVoteCount, Need);
660 end
661 else
662 begin
663 Inc(gVoteCount);
664 C^.Voted := True;
665 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Name, gVoteCount, Need]), True);
666 MH_SEND_VoteEvent(NET_VE_VOTE, Name, 'a', gVoteCount, Need);
667 g_Game_CheckVote;
668 end;
669 end;
670 end;
672 // GAME (SEND)
674 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
676 function sendItemRespawn (it: PItem): Boolean;
677 begin
678 result := false; // don't stop
679 MH_SEND_ItemSpawn(True, it.myid, ID);
680 end;
682 function sendMonSpawn (mon: TMonster): Boolean;
683 begin
684 result := false; // don't stop
685 MH_SEND_MonsterSpawn(mon.UID, ID);
686 end;
688 function sendPanelState (pan: TPanel): Boolean;
689 begin
690 result := false; // don't stop
691 MH_SEND_PanelState(pan.guid, ID); // anyway, to sync mplats
692 if (pan.CanChangeTexture) then MH_SEND_PanelTexture(pan.guid, pan.LastAnimLoop, ID);
693 end;
695 var
696 I: Integer;
697 begin
698 if gPlayers <> nil then
699 begin
700 for I := Low(gPlayers) to High(gPlayers) do
701 begin
702 if gPlayers[I] <> nil then
703 begin
704 if CreatePlayers then MH_SEND_PlayerCreate(gPlayers[I].UID, ID);
705 MH_SEND_PlayerPos(True, gPlayers[I].UID, ID);
706 MH_SEND_PlayerStats(gPlayers[I].UID, ID);
708 if (gPlayers[I].Flag <> FLAG_NONE) and (gGameSettings.GameMode = GM_CTF) then
709 begin
710 MH_SEND_FlagEvent(FLAG_STATE_CAPTURED, gPlayers[I].Flag, gPlayers[I].UID, True, ID);
711 end;
712 end;
713 end;
714 end;
716 g_Items_ForEachAlive(sendItemRespawn, true); // backwards
717 g_Mons_ForEach(sendMonSpawn);
718 g_Map_ForEachPanel(sendPanelState);
720 if gTriggers <> nil then
721 begin
722 for I := Low(gTriggers) to High(gTriggers) do
723 begin
724 if gTriggers[I].TriggerType = TRIGGER_SOUND then
725 begin
726 MH_SEND_TriggerSound(gTriggers[I], ID);
727 end;
728 end;
729 end;
731 if Shots <> nil then
732 begin
733 for I := Low(Shots) to High(Shots) do
734 begin
735 if Shots[i].ShotType in [6, 7, 8] then
736 begin
737 MH_SEND_CreateShot(i, ID);
738 end;
739 end;
740 end;
742 MH_SEND_TriggerMusic(ID);
744 MH_SEND_GameStats(ID);
745 MH_SEND_CoopStats(ID);
747 if gGameSettings.GameMode = GM_CTF then
748 begin
749 if gFlags[FLAG_RED].State <> FLAG_STATE_CAPTURED then MH_SEND_FlagEvent(gFlags[FLAG_RED].State, FLAG_RED, 0, True, ID);
750 if gFlags[FLAG_BLUE].State <> FLAG_STATE_CAPTURED then MH_SEND_FlagEvent(gFlags[FLAG_BLUE].State, FLAG_BLUE, 0, True, ID);
751 end;
753 if CreatePlayers and (ID >= 0) then NetClients[ID].State := NET_STATE_GAME;
755 if gLMSRespawn > LMS_RESPAWN_NONE then
756 begin
757 MH_SEND_GameEvent(NET_EV_LMS_WARMUP, (gLMSRespawnTime - gTime) div 1000, 'N', ID);
758 end;
760 g_Net_Flush();
761 end;
763 procedure MH_SEND_Info(ID: Byte);
764 var
765 Map: string;
766 begin
767 Map := g_ExtractFileName(gMapInfo.Map);
769 NetOut.Clear();
771 NetOut.Write(Byte(NET_MSG_INFO));
772 NetOut.Write(ID);
773 NetOut.Write(NetClients[ID].Player);
774 NetOut.Write(gGameSettings.WAD);
775 NetOut.Write(Map);
776 NetOut.Write(gWADHash);
777 NetOut.Write(gGameSettings.GameMode);
778 NetOut.Write(gGameSettings.GoalLimit);
779 NetOut.Write(gGameSettings.TimeLimit);
780 NetOut.Write(gGameSettings.MaxLives);
781 NetOut.Write(gGameSettings.Options);
782 NetOut.Write(gTime);
784 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
785 end;
787 procedure MH_SEND_Chat(Txt: string; Mode: Byte; ID: Integer = NET_EVERYONE);
788 var
789 Name: string;
790 i: Integer;
791 Team: Byte;
792 begin
793 if (Mode = NET_CHAT_TEAM) and (not gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
794 Mode := NET_CHAT_PLAYER;
796 Team := 0;
797 if (Mode = NET_CHAT_TEAM) then
798 begin
799 for i := Low(gPlayers) to High(gPlayers) do
800 if (gPlayers[i] <> nil) and (gPlayers[i].FClientID >= 0) and
801 (gPlayers[i].Team = ID) then
802 begin
803 NetOut.Write(Byte(NET_MSG_CHAT));
804 NetOut.Write(Txt);
805 NetOut.Write(Mode);
806 g_Net_Host_Send(gPlayers[i].FClientID, True, NET_CHAN_CHAT);
807 end;
808 Team := ID;
809 ID := NET_EVERYONE;
810 end
811 else
812 begin
813 NetOut.Write(Byte(NET_MSG_CHAT));
814 NetOut.Write(Txt);
815 NetOut.Write(Mode);
816 g_Net_Host_Send(ID, True, NET_CHAN_CHAT);
817 end;
819 if Mode = NET_CHAT_SYSTEM then
820 Exit;
822 if ID = NET_EVERYONE then
823 begin
824 if Mode = NET_CHAT_PLAYER then
825 begin
826 g_Console_Add(Txt, True);
827 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
828 g_Game_ChatSound(b_Text_Unformat(Txt));
829 end
830 else
831 if Mode = NET_CHAT_TEAM then
832 if gPlayer1 <> nil then
833 begin
834 if (gPlayer1.Team = TEAM_RED) and (Team = TEAM_RED) then
835 begin
836 g_Console_Add(#18'[Team] '#2 + Txt, True);
837 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
838 g_Game_ChatSound(b_Text_Unformat(Txt));
839 end
840 else if (gPlayer1.Team = TEAM_BLUE) and (Team = TEAM_BLUE) then
841 begin
842 g_Console_Add(#20'[Team] '#2 + Txt, True);
843 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
844 g_Game_ChatSound(b_Text_Unformat(Txt));
845 end;
846 end;
847 end
848 else
849 begin
850 Name := g_Net_ClientName_ByID(ID);
851 g_Console_Add('-> ' + Name + ': ' + Txt, True);
852 e_WriteLog('[Tell ' + Name + '] ' + b_Text_Unformat(Txt), TMsgType.Notify);
853 g_Game_ChatSound(b_Text_Unformat(Txt), False);
854 end;
855 end;
857 procedure MH_SEND_Effect(X, Y: Integer; Ang: SmallInt; Kind: Byte; ID: Integer = NET_EVERYONE);
858 begin
859 NetOut.Write(Byte(NET_MSG_GFX));
860 NetOut.Write(Kind);
861 NetOut.Write(X);
862 NetOut.Write(Y);
863 NetOut.Write(Ang);
865 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
866 end;
868 procedure MH_SEND_Sound(X, Y: Integer; Name: string; Pos: Boolean = True; ID: Integer = NET_EVERYONE);
869 begin
870 NetOut.Write(Byte(NET_MSG_SND));
871 NetOut.Write(Name);
872 if Pos then
873 begin
874 NetOut.Write(Byte(1));
875 NetOut.Write(X);
876 NetOut.Write(Y);
877 end
878 else
879 NetOut.Write(Byte(0));
881 g_Net_Host_Send(ID, False, NET_CHAN_GAME);
882 end;
884 procedure MH_SEND_CreateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
885 begin
886 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
888 NetOut.Write(Byte(NET_MSG_SHADD));
889 NetOut.Write(Proj);
890 NetOut.Write(Shots[Proj].ShotType);
891 NetOut.Write(Shots[Proj].Target);
892 NetOut.Write(Shots[Proj].SpawnerUID);
893 NetOut.Write(Shots[Proj].Timeout);
894 NetOut.Write(Shots[Proj].Obj.X);
895 NetOut.Write(Shots[Proj].Obj.Y);
896 NetOut.Write(Shots[Proj].Obj.Vel.X);
897 NetOut.Write(Shots[Proj].Obj.Vel.Y);
899 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
900 end;
902 procedure MH_SEND_UpdateShot(Proj: LongInt; ID: Integer = NET_EVERYONE);
903 begin
904 if (Shots = nil) or (Proj < 0) or (Proj > High(Shots)) then Exit;
906 NetOut.Write(Byte(NET_MSG_SHPOS));
907 NetOut.Write(Proj);
908 NetOut.Write(Shots[Proj].Obj.X);
909 NetOut.Write(Shots[Proj].Obj.Y);
910 NetOut.Write(Shots[Proj].Obj.Vel.X);
911 NetOut.Write(Shots[Proj].Obj.Vel.Y);
913 g_Net_Host_Send(ID, False, NET_CHAN_SHOTS);
914 end;
916 procedure MH_Send_DeleteShot(Proj: LongInt; X, Y: LongInt; Loud: Boolean = True; ID: Integer = NET_EVERYONE);
917 begin
918 NetOut.Write(Byte(NET_MSG_SHDEL));
919 NetOut.Write(Proj);
920 NetOut.Write(Byte(Loud));
921 NetOut.Write(X);
922 NetOut.Write(Y);
924 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
925 end;
927 procedure MH_SEND_GameStats(ID: Integer = NET_EVERYONE);
928 begin
929 NetOut.Write(Byte(NET_MSG_SCORE));
930 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
931 begin
932 NetOut.Write(gTeamStat[TEAM_RED].Goals);
933 NetOut.Write(gTeamStat[TEAM_BLUE].Goals);
934 end
935 else
936 if gGameSettings.GameMode = GM_COOP then
937 begin
938 NetOut.Write(gCoopMonstersKilled);
939 NetOut.Write(gCoopSecretsFound);
940 end;
942 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
943 end;
945 procedure MH_SEND_CoopStats(ID: Integer = NET_EVERYONE);
946 begin
947 NetOut.Write(Byte(NET_MSG_COOP));
948 NetOut.Write(gTotalMonsters);
949 NetOut.Write(gSecretsCount);
950 NetOut.Write(gCoopTotalMonstersKilled);
951 NetOut.Write(gCoopTotalSecretsFound);
952 NetOut.Write(gCoopTotalMonsters);
953 NetOut.Write(gCoopTotalSecrets);
954 end;
956 procedure MH_SEND_GameEvent(EvType: Byte; EvNum: Integer = 0; EvStr: string = 'N'; ID: Integer = NET_EVERYONE);
957 begin
958 NetOut.Write(Byte(NET_MSG_GEVENT));
959 NetOut.Write(EvType);
960 NetOut.Write(EvNum);
961 NetOut.Write(EvStr);
962 NetOut.Write(Byte(gLastMap));
963 NetOut.Write(gTime);
964 if (EvType = NET_EV_MAPSTART) and isWadPath(EvStr) then
965 begin
966 NetOut.Write(Byte(1));
967 NetOut.Write(gWADHash);
968 end else
969 NetOut.Write(Byte(0));
971 g_Net_Host_Send(ID, True, NET_CHAN_SERVICE);
972 end;
974 procedure MH_SEND_FlagEvent(EvType: Byte; Flag: Byte; PID: Word; Quiet: Boolean = False; ID: Integer = NET_EVERYONE);
975 begin
976 NetOut.Write(Byte(NET_MSG_FLAG));
977 NetOut.Write(EvType);
978 NetOut.Write(Flag);
979 NetOut.Write(Byte(Quiet));
980 NetOut.Write(PID);
981 NetOut.Write(gFlags[Flag].State);
982 NetOut.Write(gFlags[Flag].CaptureTime);
983 NetOut.Write(gFlags[Flag].Obj.X);
984 NetOut.Write(gFlags[Flag].Obj.Y);
985 NetOut.Write(gFlags[Flag].Obj.Vel.X);
986 NetOut.Write(gFlags[Flag].Obj.Vel.Y);
988 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
989 end;
991 procedure MH_SEND_GameSettings(ID: Integer = NET_EVERYONE);
992 begin
993 NetOut.Write(Byte(NET_MSG_GSET));
994 NetOut.Write(gGameSettings.GameMode);
995 NetOut.Write(gGameSettings.GoalLimit);
996 NetOut.Write(gGameSettings.TimeLimit);
997 NetOut.Write(gGameSettings.MaxLives);
998 NetOut.Write(gGameSettings.Options);
1000 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1001 end;
1003 // PLAYER (SEND)
1005 procedure MH_SEND_PlayerCreate(PID: Word; ID: Integer = NET_EVERYONE);
1006 var
1007 P: TPlayer;
1008 begin
1009 P := g_Player_Get(PID);
1010 if P = nil then Exit;
1012 NetOut.Write(Byte(NET_MSG_PLR));
1013 NetOut.Write(PID);
1014 NetOut.Write(P.Name);
1016 NetOut.Write(P.FActualModelName);
1017 NetOut.Write(P.FColor.R);
1018 NetOut.Write(P.FColor.G);
1019 NetOut.Write(P.FColor.B);
1020 NetOut.Write(P.Team);
1022 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT)
1023 end;
1025 procedure MH_SEND_PlayerPos(Reliable: Boolean; PID: Word; ID: Integer = NET_EVERYONE);
1026 var
1027 kByte: Word;
1028 Pl: TPlayer;
1029 begin
1030 Pl := g_Player_Get(PID);
1031 if Pl = nil then Exit;
1032 if Pl.FDummy then Exit;
1034 NetOut.Write(Byte(NET_MSG_PLRPOS));
1035 NetOut.Write(gTime);
1036 NetOut.Write(PID);
1038 kByte := 0;
1040 with Pl do
1041 begin
1042 NetOut.Write(FPing);
1043 NetOut.Write(FLoss);
1044 if IsKeyPressed(KEY_CHAT) then
1045 kByte := NET_KEY_CHAT
1046 else
1047 begin
1048 if IsKeyPressed(KEY_LEFT) then kByte := kByte or NET_KEY_LEFT;
1049 if IsKeyPressed(KEY_RIGHT) then kByte := kByte or NET_KEY_RIGHT;
1050 if IsKeyPressed(KEY_UP) then kByte := kByte or NET_KEY_UP;
1051 if IsKeyPressed(KEY_DOWN) then kByte := kByte or NET_KEY_DOWN;
1052 if IsKeyPressed(KEY_JUMP) then kByte := kByte or NET_KEY_JUMP;
1053 if JustTeleported then kByte := kByte or NET_KEY_FORCEDIR;
1054 end;
1056 NetOut.Write(kByte);
1057 if Direction = TDirection.D_LEFT then NetOut.Write(Byte(0)) else NetOut.Write(Byte(1));
1058 NetOut.Write(GameX);
1059 NetOut.Write(GameY);
1060 NetOut.Write(GameVelX);
1061 NetOut.Write(GameVelY);
1062 NetOut.Write(GameAccelX);
1063 NetOut.Write(GameAccelY);
1064 end;
1066 g_Net_Host_Send(ID, Reliable, NET_CHAN_PLAYERPOS);
1067 end;
1069 procedure MH_SEND_PlayerStats(PID: Word; ID: Integer = NET_EVERYONE);
1070 var
1071 P: TPlayer;
1072 I: Integer;
1073 begin
1074 P := g_Player_Get(PID);
1075 if P = nil then Exit;
1077 NetOut.Write(Byte(NET_MSG_PLRSTA));
1078 NetOut.Write(PID);
1080 with P do
1081 begin
1082 NetOut.Write(Byte(alive));
1083 NetOut.Write(Byte(GodMode));
1084 NetOut.Write(Health);
1085 NetOut.Write(Armor);
1086 NetOut.Write(Air);
1087 NetOut.Write(JetFuel);
1088 NetOut.Write(Lives);
1089 NetOut.Write(Team);
1091 for I := WP_FIRST to WP_LAST do
1092 NetOut.Write(Byte(FWeapon[I]));
1094 for I := A_BULLETS to A_HIGH do
1095 NetOut.Write(FAmmo[I]);
1097 for I := A_BULLETS to A_HIGH do
1098 NetOut.Write(FMaxAmmo[I]);
1100 for I := MR_SUIT to MR_MAX do
1101 NetOut.Write(LongWord(FMegaRulez[I]));
1103 NetOut.Write(Byte(R_ITEM_BACKPACK in FRulez));
1104 NetOut.Write(Byte(R_KEY_RED in FRulez));
1105 NetOut.Write(Byte(R_KEY_GREEN in FRulez));
1106 NetOut.Write(Byte(R_KEY_BLUE in FRulez));
1107 NetOut.Write(Byte(R_BERSERK in FRulez));
1109 NetOut.Write(Frags);
1110 NetOut.Write(Death);
1112 NetOut.Write(CurrWeap);
1114 NetOut.Write(Byte(FSpectator));
1115 NetOut.Write(Byte(FGhost));
1116 NetOut.Write(Byte(FPhysics));
1117 NetOut.Write(Byte(FNoRespawn));
1118 NetOut.Write(Byte(FJetpack));
1119 NetOut.Write(FFireTime);
1120 NetOut.Write(Byte(FFlaming));
1121 end;
1123 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1124 end;
1126 procedure MH_SEND_PlayerDamage(PID: Word; Kind: Byte; Attacker, Value: Word; VX, VY: Integer; ID: Integer = NET_EVERYONE);
1127 begin
1128 NetOut.Write(Byte(NET_MSG_PLRDMG));
1129 NetOut.Write(PID);
1130 NetOut.Write(Kind);
1131 NetOut.Write(Attacker);
1132 NetOut.Write(Value);
1133 NetOut.Write(VX);
1134 NetOut.Write(VY);
1136 g_Net_Host_Send(ID, False, NET_CHAN_PLAYER);
1137 end;
1139 procedure MH_SEND_PlayerDeath(PID: Word; KillType, DeathType: Byte; Attacker: Word; ID: Integer = NET_EVERYONE);
1140 begin
1141 NetOut.Write(Byte(NET_MSG_PLRDIE));
1142 NetOut.Write(PID);
1143 NetOut.Write(KillType);
1144 NetOut.Write(DeathType);
1145 NetOut.Write(Attacker);
1147 g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
1148 end;
1150 procedure MH_SEND_PlayerFire(PID: Word; Weapon: Byte; X, Y, AX, AY: Integer; ShotID: Integer = -1; ID: Integer = NET_EVERYONE);
1151 begin
1152 NetOut.Write(Byte(NET_MSG_PLRFIRE));
1153 NetOut.Write(PID);
1154 NetOut.Write(Weapon);
1155 NetOut.Write(X);
1156 NetOut.Write(Y);
1157 NetOut.Write(AX);
1158 NetOut.Write(AY);
1159 NetOut.Write(ShotID);
1161 g_Net_Host_Send(ID, True, NET_CHAN_SHOTS);
1162 end;
1164 procedure MH_SEND_PlayerDelete(PID: Word; ID: Integer = NET_EVERYONE);
1165 begin
1166 NetOut.Write(Byte(NET_MSG_PLRDEL));
1167 NetOut.Write(PID);
1169 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1170 end;
1172 procedure MH_SEND_PlayerSettings(PID: Word; Mdl: string = ''; ID: Integer = NET_EVERYONE);
1173 var
1174 Pl: TPlayer;
1175 begin
1176 Pl := g_Player_Get(PID);
1177 if Pl = nil then Exit;
1179 NetOut.Write(Byte(NET_MSG_PLRSET));
1180 NetOut.Write(PID);
1181 NetOut.Write(Pl.Name);
1182 if Mdl = '' then
1183 NetOut.Write(Pl.Model.Name)
1184 else
1185 NetOut.Write(Mdl);
1186 NetOut.Write(Pl.FColor.R);
1187 NetOut.Write(Pl.FColor.G);
1188 NetOut.Write(Pl.FColor.B);
1189 NetOut.Write(Pl.Team);
1191 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1192 end;
1194 // ITEM (SEND)
1196 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1197 var
1198 it: PItem;
1199 tt: Byte;
1200 begin
1201 it := g_Items_ByIdx(IID);
1203 NetOut.Write(Byte(NET_MSG_ISPAWN));
1204 NetOut.Write(IID);
1205 NetOut.Write(Byte(Quiet));
1206 tt := it.ItemType;
1207 if it.dropped then tt := tt or $80;
1208 NetOut.Write(tt);
1209 NetOut.Write(Byte(it.Fall));
1210 NetOut.Write(Byte(it.Respawnable));
1211 NetOut.Write(it.Obj.X);
1212 NetOut.Write(it.Obj.Y);
1213 NetOut.Write(it.Obj.Vel.X);
1214 NetOut.Write(it.Obj.Vel.Y);
1216 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1217 end;
1219 procedure MH_SEND_ItemDestroy(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
1220 begin
1221 NetOut.Write(Byte(NET_MSG_IDEL));
1222 NetOut.Write(IID);
1223 NetOut.Write(Byte(Quiet));
1225 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1226 end;
1228 // PANEL
1230 procedure MH_SEND_PanelTexture(PGUID: Integer; AnimLoop: Byte; ID: Integer = NET_EVERYONE);
1231 var
1232 TP: TPanel;
1233 begin
1234 TP := g_Map_PanelByGUID(PGUID);
1235 if (TP = nil) then exit;
1237 with TP do
1238 begin
1239 NetOut.Write(Byte(NET_MSG_PTEX));
1240 NetOut.Write(LongWord(PGUID));
1241 NetOut.Write(FCurTexture);
1242 NetOut.Write(FCurFrame);
1243 NetOut.Write(FCurFrameCount);
1244 NetOut.Write(AnimLoop);
1245 end;
1247 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1248 end;
1250 procedure MH_SEND_PanelState(PGUID: Integer; ID: Integer = NET_EVERYONE);
1251 var
1252 TP: TPanel;
1253 mpflags: Byte = 0;
1254 begin
1255 TP := g_Map_PanelByGUID(PGUID);
1256 if (TP = nil) then exit;
1258 NetOut.Write(Byte(NET_MSG_PSTATE));
1259 NetOut.Write(LongWord(PGUID));
1260 NetOut.Write(Byte(TP.Enabled));
1261 NetOut.Write(TP.LiftType);
1262 NetOut.Write(TP.X);
1263 NetOut.Write(TP.Y);
1264 NetOut.Write(Word(TP.Width));
1265 NetOut.Write(Word(TP.Height));
1266 // mplats
1267 NetOut.Write(LongInt(TP.movingSpeedX));
1268 NetOut.Write(LongInt(TP.movingSpeedY));
1269 NetOut.Write(LongInt(TP.movingStartX));
1270 NetOut.Write(LongInt(TP.movingStartY));
1271 NetOut.Write(LongInt(TP.movingEndX));
1272 NetOut.Write(LongInt(TP.movingEndY));
1273 NetOut.Write(LongInt(TP.sizeSpeedX));
1274 NetOut.Write(LongInt(TP.sizeSpeedY));
1275 NetOut.Write(LongInt(TP.sizeEndX));
1276 NetOut.Write(LongInt(TP.sizeEndY));
1277 if TP.movingActive then mpflags := mpflags or 1;
1278 if TP.moveOnce then mpflags := mpflags or 2;
1279 NetOut.Write(Byte(mpflags));
1281 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1282 end;
1284 // TRIGGER
1286 procedure MH_SEND_TriggerSound(var T: TTrigger; ID: Integer = NET_EVERYONE);
1287 begin
1288 if gTriggers = nil then Exit;
1289 if T.Sound = nil then Exit;
1291 NetOut.Write(Byte(NET_MSG_TSOUND));
1292 NetOut.Write(T.ClientID);
1293 NetOut.Write(Byte(T.Sound.IsPlaying));
1294 NetOut.Write(LongWord(T.Sound.GetPosition));
1295 NetOut.Write(T.SoundPlayCount);
1297 g_Net_Host_Send(ID, True);
1298 end;
1300 procedure MH_SEND_TriggerMusic(ID: Integer = NET_EVERYONE);
1301 begin
1302 NetOut.Write(Byte(NET_MSG_TMUSIC));
1303 NetOut.Write(gMusic.Name);
1304 NetOut.Write(Byte(gMusic.IsPlaying));
1305 NetOut.Write(LongWord(gMusic.GetPosition));
1306 NetOut.Write(Byte(gMusic.SpecPause or gMusic.IsPaused));
1308 g_Net_Host_Send(ID, True);
1309 end;
1311 // MONSTER
1313 procedure MH_SEND_MonsterSpawn(UID: Word; ID: Integer = NET_EVERYONE);
1314 var
1315 M: TMonster;
1316 begin
1317 M := g_Monsters_ByUID(UID);
1318 if M = nil then
1319 Exit;
1321 with M do
1322 begin
1323 NetOut.Write(Byte(NET_MSG_MSPAWN));
1324 NetOut.Write(UID);
1325 NetOut.Write(MonsterType);
1326 NetOut.Write(MonsterState);
1327 NetOut.Write(MonsterAnim);
1328 NetOut.Write(MonsterTargetUID);
1329 NetOut.Write(MonsterTargetTime);
1330 NetOut.Write(MonsterBehaviour);
1331 NetOut.Write(MonsterSleep);
1332 NetOut.Write(MonsterHealth);
1333 NetOut.Write(MonsterAmmo);
1334 NetOut.Write(GameX);
1335 NetOut.Write(GameY);
1336 NetOut.Write(GameVelX);
1337 NetOut.Write(GameVelY);
1338 NetOut.Write(Byte(GameDirection));
1339 end;
1341 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1342 end;
1344 procedure MH_SEND_MonsterPos(UID: Word; ID: Integer = NET_EVERYONE);
1345 var
1346 M: TMonster;
1347 begin
1348 M := g_Monsters_ByUID(UID);
1349 if M = nil then Exit;
1351 NetOut.Write(Byte(NET_MSG_MPOS));
1352 NetOut.Write(UID);
1354 with M do
1355 begin
1356 NetOut.Write(GameX);
1357 NetOut.Write(GameY);
1358 NetOut.Write(GameVelX);
1359 NetOut.Write(GameVelY);
1360 NetOut.Write(Byte(GameDirection));
1361 end;
1363 g_Net_Host_Send(ID, False, NET_CHAN_MONSTERPOS);
1364 end;
1366 procedure MH_SEND_MonsterState(UID: Word; ForcedAnim: Byte = 255; ID: Integer = NET_EVERYONE);
1367 var
1368 M: TMonster;
1369 begin
1370 M := g_Monsters_ByUID(UID);
1371 if M = nil then Exit;
1373 NetOut.Write(Byte(NET_MSG_MSTATE));
1374 NetOut.Write(UID);
1376 with M do
1377 begin
1378 NetOut.Write(MonsterState);
1379 NetOut.Write(ForcedAnim);
1380 NetOut.Write(MonsterTargetUID);
1381 NetOut.Write(MonsterTargetTime);
1382 NetOut.Write(MonsterSleep);
1383 NetOut.Write(MonsterHealth);
1384 NetOut.Write(MonsterAmmo);
1385 NetOut.Write(MonsterPain);
1386 NetOut.Write(Byte(AnimIsReverse));
1387 NetOut.Write(FFireTime);
1388 end;
1390 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1391 end;
1393 procedure MH_SEND_MonsterShot(UID: Word; X, Y, VX, VY: Integer; ID: Integer = NET_EVERYONE);
1394 begin
1395 NetOut.Write(Byte(NET_MSG_MSHOT));
1396 NetOut.Write(UID);
1397 NetOut.Write(X);
1398 NetOut.Write(Y);
1399 NetOut.Write(VX);
1400 NetOut.Write(VY);
1402 g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
1403 end;
1405 procedure MH_SEND_MonsterDelete(UID: Word; ID: Integer = NET_EVERYONE);
1406 var
1407 M: TMonster;
1408 begin
1409 M := g_Monsters_ByUID(UID);
1410 if M = nil then Exit;
1412 NetOut.Write(Byte(NET_MSG_MDEL));
1413 NetOut.Write(UID);
1415 g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
1416 end;
1418 // MISC
1420 procedure MH_SEND_TimeSync(Time: LongWord; ID: Integer = NET_EVERYONE);
1421 begin
1422 NetOut.Write(Byte(NET_MSG_TIME_SYNC));
1423 NetOut.Write(Time);
1425 g_Net_Host_Send(ID, False, NET_CHAN_SERVICE);
1426 end;
1428 procedure MH_SEND_VoteEvent(EvType: Byte;
1429 StrArg1: string = 'a'; StrArg2: string = 'b';
1430 IntArg1: SmallInt = 0; IntArg2: SmallInt = 0;
1431 ID: Integer = NET_EVERYONE);
1432 begin
1433 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
1434 NetOut.Write(EvType);
1435 NetOut.Write(IntArg1);
1436 NetOut.Write(IntArg2);
1437 NetOut.Write(StrArg1);
1438 NetOut.Write(StrArg2);
1440 g_Net_Host_Send(ID, True, NET_CHAN_IMPORTANT);
1441 end;
1443 // CLIENT MESSAGES //
1445 // GAME
1447 procedure MC_RECV_Chat(var M: TMsg);
1448 var
1449 Txt: string;
1450 Mode: Byte;
1451 begin
1452 Txt := M.ReadString();
1453 Mode := M.ReadByte();
1455 if Mode <> NET_CHAT_SYSTEM then
1456 begin
1457 if Mode = NET_CHAT_PLAYER then
1458 begin
1459 g_Console_Add(Txt, True);
1460 e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1461 g_Game_ChatSound(b_Text_Unformat(Txt));
1462 end else
1463 if (Mode = NET_CHAT_TEAM) and (gPlayer1 <> nil) then
1464 begin
1465 if gPlayer1.Team = TEAM_RED then
1466 g_Console_Add(b_Text_Format('\r[Team] ') + Txt, True);
1467 if gPlayer1.Team = TEAM_BLUE then
1468 g_Console_Add(b_Text_Format('\b[Team] ') + Txt, True);
1469 e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
1470 g_Game_ChatSound(b_Text_Unformat(Txt));
1471 end;
1472 end else
1473 g_Console_Add(Txt, True);
1474 end;
1476 procedure MC_RECV_Effect(var M: TMsg);
1477 var
1478 Kind: Byte;
1479 X, Y: Integer;
1480 Ang: SmallInt;
1481 Anim: TAnimation;
1482 ID: LongWord;
1483 begin
1484 if not gGameOn then Exit;
1485 Kind := M.ReadByte();
1486 X := M.ReadLongInt();
1487 Y := M.ReadLongInt();
1488 Ang := M.ReadSmallInt();
1490 case Kind of
1491 NET_GFX_SPARK:
1492 g_GFX_Spark(X, Y, 2 + Random(2), Ang, 0, 0);
1494 NET_GFX_TELE:
1495 begin
1496 if g_Frames_Get(ID, 'FRAMES_TELEPORT') then
1497 begin
1498 Anim := TAnimation.Create(ID, False, 3);
1499 g_GFX_OnceAnim(X, Y, Anim);
1500 Anim.Free();
1501 end;
1502 if Ang = 1 then
1503 g_Sound_PlayExAt('SOUND_GAME_TELEPORT', X, Y);
1504 end;
1506 NET_GFX_EXPLODE:
1507 begin
1508 if g_Frames_Get(ID, 'FRAMES_EXPLODE_ROCKET') then
1509 begin
1510 Anim := TAnimation.Create(ID, False, 6);
1511 Anim.Blending := False;
1512 g_GFX_OnceAnim(X-64, Y-64, Anim);
1513 Anim.Free();
1514 end;
1515 if Ang = 1 then
1516 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEROCKET', X, Y);
1517 end;
1519 NET_GFX_BFGEXPL:
1520 begin
1521 if g_Frames_Get(ID, 'FRAMES_EXPLODE_BFG') then
1522 begin
1523 Anim := TAnimation.Create(ID, False, 6);
1524 Anim.Blending := False;
1525 g_GFX_OnceAnim(X-64, Y-64, Anim);
1526 Anim.Free();
1527 end;
1528 if Ang = 1 then
1529 g_Sound_PlayExAt('SOUND_WEAPON_EXPLODEBFG', X, Y);
1530 end;
1532 NET_GFX_BFGHIT:
1533 begin
1534 if g_Frames_Get(ID, 'FRAMES_BFGHIT') then
1535 begin
1536 Anim := TAnimation.Create(ID, False, 4);
1537 g_GFX_OnceAnim(X-32, Y-32, Anim);
1538 Anim.Free();
1539 end;
1540 end;
1542 NET_GFX_FIRE:
1543 begin
1544 if g_Frames_Get(ID, 'FRAMES_FIRE') then
1545 begin
1546 Anim := TAnimation.Create(ID, False, 4);
1547 g_GFX_OnceAnim(X, Y, Anim);
1548 Anim.Free();
1549 end;
1550 if Ang = 1 then
1551 g_Sound_PlayExAt('SOUND_FIRE', X, Y);
1552 end;
1554 NET_GFX_RESPAWN:
1555 begin
1556 if g_Frames_Get(ID, 'FRAMES_ITEM_RESPAWN') then
1557 begin
1558 Anim := TAnimation.Create(ID, False, 4);
1559 g_GFX_OnceAnim(X, Y, Anim);
1560 Anim.Free();
1561 end;
1562 if Ang = 1 then
1563 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
1564 end;
1566 NET_GFX_SHELL1:
1567 g_Player_CreateShell(X, Y, 0, -2, SHELL_BULLET);
1569 NET_GFX_SHELL2:
1570 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1572 NET_GFX_SHELL3:
1573 begin
1574 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1575 g_Player_CreateShell(X, Y, 0, -2, SHELL_SHELL);
1576 end;
1577 end;
1578 end;
1580 procedure MC_RECV_Sound(var M: TMsg);
1581 var
1582 Name: string;
1583 X, Y: Integer;
1584 Pos: Boolean;
1585 begin
1586 Name := M.ReadString();
1587 Pos := M.ReadByte() <> 0;
1588 if Pos then
1589 begin
1590 X := M.ReadLongInt();
1591 Y := M.ReadLongInt();
1592 g_Sound_PlayExAt(Name, X, Y);
1593 end
1594 else
1595 g_Sound_PlayEx(Name);
1596 end;
1598 procedure MC_RECV_CreateShot(var M: TMsg);
1599 var
1600 I, X, Y, XV, YV: Integer;
1601 Timeout: LongWord;
1602 Target, Spawner: Word;
1603 ShType: Byte;
1604 begin
1605 I := M.ReadLongInt();
1606 ShType := M.ReadByte();
1607 Target := M.ReadWord();
1608 Spawner := M.ReadWord();
1609 Timeout := M.ReadLongWord();
1610 X := M.ReadLongInt();
1611 Y := M.ReadLongInt();
1612 XV := M.ReadLongInt();
1613 YV := M.ReadLongInt();
1615 I := g_Weapon_CreateShot(I, ShType, Spawner, Target, X, Y, XV, YV);
1616 if (Shots <> nil) and (I <= High(Shots)) then
1617 begin
1618 Shots[I].Timeout := Timeout;
1619 //Shots[I].Target := Target; // TODO: find a use for Target later
1620 end;
1621 end;
1623 procedure MC_RECV_UpdateShot(var M: TMsg);
1624 var
1625 I, TX, TY, TXV, TYV: Integer;
1626 begin
1627 I := M.ReadLongInt();
1628 TX := M.ReadLongInt();
1629 TY := M.ReadLongInt();
1630 TXV := M.ReadLongInt();
1631 TYV := M.ReadLongInt();
1633 if (Shots <> nil) and (I <= High(Shots)) then
1634 with (Shots[i]) do
1635 begin
1636 Obj.X := TX;
1637 Obj.Y := TY;
1638 Obj.Vel.X := TXV;
1639 Obj.Vel.Y := TYV;
1640 end;
1641 end;
1643 procedure MC_RECV_DeleteShot(var M: TMsg);
1644 var
1645 I, X, Y: Integer;
1646 L: Boolean;
1647 begin
1648 if not gGameOn then Exit;
1649 I := M.ReadLongInt();
1650 L := (M.ReadByte() <> 0);
1651 X := M.ReadLongInt();
1652 Y := M.ReadLongInt();
1654 g_Weapon_DestroyShot(I, X, Y, L);
1655 end;
1657 procedure MC_RECV_GameStats(var M: TMsg);
1658 begin
1659 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
1660 begin
1661 gTeamStat[TEAM_RED].Goals := M.ReadSmallInt();
1662 gTeamStat[TEAM_BLUE].Goals := M.ReadSmallInt();
1663 end
1664 else
1665 if gGameSettings.GameMode = GM_COOP then
1666 begin
1667 gCoopMonstersKilled := M.ReadWord();
1668 gCoopSecretsFound := M.ReadWord();
1669 end;
1670 end;
1672 procedure MC_RECV_CoopStats(var M: TMsg);
1673 begin
1674 gTotalMonsters := M.ReadLongInt();
1675 gSecretsCount := M.ReadLongInt();
1676 gCoopTotalMonstersKilled := M.ReadWord();
1677 gCoopTotalSecretsFound := M.ReadWord();
1678 gCoopTotalMonsters := M.ReadWord();
1679 gCoopTotalSecrets := M.ReadWord();
1680 end;
1682 procedure MC_RECV_GameEvent(var M: TMsg);
1683 var
1684 EvType: Byte;
1685 EvNum: Integer;
1686 EvStr: string;
1687 EvTime: LongWord;
1688 BHash: Boolean;
1689 EvHash: TMD5Digest;
1690 pl: TPlayer;
1691 i1, i2: TStrings_Locale;
1692 pln: String;
1693 cnt: Byte;
1694 begin
1695 FillChar(EvHash, Sizeof(EvHash), 0);
1696 EvType := M.ReadByte();
1697 EvNum := M.ReadLongInt();
1698 EvStr := M.ReadString();
1699 gLastMap := M.ReadByte() <> 0;
1700 if gLastMap and (gGameSettings.GameMode = GM_COOP) then gStatsOff := True;
1701 gStatsPressed := True;
1702 EvTime := M.ReadLongWord();
1703 BHash := M.ReadByte() <> 0;
1704 if BHash then
1705 EvHash := M.ReadMD5();
1707 gTime := EvTime;
1709 case EvType of
1710 NET_EV_MAPSTART:
1711 begin
1712 gGameOn := False;
1713 g_Game_ClearLoading();
1714 g_Game_StopAllSounds(True);
1716 gSwitchGameMode := Byte(EvNum);
1717 gGameSettings.GameMode := gSwitchGameMode;
1719 gWADHash := EvHash;
1720 if not g_Game_StartMap(EvStr, True) then
1721 begin
1722 if not isWadPath(EvStr) then
1723 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [gGameSettings.WAD + ':\' + EvStr]))
1724 else
1725 g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [EvStr]));
1726 Exit;
1727 end;
1729 MC_SEND_FullStateRequest;
1730 end;
1732 NET_EV_MAPEND:
1733 begin
1734 gMissionFailed := EvNum <> 0;
1735 gExit := EXIT_ENDLEVELCUSTOM;
1736 end;
1738 NET_EV_RCON:
1739 begin
1740 case EvNum of
1741 NET_RCON_NOAUTH:
1742 g_Console_Add(_lc[I_NET_RCON_NOAUTH], True);
1743 NET_RCON_PWGOOD:
1744 g_Console_Add(_lc[I_NET_RCON_PWD_VALID], True);
1745 NET_RCON_PWBAD:
1746 g_Console_Add(_lc[I_NET_RCON_PWD_INVALID], True);
1747 end;
1748 end;
1750 NET_EV_CHANGE_TEAM:
1751 begin
1752 if EvNum = TEAM_RED then
1753 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [EvStr]), True);
1754 if EvNum = TEAM_BLUE then
1755 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [EvStr]), True);
1756 end;
1758 NET_EV_PLAYER_KICK:
1759 g_Console_Add(Format(_lc[I_PLAYER_KICK], [EvStr]), True);
1761 NET_EV_PLAYER_BAN:
1762 g_Console_Add(Format(_lc[I_PLAYER_BAN], [EvStr]), True);
1764 NET_EV_LMS_WARMUP:
1765 g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [EvNum]), True);
1767 NET_EV_LMS_SURVIVOR:
1768 g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True);
1770 NET_EV_BIGTEXT:
1771 g_Game_Message(AnsiUpperCase(EvStr), Word(EvNum));
1773 NET_EV_SCORE:
1774 begin
1775 pl := g_Player_Get(EvNum and $FFFF);
1776 if pl = nil then
1777 pln := '?'
1778 else
1779 pln := pl.Name;
1780 cnt := (EvNum shr 16) and $FF;
1781 if Pos('w', EvStr) = 0 then
1782 begin
1783 // Default score
1784 if Pos('t', EvStr) = 0 then
1785 begin
1786 // Player +/- score
1787 if Pos('-', EvStr) = 0 then
1788 begin
1789 if Pos('e', EvStr) = 0 then
1790 i1 := I_PLAYER_SCORE_ADD_OWN
1791 else
1792 i1 := I_PLAYER_SCORE_ADD_ENEMY;
1793 end else
1794 begin
1795 if Pos('e', EvStr) = 0 then
1796 i1 := I_PLAYER_SCORE_SUB_OWN
1797 else
1798 i1 := I_PLAYER_SCORE_SUB_ENEMY;
1799 end;
1800 // Which team
1801 if Pos('r', EvStr) > 0 then
1802 i2 := I_PLAYER_SCORE_TO_RED
1803 else
1804 i2 := I_PLAYER_SCORE_TO_BLUE;
1805 g_Console_Add(Format(_lc[i1], [pln, cnt, _lc[i2]]), True);
1806 end else
1807 begin
1808 // Team +/- score
1809 if Pos('-', EvStr) = 0 then
1810 i1 := I_PLAYER_SCORE_ADD_TEAM
1811 else
1812 i1 := I_PLAYER_SCORE_SUB_TEAM;
1813 // Which team
1814 if Pos('r', EvStr) > 0 then
1815 i2 := I_PLAYER_SCORE_RED
1816 else
1817 i2 := I_PLAYER_SCORE_BLUE;
1818 g_Console_Add(Format(_lc[i1], [_lc[i2], cnt]), True);
1819 end;
1820 end else
1821 begin
1822 // Game Win
1823 if Pos('e', EvStr) = 0 then
1824 i1 := I_PLAYER_SCORE_WIN_OWN
1825 else
1826 i1 := I_PLAYER_SCORE_WIN_ENEMY;
1827 // Which team
1828 if Pos('r', EvStr) > 0 then
1829 i2 := I_PLAYER_SCORE_TO_RED
1830 else
1831 i2 := I_PLAYER_SCORE_TO_BLUE;
1832 g_Console_Add(Format(_lc[i1], [pln, _lc[i2]]), True);
1833 end;
1834 end;
1836 NET_EV_SCORE_MSG:
1837 begin
1838 if EvNum = TEAM_RED then
1839 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1840 if EvNum = TEAM_BLUE then
1841 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_ADD], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1842 if EvNum = -TEAM_RED then
1843 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 108);
1844 if EvNum = -TEAM_BLUE then
1845 g_Game_Message(Format(_lc[I_MESSAGE_SCORE_SUB], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 108);
1846 end;
1848 NET_EV_LMS_START:
1849 begin
1850 g_Player_RemoveAllCorpses;
1851 g_Game_Message(_lc[I_MESSAGE_LMS_START], 144);
1852 end;
1854 NET_EV_LMS_WIN:
1855 g_Game_Message(Format(_lc[I_MESSAGE_LMS_WIN], [AnsiUpperCase(EvStr)]), 144);
1857 NET_EV_TLMS_WIN:
1858 begin
1859 if EvNum = TEAM_RED then
1860 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 144);
1861 if EvNum = TEAM_BLUE then
1862 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 144);
1863 end;
1865 NET_EV_LMS_LOSE:
1866 g_Game_Message(_lc[I_MESSAGE_LMS_LOSE], 144);
1868 NET_EV_LMS_DRAW:
1869 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
1871 NET_EV_KILLCOMBO:
1872 g_Game_Announce_KillCombo(EvNum);
1874 NET_EV_PLAYER_TOUCH:
1875 begin
1876 pl := g_Player_Get(EvNum);
1877 if pl <> nil then
1878 pl.Touch();
1879 end;
1881 NET_EV_SECRET:
1882 begin
1883 pl := g_Player_Get(EvNum);
1884 if pl <> nil then
1885 begin
1886 g_Console_Add(Format(_lc[I_PLAYER_SECRET], [pl.Name]), True);
1887 g_Sound_PlayEx('SOUND_GAME_SECRET');
1888 end;
1889 end;
1891 NET_EV_INTER_READY:
1892 begin
1893 pl := g_Player_Get(EvNum);
1894 if pl <> nil then pl.FReady := (EvStr = 'Y');
1895 end;
1896 end;
1897 end;
1899 procedure MC_RECV_FlagEvent(var M: TMsg);
1900 var
1901 PID: Word;
1902 Pl: TPlayer;
1903 EvType: Byte;
1904 Fl, a: Byte;
1905 Quiet: Boolean;
1906 s, ts: string;
1907 begin
1908 EvType := M.ReadByte();
1909 Fl := M.ReadByte();
1911 if Fl = FLAG_NONE then Exit;
1913 Quiet := (M.ReadByte() <> 0);
1914 PID := M.ReadWord();
1916 gFlags[Fl].State := M.ReadByte();
1917 gFlags[Fl].CaptureTime := M.ReadLongWord();
1918 gFlags[Fl].Obj.X := M.ReadLongInt();
1919 gFlags[Fl].Obj.Y := M.ReadLongInt();
1920 gFlags[Fl].Obj.Vel.X := M.ReadLongInt();
1921 gFlags[Fl].Obj.Vel.Y := M.ReadLongInt();
1923 Pl := g_Player_Get(PID);
1924 if (Pl = nil) and
1925 (EvType <> FLAG_STATE_NORMAL) and
1926 (EvType <> FLAG_STATE_DROPPED) and
1927 (EvType <> FLAG_STATE_RETURNED) then
1928 Exit;
1930 case EvType of
1931 FLAG_STATE_NORMAL:
1932 begin
1933 if Quiet or (Pl = nil) then Exit;
1935 if Fl = FLAG_RED then
1936 s := _lc[I_PLAYER_FLAG_RED]
1937 else
1938 s := _lc[I_PLAYER_FLAG_BLUE];
1940 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
1942 if ((Pl = gPlayer1) or (Pl = gPlayer2)
1943 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
1944 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
1945 a := 0
1946 else
1947 a := 1;
1949 if not sound_ret_flag[a].IsPlaying() then
1950 sound_ret_flag[a].Play();
1951 end;
1953 FLAG_STATE_CAPTURED:
1954 begin
1955 if (Pl <> nil) then Pl.SetFlag(Fl);
1957 if Quiet then Exit;
1959 if Fl = FLAG_RED then
1960 s := _lc[I_PLAYER_FLAG_RED]
1961 else
1962 s := _lc[I_PLAYER_FLAG_BLUE];
1964 g_Console_Add(Format(_lc[I_PLAYER_FLAG_GET], [Pl.Name, s]), True);
1965 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_GET], [AnsiUpperCase(s)]), 144);
1967 if ((Pl = gPlayer1) or (Pl = gPlayer2)
1968 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
1969 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
1970 a := 0
1971 else
1972 a := 1;
1974 if not sound_get_flag[a].IsPlaying() then
1975 sound_get_flag[a].Play();
1976 end;
1978 FLAG_STATE_DROPPED:
1979 begin
1980 if (Pl <> nil) then Pl.SetFlag(FLAG_NONE);
1982 if Quiet or (Pl = nil) then Exit;
1984 if Fl = FLAG_RED then
1985 s := _lc[I_PLAYER_FLAG_RED]
1986 else
1987 s := _lc[I_PLAYER_FLAG_BLUE];
1989 g_Console_Add(Format(_lc[I_PLAYER_FLAG_DROP], [Pl.Name, s]), True);
1990 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_DROP], [AnsiUpperCase(s)]), 144);
1992 if ((Pl = gPlayer1) or (Pl = gPlayer2)
1993 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
1994 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
1995 a := 0
1996 else
1997 a := 1;
1999 if not sound_lost_flag[a].IsPlaying() then
2000 sound_lost_flag[a].Play();
2001 end;
2003 FLAG_STATE_SCORED:
2004 begin
2005 g_Map_ResetFlag(FLAG_RED);
2006 g_Map_ResetFlag(FLAG_BLUE);
2007 if Quiet or (Pl = nil) then Exit;
2008 Pl.SetFlag(FLAG_NONE);
2010 if Fl = FLAG_RED then
2011 s := _lc[I_PLAYER_FLAG_RED]
2012 else
2013 s := _lc[I_PLAYER_FLAG_BLUE];
2015 ts := Format('%.4d', [gFlags[Fl].CaptureTime]);
2016 Insert('.', ts, Length(ts) + 1 - 3);
2017 g_Console_Add(Format(_lc[I_PLAYER_FLAG_CAPTURE], [Pl.Name, s, ts]), True);
2018 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_CAPTURE], [AnsiUpperCase(s)]), 144);
2020 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2021 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2022 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2023 a := 0
2024 else
2025 a := 1;
2027 if not sound_cap_flag[a].IsPlaying() then
2028 sound_cap_flag[a].Play();
2029 end;
2031 FLAG_STATE_RETURNED:
2032 begin
2033 g_Map_ResetFlag(Fl);
2034 if Quiet then Exit;
2036 if Fl = FLAG_RED then
2037 s := _lc[I_PLAYER_FLAG_RED]
2038 else
2039 s := _lc[I_PLAYER_FLAG_BLUE];
2041 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
2043 if ((Pl = gPlayer1) or (Pl = gPlayer2)
2044 or ((gPlayer1 <> nil) and (gPlayer1.Team = Pl.Team))
2045 or ((gPlayer2 <> nil) and (gPlayer2.Team = Pl.Team))) then
2046 a := 0
2047 else
2048 a := 1;
2050 if not sound_ret_flag[a].IsPlaying() then
2051 sound_ret_flag[a].Play();
2052 end;
2053 end;
2054 end;
2056 procedure MC_RECV_GameSettings(var M: TMsg);
2057 begin
2058 gGameSettings.GameMode := M.ReadByte();
2059 gGameSettings.GoalLimit := M.ReadWord();
2060 gGameSettings.TimeLimit := M.ReadWord();
2061 gGameSettings.MaxLives := M.ReadByte();
2062 gGameSettings.Options := M.ReadLongWord();
2063 end;
2065 // PLAYER
2067 function MC_RECV_PlayerCreate(var M: TMsg): Word;
2068 var
2069 PID, DID: Word;
2070 PName, Model: string;
2071 Color: TRGB;
2072 T: Byte;
2073 Pl: TPlayer;
2074 begin
2075 PID := M.ReadWord();
2076 Pl := g_Player_Get(PID);
2078 PName := M.ReadString();
2079 Model := M.ReadString();
2080 Color.R := M.ReadByte();
2081 Color.G := M.ReadByte();
2082 Color.B := M.ReadByte();
2083 T := M.ReadByte();
2085 Result := 0;
2086 if (PID <> NetPlrUID1) and (PID <> NetPlrUID2) then
2087 begin
2088 if (Pl <> nil) then Exit;
2089 DID := g_Player_Create(Model, Color, T, False);
2090 with g_Player_Get(DID) do
2091 begin
2092 UID := PID;
2093 Name := PName;
2094 Reset(True);
2095 end;
2096 end
2097 else
2098 begin
2099 if (PID = NetPlrUID1) and (gPlayer1 <> nil) then begin
2100 gPlayer1.UID := PID;
2101 gPlayer1.Model.SetColor(Color.R, Color.G, Color.B);
2102 gPlayer1.ChangeTeam(T);
2103 end;
2104 if (PID = NetPlrUID2) and (gPlayer2 <> nil) then begin
2105 gPlayer2.UID := PID;
2106 gPlayer2.Model.SetColor(Color.R, Color.G, Color.B);
2107 gPlayer2.ChangeTeam(T);
2108 end;
2109 end;
2111 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
2112 e_WriteLog('NET: Player ' + PName + ' [' + IntToStr(PID) + '] added.', TMsgType.Notify);
2113 Result := PID;
2114 end;
2116 function MC_RECV_PlayerPos(var M: TMsg): Word;
2117 var
2118 GT: LongWord;
2119 PID: Word;
2120 kByte: Word;
2121 Pl: TPlayer;
2122 Dir: Byte;
2123 TmpX, TmpY: Integer;
2124 begin
2125 Result := 0;
2127 GT := M.ReadLongWord();
2128 if GT < gTime - NET_MAX_DIFFTIME then
2129 begin
2130 gTime := GT;
2131 Exit;
2132 end;
2133 gTime := GT;
2135 PID := M.ReadWord();
2136 Pl := g_Player_Get(PID);
2138 if Pl = nil then Exit;
2140 Result := PID;
2142 with Pl do
2143 begin
2144 FPing := M.ReadWord();
2145 FLoss := M.ReadByte();
2146 kByte := M.ReadWord();
2147 Dir := M.ReadByte();
2149 TmpX := M.ReadLongInt();
2150 TmpY := M.ReadLongInt();
2152 ReleaseKeys;
2154 if (kByte = NET_KEY_CHAT) then
2155 PressKey(KEY_CHAT, 10000)
2156 else
2157 begin
2158 if LongBool(kByte and NET_KEY_LEFT) then PressKey(KEY_LEFT, 10000);
2159 if LongBool(kByte and NET_KEY_RIGHT) then PressKey(KEY_RIGHT, 10000);
2160 if LongBool(kByte and NET_KEY_UP) then PressKey(KEY_UP, 10000);
2161 if LongBool(kByte and NET_KEY_DOWN) then PressKey(KEY_DOWN, 10000);
2162 if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
2163 end;
2165 if ((Pl <> gPlayer1) and (Pl <> gPlayer2)) or LongBool(kByte and NET_KEY_FORCEDIR) then
2166 SetDirection(TDirection(Dir));
2168 GameVelX := M.ReadLongInt();
2169 GameVelY := M.ReadLongInt();
2170 GameAccelX := M.ReadLongInt();
2171 GameAccelY := M.ReadLongInt();
2172 SetLerp(TmpX, TmpY);
2173 if NetForcePlayerUpdate then Update();
2174 end;
2175 end;
2177 function MC_RECV_PlayerStats(var M: TMsg): Word;
2178 var
2179 PID: Word;
2180 Pl: TPlayer;
2181 I, OldFire: Integer;
2182 OldJet, Flam: Boolean;
2183 NewTeam: Byte;
2184 begin
2185 PID := M.ReadWord();
2186 Pl := g_Player_Get(PID);
2187 Result := 0;
2188 if Pl = nil then
2189 Exit;
2191 with Pl do
2192 begin
2193 alive := (M.ReadByte() <> 0);
2194 GodMode := (M.ReadByte() <> 0);
2195 Health := M.ReadLongInt();
2196 Armor := M.ReadLongInt();
2197 Air := M.ReadLongInt();
2198 JetFuel := M.ReadLongInt();
2199 Lives := M.ReadByte();
2200 NewTeam := M.ReadByte();
2202 for I := WP_FIRST to WP_LAST do
2203 FWeapon[I] := (M.ReadByte() <> 0);
2205 for I := A_BULLETS to A_HIGH do
2206 FAmmo[I] := M.ReadWord();
2208 for I := A_BULLETS to A_HIGH do
2209 FMaxAmmo[I] := M.ReadWord();
2211 for I := MR_SUIT to MR_MAX do
2212 FMegaRulez[I] := M.ReadLongWord();
2214 FRulez := [];
2215 if (M.ReadByte() <> 0) then
2216 FRulez := FRulez + [R_ITEM_BACKPACK];
2217 if (M.ReadByte() <> 0) then
2218 FRulez := FRulez + [R_KEY_RED];
2219 if (M.ReadByte() <> 0) then
2220 FRulez := FRulez + [R_KEY_GREEN];
2221 if (M.ReadByte() <> 0) then
2222 FRulez := FRulez + [R_KEY_BLUE];
2223 if (M.ReadByte() <> 0) then
2224 FRulez := FRulez + [R_BERSERK];
2226 Frags := M.ReadLongInt();
2227 Death := M.ReadLongInt();
2229 SetWeapon(M.ReadByte());
2231 FSpectator := M.ReadByte() <> 0;
2232 if FSpectator then
2233 begin
2234 if Pl = gPlayer1 then
2235 begin
2236 gLMSPID1 := UID;
2237 gPlayer1 := nil;
2238 end;
2239 if Pl = gPlayer2 then
2240 begin
2241 gLMSPID2 := UID;
2242 gPlayer2 := nil;
2243 end;
2244 end
2245 else
2246 begin
2247 if (gPlayer1 = nil) and (gLMSPID1 > 0) then
2248 gPlayer1 := g_Player_Get(gLMSPID1);
2249 if (gPlayer2 = nil) and (gLMSPID2 > 0) then
2250 gPlayer2 := g_Player_Get(gLMSPID2);
2251 end;
2252 FGhost := M.ReadByte() <> 0;
2253 FPhysics := M.ReadByte() <> 0;
2254 FNoRespawn := M.ReadByte() <> 0;
2255 OldJet := FJetpack;
2256 FJetpack := M.ReadByte() <> 0;
2257 OldFire := FFireTime;
2258 FFireTime := M.ReadLongInt();
2259 if (OldFire <= 0) and (FFireTime > 0) then
2260 g_Sound_PlayExAt('SOUND_IGNITE', Obj.X, Obj.Y);
2261 Flam := M.ReadByte() <> 0;
2262 if OldJet and not FJetpack then
2263 JetpackOff
2264 else if not OldJet and FJetpack then
2265 JetpackOn;
2266 if FFlaming and not Flam then
2267 FlamerOff;
2268 if Team <> NewTeam then
2269 Pl.ChangeTeam(NewTeam);
2270 end;
2272 Result := PID;
2273 end;
2275 function MC_RECV_PlayerDamage(var M: TMsg): Word;
2276 var
2277 PID: Word;
2278 Pl: TPlayer;
2279 Kind: Byte;
2280 Attacker, Value: Word;
2281 VX, VY: Integer;
2282 begin
2283 Result := 0;
2284 if not gGameOn then Exit;
2285 PID := M.ReadWord();
2286 Pl := g_Player_Get(PID);
2287 if Pl = nil then Exit;
2289 Kind := M.ReadByte();
2290 Attacker := M.ReadWord();
2291 Value := M.ReadWord();
2292 VX := M.ReadWord();
2293 VY := M.ReadWord();
2295 with Pl do
2296 Damage(Value, Attacker, VX, VY, Kind);
2298 Result := PID;
2299 end;
2301 function MC_RECV_PlayerDeath(var M: TMsg): Word;
2302 var
2303 PID: Word;
2304 Pl: TPlayer;
2305 KillType, DeathType: Byte;
2306 Attacker: Word;
2307 begin
2308 Result := 0;
2309 if not gGameOn then Exit;
2310 PID := M.ReadWord();
2311 Pl := g_Player_Get(PID);
2312 if Pl = nil then Exit;
2314 KillType := M.ReadByte();
2315 DeathType := M.ReadByte();
2316 Attacker := M.ReadWord();
2318 with Pl do
2319 begin
2320 Kill(KillType, Attacker, DeathType);
2321 SoftReset;
2322 end;
2323 end;
2325 function MC_RECV_PlayerDelete(var M: TMsg): Word;
2326 var
2327 PID: Word;
2328 Pl: TPlayer;
2329 begin
2330 PID := M.ReadWord();
2331 Pl := g_Player_Get(PID);
2332 Result := 0;
2333 if Pl = nil then Exit;
2335 g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [Pl.Name]), True);
2336 e_WriteLog('NET: Player ' + Pl.Name + ' [' + IntToStr(PID) + '] removed.', TMsgType.Notify);
2338 g_Player_Remove(PID);
2340 Result := PID;
2341 end;
2343 function MC_RECV_PlayerFire(var M: TMsg): Word;
2344 var
2345 PID: Word;
2346 Weap: Byte;
2347 Pl: TPlayer;
2348 X, Y, AX, AY: Integer;
2349 SHID: Integer;
2350 begin
2351 Result := 0;
2352 if not gGameOn then Exit;
2353 PID := M.ReadWord();
2354 Pl := g_Player_Get(PID);
2355 if Pl = nil then Exit;
2357 Weap := M.ReadByte();
2358 X := M.ReadLongInt();
2359 Y := M.ReadLongInt();
2360 AX := M.ReadLongInt();
2361 AY := M.ReadLongInt();
2362 SHID := M.ReadLongInt();
2364 with Pl do
2365 if alive then NetFire(Weap, X, Y, AX, AY, SHID);
2366 end;
2368 procedure MC_RECV_PlayerSettings(var M: TMsg);
2369 var
2370 TmpName: string;
2371 TmpModel: string;
2372 TmpColor: TRGB;
2373 TmpTeam: Byte;
2374 Pl: TPlayer;
2375 PID: Word;
2376 begin
2377 PID := M.ReadWord();
2378 Pl := g_Player_Get(PID);
2379 if Pl = nil then Exit;
2381 TmpName := M.ReadString();
2382 TmpModel := M.ReadString();
2383 TmpColor.R := M.ReadByte();
2384 TmpColor.G := M.ReadByte();
2385 TmpColor.B := M.ReadByte();
2386 TmpTeam := M.ReadByte();
2388 if (gGameSettings.GameMode in [GM_TDM, GM_CTF]) and (Pl.Team <> TmpTeam) then
2389 begin
2390 Pl.ChangeTeam(TmpTeam);
2391 if gPlayer1 = Pl then
2392 gPlayer1Settings.Team := TmpTeam;
2393 if gPlayer2 = Pl then
2394 gPlayer2Settings.Team := TmpTeam;
2395 end else
2396 Pl.SetColor(TmpColor);
2398 if Pl.Name <> TmpName then
2399 begin
2400 g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
2401 Pl.Name := TmpName;
2402 end;
2404 if TmpModel <> Pl.Model.Name then
2405 Pl.SetModel(TmpModel);
2406 end;
2408 // ITEM
2410 procedure MC_RECV_ItemSpawn(var M: TMsg);
2411 var
2412 ID: Word;
2413 AID: DWord;
2414 X, Y, VX, VY: Integer;
2415 T: Byte;
2416 Quiet, Fall{, Resp}: Boolean;
2417 Anim: TAnimation;
2418 it: PItem;
2419 begin
2420 if not gGameOn then Exit;
2421 ID := M.ReadWord();
2422 Quiet := M.ReadByte() <> 0;
2423 T := M.ReadByte();
2424 Fall := M.ReadByte() <> 0;
2425 {Resp :=} M.ReadByte();
2426 X := M.ReadLongInt();
2427 Y := M.ReadLongInt();
2428 VX := M.ReadLongInt();
2429 VY := M.ReadLongInt();
2431 g_Items_Create(X, Y, T and $7F, Fall, False, False, ID);
2432 if ((T and $80) <> 0) then g_Items_SetDrop(ID);
2434 it := g_Items_ByIdx(ID);
2435 it.Obj.Vel.X := VX;
2436 it.Obj.Vel.Y := VY;
2438 if not Quiet then
2439 begin
2440 g_Sound_PlayExAt('SOUND_ITEM_RESPAWNITEM', X, Y);
2441 if g_Frames_Get(AID, 'FRAMES_ITEM_RESPAWN') then
2442 begin
2443 Anim := TAnimation.Create(AID, False, 4);
2444 g_GFX_OnceAnim(X+(it.Obj.Rect.Width div 2)-16, Y+(it.Obj.Rect.Height div 2)-16, Anim);
2445 Anim.Free();
2446 end;
2447 end;
2448 end;
2450 procedure MC_RECV_ItemDestroy(var M: TMsg);
2451 var
2452 ID: Word;
2453 Quiet: Boolean;
2454 begin
2455 if not gGameOn then Exit;
2456 ID := M.ReadWord();
2457 Quiet := M.ReadByte() <> 0;
2459 if not g_Items_ValidId(ID) then exit;
2461 if not Quiet then g_Items_EmitPickupSound(ID);
2463 g_Items_Remove(ID);
2464 end;
2466 // PANEL
2468 procedure MC_RECV_PanelTexture(var M: TMsg);
2469 var
2470 TP: TPanel;
2471 PGUID: Integer;
2472 Tex, Fr: Integer;
2473 Loop, Cnt: Byte;
2474 begin
2475 if not gGameOn then Exit;
2477 PGUID := Integer(M.ReadLongWord());
2478 Tex := M.ReadLongInt();
2479 Fr := M.ReadLongInt();
2480 Cnt := M.ReadByte();
2481 Loop := M.ReadByte();
2483 TP := g_Map_PanelByGUID(PGUID);
2484 if (TP <> nil) then
2485 begin
2486 // switch texture
2487 TP.SetTexture(Tex, Loop);
2488 TP.SetFrame(Fr, Cnt);
2489 end;
2490 end;
2492 procedure MC_RECV_PanelState(var M: TMsg);
2493 var
2494 PGUID: Integer;
2495 E: Boolean;
2496 Lift: Byte;
2497 X, Y, W, H: Integer;
2498 TP: TPanel;
2499 speedX, speedY, startX, startY, endX, endY: Integer;
2500 sizeSpX, sizeSpY, sizeEX, sizeEY: Integer;
2501 mpflags: Byte;
2502 begin
2503 if not gGameOn then Exit;
2505 PGUID := Integer(M.ReadLongWord());
2506 E := (M.ReadByte() <> 0);
2507 Lift := M.ReadByte();
2508 X := M.ReadLongInt();
2509 Y := M.ReadLongInt();
2510 W := M.ReadWord();
2511 H := M.ReadWord();
2512 // mplats
2513 speedX := M.ReadLongInt();
2514 speedY := M.ReadLongInt();
2515 startX := M.ReadLongInt();
2516 startY := M.ReadLongInt();
2517 endX := M.ReadLongInt();
2518 endY := M.ReadLongInt();
2519 sizeSpX := M.ReadLongInt();
2520 sizeSpY := M.ReadLongInt();
2521 sizeEX := M.ReadLongInt();
2522 sizeEY := M.ReadLongInt();
2523 mpflags := M.ReadByte(); // bit0: TP.movingActive; bit1: TP.moveOnce
2525 TP := g_Map_PanelByGUID(PGUID);
2526 if (TP = nil) then exit;
2528 // update lifts state
2529 if TP.isGLift then g_Map_SetLiftGUID(PGUID, Lift);
2531 // update enabled/disabled state for all panels
2532 if E then g_Map_EnableWallGUID(PGUID) else g_Map_DisableWallGUID(PGUID);
2534 // update panel position, as it can be moved (mplat)
2535 TP.X := X;
2536 TP.Y := Y;
2537 TP.Width := W;
2538 TP.Height := H;
2539 // update mplat state
2540 TP.movingSpeedX := speedX;
2541 TP.movingSpeedY := speedY;
2542 TP.movingStartX := startX;
2543 TP.movingStartY := startY;
2544 TP.movingEndX := endX;
2545 TP.movingEndY := endY;
2546 TP.sizeSpeedX := sizeSpX;
2547 TP.sizeSpeedY := sizeSpY;
2548 TP.sizeEndX := sizeEX;
2549 TP.sizeEndY := sizeEY;
2550 TP.movingActive := ((mpflags and 1) <> 0);
2551 TP.moveOnce := ((mpflags and 2) <> 0);
2552 // notify panel of it's position/size change, so it can fix other internal structures
2553 TP.positionChanged();
2554 end;
2556 // TRIGGERS
2558 procedure MC_RECV_TriggerSound(var M: TMsg);
2559 var
2560 SPlaying: Boolean;
2561 SPos, SID: LongWord;
2562 SCount: LongInt;
2563 I: Integer;
2564 begin
2565 if not gGameOn then Exit;
2566 if gTriggers = nil then Exit;
2568 SID := M.ReadLongWord();
2569 SPlaying := M.ReadByte() <> 0;
2570 SPos := M.ReadLongWord();
2571 SCount := M.ReadLongInt();
2573 for I := Low(gTriggers) to High(gTriggers) do
2574 if gTriggers[I].TriggerType = TRIGGER_SOUND then
2575 if gTriggers[I].ClientID = SID then
2576 with gTriggers[I] do
2577 begin
2578 if Sound <> nil then
2579 begin
2580 if SPlaying then
2581 begin
2582 if tgcLocal then
2583 Sound.PlayVolumeAt(X+(Width div 2), Y+(Height div 2), tgcVolume/255.0)
2584 else
2585 Sound.PlayPanVolume((tgcPan-127.0)/128.0, tgcVolume/255.0);
2586 Sound.SetPosition(SPos);
2587 end
2588 else
2589 if Sound.IsPlaying then Sound.Stop;
2590 end;
2592 SoundPlayCount := SCount;
2593 end;
2594 end;
2596 procedure MC_RECV_TriggerMusic(var M: TMsg);
2597 var
2598 MName: string;
2599 MPlaying: Boolean;
2600 MPos: LongWord;
2601 MPaused: Boolean;
2602 begin
2603 if not gGameOn then Exit;
2605 MName := M.ReadString();
2606 MPlaying := M.ReadByte() <> 0;
2607 MPos := M.ReadLongWord();
2608 MPaused := M.ReadByte() <> 0;
2609 MPos := MPos+1; //k8: stfu, fpc!
2611 if MPlaying then
2612 begin
2613 gMusic.SetByName(MName);
2614 gMusic.Play(True);
2615 // gMusic.SetPosition(MPos);
2616 gMusic.SpecPause := MPaused;
2617 end
2618 else
2619 if gMusic.IsPlaying then gMusic.Stop;
2620 end;
2622 // MONSTERS
2624 procedure MC_RECV_MonsterSpawn(var M: TMsg);
2625 var
2626 ID: Word;
2627 MType, MState, MDir, MAnim, MBehav: Byte;
2628 X, Y, VX, VY, MTargTime, MHealth, MAmmo, MSleep: Integer;
2629 MTarg: Word;
2630 Mon: TMonster;
2631 begin
2632 ID := M.ReadWord();
2633 Mon := g_Monsters_ByUID(ID);
2634 if Mon <> nil then
2635 Exit;
2637 MType := M.ReadByte();
2638 MState := M.ReadByte();
2639 MAnim := M.ReadByte();
2640 MTarg := M.ReadWord();
2641 MTargTime := M.ReadLongInt();
2642 MBehav := M.ReadByte();
2643 MSleep := M.ReadLongInt();
2644 MHealth := M.ReadLongInt();
2645 MAmmo := M.ReadLongInt();
2647 X := M.ReadLongInt();
2648 Y := M.ReadLongInt();
2649 VX := M.ReadLongInt();
2650 VY := M.ReadLongInt();
2651 MDir := M.ReadByte();
2653 g_Monsters_Create(MType, X, Y, TDirection(MDir), False, ID);
2654 Mon := g_Monsters_ByUID(ID);
2655 if Mon = nil then
2656 Exit;
2658 with Mon do
2659 begin
2661 MonsterAnim := MAnim;
2662 MonsterTargetUID := MTarg;
2663 MonsterTargetTime := MTargTime;
2664 MonsterBehaviour := MBehav;
2665 MonsterSleep := MSleep;
2666 MonsterAmmo := MAmmo;
2667 SetHealth(MHealth);
2669 SetState(MState);
2671 setPosition(X, Y); // this will call positionChanged();
2672 GameVelX := VX;
2673 GameVelY := VY;
2674 end;
2675 end;
2677 procedure MC_RECV_MonsterPos(var M: TMsg);
2678 var
2679 Mon: TMonster;
2680 ID: Word;
2681 X, Y: Integer;
2682 begin
2683 ID := M.ReadWord();
2684 Mon := g_Monsters_ByUID(ID);
2685 if Mon = nil then
2686 Exit;
2688 with Mon do
2689 begin
2690 X := M.ReadLongInt();
2691 Y := M.ReadLongInt();
2692 Mon.setPosition(X, Y); // this will call `positionChanged()`
2693 GameVelX := M.ReadLongInt();
2694 GameVelY := M.ReadLongInt();
2695 GameDirection := TDirection(M.ReadByte());
2696 end;
2697 end;
2699 procedure MC_RECV_MonsterState(var M: TMsg);
2700 var
2701 ID, OldFire: Integer;
2702 MState, MFAnm: Byte;
2703 Mon: TMonster;
2704 AnimRevert: Boolean;
2705 begin
2706 ID := M.ReadWord();
2707 Mon := g_Monsters_ByUID(ID);
2708 if Mon = nil then Exit;
2710 MState := M.ReadByte();
2711 MFAnm := M.ReadByte();
2713 with Mon do
2714 begin
2715 MonsterTargetUID := M.ReadWord();
2716 MonsterTargetTime := M.ReadLongInt();
2717 MonsterSleep := M.ReadLongInt();
2718 MonsterHealth := M.ReadLongInt();
2719 MonsterAmmo := M.ReadLongInt();
2720 MonsterPain := M.ReadLongInt();
2721 AnimRevert := M.ReadByte() <> 0;
2722 OldFire := FFireTime;
2723 FFireTime := M.ReadLongInt();
2724 if (OldFire <= 0) and (FFireTime > 0) then
2725 g_Sound_PlayExAt('SOUND_IGNITE', Obj.X, Obj.Y);
2726 RevertAnim(AnimRevert);
2728 if MonsterState <> MState then
2729 begin
2730 if (MState = MONSTATE_GO) and (MonsterState = MONSTATE_SLEEP) then WakeUpSound();
2731 if (MState = MONSTATE_DIE) then DieSound();
2732 if (MState = MONSTATE_PAIN) then MakeBloodSimple(Min(200, MonsterPain));
2733 if (MState = MONSTATE_ATTACK) then kick(nil);
2734 if (MState = MONSTATE_DEAD) then SetDeadAnim();
2736 SetState(MState, MFAnm);
2737 end;
2738 end;
2739 end;
2741 procedure MC_RECV_MonsterShot(var M: TMsg);
2742 var
2743 ID: Integer;
2744 Mon: TMonster;
2745 X, Y, VX, VY: Integer;
2746 begin
2747 ID := M.ReadWord();
2749 Mon := g_Monsters_ByUID(ID);
2750 if Mon = nil then Exit;
2752 X := M.ReadLongInt();
2753 Y := M.ReadLongInt();
2754 VX := M.ReadLongInt();
2755 VY := M.ReadLongInt();
2757 Mon.ClientAttack(X, Y, VX, VY);
2758 end;
2760 procedure MC_RECV_MonsterDelete(var M: TMsg);
2761 var
2762 ID: Integer;
2763 Mon: TMonster;
2764 begin
2765 ID := M.ReadWord();
2766 Mon := g_Monsters_ByUID(ID);
2767 if Mon = nil then Exit;
2768 Mon.SetState(5);
2769 Mon.MonsterRemoved := True;
2770 end;
2772 procedure MC_RECV_TimeSync(var M: TMsg);
2773 var
2774 Time: LongWord;
2775 begin
2776 Time := M.ReadLongWord();
2778 if gState = STATE_INTERCUSTOM then
2779 gServInterTime := Min(Time, 255);
2780 end;
2782 procedure MC_RECV_VoteEvent(var M: TMsg);
2783 var
2784 EvID: Byte;
2785 Str1, Str2: string;
2786 Int1, Int2: SmallInt;
2787 begin
2788 EvID := M.ReadByte();
2789 Int1 := M.ReadSmallInt();
2790 Int2 := M.ReadSmallInt();
2791 Str1 := M.ReadString();
2792 Str2 := M.ReadString();
2794 case EvID of
2795 NET_VE_STARTED:
2796 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_STARTED], [Str1, Str2, Int1]), True);
2797 NET_VE_PASSED:
2798 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_PASSED], [Str1]), True);
2799 NET_VE_FAILED:
2800 g_Console_Add(_lc[I_MESSAGE_VOTE_FAILED], True);
2801 NET_VE_VOTE:
2802 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Str1, Int1, Int2]), True);
2803 NET_VE_INPROGRESS:
2804 g_Console_Add(Format(_lc[I_MESSAGE_VOTE_INPROGRESS], [Str1]), True);
2805 end;
2806 end;
2808 // CLIENT SEND
2810 procedure MC_SEND_Info(Password: string);
2811 begin
2812 NetOut.Clear();
2814 NetOut.Write(Byte(NET_MSG_INFO));
2815 NetOut.Write(GAME_VERSION);
2816 NetOut.Write(Password);
2817 NetOut.Write(gPlayer1Settings.Name);
2818 NetOut.Write(gPlayer1Settings.Model);
2819 NetOut.Write(gPlayer1Settings.Color.R);
2820 NetOut.Write(gPlayer1Settings.Color.G);
2821 NetOut.Write(gPlayer1Settings.Color.B);
2822 NetOut.Write(gPlayer1Settings.Team);
2824 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2825 end;
2827 procedure MC_SEND_Chat(Txt: string; Mode: Byte);
2828 begin
2829 NetOut.Write(Byte(NET_MSG_CHAT));
2830 NetOut.Write(Txt);
2831 NetOut.Write(Mode);
2833 g_Net_Client_Send(True, NET_CHAN_CHAT);
2834 end;
2836 procedure MC_SEND_PlayerPos();
2837 var
2838 kByte: Word;
2839 Predict: Boolean;
2840 strafeDir: Byte;
2841 WeaponSelect: Word = 0;
2842 i: Integer;
2843 begin
2844 if not gGameOn then Exit;
2845 if gPlayers = nil then Exit;
2846 if gPlayer1 = nil then Exit;
2848 kByte := 0;
2849 Predict := NetPredictSelf; // and (not NetGotKeys);
2851 if (not gConsoleShow) and (not gChatShow) and (g_ActiveWindow = nil) then
2852 begin
2853 strafeDir := P1MoveButton shr 4;
2854 P1MoveButton := P1MoveButton and $0F;
2856 if gPlayerAction[0, ACTION_MOVELEFT] and (not gPlayerAction[0, ACTION_MOVERIGHT]) then
2857 P1MoveButton := 1
2858 else if (not gPlayerAction[0, ACTION_MOVELEFT]) and gPlayerAction[0, ACTION_MOVERIGHT] then
2859 P1MoveButton := 2
2860 else if (not gPlayerAction[0, ACTION_MOVELEFT]) and (not gPlayerAction[0, ACTION_MOVERIGHT]) then
2861 P1MoveButton := 0;
2863 // strafing
2864 if gPlayerAction[0, ACTION_STRAFE] then
2865 begin
2866 // new strafe mechanics
2867 if (strafeDir = 0) then
2868 strafeDir := P1MoveButton; // start strafing
2869 // now set direction according to strafe (reversed)
2870 if (strafeDir = 2) then
2871 gPlayer1.SetDirection(TDirection.D_LEFT)
2872 else if (strafeDir = 1) then
2873 gPlayer1.SetDirection(TDirection.D_RIGHT)
2874 end
2875 else
2876 begin
2877 strafeDir := 0; // not strafing anymore
2878 if (P1MoveButton = 2) and gPlayerAction[0, ACTION_MOVELEFT] then
2879 gPlayer1.SetDirection(TDirection.D_LEFT)
2880 else if (P1MoveButton = 1) and gPlayerAction[0, ACTION_MOVERIGHT] then
2881 gPlayer1.SetDirection(TDirection.D_RIGHT)
2882 else if P1MoveButton <> 0 then
2883 gPlayer1.SetDirection(TDirection(P1MoveButton-1));
2884 end;
2886 gPlayer1.ReleaseKeys;
2887 if P1MoveButton = 1 then
2888 begin
2889 kByte := kByte or NET_KEY_LEFT;
2890 if Predict then gPlayer1.PressKey(KEY_LEFT, 10000);
2891 end;
2892 if P1MoveButton = 2 then
2893 begin
2894 kByte := kByte or NET_KEY_RIGHT;
2895 if Predict then gPlayer1.PressKey(KEY_RIGHT, 10000);
2896 end;
2897 if gPlayerAction[0, ACTION_LOOKUP] then
2898 begin
2899 kByte := kByte or NET_KEY_UP;
2900 gPlayer1.PressKey(KEY_UP, 10000);
2901 end;
2902 if gPlayerAction[0, ACTION_LOOKDOWN] then
2903 begin
2904 kByte := kByte or NET_KEY_DOWN;
2905 gPlayer1.PressKey(KEY_DOWN, 10000);
2906 end;
2907 if gPlayerAction[0, ACTION_JUMP] then
2908 begin
2909 kByte := kByte or NET_KEY_JUMP;
2910 // gPlayer1.PressKey(KEY_JUMP, 10000); // TODO: Make a prediction option
2911 end;
2912 if gPlayerAction[0, ACTION_ATTACK] then kByte := kByte or NET_KEY_FIRE;
2913 if gPlayerAction[0, ACTION_ACTIVATE] then kByte := kByte or NET_KEY_OPEN;
2914 if gPlayerAction[0, ACTION_WEAPNEXT] then kByte := kByte or NET_KEY_NW;
2915 if gPlayerAction[0, ACTION_WEAPPREV] then kByte := kByte or NET_KEY_PW;
2917 gPlayerAction[0, ACTION_WEAPNEXT] := False; // HACK, remove after readyweaon&pendinweapon implementation
2918 gPlayerAction[0, ACTION_WEAPPREV] := False; // HACK, remove after readyweaon&pendinweapon implementation
2920 for i := WP_FIRST to WP_LAST do
2921 begin
2922 if gSelectWeapon[0, i] then
2923 begin
2924 WeaponSelect := WeaponSelect or Word(1 shl i);
2925 gSelectWeapon[0, i] := False
2926 end
2927 end;
2929 // fix movebutton state
2930 P1MoveButton := P1MoveButton or (strafeDir shl 4);
2931 end
2932 else
2933 kByte := NET_KEY_CHAT;
2935 NetOut.Write(Byte(NET_MSG_PLRPOS));
2936 NetOut.Write(gTime);
2937 NetOut.Write(kByte);
2938 NetOut.Write(Byte(gPlayer1.Direction));
2939 NetOut.Write(WeaponSelect);
2940 //e_WriteLog(Format('S:ws=%d', [WeaponSelect]), MSG_WARNING);
2941 g_Net_Client_Send(True, NET_CHAN_PLAYERPOS);
2943 //kBytePrev := kByte;
2944 //kDirPrev := gPlayer1.Direction;
2945 end;
2947 procedure MC_SEND_Vote(Start: Boolean = False; Command: string = 'a');
2948 begin
2949 NetOut.Write(Byte(NET_MSG_VOTE_EVENT));
2950 NetOut.Write(Byte(Start));
2951 NetOut.Write(Command);
2952 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2953 end;
2955 procedure MC_SEND_PlayerSettings();
2956 begin
2957 NetOut.Write(Byte(NET_MSG_PLRSET));
2958 NetOut.Write(gPlayer1Settings.Name);
2959 NetOut.Write(gPlayer1Settings.Model);
2960 NetOut.Write(gPlayer1Settings.Color.R);
2961 NetOut.Write(gPlayer1Settings.Color.G);
2962 NetOut.Write(gPlayer1Settings.Color.B);
2963 NetOut.Write(gPlayer1Settings.Team);
2965 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2966 end;
2968 procedure MC_SEND_FullStateRequest();
2969 begin
2970 NetOut.Write(Byte(NET_MSG_REQFST));
2972 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2973 end;
2975 procedure MC_SEND_CheatRequest(Kind: Byte);
2976 begin
2977 NetOut.Write(Byte(NET_MSG_CHEAT));
2978 NetOut.Write(Kind);
2980 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
2981 end;
2982 procedure MC_SEND_RCONPassword(Password: string);
2983 begin
2984 NetOut.Write(Byte(NET_MSG_RCON_AUTH));
2985 NetOut.Write(Password);
2987 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2988 end;
2989 procedure MC_SEND_RCONCommand(Cmd: string);
2990 begin
2991 NetOut.Write(Byte(NET_MSG_RCON_CMD));
2992 NetOut.Write(Cmd);
2994 g_Net_Client_Send(True, NET_CHAN_SERVICE);
2995 end;
2997 // i have no idea why all this stuff is in here
2999 function ReadFile(const FileName: TFileName): AByte;
3000 var
3001 FileStream : TStream;
3002 fname: string;
3003 begin
3004 e_WriteLog(Format('NETWORK: looking for file "%s"', [FileName]), TMsgType.Notify);
3005 fname := findDiskWad(FileName);
3006 if length(fname) = 0 then
3007 begin
3008 e_WriteLog(Format('NETWORK: file "%s" not found!', [FileName]), TMsgType.Fatal);
3009 SetLength(Result, 0);
3010 exit;
3011 end;
3012 e_WriteLog(Format('NETWORK: found file "%s"', [fname]), TMsgType.Notify);
3013 Result := nil;
3014 FileStream := openDiskFileRO(fname);
3015 try
3016 if FileStream.Size > 0 then
3017 begin
3018 SetLength(Result, FileStream.Size);
3019 FileStream.Read(Result[0], FileStream.Size);
3020 end;
3021 finally
3022 FileStream.Free;
3023 end;
3024 end;
3026 function CreateMapDataMsg(const FileName: TFileName; ResList: TStringList): TMapDataMsg;
3027 var
3028 i: Integer;
3029 begin
3030 Result.MsgId := NET_MSG_MAP_RESPONSE;
3031 Result.FileData := ReadFile(FileName);
3032 Result.FileSize := Length(Result.FileData);
3034 SetLength(Result.ExternalResources, ResList.Count);
3035 for i:=0 to ResList.Count-1 do
3036 begin
3037 Result.ExternalResources[i].Name := ResList.Strings[i];
3038 Result.ExternalResources[i].md5 := MD5File(GameDir+'/wads/'+ResList.Strings[i]);
3039 end;
3040 end;
3042 procedure ResDataMsgToBytes(var bytes: AByte; const ResData: TResDataMsg);
3043 var
3044 ResultStream: TMemoryStream;
3045 begin
3046 ResultStream := TMemoryStream.Create;
3048 ResultStream.WriteBuffer(ResData.MsgId, SizeOf(ResData.MsgId)); //msgId
3049 ResultStream.WriteBuffer(ResData.FileSize, SizeOf(ResData.FileSize)); //file size
3050 ResultStream.WriteBuffer(ResData.FileData[0], ResData.FileSize); //file data
3052 SetLength(bytes, ResultStream.Size);
3053 ResultStream.Seek(0, soFromBeginning);
3054 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
3056 ResultStream.Free;
3057 end;
3059 function ResDataFromMsgStream(msgStream: TMemoryStream):TResDataMsg;
3060 begin
3061 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
3062 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize));
3063 SetLength(Result.FileData, Result.FileSize);
3064 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize);
3065 end;
3067 procedure MapDataMsgToBytes(var bytes: AByte; const MapDataMsg: TMapDataMsg);
3068 var
3069 ResultStream: TMemoryStream;
3070 resCount: Integer;
3071 begin
3072 resCount := Length(MapDataMsg.ExternalResources);
3074 ResultStream := TMemoryStream.Create;
3076 ResultStream.WriteBuffer(MapDataMsg.MsgId, SizeOf(MapDataMsg.MsgId)); //msgId
3077 ResultStream.WriteBuffer(MapDataMsg.FileSize, SizeOf(MapDataMsg.FileSize)); //file size
3078 ResultStream.WriteBuffer(MapDataMsg.FileData[0], MapDataMsg.FileSize); //file data
3080 ResultStream.WriteBuffer(resCount, SizeOf(resCount)); //res count
3081 ResultStream.WriteBuffer(MapDataMsg.ExternalResources[0], resCount*SizeOf(TExternalResourceInfo)); //res data
3083 SetLength(bytes, ResultStream.Size);
3084 ResultStream.Seek(0, soFromBeginning);
3085 ResultStream.ReadBuffer(bytes[0], ResultStream.Size);
3087 ResultStream.Free;
3088 end;
3090 function MapDataFromMsgStream(msgStream: TMemoryStream):TMapDataMsg;
3091 var
3092 resCount: Integer;
3093 begin
3094 msgStream.ReadBuffer(Result.MsgId, SizeOf(Result.MsgId));
3095 msgStream.ReadBuffer(Result.FileSize, SizeOf(Result.FileSize)); //file size
3097 SetLength(Result.FileData, Result.FileSize);
3098 msgStream.ReadBuffer(Result.FileData[0], Result.FileSize); //file data
3100 msgStream.ReadBuffer(resCount, SizeOf(resCount)); //res count
3101 SetLength(Result.ExternalResources, resCount);
3103 msgStream.ReadBuffer(Result.ExternalResources[0], resCount * SizeOf(TExternalResourceInfo)); //res data
3104 end;
3106 function IsValidFileName(const S: String): Boolean;
3107 const
3108 Forbidden: set of Char = ['<', '>', '|', '"', ':', '*', '?'];
3109 var
3110 I: Integer;
3111 begin
3112 Result := S <> '';
3113 for I := 1 to Length(S) do
3114 Result := Result and (not(S[I] in Forbidden));
3115 end;
3117 function IsValidFilePath(const S: String): Boolean;
3118 var
3119 I: Integer;
3120 begin
3121 Result := False;
3122 if not IsValidFileName(S) then exit;
3123 if FileExists(S) then exit;
3124 I := LastDelimiter('\/', S);
3125 if (I > 0) then
3126 if (not DirectoryExists(Copy(S, 1, I-1))) then
3127 exit;
3128 Result := True;
3129 end;
3131 procedure MC_SEND_MapRequest();
3132 begin
3133 NetOut.Write(Byte(NET_MSG_MAP_REQUEST));
3134 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3135 end;
3137 procedure MC_SEND_ResRequest(const resName: AnsiString);
3138 begin
3139 NetOut.Write(Byte(NET_MSG_RES_REQUEST));
3140 NetOut.Write(resName);
3141 g_Net_Client_Send(True, NET_CHAN_IMPORTANT);
3142 end;
3144 procedure MH_RECV_MapRequest(C: pTNetClient; var M: TMsg);
3145 var
3146 peer: pENetPeer;
3147 payload: AByte;
3148 mapDataMsg: TMapDataMsg;
3149 begin
3150 e_WriteLog('NET: Received map request from ' +
3151 DecodeIPV4(C^.Peer.address.host), TMsgType.Notify);
3153 (*
3154 omsg.Alloc(NET_BUFSIZE);
3155 try
3156 omsg.Clear();
3157 dfn := findDiskWad(MapsDir+gGameSettings.WAD);
3158 if (dfn = '') then dfn := '!wad_not_found!.wad'; //FIXME
3159 md5 := MD5File(dfn);
3160 st := openDiskFileRO(dfn);
3161 if not assigned(st) then exit; //wtf?!
3162 size := st.size;
3163 st.Free;
3164 // packet type
3165 omsg.Write(Byte({NTF_SERVER_MAP_INFO}NET_MSG_MAP_RESPONSE));
3166 // map wad name
3167 omsg.Write(gGameSettings.WAD);
3168 // map wad md5
3169 omsg.Write(md5);
3170 // map wad size
3171 omsg.Write(size);
3172 // number of external resources for map
3173 omsg.Write(LongInt(gExternalResources.Count));
3174 // external resource names
3175 for f := 0 to gExternalResources.Count-1 do
3176 begin
3177 omsg.Write(ExtractFileName(gExternalResources[f])); // GameDir+'/wads/'+ResList.Strings[i]
3178 end;
3179 // send packet
3180 pkt := enet_packet_create(omsg.Data, omsg.CurSize, ENET_PACKET_FLAG_RELIABLE);
3181 if not Assigned(pkt) then exit;
3182 peer := NetClients[C^.ID].Peer;
3183 if (enet_peer_send(Peer, NET_CHAN_DOWNLOAD_EX, pkt) <> 0) then exit;
3184 finally
3185 omsg.Free();
3186 end;
3187 *)
3188 mapDataMsg := CreateMapDataMsg(MapsDir + gGameSettings.WAD, gExternalResources);
3189 peer := NetClients[C^.ID].Peer;
3191 MapDataMsgToBytes(payload, mapDataMsg);
3192 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3194 payload := nil;
3195 mapDataMsg.FileData := nil;
3196 mapDataMsg.ExternalResources := nil;
3197 end;
3199 procedure MH_RECV_ResRequest(C: pTNetClient; var M: TMsg);
3200 var
3201 payload: AByte;
3202 peer: pENetPeer;
3203 FileName: String;
3204 resDataMsg: TResDataMsg;
3205 begin
3206 FileName := ExtractFileName(M.ReadString());
3207 e_WriteLog('NET: Received res request: ' + FileName +
3208 ' from ' + DecodeIPV4(C^.Peer.address.host), TMsgType.Notify);
3210 if not IsValidFilePath(FileName) then
3211 begin
3212 e_WriteLog('Invalid filename: ' + FileName, TMsgType.Warning);
3213 exit;
3214 end;
3216 peer := NetClients[C^.ID].Peer;
3218 if gExternalResources.IndexOf(FileName) > -1 then
3219 begin
3220 resDataMsg.MsgId := NET_MSG_RES_RESPONSE;
3221 resDataMsg.FileData := ReadFile(GameDir+'/wads/'+FileName);
3222 resDataMsg.FileSize := Length(resDataMsg.FileData);
3224 ResDataMsgToBytes(payload, resDataMsg);
3225 g_Net_SendData(payload, peer, True, NET_CHAN_DOWNLOAD);
3226 end;
3227 end;
3230 end.