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

This commit is contained in:
Deukhoofd 2020-02-27 18:23:23 +01:00
parent 1d3a8da99e
commit 412e0a4d63
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
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;

View File

@ -5,10 +5,10 @@ CreatureLib::Library::AttackData::AttackData(std::string name, uint8_t type,
CreatureLib::Library::AttackCategory category, uint8_t power,
uint8_t accuracy, uint8_t baseUsage,
CreatureLib::Library::AttackTarget target, int8_t priority,
std::unordered_set<std::string> flags)
std::unordered_set<Arbutils::CaseInsensitiveConstString> flags)
: _name(std::move(name)), _type(type), _category(category), _basePower(power), _accuracy(accuracy),
_baseUsages(baseUsage), _target(target), _priority(priority), _flags(std::move(flags)) {}
bool CreatureLib::Library::AttackData::HasFlag(const std::string& key) const {
bool CreatureLib::Library::AttackData::HasFlag(const Arbutils::CaseInsensitiveConstString& key) const {
return this->_flags.find(key) != this->_flags.end();
}

View File

@ -1,6 +1,7 @@
#ifndef CREATURELIB_ATTACKDATA_HPP
#define CREATURELIB_ATTACKDATA_HPP
#include <Arbutils/ConstString.hpp>
#include <string>
#include <unordered_set>
#include "AttackCategory.hpp"
@ -17,11 +18,12 @@ namespace CreatureLib::Library {
uint8_t _baseUsages;
AttackTarget _target;
int8_t _priority;
std::unordered_set<std::string> _flags;
std::unordered_set<Arbutils::CaseInsensitiveConstString> _flags;
public:
AttackData(std::string name, uint8_t type, AttackCategory category, uint8_t power, uint8_t accuracy,
uint8_t baseUsage, AttackTarget target, int8_t priority, std::unordered_set<std::string> flags);
uint8_t baseUsage, AttackTarget target, int8_t priority,
std::unordered_set<Arbutils::CaseInsensitiveConstString> flags);
virtual ~AttackData() = default;
inline const std::string& GetName() const { return _name; }
@ -33,7 +35,7 @@ namespace CreatureLib::Library {
inline AttackTarget GetTarget() const { return _target; }
inline int8_t GetPriority() const { return _priority; }
bool HasFlag(const std::string& key) const;
bool HasFlag(const Arbutils::CaseInsensitiveConstString& key) const;
};
}

View File

