pokeemerald/src/union_room_battle.c

234 lines
6.1 KiB
C
Raw Normal View History

2019-04-02 00:42:57 +02:00
#include "global.h"
#include "battle.h"
#include "task.h"
#include "text.h"
#include "main.h"
#include "bg.h"
#include "palette.h"
#include "gpu_regs.h"
#include "malloc.h"
2019-04-02 00:42:57 +02:00
#include "menu.h"
#include "window.h"
2019-04-02 04:30:30 +02:00
#include "text_window.h"
2019-04-02 00:42:57 +02:00
#include "scanline_effect.h"
#include "overworld.h"
#include "strings.h"
2019-04-02 04:30:30 +02:00
#include "party_menu.h"
#include "battle_setup.h"
#include "link.h"
2019-04-02 00:42:57 +02:00
#include "union_room.h"
#include "union_room_battle.h"
2020-06-03 21:28:29 +02:00
#include "constants/rgb.h"
2020-06-03 01:05:59 +02:00
#include "constants/trainers.h"
2019-04-02 00:42:57 +02:00
2020-06-03 01:05:59 +02:00
struct UnionRoomBattle
2019-04-02 04:30:30 +02:00
{
2020-05-30 10:09:21 +02:00
s16 textState;
2019-04-02 04:30:30 +02:00
};
2020-06-03 01:05:59 +02:00
static EWRAM_DATA struct UnionRoomBattle * sBattle = NULL;
2019-04-02 04:30:30 +02:00
2020-06-03 01:05:59 +02:00
static const struct BgTemplate sBgTemplates[] = {
2019-04-02 04:30:30 +02:00
{
.bg = 0,
.charBaseIndex = 3,
.mapBaseIndex = 31
}
};
2020-06-03 01:05:59 +02:00
static const struct WindowTemplate sWindowTemplates[] = {
2019-04-02 04:30:30 +02:00
{
.bg = 0,
.tilemapLeft = 3,
.tilemapTop = 15,
.width = 24,
.height = 4,
.paletteNum = 0xE,
.baseBlock = 0x014
},
2020-06-03 01:05:59 +02:00
DUMMY_WIN_TEMPLATE
2019-04-02 04:30:30 +02:00
};
2021-04-10 04:39:34 +02:00
static const u8 sTextColors[] = { TEXT_COLOR_WHITE, TEXT_COLOR_DARK_GRAY, TEXT_COLOR_LIGHT_GRAY };
2019-04-02 04:30:30 +02:00
2020-06-03 01:05:59 +02:00
static void CB2_SetUpPartiesAndStartBattle(void)
2019-04-02 04:30:30 +02:00
{
s32 i;
2020-06-03 01:05:59 +02:00
StartUnionRoomBattle(BATTLE_TYPE_LINK | BATTLE_TYPE_TRAINER);
2020-04-09 21:18:53 +02:00
for (i = 0; i < UNION_ROOM_PARTY_SIZE; i++)
2019-04-02 04:30:30 +02:00
{
gEnemyParty[i] = gPlayerParty[gSelectedOrderFromParty[i] - 1];
}
2020-04-09 21:18:53 +02:00
for (i = 0; i < PARTY_SIZE; i++)
2019-04-02 04:30:30 +02:00
{
ZeroMonData(&gPlayerParty[i]);
}
2020-04-09 21:18:53 +02:00
for (i = 0; i < UNION_ROOM_PARTY_SIZE; i++)
2019-04-02 04:30:30 +02:00
{
gPlayerParty[i] = gEnemyParty[i];
}
IncrementGameStat(GAME_STAT_NUM_UNION_ROOM_BATTLES);
CalculatePlayerPartyCount();
2020-06-03 21:28:29 +02:00
gTrainerBattleOpponent_A = TRAINER_UNION_ROOM;
2019-04-02 04:30:30 +02:00
SetMainCallback2(CB2_InitBattle);
}
static void AddTextPrinterForUnionRoomBattle(u8 windowId, const u8 *str, u8 x, u8 y, s32 speed)
2019-04-02 04:30:30 +02:00
{
s32 letterSpacing = 0;
s32 lineSpacing = 1;
2020-06-03 01:05:59 +02:00
FillWindowPixelBuffer(windowId, (sTextColors[0] << 4) | sTextColors[0]);
2021-10-30 22:47:37 +02:00
AddTextPrinterParameterized4(windowId, FONT_NORMAL, x, y, letterSpacing, lineSpacing, sTextColors, speed, str);
2019-04-02 04:30:30 +02:00
}
static bool32 PrintUnionRoomBattleMessage(s16 * state, const u8 *str, s32 speed)
2019-04-02 04:30:30 +02:00
{
switch (*state)
{
case 0:
DrawTextBorderOuter(0, 0x001, 0xD);
2020-06-03 01:05:59 +02:00
AddTextPrinterForUnionRoomBattle(0, str, 0, 1, speed);
2019-04-02 04:30:30 +02:00
PutWindowTilemap(0);
2021-11-03 20:29:18 +01:00
CopyWindowToVram(0, COPYWIN_FULL);
2019-04-02 04:30:30 +02:00
(*state)++;
break;
case 1:
if (!IsTextPrinterActive(0))
{
*state = 0;
return TRUE;
}
break;
}
return FALSE;
}
2020-06-03 01:05:59 +02:00
static void VBlankCB_UnionRoomBattle(void)
2019-04-02 00:42:57 +02:00
{
2019-04-02 04:30:30 +02:00
LoadOam();
ProcessSpriteCopyRequests();
TransferPlttBuffer();
}
2020-05-30 10:09:21 +02:00
void CB2_UnionRoomBattle(void)
2019-04-02 04:30:30 +02:00
{
switch (gMain.state)
{
case 0:
SetGpuReg(REG_OFFSET_DISPCNT, 0x0000);
2020-06-03 01:05:59 +02:00
sBattle = AllocZeroed(sizeof(struct UnionRoomBattle));
2019-04-02 04:30:30 +02:00
ResetSpriteData();
FreeAllSpritePalettes();
ResetTasks();
ResetBgsAndClearDma3BusyFlags(0);
2020-06-03 01:05:59 +02:00
InitBgsFromTemplates(0, sBgTemplates, ARRAY_COUNT(sBgTemplates));
2020-05-14 10:37:09 +02:00
ResetTempTileDataBuffers();
2020-06-03 01:05:59 +02:00
if (!InitWindows(sWindowTemplates))
2019-04-02 04:30:30 +02:00
return;
DeactivateAllTextPrinters();
ClearWindowTilemap(0);
2020-06-03 01:05:59 +02:00
FillWindowPixelBuffer(0, PIXEL_FILL(0));
FillWindowPixelBuffer(0, PIXEL_FILL(1));
2022-09-26 18:22:27 +02:00
FillBgTilemapBufferRect(0, 0, 0, 0, DISPLAY_TILE_WIDTH, DISPLAY_TILE_HEIGHT, 0xF);
2019-04-02 04:30:30 +02:00
LoadUserWindowBorderGfx(0, 1, 0xD0);
LoadUserWindowBorderGfx_(0, 1, 0xD0);
2021-10-26 22:52:23 +02:00
Menu_LoadStdPal();
2020-06-03 01:05:59 +02:00
SetVBlankCallback(VBlankCB_UnionRoomBattle);
2019-04-02 04:30:30 +02:00
gMain.state++;
break;
case 1:
2020-06-03 01:05:59 +02:00
if (PrintUnionRoomBattleMessage(&sBattle->textState, gText_CommStandbyAwaitingOtherPlayer, 0))
2019-04-02 04:30:30 +02:00
{
gMain.state++;
}
break;
case 2:
2021-02-24 17:01:02 +01:00
BeginNormalPaletteFade(PALETTES_ALL, 0, 16, 0, RGB_BLACK);
2019-04-02 04:30:30 +02:00
ShowBg(0);
gMain.state++;
break;
case 3:
if (!UpdatePaletteFade())
{
2020-06-03 21:28:29 +02:00
memset(gBlockSendBuffer, 0, 0x20);
2019-04-02 04:30:30 +02:00
if (gSelectedOrderFromParty[0] == -gSelectedOrderFromParty[1])
{
2020-06-03 01:05:59 +02:00
gBlockSendBuffer[0] = ACTIVITY_DECLINE | IN_UNION_ROOM;
2019-04-02 04:30:30 +02:00
}
else
{
2020-06-03 01:05:59 +02:00
gBlockSendBuffer[0] = ACTIVITY_ACCEPT | IN_UNION_ROOM;
2019-04-02 04:30:30 +02:00
}
2020-06-03 21:28:29 +02:00
SendBlock(0, gBlockSendBuffer, 0x20);
2019-04-02 04:30:30 +02:00
gMain.state++;
}
break;
case 4:
if (GetBlockReceivedStatus() == 3)
{
if (gBlockRecvBuffer[0][0] == (ACTIVITY_ACCEPT | IN_UNION_ROOM)
2020-06-03 01:05:59 +02:00
&& gBlockRecvBuffer[1][0] == (ACTIVITY_ACCEPT | IN_UNION_ROOM))
2019-04-02 04:30:30 +02:00
{
2021-02-24 17:01:02 +01:00
BeginNormalPaletteFade(PALETTES_ALL, 0, 0, 16, RGB_BLACK);
2019-04-02 04:30:30 +02:00
gMain.state = 50;
}
else
{
2020-08-13 09:09:47 +02:00
SetCloseLinkCallback();
2020-06-03 21:28:29 +02:00
if (gBlockRecvBuffer[GetMultiplayerId()][0] == (ACTIVITY_DECLINE | IN_UNION_ROOM))
2019-04-02 04:30:30 +02:00
{
gMain.state = 6;
}
else
{
gMain.state = 8;
}
}
ResetBlockReceivedFlags();
}
break;
case 50:
if (!UpdatePaletteFade())
{
2020-08-13 09:09:47 +02:00
SetLinkStandbyCallback();
2019-04-02 04:30:30 +02:00
gMain.state++;
}
break;
case 51:
if (IsLinkTaskFinished())
{
2020-06-03 01:05:59 +02:00
SetMainCallback2(CB2_SetUpPartiesAndStartBattle);
2019-04-02 04:30:30 +02:00
}
break;
case 6:
2020-06-03 01:05:59 +02:00
if (!gReceivedRemoteLinkPlayers)
2019-04-02 04:30:30 +02:00
{
gMain.state++;
}
break;
case 7:
2020-06-03 01:05:59 +02:00
if (PrintUnionRoomBattleMessage(&sBattle->textState, gText_RefusedBattle, 1))
2019-04-02 04:30:30 +02:00
{
SetMainCallback2(CB2_ReturnToField);
}
break;
case 8:
2020-06-03 01:05:59 +02:00
if (!gReceivedRemoteLinkPlayers)
2019-04-02 04:30:30 +02:00
{
gMain.state++;
}
break;
case 9:
2020-06-03 01:05:59 +02:00
if (PrintUnionRoomBattleMessage(&sBattle->textState, gText_BattleWasRefused, 1))
2019-04-02 04:30:30 +02:00
{
SetMainCallback2(CB2_ReturnToField);
}
break;
}
RunTasks();
RunTextPrinters();
AnimateSprites();
BuildOamBuffer();
UpdatePaletteFade();
2019-04-02 00:42:57 +02:00
}