Skip to content

Commit 76c2a87

Browse files
committed
Fix setVehicleDirtLevel
1 parent 2c9f48c commit 76c2a87

File tree

4 files changed

+140
-4
lines changed

4 files changed

+140
-4
lines changed

Client/game_sa/CModelInfoSA.h

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <game/Common.h>
1515
#include <game/CModelInfo.h>
1616
#include "CRenderWareSA.h"
17+
#include "game/RenderWare.h"
1718

1819
class CPedModelInfoSA;
1920
class CPedModelInfoSAInterface;
@@ -259,11 +260,22 @@ class CTimeModelInfoSAInterface : public CBaseModelInfoSAInterface
259260
CTimeInfoSAInterface timeInfo;
260261
};
261262

263+
class CVehicleModelUpgradePosnDesc
264+
{
265+
CVector m_vPosition;
266+
RtQuat m_vRotation;
267+
int m_iParentId;
268+
};
269+
262270
class CVehicleModelVisualInfoSAInterface // Not sure about this name. If somebody knows more, please change
263271
{
264272
public:
265-
CVector vecDummies[15];
266-
char m_sUpgrade[18];
273+
CVector vecDummies[15];
274+
CVehicleModelUpgradePosnDesc m_sUpgrade[18];
275+
RpAtomic* m_pExtra[6];
276+
std::uint8_t m_numExtras;
277+
std::uint8_t _pad[3];
278+
int m_maskComponentDamagable;
267279
};
268280

269281
class CVehicleModelInfoSAInterface : public CBaseModelInfoSAInterface
@@ -290,7 +302,19 @@ class CVehicleModelInfoSAInterface : public CBaseModelInfoSAInterface
290302
float fSteeringAngle;
291303
CVehicleModelVisualInfoSAInterface* pVisualInfo; // +92
292304
char pad3[464];
293-
char pDirtMaterial[64]; // *RwMaterial
305+
//RpMaterial* pDirtMaterial[32];
306+
307+
union
308+
{
309+
struct
310+
{
311+
RpMaterial** m_dirtMaterials;
312+
size_t m_numDirtMaterials;
313+
RpMaterial* m_staticDirtMaterials[30];
314+
};
315+
RpMaterial* pDirtMaterial[32];
316+
};
317+
294318
char pad4[64];
295319
char primColors[8];
296320
char secondColors[8];

Client/game_sa/gamesa_renderware.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ typedef RpHAnimHierarchy*(__cdecl* GetAnimHierarchyFromSkinClump_t)(RpClump*);
105105
typedef int(__cdecl* RpHAnimIDGetIndex_t)(RpHAnimHierarchy*, int);
106106
typedef RwMatrix*(__cdecl* RpHAnimHierarchyGetMatrixArray_t)(RpHAnimHierarchy*);
107107
typedef RtQuat*(__cdecl* RtQuatRotate_t)(RtQuat* quat, const RwV3d* axis, float angle, RwOpCombineType combineOp);
108+
typedef RpGeometry*(__cdecl* RpGeometryForAllMaterials_t)(RpGeometry* geom, void* callback, void* data);
108109

