DEADSOFTWARE

Revert "Revert "no more delay between weapon switching: now player should release...
authorKetmar Dark <ketmar@ketmar.no-ip.org>
Mon, 1 Oct 2018 06:32:06 +0000 (09:32 +0300)
committerKetmar Dark <ketmar@ketmar.no-ip.org>
Mon, 1 Oct 2018 08:05:08 +0000 (11:05 +0300)
src/game/g_game.pas
src/game/g_player.pas
src/game/g_window.pas

index 1dcbcd26c457ddccc8378de1d3d437a0dc5f8d96..de729fae42e2b139d809d3e451dccd8c7afee1b2 100644 (file)
@@ -1504,11 +1504,18 @@ begin
   result := false;
 end;
 
   result := false;
 end;
 
+function isOneKeyPressed (key1: Word): Boolean;
+begin
+  if (key1 <> 0) and e_KeyPressed(key1) then begin result := true; exit; end;
+  result := false;
+end;
+
 procedure processPlayerControls (plr: TPlayer; var ctrl: TPlayerControl; var MoveButton: Byte; p2hack: Boolean=false);
 var
   time: Word;
   strafeDir: Byte;
   i: Integer;
 procedure processPlayerControls (plr: TPlayer; var ctrl: TPlayerControl; var MoveButton: Byte; p2hack: Boolean=false);
 var
   time: Word;
   strafeDir: Byte;
   i: Integer;
+  rwk: Boolean;
 begin
   if (plr = nil) then exit;
   if (p2hack) then time := 1000 else time := 1;
 begin
   if (plr = nil) then exit;
   if (p2hack) then time := 1000 else time := 1;
@@ -1552,13 +1559,42 @@ begin
     if isKeyPressed(KeyUp, KeyUp2) then plr.PressKey(KEY_UP, time);
     if isKeyPressed(KeyDown, KeyDown2) then plr.PressKey(KEY_DOWN, time);
     if isKeyPressed(KeyFire, KeyFire2) then plr.PressKey(KEY_FIRE);
     if isKeyPressed(KeyUp, KeyUp2) then plr.PressKey(KEY_UP, time);
     if isKeyPressed(KeyDown, KeyDown2) then plr.PressKey(KEY_DOWN, time);
     if isKeyPressed(KeyFire, KeyFire2) then plr.PressKey(KEY_FIRE);
-    if isKeyPressed(KeyNextWeapon, KeyNextWeapon2) then plr.PressKey(KEY_NEXTWEAPON);
-    if isKeyPressed(KeyPrevWeapon, KeyPrevWeapon2) then plr.PressKey(KEY_PREVWEAPON);
+    if isKeyPressed(KeyNextWeapon, KeyNextWeapon2) then
+    begin
+      rwk := plr.isWeaponSwitchKeyReleased(-1);
+      if rwk then plr.PressKey(KEY_NEXTWEAPON);
+      plr.weaponSwitchKeysStateChange(-1, true);
+    end
+    else
+    begin
+      plr.weaponSwitchKeysStateChange(-1, false);
+    end;
+    if isKeyPressed(KeyPrevWeapon, KeyPrevWeapon2) then
+    begin
+      rwk := plr.isWeaponSwitchKeyReleased(-2);
+      if rwk then plr.PressKey(KEY_PREVWEAPON);
+      plr.weaponSwitchKeysStateChange(-2, true);
+    end
+    else
+    begin
+      plr.weaponSwitchKeysStateChange(-2, false);
+    end;
     if isKeyPressed(KeyOpen, KeyOpen2) then plr.PressKey(KEY_OPEN);
 
     for i := 0 to High(KeyWeapon) do
     if isKeyPressed(KeyOpen, KeyOpen2) then plr.PressKey(KEY_OPEN);
 
     for i := 0 to High(KeyWeapon) do
+    begin
       if isKeyPressed(KeyWeapon[i], KeyWeapon2[i]) then
       if isKeyPressed(KeyWeapon[i], KeyWeapon2[i]) then
-        plr.QueueWeaponSwitch(i); // all choices are passed there, and god will take the best
+      begin
+        rwk := plr.isWeaponSwitchKeyReleased(i);
+        //writeln('rwk:', rwk);
+        plr.weaponSwitchKeysStateChange(i, true);
+        if rwk then plr.QueueWeaponSwitch(i); // all choices are passed there, and god will take the best
+      end
+      else
+      begin
+        plr.weaponSwitchKeysStateChange(i, false);
+      end;
+    end;
   end;
 
   // HACK: add dynlight here
   end;
 
   // HACK: add dynlight here
