forked from ciathyza/AlternateConversationCamera
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Camera.cpp
345 lines (271 loc) · 10.1 KB
/
Camera.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "Camera.h"
#include "ObjectRef.h"
#include "skse64/NiNodes.h"
namespace Tralala
{
uintptr_t g_playerCameraAddr = 0;
uintptr_t g_setCameraStateAddr = 0;
uintptr_t g_pushTargetCamAddr = 0;
uintptr_t g_pushCurrentCamAddr = 0;
uintptr_t g_setCamTargetAddr = 0;
uintptr_t g_forceFirstPersonAddr = 0;
uintptr_t g_updateThirdPersonAddr = 0;
uintptr_t g_getPenetrationAddr = 0;
uintptr_t g_processCamColAddr = 0;
uintptr_t g_getClosestPointAddr = 0;
uintptr_t g_camProcessColAddr = 0;
void PlayerCameraGetAddress()
{
const std::array<BYTE, 9> pattern = { 0x8B, 0x4D, 0xC7, 0x0F, 0xB6, 0xC9, 0x83, 0xF8, 0x04 };
g_playerCameraAddr = (uintptr_t)scan_memory_data(pattern, 0x21, true, 0x3, 0x7);
const std::array<BYTE, 9> camStatepattern = { 0x48, 0x8B, 0x4F, 0x28, 0x48, 0x3B, 0xD9, 0x74, 0x29 };
g_setCameraStateAddr = (uintptr_t)scan_memory(camStatepattern, 0x1F, false);
const std::array<BYTE, 7> camTargetpattern = { 0x89, 0x41, 0x3C, 0x48, 0x83, 0xC4, 0x20 };
g_setCamTargetAddr = (uintptr_t)scan_memory(camTargetpattern, 0x1C, false);
const std::array<BYTE, 6> firstPersonpattern = { 0x48, 0x39, 0x41, 0x28, 0x74, 0x19 };
g_forceFirstPersonAddr = (uintptr_t)scan_memory(firstPersonpattern, 0x1B, false);
const std::array<BYTE, 8> camColpattern = { 0xF3, 0x0F, 0x59, 0x5F, 0x18, 0x0F, 0x28, 0xC7 };
g_processCamColAddr = (uintptr_t)scan_memory_data(camColpattern, 0x7B, true, 0x1, 0x5);
const std::array<BYTE, 8> penetpattern = { 0xF3, 0x0F, 0x10, 0x55, 0x80, 0x0F, 0x28, 0xCA };
g_getPenetrationAddr = (uintptr_t)scan_memory_data(penetpattern, 0x7D, true, 0x1, 0x5);
g_getClosestPointAddr = (uintptr_t)scan_memory_data(penetpattern, 0xF0, false, 0x1, 0x5);
const std::array<BYTE, 6> processColPattern = { 0x0F, 0x28, 0xD8, 0x0F, 0x2F, 0xD8 };
g_camProcessColAddr = (uintptr_t)scan_memory_data(processColPattern, 0xBF, false, 0x1, 0x5);
}
float ThirdPersonState::GetDistanceWithinTargetHead(Actor * target)
{
if (!target || !target->processManager || !target->processManager->middleProcess) return FLT_MAX;
NiNode* headNode = (NiNode*)target->processManager->middleProcess->unk158;
if (!headNode) return FLT_MAX;
NiPoint3 dist;
dist.x = camPos.x - headNode->m_worldTransform.pos.x;
dist.y = camPos.y - headNode->m_worldTransform.pos.y;
dist.z = camPos.z - headNode->m_worldTransform.pos.z;
return (dist.x*dist.x + dist.y*dist.y + dist.z*dist.z);
}
void ThirdPersonState::SetFirstPersonSmooth(float minZoom, bool overShoulder)
{
if (unkDD[0] == 0)
{
savedZoom = curPosY;
dstPosY = minZoom;
unkDD[0] = 1;
}
else
{
curPosY = dstPosY;
}
if (!overShoulder) fOverShoulderPosX = fOverShoulderCombatAddY = fOverShoulderPosZ = 0.0f;
}
void ThirdPersonState::SetShoulderPos(const NiPoint3& pos)
{
this->fOverShoulderPosX = pos.x;
this->fOverShoulderPosZ = pos.z;
this->fOverShoulderCombatAddY = pos.y;
}
void ThirdPersonState::ProcessCameraCollision()
{
typedef void(*ProcessCamCol_t)(ThirdPersonState*);
ProcessCamCol_t ProcessCamCol = (ProcessCamCol_t)g_processCamColAddr;
ProcessCamCol(this);
}
void TESCamera::SetCameraState(TESCameraState * camState)
{
typedef UInt32(*SetCameraState_t)(TESCamera* camera, TESCameraState* cameraState);
SetCameraState_t SetCameraState = (SetCameraState_t)g_setCameraStateAddr;
SetCameraState(this, camState);
}
NiCamera* TESCamera::GetNiCamera()
{
NiCamera* camera = nullptr;
std::size_t size = cameraNode->m_children.m_arrayBufLen;
for (std::size_t i = 0; i < size; ++i)
{
NiAVObject* pObj = cameraNode->m_children.m_data[i];
if (!pObj) continue;
if (strcmp(pObj->GetRTTI()->name, "NiCamera") == 0)
{
camera = (NiCamera*)pObj;
break;
}
}
return camera;
}
PlayerCamera * PlayerCamera::GetSingleton()
{
return *(PlayerCamera**)g_playerCameraAddr;
}
bool PlayerCamera::IsCameraFirstPerson()
{
return cameraState == cameraStates[kCameraState_FirstPerson];
}
bool PlayerCamera::IsCameraThirdPerson()
{
return cameraState == cameraStates[kCameraState_ThirdPerson2];
}
TESCameraState * PlayerCamera::ProcessCameraTransition(UInt32 srcCamState, UInt32 dstCamState, bool face2face)
{
TESCameraState * target = cameraStates[srcCamState];
TweenMenuCameraState * tween = (TweenMenuCameraState*)cameraStates[kCameraState_TweenMenu];
if (cameraState == tween)
{
typedef SInt32(*PushTargetCamState_t)(Data40 * data40, TESCameraState * camState);
PushTargetCamState_t PushTargetCamState = (PushTargetCamState_t)g_pushTargetCamAddr;
PushTargetCamState(&unk40, target);
}
else
{
typedef SInt32(*PushCurrentCamState_t)(Data40 * data40, TESCameraState ** camState);
PushCurrentCamState_t PushCurrentCamState = (PushCurrentCamState_t)g_pushCurrentCamAddr;
if (face2face)
{
TESCameraState * destination = cameraStates[dstCamState];
PushCurrentCamState(&unk40, &destination);
}
else
{
PushCurrentCamState(&unk40, &cameraState);
}
SetCameraState(target);
}
return target;
}
void PlayerCamera::SetCameraTarget(Actor* target)
{
typedef void(*SetCameraTarget_t)(PlayerCamera* camera, Actor* target);
SetCameraTarget_t SetCameraTarget = (SetCameraTarget_t)g_setCamTargetAddr;
SetCameraTarget(this, target);
}
void PlayerCamera::ForceFirstPerson()
{
typedef void(*ForceFirstPerson_t)(PlayerCamera * camera);
ForceFirstPerson_t ForceFirstPerson = (ForceFirstPerson_t)g_forceFirstPersonAddr;
ThirdPersonState* tps = GetThirdPersonCamera();
tps->savedZoom = tps->curPosY;
ForceFirstPerson(this);
}
void PlayerCamera::ForceThirdPerson()
{
ThirdPersonState * tps = GetThirdPersonCamera();
tps->basePosX = tps->fOverShoulderPosX;
tps->basePosY = tps->fOverShoulderCombatAddY;
tps->basePosZ = tps->fOverShoulderPosZ;
tps->dstPosY = tps->savedZoom;
SetCameraState(tps);
}
void PlayerCamera::UpdateThirdPersonMode(bool weaponDrawn)
{
typedef void(*UpdateThirdPersonMode_t)(PlayerCamera * camera, bool weaponDrawn);
UpdateThirdPersonMode_t UpdateThirdPersonMode = (UpdateThirdPersonMode_t)g_updateThirdPersonAddr;
UpdateThirdPersonMode(this, weaponDrawn);
}
bool PlayerCamera::GetDistanceWithTargetBone(Actor * target, NiPoint3 * dist)
{
NiPoint3 targetPos;
target->GetTargetNeckPosition(&targetPos);
dist->x = targetPos.x - cameraPos.x;
dist->y = targetPos.y - cameraPos.y;
dist->z = targetPos.z - cameraPos.z;
return true;
}
float PlayerCamera::GetDistanceWithTargetBone(Actor * target, bool firstPerson)
{
NiPoint3 neckPos;
NiPoint3 camPos;
target->GetTargetNeckPosition(&neckPos);
if (firstPerson)
{
PlayerCharacter * player = PlayerCharacter::GetSingleton();
camPos.x = player->pos.x;
camPos.y = player->pos.y;
FirstPersonState * fps = GetFirstPersonCamera();
if (fps->cameraNode)
{
camPos.z = player->pos.z + fps->cameraNode->m_localTransform.pos.z;
}
else
{
player->GetMarkerPosition(&camPos);
}
}
else
{
ThirdPersonState * tps = GetThirdPersonCamera();
camPos = tps->camPos;
}
float dx = neckPos.x - camPos.x;
float dy = neckPos.y - camPos.y;
float dz = neckPos.z - camPos.z;
return sqrt(dx*dx + dy*dy + dz*dz);
}
bool PlayerCamera::GetPenetration(Actor * target, NiPoint3* sourcePos, float radiusToCheck, float offsetActorFromGround)
{
typedef bool(*GetPenetration_t)(SimpleShapePhantom*, Actor *, NiPoint3*, float, float);
GetPenetration_t GetPenetration = (GetPenetration_t)g_getPenetrationAddr;
return GetPenetration(simpleShapePhantoms, target, sourcePos, radiusToCheck, offsetActorFromGround);
}
bool PlayerCamera::GetClosestPoint(void* bhkWorldM, NiPoint3* sourcePos, NiPoint3* resultPos,
Havok::hkpRootCdPoint* resultInfo, Actor** resultActor, float radiusToCheck)
{
typedef bool(*GetClosestPoint_t)(SimpleShapePhantom*, void*, NiPoint3*, NiPoint3*, Havok::hkpRootCdPoint*, Actor**, float);
GetClosestPoint_t GetClosestPoint = (GetClosestPoint_t)g_getClosestPointAddr;
return GetClosestPoint(simpleShapePhantoms, bhkWorldM, sourcePos, resultPos, resultInfo, resultActor, radiusToCheck);
}
bool PlayerCamera::ProcessCollision(NiPoint3* camPos, bool isFade)
{
typedef bool(*ProcessCollision_t)(PlayerCamera*, NiPoint3*, bool);
ProcessCollision_t ProcessCollision = (ProcessCollision_t)g_camProcessColAddr;
return ProcessCollision(this, camPos, isFade);
}
bool PlayerCamera::IsInCameraView(NiPoint3* pos, int mode)
{
NiCamera* niCamera = GetNiCamera();
if (!niCamera) return false;
// project a world space point to screen space
float w = pos->x * niCamera->m_aafWorldToCam[3][0] + pos->y * niCamera->m_aafWorldToCam[3][1] + pos->z * niCamera->m_aafWorldToCam[3][2] + niCamera->m_aafWorldToCam[3][3];
// Check to see if we're on the appropriate side of the camera.
if (w > 1e-5f)
{
float invW = 1.0f / w;
float screenX = pos->x * niCamera->m_aafWorldToCam[0][0] + pos->y * niCamera->m_aafWorldToCam[0][1] + pos->z * niCamera->m_aafWorldToCam[0][2] + niCamera->m_aafWorldToCam[0][3];
float screenY = pos->x * niCamera->m_aafWorldToCam[1][0] + pos->y * niCamera->m_aafWorldToCam[1][1] + pos->z * niCamera->m_aafWorldToCam[1][2] + niCamera->m_aafWorldToCam[1][3];
screenX *= invW;
screenY *= invW;
screenX *= (niCamera->m_kPort.m_right - niCamera->m_kPort.m_left) * 0.5f;
screenY *= (niCamera->m_kPort.m_top - niCamera->m_kPort.m_bottom) * 0.5f;
screenX += (niCamera->m_kPort.m_right + niCamera->m_kPort.m_left) * 0.5f;
screenY += (niCamera->m_kPort.m_top + niCamera->m_kPort.m_bottom) * 0.5f;
// If on screen return true. Otherwise, we fall through to false.
switch (mode)
{
case kRotate_Fast:
{
if (screenX >= 0.3125f && screenX <= 0.6875f && screenY >= 0.28125f && screenY <= 0.71785f)
{
return true;
}
break;
}
case kRotate_Slow:
{
if (screenX >= 0.375f && screenX <= 0.625f && screenY >= 0.34375f && screenY <= 0.65625f)
{
return true;
}
break;
}
case KRotate_Stop:
{
if (screenX >= 0.4375f && screenX <= 0.5625f && screenY >= 0.40625f && screenY <= 0.59375f)
{
return true;
}
break;
}
default:
return false;
}
}
return false;
}
}