diff --git a/data/uking_functions.csv b/data/uking_functions.csv index d6b10a7b..c9ea1f30 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, +0x000000710001b8b8,vfr::updateTimer,412,_ZN4ksys8VFRValue9lerpValueEPfRKffff 0x000000710001ba54,sub_710001BA54,60, 0x000000710001ba90,sub_710001BA90,60, 0x000000710001bacc,sub_710001BACC,288, @@ -91897,26 +91897,26 @@ 0x00000071011c1960,sub_71011C1960,200,_ZN4ksys3VFR17ScopedDeltaSetterD1Ev 0x00000071011c1a28,VFR::isField29Set,8,_ZNK4ksys3VFR19hasIntervalOverrideEv 0x00000071011c1a30,VFR::x,8,_ZNK4ksys3VFR19getIntervalOverrideEv -0x00000071011c1a38,sub_71011C1A38,44, -0x00000071011c1a64,sub_71011C1A64,12, -0x00000071011c1a70,sub_71011C1A70,28, -0x00000071011c1a8c,sub_71011C1A8C,140, -0x00000071011c1b18,sub_71011C1B18,128, -0x00000071011c1b98,sub_71011C1B98,132, -0x00000071011c1c1c,sub_71011C1C1C,164, -0x00000071011c1cc0,sub_71011C1CC0,292, -0x00000071011c1de4,j_vfr::updateTimer,4, -0x00000071011c1de8,sub_71011C1DE8,220, -0x00000071011c1ec4,sub_71011C1EC4,52, -0x00000071011c1ef8,sub_71011C1EF8,192, -0x00000071011c1fb8,sub_71011C1FB8,148, -0x00000071011c204c,sub_71011C204C,204, -0x00000071011c2118,sub_71011C2118,300, -0x00000071011c2244,sub_71011C2244,16, -0x00000071011c2254,sub_71011C2254,24, -0x00000071011c226c,sub_71011C226C,24, -0x00000071011c2284,sub_71011C2284,44, -0x00000071011c22b0,sub_71011C22B0,184, +0x00000071011c1a38,sub_71011C1A38,44,_GLOBAL__sub_I_VFRValue.cpp? +0x00000071011c1a64,sub_71011C1A64,12,_ZN4ksys8VFRValueC1Ev +0x00000071011c1a70,sub_71011C1A70,28,_ZN4ksys8VFRValueC1ERKf +0x00000071011c1a8c,sub_71011C1A8C,140,_ZN4ksys8VFRValue11updateStatsEv +0x00000071011c1b18,sub_71011C1B18,128,_ZN4ksys8VFRValuepLERKf +0x00000071011c1b98,sub_71011C1B98,132,_ZN4ksys8VFRValuemLEf +0x00000071011c1c1c,sub_71011C1C1C,164,_ZN4ksys8VFRValue4lerpERKff +0x00000071011c1cc0,sub_71011C1CC0,292,_ZN4ksys8VFRValue4lerpERKfff +0x00000071011c1de4,j_vfr::updateTimer,4,_ZN4ksys8VFRValue4lerpERKffff +0x00000071011c1de8,sub_71011C1DE8,220,_ZN4ksys8VFRValue5chaseERKff +0x00000071011c1ec4,sub_71011C1EC4,52,_ZN4ksys8VFRVec3fC1ERKN4sead7Vector3IfEE +0x00000071011c1ef8,sub_71011C1EF8,192,_ZN4ksys8VFRVec3f11updateStatsEv? +0x00000071011c1fb8,sub_71011C1FB8,148,_ZN4ksys8VFRVec3fmLEf +0x00000071011c204c,sub_71011C204C,204,_ZN4ksys8VFRVec3f4lerpERKN4sead7Vector3IfEEf +0x00000071011c2118,sub_71011C2118,300,_ZN4ksys8VFRVec3f5chaseERKN4sead7Vector3IfEEf +0x00000071011c2244,sub_71011C2244,16,_ZN4ksys8VFRVec3fC1Ev +0x00000071011c2254,sub_71011C2254,24,_ZN4ksys8VFRValue8setToMaxERKf +0x00000071011c226c,sub_71011C226C,24,_ZN4ksys8VFRValue8setToMinERKf +0x00000071011c2284,sub_71011C2284,44,_ZN4ksys8VFRValue5clampERKfS2_ +0x00000071011c22b0,sub_71011C22B0,184,_ZN4ksys8VFRVec3f9normalizeEf 0x00000071011c2368,AI_ActionBase::ctor,48,_ZN4ksys3act2ai6ActionC1ERKNS1_10ActionBase7InitArgE 0x00000071011c2398,AI_ActionBase::calcRecursively,12,_ZN4ksys3act2ai6Action4calcEv 0x00000071011c23a4,AI_ActionBase::playAS,288, diff --git a/expected/_ZN4ksys8VFRVec3f11updateStatsEv.bin b/expected/_ZN4ksys8VFRVec3f11updateStatsEv.bin new file mode 100644 index 00000000..ca4bb814 Binary files /dev/null and b/expected/_ZN4ksys8VFRVec3f11updateStatsEv.bin differ diff --git a/lib/sead b/lib/sead index bb92e59f..785bd9cb 160000 --- a/lib/sead +++ b/lib/sead @@ -1 +1 @@ -Subproject commit bb92e59f569bf8e865a9710b03015352c137cc29 +Subproject commit 785bd9cbc723960f10757bcf03a338f80812d1d9 diff --git a/src/KingSystem/System/CMakeLists.txt b/src/KingSystem/System/CMakeLists.txt index 851e291e..57fb9601 100644 --- a/src/KingSystem/System/CMakeLists.txt +++ b/src/KingSystem/System/CMakeLists.txt @@ -19,4 +19,6 @@ target_sources(uking PRIVATE Timer.h VFR.cpp VFR.h + VFRValue.cpp + VFRValue.h ) diff --git a/src/KingSystem/System/VFRValue.cpp b/src/KingSystem/System/VFRValue.cpp new file mode 100644 index 00000000..a49d4161 --- /dev/null +++ b/src/KingSystem/System/VFRValue.cpp @@ -0,0 +1,172 @@ +#include "KingSystem/System/VFRValue.h" +#include +#include +#include +#include "KingSystem/System/VFR.h" +#include "KingSystem/Utils/InitTimeInfo.h" + +namespace ksys { + +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(); + *prev_value = value; + *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; + +VFRValue::VFRValue(const f32& value) : value{value}, prev_value{value}, mean{value} {} + +void VFRValue::updateStats() { + updateStatsImpl(value, &prev_value, &mean); +} + +void VFRValue::operator+=(const f32& rhs) { + addImpl(&value, rhs); +} + +void VFRValue::operator*=(f32 scalar) { + multiplyImpl(&value, scalar); +} + +void VFRValue::lerp(const f32& b, f32 t) { + lerpImpl(&value, b, t); +} + +void VFRValue::lerp(const f32& b, f32 t, f32 max_delta) { + lerpImpl(&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); +} + +bool VFRValue::chase(const f32& target, f32 step) { + const auto delta = step * VFR::instance()->getDeltaTime(); + return sead::Mathf::chase(&value, target, delta); +} + +VFRVec3f::VFRVec3f() : value{0, 0, 0}, prev_value{0, 0, 0}, mean{0, 0, 0} {} + +VFRVec3f::VFRVec3f(const sead::Vector3f& value) : value{value}, prev_value{value}, mean{value} {} + +// NON_MATCHING: float regalloc +void VFRVec3f::updateStats() { + updateStatsImpl(value, &prev_value, &mean); +} + +void VFRVec3f::operator*=(f32 scalar) { + multiplyImpl(&value, scalar); +} + +void VFRVec3f::lerp(const sead::Vector3f& b, f32 t) { + lerpImpl(&value, b, t); +} + +bool VFRVec3f::chase(const sead::Vector3f& target, f32 t) { + const auto delta = VFR::instance()->getDeltaTime() * t; + const auto diff = target - value; + const auto norm = sead::norm2(diff); + + if (norm <= delta) { + sead::MemUtil::copy(&value, &target, sizeof(value)); + return true; + } + + value += diff * (1.0f / norm) * delta; + return false; +} + +void VFRValue::setToMax(const f32& max) { + const auto a = value; + const auto b = max; + value = a < b ? b : a; +} + +void VFRValue::setToMin(const f32& min) { + const auto a = value; + const auto b = min; + value = a > b ? b : a; +} + +void VFRValue::clamp(const f32& min, const f32& max) { + const auto a = min; + const auto b = max; + value = sead::clamp(value, a, b); +} + +void VFRVec3f::normalize(f32 new_norm) { + if (sead::norm2(value) > new_norm) { + const auto norm = sead::norm2(value); + if (norm > 0.0) + value *= new_norm / norm; + } +} + +} // namespace ksys diff --git a/src/KingSystem/System/VFRValue.h b/src/KingSystem/System/VFRValue.h new file mode 100644 index 00000000..34acbdcc --- /dev/null +++ b/src/KingSystem/System/VFRValue.h @@ -0,0 +1,48 @@ +#pragma once + +#include +#include + +namespace ksys { + +/// A time-scaled floating-point value. +struct VFRValue { + VFRValue(); + explicit VFRValue(const f32& value); + + void updateStats(); + void operator+=(const f32& rhs); + void operator*=(f32 scalar); + 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); + void clamp(const f32& min, const f32& max); + + f32 value{}; + f32 prev_value{}; + f32 mean{}; +}; + +/// A time-scaled Vector3f. +struct VFRVec3f { + VFRVec3f(); + explicit VFRVec3f(const sead::Vector3f& value); + + void updateStats(); + void operator*=(f32 scalar); + void lerp(const sead::Vector3f& b, f32 t); + bool chase(const sead::Vector3f& target, f32 t); + /// Normalize the `value` vector if its norm is greater than `new_norm`. + /// Normalization will make the norm equal to `new_norm`. + void normalize(f32 new_norm); + + sead::Vector3f value; + sead::Vector3f prev_value; + sead::Vector3f mean; +}; + +} // namespace ksys