Reworked retrieving talent, new method is cleaner and harder to misunderstand.
continuous-integration/drone/push Build was killed Details

This commit is contained in:
Deukhoofd 2020-03-02 13:59:49 +01:00
parent bfc049a97c
commit 0c1580ade6
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
8 changed files with 62 additions and 27 deletions

View File

@ -28,3 +28,20 @@ export SpeciesVariant* CreatureLib_SpeciesVariant_Construct(
} }
export void CreatureLib_SpeciesVariant_Destruct(SpeciesVariant* p) { delete p; } export void CreatureLib_SpeciesVariant_Destruct(SpeciesVariant* p) { delete p; }
#define SIMPLE_GET_FUNC(type, name, returnType) \
export returnType CreatureLib_##type##_##name(const CreatureLib::Library::type* p) { return p->name(); }
export const char* CreatureLib_SpeciesVariant_GetName(SpeciesVariant* p) { return p->GetName().c_str(); }
SIMPLE_GET_FUNC(SpeciesVariant, GetHeight, float);
SIMPLE_GET_FUNC(SpeciesVariant, GetWeight, float);
SIMPLE_GET_FUNC(SpeciesVariant, GetBaseExperience, uint32_t);
SIMPLE_GET_FUNC(SpeciesVariant, GetTypeCount, size_t);
export uint8_t CreatureLib_SpeciesVariant_GetType(SpeciesVariant* p, size_t index) { return p->GetType(index); }
export uint32_t CreatureLib_SpeciesVariant_GetStatistic(SpeciesVariant* p, CreatureLib::Core::Statistic stat) {
return p->GetStatistic(stat);
}
SIMPLE_GET_FUNC(SpeciesVariant, GetTalentCount, size_t);
SIMPLE_GET_FUNC(SpeciesVariant, GetSecretTalentCount, size_t);
#undef SIMPLE_GET_FUNC

View File