index 980aec736c8905ea0c91ba5392caa2a70468aad8..fa2dcfce5243696c55b09cc25aee27d9c44229f9 100644 (file)
@@ -163,7 +163,7 @@ type
     FSecrets:   Integer;
     FCurrWeap:  Byte;
     FNextWeap:  WORD;
     FSecrets:   Integer;
     FCurrWeap:  Byte;
     FNextWeap:  WORD;
-    FNextWeapDelay: Byte; // frames
+    FNextWeapDelay: Byte; // frames (unused)
     FBFGFireCounter: SmallInt;
     FLastSpawnerUID: Word;
     FLastHit:   Byte;
     FBFGFireCounter: SmallInt;
     FLastSpawnerUID: Word;
     FLastHit:   Byte;
@@ -200,6 +200,9 @@ type
     FNetTime: LongWord;
     mEDamageType: Integer;
 
     FNetTime: LongWord;
     mEDamageType: Integer;
 
+    // client-side only
+    weaponSwitchKeyReleased: array[0..16] of Boolean; // true: was release
+
 
     function CollideLevel(XInc, YInc: Integer): Boolean;
     function StayOnStep(XInc, YInc: Integer): Boolean;
 
     function CollideLevel(XInc, YInc: Integer): Boolean;
     function StayOnStep(XInc, YInc: Integer): Boolean;
@@ -333,6 +336,10 @@ type
     procedure getMapBox (out x, y, w, h: Integer); inline;
     procedure moveBy (dx, dy: Integer); inline;
 
     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;
+
   public
     property    Vel: TPoint2i read FObj.Vel;
     property    Obj: TObj read FObj;
   public
     property    Vel: TPoint2i read FObj.Vel;
     property    Obj: TObj read FObj;
@@ -2128,8 +2135,38 @@ begin
   FNetTime := 0;
 
   resetWeaponQueue();
   FNetTime := 0;
 
   resetWeaponQueue();
+  releaseAllWeaponSwitchKeys();
+end;
+
+
+procedure TPlayer.releaseAllWeaponSwitchKeys ();
+var
+  f: Integer;
+begin
+  for f := 0 to High(weaponSwitchKeyReleased) do weaponSwitchKeyReleased[f] := true;
+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] := not pressed;
 end;
 
 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];
+  end;
+end;
+
+
 procedure TPlayer.positionChanged (); inline;
 begin
 end;
 procedure TPlayer.positionChanged (); inline;
 begin
 end;
@@ -3659,7 +3696,7 @@ begin
       begin
         //e_WriteLog(Format(' SWITCH: cur=%d; new=%d (dir=%d; log=%d)', [FCurrWeap, rwidx, dir, cwi]), TMsgType.Warning);
         result := Byte(rwidx);
       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;
         exit;
       end;
     end;
@@ -3726,7 +3763,7 @@ begin
       // i found her!
       result := Byte(rwidx);
       resetWeaponQueue();
       // 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;
       exit;
     end;
   end;
@@ -3756,7 +3793,9 @@ var
 begin
   //e_WriteLog(Format('***RealizeCurrentWeapon: FNextWeap=%x; FNextWeapDelay=%d', [FNextWeap, FNextWeapDelay]), MSG_WARNING);
   //FNextWeap := FNextWeap and $1FFF;
 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
 
   if not switchAllowed then
   begin
index 3958d24d12df79b518b6dc351942a53d487acc52..8336ea9b82cb39b11cd0b5b95c8e992e8cc3b0dd 100644 (file)
@@ -314,6 +314,8 @@ begin
     SDL_WINDOWEVENT_MINIMIZED:
     begin
       e_UnpressAllKeys();
     SDL_WINDOWEVENT_MINIMIZED:
     begin
       e_UnpressAllKeys();
+      if (gPlayer1 <> nil) then gPlayer1.releaseAllWeaponSwitchKeys();
+      if (gPlayer2 <> nil) then gPlayer2.releaseAllWeaponSwitchKeys();
       if not wMinimized then
       begin
         e_ResizeWindow(0, 0);
       if not wMinimized then
       begin
         e_ResizeWindow(0, 0);
@@ -402,6 +404,8 @@ begin
     begin
       wDeactivate := true;
       e_UnpressAllKeys();
     begin
       wDeactivate := true;
       e_UnpressAllKeys();
+      if (gPlayer1 <> nil) then gPlayer1.releaseAllWeaponSwitchKeys();
+      if (gPlayer2 <> nil) then gPlayer2.releaseAllWeaponSwitchKeys();
       //e_WriteLog('window lost focus!', MSG_NOTIFY);
     end;
   end;
       //e_WriteLog('window lost focus!', MSG_NOTIFY);
     end;
   end;