Work on evolution helpers
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2021-06-27 17:11:37 +02:00
parent 4633c6beef
commit 3d3bff3772
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
10 changed files with 85 additions and 9 deletions

View File

@ -5,7 +5,7 @@ using namespace PkmnLib::Battling;
export uint8_t PkmnLib_BattleLibrary_Construct(BattleLibrary*& out, PkmnLib::Library::PokemonLibrary* staticLib, export uint8_t PkmnLib_BattleLibrary_Construct(BattleLibrary*& out, PkmnLib::Library::PokemonLibrary* staticLib,
StatCalculator* statCalculator, DamageLibrary* damageLibrary, StatCalculator* statCalculator, DamageLibrary* damageLibrary,
ExperienceLibrary* experienceLibrary, ExperienceLibrary* experienceLibrary,
CreatureLib::Battling::ScriptResolver* scriptResolver, PkmnLib::Battling::ScriptResolver* scriptResolver,
MiscLibrary* miscLibrary) { MiscLibrary* miscLibrary) {
Try(out = new BattleLibrary(staticLib, statCalculator, damageLibrary, experienceLibrary, scriptResolver, Try(out = new BattleLibrary(staticLib, statCalculator, damageLibrary, experienceLibrary, scriptResolver,
miscLibrary)); miscLibrary));

View File

@ -2,6 +2,8 @@
#include "../Core.hpp" #include "../Core.hpp"
using namespace PkmnLib::Battling; using namespace PkmnLib::Battling;
export MiscLibrary* PkmnLib_MiscLibrary_Construct() { return new MiscLibrary(); } export MiscLibrary* PkmnLib_MiscLibrary_Construct(PkmnLib::Library::TimeOfDay(*getTime)()) {
return new MiscLibrary(getTime);
}
export void PkmnLib_MiscLibrary_Destruct(MiscLibrary* p) { delete p; } export void PkmnLib_MiscLibrary_Destruct(MiscLibrary* p) { delete p; }

View File

@ -6,14 +6,14 @@
#include "DamageLibrary.hpp" #include "DamageLibrary.hpp"
#include "ExperienceLibrary.hpp" #include "ExperienceLibrary.hpp"
#include "MiscLibrary.hpp" #include "MiscLibrary.hpp"
#include "ScriptResolver.hpp"
#include "StatCalculator.hpp" #include "StatCalculator.hpp"
namespace PkmnLib::Battling { namespace PkmnLib::Battling {
class BattleLibrary final : public CreatureLib::Battling::BattleLibrary { class BattleLibrary final : public CreatureLib::Battling::BattleLibrary {
public: public:
BattleLibrary(Library::PokemonLibrary* staticLib, StatCalculator* statCalculator, DamageLibrary* damageLibrary, BattleLibrary(Library::PokemonLibrary* staticLib, StatCalculator* statCalculator, DamageLibrary* damageLibrary,
PkmnLib::Battling::ExperienceLibrary* experienceLibrary, PkmnLib::Battling::ExperienceLibrary* experienceLibrary, ScriptResolver* scriptResolver,
CreatureLib::Battling::ScriptResolver* scriptResolver,
PkmnLib::Battling::MiscLibrary* miscLibrary) PkmnLib::Battling::MiscLibrary* miscLibrary)
: CreatureLib::Battling::BattleLibrary(staticLib, statCalculator, damageLibrary, experienceLibrary, : CreatureLib::Battling::BattleLibrary(staticLib, statCalculator, damageLibrary, experienceLibrary,
scriptResolver, miscLibrary) {} scriptResolver, miscLibrary) {}
@ -43,7 +43,7 @@ namespace PkmnLib::Battling {
return GetStaticLib()->GetNatureLibrary(); return GetStaticLib()->GetNatureLibrary();
} }
static CreatureLib::Battling::ScriptResolver* CreateScriptResolver(); static PkmnLib::Battling::ScriptResolver* CreateScriptResolver();
}; };
} }
#endif // PKMNLIB_BATTLELIBRARY_HPP #endif // PKMNLIB_BATTLELIBRARY_HPP

View File

@ -1,7 +1,10 @@
#include "MiscLibrary.hpp" #include "MiscLibrary.hpp"
#include <CreatureLib/Battling/Models/Battle.hpp> #include <CreatureLib/Battling/Models/Battle.hpp>
#include <CreatureLib/Battling/Models/LearnedAttack.hpp>
#include <CreatureLib/Battling/TurnChoices/AttackTurnChoice.hpp> #include <CreatureLib/Battling/TurnChoices/AttackTurnChoice.hpp>
#include "../PkmnScriptHook.hpp" #include "../PkmnScriptHook.hpp"
#include "../Pokemon/LearnedMove.hpp"
#include "../Pokemon/Pokemon.hpp"
bool PkmnLib::Battling::MiscLibrary::IsCritical(CreatureLib::Battling::ExecutingAttack* attack, bool PkmnLib::Battling::MiscLibrary::IsCritical(CreatureLib::Battling::ExecutingAttack* attack,
CreatureLib::Battling::Creature* target, uint8_t hit) const { CreatureLib::Battling::Creature* target, uint8_t hit) const {
@ -26,3 +29,49 @@ PkmnLib::Battling::MiscLibrary::ReplacementAttack(CreatureLib::Battling::Creatur
CreatureLib::Battling::CreatureIndex target) const { CreatureLib::Battling::CreatureIndex target) const {
return new CreatureLib::Battling::AttackTurnChoice(user, GetReplacementAttack(), target); return new CreatureLib::Battling::AttackTurnChoice(user, GetReplacementAttack(), target);
} }
using TimeOfDay = PkmnLib::Library::TimeOfDay;
bool PkmnLib::Battling::MiscLibrary::CanEvolveFromLevelUp(
const ArbUt::BorrowedPtr<const PkmnLib::Library::EvolutionData>& evolution,
const ArbUt::BorrowedPtr<const Pokemon>& pokemon) {
auto time = GetTime();
switch (evolution->GetMethod()) {
case Library::EvolutionMethod::Level: return pokemon->GetLevel() >= evolution->GetData(0)->AsInt();
case Library::EvolutionMethod::HighFriendship:
return pokemon->GetFriendship() >= evolution->GetData(0)->AsInt();
case Library::EvolutionMethod::HighFriendshipTime:
return pokemon->GetFriendship() >= evolution->GetData(0)->AsInt() &&
time >= (TimeOfDay)evolution->GetData(1)->AsInt() &&
time <= (TimeOfDay)evolution->GetData(2)->AsInt();
case Library::EvolutionMethod::KnownMove: {
auto v = evolution->GetData(0)->AsString();
return std::any_of(pokemon->GetMoves().begin(), pokemon->GetMoves().end(), [v](const auto& move) {
return move.HasValue() && move.GetValue()->GetMoveData()->GetName() == v;
});
}
case Library::EvolutionMethod::LocationBased:
// TODO: Implement this
return false;
case Library::EvolutionMethod::TimeBased:
return time >= (TimeOfDay)evolution->GetData(0)->AsInt() &&
time <= (TimeOfDay)evolution->GetData(1)->AsInt();
case Library::EvolutionMethod::HoldsItem: return pokemon->HasHeldItem(evolution->GetData(0)->AsString());
case Library::EvolutionMethod::HoldsItemTime:
return pokemon->HasHeldItem(evolution->GetData(0)->AsString()) &&
time >= (TimeOfDay)evolution->GetData(0)->AsInt() &&
time <= (TimeOfDay)evolution->GetData(1)->AsInt();
case Library::EvolutionMethod::IsGenderAndLevel:
return pokemon->GetLevel() >= evolution->GetData(1)->AsInt() &&
pokemon->GetGender() == (CreatureLib::Library::Gender)evolution->GetData(0)->AsInt();
case Library::EvolutionMethod::Custom:
// TODO
case Library::EvolutionMethod::EvolutionItemUse:
case Library::EvolutionMethod::EvolutionItemUseWithGender:
case Library::EvolutionMethod::Trade:
case Library::EvolutionMethod::TradeWithHeldItem:
case Library::EvolutionMethod::TradeWithSpecificPokemon: return false;
}
__builtin_unreachable();
}

View File

@ -2,10 +2,13 @@
#define PKMNLIB_MISCLIBRARY_HPP #define PKMNLIB_MISCLIBRARY_HPP
#include <CreatureLib/Battling/Library/MiscLibrary.hpp> #include <CreatureLib/Battling/Library/MiscLibrary.hpp>
#include "../../Library/Evolutions/EvolutionData.hpp"
#include "../../Library/Moves/MoveData.hpp" #include "../../Library/Moves/MoveData.hpp"
#include "../Pokemon/LearnedMove.hpp" #include "../Pokemon/LearnedMove.hpp"
namespace PkmnLib::Battling { namespace PkmnLib::Battling {
class Pokemon;
class MiscLibrary final : public CreatureLib::Battling::MiscLibrary { class MiscLibrary final : public CreatureLib::Battling::MiscLibrary {
std::unique_ptr<const PkmnLib::Library::MoveData> _struggleData = std::make_unique<PkmnLib::Library::MoveData>( std::unique_ptr<const PkmnLib::Library::MoveData> _struggleData = std::make_unique<PkmnLib::Library::MoveData>(
"struggle"_cnc, 0, PkmnLib::Library::MoveCategory::Physical, 50, 255, 10, "struggle"_cnc, 0, PkmnLib::Library::MoveCategory::Physical, 50, 255, 10,
@ -21,7 +24,11 @@ namespace PkmnLib::Battling {
return _struggle; return _struggle;
} }
std::function<PkmnLib::Library::TimeOfDay()> _getTime;
public: public:
MiscLibrary(std::function<PkmnLib::Library::TimeOfDay()> getTime) : _getTime(getTime) {}
~MiscLibrary() override = default; ~MiscLibrary() override = default;
bool IsCritical(CreatureLib::Battling::ExecutingAttack* attack, CreatureLib::Battling::Creature* target, bool IsCritical(CreatureLib::Battling::ExecutingAttack* attack, CreatureLib::Battling::Creature* target,
uint8_t hit) const override; uint8_t hit) const override;
@ -29,6 +36,11 @@ namespace PkmnLib::Battling {
CreatureLib::Battling::BaseTurnChoice* CreatureLib::Battling::BaseTurnChoice*
ReplacementAttack(CreatureLib::Battling::Creature* user, ReplacementAttack(CreatureLib::Battling::Creature* user,
CreatureLib::Battling::CreatureIndex target) const override; CreatureLib::Battling::CreatureIndex target) const override;
bool CanEvolveFromLevelUp(const ArbUt::BorrowedPtr<const PkmnLib::Library::EvolutionData>& evolution,
const ArbUt::BorrowedPtr<const Pokemon>& pokemon);
inline PkmnLib::Library::TimeOfDay GetTime() const noexcept { return _getTime(); }
}; };
} }

View File

@ -0,0 +1,10 @@
#ifndef PKMNLIB_SCRIPTRESOLVER_HPP
#define PKMNLIB_SCRIPTRESOLVER_HPP
namespace PkmnLib::Battling {
class ScriptResolver : public CreatureLib::Battling::ScriptResolver {
public:
};
}
#endif // PKMNLIB_SCRIPTRESOLVER_HPP

View File

@ -27,7 +27,7 @@
#include "TypeRegistry/Library/RegisterStaticLibraryTypes.hpp" #include "TypeRegistry/Library/RegisterStaticLibraryTypes.hpp"
#include "TypeRegistry/Library/RegisterTypeLibrary.hpp" #include "TypeRegistry/Library/RegisterTypeLibrary.hpp"
CreatureLib::Battling::ScriptResolver* PkmnLib::Battling::BattleLibrary::CreateScriptResolver() { PkmnLib::Battling::ScriptResolver* PkmnLib::Battling::BattleLibrary::CreateScriptResolver() {
return new AngelScriptResolver(); return new AngelScriptResolver();
} }

View File

@ -12,7 +12,7 @@
#include "AngelScriptScript.hpp" #include "AngelScriptScript.hpp"
#include "AngelScriptTypeInfo.hpp" #include "AngelScriptTypeInfo.hpp"
class AngelScriptResolver final : public CreatureLib::Battling::ScriptResolver { class AngelScriptResolver final : public PkmnLib::Battling::ScriptResolver {
private: private:
asIScriptEngine* _engine = nullptr; asIScriptEngine* _engine = nullptr;
asIScriptModule* _mainModule = nullptr; asIScriptModule* _mainModule = nullptr;

View File

@ -373,13 +373,14 @@ TEST_CASE("Handle script exceptions.") {
throw ArbUt::Exception("Didn't throw"); throw ArbUt::Exception("Didn't throw");
} }
static PkmnLib::Library::TimeOfDay GetTime() { return PkmnLib::Library::TimeOfDay::Morning; }
TEST_CASE("Add Volatile with return script function") { TEST_CASE("Add Volatile with return script function") {
auto statCalc = new PkmnLib::Battling::StatCalculator(); auto statCalc = new PkmnLib::Battling::StatCalculator();
auto resolver = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); auto resolver = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
auto mainLib = new PkmnLib::Battling::BattleLibrary( auto mainLib = new PkmnLib::Battling::BattleLibrary(
TestLibrary::BuildStaticLibrary(), statCalc, new PkmnLib::Battling::DamageLibrary(), TestLibrary::BuildStaticLibrary(), statCalc, new PkmnLib::Battling::DamageLibrary(),
new PkmnLib::Battling::ExperienceLibrary(), resolver, new PkmnLib::Battling::MiscLibrary()); new PkmnLib::Battling::ExperienceLibrary(), resolver, new PkmnLib::Battling::MiscLibrary(GetTime));
resolver->Initialize(mainLib); resolver->Initialize(mainLib);
for (auto kv : _scripts) { for (auto kv : _scripts) {
resolver->CreateScript(kv.first, kv.second); resolver->CreateScript(kv.first, kv.second);

View File

@ -20,12 +20,14 @@ public:
return _library; return _library;
} }
static PkmnLib::Library::TimeOfDay GetTime() { return PkmnLib::Library::TimeOfDay::Morning; }
static PkmnLib::Battling::BattleLibrary* BuildLibrary() { static PkmnLib::Battling::BattleLibrary* BuildLibrary() {
auto statCalc = new PkmnLib::Battling::StatCalculator(); auto statCalc = new PkmnLib::Battling::StatCalculator();
auto scriptResolver = PkmnLib::Battling::BattleLibrary::CreateScriptResolver(); auto scriptResolver = PkmnLib::Battling::BattleLibrary::CreateScriptResolver();
auto lib = new PkmnLib::Battling::BattleLibrary( auto lib = new PkmnLib::Battling::BattleLibrary(
BuildStaticLibrary(), statCalc, new PkmnLib::Battling::DamageLibrary(), BuildStaticLibrary(), statCalc, new PkmnLib::Battling::DamageLibrary(),
new PkmnLib::Battling::ExperienceLibrary(), scriptResolver, new PkmnLib::Battling::MiscLibrary()); new PkmnLib::Battling::ExperienceLibrary(), scriptResolver, new PkmnLib::Battling::MiscLibrary(GetTime));
scriptResolver->Initialize(lib); scriptResolver->Initialize(lib);
return lib; return lib;
} }