summaryrefslogtreecommitdiff
path: root/src/SSystem/SComponent/c_lib.cpp
blob: 85587fa0189de0cb49ee65f10e57238c5a8f7a10 (plain)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/**
 * c_lib.cpp
 *
 */

#include "SSystem/SComponent/c_lib.h"
#include "string.h"
#include "SSystem/SComponent/c_math.h"

/**
 * Copies a source block of memory to a destination block of memory
 * @param dst Pointer to destination memory
 * @param src Pointer to source data to be copied
 * @param num Number of bytes to copy
 */
void cLib_memCpy(void* dst, const void* src, u32 num) {
    memcpy(dst, src, num);
}

/**
 * Sets the first num bytes of given block of memory to specified value
 * @param ptr Pointer to block of memory
 * @param value Value to be set
 * @param num Number of bytes to set
 */
void cLib_memSet(void* ptr, int value, u32 num) {
    memset(ptr, value, num);
}

/**
 * Changes value by step towards target. Step amount is clamped between a min and max, and
 * scaled as a fraction of the remaining distance.
 * @param pvalue Pointer to value to change
 * @param target Target to move value towards
 * @param scale Fraction of remaining distance to scale step by
 * @param maxStep Maximum step amount
 * @param minStep Minimum step amount
 * @return The absolute value of remaining distance to target
 */
f32 cLib_addCalc(f32* pvalue, f32 target, f32 scale, f32 maxStep, f32 minStep) {
    f32 step = 0.0f;
    if (*pvalue != target) {
        step = scale * (target - *pvalue);
        if (step >= minStep || step <= -minStep) {
            if (step > maxStep) {
                step = maxStep;
            }
            if (step < -maxStep) {
                step = -maxStep;
            }
            *pvalue += step;
        } else {
            if (step > 0) {
                if (step < minStep) {
                    step = minStep;
                    *pvalue += step;
                    if (*pvalue > target) {
                        *pvalue = target;
                    }
                }
            } else {
                if (step > -minStep) {
                    step = -minStep;
                    *pvalue += step;
                    if (*pvalue < target) {
                        *pvalue = target;
                    }
                }
            }
        }
    }
    return fabsf(target - *pvalue);
}

/**
 * Changes value by step towards target. Step amount is clamped between a max and -max, and
 * scaled as a fraction of the remaining distance.
 * @param pvalue Pointer to value to change
 * @param target Target to move value towards
 * @param scale Fraction of remaining distance to scale step by
 * @param maxStep Maximum (+/-) step amount
 */
void cLib_addCalc2(f32* pvalue, f32 target, f32 scale, f32 maxStep) {
    if (*pvalue != target) {
        f32 step = scale * (target - *pvalue);
        if (step > maxStep) {
            step = maxStep;
        } else if (step < -maxStep) {
            step = -maxStep;
        }
        *pvalue += step;
    }
}

/**
 * Changes value by step towards zero. Step amount is clamped between a max and -max, and
 * scaled as a fraction of the remaining distance.
 * @param pvalue Pointer to value to change
 * @param scale Fraction of remaining distance to scale step by
 * @param maxStep Maximum (+/-) step amount
 */
void cLib_addCalc0(f32* pvalue, f32 scale, f32 maxStep) {
    f32 step = *pvalue * scale;
    if (step > maxStep) {
        step = maxStep;
    } else if (step < -maxStep) {
        step = -maxStep;
    }
    *pvalue -= step;
}

/**
 * Changes position by step towards target position. Step amount is clamped between a min and max,
 * and scaled as a fraction of the remaining distance.
 * @param ppos Pointer to position to change
 * @param target Target position to move towards
 * @param scale Fraction of remaining distance to scale step by
 * @param maxStep Maximum step amount
 * @param minStep Minimum step amount
 * @return The absolute value of remaining distance to target
 */
