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
|
# 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
<details>
<summary>
Large code block, click to show.
</summary>
```C
#include "z_en_ms.h"
#define FLAGS 0x00000009
#define THIS ((EnMs*)thisx)
void EnMs_Init(Actor* thisx, PlayState* play);
void EnMs_Destroy(Actor* thisx, PlayState* play);
void EnMs_Update(Actor* thisx, PlayState* play);
void EnMs_Draw(Actor* thisx, PlayState* play);
void func_80952734(EnMs* this, PlayState* play);
void func_809527F8(EnMs* this, PlayState* play);
void func_809529AC(EnMs* this, PlayState* play);
void func_80952A1C(EnMs* this, PlayState* play);
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, PlayState* play) {
EnMs* this = THIS;
Actor_ProcessInitChain(thisx, D_80952BCC);
SkelAnime_InitFlex(play, &this->skelAnime, &D_06003DC0, &D_060005EC, this->jointTable, this->morphTable, 9);
Collider_InitCylinder(play, &this->collider);
Collider_SetCylinderType1(play, &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.speed = 0.0f;
this->actor.velocity.y = 0.0f;
this->actor.gravity = -1.0f;
}
void EnMs_Destroy(Actor* thisx, PlayState* play) {
EnMs* this = THIS;
Collider_DestroyCylinder(play, &this->collider);
}
void func_80952734(EnMs* this, PlayState* play) {
s16 temp_v1 = this->actor.yawTowardsPlayer - this->actor.shape.rot.y;
if (gSaveContext.save.inventory.items[10] == ITEM_NONE) {
this->actor.textId = 0x92E;
} else {
this->actor.textId = 0x932;
}
if (Actor_ProcessTalkRequest(&this->actor, &play->state) != 0) {
this->actionFunc = func_809527F8;
return;
}
if (this->actor.xzDistToPlayer < 90.0f) {
if (ABS_ALT(temp_v1) < 0x2000) {
func_800B8614(&this->actor, play, 90.0f);
}
}
}
#pragma GLOBAL_ASM("asm/non_matchings/overlays/ovl_En_Ms/func_809527F8.s")
void func_809529AC(EnMs* this, PlayState* play) {
if (Actor_HasParent(&this->actor, play)) {
this->actor.textId = 0;
func_800B8500(&this->actor, play, this->actor.xzDistToPlayer, this->actor.playerHeightRel, 0);
this->actionFunc = func_80952A1C;
} else {
Actor_OfferGetItem(&this->actor, play, 0x35, this->actor.xzDistToPlayer, this->actor.playerHeightRel);
}
}
void func_80952A1C(EnMs* this, PlayState* play) {
if (Actor_ProcessTalkRequest(&this->actor, &play->state)) {
Message_ContinueTextbox(play, 0x936U);
this->actionFunc = func_809527F8;
} else {
func_800B8500(&this->actor, play, this->actor.xzDistToPlayer, this->actor.playerHeightRel, -1);
}
}
void EnMs_Update(Actor* thisx, PlayState* play) {
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, play);
Collider_UpdateCylinder(&this->actor, &this->collider);
CollisionCheck_SetOC(play, &play->colChkCtx, &this->collider.base);
}
void EnMs_Draw(Actor* thisx, PlayState* play) {
EnMs* this = THIS;
func_8012C28C(play->state.gfxCtx);
SkelAnime_DrawFlexOpa(play, this->skelAnime.skeleton, this->skelAnime.jointTable, this->skelAnime.dListCount, NULL,
NULL, &this->actor);
}
```
</details>
(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
```bash
$ ../mips_to_c/mips_to_c.py asm/non_matchings/overlays/ovl_En_Ms/func_809527F8.s --context ctx.c --gotos-only
```
```C
void func_809527F8(EnMs* this, PlayState* play) {
u8 temp_v0;
u8 temp_v0_2;
temp_v0 = Message_GetState(&play->msgCtx);
if (temp_v0 != 4) {
if (temp_v0 != 5) {
if ((temp_v0 == 6) && (Message_ShouldAdvance(play) != 0)) {
this->actionFunc = func_80952734;
return;
}
// Duplicate return node #17. Try simplifying control flow for better match
return;
}
if (Message_ShouldAdvance(play) != 0) {
Message_CloseTextbox(play);
Actor_OfferGetItem((Actor *) this, play, 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 (Message_ShouldAdvance(play) != 0) {
temp_v0_2 = play->msgCtx.choiceIndex;
if (temp_v0_2 != 0) {
if (temp_v0_2 != 1) {
}
func_8019F230();
Message_ContinueTextbox(play, 0x934U);
// Duplicate return node #17. Try simplifying control flow for better match
return;
}
Message_CloseTextbox(play);
if ((s32) gSaveContext.save.playerData.rupees < 0xA) {
play_sound(0x4806U);
Message_ContinueTextbox(play, 0x935U);
return;
}
if ((s32) gSaveContext.save.inventory.ammo[gItemSlots[0xA]] >= 0x14) {
play_sound(0x4806U);
Message_ContinueTextbox(play, 0x937U);
return;
}
func_8019F208();
Actor_OfferGetItem((Actor *) this, play, 0x35, 90.0f, 10.0f);
Rupees_ChangeBy(-0xA);
this->actionFunc = func_809529AC;
}
}
```
which is long, messy, and contains some rather nasty-looking control flow, including horrors like
```C
temp_v0 = Message_GetState(&play->msgCtx);
if (temp_v0 != 4) {
if (temp_v0 != 5) {
if ((temp_v0 == 6) && (Message_ShouldAdvance(play) != 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
```bash
../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
```C
void func_809527F8(EnMs* this, PlayState* play) {
u8 temp_v0;
u8 temp_v0_2;
temp_v0 = Message_GetState(&play->msgCtx);
if (temp_v0 == 4) {
goto block_7;
}
if (temp_v0 == 5) {
goto block_5;
}
if (temp_v0 != 6) {
goto block_17;
}
if (Message_ShouldAdvance(play) == 0) {
goto block_17;
}
this->actionFunc = func_80952734;
return;
block_5:
if (Message_ShouldAdvance(play) == 0) {
goto block_17;
}
Message_CloseTextbox(play);
Actor_OfferGetItem((Actor *) this, play, 0x35, this->actor.xzDistToPlayer, this->actor.playerHeightRel);
this->actionFunc = func_809529AC;
return;
block_7:
if (Message_ShouldAdvance(play) == 0) {
goto block_17;
}
temp_v0_2 = play->msgCtx.choiceIndex;
if (temp_v0_2 == 0) {
goto block_11;
}
if (temp_v0_2 == 1) {
goto block_16;
}
goto block_16;
block_11:
Message_CloseTextbox(play);
if ((s32) gSaveContext.save.playerData.rupees >= 0xA) {
goto block_13;
}
play_sound(0x4806U);
Message_ContinueTextbox(play, 0x935U);
return;
block_13:
if ((s32) gSaveContext.save.inventory.ammo[gItemSlots[0xA]] < 0x14) {
goto block_15;
}
play_sound(0x4806U);
Message_ContinueTextbox(play, 0x937U);
return;
block_15:
func_8019F208();
Actor_OfferGetItem((Actor *) this, play, 0x35, 90.0f, 10.0f);
Rupees_ChangeBy(-0xA);
this->actionFunc = func_809529AC;
return;
block_16:
func_8019F230();
Message_ContinueTextbox(play, 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
```C
if ((s32) gSaveContext.save.playerData.rupees >= 0xA) {
goto block_13;
}
play_sound(0x4806U);
Message_ContinueTextbox(play, 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:
```C
if (gSaveContext.save.playerData.rupees < 0xA) {
play_sound(0x4806U);
Message_ContinueTextbox(play, 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.
```C
void func_809527F8(EnMs* this, PlayState* play) {
u8 temp_v0;
u8 temp_v0_2;
temp_v0 = Message_GetState(&play->msgCtx);
if (temp_v0 == 4) {
goto block_7;
}
if (temp_v0 == 5) {
goto block_5;
}
if (temp_v0 != 6) {
goto block_17;
}
if (Message_ShouldAdvance(play) == 0) {
goto block_17;
}
this->actionFunc = func_80952734;
return;
block_5:
if (Message_ShouldAdvance(play) == 0) {
goto block_17;
}
Message_CloseTextbox(play);
Actor_OfferGetItem((Actor *) this, play, 0x35, this->actor.xzDistToPlayer, this->actor.playerHeightRel);
this->actionFunc = func_809529AC;
return;
block_7:
if (Message_ShouldAdvance(play) == 0) {
goto block_17;
}
temp_v0_2 = play->msgCtx.choiceIndex;
if (temp_v0_2 == 0) {
goto block_11;
}
if (temp_v0_2 == 1) {
goto block_16;
}
goto block_16;
block_11:
Message_CloseTextbox(play);
if (gSaveContext.save.playerData.rupees < 0xA) {
play_sound(0x4806U);
Message_ContinueTextbox(play, 0x935U);
return;
}
if (gSaveContext.save.inventory.ammo[gItemSlots[0xA]] >= 0x14) {
play_sound(0x4806U);
Message_ContinueTextbox(play, 0x937U);
return;
}
func_8019F208();
Actor_OfferGetItem((Actor *) this, play, 0x35, 90.0f, 10.0f);
Rupees_ChangeBy(-0xA);
this->actionFunc = func_809529AC;
return;
block_16:
func_8019F230();
Message_ContinueTextbox(play, 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.
```C
void func_809527F8(EnMs* this, PlayState* play) {
u8 temp_v0;
u8 temp_v0_2;
temp_v0 = Message_GetState(&play->msgCtx);
if (temp_v0 == 4) {
goto block_7;
}
if (temp_v0 == 5) {
goto block_5;
}
if (temp_v0 != 6) {
return;
}
if (Message_ShouldAdvance(play) == 0) {
return;
}
this->actionFunc = func_80952734;
return;
block_5:
if (Message_ShouldAdvance(play) == 0) {
return;
}
Message_CloseTextbox(play);
Actor_OfferGetItem((Actor *) this, play, 0x35, this->actor.xzDistToPlayer, this->actor.playerHeightRel);
this->actionFunc = func_809529AC;
return;
block_7:
if (Message_ShouldAdvance(play) == 0) {
return;
}
temp_v0_2 = play->msgCtx.choiceIndex;
if (temp_v0_2 == 0) {
goto block_11;
}
if (temp_v0_2 == 1) {
goto block_16;
}
goto block_16;
block_11:
Message_CloseTextbox(play);
if (gSaveContext.save.playerData.rupees < 0xA) {
play_sound(0x4806U);
Message_ContinueTextbox(play, 0x935U);
return;
}
if (gSaveContext.save.inventory.ammo[gItemSlots[0xA]] >= 0x14) {
play_sound(0x4806U);
Message_ContinueTextbox(play, 0x937U);
return;
}
func_8019F208();
Actor_OfferGetItem((Actor *) this, play, 0x35, 90.0f, 10.0f);
Rupees_ChangeBy(-0xA);
this->actionFunc = func_809529AC;
return;
block_16:
func_8019F230();
Message_ContinueTextbox(play, 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
```C
temp_v0_2 = play->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:
```C
switch (play->msgCtx.choiceIndex) {
case 0:
Message_CloseTextbox(play);
if (gSaveContext.save.playerData.rupees < 0xA) {
play_sound(0x4806U);
Message_ContinueTextbox(play, 0x935U);
return;
}
if (gSaveContext.save.inventory.ammo[gItemSlots[0xA]] >= 0x14) {
play_sound(0x4806U);
Message_ContinueTextbox(play, 0x937U);
return;
}
func_8019F208();
Actor_OfferGetItem((Actor *) this, play, 0x35, 90.0f, 10.0f);
Rupees_ChangeBy(-0xA);
this->actionFunc = func_809529AC;
return;
break;
case 1:
default:
func_8019F230();
Message_ContinueTextbox(play, 0x934U);
break;
}
```
There's a couple of other obvious things here:
- the last `return` in `case 0` is 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
```C
switch (play->msgCtx.choiceIndex) {
case 0:
Message_CloseTextbox(play);
if (gSaveContext.save.playerData.rupees < 0xA) {
play_sound(0x4806U);
Message_ContinueTextbox(play, 0x935U);
} else if (gSaveContext.save.inventory.ammo[gItemSlots[0xA]] >= 0x14) {
play_sound(0x4806U);
Message_ContinueTextbox(play, 0x937U);
} else {
func_8019F208();
Actor_OfferGetItem((Actor *) this, play, 0x35, 90.0f, 10.0f);
Rupees_ChangeBy(-0xA);
this->actionFunc = func_809529AC;
}
break;
case 1:
default:
func_8019F230();
Message_ContinueTextbox(play, 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
```C
if (Message_ShouldAdvance(play) == 0) {
return;
}
```
can be swapped round and made to wrap the switch. This leaves us with
```C
void func_809527F8(EnMs* this, PlayState* play) {
u8 temp_v0;
temp_v0 = Message_GetState(&play->msgCtx);
if (temp_v0 == 4) {
goto block_7;
}
if (temp_v0 == 5) {
goto block_5;
}
if (temp_v0 != 6) {
return;
}
if (Message_ShouldAdvance(play) == 0) {
return;
}
this->actionFunc = func_80952734;
return;
block_5:
if (Message_ShouldAdvance(play) == 0) {
return;
}
Message_CloseTextbox(play);
Actor_OfferGetItem((Actor *) this, play, 0x35, this->actor.xzDistToPlayer, this->actor.playerHeightRel);
this->actionFunc = func_809529AC;
return;
block_7:
if (Message_ShouldAdvance(play) != 0) {
switch (play->msgCtx.choiceIndex) {
case 0:
Message_CloseTextbox(play);
if (gSaveContext.save.playerData.rupees < 0xA) {
play_sound(0x4806U);
Message_ContinueTextbox(play, 0x935U);
} else if (gSaveContext.save.inventory.ammo[gItemSlots[0xA]] >= 0x14) {
play_sound(0x4806U);
Message_ContinueTextbox(play, 0x937U);
} else {
func_8019F208();
Actor_OfferGetItem((Actor *) this, play, 0x35, 90.0f, 10.0f);
Rupees_ChangeBy(-0xA);
this->actionFunc = func_809529AC;
}
break;
case 1:
default:
func_8019F230();
Message_ContinueTextbox(play, 0x934U);
break;
}
}
}
```
Now, the top of the function also looks like a switch:
```C
temp_v0 = Message_GetState(&play->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:
```C
void func_809527F8(EnMs* this, PlayState* play) {
switch (Message_GetState(&play->msgCtx)) {
case 6:
this->actionFunc = func_80952734;
break;
case 5:
if (Message_ShouldAdvance(play) == 0) {
return;
}
Message_CloseTextbox(play);
Actor_OfferGetItem((Actor *) this, play, 0x35, this->actor.xzDistToPlayer, this->actor.playerHeightRel);
this->actionFunc = func_809529AC;
break;
case 4:
if (Message_ShouldAdvance(play) != 0) {
switch (play->msgCtx.choiceIndex) {
case 0:
Message_CloseTextbox(play);
if (gSaveContext.save.playerData.rupees < 0xA) {
play_sound(0x4806U);
Message_ContinueTextbox(play, 0x935U);
} else if (gSaveContext.save.inventory.ammo[gItemSlots[0xA]] >= 0x14) {
play_sound(0x4806U);
Message_ContinueTextbox(play, 0x937U);
} else {
func_8019F208();
Actor_OfferGetItem((Actor *) this, play, 0x35, 90.0f, 10.0f);
Rupees_ChangeBy(-0xA);
this->actionFunc = func_809529AC;
}
break;
case 1:
default:
func_8019F230();
Message_ContinueTextbox(play, 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
```C
void func_809527F8(EnMs* this, PlayState* play) {
switch (Message_GetState(&play->msgCtx)) {
case 6:
this->actionFunc = func_80952734;
break;
case 5:
if (Message_ShouldAdvance(play) != 0) {
Message_CloseTextbox(play);
Actor_OfferGetItem((Actor *) this, play, 0x35, this->actor.xzDistToPlayer, this->actor.playerHeightRel);
this->actionFunc = func_809529AC;
}
break;
case 4:
if (Message_ShouldAdvance(play) != 0) {
switch (play->msgCtx.choiceIndex) {
case 0:
Message_CloseTextbox(play);
if (gSaveContext.save.playerData.rupees < 0xA) {
play_sound(0x4806U);
Message_ContinueTextbox(play, 0x935U);
} else if (gSaveContext.save.inventory.ammo[gItemSlots[0xA]] >= 0x14) {
play_sound(0x4806U);
Message_ContinueTextbox(play, 0x937U);
} else {
func_8019F208();
Actor_OfferGetItem((Actor *) this, play, 0x35, 90.0f, 10.0f);
Rupees_ChangeBy(-0xA);
this->actionFunc = func_809529AC;
}
break;
case 1:
default:
func_8019F230();
Message_ContinueTextbox(play, 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.
|