forked from rh-hideout/pokeemerald-expansion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbattle_controller_wally.c
442 lines (396 loc) · 16.6 KB
/
battle_controller_wally.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
#include "global.h"
#include "battle.h"
#include "battle_anim.h"
#include "battle_controllers.h"
#include "battle_interface.h"
#include "battle_message.h"
#include "battle_setup.h"
#include "battle_tv.h"
#include "bg.h"
#include "data.h"
#include "item.h"
#include "item_menu.h"
#include "link.h"
#include "main.h"
#include "m4a.h"
#include "palette.h"
#include "party_menu.h"
#include "pokeball.h"
#include "pokemon.h"
#include "random.h"
#include "reshow_battle_screen.h"
#include "sound.h"
#include "string_util.h"
#include "task.h"
#include "text.h"
#include "util.h"
#include "window.h"
#include "constants/battle_anim.h"
#include "constants/items.h"
#include "constants/moves.h"
#include "constants/songs.h"
#include "constants/trainers.h"
#include "constants/rgb.h"
static void WallyHandleDrawTrainerPic(u32 battler);
static void WallyHandleTrainerSlide(u32 battler);
static void WallyHandleSuccessBallThrowAnim(u32 battler);
static void WallyHandleBallThrowAnim(u32 battler);
static void WallyHandleMoveAnimation(u32 battler);
static void WallyHandlePrintString(u32 battler);
static void WallyHandlePrintSelectionString(u32 battler);
static void WallyHandleChooseAction(u32 battler);
static void WallyHandleChooseMove(u32 battler);
static void WallyHandleChooseItem(u32 battler);
static void WallyHandleHealthBarUpdate(u32 battler);
static void WallyHandlePlaySE(u32 battler);
static void WallyHandleFaintingCry(u32 battler);
static void WallyHandleIntroTrainerBallThrow(u32 battler);
static void WallyHandleDrawPartyStatusSummary(u32 battler);
static void WallyHandleBattleAnimation(u32 battler);
static void WallyHandleEndLinkBattle(u32 battler);
static void WallyBufferRunCommand(u32 battler);
static void WallyBufferExecCompleted(u32 battler);
static void CompleteOnChosenItem(u32 battler);
static void Intro_WaitForShinyAnimAndHealthbox(u32 battler);
static void (*const sWallyBufferCommands[CONTROLLER_CMDS_COUNT])(u32 battler) =
{
[CONTROLLER_GETMONDATA] = BtlController_HandleGetMonData,
[CONTROLLER_GETRAWMONDATA] = BtlController_HandleGetRawMonData,
[CONTROLLER_SETMONDATA] = BtlController_HandleSetMonData,
[CONTROLLER_SETRAWMONDATA] = BtlController_Empty,
[CONTROLLER_LOADMONSPRITE] = BtlController_Empty,
[CONTROLLER_SWITCHINANIM] = BtlController_Empty,
[CONTROLLER_RETURNMONTOBALL] = BtlController_HandleReturnMonToBall,
[CONTROLLER_DRAWTRAINERPIC] = WallyHandleDrawTrainerPic,
[CONTROLLER_TRAINERSLIDE] = WallyHandleTrainerSlide,
[CONTROLLER_TRAINERSLIDEBACK] = BtlController_Empty,
[CONTROLLER_FAINTANIMATION] = BtlController_Empty,
[CONTROLLER_PALETTEFADE] = BtlController_Empty,
[CONTROLLER_SUCCESSBALLTHROWANIM] = WallyHandleSuccessBallThrowAnim,
[CONTROLLER_BALLTHROWANIM] = WallyHandleBallThrowAnim,
[CONTROLLER_PAUSE] = BtlController_Empty,
[CONTROLLER_MOVEANIMATION] = WallyHandleMoveAnimation,
[CONTROLLER_PRINTSTRING] = WallyHandlePrintString,
[CONTROLLER_PRINTSTRINGPLAYERONLY] = WallyHandlePrintSelectionString,
[CONTROLLER_CHOOSEACTION] = WallyHandleChooseAction,
[CONTROLLER_YESNOBOX] = BtlController_Empty,
[CONTROLLER_CHOOSEMOVE] = WallyHandleChooseMove,
[CONTROLLER_OPENBAG] = WallyHandleChooseItem,
[CONTROLLER_CHOOSEPOKEMON] = BtlController_Empty,
[CONTROLLER_23] = BtlController_Empty,
[CONTROLLER_HEALTHBARUPDATE] = WallyHandleHealthBarUpdate,
[CONTROLLER_EXPUPDATE] = BtlController_Empty,
[CONTROLLER_STATUSICONUPDATE] = BtlController_Empty,
[CONTROLLER_STATUSANIMATION] = BtlController_Empty,
[CONTROLLER_STATUSXOR] = BtlController_Empty,
[CONTROLLER_DATATRANSFER] = BtlController_Empty,
[CONTROLLER_DMA3TRANSFER] = BtlController_Empty,
[CONTROLLER_PLAYBGM] = BtlController_Empty,
[CONTROLLER_32] = BtlController_Empty,
[CONTROLLER_TWORETURNVALUES] = BtlController_Empty,
[CONTROLLER_CHOSENMONRETURNVALUE] = BtlController_Empty,
[CONTROLLER_ONERETURNVALUE] = BtlController_Empty,
[CONTROLLER_ONERETURNVALUE_DUPLICATE] = BtlController_Empty,
[CONTROLLER_HITANIMATION] = BtlController_HandleHitAnimation,
[CONTROLLER_CANTSWITCH] = BtlController_Empty,
[CONTROLLER_PLAYSE] = WallyHandlePlaySE,
[CONTROLLER_PLAYFANFAREORBGM] = BtlController_HandlePlayFanfareOrBGM,
[CONTROLLER_FAINTINGCRY] = WallyHandleFaintingCry,
[CONTROLLER_INTROSLIDE] = BtlController_HandleIntroSlide,
[CONTROLLER_INTROTRAINERBALLTHROW] = WallyHandleIntroTrainerBallThrow,
[CONTROLLER_DRAWPARTYSTATUSSUMMARY] = WallyHandleDrawPartyStatusSummary,
[CONTROLLER_HIDEPARTYSTATUSSUMMARY] = BtlController_Empty,
[CONTROLLER_ENDBOUNCE] = BtlController_Empty,
[CONTROLLER_SPRITEINVISIBILITY] = BtlController_Empty,
[CONTROLLER_BATTLEANIMATION] = WallyHandleBattleAnimation,
[CONTROLLER_LINKSTANDBYMSG] = BtlController_Empty,
[CONTROLLER_RESETACTIONMOVESELECTION] = BtlController_Empty,
[CONTROLLER_ENDLINKBATTLE] = WallyHandleEndLinkBattle,
[CONTROLLER_DEBUGMENU] = BtlController_Empty,
[CONTROLLER_TERMINATOR_NOP] = BtlController_TerminatorNop
};
void UNUSED SetControllerToWally(u32 battler)
{
gBattlerControllerEndFuncs[battler] = WallyBufferExecCompleted;
gBattlerControllerFuncs[battler] = WallyBufferRunCommand;
gBattleStruct->wallyBattleState = 0;
gBattleStruct->wallyMovesState = 0;
gBattleStruct->wallyWaitFrames = 0;
gBattleStruct->wallyMoveFrames = 0;
}
static void WallyBufferRunCommand(u32 battler)
{
if (gBattleControllerExecFlags & (1u << battler))
{
if (gBattleResources->bufferA[battler][0] < ARRAY_COUNT(sWallyBufferCommands))
sWallyBufferCommands[gBattleResources->bufferA[battler][0]](battler);
else
WallyBufferExecCompleted(battler);
}
}
static void WallyHandleActions(u32 battler)
{
switch (gBattleStruct->wallyBattleState)
{
case 0:
gBattleStruct->wallyWaitFrames = B_WAIT_TIME_LONG;
gBattleStruct->wallyBattleState++;
case 1:
if (--gBattleStruct->wallyWaitFrames == 0)
{
PlaySE(SE_SELECT);
BtlController_EmitTwoReturnValues(battler, BUFFER_B, B_ACTION_USE_MOVE, 0);
WallyBufferExecCompleted(battler);
gBattleStruct->wallyBattleState++;
gBattleStruct->wallyMovesState = 0;
gBattleStruct->wallyWaitFrames = B_WAIT_TIME_LONG;
}
break;
case 2:
if (--gBattleStruct->wallyWaitFrames == 0)
{
PlaySE(SE_SELECT);
BtlController_EmitTwoReturnValues(battler, BUFFER_B, B_ACTION_USE_MOVE, 0);
WallyBufferExecCompleted(battler);
gBattleStruct->wallyBattleState++;
gBattleStruct->wallyMovesState = 0;
gBattleStruct->wallyWaitFrames = B_WAIT_TIME_LONG;
}
break;
case 3:
if (--gBattleStruct->wallyWaitFrames == 0)
{
BtlController_EmitTwoReturnValues(battler, BUFFER_B, B_ACTION_WALLY_THROW, 0);
WallyBufferExecCompleted(battler);
gBattleStruct->wallyBattleState++;
gBattleStruct->wallyMovesState = 0;
gBattleStruct->wallyWaitFrames = B_WAIT_TIME_LONG;
}
break;
case 4:
if (--gBattleStruct->wallyWaitFrames == 0)
{
PlaySE(SE_SELECT);
ActionSelectionDestroyCursorAt(0);
ActionSelectionCreateCursorAt(1, 0);
gBattleStruct->wallyWaitFrames = B_WAIT_TIME_LONG;
gBattleStruct->wallyBattleState++;
}
break;
case 5:
if (--gBattleStruct->wallyWaitFrames == 0)
{
PlaySE(SE_SELECT);
BtlController_EmitTwoReturnValues(battler, BUFFER_B, B_ACTION_USE_ITEM, 0);
WallyBufferExecCompleted(battler);
}
break;
}
}
static void OpenBagAfterPaletteFade(u32 battler)
{
if (!gPaletteFade.active)
{
gBattlerControllerFuncs[battler] = CompleteOnChosenItem;
ReshowBattleScreenDummy();
FreeAllWindowBuffers();
DoWallyTutorialBagMenu();
}
}
static void CompleteOnChosenItem(u32 battler)
{
if (gMain.callback2 == BattleMainCB2 && !gPaletteFade.active)
{
BtlController_EmitOneReturnValue(battler, BUFFER_B, gSpecialVar_ItemId);
WallyBufferExecCompleted(battler);
}
}
static void Intro_TryShinyAnimShowHealthbox(u32 battler)
{
if (!gBattleSpritesDataPtr->healthBoxesData[battler].triedShinyMonAnim
&& !gBattleSpritesDataPtr->healthBoxesData[battler].ballAnimActive)
TryShinyAnimation(battler, &gPlayerParty[gBattlerPartyIndexes[battler]]);
if (!gBattleSpritesDataPtr->healthBoxesData[BATTLE_PARTNER(battler)].triedShinyMonAnim
&& !gBattleSpritesDataPtr->healthBoxesData[BATTLE_PARTNER(battler)].ballAnimActive)
TryShinyAnimation(BATTLE_PARTNER(battler), &gPlayerParty[gBattlerPartyIndexes[BATTLE_PARTNER(battler)]]);
if (!gBattleSpritesDataPtr->healthBoxesData[battler].ballAnimActive
&& !gBattleSpritesDataPtr->healthBoxesData[BATTLE_PARTNER(battler)].ballAnimActive
&& gSprites[gBattleControllerData[battler]].callback == SpriteCallbackDummy
&& gSprites[gBattlerSpriteIds[battler]].callback == SpriteCallbackDummy)
{
if (IsDoubleBattle() && !(gBattleTypeFlags & BATTLE_TYPE_MULTI))
{
DestroySprite(&gSprites[gBattleControllerData[BATTLE_PARTNER(battler)]]);
UpdateHealthboxAttribute(gHealthboxSpriteIds[BATTLE_PARTNER(battler)], &gPlayerParty[gBattlerPartyIndexes[BATTLE_PARTNER(battler)]], HEALTHBOX_ALL);
StartHealthboxSlideIn(BATTLE_PARTNER(battler));
SetHealthboxSpriteVisible(gHealthboxSpriteIds[BATTLE_PARTNER(battler)]);
}
DestroySprite(&gSprites[gBattleControllerData[battler]]);
UpdateHealthboxAttribute(gHealthboxSpriteIds[battler], &gPlayerParty[gBattlerPartyIndexes[battler]], HEALTHBOX_ALL);
StartHealthboxSlideIn(battler);
SetHealthboxSpriteVisible(gHealthboxSpriteIds[battler]);
gBattleSpritesDataPtr->animationData->introAnimActive = FALSE;
gBattlerControllerFuncs[battler] = Intro_WaitForShinyAnimAndHealthbox;
}
}
static void Intro_WaitForShinyAnimAndHealthbox(u32 battler)
{
bool32 healthboxAnimDone = FALSE;
if (gSprites[gHealthboxSpriteIds[battler]].callback == SpriteCallbackDummy)
healthboxAnimDone = TRUE;
if (healthboxAnimDone && gBattleSpritesDataPtr->healthBoxesData[battler].finishedShinyMonAnim
&& gBattleSpritesDataPtr->healthBoxesData[BATTLE_PARTNER(battler)].finishedShinyMonAnim)
{
gBattleSpritesDataPtr->healthBoxesData[battler].triedShinyMonAnim = FALSE;
gBattleSpritesDataPtr->healthBoxesData[battler].finishedShinyMonAnim = FALSE;
gBattleSpritesDataPtr->healthBoxesData[BATTLE_PARTNER(battler)].triedShinyMonAnim = FALSE;
gBattleSpritesDataPtr->healthBoxesData[BATTLE_PARTNER(battler)].finishedShinyMonAnim = FALSE;
FreeSpriteTilesByTag(ANIM_TAG_GOLD_STARS);
FreeSpritePaletteByTag(ANIM_TAG_GOLD_STARS);
CreateTask(Task_PlayerController_RestoreBgmAfterCry, 10);
HandleLowHpMusicChange(&gPlayerParty[gBattlerPartyIndexes[battler]], battler);
WallyBufferExecCompleted(battler);
}
}
static void WallyBufferExecCompleted(u32 battler)
{
gBattlerControllerFuncs[battler] = WallyBufferRunCommand;
if (gBattleTypeFlags & BATTLE_TYPE_LINK)
{
u8 playerId = GetMultiplayerId();
PrepareBufferDataTransferLink(battler, 2, 4, &playerId);
gBattleResources->bufferA[battler][0] = CONTROLLER_TERMINATOR_NOP;
}
else
{
gBattleControllerExecFlags &= ~(1u << battler);
}
}
#define sSpeedX data[0]
static void WallyHandleDrawTrainerPic(u32 battler)
{
BtlController_HandleDrawTrainerPic(battler, TRAINER_BACK_PIC_WALLY, FALSE,
80, 80 + 4 * (8 - gTrainerBacksprites[TRAINER_BACK_PIC_WALLY].coordinates.size),
30);
}
static void WallyHandleTrainerSlide(u32 battler)
{
BtlController_HandleTrainerSlide(battler, TRAINER_BACK_PIC_WALLY);
}
#undef sSpeedX
static void WallyHandleSuccessBallThrowAnim(u32 battler)
{
BtlController_HandleSuccessBallThrowAnim(battler, GetBattlerAtPosition(B_POSITION_OPPONENT_LEFT), B_ANIM_BALL_THROW_WITH_TRAINER, FALSE);
}
static void WallyHandleBallThrowAnim(u32 battler)
{
BtlController_HandleBallThrowAnim(battler, GetBattlerAtPosition(B_POSITION_OPPONENT_LEFT), B_ANIM_BALL_THROW_WITH_TRAINER, FALSE);
}
static void WallyHandleMoveAnimation(u32 battler)
{
BtlController_HandleMoveAnimation(battler, FALSE);
}
static void WallyHandlePrintString(u32 battler)
{
BtlController_HandlePrintString(battler, FALSE, FALSE);
}
static void WallyHandlePrintSelectionString(u32 battler)
{
if (GetBattlerSide(battler) == B_SIDE_PLAYER)
WallyHandlePrintString(battler);
else
WallyBufferExecCompleted(battler);
}
static void HandleChooseActionAfterDma3(u32 battler)
{
if (!IsDma3ManagerBusyWithBgCopy())
{
gBattle_BG0_X = 0;
gBattle_BG0_Y = DISPLAY_HEIGHT;
gBattlerControllerFuncs[battler] = WallyHandleActions;
}
}
static void WallyHandleChooseAction(u32 battler)
{
s32 i;
gBattlerControllerFuncs[battler] = HandleChooseActionAfterDma3;
BattlePutTextOnWindow(gText_BattleMenu, B_WIN_ACTION_MENU);
for (i = 0; i < 4; i++)
ActionSelectionDestroyCursorAt(i);
ActionSelectionCreateCursorAt(gActionSelectionCursor[battler], 0);
BattleStringExpandPlaceholdersToDisplayedString(gText_WhatWillWallyDo);
BattlePutTextOnWindow(gDisplayedStringBattle, B_WIN_ACTION_PROMPT);
}
static void WallyHandleChooseMove(u32 battler)
{
switch (gBattleStruct->wallyMovesState)
{
case 0:
InitMoveSelectionsVarsAndStrings(battler);
gBattleStruct->wallyMovesState++;
gBattleStruct->wallyMoveFrames = 80;
break;
case 1:
if (!IsDma3ManagerBusyWithBgCopy())
{
gBattle_BG0_X = 0;
gBattle_BG0_Y = DISPLAY_HEIGHT * 2;
gBattleStruct->wallyMovesState++;
}
break;
case 2:
if (--gBattleStruct->wallyMoveFrames == 0)
{
PlaySE(SE_SELECT);
BtlController_EmitTwoReturnValues(battler, BUFFER_B, 10, 0x100);
WallyBufferExecCompleted(battler);
}
break;
}
}
static void WallyHandleChooseItem(u32 battler)
{
BeginNormalPaletteFade(PALETTES_ALL, 0, 0, 0x10, RGB_BLACK);
gBattlerControllerFuncs[battler] = OpenBagAfterPaletteFade;
gBattlerInMenuId = battler;
}
static void WallyHandleHealthBarUpdate(u32 battler)
{
BtlController_HandleHealthBarUpdate(battler, TRUE);
}
// For some reason Wally's SE don't take side into account and pan is always the same. Possibly a bug
static void WallyHandlePlaySE(u32 battler)
{
PlaySE(gBattleResources->bufferA[battler][1] | (gBattleResources->bufferA[battler][2] << 8));
WallyBufferExecCompleted(battler);
}
// All of the other controllers use CRY_MODE_FAINT.
// Wally's Pokémon during the tutorial is never intended to faint, so that's probably why it's different here.
static void WallyHandleFaintingCry(u32 battler)
{
u16 species = GetMonData(&gPlayerParty[gBattlerPartyIndexes[battler]], MON_DATA_SPECIES);
PlayCry_Normal(species, 25);
WallyBufferExecCompleted(battler);
}
static void WallyHandleIntroTrainerBallThrow(u32 battler)
{
const u32 *trainerPal = gTrainerBacksprites[TRAINER_BACK_PIC_WALLY].palette.data;
BtlController_HandleIntroTrainerBallThrow(battler, 0xD6F8, trainerPal, 31, Intro_TryShinyAnimShowHealthbox);
}
static void WallyHandleDrawPartyStatusSummary(u32 battler)
{
BtlController_HandleDrawPartyStatusSummary(battler, B_SIDE_PLAYER, FALSE);
}
static void WallyHandleBattleAnimation(u32 battler)
{
BtlController_HandleBattleAnimation(battler, TRUE, FALSE);
}
static void WallyHandleEndLinkBattle(u32 battler)
{
gBattleOutcome = gBattleResources->bufferA[battler][1];
FadeOutMapMusic(5);
BeginFastPaletteFade(3);
WallyBufferExecCompleted(battler);
if (!(gBattleTypeFlags & BATTLE_TYPE_IS_MASTER) && gBattleTypeFlags & BATTLE_TYPE_LINK)
gBattlerControllerFuncs[battler] = SetBattleEndCallbacks;
}