DEADSOFTWARE

game: add flag dropping
[d2df-sdl.git] / src / game / g_netmsg.pas
index 244b2ebd716ef55a21674fbd4b375f9c2324aad8..0a4facfdbdc7a0fe1c91cd362962b5fc7e3dadc1 100644 (file)
@@ -112,6 +112,7 @@ const
   NET_EV_PLAYER_TOUCH = 18;
   NET_EV_SECRET       = 19;
   NET_EV_INTER_READY  = 20;
+  NET_EV_LMS_NOSPAWN  = 21;
 
   NET_VE_STARTED      = 1;
   NET_VE_PASSED       = 2;
@@ -128,6 +129,7 @@ const
   NET_CHEAT_SUICIDE  = 1;
   NET_CHEAT_SPECTATE = 2;
   NET_CHEAT_READY    = 3;
+  NET_CHEAT_DROPFLAG = 4;
 
   NET_MAX_DIFFTIME = 5000 div 36;
 
@@ -281,17 +283,15 @@ uses
   g_language, g_monsters, g_netmaster, utils, wadreader, MAPDEF;
 
 const
-  NET_KEY_LEFT     = 1;
-  NET_KEY_RIGHT    = 2;
-  NET_KEY_UP       = 4;
-  NET_KEY_DOWN     = 8;
-  NET_KEY_JUMP     = 16;
-  NET_KEY_FIRE     = 32;
-  NET_KEY_OPEN     = 64;
-  NET_KEY_NW       = 256;
-  NET_KEY_PW       = 512;
-  NET_KEY_CHAT     = 2048;
-  NET_KEY_FORCEDIR = 4096;
+  NET_KEY_LEFT     = 1 shl 0;
+  NET_KEY_RIGHT    = 1 shl 1;
+  NET_KEY_UP       = 1 shl 2;
+  NET_KEY_DOWN     = 1 shl 3;
+  NET_KEY_JUMP     = 1 shl 4;
+  NET_KEY_FIRE     = 1 shl 5;
+  NET_KEY_OPEN     = 1 shl 6;
+  NET_KEY_CHAT     = 1 shl 7;
+  NET_KEY_FORCEDIR = 1 shl 8;
 
 //var
   //kBytePrev: Word = 0;
@@ -429,26 +429,26 @@ begin
     Name := PName;
     FClientID := C^.ID;
     // round in progress, don't spawn
-    if (gGameSettings.MaxLives > 0) and (gLMSRespawn = LMS_RESPAWN_NONE) then
-    begin
-      Lives := 0;
-      FNoRespawn := True;
-      Spectate;
-      FWantsInGame := True; // TODO: look into this later
-      C^.WaitForFirstSpawn := true;
-    end
-    else
-    begin
-      e_LogWritefln('*** client #%u (cid #%u) authenticated...', [C.ID, C.Player]);
-      //e_LogWritefln('spawning player with pid #%u...', [PID]);
-      //Respawn(gGameSettings.GameType = GT_SINGLE);
-      //k8: no, do not spawn a player yet, wait for a 'i am ready' packet
-      Lives := 0;
-      Spectate;
-      FNoRespawn := True;
-      FWantsInGame := false; // TODO: look into this later
-      C^.WaitForFirstSpawn := true;
-    end;
+    e_LogWritefln('*** client #%u (cid #%u) authenticated...', [C.ID, C.Player]);
+    //e_LogWritefln('spawning player with pid #%u...', [PID]);
+    //Respawn(gGameSettings.GameType = GT_SINGLE);
+    //k8: no, do not spawn a player yet, wait for "request full state" packet
+    Lives := 0;
+    Spectate;
+    FNoRespawn := True;
+    // `FWantsInGame` seems to mean "spawn the player on the next occasion".
+    // that is, if we'll set it to `true`, the player can be spawned after
+    // warmup time ran out, for example, regardless of the real player state.
+    // also, this seems to work only for the initial connection. further
+    // map changes could initiate resource downloading, but the player will
+    // be spawned immediately.
+    // the proper solution will require another player state, "ephemeral".
+    // the player should start any map in "ephemeral" state, and turned into
+    // real mobj only when they sent a special "i am ready" packet. this packet
+    // must be sent after receiving the full state, so the player will get a full
+    // map view before going into game.
+    FWantsInGame := false;
+    C^.WaitForFirstSpawn := true;
   end;
 
   //if not C^.WaitForFirstSpawn then
@@ -465,7 +465,11 @@ begin
   if gState in [STATE_INTERCUSTOM, STATE_FOLD] then
     MH_SEND_GameEvent(NET_EV_MAPEND, 0, 'N', C^.ID);
 