f32 cLib_addCalcPos(cXyz* ppos, const cXyz& target, f32 scale, f32 maxStep, f32 minStep) {
    if (*ppos != target) {
        cXyz diff = (*ppos - target);
        f32 step = diff.abs();
        if (step < minStep) {
            *ppos = target;
        } else {
            step *= scale;
            diff *= scale;
            if (!cLib_IsZero(step)) {
                if (step > maxStep) {
                    diff *= (maxStep / step);
                } else if (step < minStep) {
                    diff *= (minStep / step);
                }
                *ppos -= diff;
            } else {
                *ppos = target;
            }
        }
    }
    return ppos->abs(target);
}

/**
 * Changes position's X/Z components by step towards target position. Step amount is clamped
 * between a min and max, and scaled as a fraction of the remaining distance.
 * @param ppos Pointer to position to change
 * @param target Target position to move towards
 * @param scale Fraction of remaining distance to scale step by
 * @param maxStep Maximum step amount
 * @param minStep Minimum step amount
 * @return The absolute value of remaining distance to target
 */
f32 cLib_addCalcPosXZ(cXyz* ppos, const cXyz& target, f32 scale, f32 maxStep, f32 minStep) {
    if (ppos->x != target.x || ppos->z != target.z) {
        cXyz diff = (*ppos - target);
        f32 step = diff.absXZ();
        if (step < minStep) {
            ppos->x = target.x;
            ppos->z = target.z;
        } else {
            step *= scale;
            diff *= scale;
            if (!cLib_IsZero(step)) {
                if (step > maxStep) {
                    diff *= (maxStep / step);
                } else if (step < minStep) {
                    diff *= (minStep / step);
                }
                ppos->x -= diff.x;
                ppos->z -= diff.z;
            } else {
                ppos->x = target.x;
                ppos->z = target.z;
            }
        }
    }
    return (*ppos - target).absXZ();
}

/**
 * Changes position by step towards target position. Step amount is clamped between a max and -max
 * and scaled as a fraction of the remaining distance.
 * @param ppos Pointer to position to change
 * @param target Target position to move value towards
 * @param scale Fraction of remaining distance to scale step by
 * @param maxStep Maximum (+/-) step amount
 */
void cLib_addCalcPos2(cXyz* ppos, const cXyz& target, f32 scale, f32 maxStep) {
    if (*ppos != target) {
        cXyz diff = (*ppos - target) * scale;
        if (diff.abs() > maxStep) {
            diff = diff.normZP();
            diff *= maxStep;
        }
        *ppos -= diff;
    }
}

/**
 * Changes position's X/Z components by step towards target position. Step amount is clamped between
 * a max and -max and scaled as a fraction of the remaining distance.
 * @param ppos Pointer to position to change
 * @param target Target position to move value towards
 * @param scale Fraction of remaining distance to scale step by
 * @param maxStep Maximum (+/-) step amount
 */
void cLib_addCalcPosXZ2(cXyz* ppos, const cXyz& target, f32 scale, f32 maxStep) {
    if (ppos->x != target.x || ppos->z != target.z) {
        cXyz diff = (*ppos - target) * scale;
        f32 step = diff.absXZ();
        if (!cLib_IsZero(step)) {
            if (step > maxStep) {
                diff *= (maxStep / step);
            }
            ppos->x -= diff.x;
            ppos->z -= diff.z;
        }
    }
}

/**
 * Changes angle value by step towards target. Step amount is clamped between a min and max, and
 * scaled as a fraction of the remaining distance.
 * @param pvalue Pointer to angle value to change
 * @param target Target angle to move value towards
 * @param scale Fraction of remaining distance to scale step by
 * @param maxStep Maximum step amount
 * @param minStep Minimum step amount
 * @return The remaining distance to target
 */
s16 cLib_addCalcAngleS(s16* pvalue, s16 target, const s16 scale, s16 maxStep, s16 minStep) {
    s16 diff = target - *pvalue;
    if (*pvalue != target) {
        s16 step = (diff) / scale;
        if (step > minStep || step < -minStep) {
            if (step > maxStep) {
                step = maxStep;
            }
            if (step < -maxStep) {
                step = -maxStep;
            }
            *pvalue += step;
        } else {
            if (0 <= diff) {
                *pvalue += minStep;
                diff = target - *pvalue;
                if (0 >= diff) {
                    *pvalue = target;
                }
            } else {
                *pvalue -= minStep;
                diff = target - *pvalue;
                if (0 <= diff) {
                    *pvalue = target;
                }
            }
        }
    }
    return target - *pvalue;
}

