DEADSOFTWARE

no more public `gMonsters`
[d2df-sdl.git] / src / game / g_netmsg.pas
index 1b80ddd13aba6f9a9d031aa5ff3a172ed3002c92..a60f95fe10e2e313b1fd2512029fe5db397c2527 100644 (file)
@@ -13,7 +13,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *)
-{$MODE DELPHI}
+{$INCLUDE ../shared/a_modes.inc}
 unit g_netmsg;
 
 interface
@@ -430,7 +430,8 @@ end;
 
 function  MH_RECV_PlayerPos(C: pTNetClient; P: Pointer): Word;
 var
-  Dir: Byte;
+  Dir, i: Byte;
+  WeaponSelect: Word;
   PID: Word;
   kByte: Word;
   Pl: TPlayer;
@@ -452,8 +453,11 @@ begin
     NetTime := GT;
     kByte := e_Raw_Read_Word(P);
     Dir := e_Raw_Read_Byte(P);
+    WeaponSelect := e_Raw_Read_Word(P);
+    //e_WriteLog(Format('R:ws=%d', [WeaponSelect]), MSG_WARNING);
     if Direction <> TDirection(Dir) then
       JustTeleported := False;
+
     SetDirection(TDirection(Dir));
     ReleaseKeys;
 
@@ -472,6 +476,15 @@ begin
     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 15 do
+    begin
+      if (WeaponSelect and Word(1 shl i)) <> 0 then
+      begin
+        //e_WriteLog(Format(' R:wn=%d', [i]), MSG_WARNING);
+        QueueWeaponSwitch(i);
+      end;
+    end;
   end;
 
   // MH_SEND_PlayerPos(False, PID, C^.ID);
@@ -614,11 +627,26 @@ end;
 // GAME (SEND)
 
 procedure MH_SEND_Everything(CreatePlayers: Boolean = False; ID: Integer = NET_EVERYONE);
+
+  function sendItemRespawn (it: PItem): Boolean;
+  begin
+    result := false; // don't stop
+    MH_SEND_ItemSpawn(True, it.myid, ID);
+  end;
+
+  function sendMonSpawn (monidx: Integer; mon: TMonster): Boolean;
+  begin
+    result := false; // don't stop
+    MH_SEND_MonsterSpawn(mon.UID, ID);
+  end;
+
 var
   I: Integer;
 begin
   if gPlayers <> nil then
+  begin
     for I := Low(gPlayers) to High(gPlayers) do
+    begin
       if gPlayers[I] <> nil then
       begin
         if CreatePlayers then MH_SEND_PlayerCreate(gPlayers[I].UID, ID);
@@ -628,18 +656,11 @@ begin
         if (gPlayers[I].Flag <> FLAG_NONE) and (gGameSettings.GameMode = GM_CTF) then
           MH_SEND_FlagEvent(FLAG_STATE_CAPTURED, gPlayers[I].Flag, gPlayers[I].UID, True, ID);
       end;
-
-  if gItems <> nil then
-  begin
-    for I := High(gItems) downto Low(gItems) do
-      if gItems[I].Live then
-        MH_SEND_ItemSpawn(True, I, ID);
+    end;
   end;
 
-  if gMonsters <> nil then
-    for I := 0 to High(gMonsters) do
-      if gMonsters[I] <> nil then
-        MH_SEND_MonsterSpawn(gMonsters[I].UID, ID);
+  g_Items_ForEachAlive(sendItemRespawn, true); // backwards
+  g_Mons_ForEach(sendMonSpawn);
 
   if gWalls <> nil then
     for I := Low(gWalls) to High(gWalls) do
@@ -1061,13 +1082,13 @@ begin
     e_Buffer_Write(@NetOut, Lives);
     e_Buffer_Write(@NetOut, Team);
 
-    for I := WEAPON_KASTET to WEAPON_SUPERPULEMET do
+    for I := WP_FIRST to WP_LAST do
       e_Buffer_Write(@NetOut, Byte(FWeapon[I]));
 
