pokeemerald/src/coins.c

83 lines
2.0 KiB
C
Raw Normal View History

2017-09-09 14:24:18 +02:00
#include "global.h"
#include "coins.h"
#include "text.h"
#include "window.h"
2017-11-24 05:10:51 +01:00
#include "strings.h"
2017-09-09 14:24:18 +02:00
#include "string_util.h"
2017-09-29 00:11:42 +02:00
#include "menu.h"
2017-10-09 15:40:08 +02:00
#include "international_string_util.h"
2019-11-07 15:41:59 +01:00
#include "constants/coins.h"
2017-09-09 14:24:18 +02:00
2022-05-17 19:51:54 +02:00
static EWRAM_DATA u8 sCoinsWindowId = 0;
2017-09-09 14:24:18 +02:00
void PrintCoinsString(u32 coinAmount)
{
u32 xAlign;
2020-07-29 10:46:58 +02:00
ConvertIntToDecimalStringN(gStringVar1, coinAmount, STR_CONV_MODE_RIGHT_ALIGN, MAX_COIN_DIGITS);
2017-09-12 08:23:34 +02:00
StringExpandPlaceholders(gStringVar4, gText_Coins);
2017-09-09 14:24:18 +02:00
2021-10-30 22:47:37 +02:00
xAlign = GetStringRightAlignXOffset(FONT_NORMAL, gStringVar4, 0x40);
AddTextPrinterParameterized(sCoinsWindowId, FONT_NORMAL, gStringVar4, xAlign, 1, 0, NULL);
2017-09-09 14:24:18 +02:00
}
void ShowCoinsWindow(u32 coinAmount, u8 x, u8 y)
{
struct WindowTemplate template;
SetWindowTemplateFields(&template, 0, x, y, 8, 2, 0xF, 0x141);
sCoinsWindowId = AddWindow(&template);
FillWindowPixelBuffer(sCoinsWindowId, PIXEL_FILL(0));
2017-09-09 14:24:18 +02:00
PutWindowTilemap(sCoinsWindowId);
DrawStdFrameWithCustomTileAndPalette(sCoinsWindowId, FALSE, 0x214, 0xE);
2017-09-09 14:24:18 +02:00
PrintCoinsString(coinAmount);
}
void HideCoinsWindow(void)
{
ClearStdWindowAndFrame(sCoinsWindowId, TRUE);
2017-09-09 14:24:18 +02:00
RemoveWindow(sCoinsWindowId);
}
u16 GetCoins(void)
{
return gSaveBlock1Ptr->coins ^ gSaveBlock2Ptr->encryptionKey;
}
void SetCoins(u16 coinAmount)
{
gSaveBlock1Ptr->coins = coinAmount ^ gSaveBlock2Ptr->encryptionKey;
}
bool8 AddCoins(u16 toAdd)
2017-09-09 14:24:18 +02:00
{
u16 newAmount;
u16 ownedCoins = GetCoins();
if (ownedCoins >= MAX_COINS)
return FALSE;
// check overflow, can't have less coins than previously
if (ownedCoins > ownedCoins + toAdd)
{
newAmount = MAX_COINS;
}
else
{
2017-11-23 19:53:51 +01:00
ownedCoins += toAdd;
if (ownedCoins > MAX_COINS)
ownedCoins = MAX_COINS;
newAmount = ownedCoins;
2017-09-09 14:24:18 +02:00
}
SetCoins(newAmount);
return TRUE;
2017-11-23 19:53:51 +01:00
}
bool8 RemoveCoins(u16 toSub)
2017-11-23 19:53:51 +01:00
{
u16 ownedCoins = GetCoins();
if (ownedCoins >= toSub)
{
SetCoins(ownedCoins - toSub);
return TRUE;
}
return FALSE;
}