From 522a8ba3f7b8d1736cf003c55bc529a015946bdc Mon Sep 17 00:00:00 2001 From: psf <77138753+pkmnsnfrn@users.noreply.github.com> Date: Sat, 13 Jul 2024 02:16:34 -0700 Subject: [PATCH] Allow users to increase MAX_MONEY (#4943) * Added GetMaxNumMoneyDigits and GetMoneyBoxHorizontalPosition to header Replaced use of PrintMoneyAmount * Added define for MAX_MONEY_DIGITS Removed GetMaxNumMoneyDigits Converted uses of ConvertIntToDecimalStringN that called for 7 money digits to use MAX_MONEY_DIGITS Replaced 6 money digits in PrintMoneyAmount with MAX_MONEY_DIGITS * Use preproc and MAX_MONEY_DIGITS to increase the width of the sell item money box * Added GetItemSoldMoneyHorizonalPosition and fixed ConvertInt in PrintMoneyAmount to use MAX_MONEY_DIGITS * Added GetItemSoldMoneyHorizonalPosition and fixed ConvertInt in PrintMoneyAmount to use MAX_MONEY_DIGITS * Removed width and tilemap changes * Modified CountDigits to count digits greater than 1 Fixed PrintMoneyAmount to work with large digits * Added an early return to PrependFontIdToFit * First prototype working * Updated GetMoneyBoxHorizontalPosition with amount * Updated GetMoneyBoxHorizontalPosition with amount * Got version working with one space before * Got version working with one space before * Got version working with all spacingOC * cleaned up PrintMoneyAmount * Created CalculateleadingSpacesForMoney * Cleaned up for PR * Changed tabs to spaces per https://github.com/rh-hideout/pokeemerald-expansion/pull/4943\#issuecomment-2223023365 --- gflib/text.c | 22 +++++++++------------- include/money.h | 13 ++++++++++++- src/item_menu.c | 6 +++--- src/money.c | 30 +++++++++++++++++++++++------- src/shop.c | 6 +++--- src/trainer_card.c | 2 +- src/tv.c | 16 +++++++--------- 7 files changed, 58 insertions(+), 37 deletions(-) diff --git a/gflib/text.c b/gflib/text.c index b763429e3e..59c6e3f4e8 100644 --- a/gflib/text.c +++ b/gflib/text.c @@ -2155,21 +2155,17 @@ u32 GetFontIdToFit(const u8 *string, u32 fontId, u32 letterSpacing, u32 widthPx) u8 *PrependFontIdToFit(u8 *start, u8 *end, u32 fontId, u32 width) { - u32 fitFontId = GetFontIdToFit(start, fontId, 0, width); - if (fitFontId != fontId) - { - memmove(&start[3], &start[0], end - start); - start[0] = EXT_CTRL_CODE_BEGIN; - start[1] = EXT_CTRL_CODE_FONT; - start[2] = fitFontId; - end[3] = EOS; - return end + 3; - } - else - { + + if (fitFontId == fontId) return end; - } + + memmove(&start[3], &start[0], end - start); + start[0] = EXT_CTRL_CODE_BEGIN; + start[1] = EXT_CTRL_CODE_FONT; + start[2] = fitFontId; + end[3] = EOS; + return end + 3; } u8 *WrapFontIdToFit(u8 *start, u8 *end, u32 fontId, u32 width) diff --git a/include/money.h b/include/money.h index 211f9caa2e..5cffb94242 100644 --- a/include/money.h +++ b/include/money.h @@ -1,7 +1,17 @@ #ifndef GUARD_MONEY_H #define GUARD_MONEY_H -#define MAX_MONEY 999999 +#define MAX_MONEY 999999 // Can be increased to INT_MAX + +#define MAX_MONEY_DIGITS ((MAX_MONEY > 999999999) ? 10 : \ + (MAX_MONEY > 99999999) ? 9 : \ + (MAX_MONEY > 9999999) ? 8 : \ + (MAX_MONEY > 999999) ? 7 : \ + (MAX_MONEY > 99999) ? 6 : \ + (MAX_MONEY > 9999) ? 5 : \ + (MAX_MONEY > 999) ? 4 : \ + (MAX_MONEY > 99) ? 3 : \ + (MAX_MONEY > 9) ? 2 : 1) u32 GetMoney(u32 *moneyPtr); void SetMoney(u32 *moneyPtr, u32 newValue); @@ -18,5 +28,6 @@ void DrawMoneyBox(int amount, u8 x, u8 y); void HideMoneyBox(void); void AddMoneyLabelObject(u16 x, u16 y); void RemoveMoneyLabelObject(void); +u32 CalculateMoneyTextHorizontalPosition(u32 amount); #endif // GUARD_MONEY_H diff --git a/src/item_menu.c b/src/item_menu.c index ad9fc6f4bc..d885f0a8ec 100755 --- a/src/item_menu.c +++ b/src/item_menu.c @@ -1225,7 +1225,7 @@ static void PrintItemSoldAmount(int windowId, int numSold, int moneyEarned) ConvertIntToDecimalStringN(gStringVar1, numSold, STR_CONV_MODE_LEADING_ZEROS, MAX_ITEM_DIGITS); StringExpandPlaceholders(gStringVar4, gText_xVar1); AddTextPrinterParameterized(windowId, FONT_NORMAL, gStringVar4, 0, 1, TEXT_SKIP_DRAW, 0); - PrintMoneyAmount(windowId, 38, 1, moneyEarned, 0); + PrintMoneyAmount(windowId, CalculateMoneyTextHorizontalPosition(moneyEarned), 1, moneyEarned, 0); } static void Task_BagMenu_HandleInput(u8 taskId) @@ -2129,7 +2129,7 @@ static void DisplaySellItemPriceAndConfirm(u8 taskId) { s16 *data = gTasks[taskId].data; - ConvertIntToDecimalStringN(gStringVar1, (ItemId_GetPrice(gSpecialVar_ItemId) / ITEM_SELL_FACTOR) * tItemCount, STR_CONV_MODE_LEFT_ALIGN, 6); + ConvertIntToDecimalStringN(gStringVar1, (ItemId_GetPrice(gSpecialVar_ItemId) / ITEM_SELL_FACTOR) * tItemCount, STR_CONV_MODE_LEFT_ALIGN, MAX_MONEY_DIGITS); StringExpandPlaceholders(gStringVar4, gText_ICanPayVar1); DisplayItemMessage(taskId, FONT_NORMAL, gStringVar4, AskSellItems); } @@ -2189,7 +2189,7 @@ static void ConfirmSell(u8 taskId) s16 *data = gTasks[taskId].data; CopyItemName(gSpecialVar_ItemId, gStringVar2); - ConvertIntToDecimalStringN(gStringVar1, (ItemId_GetPrice(gSpecialVar_ItemId) / ITEM_SELL_FACTOR) * tItemCount, STR_CONV_MODE_LEFT_ALIGN, 6); + ConvertIntToDecimalStringN(gStringVar1, (ItemId_GetPrice(gSpecialVar_ItemId) / ITEM_SELL_FACTOR) * tItemCount, STR_CONV_MODE_LEFT_ALIGN, MAX_MONEY_DIGITS); StringExpandPlaceholders(gStringVar4, gText_TurnedOverVar1ForVar2); DisplayItemMessage(taskId, FONT_NORMAL, gStringVar4, SellItem); } diff --git a/src/money.c b/src/money.c index 321f882ff6..0a5129c934 100644 --- a/src/money.c +++ b/src/money.c @@ -9,6 +9,7 @@ #include "sprite.h" #include "strings.h" #include "decompress.h" +#include "tv.h" EWRAM_DATA static u8 sMoneyBoxWindowId = 0; EWRAM_DATA static u8 sMoneyLabelSpriteId = 0; @@ -130,23 +131,33 @@ void SubtractMoneyFromVar0x8005(void) void PrintMoneyAmountInMoneyBox(u8 windowId, int amount, u8 speed) { - PrintMoneyAmount(windowId, 38, 1, amount, speed); + PrintMoneyAmount(windowId, CalculateMoneyTextHorizontalPosition(amount), 1, amount, speed); +} + +static u32 CalculateLeadingSpacesForMoney(u32 numDigits) +{ + u32 leadingSpaces = CountDigits(INT_MAX) - StringLength(gStringVar1); + return (numDigits > 8) ? leadingSpaces : leadingSpaces - 2; } void PrintMoneyAmount(u8 windowId, u8 x, u8 y, int amount, u8 speed) { - u8 *txtPtr; - s32 strLength; + u8 *txtPtr = gStringVar4; + u32 numDigits = CountDigits(amount); + u32 maxDigits = (numDigits > 6) ? MAX_MONEY_DIGITS: 6; + u32 leadingSpaces; - ConvertIntToDecimalStringN(gStringVar1, amount, STR_CONV_MODE_LEFT_ALIGN, 6); + ConvertIntToDecimalStringN(gStringVar1, amount, STR_CONV_MODE_LEFT_ALIGN, maxDigits); - strLength = 6 - StringLength(gStringVar1); - txtPtr = gStringVar4; + leadingSpaces = CalculateLeadingSpacesForMoney(numDigits); - while (strLength-- > 0) + while (leadingSpaces-- > 0) *(txtPtr++) = CHAR_SPACER; StringExpandPlaceholders(txtPtr, gText_PokedollarVar1); + + if (numDigits > 8) + PrependFontIdToFit(gStringVar4, txtPtr + 1 + numDigits, FONT_NORMAL, 54); AddTextPrinterParameterized(windowId, FONT_NORMAL, gStringVar4, x, y, speed, NULL); } @@ -161,6 +172,11 @@ void ChangeAmountInMoneyBox(int amount) PrintMoneyAmountInMoneyBox(sMoneyBoxWindowId, amount, 0); } +u32 CalculateMoneyTextHorizontalPosition(u32 amount) +{ + return (CountDigits(amount) > 8) ? 34 : 26; +} + void DrawMoneyBox(int amount, u8 x, u8 y) { struct WindowTemplate template; diff --git a/src/shop.c b/src/shop.c index 3523aa86b1..77f2039e3b 100644 --- a/src/shop.c +++ b/src/shop.c @@ -1035,7 +1035,7 @@ static void Task_BuyMenu(u8 taskId) else { StringCopy(gStringVar1, gDecorations[itemId].name); - ConvertIntToDecimalStringN(gStringVar2, sShopData->totalCost, STR_CONV_MODE_LEFT_ALIGN, 6); + ConvertIntToDecimalStringN(gStringVar2, sShopData->totalCost, STR_CONV_MODE_LEFT_ALIGN, MAX_MONEY_DIGITS); if (sMartInfo.martType == MART_TYPE_DECOR) StringExpandPlaceholders(gStringVar4, gText_Var1IsItThatllBeVar2); @@ -1097,7 +1097,7 @@ static void Task_BuyHowManyDialogueHandleInput(u8 taskId) PutWindowTilemap(WIN_ITEM_LIST); CopyItemName(tItemId, gStringVar1); ConvertIntToDecimalStringN(gStringVar2, tItemCount, STR_CONV_MODE_LEFT_ALIGN, MAX_ITEM_DIGITS); - ConvertIntToDecimalStringN(gStringVar3, sShopData->totalCost, STR_CONV_MODE_LEFT_ALIGN, 6); + ConvertIntToDecimalStringN(gStringVar3, sShopData->totalCost, STR_CONV_MODE_LEFT_ALIGN, MAX_MONEY_DIGITS); BuyMenuDisplayMessage(taskId, gText_Var1AndYouWantedVar2, BuyMenuConfirmPurchase); } else if (JOY_NEW(B_BUTTON)) @@ -1226,7 +1226,7 @@ static void BuyMenuPrintItemQuantityAndPrice(u8 taskId) s16 *data = gTasks[taskId].data; FillWindowPixelBuffer(WIN_QUANTITY_PRICE, PIXEL_FILL(1)); - PrintMoneyAmount(WIN_QUANTITY_PRICE, 38, 1, sShopData->totalCost, TEXT_SKIP_DRAW); + PrintMoneyAmount(WIN_QUANTITY_PRICE, CalculateMoneyTextHorizontalPosition(sShopData->totalCost), 1, sShopData->totalCost, TEXT_SKIP_DRAW); ConvertIntToDecimalStringN(gStringVar1, tItemCount, STR_CONV_MODE_LEADING_ZEROS, MAX_ITEM_DIGITS); StringExpandPlaceholders(gStringVar4, gText_xVar1); BuyMenuPrint(WIN_QUANTITY_PRICE, gStringVar4, 0, 1, 0, COLORID_NORMAL); diff --git a/src/trainer_card.c b/src/trainer_card.c index 225e8241ac..29e94bfe9f 100755 --- a/src/trainer_card.c +++ b/src/trainer_card.c @@ -1045,7 +1045,7 @@ static void PrintMoneyOnCard(void) else AddTextPrinterParameterized3(WIN_CARD_TEXT, FONT_NORMAL, 16, 57, sTrainerCardTextColors, TEXT_SKIP_DRAW, gText_TrainerCardMoney); - ConvertIntToDecimalStringN(gStringVar1, sData->trainerCard.money, STR_CONV_MODE_LEFT_ALIGN, 6); + ConvertIntToDecimalStringN(gStringVar1, sData->trainerCard.money, STR_CONV_MODE_LEFT_ALIGN, MAX_MONEY_DIGITS); StringExpandPlaceholders(gStringVar4, gText_PokedollarVar1); if (!sData->isHoenn) { diff --git a/src/tv.c b/src/tv.c index 5530e519f6..e0f910eaaf 100644 --- a/src/tv.c +++ b/src/tv.c @@ -2760,16 +2760,14 @@ void ConvertIntToDecimalString(u8 varIdx, int value) size_t CountDigits(int value) { - if (value / 10 == 0) return 1; - if (value / 100 == 0) return 2; - if (value / 1000 == 0) return 3; - if (value / 10000 == 0) return 4; - if (value / 100000 == 0) return 5; - if (value / 1000000 == 0) return 6; - if (value / 10000000 == 0) return 7; - if (value / 100000000 == 0) return 8; + u32 count = 0; - return 1; + while (value > 0) + { + value /= 10; + count++; + } + return count; } static void SmartShopper_BufferPurchaseTotal(u8 varIdx, TVShow *show)