summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPikalaxALT <pikalaxalt@gmail.com>2018-11-02 14:16:03 -0400
committerPikalaxALT <pikalaxalt@gmail.com>2018-11-02 14:16:03 -0400
commit5391289fc1164eab859856617f8dede7db3fe435 (patch)
tree8cb4f701f3b49cf182715e28b0d818c778ff9e5c /src
parent46e006b94cf7a2e59652a083cdd01f0d5dc9a26f (diff)
parentc0a9d620919bd6403772c4e5a236d47cd40ae861 (diff)
Merge branch 'master' into quest_log
Diffstat (limited to 'src')
-rw-r--r--src/coins.c101
-rw-r--r--src/math_util.c86
-rw-r--r--src/money.c140
-rw-r--r--src/text_printer.c2
4 files changed, 328 insertions, 1 deletions
diff --git a/src/coins.c b/src/coins.c
new file mode 100644
index 000000000..08646e2f9
--- /dev/null
+++ b/src/coins.c
@@ -0,0 +1,101 @@
+#include "global.h"
+#include "string_util.h"
+#include "text.h"
+#include "menu.h"
+#include "text_window.h"
+
+extern const u8 gText_Coins[];
+extern const u8 gUnknown_8417C2D[];
+
+EWRAM_DATA static u8 sCoinsWindowId = 0;
+
+#define MAX_COINS 9999
+
+u16 GetCoins(void)
+{
+ return gSaveBlock1Ptr->coins ^ gSaveBlock2Ptr->encryptionKey;
+}
+
+void SetCoins(u16 coinAmount)
+{
+ gSaveBlock1Ptr->coins = coinAmount ^ gSaveBlock2Ptr->encryptionKey;
+}
+
+bool8 GiveCoins(u16 toAdd)
+{
+ u16 coins = GetCoins();
+ if (coins >= MAX_COINS)
+ return FALSE;
+ // check overflow, can't have less coins than previously
+ if (coins <= coins + toAdd)
+ {
+ coins += toAdd;
+ if (coins > MAX_COINS)
+ coins = MAX_COINS;
+ }
+ else
+ {
+ coins = MAX_COINS;
+ }
+ SetCoins(coins);
+ return TRUE;
+}
+
+bool8 TakeCoins(u16 toSub)
+{
+ u16 coins = GetCoins();
+ if (coins >= toSub)
+ {
+ SetCoins(coins - toSub);
+ return TRUE;
+ }
+ return FALSE;
+}
+
+void PrintCoinsString_Parameterized(u8 windowId, u32 coinAmount, u8 x, u8 y, u8 speed)
+{
+ ConvertIntToDecimalStringN(gStringVar1, coinAmount, STR_CONV_MODE_RIGHT_ALIGN, 4);
+ StringExpandPlaceholders(gStringVar4, gText_Coins);
+ AddTextPrinterParameterized(windowId, 0, gStringVar4, x, y, speed, NULL);
+}
+
+void sub_80D0674(u8 windowId, u16 tileStart, u8 palette, u32 coinAmount)
+{
+ SetWindowBorderStyle(windowId, FALSE, tileStart, palette);
+ AddTextPrinterParameterized(windowId, 2, gUnknown_8417C2D, 0, 0, 0xFF, 0);
+ PrintCoinsString_Parameterized(windowId, coinAmount, 0x10, 0xC, 0);
+}
+
+void PrintCoinsString(u32 coinAmount)
+{
+ u8 windowId;
+ int width;
+
+ ConvertIntToDecimalStringN(gStringVar1, coinAmount, STR_CONV_MODE_RIGHT_ALIGN, 4);
+ StringExpandPlaceholders(gStringVar4, gText_Coins);
+ width = GetStringWidth(0, gStringVar4, 0);
+ windowId = sCoinsWindowId;
+ AddTextPrinterParameterized(windowId, 0, gStringVar4, 64 - width, 0xC, 0, NULL);
+}
+
+void ShowCoinsWindow(u32 coinAmount, u8 x, u8 y)
+{
+ struct WindowTemplate template, template2;
+
+ SetWindowTemplateFields(&template, 0, x + 1, y + 1, 8, 3, 0xF, 0x20);
+ template2 = template; // again, why...
+ sCoinsWindowId = AddWindow(&template2);
+ FillWindowPixelBuffer(sCoinsWindowId, 0);
+ PutWindowTilemap(sCoinsWindowId);
+ sub_814FF2C(sCoinsWindowId, 0x21D, 0xD0);
+ SetWindowBorderStyle(sCoinsWindowId, FALSE, 0x21D, 0xD);
+ AddTextPrinterParameterized(sCoinsWindowId, 2, gUnknown_8417C2D, 0, 0, 0xFF, 0);
+ PrintCoinsString(coinAmount);
+}
+
+void HideCoinsWindow(void)
+{
+ ClearWindowTilemap(sCoinsWindowId);
+ sub_810F4D8(sCoinsWindowId, TRUE);
+ RemoveWindow(sCoinsWindowId);
+}
diff --git a/src/math_util.c b/src/math_util.c
new file mode 100644
index 000000000..466a31921
--- /dev/null
+++ b/src/math_util.c
@@ -0,0 +1,86 @@
+#include "global.h"
+
+s16 sub_80D8AA0(s16 x, s16 y)
+{
+ s32 result;
+
+ result = x;
+ result *= y;
+ result /= 256;
+ return result;
+}
+
+s16 sub_80D8ABC(u8 s, s16 x, s16 y)
+{
+ s32 result;
+
+ result = x;
+ result *= y;
+ result /= (1 << s);
+ return result;
+}
+
+s32 sub_80D8AE0(s32 x, s32 y)
+{
+ s64 result;
+
+ result = x;
+ result *= y;
+ result /= 256;
+ return result;
+}
+
+s16 sub_80D8B1C(s16 x, s16 y)
+{
+ if (y == 0)
+ {
+ return 0;
+ }
+ return (x << 8) / y;
+}
+
+s16 sub_80D8B40(u8 s, s16 x, s16 y)
+{
+ if (y == 0)
+ {
+ return 0;
+ }
+ return (x << s) / y;
+}
+
+s32 sub_80D8B68(s32 x, s32 y)
+{
+ s64 _x;
+
+ if (y == 0)
+ {
+ return 0;
+ }
+ _x = x;
+ _x *= 256;
+ return _x / y;
+}
+
+s16 sub_80D8B90(s16 y)
+{
+ s32 x;
+
+ x = 0x10000;
+ return x / y;
+}
+
+s16 sub_80D8BA8(u8 s, s16 y)
+{
+ s32 x;
+
+ x = 0x100 << s;
+ return x / y;
+}
+
+s32 sub_80D8BC8(s32 y)
+{
+ s64 x;
+
+ x = 0x10000;
+ return x / y;
+}
diff --git a/src/money.c b/src/money.c
new file mode 100644
index 000000000..6d9a0fce9
--- /dev/null
+++ b/src/money.c
@@ -0,0 +1,140 @@
+#include "global.h"
+#include "event_data.h"
+#include "text.h"
+#include "string_util.h"
+#include "menu.h"
+#include "text_window.h"
+
+extern const u8 gText_PokedollarVar1[];
+extern const u8 gUnknown_8419CE7[];
+
+#define MAX_MONEY 999999
+
+EWRAM_DATA static u8 sMoneyBoxWindowId = 0;
+
+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);
+}
+
+void RemoveMoney(u32* moneyPtr, u32 toSub)
+{
+ u32 toSet = GetMoney(moneyPtr);
+
+ // can't subtract more than you already have
+ if (toSet < toSub)
+ toSet = 0;
+ else
+ toSet -= toSub;
+
+ SetMoney(moneyPtr, toSet);
+}
+
+bool8 IsEnoughForCostInVar0x8005(void)
+{
+ return IsEnoughMoney(&gSaveBlock1Ptr->money, gSpecialVar_0x8005);
+}
+
+void SubtractMoneyFromVar0x8005(void)
+{
+ RemoveMoney(&gSaveBlock1Ptr->money, gSpecialVar_0x8005);
+}
+
+void PrintMoneyAmountInMoneyBox(u8 windowId, 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++) = 0;
+
+ StringExpandPlaceholders(txtPtr, gText_PokedollarVar1);
+ AddTextPrinterParameterized(windowId, 0, gStringVar4, 64 - GetStringWidth(0, gStringVar4, 0), 0xC, speed, NULL);
+}
+
+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++) = 0;
+
+ StringExpandPlaceholders(txtPtr, gText_PokedollarVar1);
+ AddTextPrinterParameterized(windowId, 0, gStringVar4, x, y, speed, NULL);
+}
+
+void PrintMoneyAmountInMoneyBoxWithBorder(u8 windowId, u16 tileStart, u8 pallete, int amount)
+{
+ SetWindowBorderStyle(windowId, FALSE, tileStart, pallete);
+ AddTextPrinterParameterized(windowId, 2, gUnknown_8419CE7, 0, 0, 0xFF, 0);
+ PrintMoneyAmountInMoneyBox(windowId, amount, 0);
+}
+
+void ChangeAmountInMoneyBox(int amount)
+{
+ PrintMoneyAmountInMoneyBox(sMoneyBoxWindowId, amount, 0);
+}
+
+void DrawMoneyBox(int amount, u8 x, u8 y)
+{
+ struct WindowTemplate template, template2;
+
+ SetWindowTemplateFields(&template, 0, x + 1, y + 1, 8, 3, 15, 8);
+ template2 = template;
+ sMoneyBoxWindowId = AddWindow(&template2);
+ FillWindowPixelBuffer(sMoneyBoxWindowId, 0);
+ PutWindowTilemap(sMoneyBoxWindowId);
+ sub_814FF2C(sMoneyBoxWindowId, 0x21D, 0xD0);
+ PrintMoneyAmountInMoneyBoxWithBorder(sMoneyBoxWindowId, 0x21D, 13, amount);
+}
+
+void HideMoneyBox(void)
+{
+ sub_810F4D8(sMoneyBoxWindowId, FALSE);
+ CopyWindowToVram(sMoneyBoxWindowId, 2);
+ RemoveWindow(sMoneyBoxWindowId);
+}
diff --git a/src/text_printer.c b/src/text_printer.c
index a6e55ef1c..04de1af4c 100644
--- a/src/text_printer.c
+++ b/src/text_printer.c
@@ -48,7 +48,7 @@ void DeactivateAllTextPrinters (void)
sTextPrinters[printer].sub_union.sub.active = 0;
}
-u16 PrintTextOnWindow(u8 windowId, u8 fontId, const u8 *str, u8 x, u8 y, u8 speed, void (*callback)(struct TextSubPrinter *, u16))
+u16 AddTextPrinterParameterized(u8 windowId, u8 fontId, const u8 *str, u8 x, u8 y, u8 speed, void (*callback)(struct TextSubPrinter *, u16))
{
struct TextSubPrinter subPrinter;