1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
#include "s/s_Math.h"
#include <cmath>
namespace sLib {
float extrapolate(float start, float end, float scale) {
float diff = end - start;
if (scale < 1.0f) {
return end;
}
return start + diff / scale;
}
float addCalc(float *value, float target, float ratio, float maxStepSize, float minStepSize) {
if (*value != target) {
float step = ratio * (target - *value);
if (step >= minStepSize || (step <= -minStepSize)) {
if (step > maxStepSize) {
step = maxStepSize;
}
if (step < -maxStepSize) {
step = -maxStepSize;
}
*value += step;
} else if (step > 0.0f) {
if (step < minStepSize) {
*value = *value + minStepSize;
if (*value > target) {
*value = target;
}
}
} else if (step > -minStepSize) {
*value += -minStepSize;
if (*value < target) {
*value = target;
}
}
}
float ret = target - *value;
if (ret > 0.0f) {
return ret;
} else {
return -ret;
}
}
void addCalcScaledDiff(float *value, float target, float ratio, float maxStepSize) {
if (*value == target) {
return;
}
float step = ratio * (target - *value);
if (step > maxStepSize) {
step = maxStepSize;
} else if (step < -maxStepSize) {
step = -maxStepSize;
}
*value += step;
}
void addCalcScaled(float *value, float stepSize, float maxStep) {
float step = *value * stepSize;
if (step > maxStep) {
step = maxStep;
} else if (step < -maxStep) {
step = -maxStep;
}
*value -= step;
}
int absDiff(short a1, short a2) {
return abs((short)(a1 - a2));
}
template <typename T>
T addCalcAngleT(T *value, T target, T ratio, T maxStepSize, T minStepSize) {
T diff = target - *value;
if (*value != target) {
T step = diff / ratio;
if (step > minStepSize || step < -minStepSize) {
if (step > maxStepSize) {
step = maxStepSize;
} else if (step < -maxStepSize) {
step = -maxStepSize;
}
*value += step;
} else {
if (0 <= diff) {
T newVal = *value + minStepSize;
*value = newVal;
if ((T)(target - *value) <= 0) {
*value = target;
}
} else {
T newVal = *value - minStepSize;
*value = newVal;
if ((T)(target - newVal) >= 0) {
*value = target;
}
}
}
}
return target - *value;
}
template <typename T>
void addCalcAngleT(T *value, T target, T ratio, T maxStepSize) {
T diff = target - *value;
T step = diff / ratio;
if (step > maxStepSize) {
*value += maxStepSize;
} else if (step < -maxStepSize) {
*value -= maxStepSize;
} else {
*value += step;
}
}
short addCalcAngle(short *value, short target, short ratio, short maxStepSize, short minStepSize) {
return addCalcAngleT(value, target, ratio, maxStepSize, minStepSize);
}
void addCalcAngle(short *value, short target, short ratio, short maxStepSize) {
return addCalcAngleT(value, target, ratio, maxStepSize);
}
short addCalcAngle2(short *value, short target, short ratio, short maxStepSize, short minStepSize) {
s16 oldValue = *value;
if (oldValue != target) {
s32 unclampedStep;
if (ratio > 0) {
if (target < oldValue) {
unclampedStep = 0x10000 - (oldValue - target);
} else {
unclampedStep = target - oldValue;
}
} else if (ratio < 0) {
if (target < oldValue) {
unclampedStep = oldValue - target;
} else {
unclampedStep = 0xFFFF - (target - oldValue);
}
} else {
return (s16)(target - oldValue);
}
s32 scaledStep = labs(unclampedStep / ratio);
s16 clampedStep = 0x7FFF;
if (scaledStep <= 0x7FFF) {
clampedStep = scaledStep;
}
if (clampedStep > minStepSize) {
if (clampedStep > maxStepSize) {
clampedStep = maxStepSize;
}
if (ratio < 0) {
clampedStep = -clampedStep;
}
*value += clampedStep;
} else if (ratio >= 0) {
*value += minStepSize;
if (((s16)(target - oldValue) > 0) && ((s16)(target - *value) <= 0)) {
*value = target;
}
} else {
*value -= minStepSize;
if (((s16)(target - oldValue) < 0) && ((s16)(target - *value) >= 0)) {
*value = target;
}
}
}
return (s16)(target - *value);
}
// Is the same as cLib_chaseUC
BOOL chaseUC(u8 *value, u8 target, u8 stepSize) {
if (stepSize) {
s16 val = *value;
s16 tgt = target;
s16 szs;
if (val > tgt) {
szs = -stepSize;
} else {
szs = stepSize;
}
val += szs;
if (szs * (val - tgt) >= 0) {
*value = target;
return TRUE;
} else {
*value = val;
}
}
else if (*value == target) {
return TRUE;
}
return FALSE;
}
template <typename T>
BOOL chaseT(T *value, T target, T stepSize) {
if (*value == target) {
return 1;
}
if (stepSize) {
if (*value > target) {
stepSize = -stepSize;
}
*value += stepSize;
if (stepSize * (*value - target) >= 0) {
*value = target;
return 1;
}
}
return 0;
}
BOOL chase(short *value, short target, short stepSize) {
return chaseT(value, target, stepSize);
}
BOOL chase(int *value, int target, int stepSize) {
return chaseT(value, target, stepSize);
}
BOOL chase(float *value, float target, float stepSize) {
return chaseT(value, target, stepSize);
}
template <typename T>
BOOL isInRangeT(T val, T min, T max) {
BOOL ret;
if (min < max) {
return val >= min && val <= max;
} else {
return val >= max && val <= min;
}
return ret;
}
BOOL isInRange(float val, float min, float max) {
return isInRangeT(val, min, max);
}
BOOL chaseAngle(short *value, short target, short stepSize) {
if (*value == target) {
return 1;
}
if (stepSize != 0) {
if ((short)(*value - target) > 0) {
stepSize = -stepSize;
}
*value += stepSize;
if (stepSize * (short)(*value - target) >= 0) {
*value = target;
return 1;
}
}
return 0;
}
BOOL chaseAngle2(short *value, short target, short stepSize) {
if (*value == target) {
return TRUE;
}
if (stepSize) {
short step = stepSize;
if (stepSize < 0) {
if (stepSize != 0x8000) {
stepSize = -stepSize;
} else {
stepSize = 0x7FFF;
}
}
if ((short)(*value - target) > 0) {
stepSize = -stepSize;
}
*value += step;
if (step * stepSize > 0) {
if (step * (short)(*value - target) >= 0) {
*value = target;
return TRUE;
}
}
}
return FALSE;
}
} // namespace sLib
|