Skip to content

Commit

Permalink
More static work
Browse files Browse the repository at this point in the history
  • Loading branch information
Admiral-Fish committed Jun 6, 2024
1 parent ff14f75 commit 0cdef43
Show file tree
Hide file tree
Showing 12 changed files with 348 additions and 37 deletions.
1 change: 1 addition & 0 deletions Source/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ add_library(PokeFinderCore STATIC
Gen5/States/SearcherState5.hpp
Gen5/States/State5.hpp
Gen5/States/WildState5.hpp
Gen5/StaticTemplate5.hpp
Gen8/Den.hpp
Gen8/EncounterArea8.cpp
Gen8/EncounterArea8.hpp
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/Gen5/Encounters5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ namespace Encounters5
return encounters;
}

const StaticTemplate *getStaticEncounters(int index, int *size)
const StaticTemplate5 *getStaticEncounters(int index, int *size)
{
if (index == 0)
{
Expand Down Expand Up @@ -323,9 +323,9 @@ namespace Encounters5
}
}

const StaticTemplate *getStaticEncounter(int type, int index)
const StaticTemplate5 *getStaticEncounter(int type, int index)
{
const StaticTemplate *templates = getStaticEncounters(type);
const StaticTemplate5 *templates = getStaticEncounters(type);
return &templates[index];
}
}
6 changes: 3 additions & 3 deletions Source/Core/Gen5/Encounters5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DreamRadarTemplate;
class EncounterArea5;
class HiddenGrottoArea;
class Profile5;
class StaticTemplate;
class StaticTemplate5;
enum class Encounter : u8;

namespace Encounters5
Expand Down Expand Up @@ -83,7 +83,7 @@ namespace Encounters5
*
* @return Pointer to static encounters area
*/
const StaticTemplate *getStaticEncounters(int index, int *size = nullptr);
const StaticTemplate5 *getStaticEncounters(int index, int *size = nullptr);

/**
* @brief Gets static encounters from the \p type and \p index
Expand All @@ -100,7 +100,7 @@ namespace Encounters5
*
* @return Pointer to static encounter
*/
const StaticTemplate *getStaticEncounter(int type, int index);
const StaticTemplate5 *getStaticEncounter(int type, int index);
}

#endif // ENCOUNTERS5_HPP
4 changes: 2 additions & 2 deletions Source/Core/Gen5/Generators/DreamRadarGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ std::vector<DreamRadarState> DreamRadarGenerator::generate(u64 seed) const
{
pid = Utilities5::forceGender(pid, go, 0, 0);
}
else if (radarTemplate.getGender() == 0 || radarTemplate.getGender() == 1)
else if (radarTemplate.getGender() < 2)
{
pid = Utilities5::forceGender(pid, go, radarTemplate.getGender(), info->getGender());
}
Expand All @@ -101,7 +101,7 @@ std::vector<DreamRadarState> DreamRadarGenerator::generate(u64 seed) const
pid ^= 0x10000;

// Force non-shiny
if (((pid >> 16) ^ (pid & 0xffff) ^ tsv) < 8)
if (Utilities::isShiny<true>(pid, tsv))
{
pid ^= 0x10000000;
}
Expand Down
10 changes: 5 additions & 5 deletions Source/Core/Gen5/Generators/EventGenerator5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ std::vector<State5> EventGenerator5::generate(u64 seed) const
std::array<u8, 6> ivs;
for (u8 i = 0; i < 6; i++)
{
u8 parameterIV = pgf.getIV(i);
if (parameterIV == 255)
u8 iv = pgf.getIV(i);
if (iv == 255)
{
ivs[i] = go.nextUInt(32);
}
else
{
ivs[i] = parameterIV;
ivs[i] = iv;
}
}

Expand All @@ -67,14 +67,14 @@ std::vector<State5> EventGenerator5::generate(u64 seed) const

// Gender locked handling
u32 pid = go.nextUInt();
if (pgf.getGender() == 0 || pgf.getGender() == 1)
if (pgf.getGender() < 2)
{
pid = Utilities5::forceGender(pid, go, pgf.getGender(), info->getGender());
}