/**
 * Changes angle value by step towards target angle. Step amount is clamped between a max and -max
 * and scaled as a fraction of the remaining distance.
 * @param pvalue Pointer to angle value to change
 * @param target Target angle to move value towards
 * @param scale Fraction of remaining distance to scale step by
 * @param maxStep Maximum (+/-) step amount
 */
void cLib_addCalcAngleS2(s16* pvalue, s16 target, s16 scale, s16 maxStep) {
    s16 diff = target - *pvalue;
    s16 step = diff / scale;
    if (step > maxStep) {
        *pvalue += maxStep;
    } else if (step < -maxStep) {
        *pvalue -= maxStep;
    } else {
        *pvalue += step;
    }
}

/**
 * Changes unsigned char value by step towards target
 * @param pvalue Pointer to value to change
 * @param target Target to move value towards
 * @param step Step amount
 * @return TRUE when target is reached, FALSE otherwise
 */
int cLib_chaseUC(u8* pvalue, u8 target, u8 step) {
    if (step) {
        s16 wideValue = *pvalue;
        s16 wideTarget = target;
        s16 wideStep;
        if (wideValue > wideTarget) {
            wideStep = -step;
        } else {
            wideStep = step;
        }
        wideValue += wideStep;
        if (wideStep * (wideValue - wideTarget) >= 0) {
            *pvalue = target;
            return 1;
        } else {
            *pvalue = wideValue;
        }
    } else if (*pvalue == target) {
        return 1;
    }
    return 0;
}

/**
 * Changes signed short value by step towards target
 * @param pvalue Pointer to value to change
 * @param target Target to move value towards
 * @param step Step amount
 * @return TRUE when target is reached, FALSE otherwise
 */
int cLib_chaseS(s16* pvalue, s16 target, s16 step) {
    if (step) {
        if (*pvalue > target) {
            step = -step;
        }
        *pvalue += step;
        if (step * (*pvalue - target) >= 0) {
            *pvalue = target;
            return 1;
        }
    } else if (*pvalue == target) {
        return 1;
    }
    return 0;
}

/**
 * Changes float value by step towards target
 * @param pvalue Pointer to value to change
 * @param target Target to move value towards
 * @param step Step amount
 * @return TRUE when target is reached, FALSE otherwise
 */
int cLib_chaseF(f32* pvalue, f32 target, f32 step) {
    if (step) {
        if (*pvalue > target) {
            step = -step;
        }
        *pvalue += step;
        if (step * (*pvalue - target) >= 0) {
            *pvalue = target;
            return 1;
        }
    } else if (*pvalue == target) {
        return 1;
    }
    return 0;
}

/**
 * Changes position by step towards target
 * @param pvalue Pointer to position to change
 * @param target Target position to move towards
 * @param step Step amount
 * @return TRUE when target is reached, FALSE otherwise
 */
int cLib_chasePos(cXyz* pvalue, const cXyz& target, f32 step) {
    if (step) {
        cXyz diff = *pvalue - target;
        f32 diffF = diff.abs();
        if (cLib_IsZero(diffF) || diffF <= step) {
            *pvalue = target;
            return 1;
        }
        *pvalue -= diff * (step / diffF);
    } else if (*pvalue == target) {
        return 1;
    }
    return 0;
}

/**
 * Changes position's X/Z components by step towards target
 * @param pvalue Pointer to position to change
 * @param target Target position to move towards
 * @param step Step amount
 * @return TRUE when target is reached, FALSE otherwise
 */
int cLib_chasePosXZ(cXyz* pvalue, const cXyz& target, f32 step) {
    cXyz diff = *pvalue - target;
    diff.y = 0;
    f32 diffF = diff.absXZ();
    if (step) {
        if (cLib_IsZero(diffF) || diffF <= step) {
            *pvalue = target;
            return 1;
        }
        *pvalue -= diff * (step / diffF);
    } else if (cLib_IsZero(diffF)) {
        return 1;
    }
    return 0;
}

