From a240481d5ffe7f9c7a62faf0617dff888d84d745 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Fri, 5 Feb 2021 12:08:25 -0500 Subject: [PATCH] Document secret_base.c --- include/constants/global.h | 2 +- include/constants/secret_bases.h | 58 +++- include/global.h | 4 +- src/secret_base.c | 471 ++++++++++++++++++------------- 4 files changed, 333 insertions(+), 202 deletions(-) diff --git a/include/constants/global.h b/include/constants/global.h index 55830ae8b2..bc611879eb 100644 --- a/include/constants/global.h +++ b/include/constants/global.h @@ -70,7 +70,7 @@ // party sizes #define PARTY_SIZE 6 -#define MULTI_PARTY_SIZE PARTY_SIZE / 2 +#define MULTI_PARTY_SIZE (PARTY_SIZE / 2) #define FRONTIER_PARTY_SIZE 3 #define FRONTIER_DOUBLES_PARTY_SIZE 4 #define FRONTIER_MULTI_PARTY_SIZE 2 diff --git a/include/constants/secret_bases.h b/include/constants/secret_bases.h index 2f405317d9..e9188fbf5a 100755 --- a/include/constants/secret_bases.h +++ b/include/constants/secret_bases.h @@ -12,106 +12,160 @@ // The secret base's map is determined by (id / 10). The ones // digit is used to differentiate secret bases using the same map. // Therefore, each secret base map can be used by up to 10 different -// secret bases in the game. These ids are 1-based, but there is no -// apparent reason for that. +// secret bases in the game. + +// * 4 is for each byte of data per secret base group in sSecretBaseEntrancePositions +// They are the map number, the entrance warp id, and the x/y position in front of the computer +#define SECRET_BASE_GROUP(idx) ((idx) * 4) +#define SECRET_BASE_ID_TO_GROUP(baseId) SECRET_BASE_GROUP((baseId) / 10) #define SECRET_BASE_RED_CAVE1_1 1 #define SECRET_BASE_RED_CAVE1_2 2 #define SECRET_BASE_RED_CAVE1_3 3 +#define SECRET_BASE_RED_CAVE1 SECRET_BASE_GROUP(0) + #define SECRET_BASE_RED_CAVE2_1 11 #define SECRET_BASE_RED_CAVE2_2 12 #define SECRET_BASE_RED_CAVE2_3 13 +#define SECRET_BASE_RED_CAVE2 SECRET_BASE_GROUP(1) + #define SECRET_BASE_RED_CAVE3_1 21 #define SECRET_BASE_RED_CAVE3_2 22 #define SECRET_BASE_RED_CAVE3_3 23 +#define SECRET_BASE_RED_CAVE3 SECRET_BASE_GROUP(2) + #define SECRET_BASE_RED_CAVE4_1 31 #define SECRET_BASE_RED_CAVE4_2 32 #define SECRET_BASE_RED_CAVE4_3 33 +#define SECRET_BASE_RED_CAVE4 SECRET_BASE_GROUP(3) + #define SECRET_BASE_BROWN_CAVE1_1 41 #define SECRET_BASE_BROWN_CAVE1_2 42 #define SECRET_BASE_BROWN_CAVE1_3 43 +#define SECRET_BASE_BROWN_CAVE1 SECRET_BASE_GROUP(4) + #define SECRET_BASE_BROWN_CAVE2_1 51 #define SECRET_BASE_BROWN_CAVE2_2 52 #define SECRET_BASE_BROWN_CAVE2_3 53 +#define SECRET_BASE_BROWN_CAVE2 SECRET_BASE_GROUP(5) + #define SECRET_BASE_BROWN_CAVE3_1 61 #define SECRET_BASE_BROWN_CAVE3_2 62 #define SECRET_BASE_BROWN_CAVE3_3 63 +#define SECRET_BASE_BROWN_CAVE3 SECRET_BASE_GROUP(6) + #define SECRET_BASE_BROWN_CAVE4_1 71 #define SECRET_BASE_BROWN_CAVE4_2 72 #define SECRET_BASE_BROWN_CAVE4_3 73 +#define SECRET_BASE_BROWN_CAVE4 SECRET_BASE_GROUP(7) + #define SECRET_BASE_BLUE_CAVE1_1 81 #define SECRET_BASE_BLUE_CAVE1_2 82 #define SECRET_BASE_BLUE_CAVE1_3 83 +#define SECRET_BASE_BLUE_CAVE1 SECRET_BASE_GROUP(8) + #define SECRET_BASE_BLUE_CAVE2_1 91 #define SECRET_BASE_BLUE_CAVE2_2 92 #define SECRET_BASE_BLUE_CAVE2_3 93 +#define SECRET_BASE_BLUE_CAVE2 SECRET_BASE_GROUP(9) + #define SECRET_BASE_BLUE_CAVE3_1 101 #define SECRET_BASE_BLUE_CAVE3_2 102 #define SECRET_BASE_BLUE_CAVE3_3 103 +#define SECRET_BASE_BLUE_CAVE3 SECRET_BASE_GROUP(10) + #define SECRET_BASE_BLUE_CAVE4_1 111 #define SECRET_BASE_BLUE_CAVE4_2 112 #define SECRET_BASE_BLUE_CAVE4_3 113 +#define SECRET_BASE_BLUE_CAVE4 SECRET_BASE_GROUP(11) + #define SECRET_BASE_YELLOW_CAVE1_1 121 #define SECRET_BASE_YELLOW_CAVE1_2 122 #define SECRET_BASE_YELLOW_CAVE1_3 123 +#define SECRET_BASE_YELLOW_CAVE1 SECRET_BASE_GROUP(12) + #define SECRET_BASE_YELLOW_CAVE2_1 131 #define SECRET_BASE_YELLOW_CAVE2_2 132 #define SECRET_BASE_YELLOW_CAVE2_3 133 +#define SECRET_BASE_YELLOW_CAVE2 SECRET_BASE_GROUP(13) + #define SECRET_BASE_YELLOW_CAVE3_1 141 #define SECRET_BASE_YELLOW_CAVE3_2 142 #define SECRET_BASE_YELLOW_CAVE3_3 143 +#define SECRET_BASE_YELLOW_CAVE3 SECRET_BASE_GROUP(14) + #define SECRET_BASE_YELLOW_CAVE4_1 151 #define SECRET_BASE_YELLOW_CAVE4_2 152 #define SECRET_BASE_YELLOW_CAVE4_3 153 +#define SECRET_BASE_YELLOW_CAVE4 SECRET_BASE_GROUP(15) + #define SECRET_BASE_TREE1_1 161 #define SECRET_BASE_TREE1_2 162 #define SECRET_BASE_TREE1_3 163 #define SECRET_BASE_TREE1_4 164 +#define SECRET_BASE_TREE1 SECRET_BASE_GROUP(16) + #define SECRET_BASE_TREE2_1 171 #define SECRET_BASE_TREE2_2 172 #define SECRET_BASE_TREE2_3 173 #define SECRET_BASE_TREE2_4 174 +#define SECRET_BASE_TREE2 SECRET_BASE_GROUP(17) + #define SECRET_BASE_TREE3_1 181 #define SECRET_BASE_TREE3_2 182 #define SECRET_BASE_TREE3_3 183 +#define SECRET_BASE_TREE3 SECRET_BASE_GROUP(18) + #define SECRET_BASE_TREE4_1 191 #define SECRET_BASE_TREE4_2 192 #define SECRET_BASE_TREE4_3 193 +#define SECRET_BASE_TREE4 SECRET_BASE_GROUP(19) + #define SECRET_BASE_SHRUB1_1 201 #define SECRET_BASE_SHRUB1_2 202 #define SECRET_BASE_SHRUB1_3 203 #define SECRET_BASE_SHRUB1_4 204 +#define SECRET_BASE_SHRUB1 SECRET_BASE_GROUP(20) + #define SECRET_BASE_SHRUB2_1 211 #define SECRET_BASE_SHRUB2_2 212 #define SECRET_BASE_SHRUB2_3 213 +#define SECRET_BASE_SHRUB2 SECRET_BASE_GROUP(21) + #define SECRET_BASE_SHRUB3_1 221 #define SECRET_BASE_SHRUB3_2 222 #define SECRET_BASE_SHRUB3_3 223 +#define SECRET_BASE_SHRUB3 SECRET_BASE_GROUP(22) + #define SECRET_BASE_SHRUB4_1 231 #define SECRET_BASE_SHRUB4_2 232 #define SECRET_BASE_SHRUB4_3 233 +#define SECRET_BASE_SHRUB4 SECRET_BASE_GROUP(23) + +#define NUM_SECRET_BASE_GROUPS 24 + #endif // GUARD_CONSTANTS_SECRET_BASES_H diff --git a/include/global.h b/include/global.h index 4ccd2be50a..ca3cf4452d 100644 --- a/include/global.h +++ b/include/global.h @@ -500,7 +500,7 @@ struct SecretBaseParty struct SecretBase { /*0x1A9C*/ u8 secretBaseId; - /*0x1A9D*/ u8 sbr_field_1_0:4; + /*0x1A9D*/ bool8 toRegister:4; /*0x1A9D*/ u8 gender:1; /*0x1A9D*/ u8 battledOwnerToday:1; /*0x1A9D*/ u8 registryStatus:2; @@ -509,7 +509,7 @@ struct SecretBase /*0x1AA9*/ u8 language; /*0x1AAA*/ u16 numSecretBasesReceived; /*0x1AAC*/ u8 numTimesEntered; - /*0x1AAD*/ u8 sbr_field_11; + /*0x1AAD*/ u8 unused; /*0x1AAE*/ u8 decorations[DECOR_MAX_SECRET_BASE]; /*0x1ABE*/ u8 decorationPositions[DECOR_MAX_SECRET_BASE]; /*0x1AD0*/ struct SecretBaseParty party; diff --git a/src/secret_base.c b/src/secret_base.c index d49eef2774..ddc051dca7 100644 --- a/src/secret_base.c +++ b/src/secret_base.c @@ -49,6 +49,13 @@ #include "constants/trainers.h" #include "constants/tv.h" +// Values for registryStatus +enum { + UNREGISTERED, + REGISTERED, + NEW, // Temporary, so new secret bases mixed at the same time don't overwrite each other +}; + struct SecretBaseRegistryMenu { struct ListMenuItem items[11]; @@ -72,21 +79,21 @@ static EWRAM_DATA u8 sCurSecretBaseId = 0; static EWRAM_DATA bool8 sInFriendSecretBase = FALSE; static EWRAM_DATA struct SecretBaseRegistryMenu *sRegistryMenu = NULL; -static void Task_ShowSecretBaseRegistryMenu(u8 taskId); -static void BuildRegistryMenuItems(u8 taskId); -static void RegistryMenu_OnCursorMove(s32 unused, bool8 flag, struct ListMenu *menu); -static void FinalizeRegistryMenu(u8 taskId); -static void AddRegistryMenuScrollArrows(u8 taskId); -static void HandleRegistryMenuInput(u8 taskId); -static void ShowRegistryMenuActions(u8 taskId); -static void HandleRegistryMenuActionsInput(u8 taskId); -static void ShowRegistryMenuDeleteConfirmation(u8 taskId); -static void ShowRegistryMenuDeleteYesNo(u8 taskId); -static void DeleteRegistry_Yes(u8 taskId); -static void DeleteRegistry_No(u8 taskId); -static void ReturnToMainRegistryMenu(u8 taskId); -static void GoToSecretBasePCRegisterMenu(u8 taskId); -static u8 GetSecretBaseOwnerType(u8 secretBaseId); +static void Task_ShowSecretBaseRegistryMenu(u8); +static void BuildRegistryMenuItems(u8); +static void RegistryMenu_OnCursorMove(s32, bool8, struct ListMenu *); +static void FinalizeRegistryMenu(u8); +static void AddRegistryMenuScrollArrows(u8); +static void HandleRegistryMenuInput(u8); +static void ShowRegistryMenuActions(u8); +static void HandleRegistryMenuActionsInput(u8); +static void ShowRegistryMenuDeleteConfirmation(u8); +static void ShowRegistryMenuDeleteYesNo(u8); +static void DeleteRegistry_Yes(u8); +static void DeleteRegistry_No(u8); +static void ReturnToMainRegistryMenu(u8); +static void GoToSecretBasePCRegisterMenu(u8); +static u8 GetSecretBaseOwnerType(u8); static const struct SecretBaseEntranceMetatiles sSecretBaseEntranceMetatiles[] = { @@ -101,34 +108,39 @@ static const struct SecretBaseEntranceMetatiles sSecretBaseEntranceMetatiles[] = // mapNum, warpId, x, y // x, y positions are for when the player warps in for the first time (in front of the computer) -static const u8 sSecretBaseEntrancePositions[] = +static const u8 sSecretBaseEntrancePositions[NUM_SECRET_BASE_GROUPS * 4] = { - MAP_NUM(SECRET_BASE_RED_CAVE1), 0, 1, 3, - MAP_NUM(SECRET_BASE_RED_CAVE2), 0, 5, 9, - MAP_NUM(SECRET_BASE_RED_CAVE3), 0, 1, 3, - MAP_NUM(SECRET_BASE_RED_CAVE4), 0, 7, 13, - MAP_NUM(SECRET_BASE_BROWN_CAVE1), 0, 2, 3, - MAP_NUM(SECRET_BASE_BROWN_CAVE2), 0, 9, 2, - MAP_NUM(SECRET_BASE_BROWN_CAVE3), 0, 13, 4, - MAP_NUM(SECRET_BASE_BROWN_CAVE4), 0, 1, 2, - MAP_NUM(SECRET_BASE_BLUE_CAVE1), 0, 1, 3, - MAP_NUM(SECRET_BASE_BLUE_CAVE2), 0, 1, 2, - MAP_NUM(SECRET_BASE_BLUE_CAVE3), 0, 3, 15, - MAP_NUM(SECRET_BASE_BLUE_CAVE4), 0, 3, 14, - MAP_NUM(SECRET_BASE_YELLOW_CAVE1), 0, 9, 3, - MAP_NUM(SECRET_BASE_YELLOW_CAVE2), 0, 8, 7, - MAP_NUM(SECRET_BASE_YELLOW_CAVE3), 0, 3, 6, - MAP_NUM(SECRET_BASE_YELLOW_CAVE4), 0, 5, 9, - MAP_NUM(SECRET_BASE_TREE1), 0, 2, 3, - MAP_NUM(SECRET_BASE_TREE2), 0, 5, 6, - MAP_NUM(SECRET_BASE_TREE3), 0, 15, 3, - MAP_NUM(SECRET_BASE_TREE4), 0, 4, 10, - MAP_NUM(SECRET_BASE_SHRUB1), 0, 3, 3, - MAP_NUM(SECRET_BASE_SHRUB2), 0, 1, 2, - MAP_NUM(SECRET_BASE_SHRUB3), 0, 7, 8, - MAP_NUM(SECRET_BASE_SHRUB4), 0, 9, 6, + [SECRET_BASE_RED_CAVE1] = MAP_NUM(SECRET_BASE_RED_CAVE1), 0, 1, 3, + [SECRET_BASE_RED_CAVE2] = MAP_NUM(SECRET_BASE_RED_CAVE2), 0, 5, 9, + [SECRET_BASE_RED_CAVE3] = MAP_NUM(SECRET_BASE_RED_CAVE3), 0, 1, 3, + [SECRET_BASE_RED_CAVE4] = MAP_NUM(SECRET_BASE_RED_CAVE4), 0, 7, 13, + [SECRET_BASE_BROWN_CAVE1] = MAP_NUM(SECRET_BASE_BROWN_CAVE1), 0, 2, 3, + [SECRET_BASE_BROWN_CAVE2] = MAP_NUM(SECRET_BASE_BROWN_CAVE2), 0, 9, 2, + [SECRET_BASE_BROWN_CAVE3] = MAP_NUM(SECRET_BASE_BROWN_CAVE3), 0, 13, 4, + [SECRET_BASE_BROWN_CAVE4] = MAP_NUM(SECRET_BASE_BROWN_CAVE4), 0, 1, 2, + [SECRET_BASE_BLUE_CAVE1] = MAP_NUM(SECRET_BASE_BLUE_CAVE1), 0, 1, 3, + [SECRET_BASE_BLUE_CAVE2] = MAP_NUM(SECRET_BASE_BLUE_CAVE2), 0, 1, 2, + [SECRET_BASE_BLUE_CAVE3] = MAP_NUM(SECRET_BASE_BLUE_CAVE3), 0, 3, 15, + [SECRET_BASE_BLUE_CAVE4] = MAP_NUM(SECRET_BASE_BLUE_CAVE4), 0, 3, 14, + [SECRET_BASE_YELLOW_CAVE1] = MAP_NUM(SECRET_BASE_YELLOW_CAVE1), 0, 9, 3, + [SECRET_BASE_YELLOW_CAVE2] = MAP_NUM(SECRET_BASE_YELLOW_CAVE2), 0, 8, 7, + [SECRET_BASE_YELLOW_CAVE3] = MAP_NUM(SECRET_BASE_YELLOW_CAVE3), 0, 3, 6, + [SECRET_BASE_YELLOW_CAVE4] = MAP_NUM(SECRET_BASE_YELLOW_CAVE4), 0, 5, 9, + [SECRET_BASE_TREE1] = MAP_NUM(SECRET_BASE_TREE1), 0, 2, 3, + [SECRET_BASE_TREE2] = MAP_NUM(SECRET_BASE_TREE2), 0, 5, 6, + [SECRET_BASE_TREE3] = MAP_NUM(SECRET_BASE_TREE3), 0, 15, 3, + [SECRET_BASE_TREE4] = MAP_NUM(SECRET_BASE_TREE4), 0, 4, 10, + [SECRET_BASE_SHRUB1] = MAP_NUM(SECRET_BASE_SHRUB1), 0, 3, 3, + [SECRET_BASE_SHRUB2] = MAP_NUM(SECRET_BASE_SHRUB2), 0, 1, 2, + [SECRET_BASE_SHRUB3] = MAP_NUM(SECRET_BASE_SHRUB3), 0, 7, 8, + [SECRET_BASE_SHRUB4] = MAP_NUM(SECRET_BASE_SHRUB4), 0, 9, 6, }; +#define GET_BASE_MAP_NUM(group) (sSecretBaseEntrancePositions[(group) + 0]) +#define GET_BASE_WARP_ID(group) (sSecretBaseEntrancePositions[(group) + 1]) +#define GET_BASE_COMPUTER_X(group) (sSecretBaseEntrancePositions[(group) + 2]) +#define GET_BASE_COMPUTER_Y(group) (sSecretBaseEntrancePositions[(group) + 3]) + static const struct MenuAction sRegistryMenuActions[] = { { @@ -314,6 +326,8 @@ void ToggleSecretBaseEntranceMetatile(void) GetXYCoordsOneStepInFrontOfPlayer(&x, &y); metatileId = MapGridGetMetatileIdAt(x, y); + + // Look for entrance metatiles to open for (i = 0; i < ARRAY_COUNT(sSecretBaseEntranceMetatiles); i++) { if (sSecretBaseEntranceMetatiles[i].closedMetatileId == metatileId) @@ -324,6 +338,7 @@ void ToggleSecretBaseEntranceMetatile(void) } } + // Look for entrance metatiles to close for (i = 0; i < ARRAY_COUNT(sSecretBaseEntranceMetatiles); i++) { if (sSecretBaseEntranceMetatiles[i].openMetatileId == metatileId) @@ -362,6 +377,7 @@ void SetPlayerSecretBase(void) VarSet(VAR_SECRET_BASE_MAP, gMapHeader.regionMapSectionId); } +// Set the 'open' entrance metatile for any occupied secret base on this map void SetOccupiedSecretBaseEntranceMetatiles(struct MapEvents const *events) { u16 bgId; @@ -395,24 +411,26 @@ void SetOccupiedSecretBaseEntranceMetatiles(struct MapEvents const *events) static void SetSecretBaseWarpDestination(void) { - s8 offset = (sCurSecretBaseId / 10) * 4; - SetWarpDestinationToMapWarp(MAP_GROUP(SECRET_BASE_RED_CAVE1), sSecretBaseEntrancePositions[offset], sSecretBaseEntrancePositions[offset + 1]); + s8 secretBaseGroup = SECRET_BASE_ID_TO_GROUP(sCurSecretBaseId); + SetWarpDestinationToMapWarp(MAP_GROUP(SECRET_BASE_RED_CAVE1), GET_BASE_MAP_NUM(secretBaseGroup), GET_BASE_WARP_ID(secretBaseGroup)); } +#define tState data[0] + static void Task_EnterSecretBase(u8 taskId) { - u16 secretBaseId; + u16 secretBaseIdx; - switch (gTasks[taskId].data[0]) + switch (gTasks[taskId].tState) { case 0: if (!gPaletteFade.active) - gTasks[taskId].data[0] = 1; + gTasks[taskId].tState = 1; break; case 1: - secretBaseId = VarGet(VAR_CURRENT_SECRET_BASE); - if (gSaveBlock1Ptr->secretBases[secretBaseId].numTimesEntered < 255) - gSaveBlock1Ptr->secretBases[secretBaseId].numTimesEntered++; + secretBaseIdx = VarGet(VAR_CURRENT_SECRET_BASE); + if (gSaveBlock1Ptr->secretBases[secretBaseIdx].numTimesEntered < 255) + gSaveBlock1Ptr->secretBases[secretBaseIdx].numTimesEntered++; SetSecretBaseWarpDestination(); WarpIntoMap(); @@ -423,6 +441,8 @@ static void Task_EnterSecretBase(u8 taskId) } } +#undef tState + void EnterSecretBase(void) { CreateTask(Task_EnterSecretBase, 0); @@ -467,13 +487,13 @@ static void Task_EnterNewlyCreatedSecretBase(u8 taskId) { if (!gPaletteFade.active) { - s8 offset = sCurSecretBaseId / 10 * 4; + s8 secretBaseGroup = SECRET_BASE_ID_TO_GROUP(sCurSecretBaseId); SetWarpDestination( gSaveBlock1Ptr->location.mapGroup, gSaveBlock1Ptr->location.mapNum, -1, - sSecretBaseEntrancePositions[offset + 2], - sSecretBaseEntrancePositions[offset + 3]); + GET_BASE_COMPUTER_X(secretBaseGroup), + GET_BASE_COMPUTER_Y(secretBaseGroup)); WarpIntoMap(); gFieldCallback = EnterNewlyCreatedSecretBase_StartFadeIn; SetMainCallback2(CB2_LoadMap); @@ -498,23 +518,23 @@ bool8 CurMapIsSecretBase(void) void InitSecretBaseAppearance(bool8 hidePC) { - u16 secretBaseId; + u16 secretBaseIdx; u16 x, y; u8 *decorations; u8 *decorPos; if (CurMapIsSecretBase()) { - secretBaseId = VarGet(VAR_CURRENT_SECRET_BASE); - decorations = gSaveBlock1Ptr->secretBases[secretBaseId].decorations; - decorPos = gSaveBlock1Ptr->secretBases[secretBaseId].decorationPositions; + secretBaseIdx = VarGet(VAR_CURRENT_SECRET_BASE); + decorations = gSaveBlock1Ptr->secretBases[secretBaseIdx].decorations; + decorPos = gSaveBlock1Ptr->secretBases[secretBaseIdx].decorationPositions; for (x = 0; x < DECOR_MAX_SECRET_BASE; x++) { if (decorations[x] > 0 && decorations[x] <= NUM_DECORATIONS && gDecorations[decorations[x]].permission != DECORPERM_SPRITE) ShowDecorationOnMap((decorPos[x] >> 4) + 7, (decorPos[x] & 0xF) + 7, decorations[x]); } - if (secretBaseId != 0) + if (secretBaseIdx != 0) { // Another player's secret base. Change PC type to the "Register" PC. FindMetatileIdMapCoords(&x, &y, METATILE_SecretBase_PC); @@ -549,9 +569,9 @@ void InitSecretBaseDecorationSprites(void) } else { - u16 secretBaseId = VarGet(VAR_CURRENT_SECRET_BASE); - decorations = gSaveBlock1Ptr->secretBases[secretBaseId].decorations; - decorationPositions = gSaveBlock1Ptr->secretBases[secretBaseId].decorationPositions; + u16 secretBaseIdx = VarGet(VAR_CURRENT_SECRET_BASE); + decorations = gSaveBlock1Ptr->secretBases[secretBaseIdx].decorations; + decorationPositions = gSaveBlock1Ptr->secretBases[secretBaseIdx].decorationPositions; numDecorations = DECOR_MAX_SECRET_BASE; } @@ -703,10 +723,10 @@ void IsCurSecretBaseOwnedByAnotherPlayer(void) gSpecialVar_Result = FALSE; } -static u8 *GetSecretBaseName(u8 *dest, u8 secretBaseId) +static u8 *GetSecretBaseName(u8 *dest, u8 secretBaseIdx) { - *StringCopyN(dest, gSaveBlock1Ptr->secretBases[secretBaseId].trainerName, GetNameLength(gSaveBlock1Ptr->secretBases[secretBaseId].trainerName)) = EOS; - ConvertInternationalString(dest, gSaveBlock1Ptr->secretBases[secretBaseId].language); + *StringCopyN(dest, gSaveBlock1Ptr->secretBases[secretBaseIdx].trainerName, GetNameLength(gSaveBlock1Ptr->secretBases[secretBaseIdx].trainerName)) = EOS; + ConvertInternationalString(dest, gSaveBlock1Ptr->secretBases[secretBaseIdx].language); return StringAppend(dest, gText_ApostropheSBase); } @@ -717,18 +737,18 @@ u8 *GetSecretBaseMapName(u8 *dest) void CopyCurSecretBaseOwnerName_StrVar1(void) { - u8 secretBaseId; + u8 secretBaseIdx; const u8 *name; - secretBaseId = VarGet(VAR_CURRENT_SECRET_BASE); - name = gSaveBlock1Ptr->secretBases[secretBaseId].trainerName; + secretBaseIdx = VarGet(VAR_CURRENT_SECRET_BASE); + name = gSaveBlock1Ptr->secretBases[secretBaseIdx].trainerName; *StringCopyN(gStringVar1, name, GetNameLength(name)) = EOS; - ConvertInternationalString(gStringVar1, gSaveBlock1Ptr->secretBases[secretBaseId].language); + ConvertInternationalString(gStringVar1, gSaveBlock1Ptr->secretBases[secretBaseIdx].language); } -static bool8 IsSecretBaseRegistered(u8 secretBaseId) +static bool8 IsSecretBaseRegistered(u8 secretBaseIdx) { - if (gSaveBlock1Ptr->secretBases[secretBaseId].registryStatus) + if (gSaveBlock1Ptr->secretBases[secretBaseIdx].registryStatus) return TRUE; return FALSE; @@ -812,7 +832,7 @@ static void ClosePlayerSecretBaseEntrance(void) && gSaveBlock1Ptr->secretBases[0].secretBaseId == events->bgEvents[i].bgUnion.secretBaseId) { metatileId = MapGridGetMetatileIdAt(events->bgEvents[i].x + 7, events->bgEvents[i].y + 7); - for (j = 0; j < 7; j++) + for (j = 0; j < ARRAY_COUNT(sSecretBaseEntranceMetatiles); j++) { if (sSecretBaseEntranceMetatiles[j].openMetatileId == metatileId) { @@ -879,18 +899,28 @@ void ShowSecretBaseRegistryMenu(void) CreateTask(Task_ShowSecretBaseRegistryMenu, 0); } +#define tNumBases data[0] +#define tSelectedRow data[1] +#define tScrollOffset data[2] +#define tMaxShownItems data[3] +#define tSelectedBaseId data[4] +#define tListTaskId data[5] +#define tMainWindowId data[6] +#define tActionWindowId data[7] +#define tArrowTaskId data[8] + static void Task_ShowSecretBaseRegistryMenu(u8 taskId) { s16 *data = gTasks[taskId].data; ScriptContext2_Enable(); - data[0] = GetNumRegisteredSecretBases(); - if (data[0] != 0) + tNumBases = GetNumRegisteredSecretBases(); + if (tNumBases != 0) { - data[1] = 0; - data[2] = 0; + tSelectedRow = 0; + tScrollOffset = 0; ClearDialogWindowAndFrame(0, 0); sRegistryMenu = calloc(1, sizeof(*sRegistryMenu)); - data[6] = AddWindow(&sRegistryWindowTemplates[0]); + tMainWindowId = AddWindow(&sRegistryWindowTemplates[0]); BuildRegistryMenuItems(taskId); FinalizeRegistryMenu(taskId); gTasks[taskId].func = HandleRegistryMenuInput; @@ -922,17 +952,17 @@ static void BuildRegistryMenuItems(u8 taskId) sRegistryMenu->items[count].name = gText_Cancel; sRegistryMenu->items[count].id = -2; - data[0] = count + 1; - if (data[0] < 8) - data[3] = data[0]; + tNumBases = count + 1; + if (tNumBases < 8) + tMaxShownItems = tNumBases; else - data[3] = 8; + tMaxShownItems = 8; gMultiuseListMenuTemplate = sRegistryListMenuTemplate; - gMultiuseListMenuTemplate.windowId = data[6]; - gMultiuseListMenuTemplate.totalItems = data[0]; + gMultiuseListMenuTemplate.windowId = tMainWindowId; + gMultiuseListMenuTemplate.totalItems = tNumBases; gMultiuseListMenuTemplate.items = sRegistryMenu->items; - gMultiuseListMenuTemplate.maxShowed = data[3]; + gMultiuseListMenuTemplate.maxShowed = tMaxShownItems; } static void RegistryMenu_OnCursorMove(s32 unused, bool8 flag, struct ListMenu *menu) @@ -944,8 +974,8 @@ static void RegistryMenu_OnCursorMove(s32 unused, bool8 flag, struct ListMenu *m static void FinalizeRegistryMenu(u8 taskId) { s16 *data = gTasks[taskId].data; - SetStandardWindowBorderStyle(data[6], 0); - data[5] = ListMenuInit(&gMultiuseListMenuTemplate, data[2], data[1]); + SetStandardWindowBorderStyle(tMainWindowId, 0); + tListTaskId = ListMenuInit(&gMultiuseListMenuTemplate, tScrollOffset, tSelectedRow); AddRegistryMenuScrollArrows(taskId); ScheduleBgCopyTilemapToVram(0); } @@ -953,35 +983,33 @@ static void FinalizeRegistryMenu(u8 taskId) static void AddRegistryMenuScrollArrows(u8 taskId) { s16 *data = gTasks[taskId].data; - data[8] = AddScrollIndicatorArrowPairParameterized(SCROLL_ARROW_UP, 0xbc, 0x0c, 0x94, data[0] - data[3], 0x13f8, 0x13f8, &data[2]); + tArrowTaskId = AddScrollIndicatorArrowPairParameterized(SCROLL_ARROW_UP, 188, 12, 148, tNumBases - tMaxShownItems, 0x13f8, 0x13f8, &tScrollOffset); } static void HandleRegistryMenuInput(u8 taskId) { - s16 *data; - s32 input; + s16 *data = gTasks[taskId].data; + s32 input = ListMenu_ProcessInput(tListTaskId); + ListMenuGetScrollAndRow(tListTaskId, &tScrollOffset, &tSelectedRow); - data = gTasks[taskId].data; - input = ListMenu_ProcessInput(data[5]); - ListMenuGetScrollAndRow(data[5], &data[2], &data[1]); switch (input) { case LIST_NOTHING_CHOSEN: break; case LIST_CANCEL: PlaySE(SE_SELECT); - DestroyListMenuTask(data[5], NULL, NULL); - RemoveScrollIndicatorArrowPair(data[8]); - ClearStdWindowAndFrame(data[6], 0); - ClearWindowTilemap(data[6]); - RemoveWindow(data[6]); + DestroyListMenuTask(tListTaskId, NULL, NULL); + RemoveScrollIndicatorArrowPair(tArrowTaskId); + ClearStdWindowAndFrame(tMainWindowId, 0); + ClearWindowTilemap(tMainWindowId); + RemoveWindow(tMainWindowId); ScheduleBgCopyTilemapToVram(0); free(sRegistryMenu); GoToSecretBasePCRegisterMenu(taskId); break; default: PlaySE(SE_SELECT); - data[4] = input; + tSelectedBaseId = input; ShowRegistryMenuActions(taskId); break; } @@ -991,13 +1019,13 @@ static void ShowRegistryMenuActions(u8 taskId) { struct WindowTemplate template; s16 *data = gTasks[taskId].data; - RemoveScrollIndicatorArrowPair(data[8]); + RemoveScrollIndicatorArrowPair(tArrowTaskId); template = sRegistryWindowTemplates[1]; template.width = GetMaxWidthInMenuTable(sRegistryMenuActions, 2); - data[7] = AddWindow(&template); - SetStandardWindowBorderStyle(data[7], 0); - PrintMenuTable(data[7], ARRAY_COUNT(sRegistryMenuActions), sRegistryMenuActions); - InitMenuInUpperLeftCornerPlaySoundWhenAPressed(data[7], 2, 0); + tActionWindowId = AddWindow(&template); + SetStandardWindowBorderStyle(tActionWindowId, 0); + PrintMenuTable(tActionWindowId, ARRAY_COUNT(sRegistryMenuActions), sRegistryMenuActions); + InitMenuInUpperLeftCornerPlaySoundWhenAPressed(tActionWindowId, 2, 0); ScheduleBgCopyTilemapToVram(0); gTasks[taskId].func = HandleRegistryMenuActionsInput; } @@ -1023,13 +1051,13 @@ static void HandleRegistryMenuActionsInput(u8 taskId) static void ShowRegistryMenuDeleteConfirmation(u8 taskId) { s16 *data = gTasks[taskId].data; - ClearStdWindowAndFrame(data[6], FALSE); - ClearStdWindowAndFrame(data[7], FALSE); - ClearWindowTilemap(data[6]); - ClearWindowTilemap(data[7]); - RemoveWindow(data[7]); + ClearStdWindowAndFrame(tMainWindowId, FALSE); + ClearStdWindowAndFrame(tActionWindowId, FALSE); + ClearWindowTilemap(tMainWindowId); + ClearWindowTilemap(tActionWindowId); + RemoveWindow(tActionWindowId); ScheduleBgCopyTilemapToVram(0); - GetSecretBaseName(gStringVar1, data[4]); + GetSecretBaseName(gStringVar1, tSelectedBaseId); StringExpandPlaceholders(gStringVar4, gText_OkayToDeleteFromRegistry); DisplayItemMessageOnField(taskId, gStringVar4, ShowRegistryMenuDeleteYesNo); } @@ -1044,10 +1072,10 @@ void DeleteRegistry_Yes_Callback(u8 taskId) { s16 *data = gTasks[taskId].data; ClearDialogWindowAndFrame(0, 0); - DestroyListMenuTask(data[5], &data[2], &data[1]); - gSaveBlock1Ptr->secretBases[data[4]].registryStatus = 0; + DestroyListMenuTask(tListTaskId, &tScrollOffset, &tSelectedRow); + gSaveBlock1Ptr->secretBases[tSelectedBaseId].registryStatus = UNREGISTERED; BuildRegistryMenuItems(taskId); - sub_812225C(&data[2], &data[1], data[3], data[0]); + sub_812225C(&tScrollOffset, &tSelectedRow, tMaxShownItems, tNumBases); FinalizeRegistryMenu(taskId); gTasks[taskId].func = HandleRegistryMenuInput; } @@ -1061,7 +1089,7 @@ static void DeleteRegistry_No(u8 taskId) { s16 *data = gTasks[taskId].data; ClearDialogWindowAndFrame(0, 0); - DestroyListMenuTask(data[5], &data[2], &data[1]); + DestroyListMenuTask(tListTaskId, &tScrollOffset, &tSelectedRow); FinalizeRegistryMenu(taskId); gTasks[taskId].func = HandleRegistryMenuInput; } @@ -1070,9 +1098,9 @@ static void ReturnToMainRegistryMenu(u8 taskId) { s16 *data = gTasks[taskId].data; AddRegistryMenuScrollArrows(taskId); - ClearStdWindowAndFrame(data[7], 0); - ClearWindowTilemap(data[7]); - RemoveWindow(data[7]); + ClearStdWindowAndFrame(tActionWindowId, 0); + ClearWindowTilemap(tActionWindowId); + RemoveWindow(tActionWindowId); ScheduleBgCopyTilemapToVram(0); gTasks[taskId].func = HandleRegistryMenuInput; } @@ -1087,10 +1115,20 @@ static void GoToSecretBasePCRegisterMenu(u8 taskId) DestroyTask(taskId); } -static u8 GetSecretBaseOwnerType(u8 secretBaseId) +#undef tNumBases +#undef tSelectedRow +#undef tScrollOffset +#undef tMaxShownItems +#undef tSelectedBaseId +#undef tListTaskId +#undef tMainWindowId +#undef tActionWindowId +#undef tArrowTaskId + +static u8 GetSecretBaseOwnerType(u8 secretBaseIdx) { - return (gSaveBlock1Ptr->secretBases[secretBaseId].trainerId[0] % 5) - + (gSaveBlock1Ptr->secretBases[secretBaseId].gender * 5); + return (gSaveBlock1Ptr->secretBases[secretBaseIdx].trainerId[0] % 5) + + (gSaveBlock1Ptr->secretBases[secretBaseIdx].gender * 5); } const u8 *GetSecretBaseTrainerLoseText(void) @@ -1132,10 +1170,10 @@ void SetBattledOwnerFromResult(void) void GetSecretBaseOwnerAndState(void) { - u16 secretBaseId; + u16 secretBaseIdx; u8 i; - secretBaseId = VarGet(VAR_CURRENT_SECRET_BASE); + secretBaseIdx = VarGet(VAR_CURRENT_SECRET_BASE); if (!FlagGet(FLAG_DAILY_SECRET_BASE)) { for (i = 0; i < SECRET_BASES_COUNT; i++) @@ -1143,10 +1181,16 @@ void GetSecretBaseOwnerAndState(void) FlagSet(FLAG_DAILY_SECRET_BASE); } - gSpecialVar_0x8004 = GetSecretBaseOwnerType(secretBaseId); - gSpecialVar_Result = gSaveBlock1Ptr->secretBases[secretBaseId].battledOwnerToday; + gSpecialVar_0x8004 = GetSecretBaseOwnerType(secretBaseIdx); + gSpecialVar_Result = gSaveBlock1Ptr->secretBases[secretBaseIdx].battledOwnerToday; } +#define tStepCb data[0] // See Task_RunPerStepCallback +#define tState data[1] +#define tPlayerX data[2] +#define tPlayerY data[3] +#define tFldEff data[4] + void SecretBasePerStepCallback(u8 taskId) { s16 x; @@ -1156,23 +1200,23 @@ void SecretBasePerStepCallback(u8 taskId) s16 *data; data = gTasks[taskId].data; - switch (data[1]) + switch (tState) { case 0: - if (VarGet(VAR_CURRENT_SECRET_BASE) != 0) + if (VarGet(VAR_CURRENT_SECRET_BASE)) sInFriendSecretBase = TRUE; else sInFriendSecretBase = FALSE; - PlayerGetDestCoords(&data[2], &data[3]); - data[1] = 1; + PlayerGetDestCoords(&tPlayerX, &tPlayerY); + tState = 1; break; case 1: PlayerGetDestCoords(&x, &y); - if (x != data[2] || y != data[3]) + if (x != tPlayerX || y != tPlayerY) { - data[2] = x; - data[3] = y; + tPlayerX = x; + tPlayerY = y; VarSet(VAR_SECRET_BASE_STEP_COUNTER, VarGet(VAR_SECRET_BASE_STEP_COUNTER) + 1); behavior = MapGridGetMetatileBehaviorAt(x, y); tileId = MapGridGetMetatileIdAt(x, y); @@ -1273,33 +1317,40 @@ void SecretBasePerStepCallback(u8 taskId) } break; case 2: - if (!FieldEffectActiveListContains(data[4])) - data[1] = 1; + // This state is never reached, and tFldEff is never set + if (!FieldEffectActiveListContains(tFldEff)) + tState = 1; break; } } -static void SetSecretBaseDataAndLanguage(u8 secretBaseId, struct SecretBase *secretBase, u32 version, u32 language) +#undef tStepCb +#undef tState +#undef tPlayerX +#undef tPlayerY +#undef tFldEff + +static void SaveSecretBase(u8 secretBaseIdx, struct SecretBase *secretBase, u32 version, u32 language) { int stringLength; u8 *name; - gSaveBlock1Ptr->secretBases[secretBaseId] = *secretBase; - gSaveBlock1Ptr->secretBases[secretBaseId].registryStatus = 2; + gSaveBlock1Ptr->secretBases[secretBaseIdx] = *secretBase; + gSaveBlock1Ptr->secretBases[secretBaseIdx].registryStatus = NEW; if (version == VERSION_SAPPHIRE || version == VERSION_RUBY) - gSaveBlock1Ptr->secretBases[secretBaseId].language = LANGUAGE_ENGLISH; + gSaveBlock1Ptr->secretBases[secretBaseIdx].language = GAME_LANGUAGE; if (version == VERSION_EMERALD && language == LANGUAGE_JAPANESE) { - name = gSaveBlock1Ptr->secretBases[secretBaseId].trainerName; - for (stringLength = 0; stringLength < 7; stringLength++) + name = gSaveBlock1Ptr->secretBases[secretBaseIdx].trainerName; + for (stringLength = 0; stringLength < PLAYER_NAME_LENGTH; stringLength++) { if (name[stringLength] == EOS) break; } if (stringLength > 5) - gSaveBlock1Ptr->secretBases[secretBaseId].language = LANGUAGE_ENGLISH; + gSaveBlock1Ptr->secretBases[secretBaseIdx].language = GAME_LANGUAGE; } } @@ -1363,56 +1414,67 @@ static u8 FindAvailableSecretBaseIndex(void) return 0; } -static u8 sub_80EAA18(void) +static u8 FindUnregisteredSecretBaseIndex(void) { s16 i; for (i = 1; i < SECRET_BASES_COUNT; i++) { - if (gSaveBlock1Ptr->secretBases[i].registryStatus == 0 && gSaveBlock1Ptr->secretBases[i].sbr_field_1_0 == 0) + if (gSaveBlock1Ptr->secretBases[i].registryStatus == UNREGISTERED && gSaveBlock1Ptr->secretBases[i].toRegister == FALSE) return i; } return 0; } -static u8 sub_80EAA64(struct SecretBase *secretBase, u32 version, u32 language) +static u8 TrySaveFriendsSecretBase(struct SecretBase *secretBase, u32 version, u32 language) { s16 index; + // Secret base has no location if (!secretBase->secretBaseId) return 0; index = GetSecretBaseIndexFromId(secretBase->secretBaseId); if (index != 0) { + // An existing secret base is using this location if (index != -1) { - if (gSaveBlock1Ptr->secretBases[index].sbr_field_1_0 == 1) + if (gSaveBlock1Ptr->secretBases[index].toRegister == TRUE) return 0; - if (gSaveBlock1Ptr->secretBases[index].registryStatus != 2 || secretBase->sbr_field_1_0 == 1) + if (gSaveBlock1Ptr->secretBases[index].registryStatus != NEW || secretBase->toRegister == TRUE) { - SetSecretBaseDataAndLanguage(index, secretBase, version, language); + // Overwrite unregistered base at this location + SaveSecretBase(index, secretBase, version, language); return index; } } + // No secret base is using this location, find a spot to save it else { index = FindAvailableSecretBaseIndex(); if (index != 0) { - SetSecretBaseDataAndLanguage(index, secretBase, version, language); + // Save in empty space + SaveSecretBase(index, secretBase, version, language); return index; } - index = sub_80EAA18(); + index = FindUnregisteredSecretBaseIndex(); if (index != 0) { - SetSecretBaseDataAndLanguage(index, secretBase, version, language); + // Overwrite unregistered base + SaveSecretBase(index, secretBase, version, language); return index; } } } + + // Unable to save. Either... + // - This was the player's base + // - A registered base exists at this location + // - The secret base limit has been filled with registered bases return 0; } @@ -1425,11 +1487,12 @@ static void SortSecretBasesByRegistryStatus(void) struct SecretBase *secretBases; secretBases = gSaveBlock1Ptr->secretBases; - for (i = 1; i < 19; i++) + for (i = 1; i < SECRET_BASES_COUNT - 1; i++) { for (j = i + 1; j < SECRET_BASES_COUNT; j++) { - if ((secretBases[i].registryStatus == 0 && secretBases[j].registryStatus == 1) || (secretBases[i].registryStatus == 2 && secretBases[j].registryStatus != 2)) + if ((secretBases[i].registryStatus == UNREGISTERED && secretBases[j].registryStatus == REGISTERED) + || (secretBases[i].registryStatus == NEW && secretBases[j].registryStatus != NEW)) { struct SecretBase temp; SWAP(secretBases[i], secretBases[j], temp) @@ -1438,17 +1501,19 @@ static void SortSecretBasesByRegistryStatus(void) } } -void sub_80EABA4(struct SecretBaseRecordMixer *mixer, u8 registryStatus) +// Used to save a record mixing friends' bases other than their own +// registryStatus is so registered bases can be attempted first +static void TrySaveFriendsSecretBases(struct SecretBaseRecordMixer *mixer, u8 registryStatus) { u16 i; for (i = 1; i < SECRET_BASES_COUNT; i++) { if (mixer->secretBases[i].registryStatus == registryStatus) - sub_80EAA64(&mixer->secretBases[i], mixer->version, mixer->language); + TrySaveFriendsSecretBase(&mixer->secretBases[i], mixer->version, mixer->language); } } -bool8 SecretBaseBelongsToPlayer(struct SecretBase *secretBase) +static bool8 SecretBaseBelongsToPlayer(struct SecretBase *secretBase) { u8 i; @@ -1478,7 +1543,7 @@ bool8 SecretBaseBelongsToPlayer(struct SecretBase *secretBase) #define DELETED_BASE_B (1 << 1) #define DELETED_BASE_C (1 << 2) -void DeleteFirstOldBaseFromPlayerInRecordMixingFriendsRecords(struct SecretBase *basesA, struct SecretBase *basesB, struct SecretBase *basesC) +static void DeleteFirstOldBaseFromPlayerInRecordMixingFriendsRecords(struct SecretBase *basesA, struct SecretBase *basesB, struct SecretBase *basesC) { u8 i; u8 sbFlags = 0; @@ -1524,7 +1589,7 @@ void DeleteFirstOldBaseFromPlayerInRecordMixingFriendsRecords(struct SecretBase #undef DELETED_BASE_C // returns TRUE if secretBase was deleted, FALSE otherwise -static bool8 ClearDuplicateOwnedSecretBases(struct SecretBase *secretBase, struct SecretBase *secretBases, u8 c) +static bool8 ClearDuplicateOwnedSecretBase(struct SecretBase *secretBase, struct SecretBase *secretBases, u8 idx) { u8 i; @@ -1534,7 +1599,7 @@ static bool8 ClearDuplicateOwnedSecretBases(struct SecretBase *secretBase, struc { if (SecretBasesBelongToSamePlayer(secretBase, &secretBases[i]) == TRUE) { - if (c == 0) + if (idx == 0) { ClearSecretBase(&secretBases[i]); return FALSE; @@ -1546,7 +1611,7 @@ static bool8 ClearDuplicateOwnedSecretBases(struct SecretBase *secretBase, struc return FALSE; } - secretBases[i].sbr_field_1_0 = secretBase->sbr_field_1_0; + secretBases[i].toRegister = secretBase->toRegister; ClearSecretBase(secretBase); return TRUE; } @@ -1556,87 +1621,97 @@ static bool8 ClearDuplicateOwnedSecretBases(struct SecretBase *secretBase, struc return FALSE; } -void sub_80EAD94(struct SecretBase *basesA, struct SecretBase *basesB, struct SecretBase *basesC, struct SecretBase *basesD) +static void ClearDuplicateOwnedSecretBases(struct SecretBase *playersBases, struct SecretBase *friendsBasesA, struct SecretBase *friendsBasesB, struct SecretBase *friendsBasesC) { u8 i; for (i = 1; i < SECRET_BASES_COUNT; i++) { - if (basesA[i].secretBaseId) + if (playersBases[i].secretBaseId) { - if (basesA[i].registryStatus == 1) + if (playersBases[i].registryStatus == REGISTERED) { - basesA[i].sbr_field_1_0 = 1; + // Mark registered bases, so if they're deleted as a duplicate they + // will be re-registered later + playersBases[i].toRegister = TRUE; } - if (!ClearDuplicateOwnedSecretBases(&basesA[i], basesB, i)) + if (!ClearDuplicateOwnedSecretBase(&playersBases[i], friendsBasesA, i)) { - if (!ClearDuplicateOwnedSecretBases(&basesA[i], basesC, i)) + if (!ClearDuplicateOwnedSecretBase(&playersBases[i], friendsBasesB, i)) { - ClearDuplicateOwnedSecretBases(&basesA[i], basesD, i); + ClearDuplicateOwnedSecretBase(&playersBases[i], friendsBasesC, i); } } } } for (i = 0; i < SECRET_BASES_COUNT; i++) { - if (basesB[i].secretBaseId) + if (friendsBasesA[i].secretBaseId) { - basesB[i].battledOwnerToday = 0; - if (!ClearDuplicateOwnedSecretBases(&basesB[i], basesC, i)) + friendsBasesA[i].battledOwnerToday = 0; + if (!ClearDuplicateOwnedSecretBase(&friendsBasesA[i], friendsBasesB, i)) { - ClearDuplicateOwnedSecretBases(&basesB[i], basesD, i); + ClearDuplicateOwnedSecretBase(&friendsBasesA[i], friendsBasesC, i); } } } for (i = 0; i < SECRET_BASES_COUNT; i++) { - if (basesC[i].secretBaseId) + if (friendsBasesB[i].secretBaseId) { - basesC[i].battledOwnerToday = 0; - ClearDuplicateOwnedSecretBases(&basesC[i], basesD, i); + friendsBasesB[i].battledOwnerToday = 0; + ClearDuplicateOwnedSecretBase(&friendsBasesB[i], friendsBasesC, i); } - if (basesD[i].secretBaseId) + if (friendsBasesC[i].secretBaseId) { - basesD[i].battledOwnerToday = 0; + friendsBasesC[i].battledOwnerToday = 0; } } } -void sub_80EAE90(struct SecretBase *base, u32 version, u32 language) +static void TrySaveRegisteredDuplicate(struct SecretBase *base, u32 version, u32 language) { - if (base->sbr_field_1_0 == 1) + if (base->toRegister == TRUE) { - sub_80EAA64(base, version, language); + TrySaveFriendsSecretBase(base, version, language); ClearSecretBase(base); } } -void sub_80EAEB4(struct SecretBaseRecordMixer *mixers) +static void TrySaveRegisteredDuplicates(struct SecretBaseRecordMixer *mixers) { u16 i; for (i = 0; i < SECRET_BASES_COUNT; i++) { - sub_80EAE90(&mixers[0].secretBases[i], mixers[0].version, mixers[0].language); - sub_80EAE90(&mixers[1].secretBases[i], mixers[1].version, mixers[1].language); - sub_80EAE90(&mixers[2].secretBases[i], mixers[2].version, mixers[2].language); + TrySaveRegisteredDuplicate(&mixers[0].secretBases[i], mixers[0].version, mixers[0].language); + TrySaveRegisteredDuplicate(&mixers[1].secretBases[i], mixers[1].version, mixers[1].language); + TrySaveRegisteredDuplicate(&mixers[2].secretBases[i], mixers[2].version, mixers[2].language); } } -void sub_80EAEF4(struct SecretBaseRecordMixer *mixers) +static void SaveRecordMixBases(struct SecretBaseRecordMixer *mixers) { DeleteFirstOldBaseFromPlayerInRecordMixingFriendsRecords(mixers[0].secretBases, mixers[1].secretBases, mixers[2].secretBases); - sub_80EAD94(gSaveBlock1Ptr->secretBases, mixers[0].secretBases, mixers[1].secretBases, mixers[2].secretBases); - sub_80EAEB4(mixers); - sub_80EAA64(mixers[0].secretBases, mixers[0].version, mixers[0].language); - sub_80EAA64(mixers[1].secretBases, mixers[1].version, mixers[1].language); - sub_80EAA64(mixers[2].secretBases, mixers[2].version, mixers[2].language); - sub_80EABA4(&mixers[0], 1); - sub_80EABA4(&mixers[1], 1); - sub_80EABA4(&mixers[2], 1); - sub_80EABA4(&mixers[0], 0); - sub_80EABA4(&mixers[1], 0); - sub_80EABA4(&mixers[2], 0); + ClearDuplicateOwnedSecretBases(gSaveBlock1Ptr->secretBases, mixers[0].secretBases, mixers[1].secretBases, mixers[2].secretBases); + + // First, save any registered secret bases that were deleted as duplicates + TrySaveRegisteredDuplicates(mixers); + + // Then try to save the record mixing friends' own bases + TrySaveFriendsSecretBase(mixers[0].secretBases, mixers[0].version, mixers[0].language); + TrySaveFriendsSecretBase(mixers[1].secretBases, mixers[1].version, mixers[1].language); + TrySaveFriendsSecretBase(mixers[2].secretBases, mixers[2].version, mixers[2].language); + + // Then try to save as many of their registered bases as possible + TrySaveFriendsSecretBases(&mixers[0], REGISTERED); + TrySaveFriendsSecretBases(&mixers[1], REGISTERED); + TrySaveFriendsSecretBases(&mixers[2], REGISTERED); + + // Lastly save as many of their unregistered bases as possible + TrySaveFriendsSecretBases(&mixers[0], UNREGISTERED); + TrySaveFriendsSecretBases(&mixers[1], UNREGISTERED); + TrySaveFriendsSecretBases(&mixers[2], UNREGISTERED); } #define INIT_SECRET_BASE_RECORD_MIXER(linkId1, linkId2, linkId3) \ @@ -1684,23 +1759,25 @@ void ReceiveSecretBasesData(void *secretBases, size_t recordSize, u8 linkIdx) break; } - sub_80EAEF4(mixers); + SaveRecordMixBases(mixers); + for (i = 1; i < SECRET_BASES_COUNT; i++) { - if (gSaveBlock1Ptr->secretBases[i].sbr_field_1_0 == 1) + // In the process of deleting duplicate bases, if a base the player has registered is deleted it is + // flagged with the temporary toRegister flag, so it can be re-registered after it has been newly saved + if (gSaveBlock1Ptr->secretBases[i].toRegister == TRUE) { - gSaveBlock1Ptr->secretBases[i].registryStatus = 1; - gSaveBlock1Ptr->secretBases[i].sbr_field_1_0 = 0; + gSaveBlock1Ptr->secretBases[i].registryStatus = REGISTERED; + gSaveBlock1Ptr->secretBases[i].toRegister = FALSE; } } SortSecretBasesByRegistryStatus(); for (i = 1; i < SECRET_BASES_COUNT; i++) { - if (gSaveBlock1Ptr->secretBases[i].registryStatus == 2) - { - gSaveBlock1Ptr->secretBases[i].registryStatus = 0; - } + // Unmark "new" bases, they've been saved now and are no longer important + if (gSaveBlock1Ptr->secretBases[i].registryStatus == NEW) + gSaveBlock1Ptr->secretBases[i].registryStatus = UNREGISTERED; } if (gSaveBlock1Ptr->secretBases[0].secretBaseId != 0