@ -34,8 +34,8 @@ Creature* CreateCreature::Create() {
auto rand = Arbutils::Random(); auto rand = Arbutils::Random();
auto species = this->_library->GetSpeciesLibrary()->Get(this->_species); auto species = this->_library->GetSpeciesLibrary()->Get(this->_species);
auto variant = species->GetVariant(this->_variant); auto variant = species->GetVariant(this->_variant);
int8_t talent; TalentIndex talent;
if (this->_talent.empty()) { if (this->_talent.Empty()) {
talent = variant->GetRandomTalent(&rand); talent = variant->GetRandomTalent(&rand);
} else { } else {
talent = variant->GetTalentIndex(this->_talent); talent = variant->GetTalentIndex(this->_talent);

View File

@ -12,7 +12,7 @@ namespace CreatureLib::Battling {
uint8_t _level; uint8_t _level;
std::string _nickname = ""; std::string _nickname = "";
std::string _talent = ""; Arbutils::CaseInsensitiveConstString _talent = ""_cnc;
Library::Gender _gender = static_cast<Library::Gender>(-1); Library::Gender _gender = static_cast<Library::Gender>(-1);
uint8_t _coloring = 0; uint8_t _coloring = 0;
Arbutils::CaseInsensitiveConstString _heldItem = ""_cnc; Arbutils::CaseInsensitiveConstString _heldItem = ""_cnc;

View File

@ -9,7 +9,7 @@ using namespace CreatureLib;
Battling::Creature::Creature(const BattleLibrary* library, const Library::CreatureSpecies* species, Battling::Creature::Creature(const BattleLibrary* library, const Library::CreatureSpecies* species,
const Library::SpeciesVariant* variant, uint8_t level, uint32_t experience, uint32_t uid, const Library::SpeciesVariant* variant, uint8_t level, uint32_t experience, uint32_t uid,
Library::Gender gender, uint8_t coloring, const Library::Item* heldItem, Library::Gender gender, uint8_t coloring, const Library::Item* heldItem,
std::string nickname, int8_t talent, std::vector<LearnedAttack*> attacks) std::string nickname, const TalentIndex& talent, std::vector<LearnedAttack*> attacks)
: _library(library), _species(species), _variant(variant), _level(level), _experience(experience), : _library(library), _species(species), _variant(variant), _level(level), _experience(experience),
_uniqueIdentifier(uid), _gender(gender), _coloring(coloring), _heldItem(heldItem), _nickname(std::move(nickname)), _uniqueIdentifier(uid), _gender(gender), _coloring(coloring), _heldItem(heldItem), _nickname(std::move(nickname)),
_talentIndex(talent), _hasOverridenTalent(false), _attacks(std::move(attacks)) { _talentIndex(talent), _hasOverridenTalent(false), _attacks(std::move(attacks)) {

View File

@ -42,7 +42,7 @@ namespace CreatureLib::Battling {
bool _onBattleField = false; bool _onBattleField = false;
std::string _nickname = ""; std::string _nickname = "";
int8_t _talentIndex; TalentIndex _talentIndex;
Script* _activeTalent = nullptr; Script* _activeTalent = nullptr;
bool _hasOverridenTalent; bool _hasOverridenTalent;
@ -61,7 +61,7 @@ namespace CreatureLib::Battling {
Creature(const BattleLibrary* library, const Library::CreatureSpecies* species, Creature(const BattleLibrary* library, const Library::CreatureSpecies* species,
const Library::SpeciesVariant* variant, uint8_t level, uint32_t experience, uint32_t uid, 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, Library::Gender gender, uint8_t coloring, const Library::Item* heldItem, std::string nickname,
int8_t talent, std::vector<LearnedAttack*> attacks); const TalentIndex& talent, std::vector<LearnedAttack*> attacks);
virtual ~Creature() { virtual ~Creature() {
for (auto attack : _attacks) { for (auto attack : _attacks) {

View File

@ -12,28 +12,22 @@ uint32_t CreatureLib::Library::SpeciesVariant::GetStatistic(CreatureLib::Core::S
return _baseStatistics.GetStat(stat); return _baseStatistics.GetStat(stat);
} }
const ConstString& CreatureLib::Library::SpeciesVariant::GetTalent(int32_t index) const { const TalentIndex CreatureLib::Library::SpeciesVariant::GetTalentIndex(const ConstString& talent) const {
if (index < 0) { for (size_t i = 0; i < _talents.size(); i++) {
index = -index - 1; if (_talents.at(i) == talent) {
return _secretTalents[index]; return TalentIndex(false, i);
} }
return _talents[index];
} }
for (size_t i = 0; i < _secretTalents.size(); i++) {
int8_t CreatureLib::Library::SpeciesVariant::GetTalentIndex(const std::string& talent) const { if (_secretTalents.at(i) == talent) {
auto i = std::find(_talents.begin(), _talents.end(), talent); return TalentIndex(true, i);
if (i != _talents.end()) {
return std::distance(_talents.begin(), i);
} }
i = std::find(_secretTalents.begin(), _secretTalents.end(), talent);
if (i != _secretTalents.end()) {
return std::distance(_secretTalents.begin(), i);
} }
throw CreatureException("The given talent is not a valid talent for this creature."); throw CreatureException("The given talent is not a valid talent for this creature.");
} }
int8_t CreatureLib::Library::SpeciesVariant::GetRandomTalent(Arbutils::Random* rand) const { TalentIndex CreatureLib::Library::SpeciesVariant::GetRandomTalent(Arbutils::Random* rand) const {
return rand->Get(_talents.size()); return TalentIndex(false, rand->Get(_talents.size()));
} }
const CreatureLib::Library::LearnableAttacks* CreatureLib::Library::SpeciesVariant::GetLearnableAttacks() const { const CreatureLib::Library::LearnableAttacks* CreatureLib::Library::SpeciesVariant::GetLearnableAttacks() const {

View File

@ -7,6 +7,7 @@
#include "../../Core/StatisticSet.hpp" #include "../../Core/StatisticSet.hpp"
#include "CreatureMoves.hpp" #include "CreatureMoves.hpp"
#include "LearnableAttacks.hpp" #include "LearnableAttacks.hpp"
#include "TalentIndex.hpp"
using ConstString = Arbutils::CaseInsensitiveConstString; using ConstString = Arbutils::CaseInsensitiveConstString;
namespace CreatureLib::Library { namespace CreatureLib::Library {
@ -44,10 +45,17 @@ namespace CreatureLib::Library {
[[nodiscard]] uint8_t GetType(size_t index) const; [[nodiscard]] uint8_t GetType(size_t index) const;
[[nodiscard]] const std::vector<uint8_t>& GetTypes() const; [[nodiscard]] const std::vector<uint8_t>& GetTypes() const;
[[nodiscard]] uint32_t GetStatistic(Core::Statistic stat) const; [[nodiscard]] uint32_t GetStatistic(Core::Statistic stat) const;
[[nodiscard]] const ConstString& GetTalent(int32_t index) const; [[nodiscard]] const size_t GetTalentCount() const { return _talents.size(); }
[[nodiscard]] const size_t GetSecretTalentCount() const { return _secretTalents.size(); }
[[nodiscard]] const ConstString& GetTalent(const TalentIndex& index) const {
if (index.IsSecret())
return _secretTalents.at(index.GetIndex());
return _talents.at(index.GetIndex());
}
[[nodiscard]] const TalentIndex GetTalentIndex(const ConstString& talent) const;
[[nodiscard]] const LearnableAttacks* GetLearnableAttacks() const; [[nodiscard]] const LearnableAttacks* GetLearnableAttacks() const;
[[nodiscard]] int8_t GetTalentIndex(const std::string& talent) const; [[nodiscard]] TalentIndex GetRandomTalent(Arbutils::Random* rand) const;
[[nodiscard]] int8_t GetRandomTalent(Arbutils::Random* rand) const;
[[nodiscard]] inline const std::vector<ConstString>& GetTalents() const { return _talents; } [[nodiscard]] inline const std::vector<ConstString>& GetTalents() const { return _talents; }
[[nodiscard]] inline const std::vector<ConstString>& GetSecretTalents() const { return _secretTalents; } [[nodiscard]] inline const std::vector<ConstString>& GetSecretTalents() const { return _secretTalents; }
}; };

View File

@ -0,0 +1,16 @@
#ifndef CREATURELIB_TALENTINDEX_HPP
#define CREATURELIB_TALENTINDEX_HPP
#include <cstdint>
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; }
};
#endif // CREATURELIB_TALENTINDEX_HPP