109110
/*****************************************************************************/
110111
/** Renderware function mappings **/
@@ -195,6 +196,7 @@ RWFUNC(GetAnimHierarchyFromSkinClump_t GetAnimHierarchyFromSkinClump, (GetAnimHi
195196
RWFUNC(RpHAnimIDGetIndex_t RpHAnimIDGetIndex, (RpHAnimIDGetIndex_t)0xDEAD)
196197
RWFUNC(RpHAnimHierarchyGetMatrixArray_t RpHAnimHierarchyGetMatrixArray, (RpHAnimHierarchyGetMatrixArray_t)0xDEAD)
197198
RWFUNC(RtQuatRotate_t RtQuatRotate, (RtQuatRotate_t)0xDEAD)
199+
RWFUNC(RpGeometryForAllMaterials_t RpGeometryForAllMaterials, (RpGeometryForAllMaterials_t)0xDEAD)
198200

199201
/*****************************************************************************/
200202
/** GTA function definitions and mappings **/

Client/game_sa/gamesa_renderware.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ void InitRwFunctions()
8989
RpHAnimIDGetIndex = (RpHAnimIDGetIndex_t)0x7C51A0;
9090
RpHAnimHierarchyGetMatrixArray = (RpHAnimHierarchyGetMatrixArray_t)0x7C5120;
9191
RtQuatRotate = (RtQuatRotate_t)0x7EB7C0;
92-
92+
RpGeometryForAllMaterials = (RpGeometryForAllMaterials_t)0x74C790;
93+
9394
SetTextureDict = (SetTextureDict_t)0x007319C0;
9495
LoadClumpFile = (LoadClumpFile_t)0x005371F0;
9596
LoadModel = (LoadModel_t)0x0040C6B0;

Client/multiplayer_sa/CMultiplayerSA_Vehicles.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
*****************************************************************************/
1010

1111
#include "StdInc.h"
12+
#include "..\game_sa\gamesa_renderware.h"
13+
14+
#define FUNC_CVehicleModelInfo_ResetColors 0x4C8490
15+
static RwTexture** ms_aDirtTextures = (RwTexture**)0xC02BD0;
1216

1317
static bool __fastcall AreVehicleDoorsUndamageable(CVehicleSAInterface* vehicle)
1418
{
@@ -107,6 +111,106 @@ static void _declspec(naked) HOOK_CAEVehicleAudioEntity__Initialise()
107111
}
108112
}
109113

114+
static void CVehicleModelInfo_Shutdown()
115+
{
116+
CVehicleModelInfoSAInterface* mi = nullptr;
117+
_asm
118+
{
119+
mov mi, ecx
120+
}
121+
122+
if (!mi)
123+
return;
124+
125+
// Call CBaseModelInfo::Shutdown
126+
((void(__cdecl*)(void*))0x4C4D50)(mi);
127+
128+
delete[] mi->m_dirtMaterials;
129+
mi->m_dirtMaterials = nullptr;
130+
}
131+
132+
static void SetDirtTextures(CVehicleModelInfoSAInterface* mi, std::uint32_t level)
133+
{
134+
RpMaterial** materials = mi->m_numDirtMaterials > 30 ? mi->m_dirtMaterials : mi->m_staticDirtMaterials;
135+
for (std::uint32_t i = 0; i < mi->m_numDirtMaterials; i++)
136+
RpMaterialSetTexture(materials[i], ms_aDirtTextures[level]);
137+
}
138+
139+
#define HOOKPOS_CVehicleModelInfo_SetDirtTextures 0x5D5DBB
140+
#define HOOKSIZE_CVehicleModelInfo_SetDirtTextures 6
141+
static constexpr DWORD CONTINUE_CVehicleModelInfo_SetDirtTextures = 0x5D5DE3;
142+
static void _declspec(naked) HOOK_CVehicleModelInfo_SetDirtTextures()
143+
{
144+
_asm
145+
{
146+
pushad
147+
push ebx
148+
push esi
149+
call SetDirtTextures
150+
add esp, 8
151+
popad
152+
153+
jmp CONTINUE_CVehicleModelInfo_SetDirtTextures
154+
}
155+
}
156+
157+
static RpMaterial* GetAtomicGeometryMaterialsCB(RpMaterial* material, void* data)
158+
{
159+
RwTexture* texture = material->texture;
160+
if (!texture)
161+
return nullptr;
162+
163+
auto cbData = static_cast<std::vector<RpMaterial*>*>(data);
164+
if (texture->name && std::strcmp(texture->name, "vehiclegrunge256") == 0)
165+
cbData->push_back(material);
166+
167+
return material;
168+
}
169+
170+
static bool GetEditableMaterialListCB(RpAtomic* atomic, void* data)
171+
{
172+
RpGeometryForAllMaterials(atomic->geometry, &GetAtomicGeometryMaterialsCB, data);
173+
return true;
174+
}
175+
176+
static void __fastcall FindEditableMaterialList(CVehicleModelInfoSAInterface* mi)
177+
{
178+
std::vector<RpMaterial*> list;
179+
RpClumpForAllAtomics(reinterpret_cast<RpClump*>(mi->pRwObject), &GetEditableMaterialListCB, &list);
180+
181+
for (std::uint32_t i = 0; i < mi->pVisualInfo->m_numExtras; i++)
182+
GetEditableMaterialListCB(mi->pVisualInfo->m_pExtra[i], &list);
183+
184+
mi->m_numDirtMaterials = list.size();
185+
if (mi->m_numDirtMaterials > 30)
186+
{
187+
mi->m_dirtMaterials = new RpMaterial*[mi->m_numDirtMaterials];
188+
std::copy(list.begin(), list.end(), mi->m_dirtMaterials);
189+
}
190+
else
191+
{
192+
mi->m_dirtMaterials = nullptr;
193+
std::copy(list.begin(), list.end(), mi->m_staticDirtMaterials);
194+
}
195+
196+
((void(__thiscall*)(CVehicleModelInfoSAInterface*))FUNC_CVehicleModelInfo_ResetColors)(mi);
197+
}
198+
199+
#define HOOKPOS_CVehicleModelInfo_SetClump 0x4C9648
200+
#define HOOKSIZE_CVehicleModelInfo_SetClump 24
201+
static constexpr DWORD CONTINUE_CVehicleModelInfo_SetClump = 0x4C9660;
202+
static void _declspec(naked) HOOK_CVehicleModelInfo_SetClump()
203+
{
204+
_asm
205+
{
206+
pushad
207+
call FindEditableMaterialList
208+
popad
209+
210+
jmp CONTINUE_CVehicleModelInfo_SetClump
211+
}
212+
}
213+
110214
//////////////////////////////////////////////////////////////////////////////////////////
111215
//
112216
// CMultiplayerSA::InitHooks_Vehicles
@@ -118,4 +222,9 @@ void CMultiplayerSA::InitHooks_Vehicles()
118222
{
119223
EZHookInstall(CDamageManager__ProgressDoorDamage);
120224
EZHookInstall(CAEVehicleAudioEntity__Initialise);
225+
226+
// Fix vehicle dirt level
227+
EZHookInstall(CVehicleModelInfo_SetClump);
228+
EZHookInstall(CVehicleModelInfo_SetDirtTextures);
229+
MemCpy((void*)0x85C5E4, &CVehicleModelInfo_Shutdown, 4);
121230
}

0 commit comments

Comments
 (0)