DEADSOFTWARE

network: code uglification; fixed bug with weapon switching (i hope)
[d2df-sdl.git] / src / game / g_player.pas
index beab911b07dcf0af3a95539712f9e2d413597d9d..897ece3dfc10bb11996e6ebe4df8e0d36fa54819 100644 (file)
@@ -162,8 +162,11 @@ type
     FFlag:      Byte;
     FSecrets:   Integer;
     FCurrWeap:  Byte;
+    //FNetForceWeap: Byte; // spam server with this -- this is new weapon we want to use
+    FNetForceWeapFIdx: LongWord; // frame index; ignore weapon change if it is lesser than this
+    //FCurrFrameIdx: LongWord; // increased in each `Update()`
     FNextWeap:  WORD;
-    FNextWeapDelay: Byte; // frames
+    FNextWeapDelay: Byte; // frames (unused)
     FBFGFireCounter: SmallInt;
     FLastSpawnerUID: Word;
     FLastHit:   Byte;
@@ -200,6 +203,9 @@ type
     FNetTime: LongWord;
     mEDamageType: Integer;
 
+    // client-side only
+    weaponSwitchKeyReleased: array[0..16] of Byte; // bit 0: was released on prev frame; bit 1: new status
+
 
     function CollideLevel(XInc, YInc: Integer): Boolean;
     function StayOnStep(XInc, YInc: Integer): Boolean;
@@ -271,6 +277,7 @@ type
     function    GetRespawnPoint(): Byte;
     procedure   PressKey(Key: Byte; Time: Word = 1);
     procedure   ReleaseKeys();
+    procedure   ReleaseKeysNoWeapon();
     procedure   SetModel(ModelName: String);
     procedure   SetColor(Color: TRGB);
     procedure   SetWeapon(W: Byte);
@@ -333,6 +340,11 @@ type
     procedure getMapBox (out x, y, w, h: Integer); inline;
     procedure moveBy (dx, dy: Integer); inline;
 
+    procedure releaseAllWeaponSwitchKeys ();
+    procedure weaponSwitchKeysStateChange (index: Integer; pressed: Boolean);
+    function isWeaponSwitchKeyReleased (index: Integer): Boolean;
+    procedure weaponSwitchKeysShiftNewStates ();
+
   public
     property    Vel: TPoint2i read FObj.Vel;
     property    Obj: TObj read FObj;
@@ -348,6 +360,9 @@ type
     property    Death: Integer read FDeath write FDeath;
     property    Kills: Integer read FKills write FKills;
     property    CurrWeap: Byte read FCurrWeap write FCurrWeap;
+    //property    NetForceWeap: Byte read FNetForceWeap write FNetForceWeap;
+    property    NetForceWeapFIdx: LongWord read FNetForceWeapFIdx write FNetForceWeapFIdx;
+    //property    CurrFrameIdx: LongWord read FCurrFrameIdx write FCurrFrameIdx;
     property    MonsterKills: Integer read FMonsterKills write FMonsterKills;
     property    Secrets: Integer read FSecrets;
     property    GodMode: Boolean read FGodMode write FGodMode;
@@ -901,6 +916,7 @@ begin
   gPlayers[a].FSecrets := utils.readLongInt(st);
   // Òåêóùåå îðóæèå
   gPlayers[a].FCurrWeap := utils.readByte(st);
+  //gPlayers[a].FNetForceWeap := gPlayers[a].FCurrWeap;
   // Ñëåäóþùåå æåëàåìîå îðóæèå
   gPlayers[a].FNextWeap := utils.readWord(st);
   // ...è ïàóçà
@@ -1355,7 +1371,10 @@ begin
       if gPlayers[i] is TPlayer then
       begin
         gPlayers[i].Update();
-        gPlayers[i].RealizeCurrentWeapon(); // WARNING! DO NOT MOVE THIS INTO `Update()`!
+        if g_Game_IsClient or not g_Game_IsNet then
+        begin
+          gPlayers[i].RealizeCurrentWeapon(); // WARNING! DO NOT MOVE THIS INTO `Update()`!
+        end;
       end
       else
       begin
@@ -2127,9 +2146,57 @@ begin
   FJustTeleported := False;
   FNetTime := 0;
 
