Skip to content

Commit

Permalink
Optimization Handling (#3482)
Browse files Browse the repository at this point in the history
Refactor vehicle handling
  • Loading branch information
G-Moris authored Jul 15, 2024
1 parent d6ae4e9 commit e3a8bd9
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 147 deletions.
140 changes: 67 additions & 73 deletions Client/game_sa/CHandlingManagerSA.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* PROJECT: Multi Theft Auto
* LICENSE: See LICENSE in the top level directory
* FILE: game_sa/CHandlingManagerSA.cpp
* FILE: Client/game_sa/CHandlingManagerSA.cpp
* PURPOSE: Vehicle handling manager
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
* Multi Theft Auto is available from https://multitheftauto.com/
*
*****************************************************************************/

#include "StdInc.h"
#include <core/CCoreInterface.h>
#include <multiplayer/CMultiplayer.h>
#include "CHandlingManagerSA.h"
#include "CGameSA.h"

extern CCoreInterface* g_pCore;
extern CGameSA* pGame;

#define ARRAY_HANDLINGDATA 0xC2B9DC

Expand Down Expand Up @@ -117,6 +119,12 @@ __declspec(naked) void Hook_Calculate()
}
}

static bool IsVehicleModel(eVehicleTypes eModel)
{
const auto pModelInfo = pGame->GetModelInfo(eModel);
return pModelInfo && pModelInfo->IsVehicle();
}

CHandlingManagerSA::CHandlingManagerSA()
{
// Initialize all default handlings
Expand Down Expand Up @@ -206,104 +214,88 @@ CHandlingManagerSA::~CHandlingManagerSA()
}
}

eHandlingProperty CHandlingManagerSA::GetPropertyEnumFromName(std::string strName)
eHandlingProperty CHandlingManagerSA::GetPropertyEnumFromName(const std::string& strName)
{
std::map<std::string, eHandlingProperty>::iterator it;
it = m_HandlingNames.find(strName);

if (it != m_HandlingNames.end())
{
return it->second;
}
return HANDLING_MAX;
const auto it = m_HandlingNames.find(strName);
return it != m_HandlingNames.end() ? it->second : HANDLING_MAX;
}

CHandlingEntry* CHandlingManagerSA::CreateHandlingData()
{
CHandlingEntrySA* pHandlingEntry = new CHandlingEntrySA();
return pHandlingEntry;
return new CHandlingEntrySA;
}

CFlyingHandlingEntry* CHandlingManagerSA::CreateFlyingHandlingData()
{
CFlyingHandlingEntrySA* pFlyingHandlingEntry = new CFlyingHandlingEntrySA();
return pFlyingHandlingEntry;
return new CFlyingHandlingEntrySA;
}

CBoatHandlingEntry* CHandlingManagerSA::CreateBoatHandlingData()
{
CBoatHandlingEntrySA* pBoatHandlingEntry = new CBoatHandlingEntrySA();
return pBoatHandlingEntry;
return new CBoatHandlingEntrySA;
}

CBikeHandlingEntry* CHandlingManagerSA::CreateBikeHandlingData()
{
CBikeHandlingEntrySA* pBikeHandlingEntry = new CBikeHandlingEntrySA();
return pBikeHandlingEntry;
return new CBikeHandlingEntrySA;
}

const CHandlingEntry* CHandlingManagerSA::GetOriginalHandlingData(eVehicleTypes eModel)
{
// Within range?
if (eModel >= 400 && eModel < VT_MAX)
{
// Get our Handling ID
eHandlingTypes eHandling = GetHandlingID(eModel);
// Return it
return m_pOriginalEntries[eHandling];
}

return NULL;
// Vehicle?
if (!IsVehicleModel(eModel))
return nullptr;

// Get our Handling ID, the default value will be HT_LANDSTAL
eHandlingTypes eHandling = GetHandlingID(eModel);
// Return it
return m_pOriginalEntries[eHandling];
}

