NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
d_cc.hpp
1#pragma once
2#include <types.h>
3#include <game/mLib/m_vec.hpp>
4
5class dActor_c;
6
7///< @unofficial
8enum CC_SHAPE_e {
9 CC_SHAPE_BOX, ///< Rectangular collider
10 CC_SHAPE_CIRCLE, ///< Circular / elliptical collider
11 CC_SHAPE_DAIKEI_UD, ///< Trapezoid-shaped collider (left/right sides are parallel)
12 CC_SHAPE_DAIKEI_LR, ///< Trapezoid-shaped collider (top/bottom sides are parallel)
13};
14
15///< @unofficial
16enum CC_STATUS_FLAG_e {
17 CC_STATUS_NONE = 0,
18 CC_STATUS_NO_REVISION = BIT_FLAG(0), ///< Don't set the collision offset if a collision occurs
19 /**
20 * When another collider collides with this one,
21 * don't update the result or execute the callback
22 * on the other collider.
23 */
24 CC_STATUS_NO_PASS_INFO = BIT_FLAG(2),
25 CC_STATUS_8 = BIT_FLAG(8),
26 CC_STATUS_9 = BIT_FLAG(9),
27};
28
29///< @unofficial
30enum CC_INFO_e {
31 CC_NO_HIT = BIT_FLAG(1), ///< Disables all collisions with this collider
32};
33
34///< @unofficial
35enum CC_KIND_e {
36 CC_KIND_PLAYER,
37 /**
38 * This collider can attack, which means it will use mAttackCategory and
39 * mAttackCategoryInteract to further check if the colliders will collide.
40 */
41 CC_KIND_PLAYER_ATTACK,
42 CC_KIND_YOSHI,
43 CC_KIND_ENEMY,
44 CC_KIND_BALLOON,
45 CC_KIND_ITEM,
46 CC_KIND_TAMA,
47 CC_KIND_KILLER,
48 CC_KIND_GOAL_POLE,
49 CC_KIND_COUNT = CC_KIND_GOAL_POLE // Goal pole is special and doesn't count
50};
51
52///< @unofficial
53enum CC_ATTACK_e {
54 CC_ATTACK_NONE,
55 CC_ATTACK_FIREBALL,
56 CC_ATTACK_ICEBALL,
57 CC_ATTACK_STAR,
58 CC_ATTACK_ICE_BREAK,
59 CC_ATTACK_SLIP,
60 CC_ATTACK_KOOPA_FIRE,
61 CC_ATTACK_HIP_ATTACK,
62 CC_ATTACK_WIRE_NET,
63 CC_ATTACK_SHELL,
64 CC_ATTACK_PENGUIN_SLIDE,
65 CC_ATTACK_SPIN,
66 CC_ATTACK_UNK12,
67 CC_ATTACK_SPIN_FALL,
68 CC_ATTACK_FIRE_2,
69 CC_ATTACK_YOSHI_EAT,
70 CC_ATTACK_YOSHI_MOUTH,
71 CC_ATTACK_CANNON,
72 CC_ATTACK_SPIN_LIFT_UP,
73 CC_ATTACK_YOSHI_BULLET,
74 CC_ATTACK_YOSHI_FIRE,
75 CC_ATTACK_ICE_2,
76 CC_ATTACK_SAND_PILLAR,
77};
78
79class dCc_c;
80
81/// @unofficial
82struct sCcDatNew {
83 mVec2_POD_c mOffset; ///< The offset of the collider.
84
85 ///< @brief The size of the collider.
86 ///< Note: This is the distance from the center to the edge, so half the actual size.
87 mVec2_POD_c mSize;
88
89 void set(const sCcDatNew &other) {
90 mOffset = other.mOffset;
91 mSize = other.mSize;
92 }
93};
94
95/**
96* @brief A structure that contains information about a collider.
97* @unofficial
98*/
99struct sCcDatNewF {
100 sCcDatNew mBase; ///< Base collider data.
101
102 u8 mKind; ///< The type of this collider. See CC_KIND_e.
103 u8 mAttack; ///< The attack type of this collider. See CC_ATTACK_e.
104
105 ///< @brief Which types this collider should be able to collide with.
106 ///< This is a bitfield with the bits enumerated by CC_KIND_e.
108
109 ///< @brief Which attack types this collider should be able to receive.
110 ///< This is a bitfield with the bits enumerated by CC_ATTACK_e.
111 u32 mVsDamage;
112
113 u16 mStatus; ///< Status flags for this collider. See CC_STATUS_FLAG_e.
114
115 void (*mCallback)(dCc_c *self, dCc_c *target); ///< The callback to execute when a collision occurs.
116
117 void set(const sCcDatNewF &other) {
118 mBase.set(other.mBase);
119 mKind = other.mKind;
120 mAttack = other.mAttack;
121 mVsKind = other.mVsKind;
122 mVsDamage = other.mVsDamage;
123 mStatus = other.mStatus;
124 mCallback = other.mCallback;
125 }
126};
127
128/**
129 * @brief Collider ("Collision Check") class - handles collisions between actors.
130 *
131 * It also includes logic that handles collisions
132 * within a horizontally looping stage (like 2-C).
133 */
134class dCc_c {
135public:
136 ///< @unofficial
137 class U32Holder {
138 public:
139 U32Holder(u32 value) : mValue(value) {}
140
141 u32 mValue;
142 };
143
144 ///< @unofficial
146 u32 a;
147 U32Holder b;
148 U32Holder c;
149 bool d, e;
150 };
151
152public:
153 dCc_c(); ///< Constructs a new collider.
154 virtual ~dCc_c(); ///< Destroys the collider.
155
156 void clear(); ///< Clear the data related to previous collisions.
157
158 void entry(); ///< Places this collider in the collider list.
159 void release(); ///< Removes this collider from the collider list.
160
161 /**
162 * @brief Registers an owner actor to this collider and sets the collider data.
163 * @param actor The actor to register.
164 * @param collInfo The collider data to set.
165 */
166 void set(dActor_c *actor, sCcDatNewF *collInfo);
167
168 /**
169 * @brief Registers an owner actor to this collider and sets the collider data.
170 * @param actor The actor to register.
171 * @param collInfo The collider data to set.
172 * @param amiLine The chainline fence layer to set.
173 */
174 void set(dActor_c *actor, sCcDatNewF *collInfo, u8 amiLine);
175
176 /// Sets a friend actor for this collider.
177 void setFriendActor(dActor_c *actor) { mpFriendActor = actor; }
178
179 dActor_c *getOwner() const { return mpOwner; } ///< Gets the owner actor of this collider.
180
181 /**
182 * @brief Gets the result of a hit check.
183 * @param mask The mask to check.
184 * @return The result of the hit check.
185 */
186 u16 isHit(u16 mask) const;
187
188 /**
189 * @brief Gets the result of an attack hit check.
190 * @param mask The mask to check.
191 * @return The result of the attack hit check.
192 */
193 u16 isHitAtDmg(u16 mask) const;
194
195 float getTopPos(); ///< Gets the Y position of the top of the collider.
196 float getUnderPos(); ///< Gets the Y position of the bottom of the collider.
197 float getCenterPosY(); ///< Gets the Y position of the center of the collider.
198
199 float getRightPos(); ///< Gets the X position of the right side of the collider.
200 float getLeftPos(); ///< Gets the X position of the left side of the collider.
201 float getCenterPosX(); ///< Gets the X position of the center of the collider.
202
203 /// Gets the center of the collider as a vector.
206 }
207
208 /**
209 * @brief Checks if this collider is inside another collider.
210 *
211 * @param other The collider to check against.
212 */
213 bool isInside(dCc_c *other);
214
215private:
216 float getTrpOffset(int idx) {
217 return mTrpOffsets[idx];
218 }
219
220public:
221 /**
222 * @brief Checks for collisions between two colliders.
223 *
224 * @param c1 The first collider.
225 * @param c2 The second collider.
226 * @param active Whether to update the result and execute the callback if a collision occurs.
227 * @return Whether the first collider collided with the second collider.
228 */
229 static bool checkCollision(dCc_c *c1, dCc_c *c2, int active);
230
231 static void execute(); ///< Check all colliders against each other for collisions.
232
233 /**
234 * @brief Clears the collider list.
235 *
236 * It also sets the hit check to the correct type (normal / looping stage).
237 * Note that this does not clean up the colliders themselves!
238 */
239 static void reset();
240
241 bool isLinked() const { return mIsLinked; }
242 float getCollPosX() const { return mCollPos.x; }
243 float getCollPosY() const { return mCollPos.y; }
244
245private:
246 /**
247 * @brief A hit check function for rectangular colliders. Used in _hitCheckNormal and _hitCheckLoop.
248 * @param c1 The first collider.
249 * @param c2 The second collider.
250 * @param pos1 The position of the first collider.
251 * @param pos2 The position of the second collider.
252 */
253 static bool _hitCheckSquare(dCc_c *c1, dCc_c *c2, mVec2_c pos1, mVec2_c pos2);
254
255 /// Check two rectangular colliders against each other for collisions without stage looping.
256 static bool _hitCheckNormal(dCc_c *c1, dCc_c *c2);
257 /// Check two rectangular colliders against each other for collisions with stage looping.
258 static bool _hitCheckLoop(dCc_c *c1, dCc_c *c2);
259 /// Check two circle colliders against each other for collisions.
260 static bool _hitCheckCircle(dCc_c *c1, dCc_c *c2);
261 /// Check a rectangular and a circle collider against each other for collisions.
262 static bool _hitCheckBoxCircle(dCc_c *c1, dCc_c *c2);
263
264 static int _lineCheckUD(mVec2_c p1, mVec2_c p2, float p3, float p4);
265 /// Check a rectangular collider against a trapezoid-shaped collider for collisions.
266 static bool _hitCheckDaikeiUD_R(dCc_c *ccBox, dCc_c *ccTrp);
267 /// Check a trapezoid-shaped collider against a rectangular collider for collisions.
268 static bool _hitCheckDaikeiUD(dCc_c *ccTrp, dCc_c *ccBox);
269
270 static int _lineCheckLR(mVec2_c p1, mVec2_c p2, float p3, float p4);
271 /// Check a rectangular collider against a trapezoid-shaped collider for collisions.
272 static bool _hitCheckDaikeiLR_R(dCc_c *ccBox, dCc_c *ccTrp);
273 /// Check a trapezoid-shaped collider against a rectangular collider for collisions.
274 static bool _hitCheckDaikeiLR(dCc_c *ccTrp, dCc_c *ccBox);
275
276public:
277 float getXOffset(int idx) { return mCollOffsetX[idx]; }
278 float getYOffset(int idx) { return mCollOffsetY[idx]; }
279
280 dActor_c *mpOwner; ///< The actor this collider belongs to.
281 dActor_c *mpFriendActor; ///< A second actor that this collider will not collide with.
282
283 u32 unk2; ///< [Unused (?)].
284
285 dCc_c *mpNext; ///< The next collider in the list.
286 dCc_c *mpPrev; ///< The previous collider in the list.
287
288 u32 mCanBounce; ///< [used by Giant Wigglers to allow jumping].
289
290 sCcDatNewF mCcData; ///< The collision data of this collider.
291
292 /**
293 * @brief The X or Y offset of the four corners of a trapezoid-shaped collider.
294 *
295 * Relative to the center of the collider.
296 * If mShape is CC_SHAPE_DAIKEI_UD, this is the Y offset.
297 * If mShape is CC_SHAPE_DAIKEI_LR, this is the X offset.
298 */
299 float mTrpOffsets[4];
300
301 /**
302 * @brief The X offset for a collision.
303 *
304 * One entry per kind. Each entry describes by how much the collider must be
305 * offset in the X direction in order to not collide with the other collider.
306 */
307 float mCollOffsetX[CC_KIND_COUNT];
308 /**
309 * @brief The Y offset for a collision.
310 *
311 * One entry per kind. Each entry describes by how much the collider must be
312 * offset in the Y direction in order to not collide with the other collider.
313 */
314 float mCollOffsetY[CC_KIND_COUNT];
315
316 mVec2_c mCollPos; ///< The position where the last collision occurred.
317
318 u16 mCollidedWith; ///< The categories of the previously collided with colliders.
319 u16 mAttSent; ///< The attack types sent by this collider in the previous collisions.
320 u16 mAttReceived; ///< The attack types received by this collider in the previous collisions.
321
322 u8 mShape; ///< The shape of the collider. See @ref CC_SHAPE_e .
323
324 /**
325 * @brief The non-collide mask for this collider.
326 *
327 * If the same bit is set in a second actor's non-collide mask,
328 * the two actors will not collide.
329 */
331
332 /**
333 * @brief The layer this collider is on.
334 *
335 * Colliders can only collide with other colliders on the same layer.
336 */
338
339 u8 mInfo; ///< Info flags for this collider. See CC_INFO_e.
340
341private:
342 bool mIsLinked; ///< Whether this collider has been placed in the collider list.
343
344private:
345 typedef bool (*hitCheck)(dCc_c *, dCc_c *);
346
347 /**
348 * @brief The hit check function for each combination of collider shapes.
349 *
350 * The first index is the shape of the first collider and the second index
351 * is the shape of the second collider.
352 */
353 static hitCheck _hitCheck[4][4];
354
355 static dCc_c *mEntryN; ///< The first collider in the list.
356 static dCc_c *mEntryB; ///< The last collider in the list.
357};
The minimum required implementation for a stage actor.
Definition d_actor.hpp:18
Collider ("Collision Check") class - handles collisions between actors.
Definition d_cc.hpp:134
void set(dActor_c *actor, sCcDatNewF *collInfo)
Registers an owner actor to this collider and sets the collider data.
Definition d_cc.cpp:91
mVec2_c getCenterVec()
Gets the center of the collider as a vector.
Definition d_cc.hpp:204
static bool _hitCheckSquare(dCc_c *c1, dCc_c *c2, mVec2_c pos1, mVec2_c pos2)
A hit check function for rectangular colliders. Used in _hitCheckNormal and _hitCheckLoop.
Definition d_cc.cpp:272
bool mIsLinked
Whether this collider has been placed in the collider list.
Definition d_cc.hpp:342
static bool checkCollision(dCc_c *c1, dCc_c *c2, int active)
Checks for collisions between two colliders.
Definition d_cc.cpp:168
static bool _hitCheckDaikeiUD_R(dCc_c *ccBox, dCc_c *ccTrp)
Check a rectangular collider against a trapezoid-shaped collider for collisions.
Definition d_cc.cpp:508
dActor_c * mpOwner
The actor this collider belongs to.
Definition d_cc.hpp:280
float getUnderPos()
Gets the Y position of the bottom of the collider.
Definition d_cc.cpp:128
static bool _hitCheckLoop(dCc_c *c1, dCc_c *c2)
Check two rectangular colliders against each other for collisions with stage looping.
Definition d_cc.cpp:321
float getTopPos()
Gets the Y position of the top of the collider.
Definition d_cc.cpp:124
static dCc_c * mEntryN
The first collider in the list.
Definition d_cc.hpp:355
u16 isHitAtDmg(u16 mask) const
Gets the result of an attack hit check.
Definition d_cc.cpp:120
u8 mInfo
Info flags for this collider. See CC_INFO_e.
Definition d_cc.hpp:339
float mTrpOffsets[4]
The X or Y offset of the four corners of a trapezoid-shaped collider.
Definition d_cc.hpp:299
dCc_c * mpPrev
The previous collider in the list.
Definition d_cc.hpp:286
void clear()
Clear the data related to previous collisions.
Definition d_cc.cpp:41
float getCenterPosY()
Gets the Y position of the center of the collider.
Definition d_cc.cpp:132
void entry()
Places this collider in the collider list.
Definition d_cc.cpp:52
u16 mCollidedWith
The categories of the previously collided with colliders.
Definition d_cc.hpp:318
float mCollOffsetY[CC_KIND_COUNT]
The Y offset for a collision.
Definition d_cc.hpp:314
u8 mShape
The shape of the collider. See CC_SHAPE_e .
Definition d_cc.hpp:322
u16 mAttSent
The attack types sent by this collider in the previous collisions.
Definition d_cc.hpp:319
u32 unk2
[Unused (?)].
Definition d_cc.hpp:283
u16 isHit(u16 mask) const
Gets the result of a hit check.
Definition d_cc.cpp:116
static hitCheck _hitCheck[4][4]
The hit check function for each combination of collider shapes.
Definition d_cc.hpp:20
u16 mAttReceived
The attack types received by this collider in the previous collisions.
Definition d_cc.hpp:320
u8 mLayer
The layer this collider is on.
Definition d_cc.hpp:337
float getCenterPosX()
Gets the X position of the center of the collider.
Definition d_cc.cpp:144
static bool _hitCheckCircle(dCc_c *c1, dCc_c *c2)
Check two circle colliders against each other for collisions.
Definition d_cc.cpp:345
float getLeftPos()
Gets the X position of the left side of the collider.
Definition d_cc.cpp:140
mVec2_c mCollPos
The position where the last collision occurred.
Definition d_cc.hpp:316
float mCollOffsetX[CC_KIND_COUNT]
The X offset for a collision.
Definition d_cc.hpp:307
float getRightPos()
Gets the X position of the right side of the collider.
Definition d_cc.cpp:136
static bool _hitCheckDaikeiLR_R(dCc_c *ccBox, dCc_c *ccTrp)
Check a rectangular collider against a trapezoid-shaped collider for collisions.
Definition d_cc.cpp:580
virtual ~dCc_c()
Destroys the collider.
Definition d_cc.cpp:37
static void execute()
Check all colliders against each other for collisions.
Definition d_cc.cpp:264
dActor_c * getOwner() const
Gets the owner actor of this collider.
Definition d_cc.hpp:179
void release()
Removes this collider from the collider list.
Definition d_cc.cpp:70
u32 mCanBounce
[used by Giant Wigglers to allow jumping].
Definition d_cc.hpp:288
sCcDatNewF mCcData
The collision data of this collider.
Definition d_cc.hpp:290
static bool _hitCheckDaikeiLR(dCc_c *ccTrp, dCc_c *ccBox)
Check a trapezoid-shaped collider against a rectangular collider for collisions.
Definition d_cc.cpp:584
u8 mAmiLine
The non-collide mask for this collider.
Definition d_cc.hpp:330
void setFriendActor(dActor_c *actor)
Sets a friend actor for this collider.
Definition d_cc.hpp:177
static bool _hitCheckBoxCircle(dCc_c *c1, dCc_c *c2)
Check a rectangular and a circle collider against each other for collisions.
Definition d_cc.cpp:391
static void reset()
Clears the collider list.
Definition d_cc.cpp:102
dCc_c * mpNext
The next collider in the list.
Definition d_cc.hpp:285
dCc_c()
Constructs a new collider.
Definition d_cc.cpp:27
static dCc_c * mEntryB
The last collider in the list.
Definition d_cc.hpp:356
static bool _hitCheckNormal(dCc_c *c1, dCc_c *c2)
Check two rectangular colliders against each other for collisions without stage looping.
Definition d_cc.cpp:315
static bool _hitCheckDaikeiUD(dCc_c *ccTrp, dCc_c *ccBox)
Check a trapezoid-shaped collider against a rectangular collider for collisions.
Definition d_cc.cpp:512
dActor_c * mpFriendActor
A second actor that this collider will not collide with.
Definition d_cc.hpp:281
bool isInside(dCc_c *other)
Checks if this collider is inside another collider.
Definition d_cc.cpp:148
A two-dimensional floating point vector.
Definition m_vec.hpp:26
A structure that contains information about a collider.
Definition d_cc.hpp:99
void(* mCallback)(dCc_c *self, dCc_c *target)
The callback to execute when a collision occurs.
Definition d_cc.hpp:115
u8 mKind
The type of this collider. See CC_KIND_e.
Definition d_cc.hpp:102
u8 mAttack
The attack type of this collider. See CC_ATTACK_e.
Definition d_cc.hpp:103
sCcDatNew mBase
Base collider data.
Definition d_cc.hpp:100
u16 mStatus
Status flags for this collider. See CC_STATUS_FLAG_e.
Definition d_cc.hpp:113
u32 mVsKind
Which attack types this collider should be able to receive. This is a bitfield with the bits enumerat...
Definition d_cc.hpp:107
mVec2_POD_c mOffset
The offset of the collider.
Definition d_cc.hpp:83