forked from snesrev/zelda3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
6640 lines (6010 loc) · 205 KB
/
player.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 "player.h"
#include "zelda_rtl.h"
#include "variables.h"
#include "tile_detect.h"
#include "ancilla.h"
#include "sprite.h"
#include "load_gfx.h"
#include "hud.h"
#include "overworld.h"
#include "tagalong.h"
#include "dungeon.h"
#include "misc.h"
#include "player_oam.h"
#include "sprite_main.h"
static bool g_ApplyLinksMovementToCamera_called;
static const uint8 kSpinAttackDelays[] = { 1, 0, 0, 0, 0, 3, 0, 0, 1, 0, 3, 3, 3, 3, 4, 4, 1, 5 };
static const uint8 kFireBeamSounds[] = { 1, 2, 3, 4, 0, 9, 18, 27 };
static const int8 kTagalongArr1[] = { -1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
static const int8 kTagalongArr2[] = { -1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
static const uint8 kLinkSpinGraphicsByDir[] = {
10, 11, 10, 6, 7, 8, 9, 2, 3, 4, 5, 10, 0, 1, 0, 2, 3, 4, 5, 6, 7, 8, 9, 0, 12, 13, 12, 4, 5, 6, 7, 8, 9, 2, 3, 12, 14, 15, 14, 8, 9, 2, 3, 4, 5, 6, 7, 14
};
static const uint8 kLinkSpinDelays[] = { 1, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5 };
static const uint8 kGrabWallDirs[] = { 4, 8, 1, 2 };
static const uint8 kGrabWall_AnimSteps[] = { 0, 1, 2, 3, 1, 2, 3 };
static const uint8 kGrabWall_AnimTimer[] = { 0, 5, 5, 12, 5, 5, 12 };
static const uint8 kCapeDepletionTimers[] = { 4, 8, 8 };
static const int8 kAvoidJudder1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 4, 5, 6, 7 };
static const int8 kLink_DoMoveXCoord_Outdoors_Helper2_y[2] = { -8, 8 };
static const int8 kLink_DoMoveXCoord_Outdoors_Helper2_y2[2] = { -16, 16 };
static const uint8 kLink_DoMoveXCoord_Outdoors_Helper2_velz[8] = { 32, 32, 32, 40, 48, 56, 64, 72 };
static const uint8 kLink_DoMoveXCoord_Outdoors_Helper2_velx[8] = { 16, 28, 28, 28, 28, 28, 28, 28 };
static const uint8 kLink_Lift_tab[9] = { 0x54, 0x52, 0x50, 0xFF, 0x51, 0x53, 0x55, 0x56, 0x57 };
static const uint8 kLink_Move_Helper6_tab0[] = { 8, 8, 23, 23, 8, 23, 8, 23 };
static const uint8 kLink_Move_Helper6_tab1[] = { 0, 15, 0, 15, 0, 0, 15, 15 };
static const uint8 kLink_Move_Helper6_tab2[] = { 23, 23, 8, 8, 8, 23, 8, 23 };
static const uint8 kLink_Move_Helper6_tab3[] = { 0, 15, 0, 15, 15, 15, 0, 0 };
const uint8 kSwimmingTab1[4] = { 2, 0, 1, 0 };
const uint8 kSwimmingTab2[2] = { 32, 8 };
static PlayerHandlerFunc *const kPlayerHandlers[31] = {
&LinkState_Default,
&LinkState_Pits,
&LinkState_Recoil,
&LinkState_SpinAttack,
&PlayerHandler_04_Swimming,
&LinkState_OnIce,
&LinkState_Recoil,
&LinkState_Zapped,
&LinkState_UsingEther,
&LinkState_UsingBombos,
&LinkState_UsingQuake,
&LinkHop_HoppingSouthOW,
&LinkState_HoppingHorizontallyOW,
&LinkState_HoppingDiagonallyUpOW,
&LinkState_HoppingDiagonallyDownOW,
&LinkState_0F,
&LinkState_0F,
&LinkState_Dashing,
&LinkState_ExitingDash,
&LinkState_Hookshotting,
&LinkState_CrossingWorlds,
&PlayerHandler_15_HoldItem,
&LinkState_Sleeping,
&PlayerHandler_17_Bunny,
&LinkState_HoldingBigRock,
&LinkState_ReceivingEther,
&LinkState_ReceivingBombos,
&LinkState_ReadingDesertTablet,
&LinkState_TemporaryBunny,
&LinkState_TreePull,
&LinkState_SpinAttack,
};
// forwards
static const uint8 kLinkItem_MagicCosts[] = { 16, 8, 4, 32, 16, 8, 8, 4, 2, 8, 4, 2, 8, 4, 2, 16, 8, 4, 4, 2, 2, 8, 4, 2, 16, 8, 4 };
static const uint8 kBombosAnimDelays[] = { 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 7, 1, 1, 1, 1, 1, 13 };
static const uint8 kBombosAnimStates[] = { 0, 1, 2, 3, 0, 1, 2, 3, 8, 9, 10, 11, 12, 10, 8, 13, 14, 15, 16, 17 };
static const uint8 kEtherAnimDelays[] = { 5, 5, 5, 5, 5, 5, 5, 5, 7, 7, 3, 3 };
static const uint8 kEtherAnimStates[] = { 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7 };
static const uint8 kQuakeAnimDelays[] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 19 };
static const uint8 kQuakeAnimStates[] = { 0, 1, 2, 3, 0, 1, 2, 3, 18, 19, 20, 22 };
static inline uint8 BitSum4(uint8 t);
static inline uint8 BitSum4(uint8 t) {
return (t & 1) + ((t >> 1) & 1) + ((t >> 2) & 1) + ((t >> 3) & 1);
}
void Dungeon_HandleLayerChange() { // 81ff05
link_is_on_lower_level_mirror = 1;
if (kind_of_in_room_staircase == 0)
BYTE(dungeon_room_index) += 16;
if (kind_of_in_room_staircase != 2)
link_is_on_lower_level = 1;
about_to_jump_off_ledge = 0;
SetAndSaveVisitedQuadrantFlags();
}
void CacheCameraProperties() { // 81ff28
BG2HOFS_copy2_cached = BG2HOFS_copy2;
BG2VOFS_copy2_cached = BG2VOFS_copy2;
link_y_coord_cached = link_y_coord;
link_x_coord_cached = link_x_coord;
room_scroll_vars_y_vofs1_cached = room_bounds_y.a0;
room_scroll_vars_y_vofs2_cached = room_bounds_y.a1;
room_scroll_vars_x_vofs1_cached = room_bounds_x.a0;
room_scroll_vars_x_vofs2_cached = room_bounds_x.a1;
up_down_scroll_target_cached = up_down_scroll_target;
up_down_scroll_target_end_cached = up_down_scroll_target_end;
left_right_scroll_target_cached = left_right_scroll_target;
left_right_scroll_target_end_cached = left_right_scroll_target_end;
camera_y_coord_scroll_low_cached = camera_y_coord_scroll_low;
camera_x_coord_scroll_low_cached = camera_x_coord_scroll_low;
quadrant_fullsize_x_cached = quadrant_fullsize_x;
quadrant_fullsize_y_cached = quadrant_fullsize_y;
link_quadrant_x_cached = link_quadrant_x;
link_quadrant_y_cached = link_quadrant_y;
link_direction_facing_cached = link_direction_facing;
link_is_on_lower_level_cached = link_is_on_lower_level;
link_is_on_lower_level_mirror_cached = link_is_on_lower_level_mirror;
is_standing_in_doorway_cahed = is_standing_in_doorway;
dung_cur_floor_cached = dung_cur_floor;
}
void CheckAbilityToSwim() { // 81ffb6
if (!link_is_bunny_mirror && link_item_flippers)
return;
if (link_item_moon_pearl)
link_is_bunny_mirror = 0;
link_visibility_status = 0xc;
submodule_index = player_is_indoors ? 20 : 42;
}
void Link_Main() { // 878000
// RunEmulatedFunc(0x878000, 0, 0, 0, true, true, -2, 0);
// return;
link_x_coord_prev = link_x_coord;
link_y_coord_prev = link_y_coord;
flag_unk1 = 0;
if (!flag_is_link_immobilized)
Link_ControlHandler();
HandleSomariaAndGraves();
}
void Link_ControlHandler() { // 87807f
if (link_give_damage) {
if (link_cape_mode) {
link_give_damage = 0;
link_auxiliary_state = 0;
link_incapacitated_timer = 0;
} else {
if (!link_disable_sprite_damage) {
uint8 dmg = link_give_damage;
link_give_damage = 0;
if (ancilla_type[0] == 5 && player_handler_timer == 0 && link_delay_timer_spin_attack) {
ancilla_type[0] = 0;
flag_for_boomerang_in_place = 0;
}
if (countdown_for_blink == 0)
countdown_for_blink = 58;
Ancilla_Sfx2_Near(38);
number_of_times_hurt_by_sprites++;
uint8 new_dmg = link_health_current - dmg;
if (new_dmg == 0 || new_dmg >= 0xa8) {
mapbak_TM = TM_copy;
mapbak_TS = TS_copy;
saved_module_for_menu = main_module_index;
main_module_index = 18;
submodule_index = 1;
countdown_for_blink = 0;
link_hearts_filler = 0;
new_dmg = 0;
}
link_health_current = new_dmg;
}
}
}
if (link_player_handler_state)
Player_CheckHandleCapeStuff();
kPlayerHandlers[link_player_handler_state]();
}
void LinkState_Default() { // 878109
CacheCameraPropertiesIfOutdoors();
if (Link_HandleBunnyTransformation()) {
if (link_player_handler_state == 23)
PlayerHandler_17_Bunny();
return;
}
fallhole_var2 = 0;
if (link_auxiliary_state)
HandleLink_From1D();
else
PlayerHandler_00_Ground_3();
}
void HandleLink_From1D() { // 878130
link_item_in_hand = 0;
link_position_mode = 0;
link_debug_value_1 = 0;
link_debug_value_2 = 0;
link_var30d = 0;
link_var30e = 0;
some_animation_timer_steps = 0;
bitfield_for_a_button = 0;
button_mask_b_y &= ~0x40;
link_state_bits = 0;
link_picking_throw_state = 0;
link_grabbing_wall = 0;
bitmask_of_dragstate = 0;
Link_ResetSwimmingState();
link_cant_change_direction &= ~1;
link_z_coord &= 0xff;
if (link_electrocute_on_touch != 0) {
if (link_cape_mode)
Link_ForceUnequipCape_quietly();
Link_ResetSwordAndItemUsage();
link_disable_sprite_damage = 1;
player_handler_timer = 0;
link_delay_timer_spin_attack = 2;
link_animation_steps = 0;
link_direction &= ~0xf;
Ancilla_Sfx3_Near(43);
link_player_handler_state = 7;
LinkState_Zapped();
} else {
link_moving_against_diag_tile = 0;
link_player_handler_state = 2;
LinkState_Recoil();
}
}
void PlayerHandler_00_Ground_3() { // 8781a0
g_ApplyLinksMovementToCamera_called = false;
link_z_coord = 0xffff;
link_actual_vel_z = 0xff;
link_recoilmode_timer = 0;
if (!Link_HandleToss()) {
Link_HandleAPress();
if ((link_state_bits | link_grabbing_wall) == 0 && link_unk_master_sword == 0 && link_player_handler_state != kPlayerState_StartDash) {
Link_HandleYItem();
// Ensure we're not handling potions. Things further
// down don't assume this and change the module indexes randomly.
// This also fixes a bug where bombos, ether, quake get aborted if you use spin attack at the same time.
if ((enhanced_features0 & kFeatures0_MiscBugFixes) && (
main_module_index == 14 && submodule_index != 2 ||
link_player_handler_state == kPlayerState_Bombos ||
link_player_handler_state == kPlayerState_Ether ||
link_player_handler_state == kPlayerState_Quake))
goto getout_clear_vel;
if (sram_progress_indicator != 0) {
Link_HandleSwordCooldown();
if (link_player_handler_state == 3)
goto getout_clear_vel;
}
}
}
Link_HandleCape_passive_LiftCheck();
if (link_incapacitated_timer) {
link_moving_against_diag_tile = 0;
link_var30d = 0;
link_var30e = 0;
some_animation_timer_steps = 0;
bitfield_for_a_button = 0;
link_picking_throw_state = 0;
link_state_bits = 0;
link_grabbing_wall = 0;
if (!(button_mask_b_y & 0x80))
link_cant_change_direction &= ~1;
Link_HandleRecoilAndTimer(false);
return;
}
if (link_unk_master_sword) {
link_direction = 0;
} else if (!link_is_transforming && (link_grabbing_wall & ~2) == 0 && (link_state_bits & 0x7f) == 0 &&
((link_state_bits & 0x80) == 0 || (link_picking_throw_state & 1) == 0) && !link_item_in_hand && !link_position_mode &&
(button_b_frames >= 9 || (button_mask_b_y & 0x20) != 0 || (button_mask_b_y & 0x80) == 0)) {
// if_4
if (link_flag_moving) {
swimcoll_var9[0] = swimcoll_var9[1] = 0x180;
Link_HandleSwimMovements();
return;
}
ResetAllAcceleration();
uint8 dir;
if ((dir = (force_move_any_direction & 0xf)) == 0) {
if (link_grabbing_wall & 2)
goto endif_3;
if ((dir = (joypad1H_last & kJoypadH_AnyDir)) == 0) {
link_x_vel = 0;
link_y_vel = 0;
link_direction = 0;
link_direction_last = 0;
link_animation_steps = 0;
bitmask_of_dragstate &= ~0xf;
link_timer_push_get_tired = 32;
link_timer_jump_ledge = 19;
goto endif_3;
}
}
link_direction = dir;
if (dir != link_direction_last) {
link_direction_last = dir;
link_subpixel_x = link_subpixel_y = 0;
link_moving_against_diag_tile = 0;
bitmask_of_dragstate = 0;
link_timer_push_get_tired = 32;
link_timer_jump_ledge = 19;
}
}
// endif_3
endif_3:
Link_HandleDiagonalCollision();
Link_HandleVelocity();
Link_HandleCardinalCollision();
Link_HandleMovingAnimation_FullLongEntry();
if (link_unk_master_sword) getout_clear_vel: {
link_y_vel = link_x_vel = 0;
}
fallhole_var1 = 0;
// HandleIndoorCameraAndDoors must not be called twice in the same frame,
// this might mess up camera positioning. For example when using spin attack
// in between bumpers.
if (g_ApplyLinksMovementToCamera_called && (enhanced_features0 & kFeatures0_MiscBugFixes))
return;
HandleIndoorCameraAndDoors();
}
bool Link_HandleBunnyTransformation() { // 8782da
if (!link_timer_tempbunny)
return false;
if (!link_need_for_poof_for_transform) {
if (link_player_handler_state == kPlayerState_PermaBunny || link_player_handler_state == kPlayerState_TempBunny) {
link_timer_tempbunny = 0;
return false;
}
if (link_picking_throw_state & 2)
link_state_bits = 0;
uint8 bak = link_state_bits & 0x80;
Link_ResetProperties_A();
link_state_bits = bak;
for (int i = 4; i >= 0; i--) {
if (ancilla_type[i] == 0x30 || ancilla_type[i] == 0x31)
ancilla_type[i] = 0;
}
Link_CancelDash();
AncillaAdd_CapePoof(0x23, 4);
Ancilla_Sfx2_Near(0x14);
link_bunny_transform_timer = 20;
link_disable_sprite_damage = 1;
link_need_for_poof_for_transform = 1;
link_visibility_status = 12;
}
if (sign8(--link_bunny_transform_timer)) {
link_player_handler_state = kPlayerState_TempBunny;
link_is_bunny_mirror = 1;
link_is_bunny = 1;
LoadGearPalettes_bunny();
link_visibility_status = 0;
link_disable_sprite_damage = 0;
link_need_for_poof_for_transform = 0;
}
return true;
}
void LinkState_TemporaryBunny() { // 878365
if (!link_timer_tempbunny) {
AncillaAdd_CapePoof(0x23, 4);
Ancilla_Sfx2_Near(0x15);
link_bunny_transform_timer = 32;
link_player_handler_state = 0;
Link_ResetProperties_C();
link_need_for_poof_for_transform = 0;
link_is_bunny = 0;
link_is_bunny_mirror = 0;
LoadActualGearPalettes();
link_need_for_poof_for_transform = 0;
LinkState_Default();
} else {
link_timer_tempbunny--;
PlayerHandler_17_Bunny();
}
}
void PlayerHandler_17_Bunny() { // 8783a1
CacheCameraPropertiesIfOutdoors();
fallhole_var2 = 0;
if (!link_is_in_deep_water) {
if (link_auxiliary_state == 0) {
Link_TempBunny_Func2();
return;
}
if (link_item_moon_pearl)
link_is_bunny_mirror = 0;
}
LinkState_Bunny_recache();
}
void LinkState_Bunny_recache() { // 8783c7
link_need_for_poof_for_transform = 0;
link_timer_tempbunny = 0;
if (link_item_moon_pearl) {
link_is_bunny = 0;
link_auxiliary_state = 0;
}
link_animation_steps = 0;
link_is_transforming = 0;
link_cant_change_direction = 0;
Link_ResetSwimmingState();
link_player_handler_state = kPlayerState_RecoilWall;
if (link_item_moon_pearl) {
link_player_handler_state = kPlayerState_Ground;
LoadActualGearPalettes();
}
}
void Link_TempBunny_Func2() { // 8783fa
if (link_incapacitated_timer != 0) {
Link_HandleRecoilAndTimer(false);
return;
}
link_z_coord = 0xffff;
link_actual_vel_z = 0xff;
link_recoilmode_timer = 0;
if (link_flag_moving) {
swimcoll_var9[0] = swimcoll_var9[1] = 0x180;
Link_HandleSwimMovements();
return;
}
ResetAllAcceleration();
Link_HandleYItem();
uint8 dir;
if (!(dir = force_move_any_direction & 0xf) && !(dir = joypad1H_last & kJoypadH_AnyDir)) {
link_x_vel = link_y_vel = 0;
link_direction = link_direction_last = 0;
link_animation_steps = 0;
bitmask_of_dragstate &= ~9;
link_timer_push_get_tired = 32;
link_timer_jump_ledge = 19;
} else {
link_direction = dir;
if (dir != link_direction_last) {
link_direction_last = dir;
link_subpixel_x = 0;
link_subpixel_y = 0;
link_moving_against_diag_tile = 0;
bitmask_of_dragstate = 0;
link_timer_push_get_tired = 32;
link_timer_jump_ledge = 19;
}
}
Link_HandleDiagonalCollision();
Link_HandleVelocity();
Link_HandleCardinalCollision();
Link_HandleMovingAnimation_FullLongEntry();
fallhole_var1 = 0;
HandleIndoorCameraAndDoors();
}
void LinkState_HoldingBigRock() { // 878481
if (link_auxiliary_state) {
link_item_in_hand = 0;
link_position_mode = 0;
link_debug_value_1 = 0;
link_debug_value_2 = 0;
link_var30d = 0;
link_var30e = 0;
some_animation_timer_steps = 0;
bitfield_for_a_button = 0;
link_state_bits = 0;
link_picking_throw_state = 0;
link_grabbing_wall = 0;
bitmask_of_dragstate = 0;
link_cant_change_direction &= ~1;
link_z_coord &= ~0xff;
if (link_electrocute_on_touch) {
Link_ResetSwordAndItemUsage();
link_disable_sprite_damage = 1;
player_handler_timer = 0;
link_delay_timer_spin_attack = 2;
link_animation_steps = 0;
link_direction &= ~0xf;
Ancilla_Sfx3_Near(43);
link_player_handler_state = kPlayerState_Electrocution;
LinkState_Zapped();
} else {
link_player_handler_state = kPlayerState_RecoilWall;
LinkState_Recoil();
}
return;
}
link_z_coord = 0xffff;
link_actual_vel_z = 0xff;
link_recoilmode_timer = 0;
if (link_incapacitated_timer) {
link_var30d = 0;
link_var30e = 0;
some_animation_timer_steps = 0;
bitfield_for_a_button = 0;
link_state_bits = 0;
link_picking_throw_state = 0;
link_grabbing_wall = 0;
if (!(button_mask_b_y & 0x80))
link_cant_change_direction &= ~1;
Link_HandleRecoilAndTimer(false);
return;
}
Link_HandleAPress();
if (!(joypad1H_last & kJoypadH_AnyDir)) {
link_y_vel = 0;
link_x_vel = 0;
link_direction = 0;
link_direction_last = 0;
link_animation_steps = 0;
bitmask_of_dragstate &= ~9;
link_timer_push_get_tired = 32;
link_timer_jump_ledge = 19;
} else {
link_direction = joypad1H_last & kJoypadH_AnyDir;
if (link_direction != link_direction_last) {
link_direction_last = link_direction;
link_subpixel_x = 0;
link_subpixel_y = 0;
link_moving_against_diag_tile = 0;
bitmask_of_dragstate = 0;
link_timer_push_get_tired = 32;
link_timer_jump_ledge = 19;
}
}
Link_HandleMovingAnimation_FullLongEntry();
fallhole_var1 = 0;
HandleIndoorCameraAndDoors();
}
void EtherTablet_StartCutscene() { // 87855a
button_b_frames = 0xc0;
link_delay_timer_spin_attack = 0;
link_player_handler_state = kPlayerState_ReceivingEther;
link_disable_sprite_damage = 1;
flag_block_link_menu = 1;
}
void LinkState_ReceivingEther() { // 878570
link_auxiliary_state = 0;
link_incapacitated_timer = 0;
link_give_damage = 0;
int i = --WORD(button_b_frames);
if (sign16(i)) {
button_b_frames = 0;
link_delay_timer_spin_attack = 0;
} else if (i == 0xbf) {
link_force_hold_sword_up = 1;
} else if (i == 160) {
uint16 x = link_x_coord, y = link_y_coord;
link_x_coord = 0x6b0;
link_y_coord = 0x37;
AncillaAdd_EtherSpell(0x18, 0);
link_x_coord = x, link_y_coord = y;
} else if (i == 0) {
AncillaAdd_FallingPrize(0x29, 0, 4);
flag_is_link_immobilized = 1;
flag_block_link_menu = 0;
}
}
void BombosTablet_StartCutscene() { // 8785e5
button_b_frames = 0xe0;
link_delay_timer_spin_attack = 0;
link_player_handler_state = kPlayerState_ReceivingBombos;
link_disable_sprite_damage = 1;
flag_custom_spell_anim_active = 1;
}
void LinkState_ReceivingBombos() { // 8785fb
link_auxiliary_state = 0;
link_incapacitated_timer = 0;
link_give_damage = 0;
int i = --WORD(button_b_frames);
if (sign16(i)) {
button_b_frames = 0;
link_delay_timer_spin_attack = 0;
} else if (i == 223) {
link_force_hold_sword_up = 1;
} else if (i == 160) {
uint16 x = link_x_coord, y = link_y_coord;
link_x_coord = 0x378;
link_y_coord = 0xeb0;
AncillaAdd_BombosSpell(0x19, 0);
link_x_coord = x, link_y_coord = y;
} else if (i == 0) {
AncillaAdd_FallingPrize(0x29, 5, 4);
flag_is_link_immobilized = 1;
}
}
void LinkState_ReadingDesertTablet() { // 87867b
if (!--button_b_frames) {
link_player_handler_state = kPlayerState_Ground;
Link_PerformDesertPrayer();
}
}
void HandleSomariaAndGraves() { // 878689
if (!player_is_indoors && link_something_with_hookshot) {
int i = 4;
do {
if (ancilla_type[i] == 0x24)
Gravestone_Move(i);
} while (--i >= 0);
}
int i = 4;
do {
if (ancilla_type[i] == 0x2C) {
SomariaBlock_HandlePlayerInteraction(i);
return;
}
} while (--i >= 0);
}
void LinkState_Recoil() { // 8786b5
link_y_coord_safe_return_lo = link_y_coord;
link_y_coord_safe_return_hi = link_y_coord >> 8;
link_x_coord_safe_return_lo = link_x_coord;
link_x_coord_safe_return_hi = link_x_coord >> 8;
Link_HandleChangeInZVelocity();
link_cant_change_direction = 0;
draw_water_ripples_or_grass = 0;
if (!sign8(link_z_coord) || !sign8(link_actual_vel_z)) {
Link_HandleRecoilAndTimer(false);
return;
}
TileDetect_MainHandler(5);
if (tiledetect_deepwater & 1) {
link_player_handler_state = kPlayerState_Swimming;
Link_SetToDeepWater();
Link_ResetSwordAndItemUsage();
AncillaAdd_Splash(21, 0);
Link_HandleRecoilAndTimer(true);
} else {
if (++link_recoilmode_timer != 4) {
uint8 t = link_actual_vel_z_copy, s = link_recoilmode_timer;
do {
t >>= 1;
} while (!--s); // wtf?
link_actual_vel_z = t;
} else {
link_recoilmode_timer = 3;
}
Link_HandleRecoilAndTimer(false);
}
}
void Link_HandleRecoilAndTimer(bool jump_into_middle) { // 878711
if (jump_into_middle)
goto lbl_jump_into_middle;
link_x_page_movement_delta = 0;
link_y_page_movement_delta = 0;
link_num_orthogonal_directions = 0;
Link_HandleRecoiling(); // not
if (--link_incapacitated_timer == 0) {
link_incapacitated_timer = 1;
int8 z;
z = link_z_coord & 0xfe;
if (z <= 0 && (int8)link_actual_vel_z < 0) {
if (link_auxiliary_state != 0) {
link_disable_sprite_damage = 0;
scratch_0 = link_player_handler_state;
if (link_player_handler_state != 6) {
button_b_frames = 0;
button_mask_b_y = 0;
link_delay_timer_spin_attack = 0;
link_spin_attack_step_counter = 0;
}
Link_SplashUponLanding();
if (!link_is_bunny_mirror || !link_is_in_deep_water) {
if (link_want_make_noise_when_dashed) {
link_want_make_noise_when_dashed = 0;
Ancilla_Sfx2_Near(33);
} else if (scratch_0 != 2 && link_player_handler_state != 4) {
Ancilla_Sfx2_Near(33);
}
if (link_player_handler_state == 4) {
Link_ForceUnequipCape_quietly();
if (player_is_indoors && scratch_0 != 2 && link_item_flippers) {
link_is_on_lower_level = 1;
}
AncillaAdd_Splash(21, 0);
}
TileDetect_MainHandler(0);
if (tiledetect_thick_grass & 1)
Ancilla_Sfx2_Near(26);
if (tiledetect_shallow_water & 1 && sound_effect_1 != 36)
Ancilla_Sfx2_Near(28);
if (tiledetect_deepwater & 1) {
link_player_handler_state = kPlayerState_Swimming;
Link_SetToDeepWater();
Link_ResetSwordAndItemUsage();
AncillaAdd_Splash(21, 0);
}
// OMG something jumps to here...
lbl_jump_into_middle:
if (link_is_on_lower_level == 2)
link_is_on_lower_level = 0;
if (about_to_jump_off_ledge)
Dungeon_HandleLayerChange();
}
link_z_coord = 0;
link_auxiliary_state = 0;
link_speed_setting = 0;
link_cant_change_direction = 0;
link_item_in_hand = 0;
link_position_mode = 0;
player_handler_timer = 0;
link_disable_sprite_damage = 0;
link_electrocute_on_touch = 0;
link_actual_vel_x = 0;
link_actual_vel_y = 0;
}
link_animation_steps = 0;
link_incapacitated_timer = 0;
}
}
if (link_player_handler_state != 5 && link_incapacitated_timer >= 33) {
if (!sign8(--byte_7E02C5))
goto timer_running;
byte_7E02C5 = link_incapacitated_timer >> 4;
}
Flag67WithDirections();
if (link_player_handler_state != 6) {
Link_HandleDiagonalCollision(); // not
if ((link_direction & 3) == 0)
link_actual_vel_x = 0;
if ((link_direction & 0xc) == 0)
link_actual_vel_y = 0;
}
Link_MovePosition(); // not
timer_running:
if (link_player_handler_state != 6) {
Link_HandleCardinalCollision(); // not
fallhole_var1 = 0;
}
HandleIndoorCameraAndDoors();
if (BYTE(link_z_coord) == 0 || BYTE(link_z_coord) >= 0xe0) {
Player_TileDetectNearby();
if ((tiledetect_pit_tile & 0xf) == 0xf) {
link_player_handler_state = kPlayerState_FallingIntoHole;
link_speed_setting = 4;
}
}
HIBYTE(link_z_coord) = 0;
}
void LinkState_OnIce() { // 878872
assert(0);
}
void Link_HandleChangeInZVelocity() { // 878926
Player_ChangeZ(link_player_handler_state == kPlayerState_TurtleRock ? 1 : 2);
}
void Player_ChangeZ(uint8 zd) { // 878932
if (sign8(link_actual_vel_z)) {
if (!(uint8)link_z_coord)
return;
if (sign8(link_z_coord)) {
link_z_coord = 0xffff;
link_actual_vel_z = 0xff;
return;
}
}
link_actual_vel_z -= zd;
}
void LinkHop_HoppingSouthOW() { // 87894e
link_last_direction_moved_towards = 1;
link_cant_change_direction = 0;
link_actual_vel_x = 0;
link_actual_vel_y = 0;
draw_water_ripples_or_grass = 0;
if (!link_incapacitated_timer && !link_actual_vel_z_mirror) {
Ancilla_Sfx2_Near(32);
LinkHop_FindTileToLandOnSouth();
if (!player_is_indoors)
link_is_on_lower_level = 2;
}
link_actual_vel_z = link_actual_vel_z_mirror;
link_actual_vel_z_copy = link_actual_vel_z_copy_mirror;
link_z_coord = link_z_coord_mirror;
link_actual_vel_z -= 2;
Link_MovePosition();
if (sign8(link_actual_vel_z)) {
if (link_actual_vel_z < 0xa0)
link_actual_vel_z = 0xa0;
if (link_z_coord >= 0xfff0) {
link_z_coord = 0;
Link_SplashUponLanding();
// This is the place that caused the water walking bug after bonk,
// player_near_pit_state was not reset.
if (player_near_pit_state)
link_player_handler_state = kPlayerState_FallingIntoHole;
if (link_player_handler_state != kPlayerState_Swimming &&
link_player_handler_state != kPlayerState_FallingIntoHole && !link_is_in_deep_water)
Ancilla_Sfx2_Near(33);
link_disable_sprite_damage = 0;
allow_scroll_z = 0;
link_auxiliary_state = 0;
link_actual_vel_z = 0xff;
link_z_coord = 0xffff;
link_incapacitated_timer = 0;
if (!player_is_indoors)
link_is_on_lower_level = 0;
} else {
link_y_vel = link_z_coord_mirror - link_z_coord;
}
} else {
link_y_vel = link_z_coord_mirror - link_z_coord;
}
link_actual_vel_z_mirror = link_actual_vel_z;
link_actual_vel_z_copy_mirror = link_actual_vel_z_copy;
link_z_coord_mirror = link_z_coord;
}
void LinkState_HandlingJump() { // 878a05
link_actual_vel_z = link_actual_vel_z_mirror;
link_actual_vel_z_copy = link_actual_vel_z_copy_mirror;
BYTE(link_z_coord) = link_z_coord_mirror;
link_actual_vel_z -= 2;
Link_MovePosition();
if (sign8(link_actual_vel_z)) {
if (link_actual_vel_z < 0xa0)
link_actual_vel_z = 0xa0;
if ((uint8)link_z_coord >= 0xf0) {
link_z_coord = 0;
if (link_player_handler_state == kPlayerState_FallOfLeftRightLedge || link_player_handler_state == kPlayerState_JumpOffLedgeDiag) {
TileDetect_MainHandler(0);
if (tiledetect_deepwater & 1) {
link_player_handler_state = kPlayerState_Swimming;
Link_SetToDeepWater();
Link_ResetSwordAndItemUsage();
AncillaAdd_Splash(21, 0);
} else if (tiledetect_pit_tile & 1) {
byte_7E005C = 9;
link_this_controls_sprite_oam = 0;
player_near_pit_state = 1;
link_player_handler_state = kPlayerState_FallingIntoHole;
goto after_pit;
}
}
Link_SplashUponLanding();
if (link_player_handler_state != kPlayerState_Swimming && !link_is_in_deep_water)
Ancilla_Sfx2_Near(33);
after_pit:
if (link_player_handler_state != kPlayerState_Swimming || !link_is_bunny_mirror)
link_disable_sprite_damage = 0;
allow_scroll_z = 0;
link_auxiliary_state = 0;
link_actual_vel_z = 0xff;
link_z_coord = 0xffff;
link_incapacitated_timer = 0;
if (!player_is_indoors)
link_is_on_lower_level = 0;
} else {
link_y_vel = link_z_coord_mirror - link_z_coord;
}
} else {
link_y_vel = link_z_coord_mirror - link_z_coord;
}
link_actual_vel_z_mirror = link_actual_vel_z;
link_actual_vel_z_copy_mirror = link_actual_vel_z_copy;
BYTE(link_z_coord_mirror) = link_z_coord;
}
void LinkHop_FindTileToLandOnSouth() { // 878ad1
link_y_coord_original = link_y_coord;
link_y_vel = link_y_coord - link_y_coord_safe_return_lo;
for (;;) {
link_y_coord += kLink_DoMoveXCoord_Outdoors_Helper2_y[link_last_direction_moved_towards];
TileDetect_Movement_Y(link_last_direction_moved_towards);
uint8 k = tiledetect_normal_tiles | tiledetect_pit_tile | tiledetect_destruction_aftermath | tiledetect_thick_grass | tiledetect_deepwater;
if ((k & 7) == 7)
break;
}
if (tiledetect_deepwater & 7) {
link_is_in_deep_water = 1;
if (link_auxiliary_state != 4)
link_auxiliary_state = 2;
link_some_direction_bits = link_direction_last;
Link_ResetSwimmingState();
link_grabbing_wall = 0;
link_speed_setting = 0;
}
if (tiledetect_pit_tile & 7) {
byte_7E005C = 9;
link_this_controls_sprite_oam = 0;
player_near_pit_state = 1;
}
link_y_coord += kLink_DoMoveXCoord_Outdoors_Helper2_y2[link_last_direction_moved_towards];
link_y_coord_safe_return_lo = link_y_coord;
link_y_coord_safe_return_hi = link_y_coord >> 8;
link_incapacitated_timer = 1;
uint8 z = link_z_coord;
if (z >= 0xf0)
z = 0;
link_z_coord = link_z_coord_mirror = link_y_coord - link_y_coord_original + z;
}
// used on right ledges
void LinkState_HoppingHorizontallyOW() { // 878b74
link_direction = sign8(link_actual_vel_x) ? 6 : 5;
link_cant_change_direction = 0;
link_actual_vel_y = 0;
draw_water_ripples_or_grass = 0;
LinkState_HandlingJump();
}
void Link_HoppingHorizontally_FindTile_Y() { // 878b9b
link_y_coord_original = link_y_coord;
link_y_vel = link_y_coord - link_y_coord_safe_return_lo;
link_y_coord += kLink_DoMoveXCoord_Outdoors_Helper2_y[link_last_direction_moved_towards];
TileDetect_Movement_Y(link_last_direction_moved_towards);
uint8 tt = tiledetect_normal_tiles | tiledetect_destruction_aftermath | tiledetect_thick_grass |
tiledetect_deepwater;
if ((tt & 7) != 7) {
link_y_coord = link_y_coord_original;
link_incapacitated_timer = 1;
int8 velx = link_actual_vel_x, org_velx = velx;
if (velx < 0) velx = -velx;
velx >>= 4;
link_actual_vel_z_mirror = link_actual_vel_z_copy_mirror = kLink_DoMoveXCoord_Outdoors_Helper2_velz[velx];
uint8 xt = kLink_DoMoveXCoord_Outdoors_Helper2_velx[velx];
if (org_velx < 0) xt = -xt;
link_actual_vel_x = xt;
} else { // else_1
link_y_coord += kLink_DoMoveXCoord_Outdoors_Helper2_y2[link_last_direction_moved_towards];
link_y_coord_safe_return_lo = link_y_coord;
link_y_coord_safe_return_hi = link_y_coord >> 8;
link_incapacitated_timer = 1;
uint8 z = link_z_coord;
if (z == 255) z = 0;
link_z_coord_mirror = link_z_coord = link_y_coord - link_y_coord_original + z;
} // endif_1
if (tiledetect_deepwater & 7) {
link_auxiliary_state = 2;
Link_SetToDeepWater();
}
}
void Link_SetToDeepWater() { // 878c44
link_is_in_deep_water = 1;
link_some_direction_bits = link_direction_last;
Link_ResetSwimmingState();
link_grabbing_wall = 0;
link_speed_setting = 0;
}
void LinkState_0F() { // 878c69
assert(0);
}
uint8 Link_HoppingHorizontally_FindTile_X(uint8 o) { // 878d2b
assert(o == 0 || o == 2);
link_y_coord_original = link_x_coord;
int i = 7;
static const int8 kLink_DoMoveXCoord_Outdoors_Helper1_tab1[2] = { -8, 8 };
static const int8 kLink_DoMoveXCoord_Outdoors_Helper1_tab2[2] = { -32, 32 };
static const int8 kLink_DoMoveXCoord_Outdoors_Helper1_tab3[2] = { -16, 16 };
static const uint8 kLink_DoMoveXCoord_Outdoors_Helper1_velx[24] = { 20, 20, 20, 24, 24, 24, 24, 28, 28, 36, 36, 36, 36, 36, 36, 38, 38, 38, 38, 38, 38, 38, 40, 40 };
static const uint8 kLink_DoMoveXCoord_Outdoors_Helper1_velz[24] = { 20, 20, 20, 20, 20, 20, 20, 24, 24, 32, 32, 32, 36, 36, 36, 38, 38, 38, 38, 38, 38, 38, 40, 40 };
do {
link_x_coord += kLink_DoMoveXCoord_Outdoors_Helper1_tab1[o >> 1];
TileDetect_Movement_X(link_last_direction_moved_towards);