const CFlyingHandlingEntry* CHandlingManagerSA::GetOriginalFlyingHandlingData(eVehicleTypes eModel)
{
// Within range?
if (eModel >= 400 && eModel < VT_MAX)
{
// Get our Handling ID
eHandlingTypes eHandling = GetHandlingID(eModel);
// Original GTA:SA behavior
if (eHandling < 186 || eHandling > 209)
return m_pOriginalFlyingEntries[0];
else
return m_pOriginalFlyingEntries[eHandling - 186];
}

return NULL;
// Vehicle?
if (!IsVehicleModel(eModel))
return nullptr;

// Get our Handling ID, the default value will be HT_LANDSTAL
eHandlingTypes eHandling = GetHandlingID(eModel);
// Original GTA:SA behavior
if (eHandling < HT_SEAPLANE || eHandling > HT_RCRAIDER)
return m_pOriginalFlyingEntries[0];
else
return m_pOriginalFlyingEntries[eHandling - HT_SEAPLANE];
}

const CBoatHandlingEntry* CHandlingManagerSA::GetOriginalBoatHandlingData(eVehicleTypes eModel)
{
// Within range?
if (eModel >= 400 && eModel < VT_MAX)
{
// Get our Handling ID
eHandlingTypes eHandling = GetHandlingID(eModel);
// Original GTA:SA behavior
if (eHandling < 175 || eHandling > 186)
return m_pOriginalBoatEntries[0];
else
return m_pOriginalBoatEntries[eHandling - 175];
}

return NULL;
// Vehicle?
if (!IsVehicleModel(eModel))
return nullptr;

// Get our Handling ID, the default value will be HT_LANDSTAL
eHandlingTypes eHandling = GetHandlingID(eModel);
// Original GTA:SA behavior
if (eHandling < HT_PREDATOR || eHandling > HT_SEAPLANE)
return m_pOriginalBoatEntries[0];
else
return m_pOriginalBoatEntries[eHandling - HT_PREDATOR];
}

const CBikeHandlingEntry* CHandlingManagerSA::GetOriginalBikeHandlingData(eVehicleTypes eModel)
{
// Within range?
if (eModel >= 400 && eModel < VT_MAX)
{
// Get our Handling ID
eHandlingTypes eHandling = GetHandlingID(eModel);
if (eHandling >= HT_BIKE && eHandling <= HT_FREEWAY)
return m_pOriginalBikeEntries[eHandling - HT_BIKE];
else if (eHandling == HT_FAGGIO)
return m_pOriginalBikeEntries[13];
}

return NULL;
// Vehicle?
if (!IsVehicleModel(eModel))
return nullptr;

// Get our Handling ID, the default value will be HT_LANDSTAL
eHandlingTypes eHandling = GetHandlingID(eModel);
if (eHandling >= HT_BIKE && eHandling <= HT_FREEWAY)
return m_pOriginalBikeEntries[eHandling - HT_BIKE];
else if (eHandling == HT_FAGGIO)
return m_pOriginalBikeEntries[13];
else
return nullptr;
}

// Return the handling manager id
Expand Down Expand Up @@ -9167,7 +9159,7 @@ void CHandlingManagerSA::CheckSuspensionChanges(CHandlingEntry* pEntry)
CMultiplayer* pMultiplayer = g_pCore->GetMultiplayer();
eVehicleTypes eModel = pEntry->GetModel();
// Find our original data
const CHandlingEntry* pOriginal = m_pOriginalEntries[eModel];
CHandlingEntrySA* pOriginal = m_pOriginalEntries[eModel];
// Default bChanged to false
bool bChanged = false;

Expand Down Expand Up @@ -9195,10 +9187,10 @@ void CHandlingManagerSA::CheckSuspensionChanges(CHandlingEntry* pEntry)
bChanged = true;

