Use smart pointers for basic libraries.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-03-26 22:46:14 +01:00
parent b5894ea8f2
commit 214ff81992
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
10 changed files with 103 additions and 126 deletions

View File

@ -8,7 +8,9 @@ export const BattleLibrary* CreatureLib_BattleLibrary_Construct(const CreatureLi
ExperienceLibrary* experienceLibrary,
ScriptResolver* scriptResolver,
MiscLibrary* miscLibrary) {
return new BattleLibrary(staticLib, statCalculator, damageLibrary, experienceLibrary, scriptResolver, miscLibrary);
return nullptr;
// return new BattleLibrary(staticLib, statCalculator, damageLibrary, experienceLibrary, scriptResolver,
// miscLibrary);
}
export void CreatureLib_BattleLibrary_Destruct(const BattleLibrary* p) { delete p; }
@ -17,9 +19,9 @@ export void CreatureLib_BattleLibrary_Destruct(const BattleLibrary* p) { delete
export returnType CreatureLib_##type##_##name(const type* p) { return p->name(); }
SIMPLE_GET_FUNC(BattleLibrary, GetStaticLib, const CreatureLib::Library::DataLibrary*);
SIMPLE_GET_FUNC(BattleLibrary, GetStatCalculator, const BattleStatCalculator*);
SIMPLE_GET_FUNC(BattleLibrary, GetDamageLibrary, const DamageLibrary*);
SIMPLE_GET_FUNC(BattleLibrary, GetMiscLibrary, const MiscLibrary*);
SIMPLE_GET_FUNC(BattleLibrary, GetExperienceLibrary, const ExperienceLibrary*);
// SIMPLE_GET_FUNC(BattleLibrary, GetStatCalculator, const BattleStatCalculator*);
// SIMPLE_GET_FUNC(BattleLibrary, GetDamageLibrary, const DamageLibrary*);
// SIMPLE_GET_FUNC(BattleLibrary, GetMiscLibrary, const MiscLibrary*);
// SIMPLE_GET_FUNC(BattleLibrary, GetExperienceLibrary, const ExperienceLibrary*);
#undef SIMPLE_GET_FUNC

View File

@ -5,13 +5,16 @@ using namespace CreatureLib::Library;
export uint8_t CreatureLib_DataLibrary_Construct(const DataLibrary*& out, LibrarySettings* settings,
SpeciesLibrary* species, AttackLibrary* attacks, ItemLibrary* items,
GrowthRateLibrary* growthRates, TypeLibrary* typeLibrary) {
Try(out = new DataLibrary(settings, species, attacks, items, growthRates, typeLibrary);)
Try(out = new DataLibrary(
std::unique_ptr<const LibrarySettings>(settings), std::unique_ptr<const SpeciesLibrary>(species),
std::unique_ptr<const AttackLibrary>(attacks), std::unique_ptr<const ItemLibrary>(items),
std::unique_ptr<const GrowthRateLibrary>(growthRates), std::unique_ptr<const TypeLibrary>(typeLibrary));)
}
export void CreatureLib_DataLibrary_Destruct(const DataLibrary* p) { delete p; }
#define SIMPLE_GET_FUNC(type, name, returnType) \
export returnType CreatureLib_##type##_##name(const type* p) { return p->name(); }
export returnType CreatureLib_##type##_##name(const type* p) { return p->name().get(); }
SIMPLE_GET_FUNC(DataLibrary, GetSettings, const LibrarySettings*);
SIMPLE_GET_FUNC(DataLibrary, GetSpeciesLibrary, const SpeciesLibrary*);

View File

@ -134,7 +134,7 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* targe
auto attackData = attack->GetAttack()->GetAttack();
auto library = user->GetBattle()->GetLibrary();
AssertNotNull(library)
auto dmgLibrary = library->GetDamageLibrary();
auto& dmgLibrary = library->GetDamageLibrary();
for (uint8_t hitIndex = 0; hitIndex < numHits; hitIndex++) {
if (user->IsFainted()) {
break;

View File

@ -2,46 +2,24 @@
using namespace CreatureLib::Battling;
BattleLibrary::BattleLibrary(const CreatureLib::Library::DataLibrary* staticLib, BattleStatCalculator* statCalculator,
DamageLibrary* damageLibrary, ExperienceLibrary* experienceLibrary,
ScriptResolver* scriptResolver, MiscLibrary* miscLibrary)
: _staticLib(staticLib), _statCalculator(statCalculator), _damageLibrary(damageLibrary),
_experienceLibrary(experienceLibrary), _scriptResolver(scriptResolver), _miscLibrary(miscLibrary) {}
BattleLibrary::BattleLibrary(const Library::DataLibrary* staticLib,
std::unique_ptr<BattleStatCalculator> statCalculator,
std::unique_ptr<DamageLibrary> damageLibrary,
std::unique_ptr<ExperienceLibrary> experienceLibrary,
std::unique_ptr<ScriptResolver> scriptResolver, std::unique_ptr<MiscLibrary> miscLibrary)
: _staticLib(staticLib), _statCalculator(std::move(statCalculator)), _damageLibrary(std::move(damageLibrary)),
_experienceLibrary(std::move(experienceLibrary)), _scriptResolver(std::move(scriptResolver)),
_miscLibrary(std::move(miscLibrary)) {}
BattleLibrary::~BattleLibrary() {
delete _staticLib;
delete _statCalculator;
delete _damageLibrary;
delete _experienceLibrary;
delete _scriptResolver;
delete _miscLibrary;
BattleLibrary::~BattleLibrary() { delete _staticLib; }
const std::unique_ptr<BattleStatCalculator>& BattleLibrary::GetStatCalculator() const noexcept {
return _statCalculator;
}
const CreatureLib::Library::LibrarySettings* BattleLibrary::GetSettings() const noexcept {
return _staticLib->GetSettings();
}
const std::unique_ptr<DamageLibrary>& BattleLibrary::GetDamageLibrary() const noexcept { return _damageLibrary; }
const BattleStatCalculator* BattleLibrary::GetStatCalculator() const noexcept { return _statCalculator; }
const CreatureLib::Library::SpeciesLibrary* BattleLibrary::GetSpeciesLibrary() const noexcept {
return _staticLib->GetSpeciesLibrary();
}
const CreatureLib::Library::ItemLibrary* BattleLibrary::GetItemLibrary() const noexcept {
return _staticLib->GetItemLibrary();
}
const CreatureLib::Library::AttackLibrary* BattleLibrary::GetAttackLibrary() const noexcept {
return _staticLib->GetAttackLibrary();
}
const CreatureLib::Library::TypeLibrary* BattleLibrary::GetTypeLibrary() const noexcept {
return _staticLib->GetTypeLibrary();
}
const DamageLibrary* BattleLibrary::GetDamageLibrary() const noexcept { return _damageLibrary; }
const MiscLibrary* BattleLibrary::GetMiscLibrary() const noexcept { return _miscLibrary; }
const std::unique_ptr<MiscLibrary>& BattleLibrary::GetMiscLibrary() const noexcept { return _miscLibrary; }
Script* BattleLibrary::LoadScript(ScriptCategory category, const ConstString& scriptName) const {
return _scriptResolver->LoadScript(category, scriptName);

View File

@ -1,6 +1,7 @@
#ifndef CREATURELIB_BATTLELIBRARY_HPP
#define CREATURELIB_BATTLELIBRARY_HPP
#include <memory>
#include "../../Library/DataLibrary.hpp"
#include "../ScriptHandling/ScriptResolver.hpp"
#include "BattleStatCalculator.hpp"
@ -12,32 +13,45 @@ namespace CreatureLib::Battling {
class BattleLibrary {
protected:
const Library::DataLibrary* _staticLib = nullptr;
BattleStatCalculator* _statCalculator = nullptr;
DamageLibrary* _damageLibrary = nullptr;
ExperienceLibrary* _experienceLibrary = nullptr;
ScriptResolver* _scriptResolver = nullptr;
MiscLibrary* _miscLibrary = nullptr;
const std::unique_ptr<BattleStatCalculator> _statCalculator;
const std::unique_ptr<DamageLibrary> _damageLibrary = nullptr;
const std::unique_ptr<ExperienceLibrary> _experienceLibrary = nullptr;
const std::unique_ptr<ScriptResolver> _scriptResolver = nullptr;
const std::unique_ptr<MiscLibrary> _miscLibrary = nullptr;
public:
BattleLibrary(const Library::DataLibrary* staticLib, BattleStatCalculator* statCalculator,
DamageLibrary* damageLibrary, ExperienceLibrary* experienceLibrary,
ScriptResolver* scriptResolver, MiscLibrary* miscLibrary);
BattleLibrary(const Library::DataLibrary* staticLib, std::unique_ptr<BattleStatCalculator> statCalculator,
std::unique_ptr<DamageLibrary> damageLibrary,
std::unique_ptr<ExperienceLibrary> experienceLibrary,
std::unique_ptr<ScriptResolver> scriptResolver, std::unique_ptr<MiscLibrary> miscLibrary);
~BattleLibrary();
inline const Library::DataLibrary* GetStaticLib() const noexcept { return _staticLib; }
[[nodiscard]] const Library::LibrarySettings* GetSettings() const noexcept;
[[nodiscard]] const Library::SpeciesLibrary* GetSpeciesLibrary() const noexcept;
[[nodiscard]] const Library::ItemLibrary* GetItemLibrary() const noexcept;
[[nodiscard]] const Library::AttackLibrary* GetAttackLibrary() const noexcept;
[[nodiscard]] const Library::TypeLibrary* GetTypeLibrary() const noexcept;
[[nodiscard]] const Library::GrowthRateLibrary* GetGrowthRateLibrary() const noexcept {
[[nodiscard]] const std::unique_ptr<const Library::LibrarySettings>& GetSettings() const noexcept {
return _staticLib->GetSettings();
}
[[nodiscard]] const std::unique_ptr<const Library::SpeciesLibrary>& GetSpeciesLibrary() const noexcept {
return _staticLib->GetSpeciesLibrary();
}
[[nodiscard]] const std::unique_ptr<const Library::ItemLibrary>& GetItemLibrary() const noexcept {
return _staticLib->GetItemLibrary();
}
[[nodiscard]] const std::unique_ptr<const Library::AttackLibrary>& GetAttackLibrary() const noexcept {
return _staticLib->GetAttackLibrary();
}
[[nodiscard]] const std::unique_ptr<const Library::TypeLibrary>& GetTypeLibrary() const noexcept {
return _staticLib->GetTypeLibrary();
}
[[nodiscard]] const std::unique_ptr<const Library::GrowthRateLibrary>& GetGrowthRateLibrary() const noexcept {
return _staticLib->GetGrowthRates();
}
[[nodiscard]] const BattleStatCalculator* GetStatCalculator() const noexcept;
[[nodiscard]] const DamageLibrary* GetDamageLibrary() const noexcept;
[[nodiscard]] const MiscLibrary* GetMiscLibrary() const noexcept;
[[nodiscard]] const ExperienceLibrary* GetExperienceLibrary() const noexcept { return _experienceLibrary; }
[[nodiscard]] const std::unique_ptr<BattleStatCalculator>& GetStatCalculator() const noexcept;
[[nodiscard]] const std::unique_ptr<DamageLibrary>& GetDamageLibrary() const noexcept;
[[nodiscard]] const std::unique_ptr<MiscLibrary>& GetMiscLibrary() const noexcept;
[[nodiscard]] const std::unique_ptr<ExperienceLibrary>& GetExperienceLibrary() const noexcept {
return _experienceLibrary;
}
[[nodiscard]] Script* LoadScript(ScriptCategory category, const ConstString& scriptName) const;
};

View File

@ -70,7 +70,7 @@ uint32_t Battling::Creature::GetBaseStat(Library::Statistic stat) const noexcept
int8_t Battling::Creature::GetStatBoost(Library::Statistic stat) const noexcept { return _statBoost.GetStat(stat); }
void Battling::Creature::RecalculateFlatStats() {
auto statCalc = this->_library->GetStatCalculator();
auto& statCalc = this->_library->GetStatCalculator();
this->_flatStats = statCalc->CalculateFlatStats(this);
RecalculateBoostedStats();
}

View File

@ -1,40 +1,17 @@
#include "DataLibrary.hpp"
CreatureLib::Library::DataLibrary::DataLibrary(LibrarySettings* settings, CreatureLib::Library::SpeciesLibrary* species,
CreatureLib::Library::AttackLibrary* attacks,
CreatureLib::Library::ItemLibrary* items,
CreatureLib::Library::GrowthRateLibrary* growthRates,
TypeLibrary* typeLibrary)
: _settings(settings), _species(species), _attacks(attacks), _items(items), _growthRates(growthRates),
_typeLibrary(typeLibrary) {
CreatureLib::Library::DataLibrary::DataLibrary(std::unique_ptr<const LibrarySettings> settings,
std::unique_ptr<const SpeciesLibrary> species,
std::unique_ptr<const AttackLibrary> attacks,
std::unique_ptr<const ItemLibrary> items,
std::unique_ptr<const GrowthRateLibrary> growthRates,
std::unique_ptr<const TypeLibrary> typeLibrary)
: _settings(std::move(settings)), _species(std::move(species)), _attacks(std::move(attacks)),
_items(std::move(items)), _growthRates(std::move(growthRates)), _typeLibrary(std::move(typeLibrary)) {
AssertNotNull(_settings)
AssertNotNull(_species)
AssertNotNull(_attacks)
AssertNotNull(_items)
AssertNotNull(_growthRates)
AssertNotNull(_typeLibrary)
}
const CreatureLib::Library::LibrarySettings* CreatureLib::Library::DataLibrary::GetSettings() const noexcept {
return _settings;
}
const CreatureLib::Library::SpeciesLibrary* CreatureLib::Library::DataLibrary::GetSpeciesLibrary() const noexcept {
return _species;
}
const CreatureLib::Library::AttackLibrary* CreatureLib::Library::DataLibrary::GetAttackLibrary() const noexcept {
return _attacks;
}
const CreatureLib::Library::ItemLibrary* CreatureLib::Library::DataLibrary::GetItemLibrary() const noexcept {
return _items;
}
const CreatureLib::Library::GrowthRateLibrary* CreatureLib::Library::DataLibrary::GetGrowthRates() const noexcept {
return _growthRates;
}
const CreatureLib::Library::TypeLibrary* CreatureLib::Library::DataLibrary::GetTypeLibrary() const noexcept {
return _typeLibrary;
}
}

View File

@ -1,6 +1,7 @@
#ifndef CREATURELIB_DATALIBRARY_HPP
#define CREATURELIB_DATALIBRARY_HPP
#include <memory>
#include "AttackLibrary.hpp"
#include "GrowthRates/GrowthRateLibrary.hpp"
#include "ItemLibrary.hpp"
@ -14,32 +15,31 @@ namespace CreatureLib::Library {
*/
class DataLibrary {
private:
const LibrarySettings* _settings;
const SpeciesLibrary* _species;
const AttackLibrary* _attacks;
const ItemLibrary* _items;
const GrowthRateLibrary* _growthRates;
const TypeLibrary* _typeLibrary;
const std::unique_ptr<const LibrarySettings> _settings;
const std::unique_ptr<const SpeciesLibrary> _species;
const std::unique_ptr<const AttackLibrary> _attacks;
const std::unique_ptr<const ItemLibrary> _items;
const std::unique_ptr<const GrowthRateLibrary> _growthRates;
const std::unique_ptr<const TypeLibrary> _typeLibrary;
public:
DataLibrary(LibrarySettings* settings, CreatureLib::Library::SpeciesLibrary* species,
CreatureLib::Library::AttackLibrary* attacks, CreatureLib::Library::ItemLibrary* items,
CreatureLib::Library::GrowthRateLibrary* growthRates, TypeLibrary* typeLibrary);
DataLibrary(std::unique_ptr<const LibrarySettings> settings, std::unique_ptr<const SpeciesLibrary> species,
std::unique_ptr<const AttackLibrary> attacks, std::unique_ptr<const ItemLibrary> items,
std::unique_ptr<const GrowthRateLibrary> growthRates,
std::unique_ptr<const TypeLibrary> typeLibrary);
virtual ~DataLibrary() {
delete _species;
delete _attacks;
delete _items;
delete _growthRates;
delete _typeLibrary;
virtual ~DataLibrary() {}
[[nodiscard]] const std::unique_ptr<const LibrarySettings>& GetSettings() const noexcept { return _settings; }
[[nodiscard]] const std::unique_ptr<const SpeciesLibrary>& GetSpeciesLibrary() const noexcept {
return _species;
}
[[nodiscard]] const LibrarySettings* GetSettings() const noexcept;
[[nodiscard]] const SpeciesLibrary* GetSpeciesLibrary() const noexcept;
[[nodiscard]] const AttackLibrary* GetAttackLibrary() const noexcept;
[[nodiscard]] const ItemLibrary* GetItemLibrary() const noexcept;
[[nodiscard]] const GrowthRateLibrary* GetGrowthRates() const noexcept;
[[nodiscard]] const TypeLibrary* GetTypeLibrary() const noexcept;
[[nodiscard]] const std::unique_ptr<const AttackLibrary>& GetAttackLibrary() const noexcept { return _attacks; }
[[nodiscard]] const std::unique_ptr<const ItemLibrary>& GetItemLibrary() const noexcept { return _items; }
[[nodiscard]] const std::unique_ptr<const GrowthRateLibrary>& GetGrowthRates() const noexcept {
return _growthRates;
}
[[nodiscard]] const std::unique_ptr<const TypeLibrary>& GetTypeLibrary() const noexcept { return _typeLibrary; }
};
}

View File

@ -29,7 +29,7 @@ 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& l = TestLibrary::Get()->GetAttackLibrary();
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));
@ -51,7 +51,7 @@ 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& l = TestLibrary::Get()->GetAttackLibrary();
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));
@ -73,7 +73,7 @@ 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& l = TestLibrary::Get()->GetAttackLibrary();
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));
@ -95,7 +95,7 @@ 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& l = TestLibrary::Get()->GetAttackLibrary();
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));

