Skip to content

Commit f6b7d2c

Browse files
committed
Add current work for custom states
Also changed the following: 1. setCustomText and setCustomTextButtonControls were modified to take a bool for deciding if a null terminator should be applied to textOut. setCustomText also returns an int32_t now. 2. ReloadRoomStruct variables NewBero and NewMap are no longer null terminated. The size of NewBero was also halved. 3. customTextInit was moved to global.cpp to not be inlined. 4. reloadRoomMain now manually applies null terminators to the map and bero buffers. 5. setNextMap and setNextBero now use temporary buffers for their strings, and now manually apply null terminators. 6. saveSettings now creates a new file if the new data would use more/less blocks than the data currently on the memory card. 7. loadSettings now allocates the read buffer on the smart heap to help reduce fragmentation with dynamic data in the settings file.
1 parent ab21631 commit f6b7d2c

File tree

11 files changed

+1032
-89
lines changed

11 files changed

+1032
-89
lines changed

ttyd-tools/rel/include/draw.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void drawMultipleColumnsVertical(int32_t posX, int32_t posY, uint32_t currentMen
5959
uint32_t maxOptionsPerRow, bool showCurrentOption,
6060
uint32_t posXIncrementAmount, const char **lines);
6161

62-
bool setCustomText(char *textOut, uint32_t textSize);
62+
int32_t setCustomText(char *textOut, uint32_t textSize, bool applyNullTerminator);
6363

6464
void drawInventoryIconAndTextColumns();
6565

@@ -139,6 +139,8 @@ void drawCheatsResolveFades();
139139
void drawCheatsLockFlags();
140140
void drawCheatsManageFlagsMain(uint32_t currentMenu);
141141
void drawCheatsClearArea();
142+
void drawCheatsManageCustomStates();
143+
void drawCheatsHandleCustomStateAction();
142144
void drawDisplaysMemoryUsageMenu();
143145
void drawWarpsOptions();
144146
void drawEventDetails(int32_t posX, int32_t posY, int32_t index);

ttyd-tools/rel/include/global.h

Lines changed: 95 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
#include <gc/pad.h>
77
#include <gc/DEMOPad.h>
88
#include <ttyd/item_data.h>
9+
#include <ttyd/mario_pouch.h>
10+
#include <ttyd/party.h>
911
#include <ttyd/mapdata.h>
10-
#include <ttyd/msgdrv.h>
1112

1213
#include <cstdint>
13-
#include <cstring>
1414

1515
namespace mod {
1616

@@ -39,6 +39,7 @@ enum MENU_NAMES
3939
CHEATS_MANAGE_FLAGS,
4040
CHEATS_MANAGE_FLAGS_MAIN,
4141
CHEATS_CLEAR_AREA_FLAGS,
42+
CHEATS_MANAGE_CUSTOM_STATES,
4243
STATS_MARIO,
4344
STATS_PARTNERS,
4445
STATS_FOLLOWERS,
@@ -108,6 +109,7 @@ enum CHEATS_OPTIONS
108109
LOCK_FLAGS,
109110
MANAGE_FLAGS,
110111
CLEAR_AREA_FLAGS,
112+
MANAGE_CUSTOM_STATES,
111113
};
112114

113115
enum CHEATS_CHANGE_SEQUENCE_SELECTION
@@ -238,6 +240,22 @@ enum CHEATS_CLEAR_AREA_FLAGS_AREAS
238240
AREA_JON,
239241
};
240242

243+
enum CHEATS_MANAGE_CUSTOM_STATES_SELECTION
244+
{
245+
LOAD_CUSTOM_STATE = 1,
246+
CREATE_CUSTOM_STATE,
247+
DELETE_CUSTOM_STATE,
248+
RENAME_CUSTOM_STATE,
249+
};
250+
251+
enum CHEATS_MANAGE_CUSTOM_STATES_RETURN_VALUES
252+
{
253+
NO_STATES_EXIST = -4,
254+
MAX_STATES_EXIST,
255+
STATES_CREATE_NOT_IN_GAME,
256+
STATES_WARP_NOT_IN_GAME,
257+
};
258+
241259
// Various menu values
242260
#define NO_NUMBERS_TO_DISPLAY 0x2000
243261
#define ADDING_BY_ID 0x2008
@@ -688,8 +706,8 @@ struct ReloadRoomStruct
688706
{
689707
bool ManuallyReloadingRoom;
690708
bool SystemLevelShouldBeLowered;
691-
char NewBero[32]; // 31 bytes for NextBero, 1 byte for NULL
692-
char NewMap[9]; // 8 bytes for NextMap, 1 byte for NULL
709+
char NewBero[16]; // Not null terminated
710+
char NewMap[8]; // Not null terminated
693711
};
694712

695713
struct SpawnItems
@@ -991,6 +1009,65 @@ struct FrameAdvanceStruct
9911009
}
9921010
};
9931011

