diff --git a/data/uking_functions.csv b/data/uking_functions.csv index fd6b1742..7d5a618a 100644 --- a/data/uking_functions.csv +++ b/data/uking_functions.csv @@ -628,7 +628,7 @@ 0x000000710001b4d0,sub_710001B4D0,100, 0x000000710001b534,sub_710001B534,104, 0x000000710001b59c,sub_710001B59C,796, -0x000000710001b8b8,vfr::updateTimer,412,_ZN4ksys8VFRValue9lerpValueEPfRKffff +0x000000710001b8b8,vfr::updateTimer,412,_ZN4ksys3VFR4lerpIfEEbPT_RKS2_fff 0x000000710001ba54,sub_710001BA54,60, 0x000000710001ba90,sub_710001BA90,60, 0x000000710001bacc,sub_710001BACC,288, diff --git a/src/KingSystem/System/VFR.h b/src/KingSystem/System/VFR.h index 97562e87..10a8bcaa 100644 --- a/src/KingSystem/System/VFR.h +++ b/src/KingSystem/System/VFR.h @@ -1,9 +1,11 @@ #pragma once #include +#include #include #include #include +#include #include #include #include "KingSystem/Utils/Types.h" @@ -69,6 +71,61 @@ public: f32 getDeltaTime(u32 core) const { return *mDeltaFrames[core]; } f32 getDeltaTime() const { return getDeltaTime(sead::CoreInfo::getCurrentCoreId()); } + template + static inline void add(T* value, const T& v) { + *value += v * instance()->getDeltaTime(); + } + + template + static inline void multiply(T* value, f32 scalar) { + *value *= std::pow(scalar, instance()->getDeltaTime()); + } + + static inline f32 getLerpFactor(f32 t) { + return 1.0f - std::pow(1.0f - t, instance()->getDeltaTime()); + } + + template + static inline void lerp(T* value, const T& b, f32 t) { + *value += getLerpFactor(t) * (b - *value); + } + + template + static inline void lerp(T* value, const T& b, f32 t, f32 max_delta) { + const auto f = getLerpFactor(t); + const auto max_d = instance()->getDeltaTime() * max_delta; + const auto diff = b - *value; + const auto d = f * sead::absf(diff); + if (d > max_d) + *value += diff < 0.0 ? -max_d : max_d; + else + *value += f * diff; + } + + template + static inline bool lerp(T* value, const T& b, f32 t, f32 max_delta, f32 min_delta) { + const auto f = getLerpFactor(t); + const auto max_d = instance()->getDeltaTime() * max_delta; + const auto min_d = instance()->getDeltaTime() * min_delta; + + const auto diff = b - *value; + const auto d = f * sead::absf(diff); + + if (sead::absf(diff) <= min_d) { + *value = b; + return true; + } + + if (d > max_d) { + *value += diff < 0.0 ? -max_d : max_d; + } else if (d < min_d) { + *value += diff < 0.0 ? -min_d : min_d; + } else { + *value = *value + f * diff; + } + return false; + } + private: struct TimeSpeedMultipliers : sead::Buffer { TimeSpeedMultipliers() { diff --git a/src/KingSystem/System/VFRValue.cpp b/src/KingSystem/System/VFRValue.cpp index a49d4161..471f18a2 100644 --- a/src/KingSystem/System/VFRValue.cpp +++ b/src/KingSystem/System/VFRValue.cpp @@ -11,10 +11,6 @@ namespace { util::InitTimeInfoEx sInitInfo; -f32 getLerpFactor(f32 t) { - return 1.0f - std::pow(1.0f - t, VFR::instance()->getDeltaTime()); -} - template void updateStatsImpl(const T& value, T* prev_value, T* mean) { const T new_mean = ((*prev_value + value) / 2) * VFR::instance()->getDeltaTime(); @@ -22,57 +18,6 @@ void updateStatsImpl(const T& value, T* prev_value, T* mean) { *mean = new_mean; } -template -void addImpl(T* value, const T& v) { - *value += v * VFR::instance()->getDeltaTime(); -} - -template -void multiplyImpl(T* value, f32 scalar) { - *value *= std::pow(scalar, VFR::instance()->getDeltaTime()); -} - -template -void lerpImpl(T* value, const T& b, f32 t) { - *value += getLerpFactor(t) * (b - *value); -} - -template -void lerpImpl(T* value, const T& b, f32 t, f32 max_delta) { - const auto f = getLerpFactor(t); - const auto max_d = VFR::instance()->getDeltaTime() * max_delta; - const auto diff = b - *value; - const auto d = f * sead::absf(diff); - if (d > max_d) - *value += diff < 0.0 ? -max_d : max_d; - else - *value += f * diff; -} - -template -bool lerpImpl(T* value, const T& b, f32 t, f32 max_delta, f32 min_delta) { - const auto f = getLerpFactor(t); - const auto max_d = VFR::instance()->getDeltaTime() * max_delta; - const auto min_d = VFR::instance()->getDeltaTime() * min_delta; - - const auto diff = b - *value; - const auto d = f * sead::absf(diff); - - if (sead::absf(diff) <= min_d) { - *value = b; - return true; - } - - if (d > max_d) { - *value += diff < 0.0 ? -max_d : max_d; - } else if (d < min_d) { - *value += diff < 0.0 ? -min_d : min_d; - } else { - *value += f * diff; - } - return false; -} - } // namespace VFRValue::VFRValue() = default; @@ -84,27 +29,23 @@ void VFRValue::updateStats() { } void VFRValue::operator+=(const f32& rhs) { - addImpl(&value, rhs); + VFR::add(&value, rhs); } void VFRValue::operator*=(f32 scalar) { - multiplyImpl(&value, scalar); + VFR::multiply(&value, scalar); } void VFRValue::lerp(const f32& b, f32 t) { - lerpImpl(&value, b, t); + VFR::lerp(&value, b, t); } void VFRValue::lerp(const f32& b, f32 t, f32 max_delta) { - lerpImpl(&value, b, t, max_delta); + VFR::lerp(&value, b, t, max_delta); } bool VFRValue::lerp(const f32& b, f32 t, f32 max_delta, f32 min_delta) { - return lerpValue(&value, b, t, max_delta, min_delta); -} - -bool VFRValue::lerpValue(f32* value, const f32& b, f32 t, f32 max_delta, f32 min_delta) { - return lerpImpl(value, b, t, max_delta, min_delta); + return VFR::lerp(&value, b, t, max_delta, min_delta); } bool VFRValue::chase(const f32& target, f32 step) { @@ -122,11 +63,11 @@ void VFRVec3f::updateStats() { } void VFRVec3f::operator*=(f32 scalar) { - multiplyImpl(&value, scalar); + VFR::multiply(&value, scalar); } void VFRVec3f::lerp(const sead::Vector3f& b, f32 t) { - lerpImpl(&value, b, t); + VFR::lerp(&value, b, t); } bool VFRVec3f::chase(const sead::Vector3f& target, f32 t) { diff --git a/src/KingSystem/System/VFRValue.h b/src/KingSystem/System/VFRValue.h index 34acbdcc..ff7a5a41 100644 --- a/src/KingSystem/System/VFRValue.h +++ b/src/KingSystem/System/VFRValue.h @@ -16,7 +16,6 @@ struct VFRValue { void lerp(const f32& b, f32 t); void lerp(const f32& b, f32 t, f32 max_delta); bool lerp(const f32& b, f32 t, f32 max_delta, f32 min_delta); - static bool lerpValue(f32* value, const f32& b, f32 t, f32 max_delta, f32 min_delta); bool chase(const f32& target, f32 step); void setToMax(const f32& max); void setToMin(const f32& min);