+  //FNetForceWeap := FCurrWeap;
+  FNetForceWeapFIdx := 0;
+  //FCurrFrameIdx := 0;
+
   resetWeaponQueue();
+  releaseAllWeaponSwitchKeys();
+end;
+
+
+procedure TPlayer.releaseAllWeaponSwitchKeys ();
+var
+  f: Integer;
+begin
+  for f := 0 to High(weaponSwitchKeyReleased) do weaponSwitchKeyReleased[f] := $03;
+end;
+
+procedure TPlayer.weaponSwitchKeysStateChange (index: Integer; pressed: Boolean);
+begin
+  Inc(index, 2); // -2: prev; -1: next
+  if (index < 0) or (index > High(weaponSwitchKeyReleased)) then exit;
+  weaponSwitchKeyReleased[index] := weaponSwitchKeyReleased[index] or $02;
+  if (pressed) then weaponSwitchKeyReleased[index] := weaponSwitchKeyReleased[index] xor $02;
+end;
+
+function TPlayer.isWeaponSwitchKeyReleased (index: Integer): Boolean;
+begin
+  Inc(index, 2); // -2: prev; -1: next
+  if (index < 0) or (index > High(weaponSwitchKeyReleased)) then
+  begin
+    result := true;
+  end
+  else
+  begin
+    result := (weaponSwitchKeyReleased[index] and $01) <> 0;
+  end;
+end;
+
+procedure TPlayer.weaponSwitchKeysShiftNewStates ();
+var
+  f: Integer;
+begin
+  // copy bit 1 to bit 0
+  for f := 0 to High(weaponSwitchKeyReleased) do
+  begin
+    weaponSwitchKeyReleased[f] :=
+      (weaponSwitchKeyReleased[f] and $02) or
+      ((weaponSwitchKeyReleased[f] shr 1) and $01);
+  end;
 end;
 
+
 procedure TPlayer.positionChanged (); inline;
 begin
 end;
@@ -3537,7 +3604,7 @@ end;
 
 procedure TPlayer.QueueWeaponSwitch(Weapon: Byte);
 begin
-  if g_Game_IsClient then Exit;
+  //if g_Game_IsClient then Exit;
   if Weapon > High(FWeapon) then Exit;
   FNextWeap := FNextWeap or (1 shl Weapon);
 end;
@@ -3553,8 +3620,8 @@ begin
   result := false;
   case weapon of
     WEAPON_KASTET, WEAPON_SAW: result := true;
-    WEAPON_SHOTGUN1, WEAPON_SHOTGUN2: result := (FAmmo[A_SHELLS] > 0);
-    WEAPON_PISTOL, WEAPON_CHAINGUN, WEAPON_SUPERPULEMET: result := (FAmmo[A_BULLETS] > 0);
+    WEAPON_SHOTGUN1, WEAPON_SHOTGUN2, WEAPON_SUPERPULEMET: result := (FAmmo[A_SHELLS] > 0);
+    WEAPON_PISTOL, WEAPON_CHAINGUN: result := (FAmmo[A_BULLETS] > 0);
     WEAPON_ROCKETLAUNCHER: result := (FAmmo[A_ROCKETS] > 0);
     WEAPON_PLASMA, WEAPON_BFG: result := (FAmmo[A_CELLS] > 0);
     WEAPON_FLAMETHROWER: result := (FAmmo[A_FUEL] > 0);
@@ -3598,18 +3665,18 @@ begin
   // two knuckles are for "normal" and "berserk" (see `if` below -- it removes the one that is not needed)
   // priorities:
   //   bfg, launcher, plasma, flamethrower, ssg, minigun, sg, pistol, berserk, chainsaw, fist
