Shipwright/soh/src/overlays/actors/ovl_En_Fu/z_en_fu.c
Christopher Leggett 168e84498f
Rework Get Item Table to be more flexible for adding custom items (#1050)
* Adds ItemTableManager class.

* Implements new getItem table in game.

* Adds rando item table and way to differentiate tables in GetItemEntry.

* Adds rough ability to differentiate between rando and vanilla items.merge stashed changes from before develop-zhora merge

* Change ItemTableID to be uint16_t so we can use ModIndex for it.

* Should fix switch build

* Should fix switch build pt 2

* Adds new files to CMakeLists.

* Implements fixes for competing getItem calls.

* Correctly renders freestanding items

Particle effects are probably broken, need to fix them still, I pretty
much know how I would do that.

* Fixed Particle effects in the new getItem system.

* Fixes item fanfares

* Partially fixes Ice Traps

Obtaining a freestanding Ice Trap causes link to slide forward
and receiving one from an NPC plays the sound effect and damage
animation but doesn't freeze link.

* Some more partial ice trap fixing that wasn't pushed earlier

* Removes unused function override

* Replaces ::find with ::at and adds exception handling

* Removes some commented out code.

* Refactors rando's GetItemEntry array into two arrays.

One array is for the vanilla items that don't have GetItemEntries in
vanilla, the other is for rando exclusive items. They are stored in
separate arrays before getting added to the table so that we can apply
different modIndexes. The items in the first table have are handled
by the vanilla Item_Give, and the second table needed a custom
`Randomizer_Item_Give` function.

* Renames, relocates, and implements ModIndex enum.

* Removes now unused ItemIDs and GetItemIDs

Also makes all the necessary changes to other code that was still
using them indirectly through the GI to GID map that was removed.
There's quite a lot of changes here and I haven't had time to test them
yet.

* Re-implements GIMESSAGE_UNTRANSLATED as macro

* Removes commented out function.

* Throws exception if an invalid itemID is used

Addresses https://github.com/HarbourMasters/Shipwright/pull/1050#discussion_r943694857

* Removes ARRAY_SIZE in favor of ARRAY_COUNT

ARRAY_COUNT already exists in `macros.h`, I just didn't find it before.

Addresses https://github.com/HarbourMasters/Shipwright/pull/1050#discussion_r943153833

* Inverts CheckContainsRandoItem to CheckContainsVanillaItem.

Addresses https://github.com/HarbourMasters/Shipwright/pull/1050#discussion_r940895135

* Cleanup, bugfixes, removing the `- 1`s from `z_player.c`

* Fixes some funky formatting that got committed earlier.

* Adds else if to added fanfare sound cases.

Addresses https://github.com/HarbourMasters/Shipwright/pull/1050#discussion_r940112924 and https://github.com/HarbourMasters/Shipwright/pull/1050#discussion_r940113492

* Extends GetItemEntry to include getItemId

Also adapts some existing calls for both the entry and the id to only
get the entry.

* Extends GetItemEntry to include GID.

This allows for using it later when drawing freestanding items.

Addresses https://github.com/HarbourMasters/Shipwright/pull/1050#discussion_r943168136

* Rando-specific items use new textId again.

This got lost when merging develop-zhora in because I didn't have custom
messages merged when I started this.

* Sets global modIndex to MOD_NONE on scene load

Fixes a crash when buying items in shops due to them
not triggering the rando code that normally sets these items.
May have also been crashing vanilla playthroughs.

* Realized I had the bgm conditions wrong.

* Fixes "static drops" (i.e. sticks from withered babas)

* Fixes LACS/Prelude situation... again.

* Fixes too many arguments error.

Not sure why this didn't fail to build on Windows before.

* Fixes Link's Pocket items.

* Simplifies sram init for rando-specific items

* Fixes issues with approaching bottleable items.

* Fixes Ruto's Letter.

It was accidentally getting classified as a rando item.

* Should re-fix freestanding ice traps

* Makes freestanding items set player->getItemEntry.

This prevents freestanding items from setting the global modIndex.
This is part of a larger transition that needs to happen to switch
to setting getItemEntries for all of the rando items. This prevents
some things that set getItemId of GI_MAX from granting a Fire Medallion
when the global modIndex is MOD_RANDOMIZER.

* Makes sure we aren't using getItemEntry when not randoed.

* Replace Randomizer_GetRandomizedItemId with Randomizer_GetRandomizedItem and Randomizer_GetItemIdFromKnownCheck with Randomizer_GetItemFromKnownCheck

* Introduce some new methods and migrate most actors to them

* Fixes ocarina game skull kids to set player->getItemEntry

* Sets `z_en_box.c` to set `player->getItemEntry`

* Fix logical errors and migrate most of the rest of the rando checks to GiveItemEntryFromActor

* Use GiveItemEntryFromActorWithFixedRange in item00

* Fixes Anju to set player->getItemEntry.

* Add a few missing cases

* Additional fix for Skull Kid

* Fixes vanilla ice traps and randomized ice smoke

* Fixes rendering of treasure chest game items.

* Removes unused `Randomizer_GetItemIdFromGetItemId`.

* Cleans up an if statement for item00.

* Cleans up another if statement in item00

* This should fix a bug with the Gerudo Archery minigame.

I wasn't able to get the bug to happen after making this change.

* Documents our new GiveItemEntry fuctions.

* Uses more descriptive type name for ItemIDs for creating custom messages.

* Fixes potential issue with if statement.

* Fixes missed type change.

Co-authored-by: Garrett Cox <garrettjcox@gmail.com>
2022-08-23 20:11:38 -04:00

338 lines
12 KiB
C

/*
* File: z_en_fu.c
* Overlay: ovl_En_Fu
* Description: Windmill Man
*/
#include "z_en_fu.h"
#include "objects/object_fu/object_fu.h"
#include "scenes/indoors/hakasitarelay/hakasitarelay_scene.h"
#define FLAGS (ACTOR_FLAG_0 | ACTOR_FLAG_3 | ACTOR_FLAG_4 | ACTOR_FLAG_25)
#define FU_RESET_LOOK_ANGLE (1 << 0)
#define FU_WAIT (1 << 1)
void EnFu_Init(Actor* thisx, GlobalContext* globalCtx);
void EnFu_Destroy(Actor* thisx, GlobalContext* globalCtx);
void EnFu_Update(Actor* thisx, GlobalContext* globalCtx);
void EnFu_Draw(Actor* thisx, GlobalContext* globalCtx);
void EnFu_WaitChild(EnFu* this, GlobalContext* globalCtx);
void func_80A1DA04(EnFu* this, GlobalContext* globalCtx);
void EnFu_WaitAdult(EnFu* this, GlobalContext* globalCtx);
void EnFu_TeachSong(EnFu* this, GlobalContext* globalCtx);
void EnFu_WaitForPlayback(EnFu* this, GlobalContext* globalCtx);
void func_80A1DBA0(EnFu* this, GlobalContext* globalCtx);
void func_80A1DBD4(EnFu* this, GlobalContext* globalCtx);
void func_80A1DB60(EnFu* this, GlobalContext* globalCtx);
const ActorInit En_Fu_InitVars = {
ACTOR_EN_FU,
ACTORCAT_NPC,
FLAGS,
OBJECT_FU,
sizeof(EnFu),
(ActorFunc)EnFu_Init,
(ActorFunc)EnFu_Destroy,
(ActorFunc)EnFu_Update,
(ActorFunc)EnFu_Draw,
NULL,
};
static ColliderCylinderInit sCylinderInit = {
{
COLTYPE_NONE,
AT_NONE,
AC_ON | AC_TYPE_ENEMY,
OC1_ON | OC1_TYPE_ALL,
OC2_TYPE_1,
COLSHAPE_CYLINDER,
},
{
ELEMTYPE_UNK0,
{ 0x00000000, 0x00, 0x00 },
{ 0xFFCFFFFF, 0x00, 0x00 },
TOUCH_NONE,
BUMP_ON,
OCELEM_ON,
},
{ 30, 40, 0, { 0, 0, 0 } },
};
static Vec3f sMtxSrc = {
700.0f,
700.0f,
0.0f,
};
typedef enum {
/* 0x00 */ FU_FACE_CALM,
/* 0x01 */ FU_FACE_MAD
} EnFuFace;
void EnFu_Init(Actor* thisx, GlobalContext* globalCtx) {
s32 pad;
EnFu* this = (EnFu*)thisx;
ActorShape_Init(&this->actor.shape, 0.0f, ActorShadow_DrawCircle, 36.0f);
SkelAnime_InitFlex(globalCtx, &this->skelanime, &gWindmillManSkel, &gWindmillManPlayStillAnim, this->jointTable,
this->morphTable, FU_LIMB_MAX);
Animation_PlayLoop(&this->skelanime, &gWindmillManPlayStillAnim);
Collider_InitCylinder(globalCtx, &this->collider);
Collider_SetCylinder(globalCtx, &this->collider, &this->actor, &sCylinderInit);
this->actor.colChkInfo.mass = MASS_IMMOVABLE;
Actor_SetScale(&this->actor, 0.01f);
if (!LINK_IS_ADULT) {
this->actionFunc = EnFu_WaitChild;
this->facialExpression = FU_FACE_CALM;
} else {
this->actionFunc = EnFu_WaitAdult;
this->facialExpression = FU_FACE_MAD;
this->skelanime.playSpeed = 2.0f;
}
this->behaviorFlags = 0;
this->actor.targetMode = 6;
}
void EnFu_Destroy(Actor* thisx, GlobalContext* globalCtx) {
EnFu* this = (EnFu*)thisx;
Collider_DestroyCylinder(globalCtx, &this->collider);
}
s32 func_80A1D94C(EnFu* this, GlobalContext* globalCtx, u16 textID, EnFuActionFunc actionFunc) {
s16 yawDiff;
if (Actor_ProcessTalkRequest(&this->actor, globalCtx)) {
this->actionFunc = actionFunc;
return true;
}
this->actor.textId = textID;
yawDiff = this->actor.yawTowardsPlayer - this->actor.shape.rot.y;
if ((ABS(yawDiff) < 0x2301) && (this->actor.xzDistToPlayer < 100.0f)) {
func_8002F2CC(&this->actor, globalCtx, 100.0f);
} else {
this->behaviorFlags |= FU_RESET_LOOK_ANGLE;
}
return false;
}
void func_80A1DA04(EnFu* this, GlobalContext* globalCtx) {
if (Actor_TextboxIsClosing(&this->actor, globalCtx)) {
this->behaviorFlags &= ~FU_WAIT;
this->actionFunc = EnFu_WaitChild;
if (this->skelanime.animation == &gWindmillManPlayAndMoveHeadAnim) {
Animation_Change(&this->skelanime, &gWindmillManPlayStillAnim, 1.0f, 0.0f,
Animation_GetLastFrame(&gWindmillManPlayStillAnim), ANIMMODE_ONCE, -4.0f);
}
}
}
void EnFu_WaitChild(EnFu* this, GlobalContext* globalCtx) {
u16 textID = Text_GetFaceReaction(globalCtx, 0xB);
if (textID == 0) {
textID = (gSaveContext.eventChkInf[6] & 0x80) ? 0x5033 : 0x5032;
}
// if ACTOR_FLAG_8 is set and textID is 0x5033, change animation
// if func_80A1D94C returns 1, actionFunc is set to func_80A1DA04
if (func_80A1D94C(this, globalCtx, textID, func_80A1DA04)) {
if (textID == 0x5033) {
Animation_Change(&this->skelanime, &gWindmillManPlayAndMoveHeadAnim, 1.0f, 0.0f,
Animation_GetLastFrame(&gWindmillManPlayAndMoveHeadAnim), ANIMMODE_ONCE, -4.0f);
}
}
}
void GivePlayerRandoRewardSongOfStorms(EnFu* windmillGuy, GlobalContext* globalCtx, RandomizerCheck check) {
if (windmillGuy->actor.parent != NULL && windmillGuy->actor.parent->id == GET_PLAYER(globalCtx)->actor.id &&
!Flags_GetTreasure(globalCtx, 0x1F)) {
Flags_SetTreasure(globalCtx, 0x1F);
windmillGuy->actionFunc = func_80A1DBD4;
} else if (!Flags_GetTreasure(globalCtx, 0x1F)) {
GetItemEntry getItemEntry = Randomizer_GetItemFromKnownCheck(check, RG_SONG_OF_STORMS);
GiveItemEntryFromActor(&windmillGuy->actor, globalCtx, getItemEntry, 10000.0f, 100.0f);
}
}
void func_WaitForSongGive(EnFu* this, GlobalContext* globalCtx) {
GivePlayerRandoRewardSongOfStorms(this, globalCtx, RC_SONG_FROM_WINDMILL);
}
void func_80A1DB60(EnFu* this, GlobalContext* globalCtx) {
if (globalCtx->csCtx.state == CS_STATE_IDLE) {
this->actionFunc = EnFu_WaitAdult;
gSaveContext.eventChkInf[5] |= 0x800;
globalCtx->msgCtx.ocarinaMode = OCARINA_MODE_04;
}
if (gSaveContext.n64ddFlag) {
this->actionFunc = func_WaitForSongGive;
}
}
void func_80A1DBA0(EnFu* this, GlobalContext* globalCtx) {
if (Actor_TextboxIsClosing(&this->actor, globalCtx)) {
this->actionFunc = EnFu_WaitAdult;
}
}
void func_80A1DBD4(EnFu* this, GlobalContext* globalCtx) {
Player* player = GET_PLAYER(globalCtx);
if (gSaveContext.n64ddFlag && (Message_GetState(&globalCtx->msgCtx) == TEXT_STATE_CLOSING)) {
globalCtx->msgCtx.ocarinaMode = OCARINA_MODE_03;
}
if (globalCtx->msgCtx.ocarinaMode >= OCARINA_MODE_04) {
this->actionFunc = EnFu_WaitAdult;
globalCtx->msgCtx.ocarinaMode = OCARINA_MODE_04;
this->actor.flags &= ~ACTOR_FLAG_16;
} else if (globalCtx->msgCtx.ocarinaMode == OCARINA_MODE_03) {
func_80078884(NA_SE_SY_CORRECT_CHIME);
this->actionFunc = func_80A1DB60;
this->actor.flags &= ~ACTOR_FLAG_16;
if (!gSaveContext.n64ddFlag) {
globalCtx->csCtx.segment = SEGMENTED_TO_VIRTUAL(gSongOfStormsCs);
gSaveContext.cutsceneTrigger = 1;
Item_Give(globalCtx, ITEM_SONG_STORMS);
}
globalCtx->msgCtx.ocarinaMode = OCARINA_MODE_00;
gSaveContext.eventChkInf[6] |= 0x20;
} else if (globalCtx->msgCtx.ocarinaMode == OCARINA_MODE_02) {
player->stateFlags2 &= ~0x1000000;
this->actionFunc = EnFu_WaitAdult;
} else if (globalCtx->msgCtx.ocarinaMode == OCARINA_MODE_01) {
player->stateFlags2 |= 0x800000;
}
}
void EnFu_WaitForPlayback(EnFu* this, GlobalContext* globalCtx) {
Player* player = GET_PLAYER(globalCtx);
player->stateFlags2 |= 0x800000;
// if dialog state is 7, player has played back the song
if (Message_GetState(&globalCtx->msgCtx) == TEXT_STATE_SONG_DEMO_DONE) {
func_8010BD58(globalCtx, OCARINA_ACTION_PLAYBACK_STORMS);
this->actionFunc = func_80A1DBD4;
}
}
void EnFu_TeachSong(EnFu* this, GlobalContext* globalCtx) {
Player* player = GET_PLAYER(globalCtx);
player->stateFlags2 |= 0x800000;
// if dialog state is 2, start song demonstration
if (Message_GetState(&globalCtx->msgCtx) == TEXT_STATE_CLOSING) {
this->behaviorFlags &= ~FU_WAIT;
Audio_OcaSetInstrument(4); // seems to be related to setting instrument type
func_8010BD58(globalCtx, OCARINA_ACTION_TEACH_STORMS);
this->actionFunc = EnFu_WaitForPlayback;
}
}
void EnFu_WaitAdult(EnFu* this, GlobalContext* globalCtx) {
static s16 yawDiff;
Player* player = GET_PLAYER(globalCtx);
yawDiff = this->actor.yawTowardsPlayer - this->actor.shape.rot.y;
if ((gSaveContext.eventChkInf[5] & 0x800)) {
func_80A1D94C(this, globalCtx, 0x508E, func_80A1DBA0);
} else if (player->stateFlags2 & 0x1000000) {
this->actor.textId = 0x5035;
Message_StartTextbox(globalCtx, this->actor.textId, NULL);
this->actionFunc = gSaveContext.n64ddFlag ? func_80A1DBD4 : EnFu_TeachSong;
this->behaviorFlags |= FU_WAIT;
} else if (Actor_ProcessTalkRequest(&this->actor, globalCtx)) {
this->actionFunc = func_80A1DBA0;
} else if (ABS(yawDiff) < 0x2301) {
if (this->actor.xzDistToPlayer < 100.0f) {
this->actor.textId = 0x5034;
func_8002F2CC(&this->actor, globalCtx, 100.0f);
player->stateFlags2 |= 0x800000;
}
}
}
void EnFu_Update(Actor* thisx, GlobalContext* globalCtx) {
s32 pad;
EnFu* this = (EnFu*)thisx;
Collider_UpdateCylinder(&this->actor, &this->collider);
CollisionCheck_SetOC(globalCtx, &globalCtx->colChkCtx, &this->collider.base);
Actor_MoveForward(&this->actor);
Actor_UpdateBgCheckInfo(globalCtx, &this->actor, 0.0f, 0.0f, 0.0f, 4);
if ((!(this->behaviorFlags & FU_WAIT)) && (SkelAnime_Update(&this->skelanime) != 0)) {
Animation_Change(&this->skelanime, this->skelanime.animation, 1.0f, 0.0f,
Animation_GetLastFrame(this->skelanime.animation), ANIMMODE_ONCE, 0.0f);
}
this->actionFunc(this, globalCtx);
if ((this->behaviorFlags & FU_RESET_LOOK_ANGLE)) {
Math_SmoothStepToS(&this->lookAngleOffset.x, 0, 6, 6200, 100);
Math_SmoothStepToS(&this->lookAngleOffset.y, 0, 6, 6200, 100);
Math_SmoothStepToS(&this->unk_2A2.x, 0, 6, 6200, 100);
Math_SmoothStepToS(&this->unk_2A2.y, 0, 6, 6200, 100);
this->behaviorFlags &= ~FU_RESET_LOOK_ANGLE;
} else {
func_80038290(globalCtx, &this->actor, &this->lookAngleOffset, &this->unk_2A2, this->actor.focus.pos);
}
}
s32 EnFu_OverrideLimbDraw(GlobalContext* globalCtx, s32 limbIndex, Gfx** dList, Vec3f* pos, Vec3s* rot, void* thisx) {
EnFu* this = (EnFu*)thisx;
s32 pad;
if (limbIndex == FU_LIMB_UNK) {
return false;
}
switch (limbIndex) {
case FU_LIMB_HEAD:
rot->x += this->lookAngleOffset.y;
rot->z += this->lookAngleOffset.x;
break;
case FU_LIMB_CHEST_MUSIC_BOX:
break;
}
if (!(this->behaviorFlags & FU_WAIT)) {
return false;
}
if (limbIndex == FU_LIMB_CHEST_MUSIC_BOX) {
rot->y += (Math_SinS((globalCtx->state.frames * (limbIndex * 50 + 0x814))) * 200.0f);
rot->z += (Math_CosS((globalCtx->state.frames * (limbIndex * 50 + 0x940))) * 200.0f);
}
return false;
}
void EnFu_PostLimbDraw(GlobalContext* globalCtx, s32 limbIndex, Gfx** dList, Vec3s* rot, void* thisx) {
EnFu* this = (EnFu*)thisx;
if (limbIndex == FU_LIMB_HEAD) {
Matrix_MultVec3f(&sMtxSrc, &this->actor.focus.pos);
}
}
void EnFu_Draw(Actor* thisx, GlobalContext* globalCtx) {
static void* sEyesSegments[] = { gWindmillManEyeClosedTex, gWindmillManEyeAngryTex };
static void* sMouthSegments[] = { gWindmillManMouthOpenTex, gWindmillManMouthAngryTex };
s32 pad;
EnFu* this = (EnFu*)thisx;
OPEN_DISPS(globalCtx->state.gfxCtx);
func_800943C8(globalCtx->state.gfxCtx);
gSPSegment(POLY_OPA_DISP++, 0x08, SEGMENTED_TO_VIRTUAL(sEyesSegments[this->facialExpression]));
gSPSegment(POLY_OPA_DISP++, 0x09, SEGMENTED_TO_VIRTUAL(sMouthSegments[this->facialExpression]));
SkelAnime_DrawFlexOpa(globalCtx, this->skelanime.skeleton, this->skelanime.jointTable, this->skelanime.dListCount,
EnFu_OverrideLimbDraw, EnFu_PostLimbDraw, this);
CLOSE_DISPS(globalCtx->state.gfxCtx);
}