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
|
#include <libultraship/bridge/consolevariablebridge.h>
#include "2s2h/GameInteractor/GameInteractor.h"
#include "2s2h/ShipInit.hpp"
extern "C" {
#include "macros.h"
#include "variables.h"
#include "functions.h"
#include "z64horse.h"
#include "overlays/actors/ovl_En_Arrow/z_en_arrow.h"
void Player_InitItemAction(PlayState* play, Player* thisx, PlayerItemAction itemAction);
s32 Player_UpperAction_7(Player* thisx, PlayState* play);
s32 Player_UpperAction_8(Player* thisx, PlayState* play);
}
#define CVAR_NAME "gEnhancements.PlayerActions.ArrowCycle"
#define CVAR CVarGetInteger(CVAR_NAME, 0)
// Magic arrow costs based on z_player.c
static const s16 sMagicArrowCosts[] = { 4, 4, 8 };
// Button Flash Effect Configuration
static const s16 BUTTON_FLASH_DURATION = 3;
static const s16 BUTTON_FLASH_COUNT = 3;
static const s16 BUTTON_HIGHLIGHT_ALPHA = 128;
// State Variables
static s16 sButtonFlashTimer = 0;
static s16 sButtonFlashCount = 0;
static s8 sJustCycledFrames = 0;
// Arrow Type Definitions
static const PlayerItemAction ARROW_NORMAL = PLAYER_IA_BOW;
static const PlayerItemAction ARROW_FIRE = PLAYER_IA_BOW_FIRE;
static const PlayerItemAction ARROW_ICE = PLAYER_IA_BOW_ICE;
static const PlayerItemAction ARROW_LIGHT = PLAYER_IA_BOW_LIGHT;
static const PlayerItemAction sArrowCycleOrder[] = {
ARROW_NORMAL,
ARROW_FIRE,
ARROW_ICE,
ARROW_LIGHT,
};
// Utility Functions
static bool IsHoldingBow(Player* player) {
return player->heldItemAction >= ARROW_NORMAL && player->heldItemAction <= ARROW_LIGHT;
}
static bool IsHoldingMagicBow(Player* player) {
return player->heldItemAction >= ARROW_FIRE && player->heldItemAction <= ARROW_LIGHT;
}
static bool IsAimingBow(Player* player) {
return IsHoldingBow(player) && ((player->unk_AA5 == PLAYER_UNKAA5_3) || /* Aiming box in first person */
(player->upperActionFunc == Player_UpperAction_7) /* Arrow pulled back on bow */);
}
static bool HasArrowType(PlayerItemAction arrowType) {
switch (arrowType) {
case ARROW_NORMAL:
return true;
case ARROW_FIRE:
return (INV_CONTENT(ITEM_ARROW_FIRE) == ITEM_ARROW_FIRE);
case ARROW_ICE:
return (INV_CONTENT(ITEM_ARROW_ICE) == ITEM_ARROW_ICE);
case ARROW_LIGHT:
return (INV_CONTENT(ITEM_ARROW_LIGHT) == ITEM_ARROW_LIGHT);
default:
return false;
}
}
static s32 GetBowItemForArrow(PlayerItemAction arrowType) {
switch (arrowType) {
case ARROW_FIRE:
return ITEM_BOW_FIRE;
case ARROW_ICE:
return ITEM_BOW_ICE;
case ARROW_LIGHT:
return ITEM_BOW_LIGHT;
default:
return ITEM_BOW;
}
}
static bool CanCycleArrows() {
Player* player = GET_PLAYER(gPlayState);
// Don't allow cycling during bow minigames in specific scenes
if (gSaveContext.minigameStatus == MINIGAME_STATUS_ACTIVE &&
(gPlayState->sceneId == SCENE_SYATEKI_MIZU || // Town Shooting Gallery
gPlayState->sceneId == SCENE_SYATEKI_MORI || // Swamp Shooting Gallery
gPlayState->sceneId == SCENE_20SICHITAI2)) { // Tourist Center boat cruise
return false;
}
return !gHorseIsMounted && player->rideActor == NULL && INV_CONTENT(SLOT_BOW) == ITEM_BOW &&
(INV_CONTENT(ITEM_ARROW_FIRE) == ITEM_ARROW_FIRE || INV_CONTENT(ITEM_ARROW_ICE) == ITEM_ARROW_ICE ||
INV_CONTENT(ITEM_ARROW_LIGHT) == ITEM_ARROW_LIGHT);
}
// Arrow Cycling Logic
static s8 GetNextArrowType(s8 currentArrowType) {
int currentIndex = 0;
for (int i = 0; i < (int)ARRAY_COUNT(sArrowCycleOrder); i++) {
if (sArrowCycleOrder[i] == currentArrowType) {
currentIndex = i;
break;
}
}
for (int offset = 1; offset <= (int)ARRAY_COUNT(sArrowCycleOrder); offset++) {
int nextIndex = (currentIndex + offset) % ARRAY_COUNT(sArrowCycleOrder);
if (HasArrowType(sArrowCycleOrder[nextIndex])) {
return sArrowCycleOrder[nextIndex];
}
}
return ARROW_NORMAL;
}
// UI Update Functions
static void UpdateButtonAlpha(s16 flashAlpha, bool isButtonBow, s16* buttonAlpha) {
if (isButtonBow) {
*buttonAlpha = flashAlpha;
if (sButtonFlashTimer == 0) {
*buttonAlpha = 255;
}
}
}
static void UpdateFlashEffect(PlayState* play) {
if (sButtonFlashTimer <= 0) {
return;
}
sButtonFlashTimer--;
s16 flashAlpha = (sButtonFlashTimer % 3) ? BUTTON_HIGHLIGHT_ALPHA : 255;
if (sButtonFlashTimer == 0 && sButtonFlashCount < BUTTON_FLASH_COUNT - 1) {
sButtonFlashTimer = BUTTON_FLASH_DURATION;
sButtonFlashCount++;
}
// Update C-buttons
UpdateButtonAlpha(flashAlpha,
(GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_C_LEFT) == ITEM_BOW) ||
(GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_C_LEFT) >= ITEM_BOW_FIRE &&
GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_C_LEFT) <= ITEM_BOW_LIGHT),
&play->interfaceCtx.cLeftAlpha);
UpdateButtonAlpha(flashAlpha,
(GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_C_DOWN) == ITEM_BOW) ||
(GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_C_DOWN) >= ITEM_BOW_FIRE &&
GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_C_DOWN) <= ITEM_BOW_LIGHT),
&play->interfaceCtx.cDownAlpha);
UpdateButtonAlpha(flashAlpha,
(GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_C_RIGHT) == ITEM_BOW) ||
(GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_C_RIGHT) >= ITEM_BOW_FIRE &&
GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_C_RIGHT) <= ITEM_BOW_LIGHT),
&play->interfaceCtx.cRightAlpha);
// Update D-pad
UpdateButtonAlpha(flashAlpha,
(DPAD_GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_D_RIGHT) == ITEM_BOW) ||
(DPAD_GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_D_RIGHT) >= ITEM_BOW_FIRE &&
DPAD_GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_D_RIGHT) <= ITEM_BOW_LIGHT),
&play->interfaceCtx.shipInterface.dpad.dRightAlpha);
UpdateButtonAlpha(flashAlpha,
(DPAD_GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_D_LEFT) == ITEM_BOW) ||
(DPAD_GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_D_LEFT) >= ITEM_BOW_FIRE &&
DPAD_GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_D_LEFT) <= ITEM_BOW_LIGHT),
&play->interfaceCtx.shipInterface.dpad.dLeftAlpha);
UpdateButtonAlpha(flashAlpha,
(DPAD_GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_D_DOWN) == ITEM_BOW) ||
(DPAD_GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_D_DOWN) >= ITEM_BOW_FIRE &&
DPAD_GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_D_DOWN) <= ITEM_BOW_LIGHT),
&play->interfaceCtx.shipInterface.dpad.dDownAlpha);
UpdateButtonAlpha(flashAlpha,
(DPAD_GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_D_UP) == ITEM_BOW) ||
(DPAD_GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_D_UP) >= ITEM_BOW_FIRE &&
DPAD_GET_CUR_FORM_BTN_ITEM(EQUIP_SLOT_D_UP) <= ITEM_BOW_LIGHT),
&play->interfaceCtx.shipInterface.dpad.dUpAlpha);
}
static void UpdateEquippedBow(PlayState* play, s8 arrowType) {
s32 bowItem = GetBowItemForArrow(static_cast<PlayerItemAction>(arrowType));
// Update C-buttons
for (s32 i = EQUIP_SLOT_C_LEFT; i <= EQUIP_SLOT_C_RIGHT; i++) {
if ((BUTTON_ITEM_EQUIP(0, i) == ITEM_BOW) ||
(BUTTON_ITEM_EQUIP(0, i) >= ITEM_BOW_FIRE && BUTTON_ITEM_EQUIP(0, i) <= ITEM_BOW_LIGHT)) {
BUTTON_ITEM_EQUIP(0, i) = bowItem;
C_SLOT_EQUIP(0, i) = SLOT_BOW;
Interface_LoadItemIcon(play, i);
gSaveContext.buttonStatus[i] = BTN_ENABLED;
sButtonFlashTimer = BUTTON_FLASH_DURATION;
sButtonFlashCount = 0;
}
}
// Update D-pad
for (s32 i = EQUIP_SLOT_D_RIGHT; i <= EQUIP_SLOT_D_UP; i++) {
if ((DPAD_BUTTON_ITEM_EQUIP(0, i) == ITEM_BOW) ||
(DPAD_BUTTON_ITEM_EQUIP(0, i) >= ITEM_BOW_FIRE && DPAD_BUTTON_ITEM_EQUIP(0, i) <= ITEM_BOW_LIGHT)) {
DPAD_BUTTON_ITEM_EQUIP(0, i) = bowItem;
DPAD_SLOT_EQUIP(0, i) = SLOT_BOW;
Interface_Dpad_LoadItemIcon(play, i);
gSaveContext.shipSaveContext.dpad.status[i] = BTN_ENABLED;
sButtonFlashTimer = BUTTON_FLASH_DURATION;
sButtonFlashCount = 0;
}
}
UpdateFlashEffect(play);
}
// Core Arrow Cycling Function
static void CycleToNextArrow(PlayState* play, Player* player) {
s8 nextArrow = GetNextArrowType(player->heldItemAction);
if (player->heldActor != NULL && player->heldActor->id == ACTOR_EN_ARROW) {
EnArrow* arrow = (EnArrow*)player->heldActor;
if (arrow->actor.child != NULL) {
Actor_Kill(arrow->actor.child);
}
Actor_Kill(&arrow->actor);
}
Player_InitItemAction(play, player, static_cast<PlayerItemAction>(nextArrow));
UpdateEquippedBow(play, nextArrow);
Audio_PlaySfx(NA_SE_PL_CHANGE_ARMS);
}
void ArrowCycleMain() {
if (sJustCycledFrames > 0) {
sJustCycledFrames--;
}
if (gPlayState == nullptr || !CanCycleArrows()) {
return;
}
UpdateFlashEffect(gPlayState);
Player* player = GET_PLAYER(gPlayState);
Input* input = CONTROLLER1(&gPlayState->state);
// Block camera changes when cycling arrows while drawing the bow
if ((player->stateFlags3 & PLAYER_STATE3_40) && player->unk_ACE == 0) {
return;
}
if (IsAimingBow(player) && CHECK_BTN_ANY(input->press.button, BTN_R) && player->heldActor != NULL &&
player->heldActor->id == ACTOR_EN_ARROW) {
if (IsHoldingMagicBow(player) && gSaveContext.magicState != MAGIC_STATE_IDLE && player->heldActor == NULL) {
Audio_PlaySfx(NA_SE_SY_ERROR);
return;
}
EnArrow* heldArrow = (EnArrow*)player->heldActor;
// If the held arrow itself is magical, then we should "restore" the consumed magic upon cycling
if (ARROW_IS_MAGICAL(heldArrow->actor.params)) {
Magic_Add(gPlayState, sMagicArrowCosts[ARROW_GET_MAGIC_FROM_TYPE(heldArrow->actor.params)]);
}
CycleToNextArrow(gPlayState, player);
// Track that we just cycled for 2 frames to prevent held R input from triggering the shield action when in
// Z-Target mode as the arrow is respawned (Player_UpperAction_8)
sJustCycledFrames = 2;
}
}
// Registration and Hooks
void RegisterArrowCycle() {
COND_VB_SHOULD(VB_SHIELD_FROM_BUTTON_HOLD, CVAR, {
if (CanCycleArrows()) {
Player* player = GET_PLAYER(gPlayState);
Input* input = CONTROLLER1(&gPlayState->state);
// Suppress Shield input when holding an arrow in Z-Target mode
if (IsHoldingBow(player) && sJustCycledFrames > 0 && CHECK_BTN_ANY(input->cur.button, BTN_R)) {
*should = false;
}
}
});
COND_VB_SHOULD(VB_EXIT_FIRST_PERSON_MODE_FROM_BUTTON, CVAR, {
if (CanCycleArrows()) {
Player* player = GET_PLAYER(gPlayState);
Input* input = CONTROLLER1(&gPlayState->state);
// Suppress Shield input first person cancel when aiming the bow
if (IsAimingBow(player) && CHECK_BTN_ANY(input->cur.button, BTN_R) && player->heldActor != NULL &&
player->heldActor->id == ACTOR_EN_ARROW) {
*should = false;
}
}
});
COND_ID_HOOK(OnActorUpdate, ACTOR_PLAYER, CVAR, [](Actor* actor) { ArrowCycleMain(); });
}
static RegisterShipInitFunc initFunc(RegisterArrowCycle, { CVAR_NAME });
|