forked from snesrev/zelda3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hud.c
1554 lines (1388 loc) · 53.3 KB
/
hud.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "hud.h"
#include "zelda_rtl.h"
#include "variables.h"
#include "messaging.h"
enum {
kNewStyleInventory = 0,
kHudItemCount = kNewStyleInventory ? 24 : 20,
};
typedef struct ItemBoxGfx {
uint16 v[4];
} ItemBoxGfx;
static void Hud_DrawItem(uint16 *dst, const ItemBoxGfx *src);
static bool Hud_DoWeHaveThisItem(uint8 item);
static void Hud_EquipPrevItem(uint8 *item);
static void Hud_EquipNextItem(uint8 *item);
static int Hud_GetItemPosition(int item);
static void Hud_ReorderItem(int direction);
static void Hud_Update_Magic();
static void Hud_Update_Inventory();
static void Hud_Update_Hearts();
const uint8 kMaxBombsForLevel[] = { 10, 15, 20, 25, 30, 35, 40, 50 };
const uint8 kMaxArrowsForLevel[] = { 30, 35, 40, 45, 50, 55, 60, 70 };
static const uint8 kMaxHealthForLevel[] = { 9, 9, 9, 9, 9, 9, 9, 9, 17, 17, 17, 17, 17, 17, 17, 25, 25, 25, 25, 25, 25 };
#define HUDXY(x, y) ((x) + (y) * 32)
static const uint16 kHudItemInVramPtr_New[24] = {
HUDXY(2, 7), HUDXY(5, 7), HUDXY(8, 7), HUDXY(11, 7), HUDXY(14, 7), HUDXY(17, 7),
HUDXY(2, 10), HUDXY(5, 10), HUDXY(8, 10), HUDXY(11, 10), HUDXY(14, 10), HUDXY(17, 10),
HUDXY(2, 13), HUDXY(5, 13), HUDXY(8, 13), HUDXY(11, 13), HUDXY(14, 13), HUDXY(17, 13),
HUDXY(2, 16), HUDXY(5, 16), HUDXY(8, 16), HUDXY(11, 16), HUDXY(14, 16), HUDXY(17, 16),
};
static const uint16 kHudItemInVramPtr_Old[20] = {
HUDXY(4, 7), HUDXY(7, 7), HUDXY(10, 7), HUDXY(13, 7), HUDXY(16, 7),
HUDXY(4, 10), HUDXY(7, 10), HUDXY(10, 10), HUDXY(13, 10), HUDXY(16, 10),
HUDXY(4, 13), HUDXY(7, 13), HUDXY(10, 13), HUDXY(13, 13), HUDXY(16, 13),
HUDXY(4, 16), HUDXY(7, 16), HUDXY(10, 16), HUDXY(13, 16), HUDXY(16, 16),
};
#define kHudItemInVramPtr (kNewStyleInventory ? kHudItemInVramPtr_New : kHudItemInVramPtr_Old)
static const ItemBoxGfx kHudItemBottles[9] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x2044, 0x2045, 0x2046, 0x2047}},
{{0x2837, 0x2838, 0x2cc3, 0x2cd3}},
{{0x24d2, 0x64d2, 0x24e2, 0x24e3}},
{{0x3cd2, 0x7cd2, 0x3ce2, 0x3ce3}},
{{0x2cd2, 0x6cd2, 0x2ce2, 0x2ce3}},
{{0x2855, 0x6855, 0x2c57, 0x2c5a}},
{{0x2837, 0x2838, 0x2839, 0x283a}},
{{0x2837, 0x2838, 0x2839, 0x283a}},
};
static const ItemBoxGfx kHudItemBow[5] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x28ba, 0x28e9, 0x28e8, 0x28cb}},
{{0x28ba, 0x284a, 0x2849, 0x28cb}},
{{0x28ba, 0x28e9, 0x28e8, 0x28cb}},
{{0x28ba, 0x28bb, 0x24ca, 0x28cb}},
};
static const ItemBoxGfx kHudItemBoomerang[3] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x2cb8, 0x2cb9, 0x2cf5, 0x2cc9}},
{{0x24b8, 0x24b9, 0x24f5, 0x24c9}},
};
static const ItemBoxGfx kHudItemHookshot[2] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x24f5, 0x24f6, 0x24c0, 0x24f5}},
};
static const ItemBoxGfx kHudItemBombs[2] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x2cb2, 0x2cb3, 0x2cc2, 0x6cc2}},
};
static const ItemBoxGfx kHudItemMushroom[3] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x2444, 0x2445, 0x2446, 0x2447}},
{{0x203b, 0x203c, 0x203d, 0x203e}},
};
static const ItemBoxGfx kHudItemFireRod[2] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x24b0, 0x24b1, 0x24c0, 0x24c1}},
};
static const ItemBoxGfx kHudItemIceRod[2] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x2cb0, 0x2cbe, 0x2cc0, 0x2cc1}},
};
static const ItemBoxGfx kHudItemBombos[2] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x287d, 0x287e, 0xe87e, 0xe87d}},
};
static const ItemBoxGfx kHudItemEther[2] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x2876, 0x2877, 0xE877, 0xE876}},
};
static const ItemBoxGfx kHudItemQuake[2] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x2866, 0x2867, 0xE867, 0xE866}},
};
static const ItemBoxGfx kHudItemTorch[2] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x24bc, 0x24bd, 0x24cc, 0x24cd}},
};
static const ItemBoxGfx kHudItemHammer[2] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x20b6, 0x20b7, 0x20c6, 0x20c7}},
};
static const ItemBoxGfx kHudItemFlute[4] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x20d0, 0x20d1, 0x20e0, 0x20e1}},
{{0x2cd4, 0x2cd5, 0x2ce4, 0x2ce5}},
{{0x2cd4, 0x2cd5, 0x2ce4, 0x2ce5}},
};
static const ItemBoxGfx kHudItemBugNet[2] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x3c40, 0x3c41, 0x2842, 0x3c43}},
};
static const ItemBoxGfx kHudItemBookMudora[2] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x3ca5, 0x3ca6, 0x3cd8, 0x3cd9}},
};
static const ItemBoxGfx kHudItemCaneSomaria[2] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x24dc, 0x24dd, 0x24ec, 0x24ed}},
};
static const ItemBoxGfx kHudItemCaneByrna[2] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x2cdc, 0x2cdd, 0x2cec, 0x2ced}},
};
static const ItemBoxGfx kHudItemCape[2] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x24b4, 0x24b5, 0x24c4, 0x24c5}},
};
static const ItemBoxGfx kHudItemMirror[4] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x28de, 0x28df, 0x28ee, 0x28ef}},
{{0x2c62, 0x2c63, 0x2c72, 0x2c73}},
{{0x2886, 0x2887, 0x2888, 0x2889}},
};
static const ItemBoxGfx kHudItemGloves[3] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x2130, 0x2131, 0x2140, 0x2141}},
{{0x28da, 0x28db, 0x28ea, 0x28eb}},
};
static const ItemBoxGfx kHudItemBoots[2] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x3429, 0x342a, 0x342b, 0x342c}},
};
static const ItemBoxGfx kHudItemFlippers[2] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x2c9a, 0x2c9b, 0x2c9d, 0x2c9e}},
};
static const ItemBoxGfx kHudItemMoonPearl[2] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x2433, 0x2434, 0x2435, 0x2436}},
};
static const ItemBoxGfx kHudItemEmpty[1] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
};
static const ItemBoxGfx kHudItemSword[5] = {
{{0x20f5, 0x20f5, 0x20f5, 0x20f5}},
{{0x2c64, 0x2cce, 0x2c75, 0x3d25}},
{{0x2c8a, 0x2c65, 0x2474, 0x3d26}},
{{0x248a, 0x2465, 0x3c74, 0x2d48}},
{{0x288a, 0x2865, 0x2c74, 0x2d39}},
};
static const ItemBoxGfx kHudItemShield[4] = {
{{0x24f5, 0x24f5, 0x24f5, 0x24f5}},
{{0x2cfd, 0x6cfd, 0x2cfe, 0x6cfe}},
{{0x34ff, 0x74ff, 0x349f, 0x749f}},
{{0x2880, 0x2881, 0x288d, 0x288e}},
};
static const ItemBoxGfx kHudItemArmor[5] = {
{{0x3c68, 0x7c68, 0x3c78, 0x7c78}},
{{0x2c68, 0x6c68, 0x2c78, 0x6c78}},
{{0x2468, 0x6468, 0x2478, 0x6478}},
};
static const ItemBoxGfx * const kHudItemBoxGfxPtrs[] = {
kHudItemBow,
kHudItemBoomerang,
kHudItemHookshot,
kHudItemBombs,
kHudItemMushroom,
kHudItemFireRod,
kHudItemIceRod,
kHudItemBombos,
kHudItemEther,
kHudItemQuake,
kHudItemTorch,
kHudItemHammer,
kHudItemFlute,
kHudItemBugNet,
kHudItemBookMudora,
kHudItemBottles,
kHudItemCaneSomaria,
kHudItemCaneByrna,
kHudItemCape,
kHudItemMirror,
kHudItemGloves,
kHudItemBoots,
kHudItemFlippers,
kHudItemMoonPearl,
kHudItemEmpty,
kHudItemSword,
kHudItemShield,
kHudItemArmor,
kHudItemBottles,
kHudItemBottles,
kHudItemBottles,
kHudItemBottles,
};
static const uint16 kUpdateMagicPowerTilemap[17][4] = {
{0x3cf5, 0x3cf5, 0x3cf5, 0x3cf5},
{0x3cf5, 0x3cf5, 0x3cf5, 0x3c5f},
{0x3cf5, 0x3cf5, 0x3cf5, 0x3c4c},
{0x3cf5, 0x3cf5, 0x3cf5, 0x3c4d},
{0x3cf5, 0x3cf5, 0x3cf5, 0x3c4e},
{0x3cf5, 0x3cf5, 0x3c5f, 0x3c5e},
{0x3cf5, 0x3cf5, 0x3c4c, 0x3c5e},
{0x3cf5, 0x3cf5, 0x3c4d, 0x3c5e},
{0x3cf5, 0x3cf5, 0x3c4e, 0x3c5e},
{0x3cf5, 0x3c5f, 0x3c5e, 0x3c5e},
{0x3cf5, 0x3c4c, 0x3c5e, 0x3c5e},
{0x3cf5, 0x3c4d, 0x3c5e, 0x3c5e},
{0x3cf5, 0x3c4e, 0x3c5e, 0x3c5e},
{0x3c5f, 0x3c5e, 0x3c5e, 0x3c5e},
{0x3c4c, 0x3c5e, 0x3c5e, 0x3c5e},
{0x3c4d, 0x3c5e, 0x3c5e, 0x3c5e},
{0x3c4e, 0x3c5e, 0x3c5e, 0x3c5e},
};
static const uint16 kDungFloorIndicator_Gfx0[11] = { 0x2508, 0x2509, 0x2509, 0x250a, 0x250b, 0x250c, 0x250d, 0x251d, 0xe51c, 0x250e, 0x7f };
static const uint16 kDungFloorIndicator_Gfx1[11] = { 0x2518, 0x2519, 0xa509, 0x251a, 0x251b, 0x251c, 0x2518, 0xa51d, 0xe50c, 0xa50e, 0x7f };
void Hud_RefreshIcon() {
Hud_SearchForEquippedItem();
Hud_UpdateHud();
Hud_Rebuild();
overworld_map_state = 0;
}
uint8 CheckPalaceItemPosession() {
switch (cur_palace_index_x2 >> 1) {
case 2: return link_item_bow != 0;
case 3: return link_item_gloves != 0;
case 5: return link_item_hookshot != 0;
case 6: return link_item_hammer != 0;
case 7: return link_item_cane_somaria != 0;
case 8: return link_item_fire_rod != 0;
case 9: return link_armor != 0;
case 10: return link_item_moon_pearl != 0;
case 11: return link_item_gloves != 1;
case 12: return link_shield_type == 3;
case 13: return link_armor == 2;
default:
return 0;
}
}
// Returns the zero based index of the currently selected hud item,
// or -1 if the item is item 0.
static int Hud_GetItemPosition(int item) {
if (item <= 0)
return -1;
if (hud_inventory_order[0] != 0) {
int i = 0;
for (; i < kHudItemCount - 1 && hud_inventory_order[i] != item; i++) {}
return i;
} else {
return item ? item - 1 : item;
}
}
static void Hud_GotoPrevItem(uint8 *item, uint8 first_item_index) {
if (hud_inventory_order[0] != 0) {
int pos = Hud_GetItemPosition(*item);
*item = (pos == 0 && !first_item_index) ? 0 :
hud_inventory_order[(pos <= 0 ? kHudItemCount : pos) - 1];
} else {
*item = (*item > first_item_index) ? *item - 1 : kHudItemCount;
}
}
static void Hud_GotoNextItem(uint8 *item, uint8 first_item_index) {
if (hud_inventory_order[0] != 0) {
int i = Hud_GetItemPosition(*item);
*item = hud_inventory_order[((uint)i >= kHudItemCount - 1) ? 0 : i + 1];
} else {
*item = (*item < kHudItemCount) ? *item + 1 : first_item_index;
}
}
void Hud_FloorIndicator() { // 8afd0c
uint16 a = hud_floor_changed_timer;
if (a == 0) {
Hud_RemoveSuperBombIndicator();
return;
}
a += 1;
if (a == 0xc0)
a = 0;
WORD(hud_floor_changed_timer) = a;
hud_tile_indices_buffer[0xf2 / 2] = 0x251e;
hud_tile_indices_buffer[0x134 / 2] = 0x251f;
hud_tile_indices_buffer[0x132 / 2] = 0x2520;
hud_tile_indices_buffer[0xf4 / 2] = 0x250f;
int k = 0, j;
if (!sign8(dung_cur_floor)) {
if (!WORD(dung_cur_floor) && dungeon_room_index != 2 && sram_progress_indicator < 2)
sound_effect_ambient = 3;
j = dung_cur_floor;
} else {
sound_effect_ambient = 5;
k++;
j = dung_cur_floor ^ 0xff;
}
hud_tile_indices_buffer[k + 0xf2 / 2] = kDungFloorIndicator_Gfx0[j];
hud_tile_indices_buffer[k + 0x132 / 2] = kDungFloorIndicator_Gfx1[j];
flag_update_hud_in_nmi++;
}
void Hud_RemoveSuperBombIndicator() { // 8afd90
hud_tile_indices_buffer[0xf2 / 2] = 0x7f;
hud_tile_indices_buffer[0x132 / 2] = 0x7f;
hud_tile_indices_buffer[0xf4 / 2] = 0x7f;
hud_tile_indices_buffer[0x134 / 2] = 0x7f;
}
void Hud_SuperBombIndicator() { // 8afda8
if (!super_bomb_indicator_unk1) {
if (sign8(super_bomb_indicator_unk2))
goto remove;
super_bomb_indicator_unk2--;
super_bomb_indicator_unk1 = 62;
}
super_bomb_indicator_unk1--;
if (sign8(super_bomb_indicator_unk2)) {
remove:
super_bomb_indicator_unk2 = 0xff;
Hud_RemoveSuperBombIndicator();
return;
}
int r = super_bomb_indicator_unk2 % 10;
int q = super_bomb_indicator_unk2 / 10;
int j = sign8(r - 1) ? 9 : r - 1;
hud_tile_indices_buffer[0xf4 / 2] = kDungFloorIndicator_Gfx0[j];
hud_tile_indices_buffer[0x134 / 2] = kDungFloorIndicator_Gfx1[j];
j = sign8(q - 1) ? 10 : q - 1;
hud_tile_indices_buffer[0xf2 / 2] = kDungFloorIndicator_Gfx0[j];
hud_tile_indices_buffer[0x132 / 2] = kDungFloorIndicator_Gfx1[j];
}
static int MaxRupees() {
return enhanced_features0 & kFeatures0_CarryMoreRupees ? 9999 : 999;
}
void Hud_RefillLogic() { // 8ddb92
if (overworld_map_state)
return;
if (link_magic_filler) {
if (link_magic_power >= 128) {
link_magic_power = 128;
link_magic_filler = 0;
} else {
link_magic_filler--;
link_magic_power++;
if ((frame_counter & 3) == 0 && sound_effect_1 == 0)
sound_effect_1 = 45;
}
}
uint16 a = link_rupees_actual;
if (a != link_rupees_goal) {
if (a >= link_rupees_goal) {
if ((int16)--a < 0)
link_rupees_goal = a = 0;
} else {
int m = MaxRupees();
if (++a > m)
link_rupees_goal = a = m;
}
link_rupees_actual = a;
if (sound_effect_1 == 0) {
if ((rupee_sfx_sound_delay++ & 7) == 0)
sound_effect_1 = 41;
} else {
rupee_sfx_sound_delay = 0;
}
} else {
rupee_sfx_sound_delay = 0;
}
if (link_bomb_filler) {
link_bomb_filler--;
if (link_item_bombs != kMaxBombsForLevel[link_bomb_upgrades])
link_item_bombs++;
}
if (link_arrow_filler) {
link_arrow_filler--;
if (link_num_arrows != kMaxArrowsForLevel[link_arrow_upgrades])
link_num_arrows++;
if (link_item_bow && (link_item_bow & 1) == 1) {
link_item_bow++;
Hud_RefreshIcon();
}
}
if (!flag_is_link_immobilized && !link_hearts_filler &&
link_health_current < kMaxHealthForLevel[link_health_capacity >> 3]) {
if (link_lowlife_countdown_timer_beep) {
link_lowlife_countdown_timer_beep--;
} else if (!sound_effect_1) {
if (!(enhanced_features0 & kFeatures0_DisableLowHealthBeep))
sound_effect_1 = 43;
link_lowlife_countdown_timer_beep = 32 - 1;
}
}
if (is_doing_heart_animation)
goto doing_animation;
if (link_hearts_filler) {
if (link_health_current < link_health_capacity) {
link_health_current += 8;
if (link_health_current >= link_health_capacity)
link_health_current = link_health_capacity;
if (sound_effect_2 == 0)
sound_effect_2 = 13;
link_hearts_filler -= 8;
is_doing_heart_animation++;
animate_heart_refill_countdown = 7;
doing_animation:
Hud_Update_Magic();
Hud_Update_Inventory();
Hud_AnimateHeartRefill();
flag_update_hud_in_nmi++;
return;
}
link_health_current = link_health_capacity;
link_hearts_filler = 0;
}
Hud_Update_Hearts();
Hud_Update_Magic();
Hud_Update_Inventory();
flag_update_hud_in_nmi++;
}
void Hud_Module_Run() { // 8ddd36
byte_7E0206++;
switch (overworld_map_state) {
case 0: Hud_ClearTileMap(); break;
case 1: Hud_Init(); break;
case 2: Hud_BringMenuDown(); break;
case 3: Hud_ChooseNextMode(); break;
case 4: Hud_NormalMenu(); break;
case 5: Hud_UpdateHud(); break;
case 6: Hud_CloseMenu(); break;
case 7: Hud_GotoBottleMenu(); break;
case 8: Hud_InitBottleMenu(); break;
case 9: Hud_ExpandBottleMenu(); break;
case 10: Hud_BottleMenu(); break;
case 11: Hud_EraseBottleMenu(); break;
case 12: Hud_RestoreNormalMenu(); break;
default:
assert(0);
}
}
void Hud_ClearTileMap() { // 8ddd5a
uint16 *target = (uint16 *)&g_ram[0x1000];
for (int i = 0; i < 1024; i++)
target[i] = 0x207f;
sound_effect_2 = 17;
nmi_subroutine_index = 1;
BYTE(nmi_load_target_addr) = 0x22;
overworld_map_state++;
}
bool Hud_HaveAnyItems() { // new
for (int i = 0; i < 20; i++)
if ((&link_item_bow)[i])
return true;
return false;
}
void Hud_Init() { // 8dddab
Hud_SearchForEquippedItem();
Hud_DrawYButtonItems();
Hud_DrawAbilityBox();
Hud_DrawProgressIcons();
Hud_DrawEquipmentBox();
Hud_DrawSelectedYButtonItem();
if (Hud_HaveAnyItems()) {
// This causes bottle flicker because it's not early enough
int first_bottle = 0;
while (first_bottle < 4 && link_bottle_info[first_bottle] == 0)
first_bottle++;
if (first_bottle == 4)
link_item_bottle_index = 0;
else if (link_item_bottle_index == 0)
link_item_bottle_index = first_bottle + 1;
if (hud_cur_item == kHudItem_BottleOld && !kNewStyleInventory) {
timer_for_flashing_circle = 16;
Hud_DrawBottleMenu();
}
}
timer_for_flashing_circle = 16;
nmi_subroutine_index = 1;
BYTE(nmi_load_target_addr) = 0x22;
overworld_map_state++;
}
void Hud_BringMenuDown() { // 8dde59
BG3VOFS_copy2 -= 8;
if (BG3VOFS_copy2 == 0xff18)
overworld_map_state++;
}
void Hud_ChooseNextMode() { // 8dde6e
if (Hud_HaveAnyItems()) {
nmi_subroutine_index = 1;
BYTE(nmi_load_target_addr) = 0x22;
Hud_DrawSelectedYButtonItem();
// Pick either the bottle state or normal one
overworld_map_state = (hud_cur_item == kHudItem_BottleOld && !kNewStyleInventory) ? 10 : 4;
} else {
if (filtered_joypad_H)
overworld_map_state = 5;
}
}
static bool Hud_DoWeHaveThisItem(uint8 item) { // 8ddeb0
if (item == 0)
return true; // for the x item, 0 is valid
if (item == kHudItem_Flute && kNewStyleInventory)
return link_item_flute >= 2;
if (item == kHudItem_Shovel && kNewStyleInventory)
return link_item_flute >= 1;
if (item >= kHudItem_Bottle1)
return link_bottle_info[item - kHudItem_Bottle1] != 0;
return (&link_item_bow)[item - 1] != 0;
}
static void Hud_EquipPrevItem(uint8 *item) { // 8dded9
do {
Hud_GotoPrevItem(item, item == &hud_cur_item);
} while (!Hud_DoWeHaveThisItem(*item));
}
static void Hud_EquipNextItem(uint8 *item) { // 8ddee2
do {
Hud_GotoNextItem(item, item == &hud_cur_item);
} while (!Hud_DoWeHaveThisItem(*item));
}
static void Hud_EquipItemAbove(uint8 *item) { // 8ddeeb
do {
for(int i = 0; i < (kNewStyleInventory ? 6 : 5); i++)
Hud_GotoPrevItem(item, 1);
} while (!Hud_DoWeHaveThisItem(*item));
}
static void Hud_EquipItemBelow(uint8 *item) { // 8ddf00
int num = *item == 0 ? 1 : (kNewStyleInventory ? 6 : 5);
do {
for (int i = 0; i < num; i++)
Hud_GotoNextItem(item, 1);
} while (!Hud_DoWeHaveThisItem(*item));
}
int GetCurrentItemButtonIndex() {
if (enhanced_features0 & kFeatures0_SwitchLR) {
return (joypad1L_last & kJoypadL_X) ? 1 :
(joypad1L_last & kJoypadL_L) ? 2 :
(joypad1L_last & kJoypadL_R) ? 3 : 0;
}
return 0;
}
uint8 *GetCurrentItemButtonPtr(int i) {
return (i == 0) ? &hud_cur_item :
(i == 1) ? &hud_cur_item_x :
(i == 2) ? &hud_cur_item_l : &hud_cur_item_r;
}
void Hud_NormalMenu() { // 8ddf15
timer_for_flashing_circle++;
if (!BYTE(joypad1H_last))
BYTE(hud_tmp1) = 0;
if (filtered_joypad_H & kJoypadH_Start) {
overworld_map_state = 5;
sound_effect_2 = 18;
return;
}
// Allow select to open the save/exit thing
if (joypad1H_last & kJoypadH_Select && sram_progress_indicator) {
BG3VOFS_copy2 = -8;
Hud_CloseMenu();
DisplaySelectMenu();
return;
}
if (joypad1H_last & kJoypadH_Y && !(joypad1L_last & kJoypadL_X) && (enhanced_features0 & kFeatures0_SwitchLR)) {
if (filtered_joypad_H & kJoypadH_Up) {
Hud_ReorderItem(kNewStyleInventory ? -6 : -5);
} else if (filtered_joypad_H & kJoypadH_Down) {
Hud_ReorderItem(kNewStyleInventory ? 6 : 5);
} else if (filtered_joypad_H & kJoypadH_Left) {
Hud_ReorderItem(-1);
} else if (filtered_joypad_H & kJoypadH_Right) {
Hud_ReorderItem(1);
}
} else if (!BYTE(hud_tmp1)) {
// If Special Key button is down, then move their circle
int btn_index = GetCurrentItemButtonIndex();
uint8 *item_p = GetCurrentItemButtonPtr(btn_index);
uint16 old_item = *item_p;
if (filtered_joypad_H & kJoypadH_Up) {
Hud_EquipItemAbove(item_p);
} else if (filtered_joypad_H & kJoypadH_Down) {
Hud_EquipItemBelow(item_p);
} else if (filtered_joypad_H & kJoypadH_Left) {
Hud_EquipPrevItem(item_p);
} else if (filtered_joypad_H & kJoypadH_Right) {
Hud_EquipNextItem(item_p);
}
BYTE(hud_tmp1) = filtered_joypad_H;
if (*item_p != old_item) {
timer_for_flashing_circle = 16;
sound_effect_2 = 32;
}
}
Hud_DrawYButtonItems();
Hud_DrawSelectedYButtonItem();
if (hud_cur_item == kHudItem_BottleOld && !kNewStyleInventory)
overworld_map_state = 7;
nmi_subroutine_index = 1;
BYTE(nmi_load_target_addr) = 0x22;
//g_ram[0x15d0] = 0;
}
void Hud_UpdateHud() { // 8ddfa9
overworld_map_state++;
Hud_Rebuild();
Hud_UpdateEquippedItem();
}
uint8 Hud_LookupInventoryItem(uint8 item) {
static const uint8 kHudItemToItemOrg[21] = {
0,
3, 2, 14, 1, 10, 5,
6, 15, 16, 17, 9, 4,
8, 7, 12, 11, 18, 13,
19, 20,
};
static const uint8 kHudItemToItemNew[25] = {
0,
3, 2, 14, 1, 10, 5,
6, 15, 16, 17, 9, 4,
8, 7, 12, 21, 18, 13, // 8 is ocarina / shovel combined. moved shovel to 21.
19, 20,11, 11, 11, 11, // 11 means bottle
};
return kNewStyleInventory ? kHudItemToItemNew[item] : kHudItemToItemOrg[item];
}
void Hud_UpdateEquippedItem() { // 8ddfaf
if (hud_cur_item >= kHudItem_Bottle1)
link_item_bottle_index = hud_cur_item - kHudItem_Bottle1 + 1;
assert(hud_cur_item < 25);
current_item_y = Hud_LookupInventoryItem(hud_cur_item);
}
void Hud_CloseMenu() { // 8ddfba
BG3VOFS_copy2 += 8;
if (BG3VOFS_copy2)
return;
Hud_Rebuild();
overworld_map_state = 0;
submodule_index = 0;
main_module_index = saved_module_for_menu;
if (submodule_index)
Hud_RestoreTorchBackground();
if (current_item_y != 5 && current_item_y != 6) {
eq_debug_variable = 2;
link_debug_value_1 = 0;
} else {
assert(!link_debug_value_1);
eq_debug_variable = 0;
}
}
void Hud_GotoBottleMenu() { // 8ddffb
bottle_menu_expand_row = 0;
overworld_map_state++;
}
void Hud_InitBottleMenu() { // 8de002
int r = bottle_menu_expand_row;
uint16 *dst = uvram_screen.row[0].col + (kNewStyleInventory ? 1 : 0);
for (int i = 21; i <= 30; i++)
dst[HUDXY(i, 11 + r)] = 0x207f;
if (++bottle_menu_expand_row == 19) {
overworld_map_state++;
bottle_menu_expand_row = 17;
}
nmi_subroutine_index = 1;
BYTE(nmi_load_target_addr) = 0x22;
}
void Hud_ExpandBottleMenu() { // 8de08c
static const uint16 kBottleMenuTop[] = { 0x28FB, 0x28F9, 0x28F9, 0x28F9, 0x28F9, 0x28F9, 0x28F9, 0x28F9, 0x28F9, 0x68FB };
static const uint16 kBottleMenuTop2[] = { 0x28FC, 0x24F5, 0x24F5, 0x24F5, 0x24F5, 0x24F5, 0x24F5, 0x24F5, 0x24F5, 0x68FC };
static const uint16 kBottleMenuBottom[] = { 0xA8FB, 0xA8F9, 0xA8F9, 0xA8F9, 0xA8F9, 0xA8F9, 0xA8F9, 0xA8F9, 0xA8F9, 0xE8FB };
int r = bottle_menu_expand_row;
uint16 *dst = uvram_screen.row[0].col + (kNewStyleInventory ? 1 : 0);
memcpy(&dst[HUDXY(21, 11 + r)], kBottleMenuTop, sizeof(uint16) * 10);
memcpy(&dst[HUDXY(21, 12 + r)], kBottleMenuTop2, sizeof(uint16) * 10);
memcpy(&dst[HUDXY(21, 29)], kBottleMenuBottom, sizeof(uint16) * 10);
if (sign8(--bottle_menu_expand_row))
overworld_map_state++;
nmi_subroutine_index = 1;
BYTE(nmi_load_target_addr) = 0x22;
}
void Hud_BottleMenu() { // 8de0df
timer_for_flashing_circle++;
if (filtered_joypad_H & kJoypadH_Start) {
sound_effect_2 = 18;
overworld_map_state = 5;
} else if (filtered_joypad_H & (kJoypadH_Left | kJoypadH_Right)) {
if (filtered_joypad_H & kJoypadH_Left) {
Hud_EquipPrevItem(&hud_cur_item);
} else {
Hud_EquipNextItem(&hud_cur_item);
}
timer_for_flashing_circle = 16;
sound_effect_2 = 32;
Hud_DrawYButtonItems();
Hud_DrawSelectedYButtonItem();
overworld_map_state++;
bottle_menu_expand_row = 0;
return;
}
Hud_DrawBottleMenu_Update();
if (filtered_joypad_H & (kJoypadH_Down | kJoypadH_Up)) {
uint8 old_val = link_item_bottle_index - 1, val = old_val;
if (filtered_joypad_H & kJoypadH_Up) {
do {
val = (val - 1) & 3;
} while (!link_bottle_info[val]);
} else {
do {
val = (val + 1) & 3;
} while (!link_bottle_info[val]);
}
if (old_val != val) {
link_item_bottle_index = val + 1;
timer_for_flashing_circle = 16;
sound_effect_2 = 32;
}
}
}
static void Hud_DrawItem(uint16 *dst, const ItemBoxGfx *src) { // new
dst[0] = src->v[0];
dst[1] = src->v[1];
dst[32] = src->v[2];
dst[33] = src->v[3];
}
static void Hud_DrawNxN(uint16 *dst, const uint16 *src, int w, int h) { // new
for (int y = 0; y < h; y++) {
memcpy(dst, src, sizeof(uint16) * w);
dst += 32, src += w;
}
}
static void Hud_Copy2x2(uint16 *dst, uint16 *src) { // new
dst[0] = src[0];
dst[1] = src[1];
dst[32] = src[32];
dst[33] = src[33];
}
static void Hud_DrawBox(uint16 *dst, int x1, int y1, int x2, int y2, uint8 palette) { // new
uint16 t;
t = 0x20fb | palette << 10;
dst[HUDXY(x1, y1)] = t;
dst[HUDXY(x2, y1)] = t + 0x4000;
dst[HUDXY(x1, y2)] = t + 0x8000;
dst[HUDXY(x2, y2)] = t + 0xc000;
t = 0x20fc | palette << 10;
for (int y = y1 + 1; y < y2; y++) {
dst[HUDXY(x1, y)] = t;
dst[HUDXY(x2, y)] = t + 0x4000;
}
t = 0x20f9 | palette << 10;
for (int x = x1 + 1; x < x2; x++) {
dst[HUDXY(x, y1)] = t;
dst[HUDXY(x, y2)] = t + 0x8000;
}
for (int y = y1 + 1; y < y2; y++) {
for (int x = x1 + 1; x < x2; x++)
dst[HUDXY(x, y)] = 0x24F5;
}
}
static void Hud_DrawFlashingCircle(uint16 *p, uint8 palette) { // new
int pp = palette << 10;
p[HUDXY(0, -1)] = pp | 0x2061;
p[HUDXY(1, -1)] = pp | 0x2061 | 0x4000;
p[HUDXY(-1, 0)] = pp | 0x2070;
p[HUDXY(2, 0)] = pp | 0x2070 | 0x4000;
p[HUDXY(-1, 1)] = pp | 0xa070;
p[HUDXY(2, 1)] = pp | 0xa070 | 0x4000;
p[HUDXY(0, 2)] = pp | 0xa061;
p[HUDXY(1, 2)] = pp | 0xa061 | 0x4000;
p[HUDXY(-1, -1)] = pp | 0x2060;
p[HUDXY(2, -1)] = pp | 0x2060 | 0x4000;
p[HUDXY(2, 2)] = pp | 0x2060 | 0xC000;
p[HUDXY(-1, 2)] = pp | 0x2060 | 0x8000;
}
void Hud_DrawBottleMenu() { // 8def67
uint16 *dst = uvram_screen.row[0].col + (kNewStyleInventory ? 1 : 0);
Hud_DrawBox(dst, 21, 11, 30, 29, 2);
for (int i = 0; i < 4; i++)
Hud_DrawItem(dst + HUDXY(25, 13 + i * 4), &kHudItemBottles[link_bottle_info[i]]);
const ItemBoxGfx *p = &kHudItemBottles[link_bottle_info[link_item_bottle_index - 1]];
Hud_DrawItem(uvram_screen.row[0].col + kHudItemInVramPtr[15], p);
if (timer_for_flashing_circle & 0x10)
Hud_DrawFlashingCircle(dst + HUDXY(25, 13 + (link_item_bottle_index - 1) * 4), 7);
}
void Hud_DrawBottleMenu_Update() { // 8de17f
Hud_DrawBottleMenu();
Hud_DrawSelectedYButtonItem();
nmi_subroutine_index = 1;
BYTE(nmi_load_target_addr) = 0x22;
}
void Hud_EraseBottleMenu() { // 8de2fd
uint16 *dst = uvram_screen.row[0].col + (kNewStyleInventory ? 1 : 0);
int r = bottle_menu_expand_row;
for (int i = 0; i < 10; i++)
dst[HUDXY(21 + i, 11 + r)] = 0x207f;
if (++bottle_menu_expand_row == 19)
overworld_map_state++;
nmi_subroutine_index = 1;
BYTE(nmi_load_target_addr) = 0x22;
}
void Hud_RestoreNormalMenu() { // 8de346
Hud_DrawProgressIcons();
Hud_DrawEquipmentBox();
overworld_map_state = 4;
nmi_subroutine_index = 1;
BYTE(nmi_load_target_addr) = 0x22;
}
void Hud_SearchForEquippedItem() { // 8de399
if (!Hud_HaveAnyItems()) {
hud_cur_item = 0;
hud_var1 = 0;
} else {
if (!hud_cur_item)
hud_cur_item = 1;
if (!Hud_DoWeHaveThisItem(hud_cur_item))
Hud_EquipNextItem(&hud_cur_item);
}
}
static const ItemBoxGfx *Hud_GetIconForItem(int i) {
if (i <= 0)
return kHudItemEmpty;
if (i >= kHudItem_Bottle1)
return &kHudItemBottles[link_bottle_info[i - kHudItem_Bottle1]];
if (i == kHudItem_Shovel && kNewStyleInventory)
return &kHudItemFlute[link_item_flute >= 1];
uint8 item_val = (&link_item_bow)[i - 1];
if (i == kHudItem_Bombs) // bombs
item_val = (item_val != 0);
else if (i == kHudItem_BottleOld && !kNewStyleInventory)
item_val = link_item_bottle_index ? link_bottle_info[link_item_bottle_index - 1] : 0;
return &kHudItemBoxGfxPtrs[i - 1][item_val];
}
static void CopyTilesForSwitchLR(int switch_lr) {
#define PV(a0,a1,a2,a3,a4,a5,a6,a7) ((a0 & 1) << 7 | (a0 >> 1 & 1) << 15 | (a1 & 1) << 6 | (a1 >> 1 & 1) << 14 | (a2 & 1) <<5 | (a2 >> 1&1) <<13 | (a3 & 1) << 4 | (a3>> 1 & 1) << 12 | (a4 & 1) << 3 | (a4 >> 1 & 1) << 11 | (a5 & 1) << 2 | (a5 >> 1 & 1) << 10 | (a6 & 1) << 1 | (a6 >> 1 & 1) << 9 | (a7 & 1) << 0 | (a7 >> 1 & 1) << 8)
if (switch_lr == 3) {
static const uint16 kBytesForNewTile0xC_TopOfR[8] = {
PV(1,1,1,1,1,1,3,3),
PV(1,1,1,1,1,1,1,3),
PV(1,1,1,1,1,1,1,1),
PV(1,1,1,3,3,1,1,1),
PV(1,1,1,3,3,1,1,1),
PV(1,1,1,3,3,1,1,1),
PV(1,1,1,3,3,1,1,1),
PV(1,1,1,1,1,1,1,3)
};
memcpy(&g_zenv.vram[0x7000 + 0xc * 8], kBytesForNewTile0xC_TopOfR, sizeof(kBytesForNewTile0xC_TopOfR));
static const uint16 kBytesForNewTile0xD_BottomofR[8] = {
PV(1,1,1,1,1,1,3,3),
PV(1,1,1,3,1,1,1,3),
PV(1,1,1,3,3,1,1,1),
PV(1,1,1,3,3,1,1,1),
PV(1,1,1,3,3,1,1,1),
PV(1,1,1,3,3,1,1,1),
PV(1,1,1,3,3,1,1,1),
PV(1,1,1,3,3,1,1,1)
};
memcpy(&g_zenv.vram[0x7000 + 0xd * 8], kBytesForNewTile0xD_BottomofR, sizeof(kBytesForNewTile0xD_BottomofR));
} else if (switch_lr == 2) {
static const uint16 kBytesForNewTile0xE_TopOfL[8] = {
PV(1,1,1,3,3,3,3,3),
PV(1,1,1,3,3,3,3,3),
PV(1,1,1,3,3,3,3,3),
PV(1,1,1,3,3,3,3,3),
PV(1,1,1,3,3,3,3,3),
PV(1,1,1,3,3,3,3,3),
PV(1,1,1,3,3,3,3,3),
PV(1,1,1,3,3,3,3,3)
};
memcpy(&g_zenv.vram[0x7000 + 0xe * 8], kBytesForNewTile0xE_TopOfL, sizeof(kBytesForNewTile0xE_TopOfL));
static const uint16 kBytesForNewTile0xF_BottomofL[8] = {
PV(1,1,1,3,3,3,3,3),
PV(1,1,1,3,3,3,3,3),
PV(1,1,1,3,3,3,3,3),
PV(1,1,1,3,3,3,3,3),
PV(1,1,1,3,3,3,3,3),
PV(1,1,1,1,1,1,1,1),
PV(1,1,1,1,1,1,1,1),
PV(1,1,1,1,1,1,1,1)
};
memcpy(&g_zenv.vram[0x7000 + 0xf * 8], kBytesForNewTile0xF_BottomofL, sizeof(kBytesForNewTile0xF_BottomofL));
}
#undef PV
}
static const uint8 kSwitchLR_palettes[] = { 7, 3, 4, 4 };
void Hud_DrawYButtonItems() { // 8de3d9
uint16 *dst = uvram_screen.row[0].col;
int x = kNewStyleInventory ? 0 : 1;
int btn_index = GetCurrentItemButtonIndex();
CopyTilesForSwitchLR(btn_index);
Hud_DrawBox(dst, x, 5, 20 - x, 19, kSwitchLR_palettes[btn_index]);
static const uint16 kEquipmentLetterTiles[4][2] = {
{0x3CF0, 0x3CF1}, // Y
{0x2CF0, 0x2CF0 | 0x8000}, // X
{0x200E | 4 << 10, 0x200F | 4 << 10}, // L
{0x200C | 4 << 10, 0x200D | 4 << 10} // R
};