forked from pret/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcoins.c
96 lines (85 loc) · 2.44 KB
/
coins.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
#include "global.h"
#include "coins.h"
#include "text.h"
#include "window.h"
#include "strings.h"
#include "string_util.h"
#include "menu.h"
#include "international_string_util.h"
#include "constants/coins.h"
#if FRENCH
#define COINS_STRING_FONT FONT_NARROW
#define COINS_STRING_TOTAL_WIDTH 0x38
#define COINS_WINDOW_WIDTH 7
#elif ITALIAN
#define COINS_STRING_FONT FONT_NARROW
#define COINS_STRING_TOTAL_WIDTH 0x30
#define COINS_WINDOW_WIDTH 6
#else //ENGLISH
#define COINS_STRING_FONT FONT_NORMAL
#define COINS_STRING_TOTAL_WIDTH 0x40
#define COINS_WINDOW_WIDTH 8
#endif
static EWRAM_DATA u8 sCoinsWindowId = 0;
void PrintCoinsString(u32 coinAmount)
{
u32 xAlign;
ConvertIntToDecimalStringN(gStringVar1, coinAmount, STR_CONV_MODE_RIGHT_ALIGN, MAX_COIN_DIGITS);
StringExpandPlaceholders(gStringVar4, gText_Coins);
xAlign = GetStringRightAlignXOffset(COINS_STRING_FONT, gStringVar4, COINS_STRING_TOTAL_WIDTH);
AddTextPrinterParameterized(sCoinsWindowId, COINS_STRING_FONT, gStringVar4, xAlign, 1, 0, NULL);
}
void ShowCoinsWindow(u32 coinAmount, u8 x, u8 y)
{
struct WindowTemplate template;
SetWindowTemplateFields(&template, 0, x, y, COINS_WINDOW_WIDTH, 2, 0xF, 0x141);
sCoinsWindowId = AddWindow(&template);
FillWindowPixelBuffer(sCoinsWindowId, PIXEL_FILL(0));
PutWindowTilemap(sCoinsWindowId);
DrawStdFrameWithCustomTileAndPalette(sCoinsWindowId, FALSE, 0x214, 0xE);
PrintCoinsString(coinAmount);
}
void HideCoinsWindow(void)
{
ClearStdWindowAndFrame(sCoinsWindowId, TRUE);
RemoveWindow(sCoinsWindowId);
}
u16 GetCoins(void)
{
return gSaveBlock1Ptr->coins ^ gSaveBlock2Ptr->encryptionKey;
}
void SetCoins(u16 coinAmount)
{
gSaveBlock1Ptr->coins = coinAmount ^ gSaveBlock2Ptr->encryptionKey;
}
bool8 AddCoins(u16 toAdd)
{
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
{
ownedCoins += toAdd;
if (ownedCoins > MAX_COINS)
ownedCoins = MAX_COINS;
newAmount = ownedCoins;
}
SetCoins(newAmount);
return TRUE;
}
bool8 RemoveCoins(u16 toSub)
{
u16 ownedCoins = GetCoins();
if (ownedCoins >= toSub)
{
SetCoins(ownedCoins - toSub);
return TRUE;
}
return FALSE;
}