From f17e85d1fd2a124ddde9adaecbb044db341ec0d4 Mon Sep 17 00:00:00 2001 From: mzxrules Date: Wed, 11 Oct 2023 08:32:02 -0400 Subject: [PATCH] ovl_Obj_Grass_Unit OK and some grass documentation (#1172) * z_obj_grass_unit OK * document some grass * pr suggestions, add grass_unit comment * pr suggestions * implement constants --- include/z64math.h | 9 +- spec | 3 +- .../actors/ovl_Obj_Grass/z_obj_grass.c | 380 +++++++++--------- .../actors/ovl_Obj_Grass/z_obj_grass.h | 54 ++- .../ovl_Obj_Grass_Carry/z_obj_grass_carry.c | 283 +++++++------ .../ovl_Obj_Grass_Carry/z_obj_grass_carry.h | 10 +- .../ovl_Obj_Grass_Unit/z_obj_grass_unit.c | 166 +++++++- .../ovl_Obj_Grass_Unit/z_obj_grass_unit.h | 3 + 8 files changed, 533 insertions(+), 375 deletions(-) diff --git a/include/z64math.h b/include/z64math.h index 6fa64f4f43..4a4d15b98d 100644 --- a/include/z64math.h +++ b/include/z64math.h @@ -39,6 +39,11 @@ typedef struct { /* 0x8 */ s32 z; } Vec3i; // size = 0xC +typedef struct { + /* 0x0 */ f32 distance; + /* 0x4 */ s16 angle; +} VecPolar; // size = 0x8 + typedef struct { /* 0x0 */ Vec3s center; /* 0x6 */ s16 radius; @@ -49,8 +54,8 @@ typedef struct { /* 0xC */ f32 radius; } Spheref; // size = 0x10 -/* -The plane paramaters are of form `ax + by + cz + d = 0` +/* +The plane paramaters are of form `ax + by + cz + d = 0` where `(a,b,c)` is the plane's normal vector and d is the originDist */ typedef struct { diff --git a/spec b/spec index e4508b5bd4..7aab83ed8b 100644 --- a/spec +++ b/spec @@ -2137,8 +2137,7 @@ beginseg name "ovl_Obj_Grass_Unit" compress include "build/src/overlays/actors/ovl_Obj_Grass_Unit/z_obj_grass_unit.o" - include "build/data/ovl_Obj_Grass_Unit/ovl_Obj_Grass_Unit.data.o" - include "build/data/ovl_Obj_Grass_Unit/ovl_Obj_Grass_Unit.reloc.o" + include "build/src/overlays/actors/ovl_Obj_Grass_Unit/ovl_Obj_Grass_Unit_reloc.o" endseg beginseg diff --git a/src/overlays/actors/ovl_Obj_Grass/z_obj_grass.c b/src/overlays/actors/ovl_Obj_Grass/z_obj_grass.c index e95f971c9e..a1ce45b829 100644 --- a/src/overlays/actors/ovl_Obj_Grass/z_obj_grass.c +++ b/src/overlays/actors/ovl_Obj_Grass/z_obj_grass.c @@ -1,7 +1,9 @@ /* * File: z_obj_grass.c * Overlay: ovl_Obj_Grass - * Description: "Master" instance of grass for unit spawned by Obj_Grass_Unit + * Description: Optimized manager for processing grass "spawned" by ObjGrassUnit + * + * Instances of ObjGrass must be spawned by ObjGrassUnit to function correctly. */ #include "prevent_bss_reordering.h" @@ -18,10 +20,10 @@ void ObjGrass_Destroy(Actor* thisx, PlayState* play); void ObjGrass_Update(Actor* thisx, PlayState* play); void ObjGrass_Draw(Actor* thisx, PlayState* play); -ObjGrassStruct1* D_809AADA0[4]; -f32 D_809AADB0[4]; -ObjGrassStruct1_1* D_809AADC0[20]; -f32 D_809AAE10[20]; +ObjGrassGroup* sNearestGrassGroups[OBJ_GRASS_NEAREST_GROUP_MAX]; +f32 sNearestGrassGroupsDist[OBJ_GRASS_NEAREST_GROUP_MAX]; +ObjGrassElement* sNearestGrassElements[OBJ_GRASS_NEAREST_ELEM_MAX]; +f32 sNearestGrassElementsDistSq[OBJ_GRASS_NEAREST_ELEM_MAX]; #include "overlays/ovl_Obj_Grass/ovl_Obj_Grass.c" @@ -57,35 +59,35 @@ static ColliderCylinderInit sCylinderInit = { { 6, 44, 0, { 0, 0, 0 } }, }; -Vec3f D_809AAB4C[] = { - { 0.0f, 0.707099974155f, 0.707099974155f }, - { 0.707099974155f, 0.707099974155f, 0.0f }, - { 0.0f, 0.707099974155f, -0.707099974155f }, - { -0.707099974155f, 0.707099974155f, 0.0f }, +static Vec3f sUnitDirections[] = { + { 0.0f, 0.7071f, 0.7071f }, + { 0.7071f, 0.7071f, 0.0f }, + { 0.0f, 0.7071f, -0.7071f }, + { -0.7071f, 0.7071f, 0.0f }, }; -s16 D_809AAB7C[] = { 0x6C, 0x66, 0x60, 0x54, 0x42, 0x37, 0x2A, 0x26 }; +static s16 sFragmentScales[] = { 108, 102, 96, 84, 66, 55, 42, 38 }; -s32 func_809A9110(PlayState* play, Vec3f* arg1) { - f32 sp2C; - Vec3f sp20; +s32 func_809A9110(PlayState* play, Vec3f* pos) { + f32 w; + Vec3f projectedPos; - SkinMatrix_Vec3fMtxFMultXYZW(&play->viewProjectionMtxF, arg1, &sp20, &sp2C); + SkinMatrix_Vec3fMtxFMultXYZW(&play->viewProjectionMtxF, pos, &projectedPos, &w); - if ((play->projectionMtxFDiagonal.z * -130.13191f) < sp20.z) { - if (sp2C < 1.0f) { - sp2C = 1.0f; + if ((play->projectionMtxFDiagonal.z * -130.13191f) < projectedPos.z) { + if (w < 1.0f) { + w = 1.0f; } - if (((fabsf(sp20.x) - (130.13191f * play->projectionMtxFDiagonal.x)) < sp2C) && - ((fabsf(sp20.y) - (130.13191f * play->projectionMtxFDiagonal.y)) < sp2C)) { + if (((fabsf(projectedPos.x) - (130.13191f * play->projectionMtxFDiagonal.x)) < w) && + ((fabsf(projectedPos.y) - (130.13191f * play->projectionMtxFDiagonal.y)) < w)) { return true; } } return false; } -void func_809A91FC(MtxF* matrix) { +void ObjGrass_OverrideMatrixCurrent(MtxF* matrix) { s32 i; MtxF* temp = Matrix_GetCurrent(); f32* tmp = &temp->xx; @@ -96,42 +98,44 @@ void func_809A91FC(MtxF* matrix) { } } -void func_809A92D0(ObjGrassStruct1_1* ptr, PlayState* play) { - if (!(ptr->unk_0E & 0x10)) { - Item_DropCollectibleRandom(play, NULL, &ptr->unk_00, ptr->unk_0E * 0x10); +void ObjGrass_DropCollectible(ObjGrassElement* grassElem, PlayState* play) { + if (!(grassElem->dropTable & 0x10)) { + Item_DropCollectibleRandom(play, NULL, &grassElem->pos, grassElem->dropTable * 0x10); } } -void func_809A9314(ObjGrassStruct1_1* ptr, PlayState* play) { - Vec3f spBC; - Vec3f spB0; +void ObjGrass_SpawnFragments(Vec3f* basePos, PlayState* play) { + Vec3f velocity; + Vec3f pos; s32 i; - Vec3f* ptr2; + Vec3f* dir; - for (i = 0; i < ARRAY_COUNT(D_809AAB4C); i++) { - ptr2 = &D_809AAB4C[i]; + for (i = 0; i < ARRAY_COUNT(sUnitDirections); i++) { + dir = &sUnitDirections[i]; - spB0.x = ptr->unk_00.x + (ptr2->x * 8.0f); - spB0.y = (ptr->unk_00.y + (ptr2->y * 8.0f)) + 10.0f; - spB0.z = ptr->unk_00.z + (ptr2->z * 8.0f); + pos.x = basePos->x + (dir->x * 8.0f); + pos.y = basePos->y + (dir->y * 8.0f) + 10.0f; + pos.z = basePos->z + (dir->z * 8.0f); - spBC.x = (Rand_ZeroOne() - 0.5f) * 8.0f; - spBC.y = Rand_ZeroOne() * 10.0f; - spBC.z = (Rand_ZeroOne() - 0.5f) * 8.0f; + velocity.x = (Rand_ZeroOne() - 0.5f) * 8.0f; + velocity.y = Rand_ZeroOne() * 10.0f; + velocity.z = (Rand_ZeroOne() - 0.5f) * 8.0f; - EffectSsKakera_Spawn(play, &spB0, &spBC, &spB0, -100, 64, 40, 3, 0, - D_809AAB7C[(s32)(Rand_ZeroOne() * 111.1f) & 7], 0, 0, 80, -1, 1, gKakeraLeafMiddleDL); + EffectSsKakera_Spawn(play, &pos, &velocity, &pos, -100, 64, 40, 3, 0, + sFragmentScales[(s32)(Rand_ZeroOne() * 111.1f) & 7], 0, 0, 80, -1, GAMEPLAY_KEEP, + gKakeraLeafMiddleDL); - spB0.x = ptr->unk_00.x + (ptr2->x * 16.0f); - spB0.y = (ptr->unk_00.y + (ptr2->y * 16.0f)) + 10.0f; - spB0.z = ptr->unk_00.z + (ptr2->z * 16.0f); + pos.x = basePos->x + (dir->x * 16.0f); + pos.y = basePos->y + (dir->y * 16.0f) + 10.0f; + pos.z = basePos->z + (dir->z * 16.0f); - spBC.x = (Rand_ZeroOne() - 0.5f) * 6.0f; - spBC.y = Rand_ZeroOne() * 10.0f; - spBC.z = (Rand_ZeroOne() - 0.5f) * 6.0f; + velocity.x = (Rand_ZeroOne() - 0.5f) * 6.0f; + velocity.y = Rand_ZeroOne() * 10.0f; + velocity.z = (Rand_ZeroOne() - 0.5f) * 6.0f; - EffectSsKakera_Spawn(play, &spB0, &spBC, &spB0, -100, 64, 40, 3, 0, - D_809AAB7C[(s32)(Rand_ZeroOne() * 111.1f) % 7], 0, 0, 80, -1, 1, gKakeraLeafTipDL); + EffectSsKakera_Spawn(play, &pos, &velocity, &pos, -100, 64, 40, 3, 0, + sFragmentScales[(s32)(Rand_ZeroOne() * 111.1f) % 7], 0, 0, 80, -1, GAMEPLAY_KEEP, + gKakeraLeafTipDL); } } @@ -141,9 +145,9 @@ void ObjGrass_Init(Actor* thisx, PlayState* play) { Actor_SetScale(&this->actor, 0.4f); - for (i = 0; i < ARRAY_COUNT(this->unk_2948); i++) { - Collider_InitCylinder(play, &this->unk_2948[i].collider); - Collider_SetCylinder(play, &this->unk_2948[i].collider, &this->actor, &sCylinderInit); + for (i = 0; i < ARRAY_COUNT(this->grassElemColliders); i++) { + Collider_InitCylinder(play, &this->grassElemColliders[i].collider); + Collider_SetCylinder(play, &this->grassElemColliders[i].collider, &this->actor, &sCylinderInit); } this->actor.colChkInfo.mass = MASS_IMMOVABLE; @@ -158,117 +162,121 @@ void ObjGrass_Destroy(Actor* thisx, PlayState* play) { ObjGrass* this = THIS; s32 i; - for (i = 0; i < ARRAY_COUNT(this->unk_2948); i++) { - Collider_DestroyCylinder(play, &this->unk_2948[i].collider); + for (i = 0; i < ARRAY_COUNT(this->grassElemColliders); i++) { + Collider_DestroyCylinder(play, &this->grassElemColliders[i].collider); } - for (i = 0; i < ARRAY_COUNT(this->unk_3298); i++) { - ObjGrassCarry** ptr = &this->unk_3298[i]; + for (i = 0; i < ARRAY_COUNT(this->grassCarry); i++) { + ObjGrassCarry** grassCarry = &this->grassCarry[i]; - if (*ptr != NULL) { - (*ptr)->unk_190 = 0; - *ptr = NULL; + if (*grassCarry != NULL) { + (*grassCarry)->grassManager = NULL; + *grassCarry = NULL; } } } -void func_809A9790(ObjGrass* this, PlayState* play) { +void ObjGrass_ProcessColliders(ObjGrass* this, PlayState* play) { s32 i; - for (i = 0; i < ARRAY_COUNT(this->unk_2948); i++) { - ObjGrassStruct1_1* ptr = this->unk_2948[i].unk_4C; + for (i = 0; i < ARRAY_COUNT(this->grassElemColliders); i++) { + ObjGrassElement* grassElem = this->grassElemColliders[i].entity; - if ((ptr != NULL) && (this->unk_2948[i].collider.base.acFlags & AC_HIT)) { - func_809A9314(ptr, play); - func_809A92D0(ptr, play); - ptr->unk_0F |= 4; - SoundSource_PlaySfxAtFixedWorldPos(play, &ptr->unk_00, 20, NA_SE_EV_PLANT_BROKEN); + if ((grassElem != NULL) && (this->grassElemColliders[i].collider.base.acFlags & AC_HIT)) { + ObjGrass_SpawnFragments(&grassElem->pos, play); + ObjGrass_DropCollectible(grassElem, play); + grassElem->flags |= OBJ_GRASS_ELEM_REMOVED; + SoundSource_PlaySfxAtFixedWorldPos(play, &grassElem->pos, 20, NA_SE_EV_PLANT_BROKEN); } } } -void func_809A983C(ObjGrass* this, PlayState* play) { +void ObjGrass_UpdateGrass(ObjGrass* this, PlayState* play) { Player* player = GET_PLAYER(play); s32 i; s32 j; s32 x; s32 y; - f32 temp_f0; - ObjGrassStruct1* ptr; - ObjGrassStruct1_1* ptr2; - ObjGrassStruct2* ptr3; + f32 distSq; + ObjGrassGroup* grassGroup; s16 yaw; - for (i = 0; i < ARRAY_COUNT(D_809AADC0); i++) { - D_809AADC0[i] = NULL; - D_809AAE10[i] = 422500.0f; + for (i = 0; i < OBJ_GRASS_NEAREST_ELEM_MAX; i++) { + sNearestGrassElements[i] = NULL; + sNearestGrassElementsDistSq[i] = SQ(650.0f); } - for (i = 0; i < ARRAY_COUNT(D_809AADA0); i++) { - D_809AADA0[i] = NULL; - D_809AADB0[i] = 422500.0f; + for (i = 0; i < OBJ_GRASS_NEAREST_GROUP_MAX; i++) { + sNearestGrassGroups[i] = NULL; + sNearestGrassGroupsDist[i] = SQ(650.0f); } - for (i = 0; i < this->unk_2944; i++) { - ptr = &this->unk_144[i]; + for (i = 0; i < this->activeGrassGroups; i++) { + grassGroup = &this->grassGroups[i]; - for (j = 0; j < ptr->unk_FC; j++) { - ptr->unk_0C[j].unk_0F &= (u16)~2; + for (j = 0; j < grassGroup->count; j++) { + grassGroup->elements[j].flags &= (u16)~OBJ_GRASS_ELEM_ANIM; } } - for (i = 0; i < this->unk_2944; i++) { - ptr = &this->unk_144[i]; - temp_f0 = Math3D_Vec3fDistSq(&ptr->unk_00, &player->actor.world.pos); + for (i = 0; i < this->activeGrassGroups; i++) { + grassGroup = &this->grassGroups[i]; + distSq = Math3D_Vec3fDistSq(&grassGroup->homePos, &player->actor.world.pos); - for (j = 0; j < ARRAY_COUNT(D_809AADB0); j++) { - if (temp_f0 < D_809AADB0[j]) { + for (j = 0; j < OBJ_GRASS_NEAREST_GROUP_MAX; j++) { + if (distSq < sNearestGrassGroupsDist[j]) { break; } } - if (j < ARRAY_COUNT(D_809AADB0)) { - for (x = 2; x >= j; x--) { - D_809AADB0[x + 1] = D_809AADB0[x]; - D_809AADA0[x + 1] = D_809AADA0[x]; + if (j < OBJ_GRASS_NEAREST_GROUP_MAX) { + // Insert the grassGroup into the list + + // Shift each element under j that remains in the list down 1 + for (x = OBJ_GRASS_NEAREST_GROUP_MAX - 2; x >= j; x--) { + sNearestGrassGroupsDist[x + 1] = sNearestGrassGroupsDist[x]; + sNearestGrassGroups[x + 1] = sNearestGrassGroups[x]; } - D_809AADB0[j] = temp_f0; - D_809AADA0[j] = ptr; + sNearestGrassGroupsDist[j] = distSq; + sNearestGrassGroups[j] = grassGroup; } } - this->unk_3294 = NULL; + this->carryGrassElem = NULL; - for (i = 0; i < ARRAY_COUNT(D_809AADA0); i++) { - ptr = D_809AADA0[i]; + for (i = 0; i < OBJ_GRASS_NEAREST_GROUP_MAX; i++) { + grassGroup = sNearestGrassGroups[i]; - if (ptr != NULL) { - for (j = 0; j < ptr->unk_FC; j++) { - if (!(ptr->unk_0C[j].unk_0F & 4)) { - temp_f0 = Math3D_Vec3fDistSq(&ptr->unk_0C[j].unk_00, &player->actor.world.pos); + if (grassGroup != NULL) { + for (j = 0; j < grassGroup->count; j++) { + if (!(grassGroup->elements[j].flags & OBJ_GRASS_ELEM_REMOVED)) { + distSq = Math3D_Vec3fDistSq(&grassGroup->elements[j].pos, &player->actor.world.pos); - for (x = 0; x < ARRAY_COUNT(D_809AAE10); x++) { - if (temp_f0 < D_809AAE10[x]) { + for (x = 0; x < OBJ_GRASS_NEAREST_ELEM_MAX; x++) { + if (distSq < sNearestGrassElementsDistSq[x]) { break; } } - if (x < ARRAY_COUNT(D_809AAE10)) { - for (y = 18; y >= x; y--) { - D_809AAE10[y + 1] = D_809AAE10[y]; - D_809AADC0[y + 1] = D_809AADC0[y]; + if (x < OBJ_GRASS_NEAREST_ELEM_MAX) { + // Insert the GrassElement into the list + + // Shift each element under j that remains in the list down 1 + for (y = OBJ_GRASS_NEAREST_ELEM_MAX - 2; y >= x; y--) { + sNearestGrassElementsDistSq[y + 1] = sNearestGrassElementsDistSq[y]; + sNearestGrassElements[y + 1] = sNearestGrassElements[y]; } - D_809AAE10[x] = temp_f0; - D_809AADC0[x] = &ptr->unk_0C[j]; + sNearestGrassElementsDistSq[x] = distSq; + sNearestGrassElements[x] = &grassGroup->elements[j]; - if (temp_f0 < 2500.0f) { + if (distSq < SQ(50.0f)) { yaw = player->actor.shape.rot.y - - Math_Vec3f_Yaw(&player->actor.world.pos, &ptr->unk_0C[j].unk_00); + Math_Vec3f_Yaw(&player->actor.world.pos, &grassGroup->elements[j].pos); if (ABS_ALT(yaw) < 0x2000) { - this->unk_3294 = &ptr->unk_0C[j]; + this->carryGrassElem = &grassGroup->elements[j]; } } } @@ -277,28 +285,29 @@ void func_809A983C(ObjGrass* this, PlayState* play) { } } - for (i = 0; i < ARRAY_COUNT(this->unk_2948); i++) { - ptr3 = &this->unk_2948[i]; + for (i = 0; i < ARRAY_COUNT(this->grassElemColliders); i++) { + ObjGrassCollider* grassCol = &this->grassElemColliders[i]; + ObjGrassElement* grassElem; - ptr3->collider.base.acFlags &= (u16)~AC_HIT; - ptr3->collider.base.ocFlags1 &= (u16)~OC1_HIT; + grassCol->collider.base.acFlags &= (u16)~AC_HIT; + grassCol->collider.base.ocFlags1 &= (u16)~OC1_HIT; - ptr2 = D_809AADC0[i]; - if (ptr2 != NULL) { - ptr3->collider.dim.pos.x = ptr2->unk_00.x; - ptr3->collider.dim.pos.y = ptr2->unk_00.y; - ptr3->collider.dim.pos.z = ptr2->unk_00.z; - CollisionCheck_SetAC(play, &play->colChkCtx, &ptr3->collider.base); - CollisionCheck_SetOC(play, &play->colChkCtx, &ptr3->collider.base); - ptr2->unk_0F |= 2; - ptr3->unk_4C = ptr2; + grassElem = sNearestGrassElements[i]; + if (grassElem != NULL) { + grassCol->collider.dim.pos.x = grassElem->pos.x; + grassCol->collider.dim.pos.y = grassElem->pos.y; + grassCol->collider.dim.pos.z = grassElem->pos.z; + CollisionCheck_SetAC(play, &play->colChkCtx, &grassCol->collider.base); + CollisionCheck_SetOC(play, &play->colChkCtx, &grassCol->collider.base); + grassElem->flags |= OBJ_GRASS_ELEM_ANIM; + grassCol->entity = grassElem; } else { - ptr3->unk_4C = NULL; + grassCol->entity = NULL; } } } -void func_809A9DB8(ObjGrass* this) { +void ObjGrass_CalcAnimationMatrices(ObjGrass* this) { s32 i; s32 pad; f32* ptr; @@ -345,8 +354,8 @@ void func_809A9DB8(ObjGrass* this) { sp6C[6] = (temp_f26 - temp_f22) * temp_f22 * temp_f20 * temp_f16 * 0.0015f; sp6C[7] = (spA4 - temp_f20) * sp94 * sp90 * temp_f16 * 0.0015f; - for (i = 0; i < ARRAY_COUNT(this->unk_2F88); i++) { - ptr = &this->unk_2F88[i].mf[0][0]; + for (i = 0; i < ARRAY_COUNT(this->distortionMtx); i++) { + ptr = &this->distortionMtx[i].mf[0][0]; tempf1 = sp6C[(i + 0) & 7]; tempf2 = sp6C[(i + 1) & 7]; @@ -379,64 +388,65 @@ void func_809A9DB8(ObjGrass* this) { void ObjGrass_Update(Actor* thisx, PlayState* play) { ObjGrass* this = THIS; - func_809A9790(this, play); - func_809A983C(this, play); - func_809A9DB8(this); + ObjGrass_ProcessColliders(this, play); + ObjGrass_UpdateGrass(this, play); + ObjGrass_CalcAnimationMatrices(this); } -void func_809AA278(ObjGrass* this, PlayState* play) { - ObjGrassStruct1* ptr; - ObjGrassStruct1_1* ptr2; +void ObjGrass_InitDraw(ObjGrass* this, PlayState* play) { + ObjGrassGroup* grassGroup; + ObjGrassElement* grassElem; s32 i; s32 j; f32 distSq; - f32 temp_f22; + f32 eyeDist; - for (i = 0; i < this->unk_2944; i++) { - ptr = &this->unk_144[i]; + for (i = 0; i < this->activeGrassGroups; i++) { + grassGroup = &this->grassGroups[i]; - temp_f22 = Math3D_Vec3fDistSq(&ptr->unk_00, &GET_ACTIVE_CAM(play)->eye); + eyeDist = Math3D_Vec3fDistSq(&grassGroup->homePos, &GET_ACTIVE_CAM(play)->eye); - if ((temp_f22 < SQ(1280.0f)) && func_809A9110(play, &ptr->unk_00)) { - ptr->unk_FE |= 1; + if ((eyeDist < SQ(1280.0f)) && func_809A9110(play, &grassGroup->homePos)) { + grassGroup->flags |= OBJ_GRASS_GROUP_DRAW; - for (j = 0; j < ptr->unk_FC; j++) { - ptr2 = &ptr->unk_0C[j]; + for (j = 0; j < grassGroup->count; j++) { + grassElem = &grassGroup->elements[j]; - if (ptr2->unk_0F & 4) { - ptr2->unk_10 = 255; - ptr2->unk_0F &= ~1; + if (grassElem->flags & OBJ_GRASS_ELEM_REMOVED) { + grassElem->alpha = 255; + grassElem->flags &= ~OBJ_GRASS_ELEM_DRAW; } else { - ptr2->unk_0F |= 1; - if (temp_f22 < SQ(980.0f)) { - ptr2->unk_10 = 255; + grassElem->flags |= OBJ_GRASS_ELEM_DRAW; + if (eyeDist < SQ(980.0f)) { + grassElem->alpha = 255; } else { - distSq = Math3D_Vec3fDistSq(&ptr2->unk_00, &GET_ACTIVE_CAM(play)->eye); - if ((distSq <= SQ(1080.0f)) || ((ptr2->unk_0F & 8) && (distSq < SQ(1180.0f)))) { - ptr2->unk_10 = 255; + distSq = Math3D_Vec3fDistSq(&grassElem->pos, &GET_ACTIVE_CAM(play)->eye); + if ((distSq <= SQ(1080.0f)) || + ((grassElem->flags & OBJ_GRASS_ELEM_UNDERWATER) && (distSq < SQ(1180.0f)))) { + grassElem->alpha = 255; } else if (distSq >= SQ(1180.0f)) { - ptr2->unk_10 = 0; + grassElem->alpha = 0; } else { - ptr2->unk_10 = (1180.0f - sqrtf(distSq)) * 2.55f; + grassElem->alpha = (1180.0f - sqrtf(distSq)) * 2.55f; } } } } } else { - ptr->unk_FE &= ~1; + grassGroup->flags &= ~OBJ_GRASS_GROUP_DRAW; } } } -void func_809AA54C(Actor* thisx, PlayState* play2) { +void ObjGrass_DrawOpa(Actor* thisx, PlayState* play2) { ObjGrass* this = THIS; PlayState* play = play2; - Lights* temp_s0; - ObjGrassStruct1* ptr; + Lights* lights; + ObjGrassGroup* grassGroup; s32 i; s32 j; - Vec3s sp70 = { 0, 0, 0 }; - ObjGrassStruct1_1* ptr2; + Vec3s rot = { 0, 0, 0 }; + ObjGrassElement* grassElem; OPEN_DISPS(play->state.gfxCtx); @@ -445,23 +455,23 @@ void func_809AA54C(Actor* thisx, PlayState* play2) { gDPSetPrimColor(POLY_OPA_DISP++, 0, 0, 255, 255, 255, 255); gSPDisplayList(POLY_OPA_DISP++, gObjGrass_D_809AA9F0); - for (i = 0; i < this->unk_2944; i++) { - ptr = &this->unk_144[i]; + for (i = 0; i < this->activeGrassGroups; i++) { + grassGroup = &this->grassGroups[i]; - if (ptr->unk_FE & 1) { - temp_s0 = LightContext_NewLights(&play->lightCtx, play->state.gfxCtx); - Lights_BindAll(temp_s0, play->lightCtx.listHead, &ptr->unk_00, play); - Lights_Draw(temp_s0, play->state.gfxCtx); + if (grassGroup->flags & OBJ_GRASS_GROUP_DRAW) { + lights = LightContext_NewLights(&play->lightCtx, play->state.gfxCtx); + Lights_BindAll(lights, play->lightCtx.listHead, &grassGroup->homePos, play); + Lights_Draw(lights, play->state.gfxCtx); - for (j = 0; j < ptr->unk_FC; j++) { - ptr2 = &ptr->unk_0C[j]; + for (j = 0; j < grassGroup->count; j++) { + grassElem = &grassGroup->elements[j]; - if ((ptr2->unk_0F & 1) && (ptr2->unk_10 == 255)) { - sp70.y = ptr2->unk_0C; - Matrix_SetTranslateRotateYXZ(ptr2->unk_00.x, ptr2->unk_00.y, ptr2->unk_00.z, &sp70); + if ((grassElem->flags & OBJ_GRASS_ELEM_DRAW) && (grassElem->alpha == 255)) { + rot.y = grassElem->rotY; + Matrix_SetTranslateRotateYXZ(grassElem->pos.x, grassElem->pos.y, grassElem->pos.z, &rot); Matrix_Scale(this->actor.scale.x, this->actor.scale.y, this->actor.scale.z, MTXMODE_APPLY); - if (ptr2->unk_0F & 2) { - func_809A91FC(&this->unk_2F88[j]); + if (grassElem->flags & OBJ_GRASS_ELEM_ANIM) { + ObjGrass_OverrideMatrixCurrent(&this->distortionMtx[j]); } gSPMatrix(POLY_OPA_DISP++, Matrix_NewMtx(play->state.gfxCtx), @@ -475,13 +485,13 @@ void func_809AA54C(Actor* thisx, PlayState* play2) { CLOSE_DISPS(play->state.gfxCtx); } -void func_809AA798(Actor* thisx, PlayState* play) { +void ObjGrass_DrawXlu(Actor* thisx, PlayState* play) { ObjGrass* this = THIS; - ObjGrassStruct1* ptr; - ObjGrassStruct1_1* ptr2; + ObjGrassGroup* grassGroup; + ObjGrassElement* grassElem; s32 i; s32 j; - Vec3s sp6C = { 0, 0, 0 }; + Vec3s rot = { 0, 0, 0 }; OPEN_DISPS(play->state.gfxCtx); @@ -489,21 +499,21 @@ void func_809AA798(Actor* thisx, PlayState* play) { gSPDisplayList(POLY_XLU_DISP++, gObjGrass_D_809AAA68); - for (i = 0; i < this->unk_2944; i++) { - ptr = &this->unk_144[i]; + for (i = 0; i < this->activeGrassGroups; i++) { + grassGroup = &this->grassGroups[i]; - if (ptr->unk_FE & 1) { - for (j = 0; j < ptr->unk_FC; j++) { - ptr2 = &ptr->unk_0C[j]; + if (grassGroup->flags & OBJ_GRASS_GROUP_DRAW) { + for (j = 0; j < grassGroup->count; j++) { + grassElem = &grassGroup->elements[j]; - if ((ptr2->unk_0F & 1) && (ptr2->unk_10 > 0) && (ptr2->unk_10 < 255)) { - sp6C.y = ptr2->unk_0C; - Matrix_SetTranslateRotateYXZ(ptr2->unk_00.x, ptr2->unk_00.y, ptr2->unk_00.z, &sp6C); + if ((grassElem->flags & OBJ_GRASS_ELEM_DRAW) && (grassElem->alpha > 0) && (grassElem->alpha < 255)) { + rot.y = grassElem->rotY; + Matrix_SetTranslateRotateYXZ(grassElem->pos.x, grassElem->pos.y, grassElem->pos.z, &rot); Matrix_Scale(this->actor.scale.x, this->actor.scale.y, this->actor.scale.z, MTXMODE_APPLY); gSPMatrix(POLY_XLU_DISP++, Matrix_NewMtx(play->state.gfxCtx), G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_MODELVIEW); - gDPSetPrimColor(POLY_XLU_DISP++, 0, 0, 255, 255, 255, ptr2->unk_10); + gDPSetPrimColor(POLY_XLU_DISP++, 0, 0, 255, 255, 255, grassElem->alpha); gSPDisplayList(POLY_XLU_DISP++, gObjGrass_D_809AAAE0); } } @@ -516,7 +526,7 @@ void func_809AA798(Actor* thisx, PlayState* play) { void ObjGrass_Draw(Actor* thisx, PlayState* play) { ObjGrass* this = THIS; - func_809AA278(this, play); - func_809AA54C(thisx, play); - func_809AA798(thisx, play); + ObjGrass_InitDraw(this, play); + ObjGrass_DrawOpa(thisx, play); + ObjGrass_DrawXlu(thisx, play); } diff --git a/src/overlays/actors/ovl_Obj_Grass/z_obj_grass.h b/src/overlays/actors/ovl_Obj_Grass/z_obj_grass.h index fffcdad4ef..f3fb8705da 100644 --- a/src/overlays/actors/ovl_Obj_Grass/z_obj_grass.h +++ b/src/overlays/actors/ovl_Obj_Grass/z_obj_grass.h @@ -7,40 +7,52 @@ struct ObjGrass; struct ObjGrassCarry; -typedef struct ObjGrassStruct1_1 { - /* 0x00 */ Vec3f unk_00; - /* 0x0C */ s16 unk_0C; - /* 0x0E */ s8 unk_0E; - /* 0x0F */ u8 unk_0F; - /* 0x10 */ u8 unk_10; -} ObjGrassStruct1_1; // size = 0x14 +#define OBJ_GRASS_NEAREST_GROUP_MAX 4 +#define OBJ_GRASS_NEAREST_ELEM_MAX 20 + +#define OBJ_GRASS_GROUP_ELEM_COUNT_MAX 12 + +#define OBJ_GRASS_GROUP_DRAW 1 + +#define OBJ_GRASS_ELEM_DRAW 1 +#define OBJ_GRASS_ELEM_ANIM 2 +#define OBJ_GRASS_ELEM_REMOVED 4 +#define OBJ_GRASS_ELEM_UNDERWATER 8 + +typedef struct ObjGrassElement { + /* 0x00 */ Vec3f pos; + /* 0x0C */ s16 rotY; + /* 0x0E */ s8 dropTable; + /* 0x0F */ u8 flags; + /* 0x10 */ u8 alpha; +} ObjGrassElement; // size = 0x14 typedef struct { - /* 0x00 */ Vec3f unk_00; - /* 0x0C */ ObjGrassStruct1_1 unk_0C[12]; - /* 0xFC */ s16 unk_FC; - /* 0xFE */ u8 unk_FE; -} ObjGrassStruct1; // size = 0x100 + /* 0x00 */ Vec3f homePos; + /* 0x0C */ ObjGrassElement elements[OBJ_GRASS_GROUP_ELEM_COUNT_MAX]; + /* 0xFC */ s16 count; + /* 0xFE */ u8 flags; +} ObjGrassGroup; // size = 0x100 typedef struct { /* 0x00 */ ColliderCylinder collider; - /* 0x4C */ ObjGrassStruct1_1* unk_4C; -} ObjGrassStruct2; // size = 0x50 + /* 0x4C */ ObjGrassElement* entity; +} ObjGrassCollider; // size = 0x50 typedef struct ObjGrass { /* 0x0000 */ Actor actor; - /* 0x0144 */ ObjGrassStruct1 unk_144[40]; - /* 0x2944 */ s16 unk_2944; - /* 0x2948 */ ObjGrassStruct2 unk_2948[20]; - /* 0x2F88 */ MtxF unk_2F88[12]; + /* 0x0144 */ ObjGrassGroup grassGroups[40]; + /* 0x2944 */ s16 activeGrassGroups; + /* 0x2948 */ ObjGrassCollider grassElemColliders[OBJ_GRASS_NEAREST_ELEM_MAX]; + /* 0x2F88 */ MtxF distortionMtx[OBJ_GRASS_GROUP_ELEM_COUNT_MAX]; /* 0x3288 */ s16 unk_3288; /* 0x328A */ s16 unk_328A; /* 0x328C */ s16 unk_328C; /* 0x328E */ s16 unk_328E; /* 0x3290 */ s16 unk_3290; - /* 0x3292 */ s16 unk_3292; - /* 0x3294 */ ObjGrassStruct1_1* unk_3294; - /* 0x3298 */ struct ObjGrassCarry* unk_3298[2]; + /* 0x3292 */ s16 activeGrassCarry; + /* 0x3294 */ ObjGrassElement* carryGrassElem; + /* 0x3298 */ struct ObjGrassCarry* grassCarry[2]; } ObjGrass; // size = 0x32A0 #endif // Z_OBJ_GRASS_H diff --git a/src/overlays/actors/ovl_Obj_Grass_Carry/z_obj_grass_carry.c b/src/overlays/actors/ovl_Obj_Grass_Carry/z_obj_grass_carry.c index 1554a24036..4b9c424f4e 100644 --- a/src/overlays/actors/ovl_Obj_Grass_Carry/z_obj_grass_carry.c +++ b/src/overlays/actors/ovl_Obj_Grass_Carry/z_obj_grass_carry.c @@ -17,17 +17,17 @@ void ObjGrassCarry_Init(Actor* thisx, PlayState* play); void ObjGrassCarry_Destroy(Actor* thisx, PlayState* play); void ObjGrassCarry_Update(Actor* thisx, PlayState* play); -void func_809AB3C4(ObjGrassCarry* this); -void func_809AB3D8(ObjGrassCarry* this, PlayState* play); -void func_809AB428(ObjGrassCarry* this); -void func_809AB43C(ObjGrassCarry* this, PlayState* play); -void func_809AB474(ObjGrassCarry* this); -void func_809AB4A8(ObjGrassCarry* this, PlayState* play); -void func_809AB5FC(ObjGrassCarry* this); -void func_809AB610(ObjGrassCarry* this, PlayState* play); -void func_809AB6FC(ObjGrassCarry* this); -void func_809AB77C(ObjGrassCarry* this, PlayState* play); -void func_809ABB7C(Actor* this, PlayState* play); +void ObjGrassCarry_SetupAwaitSpawnManager(ObjGrassCarry* this); +void ObjGrassCarry_AwaitSpawnManager(ObjGrassCarry* this, PlayState* play); +void ObjGrassCarry_SetupIdle(ObjGrassCarry* this); +void ObjGrassCarry_Idle(ObjGrassCarry* this, PlayState* play); +void ObjGrassCarry_Reset(ObjGrassCarry* this); +void ObjGrassCarry_Main(ObjGrassCarry* this, PlayState* play); +void ObjGrassCarry_SetupLiftedUp(ObjGrassCarry* this); +void ObjGrassCarry_LiftedUp(ObjGrassCarry* this, PlayState* play); +void ObjGrassCarry_SetupFall(ObjGrassCarry* this); +void ObjGrassCarry_Fall(ObjGrassCarry* this, PlayState* play); +void ObjGrassCarry_Draw(Actor* this, PlayState* play); ActorInit Obj_Grass_Carry_InitVars = { ACTOR_OBJ_GRASS_CARRY, @@ -61,19 +61,19 @@ static ColliderCylinderInit sCylinderInit = { { 10, 44, 0, { 0, 0, 0 } }, }; -s16 D_809ABBFC = 0; -s16 D_809ABC00 = 0; -s16 D_809ABC04 = 0; -s16 D_809ABC08 = 0; +static s16 sRotSpeedXTarget = 0; +static s16 sRotSpeedX = 0; +static s16 sRotSpeedYTarget = 0; +static s16 sRotSpeedY = 0; -Vec3f D_809ABC0C[] = { +static Vec3f sUnitDirections[] = { { 0.0f, 0.7071f, 0.7071f }, { 0.7071f, 0.7071f, 0.0f }, { 0.0f, 0.7071f, -0.7071f }, { -0.7071f, 0.7071f, 0.0f }, }; -s16 D_809ABC3C[] = { 0x6C, 0x66, 0x60, 0x54, 0x42, 0x37, 0x2A, 0x26 }; +static s16 sFragmentScales[] = { 108, 102, 96, 84, 66, 55, 42, 38 }; static InitChainEntry sInitChain[] = { ICHAIN_F32_DIV1000(gravity, -3200, ICHAIN_CONTINUE), @@ -81,69 +81,71 @@ static InitChainEntry sInitChain[] = { ICHAIN_VEC3F_DIV1000(scale, 400, ICHAIN_STOP), }; -void func_809AAE60(ObjGrassCarry* this) { +void ObjGrassCarry_UpdateVelY(ObjGrassCarry* this) { this->actor.velocity.y += this->actor.gravity; if (this->actor.velocity.y < this->actor.terminalVelocity) { this->actor.velocity.y = this->actor.terminalVelocity; } } -void func_809AAE94(Vec3f* arg0, f32 arg1) { - arg1 += ((Rand_ZeroOne() * 0.2f) - 0.1f) * arg1; +void ObjGrassCarry_RandScaleVecToZero(Vec3f* velocity, f32 scale) { + scale += ((Rand_ZeroOne() * 0.2f) - 0.1f) * scale; - arg0->x -= (arg0->x * arg1); - arg0->y -= (arg0->y * arg1); - arg0->z -= (arg0->z * arg1); + velocity->x -= velocity->x * scale; + velocity->y -= velocity->y * scale; + velocity->z -= velocity->z * scale; } -void func_809AAF18(ObjGrassCarry* this) { - func_809AAE60(this); - func_809AAE94(&this->actor.velocity, 0.05f); +void ObjGrassCarry_UpdatePos(ObjGrassCarry* this) { + ObjGrassCarry_UpdateVelY(this); + ObjGrassCarry_RandScaleVecToZero(&this->actor.velocity, 0.05f); Actor_UpdatePos(&this->actor); } -void func_809AAF58(ObjGrassCarry* this, PlayState* play) { +void ObjGrassCarry_UpdateBgCheckInfo(ObjGrassCarry* this, PlayState* play) { Actor_UpdateBgCheckInfo(play, &this->actor, 7.5f, 35.0f, 0.0f, UPDBGCHECKINFO_FLAG_1 | UPDBGCHECKINFO_FLAG_4 | UPDBGCHECKINFO_FLAG_40 | UPDBGCHECKINFO_FLAG_80); } -void func_809AAF9C(Vec3f* arg0, s16 arg1, PlayState* play) { - if (!(arg1 & 0x10)) { - Item_DropCollectibleRandom(play, NULL, arg0, arg1 * 0x10); +void ObjGrassCarry_DropCollectible(Vec3f* pos, s16 dropTable, PlayState* play) { + if ((dropTable & 0x10) == 0) { + Item_DropCollectibleRandom(play, NULL, pos, dropTable * 0x10); } } -void func_809AAFE8(Vec3f* arg0, PlayState* play) { - Vec3f spBC; - Vec3f spB0; +void ObjGrassCarry_SpawnFragments(Vec3f* basePos, PlayState* play) { + Vec3f velocity; + Vec3f pos; s32 i; - Vec3f* ptr; + Vec3f* dir; - for (i = 0; i < ARRAY_COUNT(D_809ABC0C); i++) { - ptr = &D_809ABC0C[i]; + for (i = 0; i < ARRAY_COUNT(sUnitDirections); i++) { + dir = &sUnitDirections[i]; - spB0.x = arg0->x + (ptr->x * 8.0f); - spB0.y = arg0->y + (ptr->y * 8.0f) + 10.0f; - spB0.z = arg0->z + (ptr->z * 8.0f); + pos.x = basePos->x + (dir->x * 8.0f); + pos.y = basePos->y + (dir->y * 8.0f) + 10.0f; + pos.z = basePos->z + (dir->z * 8.0f); - spBC.x = (Rand_ZeroOne() - 0.5f) * 8.0f; - spBC.y = Rand_ZeroOne() * 10.0f; - spBC.z = (Rand_ZeroOne() - 0.5f) * 8.0f; + velocity.x = (Rand_ZeroOne() - 0.5f) * 8.0f; + velocity.y = Rand_ZeroOne() * 10.0f; + velocity.z = (Rand_ZeroOne() - 0.5f) * 8.0f; - EffectSsKakera_Spawn(play, &spB0, &spBC, &spB0, -100, 64, 40, 3, 0, - D_809ABC3C[(s32)(Rand_ZeroOne() * 111.1f) & 7], 0, 0, 80, -1, 1, gKakeraLeafMiddleDL); + EffectSsKakera_Spawn(play, &pos, &velocity, &pos, -100, 64, 40, 3, 0, + sFragmentScales[(s32)(Rand_ZeroOne() * 111.1f) & 7], 0, 0, 80, -1, GAMEPLAY_KEEP, + gKakeraLeafMiddleDL); - spB0.x = arg0->x + (ptr->x * 16.0f); - spB0.y = arg0->y + (ptr->y * 16.0f) + 10.0f; - spB0.z = arg0->z + (ptr->z * 16.0f); + pos.x = basePos->x + (dir->x * 16.0f); + pos.y = basePos->y + (dir->y * 16.0f) + 10.0f; + pos.z = basePos->z + (dir->z * 16.0f); - spBC.x = (Rand_ZeroOne() - 0.5f) * 6.0f; - spBC.y = Rand_ZeroOne() * 10.0f; - spBC.z = (Rand_ZeroOne() - 0.5f) * 6.0f; + velocity.x = (Rand_ZeroOne() - 0.5f) * 6.0f; + velocity.y = Rand_ZeroOne() * 10.0f; + velocity.z = (Rand_ZeroOne() - 0.5f) * 6.0f; - EffectSsKakera_Spawn(play, &spB0, &spBC, &spB0, -100, 64, 40, 3, 0, - D_809ABC3C[(s32)(Rand_ZeroOne() * 111.1f) % 7], 0, 0, 80, -1, 1, gKakeraLeafTipDL); + EffectSsKakera_Spawn(play, &pos, &velocity, &pos, -100, 64, 40, 3, 0, + sFragmentScales[(s32)(Rand_ZeroOne() * 111.1f) % 7], 0, 0, 80, -1, GAMEPLAY_KEEP, + gKakeraLeafTipDL); } } @@ -154,7 +156,7 @@ void ObjGrassCarry_Init(Actor* thisx, PlayState* play) { Collider_InitCylinder(play, &this->collider); Collider_SetCylinder(play, &this->collider, &this->actor, &sCylinderInit); this->actor.colChkInfo.mass = 80; - func_809AB3C4(this); + ObjGrassCarry_SetupAwaitSpawnManager(this); } void ObjGrassCarry_Destroy(Actor* thisx, PlayState* play) { @@ -162,73 +164,69 @@ void ObjGrassCarry_Destroy(Actor* thisx, PlayState* play) { Collider_DestroyCylinder(play, &this->collider); - if (this->unk_190 != NULL) { - ObjGrassCarry** carry = &this->unk_190->unk_3298[this->actor.params]; + if (this->grassManager != NULL) { + ObjGrassCarry** carry = &this->grassManager->grassCarry[this->actor.params]; if (this == *carry) { *carry = NULL; - this->unk_190 = NULL; + this->grassManager = NULL; } } } -void func_809AB3C4(ObjGrassCarry* this) { - this->actionFunc = func_809AB3D8; +void ObjGrassCarry_SetupAwaitSpawnManager(ObjGrassCarry* this) { + this->actionFunc = ObjGrassCarry_AwaitSpawnManager; } -void func_809AB3D8(ObjGrassCarry* this, PlayState* play) { - if (this->unk_190 != NULL) { - if (this->actor.params != this->unk_190->unk_3292) { - func_809AB474(this); +void ObjGrassCarry_AwaitSpawnManager(ObjGrassCarry* this, PlayState* play) { + if (this->grassManager != NULL) { + if (this->actor.params != this->grassManager->activeGrassCarry) { + ObjGrassCarry_Reset(this); } else { - func_809AB428(this); + ObjGrassCarry_SetupIdle(this); } } } -void func_809AB428(ObjGrassCarry* this) { - this->actionFunc = func_809AB43C; +void ObjGrassCarry_SetupIdle(ObjGrassCarry* this) { + this->actionFunc = ObjGrassCarry_Idle; } -void func_809AB43C(ObjGrassCarry* this, PlayState* play) { - if (this->actor.params != this->unk_190->unk_3292) { - func_809AB474(this); +void ObjGrassCarry_Idle(ObjGrassCarry* this, PlayState* play) { + if (this->actor.params != this->grassManager->activeGrassCarry) { + ObjGrassCarry_Reset(this); } } -void func_809AB474(ObjGrassCarry* this) { - this->actor.shape.rot.z = 0; - this->actionFunc = func_809AB4A8; - this->unk_194 = NULL; - this->actor.shape.rot.y = this->actor.shape.rot.z; - this->actor.shape.rot.x = this->actor.shape.rot.z; - this->actor.world.rot.z = this->actor.shape.rot.z; - this->actor.world.rot.y = this->actor.shape.rot.z; - this->actor.world.rot.x = this->actor.shape.rot.z; +void ObjGrassCarry_Reset(ObjGrassCarry* this) { + this->actionFunc = ObjGrassCarry_Main; + this->grassElem = NULL; + this->actor.world.rot.x = this->actor.world.rot.y = this->actor.world.rot.z = this->actor.shape.rot.x = + this->actor.shape.rot.y = this->actor.shape.rot.z = 0; } -void func_809AB4A8(ObjGrassCarry* this, PlayState* play) { +void ObjGrassCarry_Main(ObjGrassCarry* this, PlayState* play) { Actor* thisx = &this->actor; if (Actor_HasParent(thisx, play)) { - func_809AB5FC(this); - if (this->unk_194 != NULL) { - this->unk_194->unk_0F |= 4; + ObjGrassCarry_SetupLiftedUp(this); + if (this->grassElem != NULL) { + this->grassElem->flags |= OBJ_GRASS_ELEM_REMOVED; } - thisx->draw = func_809ABB7C; + thisx->draw = ObjGrassCarry_Draw; thisx->shape.shadowDraw = ActorShadow_DrawCircle; thisx->shape.shadowAlpha = 60; thisx->shape.shadowScale = 1.0f; - this->unk_190->unk_3292 ^= 1; + this->grassManager->activeGrassCarry ^= 1; thisx->room = -1; SoundSource_PlaySfxAtFixedWorldPos(play, &thisx->world.pos, 20, NA_SE_PL_PULL_UP_PLANT); - } else if (this->unk_190->unk_3294 != NULL) { + } else if (this->grassManager->carryGrassElem != NULL) { Player* player = GET_PLAYER(play); - this->unk_194 = this->unk_190->unk_3294; - Math_Vec3f_Copy(&thisx->world.pos, &this->unk_194->unk_00); - thisx->shape.rot.y = thisx->world.rot.y = this->unk_194->unk_0C; - this->unk_198 = this->unk_194->unk_0E; + this->grassElem = this->grassManager->carryGrassElem; + Math_Vec3f_Copy(&thisx->world.pos, &this->grassElem->pos); + thisx->shape.rot.y = thisx->world.rot.y = this->grassElem->rotY; + this->dropTable = this->grassElem->dropTable; thisx->xzDistToPlayer = Actor_WorldDistXZToActor(&this->actor, &player->actor); thisx->playerHeightRel = Actor_HeightDiff(&this->actor, &player->actor); thisx->xyzDistToPlayerSq = SQ(thisx->xzDistToPlayer) + SQ(thisx->playerHeightRel); @@ -237,87 +235,88 @@ void func_809AB4A8(ObjGrassCarry* this, PlayState* play) { } } -void func_809AB5FC(ObjGrassCarry* this) { - this->actionFunc = func_809AB610; +void ObjGrassCarry_SetupLiftedUp(ObjGrassCarry* this) { + this->actionFunc = ObjGrassCarry_LiftedUp; } -void func_809AB610(ObjGrassCarry* this, PlayState* play) { +void ObjGrassCarry_LiftedUp(ObjGrassCarry* this, PlayState* play) { s32 pad; - Vec3f sp30; - s32 sp2C; if (Actor_HasNoParent(&this->actor, play)) { - func_809AB6FC(this); + ObjGrassCarry_SetupFall(this); this->actor.velocity.x = Math_SinS(this->actor.world.rot.y) * this->actor.speed; this->actor.velocity.z = Math_CosS(this->actor.world.rot.y) * this->actor.speed; this->actor.gravity = -0.1f; this->actor.terminalVelocity = -17.0f; - func_809AAF18(this); - func_809AAF58(this, play); + ObjGrassCarry_UpdatePos(this); + ObjGrassCarry_UpdateBgCheckInfo(this, play); this->actor.gravity = -3.2f; } else { - sp30.x = this->actor.world.pos.x; - sp30.y = this->actor.world.pos.y + 20.0f; - sp30.z = this->actor.world.pos.z; + Vec3f pos; + s32 bgId; + + pos.x = this->actor.world.pos.x; + pos.y = this->actor.world.pos.y + 20.0f; + pos.z = this->actor.world.pos.z; this->actor.floorHeight = - BgCheck_EntityRaycastFloor5(&play->colCtx, &this->actor.floorPoly, &sp2C, &this->actor, &sp30); + BgCheck_EntityRaycastFloor5(&play->colCtx, &this->actor.floorPoly, &bgId, &this->actor, &pos); } } -void func_809AB6FC(ObjGrassCarry* this) { - this->actionFunc = func_809AB77C; - D_809ABBFC = -0xBB8; - D_809ABC04 = (Rand_ZeroOne() - 0.5f) * 1600.0f; - D_809ABC00 = 0; - D_809ABC08 = 0; - this->unk_19A = 60; +void ObjGrassCarry_SetupFall(ObjGrassCarry* this) { + this->actionFunc = ObjGrassCarry_Fall; + sRotSpeedXTarget = -0xBB8; + sRotSpeedYTarget = (Rand_ZeroOne() - 0.5f) * 1600.0f; + sRotSpeedX = 0; + sRotSpeedY = 0; + this->fallTimer = 60; } -void func_809AB77C(ObjGrassCarry* this, PlayState* play) { - s16 phi_s0; - s32 temp_v0 = (this->collider.base.atFlags & AT_HIT) != 0; - Vec3f sp5C; +void ObjGrassCarry_Fall(ObjGrassCarry* this, PlayState* play) { + s16 angle; + s32 atHit = (this->collider.base.atFlags & AT_HIT) != 0; + Vec3f pos; s32 i; - if (temp_v0) { + if (atHit) { this->collider.base.atFlags &= ~AT_HIT; } - this->unk_19A--; + this->fallTimer--; - if ((this->actor.bgCheckFlags & (BGCHECKFLAG_GROUND | BGCHECKFLAG_GROUND_TOUCH | BGCHECKFLAG_WALL)) || temp_v0 || - (this->unk_19A <= 0)) { - func_809AAFE8(&this->actor.world.pos, play); - func_809AAF9C(&this->actor.world.pos, this->unk_198, play); + if ((this->actor.bgCheckFlags & (BGCHECKFLAG_GROUND | BGCHECKFLAG_GROUND_TOUCH | BGCHECKFLAG_WALL)) || atHit || + (this->fallTimer <= 0)) { + ObjGrassCarry_SpawnFragments(&this->actor.world.pos, play); + ObjGrassCarry_DropCollectible(&this->actor.world.pos, this->dropTable, play); this->actor.draw = NULL; this->actor.shape.shadowDraw = NULL; - if (this->unk_190 != NULL) { - this->actor.room = this->unk_190->actor.room; + if (this->grassManager != NULL) { + this->actor.room = this->grassManager->actor.room; } if (!(this->actor.bgCheckFlags & BGCHECKFLAG_WATER)) { SoundSource_PlaySfxAtFixedWorldPos(play, &this->actor.world.pos, 20, NA_SE_EV_PLANT_BROKEN); } - func_809AB428(this); + ObjGrassCarry_SetupIdle(this); return; } if (this->actor.bgCheckFlags & BGCHECKFLAG_WATER_TOUCH) { - sp5C.y = this->actor.world.pos.y + this->actor.depthInWater; + pos.y = this->actor.world.pos.y + this->actor.depthInWater; - for (phi_s0 = 0, i = 0; i < 4; i++, phi_s0 += 0x4000) { - sp5C.x = (Math_SinS((s32)(Rand_ZeroOne() * 7200.0f) + phi_s0) * 15.0f) + this->actor.world.pos.x; - sp5C.z = (Math_CosS((s32)(Rand_ZeroOne() * 7200.0f) + phi_s0) * 15.0f) + this->actor.world.pos.z; - EffectSsGSplash_Spawn(play, &sp5C, NULL, NULL, 0, 190); + for (angle = 0, i = 0; i < 4; i++, angle += 0x4000) { + pos.x = (Math_SinS((s32)(Rand_ZeroOne() * 7200.0f) + angle) * 15.0f) + this->actor.world.pos.x; + pos.z = (Math_CosS((s32)(Rand_ZeroOne() * 7200.0f) + angle) * 15.0f) + this->actor.world.pos.z; + EffectSsGSplash_Spawn(play, &pos, NULL, NULL, 0, 190); } - sp5C.x = this->actor.world.pos.x; - sp5C.z = this->actor.world.pos.z; + pos.x = this->actor.world.pos.x; + pos.z = this->actor.world.pos.z; - EffectSsGSplash_Spawn(play, &sp5C, NULL, NULL, 0, 280); - EffectSsGRipple_Spawn(play, &sp5C, 300, 700, 0); + EffectSsGSplash_Spawn(play, &pos, NULL, NULL, 0, 280); + EffectSsGRipple_Spawn(play, &pos, 300, 700, 0); this->actor.terminalVelocity = -3.0f; this->actor.velocity.x *= 0.1f; @@ -325,20 +324,20 @@ void func_809AB77C(ObjGrassCarry* this, PlayState* play) { this->actor.velocity.z *= 0.1f; this->actor.gravity *= 0.5f; - D_809ABC00 = D_809ABC00 >> 1; - D_809ABBFC = D_809ABBFC >> 1; - D_809ABC08 = D_809ABC08 >> 1; - D_809ABC04 = D_809ABC04 >> 1; + sRotSpeedX >>= 1; + sRotSpeedXTarget >>= 1; + sRotSpeedY >>= 1; + sRotSpeedYTarget >>= 1; this->actor.bgCheckFlags &= ~BGCHECKFLAG_WATER_TOUCH; SoundSource_PlaySfxAtFixedWorldPos(play, &this->actor.world.pos, 40, NA_SE_EV_DIVE_INTO_WATER_L); } - Math_StepToS(&D_809ABC00, D_809ABBFC, 0x1F4); - Math_StepToS(&D_809ABC08, D_809ABC04, 0xAA); - this->actor.shape.rot.x += D_809ABC00; - this->actor.shape.rot.y += D_809ABC08; - func_809AAF18(this); - func_809AAF58(this, play); + Math_StepToS(&sRotSpeedX, sRotSpeedXTarget, 0x1F4); + Math_StepToS(&sRotSpeedY, sRotSpeedYTarget, 0xAA); + this->actor.shape.rot.x += sRotSpeedX; + this->actor.shape.rot.y += sRotSpeedY; + ObjGrassCarry_UpdatePos(this); + ObjGrassCarry_UpdateBgCheckInfo(this, play); Collider_UpdateCylinder(&this->actor, &this->collider); CollisionCheck_SetAT(play, &play->colChkCtx, &this->collider.base); @@ -348,8 +347,8 @@ void func_809AB77C(ObjGrassCarry* this, PlayState* play) { void ObjGrassCarry_Update(Actor* thisx, PlayState* play) { ObjGrassCarry* this = THIS; - if (this->unk_190 == NULL) { - if ((this->actionFunc != func_809AB610) && (this->actionFunc != func_809AB77C)) { + if (this->grassManager == NULL) { + if ((this->actionFunc != ObjGrassCarry_LiftedUp) && (this->actionFunc != ObjGrassCarry_Fall)) { Actor_Kill(&this->actor); return; } @@ -358,6 +357,6 @@ void ObjGrassCarry_Update(Actor* thisx, PlayState* play) { this->actionFunc(this, play); } -void func_809ABB7C(Actor* this, PlayState* play) { +void ObjGrassCarry_Draw(Actor* this, PlayState* play) { Gfx_DrawDListOpa(play, gKusaBushType1DL); } diff --git a/src/overlays/actors/ovl_Obj_Grass_Carry/z_obj_grass_carry.h b/src/overlays/actors/ovl_Obj_Grass_Carry/z_obj_grass_carry.h index fa870305ef..3f20ea1c17 100644 --- a/src/overlays/actors/ovl_Obj_Grass_Carry/z_obj_grass_carry.h +++ b/src/overlays/actors/ovl_Obj_Grass_Carry/z_obj_grass_carry.h @@ -5,17 +5,17 @@ struct ObjGrassCarry; struct ObjGrass; -struct ObjGrassStruct1_1; +struct ObjGrassElement; typedef void (*ObjGrassCarryActionFunc)(struct ObjGrassCarry*, PlayState*); typedef struct ObjGrassCarry { /* 0x000 */ Actor actor; /* 0x144 */ ColliderCylinder collider; - /* 0x190 */ struct ObjGrass* unk_190; - /* 0x194 */ struct ObjGrassStruct1_1* unk_194; - /* 0x198 */ s16 unk_198; - /* 0x19A */ s16 unk_19A; + /* 0x190 */ struct ObjGrass* grassManager; + /* 0x194 */ struct ObjGrassElement* grassElem; + /* 0x198 */ s16 dropTable; + /* 0x19A */ s16 fallTimer; /* 0x19C */ ObjGrassCarryActionFunc actionFunc; } ObjGrassCarry; // size = 0x1A0 diff --git a/src/overlays/actors/ovl_Obj_Grass_Unit/z_obj_grass_unit.c b/src/overlays/actors/ovl_Obj_Grass_Unit/z_obj_grass_unit.c index c57ed608fc..dfb5d05276 100644 --- a/src/overlays/actors/ovl_Obj_Grass_Unit/z_obj_grass_unit.c +++ b/src/overlays/actors/ovl_Obj_Grass_Unit/z_obj_grass_unit.c @@ -1,38 +1,168 @@ /* * File: z_obj_grass_unit.c * Overlay: ovl_Obj_Grass_Unit - * Description: Spawner for circular patch of grass + * Description: Initializes a pattern of grass to be passed to an ObjGrass instance. + * + * ObjGrassUnit instances are intended to be spawned from room data so that just one ObjGrass instance manages the grass + * for that room. When a room with ObjGrassUnit spawns loads, it allocates ovl_Obj_Grass_Unit and creates N instances of + * ObjGrassUnit. The first instance processed initializes sGrassManager, then all instances live on that cycle pass type + * and position data to that ObjGrass instance before calling Actor_Kill. Since ovl_Obj_Grass_Unit uses + * ALLOCTYPE_NORMAL, it is deleted and loses the reference to the newly created ObjGrass. This allows for ObjGrassUnit + * spawned in a different room to be associated to a new ObjGrass instance. */ #include "z_obj_grass_unit.h" +#include "overlays/actors/ovl_Obj_Grass/z_obj_grass.h" +#include "overlays/actors/ovl_Obj_Grass_Carry/z_obj_grass_carry.h" #define FLAGS 0x00000000 #define THIS ((ObjGrassUnit*)thisx) -void ObjGrassUnit_Init(Actor* thisx, PlayState* play); +void ObjGrassUnit_Init(Actor* this, PlayState* play2); -#if 0 ActorInit Obj_Grass_Unit_InitVars = { - ACTOR_OBJ_GRASS_UNIT, - ACTORCAT_BG, - FLAGS, - GAMEPLAY_FIELD_KEEP, - sizeof(ObjGrassUnit), - (ActorFunc)ObjGrassUnit_Init, - (ActorFunc)Actor_Noop, - (ActorFunc)Actor_Noop, - (ActorFunc)NULL, + ACTOR_OBJ_GRASS_UNIT, ACTORCAT_BG, FLAGS, + GAMEPLAY_FIELD_KEEP, sizeof(ObjGrassUnit), (ActorFunc)ObjGrassUnit_Init, + (ActorFunc)Actor_Noop, (ActorFunc)Actor_Noop, (ActorFunc)NULL, }; -#endif +// Neat circular pattern with a single bush in the center +static VecPolar sGrassPatternCircle[] = { + { 0.0f, 0x0000 }, { 80.0f, 0x0000 }, { 80.0f, 0x2000 }, { 80.0f, 0x4000 }, { 80.0f, 0x6000 }, + { 80.0f, 0x8000 }, { 80.0f, 0xA000 }, { 80.0f, 0xC000 }, { 80.0f, 0xE000 }, +}; -#pragma GLOBAL_ASM("asm/non_matchings/overlays/ovl_Obj_Grass_Unit/func_809ABDE0.s") +// "Random" looking pattern +static VecPolar sGrassPatternMixed[] = { + { 40.0f, 0x0666 }, { 40.0f, 0x2CCC }, { 40.0f, 0x5999 }, { 40.0f, 0x8667 }, { 20.0f, 0xC000 }, { 80.0f, 0x1333 }, + { 80.0f, 0x4000 }, { 80.0f, 0x6CCC }, { 80.0f, 0x9334 }, { 80.0f, 0xACCD }, { 80.0f, 0xC667 }, { 60.0f, 0xE000 }, +}; -#pragma GLOBAL_ASM("asm/non_matchings/overlays/ovl_Obj_Grass_Unit/func_809ABE54.s") +typedef struct { + /* 0x0 */ s32 count; + /* 0x4 */ VecPolar* positions; +} ObjGrassUnitPattern; // size = 0x8 -#pragma GLOBAL_ASM("asm/non_matchings/overlays/ovl_Obj_Grass_Unit/func_809ABEC4.s") +static ObjGrassUnitPattern sGrassPatterns[2] = { + { ARRAY_COUNT(sGrassPatternCircle), sGrassPatternCircle }, + { ARRAY_COUNT(sGrassPatternMixed), sGrassPatternMixed }, +}; -#pragma GLOBAL_ASM("asm/non_matchings/overlays/ovl_Obj_Grass_Unit/func_809ABF38.s") +ObjGrass* sGrassManager = NULL; +ObjGrassCarry* sGrassCarry0 = NULL; +ObjGrassCarry* sGrassCarry1 = NULL; +s32 sInitialized = false; -#pragma GLOBAL_ASM("asm/non_matchings/overlays/ovl_Obj_Grass_Unit/ObjGrassUnit_Init.s") +s32 ObjGrassUnit_SpawnObjGrass(Actor* this, PlayState* play) { + sGrassManager = (ObjGrass*)Actor_Spawn(&play->actorCtx, play, ACTOR_OBJ_GRASS, 0.0f, 0.0f, 0.0f, 0, 0, 0, -1); + if (sGrassManager != NULL) { + sGrassManager->actor.room = this->room; + return true; + } + return false; +} + +s32 ObjGrassUnit_SpawnObjGrassCarry0(Actor* this, PlayState* play) { + sGrassCarry0 = + (ObjGrassCarry*)Actor_Spawn(&play->actorCtx, play, ACTOR_OBJ_GRASS_CARRY, 0.0f, 0.0f, 0.0f, 0, 0, 0, 0); + if (sGrassCarry0 != NULL) { + sGrassCarry0->actor.room = this->room; + return true; + } + return false; +} + +s32 ObjGrassUnit_SpawnObjGrassCarry1(Actor* this, PlayState* play) { + sGrassCarry1 = + (ObjGrassCarry*)Actor_Spawn(&play->actorCtx, play, ACTOR_OBJ_GRASS_CARRY, 0.0f, 0.0f, 0.0f, 0, 0, 0, 1); + if (sGrassCarry1 != NULL) { + sGrassCarry1->actor.room = this->room; + return true; + } + return false; +} + +s32 ObjGrassUnit_IsUnderwater(PlayState* play, Vec3f* pos) { + WaterBox* waterBox; + f32 ySurface; + s32 bgId; + + if (WaterBox_GetSurfaceImpl(play, &play->colCtx, pos->x, pos->z, &ySurface, &waterBox, &bgId) && + (pos->y < ySurface)) { + return true; + } + return false; +} + +void ObjGrassUnit_Init(Actor* this, PlayState* play2) { + PlayState* play = play2; + ObjGrassGroup* grassGroup; + ObjGrass* grassManager; + f32 homePosYSum; + f32 tmp; + s32 i; + VecPolar* grassPos; + CollisionPoly* poly; + s32 bgId; + ObjGrassElement* grassElem; + ObjGrassUnitPattern* grassPattern; + s8 dropTable = OBJGRASSUNIT_GET_DROPTABLE(this); + + if ((sGrassManager == NULL) && !ObjGrassUnit_SpawnObjGrass(this, play)) { + Actor_Kill(this); + return; + } + if ((sGrassCarry0 == NULL) && ObjGrassUnit_SpawnObjGrassCarry0(this, play)) { + sGrassCarry0->grassManager = sGrassManager; + } + if ((sGrassCarry1 == NULL) && ObjGrassUnit_SpawnObjGrassCarry1(this, play)) { + sGrassCarry1->grassManager = sGrassManager; + } + if (!sInitialized && (sGrassManager != NULL) && (sGrassCarry0 != NULL) && (sGrassCarry1 != NULL)) { + sInitialized = true; + sGrassManager->grassCarry[0] = sGrassCarry0; + sGrassManager->grassCarry[1] = sGrassCarry1; + sGrassCarry0->grassManager = sGrassManager; + sGrassCarry1->grassManager = sGrassManager; + } + + grassManager = sGrassManager; + if (sGrassManager->activeGrassGroups >= ARRAY_COUNT(sGrassManager->grassGroups)) { + Actor_Kill(this); + return; + } + + homePosYSum = 0.0f; + grassGroup = &grassManager->grassGroups[sGrassManager->activeGrassGroups]; + grassGroup->count = 0; + grassPattern = &sGrassPatterns[OBJGRASSUNIT_GET_PATTERN(this)]; + + for (i = 0; i < grassPattern->count; i++) { + grassElem = &grassGroup->elements[grassGroup->count]; + grassPos = &grassPattern->positions[i]; + + grassElem->pos.x = (Math_CosS((this->home.rot.y + grassPos->angle)) * grassPos->distance) + this->home.pos.x; + grassElem->pos.y = this->home.pos.y + 100.0f; + grassElem->pos.z = (Math_SinS((this->home.rot.y + grassPos->angle)) * grassPos->distance) + this->home.pos.z; + + grassElem->pos.y = BgCheck_EntityRaycastFloor5(&play->colCtx, &poly, &bgId, this, &grassElem->pos); + tmp = grassElem->pos.y - this->home.pos.y; + if ((fabsf(tmp) < 80.0f) && (grassElem->pos.y > BGCHECK_Y_MIN)) { + grassGroup->count++; + grassElem->rotY = (s16)(Rand_Next() >> 0x10); + grassElem->dropTable = dropTable; + if (ObjGrassUnit_IsUnderwater(play, &grassElem->pos)) { + grassElem->flags |= OBJ_GRASS_ELEM_UNDERWATER; + } + homePosYSum += grassElem->pos.y; + } + } + if (grassGroup->count > 0) { + grassManager->activeGrassGroups++; + grassGroup->homePos.x = this->home.pos.x; + grassGroup->homePos.y = (homePosYSum / grassGroup->count); + grassGroup->homePos.z = this->home.pos.z; + } + Actor_Kill(this); +} diff --git a/src/overlays/actors/ovl_Obj_Grass_Unit/z_obj_grass_unit.h b/src/overlays/actors/ovl_Obj_Grass_Unit/z_obj_grass_unit.h index 58a8a427ad..7c24d1e586 100644 --- a/src/overlays/actors/ovl_Obj_Grass_Unit/z_obj_grass_unit.h +++ b/src/overlays/actors/ovl_Obj_Grass_Unit/z_obj_grass_unit.h @@ -3,6 +3,9 @@ #include "global.h" +#define OBJGRASSUNIT_GET_DROPTABLE(thisx) (((thisx)->params >> 8) & 0x1F) +#define OBJGRASSUNIT_GET_PATTERN(thisx) ((thisx)->params & 1) + struct ObjGrassUnit; typedef struct ObjGrassUnit {