/
CFxSystemSA.cpp
264 lines (231 loc) · 8.76 KB
/
CFxSystemSA.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: game_sa/CFxSystemSA.cpp
* PURPOSE: Game effects handling
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#include "StdInc.h"
// Variables used in the hooks
static CFxSystemSAInterface* ms_pUsingFxSystemSAInterface = NULL;
static float ms_fUsingDrawDistance = 0;
static ushort ms_usFxSystemSavedCullDistance = 0;
static CFxEmitterSAInterface* ms_pUsingFxEmitterSAInterface = NULL;
static ushort ms_usFxEmitterSavedFadeFarDistance = 0;
static ushort ms_usFxEmitterSavedFadeNearDistance = 0;
static float ms_fFxSystemUpdateCullDistMultiplier = FX_SYSTEM_UPDATE_CULL_DIST_MULTIPLIER_DEFAULT;
static float ms_fFxCreateParticleCullDistMultiplier = FX_CREATE_PARTICLE_CULL_DIST_MULTIPLIER_DEFAULT;
CFxSystemSA::CFxSystemSA(CFxSystemSAInterface* pInterface)
{
m_pInterface = pInterface;
m_fDrawDistance = 0;
pGame->GetFxManagerSA()->AddToList(m_pInterface, this);
}
CFxSystemSA::~CFxSystemSA()
{
pGame->GetFxManagerSA()->RemoveFromList(this);
}
void CFxSystemSA::PlayAndKill()
{
DWORD dwThis = (DWORD)m_pInterface;
DWORD dwFunc = FUNC_FxSystem_c__PlayAndKill;
_asm
{
mov ecx, dwThis
call dwFunc
}
}
void CFxSystemSA::GetMatrix(CMatrix& matrix)
{
MemCpyFast(&matrix.vFront, &m_pInterface->matPosition.at, sizeof(CVector));
MemCpyFast(&matrix.vPos, &m_pInterface->matPosition.pos, sizeof(CVector));
MemCpyFast(&matrix.vUp, &m_pInterface->matPosition.up, sizeof(CVector));
MemCpyFast(&matrix.vRight, &m_pInterface->matPosition.right, sizeof(CVector));
}
void CFxSystemSA::SetMatrix(const CMatrix& matrix)
{
MemCpyFast(&m_pInterface->matPosition.at, &matrix.vFront, sizeof(CVector));
MemCpyFast(&m_pInterface->matPosition.pos, &matrix.vPos, sizeof(CVector));
MemCpyFast(&m_pInterface->matPosition.up, &matrix.vUp, sizeof(CVector));
MemCpyFast(&m_pInterface->matPosition.right, &matrix.vRight, sizeof(CVector));
}
void CFxSystemSA::GetPosition(CVector& vecPos)
{
vecPos.fX = m_pInterface->matPosition.pos.x;
vecPos.fY = m_pInterface->matPosition.pos.y;
vecPos.fZ = m_pInterface->matPosition.pos.z;
}
void CFxSystemSA::SetPosition(const CVector& vecPos)
{
m_pInterface->matPosition.pos.x = vecPos.fX;
m_pInterface->matPosition.pos.y = vecPos.fY;
m_pInterface->matPosition.pos.z = vecPos.fZ;
// Set the update flag(s)
// this is what RwMatrixUpdate (@0x7F18E0) does
m_pInterface->matPosition.flags &= 0xFFFDFFFC;
}
float CFxSystemSA::GetEffectDensity()
{
return m_pInterface->sRateMult / 1000.0f;
}
void CFxSystemSA::SetEffectDensity(float fDensity)
{
m_pInterface->sRateMult = (short)(fDensity * 1000.0f);
}
float CFxSystemSA::GetEffectSpeed()
{
return m_pInterface->sTimeMult / 1000.0f;
}
void CFxSystemSA::SetEffectSpeed(float fSpeed)
{
m_pInterface->sTimeMult = (short)(fSpeed * 1000.0f);
}
void CFxSystemSA::SetDrawDistance(float fDrawDistance)
{
m_fDrawDistance = Clamp(0.f, fDrawDistance, 8191.f);
}
float CFxSystemSA::GetDrawDistance(void)
{
if (HasCustomDrawDistance())
return m_fDrawDistance;
else
return m_pInterface->pBlueprint->usCullDistance / 256.f;
}
bool CFxSystemSA::HasCustomDrawDistance(void)
{
return m_fDrawDistance != 0;
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// FxSystem_c::Update_MidA
//
// Poke new draw distance for the FxSystem
//
//////////////////////////////////////////////////////////////////////////////////////////
void OnMY_FxSystem_c_Update_MidA_Pre(CFxSystemSAInterface* pFxSystemSAInterface)
{
CFxSystemSA* pFxSystemSA = pGame->GetFxManagerSA()->GetFxSystem(pFxSystemSAInterface);
if (pFxSystemSA && pFxSystemSA->HasCustomDrawDistance())
{
ms_pUsingFxSystemSAInterface = pFxSystemSAInterface;
ms_usFxSystemSavedCullDistance = pFxSystemSAInterface->pBlueprint->usCullDistance;
ms_fUsingDrawDistance = pFxSystemSA->GetDrawDistance();
pFxSystemSAInterface->pBlueprint->usCullDistance = (ushort)(ms_fUsingDrawDistance * 8);
ms_fFxSystemUpdateCullDistMultiplier = 1 / 8.f;
}
else
ms_fUsingDrawDistance = 0;
}
void OnMY_FxSystem_c_Update_MidA_Post(void)
{
if (ms_pUsingFxSystemSAInterface)
{
// Restore default settings
ms_pUsingFxSystemSAInterface->pBlueprint->usCullDistance = ms_usFxSystemSavedCullDistance;
ms_fUsingDrawDistance = 0;
ms_pUsingFxSystemSAInterface = NULL;
ms_fFxSystemUpdateCullDistMultiplier = FX_SYSTEM_UPDATE_CULL_DIST_MULTIPLIER_DEFAULT;
}
}
// Hook info
#define HOOKCHECK_FxSystem_c_Update_MidA 0x83
#define HOOKPOS_FxSystem_c_Update_MidA 0x04AAF70
#define HOOKSIZE_FxSystem_c_Update_MidA 5
DWORD RETURN_FxSystem_c_Update_MidA = 0x04AAF75;
void _declspec(naked) HOOK_FxSystem_c_Update_MidA()
{
_asm
{
pushad
push ecx
call OnMY_FxSystem_c_Update_MidA_Pre
add esp, 4*1
popad
push [esp+4*2]
push [esp+4*2]
call inner
pushad
call OnMY_FxSystem_c_Update_MidA_Post
popad
retn 8
inner:
sub esp, 10h
push ebx
push ebp
jmp RETURN_FxSystem_c_Update_MidA
}
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// FxSystem_c::Update_MidB
//
// Poke new draw distances for each emitter
//
//////////////////////////////////////////////////////////////////////////////////////////
void OnMY_FxSystem_c_Update_MidB_Pre(CFxEmitterSAInterface* pFxEmitterSAInterface)
{
if (ms_fUsingDrawDistance)
{
dassert(pFxEmitterSAInterface->vtbl == (void*)0x085A7A4);
ms_pUsingFxEmitterSAInterface = pFxEmitterSAInterface;
ms_usFxEmitterSavedFadeFarDistance = pFxEmitterSAInterface->pBlueprint->usFadeFarDistance;
ms_usFxEmitterSavedFadeNearDistance = pFxEmitterSAInterface->pBlueprint->usFadeNearDistance;
pFxEmitterSAInterface->pBlueprint->usFadeFarDistance = (ushort)(ms_fUsingDrawDistance * 8);
float fNearDistanceRatio = ms_usFxEmitterSavedFadeNearDistance / (float)ms_usFxEmitterSavedFadeFarDistance;
pFxEmitterSAInterface->pBlueprint->usFadeNearDistance = (ushort)(ms_fUsingDrawDistance * fNearDistanceRatio * 8);
ms_fFxCreateParticleCullDistMultiplier = 1 / 8.f;
}
}
void OnMY_FxSystem_c_Update_MidB_Post(void)
{
if (ms_pUsingFxEmitterSAInterface)
{
// Restore default settings
ms_pUsingFxEmitterSAInterface->pBlueprint->usFadeFarDistance = ms_usFxEmitterSavedFadeFarDistance;
ms_pUsingFxEmitterSAInterface->pBlueprint->usFadeNearDistance = ms_usFxEmitterSavedFadeNearDistance;
ms_pUsingFxEmitterSAInterface = NULL;
ms_fFxCreateParticleCullDistMultiplier = FX_CREATE_PARTICLE_CULL_DIST_MULTIPLIER_DEFAULT;
}
}
// Hook info
#define HOOKCHECK_FxSystem_c_Update_MidB 0x83
#define HOOKPOS_FxSystem_c_Update_MidB 0x04AB21D
#define HOOKSIZE_FxSystem_c_Update_MidB 7
DWORD RETURN_FxSystem_c_Update_MidB = 0x04AB224;
void _declspec(naked) HOOK_FxSystem_c_Update_MidB()
{
_asm
{
pushad
push ecx
call OnMY_FxSystem_c_Update_MidB_Pre
add esp, 4*1
popad
mov eax, [ebp+54h]
push eax
call dword ptr [edx+8] // FxEmitter_c::CreateParticles
pushad
call OnMY_FxSystem_c_Update_MidB_Post
popad
jmp RETURN_FxSystem_c_Update_MidB
}
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// CFxSystemSA::StaticSetHooks
//
// Setup hooks
//
//////////////////////////////////////////////////////////////////////////////////////////
void CFxSystemSA::StaticSetHooks(void)
{
EZHookInstall(FxSystem_c_Update_MidA);
EZHookInstall(FxSystem_c_Update_MidB);
// Redirect these constants so we can change them
MemPut<float*>(VAR_FxSystemUpdateCullDistMultiplier, &ms_fFxSystemUpdateCullDistMultiplier);
MemPut<float*>(VAR_FxCreateParticleCullDistMultiplierA, &ms_fFxCreateParticleCullDistMultiplier);
MemPut<float*>(VAR_FxCreateParticleCullDistMultiplierB, &ms_fFxCreateParticleCullDistMultiplier);
}