-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgravity_meshclsn.cpp
More file actions
382 lines (304 loc) · 9.47 KB
/
gravity_meshclsn.cpp
File metadata and controls
382 lines (304 loc) · 9.47 KB
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include "gravity_actor_extension.h"
extern Actor* behavingActor;
extern const Actor* spawningActor;
void RedirectDelta(Actor& actor, ActorExtension& extension);
class Transform
{
public:
using Func = void(BgCh&, const Vector3&, const Matrix3x3&, bool afterClsn);
private:
ActorExtension* extension = nullptr;
BgCh* bgch = nullptr;
Func* func = nullptr;
public:
void operator()(const Vector3& v, const Matrix3x3& m) const
{
func(*bgch, v, m, false);
}
void operator()(const Vector3& v, Matrix3x3::TransposeProxy m) const
{
func(*bgch, v, m.Transpose(), true);
}
void Activate(ActorExtension& extension, BgCh& bgch, Func& func)
{
this->extension = &extension;
this->bgch = &bgch;
this->func = &func;
}
void Deactivate() { extension = nullptr; }
ActorExtension* GetActorExtension() { return extension; }
constexpr bool IsActive() const { return extension; }
consteval Transform() = default;
}
static constinit transform;
template<class T, std::invocable<T&, const Vector3&, const Matrix3x3&, bool> F>
static void ApplyBgChTransform(T& bgch, F&&)
{
[](BgCh& bgch, Transform::Func& func) // a lambda to avoid code duplication
{
if (Actor* actor = bgch.objPtr ?: behavingActor)
{
#ifdef GRAVITY_DEBUG_COUNTERS
extern unsigned bgChTransformCounter;
++bgChTransformCounter;
#endif
auto& extension = ActorExtension::Get(*actor);
if (extension.IsInTrivialField()) return;
transform.Activate(extension, bgch, func);
transform(extension.savedPos, extension.GetGravityMatrix());
}
}
(bgch, *[](BgCh& bgch, const Vector3& actorPos, const Matrix3x3& m, bool afterClsn)
{
if (afterClsn)
F()(static_cast<T&>(bgch), actorPos, m.Transpose(), afterClsn);
else
F()(static_cast<T&>(bgch), actorPos, m, afterClsn);
});
}
static void RestoreBgChTransform()
{
if (!transform.IsActive()) return;
auto* extension = transform.GetActorExtension();
transform(extension->GetActor().pos, extension->GetGravityMatrix().Transpose());
transform.Deactivate();
}
asm(R"(
repl_0203863c: @ the beginning of RaycastLine::DetectClsn
sub r13, r13, #0x1c
b _Z10BeforeClsnR11RaycastLine
repl_020385a0: @ the beginning of RaycastLine::DetectClsnStageOnly
sub r13, r13, #8
b _Z10BeforeClsnR11RaycastLine
)");
constinit const RaycastGround* currRaycastGround = nullptr;
constinit const GravityField* raycastGroundField = nullptr;
RaycastLine& BeforeClsn(RaycastLine& line)
{
ApplyBgChTransform(line, [](RaycastLine& line, const Vector3& actorPos, const auto& rotation, bool afterClsn)
{
line.line.pos0.RotateAround(actorPos, rotation);
if (currRaycastGround && !afterClsn)
{
const Vector3& p0 = line.line.pos0;
Vector3 v;
if (const Vector3* u = raycastGroundField->GetHomogeneousUpVectorQ12())
v = *u << 10;
else
{
v = raycastGroundField->GetAltitudeVector(p0);
if (const Fix12i altitude = v.Len(); altitude > 1024._f)
v *= 1024._f / altitude;
}
line.SetObjAndLine(p0, p0 - v, line.objPtr);
line.result = currRaycastGround->result;
}
else
{
line.line.pos1.RotateAround(actorPos, rotation);
line.average.RotateAround(actorPos, rotation);
line.clsnPos.RotateAround(actorPos, rotation);
}
});
return line;
}
asm(R"(
repl_02038b7c: @ the beginning of SphereClsn::DetectClsn
sub r13, r13, #0x1c
b _Z10BeforeClsnR10SphereClsn
)");
SphereClsn& BeforeClsn(SphereClsn& sphere)
{
ApplyBgChTransform(sphere, [](SphereClsn& sphere, const Vector3& actorPos, const auto& rotation, bool afterClsn)
{
sphere.pos.RotateAround(actorPos, rotation);
});
return sphere;
}
asm("nsub_0203881c = _Z9AfterClsnb"); // the end of RayCastLine::DetectClsn
asm("nsub_02038630 = _Z9AfterClsnb"); // the end of RayCastLine::DetectClsnStageOnly
asm("nsub_02038e9c = _Z9AfterClsnb"); // the end of SphereClsn::DetectClsn
bool AfterClsn(bool res)
{
RestoreBgChTransform();
return res;
}
void TransformNormal(Vector3& normal)
{
if (auto* extension = transform.GetActorExtension())
{
normal *= extension->GetGravityMatrix().Transpose();
normal.NormalizeTwice();
}
}
void nsub_01ffd91c(void*, void*, Vector3& normal)
{
TransformNormal(normal);
}
void repl_02039f10(const MovingMeshCollider& meshClsn, const Vector3& normal, Vector3& res)
{
res = meshClsn.newTransform.Linear()(normal);
TransformNormal(res);
}
asm(R"(
_Z20ContinueToDetectClsnR13RaycastGround:
push {r4-r11, r14}
sub r13, r13, #0x1c
b 0x02038f4c
nsub_02038f48 = _Z15TransformNormalR7Vector3
)");
bool ContinueToDetectClsn(RaycastGround&);
bool nsub_02038f44(RaycastGround& raycastGround)
{
const Vector3& pos0 = raycastGround.pos;
const GravityField* field;
Actor* actor = raycastGround.objPtr ?: behavingActor;
if (actor)
{
const ActorExtension& extension = ActorExtension::Get(*actor);
if (extension.IsInTrivialField())
return ContinueToDetectClsn(raycastGround);
field = &extension.GetGravityField();
}
else
{
field = &GravityField::GetFieldAt(pos0);
if (field->IsTrivial())
return ContinueToDetectClsn(raycastGround);
}
currRaycastGround = &raycastGround;
raycastGroundField = field;
RaycastLine line;
line.line.pos0 = pos0;
line.objPtr = actor;
line.flags = raycastGround.flags;
const bool detected = line.DetectClsn();
currRaycastGround = nullptr;
raycastGroundField = nullptr;
raycastGround.flags = line.flags;
raycastGround.result = line.result;
raycastGround.clsnPosY = pos0.y - line.line.pos0.Dist(line.clsnPos);
raycastGround.hadCollision = line.hadCollision;
return detected;
}
static const Matrix4x3& TransformMMC(MovingMeshCollider& meshClsn, const Matrix4x3& transform, Matrix4x3& res)
{
const Actor* actor = meshClsn.actor;
if (actor == nullptr)
{
actor = spawningActor;
if (actor == nullptr)
actor = behavingActor;
}
if (actor)
{
const auto& extension = ActorExtension::Get(*actor);
if (!extension.IsInTrivialField())
{
res.Linear() = extension.GetGravityMatrix()(transform.Linear());
res.c3 = transform.c3;
return res;
}
}
return transform;
}
asm(R"(
_Z19ContinueToTransformR18MovingMeshColliderRK9Matrix4x3s:
push {r4-r11, r14}
b 0x02039f24
_Z17ContinueToSetFileR18MovingMeshColliderPcRK9Matrix4x35Fix12IiEsR10CLPS_Block:
push {r4-r10, r14}
b 0x0203a1e4
)");
void ContinueToTransform(MovingMeshCollider& meshClsn, const Matrix4x3& newTransform, short angleY);
void ContinueToSetFile (
MovingMeshCollider& meshClsn, char* clsnFile,
const Matrix4x3& transform, Fix12i scale,
short angleY, CLPS_Block& clps
);
void nsub_0203a1e0 (
MovingMeshCollider& meshClsn, char* clsnFile,
const Matrix4x3& transform, Fix12i scale,
short angleY, CLPS_Block& clps
)
{
Matrix4x3 res;
const Matrix4x3& newTransform = TransformMMC(meshClsn, transform, res);
ContinueToSetFile(meshClsn, clsnFile, newTransform, scale, angleY, clps);
}
void nsub_02039f20(MovingMeshCollider& meshClsn, const Matrix4x3& transform, short angleY)
{
Matrix4x3 res;
const Matrix4x3& newTransform = TransformMMC(meshClsn, transform, res);
ContinueToTransform(meshClsn, newTransform, angleY);
}
auto repl_020383a4() // before calling the function that calls beforeClsnCallback
{
if (behavingActor)
{
auto& extension = ActorExtension::Get(*behavingActor);
if (!extension.IsInTrivialField())
RedirectDelta(*behavingActor, extension);
}
return ACTIVE_MESH_COLLIDERS;
}
void nsub_020383dc() // after calling the function that calls beforeClsnCallback
{
if (behavingActor)
ActorExtension::Get(*behavingActor).savedPos = behavingActor->pos;
}
Vector3 savedPrevPos;
asm(R"(
repl_0203702c: @ at the beginning of WithMeshClsn::UpdateDiscreteNoLava
repl_02036eec: @ at the beginning of WithMeshClsn::UpdateDiscreteNoLava_2
mov r7, r0
b _Z15BeforeWMCUpdateRK12WithMeshClsn
repl_020366bc: @ at the beginning of WithMeshClsn::UpdateContinuous
repl_02036320: @ at the beginning of WithMeshClsn::UpdateContinuousNoLava
repl_020358b4: @ at the beginning of WithMeshClsn::UpdateExtraContinuous
mov r10, r0
b _Z15BeforeWMCUpdateRK12WithMeshClsn
)");
const WithMeshClsn& BeforeWMCUpdate(const WithMeshClsn& wmClsn)
{
if (!wmClsn.actor) return wmClsn;
Actor& actor = *wmClsn.actor;
auto& extension = ActorExtension::Get(actor);
const bool trivial = extension.IsInTrivialField();
if (!trivial)
RedirectDelta(actor, extension);
savedPrevPos = actor.prevPos;
if (!trivial)
actor.prevPos.RotateAround(actor.pos, extension.GetGravityMatrix().Transpose());
return wmClsn;
}
asm(R"(
repl_0203717c: @ at the first exit point of WithMeshClsn::UpdateDiscreteNoLava
repl_02036ff0: @ at the first exit point of WithMeshClsn::UpdateDiscreteNoLava_2
bxne lr
b nsub_020371a8
repl_02037194: @ at the second exit point of WithMeshClsn::UpdateDiscreteNoLava
repl_02037008: @ at the second exit point of WithMeshClsn::UpdateDiscreteNoLava_2
bxeq lr
nsub_020371a8: @ at the last exit point of WithMeshClsn::UpdateDiscreteNoLava
nsub_0203701c: @ at the last exit point of WithMeshClsn::UpdateDiscreteNoLava_2
mov r0, r7
pop {r4-r7, r14}
b _Z14AfterWMCUpdateRK12WithMeshClsn
nsub_02036ac4: @ at the only exit point of WithMeshClsn::UpdateContinuous
nsub_020366ac: @ at the only exit point of WithMeshClsn::UpdateContinuousNoLava
nsub_0203630c: @ at the only exit point of WithMeshClsn::UpdateExtraContinuous
mov r0, r10
pop {r4-r11, r14}
b _Z14AfterWMCUpdateRK12WithMeshClsn
)");
void AfterWMCUpdate(const WithMeshClsn& wmClsn)
{
if (wmClsn.actor)
{
auto& extension = ActorExtension::Get(*wmClsn.actor);
if (!extension.IsInTrivialField())
RedirectDelta(*wmClsn.actor, extension);
wmClsn.actor->prevPos = savedPrevPos;
}
}