-  if NetUseMaster then g_Net_Slist_Update;
+  if NetUseMaster then
+  begin
+    //g_Net_Slist_Update;
+    g_Net_Slist_Pulse();
+  end;
 end;
 
 
@@ -476,11 +480,23 @@ begin
   if not C.WaitForFirstSpawn then exit;
   plr := g_Player_Get(C^.Player);
   if not assigned(plr) then exit;
+  g_Net_Slist_ServerPlayerComes();
   e_LogWritefln('*** client #%u (cid #%u) first spawn', [C.ID, C.Player]);
   C.WaitForFirstSpawn := false;
   plr.FNoRespawn := false;
   plr.FWantsInGame := true; // TODO: look into this later
-  plr.Respawn(False);
+
+  if (gGameSettings.MaxLives > 0) and (gLMSRespawn = LMS_RESPAWN_NONE) then
+  begin
+    plr.Spectate;
+    MH_SEND_GameEvent(NET_EV_LMS_NOSPAWN, 0, 'N', C.ID);
+  end
+  else
+  begin
+    plr.Respawn(False);
+    if gLMSRespawn > LMS_RESPAWN_NONE then
+      MH_SEND_GameEvent(NET_EV_LMS_WARMUP, gLMSRespawnTime - gTime, 'N', C.ID);
+  end;
 end;
 
 
@@ -502,6 +518,7 @@ end;
 function  MH_RECV_PlayerPos(C: pTNetClient; var M: TMsg): Word;
 var
   Dir, i: Byte;
+  WeaponAct: Byte;
   WeaponSelect: Word;
   PID: Word;
   kByte: Word;
@@ -524,6 +541,7 @@ begin
     NetTime := GT;
     kByte := M.ReadWord();
     Dir := M.ReadByte();
+    WeaponAct := M.ReadByte();
     WeaponSelect := M.ReadWord();
     //e_WriteLog(Format('R:ws=%d', [WeaponSelect]), MSG_WARNING);
     if Direction <> TDirection(Dir) then
@@ -545,8 +563,15 @@ begin
     if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
     if LongBool(kByte and NET_KEY_FIRE) then PressKey(KEY_FIRE, 10000);
     if LongBool(kByte and NET_KEY_OPEN) then PressKey(KEY_OPEN, 10000);
-    if LongBool(kByte and NET_KEY_NW) then PressKey(KEY_NEXTWEAPON, 10000);
-    if LongBool(kByte and NET_KEY_PW) then PressKey(KEY_PREVWEAPON, 10000);
+
+    for i := 0 to 7 do
+    begin
+      if (WeaponAct and Byte(1 shl i)) <> 0 then
+      begin
+        //e_WriteLog(Format(' R:wn=%d', [i]), MSG_WARNING);
+        ProcessWeaponAction(i);
+      end;
+    end;
 
     for i := 0 to 15 do
     begin
@@ -577,7 +602,12 @@ begin
     NET_CHEAT_SPECTATE:
     begin
       if Pl.FSpectator then
-        Pl.Respawn(False)
+      begin
+        if (gGameSettings.MaxLives = 0) or (gLMSRespawn > LMS_RESPAWN_NONE) then
+          Pl.Respawn(False)
+        else
+          MH_SEND_GameEvent(NET_EV_LMS_NOSPAWN, Pl.UID);
+      end
       else
         Pl.Spectate;
     end;
@@ -596,6 +626,8 @@ begin
         Dec(gInterReadyCount);
       end;
     end;
+    NET_CHEAT_DROPFLAG:
+      Pl.TryDropFlag();
   end;
 end;
 
@@ -799,28 +831,18 @@ begin
 
   if CreatePlayers and (ID >= 0) then NetClients[ID].State := NET_STATE_GAME;
 
-  if gLMSRespawn > LMS_RESPAWN_NONE then
-  begin
-    e_LogWritefln('*** client #%u (cid #%u) WARMUP', [ID, NetClients[ID].Player]);
-    MH_SEND_GameEvent(NET_EV_LMS_WARMUP, (gLMSRespawnTime - gTime) div 1000, 'N', ID);
-  end;
-
   g_Net_Flush();
 end;
 
 procedure MH_SEND_Info(ID: Byte);
-var
-  Map: string;
 begin
-  Map := g_ExtractFileName(gMapInfo.Map);
-
   NetOut.Clear();
 
   NetOut.Write(Byte(NET_MSG_INFO));
   NetOut.Write(ID);
   NetOut.Write(NetClients[ID].Player);
