From e990c13109e00056c954ee49e3d3d76ffc763fa1 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Thu, 5 Mar 2020 11:25:41 +0100 Subject: [PATCH] Added Creature C Interface, misc fixes and changes for Creature. --- CInterface/Battling/Creature.cpp | 111 ++++++++++++++++++++ src/Battling/Models/CreateCreature.cpp | 5 +- src/Battling/Models/Creature.cpp | 18 +++- src/Battling/Models/Creature.hpp | 20 ++-- src/Library/CreatureData/SpeciesVariant.cpp | 5 +- src/Library/CreatureData/TalentIndex.hpp | 20 ++-- 6 files changed, 153 insertions(+), 26 deletions(-) create mode 100644 CInterface/Battling/Creature.cpp diff --git a/CInterface/Battling/Creature.cpp b/CInterface/Battling/Creature.cpp new file mode 100644 index 0000000..ea59590 --- /dev/null +++ b/CInterface/Battling/Creature.cpp @@ -0,0 +1,111 @@ +#include "../../src/Battling/Models/Creature.hpp" +#define export extern "C" +using namespace CreatureLib::Battling; +using ConstString = Arbutils::CaseInsensitiveConstString; + +export Creature* CreatureLib_Creature_Construct(const BattleLibrary* library, + const CreatureLib::Library::CreatureSpecies* species, + const CreatureLib::Library::SpeciesVariant* variant, uint8_t level, + uint32_t experience, uint32_t uid, CreatureLib::Library::Gender gender, + uint8_t coloring, const CreatureLib::Library::Item* heldItem, + std::string nickname, bool secretTalent, const uint8_t talent, + LearnedAttack* attacks[], size_t attacksNum) { + auto attacksVec = std::vector(attacks, attacks + attacksNum); + return new Creature(library, species, variant, level, experience, uid, gender, coloring, heldItem, nickname, + CreatureLib::Library::TalentIndex(secretTalent, talent), attacksVec); +}; + +export void CreatureLib_Creature_Destruct(const Creature* p) { delete p; } + +#define SIMPLE_GET_FUNC(type, name, returnType) \ + export returnType CreatureLib_##type##_##name(const type* p) { return p->name(); } + +SIMPLE_GET_FUNC(Creature, GetSpecies, const CreatureLib::Library::CreatureSpecies*); +SIMPLE_GET_FUNC(Creature, GetVariant, const CreatureLib::Library::SpeciesVariant*); +SIMPLE_GET_FUNC(Creature, GetLevel, uint8_t); +SIMPLE_GET_FUNC(Creature, GetExperience, uint32_t); +SIMPLE_GET_FUNC(Creature, GetGender, CreatureLib::Library::Gender); +SIMPLE_GET_FUNC(Creature, GetColoring, uint8_t); + +export bool CreatureLib_Creature_HasHeldItem(const Creature* p, const char* name) { + return p->HasHeldItem(ConstString(name)); +} +export bool CreatureLib_Creature_HasHeldItemWithHash(const Creature* p, uint32_t hash) { return p->HasHeldItem(hash); } +SIMPLE_GET_FUNC(Creature, GetHeldItem, const CreatureLib::Library::Item*); +export void CreatureLib_Creature_SetHeldItem(Creature* p, const char* name) { + return p->SetHeldItem(ConstString(name)); +} +export void CreatureLib_Creature_SetHeldItemWithHash(Creature* p, uint32_t hash) { return p->SetHeldItem(hash); } +export void CreatureLib_Creature_SetHeldItemFromItem(Creature* p, const CreatureLib::Library::Item* item) { + return p->SetHeldItem(item); +} +SIMPLE_GET_FUNC(Creature, GetCurrentHealth, uint32_t); +SIMPLE_GET_FUNC(Creature, GetBattle, Battle*); +SIMPLE_GET_FUNC(Creature, GetBattleSide, BattleSide*); +SIMPLE_GET_FUNC(Creature, IsOnBattleField, bool); +export const char* CreatureLib_Creature_GetNickname(Creature* p) { return p->GetNickname().c_str(); } +export size_t CreatureLib_Creature_GetTypesCount(Creature* p) { return p->GetTypes().size(); } +export const uint8_t* CreatureLib_Creature_GetTypes(Creature* p) { return p->GetTypes().data(); } +export bool CreatureLib_Creature_HasType(Creature* p, uint8_t type) { return p->HasType(type); } +SIMPLE_GET_FUNC(Creature, GetMaxHealth, uint32_t); +export void CreatureLib_Creature_ChangeLevelBy(Creature* p, int8_t level) { + return p->ChangeLevelBy(level); +} +export void CreatureLib_Creature_Damage(Creature* p, uint32_t damage, DamageSource source){ + p->Damage(damage, source); +} +export void CreatureLib_Creature_Heal(Creature* p, uint32_t health){ + p->Heal(health); +} +export void CreatureLib_Creature_OverrideActiveTalent(Creature* p, const char* talent){ + p->OverrideActiveTalent(ConstString(talent)); +} +export void CreatureLib_Creature_AddExperience(Creature* p, uint32_t experience) { + return p->AddExperience(experience); +} +export void CreatureLib_Creature_ClearVolatileScripts(Creature* p) { + return p->ClearVolatileScripts(); +} +export void CreatureLib_Creature_AddVolatileScriptByName(Creature* p, const char* scriptName) { + return p->AddVolatileScript(ConstString(scriptName)); +} +export void CreatureLib_Creature_AddVolatileScript(Creature* p, Script* script) { + return p->AddVolatileScript(script); +} +export void CreatureLib_Creature_RemoveVolatileScriptByName(Creature* p, const char* scriptName) { + return p->RemoveVolatileScript(ConstString(scriptName)); +} +export void CreatureLib_Creature_RemoveVolatileScript(Creature* p, Script* script) { + return p->RemoveVolatileScript(script); +} +export bool CreatureLib_Creature_HasVolatileScript(Creature* p, const char* scriptName) { + return p->HasVolatileScript(ConstString(scriptName)); +} +export size_t CreatureLib_Creature_GetAttacksCount(Creature* p) { return p->GetAttacks().size(); } +export LearnedAttack** CreatureLib_Creature_GetAttacks(Creature* p) { return p->GetAttacks().data(); } +SIMPLE_GET_FUNC(Creature, GetDisplaySpecies, const CreatureLib::Library::CreatureSpecies*); +SIMPLE_GET_FUNC(Creature, GetDisplayVariant, const CreatureLib::Library::SpeciesVariant*); +export void CreatureLib_Creature_SetDisplaySpecies(Creature* p, const CreatureLib::Library::CreatureSpecies* species) { + return p->SetDisplaySpecies(species); +} +export void CreatureLib_Creature_SetDisplayVariant(Creature* p, const CreatureLib::Library::SpeciesVariant* variant) { + return p->SetDisplayVariant(variant); +} +export void CreatureLib_Creature_ChangeStatBoost(Creature* p, CreatureLib::Core::Statistic stat, int8_t diffAmount){ + p->ChangeStatBoost(stat, diffAmount); +} +export uint32_t CreatureLib_Creature_GetFlatStat(Creature* p, CreatureLib::Core::Statistic stat){ + return p->GetFlatStat(stat); +} +export uint32_t CreatureLib_Creature_GetBoostedStat(Creature* p, CreatureLib::Core::Statistic stat){ + return p->GetBoostedStat(stat); +} +export uint32_t CreatureLib_Creature_GetBaseStat(Creature* p, CreatureLib::Core::Statistic stat){ + return p->GetBaseStat(stat); +} +export int8_t CreatureLib_Creature_GetStatBoost(Creature* p, CreatureLib::Core::Statistic stat){ + return p->GetStatBoost(stat); +} + + +#undef SIMPLE_GET_FUNC \ No newline at end of file diff --git a/src/Battling/Models/CreateCreature.cpp b/src/Battling/Models/CreateCreature.cpp index 2f08a9b..e264071 100644 --- a/src/Battling/Models/CreateCreature.cpp +++ b/src/Battling/Models/CreateCreature.cpp @@ -34,7 +34,7 @@ Creature* CreateCreature::Create() { auto rand = Arbutils::Random(); auto species = this->_library->GetSpeciesLibrary()->Get(this->_species); auto variant = species->GetVariant(this->_variant); - TalentIndex talent; + Library::TalentIndex talent; if (this->_talent.Empty()) { talent = variant->GetRandomTalent(&rand); } else { @@ -54,8 +54,7 @@ Creature* CreateCreature::Create() { throw CreatureException("Invalid held item."); } } - // FIXME: implement experience - auto experience = 0; + auto experience = _library->GetGrowthRateLibrary()->CalculateExperience(species->GetGrowthRate(), _level); auto attacks = std::vector(_attacks.size()); for (size_t i = 0; i < attacks.size(); i++) { diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index ed229c7..728a5f2 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -9,7 +9,8 @@ using namespace CreatureLib; Battling::Creature::Creature(const BattleLibrary* library, const Library::CreatureSpecies* species, const Library::SpeciesVariant* variant, uint8_t level, uint32_t experience, uint32_t uid, Library::Gender gender, uint8_t coloring, const Library::Item* heldItem, - std::string nickname, const TalentIndex& talent, std::vector attacks) + std::string nickname, const Library::TalentIndex& talent, + std::vector attacks) : _library(library), _species(species), _variant(variant), _level(level), _experience(experience), _uniqueIdentifier(uid), _gender(gender), _coloring(coloring), _heldItem(heldItem), _nickname(std::move(nickname)), _talentIndex(talent), _hasOverridenTalent(false), _attacks(std::move(attacks)) { @@ -20,8 +21,9 @@ Battling::Creature::Creature(const BattleLibrary* library, const Library::Creatu } } -void Battling::Creature::ChangeLevel(int8_t amount) { +void Battling::Creature::ChangeLevelBy(int8_t amount) { this->_level += amount; + _experience = _library->GetGrowthRateLibrary()->CalculateExperience(_species->GetGrowthRate(), _level); RecalculateFlatStats(); } @@ -176,13 +178,21 @@ const Library::SpeciesVariant* Battling::Creature::GetDisplayVariant() const { variant = _variant; return variant; } -void Battling::Creature::SetHeldItem(const Arbutils::CaseInsensitiveConstString& itemName) { +void Battling::Creature::SetHeldItem(const ConstString& itemName) { const Library::Item* item; if (!_library->GetItemLibrary()->TryGet(itemName, item)) { throw CreatureException("Item not found."); } _heldItem = item; } +void Battling::Creature::SetHeldItem(uint32_t itemNameHash) { + const Library::Item* item; + if (!_library->GetItemLibrary()->TryGet(itemNameHash, item)) { + throw CreatureException("Item not found."); + } + _heldItem = item; +} + void Battling::Creature::AddVolatileScript(const ConstString& name) { auto script = _volatile.Get(name); if (script != nullptr) { @@ -196,4 +206,4 @@ void Battling::Creature::AddVolatileScript(const ConstString& name) { void Battling::Creature::AddVolatileScript(Script* script) { _volatile.Add(script); } void Battling::Creature::RemoveVolatileScript(const ConstString& name) { _volatile.Remove(name); } void Battling::Creature::RemoveVolatileScript(Battling::Script* script) { _volatile.Remove(script->GetName()); } -void Battling::Creature::HasVolatileScript(const ConstString& name) const { _volatile.Has(name); } +bool Battling::Creature::HasVolatileScript(const ConstString& name) const { return _volatile.Has(name); } diff --git a/src/Battling/Models/Creature.hpp b/src/Battling/Models/Creature.hpp index 5cbd64a..c803118 100644 --- a/src/Battling/Models/Creature.hpp +++ b/src/Battling/Models/Creature.hpp @@ -42,7 +42,7 @@ namespace CreatureLib::Battling { bool _onBattleField = false; std::string _nickname = ""; - TalentIndex _talentIndex; + CreatureLib::Library::TalentIndex _talentIndex; Script* _activeTalent = nullptr; bool _hasOverridenTalent; @@ -61,7 +61,7 @@ namespace CreatureLib::Battling { Creature(const BattleLibrary* library, const Library::CreatureSpecies* species, const Library::SpeciesVariant* variant, uint8_t level, uint32_t experience, uint32_t uid, Library::Gender gender, uint8_t coloring, const Library::Item* heldItem, std::string nickname, - const TalentIndex& talent, std::vector attacks); + const Library::TalentIndex& talent, std::vector attacks); virtual ~Creature() { for (auto attack : _attacks) { @@ -82,11 +82,15 @@ namespace CreatureLib::Battling { inline uint32_t GetExperience() const { return _experience; } inline Library::Gender GetGender() const { return _gender; } inline uint8_t GetColoring() const { return _coloring; } - inline const bool HasHeldItem(const std::string& name) const { + inline bool HasHeldItem(const ConstString& name) const { return _heldItem != nullptr && _heldItem->GetName() == name; } + inline bool HasHeldItem(uint32_t nameHash) const { + return _heldItem != nullptr && _heldItem->GetName() == nameHash; + } inline const Library::Item* GetHeldItem() const { return _heldItem; } - void SetHeldItem(const Arbutils::CaseInsensitiveConstString& itemName); + void SetHeldItem(const ConstString& itemName); + void SetHeldItem(uint32_t itemNameHash); inline void SetHeldItem(const Library::Item* item) { _heldItem = item; }; inline uint32_t GetCurrentHealth() const { return _currentHealth; } @@ -105,7 +109,7 @@ namespace CreatureLib::Battling { [[nodiscard]] bool HasType(uint8_t type) const; uint32_t GetMaxHealth() const { return _boostedStats.GetHealth(); } - void ChangeLevel(int8_t amount); + void ChangeLevelBy(int8_t amount); void Damage(uint32_t damage, DamageSource source); void Heal(uint32_t amount); void OverrideActiveTalent(const ConstString& talent); @@ -120,15 +124,15 @@ namespace CreatureLib::Battling { void AddVolatileScript(Script* script); void RemoveVolatileScript(const ConstString& name); void RemoveVolatileScript(Script* script); - void HasVolatileScript(const ConstString& name) const; + bool HasVolatileScript(const ConstString& name) const; std::vector& GetAttacks() { return _attacks; } const Library::CreatureSpecies* GetDisplaySpecies() const; const Library::SpeciesVariant* GetDisplayVariant() const; - const void SetDisplaySpecies(Library::CreatureSpecies* species) { _displaySpecies = species; } - const void SetDisplayVariant(Library::SpeciesVariant* variant) { _displayVariant = variant; }; + const void SetDisplaySpecies(const Library::CreatureSpecies* species) { _displaySpecies = species; } + const void SetDisplayVariant(const Library::SpeciesVariant* variant) { _displayVariant = variant; }; // region Stat APIs diff --git a/src/Library/CreatureData/SpeciesVariant.cpp b/src/Library/CreatureData/SpeciesVariant.cpp index 3ac26ca..7174572 100644 --- a/src/Library/CreatureData/SpeciesVariant.cpp +++ b/src/Library/CreatureData/SpeciesVariant.cpp @@ -12,7 +12,8 @@ uint32_t CreatureLib::Library::SpeciesVariant::GetStatistic(CreatureLib::Core::S return _baseStatistics.GetStat(stat); } -const TalentIndex CreatureLib::Library::SpeciesVariant::GetTalentIndex(const ConstString& talent) const { +const CreatureLib::Library::TalentIndex +CreatureLib::Library::SpeciesVariant::GetTalentIndex(const ConstString& talent) const { for (size_t i = 0; i < _talents.size(); i++) { if (_talents.at(i) == talent) { return TalentIndex(false, i); @@ -26,7 +27,7 @@ const TalentIndex CreatureLib::Library::SpeciesVariant::GetTalentIndex(const Con throw CreatureException("The given talent is not a valid talent for this creature."); } -TalentIndex CreatureLib::Library::SpeciesVariant::GetRandomTalent(Arbutils::Random* rand) const { +CreatureLib::Library::TalentIndex CreatureLib::Library::SpeciesVariant::GetRandomTalent(Arbutils::Random* rand) const { return TalentIndex(false, rand->Get(_talents.size())); } diff --git a/src/Library/CreatureData/TalentIndex.hpp b/src/Library/CreatureData/TalentIndex.hpp index cd4cd3b..683f259 100644 --- a/src/Library/CreatureData/TalentIndex.hpp +++ b/src/Library/CreatureData/TalentIndex.hpp @@ -2,15 +2,17 @@ #define CREATURELIB_TALENTINDEX_HPP #include -class TalentIndex { - bool _secret; - uint8_t _index; +namespace CreatureLib::Library { + class TalentIndex { + bool _secret; + uint8_t _index; -public: - TalentIndex() : _secret(false), _index(0){}; - TalentIndex(bool secret, uint8_t index) : _secret(secret), _index(index) {} - constexpr inline bool IsSecret() const { return _secret; } - constexpr inline bool GetIndex() const { return _index; } -}; + public: + TalentIndex() : _secret(false), _index(0){}; + TalentIndex(bool secret, uint8_t index) : _secret(secret), _index(index) {} + constexpr inline bool IsSecret() const { return _secret; } + constexpr inline bool GetIndex() const { return _index; } + }; +} #endif // CREATURELIB_TALENTINDEX_HPP