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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
|
#include "ultra64.h"
#include "z_lib.h"
#include "ichain.h"
#include "printf.h"
#include "regs.h"
#include "sys_math.h"
#include "rand.h"
#include "sfx.h"
/**
* memset: sets `len` bytes to `val` starting at address `dest`.
*
* Unlike normal memset,
* - `dest` is a `u8*` already,
* - does not return `dest`,
* - the arguments are in a different order,
* - `val` is a `u8` instead of the standard `s32`.
*
* @see There are two other memsets in this codebase, memset(), MemSet()
*
* @param dest address to start at
* @param len number of bytes to write
* @param val value to write
*/
void Lib_MemSet(u8* dest, size_t len, u8 val) {
size_t i;
// clang-format off
for (i = 0; i < len; i++) { *dest++ = val; }
// clang-format on
}
/**
* @param angle binang
* @return cos(angle)
*/
f32 Math_CosS(s16 angle) {
return coss(angle) * SHT_MINV;
}
/**
* @param angle binang
* @return sin(angle)
*/
f32 Math_SinS(s16 angle) {
return sins(angle) * SHT_MINV;
}
/**
* Changes pValue by step (scaled by the update rate) towards target, setting it equal when the target is reached.
* Returns true when target is reached, false otherwise.
*/
s32 Math_ScaledStepToS(s16* pValue, s16 target, s16 step) {
if (step != 0) {
f32 updateScale = R_UPDATE_RATE * 0.5f;
if ((s16)(*pValue - target) > 0) {
step = -step;
}
*pValue += (s16)(step * updateScale);
if (((s16)(*pValue - target) * step) >= 0) {
*pValue = target;
return true;
}
} else if (target == *pValue) {
return true;
}
return false;
}
/**
* Changes pValue by step towards target, setting it equal when the target is reached.
* Returns true when target is reached, false otherwise.
*/
s32 Math_StepToS(s16* pValue, s16 target, s16 step) {
if (step != 0) {
if (target < *pValue) {
step = -step;
}
*pValue += step;
if (((*pValue - target) * step) >= 0) {
*pValue = target;
return true;
}
} else if (target == *pValue) {
return true;
}
return false;
}
/**
* Changes pValue by step towards target, setting it equal when the target is reached.
* Returns true when target is reached, false otherwise.
*/
s32 Math_StepToF(f32* pValue, f32 target, f32 step) {
if (step != 0.0f) {
if (target < *pValue) {
step = -step;
}
*pValue += step;
if (((*pValue - target) * step) >= 0) {
*pValue = target;
return true;
}
} else if (target == *pValue) {
return true;
}
return false;
}
/**
* Changes pValue by step. If pvalue reaches limit angle or its opposite, sets it equal to limit angle.
* Returns true when limit angle or its opposite is reached, false otherwise.
*/
s32 Math_StepUntilAngleS(s16* pValue, s16 limit, s16 step) {
s16 orig = *pValue;
*pValue += step;
if (((s16)(*pValue - limit) * (s16)(orig - limit)) <= 0) {
*pValue = limit;
return true;
}
return false;
}
/**
* Changes pValue by step. If pvalue reaches limit, sets it equal to limit.
* Returns true when limit is reached, false otherwise.
*/
s32 Math_StepUntilS(s16* pValue, s16 limit, s16 step) {
s16 orig = *pValue;
*pValue += step;
if (((*pValue - limit) * (orig - limit)) <= 0) {
*pValue = limit;
return true;
}
return false;
}
/**
* Changes pValue by step towards target angle, setting it equal when the target is reached.
* Returns true when target is reached, false otherwise.
*/
s32 Math_StepToAngleS(s16* pValue, s16 target, s16 step) {
s32 diff = target - *pValue;
if (diff < 0) {
step = -step;
}
if (diff >= 0x8000) {
step = -step;
diff = -0xFFFF - -diff;
} else if (diff <= -0x8000) {
diff += 0xFFFF;
step = -step;
}
if (step != 0) {
*pValue += step;
if ((diff * step) <= 0) {
*pValue = target;
return true;
}
} else if (target == *pValue) {
return true;
}
return false;
}
/**
* Changes pValue by step. If pvalue reaches limit, sets it equal to limit.
* Returns true when limit is reached, false otherwise.
*/
s32 Math_StepUntilF(f32* pValue, f32 limit, f32 step) {
f32 orig = *pValue;
*pValue += step;
if (((*pValue - limit) * (orig - limit)) <= 0) {
*pValue = limit;
return true;
}
return false;
}
/**
* Changes pValue toward target by incrStep if pValue is smaller and by decrStep if it is greater, setting it equal when
* target is reached. Returns true when target is reached, false otherwise.
*/
s32 Math_AsymStepToF(f32* pValue, f32 target, f32 incrStep, f32 decrStep) {
f32 step = (target >= *pValue) ? incrStep : decrStep;
if (step != 0.0f) {
if (target < *pValue) {
step = -step;
}
*pValue += step;
if (((*pValue - target) * step) >= 0) {
*pValue = target;
return 1;
}
} else if (target == *pValue) {
return 1;
}
return 0;
}
void Lib_GetControlStickData(f32* outMagnitude, s16* outAngle, Input* input) {
f32 relX = input->rel.stick_x;
f32 relY = input->rel.stick_y;
*outMagnitude = sqrtf(SQ(relX) + SQ(relY));
*outMagnitude = (60.0f < *outMagnitude) ? 60.0f : *outMagnitude;
*outAngle = Math_Atan2S(relY, -relX);
}
s16 Rand_S16Offset(s16 base, s16 range) {
return (s16)(Rand_ZeroOne() * range) + base;
}
s16 Rand_S16OffsetStride(s16 base, s16 stride, s16 range) {
return (s16)(Rand_ZeroOne() * range) * stride + base;
}
void Math_Vec3f_Copy(Vec3f* dest, Vec3f* src) {
dest->x = src->x;
dest->y = src->y;
dest->z = src->z;
}
void Math_Vec3s_ToVec3f(Vec3f* dest, Vec3s* src) {
dest->x = src->x;
dest->y = src->y;
dest->z = src->z;
}
void Math_Vec3f_Sum(Vec3f* a, Vec3f* b, Vec3f* dest) {
dest->x = a->x + b->x;
dest->y = a->y + b->y;
dest->z = a->z + b->z;
}
void Math_Vec3f_Diff(Vec3f* a, Vec3f* b, Vec3f* dest) {
dest->x = a->x - b->x;
dest->y = a->y - b->y;
dest->z = a->z - b->z;
}
void Math_Vec3s_DiffToVec3f(Vec3f* dest, Vec3s* a, Vec3s* b) {
dest->x = a->x - b->x;
dest->y = a->y - b->y;
dest->z = a->z - b->z;
}
void Math_Vec3f_Scale(Vec3f* vec, f32 scaleF) {
vec->x *= scaleF;
vec->y *= scaleF;
vec->z *= scaleF;
}
f32 Math_Vec3f_DistXYZ(Vec3f* a, Vec3f* b) {
f32 dx = b->x - a->x;
f32 dy = b->y - a->y;
f32 dz = b->z - a->z;
return sqrtf(SQ(dx) + SQ(dy) + SQ(dz));
}
f32 Math_Vec3f_DistXYZAndStoreDiff(Vec3f* a, Vec3f* b, Vec3f* dest) {
dest->x = b->x - a->x;
dest->y = b->y - a->y;
dest->z = b->z - a->z;
return sqrtf(SQ(dest->x) + SQ(dest->y) + SQ(dest->z));
}
f32 Math_Vec3f_DistXZ(Vec3f* a, Vec3f* b) {
f32 dx = b->x - a->x;
f32 dz = b->z - a->z;
return sqrtf(SQ(dx) + SQ(dz));
}
f32 Math_Vec3f_DiffY(Vec3f* a, Vec3f* b) {
return b->y - a->y;
}
/**
* @param origin Position of the origin, the location from which to look at the target `point`
* @param point Position of the target point, in the same space as `origin`
* @return The yaw towards `point` when at `origin`, assuming +z is forwards.
*/
s16 Math_Vec3f_Yaw(Vec3f* origin, Vec3f* point) {
f32 dx = point->x - origin->x;
f32 dz = point->z - origin->z;
return Math_Atan2S(dz, dx);
}
s16 Math_Vec3f_Pitch(Vec3f* a, Vec3f* b) {
return Math_Atan2S(Math_Vec3f_DistXZ(a, b), a->y - b->y);
}
void IChain_Apply_u8(u8* ptr, InitChainEntry* ichain);
void IChain_Apply_s8(u8* ptr, InitChainEntry* ichain);
void IChain_Apply_u16(u8* ptr, InitChainEntry* ichain);
void IChain_Apply_s16(u8* ptr, InitChainEntry* ichain);
void IChain_Apply_u32(u8* ptr, InitChainEntry* ichain);
void IChain_Apply_s32(u8* ptr, InitChainEntry* ichain);
void IChain_Apply_f32(u8* ptr, InitChainEntry* ichain);
void IChain_Apply_f32div1000(u8* ptr, InitChainEntry* ichain);
void IChain_Apply_Vec3f(u8* ptr, InitChainEntry* ichain);
void IChain_Apply_Vec3fdiv1000(u8* ptr, InitChainEntry* ichain);
void IChain_Apply_Vec3s(u8* ptr, InitChainEntry* ichain);
void (*sInitChainHandlers[])(u8* ptr, InitChainEntry* ichain) = {
IChain_Apply_u8, IChain_Apply_s8, IChain_Apply_u16, IChain_Apply_s16,
IChain_Apply_u32, IChain_Apply_s32, IChain_Apply_f32, IChain_Apply_f32div1000,
IChain_Apply_Vec3f, IChain_Apply_Vec3fdiv1000, IChain_Apply_Vec3s,
};
void Actor_ProcessInitChain(struct Actor* actor, InitChainEntry* ichain) {
do {
sInitChainHandlers[ichain->type]((u8*)actor, ichain);
} while ((ichain++)->cont);
}
void IChain_Apply_u8(u8* ptr, InitChainEntry* ichain) {
*(u8*)(ptr + ichain->offset) = ichain->value;
}
void IChain_Apply_s8(u8* ptr, InitChainEntry* ichain) {
*(s8*)(ptr + ichain->offset) = ichain->value;
}
void IChain_Apply_u16(u8* ptr, InitChainEntry* ichain) {
*(u16*)(ptr + ichain->offset) = ichain->value;
}
void IChain_Apply_s16(u8* ptr, InitChainEntry* ichain) {
*(s16*)(ptr + ichain->offset) = ichain->value;
}
void IChain_Apply_u32(u8* ptr, InitChainEntry* ichain) {
*(u32*)(ptr + ichain->offset) = ichain->value;
}
void IChain_Apply_s32(u8* ptr, InitChainEntry* ichain) {
*(s32*)(ptr + ichain->offset) = ichain->value;
}
void IChain_Apply_f32(u8* ptr, InitChainEntry* ichain) {
*(f32*)(ptr + ichain->offset) = ichain->value;
}
void IChain_Apply_f32div1000(u8* ptr, InitChainEntry* ichain) {
*(f32*)(ptr + ichain->offset) = ichain->value / 1000.0f;
}
void IChain_Apply_Vec3f(u8* ptr, InitChainEntry* ichain) {
Vec3f* vec = (Vec3f*)(ptr + ichain->offset);
f32 val = ichain->value;
vec->z = val;
vec->y = val;
vec->x = val;
}
void IChain_Apply_Vec3fdiv1000(u8* ptr, InitChainEntry* ichain) {
Vec3f* vec = (Vec3f*)(ptr + ichain->offset);
f32 val;
PRINTF("pp=%x data=%f\n", vec, ichain->value / 1000.0f);
val = ichain->value / 1000.0f;
vec->z = val;
vec->y = val;
vec->x = val;
}
void IChain_Apply_Vec3s(u8* ptr, InitChainEntry* ichain) {
Vec3s* vec = (Vec3s*)(ptr + ichain->offset);
s16 val = ichain->value;
vec->z = val;
vec->y = val;
vec->x = val;
}
/**
* Changes pValue by step towards target. If this step is more than fraction of the remaining distance, step by that
* instead, with a minimum step of minStep. Returns remaining distance to target.
*/
f32 Math_SmoothStepToF(f32* pValue, f32 target, f32 fraction, f32 step, f32 minStep) {
f32 stepSize;
if (*pValue != target) {
stepSize = (target - *pValue) * fraction;
if ((stepSize >= minStep) || (stepSize <= -minStep)) {
if (stepSize > step) {
stepSize = step;
}
if (stepSize < -step) {
stepSize = -step;
}
*pValue += stepSize;
} else {
if (stepSize < minStep) {
*pValue += minStep;
stepSize = minStep;
if (target < *pValue) {
*pValue = target;
}
}
if (stepSize > -minStep) {
*pValue += -minStep;
if (*pValue < target) {
*pValue = target;
}
}
}
}
return fabsf(target - *pValue);
}
/**
* Changes pValue by step towards target. If step is more than fraction of the remaining distance, step by that instead.
*/
void Math_ApproachF(f32* pValue, f32 target, f32 fraction, f32 step) {
if (*pValue != target) {
f32 stepSize = (target - *pValue) * fraction;
if (stepSize > step) {
stepSize = step;
} else if (stepSize < -step) {
stepSize = -step;
}
*pValue += stepSize;
}
}
/**
* Changes pValue by step towards zero. If step is more than fraction of the remaining distance, step by that instead.
*/
void Math_ApproachZeroF(f32* pValue, f32 fraction, f32 step) {
f32 stepSize = *pValue * fraction;
if (stepSize > step) {
stepSize = step;
} else if (stepSize < -step) {
stepSize = -step;
}
*pValue -= stepSize;
}
/**
* Changes pValue by step towards target angle in degrees. If this step is more than fraction of the remaining distance,
* step by that instead, with a minimum step of minStep. Returns the value of the step taken.
*/
f32 Math_SmoothStepToDegF(f32* pValue, f32 target, f32 fraction, f32 step, f32 minStep) {
f32 stepSize = 0.0f;
f32 diff = target - *pValue;
if (*pValue != target) {
if (diff > 180.0f) {
diff = -(360.0f - diff);
} else if (diff < -180.0f) {
diff = 360.0f + diff;
}
stepSize = diff * fraction;
if ((stepSize >= minStep) || (stepSize <= -minStep)) {
if (stepSize > step) {
stepSize = step;
}
if (stepSize < -step) {
stepSize = -step;
}
*pValue += stepSize;
} else {
if (stepSize < minStep) {
stepSize = minStep;
*pValue += stepSize;
if (*pValue > target) {
*pValue = target;
}
}
if (stepSize > -minStep) {
stepSize = -minStep;
*pValue += stepSize;
if (*pValue < target) {
*pValue = target;
}
}
}
}
if (*pValue >= 360.0f) {
*pValue -= 360.0f;
}
if (*pValue < 0.0f) {
*pValue += 360.0f;
}
return stepSize;
}
/**
* Changes pValue by step towards target. If this step is more than 1/scale of the remaining distance, step by that
* instead, with a minimum step of minStep. Returns remaining distance to target.
*/
s16 Math_SmoothStepToS(s16* pValue, s16 target, s16 scale, s16 step, s16 minStep) {
s16 stepSize = 0;
s16 diff = target - *pValue;
if (*pValue != target) {
stepSize = diff / scale;
if ((stepSize > minStep) || (stepSize < -minStep)) {
if (stepSize > step) {
stepSize = step;
}
if (stepSize < -step) {
stepSize = -step;
}
*pValue += stepSize;
} else {
if (diff >= 0) {
*pValue += minStep;
if ((s16)(target - *pValue) <= 0) {
*pValue = target;
}
} else {
*pValue -= minStep;
if ((s16)(target - *pValue) >= 0) {
*pValue = target;
}
}
}
}
return diff;
}
/**
* Changes pValue by step towards target. If step is more than 1/scale of the remaining distance, step by that instead.
*/
void Math_ApproachS(s16* pValue, s16 target, s16 scale, s16 step) {
s16 diff = target - *pValue;
diff /= scale;
if (diff > step) {
*pValue += step;
} else if (diff < -step) {
*pValue -= step;
} else {
*pValue += diff;
}
}
void Color_RGBA8_Copy(Color_RGBA8* dst, Color_RGBA8* src) {
dst->r = src->r;
dst->g = src->g;
dst->b = src->b;
dst->a = src->a;
}
/**
* Play a sound effect at the center of the screen.
*/
void Sfx_PlaySfxCentered(u16 sfxId) {
SFX_PLAY_CENTERED(sfxId);
}
/**
* Play a sound effect at the center of the screen. Identical to `Sfx_PlaySfxCentered`.
*/
void Sfx_PlaySfxCentered2(u16 sfxId) {
SFX_PLAY_CENTERED(sfxId);
}
/**
* Play a sound effect at the requested position.
*/
void Sfx_PlaySfxAtPos(Vec3f* projectedPos, u16 sfxId) {
SFX_PLAY_AT_POS(projectedPos, sfxId);
}
|