clear item, save and load save files

This commit is contained in:
DizzyEggg
2018-04-29 14:21:59 +02:00
parent 25c669e36b
commit fd40e93cb5
43 changed files with 239 additions and 253 deletions

View File

@@ -8,6 +8,8 @@
#include "malloc.h"
#include "secret_base.h"
#include "item_menu.h"
#include "strings.h"
#include "load_save.h"
// These constants are used in gItems
enum
@@ -20,40 +22,34 @@ enum
POCKET_KEY_ITEMS,
};
extern void ApplyNewEncryptionKeyToHword(u16 *hword, u32 newKey);
extern bool8 InBattlePyramid(void);
extern const u8 gText_PokeBalls[];
extern const u8 gText_Berries[];
extern const u8 gText_Berry[];
extern u16 gUnknown_0203CF30[];
extern const struct Item gItems[];
// this file's functions
static bool8 CheckPyramidBagHasItem(u16 itemId, u16 count);
static bool8 CheckPyramidBagHasSpace(u16 itemId, u16 count);
static bool8 AddPyramidBagItem(u16 itemId, u16 count);
static bool8 RemovePyramidBagItem(u16 itemId, u16 count);
// EWRAM variables
EWRAM_DATA struct BagPocket gBagPockets[POCKETS_COUNT] = {0};
// code
u16 GetBagItemQuantity(u16 *quantity)
static u16 GetBagItemQuantity(u16 *quantity)
{
return gSaveBlock2Ptr->encryptionKey ^ *quantity;
}
void SetBagItemQuantity(u16 *quantity, u16 newValue)
static void SetBagItemQuantity(u16 *quantity, u16 newValue)
{
*quantity = newValue ^ gSaveBlock2Ptr->encryptionKey;
}
u16 GetPCItemQuantity(u16 *quantity)
static u16 GetPCItemQuantity(u16 *quantity)
{
return *quantity;
}
void SetPCItemQuantity(u16 *quantity, u16 newValue)
static void SetPCItemQuantity(u16 *quantity, u16 newValue)
{
*quantity = newValue;
}
@@ -91,26 +87,26 @@ void SetBagItemsPointers(void)
gBagPockets[BERRIES_POCKET].capacity = BAG_BERRIES_COUNT;
}
void CopyItemName(u16 itemId, u8 *string)
void CopyItemName(u16 itemId, u8 *dst)
{
StringCopy(string, ItemId_GetName(itemId));
StringCopy(dst, ItemId_GetName(itemId));
}
void CopyItemNameHandlePlural(u16 itemId, u8 *string, u32 quantity)
void CopyItemNameHandlePlural(u16 itemId, u8 *dst, u32 quantity)
{
if (itemId == ITEM_POKE_BALL)
{
if (quantity < 2)
StringCopy(string, ItemId_GetName(ITEM_POKE_BALL));
StringCopy(dst, ItemId_GetName(ITEM_POKE_BALL));
else
StringCopy(string, gText_PokeBalls);
StringCopy(dst, gText_PokeBalls);
}
else
{
if (itemId >= ITEM_CHERI_BERRY && itemId <= ITEM_ENIGMA_BERRY)
GetBerryCountString(string, gBerries[itemId - ITEM_CHERI_BERRY].name, quantity);
GetBerryCountString(dst, gBerries[itemId - ITEM_CHERI_BERRY].name, quantity);
else
StringCopy(string, ItemId_GetName(itemId));
StringCopy(dst, ItemId_GetName(itemId));
}
}
@@ -157,12 +153,12 @@ bool8 CheckBagHasItem(u16 itemId, u16 count)
if (gBagPockets[pocket].itemSlots[i].itemId == itemId)
{
u16 quantity;
//Does this item slot contain enough of the item?
// Does this item slot contain enough of the item?
quantity = GetBagItemQuantity(&gBagPockets[pocket].itemSlots[i].quantity);
if (quantity >= count)
return TRUE;
count -= quantity;
//Does this item slot and all previous slots contain enough of the item?
// Does this item slot and all previous slots contain enough of the item?
if (count == 0)
return TRUE;
}
@@ -901,7 +897,7 @@ static bool8 CheckPyramidBagHasSpace(u16 itemId, u16 count)
return FALSE;
}
static bool8 AddPyramidBagItem(u16 itemId, u16 count)
bool8 AddPyramidBagItem(u16 itemId, u16 count)
{
u16 i;
@@ -974,9 +970,7 @@ static bool8 AddPyramidBagItem(u16 itemId, u16 count)
}
}
extern u16 gUnknown_0203CF30[];
static bool8 RemovePyramidBagItem(u16 itemId, u16 count)
bool8 RemovePyramidBagItem(u16 itemId, u16 count)
{
u16 i;
@@ -1077,7 +1071,6 @@ const u8 *ItemId_GetDescription(u16 itemId)
return gItems[SanitizeItemId(itemId)].description;
}
u8 ItemId_GetImportance(u16 itemId)
{
return gItems[SanitizeItemId(itemId)].importance;

View File

@@ -4,33 +4,30 @@
#include "main.h"
#include "pokemon.h"
#include "random.h"
#include "malloc.h"
#include "item.h"
extern void* gUnknown_0203CF5C;
extern bool16 IdentifyFlash(void);
extern void SetBagItemsPointers(void);
extern void SetDecorationInventoriesPointers(void);
extern void ApplyNewEncryptionKeyToGameStats(u32 key);
extern void ApplyNewEncryptionKeyToBagItems(u32 newKey);
extern void ApplyNewEncryptionKeyToBagItems_(u32 key);
extern void ApplyNewEncryptionKeyToBerryPowder(u32 key);
extern void sub_8084FAC(int unused);
// this is probably wrong or misleading due to it being used in ResetHeap...
extern void InitHeap(void *pointer, u32 size);
#define SAVEBLOCK_MOVE_RANGE 128
struct LoadedSaveData
{
/*0x0000*/ struct ItemSlot items[30];
/*0x0078*/ struct ItemSlot keyItems[30];
/*0x00F0*/ struct ItemSlot pokeBalls[16];
/*0x0130*/ struct ItemSlot TMsHMs[64];
/*0x0230*/ struct ItemSlot berries[46];
/*0x0000*/ struct ItemSlot items[BAG_ITEMS_COUNT];
/*0x0078*/ struct ItemSlot keyItems[BAG_KEYITEMS_COUNT];
/*0x00F0*/ struct ItemSlot pokeBalls[BAG_POKEBALLS_COUNT];
/*0x0130*/ struct ItemSlot TMsHMs[BAG_TMHM_COUNT];
/*0x0230*/ struct ItemSlot berries[BAG_BERRIES_COUNT];
/*0x02E8*/ struct MailStruct mail[MAIL_COUNT];
};
// EWRAM DATA
EWRAM_DATA struct SaveBlock2 gSaveblock2 = {0};
EWRAM_DATA u8 gSaveblock2_DMA[SAVEBLOCK_MOVE_RANGE] = {0};
@@ -43,8 +40,13 @@ EWRAM_DATA u8 gSaveblock3_DMA[SAVEBLOCK_MOVE_RANGE] = {0};
EWRAM_DATA struct LoadedSaveData gLoadedSaveData = {0};
EWRAM_DATA u32 gLastEncryptionKey = {0};
void ApplyNewEncryptionKeyToAllEncryptedData(u32 encryptionKey);
// IWRAM common
IWRAM_DATA bool32 gFlashMemoryPresent;
IWRAM_DATA struct SaveBlock1 *gSaveBlock1Ptr;
IWRAM_DATA struct SaveBlock2 *gSaveBlock2Ptr;
IWRAM_DATA struct PokemonStorage *gPokemonStoragePtr;
// code
void CheckForFlashMemory(void)
{
if (!IdentifyFlash())
@@ -53,7 +55,9 @@ void CheckForFlashMemory(void)
InitFlashTimer();
}
else
{
gFlashMemoryPresent = FALSE;
}
}
void ClearSav2(void)
@@ -80,8 +84,6 @@ void SetSaveBlocksPointers(u16 offset)
SetDecorationInventoriesPointers();
}
extern u8 gHeap[];
void MoveSaveBlocks_ResetHeap(void)
{
void *vblankCB, *hblankCB;
@@ -132,7 +134,6 @@ void MoveSaveBlocks_ResetHeap(void)
gSaveBlock2Ptr->encryptionKey = encryptionKey;
}
u32 GetSecretBase2Field_9(void)
{
return gSaveBlock2Ptr->specialSaveWarp & 1;
@@ -159,112 +160,112 @@ void sav2_gender2_inplace_and_xFE(void)
gSaveBlock2Ptr->specialSaveWarp &= ~1;
}
void copy_player_party_to_sav1(void) // SavePlayerParty
void SavePlayerParty(void)
{
int i;
gSaveBlock1Ptr->playerPartyCount = gPlayerPartyCount;
for (i = 0; i < 6; i++)
for (i = 0; i < PARTY_SIZE; i++)
gSaveBlock1Ptr->playerParty[i] = gPlayerParty[i];
}
void copy_player_party_from_sav1(void) // LoadPlayerParty
void LoadPlayerParty(void)
{
int i;
gPlayerPartyCount = gSaveBlock1Ptr->playerPartyCount;
for (i = 0; i < 6; i++)
for (i = 0; i < PARTY_SIZE; i++)
gPlayerParty[i] = gSaveBlock1Ptr->playerParty[i];
}
void save_serialize_npcs(void) // SaveMapObjects
void SaveMapObjects(void)
{
int i;
for (i = 0; i < 16; i++)
for (i = 0; i < MAP_OBJECTS_COUNT; i++)
gSaveBlock1Ptr->mapObjects[i] = gMapObjects[i];
}
void save_deserialize_npcs(void) // LoadMapObjects
void LoadMapObjects(void)
{
int i;
for (i = 0; i < 16; i++)
for (i = 0; i < MAP_OBJECTS_COUNT; i++)
gMapObjects[i] = gSaveBlock1Ptr->mapObjects[i];
}
void SaveSerializedGame(void)
{
copy_player_party_to_sav1();
save_serialize_npcs();
SavePlayerParty();
SaveMapObjects();
}
void LoadSerializedGame(void)
{
copy_player_party_from_sav1();
save_deserialize_npcs();
LoadPlayerParty();
LoadMapObjects();
}
void copy_bags_and_unk_data_from_save_blocks(void)
void LoadPlayerBag(void)
{
int i;
// load player items.
for (i = 0; i < 30; i++)
for (i = 0; i < BAG_ITEMS_COUNT; i++)
gLoadedSaveData.items[i] = gSaveBlock1Ptr->bagPocket_Items[i];
// load player key items.
for (i = 0; i < 30; i++)
for (i = 0; i < BAG_KEYITEMS_COUNT; i++)
gLoadedSaveData.keyItems[i] = gSaveBlock1Ptr->bagPocket_KeyItems[i];
// load player pokeballs.
for (i = 0; i < 16; i++)
for (i = 0; i < BAG_POKEBALLS_COUNT; i++)
gLoadedSaveData.pokeBalls[i] = gSaveBlock1Ptr->bagPocket_PokeBalls[i];
// load player TMs and HMs.
for (i = 0; i < 64; i++)
for (i = 0; i < BAG_TMHM_COUNT; i++)
gLoadedSaveData.TMsHMs[i] = gSaveBlock1Ptr->bagPocket_TMHM[i];
// load player berries.
for (i = 0; i < 46; i++)
for (i = 0; i < BAG_BERRIES_COUNT; i++)
gLoadedSaveData.berries[i] = gSaveBlock1Ptr->bagPocket_Berries[i];
// load mail.
for (i = 0; i < 16; i++)
for (i = 0; i < MAIL_COUNT; i++)
gLoadedSaveData.mail[i] = gSaveBlock1Ptr->mail[i];
gLastEncryptionKey = gSaveBlock2Ptr->encryptionKey;
}
void copy_bags_and_unk_data_to_save_blocks(void)
void SavePlayerBag(void)
{
int i;
u32 encryptionKeyBackup;
// save player items.
for (i = 0; i < 30; i++)
for (i = 0; i < BAG_ITEMS_COUNT; i++)
gSaveBlock1Ptr->bagPocket_Items[i] = gLoadedSaveData.items[i];
// save player key items.
for (i = 0; i < 30; i++)
for (i = 0; i < BAG_KEYITEMS_COUNT; i++)
gSaveBlock1Ptr->bagPocket_KeyItems[i] = gLoadedSaveData.keyItems[i];
// save player pokeballs.
for (i = 0; i < 16; i++)
for (i = 0; i < BAG_POKEBALLS_COUNT; i++)
gSaveBlock1Ptr->bagPocket_PokeBalls[i] = gLoadedSaveData.pokeBalls[i];
// save player TMs and HMs.
for (i = 0; i < 64; i++)
for (i = 0; i < BAG_TMHM_COUNT; i++)
gSaveBlock1Ptr->bagPocket_TMHM[i] = gLoadedSaveData.TMsHMs[i];
// save player berries.
for (i = 0; i < 46; i++)
for (i = 0; i < BAG_BERRIES_COUNT; i++)
gSaveBlock1Ptr->bagPocket_Berries[i] = gLoadedSaveData.berries[i];
// save mail.
for (i = 0; i < 16; i++)
for (i = 0; i < MAIL_COUNT; i++)
gSaveBlock1Ptr->mail[i] = gLoadedSaveData.mail[i];
encryptionKeyBackup = gSaveBlock2Ptr->encryptionKey;

View File

@@ -7,6 +7,10 @@
EWRAM_DATA static u8 sUnknown = 0;
EWRAM_DATA static u32 sRandCount = 0;
// IWRAM common
IWRAM_DATA u32 gRngValue;
IWRAM_DATA u32 gRng2Value;
u16 Random(void)
{
gRngValue = 1103515245 * gRngValue + 24691;

View File

@@ -4,17 +4,15 @@
#include "constants/game_stat.h"
#include "task.h"
#include "decompress.h"
#include "load_save.h"
#include "overworld.h"
// for the chunk declarations
extern struct SaveBlock2 gSaveblock2;
extern struct SaveBlock1 gSaveblock1;
extern struct PokemonStorage gPokemonStorage;
extern struct SaveSectionLocation gRamSaveSectionLocations[0xE];
extern u8 gDecompressionBuffer[];
extern u32 gFlashMemoryPresent;
extern u16 gUnknown_03006294;
extern bool8 gSoftResetDisabled;
extern u32 gUnknown_0203CF5C;
// Divide save blocks into individual chunks to be written to flash sectors
@@ -24,13 +22,13 @@ extern bool8 gSoftResetDisabled;
/*
* Sector Layout:
*
*
* Sectors 0 - 13: Save Slot 1
* Sectors 14 - 27: Save Slot 2
* Sectors 28 - 29: Hall of Fame
* Sector 30: e-Reader/Mystery Gift Stuff (note: e-Reader is deprecated in Emerald US)
* Sector 31: Recorded Battle
*
*
* There are two save slots for saving the player's game data. We alternate between
* them each time the game is saved, so that if the current save slot is corrupt,
* we can load the previous one. We also rotate the sectors in each save slot
@@ -41,7 +39,7 @@ extern bool8 gSoftResetDisabled;
// (u8 *)structure was removed from the first statement of the macro in Emerald.
// This is because malloc is used to allocate addresses so storing the raw
// addresses should not be done in the offsets information.
// addresses should not be done in the offsets information.
#define SAVEBLOCK_CHUNK(structure, chunkNum) \
{ \
chunkNum * SECTOR_DATA_SIZE, \
@@ -69,8 +67,10 @@ const struct SaveSectionOffsets gSaveSectionOffsets[] =
};
extern void DoSaveFailedScreen(u8); // save_failed_screen
extern void LoadSerializedGame(void); // load_save
extern bool32 ProgramFlashSectorAndVerify(u8 sector, u8 *data);
extern void save_serialize_map(void);
extern void sub_800ADF8(void);
extern bool8 sub_800A520(void);
// iwram common
u16 gLastWrittenSector;
@@ -656,11 +656,6 @@ void UpdateSaveAddresses(void)
}
}
extern u32 GetGameStat(u8 index); // rom4
extern void IncrementGameStat(u8 index); // rom4
extern void SaveSerializedGame(void); // load_save
extern u32 gUnknown_0203CF5C;
u8 HandleSavingData(u8 saveType)
{
u8 i;
@@ -836,7 +831,7 @@ u16 sub_815355C(void)
struct SaveSection* savSection;
savSection = gFastSaveSection = &gSaveDataBuffer;
if (gFlashMemoryPresent != 1)
if (gFlashMemoryPresent != TRUE)
return 0;
UpdateSaveAddresses();
GetSaveValidStatus(gRamSaveSectionLocations);
@@ -897,12 +892,6 @@ u32 sub_8153634(u8 sector, u8* src)
return 1;
}
extern void save_serialize_map(void);
extern void sub_8076D5C(void);
extern void sav2_gender2_inplace_and_xFE(void);
extern void sub_800ADF8(void);
extern bool8 sub_800A520(void);
void sub_8153688(u8 taskId)
{
s16* taskData = gTasks[taskId].data;