diff --git a/include/nlib/Geometry.h b/include/nlib/Geometry.h index 2c1c4cc4..017c1bfe 100644 --- a/include/nlib/Geometry.h +++ b/include/nlib/Geometry.h @@ -1,10 +1,11 @@ #ifndef _NLIB_GEOMETRY_H #define _NLIB_GEOMETRY_H -#include "types.h" #include "Dolphin/mtx.h" -#include "Vector.h" +#include "nlib/Math.h" #include "Matrix4f.h" +#include "Vector.h" +#include "types.h" struct Stream; struct NTransform3D; @@ -359,6 +360,16 @@ struct NTransform3D { NMatrix4f mMtx; // _00 }; +struct NAlphaMode { + enum _ { + Linear = 0, + FadeOut = 1, + FadeIn = 2, + FadeInOut = 3, + FadeOutIn = 4, + }; +}; + /** * @brief TODO */ @@ -370,17 +381,17 @@ struct NAlpha { void reset(); f32 getValue(f32); - // unused/inlined: - void fadeInValue(f32); - void fadeOutValue(f32); - void fadeInOutValue(f32); - void fadeOutInValue(f32); + // inlined + f32 fadeInValue(f32 x); + f32 fadeOutValue(f32 x); + f32 fadeInOutValue(f32 x); + f32 fadeOutInValue(f32 x); // _00 = VTBL - f32 _04; // _04 - f32 _08; // _08 - f32 _0C; // _0C - u8 _10; // _10 + f32 mValue; // _04 + f32 mOffset; // _08 + f32 mScale; // _0C, actually named 'period' + u8 mMode; // _10 }; #endif diff --git a/include/nlib/Math.h b/include/nlib/Math.h index bab15e46..ca10db20 100644 --- a/include/nlib/Math.h +++ b/include/nlib/Math.h @@ -15,11 +15,9 @@ f32 remainder(f32, f32); // unused/inlined: f32 roundAngle(f32); - inline f32 atan2Vec(Vector3f vec) { return atan2(vec.x, vec.z); } extern f32 pi; - inline f32 getRandomAngle() { return 2.0f * StdSystem::getRand(1.0f) * pi; } extern f32 error; @@ -54,6 +52,8 @@ struct NMath { } return val; } + + static bool isZero(f32 period) { return absolute(period) <= NMathF::error; } }; typedef NMath NMathf; diff --git a/include/types.h b/include/types.h index 3ba4e797..d047eba8 100644 --- a/include/types.h +++ b/include/types.h @@ -34,6 +34,17 @@ typedef volatile f128 vf128; typedef u32 unknown; +#ifdef __MWERKS__ +#define PRINT(...) +#define ERROR(...) +#else +#define PRINT(...) _Print(__VA_ARGS__) +#define ERROR(...) _Error(__VA_ARGS__) +#endif + +#define WINDOWS_ONLY_START (#ifdef _WIN32) +#define WINDOWS_ONLY_END (#endif) + #ifndef __cplusplus typedef u16 wchar_t; #endif diff --git a/src/plugPikiKando/interactBattle.cpp b/src/plugPikiKando/interactBattle.cpp index 7a95eef4..1f241033 100644 --- a/src/plugPikiKando/interactBattle.cpp +++ b/src/plugPikiKando/interactBattle.cpp @@ -482,18 +482,21 @@ bool InteractSwallow::actPiki(Piki* piki) return false; } - bool check = false; + bool isPiki = false; Stickers stickers(mOwner); Stickers* stickPtr = &stickers; for (int i = stickPtr->getFirst(); !stickPtr->isEnd(i); i = stickPtr->getNext(i)) { Creature* stuck; + + // On invalid index, get the first creature if (i == -1) { stuck = stickPtr->getCreature(0); } else { stuck = stickPtr->getCreature(i); } + if (stuck == piki) { - check = true; + isPiki = true; } if (stuck->isCreatureFlag(CF_StuckToMouth)) { @@ -501,7 +504,7 @@ bool InteractSwallow::actPiki(Piki* piki) } } - if (!check) { + if (!isPiki) { Creature* target = mOwner->_180; while (target) { target = target->_18C; diff --git a/src/plugPikiNakata/nlibgeometry.cpp b/src/plugPikiNakata/nlibgeometry.cpp index d6556658..aed01e94 100644 --- a/src/plugPikiNakata/nlibgeometry.cpp +++ b/src/plugPikiNakata/nlibgeometry.cpp @@ -2255,7 +2255,7 @@ void NVector3f::normalizeByLength(f32 length) bool NVector3f::normalizeCheck() { f32 len = length(); - if (NMathf::absolute(len) <= NMathF::error) { + if (NMathf::isZero(len)) { return false; } @@ -2270,13 +2270,16 @@ bool NVector3f::normalizeCheck() */ void NVector3f::normalize() { - u32 badCompiler; // idek man. f32 len = length(); - if (NMathf::absolute(len) <= NMathF::error) { + if (NMathf::isZero(len)) { + f32 lenCopy = len; + PRINT("!normalize:zero:%f\n", lenCopy); return; } - normalizeByLength(len); + x /= len; + y /= len; + z /= len; } /* @@ -2284,9 +2287,10 @@ void NVector3f::normalize() * Address: ........ * Size: 00003C */ -void NAlpha::fadeInValue(f32) +f32 NAlpha::fadeInValue(f32 x) { - // UNUSED FUNCTION + f32 hpi = NMathF::pi / 2; + return sinf(x * hpi); } /* @@ -2294,9 +2298,10 @@ void NAlpha::fadeInValue(f32) * Address: ........ * Size: 000030 */ -void NAlpha::fadeOutValue(f32) +f32 NAlpha::fadeOutValue(f32 x) { - // UNUSED FUNCTION + f32 hpi = NMathF::pi / 2; + return 1.0f - sinf((x * hpi) + hpi); } /* @@ -2304,9 +2309,15 @@ void NAlpha::fadeOutValue(f32) * Address: ........ * Size: 000080 */ -void NAlpha::fadeInOutValue(f32) +f32 NAlpha::fadeInOutValue(f32 x) { - // UNUSED FUNCTION + if (x < 0.5f) { + f32 function = (sinf((NMathF::pi / 2) + (2.0f * x) * (NMathF::pi / 2))); + return 0.5f * (1.0f - function); + } + + f32 function = (sinf((2.0f * (x - 0.5f)) * (NMathF::pi / 2))); + return 0.5f + (0.5f * function); } /* @@ -2314,9 +2325,13 @@ void NAlpha::fadeInOutValue(f32) * Address: ........ * Size: 000080 */ -void NAlpha::fadeOutInValue(f32) +f32 NAlpha::fadeOutInValue(f32 x) { - // UNUSED FUNCTION + if (x < 0.5f) { + return (0.5f * sinf((2.0f * x) * (NMathF::pi / 2))); + } + + return 0.5f + 0.5f * (1.0f - sinf((NMathF::pi / 2) + (2.0f * (x - 0.5f)) * (NMathF::pi / 2))); } /* @@ -2334,168 +2349,38 @@ NAlpha::NAlpha() * Address: 8011D1DC * Size: 00000C */ -void NAlpha::reset() { _04 = _08; } +void NAlpha::reset() { mValue = mOffset; } /* * --INFO-- - * Address: 8011D1E8 + * Address: 8011D1E8 (1004BE90 in plugPiki) * Size: 000198 */ -f32 NAlpha::getValue(f32 p1) +f32 NAlpha::getValue(f32 input) { - if (NMathf::absolute(_0C) <= NMathF::error) { - _0C = 1.0f; + // If the scale is very small, set it to 1.0f + if (NMathf::isZero(mScale)) { + PRINT("?isZero(period)\n"); + mScale = 1.0f; } - f32 val = (p1 - _08) / _0C; - - switch (_10) { - case 0: - return val; - case 1: - return sinf(NMathF::pi + val * (NMathF::pi / 2)) - 1.0f; - case 2: - return sinf(val * (NMathF::pi / 2)); - case 3: - if (val < 0.5f) { - return 0.5f * (1.0f - sinf((NMathF::pi / 2) + (2.0f * val) * (NMathF::pi / 2))); - } - return 0.5f + (0.5f * sinf((2.0f * (val - 0.5f)) * (NMathF::pi / 2))); - case 4: - if (val < 0.5f) { - return (0.5f * sinf((2.0f * val) * (NMathF::pi / 2))); - } - return 0.5f + 0.5f * (1.0f - sinf((NMathF::pi / 2) + (2.0f * (val - 0.5f)) * (NMathF::pi / 2))); + // Normalise the input value to the range [mOffset, mOffset + mScale] + f32 normalisedValue = (input - mOffset) / mScale; + + switch (mMode) { + case NAlphaMode::Linear: + return normalisedValue; + case NAlphaMode::FadeOut: + return fadeOutValue(normalisedValue); + case NAlphaMode::FadeIn: + return fadeInValue(normalisedValue); + case NAlphaMode::FadeInOut: + return fadeInOutValue(normalisedValue); + case NAlphaMode::FadeOutIn: + return fadeOutInValue(normalisedValue); default: - return val; + return normalisedValue; } - /* - .loc_0x0: - mflr r0 - stw r0, 0x4(r1) - stwu r1, -0x8(r1) - lfs f2, 0xC(r3) - lfs f0, -0x5FF0(r2) - fcmpo cr0, f2, f0 - ble- .loc_0x20 - b .loc_0x24 - - .loc_0x20: - fneg f2, f2 - - .loc_0x24: - lfs f0, -0x1CB0(r13) - fcmpo cr0, f2, f0 - cror 2, 0, 0x2 - bne- .loc_0x3C - lfs f0, -0x5FEC(r2) - stfs f0, 0xC(r3) - - .loc_0x3C: - lfs f0, 0x8(r3) - lbz r0, 0x10(r3) - fsubs f1, f1, f0 - lfs f0, 0xC(r3) - cmpwi r0, 0x2 - fdivs f1, f1, f0 - beq- .loc_0xA4 - bge- .loc_0x6C - cmpwi r0, 0 - beq- .loc_0x188 - bge- .loc_0x80 - b .loc_0x188 - - .loc_0x6C: - cmpwi r0, 0x4 - beq- .loc_0x124 - bge- .loc_0x188 - b .loc_0xBC - b .loc_0x188 - - .loc_0x80: - lfs f2, -0x1CA4(r13) - lfs f0, -0x5FE8(r2) - fmuls f2, f2, f0 - fmuls f0, f1, f2 - fadds f1, f2, f0 - bl 0xFEA6C - lfs f0, -0x5FEC(r2) - fsubs f1, f0, f1 - b .loc_0x188 - - .loc_0xA4: - lfs f2, -0x1CA4(r13) - lfs f0, -0x5FE8(r2) - fmuls f0, f2, f0 - fmuls f1, f1, f0 - bl 0xFEA4C - b .loc_0x188 - - .loc_0xBC: - lfs f3, -0x5FE8(r2) - fcmpo cr0, f1, f3 - bge- .loc_0xF8 - lfs f2, -0x1CA4(r13) - lfs f0, -0x5FD0(r2) - fmuls f2, f2, f3 - fmuls f0, f0, f1 - fmuls f0, f0, f2 - fadds f1, f2, f0 - bl 0xFEA20 - lfs f0, -0x5FEC(r2) - lfs f2, -0x5FE8(r2) - fsubs f0, f0, f1 - fmuls f1, f2, f0 - b .loc_0x188 - - .loc_0xF8: - fsubs f1, f1, f3 - lfs f2, -0x5FD0(r2) - lfs f0, -0x1CA4(r13) - fmuls f1, f2, f1 - fmuls f0, f0, f3 - fmuls f1, f1, f0 - bl 0xFE9F0 - lfs f2, -0x5FE8(r2) - fmuls f0, f2, f1 - fadds f1, f2, f0 - b .loc_0x188 - - .loc_0x124: - lfs f3, -0x5FE8(r2) - fcmpo cr0, f1, f3 - bge- .loc_0x154 - lfs f2, -0x5FD0(r2) - lfs f0, -0x1CA4(r13) - fmuls f1, f2, f1 - fmuls f0, f0, f3 - fmuls f1, f1, f0 - bl 0xFE9BC - lfs f0, -0x5FE8(r2) - fmuls f1, f0, f1 - b .loc_0x188 - - .loc_0x154: - fsubs f0, f1, f3 - lfs f2, -0x1CA4(r13) - lfs f1, -0x5FD0(r2) - fmuls f2, f2, f3 - fmuls f0, f1, f0 - fmuls f0, f0, f2 - fadds f1, f2, f0 - bl 0xFE990 - lfs f0, -0x5FEC(r2) - lfs f2, -0x5FE8(r2) - fsubs f0, f0, f1 - fmuls f0, f2, f0 - fadds f1, f2, f0 - - .loc_0x188: - lwz r0, 0xC(r1) - addi r1, r1, 0x8 - mtlr r0 - blr - */ } /* @@ -2505,7 +2390,7 @@ f32 NAlpha::getValue(f32 p1) */ void NAlpha::readData(Stream& input) { - _08 = input.readFloat(); - _0C = input.readFloat(); - _10 = input.readByte(); + mOffset = input.readFloat(); + mScale = input.readFloat(); + mMode = input.readByte(); }