Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wjrogers/852105 to your computer and use it in GitHub Desktop.
Save wjrogers/852105 to your computer and use it in GitHub Desktop.
Cleans up and simplifies math in the recoil calculations.
Index: Build/Tactical/LOS.cpp
===================================================================
--- Build/Tactical/LOS.cpp (revision 4223)
+++ Build/Tactical/LOS.cpp (working copy)
@@ -7950,11 +7950,11 @@
// Calculate Ideal X and limit to max force
FLOAT iIdealCounterForceX = 0.0;
- iIdealCounterForceX = __min( iCounterForceMax, (bGunRecoilX + (*dMuzzleOffsetX / iDistanceRatio / iRound) ));
+ iIdealCounterForceX = -(bGunRecoilX + (*dMuzzleOffsetX / iDistanceRatio / iRound) );
// Calculate Ideal Y and limit to max force
FLOAT iIdealCounterForceY = 0.0;
- iIdealCounterForceY = __min( iCounterForceMax, (bGunRecoilY + (*dMuzzleOffsetY / iDistanceRatio / iRound) ));
+ iIdealCounterForceY = -(bGunRecoilY + (*dMuzzleOffsetY / iDistanceRatio / iRound) );
// STEP 3: Now we need to determine how accurate the shooter is when applying counter-force. He won't always apply
// as much as necessary, and may sometimes apply too much. The ability to apply exactly (or close to exactly) the
@@ -7973,13 +7973,10 @@
// Randomize a deviation value.
INT32 uiCounterForceDeviationX = PreRandom(uiCounterForceAccuracy);
// Use as a percentage to the ideal counter force.
- FLOAT iCounterForceDeviationX = (FLOAT)((uiCounterForceDeviationX * iIdealCounterForceX) / 100.0);
+ FLOAT iCounterForceDeviationX = abs((uiCounterForceDeviationX * iIdealCounterForceX) / 100.0);
// Make sure there's always a little randomness - no one is THAT accurate unless the gun is producing really minimal
// amounts of recoil (or none).
- //CHRISL: I think this should really be a MIN comparison instead of MAX.
- //iCounterForceDeviationX = __max(iCounterForceDeviationX, iGunTotalRecoil * gGameCTHConstants.RECOIL_COUNTER_ACCURACY_MIN_ERROR);
- //iCounterForceDeviationX = __min(iCounterForceDeviationX, iGunTotalRecoil * gGameCTHConstants.RECOIL_COUNTER_ACCURACY_MIN_ERROR);
- iCounterForceDeviationX = __max(iCounterForceDeviationX, iGunTotalRecoil * gGameCTHConstants.RECOIL_COUNTER_ACCURACY_MIN_ERROR * max(0.1f, (FLOAT)bGunRecoilX/iGunTotalRecoil));
+ iCounterForceDeviationX = __max(iCounterForceDeviationX, abs(iIdealCounterForceX * gGameCTHConstants.RECOIL_COUNTER_ACCURACY_MIN_ERROR));
// Determine whether we're under- or over-compensating.
INT8 bUpDownX = PreRandom(2)?(1):(-1);
@@ -7988,23 +7985,16 @@
// So now we have the ideal amount, and the randomal deviation from that amount. Let's add them up.
iAppliedCounterForceX = iIdealCounterForceX + iCounterForceDeviationX;
- // Limit to the maximum applied force. Our shooter cannot exert more force than his personal maximum.
- iAppliedCounterForceX = __min(iAppliedCounterForceX, iCounterForceMax);
-
//////////////////////////
// CALCULATE FOR Y
// Randomize a deviation value.
INT32 uiCounterForceDeviationY = PreRandom(uiCounterForceAccuracy);
// Use as a percentage to the ideal counter force.
- FLOAT iCounterForceDeviationY = (FLOAT)((uiCounterForceDeviationY * iIdealCounterForceY) / 100.0);
+ FLOAT iCounterForceDeviationY = abs((uiCounterForceDeviationY * iIdealCounterForceY) / 100.0);
// Make sure there's always a little randomness - no one is THAT accurate unless the gun is producing really minimal
// amounts of recoil (or none).
- //CHRISL: Not only should this be a MIN comparison, we shouldn't be using "min 1 or iGunTotalRecoil" and we should be including
- // the RECOIL_COUNTER_ACCURACY_MIN_ERROR external value.
- //iCounterForceDeviationY = __max(iCounterForceDeviationY, iGunTotalRecoil * gGameCTHConstants.RECOIL_COUNTER_ACCURACY_MIN_ERROR);
- //iCounterForceDeviationY = __min(iCounterForceDeviationY, iGunTotalRecoil * gGameCTHConstants.RECOIL_COUNTER_ACCURACY_MIN_ERROR);
- iCounterForceDeviationY = __max(iCounterForceDeviationY, iGunTotalRecoil * gGameCTHConstants.RECOIL_COUNTER_ACCURACY_MIN_ERROR * max(0.1f, (FLOAT)bGunRecoilY/iGunTotalRecoil));
+ iCounterForceDeviationY = __max(iCounterForceDeviationY, abs(iIdealCounterForceY * gGameCTHConstants.RECOIL_COUNTER_ACCURACY_MIN_ERROR));
// Determine whether we're under- or over-compensating.
INT8 bUpDownY = PreRandom(2)?(1):(-1);
@@ -8013,9 +8003,6 @@
// So now we have the ideal amount, and the randomal deviation from that amount. Let's add them up.
iAppliedCounterForceY = iIdealCounterForceY + iCounterForceDeviationY;
- // Limit to the maximum applied force. Our shooter cannot exert more force than his personal maximum.
- iAppliedCounterForceY = __min(iAppliedCounterForceY, iCounterForceMax);
-
/////////////////////////////
// Use pythagorean to scale this to the max force allowed.
// We want to use pythagorean here so that we calculate the total "vector length" of recoil and compare that to CFM
@@ -8031,35 +8018,6 @@
}
// This works because sqrt((iACFX^2)+(iACFY^2)) should result in being less then or equal to CFM
- /////////////////////////////
- // If a merc has less CFM then the weapons total recoil, control is pretty much not possible. However, this code may allow such a merc
- // a small amount of control by letting the merc focus on a single axis. By focusing on the "heavier" axis, the merc doesn't gain control
- // of the weapon, but does allow for the possibility of an extra round or two being on target.
- FLOAT iRecoilThreshold = (iCounterForceMax * uiCounterForceAccuracy / 100) * 2;
- if(bGunRecoilX + bGunRecoilY > iCounterForceMax)
- {
- if(bGunRecoilX > iCounterForceMax){
- FLOAT iDiff = iAppliedCounterForceY * .75f;
- iAppliedCounterForceX += iDiff;
- iAppliedCounterForceY -= iDiff;
- }
- else if(bGunRecoilY > iCounterForceMax){
- FLOAT iDiff = iAppliedCounterForceX * .75f;
- iAppliedCounterForceX -= iDiff;
- iAppliedCounterForceY += iDiff;
- }
- }
-
- // Make sure it's the correct direction compared to where the gun is pulling.
- if (dNextOffsetX > 0)
- {
- iAppliedCounterForceX *= (-1);
- }
- if (dNextOffsetY > 0)
- {
- iAppliedCounterForceY *= (-1);
- }
-
// Record how much counter force was applied this time. It will be used for the next few shots until the
// shooter can recalculate.
pShooter->dPrevCounterForceX = iAppliedCounterForceX;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment