pokeemerald/src/money.c

198 lines
4.6 KiB
C
Raw Normal View History

2017-09-09 00:45:25 +02:00
#include "global.h"
#include "money.h"
2018-10-21 09:24:57 +02:00
#include "graphics.h"
2017-09-29 00:11:42 +02:00
#include "event_data.h"
#include "string_util.h"
#include "text.h"
#include "menu.h"
#include "window.h"
#include "sprite.h"
2018-09-02 00:15:35 +02:00
#include "strings.h"
2017-09-29 00:11:42 +02:00
#include "decompress.h"
2017-09-09 00:45:25 +02:00
#define MAX_MONEY 999999
2017-09-29 00:11:42 +02:00
EWRAM_DATA static u8 sMoneyBoxWindowId = 0;
EWRAM_DATA static u8 sMoneyLabelSpriteId = 0;
#define MONEY_LABEL_TAG 0x2722
static const struct OamData sOamData_MoneyLabel =
{
.y = 0,
.affineMode = ST_OAM_AFFINE_OFF,
.objMode = ST_OAM_OBJ_NORMAL,
2017-09-29 00:11:42 +02:00
.mosaic = 0,
.bpp = ST_OAM_4BPP,
.shape = SPRITE_SHAPE(32x16),
2017-09-29 00:11:42 +02:00
.x = 0,
.matrixNum = 0,
.size = SPRITE_SIZE(32x16),
2017-09-29 00:11:42 +02:00
.tileNum = 0,
.priority = 0,
.paletteNum = 0,
.affineParam = 0,
};
static const union AnimCmd sSpriteAnim_MoneyLabel[] =
{
ANIMCMD_FRAME(0, 0),
ANIMCMD_END
};
static const union AnimCmd *const sSpriteAnimTable_MoneyLabel[] =
{
sSpriteAnim_MoneyLabel,
};
static const struct SpriteTemplate sSpriteTemplate_MoneyLabel =
{
.tileTag = MONEY_LABEL_TAG,
.paletteTag = MONEY_LABEL_TAG,
.oam = &sOamData_MoneyLabel,
.anims = sSpriteAnimTable_MoneyLabel,
.images = NULL,
.affineAnims = gDummySpriteAffineAnimTable,
.callback = SpriteCallbackDummy
};
static const struct CompressedSpriteSheet sSpriteSheet_MoneyLabel =
{
.data = gMenuMoneyGfx,
.size = 256,
.tag = MONEY_LABEL_TAG,
};
static const struct CompressedSpritePalette sSpritePalette_MoneyLabel =
{
.data = gMenuMoneyPal,
.tag = MONEY_LABEL_TAG
};
2017-09-09 00:45:25 +02:00
u32 GetMoney(u32* moneyPtr)
{
return *moneyPtr ^ gSaveBlock2Ptr->encryptionKey;
}
void SetMoney(u32* moneyPtr, u32 newValue)
{
*moneyPtr = gSaveBlock2Ptr->encryptionKey ^ newValue;
}
bool8 IsEnoughMoney(u32* moneyPtr, u32 cost)
{
if (GetMoney(moneyPtr) >= cost)
return TRUE;
else
return FALSE;
}
void AddMoney(u32* moneyPtr, u32 toAdd)
{
u32 toSet = GetMoney(moneyPtr);
// can't have more money than MAX
if (toSet + toAdd > MAX_MONEY)
{
toSet = MAX_MONEY;
}
else
{
toSet += toAdd;
// check overflow, can't have less money after you receive more
if (toSet < GetMoney(moneyPtr))
toSet = MAX_MONEY;
}
SetMoney(moneyPtr, toSet);
}
2017-10-12 09:06:19 +02:00
void RemoveMoney(u32* moneyPtr, u32 toSub)
2017-09-09 00:45:25 +02:00
{
u32 toSet = GetMoney(moneyPtr);
// can't subtract more than you already have
if (toSet < toSub)
toSet = 0;
else
toSet -= toSub;
SetMoney(moneyPtr, toSet);
}
2017-09-29 00:11:42 +02:00
bool8 IsEnoughForCostInVar0x8005(void)
{
return IsEnoughMoney(&gSaveBlock1Ptr->money, gSpecialVar_0x8005);
}
void SubtractMoneyFromVar0x8005(void)
{
2017-10-12 09:06:19 +02:00
RemoveMoney(&gSaveBlock1Ptr->money, gSpecialVar_0x8005);
2017-09-29 00:11:42 +02:00
}
void PrintMoneyAmountInMoneyBox(u8 windowId, int amount, u8 speed)
{
PrintMoneyAmount(windowId, 0x26, 1, amount, speed);
}
void PrintMoneyAmount(u8 windowId, u8 x, u8 y, int amount, u8 speed)
{
u8 *txtPtr;
s32 strLength;
ConvertIntToDecimalStringN(gStringVar1, amount, STR_CONV_MODE_LEFT_ALIGN, 6);
strLength = 6 - StringLength(gStringVar1);
txtPtr = gStringVar4;
while (strLength-- > 0)
*(txtPtr++) = 0x77;
StringExpandPlaceholders(txtPtr, gText_PokedollarVar1);
AddTextPrinterParameterized(windowId, 1, gStringVar4, x, y, speed, NULL);
2017-09-29 00:11:42 +02:00
}
void PrintMoneyAmountInMoneyBoxWithBorder(u8 windowId, u16 tileStart, u8 pallete, int amount)
{
DrawStdFrameWithCustomTileAndPalette(windowId, FALSE, tileStart, pallete);
2017-09-29 00:11:42 +02:00
PrintMoneyAmountInMoneyBox(windowId, amount, 0);
}
void ChangeAmountInMoneyBox(int amount)
{
PrintMoneyAmountInMoneyBox(sMoneyBoxWindowId, amount, 0);
}
void DrawMoneyBox(int amount, u8 x, u8 y)
{
struct WindowTemplate template;
SetWindowTemplateFields(&template, 0, x + 1, y + 1, 10, 2, 15, 8);
sMoneyBoxWindowId = AddWindow(&template);
FillWindowPixelBuffer(sMoneyBoxWindowId, PIXEL_FILL(0));
2017-09-29 00:11:42 +02:00
PutWindowTilemap(sMoneyBoxWindowId);
CopyWindowToVram(sMoneyBoxWindowId, 1);
PrintMoneyAmountInMoneyBoxWithBorder(sMoneyBoxWindowId, 0x214, 14, amount);
AddMoneyLabelObject((8 * x) + 19, (8 * y) + 11);
}
void HideMoneyBox(void)
{
RemoveMoneyLabelObject();
ClearStdWindowAndFrameToTransparent(sMoneyBoxWindowId, FALSE);
2017-09-29 00:11:42 +02:00
CopyWindowToVram(sMoneyBoxWindowId, 2);
RemoveWindow(sMoneyBoxWindowId);
}
void AddMoneyLabelObject(u16 x, u16 y)
{
2018-12-17 23:00:08 +01:00
LoadCompressedSpriteSheet(&sSpriteSheet_MoneyLabel);
LoadCompressedSpritePalette(&sSpritePalette_MoneyLabel);
2017-09-29 00:11:42 +02:00
sMoneyLabelSpriteId = CreateSprite(&sSpriteTemplate_MoneyLabel, x, y, 0);
}
void RemoveMoneyLabelObject(void)
{
DestroySpriteAndFreeResources(&gSprites[sMoneyLabelSpriteId]);
}