// Is bChanged true and the suspension flag changed marker false
if (bChanged == true && pEntry->HasSuspensionChanged() == false)
if (bChanged && !pEntry->HasSuspensionChanged())
{
// Is our hook uninstalled?
if (pMultiplayer->IsSuspensionEnabled() == false)
if (!pMultiplayer->IsSuspensionEnabled())
// Install the hook
pMultiplayer->SetSuspensionEnabled(true);

Expand All @@ -9208,17 +9200,19 @@ void CHandlingManagerSA::CheckSuspensionChanges(CHandlingEntry* pEntry)
pEntry->SetSuspensionChanged(true);
}
// is bChanged false and is this model supposed to contain non-default info? (i.e. they just reverted)
else if (bChanged == false && pEntry->HasSuspensionChanged() == true)
else if (!bChanged && pEntry->HasSuspensionChanged())
{
// Decrement iChangedVehicles
iChangedVehicles--;
// Set the suspension Changed flag to false
pEntry->SetSuspensionChanged(false);
}

// if we hit 0 vehicles installed and it's installed uninstall the hook
if (iChangedVehicles == 0 && pMultiplayer->IsSuspensionEnabled() == true)
if (iChangedVehicles == 0 && pMultiplayer->IsSuspensionEnabled())
pMultiplayer->SetSuspensionEnabled(false);
}

void CHandlingManagerSA::RemoveChangedVehicle()
{
// Decrement the count
Expand Down
8 changes: 4 additions & 4 deletions Client/game_sa/CHandlingManagerSA.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* PROJECT: Multi Theft Auto
* LICENSE: See LICENSE in the top level directory
* FILE: game_sa/CHandlingManagerSA.h
* FILE: Client/game_sa/CHandlingManagerSA.h
* PURPOSE: Header file for vehicle handling manager class
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
* Multi Theft Auto is available from https://multitheftauto.com/
*
*****************************************************************************/

Expand Down Expand Up @@ -35,7 +35,7 @@ class CHandlingManagerSA : public CHandlingManager

eHandlingTypes GetHandlingID(eVehicleTypes eModel);

eHandlingProperty GetPropertyEnumFromName(std::string strName);
eHandlingProperty GetPropertyEnumFromName(const std::string& strName);

void CheckSuspensionChanges(CHandlingEntry* pEntry);
void RemoveChangedVehicle();
Expand Down
8 changes: 4 additions & 4 deletions Client/sdk/game/CHandlingManager.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* PROJECT: Multi Theft Auto
* LICENSE: See LICENSE in the top level directory
* FILE: sdk/game/CHandlingManager.h
* FILE: Client/sdk/game/CHandlingManager.h
* PURPOSE: Vehicle handling manager interface
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
* Multi Theft Auto is available from https://multitheftauto.com/
*
*****************************************************************************/

Expand All @@ -32,7 +32,7 @@ class CHandlingManager
virtual const CBoatHandlingEntry* GetOriginalBoatHandlingData(enum eVehicleTypes eModel) = 0;
virtual const CBikeHandlingEntry* GetOriginalBikeHandlingData(enum eVehicleTypes eModel) = 0;

virtual eHandlingProperty GetPropertyEnumFromName(std::string strName) = 0;
virtual eHandlingProperty GetPropertyEnumFromName(const std::string& strName) = 0;
virtual void RemoveChangedVehicle() = 0;
virtual void CheckSuspensionChanges(CHandlingEntry* pEntry) = 0;
};
6 changes: 3 additions & 3 deletions Client/sdk/game/Common.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* PROJECT: Multi Theft Auto
* LICENSE: See LICENSE in the top level directory
* FILE: sdk/game/Common.h
* FILE: Client/sdk/game/Common.h
* PURPOSE: Grand Theft Auto: San Andreas game definitions
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
* Multi Theft Auto is available from https://multitheftauto.com/
*
*****************************************************************************/

Expand Down
Loading

0 comments on commit e3a8bd9

Please sign in to comment.