Skip to content

Commit f676181

Browse files
Tweak wheel scaling behavior to be more consistent (#1650)
* Tweak wheel scaling behavior to be more consistent * Handle m_pUpgrades being nullptr in CClientVehicle::ResetWheelScale m_pUpgrades shouldn't be null unless its allocation during the constructor fails without throwing an exception, but other functions in the same file check for null before using m_pUpgrades, so add a check here too to be safe. * Revert "Handle m_pUpgrades being nullptr in CClientVehicle::ResetWheelScale" This reverts commit b0880ad. Checking for nullptr here actually is probably overly defensive, as the new operator that allocates this field may throw an exception. I think it is better to be a bit offensive here, because if m_pUpgrades becomes null something is going very wrong. * Assert that m_pUpgrades is not null in CClientVehicle::ResetWheelScale As explained in previous commits, if the software behaves as specified, checking for m_pUpgrades is not necessary. Instead, check that invariant is being held via an assertion, which only impacts debug builds. This may be useful to catch errors if the `CClientVehicle` class is extended in such a way that a subclass sets m_pUpgrades to nullptr. * Address suggested change by @qaisjp * Restore m_fWheelScale in CClientVehicle::Create if needed Restoring m_fWheelScale to fPreviousWheelScale is needed to handle succesive restreams properly.
1 parent eb2ca52 commit f676181

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

Client/mods/deathmatch/logic/CClientVehicle.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ CClientVehicle::CClientVehicle(CClientManager* pManager, ElementID ID, unsigned
171171

172172
// We've not yet been streamed in
173173
m_bJustStreamedIn = false;
174+
175+
// We've not changed the wheel scale
176+
m_bWheelScaleChanged = false;
174177
}
175178

176179
CClientVehicle::~CClientVehicle()
@@ -2682,7 +2685,6 @@ void CClientVehicle::Create()
26822685
m_pVehicle->SetGravity(&m_vecGravity);
26832686
m_pVehicle->SetHeadLightColor(m_HeadLightColor);
26842687
m_pVehicle->SetRadioStatus(0);
2685-
m_pVehicle->SetWheelScale(m_fWheelScale);
26862688

26872689
if (IsNitroInstalled())
26882690
{
@@ -2825,10 +2827,24 @@ void CClientVehicle::Create()
28252827
if (m_bHasCustomHandling)
28262828
ApplyHandling();
28272829
}
2830+
2831+
// Applying wheel upgrades can change these values.
2832+
// We should keep track of the original values to restore them
2833+
bool bPreviousWheelScaleChanged = m_bWheelScaleChanged;
2834+
float fPreviousWheelScale = m_fWheelScale;
2835+
28282836
// Re-add all the upgrades - Has to be applied after handling *shrugs*
28292837
if (m_pUpgrades)
28302838
m_pUpgrades->ReAddAll();
28312839

2840+
// Restore custom wheel scale
2841+
if (bPreviousWheelScaleChanged)
2842+
{
2843+
m_pVehicle->SetWheelScale(fPreviousWheelScale);
2844+
m_fWheelScale = fPreviousWheelScale;
2845+
m_bWheelScaleChanged = true;
2846+
}
2847+
28322848
if (m_ComponentData.empty())
28332849
{
28342850
// grab our map of components
@@ -4992,6 +5008,8 @@ void CClientVehicle::SetWheelScale(float fWheelScale)
49925008
m_pVehicle->SetWheelScale(fWheelScale);
49935009
}
49945010
m_fWheelScale = fWheelScale;
5011+
5012+
m_bWheelScaleChanged = true;
49955013
}
49965014

49975015
float CClientVehicle::GetWheelScale()
@@ -5002,3 +5020,19 @@ float CClientVehicle::GetWheelScale()
50025020
}
50035021
return m_fWheelScale;
50045022
}
5023+
5024+
// This function is meant to be called after GTA resets wheel scale
5025+
// (i.e. after installing a wheel upgrade)
5026+
void CClientVehicle::ResetWheelScale()
5027+
{
5028+
assert(m_pUpgrades);
5029+
5030+
// The calculation of the default wheel scale is based on original GTA code at functions
5031+
// 0x6E3290 (CVehicle::AddVehicleUpgrade) and 0x6DF930 (CVehicle::RemoveVehicleUpgrade)
5032+
if (m_pUpgrades->GetSlotState(12) != 0)
5033+
m_fWheelScale = m_pModelInfo->GetVehicleWheelSize(eResizableVehicleWheelGroup::FRONT_AXLE);
5034+
else
5035+
m_fWheelScale = 1.0f;
5036+
5037+
m_bWheelScaleChanged = false;
5038+
}

Client/mods/deathmatch/logic/CClientVehicle.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ class CClientVehicle : public CClientStreamElement
496496

497497
float GetWheelScale();
498498
void SetWheelScale(float fWheelScale);
499+
void ResetWheelScale();
499500

500501
bool OnVehicleFallThroughMap();
501502

@@ -621,6 +622,7 @@ class CClientVehicle : public CClientStreamElement
621622
float m_fTrainPosition;
622623
uchar m_ucTrackID;
623624
bool m_bJustStreamedIn;
625+
bool m_bWheelScaleChanged;
624626

625627
// Time dependent error compensation interpolation
626628
struct

Client/mods/deathmatch/logic/CVehicleUpgrades.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,10 @@ void CVehicleUpgrades::ForceAddUpgrade(unsigned short usUpgrade)
611611

612612
// Add it to the slot
613613
m_SlotStates[ucSlot] = usUpgrade;
614+
615+
// Reset wheel scale if it is a wheel upgrade
616+
if (ucSlot == 12)
617+
m_pVehicle->ResetWheelScale();
614618
}
615619
}
616620

@@ -656,6 +660,10 @@ bool CVehicleUpgrades::RemoveUpgrade(unsigned short usUpgrade)
656660
m_SlotStates[ucSlot] = 0;
657661
}
658662

663+
// Reset wheel scale if it is a wheel upgrade
664+
if (ucSlot == 12)
665+
m_pVehicle->ResetWheelScale();
666+
659667
return true;
660668
}
661669
}
@@ -710,6 +718,10 @@ void CVehicleUpgrades::RemoveAll(bool bRipFromVehicle)
710718
}
711719
}
712720
m_SlotStates[ucSlot] = 0;
721+
722+
// Reset wheel scale for wheel upgrades
723+
if (ucSlot == 12 && m_pVehicle)
724+
m_pVehicle->ResetWheelScale();
713725
}
714726
}
715727
}

0 commit comments

Comments
 (0)