-  weaponOrder[0] := WEAPON_BFG;
-  weaponOrder[1] := WEAPON_ROCKETLAUNCHER;
-  weaponOrder[2] := WEAPON_PLASMA;
-  weaponOrder[3] := WEAPON_FLAMETHROWER;
-  weaponOrder[4] := WEAPON_SHOTGUN2;
-  weaponOrder[5] := WEAPON_CHAINGUN;
-  weaponOrder[6] := WEAPON_SHOTGUN1;
-  weaponOrder[7] := WEAPON_PISTOL;
-  weaponOrder[8] := WEAPON_KASTET; // normal fist
-  weaponOrder[9] := WEAPON_SAW;
-  weaponOrder[10] := WEAPON_KASTET+666; // berserk fist
-  weaponOrder[11] := WEAPON_SUPERPULEMET;
+  weaponOrder[0] := WEAPON_SUPERPULEMET;
+  weaponOrder[1] := WEAPON_BFG;
+  weaponOrder[2] := WEAPON_ROCKETLAUNCHER;
+  weaponOrder[3] := WEAPON_PLASMA;
+  weaponOrder[4] := WEAPON_FLAMETHROWER;
+  weaponOrder[5] := WEAPON_SHOTGUN2;
+  weaponOrder[6] := WEAPON_CHAINGUN;
+  weaponOrder[7] := WEAPON_SHOTGUN1;
+  weaponOrder[8] := WEAPON_PISTOL;
+  weaponOrder[9] := WEAPON_KASTET+666; // berserk fist
+  weaponOrder[10] := WEAPON_SAW;
+  weaponOrder[11] := WEAPON_KASTET; // normal fist
 
   for f := 0 to High(weaponOrder) do
   begin
@@ -3659,7 +3726,7 @@ begin
       begin
         //e_WriteLog(Format(' SWITCH: cur=%d; new=%d (dir=%d; log=%d)', [FCurrWeap, rwidx, dir, cwi]), TMsgType.Warning);
         result := Byte(rwidx);
-        FNextWeapDelay := 10;
+        //FNextWeapDelay := 10; //k8: not needed anymore
         exit;
       end;
     end;
@@ -3670,13 +3737,21 @@ begin
   // no cycling
   for i := 0 to High(wantThisWeapon) do wantThisWeapon[i] := false;
   wwc := 0;
+
+  curlidx := -1; // start from a weapon with a highest priority (-1, 'cause loop will immediately increment this index)
+
   for i := 0 to High(FWeapon) do
   begin
     if (FNextWeap and (1 shl i)) <> 0 then
     begin
       cwi := real2log(i);
-      if (cwi >= 0) then wantThisWeapon[cwi] := true;
-      Inc(wwc);
+      if (cwi >= 0) then
+      begin
+        wantThisWeapon[cwi] := true;
+        Inc(wwc);
+        // if we hit currently selected weapon, start seachring from it, so we'll get "next weaker" weapon
+        if (i = FCurrWeap) then curlidx := cwi; // compare real, start from logical
+      end;
     end;
   end;
 
@@ -3700,30 +3775,25 @@ begin
     exit;
   end;
 
-  // exclude currently selected weapon from the set
-  curlidx := real2log(FCurrWeap);
   //e_WriteLog(Format('*** wwc=%d; currweap=%d; logweap=%d', [wwc, FCurrWeap, curlidx]), TMsgType.Warning);
-  //e_WriteLog(Format('FCurrWeap=%d; curlidx=%d', [FCurrWeap, curlidx]), TMsgType.Warning);
-  //if (curlidx >= 0) then wantThisWeapon[curlidx] := false;
-  //if (wwc = 2) then begin Dec(wwc); end; // easy case: switch between two weapons
 
   // find next weapon to switch onto
   cwi := curlidx;
-  for i := 0 to High(FWeapon) do
+  for i := 0 to High(weaponOrder) do
   begin
-    cwi := (cwi+length(FWeapon)+1) mod length(FWeapon);
+    cwi := (cwi+length(weaponOrder)+1) mod length(weaponOrder);
     if (cwi = curlidx) then continue; // skip current weapon
     if not wantThisWeapon[cwi] then continue;
     rwidx := weaponOrder[cwi];
     if (rwidx < 0) then continue;
-    //e_WriteLog(Format('  trying logical %d (real %d)', [cwi, rwidx]), TMsgType.Warning);
+    //e_WriteLog(Format('  trying logical %d (real %d); has=%d, hasammo=%d', [cwi, rwidx, Integer(FWeapon[rwidx]), Integer(hasAmmoForWeapon(rwidx))]), TMsgType.Warning);
     if FWeapon[rwidx] and ((wwc = 1) or hasAmmoForWeapon(rwidx)) then
     begin
       //e_WriteLog('    I FOUND HER!', TMsgType.Warning);
       // i found her!
       result := Byte(rwidx);
       resetWeaponQueue();
-      FNextWeapDelay := 10; // anyway, 'cause why not
+      //FNextWeapDelay := 10; // anyway, 'cause why not; k8: not needed anymore
       exit;
     end;
   end;
@@ -3753,7 +3823,9 @@ var
 begin
   //e_WriteLog(Format('***RealizeCurrentWeapon: FNextWeap=%x; FNextWeapDelay=%d', [FNextWeap, FNextWeapDelay]), MSG_WARNING);
   //FNextWeap := FNextWeap and $1FFF;
-  if FNextWeapDelay > 0 then Dec(FNextWeapDelay); // "alteration delay"
+  //HACK: alteration delay will be reset when player released any weapon switch key
+  FNextWeapDelay := 0; //k8: just in case
+  //if FNextWeapDelay > 0 then Dec(FNextWeapDelay); // "alteration delay"
 
   if not switchAllowed then
   begin
@@ -3774,35 +3846,48 @@ begin
 
   if FWeapon[nw] then
   begin
+    //k8: emulate this on client immediately, or wait for server confirmation?
+    {
+    if g_Game_IsClient then
+    begin
+      FNetForceWeap := nw;
+      //FNetForceWeapFIdx := FCurrFrameIdx+5; // force for ~5 frames
+      FNetForceWeapFIdx := gTime+5; // force for ~5 frames
+      writeln('NETWEAPONSWITCH: fw=', NetForceWeap, '; fwfidx=', NetForceWeapFIdx, '; cfrm=', gTime);
+    end;
+    }
+    FNetForceWeapFIdx := gTime+5; // force for ~5 frames
     FCurrWeap := nw;
+    if nw = WEAPON_SAW then FSawSoundSelect.PlayAt(FObj.X, FObj.Y);
+    FModel.SetWeapon(nw);
     FTime[T_SWITCH] := gTime+156;
-    if FCurrWeap = WEAPON_SAW then FSawSoundSelect.PlayAt(FObj.X, FObj.Y);
-    FModel.SetWeapon(FCurrWeap);
     if g_Game_IsNet then MH_SEND_PlayerStats(FUID);
+    //if g_Game_IsNet and g_Game_IsClient then
   end;
 end;
 
 procedure TPlayer.NextWeapon();
 begin
-  if g_Game_IsClient then Exit;
+  //if g_Game_IsClient then Exit;
   FNextWeap := $8000;
 end;
 
 procedure TPlayer.PrevWeapon();
 begin
-  if g_Game_IsClient then Exit;
+  //if g_Game_IsClient then Exit;
   FNextWeap := $4000;
 end;
 
+// used by network layer
 procedure TPlayer.SetWeapon(W: Byte);
 begin
   if FCurrWeap <> W then
-    if W = WEAPON_SAW then
+    if (W = WEAPON_SAW) then
       FSawSoundSelect.PlayAt(FObj.X, FObj.Y);
 
   FCurrWeap := W;
   FModel.SetWeapon(CurrWeap);
-  resetWeaponQueue();
+  //if g_Game_IsClient then resetWeaponQueue();
 end;
 
 function TPlayer.PickItem(ItemType: Byte; arespawn: Boolean; var remove: Boolean): Boolean;
@@ -4161,6 +4246,7 @@ begin
           if allowBerserkSwitching then
           begin
             FCurrWeap := WEAPON_KASTET;
+            //FNetForceWeap := FCurrWeap;
             resetWeaponQueue();
             FModel.SetWeapon(WEAPON_KASTET);
           end;
@@ -4173,6 +4259,7 @@ begin
           Result := True;
           remove := True;
           FFireTime := 0;
+          //k8:do we need it? if g_Game_IsNet and g_Game_IsServer then MH_SEND_PlayerStats(FUID);
         end;
         if FHealth < PLAYER_HP_SOFT then
         begin
@@ -4273,6 +4360,9 @@ begin
   FMonsterKills := 0;
   FDeath := 0;
   FSecrets := 0;
+  //FCurrFrameIdx := 0;
+  //FNetForceWeap := FCurrWeap;
+  //FNetForceWeapFIdx := 0;
   if FNoRespawn then
   begin
     FSpectator := False;