1012+
struct CustomStateMarioVars
1013+
{
1014+
int16_t currentHP;
1015+
int16_t maxHP;
1016+
int16_t currentFP;
1017+
int16_t maxFP;
1018+
int16_t maxHPEnteringBattle;
1019+
int16_t maxFPEnteringBattle;
1020+
int16_t currentSP;
1021+
int16_t maxSP;
1022+
int16_t availableBP;
1023+
int16_t maxBP;
1024+
int16_t rank;
1025+
int16_t level;
1026+
uint16_t starPowersObtained;
1027+
int16_t starPoints;
1028+
int16_t coins;
1029+
} __attribute__((__packed__));
1030+
1031+
struct CustomStateStruct
1032+
{
1033+
int16_t StandardItems[20];
1034+
int16_t ImportantItems[121];
1035+
int16_t Badges[200];
1036+
int16_t EquippedBadges[200];
1037+
int16_t StoredItems[32];
1038+
uint16_t SequencePosition;
1039+
CustomStateMarioVars MarioVars;
1040+
ttyd::mario_pouch::PouchPartyData PartyData[7];
1041+
ttyd::party::PartyMembers PartnerOut;
1042+
ttyd::party::PartyMembers FollowerOut;
1043+
bool MarioIsShip;
1044+
char StateName[16]; // Does not include null terminator
1045+
char CurrentMap[8]; // Does not include null terminator
1046+
char CurrentBero[16]; // Does not include null terminator
1047+
uint8_t padding;
1048+
} __attribute__((__packed__));
1049+
1050+
struct ManageCustomStates
1051+
{
1052+
#define CUSTOM_STATES_MAX_COUNT 10
1053+
#define CUSTOM_STATES_MAX_NAMES_PER_PAGE 18
1054+
1055+
CustomStateStruct *State;
1056+
uint8_t TotalEntries;
1057+
bool StateWasSelected;
1058+
uint8_t SelectedState;
1059+
1060+
// Do not inline createCustomState nor deleteCustomState
1061+
char *createCustomState();
1062+
uint32_t deleteCustomState(uint32_t stateIndex);
1063+
};
1064+
1065+
struct CustomStateSettingsStruct
1066+
{
1067+
uint32_t CustomStateCount;
1068+
uint32_t OffsetToCustomStates;
1069+
} __attribute__((__packed__));
1070+
9941071
struct SettingsStruct
9951072
{
9961073
bool CheatsActive[100];
@@ -1000,6 +1077,7 @@ struct SettingsStruct
10001077
MemoryWatchStruct MemoryWatchSettings[60];
10011078
MemoryEditorSaveStruct MemoryEditorSave;
10021079
FrameAdvanceButtonCombosStruct FrameAdvanceButtonCombos;
1080+
CustomStateSettingsStruct CustomStateSettings;
10031081
} __attribute__((__packed__));
10041082

10051083
struct SaveFileDecriptionInfo
@@ -1117,6 +1195,10 @@ struct SetCustomText
11171195
#define CUSTOM_TEXT_CANCEL 0x1100
11181196
#define CUSTOM_TEXT_DONE 0x1200
11191197

1198+
#define CUSTOM_TEXT_RETURN_CANCEL -1
1199+
#define CUSTOM_TEXT_RETURN_BUSY 0
1200+
#define CUSTOM_TEXT_RETURN_DONE 1
1201+
11201202
char *Buffer;
11211203
const char *CharsToChooseFrom;
11221204
uint8_t CharsLength;
@@ -1131,51 +1213,21 @@ struct SetCustomText
11311213
CharsPerRow = 1 + ((CharsLength - 1) / CUSTOM_TEXT_TOTAL_CHARS_ROWS); // Round up
11321214
}
11331215

1134-
void customTextInit(const char *initialText, uint32_t maxTextSize)
1216+
~SetCustomText()
11351217
{
1136-
char *tempBuffer = reinterpret_cast<char *>(
1137-
clearMemory(Buffer, CUSTOM_TEXT_BUFFER_SIZE));
1138-
1139-
if (initialText)
1218+
if (Buffer)
11401219
{
1141-
#ifdef TTYD_JP
1142-
// Custom text doesn't currently support Japanese characters
1143-
if (ttyd::msgdrv::_ismbblead(initialText[0]))
1144-
{
1145-
// Text starts with a Japanese character
1146-
CurrentIndex = 0;
1147-
}
1148-
else
1149-
{
1150-
#endif
1151-
uint32_t MaxIndex = maxTextSize - 1;
1152-
if (MaxIndex > (CUSTOM_TEXT_BUFFER_SIZE - 1))
1153-
{
1154-
MaxIndex = (CUSTOM_TEXT_BUFFER_SIZE - 1);
1155-
}
1156-
1157-
uint32_t Length = strlen(initialText);
1158-
if (Length > MaxIndex)
1159-
{
1160-
Length = MaxIndex;
1161-
}
1162-
1163-
CurrentIndex = static_cast<uint8_t>(Length);
1164-
strncpy(tempBuffer, initialText, Length);
1165-
#ifdef TTYD_JP
1166-
}
1167-
#endif
1168-
}
1169-
else
1170-
{
1171-
CurrentIndex = 0;
1220+
delete[] (Buffer);
11721221
}
11731222
}
1223+
1224+
// Do not inline customTextInit
1225+
void customTextInit(const char *initialText, uint32_t maxTextSize);
11741226
};
11751227

