From 08dfb0aa80668b80c4a31bd064f5f2d729e5b7f6 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 18 Aug 2017 13:49:00 -0400 Subject: Started working on serializing pokemon data The GBA program now sends serialized data about the first pokemon in the player's party over to the Wii. This data doesn't yet include all of the information that we will eventually want. It does, however, not transfer any private data, specifically IVs, EVs, and the personality value. It does this by deriving the public information (stats, nature, gender, shiny) before sending the pokemon over. Because of this, lookup tables for things such as base stats were needed, and given that these are large tables, it was easier to use the tables already existent in the game's ROM. Thus, the addresses of the three lookup tables that are now used are necessary for each ROM that this tool supports. I derived the addresses for version 1 of English Pokemon LeafGreen by dumping my own copy and searching through it with a text editor. Thus, at the current time, that cartridge is the only one that is supported. I will supplement this soon with addresses for the other four gen 3 carts that I have, but that will still not provide a very large amount of coverage. I have not yet decided how to address this issue. There is one current bug with the serialized data: the Wii doesn't seem to see the original trainer ID. Will fix. --- gba/Makefile | 2 +- gba/source/gamedata.c | 167 ++++++++++++++++++++++++++++++++-- gba/source/gamedata.h | 191 ++++++++++++++++++++++++++++++++++++++- gba/source/main.c | 86 ++++++++++++------ gba/source/savestructs.h | 7 +- gba/source/serialize.c | 227 +++++++++++++++++++++++++++++++++++++++++++++++ gba/source/serialize.h | 19 ++++ 7 files changed, 656 insertions(+), 43 deletions(-) create mode 100644 gba/source/serialize.c create mode 100644 gba/source/serialize.h (limited to 'gba') diff --git a/gba/Makefile b/gba/Makefile index 49d0777..7097b49 100644 --- a/gba/Makefile +++ b/gba/Makefile @@ -25,7 +25,7 @@ BUILD := build SOURCES := source DATA := GRAPHICS := gfx -INCLUDES := +INCLUDES := ../include #--------------------------------------------------------------------------------- # options for code generation diff --git a/gba/source/gamedata.c b/gba/source/gamedata.c index bbcf4dd..ae593b1 100644 --- a/gba/source/gamedata.c +++ b/gba/source/gamedata.c @@ -7,7 +7,7 @@ */ #include "gamedata.h" -void decryptSaveStructures( +static void decryptSaveStructures( pSaveBlock1 SaveBlock1, pSaveBlock2 SaveBlock2, pSaveBlock3 SaveBlock3) @@ -119,10 +119,15 @@ void decryptSaveStructures( *xor_key_ptr = 0; } -bool initSaveData( - pSaveBlock1* SaveBlock1, - pSaveBlock2* SaveBlock2, - pSaveBlock3* SaveBlock3) +static void CryptBoxPokemon(struct BoxPokemon* pkm) +{ + for (u32 i = 0; i < 12; i++) + { + pkm->secure.raw[i] ^= (pkm->otId ^ pkm->personality); + } +} + +bool initSaveData(struct GameData* gameData) { // check the ROM code, make sure this game is supported. u8* ROM = (u8*) 0x8000000; @@ -135,6 +140,9 @@ bool initSaveData( pSaveBlock1 gSaveBlock1; pSaveBlock2 gSaveBlock2; pSaveBlock3 gSaveBlock3; + struct BaseStats* gBaseStats; + const u32 (*gExpTables)[101]; + const u16* gNatOrder; //u32 titlemid = 0; // get the address of the save loading function. @@ -337,6 +345,9 @@ bool initSaveData( //mainloop = (void(*)()) 0x8000429; //titlemid = 0x807928f; //load_pokemon = (void(*)()) 0x804c245; + gBaseStats = (struct BaseStats*) ( GAME_FR ? 0 : 0x82547d0 ); + gExpTables = (ExperienceTables) ( GAME_FR ? 0 : 0x8253b30 ); + gNatOrder = (const u16*) ( GAME_FR ? 0 : 0x825203a ); break; } @@ -504,9 +515,149 @@ bool initSaveData( // applicable. decryptSaveStructures(gSaveBlock1,gSaveBlock2,gSaveBlock3); - *SaveBlock1 = gSaveBlock1; - *SaveBlock2 = gSaveBlock2; - *SaveBlock3 = gSaveBlock3; + gameData->SaveBlock1 = gSaveBlock1; + gameData->SaveBlock2 = gSaveBlock2; + gameData->SaveBlock3 = gSaveBlock3; + gameData->baseStats = gBaseStats; + gameData->expTables = gExpTables; + gameData->natOrder = gNatOrder; return true; } + +void DecryptPokemon(struct Pokemon* pkm) +{ + struct BoxPokemon* boxMon = &(pkm->box); + + CryptBoxPokemon(boxMon); +} + +void DecryptBoxPokemon(struct BoxPokemon* pkm) +{ + CryptBoxPokemon(pkm); +} + +void EncryptPokemon(struct Pokemon* pkm) +{ + struct BoxPokemon* boxMon = &(pkm->box); + + EncryptBoxPokemon(boxMon); +} + +void EncryptBoxPokemon(struct BoxPokemon* pkm) +{ + FixBoxPokemonChecksum(pkm); + CryptBoxPokemon(pkm); +} + +union PokemonSubstruct* GetPokemonSubstruct(struct Pokemon* pkm,u8 substructId) +{ + struct BoxPokemon* boxMon = &(pkm->box); + + return GetBoxPokemonSubstruct(boxMon,substructId); +} + +union PokemonSubstruct* GetBoxPokemonSubstruct( + struct BoxPokemon* pkm, + u8 substructId) +{ + if (substructId > 3) + { + return NULL; + } + + u32 personality = pkm->personality; + u32 modulo = (personality % 24); + + // Staring at the substruct indexes, I noticed the last two columns are the + // reverse of the first two! that is, substructId==2 column is the reverse of + // substructId==1, substructId==3 is the reverse of substructId==0. + // At least that means there's no need to hardcode all four. + u8 substruct_idxes[2][24] = { + { 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 2, 3, 1, 1, 2, 3, 2, 3, 1, 1, 2, 3, 2, 3 }, + { 1, 1, 2, 3, 2, 3, 0, 0, 0, 0, 0, 0, 2, 3, 1, 1, 3, 2, 2, 3, 1, 1, 3, 2 } + }; + + if (substructId < 2) + { + return &(pkm->secure.substructs[substruct_idxes[substructId][modulo]]); + } + + return &(pkm->secure.substructs[ + substruct_idxes[3 - substructId][23 - modulo]]); +} + +u16 CalculateBoxPokemonChecksum(struct BoxPokemon* pkm) +{ + u16 checksum = 0; + + union PokemonSubstruct* substructs[4] = { + GetBoxPokemonSubstruct(pkm,0), + GetBoxPokemonSubstruct(pkm,1), + GetBoxPokemonSubstruct(pkm,2), + GetBoxPokemonSubstruct(pkm,3) + }; + + for (int substruct = 0; substruct < 4; substruct++) + { + for (int i = 0; i < 6; i++) + { + checksum += substructs[substruct]->raw[i]; + } + } + + return checksum; +} + +void FixBoxPokemonChecksum(struct BoxPokemon* pkm) +{ + pkm->checksum = CalculateBoxPokemonChecksum(pkm); +} + +struct PokemonSubstruct0* GetPokemonSubstruct0(struct Pokemon* pkm) +{ + struct BoxPokemon* boxMon = &(pkm->box); + + return GetBoxPokemonSubstruct0(boxMon); +} + +struct PokemonSubstruct0* GetBoxPokemonSubstruct0(struct BoxPokemon* pkm) +{ + return &(GetBoxPokemonSubstruct(pkm,0)->type0); +} + +struct PokemonSubstruct1* GetPokemonSubstruct1(struct Pokemon* pkm) +{ + struct BoxPokemon* boxMon = &(pkm->box); + + return GetBoxPokemonSubstruct1(boxMon); +} + +struct PokemonSubstruct1* GetBoxPokemonSubstruct1(struct BoxPokemon* pkm) +{ + return &(GetBoxPokemonSubstruct(pkm,1)->type1); +} + +struct PokemonSubstruct2* GetPokemonSubstruct2(struct Pokemon* pkm) +{ + struct BoxPokemon* boxMon = &(pkm->box); + + return GetBoxPokemonSubstruct2(boxMon); +} + +struct PokemonSubstruct2* GetBoxPokemonSubstruct2(struct BoxPokemon* pkm) +{ + return &(GetBoxPokemonSubstruct(pkm,2)->type2); +} + +struct PokemonSubstruct3* GetPokemonSubstruct3(struct Pokemon* pkm) +{ + struct BoxPokemon* boxMon = &(pkm->box); + + return GetBoxPokemonSubstruct3(boxMon); +} + +struct PokemonSubstruct3* GetBoxPokemonSubstruct3(struct BoxPokemon* pkm) +{ + return &(GetBoxPokemonSubstruct(pkm,3)->type3); +} diff --git a/gba/source/gamedata.h b/gba/source/gamedata.h index 4d0a2a9..247626a 100644 --- a/gba/source/gamedata.h +++ b/gba/source/gamedata.h @@ -21,9 +21,192 @@ #define LANG_JAPAN ((*(u8*)(0x80000AF)) == 'J') -bool initSaveData( - pSaveBlock1* SaveBlock1, - pSaveBlock2* SaveBlock2, - pSaveBlock3* SaveBlock3); +typedef const u32 (*ExperienceTables)[101]; + +struct GameData { + pSaveBlock1 SaveBlock1; + pSaveBlock2 SaveBlock2; + pSaveBlock3 SaveBlock3; + struct BaseStats* baseStats; + ExperienceTables expTables; + const u16* natOrder; +}; + +bool initSaveData(struct GameData* gameData); + +/** + * Decrypts the substructures of a Pokémon structure, so they can be viewed or + * modified easily. + * + * Remember to call EncryptPokemon() afterwards. + * + * @param pkm The Pokémon to decrypt the substructures of. + */ +void DecryptPokemon(struct Pokemon* pkm); + +/** + * Decrypts the substructures of a core Pokémon structure, so they can be viewed + * or modified easily. + * + * Used by DecryptPokemon(). + * + * Remember to call EncryptPokemon() afterwards. + * + * @param pkm The BoxPokemon to decrypt the substructures of. + */ +void DecryptBoxPokemon(struct BoxPokemon* pkm); + +/** + * Encrypts the substructures of a Pokémon structure, and fixes the checksum. + * + * Must be used after DecryptPokemon() has been called, otherwise the Pokémon + * you decrypted and forgot to re-encrypt will become a Bad Egg. + * + * @param pkm The Pokémon to encrypt the substructures and fix + * the checksum of. + */ +void EncryptPokemon(struct Pokemon* pkm); + +/** + * Encrypts the substructures of a core Pokémon structure, and fixes the + * checksum. + * + * Must be used after DecryptBoxPokemon() has been called, otherwise the Pokémon + * you decrypted and forgot to re-encrypt will become a Bad Egg. + * + * @param pkm The BoxPokemon to encrypt the substructures and fix the checksum + * of. + */ +void EncryptBoxPokemon(struct BoxPokemon* pkm); + +/** + * Gets a substructure of a Pokémon structure. + * + * Call DecryptPokemon() first or the substructure data will be encrypted. + * + * @param pkm The Pokemon to get a substructure of. + * @param substructId The substructure to get. + * + * @return The substructure. + */ +union PokemonSubstruct* GetPokemonSubstruct(struct Pokemon* pkm,u8 substructId); + +/** + * Gets a substructure of a core Pokémon structure. + * + * Call DecryptBoxPokemon() first or the substructure data will be encrypted. + * + * @param pkm The Pokemon to get a substructure of. + * @param substructId The substructure to get. + * + * @return The substructure. + */ +union PokemonSubstruct* GetBoxPokemonSubstruct( + struct BoxPokemon* pkm, + u8 substructId); + +/** + * Gets the checksum of a core Pokémon structure. + * + * @param pkm The BoxPokemon to calculate the checksum of. + * + * @return The checksum. + */ +u16 CalculateBoxPokemonChecksum(struct BoxPokemon* pkm); + +/** + * Fixes the checksum of a core Pokémon structure. + * + * @param pkm The BoxPokemon to fix the checksum of. + */ +void FixBoxPokemonChecksum(struct BoxPokemon* pkm); + +/** + * Gets the zeroth substructure ("Growth") of a Pokémon structure. + * + * Call DecryptPokemon() first or the substructure data will be encrypted. + * + * @param pkm The Pokémon to get a substructure of. + * + * @return The substructure. + */ +struct PokemonSubstruct0* GetPokemonSubstruct0(struct Pokemon* pkm); + +/** + * Gets the zeroth substructure ("Growth") of a core Pokémon structure. + * + * Call DecryptBoxPokemon() first or the substructure data will be encrypted. + * + * @param pkm The BoxPokemon to get the substructure of. + * + * @return The substructure. + */ +struct PokemonSubstruct0* GetBoxPokemonSubstruct0(struct BoxPokemon* pkm); + +/** + * Gets the first substructure ("Attacks") of a Pokémon structure. + * + * Call DecryptPokemon() first or the substructure data will be encrypted. + * + * @param pkm The Pokémon to get a substructure of. + * + * @return The substructure. + */ +struct PokemonSubstruct1* GetPokemonSubstruct1(struct Pokemon* pkm); + +/** + * Gets the first substructure ("Attacks") of a core Pokémon structure. + * + * Call DecryptBoxPokemon() first or the substructure data will be encrypted. + * + * @param pkm The BoxPokemon to get the substructure of. + * + * @return The substructure. + */ +struct PokemonSubstruct1* GetBoxPokemonSubstruct1(struct BoxPokemon* pkm); + +/** + * Gets the second substructure ("EVs & Condition") of a Pokémon structure. + * + * Call DecryptPokemon() first or the substructure data will be encrypted. + * + * @param pkm The Pokémon to get a substructure of. + * + * @return The substructure. + */ +struct PokemonSubstruct2* GetPokemonSubstruct2(struct Pokemon* pkm); + +/** + * Gets the second substructure ("EVs & Condition") of a core Pokémon structure. + * + * Call DecryptBoxPokemon() first or the substructure data will be encrypted. + * + * @param pkm The BoxPokemon to get the substructure of. + * + * @return The substructure. + */ +struct PokemonSubstruct2* GetBoxPokemonSubstruct2(struct BoxPokemon* pkm); + +/** + * Gets the third substructure ("Miscellaneous") of a Pokémon structure. + * + * Call DecryptPokemon() first or the substructure data will be encrypted. + * + * @param pkm The Pokémon to get a substructure of. + * + * @return The substructure. + */ +struct PokemonSubstruct3* GetPokemonSubstruct3(struct Pokemon* pkm); + +/** + * Gets the third substructure ("Miscellaneous") of a core Pokémon structure. + * + * Call DecryptBoxPokemon() first or the substructure data will be encrypted. + * + * @param pkm The BoxPokemon to get the substructure of. + * + * @return The substructure. + */ +struct PokemonSubstruct3* GetBoxPokemonSubstruct3(struct BoxPokemon* pkm); #endif diff --git a/gba/source/main.c b/gba/source/main.c index 14d2f1d..aeb05af 100644 --- a/gba/source/main.c +++ b/gba/source/main.c @@ -7,6 +7,7 @@ #include #include "gamedata.h" #include "link.h" +#include "serialize.h" int main(void) { @@ -42,11 +43,9 @@ int main(void) waitForAck(); // Get access to save data. - pSaveBlock1 SaveBlock1; - pSaveBlock2 SaveBlock2; - pSaveBlock3 SaveBlock3; + struct GameData gameData; - if (!initSaveData(&SaveBlock1, &SaveBlock2, &SaveBlock3)) + if (!initSaveData(&gameData)) { // Unsupported game version. sendS32(-1); @@ -63,13 +62,13 @@ int main(void) if (GAME_RS) { - trainerName = SaveBlock2->rs.playerName; + trainerName = gameData.SaveBlock2->rs.playerName; } else if (GAME_FRLG) { - trainerName = SaveBlock2->frlg.playerName; + trainerName = gameData.SaveBlock2->frlg.playerName; } else if (GAME_EM) { - trainerName = SaveBlock2->e.playerName; + trainerName = gameData.SaveBlock2->e.playerName; } u32 tn1 = @@ -94,20 +93,24 @@ int main(void) u8* trainerId = 0; if (GAME_RS) { - trainerId = SaveBlock2->rs.playerTrainerId; + trainerId = gameData.SaveBlock2->rs.playerTrainerId; } else if (GAME_FRLG) { - trainerId = SaveBlock2->frlg.playerTrainerId; + trainerId = gameData.SaveBlock2->frlg.playerTrainerId; } else if (GAME_EM) { - trainerId = SaveBlock2->e.playerTrainerId; + trainerId = gameData.SaveBlock2->e.playerTrainerId; } - u32 tti = + u16 trainerIdNum = (trainerId[1] << 8) | (trainerId[0]); - sendU32(tti); + u16 secretIdNum = + (trainerId[3] << 8) + | (trainerId[2]); + + sendU32(trainerIdNum); waitForAck(); // Does the player want to import this game? @@ -120,22 +123,22 @@ int main(void) u8* pokedexSeen = 0; if (GAME_RS) { - pokedexSeen = SaveBlock2->rs.pokedex.seen; + pokedexSeen = gameData.SaveBlock2->rs.pokedex.seen; } else if (GAME_FRLG) { - pokedexSeen = SaveBlock2->frlg.pokedex.seen; + pokedexSeen = gameData.SaveBlock2->frlg.pokedex.seen; } else if (GAME_EM) { - pokedexSeen = SaveBlock2->e.pokedex.seen; + pokedexSeen = gameData.SaveBlock2->e.pokedex.seen; } for (int i=0; i<13; i++) { u32 psi = - (pokedexSeen[i*4]) - | (pokedexSeen[i*4+1] << 8) - | (pokedexSeen[i*4+2] << 16) - | (pokedexSeen[i*4+3] << 24); + (pokedexSeen[i*4] << 24) + | (pokedexSeen[i*4+1] << 16) + | (pokedexSeen[i*4+2] << 8) + | (pokedexSeen[i*4+3]); directSendU32(psi); } @@ -143,23 +146,54 @@ int main(void) u8* pokedexCaught = 0; if (GAME_RS) { - pokedexCaught = SaveBlock2->rs.pokedex.owned; + pokedexCaught = gameData.SaveBlock2->rs.pokedex.owned; } else if (GAME_FRLG) { - pokedexCaught = SaveBlock2->frlg.pokedex.owned; + pokedexCaught = gameData.SaveBlock2->frlg.pokedex.owned; } else if (GAME_EM) { - pokedexCaught = SaveBlock2->e.pokedex.owned; + pokedexCaught = gameData.SaveBlock2->e.pokedex.owned; } for (int i=0; i<13; i++) { u32 psi = - (pokedexCaught[i*4]) - | (pokedexCaught[i*4+1] << 8) - | (pokedexCaught[i*4+2] << 16) - | (pokedexCaught[i*4+3] << 24); + (pokedexCaught[i*4] << 24) + | (pokedexCaught[i*4+1] << 16) + | (pokedexCaught[i*4+2] << 8) + | (pokedexCaught[i*4+3]); directSendU32(psi); } + + // Start sending over party pokémon. + struct Pokemon* playerParty = 0; + if (GAME_RS) + { + playerParty = gameData.SaveBlock1->rs.playerParty; + } else if (GAME_FRLG) + { + playerParty = gameData.SaveBlock1->frlg.playerParty; + } else if (GAME_EM) + { + playerParty = gameData.SaveBlock1->e.playerParty; + } + + waitForResponse(); + + u32 partyCount = 1; + + sendU32(partyCount); + waitForAck(); + + for (int pki=0; pkibox); + + struct PokemonIntermediate pki; + + PokemonIntermediateInit(&pki, bpkm, trainerIdNum, secretIdNum, &gameData); + PokemonIntermediateStream(&pki); + } } diff --git a/gba/source/savestructs.h b/gba/source/savestructs.h index 2bf4d4d..fb8ef36 100644 --- a/gba/source/savestructs.h +++ b/gba/source/savestructs.h @@ -9,9 +9,7 @@ // Most of the structures come from pokeruby, FR/LG changes come from my own research / the firered IDB on pokecommunity -#define POKEMON_NAME_LENGTH 10 -#define OT_NAME_LENGTH 7 -#define TILE_SIZE_4BPP 32 +#include "pokemon.h" struct Coords16 { @@ -495,6 +493,7 @@ struct BaseStats /* 0x17 */ u8 ability2; /* 0x18 */ u8 safariZoneFleeRate; /* 0x19 */ u8 bodyColor; + u16 filler; }; struct BattleMove @@ -790,4 +789,4 @@ struct Pokedex /*0x0C*/ u32 unknown3; /*0x10*/ u8 owned[52]; /*0x44*/ u8 seen[52]; -}; \ No newline at end of file +}; diff --git a/gba/source/serialize.c b/gba/source/serialize.c new file mode 100644 index 0000000..6303772 --- /dev/null +++ b/gba/source/serialize.c @@ -0,0 +1,227 @@ +#include "serialize.h" +#include "gamedata.h" +#include "link.h" + +#define UNOWN_SPECIES_INDEX 201 +#define SHEDINJA_SPECIES_INDEX 303 + +enum Stat { + StatAttack, + StatDefense, + StatSpeed, + StatSpAttack, + StatSpDefense +}; + +u32 CalculateStat( + u8 base, + u32 iv, + u8 ev, + u8 level, + u8 statIndex, + u8 nature) +{ + u32 n = (((2 * base + iv + ev / 4) * level) / 100) + 5; + + u8 naturePlus = (nature / 5); + u8 natureMinus = (nature % 5); + + if (naturePlus != natureMinus) + { + if (statIndex == naturePlus) + { + return (u32)(n * 110) / 100; + } else if (statIndex == natureMinus) + { + return (u32)(n * 90) / 100; + } + } + + return n; +} + +void PokemonIntermediateInit( + struct PokemonIntermediate* pki, + struct BoxPokemon* bpkm, + u16 trainerId, + u16 secretId, + const struct GameData* gameData) +{ + DecryptBoxPokemon(bpkm); + + struct PokemonSubstruct0* sub0 = GetBoxPokemonSubstruct0(bpkm); + struct PokemonSubstruct1* sub1 = GetBoxPokemonSubstruct1(bpkm); + struct PokemonSubstruct2* sub2 = GetBoxPokemonSubstruct2(bpkm); + struct PokemonSubstruct3* sub3 = GetBoxPokemonSubstruct3(bpkm); + + struct BaseStats* baseStats = &gameData->baseStats[sub0->species]; + + for (int i=0; inickname[i] = bpkm->nickname[i]; + } + + for (int i=0; iotName[i] = bpkm->otName[i]; + } + + pki->otId = bpkm->otId; + pki->otGender = sub3->otGender; + pki->species = gameData->natOrder[sub0->species - 1]; + pki->heldItem = sub0->heldItem; + pki->experience = sub0->experience; + + pki->moves[0] = sub1->moves[0]; + pki->moves[1] = sub1->moves[1]; + pki->moves[2] = sub1->moves[2]; + pki->moves[3] = sub1->moves[3]; + + pki->ppBonuses = sub0->ppBonuses; + pki->metLevel = sub3->metLevel; + pki->metLocation = sub3->metLocation; + pki->pokeball = sub3->pokeball; + pki->altAbility = sub3->altAbility; + + // Derive nature from the personality value. + pki->nature = (bpkm->personality % 25); + + // Derive gender from the personality value. + int genderThreshold = baseStats->genderRatio; + + if ((genderThreshold == 0) || (genderThreshold == 255)) + { + pki->gender = 0; + } else if (genderThreshold == 254) + { + pki->gender = 1; + } else { + u8 genderDeterminer = bpkm->personality & 0x000000FF; + + if (genderDeterminer >= genderThreshold) + { + pki->gender = 0; + } else { + pki->gender = 1; + } + } + + // Determine shininess from the personality value. + u16 shinyDeterminer = + (trainerId) + ^ (secretId) + ^ ((bpkm->personality >> 16) & 0x0000FFFF) + ^ (bpkm->personality & 0x0000FFFF); + + if (shinyDeterminer < 8) + { + pki->shiny = 1; + } else { + pki->shiny = 0; + } + + // Determine Unown letter from the personality value. + if (sub0->species == UNOWN_SPECIES_INDEX) + { + u8 unownDeterminer = + ((bpkm->personality & 0x07000000) >> 18) + | ((bpkm->personality & 0x00070000) >> 12) + | ((bpkm->personality & 0x00000700) >> 6) + | (bpkm->personality & 0x00000007); + + pki->unownLetter = (unownDeterminer % 28); + } + + // Calculate level from experience. + pki->level = 1; + + const u32* expTable = gameData->expTables[baseStats->growthRate]; + while ((pki->level <= 100) && (expTable[pki->level] <= sub0->experience)) + { + pki->level++; + } + + pki->level--; + + // Calculate stats. + if (sub0->species == SHEDINJA_SPECIES_INDEX) + { + pki->hp = 1; + } else { + u32 n = 2 * baseStats->baseHP + sub3->hpIV; + pki->hp = (((n + sub2->hpEV / 4) * pki->level) / 100) + pki->level + 10; + } + + pki->attack = CalculateStat( + baseStats->baseAttack, + sub3->attackIV, + sub2->attackEV, + pki->level, + 0, + pki->nature); + + pki->defense = CalculateStat( + baseStats->baseDefense, + sub3->defenseIV, + sub2->defenseEV, + pki->level, + 1, + pki->nature); + + pki->speed = CalculateStat( + baseStats->baseSpeed, + sub3->speedIV, + sub2->speedEV, + pki->level, + 2, + pki->nature); + + pki->spAttack = CalculateStat( + baseStats->baseSpAttack, + sub3->spAttackIV, + sub2->spAttackEV, + pki->level, + 3, + pki->nature); + + pki->spDefense = CalculateStat( + baseStats->baseSpDefense, + sub3->spDefenseIV, + sub2->spDefenseEV, + pki->level, + 4, + pki->nature); + + // Approximate the values of the contest conditions. + pki->cool = ((u16)(sub2->cool) * 10) / 255; + pki->beauty = ((u16)(sub2->beauty) * 10) / 255; + pki->cute = ((u16)(sub2->cute) * 10) / 255; + pki->smart = ((u16)(sub2->smart) * 10) / 255; + pki->tough = ((u16)(sub2->tough) * 10) / 255; + pki->sheen = ((u16)(sub2->sheen) * 10) / 255; + + // Determine Pokerus status. + if (sub3->pokerus & 0xF0) + { + if (sub3->pokerus & 0x0F) + { + pki->pokerus = 1; + } else { + pki->pokerus = 2; + } + } else { + pki->pokerus = 0; + } + + EncryptBoxPokemon(bpkm); +} + +void PokemonIntermediateStream(struct PokemonIntermediate* pki) +{ + u32* raw = (u32*)pki; + + for (int i=0; i<(sizeof(struct PokemonIntermediate)/4); i++) + { + directSendU32(raw[i]); + } +} diff --git a/gba/source/serialize.h b/gba/source/serialize.h new file mode 100644 index 0000000..21049c5 --- /dev/null +++ b/gba/source/serialize.h @@ -0,0 +1,19 @@ +#ifndef POKEMON_H_67C60AC1 +#define POKEMON_H_67C60AC1 + +#include + +struct BoxPokemon; +struct PokemonIntermediate; +struct GameData; + +void PokemonIntermediateInit( + struct PokemonIntermediate* pki, + struct BoxPokemon* bpkm, + u16 trainerId, + u16 secretId, + const struct GameData* gameData); + +void PokemonIntermediateStream(struct PokemonIntermediate* pki); + +#endif /* end of include guard: POKEMON_H_67C60AC1 */ -- cgit 1.4.1