Skip to content

Commit

Permalink
Merge pull request #1908 from GriffinRichards/name-buffer
Browse files Browse the repository at this point in the history
Automatically update nickname buffer sizes
  • Loading branch information
GriffinRichards authored Aug 13, 2023
2 parents c5fb5fb + 344fe4d commit 4c2b125
Show file tree
Hide file tree
Showing 20 changed files with 83 additions and 73 deletions.
6 changes: 5 additions & 1 deletion include/battle_message.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#ifndef GUARD_BATTLE_MESSAGE_H
#define GUARD_BATTLE_MESSAGE_H

#define TEXT_BUFF_ARRAY_COUNT 16
// This buffer can hold many different things. Some of the things it can hold
// that have explicit sizes are listed below to ensure it can contain them.
#define TEXT_BUFF_ARRAY_COUNT max(16, \
max(MOVE_NAME_LENGTH + 2, /* +2 to hold the "!" and EOS. */ \
POKEMON_NAME_LENGTH + 1))

// for 0xFD
#define B_TXT_BUFF1 0x0
Expand Down
1 change: 1 addition & 0 deletions include/constants/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
// string lengths
#define ITEM_NAME_LENGTH 14
#define POKEMON_NAME_LENGTH 10
#define POKEMON_NAME_BUFFER_SIZE max(20, POKEMON_NAME_LENGTH + 1) // Frequently used buffer size. Larger than necessary
#define PLAYER_NAME_LENGTH 7
#define MAIL_WORDS_COUNT 9
#define EASY_CHAT_BATTLE_WORDS_COUNT 6
Expand Down
2 changes: 1 addition & 1 deletion src/battle_controller_link_opponent.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ static u32 CopyLinkOpponentMonData(u8 monId, u8 *dst)
{
struct BattlePokemon battleMon;
struct MovePpInfo moveData;
u8 nickname[20];
u8 nickname[POKEMON_NAME_BUFFER_SIZE];
u8 *src;
s16 data16;
u32 data32;
Expand Down
2 changes: 1 addition & 1 deletion src/battle_controller_link_partner.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ static u32 CopyLinkPartnerMonData(u8 monId, u8 *dst)
{
struct BattlePokemon battleMon;
struct MovePpInfo moveData;
u8 nickname[20];
u8 nickname[POKEMON_NAME_BUFFER_SIZE];
u8 *src;
s16 data16;
u32 data32;
Expand Down
2 changes: 1 addition & 1 deletion src/battle_controller_opponent.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ static u32 GetOpponentMonData(u8 monId, u8 *dst)
{
struct BattlePokemon battleMon;
struct MovePpInfo moveData;
u8 nickname[20];
u8 nickname[POKEMON_NAME_BUFFER_SIZE];
u8 *src;
s16 data16;
u32 data32;
Expand Down
2 changes: 1 addition & 1 deletion src/battle_controller_player.c
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,7 @@ static u32 CopyPlayerMonData(u8 monId, u8 *dst)
{
struct BattlePokemon battleMon;
struct MovePpInfo moveData;
u8 nickname[20];
u8 nickname[POKEMON_NAME_BUFFER_SIZE];
u8 *src;
s16 data16;
u32 data32;
Expand Down
2 changes: 1 addition & 1 deletion src/battle_controller_player_partner.c
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ static u32 CopyPlayerPartnerMonData(u8 monId, u8 *dst)
{
struct BattlePokemon battleMon;
struct MovePpInfo moveData;
u8 nickname[20];
u8 nickname[POKEMON_NAME_BUFFER_SIZE];
u8 *src;
s16 data16;
u32 data32;
Expand Down
2 changes: 1 addition & 1 deletion src/battle_controller_recorded_opponent.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ static u32 CopyRecordedOpponentMonData(u8 monId, u8 *dst)
{
struct BattlePokemon battleMon;
struct MovePpInfo moveData;
u8 nickname[20];
u8 nickname[POKEMON_NAME_BUFFER_SIZE];
u8 *src;
s16 data16;
u32 data32;
Expand Down
2 changes: 1 addition & 1 deletion src/battle_controller_recorded_player.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ static u32 CopyRecordedPlayerMonData(u8 monId, u8 *dst)
{
struct BattlePokemon battleMon;
struct MovePpInfo moveData;
u8 nickname[20];
u8 nickname[POKEMON_NAME_BUFFER_SIZE];
u8 *src;
s16 data16;
u32 data32;
Expand Down
2 changes: 1 addition & 1 deletion src/battle_controller_wally.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ static u32 CopyWallyMonData(u8 monId, u8 *dst)
{
struct BattlePokemon battleMon;
struct MovePpInfo moveData;
u8 nickname[20];
u8 nickname[POKEMON_NAME_BUFFER_SIZE];
u8 *src;
s16 data16;
u32 data32;
Expand Down
26 changes: 16 additions & 10 deletions src/battle_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,8 @@ const u8 gText_Draw[] = _("{HIGHLIGHT TRANSPARENT}Draw");
static const u8 sText_SpaceIs[] = _(" is");
static const u8 sText_ApostropheS[] = _("'s");

// For displaying names of invalid moves
// For displaying names of invalid moves.
// This is large enough that the text for TYPE_ELECTRIC will exceed TEXT_BUFF_ARRAY_COUNT.
static const u8 sATypeMove_Table[NUMBER_OF_MON_TYPES][17] =
{
[TYPE_NORMAL] = _("a NORMAL move"),
Expand Down Expand Up @@ -1472,7 +1473,7 @@ static const u16 sGrammarMoveUsedTable[] =
MOVE_COVET, 0
};

static const u8 sDummyWeirdStatusString[] = {EOS, EOS, EOS, EOS, EOS, EOS, EOS, EOS, 0, 0};
static const u8 sText_EmptyStatus[] = _("$$$$$$$");

static const struct BattleWindowText sTextOnWindowsInfo_Normal[] =
{
Expand Down Expand Up @@ -2259,10 +2260,10 @@ static const u8 *TryGetStatusString(u8 *src)
u32 chars1, chars2;
u8 *statusPtr;

memcpy(status, sDummyWeirdStatusString, 8);
memcpy(status, sText_EmptyStatus, min(ARRAY_COUNT(status), ARRAY_COUNT(sText_EmptyStatus)));

statusPtr = status;
for (i = 0; i < 8; i++)
for (i = 0; i < ARRAY_COUNT(status); i++)
{
if (*src == EOS) break; // one line required to match -g
*statusPtr = *src;
Expand Down Expand Up @@ -2304,11 +2305,16 @@ static const u8 *TryGetStatusString(u8 *src)
StringGet_Nickname(text); \
toCpy = text;

// Ensure the defined length for an item name can contain the full defined length of a berry name.
// This ensures that custom Enigma Berry names will fit in the text buffer at the top of BattleStringExpandPlaceholders.
STATIC_ASSERT(BERRY_NAME_LENGTH + ARRAY_COUNT(sText_BerrySuffix) <= ITEM_NAME_LENGTH, BerryNameTooLong);

u32 BattleStringExpandPlaceholders(const u8 *src, u8 *dst)
{
u32 dstID = 0; // if they used dstID, why not use srcID as well?
const u8 *toCpy = NULL;
u8 text[30];
// This buffer may hold either the name of a trainer, pokemon, or item.
u8 text[max(max(max(32, TRAINER_NAME_LENGTH + 1), POKEMON_NAME_LENGTH + 1), ITEM_NAME_LENGTH)];
u8 multiplayerId;
s32 i;

Expand Down Expand Up @@ -2753,7 +2759,7 @@ static void ExpandBattleTextBuffPlaceholders(const u8 *src, u8 *dst)
{
u32 srcID = 1;
u32 value = 0;
u8 text[12];
u8 nickname[POKEMON_NAME_LENGTH + 1];
u16 hword;

*dst = EOS;
Expand Down Expand Up @@ -2793,7 +2799,7 @@ static void ExpandBattleTextBuffPlaceholders(const u8 *src, u8 *dst)
case B_BUFF_MON_NICK_WITH_PREFIX: // poke nick with prefix
if (GetBattlerSide(src[srcID + 1]) == B_SIDE_PLAYER)
{
GetMonData(&gPlayerParty[src[srcID + 2]], MON_DATA_NICKNAME, text);
GetMonData(&gPlayerParty[src[srcID + 2]], MON_DATA_NICKNAME, nickname);
}
else
{
Expand All @@ -2802,10 +2808,10 @@ static void ExpandBattleTextBuffPlaceholders(const u8 *src, u8 *dst)
else
StringAppend(dst, sText_WildPkmnPrefix);

GetMonData(&gEnemyParty[src[srcID + 2]], MON_DATA_NICKNAME, text);
GetMonData(&gEnemyParty[src[srcID + 2]], MON_DATA_NICKNAME, nickname);
}
StringGet_Nickname(text);
StringAppend(dst, text);
StringGet_Nickname(nickname);
StringAppend(dst, nickname);
srcID += 3;
break;
case B_BUFF_STAT: // stats
Expand Down
20 changes: 13 additions & 7 deletions src/contest.c
Original file line number Diff line number Diff line change
Expand Up @@ -2775,7 +2775,7 @@ static bool8 IsPlayerLinkLeader(void)

void CreateContestMonFromParty(u8 partyIndex)
{
u8 name[20];
u8 name[max(PLAYER_NAME_LENGTH + 1, POKEMON_NAME_BUFFER_SIZE)];
u16 heldItem;
s16 cool;
s16 beauty;
Expand All @@ -2785,10 +2785,8 @@ void CreateContestMonFromParty(u8 partyIndex)

StringCopy(name, gSaveBlock2Ptr->playerName);
if (gLinkContestFlags & LINK_CONTEST_FLAG_IS_LINK)
{
StripPlayerNameForLinkContest(name);
}
memcpy(gContestMons[gContestPlayerMonIndex].trainerName, name, 8);
memcpy(gContestMons[gContestPlayerMonIndex].trainerName, name, PLAYER_NAME_LENGTH + 1);
if (gSaveBlock2Ptr->playerGender == MALE)
gContestMons[gContestPlayerMonIndex].trainerGfxId = OBJ_EVENT_GFX_LINK_BRENDAN;
else
Expand Down Expand Up @@ -6020,8 +6018,10 @@ static u8 GetMonNicknameLanguage(u8 *nickname)
if (nickname[0] == EXT_CTRL_CODE_BEGIN && nickname[1] == EXT_CTRL_CODE_JPN)
return GAME_LANGUAGE;

if (StringLength(nickname) < PLAYER_NAME_LENGTH - 1)
if (StringLength(nickname) <= 5)
{
// Name is short enough that it might be Japanese.
// Make sure all the character values are valid latin name characters.
while (*nickname != EOS)
{
if ((*nickname >= CHAR_A && *nickname <= CHAR_z)
Expand All @@ -6039,12 +6039,18 @@ static u8 GetMonNicknameLanguage(u8 *nickname)
|| *nickname == CHAR_DBL_QUOTE_LEFT
|| *nickname == CHAR_DBL_QUOTE_RIGHT
|| *nickname == CHAR_SGL_QUOTE_LEFT
|| *nickname == CHAR_DBL_QUOTE_LEFT) // Most likely a typo, CHAR_SGL_QUOTE_RIGHT should be here instead.
#ifdef BUGFIX
|| *nickname == CHAR_SGL_QUOTE_RIGHT
#else
|| *nickname == CHAR_DBL_QUOTE_LEFT // Most likely a typo, CHAR_SGL_QUOTE_RIGHT should be here instead.
#endif
)
{
nickname++;
}
else
{
// Invalid latin name character, assume the name was Japanese.
ret = LANGUAGE_JAPANESE;
break;
}
Expand Down Expand Up @@ -6101,7 +6107,7 @@ void StripPlayerAndMonNamesForLinkContest(struct ContestPokemon *mon, s32 langua
name = mon->trainerName;
if (language == LANGUAGE_JAPANESE)
{
name[PLAYER_NAME_LENGTH] = EOS;
name[7] = EOS;
name[6] = name[4];
name[5] = name[3];
name[4] = name[2];
Expand Down
17 changes: 7 additions & 10 deletions src/daycare.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,14 @@ static const u8 sJapaneseEggNickname[] = _("タマゴ"); // "tamago" ("egg" in J

u8 *GetMonNickname2(struct Pokemon *mon, u8 *dest)
{
u8 nickname[POKEMON_NAME_LENGTH * 2];

u8 nickname[POKEMON_NAME_BUFFER_SIZE];
GetMonData(mon, MON_DATA_NICKNAME, nickname);
return StringCopy_Nickname(dest, nickname);
}

u8 *GetBoxMonNickname(struct BoxPokemon *mon, u8 *dest)
{
u8 nickname[POKEMON_NAME_LENGTH * 2];

u8 nickname[POKEMON_NAME_BUFFER_SIZE];
GetBoxMonData(mon, MON_DATA_NICKNAME, nickname);
return StringCopy_Nickname(dest, nickname);
}
Expand Down Expand Up @@ -946,12 +944,12 @@ static bool8 IsEggPending(struct DayCare *daycare)
// gStringVar3 = first mon trainer's name
static void _GetDaycareMonNicknames(struct DayCare *daycare)
{
u8 text[12];
u8 otName[max(12, PLAYER_NAME_LENGTH + 1)];
if (GetBoxMonData(&daycare->mons[0].mon, MON_DATA_SPECIES) != 0)
{
GetBoxMonNickname(&daycare->mons[0].mon, gStringVar1);
GetBoxMonData(&daycare->mons[0].mon, MON_DATA_OT_NAME, text);
StringCopy(gStringVar3, text);
GetBoxMonData(&daycare->mons[0].mon, MON_DATA_OT_NAME, otName);
StringCopy(gStringVar3, otName);
}

if (GetBoxMonData(&daycare->mons[1].mon, MON_DATA_SPECIES) != 0)
Expand Down Expand Up @@ -1146,7 +1144,7 @@ static u8 *AppendMonGenderSymbol(u8 *name, struct BoxPokemon *boxMon)

static void GetDaycareLevelMenuText(struct DayCare *daycare, u8 *dest)
{
u8 monNames[DAYCARE_MON_COUNT][20];
u8 monNames[DAYCARE_MON_COUNT][POKEMON_NAME_BUFFER_SIZE];
u8 i;

*dest = EOS;
Expand Down Expand Up @@ -1204,8 +1202,7 @@ static void DaycareAddTextPrinter(u8 windowId, const u8 *text, u32 x, u32 y)

static void DaycarePrintMonNickname(struct DayCare *daycare, u8 windowId, u32 daycareSlotId, u32 y)
{
u8 nickname[POKEMON_NAME_LENGTH * 2];

u8 nickname[POKEMON_NAME_BUFFER_SIZE];
GetBoxMonNickname(&daycare->mons[daycareSlotId].mon, nickname);
AppendMonGenderSymbol(nickname, &daycare->mons[daycareSlotId].mon);
DaycareAddTextPrinter(windowId, nickname, 8, y);
Expand Down
2 changes: 1 addition & 1 deletion src/egg_hatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ void ScriptHatchMon(void)

static bool8 _CheckDaycareMonReceivedMail(struct DayCare *daycare, u8 daycareId)
{
u8 nickname[32];
u8 nickname[max(32, POKEMON_NAME_BUFFER_SIZE)];
struct DaycareMon *daycareMon = &daycare->mons[daycareId];

GetBoxMonNickname(&daycareMon->mon, nickname);
Expand Down
18 changes: 8 additions & 10 deletions src/evolution_scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void BeginEvolutionScene(struct Pokemon *mon, u16 postEvoSpecies, bool8 canStopE

void EvolutionScene(struct Pokemon *mon, u16 postEvoSpecies, bool8 canStopEvo, u8 partyId)
{
u8 name[20];
u8 name[POKEMON_NAME_BUFFER_SIZE];
u16 currSpecies;
u32 trainerId, personality;
const struct CompressedSpritePalette* pokePal;
Expand Down Expand Up @@ -467,7 +467,7 @@ static void CB2_TradeEvolutionSceneLoadGraphics(void)

void TradeEvolutionScene(struct Pokemon *mon, u16 postEvoSpecies, u8 preEvoSpriteId, u8 partyId)
{
u8 name[20];
u8 name[POKEMON_NAME_BUFFER_SIZE];
u16 currSpecies;
u32 trainerId, personality;
const struct CompressedSpritePalette* pokePal;
Expand Down Expand Up @@ -775,8 +775,7 @@ static void Task_EvolutionScene(u8 taskId)
var = MonTryLearningNewMove(mon, gTasks[taskId].tLearnsFirstMove);
if (var != MOVE_NONE && !gTasks[taskId].tEvoWasStopped)
{
u8 text[20];

u8 nickname[POKEMON_NAME_BUFFER_SIZE];
if (!(gTasks[taskId].tBits & TASK_BIT_LEARN_MOVE))
{
StopMapMusic();
Expand All @@ -786,8 +785,8 @@ static void Task_EvolutionScene(u8 taskId)
gTasks[taskId].tBits |= TASK_BIT_LEARN_MOVE;
gTasks[taskId].tLearnsFirstMove = FALSE;
gTasks[taskId].tLearnMoveState = MVSTATE_INTRO_MSG_1;
GetMonData(mon, MON_DATA_NICKNAME, text);
StringCopy_Nickname(gBattleTextBuff1, text);
GetMonData(mon, MON_DATA_NICKNAME, nickname);
StringCopy_Nickname(gBattleTextBuff1, nickname);

if (var == MON_HAS_MAX_MOVES)
gTasks[taskId].tState = EVOSTATE_REPLACE_MOVE;
Expand Down Expand Up @@ -1195,13 +1194,12 @@ static void Task_TradeEvolutionScene(u8 taskId)
var = MonTryLearningNewMove(mon, gTasks[taskId].tLearnsFirstMove);
if (var != MOVE_NONE && !gTasks[taskId].tEvoWasStopped)
{
u8 text[20];

u8 nickname[POKEMON_NAME_BUFFER_SIZE];
gTasks[taskId].tBits |= TASK_BIT_LEARN_MOVE;
gTasks[taskId].tLearnsFirstMove = FALSE;
gTasks[taskId].tLearnMoveState = 0;
GetMonData(mon, MON_DATA_NICKNAME, text);
StringCopy_Nickname(gBattleTextBuff1, text);
GetMonData(mon, MON_DATA_NICKNAME, nickname);
StringCopy_Nickname(gBattleTextBuff1, nickname);

if (var == MON_HAS_MAX_MOVES)
gTasks[taskId].tState = T_EVOSTATE_REPLACE_MOVE;
Expand Down
Loading

0 comments on commit 4c2b125

Please sign in to comment.