summaryrefslogtreecommitdiff
path: root/src/code/m_lib.c
blob: f39845db7c08003a36cd69c6997495e8787488ec (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
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
#include "m_lib.h"
#include "game.h"
#include "libc/math.h"
#include "m_rcp.h"
#include "macros.h"
#include "sys_math.h"
#include "sys_math_atan.h"

ValueSetProc D_80107B00_jp[] = {
    ValueSet__s_char, ValueSet__u_char,      ValueSet__s_short, ValueSet__u_short,     ValueSet__s_int, ValueSet__u_int,
    ValueSet__float,  ValueSet__float_x1000, ValueSet__xyz_t,   ValueSet__xyz_t_x1000, ValueSet__s_xyz,
};

void mem_copy(u8* dest, u8* src, size_t len) {
    while (len != 0) {
        *dest = *src;
        src++;
        dest++;
        len--;
    }
}

void mem_clear(u8* ptr, size_t len, u8 value) {
    size_t i;

    // clang-format off
    for (i = 0; i < len; i++) {*ptr++ = value;}
    // clang-format on
}

s32 mem_cmp(u8* ptr1, u8* ptr2, size_t len) {
    while (len != 0) {
        if (*ptr1 != *ptr2) {
            return 0;
        }
        ptr1++;
        ptr2++;
        len--;
    }
    return 1;
}

f32 cos_s(s16 angle) {
    return coss(angle) * SHT_MINV;
}

f32 sin_s(s16 angle) {
    return sins(angle) * SHT_MINV;
}

s32 chase_angle(s16* pValue, s16 target, s16 step) {
    if (step != 0) {
        f32 updateScale = game_GameFrame_2F;

        if ((s16)(*pValue - target) > 0) {
            step = -step;
        }

        *pValue += (s16)(step * updateScale);

        if (((s16)(*pValue - target) * step) >= 0) {
            *pValue = target;
            return TRUE;
        }
    } else if (*pValue == target) {
        return TRUE;
    }

    return FALSE;
}

s32 chase_s(s16* pValue, s16 target, s16 step) {
    if (step) {
        if (*pValue > target) {
            step = -step;
        }

        *pValue += step;

        if ((step * (*pValue - target)) >= 0) {
            *pValue = target;
            return TRUE;
        }
    } else {
        if (*pValue == target) {
            return TRUE;
        }
    }
    return FALSE;
}

s32 chase_f(f32* pValue, f32 target, f32 step) {
    if (step) {
        if (*pValue > target) {
            step = -step;
        }

        *pValue += step;

        if ((step * (*pValue - target)) >= 0.0f) {
            *pValue = target;
            return TRUE;
        }
    } else {
        if (*pValue == target) {
            return TRUE;
        }
    }
    return FALSE;
}

s32 chase_angle2(s16* pValue, s16 limit, s16 step) {
    s16 prev = *pValue;

    *pValue += step;
    if (((s16)(*pValue - limit) * (s16)(prev - limit)) <= 0) {
        *pValue = limit;
        return 1;
    }

    return 0;
}

s32 chase_s2(s16* pValue, s16 limit, s16 step) {
    s16 prev = *pValue;

    *pValue += step;
    if ((*pValue - limit) * (prev - limit) <= 0) {
        *pValue = limit;
        return 1;
    }
    return 0;
}

s32 chase_s3(s16* pValue, s16 target, s16 step) {
    if (step) {
        s32 diff = target - *pValue;

        if (diff < 0) {
            step = -step;
        }

        if (diff >= 0x8000) {
            step = -step;
            diff = -0xFFFF - -diff;
        } else if (diff <= -0x8000) {
            step = -step;
            diff += 0xFFFF;
        }

        *pValue += step;

        if ((diff * step) <= 0) {
            *pValue = target;
            return TRUE;
        }

    } else if (target == *pValue) {
        return TRUE;
    }
    return FALSE;
}

s32 chase_f2(f32* pValue, f32 limit, f32 step) {
    f32 prev = *pValue;

    *pValue += step;
    if ((*pValue - limit) * (prev - limit) <= 0.0f) {
        *pValue = limit;
        return 1;
    }
    return 0;
}

s32 chase_f3(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 inter_float(f32* pValue, f32 arg1, s32 stepCount) {
    if (stepCount <= 0) {
        *pValue = arg1;
    } else {
        f32 diff = arg1 - *pValue;

        *pValue += diff / stepCount;
    }
}

void stick_ratio_set(f32* magnitude, s16* angle, Input* input) {
    f32 relX = input->rel.stick_x;
    f32 relY = input->rel.stick_y;

    *magnitude = sqrtf(SQ(relX) + SQ(relY));
    *magnitude = (*magnitude > 60.0f) ? 60.0f : *magnitude;

    *angle = atans_table(relY, -relX);
}

s16 get_random_timer(s16 base, s16 range) {
    return base + (s16)(RANDOM_F(range));
}

s16 get_random_pattern_timer(s16 base, s16 stride, s16 range) {
    return base + (s16)(RANDOM_F(range)) * stride;
}

void xyz_t_move(xyz_t* dest, xyz_t* src) {
    dest->x = src->x;
    dest->y = src->y;
    dest->z = src->z;
}

void xyz_t_move_s_xyz(xyz_t* dest, s_xyz* src) {
    dest->x = src->x;
    dest->y = src->y;
    dest->z = src->z;
}

void xyz_t_add(xyz_t* augend, xyz_t* addend, xyz_t* total) {
    total->x = augend->x + addend->x;
    total->y = augend->y + addend->y;
    total->z = augend->z + addend->z;
}

void xyz_t_sub(xyz_t* minuend, xyz_t* subtrahend, xyz_t* diff) {
    diff->x = minuend->x - subtrahend->x;
    diff->y = minuend->y - subtrahend->y;
    diff->z = minuend->z - subtrahend->z;
}

void xyz_t_sub_ss(xyz_t* dest, s_xyz* minuend, s_xyz* subtrahend) {
    dest->x = minuend->x - subtrahend->x;
    dest->y = minuend->y - subtrahend->y;
    dest->z = minuend->z - subtrahend->z;
}

void xyz_t_mult_v(xyz_t* multiplicand, f32 multiplier) {
    multiplicand->x *= multiplier;
    multiplicand->y *= multiplier;
    multiplicand->z *= multiplier;
}

f32 search_position_distance(xyz_t* subtrahend, xyz_t* minuend) {
    f32 diffX = minuend->x - subtrahend->x;
    f32 diffY = minuend->y - subtrahend->y;
    f32 diffZ = minuend->z - subtrahend->z;

    return sqrtf(SQ(diffX) + SQ(diffY) + SQ(diffZ));
}

f32 search_position_distance2(xyz_t* subtrahend, xyz_t* minuend, xyz_t* dest) {
    dest->x = minuend->x - subtrahend->x;
    dest->y = minuend->y - subtrahend->y;
    dest->z = minuend->z - subtrahend->z;

    return sqrtf(SQ(dest->x) + SQ(dest->y) + SQ(dest->z));
}

f32 search_position_distanceXZ(xyz_t* subtrahend, xyz_t* minuend) {
    f32 diffX = minuend->x - subtrahend->x;
    f32 diffZ = minuend->z - subtrahend->z;

    return sqrtf(SQ(diffX) + SQ(diffZ));
}

f32 search_position_high(xyz_t* subtrahend, xyz_t* minuend) {
    return minuend->y - subtrahend->y;
}

s16 search_position_angleY(xyz_t* subtrahend, xyz_t* minuend) {
    f32 diffX = minuend->x - subtrahend->x;
    f32 diffZ = minuend->z - subtrahend->z;

    return atans_table(diffZ, diffX);
}

s16 search_position_angleX(xyz_t* subtrahend, xyz_t* minuend) {
    f32 distXZ = search_position_distanceXZ(subtrahend, minuend);
    f32 diffY = subtrahend->y - minuend->y;

    return atans_table(distXZ, diffY);
}

void ValueSet_process(u8* ptr, ValueSet* values) {
    do {
        D_80107B00_jp[values->type](ptr, values);
    } while ((values++)->cont);
}

void ValueSet__s_char(u8* ptr, ValueSet* value_set) {
    *(s8*)(ptr + value_set->offset) = value_set->value;
}

void ValueSet__u_char(u8* ptr, ValueSet* value_set) {
    *(u8*)(ptr + value_set->offset) = value_set->value;
}

void ValueSet__s_short(u8* ptr, ValueSet* value_set) {
    *(s16*)(ptr + value_set->offset) = value_set->value;
}

void ValueSet__u_short(u8* ptr, ValueSet* value_set) {
    *(u16*)(ptr + value_set->offset) = value_set->value;
}

void ValueSet__s_int(u8* ptr, ValueSet* value_set) {
    *(s32*)(ptr + value_set->offset) = value_set->value;
}

void ValueSet__u_int(u8* ptr, ValueSet* value_set) {
    *(u32*)(ptr + value_set->offset) = value_set->value;
}

void ValueSet__float(u8* ptr, ValueSet* value_set) {
    *(f32*)(ptr + value_set->offset) = value_set->value;
}

void ValueSet__float_x1000(u8* ptr, ValueSet* value_set) {
    *(f32*)(ptr + value_set->offset) = value_set->value / 1000.0f;
}

void ValueSet__xyz_t(u8* ptr, ValueSet* value_set) {
    xyz_t* vec = (xyz_t*)(ptr + value_set->offset);
    f32 val = value_set->value;

    vec->z = val;
    vec->y = val;
    vec->x = val;
}

void ValueSet__xyz_t_x1000(u8* ptr, ValueSet* value_set) {
    xyz_t* vec = (xyz_t*)(ptr + value_set->offset);
    f32 val = value_set->value / 1000.0f;

    vec->z = val;
    vec->y = val;
    vec->x = val;
}

void ValueSet__s_xyz(u8* ptr, ValueSet* value_set) {
    s_xyz* vec = (s_xyz*)(ptr + value_set->offset);
    s16 val = value_set->value;

    vec->z = val;
    vec->y = val;
    vec->x = val;
}

f32 add_calc(f32* pValue, f32 target, f32 fraction, f32 step, f32 minStep) {
    f32 negMinStep;
    f32 stepSize;

    if (*pValue != target) {
        stepSize = fraction * (target - *pValue);
        negMinStep = -minStep;

        if ((stepSize <= negMinStep) || (minStep <= stepSize)) {
            if (stepSize > step) {
                stepSize = step;
            } else if (stepSize < -step) {
                stepSize = -step;
            }

            *pValue += stepSize;

            if (stepSize > 0.0f) {
                if (*pValue > target) {
                    *pValue = target;
                }
            } else {
                if (*pValue < target) {
                    *pValue = target;
                }
            }
        } else {
            if (stepSize > 0.0f) {
                *pValue += minStep;
                if (*pValue > target) {
                    *pValue = target;
                }
            } else {
                *pValue += negMinStep;
                if (*pValue < target) {
                    *pValue = target;
                }
            }
        }
    }

    return target - *pValue;
}

void add_calc2(f32* pValue, f32 target, f32 fraction, f32 step) {
    f32 stepSize;
    if (*pValue != target) {
        stepSize = fraction * (target - *pValue);

        if (stepSize > step) {
            stepSize = step;
        } else if (stepSize < -step) {
            stepSize = -step;
        }

        *pValue += stepSize;
    }
}

void add_calc0(f32* pValue, f32 fraction, f32 step) {
    f32 stepSize = *pValue * fraction;

    if (stepSize > step) {
        stepSize = step;
    } else if (stepSize < -step) {
        stepSize = -step;
    }

    *pValue -= stepSize;
}

f32 add_calc_a(f32* pValue, f32 target, f32 fraction, f32 step, f32 minStep) {
    f32 stepSize = 0.0f;
    f32 diff = target - *pValue;

    if (target != *pValue) {
        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 (step < stepSize) {
                stepSize = step;
            } else if (stepSize < -step) {
                stepSize = -step;
            }

            *pValue += stepSize;

            if (stepSize > 0.0f) {
                if (target < *pValue) {
                    *pValue = target;
                }
            } else if (*pValue < target) {
                *pValue = target;
            }
        } else {
            if (stepSize > 0.0f) {
                stepSize = minStep;
                *pValue += stepSize;
                if (target < *pValue) {
                    *pValue = target;
                }
            } else {
                stepSize = -minStep;
                *pValue += -minStep;
                if (*pValue < target) {
                    *pValue = target;
                }
            }
        }
    }
    if (*pValue >= 360.0f) {
        *pValue -= 360.0f;
    }
    if (*pValue < 0.0f) {
        *pValue += 360.0f;
    }
    return stepSize;
}

s16 add_calc_short_angle2(s16* pValue, s16 target, f32 fraction, s16 step, s16 minStep) {
    s16 stepSize = 0;
    s16 diff = target - *pValue;

    if (*pValue != target) {
        stepSize = (s32)(diff * fraction);

        if ((minStep < stepSize) || (stepSize < -minStep)) {
            if (step < stepSize) {
                stepSize = step;
            } else if (stepSize < -step) {
                stepSize = -step;
            }

            *pValue += stepSize;

            if (stepSize > 0) {
                if ((s16)(target - *pValue) < 0) {
                    *pValue = target;
                }
            } else {
                if ((s16)(target - *pValue) > 0) {
                    *pValue = target;
                }
            }
        } else {
            if (diff >= 0) {
                *pValue += minStep;
                if ((s16)(target - *pValue) < 0) {
                    *pValue = target;
                }
            } else {
                *pValue -= minStep;
                if ((s16)(target - *pValue) > 0) {
                    *pValue = target;
                }
            }
        }
    }
    return target - *pValue;
}

s16 add_calc_short_angle3(s16* pValue, s16 target, f32 fraction, s16 maxStep, s16 minStep) {
    f32 stepSize;
    s32 uTarget;
    s32 newValue;
    s32 uValue;

    if (target != *pValue) {
        uValue = (u16)*pValue;
        uTarget = (u16)target;

        if (uValue > uTarget) {
            uTarget += 0x10000;
        }

        stepSize = (uTarget - uValue) * fraction;

        if (stepSize > maxStep) {
            stepSize = maxStep;
        } else if (stepSize < minStep) {
            stepSize = minStep;
        }

        newValue = uValue + (s32)stepSize;
        if (newValue > uTarget) {
            newValue = uTarget;
        }
        *pValue = newValue;
    }

    return target - *pValue;
}

void adds(s16* pValue, s16 target, s16 scale, s16 maxStep) {
    s16 diff = target - *pValue;
    diff /= scale;

    if (diff > maxStep) {
        *pValue += maxStep;
        return;
    }

    if (diff < -maxStep) {
        *pValue -= maxStep;
        return;
    }

    *pValue += diff;
}

void rgba_t_move(Color_RGBA8* dest, Color_RGBA8* src) {
    dest->r = src->r;
    dest->g = src->g;
    dest->b = src->b;
    dest->a = src->a;
}

s32 none_proc1() {
    return 0;
}

void none_proc2(Actor* actor UNUSED, Game_Play* play UNUSED) {
}

void Cheap_gfx_display(Game_Play* play, Gfx* dl) {
    //! FAKE
    if ((!(&play->state)) && (!(&play->state))) {}

    OPEN_DISPS(play->state.gfxCtx);

    _texture_z_light_fog_prim(play->state.gfxCtx);

    gSPMatrix(POLY_OPA_DISP++, _Matrix_to_Mtx_new(play->state.gfxCtx), G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_MODELVIEW);

    gSPDisplayList(POLY_OPA_DISP++, dl);

    CLOSE_DISPS(play->state.gfxCtx);
}

void Cheap_gfx_display_xlu(Game_Play* play, Gfx* dl) {
    //! FAKE
    if ((!(&play->state)) && (!(&play->state))) {}

    OPEN_DISPS(play->state.gfxCtx);

    _texture_z_light_fog_prim_xlu(play->state.gfxCtx);

    gSPMatrix(POLY_XLU_DISP++, _Matrix_to_Mtx_new(play->state.gfxCtx), G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_MODELVIEW);

    gSPDisplayList(POLY_XLU_DISP++, dl);

    CLOSE_DISPS(play->state.gfxCtx);
}

s32 _Game_play_isPause(Game_Play* play) {
    return play->pause.enabled != 0;
}

void* Lib_SegmentedToVirtual(void* ptr) {
    return SEGMENTED_TO_K0(ptr);
}

void* Lib_SegmentedToVirtualNull(void* ptr) {
    if (((uintptr_t)ptr >> 28) == 0) {
        return ptr;
    } else {
        return SEGMENTED_TO_K0(ptr);
    }
}

void* PhysicalToVirtual(uintptr_t ptr) {
    if (ptr == 0) {
        return 0;
    }

    return OS_PHYSICAL_TO_K0(ptr);
}

void* PhysicalToVirtualNull(uintptr_t ptr) {
    if (ptr == 0) {
        return 0;
    }

    return OS_PHYSICAL_TO_K0(ptr);
}

f32 check_percent_abs(f32 x, f32 min, f32 max, f32 scale, s32 shiftMin) {
    if ((-min <= x) && (x <= min)) {
        return 0.0f;
    }
    if (x >= max) {
        return 1.0f;
    }
    if (x <= -max) {
        return -1.0f;
    }
    if (shiftMin) {
        if (x > 0.0f) {
            return (x - min) * scale;
        } else {
            return (x + min) * scale;
        }
    } else {
        return x * scale;
    }
}

f32 get_percent_forAccelBrake(f32 x, f32 min, f32 max, f32 accelRange, f32 brakeRange) {
    f32 percent;
    f32 range;
    f32 cur;
    f32 scale;

    if (x >= max) {
        return 1.0f;
    }
    if (x <= min) {
        return 0.0f;
    }

    cur = x - min;
    range = max - min;

    if (range < accelRange + brakeRange) {
        return 0.0f;
    }

    scale = 1.0f / (2.0f * range - accelRange - brakeRange);

    if (accelRange != 0.0f) {
        if (cur <= accelRange) {
            percent = scale * cur * cur;
            percent /= accelRange;
            return percent;
        } else {
            percent = scale * accelRange;
        }
    } else {
        percent = 0.0f;
    }

    if (cur <= range - brakeRange) {
        percent += (scale * 2.0f) * (cur - accelRange);
        return percent;
    }

    percent += 2.0f * scale * (range - accelRange - brakeRange);

    if (brakeRange != 0.0f) {
        percent += scale * brakeRange;
        if (cur < range) {
            f32 left = range - cur;

            percent -= scale * left * left / brakeRange;
        }
    }
    return percent;
}

void Game_play_Projection_Trans(Game_Play* play, xyz_t* src, xyz_t* dst) {
    f32 temp_f0;

    Matrix_mult(&play->viewProjectionMtxF, 0);
    Matrix_Position(src, dst);
    temp_f0 =
        play->viewProjectionMtxF.ww + ((play->viewProjectionMtxF.wx * src->x) + (play->viewProjectionMtxF.wy * src->y) +
                                       (play->viewProjectionMtxF.wz * src->z));
    dst->x = ((dst->x / temp_f0) * 160.0f) + 160.0f;
    dst->y = 120.0f - ((dst->y / temp_f0) * 120.0f);
}

f32 get_percent(s32 max, s32 min, s32 x) {
    f32 denom;
    f32 percent;

    percent = 1.0f;
    if (x < min) {
        percent = 0.0f;
    } else if (x < max) {
        denom = (f32)(max - min);
        if (denom != 0.0f) {
            percent = (f32)(x - min) / denom;
            if (percent > 1.0f) {
                percent = 1.0f;
            }
        }
    }
    return percent;
}