* Match Player_GetHeight * Another bunch * Fix merge conflict * rename Gfx_DrawDListXlu * add WEEROR * Actor_Spawn * almost Actor_SpawnTransitionActors and Actor_Delete * A bunch of small actors * More renames * format * Some Player renames * a few more * import data * run formatter * func_800B7170 * whoops * Fix merge issues * Whoops 2 * func_800B83BC and func_800B83F8 * Actor_IsActorFacingPlayerAndWithinRange * add some prototypes * match Actor_UpdateBgCheckInfo * func_800B7678 * mark Actor_SpawnAsChildAndCutscene as non_matching * Actor_Draw * Update is chaotic * 2 new matches * func_800BC8B8 * Another bunch * function renames * run formatter * cleanup * remove unnecesary casts * add missing sfx * Fix renames * fix merge * func_800BF7CC * small bunch * another bunch * func_800BE184 non_matching * two more * split z_cheap_proc * Another bunch * another bunch * a few and a non matching * yeee * a * Actor_DrawAll non_equivalent * Actor_RecordUndrawnActor * i don't know what to put in this commit message * func_800B4B50 non matching * func_800B42F8 non matching * func_800B5040 * func_800B5814 non_equiv * func_800B6584 * func_800B6608 * func_800B6680 * func_800B7E04 * func_800B8118 * func_800b9170 * , * func_800BC4EC * func_800BA6FC * func_800BA798 * func_800BA8B8 * Actor_LoadOverlay * small cleanup * func_800BB2D0 * meh * func_800BBAC0 * func_800BC270 * func_800B5208 non matching * Fix warnings * meh * rename some ActorShadow_ functions * fairy * Flags_ * fix warnings * format * Actor_PickUp and family * func_800B8E58 * match Actor_RemoveFromCategory * another bit of docs * Match func_800B86C8 * And another bit * rename Player_GetRunSpeedLimit * func_800B9E84 * func_800BE63C * func_800BB8EC * match func_800B5814 * match func_800B9334 * cleanup * fix conflicts: first pass * another fix * actorfixer fix * fix conflicts * func_800BE680 non_equivalent * Improve func_800BE680 a bit * func_800BE680 equivalent (?) * func_800BE680 equivalent * Actor_UpdateActor equivalent * format * use some ExchangeItemID enum values * Some more cleaning * more cleanup * More name stealing from OoT * match func_800B82EC * match func_800B9D1C and a bit of cleanup * Add ACTOR_FLAGS placeholders * Renames and match func_800BE184 * last pass of name stealing * format * fix conflicts * more cleanup * more cleanup * cleanup and OVERLAY_RELOCATION_OFFSET macro * Remove prototypes of obviously internal-only functions, update variable names, forward declare where necessary, remove all `param_\d`s * remove newlines * minor rename * Use ACTOR_FLAGS in z_actor * Match func_800BE3D0 * Rename movement functions * Document Actor_CalcOffsetOrientedToDrawRotation * velX -> horizontalSpeed * A bit of documentation for actor movement functions * format * Fix merge issues * format * Format * Fix renames * fix warnings * fix conflicts * review :D * Update src/overlays/actors/ovl_En_Ma4/z_en_ma4.c Co-authored-by: Derek Hensley <hensley.derek58@gmail.com> * Fix * format * Actor_SpawnSetupActors * engineer review * Update src/code/z_actor.c Co-authored-by: engineer124 <47598039+engineer124@users.noreply.github.com> * A bunch of Engineer's reviews * more Engineer's review * a * whoops * run actorfixer * c'mon * 😮💨 * whoops * warning * More engineer's review * run format * I'm dumb * a * match func_800BE680 * Match Actor_DrawZTarget * Match Actor_SpawnAsChildAndCutscene, fix non-equivalent in Actor_UpdateActor * Fix merge issue * format * update actor * Steal a bit of @Thar0 documentation from OoT's z_message * Run actorfixer * Fix renames * Match func_800B4B50 thanks to @hensldm * Improve ActorShadow_DrawFeet thanks to @hensldm * whoops * Actor_PlaySfxAtProjectedPos * Actor_UpdateActor matched by @hensldm * Match func_800BA2FC by @hensldm * Match Actor_SpawnTransitionActors by @hensldm * Match func_800BB604 by @hensldm * Match Actor_DrawAll by @hensldm * ActorShadow_DrawFeet by @hensldm * Actor_UpdateAll by @hensldm * Match func_800BCCDC by @engineer124 * Small Actor_PlaySfxAtPos by @engineer124 * ACTOR_FLAGS_ALL and a bit of cleanup * Add invisible comment * Small docs pass * Fix merge * Engineer's review * format lol * Actor_DrawDoorLock docs * Actor_SpawnShieldParticlesMetal * fix merge issues * sActorFaultClient * fix * commit message * Run actorfixer.py && format.sh * Fix warnings * fixes * format * bss * Update include/functions.h Co-authored-by: Derek Hensley <hensley.derek58@gmail.com> * Address review * Fix merge issues, format and such * fix merge issues * Add ACTORCAT_MAX * actorList -> actorLists * Fix merge issues * format * Enable WERROR on jenkinsfile * Fix merge * Use object symbols * address review * format * review * fix merge issues * fix * VRAM_PTR_SIZE, small cleanup and format * review Co-authored-by: Elliptic Ellipsis <elliptic.ellipsis@gmail.com> Co-authored-by: Derek Hensley <hensley.derek58@gmail.com> Co-authored-by: engineer124 <47598039+engineer124@users.noreply.github.com> Co-authored-by: engineer124 <engineer124engineer124@gmail.com>
24 KiB
Advanced control flow
Nice as EnRecepgirl was, she was somewhat lacking in complexity. In this document, we'll look at something rather more complicated than any of the functions she had.
Again our example will be taken from a small NPC: this time, EnMs (Bean Seller). Most of its functions are even simpler than EnRecepgirl's, and fairly quickly we can get to
Large code block, click to show.
#include "z_en_ms.h"
#define FLAGS 0x00000009
#define THIS ((EnMs*)thisx)
void EnMs_Init(Actor* thisx, GlobalContext* globalCtx);
void EnMs_Destroy(Actor* thisx, GlobalContext* globalCtx);
void EnMs_Update(Actor* thisx, GlobalContext* globalCtx);
void EnMs_Draw(Actor* thisx, GlobalContext* globalCtx);
void func_80952734(EnMs* this, GlobalContext* globalCtx);
void func_809527F8(EnMs* this, GlobalContext* globalCtx);
void func_809529AC(EnMs* this, GlobalContext* globalCtx);
void func_80952A1C(EnMs *this, GlobalContext *globalCtx);
const ActorInit En_Ms_InitVars = {
ACTOR_EN_MS,
ACTORCAT_NPC,
FLAGS,
OBJECT_MS,
sizeof(EnMs),
(ActorFunc)EnMs_Init,
(ActorFunc)EnMs_Destroy,
(ActorFunc)EnMs_Update,
(ActorFunc)EnMs_Draw,
};
static ColliderCylinderInitType1 D_80952BA0 = {
{ COLTYPE_NONE, AT_NONE, AC_ON | AC_TYPE_PLAYER, OC1_ON | OC1_TYPE_ALL, COLSHAPE_CYLINDER, },
{ ELEMTYPE_UNK0, { 0x00000000, 0x00, 0x00 }, { 0xF7CFFFFF, 0x00, 0x00 }, TOUCH_NONE | TOUCH_SFX_NORMAL, BUMP_ON, OCELEM_ON, },
{ 22, 37, 0, { 0, 0, 0 } },
};
static InitChainEntry D_80952BCC[] = {
ICHAIN_U8(targetMode, 2, ICHAIN_CONTINUE),
ICHAIN_F32(targetArrowOffset, 500, ICHAIN_STOP),
};
extern ColliderCylinderInitType1 D_80952BA0;
extern InitChainEntry D_80952BCC[];
extern AnimationHeader D_060005EC;
extern FlexSkeletonHeader D_06003DC0;
void EnMs_Init(Actor* thisx, GlobalContext* globalCtx) {
EnMs* this = THIS;
Actor_ProcessInitChain(thisx, D_80952BCC);
SkelAnime_InitFlex(globalCtx, &this->skelAnime, &D_06003DC0, &D_060005EC, this->jointTable, this->morphTable, 9);
Collider_InitCylinder(globalCtx, &this->collider);
Collider_SetCylinderType1(globalCtx, &this->collider, &this->actor, &D_80952BA0);
ActorShape_Init(&this->actor.shape, 0.0f, ActorShadow_DrawCircle, 35.0f);
Actor_SetScale(&this->actor, 0.015f);
this->actor.colChkInfo.mass = 0xFF;
this->actionFunc = func_80952734;
this->actor.speedXZ = 0.0f;
this->actor.velocity.y = 0.0f;
this->actor.gravity = -1.0f;
}
void EnMs_Destroy(Actor* thisx, GlobalContext* globalCtx) {
EnMs* this = THIS;
Collider_DestroyCylinder(globalCtx, &this->collider);
}
void func_80952734(EnMs* this, GlobalContext* globalCtx) {
s16 temp_v1 = this->actor.yawTowardsPlayer - this->actor.shape.rot.y;
if (gSaveContext.inventory.items[10] == ITEM_NONE) {
this->actor.textId = 0x92E;
} else {
this->actor.textId = 0x932;
}
if (Actor_ProcessTalkRequest(&this->actor, &globalCtx->state) != 0) {
this->actionFunc = func_809527F8;
return;
}
if (this->actor.xzDistToPlayer < 90.0f) {
if (ABS_ALT(temp_v1) < 0x2000) {
func_800B8614(&this->actor, globalCtx, 90.0f);
}
}
}
#pragma GLOBAL_ASM("asm/non_matchings/overlays/ovl_En_Ms/func_809527F8.s")
void func_809529AC(EnMs *this, GlobalContext *globalCtx) {
if (Actor_HasParent(&this->actor, globalCtx)) {
this->actor.textId = 0;
func_800B8500(&this->actor, globalCtx, this->actor.xzDistToPlayer, this->actor.playerHeightRel, 0);
this->actionFunc = func_80952A1C;
} else {
Actor_PickUp(&this->actor, globalCtx, 0x35, this->actor.xzDistToPlayer, this->actor.playerHeightRel);
}
}
void func_80952A1C(EnMs *this, GlobalContext *globalCtx) {
if (Actor_ProcessTalkRequest(&this->actor, &globalCtx->state)) {
func_80151938(globalCtx, 0x936U);
this->actionFunc = func_809527F8;
} else {
func_800B8500(&this->actor, globalCtx, this->actor.xzDistToPlayer, this->actor.playerHeightRel, -1);
}
}
void EnMs_Update(Actor* thisx, GlobalContext* globalCtx) {
s32 pad;
EnMs* this = THIS;
Actor_SetFocus(&this->actor, 20.0f);
this->actor.targetArrowOffset = 500.0f;
Actor_SetScale(&this->actor, 0.015f);
SkelAnime_Update(&this->skelAnime);
this->actionFunc(this, globalCtx);
Collider_UpdateCylinder(&this->actor, &this->collider);
CollisionCheck_SetOC(globalCtx, &globalCtx->colChkCtx, &this->collider.base);
}
void EnMs_Draw(Actor* thisx, GlobalContext* globalCtx) {
EnMs* this = THIS;
func_8012C28C(globalCtx->state.gfxCtx);
SkelAnime_DrawFlexOpa(globalCtx, this->skelAnime.skeleton, this->skelAnime.jointTable, this->skelAnime.dListCount, NULL,
NULL, &this->actor);
}
(Skipping any documentation we might have done.) Indeed, this actor is so simple so far that you can see why it wasn't worth using most of it for the rest of the tutorial. func_809527F8 is a different story, however. We know it's an action function since it's set to the actionFunc in func_80952A1C. But mips2c gives us
$ ../mips_to_c/mips_to_c.py asm/non_matchings/overlays/ovl_En_Ms/func_809527F8.s --context ctx.c --gotos-only
void func_809527F8(EnMs *this, GlobalContext *globalCtx) {
u8 temp_v0;
u8 temp_v0_2;
temp_v0 = Message_GetState(&globalCtx->msgCtx);
if (temp_v0 != 4) {
if (temp_v0 != 5) {
if ((temp_v0 == 6) && (func_80147624(globalCtx) != 0)) {
this->actionFunc = func_80952734;
return;
}
// Duplicate return node #17. Try simplifying control flow for better match
return;
}
if (func_80147624(globalCtx) != 0) {
func_801477B4(globalCtx);
Actor_PickUp((Actor *) this, globalCtx, 0x35, this->actor.xzDistToPlayer, this->actor.playerHeightRel);
this->actionFunc = func_809529AC;
return;
}
// Duplicate return node #17. Try simplifying control flow for better match
return;
}
if (func_80147624(globalCtx) != 0) {
temp_v0_2 = globalCtx->msgCtx.choiceIndex;
if (temp_v0_2 != 0) {
if (temp_v0_2 != 1) {
}
func_8019F230();
func_80151938(globalCtx, 0x934U);
// Duplicate return node #17. Try simplifying control flow for better match
return;
}
func_801477B4(globalCtx);
if ((s32) gSaveContext.rupees < 0xA) {
play_sound(0x4806U);
func_80151938(globalCtx, 0x935U);
return;
}
if ((s32) gSaveContext.inventory.ammo[gItemSlots[0xA]] >= 0x14) {
play_sound(0x4806U);
func_80151938(globalCtx, 0x937U);
return;
}
func_8019F208();
Actor_PickUp((Actor *) this, globalCtx, 0x35, 90.0f, 10.0f);
func_801159EC(-0xA);
this->actionFunc = func_809529AC;
}
}
which is long, messy, and contains some rather nasty-looking control flow, including horrors like
temp_v0 = Message_GetState(&globalCtx->msgCtx);
if (temp_v0 != 4) {
if (temp_v0 != 5) {
if ((temp_v0 == 6) && (func_80147624(globalCtx) != 0)) {
this->actionFunc = func_80952734;
return;
}
// Duplicate return node #17. Try simplifying control flow for better match
return;
}
If you read the OoT tutorial, you'll know these nested negated ifs all using the same variable are a good indicator that there's a switch. The problem is working out how to write it.
Goto-only mode
For didactic purposes, we'll use a feature of mips2c called goto-only mode to examine this. This is not the only way of doing it, but it is good practice for a beginner to this sort of control flow. Running
../mips_to_c/mips_to_c.py asm/non_matchings/overlays/ovl_En_Ms/func_809527F8.s --context ctx.c --gotos-only
instead will produce
void func_809527F8(EnMs *this, GlobalContext *globalCtx) {
u8 temp_v0;
u8 temp_v0_2;
temp_v0 = Message_GetState(&globalCtx->msgCtx);
if (temp_v0 == 4) {
goto block_7;
}
if (temp_v0 == 5) {
goto block_5;
}
if (temp_v0 != 6) {
goto block_17;
}
if (func_80147624(globalCtx) == 0) {
goto block_17;
}
this->actionFunc = func_80952734;
return;
block_5:
if (func_80147624(globalCtx) == 0) {
goto block_17;
}
func_801477B4(globalCtx);
Actor_PickUp((Actor *) this, globalCtx, 0x35, this->actor.xzDistToPlayer, this->actor.playerHeightRel);
this->actionFunc = func_809529AC;
return;
block_7:
if (func_80147624(globalCtx) == 0) {
goto block_17;
}
temp_v0_2 = globalCtx->msgCtx.choiceIndex;
if (temp_v0_2 == 0) {
goto block_11;
}
if (temp_v0_2 == 1) {
goto block_16;
}
goto block_16;
block_11:
func_801477B4(globalCtx);
if ((s32) gSaveContext.rupees >= 0xA) {
goto block_13;
}
play_sound(0x4806U);
func_80151938(globalCtx, 0x935U);
return;
block_13:
if ((s32) gSaveContext.inventory.ammo[gItemSlots[0xA]] < 0x14) {
goto block_15;
}
play_sound(0x4806U);
func_80151938(globalCtx, 0x937U);
return;
block_15:
func_8019F208();
Actor_PickUp((Actor *) this, globalCtx, 0x35, 90.0f, 10.0f);
func_801159EC(-0xA);
this->actionFunc = func_809529AC;
return;
block_16:
func_8019F230();
func_80151938(globalCtx, 0x934U);
block_17:
return;
}
which in many ways looks worse: you can see why the use of gotos in code is strongly discouraged. However, if you throw this in diff.py, you'll find it's rather closer than you'd have thought. Goto-only mode has the advantages that
- code is always in the right order: mips2c has not had to reorder anything to get the ifs to work out
- it is often possible to get quite close with gotos, then start removing them, checking the matching status at each point. This is usually easier than trying to puzzle out the way it's trying to jump out of an
if ( || )or similar. - if you're trying to keep track of where you are in the code, the gotos mean that it is closer to the assembly in the first place.
Eliminating the gotos
The simplest sort of block label to eliminate is one that is only used once, and where the corresponding goto jumps over a simple block of code with no extra internal control flow structure. There are two obvious examples of this here, the first being
if ((s32) gSaveContext.rupees >= 0xA) {
goto block_13;
}
play_sound(0x4806U);
func_80151938(globalCtx, 0x935U);
return;
block_13:
Currently, this says to jump over the code block play_sound... if the condition in the if is satisfied. In non-goto terms, this means that the block should be run if the condition is not satisfied. This also illustrates a general property of goto-only mode: you have to reverse the senses of all of the ifs. Therefore the appropriate approach is to swap the if round, put the code block inside, and remove the goto and the label:
if (gSaveContext.rupees < 0xA) {
play_sound(0x4806U);
func_80151938(globalCtx, 0x935U);
return;
}
Likewise, one can do this with block_15.
If you examine appropriate part of the diff, you will usually find that such eliminations make no, or very little, difference to the compiled code.
void func_809527F8(EnMs *this, GlobalContext *globalCtx) {
u8 temp_v0;
u8 temp_v0_2;
temp_v0 = Message_GetState(&globalCtx->msgCtx);
if (temp_v0 == 4) {
goto block_7;
}
if (temp_v0 == 5) {
goto block_5;
}
if (temp_v0 != 6) {
goto block_17;
}
if (func_80147624(globalCtx) == 0) {
goto block_17;
}
this->actionFunc = func_80952734;
return;
block_5:
if (func_80147624(globalCtx) == 0) {
goto block_17;
}
func_801477B4(globalCtx);
Actor_PickUp((Actor *) this, globalCtx, 0x35, this->actor.xzDistToPlayer, this->actor.playerHeightRel);
this->actionFunc = func_809529AC;
return;
block_7:
if (func_80147624(globalCtx) == 0) {
goto block_17;
}
temp_v0_2 = globalCtx->msgCtx.choiceIndex;
if (temp_v0_2 == 0) {
goto block_11;
}
if (temp_v0_2 == 1) {
goto block_16;
}
goto block_16;
block_11:
func_801477B4(globalCtx);
if (gSaveContext.rupees < 0xA) {
play_sound(0x4806U);
func_80151938(globalCtx, 0x935U);
return;
}
if (gSaveContext.inventory.ammo[gItemSlots[0xA]] >= 0x14) {
play_sound(0x4806U);
func_80151938(globalCtx, 0x937U);
return;
}
func_8019F208();
Actor_PickUp((Actor *) this, globalCtx, 0x35, 90.0f, 10.0f);
func_801159EC(-0xA);
this->actionFunc = func_809529AC;
return;
block_16:
func_8019F230();
func_80151938(globalCtx, 0x934U);
block_17:
return;
}
We can't apply this rule any more, so we need to move on to the next: block_17 just contains a return. So we can replace it by return everywhere it appears.
void func_809527F8(EnMs *this, GlobalContext *globalCtx) {
u8 temp_v0;
u8 temp_v0_2;
temp_v0 = Message_GetState(&globalCtx->msgCtx);
if (temp_v0 == 4) {
goto block_7;
}
if (temp_v0 == 5) {
goto block_5;
}
if (temp_v0 != 6) {
return;
}
if (func_80147624(globalCtx) == 0) {
return;
}
this->actionFunc = func_80952734;
return;
block_5:
if (func_80147624(globalCtx) == 0) {
return;
}
func_801477B4(globalCtx);
Actor_PickUp((Actor *) this, globalCtx, 0x35, this->actor.xzDistToPlayer, this->actor.playerHeightRel);
this->actionFunc = func_809529AC;
return;
block_7:
if (func_80147624(globalCtx) == 0) {
return;
}
temp_v0_2 = globalCtx->msgCtx.choiceIndex;
if (temp_v0_2 == 0) {
goto block_11;
}
if (temp_v0_2 == 1) {
goto block_16;
}
goto block_16;
block_11:
func_801477B4(globalCtx);
if (gSaveContext.rupees < 0xA) {
play_sound(0x4806U);
func_80151938(globalCtx, 0x935U);
return;
}
if (gSaveContext.inventory.ammo[gItemSlots[0xA]] >= 0x14) {
play_sound(0x4806U);
func_80151938(globalCtx, 0x937U);
return;
}
func_8019F208();
Actor_PickUp((Actor *) this, globalCtx, 0x35, 90.0f, 10.0f);
func_801159EC(-0xA);
this->actionFunc = func_809529AC;
return;
block_16:
func_8019F230();
func_80151938(globalCtx, 0x934U);
}
Our next rule is about non-crossing blocks. If two code blocks do not contain any jumps between them, we can treat them separately. This is almost true for the code after block_7, were it not for the returns; of course returns are a special case because they can be used to be escape from a function at any point. This doesn't get us very far in this case, unfortunately, but it does tell us we can look at the second half of the function separately.
Now let's start thinking about switches. A good indicator of a switch in goto-only mode is something like
temp_v0_2 = globalCtx->msgCtx.choiceIndex;
if (temp_v0_2 == 0) {
goto block_11;
}
if (temp_v0_2 == 1) {
goto block_16;
}
goto block_16;
because
- there are multiple ifs that are simple numeric comparisons of the same argument
- the goto blocks are in the same order as the ifs
- there is one last goto at the end that triggers if none of the ifs does: this sounds an awful lot like a
default!
So let us rewrite the entire second half as a switch:
switch (globalCtx->msgCtx.choiceIndex) {
case 0:
func_801477B4(globalCtx);
if (gSaveContext.rupees < 0xA) {
play_sound(0x4806U);
func_80151938(globalCtx, 0x935U);
return;
}
if (gSaveContext.inventory.ammo[gItemSlots[0xA]] >= 0x14) {
play_sound(0x4806U);
func_80151938(globalCtx, 0x937U);
return;
}
func_8019F208();
Actor_PickUp((Actor *) this, globalCtx, 0x35, 90.0f, 10.0f);
func_801159EC(-0xA);
this->actionFunc = func_809529AC;
return;
break;
case 1:
default:
func_8019F230();
func_80151938(globalCtx, 0x934U);
break;
}
There's a couple of other obvious things here:
- the last
returnincase 0is unnecessary since there is no other code after the switch, so breaking is equivalent to the return` - a common pattern everywhere, a sequence of ifs with returns as the last thing inside is the same as an if-else chain, so we can rewrite these as
switch (globalCtx->msgCtx.choiceIndex) {
case 0:
func_801477B4(globalCtx);
if (gSaveContext.rupees < 0xA) {
play_sound(0x4806U);
func_80151938(globalCtx, 0x935U);
} else if (gSaveContext.inventory.ammo[gItemSlots[0xA]] >= 0x14) {
play_sound(0x4806U);
func_80151938(globalCtx, 0x937U);
} else {
func_8019F208();
Actor_PickUp((Actor *) this, globalCtx, 0x35, 90.0f, 10.0f);
func_801159EC(-0xA);
this->actionFunc = func_809529AC;
}
break;
case 1:
default:
func_8019F230();
func_80151938(globalCtx, 0x934U);
break;
}
Well, at least the bottom half looks respectable now. Again, there is no code after the switch, so the next thing up, namely
if (func_80147624(globalCtx) == 0) {
return;
}
can be swapped round and made to wrap the switch. This leaves us with
void func_809527F8(EnMs *this, GlobalContext *globalCtx) {
u8 temp_v0;
temp_v0 = Message_GetState(&globalCtx->msgCtx);
if (temp_v0 == 4) {
goto block_7;
}
if (temp_v0 == 5) {
goto block_5;
}
if (temp_v0 != 6) {
return;
}
if (func_80147624(globalCtx) == 0) {
return;
}
this->actionFunc = func_80952734;
return;
block_5:
if (func_80147624(globalCtx) == 0) {
return;
}
func_801477B4(globalCtx);
Actor_PickUp((Actor *) this, globalCtx, 0x35, this->actor.xzDistToPlayer, this->actor.playerHeightRel);
this->actionFunc = func_809529AC;
return;
block_7:
if (func_80147624(globalCtx) != 0) {
switch (globalCtx->msgCtx.choiceIndex) {
case 0:
func_801477B4(globalCtx);
if (gSaveContext.rupees < 0xA) {
play_sound(0x4806U);
func_80151938(globalCtx, 0x935U);
} else if (gSaveContext.inventory.ammo[gItemSlots[0xA]] >= 0x14) {
play_sound(0x4806U);
func_80151938(globalCtx, 0x937U);
} else {
func_8019F208();
Actor_PickUp((Actor *) this, globalCtx, 0x35, 90.0f, 10.0f);
func_801159EC(-0xA);
this->actionFunc = func_809529AC;
}
break;
case 1:
default:
func_8019F230();
func_80151938(globalCtx, 0x934U);
break;
}
}
}
Now, the top of the function also looks like a switch:
temp_v0 = Message_GetState(&globalCtx->msgCtx);
if (temp_v0 == 4) {
goto block_7;
}
if (temp_v0 == 5) {
goto block_5;
}
if (temp_v0 != 6) {
return;
}
Interestingly, this time the blocks are the other way round. Also, the last statement is a != rather than an ==: this should be the default this time. The code order takes priority over the check order, because the compiler likes to put those in numerical order. There will be cases 4,5,6, but in the order 6,5,4, because that's how the code ordering goes. Also, notice that every case returns at the end: this means there's nothing else in the function after this switch, so everything after block_7 is actually part of case 4.
Putting all this together, we write down a function with no gotos in it:
void func_809527F8(EnMs *this, GlobalContext *globalCtx) {
switch (Message_GetState(&globalCtx->msgCtx)) {
case 6:
this->actionFunc = func_80952734;
break;
case 5:
if (func_80147624(globalCtx) == 0) {
return;
}
func_801477B4(globalCtx);
Actor_PickUp((Actor *) this, globalCtx, 0x35, this->actor.xzDistToPlayer, this->actor.playerHeightRel);
this->actionFunc = func_809529AC;
break;
case 4:
if (func_80147624(globalCtx) != 0) {
switch (globalCtx->msgCtx.choiceIndex) {
case 0:
func_801477B4(globalCtx);
if (gSaveContext.rupees < 0xA) {
play_sound(0x4806U);
func_80151938(globalCtx, 0x935U);
} else if (gSaveContext.inventory.ammo[gItemSlots[0xA]] >= 0x14) {
play_sound(0x4806U);
func_80151938(globalCtx, 0x937U);
} else {
func_8019F208();
Actor_PickUp((Actor *) this, globalCtx, 0x35, 90.0f, 10.0f);
func_801159EC(-0xA);
this->actionFunc = func_809529AC;
}
break;
case 1:
default:
func_8019F230();
func_80151938(globalCtx, 0x934U);
break;
}
}
break;
default:
break;
}
}
Lastly, we can simplify case 5 to replace the return in the if by the rest of the code, and we end up with
void func_809527F8(EnMs *this, GlobalContext *globalCtx) {
switch (Message_GetState(&globalCtx->msgCtx)) {
case 6:
this->actionFunc = func_80952734;
break;
case 5:
if (func_80147624(globalCtx) != 0) {
func_801477B4(globalCtx);
Actor_PickUp((Actor *) this, globalCtx, 0x35, this->actor.xzDistToPlayer, this->actor.playerHeightRel);
this->actionFunc = func_809529AC;
}
break;
case 4:
if (func_80147624(globalCtx) != 0) {
switch (globalCtx->msgCtx.choiceIndex) {
case 0:
func_801477B4(globalCtx);
if (gSaveContext.rupees < 0xA) {
play_sound(0x4806U);
func_80151938(globalCtx, 0x935U);
} else if (gSaveContext.inventory.ammo[gItemSlots[0xA]] >= 0x14) {
play_sound(0x4806U);
func_80151938(globalCtx, 0x937U);
} else {
func_8019F208();
Actor_PickUp((Actor *) this, globalCtx, 0x35, 90.0f, 10.0f);
func_801159EC(-0xA);
this->actionFunc = func_809529AC;
}
break;
case 1:
default:
func_8019F230();
func_80151938(globalCtx, 0x934U);
break;
}
}
break;
default:
break;
}
}
And this matches!
We will not document this now, although even with so few function named it seems pretty clear that it's to do with buying beans (and indeed, Magic Beans cost 10 Rupees and have Get Item ID 0x35) You might like to try to match this function without using goto-only mode, to compare. It is also an interesting exercise to see what each elimination does to the diff: sometimes it will stray surprisingly far for a small change.