View File

@ -9,11 +9,14 @@ BattleLibrary* TestLibrary::_library = nullptr;
BattleLibrary* TestLibrary::Get() {
if (TestLibrary::_library == nullptr) {
auto l = new DataLibrary(new LibrarySettings(100, 4), BuildSpeciesLibrary(), BuildAttackLibrary(),
BuildItemLibrary(), BuildGrowthRateLibrary(), BuildTypeLibrary());
auto statCalc = new BattleStatCalculator();
auto battleLib = new BattleLibrary(l, statCalc, new DamageLibrary(), new ExperienceLibrary(),
new ScriptResolver(), new MiscLibrary());
auto l = new DataLibrary(
std::make_unique<LibrarySettings>(100, 4), std::unique_ptr<SpeciesLibrary>(BuildSpeciesLibrary()),
std::unique_ptr<AttackLibrary>(BuildAttackLibrary()), std::unique_ptr<ItemLibrary>(BuildItemLibrary()),
std::unique_ptr<GrowthRateLibrary>(BuildGrowthRateLibrary()),
std::unique_ptr<TypeLibrary>(BuildTypeLibrary()));
auto battleLib = new BattleLibrary(l, std::make_unique<BattleStatCalculator>(),
std::make_unique<DamageLibrary>(), std::make_unique<ExperienceLibrary>(),
std::make_unique<ScriptResolver>(), std::make_unique<MiscLibrary>());
TestLibrary::_library = battleLib;
}
return TestLibrary::_library;