Implements ConstString in several core places in the library, improving performance.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-02-27 18:23:23 +01:00
parent 1d3a8da99e
commit 412e0a4d63
17 changed files with 161 additions and 148 deletions

View File

@@ -5,8 +5,8 @@
using namespace CreatureLib::Battling;
CreateCreature* CreateCreature::WithVariant(std::string variant) {
this->_variant = std::move(variant);
CreateCreature* CreateCreature::WithVariant(const Arbutils::CaseInsensitiveConstString& variant) {
this->_variant = variant;
return this;
}
@@ -20,18 +20,19 @@ CreateCreature* CreateCreature::WithGender(Library::Gender gender) {
return this;
}
CreateCreature* CreateCreature::WithAttack(const std::string& attackName, AttackLearnMethod learnMethod) {
CreateCreature* CreateCreature::WithAttack(const Arbutils::CaseInsensitiveConstString& attackName,
AttackLearnMethod learnMethod) {
if (_attacks.size() >= _library->GetSettings()->GetMaximalMoves())
throw CreatureException("You have already set the maximum amount of allowed moves.");
auto attackData = _library->GetAttackLibrary()->Get(attackName.c_str());
auto attackData = _library->GetAttackLibrary()->Get(attackName);
_attacks.emplace_back(attackData, learnMethod);
return this;
}
Creature* CreateCreature::Create() {
auto rand = Arbutils::Random();
auto species = this->_library->GetSpeciesLibrary()->Get(this->_species.c_str());
auto species = this->_library->GetSpeciesLibrary()->Get(this->_species);
auto variant = species->GetVariant(this->_variant);
int8_t talent;
if (this->_talent.empty()) {
@@ -48,8 +49,8 @@ Creature* CreateCreature::Create() {
gender = species->GetRandomGender(rand);
}
const Library::Item* heldItem = nullptr;
if (!this->_heldItem.empty()) {
if (!_library->GetItemLibrary()->TryGet(this->_heldItem.c_str(), heldItem)) {
if (!this->_heldItem.Empty()) {
if (!_library->GetItemLibrary()->TryGet(this->_heldItem, heldItem)) {
throw CreatureException("Invalid held item.");
}
}

View File

@@ -7,15 +7,15 @@
namespace CreatureLib::Battling {
class CreateCreature {
const BattleLibrary* _library;
std::string _species;
std::string _variant = "default";
Arbutils::CaseInsensitiveConstString _species;
Arbutils::CaseInsensitiveConstString _variant = "default"_cnc;
uint8_t _level;
std::string _nickname = "";
std::string _talent = "";
Library::Gender _gender = static_cast<Library::Gender>(-1);
uint8_t _coloring = 0;
std::string _heldItem = "";
Arbutils::CaseInsensitiveConstString _heldItem = ""_cnc;
uint32_t _identifier = 0;
std::vector<std::tuple<const Library::AttackData*, AttackLearnMethod>> _attacks = {};
@@ -23,10 +23,11 @@ namespace CreatureLib::Battling {
CreateCreature(const BattleLibrary* library, std::string species, uint8_t level)
: _library(library), _species(std::move(species)), _level(level) {}
CreateCreature* WithVariant(std::string variant);
CreateCreature* WithVariant(const Arbutils::CaseInsensitiveConstString& variant);
CreateCreature* WithNickname(std::string nickname);
CreateCreature* WithGender(Library::Gender gender);
CreateCreature* WithAttack(const std::string& attackName, AttackLearnMethod learnMethod);
CreateCreature* WithAttack(const Arbutils::CaseInsensitiveConstString& attackName,
AttackLearnMethod learnMethod);
Creature* Create();
};

View File

@@ -13,7 +13,11 @@ Battling::Creature::Creature(const BattleLibrary* library, const Library::Creatu
: _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)) {
_activeTalent = _library->LoadScript(ScriptCategory::Talent, GetActiveTalent());
if (_nickname.empty()) {
_nickname = species->GetName().std_str();
}
}
void Battling::Creature::ChangeLevel(int8_t amount) {
@@ -21,12 +25,6 @@ void Battling::Creature::ChangeLevel(int8_t amount) {
RecalculateFlatStats();
}
const std::string& Battling::Creature::GetNickname() const {
if (_nickname.empty())
return _species->GetName();
return _nickname;
}
const std::string& Battling::Creature::GetActiveTalent() const {
if (_hasOverridenTalent) {
return _overridenTalentName;
@@ -178,7 +176,7 @@ const Library::SpeciesVariant* Battling::Creature::GetDisplayVariant() const {
variant = _variant;
return variant;
}
void Battling::Creature::SetHeldItem(const std::string& itemName) {
void Battling::Creature::SetHeldItem(const Arbutils::CaseInsensitiveConstString& itemName) {
const Library::Item* item;
if (!_library->GetItemLibrary()->TryGet(itemName, item)) {
throw CreatureException("Item not found.");

View File

@@ -86,7 +86,7 @@ namespace CreatureLib::Battling {
return _heldItem != nullptr && _heldItem->GetName() == name;
}
inline const Library::Item* GetHeldItem() const { return _heldItem; }
void SetHeldItem(const std::string& itemName);
void SetHeldItem(const Arbutils::CaseInsensitiveConstString& itemName);
inline void SetHeldItem(const Library::Item* item) { _heldItem = item; };
inline uint32_t GetCurrentHealth() const { return _currentHealth; }
@@ -97,7 +97,7 @@ namespace CreatureLib::Battling {
void SetOnBattleField(bool value) { _onBattleField = value; }
bool IsOnBattleField() const { return _onBattleField; }
const std::string& GetNickname() const;
const std::string& GetNickname() const { return _nickname; }
const std::string& GetActiveTalent() const;
[[nodiscard]] bool IsFainted() const;