NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
s_lib.cpp
1#include <game/sLib/s_lib.hpp>
2#include <MSL/cstdio>
3
4vprintfFunc sLib::p_VPrintfFuncPtr = vprintf;
5
6float sLib::addCalc(float *value, float target, float smoothing, float maxStep, float minStep) {
7 if (*value != target) {
8 float dist = target - *value;
9 float step = smoothing * dist;
10
11 if (step >= minStep || step <= -minStep) {
12 if (step > maxStep) {
13 step = maxStep;
14 }
15 if (step < -maxStep) { // [Possible optimization: add else to prevent unnecessary extra evaluation]
16 step = -maxStep;
17 }
18
19 *value += step;
20
21 } else if (step > 0.0f) {
22 if (step < minStep) { // [Possible optimization: this check is always true]
23 *value += minStep;
24 if (*value > target) {
25 *value = target;
26 }
27 }
28
29 } else {
30 if (step > -minStep) { // [Possible optimization: this check is always true]
31 *value += -minStep;
32 if (*value < target) {
33 *value = target;
34 }
35 }
36 }
37 }
38
39 // [Possible optimization: use fabsf]
40 float dist = target - *value;
41 return (dist > 0.0f) ? dist : -dist;
42}
43
44template <typename T>
45T sLib::addCalcAngleT(T *value, T target, T smoothing, T maxStep, T minStep) {
46 T dist = target - *value; // [Possible optimization: move this declaration inside the if block]
47 if (*value != target) {
48 T step = dist / smoothing;
49
50 if (step > minStep || step < -minStep) {
51 if (step > maxStep) {
52 step = maxStep;
53 }
54 else if (step < -maxStep) {
55 step = -maxStep;
56 }
57
58 *value += step;
59
60 } else if (dist >= 0) {
61 *value += minStep;
62 dist = target - *value;
63 if (dist <= 0) {
64 *value = target;
65 }
66
67 } else {
68 *value -= minStep;
69 dist = target - *value;
70 if (dist >= 0) {
71 *value = target;
72 }
73 }
74 }
75
76 return target - *value;
77}
78
79s16 sLib::addCalcAngle(s16 *value, s16 target, s16 smoothing, s16 maxStep, s16 minStep) {
80 return addCalcAngleT<s16>(value, target, smoothing, maxStep, minStep);
81}
82
83template <typename T>
84void sLib::addCalcAngleT(T *value, T target, T smoothing, T maxStep) {
85 T dist = target - *value;
86 T step = dist / smoothing;
87
88 if (step > maxStep) {
89 *value += maxStep;
90 } else if (step < -maxStep) {
91 *value -= maxStep;
92 } else {
93 *value += step;
94 }
95}
96
97void sLib::addCalcAngle(s16 *value, s16 target, s16 smoothing, s16 maxStep) {
98 addCalcAngleT<s16>(value, target, smoothing, maxStep);
99}
100
101template <typename T>
102BOOL sLib::chaseT(T *value, T target, T step) {
103 if (*value == target) {
104 return TRUE;
105 }
106
107 if (step) {
108 if (*value > target) {
109 step = -step;
110 }
111
112 *value += step;
113 if (step * (*value - target) >= 0) {
114 *value = target;
115 return TRUE;
116 }
117 }
118
119 return FALSE;
120}
121
122BOOL sLib::chase(s16 *value, s16 target, s16 step) {
123 return sLib::chaseT<s16>(value, target, step);
124}
125
126BOOL sLib::chase(int *value, int target, int step) {
127 return sLib::chaseT<int>(value, target, step);
128}
129
130BOOL sLib::chase(long *value, long target, long step) {
131 return sLib::chaseT<long>(value, target, step);
132}
133
134BOOL sLib::chase(float *value, float target, float step) {
135 return sLib::chaseT<float>(value, target, step);
136}
137
138BOOL sLib::chaseAngle(s16 *value, s16 target, s16 step) {
139 if (*value == target) {
140 return TRUE;
141 }
142
143 if (step != 0) {
144 s16 dist = *value - target;
145 if (dist > 0) {
146 step = -step;
147 }
148
149 *value += step;
150 dist = *value - target;
151
152 if (step * dist >= 0) {
153 *value = target;
154 return TRUE;
155 }
156 }
157
158 return FALSE;
159}
160
161BOOL sLib::chaseAngleByRotDir(s16 *value, s16 target, s16 step) {
162 if (*value == target) {
163 return TRUE;
164 }
165
166 if (step != 0) {
167 s16 increment = step;
168
169 // Get absolute step value, accounting for overflow
170 if (step < 0) {
171 step = (step == 0x8000) ? 0x7fff : (s16)-step;
172 }
173
174 // If the distance to the target is negative, then negate step to move towards it
175 s16 dist = *value - target;
176 if (dist > 0) {
177 step = -step;
178 }
179
180 *value += increment;
181
182 // Check if the target's direction matches the intended direction
183 // If so, perform the overshoot check
184 if (increment * step > 0) {
185 s16 dist = *value - target;
186 if (increment * dist >= 0) {
187 *value = target;
188 return TRUE;
189 }
190 }
191 }
192
193 return FALSE;
194}
T addCalcAngleT(T *value, T target, T smoothing, T maxStep, T minStep)
Smoothly moves value towards target using proportional scaling.
Definition s_lib.cpp:45
BOOL chaseT(T *value, T target, T step)
Moves value towards target by a fixed step amount.
Definition s_lib.cpp:102
float addCalc(float *value, float target, float smoothing, float maxStep, float minStep)
Smoothly moves value towards target using proportional scaling.
Definition s_lib.cpp:6
s16 addCalcAngle(s16 *value, s16 target, s16 smoothing, s16 maxStep, s16 minStep)
Smoothly moves value towards target using proportional scaling.
Definition s_lib.cpp:79
BOOL chaseAngleByRotDir(s16 *value, s16 target, s16 step)
Moves value towards target by a fixed step amount.
Definition s_lib.cpp:161
vprintfFunc p_VPrintfFuncPtr
Pointer to std::printf.
Definition s_lib.cpp:4
BOOL chaseAngle(s16 *value, s16 target, s16 step)
Moves value towards target by a fixed step amount.
Definition s_lib.cpp:138
BOOL chase(s16 *value, s16 target, s16 step)
Moves value towards target by a fixed step amount.
Definition s_lib.cpp:122