-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgravity_actor_extension.h
More file actions
194 lines (151 loc) · 4.81 KB
/
gravity_actor_extension.h
File metadata and controls
194 lines (151 loc) · 4.81 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
#pragma once
#include "SM64DS_PI.h"
#include "gravity_field.h"
#include "gravity_actor_tree.h"
#include "gravity_math.h"
class ActorExtension : public ActorTreeNode, public ActorList::Node
{
struct ConverterBase
{
const Matrix3x3& basis0;
const Matrix3x3& basis1;
const Vector3& pivot;
ConverterBase(ActorExtension& ext0, const Actor& actor1, const ActorExtension& ext1):
basis0(ext0.GetGravityMatrix()),
basis1(ext1.GetGravityMatrix()),
pivot(actor1.prevPos)
{}
};
struct Converter : private ConverterBase
{
using ConverterBase::ConverterBase;
auto& operator()(Vector3& p) const { return p.RotateAround(pivot, basis1.Transpose()); }
auto& operator()(short& a) const { return ConvertAngle(a, basis0, basis1); }
};
struct ReverseConverter : private ConverterBase
{
using ConverterBase::ConverterBase;
auto& operator()(Vector3& p) const { return p.RotateAround(pivot, basis1); }
auto& operator()(short& a) const { return ConvertAngle(a, basis1, basis0); }
};
struct Initializer : public ReverseConverter { using ReverseConverter::ReverseConverter; };
struct Restorer : public ReverseConverter { using ReverseConverter::ReverseConverter; };
template<auto... memberPath>
class Property
{
using T = std::remove_reference_t<decltype((std::declval<Actor>() .* ... .* memberPath))>;
T original;
T transformed;
public:
void Set(Actor& actor)
{
T& val = (actor .* ... .* memberPath);
original = transformed = val;
}
void Set(Actor& actor, const Converter& convert)
{
T& val = (actor .* ... .* memberPath);
original = val;
transformed = convert(val);
}
void Set(Actor& actor, const Initializer& init)
{
Set(actor);
init(original);
}
void Set(Actor& actor, const Restorer& restore)
{
T& val = (actor .* ... .* memberPath);
if (val == transformed) [[likely]]
val = original;
else [[unlikely]]
restore(val);
}
constexpr const T& GetOriginal() const { return original; }
};
template<class... P>
struct Properties : public P...
{
[[gnu::always_inline]]
void SetAll(Actor& actor, auto&&... f) { (..., static_cast<P&>(*this).Set(actor, f...)); }
template<auto... memberPath>
const auto& GetRealValue() const
{
return static_cast<const Property<memberPath...>&>(*this).GetOriginal();
}
};
using ActorTreeNode::Find;
Matrix3x3 currMatrix;
Vector3 lastUpdatePoint;
Properties <
Property<&Actor::pos>,
Property<&Actor::prevPos>,
Property<&Actor::ang, &Vector3_16::y>,
Property<&Actor::motionAng, &Vector3_16::y>
> properties;
Sqaerp fieldSqaerp;
uint16_t angleToNewField = 0;
int CalculateUpVector(Vector3_Q24& __restrict__ res, const Vector3& pos, Sqaerp& sqaerp) const;
public:
Matrix3x3 cylClsnPushbackBasis;
Vector3 savedPos;
template<auto... memberPath>
const auto& GetRealValue() const
{
return properties.GetRealValue<memberPath...>();
}
using ActorList::Node::GetGravityField;
using ActorList::Node::ShouldBeTransformed;
void UpdateGravity();
const Vector3& GetUpVectorQ12() const { return currMatrix.c1; }
const Vector3& GetLastUpdatePoint() const { return lastUpdatePoint; }
const Matrix3x3& GetGravityMatrix() const { return currMatrix; }
bool IsInTrivialField() const
{
return GetGravityField().IsTrivial() && angleToNewField == 0;
}
[[gnu::noinline]]
static ActorExtension& Get(const Actor& actor)
{
const std::size_t offset = Memory::gameHeapPtr->Sizeof(&actor) - sizeof(ActorExtension);
return const_cast<ActorExtension&>(
*reinterpret_cast<const ActorExtension*>(
reinterpret_cast<const std::byte*>(&actor) + offset
)
);
}
[[gnu::always_inline]]
void SetProperties(Actor& pivotActor, const ActorExtension& behavingExtension)
{
properties.SetAll(GetActor(), Converter(*this, pivotActor, behavingExtension));
}
[[gnu::always_inline]]
void RestoreProperties(Actor& pivotActor, const ActorExtension& behavingExtension)
{
properties.SetAll(GetActor(), Restorer(*this, pivotActor, behavingExtension));
}
int PredictNextUpVector(Vector3_Q24& __restrict__ res, const Vector3& nextPos) const
{
Sqaerp sqaerpCopy = fieldSqaerp;
return CalculateUpVector(res, nextPos, sqaerpCopy);
}
[[gnu::always_inline]]
ActorExtension(Actor& actor):
ActorTreeNode(actor),
ActorList::Node(actor),
currMatrix(GetGravityField().GetFirstFieldMatrix(actor.pos, actor.actorID)),
cylClsnPushbackBasis(Matrix3x3::Identity())
{
if (extern Actor* behavingActor; behavingActor && CanSpawnAsSubActor())
properties.SetAll(actor, Initializer(*this, *behavingActor, ActorExtension::Get(*behavingActor)));
else
properties.SetAll(actor);
lastUpdatePoint = savedPos = GetRealValue<&Actor::pos>();
}
};
const Actor* ActorCast(const ActorBase& actorBase);
[[gnu::always_inline]]
inline Actor* ActorCast(ActorBase& actorBase)
{
return const_cast<Actor*>(ActorCast(std::as_const(actorBase)));
}