-    for I := A_BULLETS to A_CELLS do
+    for I := A_BULLETS to A_HIGH do
       e_Buffer_Write(@NetOut, FAmmo[I]);
 
-    for I := A_BULLETS to A_CELLS do
+    for I := A_BULLETS to A_HIGH do
       e_Buffer_Write(@NetOut, FMaxAmmo[I]);
 
     for I := MR_SUIT to MR_MAX do
@@ -1089,6 +1110,7 @@ begin
     e_Buffer_Write(@NetOut, Byte(FPhysics));
     e_Buffer_Write(@NetOut, Byte(FNoRespawn));
     e_Buffer_Write(@NetOut, Byte(FJetpack));
+    e_Buffer_Write(@NetOut, FFireTime);
   end;
 
   g_Net_Host_Send(ID, True, NET_CHAN_PLAYER);
@@ -1165,17 +1187,21 @@ end;
 // ITEM (SEND)
 
 procedure MH_SEND_ItemSpawn(Quiet: Boolean; IID: Word; ID: Integer = NET_EVERYONE);
+var
+  it: PItem;
 begin
+  it := g_ItemByIdx(IID);
+
   e_Buffer_Write(@NetOut, Byte(NET_MSG_ISPAWN));
   e_Buffer_Write(@NetOut, IID);
   e_Buffer_Write(@NetOut, Byte(Quiet));
-  e_Buffer_Write(@NetOut, gItems[IID].ItemType);
-  e_Buffer_Write(@NetOut, Byte(gItems[IID].Fall));
-  e_Buffer_Write(@NetOut, Byte(gItems[IID].Respawnable));
-  e_Buffer_Write(@NetOut, gItems[IID].Obj.X);
-  e_Buffer_Write(@NetOut, gItems[IID].Obj.Y);
-  e_Buffer_Write(@NetOut, gItems[IID].Obj.Vel.X);
-  e_Buffer_Write(@NetOut, gItems[IID].Obj.Vel.Y);
+  e_Buffer_Write(@NetOut, it.ItemType);
+  e_Buffer_Write(@NetOut, Byte(it.Fall));
+  e_Buffer_Write(@NetOut, Byte(it.Respawnable));
+  e_Buffer_Write(@NetOut, it.Obj.X);
+  e_Buffer_Write(@NetOut, it.Obj.Y);
+  e_Buffer_Write(@NetOut, it.Obj.Vel.X);
+  e_Buffer_Write(@NetOut, it.Obj.Vel.Y);
 
   g_Net_Host_Send(ID, True, NET_CHAN_LARGEDATA);
 end;
@@ -1365,6 +1391,7 @@ begin
     e_Buffer_Write(@NetOut, MonsterAmmo);
     e_Buffer_Write(@NetOut, MonsterPain);
     e_Buffer_Write(@NetOut, Byte(AnimIsReverse));
+    e_Buffer_Write(@NetOut, FFireTime);
   end;
 
   g_Net_Host_Send(ID, True, NET_CHAN_MONSTER);
@@ -2114,13 +2141,13 @@ begin
     Lives := e_Raw_Read_Byte(P);
     NewTeam := e_Raw_Read_Byte(P);
 
-    for I := WEAPON_KASTET to WEAPON_SUPERPULEMET do
+    for I := WP_FIRST to WP_LAST do
       FWeapon[I] := (e_Raw_Read_Byte(P) <> 0);
 
-    for I := A_BULLETS to A_CELLS do
+    for I := A_BULLETS to A_HIGH do
       FAmmo[I] := e_Raw_Read_Word(P);
 
-    for I := A_BULLETS to A_CELLS do
+    for I := A_BULLETS to A_HIGH do
       FMaxAmmo[I] := e_Raw_Read_Word(P);
 
     for I := MR_SUIT to MR_MAX do