-  NetOut.Write(gGameSettings.WAD);
-  NetOut.Write(Map);
+  NetOut.Write(ExtractFileName(gGameSettings.WAD));
+  NetOut.Write(g_ExtractFileName(gMapInfo.Map));
   NetOut.Write(gWADHash);
   NetOut.Write(gGameSettings.GameMode);
   NetOut.Write(gGameSettings.GoalLimit);
@@ -1098,9 +1120,10 @@ begin
       if IsKeyPressed(KEY_UP) then kByte := kByte or NET_KEY_UP;
       if IsKeyPressed(KEY_DOWN) then kByte := kByte or NET_KEY_DOWN;
       if IsKeyPressed(KEY_JUMP) then kByte := kByte or NET_KEY_JUMP;
-      if JustTeleported then kByte := kByte or NET_KEY_FORCEDIR;
     end;
 
+    if JustTeleported then kByte := kByte or NET_KEY_FORCEDIR;
+
     NetOut.Write(kByte);
     if Direction = TDirection.D_LEFT then NetOut.Write(Byte(0)) else NetOut.Write(Byte(1));
     NetOut.Write(GameX);
@@ -1166,6 +1189,7 @@ begin
     NetOut.Write(Byte(FJetpack));
     NetOut.Write(FFireTime);
     NetOut.Write(Byte(FFlaming));
+    NetOut.Write(FSpawnInvul);
   end;
 
   g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
@@ -1502,22 +1526,25 @@ begin
 
   if Mode <> NET_CHAT_SYSTEM then
   begin
-    if Mode = NET_CHAT_PLAYER then
+    if NetDeafLevel = 0 then
     begin
-      g_Console_Add(Txt, True);
-      e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
-      g_Game_ChatSound(b_Text_Unformat(Txt));
-    end else
-    if (Mode = NET_CHAT_TEAM) and (gPlayer1 <> nil) then
-    begin
-      if gPlayer1.Team = TEAM_RED then
-        g_Console_Add(b_Text_Format('\r[Team] ') + Txt, True);
-      if gPlayer1.Team = TEAM_BLUE then
-        g_Console_Add(b_Text_Format('\b[Team] ') + Txt, True);
-      e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
-      g_Game_ChatSound(b_Text_Unformat(Txt));
+      if Mode = NET_CHAT_PLAYER then
+      begin
+        g_Console_Add(Txt, True);
+        e_WriteLog('[Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
+        g_Game_ChatSound(b_Text_Unformat(Txt));
+      end else
+      if (Mode = NET_CHAT_TEAM) and (gPlayer1 <> nil) then
+      begin
+        if gPlayer1.Team = TEAM_RED then
+          g_Console_Add(b_Text_Format('\r[Team] ') + Txt, True);
+        if gPlayer1.Team = TEAM_BLUE then
+          g_Console_Add(b_Text_Format('\b[Team] ') + Txt, True);
+        e_WriteLog('[Team Chat] ' + b_Text_Unformat(Txt), TMsgType.Notify);
+        g_Game_ChatSound(b_Text_Unformat(Txt));
+      end;
     end;
-  end else
+  end else if (NetDeafLevel < 2) then
     g_Console_Add(Txt, True);
 end;
 
@@ -1764,6 +1791,7 @@ begin
       NET_EV_MAPEND: goodCmd := true;
       NET_EV_PLAYER_KICK: goodCmd := true;
       NET_EV_PLAYER_BAN: goodCmd := true;
+      NET_EV_LMS_WARMUP: goodCmd := true;
     end;
     if not goodCmd then exit;
   end;
@@ -1785,7 +1813,7 @@ begin
         gGameSettings.GameMode := gSwitchGameMode;
 
         gWADHash := EvHash;
-        if not g_Game_StartMap(EvStr, True) then
+        if not g_Game_StartMap(false{asMegawad}, EvStr, True) then
         begin
           if not isWadPath(EvStr) then
             g_FatalError(Format(_lc[I_GAME_ERROR_MAP_LOAD], [gGameSettings.WAD + ':\' + EvStr]))
@@ -1800,6 +1828,8 @@ begin
 
     NET_EV_MAPEND:
     begin
+      gLMSRespawn := LMS_RESPAWN_NONE;
+      gLMSRespawnTime := 0;
       if (g_Res_received_map_start <> 0) then
       begin
         g_Res_received_map_start := -1;
@@ -1825,10 +1855,13 @@ begin
 
     NET_EV_CHANGE_TEAM:
     begin
-      if EvNum = TEAM_RED then
-        g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [EvStr]), True);
-      if EvNum = TEAM_BLUE then
-        g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [EvStr]), True);
+      if NetDeafLevel < 2 then
+      begin
+        if EvNum = TEAM_RED then
+          g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [EvStr]), True);
+        if EvNum = TEAM_BLUE then
+          g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [EvStr]), True);
+      end;
     end;
 
     NET_EV_PLAYER_KICK:
@@ -1844,13 +1877,24 @@ begin
       end;
 
     NET_EV_LMS_WARMUP:
-      g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [EvNum]), True);
+    begin
+      if EvNum > 0 then
+      begin
+        gLMSRespawn := LMS_RESPAWN_WARMUP;
+        gLMSRespawnTime := gTime + EvNum;
+        g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [EvNum div 1000]), True);
+      end
+      else if gPlayer1 = nil then
+      begin
+        g_Console_Add(_lc[I_PLAYER_SPECT4], True);
+      end;
+    end;
 
     NET_EV_LMS_SURVIVOR:
       g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True);
 
     NET_EV_BIGTEXT:
-      g_Game_Message(AnsiUpperCase(EvStr), Word(EvNum));
+      if NetDeafLevel < 2 then g_Game_Message(AnsiUpperCase(EvStr), Word(EvNum));
 
     NET_EV_SCORE:
     begin
@@ -1930,6 +1974,7 @@ begin
     NET_EV_LMS_START:
     begin
       g_Player_RemoveAllCorpses;
+      gLMSRespawn := LMS_RESPAWN_NONE;
       g_Game_Message(_lc[I_MESSAGE_LMS_START], 144);
     end;
 
@@ -1950,6 +1995,9 @@ begin
     NET_EV_LMS_DRAW:
       g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
 
+    NET_EV_LMS_NOSPAWN:
+      g_Console_Add(_lc[I_PLAYER_SPECT4], True);
+
     NET_EV_KILLCOMBO:
       g_Game_Announce_KillCombo(EvNum);
 
@@ -2190,7 +2238,8 @@ begin
     end;
   end;
 
-  g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
+  if NetDeafLevel < 3 then 
+    g_Console_Add(Format(_lc[I_PLAYER_JOIN], [PName]), True);
   e_WriteLog('NET: Player ' + PName + ' [' + IntToStr(PID) + '] added.', TMsgType.Notify);
   Result := PID;
 end;
@@ -2233,7 +2282,7 @@ begin
 
     ReleaseKeys;
 
-    if (kByte = NET_KEY_CHAT) then
+    if LongBool(kByte and NET_KEY_CHAT) then
       PressKey(KEY_CHAT, 10000)
     else
     begin
@@ -2244,7 +2293,9 @@ begin
       if LongBool(kByte and NET_KEY_JUMP) then PressKey(KEY_JUMP, 10000);
     end;
 
-    if ((Pl <> gPlayer1) and (Pl <> gPlayer2)) or LongBool(kByte and NET_KEY_FORCEDIR) then
+    JustTeleported := LongBool(kByte and NET_KEY_FORCEDIR);
+
+    if ((Pl <> gPlayer1) and (Pl <> gPlayer2)) or JustTeleported then
       SetDirection(TDirection(Dir));
 
     GameVelX := M.ReadLongInt();
@@ -2313,23 +2364,29 @@ begin
     FSpectator := M.ReadByte() <> 0;
     if FSpectator then
     begin
-      if Pl = gPlayer1 then
+      if UID = NetPlrUID1 then
       begin
-        gLMSPID1 := UID;
+        gSpectLatchPID1 := UID;
         gPlayer1 := nil;
       end;
-      if Pl = gPlayer2 then
+      if UID = NetPlrUID2 then
       begin
-        gLMSPID2 := UID;
+        gSpectLatchPID2 := UID;
         gPlayer2 := nil;
       end;
     end
     else
     begin
-      if (gPlayer1 = nil) and (gLMSPID1 > 0) then
-        gPlayer1 := g_Player_Get(gLMSPID1);
-      if (gPlayer2 = nil) and (gLMSPID2 > 0) then
-        gPlayer2 := g_Player_Get(gLMSPID2);
+      if (gPlayer1 = nil) and (gSpectLatchPID1 > 0) and (UID = gSpectLatchPID1) then
+      begin
+        gPlayer1 := Pl;
+        gSpectLatchPID1 := 0;
+      end;
+      if (gPlayer2 = nil) and (gSpectLatchPID2 > 0) and (UID = gSpectLatchPID2) then
+      begin
+        gPlayer2 := Pl;
+        gSpectLatchPID2 := 0;
+      end;
     end;
     FGhost := M.ReadByte() <> 0;
     FPhysics := M.ReadByte() <> 0;