@ -1,16 +1,13 @@
#ifndef CREATURELIB_BASELIBRARY_HPP
#define CREATURELIB_BASELIBRARY_HPP
#include <Arbutils/ConstString.hpp>
#include <algorithm>
#include <string>
#include <unordered_map>
namespace CreatureLib::Library {
template <class T> class BaseLibrary {
inline static constexpr char charToLower(const char c) { return (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c; }
inline static uint32_t constexpr Hash(char const* input) {
return charToLower(*input) ? static_cast<uint32_t>(charToLower(*input)) + 33 * Hash(input + 1) : 5381;
}
std::unordered_map<uint32_t, const T*> _values;
public:
@ -23,14 +20,13 @@ namespace CreatureLib::Library {
_values.clear();
}
inline void Insert(const char* key, const T* value) { _values.insert({Hash(key), value}); }
inline void Insert(const std::string& key, const T* value) { Insert(key.c_str(), value); }
inline void Insert(const Arbutils::CaseInsensitiveConstString& key, const T* value) {
_values.insert({key.GetHash(), value});
}
inline void Delete(const Arbutils::CaseInsensitiveConstString& key) { _values.erase(key.GetHash()); }
inline void Delete(const char* key) { _values.erase(Hash(key)); }
inline void Delete(const std::string& key) { Delete(key.c_str()); }
bool TryGet(const char* name, const T*& out) const {
auto find = this->_values.find(Hash(name));
bool TryGet(const Arbutils::CaseInsensitiveConstString& name, const T*& out) const {
auto find = this->_values.find(name.GetHash());
if (find == this->_values.end()) {
out = nullptr;
return false;
@ -38,16 +34,10 @@ namespace CreatureLib::Library {
out = find->second;
return true;
}
bool TryGet(const std::string& name, const T*& out) const {
return TryGet(name.c_str(), out);
inline const T* Get(const Arbutils::CaseInsensitiveConstString& name) const {
return _values.at(name.GetHash());
}
inline const T* Get(const char* name) const { return _values.at(Hash(name)); }
inline const T* Get(const std::string& name) const { return Get(name.c_str()); }
inline const T* operator[](const char* name) const { return Get(name); }
inline const T* operator[](const std::string& name) const { return Get(name.c_str()); }
inline const T* operator[](const Arbutils::CaseInsensitiveConstString& name) const { return Get(name); }
inline const std::unordered_map<uint32_t, const T*>& GetIterator() const { return _values; }
size_t GetCount() const { return _values.size(); }

View File

@ -2,21 +2,19 @@
using namespace CreatureLib::Library;
CreatureSpecies::CreatureSpecies(uint16_t id, std::string name, const SpeciesVariant* defaultVariant, float genderRatio,
std::string growthRate, uint8_t captureRate)
: _id(id), _genderRate(genderRatio), _growthRate(std::move(growthRate)), _captureRate(captureRate),
_variants({{"default", defaultVariant}}), _name(std::move(name)) {}
CreatureSpecies::CreatureSpecies(uint16_t id, const Arbutils::CaseInsensitiveConstString& name,
const SpeciesVariant* defaultVariant, float genderRatio,
const Arbutils::CaseInsensitiveConstString& growthRate, uint8_t captureRate)
: _id(id), _genderRate(genderRatio), _growthRate(growthRate), _captureRate(captureRate),
_variants({{"default"_cnc, defaultVariant}}), _name(name) {}
bool CreatureSpecies::HasVariant(const std::string& name) const {
auto key = name;
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
return _variants.find(key) != _variants.end();
bool CreatureSpecies::HasVariant(const Arbutils::CaseInsensitiveConstString& name) const {
return _variants.find(name) != _variants.end();
}
bool CreatureSpecies::TryGetVariant(const std::string& name, const SpeciesVariant*& out) const {
auto key = name;
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
auto find = _variants.find(key);
bool CreatureSpecies::TryGetVariant(const Arbutils::CaseInsensitiveConstString& name,
const SpeciesVariant*& out) const {
auto find = _variants.find(name);
if (find != _variants.end()) {
out = find->second;
return true;
@ -24,20 +22,17 @@ bool CreatureSpecies::TryGetVariant(const std::string& name, const SpeciesVarian
return false;
}
const SpeciesVariant* CreatureSpecies::GetVariant(const std::string& name) const {
const SpeciesVariant* CreatureSpecies::GetVariant(const Arbutils::CaseInsensitiveConstString& name) const {
auto key = name;
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
return _variants.at(key);
}
void CreatureSpecies::SetVariant(const std::string& name, const SpeciesVariant* variant) {
auto key = name;
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
auto find = _variants.find(key);
void CreatureSpecies::SetVariant(const Arbutils::CaseInsensitiveConstString& name, const SpeciesVariant* variant) {
auto find = _variants.find(name);
if (find != _variants.end()) {
delete find->second;
}
_variants[key] = variant;
_variants[name] = variant;
}
Gender CreatureSpecies::GetRandomGender(Arbutils::Random& rand) const {
@ -46,6 +41,4 @@ Gender CreatureSpecies::GetRandomGender(Arbutils::Random& rand) const {
if (val >= this->_genderRate)
return Gender ::Female;
return Gender ::Male;
}
const std::string& CreatureSpecies::GetName() const { return _name; }
}

View File

@ -1,6 +1,7 @@
#ifndef CREATURELIB_CREATURESPECIES_HPP
#define CREATURELIB_CREATURESPECIES_HPP
#include <Arbutils/ConstString.hpp>
#include <string>
#include <unordered_map>
#include "../Gender.hpp"
@ -14,16 +15,17 @@ namespace CreatureLib::Library {
class CreatureSpecies {
uint16_t _id;
float _genderRate;
std::string _growthRate;
const Arbutils::CaseInsensitiveConstString _growthRate;
uint8_t _captureRate;
private:
std::unordered_map<std::string, const SpeciesVariant*> _variants;
std::string _name;
std::unordered_map<Arbutils::CaseInsensitiveConstString, const SpeciesVariant*> _variants;
Arbutils::CaseInsensitiveConstString _name;
public:
CreatureSpecies(uint16_t id, std::string name, const SpeciesVariant* defaultVariant, float genderRatio,
std::string growthRate, uint8_t captureRate);
CreatureSpecies(uint16_t id, const Arbutils::CaseInsensitiveConstString& name,
const SpeciesVariant* defaultVariant, float genderRatio,
const Arbutils::CaseInsensitiveConstString& growthRate, uint8_t captureRate);
virtual ~CreatureSpecies() {
for (auto v : _variants)
@ -33,18 +35,22 @@ namespace CreatureLib::Library {
inline uint16_t GetId() const { return _id; }
inline float GetGenderRate() const { return _genderRate; }
inline const std::string& GetGrowthRate() const { return _growthRate; }
inline const Arbutils::CaseInsensitiveConstString& GetGrowthRate() const { return _growthRate; }
inline uint8_t GetCaptureRate() const { return _captureRate; }
[[nodiscard]] bool HasVariant(const std::string& key) const;
[[nodiscard]] bool TryGetVariant(const std::string& name, const SpeciesVariant*& out) const;
[[nodiscard]] const SpeciesVariant* GetVariant(const std::string& key) const;
[[nodiscard]] bool HasVariant(const Arbutils::CaseInsensitiveConstString& key) const;
[[nodiscard]] bool TryGetVariant(const Arbutils::CaseInsensitiveConstString& name,
const SpeciesVariant*& out) const;
[[nodiscard]] const SpeciesVariant* GetVariant(const Arbutils::CaseInsensitiveConstString& key) const;
[[nodiscard]] Gender GetRandomGender(Arbutils::Random& rand) const;
[[nodiscard]] const std::string& GetName() const;
[[nodiscard]] const Arbutils::CaseInsensitiveConstString& GetName() const { return _name; }
void SetVariant(const std::string& name, const SpeciesVariant* variant);
void SetVariant(const Arbutils::CaseInsensitiveConstString& name, const SpeciesVariant* variant);
const std::unordered_map<std::string, const SpeciesVariant*>& GetVariantsIterator() const { return _variants; }
const std::unordered_map<Arbutils::CaseInsensitiveConstString, const SpeciesVariant*>&
GetVariantsIterator() const {
return _variants;
}
};
}

View File

@ -2,30 +2,25 @@
#include <algorithm>
#include "../../Core/Exceptions/CreatureException.hpp"
uint8_t CreatureLib::Library::GrowthRateLibrary::CalculateLevel(const std::string& growthRate,
uint8_t CreatureLib::Library::GrowthRateLibrary::CalculateLevel(const Arbutils::CaseInsensitiveConstString& growthRate,
uint32_t experience) const {
auto g = growthRate;
std::transform(g.begin(), g.end(), g.begin(), ::tolower);
auto find = _growthRates.find(g);
auto find = _growthRates.find(growthRate);
if (find == _growthRates.end()) {
throw CreatureException("Invalid growth rate was requested.");
}
return find->second->CalculateLevel(experience);
}
uint32_t CreatureLib::Library::GrowthRateLibrary::CalculateExperience(const std::string& growthRate,
uint8_t level) const {
auto g = growthRate;
std::transform(g.begin(), g.end(), g.begin(), ::tolower);
auto find = _growthRates.find(g);
uint32_t
CreatureLib::Library::GrowthRateLibrary::CalculateExperience(const Arbutils::CaseInsensitiveConstString& growthRate,
uint8_t level) const {
auto find = _growthRates.find(growthRate);
if (find == _growthRates.end()) {
throw CreatureException("Invalid growth rate was requested.");
}
return find->second->CalculateExperience(level);
}
void CreatureLib::Library::GrowthRateLibrary::AddGrowthRate(const std::string& name,
void CreatureLib::Library::GrowthRateLibrary::AddGrowthRate(const Arbutils::CaseInsensitiveConstString& name,
CreatureLib::Library::GrowthRate* rate) {
auto g = name;
std::transform(g.begin(), g.end(), g.begin(), ::tolower);
_growthRates.insert({g, rate});
_growthRates.insert({name, rate});
}

View File

@ -1,6 +1,7 @@
#ifndef CREATURELIB_GROWTHRATELIBRARY_HPP
#define CREATURELIB_GROWTHRATELIBRARY_HPP
#include <Arbutils/ConstString.hpp>
#include <cstdint>
#include <string>
#include <unordered_map>
@ -9,11 +10,11 @@
namespace CreatureLib::Library {
class GrowthRateLibrary {
private:
std::unordered_map<std::string, GrowthRate*> _growthRates;
std::unordered_map<Arbutils::CaseInsensitiveConstString, GrowthRate*> _growthRates;
public:
GrowthRateLibrary(size_t initialCapacity = 10)
: _growthRates(std::unordered_map<std::string, GrowthRate*>(initialCapacity)) {}
: _growthRates(std::unordered_map<Arbutils::CaseInsensitiveConstString, GrowthRate*>(initialCapacity)) {}
virtual ~GrowthRateLibrary() {
for (auto gr : _growthRates) {
@ -21,10 +22,12 @@ namespace CreatureLib::Library {
}
}
[[nodiscard]] uint8_t CalculateLevel(const std::string& growthRate, uint32_t experience) const;
[[nodiscard]] uint32_t CalculateExperience(const std::string& growthRate, uint8_t level) const;
[[nodiscard]] uint8_t CalculateLevel(const Arbutils::CaseInsensitiveConstString& growthRate,
uint32_t experience) const;
[[nodiscard]] uint32_t CalculateExperience(const Arbutils::CaseInsensitiveConstString& growthRate,
uint8_t level) const;
void AddGrowthRate(const std::string& name, GrowthRate* rate);
void AddGrowthRate(const Arbutils::CaseInsensitiveConstString& name, GrowthRate* rate);
};
}

View File

@ -1,9 +1,9 @@
#include "Item.hpp"
bool CreatureLib::Library::Item::HasFlag(const std::string& flag) const {
bool CreatureLib::Library::Item::HasFlag(const Arbutils::CaseInsensitiveConstString& flag) const {
return this->_flags.find(flag) != this->_flags.end();
}
CreatureLib::Library::Item::Item(std::string name, CreatureLib::Library::ItemCategory category,
CreatureLib::Library::BattleItemCategory battleCategory, int32_t price,
std::unordered_set<std::string> flags)
std::unordered_set<Arbutils::CaseInsensitiveConstString> flags)
: _name(name), _category(category), _battleCategory(battleCategory), _price(price), _flags(flags) {}

View File

@ -1,6 +1,7 @@
#ifndef CREATURELIB_ITEM_HPP
#define CREATURELIB_ITEM_HPP
#include <Arbutils/ConstString.hpp>
#include <string>
#include <unordered_set>
#include "BattleItemCategory.hpp"
@ -13,18 +14,18 @@ namespace CreatureLib::Library {
ItemCategory _category;
BattleItemCategory _battleCategory;
int32_t _price;
std::unordered_set<std::string> _flags;
std::unordered_set<Arbutils::CaseInsensitiveConstString> _flags;
public:
Item(std::string name, ItemCategory category, BattleItemCategory battleCategory, int32_t price,
std::unordered_set<std::string> flags);
std::unordered_set<Arbutils::CaseInsensitiveConstString> flags);
inline const std::string& GetName() const { return _name; }
inline ItemCategory GetCategory() const { return _category; }
inline BattleItemCategory GetBattleCategory() const { return _battleCategory; }
inline const int32_t GetPrice() const { return _price; }
bool HasFlag(const std::string& flag) const;
bool HasFlag(const Arbutils::CaseInsensitiveConstString& flag) const;
};
}

View File

@ -28,8 +28,8 @@ TEST_CASE("Turn ordering: Attack before pass", "[Battling]") {
TEST_CASE("Turn ordering: High priority goes before no priority", "[Battling]") {
auto l = TestLibrary::Get()->GetAttackLibrary();
auto a1 = new LearnedAttack(l->Get("standard"), AttackLearnMethod::Unknown);
auto a2 = new LearnedAttack(l->Get("highPriority"), AttackLearnMethod::Unknown);
auto a1 = new LearnedAttack(l->Get("standard"_cnc), AttackLearnMethod::Unknown);
auto a2 = new LearnedAttack(l->Get("highPriority"_cnc), AttackLearnMethod::Unknown);
auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0));
auto choice2 = new AttackTurnChoice(nullptr, a2, CreatureIndex(0, 0));
auto vec = std::vector<BaseTurnChoice*>{choice1, choice2};
@ -50,8 +50,8 @@ TEST_CASE("Turn ordering: High priority goes before no priority", "[Battling]")
TEST_CASE("Turn ordering: Higher priority goes before high priority", "[Battling]") {
auto l = TestLibrary::Get()->GetAttackLibrary();
auto a1 = new LearnedAttack(l->Get("highPriority"), AttackLearnMethod::Unknown);
auto a2 = new LearnedAttack(l->Get("higherPriority"), AttackLearnMethod::Unknown);
auto a1 = new LearnedAttack(l->Get("highPriority"_cnc), AttackLearnMethod::Unknown);
auto a2 = new LearnedAttack(l->Get("higherPriority"_cnc), AttackLearnMethod::Unknown);
auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0));
auto choice2 = new AttackTurnChoice(nullptr, a2, CreatureIndex(0, 0));
auto vec = std::vector<BaseTurnChoice*>{choice1, choice2};
@ -72,8 +72,8 @@ TEST_CASE("Turn ordering: Higher priority goes before high priority", "[Battling
TEST_CASE("Turn ordering: High priority goes before low priority", "[Battling]") {
auto l = TestLibrary::Get()->GetAttackLibrary();
auto a1 = new LearnedAttack(l->Get("lowPriority"), AttackLearnMethod::Unknown);
auto a2 = new LearnedAttack(l->Get("higherPriority"), AttackLearnMethod::Unknown);
auto a1 = new LearnedAttack(l->Get("lowPriority"_cnc), AttackLearnMethod::Unknown);
auto a2 = new LearnedAttack(l->Get("higherPriority"_cnc), AttackLearnMethod::Unknown);
auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0));
auto choice2 = new AttackTurnChoice(nullptr, a2, CreatureIndex(0, 0));
auto vec = std::vector<BaseTurnChoice*>{choice1, choice2};
@ -94,8 +94,8 @@ TEST_CASE("Turn ordering: High priority goes before low priority", "[Battling]")
TEST_CASE("Turn ordering: No priority goes before low priority", "[Battling]") {
auto l = TestLibrary::Get()->GetAttackLibrary();
auto a1 = new LearnedAttack(l->Get("lowPriority"), AttackLearnMethod::Unknown);
auto a2 = new LearnedAttack(l->Get("standard"), AttackLearnMethod::Unknown);
auto a1 = new LearnedAttack(l->Get("lowPriority"_cnc), AttackLearnMethod::Unknown);
auto a2 = new LearnedAttack(l->Get("standard"_cnc), AttackLearnMethod::Unknown);
auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0));
auto choice2 = new AttackTurnChoice(nullptr, a2, CreatureIndex(0, 0));
auto vec = std::vector<BaseTurnChoice*>{choice1, choice2};

View File

@ -13,7 +13,8 @@ using namespace Battling;
TEST_CASE("Create Party", "[Integrations]") {
auto library = TestLibrary::Get();
auto c1 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c1 =
CreateCreature(library, "testSpecies1", 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party1{c1};
auto battleParty = BattleParty(&party1, {CreatureIndex(0, 0)});
REQUIRE(battleParty.GetParty()->GetAtIndex(0) == c1);
@ -21,10 +22,12 @@ TEST_CASE("Create Party", "[Integrations]") {
TEST_CASE("Create Battle", "[Integrations]") {
auto library = TestLibrary::Get();
auto c1 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c1 =
CreateCreature(library, "testSpecies1", 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party1{c1};
auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)});
auto c2 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c2 =
CreateCreature(library, "testSpecies1", 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party2{c2};
auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)});
@ -33,10 +36,12 @@ TEST_CASE("Create Battle", "[Integrations]") {
TEST_CASE("Use damaging move", "[Integrations]") {
auto library = TestLibrary::Get();
auto c1 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c1 =
CreateCreature(library, "testSpecies1", 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party1{c1};
auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)});
auto c2 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c2 =
CreateCreature(library, "testSpecies1", 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party2{c2};
auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)});
@ -53,10 +58,12 @@ TEST_CASE("Use damaging move", "[Integrations]") {
TEST_CASE("Finish battle when all battle of one side have fainted", "[Integrations]") {
auto library = TestLibrary::Get();
auto c1 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c1 =
CreateCreature(library, "testSpecies1", 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party1{c1};
auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)});
auto c2 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c2 =
CreateCreature(library, "testSpecies1", 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party2{c2};
auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)});
@ -86,10 +93,12 @@ TEST_CASE("Finish battle when all battle of one side have fainted", "[Integratio
TEST_CASE("When creature is dealt enough damage, faint it and mark battle as ended", "[Integrations]") {
auto library = TestLibrary::Get();
auto c1 = CreateCreature(library, "testSpecies1", 100).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c1 =
CreateCreature(library, "testSpecies1", 100).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party1{c1};
auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)});
auto c2 = CreateCreature(library, "testSpecies1", 1).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c2 =
CreateCreature(library, "testSpecies1", 1).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party2{c2};
auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)});
@ -113,11 +122,14 @@ TEST_CASE("When creature is dealt enough damage, faint it and mark battle as end
TEST_CASE("When another creature is available on faint, make sure the battle hasn't ended", "[Integrations]") {
auto library = TestLibrary::Get();
auto c1 = CreateCreature(library, "testSpecies1", 100).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c1 =
CreateCreature(library, "testSpecies1", 100).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party1{c1};
auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)});
auto c2 = CreateCreature(library, "testSpecies1", 1).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c3 = CreateCreature(library, "testSpecies1", 1).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c2 =
CreateCreature(library, "testSpecies1", 1).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
auto c3 =
CreateCreature(library, "testSpecies1", 1).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party2{c2, c3};
auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)});
@ -148,11 +160,14 @@ TEST_CASE("When another creature is available on faint, make sure the battle has
TEST_CASE("Switch Creature in", "[Integrations]") {
auto library = TestLibrary::Get();
auto c1 = CreateCreature(library, "testSpecies1", 100).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c2 = CreateCreature(library, "testSpecies1", 1).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c1 =
CreateCreature(library, "testSpecies1", 100).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
auto c2 =
CreateCreature(library, "testSpecies1", 1).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party1{c1, c2};
auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)});
auto c3 = CreateCreature(library, "testSpecies1", 1).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c3 =
CreateCreature(library, "testSpecies1", 1).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party2{c3};
auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)});
@ -171,11 +186,14 @@ TEST_CASE("Switch Creature in", "[Integrations]") {
TEST_CASE("Switch Creature in, but have attack aimed at it. Attack should hit new creature", "[Integrations]") {
auto library = TestLibrary::Get();
auto c1 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c2 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c1 =
CreateCreature(library, "testSpecies1", 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
auto c2 =
CreateCreature(library, "testSpecies1", 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party1{c1, c2};
auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)});
auto c3 = CreateCreature(library, "testSpecies1", 50).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c3 =
CreateCreature(library, "testSpecies1", 50).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party2{c3};
auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)});
@ -193,11 +211,14 @@ TEST_CASE("Switch Creature in, but have attack aimed at it. Attack should hit ne
TEST_CASE("Switch Creature in, mark as seen opponent for opponent", "[Integrations]") {
auto library = TestLibrary::Get();
auto c1 = CreateCreature(library, "testSpecies1", 100).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c2 = CreateCreature(library, "testSpecies1", 1).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c1 =
CreateCreature(library, "testSpecies1", 100).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
auto c2 =
CreateCreature(library, "testSpecies1", 1).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party1{c1, c2};
auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)});
auto c3 = CreateCreature(library, "testSpecies1", 1).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c3 =
CreateCreature(library, "testSpecies1", 1).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party2{c3};
auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)});
@ -229,10 +250,12 @@ TEST_CASE("Switch Creature in, mark as seen opponent for opponent", "[Integratio
TEST_CASE("Flee Battle", "[Integrations]") {
auto library = TestLibrary::Get();
auto c1 = CreateCreature(library, "testSpecies1", 100).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c1 =
CreateCreature(library, "testSpecies1", 100).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party1{c1};
auto battleParty1 = BattleParty(&party1, {CreatureIndex(0, 0)});
auto c2 = CreateCreature(library, "testSpecies1", 1).WithAttack("standard", AttackLearnMethod::Unknown)->Create();
auto c2 =
CreateCreature(library, "testSpecies1", 1).WithAttack("standard"_cnc, AttackLearnMethod::Unknown)->Create();
CreatureParty party2{c2};
auto battleParty2 = BattleParty(&party2, {CreatureIndex(1, 0)});

View File

@ -13,7 +13,7 @@ TEST_CASE("Create basic creature", "[Library]") {
TEST_CASE("Get creature species", "[Library]") {
auto library = TestLibrary::Get();
auto creature = CreateCreature(library, "testSpecies1", 1).Create();
REQUIRE(creature->GetSpecies()->GetName() == "testSpecies1");
REQUIRE(creature->GetSpecies()->GetName() == "testSpecies1"_cnc);
delete creature;
}

View File

@ -22,25 +22,25 @@ BattleLibrary* TestLibrary::Get() {
SpeciesLibrary* TestLibrary::BuildSpeciesLibrary() {
auto l = new SpeciesLibrary();
l->Insert("testSpecies1",
new CreatureSpecies(0, "testSpecies1",
l->Insert("testSpecies1"_cnc,
new CreatureSpecies(0, "testSpecies1"_cnc,
new SpeciesVariant("default", 1, 1, 10, {0, 1},
StatisticSet<uint16_t>(10, 10, 10, 10, 10, 10), {"testTalent"},
{"testSecretTalent"}, new LearnableAttacks(100)),
0.5f, "testGrowthRate", 5));
0.5f, "testGrowthRate"_cnc, 5));
return l;
}
AttackLibrary* TestLibrary::BuildAttackLibrary() {
auto l = new AttackLibrary();
l->Insert("standard", new AttackData("standard", 0, AttackCategory::Physical, 20, 100, 30,
AttackTarget::AdjacentOpponent, 0, {}));
l->Insert("highPriority", new AttackData("highPriority", 0, AttackCategory::Physical, 20, 100, 30,
AttackTarget::AdjacentOpponent, 1, {}));
l->Insert("higherPriority", new AttackData("higherPriority", 0, AttackCategory::Physical, 20, 100, 30,
AttackTarget::AdjacentOpponent, 2, {}));
l->Insert("lowPriority", new AttackData("lowPriority", 0, AttackCategory::Physical, 20, 100, 30,
AttackTarget::AdjacentOpponent, -1, {}));
l->Insert("standard"_cnc, new AttackData("standard", 0, AttackCategory::Physical, 20, 100, 30,
AttackTarget::AdjacentOpponent, 0, {}));
l->Insert("highPriority"_cnc, new AttackData("highPriority", 0, AttackCategory::Physical, 20, 100, 30,
AttackTarget::AdjacentOpponent, 1, {}));
l->Insert("higherPriority"_cnc, new AttackData("higherPriority", 0, AttackCategory::Physical, 20, 100, 30,
AttackTarget::AdjacentOpponent, 2, {}));
l->Insert("lowPriority"_cnc, new AttackData("lowPriority", 0, AttackCategory::Physical, 20, 100, 30,
AttackTarget::AdjacentOpponent, -1, {}));
return l;
}
@ -51,7 +51,7 @@ ItemLibrary* TestLibrary::BuildItemLibrary() {
GrowthRateLibrary* TestLibrary::BuildGrowthRateLibrary() {
auto l = new GrowthRateLibrary();
l->AddGrowthRate("testGrowthRate", new LookupGrowthRate());
l->AddGrowthRate("testGrowthRate"_cnc, new LookupGrowthRate());
return l;
}