-
Notifications
You must be signed in to change notification settings - Fork 2k
/
HeadlightConfiguration.cpp
177 lines (143 loc) · 3.78 KB
/
HeadlightConfiguration.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
#include "StdInc.h"
#include "Hooking.h"
#include <ScriptEngine.h>
#include <ICoreGameInit.h>
static hook::cdecl_stub<void(void*, int, int, float, int, void*, void*)> setPartRotation([]()
{
return hook::get_call(hook::get_pattern("41 83 E0 07 48 89 44 24 30 48 83", 20));
});
static std::map<uint32_t, std::tuple<float, float, bool>> g_headlightConfiguration;
static void CustomHandleHeadlights(char* vehicle)
{
// run the function
char* modelData = *(char**)(vehicle + 32);
char* unkBit = **(char***)(modelData + 176);
// light index?
int lightIndex = *(char*)(unkBit + 277); // VERSION-SPECIFIC: 505
if (lightIndex < 0)
{
// no light
*(float*)(vehicle + 740) = 1.0f; // 505 specific
}
else
{
// default values
float rate = 3.0f;
float rotation = 7.0f;
bool inverse = true;
// is this in any configuration? if so, overwrite values
{
uint32_t mi = *(uint32_t*)(modelData + 24);
auto it = g_headlightConfiguration.find(mi);
if (it != g_headlightConfiguration.end())
{
rate = std::get<0>(it->second);
rotation = std::get<1>(it->second);
inverse = std::get<bool>(it->second);
}
else if (mi == HashString("tropos"))
{
rate = 30.0f;
rotation = 40.0f;
inverse = false;
}
}
// hardcoded 505
float deltaTime = *(float*)0x142ACFF94;
float step = (rate / rotation) * deltaTime;
// if paused, don't step at all
if (*(char*)0x142ACFF37 || *(char*)0x142ACFF38 || *(char*)0x142ACFF39 || *(char*)0x142ACFF3B)
{
step = 0.0f;
}
// should the headlights be up or down?
bool shouldBeUp = false;
if (*(uint8_t*)(vehicle + 2114) & 0x10)
{
if (*(uint8_t*)(vehicle + 2112) & 0xC0)
{
shouldBeUp = true;
}
else if ((*(uint8_t*)(vehicle + 2320) & 7) == 2)
{
shouldBeUp = true;
}
}
// rotate up/down
uint32_t stage = *(uint32_t*)(vehicle + 736);
// new rotation value
float rotationValue = *(float*)(vehicle + 740);
switch (stage)
{
// stage: down
case 0:
if (shouldBeUp)
{
// initiate moving upwards
stage = 2;
}
rotationValue = 0.0f;
break;
// stage: up
case 1:
if (!shouldBeUp)
{
// initiate moving downwards
stage = 3;
}
rotationValue = 1.0f;
break;
// stage: moving up
case 2:
rotationValue += step;
// are we up already?
if (rotationValue >= 1.0f)
{
stage = 1;
rotationValue = 1.0f;
}
break;
// stage: moving down
case 3:
rotationValue -= step;
// are we down already?
if (rotationValue <= 0.0f)
{
stage = 0;
rotationValue = 0.0f;
}
break;
}
// store new values
*(uint32_t*)(vehicle + 736) = stage;
*(float*)(vehicle + 740) = rotationValue;
// compute the actual rotation
float newRotation = (rotationValue * rotation * 0.017453292f);
if (inverse)
{
newRotation = -newRotation;
}
// set part rotation
setPartRotation(vehicle, lightIndex, 3, newRotation, 1, nullptr, nullptr);
}
}
static HookFunction hookFunction([]()
{
hook::jump(hook::get_pattern("83 FE 03 7C CF 48 8B CF 4C", 0x27), CustomHandleHeadlights);
Instance<ICoreGameInit>::Get()->OnGameRequestLoad.Connect([]()
{
g_headlightConfiguration.clear();
});
});
static InitFunction initFunction([]()
{
fx::ScriptEngine::RegisterNativeHandler("SET_MODEL_HEADLIGHT_CONFIGURATION", [] (fx::ScriptContext& context)
{
uint32_t modelHash = context.GetArgument<uint32_t>(0);
float step = context.GetArgument<float>(1);
float rotation = context.GetArgument<float>(2);
bool inverse = context.GetArgument<bool>(3);
g_headlightConfiguration[modelHash] = { step, rotation, inverse };
trace("SET_MODEL_HEADLIGHT_CONFIGURATION: set headlight configuration for %08x to {step: %f deg, rotation: %f deg, inverse: %d}\n", modelHash, step, rotation, (inverse) ? 1 : 0);
});
});