shithub: zelda3

Download patch

ref: 8bee71746f3ebf285976e16f88ddb2aed03e09f1
parent: 87685c8b06551a1b7d173877cccd0b6eb7cc251b
author: Snesrev <snesrev@protonmail.com>
date: Sat Oct 22 08:56:59 EDT 2022

Change some numbers into joypad constants

--- a/hud.c
+++ b/hud.c
@@ -599,7 +599,7 @@
   if (!BYTE(joypad1H_last))
     BYTE(hud_tmp1) = 0;
 
-  if (filtered_joypad_H & 0x10) {  // start
+  if (filtered_joypad_H & kJoypadH_Start) {
     overworld_map_state = 5;
     sound_effect_2 = 18;
     return;
@@ -606,7 +606,7 @@
   }
 
   // Allow select to open the save/exit thing
-  if (joypad1H_last & 0x20 && sram_progress_indicator) {  // select
+  if (joypad1H_last & kJoypadH_Select && sram_progress_indicator) {
     BG3VOFS_copy2 = -8;
     Hud_CloseMenu();
     DisplaySelectMenu();
@@ -613,27 +613,27 @@
     return;
   }
 
-  if (joypad1H_last & 0x40 && !(joypad1L_last & 0x40) && (enhanced_features0 & kFeatures0_SwitchLR)) {
-    if (filtered_joypad_H & 8) {
+  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 & 4) {
+    } else if (filtered_joypad_H & kJoypadH_Down) {
       Hud_ReorderItem(kNewStyleInventory ? 6 : 5);
-    } else if (filtered_joypad_H & 2) {
+    } else if (filtered_joypad_H & kJoypadH_Left) {
       Hud_ReorderItem(-1);
-    } else if (filtered_joypad_H & 1) {
+    } else if (filtered_joypad_H & kJoypadH_Right) {
       Hud_ReorderItem(1);
     }
   } else if (!BYTE(hud_tmp1)) {
     // If the x button is down, then move the blue circle
-    uint8 *item_p = (joypad1L_last & 0x40 && (enhanced_features0 & kFeatures0_SwitchLR)) ? &hud_cur_item_x : &hud_cur_item;
+    uint8 *item_p = (joypad1L_last & kJoypadL_X && (enhanced_features0 & kFeatures0_SwitchLR)) ? &hud_cur_item_x : &hud_cur_item;
     uint16 old_item = *item_p;
-    if (filtered_joypad_H & 8) {
+    if (filtered_joypad_H & kJoypadH_Up) {
       Hud_EquipItemAbove(item_p);
-    } else if (filtered_joypad_H & 4) {
+    } else if (filtered_joypad_H & kJoypadH_Down) {
       Hud_EquipItemBelow(item_p);
-    } else if (filtered_joypad_H & 2) {
+    } else if (filtered_joypad_H & kJoypadH_Left) {
       Hud_EquipPrevItem(item_p);
-    } else if (filtered_joypad_H & 1) {
+    } else if (filtered_joypad_H & kJoypadH_Right) {
       Hud_EquipNextItem(item_p);
     }
     BYTE(hud_tmp1) = filtered_joypad_H;
@@ -742,11 +742,11 @@
 
 void Hud_BottleMenu() {  // 8de0df
   timer_for_flashing_circle++;
-  if (filtered_joypad_H & 0x10) {
+  if (filtered_joypad_H & kJoypadH_Start) {
     sound_effect_2 = 18;
     overworld_map_state = 5;
-  } else if (filtered_joypad_H & 3) {
-    if (filtered_joypad_H & 2) {
+  } 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);
@@ -760,10 +760,10 @@
     return;
   }
   Hud_DrawBottleMenu_Update();
-  if (filtered_joypad_H & 12) {
+  if (filtered_joypad_H & (kJoypadH_Down | kJoypadH_Up)) {
     uint8 old_val = link_item_bottle_index - 1, val = old_val;
 
-    if (filtered_joypad_H & 8) {
+    if (filtered_joypad_H & kJoypadH_Up) {
       do {
         val = (val - 1) & 3;
       } while (!link_bottle_info[val]);
@@ -916,7 +916,7 @@
   uint16 *dst = uvram_screen.row[0].col;
   int x = kNewStyleInventory ? 0 : 1;
 
-  bool is_x = (joypad1L_last & 0x40 && (enhanced_features0 & kFeatures0_SwitchLR));
+  bool is_x = (joypad1L_last & kJoypadL_X && (enhanced_features0 & kFeatures0_SwitchLR));
   uint8 palette = is_x ? 3 : 7;
   Hud_DrawBox(dst, x, 5, 20 - x, 19, palette);
 
@@ -1129,7 +1129,7 @@
   uint16 *dst_org = uvram_screen.row[0].col;
   uint16 *dst_box = dst_org + (kNewStyleInventory ? 1 : 0);
 
-  bool is_x = (joypad1L_last & 0x40 && (enhanced_features0 & kFeatures0_SwitchLR));
+  bool is_x = (joypad1L_last & kJoypadL_X && (enhanced_features0 & kFeatures0_SwitchLR));
   uint8 palette = is_x ? 3 : 7;
   Hud_DrawBox(dst_box, 21, 5, 21 + 9, 10, palette);
 
@@ -1434,7 +1434,7 @@
 void Hud_HandleItemSwitchInputs() {
   if (!(enhanced_features0 & kFeatures0_SwitchLR))
     return;
-  if (filtered_joypad_L & (0x20 | 0x10)) {  // left/right shoulder
+  if (filtered_joypad_L & (kJoypadL_L | kJoypadL_R)) {
     int old_item = hud_cur_item;
     for (int i = 0; ; i++) {
       if (i >= kHudItemCount) {
@@ -1441,7 +1441,7 @@
         hud_cur_item = 0;
         break;
       }
-      if (filtered_joypad_L & 0x20)
+      if (filtered_joypad_L & kJoypadL_L)
         Hud_GotoPrevItem(&hud_cur_item, 1);
       else
         Hud_GotoNextItem(&hud_cur_item, 1);
--- a/player.c
+++ b/player.c
@@ -294,7 +294,7 @@
     if ((dir = (force_move_any_direction & 0xf)) == 0) {
       if (link_grabbing_wall & 2)
         goto endif_3;
-      if ((dir = (joypad1H_last & 0xf)) == 0) {
+      if ((dir = (joypad1H_last & kJoypadH_AnyDir)) == 0) {
         link_x_vel = 0;
         link_y_vel = 0;
         link_direction = 0;
@@ -444,7 +444,7 @@
   ResetAllAcceleration();
   Link_HandleYItem();
   uint8 dir;
-  if (!(dir = force_move_any_direction & 0xf) && !(dir = joypad1H_last & 0xf)) {
+  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;
@@ -522,7 +522,7 @@
   }
 
   Link_HandleAPress();
-  if (!(joypad1H_last & 0xf)) {
+  if (!(joypad1H_last & kJoypadH_AnyDir)) {
     link_y_vel = 0;
     link_x_vel = 0;
     link_direction = 0;
@@ -532,7 +532,7 @@
     link_timer_push_get_tired = 32;
     link_timer_jump_ledge = 19;
   } else {
-    link_direction = joypad1H_last & 0xf;
+    link_direction = joypad1H_last & kJoypadH_AnyDir;
     if (link_direction != link_direction_last) {
       link_direction_last = link_direction;
       link_subpixel_x = 0;
@@ -1192,7 +1192,7 @@
       follower_indicator = kTagalongArr2[follower_indicator];
   } else {
     index_of_dashing_sfx = 0;
-    if (!(joypad1L_last & 0x80)) {
+    if (!(joypad1L_last & kJoypadL_A)) {
       link_animation_steps = 0;
       link_countdown_for_dash = 0;
       link_speed_setting = 0;
@@ -1207,7 +1207,7 @@
     link_dash_ctr = 64;
     link_speed_setting = 16;
     uint8 dir;
-    if (button_mask_b_y & 0x80 || is_standing_in_doorway || (dir = joypad1H_last & 0xf) == 0)
+    if (button_mask_b_y & 0x80 || is_standing_in_doorway || (dir = joypad1H_last & kJoypadH_AnyDir) == 0)
       dir = kDashTab2[link_direction_facing >> 1];
     link_some_direction_bits = link_direction = link_direction_last = dir;
     link_moving_against_diag_tile = 0;
@@ -1251,12 +1251,12 @@
   bool want_stop_dash = false;
 
   if (enhanced_features0 & kFeatures0_TurnWhileDashing) {
-    if (!(joypad1L_last & 0x80)) {
+    if (!(joypad1L_last & kJoypadL_A)) {
       link_countdown_for_dash = 0x11;
       want_stop_dash = true;
     } else {
       static const uint8 kDashCtrlsToDir[16] = { 0, 1, 2, 0, 4, 4, 4, 0, 8, 8, 8, 0, 0, 0, 0, 0 };
-      uint8 t = kDashCtrlsToDir[joypad1H_last & 0xf];
+      uint8 t = kDashCtrlsToDir[joypad1H_last & kJoypadH_AnyDir];
       if (t != 0 && t != link_direction_last) {
         link_direction = link_direction_last = t;
         link_some_direction_bits = t;
@@ -1264,7 +1264,7 @@
       }
     }
   } else {
-    want_stop_dash = (joypad1H_last & 0xf) && (joypad1H_last & 0xf) != kDashTab2[link_direction_facing >> 1];
+    want_stop_dash = (joypad1H_last & kJoypadH_AnyDir) && (joypad1H_last & kJoypadH_AnyDir) != kDashTab2[link_direction_facing >> 1];
   }
 
   if (want_stop_dash) {
@@ -1293,7 +1293,7 @@
 
 void LinkState_ExitingDash() {  // 87915e
   CacheCameraPropertiesIfOutdoors();
-  if (joypad1H_last & 0xf || link_countdown_for_dash >= 16) {
+  if (joypad1H_last & kJoypadH_AnyDir || link_countdown_for_dash >= 16) {
     link_countdown_for_dash = 0;
     link_speed_setting = 0;
     link_player_handler_state = kPlayerState_Ground;
@@ -1393,15 +1393,15 @@
     // can levitate across chasms.
     // Fix this by ensuring that the dash button is held down before proceeding to the dash state.
     if (link_countdown_for_dash &&
-        (!(enhanced_features0 & kFeatures0_MiscBugFixes) || (joypad1L_last & 0x80))) {
+        (!(enhanced_features0 & kFeatures0_MiscBugFixes) || (joypad1L_last & kJoypadL_A))) {
       LinkState_Dashing();
       return;
     }
-    if (joypad1H_last & 0xf && !(joypad1H_last & 0xf & link_direction)) {
+    if (joypad1H_last & kJoypadH_AnyDir && !(joypad1H_last & kJoypadH_AnyDir & link_direction)) {
       Link_CancelDash();
 aux_state:
       if (link_auxiliary_state != 1)
-        link_direction = joypad1H_last & 0xF;
+        link_direction = joypad1H_last & kJoypadH_AnyDir;
     }
   }
   TileDetect_MainHandler(4);
@@ -1674,7 +1674,7 @@
 
   if (!link_swim_hard_stroke) {
     uint8 t;
-    if (!(swimcoll_var7[0] | swimcoll_var7[1]) || (t = ((filtered_joypad_L & 0x80) | filtered_joypad_H) & 0xc0) == 0) {
+    if (!(swimcoll_var7[0] | swimcoll_var7[1]) || (t = ((filtered_joypad_L & kJoypadL_A) | filtered_joypad_H) & 0xc0) == 0) {
       Link_HandleSwimMovements();
       return;
     }
@@ -1698,7 +1698,7 @@
 void Link_HandleSwimMovements() {  // 879715
   uint8 t;
 
-  if (!(t = force_move_any_direction & 0xf) && !(t = joypad1H_last & 0xf)) {
+  if (!(t = force_move_any_direction & 0xf) && !(t = joypad1H_last & kJoypadH_AnyDir)) {
     link_y_vel = link_x_vel = 0;
     Link_FlagMaxAccels();
     if (link_flag_moving) {
@@ -1755,7 +1755,7 @@
 }
 
 void Link_SetMomentum() {  // 8797c7
-  uint8 joy = joypad1H_last & 0xf;
+  uint8 joy = joypad1H_last & kJoypadH_AnyDir;
   uint8 mask = 12, bit = 8;
   for (int i = 0; i < 2; i++, mask >>= 2, bit >>= 2) {
     if (joy & mask) {
@@ -1987,14 +1987,14 @@
   uint8 old_down = joypad1H_last, old_pressed = filtered_joypad_H, old_bottle = link_item_bottle_index;
   if ((link_item_in_hand | link_position_mode) == 0) {
     // Is X held down?
-    if (joypad1L_last & 0x40 && !(old_down & 0x40) && hud_cur_item_x != 0) {
+    if (joypad1L_last & kJoypadL_X && !(old_down & kJoypadH_Y) && hud_cur_item_x != 0) {
       
       if (hud_cur_item_x >= kHudItem_Bottle1)
         link_item_bottle_index = hud_cur_item_x - kHudItem_Bottle1 + 1;
       item = Hud_LookupInventoryItem(hud_cur_item_x);
       // Pretend it's actually Y that's down
-      joypad1H_last = old_down | 0x40;
-      filtered_joypad_H = old_pressed | filtered_joypad_L & 0x40;
+      joypad1H_last = old_down | kJoypadH_Y;
+      filtered_joypad_H = old_pressed | ((filtered_joypad_L & kJoypadL_X) ? kJoypadH_Y : 0);
     }
   }
 
@@ -2165,7 +2165,7 @@
     link_animation_steps = 0;
   }
 
-  if (!(joypad1H_last & 0x80))
+  if (!(joypad1H_last & kJoypadH_B))
     button_mask_b_y |= 1;
   HaltLinkWhenUsingItems();
   link_direction &= ~0xf;
@@ -2180,7 +2180,7 @@
         AncillaAdd_SwordSwingSparkle(0x26, 4);
       if (link_sword_type != 0 && link_sword_type != 0xff)
         TileDetect_MainHandler(link_sword_type == 1 ? 1 : 6);
-    } else if (button_b_frames >= 4 && (button_mask_b_y & 1) && (joypad1H_last & 0x80)) {
+    } else if (button_b_frames >= 4 && (button_mask_b_y & 1) && (joypad1H_last & kJoypadH_B)) {
       button_mask_b_y &= ~1;
       HandleSwordSfxAndBeam();
       return;
@@ -2190,7 +2190,7 @@
 }
 
 void HandleSwordControls() {  // 879d72
-  if (joypad1H_last & 0x80) {
+  if (joypad1H_last & kJoypadH_B) {
     Player_Sword_SpinAttackJerks_HoldDown();
   } else {
     if (link_spin_attack_step_counter < 48) {
@@ -2306,7 +2306,7 @@
   if (link_item_in_hand & 0x10)
     return;
   if (!(button_mask_b_y & 0x40)) {
-    if (is_standing_in_doorway || !(filtered_joypad_H & 0x40))
+    if (is_standing_in_doorway || !(filtered_joypad_H & kJoypadH_Y))
       return;
     button_mask_b_y |= 0x40;
     link_delay_timer_spin_attack = kHammerAnimDelays[0];
@@ -2402,7 +2402,7 @@
     }
 
     if (!s0) {
-      link_direction_last = joypad1H_last & 0xf;
+      link_direction_last = joypad1H_last & kJoypadH_AnyDir;
     } else {
       link_cant_change_direction |= 1;
     }
@@ -2890,7 +2890,7 @@
     state_for_spin_attack = 0;
     step_counter_for_spin_attack = 0;
     if (link_player_handler_state != kPlayerState_SpinAttackMotion) {
-      button_mask_b_y = (button_b_frames) ? (joypad1H_last & 0x80) : 0; // wtf, it's zero,
+      button_mask_b_y = (button_b_frames) ? (joypad1H_last & kJoypadH_B) : 0; // wtf, it's zero,
     }
     link_player_handler_state = kPlayerState_Ground;
   } else {
@@ -3247,7 +3247,7 @@
     }
     if (sign8(--link_bunny_transform_timer)) {
       link_bunny_transform_timer = 0;
-      if (filtered_joypad_H & 0x40)
+      if (filtered_joypad_H & kJoypadH_Y)
         Link_ForceUnequipCape();
     }
   }
@@ -3435,7 +3435,7 @@
 }
 
 bool CheckYButtonPress() {  // 87b073
-  if (button_mask_b_y & 0x40 || link_incapacitated_timer || !(filtered_joypad_H & 0x40))
+  if (button_mask_b_y & 0x40 || link_incapacitated_timer || !(filtered_joypad_H & kJoypadH_Y))
     return false;
   button_mask_b_y |= 0x40;
   return true;
@@ -3500,7 +3500,7 @@
 
       flag_is_sprite_to_pick_up = 1;
       Sprite_SpawnThrowableTerrain(i, pt.x, pt.y);
-      filtered_joypad_L &= ~0x80;
+      filtered_joypad_L &= ~kJoypadL_A;
       player_handler_timer = 0;
     }
   } else {
@@ -3563,7 +3563,7 @@
           link_player_handler_state = 24;
           flag_is_sprite_to_pick_up = 1;
           Sprite_SpawnThrowableTerrain((what & 0xf) + 1, pt.x, pt.y);
-          filtered_joypad_L &= ~0x80;
+          filtered_joypad_L &= ~kJoypadL_A;
         }
         return;
       }
@@ -3632,7 +3632,7 @@
     some_animation_timer = kGrabWall_AnimTimer[link_var30d];
   }
 
-  if (!(joypad1L_last & 0x80)) {
+  if (!(joypad1L_last & kJoypadL_A)) {
     link_var30d = 0;
     some_animation_timer_steps = 0;
     link_grabbing_wall = 0;
@@ -3667,7 +3667,7 @@
   some_animation_timer_steps = kGrabWall_AnimSteps[link_var30d];
   some_animation_timer = kGrabWall_AnimTimer[link_var30d];
 skip_set:
-  if (!(joypad1L_last & 0x80)) {
+  if (!(joypad1L_last & kJoypadL_A)) {
     link_speed_setting = 0;
     link_is_near_moveable_statue = 0;
     link_var30d = 0;
@@ -3704,7 +3704,7 @@
 
   if (link_grabbing_wall) {
     if (!button_mask_b_y) {
-      if (!(joypad1L_last & 0x80)) {
+      if (!(joypad1L_last & kJoypadL_A)) {
         link_grabbing_wall = 0;
         link_var30d = 0;
         some_animation_timer = 2;
@@ -3714,7 +3714,7 @@
         LinkState_Default();
         return;
       }
-      if (!(joypad1H_last & 4))
+      if (!(joypad1H_last & kJoypadH_Down))
         goto out;
       button_mask_b_y = 4;
       Ancilla_Sfx2_Near(0x22);
@@ -3745,7 +3745,7 @@
     return;
   }
   if (link_var30d == 9) {
-    if (!(filtered_joypad_H & 0xf))
+    if (!(filtered_joypad_H & kJoypadH_AnyDir))
       goto out2;
     link_player_handler_state = kPlayerState_Ground;
     LinkState_Default();
@@ -3807,7 +3807,7 @@
 }
 
 bool Link_CheckNewAPress() {  // 87b5c0
-  if (bitfield_for_a_button & 0x80 || link_incapacitated_timer || !(filtered_joypad_L & 0x80))
+  if (bitfield_for_a_button & 0x80 || link_incapacitated_timer || !(filtered_joypad_L & kJoypadL_A))
     return false;
   bitfield_for_a_button |= 0x80;
   return true;
@@ -3814,7 +3814,7 @@
 }
 
 bool Link_HandleToss() {  // 87b5d6
-  if (!(bitfield_for_a_button & 0x80) || !(filtered_joypad_L & 0x80) || (link_picking_throw_state & 1))
+  if (!(bitfield_for_a_button & 0x80) || !(filtered_joypad_L & kJoypadL_A) || (link_picking_throw_state & 1))
     return false;
   link_var30d = 0;
   link_var30e = 0;
@@ -5967,7 +5967,7 @@
   if (link_speed_setting == 6) {
     x += 4;
   } else if (link_flag_moving) {
-    if (!(joypad1H_last & 0xf)) {
+    if (!(joypad1H_last & kJoypadH_AnyDir)) {
       link_animation_steps = 0;
       return;
     }
@@ -6360,7 +6360,7 @@
   if (!ancilla_H[k]) {
     if (link_auxiliary_state || (link_state_bits & 1) || ancilla_z[k] != 0 && ancilla_z[k] != 0xff || ancilla_K[k] || ancilla_L[k])
       return;
-    if (!(joypad1H_last & 0xf)) {
+    if (!(joypad1H_last & kJoypadH_AnyDir)) {
       ancilla_arr3[k] = 0;
       bitmask_of_dragstate = 0;
       ancilla_A[k] = 255;
@@ -6368,11 +6368,11 @@
         link_speed_setting = 0;
         return;
       }
-    } else if ((joypad1H_last & 0xf) == ancilla_arr3[k]) {
+    } else if ((joypad1H_last & kJoypadH_AnyDir) == ancilla_arr3[k]) {
       if (link_speed_setting == 18)
         bitmask_of_dragstate |= 0x81;
     } else {
-      ancilla_arr3[k] = (joypad1H_last & 0xf);
+      ancilla_arr3[k] = (joypad1H_last & kJoypadH_AnyDir);
       link_speed_setting = 0;
     }
 
@@ -6384,7 +6384,7 @@
       uint8 t;
       ancilla_x_vel[k] = 0;
       ancilla_y_vel[k] = 0;
-      ancilla_arr3[k] = t = joypad1H_last & 15;
+      ancilla_arr3[k] = t = joypad1H_last & kJoypadH_AnyDir;
       if (t & 3) {
         ancilla_x_vel[k] = t & 1 ? 16 : -16;
         ancilla_dir[k] = t & 1 ? 3 : 2;
--- a/zelda_rtl.h
+++ b/zelda_rtl.h
@@ -70,4 +70,24 @@
 
 void ZeldaSetupEmuCallbacks(uint8 *emu_ram, ZeldaRunFrameFunc *func, ZeldaSyncAllFunc *sync_all);
 
+// Button definitions, zelda splits them in separate 8-bit high/low
+enum {
+  kJoypadL_A = 0x80,
+  kJoypadL_X = 0x40,
+  kJoypadL_L = 0x20,
+  kJoypadL_R = 0x10,
+
+  kJoypadH_B = 0x80,
+  kJoypadH_Y = 0x40,
+  kJoypadH_Select = 0x20,
+  kJoypadH_Start = 0x10,
+
+  kJoypadH_Up = 0x8,
+  kJoypadH_Down = 0x4,
+  kJoypadH_Left = 0x2,
+  kJoypadH_Right = 0x1,
+
+  kJoypadH_AnyDir = 0xf,
+};
+
 #endif  // ZELDA3_ZELDA_RTL_H_