/**
 * Changes angle value by step towards target
 * @param pvalue Pointer to value to change
 * @param target Target to move value towards
 * @param step Step amount
 * @return TRUE when target is reached, FALSE otherwise
 */
int cLib_chaseAngleS(s16* pvalue, s16 target, s16 step) {
    if (step) {
        if ((s16)(*pvalue - target) > 0) {
            step = -step;
        }
        *pvalue += step;
        if (step * (s16)(*pvalue - target) >= 0) {
            *pvalue = target;
            return 1;
        }
    } else if (*pvalue == target) {
        return 1;
    }
    return 0;
}

/**
 * Gets the target y-angle from position A to position B
 * @param lhs Pointer to position A
 * @param rhs Pointer to position B
 * @return Y-angle from position A to position B
 */
s16 cLib_targetAngleY(const Vec* lhs, const Vec* rhs) {
    return cM_atan2s(rhs->x - lhs->x, rhs->z - lhs->z);
}

/**
 * Gets the target y-angle from position A to position B
 * @param lhs Reference to position A
 * @param rhs Reference to position B
 * @return Y-angle from position A to position B
 */
s16 cLib_targetAngleY(const Vec& lhs, const Vec& rhs) {
    return cM_atan2s(rhs.x - lhs.x, rhs.z - lhs.z);
}

/**
 * Gets the target x-angle from position A to position B
 * @param lhs Pointer to position A
 * @param rhs Pointer to position B
 * @return X-Angle from position A to position B
 */
s16 cLib_targetAngleX(cXyz const* lhs, cXyz const* rhs) {
    cXyz diff = *rhs - *lhs;
    f32 f1 = sqrtf(diff.getMagXZ());
    return cM_atan2s(diff.y, f1);
}

/**
 * Adds an offset to a source position in a given angle direction and places the result in pdest
 * @param pdest The resulting position
 * @param psrc The source position
 * @param angle The direction to offset psrc in
 * @param vec The offset cXyz to add to psrc
 */
void cLib_offsetPos(cXyz* pdest, cXyz const* psrc, s16 angle, cXyz const* vec) {
    f32 cos = cM_scos(angle);
    f32 sin = cM_ssin(angle);
    pdest->x = psrc->x + (vec->x * cos + vec->z * sin);
    pdest->y = psrc->y + vec->y;
    pdest->z = psrc->z + (vec->z * cos - vec->x * sin);
}

/**
 * Gets the target x-angle from position A to position B
 * @param lhs Pointer to position A
 * @param rhs Pointer to position B
 * @return X-Angle from position A to position B
 */
s32 cLib_distanceAngleS(s16 x, s16 y) {
    return abs(static_cast<s16>(x - y));
}

static Mtx mtx[10];

Mtx* calc_mtx = mtx;

/**
 * Initializes calc_mtx to mtx stack
 */
void MtxInit() {
    calc_mtx = mtx;
}

void MtxTrans(f32 x_trans, f32 y_trans, f32 z_trans, u8 param_3) {
    if (param_3 == 0) {
        MTXTrans(*calc_mtx, x_trans, y_trans, z_trans);
    } else {
        Mtx mtx;
        MTXTrans(mtx, x_trans, y_trans, z_trans);
        MTXConcat(*calc_mtx, mtx, *calc_mtx);
    }
}

void MtxScale(f32 x_trans, f32 y_trans, f32 z_trans, u8 param_3) {
    if (param_3 == 0) {
        MTXScale(*calc_mtx, x_trans, y_trans, z_trans);
    } else {
        Mtx mtx;
        MTXScale(mtx, x_trans, y_trans, z_trans);
        MTXConcat(*calc_mtx, mtx, *calc_mtx);
    }
}

/**
 * Multiplies a src position by the calc_mtx and puts the result in dest
 * @param src The src position to be multiplied
 * @param dest The resulting multiplied position
 */
void MtxPosition(cXyz* src, cXyz* dest) {
    MTXMultVec(*calc_mtx, src, dest);
}

void MtxPush() {
    Mtx mtx;
    MTXCopy(*calc_mtx, mtx);
    calc_mtx++;
    MTXCopy(mtx, *calc_mtx);
}

Mtx* MtxPull() {
    return calc_mtx--;
}