pokeemerald/src/trader.c

210 lines
5.8 KiB
C
Raw Normal View History

2018-05-01 21:19:24 +02:00
#include "global.h"
#include "constants/decorations.h"
2019-10-20 22:11:07 +02:00
#include "constants/mauville_old_man.h"
2018-05-01 21:19:24 +02:00
#include "decoration.h"
#include "decoration_inventory.h"
#include "event_data.h"
#include "main.h"
#include "menu.h"
#include "menu_helpers.h"
#include "script.h"
#include "constants/songs.h"
#include "sound.h"
#include "string_util.h"
#include "strings.h"
#include "task.h"
#include "script_menu.h"
static const u8 * const sDefaultTraderNames[] =
2018-05-01 21:19:24 +02:00
{
gText_Tristan,
gText_Philip,
gText_Dennis,
gText_Roberto,
};
static const u8 sDefaultTraderDecorations[] =
2018-05-01 21:19:24 +02:00
{
DECOR_DUSKULL_DOLL,
DECOR_BALL_CUSHION,
DECOR_TIRE,
DECOR_PRETTY_FLOWERS,
};
void TraderSetup(void)
{
u8 i;
struct MauvilleOldManTrader *trader = &gSaveBlock1Ptr->oldMan.trader;
trader->id = MAUVILLE_MAN_TRADER;
trader->alreadyTraded = FALSE;
for (i = 0; i < 4; i++)
{
StringCopy(trader->playerNames[i], sDefaultTraderNames[i]);
trader->decorIds[i] = sDefaultTraderDecorations[i];
2018-05-01 21:19:24 +02:00
trader->language[i] = GAME_LANGUAGE;
}
}
void Trader_ResetFlag(void)
2018-05-01 21:19:24 +02:00
{
struct MauvilleOldManTrader *trader = &gSaveBlock1Ptr->oldMan.trader;
trader->alreadyTraded = FALSE;
}
void CreateAvailableDecorationsMenu(u8 taskId)
{
u8 i;
s16 * data = gTasks[taskId].data;
struct MauvilleOldManTrader *trader = &gSaveBlock1Ptr->oldMan.trader;
struct WindowTemplate windowTemplate = {0, 1, 1, 10, 10, 15, 1};
s32 windowWidth = GetStringWidth(1, gText_Exit, 0);
s32 fiveMarksWidth = GetStringWidth(1, gText_FiveMarks, 0);
for (i = 0; i < 4; i++)
{
s32 curWidth;
if (trader->decorIds[i] > NUM_DECORATIONS)
2018-05-01 21:19:24 +02:00
curWidth = fiveMarksWidth;
else
curWidth = GetStringWidth(1, gDecorations[trader->decorIds[i]].name, 0);
2018-05-01 21:19:24 +02:00
if (curWidth > windowWidth)
windowWidth = curWidth;
}
windowTemplate.width = ConvertPixelWidthToTileWidth(windowWidth);
2018-05-01 21:19:24 +02:00
data[3] = AddWindow(&windowTemplate);
DrawStdFrameWithCustomTileAndPalette(data[3], FALSE, 0x214, 14);
2018-05-01 21:19:24 +02:00
for (i = 0; i < 4; i++)
{
if (trader->decorIds[i] > NUM_DECORATIONS)
AddTextPrinterParameterized(data[3], 1, gText_FiveMarks, 8, 16 * i + 1, 255, NULL);
2018-05-01 21:19:24 +02:00
else
AddTextPrinterParameterized(data[3], 1, gDecorations[trader->decorIds[i]].name, 8, 16 * i + 1, 255, NULL);
2018-05-01 21:19:24 +02:00
}
AddTextPrinterParameterized(data[3], 1, gText_Exit, 8, 16 * i + 1, 255, NULL);
2018-05-01 21:19:24 +02:00
InitMenuInUpperLeftCornerPlaySoundWhenAPressed(data[3], 5, 0);
schedule_bg_copy_tilemap_to_vram(0);
}
2019-10-20 22:11:07 +02:00
void Task_BufferDecorSelectionAndCloseWindow(u8 taskId, u8 decorationId)
2018-05-01 21:19:24 +02:00
{
s16 * data = gTasks[taskId].data;
if (decorationId > NUM_DECORATIONS)
{
gSpecialVar_0x8004 = 0xFFFF;
}
else
{
gSpecialVar_0x8004 = decorationId;
}
ClearStdWindowAndFrameToTransparent(data[3], FALSE);
2018-05-01 21:19:24 +02:00
ClearWindowTilemap(data[3]);
RemoveWindow(data[3]);
schedule_bg_copy_tilemap_to_vram(0);
DestroyTask(taskId);
EnableBothScriptContexts();
}
void Task_HandleGetDecorationMenuInput(u8 taskId)
{
struct MauvilleOldManTrader *trader = &gSaveBlock1Ptr->oldMan.trader;
2018-11-05 21:45:54 +01:00
s8 input = Menu_ProcessInput();
2018-05-01 21:19:24 +02:00
switch (input)
{
2019-02-02 11:04:38 +01:00
case MENU_NOTHING_CHOSEN:
2018-05-01 21:19:24 +02:00
break;
2019-02-02 11:04:38 +01:00
case MENU_B_PRESSED:
2018-05-01 21:19:24 +02:00
case 4:
PlaySE(SE_SELECT);
2019-10-20 22:11:07 +02:00
Task_BufferDecorSelectionAndCloseWindow(taskId, 0);
2018-05-01 21:19:24 +02:00
break;
default:
PlaySE(SE_SELECT);
gSpecialVar_0x8005 = input;
StringCopy(gStringVar1, trader->playerNames[input]);
2018-05-01 21:19:24 +02:00
ConvertInternationalString(gStringVar1, trader->language[input]);
2019-10-20 22:11:07 +02:00
Task_BufferDecorSelectionAndCloseWindow(taskId, trader->decorIds[input]);
2018-05-01 21:19:24 +02:00
break;
}
}
void ScrSpecial_GetTraderTradedFlag(void)
2018-05-01 21:19:24 +02:00
{
struct MauvilleOldManTrader *trader = &gSaveBlock1Ptr->oldMan.trader;
gSpecialVar_Result = trader->alreadyTraded;
}
void ScrSpecial_DoesPlayerHaveNoDecorations(void)
2018-05-01 21:19:24 +02:00
{
u8 i;
for (i = 0; i < 8; i++)
{
if (GetNumOwnedDecorationsInCategory(i))
2018-05-01 21:19:24 +02:00
{
gSpecialVar_Result = FALSE;
return;
}
}
gSpecialVar_Result = TRUE;
}
void ScrSpecial_IsDecorationFull(void)
2018-05-01 21:19:24 +02:00
{
gSpecialVar_Result = FALSE;
if (gDecorations[gSpecialVar_0x8004].category != gDecorations[gSpecialVar_0x8006].category
&& GetFirstEmptyDecorSlot(gDecorations[gSpecialVar_0x8004].category) == -1)
{
CopyDecorationCategoryName(gStringVar2, gDecorations[gSpecialVar_0x8004].category);
2018-05-01 21:19:24 +02:00
gSpecialVar_Result = TRUE;
}
}
void ScrSpecial_TraderMenuGiveDecoration(void)
2018-05-01 21:19:24 +02:00
{
CreateTask(ShowDecorationCategoriesWindow, 0);
2018-05-01 21:19:24 +02:00
}
void sub_8133DA0(u8 taskId)
{
if (IsSelectedDecorInThePC() == TRUE)
{
gSpecialVar_0x8006 = gCurDecorationItems[gCurDecorationIndex];
2018-05-01 21:19:24 +02:00
StringCopy(gStringVar3, gDecorations[gSpecialVar_0x8004].name);
StringCopy(gStringVar2, gDecorations[gSpecialVar_0x8006].name);
}
else
{
gSpecialVar_0x8006 = 0xFFFF;
}
DestroyTask(taskId);
EnableBothScriptContexts();
}
void ExitTraderMenu(u8 taskId)
2018-05-01 21:19:24 +02:00
{
gSpecialVar_0x8006 = 0;
DestroyTask(taskId);
EnableBothScriptContexts();
}
void ScrSpecial_TraderDoDecorationTrade(void)
2018-05-01 21:19:24 +02:00
{
struct MauvilleOldManTrader *trader = &gSaveBlock1Ptr->oldMan.trader;
DecorationRemove(gSpecialVar_0x8006);
DecorationAdd(gSpecialVar_0x8004);
StringCopy(trader->playerNames[gSpecialVar_0x8005], gSaveBlock2Ptr->playerName);
trader->decorIds[gSpecialVar_0x8005] = gSpecialVar_0x8006;
2018-05-01 21:19:24 +02:00
trader->language[gSpecialVar_0x8005] = GAME_LANGUAGE;
trader->alreadyTraded = TRUE;
}
void ScrSpecial_TraderMenuGetDecoration(void)
2018-05-01 21:19:24 +02:00
{
u8 taskId = CreateTask(Task_HandleGetDecorationMenuInput, 0);
CreateAvailableDecorationsMenu(taskId);
}