@@ -2341,6 +2398,7 @@ begin
     if (OldFire <= 0) and (FFireTime > 0) then
       g_Sound_PlayExAt('SOUND_IGNITE', Obj.X, Obj.Y);
     Flam := M.ReadByte() <> 0;
+    FSpawnInvul := M.ReadLongInt();
     if OldJet and not FJetpack then
       JetpackOff
     else if not OldJet and FJetpack then
@@ -2414,7 +2472,8 @@ begin
   Result := 0;
   if Pl = nil then Exit;
 
-  g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [Pl.Name]), True);
+  if NetDeafLevel < 3 then 
+    g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [Pl.Name]), True);
   e_WriteLog('NET: Player ' + Pl.Name + ' [' + IntToStr(PID) + '] removed.', TMsgType.Notify);
 
   g_Player_Remove(PID);
@@ -2479,7 +2538,8 @@ begin
 
   if Pl.Name <> TmpName then
   begin
-    g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
+    if NetDeafLevel < 3 then 
+      g_Console_Add(Format(_lc[I_PLAYER_NAME], [Pl.Name, TmpName]), True);
     Pl.Name := TmpName;
   end;
 
@@ -2873,18 +2933,19 @@ begin
   Str1 := M.ReadString();
   Str2 := M.ReadString();
 
-  case EvID of
-    NET_VE_STARTED:
-      g_Console_Add(Format(_lc[I_MESSAGE_VOTE_STARTED], [Str1, Str2, Int1]), True);
-    NET_VE_PASSED:
-      g_Console_Add(Format(_lc[I_MESSAGE_VOTE_PASSED], [Str1]), True);
-    NET_VE_FAILED:
-      g_Console_Add(_lc[I_MESSAGE_VOTE_FAILED], True);
-    NET_VE_VOTE:
-      g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Str1, Int1, Int2]), True);
-    NET_VE_INPROGRESS:
-      g_Console_Add(Format(_lc[I_MESSAGE_VOTE_INPROGRESS], [Str1]), True);
-  end;
+  if NetDeafLevel < 2 then 
+    case EvID of
+      NET_VE_STARTED:
+        g_Console_Add(Format(_lc[I_MESSAGE_VOTE_STARTED], [Str1, Str2, Int1]), True);
+      NET_VE_PASSED:
+        g_Console_Add(Format(_lc[I_MESSAGE_VOTE_PASSED], [Str1]), True);
+      NET_VE_FAILED:
+        g_Console_Add(_lc[I_MESSAGE_VOTE_FAILED], True);
+      NET_VE_VOTE:
+        g_Console_Add(Format(_lc[I_MESSAGE_VOTE_VOTE], [Str1, Int1, Int2]), True);
+      NET_VE_INPROGRESS:
+        g_Console_Add(Format(_lc[I_MESSAGE_VOTE_INPROGRESS], [Str1]), True);
+    end;
 end;
 
 // CLIENT SEND
@@ -2920,6 +2981,7 @@ var
   kByte: Word;
   Predict: Boolean;
   strafeDir: Byte;
+  WeaponAct: Byte = 0;
   WeaponSelect: Word = 0;
   i: Integer;
 begin
@@ -2993,11 +3055,15 @@ begin
     end;
     if gPlayerAction[0, ACTION_ATTACK] then kByte := kByte or NET_KEY_FIRE;
     if gPlayerAction[0, ACTION_ACTIVATE] then kByte := kByte or NET_KEY_OPEN;
-    if gPlayerAction[0, ACTION_WEAPNEXT] then kByte := kByte or NET_KEY_NW;
-    if gPlayerAction[0, ACTION_WEAPPREV] then kByte := kByte or NET_KEY_PW;
 
-    gPlayerAction[0, ACTION_WEAPNEXT] := False; // HACK, remove after readyweaon&pendinweapon implementation
-    gPlayerAction[0, ACTION_WEAPPREV] := False; // HACK, remove after readyweaon&pendinweapon implementation
+    for i := WP_FACT to WP_LACT do
+    begin
+      if gWeaponAction[0, i] then
+      begin
+        WeaponAct := WeaponAct or Byte(1 shl i);
+        gWeaponAction[0, i] := False
+      end
+    end;
 
     for i := WP_FIRST to WP_LAST do
     begin
@@ -3018,6 +3084,7 @@ begin
   NetOut.Write(gTime);
   NetOut.Write(kByte);
   NetOut.Write(Byte(gPlayer1.Direction));
+  NetOut.Write(WeaponAct);
   NetOut.Write(WeaponSelect);
   //e_WriteLog(Format('S:ws=%d', [WeaponSelect]), MSG_WARNING);
   g_Net_Client_Send(True, NET_CHAN_PLAYERPOS);