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