if (pgf.getShiny() == 0) // No shiny
{
if (((pid >> 16) ^ (pid & 0xffff) ^ tsv) < 8)
if (Utilities::isShiny<true>(pid, tsv))
{
pid ^= 0x10000000;
}
Expand Down
167 changes: 164 additions & 3 deletions Source/Core/Gen5/Generators/StaticGenerator5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,181 @@
*/

#include "StaticGenerator5.hpp"
#include <Core/Enum/Lead.hpp>
#include <Core/Gen5/States/State5.hpp>
#include <Core/RNG/LCRNG64.hpp>
#include <Core/RNG/MT.hpp>
#include <Core/RNG/RNGList.hpp>
#include <Core/Util/Utilities.hpp>
#include <algorithm>

StaticGenerator5::StaticGenerator5(u32 initialAdvances, u32 maxAdvances, u32 delay, Method method, Lead lead,
const StaticTemplate &staticTemplate, const Profile5 &profile, const StateFilter &filter) :
StaticGenerator(initialAdvances, maxAdvances, delay, method, lead, staticTemplate, profile, filter)
static u8 gen(MT &rng)
{
return rng.next() >> 27;
}

static u16 getItem(BWRNG &rng, Lead lead, const PersonalInfo *info)
{
constexpr u8 ItemTableRange[3][3] = { { 50, 55, 56 }, { 60, 80, 85 } };

u8 rand = rng.nextUInt(100);
if (rand < ItemTableRange[lead == Lead::CompoundEyes ? 1 : 0][0])
{
return info->getItem(0);
}
else if (rand < ItemTableRange[lead == Lead::CompoundEyes ? 1 : 0][1])
{
return info->getItem(1);
}
else if (rand < ItemTableRange[lead == Lead::CompoundEyes ? 1 : 0][2])
{
return info->getItem(2);
}
else
{
return 0;
}
}

StaticGenerator5::StaticGenerator5(u32 initialAdvances, u32 maxAdvances, u32 delay, Method method, Lead lead, u8 luckyPower,
const StaticTemplate5 &staticTemplate, const Profile5 &profile, const StateFilter &filter) :
StaticGenerator(initialAdvances, maxAdvances, delay, method, lead, staticTemplate, profile, filter),
luckyPower((profile.getVersion() & Game::BW) != Game::None ? 0 : luckyPower)
{
}

std::vector<State5> StaticGenerator5::generate(u64 seed, u32 initialAdvances, u32 maxAdvances) const
{
bool bw = (profile.getVersion() & Game::BW) != Game::None;

std::vector<std::pair<u32, std::array<u8, 6>>> ivs;

RNGList<u8, MT, 8, gen> rngList(seed >> 32, initialAdvances + (bw ? 0 : 2));
for (u32 cnt = 0; cnt <= maxAdvances; cnt++)
{
std::array<u8, 6> iv;
std::generate(iv.begin(), iv.end(), [&rngList] { return rngList.next(); });
if (filter.compareIV(iv))
{
ivs.emplace_back(initialAdvances + cnt, iv);
}
}

if (ivs.empty())
{
return std::vector<State5>();
}
else
{
return generate(seed, ivs);
}

return std::vector<State5>();
}

std::vector<State5> StaticGenerator5::generate(u64 seed, const std::vector<std::pair<u32, std::array<u8, 6>>> &ivs) const
{
u32 advances = Utilities5::initialAdvances(seed, profile);
BWRNG rng(seed, advances + initialAdvances);
auto jump = rng.getJump(delay);
const PersonalInfo *info = staticTemplate.getInfo();

bool bw = (profile.getVersion() & Game::BW) != Game::None;
bool id = (profile.getTID() & 1) ^ (profile.getSID() & 1);

u8 shinyRolls = 1;
if ((profile.getVersion() & Game::BW2) != Game::None)
{
if (profile.getShinyCharm())
{
shinyRolls += 2;
}

if (luckyPower == 3)
{
shinyRolls++;
}
}

std::vector<State5> states;
for (u32 cnt = 0; cnt <= maxAdvances; cnt++)
{
BWRNG go(rng, jump);

bool cuteCharm = false;
bool sync = false;

if (lead <= Lead::SynchronizeEnd)
{
sync = go.nextUInt(2);
}
else if (lead == Lead::CuteCharmM || lead == Lead::CuteCharmF)
{
cuteCharm = (go.nextUInt(0xffff) / 656) < 67;

// Failed cute charm seems to consume another advance
if (!cuteCharm)
{
go.next();
}
}
else if (lead != Lead::CompoundEyes)
{
go.next();
}

u32 pid;
if (staticTemplate.getShiny() == Shiny::Never)
{
}
else if (staticTemplate.getShiny() == Shiny::Always)
{
}
else
{
for (u8 i = 0; i < shinyRolls; i++)
{
pid = go.nextUInt() ^ 0x10000;
if (cuteCharm)
{
pid = Utilities5::forceGender(pid, go, lead == Lead::CuteCharmF ? 0 : 1, info->getGender());
}

if (id ^ (pid >> 31) ^ (pid & 1))
{
pid ^= 0x80000000;
}

if (Utilities::isShiny<true>(pid, tsv))
{
break;
}
}
}

u8 ability = (pid >> 16) & 1;
u8 gender = Utilities::getGender(pid, info);
u8 shiny = Utilities::getShiny<true>(pid, tsv);

u8 nature = go.nextUInt(25);
if (sync)
{
nature = toInt(lead);
}

u16 item = getItem(go, lead, info);

u16 chatot = rng.nextUInt(0x1fff);
/*for (const auto &iv : ivs)
{
State5 state(chatot, advances + initialAdvances + cnt, iv.first, pid, iv.second, ability, gender, staticTemplate.getLevel(),
nature, shiny, item, staticTemplate.getSpecie(), staticTemplate.getForm(), info);
if (filter.compareState(static_cast<const State &>(state)))
{
states.emplace_back(state);
}
}*/
}

return std::vector<State5>();
}
12 changes: 8 additions & 4 deletions Source/Core/Gen5/Generators/StaticGenerator5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
#define STATICGENERATOR5_HPP

#include <Core/Gen5/Profile5.hpp>
#include <Core/Gen5/StaticTemplate5.hpp>
#include <Core/Parents/Filters/StateFilter.hpp>
#include <Core/Parents/Generators/StaticGenerator.hpp>
#include <Core/Parents/StaticTemplate.hpp>

class State5;

/**
* @brief Static encounter generator for Gen5
*/
class StaticGenerator5 : public StaticGenerator<StaticTemplate, Profile5, StateFilter>
class StaticGenerator5 : public StaticGenerator<StaticTemplate5, Profile5, StateFilter>
{
public:
/**
Expand All @@ -41,12 +41,13 @@ class StaticGenerator5 : public StaticGenerator<StaticTemplate, Profile5, StateF
* @param delay Number of advances to offset
* @param method Encounter method
* @param lead Encounter lead
* @param luckyPower Lucky power level
* @param staticTemplate Pokemon template
* @param profile Profile Information
* @param filter State filter
*/
StaticGenerator5(u32 initialAdvances, u32 maxAdvances, u32 delay, Method method, Lead lead, const StaticTemplate &staticTemplate,
const Profile5 &profile, const StateFilter &filter);
StaticGenerator5(u32 initialAdvances, u32 maxAdvances, u32 delay, Method method, Lead lead, u8 luckyPower,
const StaticTemplate5 &staticTemplate, const Profile5 &profile, const StateFilter &filter);

/**
* @brief Generates states for the \p encounterArea
Expand All @@ -68,6 +69,9 @@ class StaticGenerator5 : public StaticGenerator<StaticTemplate, Profile5, StateF
* @return Vector of computed states
*/
std::vector<State5> generate(u64 seed, const std::vector<std::pair<u32, std::array<u8, 6>>> &ivs) const;

private:
u8 luckyPower;
};

#endif // STATICGENERATOR5_HPP
7 changes: 2 additions & 5 deletions Source/Core/Gen5/Generators/WildGenerator5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,9 @@ static u16 getItem(BWRNG &rng, Lead lead, const PersonalInfo *info)

WildGenerator5::WildGenerator5(u32 initialAdvances, u32 maxAdvances, u32 delay, Method method, Lead lead, u8 luckyPower,
const EncounterArea5 &area, const Profile5 &profile, const WildStateFilter &filter) :
WildGenerator(initialAdvances, maxAdvances, delay, method, lead, area, profile, filter), luckyPower(luckyPower)
WildGenerator(initialAdvances, maxAdvances, delay, method, lead, area, profile, filter),
luckyPower((profile.getVersion() & Game::BW) != Game::None ? 0 : luckyPower)
{
if ((profile.getVersion() & Game::BW) != Game::None)
{
luckyPower = 0;
}
}

std::vector<WildState5> WildGenerator5::generate(u64 seed, u32 initialAdvances, u32 maxAdvances) const
Expand Down
Loading

0 comments on commit 0cdef43

Please sign in to comment.