@@ -2169,6 +2196,7 @@ begin
     FNoRespawn := e_Raw_Read_Byte(P) <> 0;
     OldJet := FJetpack;
     FJetpack := e_Raw_Read_Byte(P) <> 0;
+    FFireTime := e_Raw_Read_LongInt(P);
     if OldJet and not FJetpack then
       JetpackOff
     else if not OldJet and FJetpack then
@@ -2323,6 +2351,7 @@ var
   T: Byte;
   Quiet, Fall{, Resp}: Boolean;
   Anim: TAnimation;
+  it: PItem;
 begin
   if not gGameOn then Exit;
   ID := e_Raw_Read_Word(P);
@@ -2336,8 +2365,10 @@ begin
   VY := e_Raw_Read_LongInt(P);
 
   g_Items_Create(X, Y, T, Fall, False, False, ID);
-  gItems[ID].Obj.Vel.X := VX;
-  gItems[ID].Obj.Vel.Y := VY;
+
+  it := g_ItemByIdx(ID);
+  it.Obj.Vel.X := VX;
+  it.Obj.Vel.Y := VY;
 
   if not Quiet then
   begin
@@ -2345,7 +2376,7 @@ begin
     if g_Frames_Get(AID, 'FRAMES_ITEM_RESPAWN') then
     begin
       Anim := TAnimation.Create(AID, False, 4);
-      g_GFX_OnceAnim(X+(gItems[ID].Obj.Rect.Width div 2)-16, Y+(gItems[ID].Obj.Rect.Height div 2)-16, Anim);
+      g_GFX_OnceAnim(X+(it.Obj.Rect.Width div 2)-16, Y+(it.Obj.Rect.Height div 2)-16, Anim);
       Anim.Free();
     end;
   end;
@@ -2359,42 +2390,10 @@ begin
   if not gGameOn then Exit;
   ID := e_Raw_Read_Word(P);
   Quiet := e_Raw_Read_Byte(P) <> 0;
-  if gItems = nil then Exit;
-  if (ID > High(gItems)) then Exit;
 
-  if not Quiet then
-    if gSoundEffectsDF then
-    begin
-      if gItems[ID].ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL,
-                                 ITEM_INVIS, ITEM_MEDKIT_BLACK, ITEM_JETPACK] then
-        g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ',
-          gItems[ID].Obj.X, gItems[ID].Obj.Y)
-        else
-          if gItems[ID].ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
-                                     ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
-                                     ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET, ITEM_AMMO_BACKPACK] then
-            g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON',
-              gItems[ID].Obj.X, gItems[ID].Obj.Y)
-            else
-              g_Sound_PlayExAt('SOUND_ITEM_GETITEM',
-                gItems[ID].Obj.X, gItems[ID].Obj.Y);
-    end
-    else
-    begin
-      if gItems[ID].ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_SUIT,
-                                 ITEM_MEDKIT_BLACK, ITEM_INVUL, ITEM_INVIS, ITEM_JETPACK] then
-        g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ',
-          gItems[ID].Obj.X, gItems[ID].Obj.Y)
-      else
-        if gItems[ID].ItemType in [ITEM_WEAPON_SAW, ITEM_WEAPON_PISTOL, ITEM_WEAPON_SHOTGUN1, ITEM_WEAPON_SHOTGUN2,
-                                   ITEM_WEAPON_CHAINGUN, ITEM_WEAPON_ROCKETLAUNCHER, ITEM_WEAPON_PLASMA,
-                                   ITEM_WEAPON_BFG, ITEM_WEAPON_SUPERPULEMET] then
-          g_Sound_PlayExAt('SOUND_ITEM_GETWEAPON',
-            gItems[ID].Obj.X, gItems[ID].Obj.Y)
-        else
-          g_Sound_PlayExAt('SOUND_ITEM_GETITEM',
-            gItems[ID].Obj.X, gItems[ID].Obj.Y);
-    end;
+  if not g_ItemValidId(ID) then exit;
+
+  if not Quiet then g_Item_EmitPickupSound(ID);
 
   g_Items_Remove(ID);
 end;