@@ -4464,6 +4554,7 @@ begin
   FShellTimer := -1;
   FPain := 0;
   FLastHit := 0;
+  //FNetForceWeap := FCurrWeap;
 
   if not g_Game_IsServer then
     Exit;
@@ -4527,6 +4618,7 @@ begin
     FWeapon[WEAPON_PISTOL] := True;
     FWeapon[WEAPON_KASTET] := True;
     FCurrWeap := WEAPON_PISTOL;
+    //FNetForceWeap := FCurrWeap;
     resetWeaponQueue();
 
     FModel.SetWeapon(FCurrWeap);
@@ -4606,6 +4698,8 @@ begin
   if (gPlayer2 = nil) and (gLMSPID2 = FUID) then
     gPlayer2 := self;
 
+  //FNetForceWeap := FCurrWeap;
+
   if g_Game_IsNet then
   begin
     MH_SEND_PlayerPos(True, FUID, NET_EVERYONE);
@@ -4898,6 +4992,8 @@ var
   AnyServer: Boolean;
   SetSpect: Boolean;
 begin
+  //Inc(FCurrFrameIdx);
+
   NetServer := g_Game_IsNet and g_Game_IsServer;
   AnyServer := g_Game_IsServer;
 
@@ -4908,6 +5004,7 @@ begin
       DoLerp(4);
 
   if NetServer then
+  begin
     if FClientID >= 0 then
     begin
       FPing := NetClients[FClientID].Peer^.lastRoundTripTime;
@@ -4920,6 +5017,7 @@ begin
       FPing := 0;
       FLoss := 0;
     end;
+  end;
 
   if FAlive and (FPunchAnim <> nil) then
     FPunchAnim.Update();
@@ -4950,8 +5048,8 @@ begin
   end;
 
   // no need to do that each second frame, weapon queue will take care of it
-  if FAlive and FKeys[KEY_NEXTWEAPON].Pressed and AnyServer then NextWeapon();
-  if FAlive and FKeys[KEY_PREVWEAPON].Pressed and AnyServer then PrevWeapon();
+  if FAlive and FKeys[KEY_NEXTWEAPON].Pressed {and AnyServer} then NextWeapon();
+  if FAlive and FKeys[KEY_PREVWEAPON].Pressed {and AnyServer} then PrevWeapon();
 
   if gTime mod (GAME_TICK*2) <> 0 then
   begin
@@ -5771,6 +5869,18 @@ begin
   end;
 end;
 
+procedure TPlayer.ReleaseKeysNoWeapon();
+var
+  a: Integer;
+begin
+  for a := Low(FKeys) to High(FKeys) do
+  begin
+    if (a = KEY_PREVWEAPON) or (a = KEY_NEXTWEAPON) then continue;
+    FKeys[a].Pressed := False;
+    FKeys[a].Time := 0;
+  end;
+end;
+
 procedure TPlayer.OnDamage(Angle: SmallInt);
 begin
 end;
@@ -5814,6 +5924,7 @@ begin
   FAir := FSavedState.Air;
   FJetFuel := FSavedState.JetFuel;
   FCurrWeap := FSavedState.CurrWeap;
+  //FNetForceWeap := FCurrWeap;
   FNextWeap := FSavedState.NextWeap;
   FNextWeapDelay := FSavedState.NextWeapDelay;
 
@@ -5984,6 +6095,7 @@ begin
   FSecrets := utils.readLongInt(st);
   // Òåêóùåå îðóæèå
   FCurrWeap := utils.readByte(st);
+  //FNetForceWeap := FCurrWeap;
   // Æåëàåìîå îðóæèå
   FNextWeap := utils.readWord(st);
   // ...è ïàóçà
@@ -6119,6 +6231,7 @@ begin
           if FBFGFireCounter < 1 then
           begin
             FCurrWeap := WEAPON_KASTET;
+            //FNetForceWeap := FCurrWeap;
             resetWeaponQueue();
             FModel.SetWeapon(WEAPON_KASTET);
           end;
@@ -6977,7 +7090,7 @@ begin
             end;
         end;
 
-  //HACK! (does it belongs there?)
+  //HACK! (does it belong there?)
   RealizeCurrentWeapon();
 
 // Åñëè åñòü âîçìîæíûå öåëè: