-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgravity_actor_extension.cpp
More file actions
163 lines (130 loc) · 3.76 KB
/
gravity_actor_extension.cpp
File metadata and controls
163 lines (130 loc) · 3.76 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
#include "gravity_actor_extension.h"
asm(R"(
@ at the beginning of Actor::Spawn
nsub_02010e34:
ldr r5, =spawningActorID
strh r0,[r5]
mov r5, r0
b 0x02010e38
@ at the end of Actor::Spawn
nsub_02010e6c:
ldr r5, =spawningActorID
mov r4, #0
strh r4,[r5]
ldr r5, =spawningActor
str r4,[r5]
pop {r4, r5, r15}
_Z18AllocateOnGameHeapj:
push {r4, r5, r14}
b _ZN9ActorBasenwEj + 4
)");
static_assert(alignof(ActorExtension) <= alignof(Actor));
const Actor* ActorCast(const ActorBase& actorBase)
{
if (actorBase.category == ActorBase::ACTOR)
return static_cast<const Actor*>(&actorBase);
else
return nullptr;
}
uint16_t spawningActorID = 0;
static std::byte* spawningExtensionAddr;
std::byte* AllocateOnGameHeap(size_t size);
// at the beginning of ActorBase::operator new
void* nsub_02043444(size_t size)
{
if (spawningActorID == 0)
return AllocateOnGameHeap(size);
std::byte* allocAddr = AllocateOnGameHeap(size + sizeof(ActorExtension));
spawningExtensionAddr = allocAddr + size;
return allocAddr;
}
const Actor* spawningActor = nullptr;
asm(R"(
nsub_020114e0 = _Z18ConstructExtensionR5Actor
nsub_0201162c = _Z18ConstructExtensionR5Actor
)");
Actor& ConstructExtension(Actor& actor)
{
new (spawningExtensionAddr) ActorExtension(actor);
spawningActor = &actor;
return actor;
}
asm(R"(
repl_020112cc:
repl_02011318:
repl_02011378:
mov r4, r0
b _Z17DestructExtensionRK5Actor
)");
void DestructExtension(const Actor& actor)
{
ActorExtension::Get(actor).~ActorExtension();
}
int ActorExtension::CalculateUpVector(Vector3_Q24& __restrict__ res, const Vector3& pos, Sqaerp& sqaerp) const
{
AssureUnaliased(res) = Vector3_Q24::Raw(currMatrix.c1).NormalizedTwice();
return sqaerp(res, GetGravityField().GetUpVectorQ24(pos), 1_deg, false, angleToNewField);
}
static Actor& GetHoldingActor(Actor& actor)
{
if ((actor.flags & Actor::IN_PLAYER_HAND) && PLAYER_ARR[0])
return *PLAYER_ARR[0];
else
return actor;
}
void ActorExtension::UpdateGravity()
{
Actor& actor = GetActor();
const Vector3 delta = actor.pos - GetLastUpdatePoint();
if (delta.Dot(delta) < 1._f && angleToNewField == 0)
return;
if (!AlwaysInDefaultField())
{
Actor& holdingActor = GetHoldingActor(actor);
auto& found = GravityField::GetFieldAt(holdingActor.pos);
const bool fieldChanged = found.GetPriority() >= 0 && &found != &GetGravityField();
if (fieldChanged)
{
GetGravityField().GetActorList().Remove(*this);
found.GetActorList().Insert(*this);
SetGravityField(found);
fieldSqaerp.Reset();
angleToNewField = 180_deg;
}
if (fieldChanged || !GetGravityField().IsHomogeneous() || angleToNewField > 0)
{
const Matrix3x3 prevMatrix = currMatrix;
Vector3_Q24 currUpVector;
angleToNewField = CalculateUpVector(currUpVector, holdingActor.pos, fieldSqaerp);
if (IsInTrivialField())
currMatrix = Matrix3x3::Identity();
else
{
currMatrix.c1 = currUpVector.data.NormalizedTwice();
if (actor.actorID == 0xbf)
{
if (static_cast<const Player&>(actor).currState == &Player::ST_FIRST_PERSON)
{
lastUpdatePoint = actor.pos;
return;
}
// This makes the controls of the player feel more accurate and responsive.
// The difference may not be noticable to an inexperienced player.
SphericalForwardField (
currMatrix.c2,
static_cast<Vector3_Q24>(INV_VIEW_MATRIX_ASR_3.c0),
static_cast<Vector3_Q24>(INV_VIEW_MATRIX_ASR_3.c2),
currUpVector
);
}
else
currMatrix.c2 = currMatrix.c0.Cross(currMatrix.c1);
currMatrix.c2.NormalizeTwice();
currMatrix.c0 = currMatrix.c1.Cross(currMatrix.c2).NormalizedTwice();
}
ConvertAngle(actor.ang.y, prevMatrix, currMatrix);
ConvertAngle(actor.motionAng.y, prevMatrix, currMatrix);
}
}
lastUpdatePoint = actor.pos;
}