11761228
extern MenuVars MenuVar;
1177-
extern Menus Menu[38];
1178-
extern Cheats Cheat[28];
1229+
extern Menus Menu[39];
1230+
extern Cheats Cheat[29];
11791231
extern bool Displays[19];
11801232
extern char DisplayBuffer[256];
11811233
extern MemoryWatchStruct MemoryWatch[60];
@@ -1212,6 +1264,7 @@ extern EnemyEncounterNotifierStruct EnemyEncounterNotifier;
12121264
extern FrameAdvanceStruct FrameAdvance;
12131265
extern UnusedMapStruct UnusedMap;
12141266
extern SetCustomText CustomText;
1267+
extern ManageCustomStates CustomState;
12151268

12161269
extern uint8_t CheatsOrder[];
12171270
extern uint16_t StatsMarioIcons[];

ttyd-tools/rel/include/menufunctions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ uint32_t marioSpecialMovesButtonControls();
4949
uint32_t partnerChangeYoshiColorButtonControls();
5050
uint32_t followersOptionsButtonControls();
5151

52-
uint32_t setCustomTextButtonControls(char *textOut, uint32_t textSize);
52+
uint32_t setCustomTextButtonControls(char *textOut, uint32_t textSize, bool applyNullTerminator);
5353

5454
void adjustMenuItemBoundsMain(int32_t valueChangedBy, int32_t lowerBound, int32_t upperBound);
5555
void adjustMenuItemBoundsMainUnsigned(int32_t valueChangedBy, uint32_t lowerBound, uint32_t upperBound);
@@ -123,6 +123,7 @@ void adjustCheatsResolveFadesSelection(uint32_t button);
123123
void adjustCheatsManageFlagsMainMenu(uint32_t button);
124124
void adjustMenuSelectionInventory(uint32_t button);
125125
void adjustCheatClearAreaFlagSelection(uint32_t button);
126+
void adjustCustomStatesSelection(uint32_t button);
126127
void adjustMarioStatsSelection(uint32_t button);
127128
void adjustPartnerStatsSelection(uint32_t button);
128129
void adjustMemoryWatchSelection(uint32_t button);

ttyd-tools/rel/include/ttyd/mario_pouch.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44

55
namespace ttyd::mario_pouch {
66

7+
struct PouchPartyData
8+
{
9+
uint16_t flags;
10+
int16_t maxHP;
11+
int16_t maxHPEnteringBattle;
12+
int16_t currentHP;
13+
int16_t hpLevel;
14+
int16_t attackLevel;
15+
int16_t techLevel;
16+
} __attribute__((__packed__));
17+
18+
static_assert(sizeof(PouchPartyData) == 0xE);
19+
720
extern "C" {
821

922
const char *pouchGetYoshiName();

ttyd-tools/rel/source/codes.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,10 +551,15 @@ void reloadRoomMain()
551551
// NewBero and NewMap need to be global variables
552552

553553
char *tempNewBero = ReloadRoom.NewBero;
554+
constexpr uint32_t NewBeroSize = sizeof(ReloadRoom.NewBero);
555+
strncpy(tempNewBero, ttyd::seq_mapchange::NextBero, NewBeroSize);
556+
tempNewBero[NewBeroSize - 1] = '\0';
557+
558+
constexpr uint32_t NewMapSize = sizeof(ReloadRoom.NewMap);
554559
char *tempNewMap = ReloadRoom.NewMap;
560+
strncpy(tempNewMap, ttyd::seq_mapchange::NextMap, NewMapSize);
561+
tempNewMap[NewMapSize - 1] = '\0';
555562

556-
strcpy(tempNewBero, ttyd::seq_mapchange::NextBero);
557-
strcpy(tempNewMap, ttyd::seq_mapchange::NextMap);
558563
setSeqMapChange(tempNewMap, tempNewBero);
559564

560565
// Set a bool to prevent battles from starting

ttyd-tools/rel/source/commonfunctions.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,21 @@ void setSequencePosition(uint32_t value)
187187

188188
void setNextMap(const char *map)
189189
{
190-
strcpy(ttyd::seq_mapchange::NextMap, map);
191-
strncpy(ttyd::seq_mapchange::NextArea, map, 3);
190+
char tempMap[9];
191+
strncpy(tempMap, map, sizeof(tempMap) - 1);
192+
tempMap[sizeof(tempMap) - 1] = '\0';
193+
194+
strcpy(ttyd::seq_mapchange::NextMap, tempMap);
195+
strncpy(ttyd::seq_mapchange::NextArea, tempMap, 3);
192196
}
193197

194198
void setNextBero(const char *bero)
195199
{
196-
strcpy(ttyd::seq_mapchange::NextBero, bero);
200+
char tempBero[17];
201+
strncpy(tempBero, bero, sizeof(tempBero) - 1);
202+
tempBero[sizeof(tempBero) - 1] = '\0';
203+
204+
strcpy(ttyd::seq_mapchange::NextBero, tempBero);
197205
}
198206

199207
bool compareStrings(const char *str1, const char *str2)

0 commit comments

Comments
 (0)