@@ -2608,6 +2607,8 @@ begin
     SetHealth(MHealth);
 
     SetState(MState);
+
+    positionChanged(); // this updates spatial accelerators
   end;
 end;
 
@@ -2628,6 +2629,7 @@ begin
     GameVelX := e_Raw_Read_LongInt(P);
     GameVelY := e_Raw_Read_LongInt(P);
     GameDirection := TDirection(e_Raw_Read_Byte(P));
+    positionChanged(); // this updates spatial accelerators
   end;
 end;
 
@@ -2654,6 +2656,7 @@ begin
     MonsterAmmo := e_Raw_Read_LongInt(P);
     MonsterPain := e_Raw_Read_LongInt(P);
     AnimRevert := e_Raw_Read_Byte(P) <> 0;
+    FFireTime := e_Raw_Read_LongInt(P);
     RevertAnim(AnimRevert);
 
     if MonsterState <> MState then
@@ -2696,14 +2699,15 @@ end;
 procedure MC_RECV_MonsterDelete(P: Pointer);
 var
   ID: Integer;
-  M: TMonster;
+  M, mon: TMonster;
 begin
   ID := e_Raw_Read_Word(P);
   M := g_Monsters_Get(ID);
   if M = nil then Exit;
 
-  gMonsters[ID].SetState(5);
-  gMonsters[ID].MonsterRemoved := True;
+  mon := g_Mons_ByIdx(ID);
+  mon.SetState(5);
+  mon.MonsterRemoved := True;
 end;
 
 procedure MC_RECV_TimeSync(P: Pointer);
@@ -2782,6 +2786,8 @@ var
   kByte: Word;
   Predict: Boolean;
   strafeDir: Byte;
+  WeaponSelect: Word = 0;
+  I: Integer;
 begin
   if not gGameOn then Exit;
   if gPlayers = nil then Exit;
@@ -2805,10 +2811,9 @@ begin
       begin
         // new strafe mechanics
         if (strafeDir = 0) then strafeDir := P1MoveButton; // start strafing
-        // now set direction according to strafe
-             if (strafeDir = 1) then gPlayer1.SetDirection(D_LEFT)
-        else if (strafeDir = 2) then gPlayer1.SetDirection(D_RIGHT)
-        else gPlayer1.SetDirection(TDirection(P1MoveButton-1));
+        // now set direction according to strafe (reversed)
+             if (strafeDir = 2) then gPlayer1.SetDirection(D_LEFT)
+        else if (strafeDir = 1) then gPlayer1.SetDirection(D_RIGHT);
       end
       else
       begin
@@ -2847,6 +2852,9 @@ begin
       if isKeyPressed(KeyOpen, KeyOpen2) then kByte := kByte or NET_KEY_OPEN;
       if isKeyPressed(KeyNextWeapon, KeyNextWeapon2) then kByte := kByte or NET_KEY_NW;
       if isKeyPressed(KeyPrevWeapon, KeyPrevWeapon2) then kByte := kByte or NET_KEY_PW;
+      for I := 0 to High(KeyWeapon) do
+        if isKeyPressed(KeyWeapon[I], KeyWeapon2[I]) then
+          WeaponSelect := WeaponSelect or Word(1 shl I);
     end;
     // fix movebutton state
     P1MoveButton := P1MoveButton or (strafeDir shl 4);
@@ -2858,6 +2866,8 @@ begin
   e_Buffer_Write(@NetOut, gTime);
   e_Buffer_Write(@NetOut, kByte);
   e_Buffer_Write(@NetOut, Byte(gPlayer1.Direction));
+  e_Buffer_Write(@NetOut, WeaponSelect);
+  //e_WriteLog(Format('S:ws=%d', [WeaponSelect]), MSG_WARNING);
   g_Net_Client_Send(True, NET_CHAN_PLAYERPOS);
 
   //kBytePrev := kByte;