forked from sm64pc/sm64ex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_helpers.c
2911 lines (2366 loc) · 84.7 KB
/
object_helpers.c
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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <PR/ultratypes.h>
#include "sm64.h"
#include "area.h"
#include "behavior_actions.h"
#include "behavior_data.h"
#include "camera.h"
#include "debug.h"
#include "dialog_ids.h"
#include "engine/behavior_script.h"
#include "engine/geo_layout.h"
#include "engine/math_util.h"
#include "engine/surface_collision.h"
#include "game_init.h"
#include "helper_macros.h"
#include "ingame_menu.h"
#include "interaction.h"
#include "level_table.h"
#include "level_update.h"
#include "mario.h"
#include "mario_actions_cutscene.h"
#include "memory.h"
#include "obj_behaviors.h"
#include "object_helpers.h"
#include "object_list_processor.h"
#include "rendering_graph_node.h"
#include "spawn_object.h"
#include "spawn_sound.h"
s8 D_8032F0A0[] = { 0xF8, 0x08, 0xFC, 0x04 };
s16 D_8032F0A4[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
static s8 sLevelsWithRooms[] = { LEVEL_BBH, LEVEL_CASTLE, LEVEL_HMC, -1 };
static s32 clear_move_flag(u32 *, s32);
#define o gCurrentObject
Gfx *geo_update_projectile_pos_from_parent(s32 callContext, UNUSED struct GraphNode *node, Mat4 mtx) {
Mat4 sp20;
struct Object *sp1C;
if (callContext == GEO_CONTEXT_RENDER) {
sp1C = (struct Object *) gCurGraphNodeObject; // TODO: change global type to Object pointer
if (sp1C->prevObj) {
create_transformation_from_matrices(sp20, mtx, *gCurGraphNodeCamera->matrixPtr);
obj_update_pos_from_parent_transformation(sp20, sp1C->prevObj);
obj_set_gfx_pos_from_pos(sp1C->prevObj);
}
}
return NULL;
}
Gfx *geo_update_layer_transparency(s32 callContext, struct GraphNode *node, UNUSED void *context) {
Gfx *dlStart, *dlHead;
struct Object *objectGraphNode;
struct GraphNodeGenerated *currentGraphNode;
UNUSED struct GraphNodeGenerated *sp2C;
s32 objectOpacity;
dlStart = NULL;
if (callContext == GEO_CONTEXT_RENDER) {
objectGraphNode = (struct Object *) gCurGraphNodeObject; // TODO: change this to object pointer?
currentGraphNode = (struct GraphNodeGenerated *) node;
sp2C = (struct GraphNodeGenerated *) node;
if (gCurGraphNodeHeldObject) {
objectGraphNode = gCurGraphNodeHeldObject->objNode;
}
objectOpacity = objectGraphNode->oOpacity;
dlStart = alloc_display_list(sizeof(Gfx) * 3);
dlHead = dlStart;
if (objectOpacity == 0xFF) {
if (currentGraphNode->parameter == 20) {
currentGraphNode->fnNode.node.flags =
0x600 | (currentGraphNode->fnNode.node.flags & 0xFF);
} else {
currentGraphNode->fnNode.node.flags =
0x100 | (currentGraphNode->fnNode.node.flags & 0xFF);
}
objectGraphNode->oAnimState = 0;
} else {
if (currentGraphNode->parameter == 20) {
currentGraphNode->fnNode.node.flags =
0x600 | (currentGraphNode->fnNode.node.flags & 0xFF);
} else {
currentGraphNode->fnNode.node.flags =
0x500 | (currentGraphNode->fnNode.node.flags & 0xFF);
}
objectGraphNode->oAnimState = 1;
#ifdef VERSION_JP
if (currentGraphNode->parameter == 10) {
if (gDebugInfo[DEBUG_PAGE_ENEMYINFO][3]) {
gDPSetAlphaCompare(dlHead++, G_AC_DITHER);
}
} else {
if (objectGraphNode->activeFlags & ACTIVE_FLAG_DITHERED_ALPHA) {
gDPSetAlphaCompare(dlHead++, G_AC_DITHER);
}
}
#else // gDebugInfo accesses were removed in all non-JP versions.
if (objectOpacity == 0 && segmented_to_virtual(bhvBowser) == objectGraphNode->behavior) {
objectGraphNode->oAnimState = 2;
}
// the debug info check was removed in US. so we need to
// perform the only necessary check instead of the debuginfo
// one.
if (currentGraphNode->parameter != 10) {
if (objectGraphNode->activeFlags & ACTIVE_FLAG_DITHERED_ALPHA) {
gDPSetAlphaCompare(dlHead++, G_AC_DITHER);
}
}
#endif
}
gDPSetEnvColor(dlHead++, 255, 255, 255, objectOpacity);
gSPEndDisplayList(dlHead);
}
return dlStart;
}
/**
* @bug Every geo function declares the 3 parameters of callContext, node, and
* the matrix array. This one (see also geo_switch_area) doesn't. When executed,
* the node function executor passes the 3rd argument to a function that doesn't
* declare it. This is undefined behavior, but harmless in practice due to the
* o32 calling convention.
*/
#ifdef AVOID_UB
Gfx *geo_switch_anim_state(s32 callContext, struct GraphNode *node, UNUSED void *context) {
#else
Gfx *geo_switch_anim_state(s32 callContext, struct GraphNode *node) {
#endif
struct Object *obj;
struct GraphNodeSwitchCase *switchCase;
if (callContext == GEO_CONTEXT_RENDER) {
obj = (struct Object *) gCurGraphNodeObject; // TODO: change global type to Object pointer
// move to a local var because GraphNodes are passed in all geo functions.
// cast the pointer.
switchCase = (struct GraphNodeSwitchCase *) node;
if (gCurGraphNodeHeldObject != NULL) {
obj = gCurGraphNodeHeldObject->objNode;
}
// if the case is greater than the number of cases, set to 0 to avoid overflowing
// the switch.
if (obj->oAnimState >= switchCase->numCases) {
obj->oAnimState = 0;
}
// assign the case number for execution.
switchCase->selectedCase = obj->oAnimState;
}
return NULL;
}
//! @bug Same issue as geo_switch_anim_state.
#ifdef AVOID_UB
Gfx *geo_switch_area(s32 callContext, struct GraphNode *node, UNUSED void *context) {
#else
Gfx *geo_switch_area(s32 callContext, struct GraphNode *node) {
#endif
s16 sp26;
struct Surface *sp20;
UNUSED struct Object *sp1C =
(struct Object *) gCurGraphNodeObject; // TODO: change global type to Object pointer
struct GraphNodeSwitchCase *switchCase = (struct GraphNodeSwitchCase *) node;
if (callContext == GEO_CONTEXT_RENDER) {
if (gMarioObject == NULL) {
switchCase->selectedCase = 0;
} else {
gFindFloorIncludeSurfaceIntangible = TRUE;
find_floor(gMarioObject->oPosX, gMarioObject->oPosY, gMarioObject->oPosZ, &sp20);
if (sp20) {
gMarioCurrentRoom = sp20->room;
sp26 = sp20->room - 1;
print_debug_top_down_objectinfo("areainfo %d", sp20->room);
if (sp26 >= 0) {
switchCase->selectedCase = sp26;
}
}
}
} else {
switchCase->selectedCase = 0;
}
return NULL;
}
void obj_update_pos_from_parent_transformation(Mat4 a0, struct Object *a1) {
f32 spC, sp8, sp4;
spC = a1->oParentRelativePosX;
sp8 = a1->oParentRelativePosY;
sp4 = a1->oParentRelativePosZ;
a1->oPosX = spC * a0[0][0] + sp8 * a0[1][0] + sp4 * a0[2][0] + a0[3][0];
a1->oPosY = spC * a0[0][1] + sp8 * a0[1][1] + sp4 * a0[2][1] + a0[3][1];
a1->oPosZ = spC * a0[0][2] + sp8 * a0[1][2] + sp4 * a0[2][2] + a0[3][2];
}
void obj_apply_scale_to_matrix(struct Object *obj, Mat4 dst, Mat4 src) {
dst[0][0] = src[0][0] * obj->header.gfx.scale[0];
dst[1][0] = src[1][0] * obj->header.gfx.scale[1];
dst[2][0] = src[2][0] * obj->header.gfx.scale[2];
dst[3][0] = src[3][0];
dst[0][1] = src[0][1] * obj->header.gfx.scale[0];
dst[1][1] = src[1][1] * obj->header.gfx.scale[1];
dst[2][1] = src[2][1] * obj->header.gfx.scale[2];
dst[3][1] = src[3][1];
dst[0][2] = src[0][2] * obj->header.gfx.scale[0];
dst[1][2] = src[1][2] * obj->header.gfx.scale[1];
dst[2][2] = src[2][2] * obj->header.gfx.scale[2];
dst[3][2] = src[3][2];
dst[0][3] = src[0][3];
dst[1][3] = src[1][3];
dst[2][3] = src[2][3];
dst[3][3] = src[3][3];
}
void create_transformation_from_matrices(Mat4 a0, Mat4 a1, Mat4 a2) {
f32 spC, sp8, sp4;
spC = a2[3][0] * a2[0][0] + a2[3][1] * a2[0][1] + a2[3][2] * a2[0][2];
sp8 = a2[3][0] * a2[1][0] + a2[3][1] * a2[1][1] + a2[3][2] * a2[1][2];
sp4 = a2[3][0] * a2[2][0] + a2[3][1] * a2[2][1] + a2[3][2] * a2[2][2];
a0[0][0] = a1[0][0] * a2[0][0] + a1[0][1] * a2[0][1] + a1[0][2] * a2[0][2];
a0[0][1] = a1[0][0] * a2[1][0] + a1[0][1] * a2[1][1] + a1[0][2] * a2[1][2];
a0[0][2] = a1[0][0] * a2[2][0] + a1[0][1] * a2[2][1] + a1[0][2] * a2[2][2];
a0[1][0] = a1[1][0] * a2[0][0] + a1[1][1] * a2[0][1] + a1[1][2] * a2[0][2];
a0[1][1] = a1[1][0] * a2[1][0] + a1[1][1] * a2[1][1] + a1[1][2] * a2[1][2];
a0[1][2] = a1[1][0] * a2[2][0] + a1[1][1] * a2[2][1] + a1[1][2] * a2[2][2];
a0[2][0] = a1[2][0] * a2[0][0] + a1[2][1] * a2[0][1] + a1[2][2] * a2[0][2];
a0[2][1] = a1[2][0] * a2[1][0] + a1[2][1] * a2[1][1] + a1[2][2] * a2[1][2];
a0[2][2] = a1[2][0] * a2[2][0] + a1[2][1] * a2[2][1] + a1[2][2] * a2[2][2];
a0[3][0] = a1[3][0] * a2[0][0] + a1[3][1] * a2[0][1] + a1[3][2] * a2[0][2] - spC;
a0[3][1] = a1[3][0] * a2[1][0] + a1[3][1] * a2[1][1] + a1[3][2] * a2[1][2] - sp8;
a0[3][2] = a1[3][0] * a2[2][0] + a1[3][1] * a2[2][1] + a1[3][2] * a2[2][2] - sp4;
a0[0][3] = 0;
a0[1][3] = 0;
a0[2][3] = 0;
a0[3][3] = 1.0f;
}
void obj_set_held_state(struct Object *obj, const BehaviorScript *heldBehavior) {
obj->parentObj = o;
if (obj->oFlags & OBJ_FLAG_HOLDABLE) {
if (heldBehavior == bhvCarrySomething3) {
obj->oHeldState = HELD_HELD;
}
if (heldBehavior == bhvCarrySomething5) {
obj->oHeldState = HELD_THROWN;
}
if (heldBehavior == bhvCarrySomething4) {
obj->oHeldState = HELD_DROPPED;
}
} else {
obj->curBhvCommand = segmented_to_virtual(heldBehavior);
obj->bhvStackIndex = 0;
}
}
f32 lateral_dist_between_objects(struct Object *obj1, struct Object *obj2) {
f32 dx = obj1->oPosX - obj2->oPosX;
f32 dz = obj1->oPosZ - obj2->oPosZ;
return sqrtf(dx * dx + dz * dz);
}
f32 dist_between_objects(struct Object *obj1, struct Object *obj2) {
f32 dx = obj1->oPosX - obj2->oPosX;
f32 dy = obj1->oPosY - obj2->oPosY;
f32 dz = obj1->oPosZ - obj2->oPosZ;
return sqrtf(dx * dx + dy * dy + dz * dz);
}
void cur_obj_forward_vel_approach_upward(f32 target, f32 increment) {
if (o->oForwardVel >= target) {
o->oForwardVel = target;
} else {
o->oForwardVel += increment;
}
}
s32 approach_f32_signed(f32 *value, f32 target, f32 increment) {
s32 reachedTarget = FALSE;
*value += increment;
if (increment >= 0.0f) {
if (*value > target) {
*value = target;
reachedTarget = TRUE;
}
} else {
if (*value < target) {
*value = target;
reachedTarget = TRUE;
}
}
return reachedTarget;
}
f32 approach_f32_symmetric(f32 value, f32 target, f32 increment) {
f32 dist;
if ((dist = target - value) >= 0.0f) {
if (dist > increment) {
value += increment;
} else {
value = target;
}
} else {
if (dist < -increment) {
value -= increment;
} else {
value = target;
}
}
return value;
}
s16 approach_s16_symmetric(s16 value, s16 target, s16 increment) {
s16 dist = target - value;
if (dist >= 0) {
if (dist > increment) {
value += increment;
} else {
value = target;
}
} else {
if (dist < -increment) {
value -= increment;
} else {
value = target;
}
}
return value;
}
s32 cur_obj_rotate_yaw_toward(s16 target, s16 increment) {
s16 startYaw;
startYaw = (s16) o->oMoveAngleYaw;
o->oMoveAngleYaw = approach_s16_symmetric(o->oMoveAngleYaw, target, increment);
if ((o->oAngleVelYaw = (s16)((s16) o->oMoveAngleYaw - startYaw)) == 0) {
return TRUE;
} else {
return FALSE;
}
}
s16 obj_angle_to_object(struct Object *obj1, struct Object *obj2) {
f32 z1, x1, z2, x2;
s16 angle;
z1 = obj1->oPosZ; z2 = obj2->oPosZ; //ordering of instructions..
x1 = obj1->oPosX; x2 = obj2->oPosX;
angle = atan2s(z2 - z1, x2 - x1);
return angle;
}
s16 obj_turn_toward_object(struct Object *obj, struct Object *target, s16 angleIndex, s16 turnAmount) {
f32 a, b, c, d;
UNUSED s32 unused;
s16 targetAngle, startAngle;
switch (angleIndex) {
case O_MOVE_ANGLE_PITCH_INDEX:
case O_FACE_ANGLE_PITCH_INDEX:
a = target->oPosX - obj->oPosX;
c = target->oPosZ - obj->oPosZ;
a = sqrtf(a * a + c * c);
b = -obj->oPosY;
d = -target->oPosY;
targetAngle = atan2s(a, d - b);
break;
case O_MOVE_ANGLE_YAW_INDEX:
case O_FACE_ANGLE_YAW_INDEX:
a = obj->oPosZ;
c = target->oPosZ;
b = obj->oPosX;
d = target->oPosX;
targetAngle = atan2s(c - a, d - b);
break;
}
startAngle = o->rawData.asU32[angleIndex];
o->rawData.asU32[angleIndex] = approach_s16_symmetric(startAngle, targetAngle, turnAmount);
return targetAngle;
}
void obj_set_parent_relative_pos(struct Object *obj, s16 relX, s16 relY, s16 relZ) {
obj->oParentRelativePosX = relX;
obj->oParentRelativePosY = relY;
obj->oParentRelativePosZ = relZ;
}
void obj_set_pos(struct Object *obj, s16 x, s16 y, s16 z) {
obj->oPosX = x;
obj->oPosY = y;
obj->oPosZ = z;
}
void obj_set_angle(struct Object *obj, s16 pitch, s16 yaw, s16 roll) {
obj->oFaceAnglePitch = pitch;
obj->oFaceAngleYaw = yaw;
obj->oFaceAngleRoll = roll;
obj->oMoveAnglePitch = pitch;
obj->oMoveAngleYaw = yaw;
obj->oMoveAngleRoll = roll;
}
/*
* Spawns an object at an absolute location with a specified angle.
*/
struct Object *spawn_object_abs_with_rot(struct Object *parent, s16 uselessArg, u32 model,
const BehaviorScript *behavior,
s16 x, s16 y, s16 z, s16 rx, s16 ry, s16 rz) {
// 'uselessArg' is unused in the function spawn_object_at_origin()
struct Object *newObj = spawn_object_at_origin(parent, uselessArg, model, behavior);
obj_set_pos(newObj, x, y, z);
obj_set_angle(newObj, rx, ry, rz);
return newObj;
}
/*
* Spawns an object relative to the parent with a specified angle... is what it is supposed to do.
* The rz argument is never used, and the z offset is used for z-rotation instead. This is most likely
* a copy-paste typo by one of the programmers.
*/
struct Object *spawn_object_rel_with_rot(struct Object *parent, u32 model, const BehaviorScript *behavior,
s16 xOff, s16 yOff, s16 zOff, s16 rx, s16 ry, UNUSED s16 rz) {
struct Object *newObj = spawn_object_at_origin(parent, 0, model, behavior);
newObj->oFlags |= OBJ_FLAG_TRANSFORM_RELATIVE_TO_PARENT;
obj_set_parent_relative_pos(newObj, xOff, yOff, zOff);
obj_set_angle(newObj, rx, ry, zOff); // Nice typo you got there Nintendo.
return newObj;
}
struct Object *spawn_obj_with_transform_flags(struct Object *sp20, s32 model, const BehaviorScript *sp28) {
struct Object *sp1C = spawn_object(sp20, model, sp28);
sp1C->oFlags |= OBJ_FLAG_0020 | OBJ_FLAG_SET_THROW_MATRIX_FROM_TRANSFORM;
return sp1C;
}
struct Object *spawn_water_droplet(struct Object *parent, struct WaterDropletParams *params) {
f32 randomScale;
struct Object *newObj = spawn_object(parent, params->model, params->behavior);
if (params->flags & WATER_DROPLET_FLAG_RAND_ANGLE) {
newObj->oMoveAngleYaw = random_u16();
}
if (params->flags & WATER_DROPLET_FLAG_RAND_ANGLE_INCR_PLUS_8000) {
newObj->oMoveAngleYaw = (s16)(newObj->oMoveAngleYaw + 0x8000)
+ (s16) random_f32_around_zero(params->moveAngleRange);
}
if (params->flags & WATER_DROPLET_FLAG_RAND_ANGLE_INCR) {
newObj->oMoveAngleYaw =
(s16) newObj->oMoveAngleYaw + (s16) random_f32_around_zero(params->moveAngleRange);
}
if (params->flags & WATER_DROPLET_FLAG_SET_Y_TO_WATER_LEVEL) {
newObj->oPosY = find_water_level(newObj->oPosX, newObj->oPosZ);
}
if (params->flags & WATER_DROPLET_FLAG_RAND_OFFSET_XZ) {
obj_translate_xz_random(newObj, params->moveRange);
}
if (params->flags & WATER_DROPLET_FLAG_RAND_OFFSET_XYZ) {
obj_translate_xyz_random(newObj, params->moveRange);
}
newObj->oForwardVel = random_float() * params->randForwardVelScale + params->randForwardVelOffset;
newObj->oVelY = random_float() * params->randYVelScale + params->randYVelOffset;
randomScale = random_float() * params->randSizeScale + params->randSizeOffset;
obj_scale(newObj, randomScale);
return newObj;
}
struct Object *spawn_object_at_origin(struct Object *parent, UNUSED s32 unusedArg, u32 model,
const BehaviorScript *behavior) {
struct Object *obj;
const BehaviorScript *behaviorAddr;
behaviorAddr = segmented_to_virtual(behavior);
obj = create_object(behaviorAddr);
obj->parentObj = parent;
obj->header.gfx.unk18 = parent->header.gfx.unk18;
obj->header.gfx.unk19 = parent->header.gfx.unk18;
geo_obj_init((struct GraphNodeObject *) &obj->header.gfx, gLoadedGraphNodes[model], gVec3fZero,
gVec3sZero);
return obj;
}
struct Object *spawn_object(struct Object *parent, s32 model, const BehaviorScript *behavior) {
struct Object *obj;
obj = spawn_object_at_origin(parent, 0, model, behavior);
obj_copy_pos_and_angle(obj, parent);
return obj;
}
struct Object *try_to_spawn_object(s16 offsetY, f32 scale, struct Object *parent, s32 model,
const BehaviorScript *behavior) {
struct Object *obj;
if (gFreeObjectList.next != NULL) {
obj = spawn_object(parent, model, behavior);
obj->oPosY += offsetY;
obj_scale(obj, scale);
return obj;
} else {
return NULL;
}
}
struct Object *spawn_object_with_scale(struct Object *parent, s32 model, const BehaviorScript *behavior, f32 scale) {
struct Object *obj;
obj = spawn_object_at_origin(parent, 0, model, behavior);
obj_copy_pos_and_angle(obj, parent);
obj_scale(obj, scale);
return obj;
}
static void obj_build_relative_transform(struct Object *obj) {
obj_build_transform_from_pos_and_angle(obj, O_PARENT_RELATIVE_POS_INDEX, O_FACE_ANGLE_INDEX);
obj_translate_local(obj, O_POS_INDEX, O_PARENT_RELATIVE_POS_INDEX);
}
struct Object *spawn_object_relative(s16 behaviorParam, s16 relativePosX, s16 relativePosY, s16 relativePosZ,
struct Object *parent, s32 model, const BehaviorScript *behavior) {
struct Object *obj = spawn_object_at_origin(parent, 0, model, behavior);
obj_copy_pos_and_angle(obj, parent);
obj_set_parent_relative_pos(obj, relativePosX, relativePosY, relativePosZ);
obj_build_relative_transform(obj);
obj->oBehParams2ndByte = behaviorParam;
obj->oBehParams = (behaviorParam & 0xFF) << 16;
return obj;
}
struct Object *spawn_object_relative_with_scale(s16 behaviorParam, s16 relativePosX, s16 relativePosY,
s16 relativePosZ, f32 scale, struct Object *parent,
s32 model, const BehaviorScript *behavior) {
struct Object *obj;
obj = spawn_object_relative(behaviorParam, relativePosX, relativePosY, relativePosZ, parent, model,
behavior);
obj_scale(obj, scale);
return obj;
}
void cur_obj_move_using_vel(void) {
o->oPosX += o->oVelX;
o->oPosY += o->oVelY;
o->oPosZ += o->oVelZ;
}
void obj_copy_graph_y_offset(struct Object *dst, struct Object *src) {
dst->oGraphYOffset = src->oGraphYOffset;
}
void obj_copy_pos_and_angle(struct Object *dst, struct Object *src) {
obj_copy_pos(dst, src);
obj_copy_angle(dst, src);
}
void obj_copy_pos(struct Object *dst, struct Object *src) {
dst->oPosX = src->oPosX;
dst->oPosY = src->oPosY;
dst->oPosZ = src->oPosZ;
}
void obj_copy_angle(struct Object *dst, struct Object *src) {
dst->oMoveAnglePitch = src->oMoveAnglePitch;
dst->oMoveAngleYaw = src->oMoveAngleYaw;
dst->oMoveAngleRoll = src->oMoveAngleRoll;
dst->oFaceAnglePitch = src->oFaceAnglePitch;
dst->oFaceAngleYaw = src->oFaceAngleYaw;
dst->oFaceAngleRoll = src->oFaceAngleRoll;
}
void obj_set_gfx_pos_from_pos(struct Object *obj) {
obj->header.gfx.pos[0] = obj->oPosX;
obj->header.gfx.pos[1] = obj->oPosY;
obj->header.gfx.pos[2] = obj->oPosZ;
}
void obj_init_animation(struct Object *obj, s32 animIndex) {
struct Animation **anims = o->oAnimations;
geo_obj_init_animation(&obj->header.gfx, &anims[animIndex]);
}
/**
* Multiply a vector by a matrix of the form
* | ? ? ? 0 |
* | ? ? ? 0 |
* | ? ? ? 0 |
* | 0 0 0 1 |
* i.e. a matrix representing a linear transformation over 3 space.
*/
void linear_mtxf_mul_vec3f(Mat4 m, Vec3f dst, Vec3f v) {
s32 i;
for (i = 0; i < 3; i++) {
dst[i] = m[0][i] * v[0] + m[1][i] * v[1] + m[2][i] * v[2];
}
}
/**
* Multiply a vector by the transpose of a matrix of the form
* | ? ? ? 0 |
* | ? ? ? 0 |
* | ? ? ? 0 |
* | 0 0 0 1 |
* i.e. a matrix representing a linear transformation over 3 space.
*/
void linear_mtxf_transpose_mul_vec3f(Mat4 m, Vec3f dst, Vec3f v) {
s32 i;
for (i = 0; i < 3; i++) {
dst[i] = m[i][0] * v[0] + m[i][1] * v[1] + m[i][2] * v[2];
}
}
void obj_apply_scale_to_transform(struct Object *obj) {
f32 scaleX, scaleY, scaleZ;
scaleX = obj->header.gfx.scale[0];
scaleY = obj->header.gfx.scale[1];
scaleZ = obj->header.gfx.scale[2];
obj->transform[0][0] *= scaleX;
obj->transform[0][1] *= scaleX;
obj->transform[0][2] *= scaleX;
obj->transform[1][0] *= scaleY;
obj->transform[1][1] *= scaleY;
obj->transform[1][2] *= scaleY;
obj->transform[2][0] *= scaleZ;
obj->transform[2][1] *= scaleZ;
obj->transform[2][2] *= scaleZ;
}
void obj_copy_scale(struct Object *dst, struct Object *src) {
dst->header.gfx.scale[0] = src->header.gfx.scale[0];
dst->header.gfx.scale[1] = src->header.gfx.scale[1];
dst->header.gfx.scale[2] = src->header.gfx.scale[2];
}
void obj_scale_xyz(struct Object *obj, f32 xScale, f32 yScale, f32 zScale) {
obj->header.gfx.scale[0] = xScale;
obj->header.gfx.scale[1] = yScale;
obj->header.gfx.scale[2] = zScale;
}
void obj_scale(struct Object *obj, f32 scale) {
obj->header.gfx.scale[0] = scale;
obj->header.gfx.scale[1] = scale;
obj->header.gfx.scale[2] = scale;
}
void cur_obj_scale(f32 scale) {
o->header.gfx.scale[0] = scale;
o->header.gfx.scale[1] = scale;
o->header.gfx.scale[2] = scale;
}
void cur_obj_init_animation(s32 animIndex) {
struct Animation **anims = o->oAnimations;
geo_obj_init_animation(&o->header.gfx, &anims[animIndex]);
}
void cur_obj_init_animation_with_sound(s32 animIndex) {
struct Animation **anims = o->oAnimations;
geo_obj_init_animation(&o->header.gfx, &anims[animIndex]);
o->oSoundStateID = animIndex;
}
void cur_obj_init_animation_with_accel_and_sound(s32 animIndex, f32 accel) {
struct Animation **anims = o->oAnimations;
s32 animAccel = (s32)(accel * 65536.0f);
geo_obj_init_animation_accel(&o->header.gfx, &anims[animIndex], animAccel);
o->oSoundStateID = animIndex;
}
void obj_init_animation_with_sound(struct Object *obj, const struct Animation * const* animations, s32 animIndex) {
struct Animation **anims = (struct Animation **)animations;
obj->oAnimations = (struct Animation **)animations;
geo_obj_init_animation(&obj->header.gfx, &anims[animIndex]);
obj->oSoundStateID = animIndex;
}
void cur_obj_enable_rendering_and_become_tangible(struct Object *obj) {
obj->header.gfx.node.flags |= GRAPH_RENDER_ACTIVE;
obj->oIntangibleTimer = 0;
}
void cur_obj_enable_rendering(void) {
o->header.gfx.node.flags |= GRAPH_RENDER_ACTIVE;
}
void cur_obj_disable_rendering_and_become_intangible(struct Object *obj) {
obj->header.gfx.node.flags &= ~GRAPH_RENDER_ACTIVE;
obj->oIntangibleTimer = -1;
}
void cur_obj_disable_rendering(void) {
o->header.gfx.node.flags &= ~GRAPH_RENDER_ACTIVE;
}
void cur_obj_unhide(void) {
o->header.gfx.node.flags &= ~GRAPH_RENDER_INVISIBLE;
}
void cur_obj_hide(void) {
o->header.gfx.node.flags |= GRAPH_RENDER_INVISIBLE;
}
void cur_obj_set_pos_relative(struct Object *other, f32 dleft, f32 dy, f32 dforward) {
f32 facingZ = coss(other->oMoveAngleYaw);
f32 facingX = sins(other->oMoveAngleYaw);
f32 dz = dforward * facingZ - dleft * facingX;
f32 dx = dforward * facingX + dleft * facingZ;
o->oMoveAngleYaw = other->oMoveAngleYaw;
o->oPosX = other->oPosX + dx;
o->oPosY = other->oPosY + dy;
o->oPosZ = other->oPosZ + dz;
}
void cur_obj_set_pos_relative_to_parent(f32 dleft, f32 dy, f32 dforward) {
cur_obj_set_pos_relative(o->parentObj, dleft, dy, dforward);
}
void cur_obj_enable_rendering_2(void) {
cur_obj_enable_rendering();
}
void cur_obj_unused_init_on_floor(void) {
cur_obj_enable_rendering();
o->oPosY = find_floor_height(o->oPosX, o->oPosY, o->oPosZ);
if (o->oPosY < -10000.0f) {
cur_obj_set_pos_relative_to_parent(0, 0, -70);
o->oPosY = find_floor_height(o->oPosX, o->oPosY, o->oPosZ);
}
}
void obj_set_face_angle_to_move_angle(struct Object *obj) {
obj->oFaceAnglePitch = obj->oMoveAnglePitch;
obj->oFaceAngleYaw = obj->oMoveAngleYaw;
obj->oFaceAngleRoll = obj->oMoveAngleRoll;
}
u32 get_object_list_from_behavior(const BehaviorScript *behavior) {
u32 objectList;
// If the first behavior command is "begin", then get the object list header
// from there
if ((behavior[0] >> 24) == 0) {
objectList = (behavior[0] >> 16) & 0xFFFF;
} else {
objectList = OBJ_LIST_DEFAULT;
}
return objectList;
}
struct Object *cur_obj_nearest_object_with_behavior(const BehaviorScript *behavior) {
struct Object *obj;
f32 dist;
obj = cur_obj_find_nearest_object_with_behavior(behavior, &dist);
return obj;
}
f32 cur_obj_dist_to_nearest_object_with_behavior(const BehaviorScript *behavior) {
struct Object *obj;
f32 dist;
obj = cur_obj_find_nearest_object_with_behavior(behavior, &dist);
if (obj == NULL) {
dist = 15000.0f;
}
return dist;
}
struct Object *cur_obj_find_nearest_object_with_behavior(const BehaviorScript *behavior, f32 *dist) {
uintptr_t *behaviorAddr = segmented_to_virtual(behavior);
struct Object *closestObj = NULL;
struct Object *obj;
struct ObjectNode *listHead;
f32 minDist = 0x20000;
listHead = &gObjectLists[get_object_list_from_behavior(behaviorAddr)];
obj = (struct Object *) listHead->next;
while (obj != (struct Object *) listHead) {
if (obj->behavior == behaviorAddr) {
if (obj->activeFlags != ACTIVE_FLAG_DEACTIVATED && obj != o) {
f32 objDist = dist_between_objects(o, obj);
if (objDist < minDist) {
closestObj = obj;
minDist = objDist;
}
}
}
obj = (struct Object *) obj->header.next;
}
*dist = minDist;
return closestObj;
}
struct Object *find_unimportant_object(void) {
struct ObjectNode *listHead = &gObjectLists[OBJ_LIST_UNIMPORTANT];
struct ObjectNode *obj = listHead->next;
if (listHead == obj) {
obj = NULL;
}
return (struct Object *) obj;
}
s32 count_unimportant_objects(void) {
struct ObjectNode *listHead = &gObjectLists[OBJ_LIST_UNIMPORTANT];
struct ObjectNode *obj = listHead->next;
s32 count = 0;
while (listHead != obj) {
count++;
obj = obj->next;
}
return count;
}
s32 count_objects_with_behavior(const BehaviorScript *behavior) {
uintptr_t *behaviorAddr = segmented_to_virtual(behavior);
struct ObjectNode *listHead = &gObjectLists[get_object_list_from_behavior(behaviorAddr)];
struct ObjectNode *obj = listHead->next;
s32 count = 0;
while (listHead != obj) {
if (((struct Object *) obj)->behavior == behaviorAddr) {
count++;
}
obj = obj->next;
}
return count;
}
struct Object *cur_obj_find_nearby_held_actor(const BehaviorScript *behavior, f32 maxDist) {
const BehaviorScript *behaviorAddr = segmented_to_virtual(behavior);
struct ObjectNode *listHead;
struct Object *obj;
struct Object *foundObj;
listHead = &gObjectLists[OBJ_LIST_GENACTOR];
obj = (struct Object *) listHead->next;
foundObj = NULL;
while ((struct Object *) listHead != obj) {
if (obj->behavior == behaviorAddr) {
if (obj->activeFlags != ACTIVE_FLAG_DEACTIVATED) {
// This includes the dropped and thrown states. By combining instant
// release, this allows us to activate mama penguin remotely
if (obj->oHeldState != HELD_FREE) {
if (dist_between_objects(o, obj) < maxDist) {
foundObj = obj;
break;
}
}
}
}
obj = (struct Object *) obj->header.next;
}
return foundObj;
}
static void cur_obj_reset_timer_and_subaction(void) {
o->oTimer = 0;
o->oSubAction = 0;
}
void cur_obj_change_action(s32 action) {
o->oAction = action;
o->oPrevAction = action;
cur_obj_reset_timer_and_subaction();
}
void cur_obj_set_vel_from_mario_vel(f32 f12, f32 f14) {
f32 sp4 = gMarioStates[0].forwardVel;
f32 sp0 = f12 * f14;
if (sp4 < sp0) {
o->oForwardVel = sp0;
} else {
o->oForwardVel = sp4 * f14;
}
}
BAD_RETURN(s16) cur_obj_reverse_animation(void) {
if (o->header.gfx.unk38.animFrame >= 0) {
o->header.gfx.unk38.animFrame--;
}
}
BAD_RETURN(s32) cur_obj_extend_animation_if_at_end(void) {
s32 sp4 = o->header.gfx.unk38.animFrame;
s32 sp0 = o->header.gfx.unk38.curAnim->unk08 - 2;
if (sp4 == sp0) o->header.gfx.unk38.animFrame--;
}
s32 cur_obj_check_if_near_animation_end(void) {
u32 spC = (s32) o->header.gfx.unk38.curAnim->flags;
s32 sp8 = o->header.gfx.unk38.animFrame;
s32 sp4 = o->header.gfx.unk38.curAnim->unk08 - 2;
s32 sp0 = FALSE;
if (spC & 0x01) {
if (sp4 + 1 == sp8) {
sp0 = TRUE;
}
}
if (sp8 == sp4) {
sp0 = TRUE;
}
return sp0;
}