Make most of the battle library use unique_ptr.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-05-26 15:11:04 +02:00
parent e46117ea06
commit 16b67b0d54
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
6 changed files with 27 additions and 25 deletions

View File

@ -15,11 +15,14 @@ export void CreatureLib_BattleLibrary_Destruct(const BattleLibrary* p) { delete
#define SIMPLE_GET_FUNC(type, name, returnType) \ #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(); }
#define SIMPLE_GET_FUNC_SMART_PTR(type, name, returnType) \
export returnType CreatureLib_##type##_##name(const type* p) { return p->name().operator->(); }
SIMPLE_GET_FUNC(BattleLibrary, GetStaticLib, const CreatureLib::Library::DataLibrary*); SIMPLE_GET_FUNC(BattleLibrary, GetStaticLib, const CreatureLib::Library::DataLibrary*);
SIMPLE_GET_FUNC(BattleLibrary, GetStatCalculator, const BattleStatCalculator*); SIMPLE_GET_FUNC_SMART_PTR(BattleLibrary, GetStatCalculator, const BattleStatCalculator*);
SIMPLE_GET_FUNC(BattleLibrary, GetDamageLibrary, const DamageLibrary*); SIMPLE_GET_FUNC_SMART_PTR(BattleLibrary, GetDamageLibrary, const DamageLibrary*);
SIMPLE_GET_FUNC(BattleLibrary, GetMiscLibrary, const MiscLibrary*); SIMPLE_GET_FUNC_SMART_PTR(BattleLibrary, GetMiscLibrary, const MiscLibrary*);
SIMPLE_GET_FUNC(BattleLibrary, GetExperienceLibrary, const ExperienceLibrary*); SIMPLE_GET_FUNC_SMART_PTR(BattleLibrary, GetExperienceLibrary, const ExperienceLibrary*);
#undef SIMPLE_GET_FUNC #undef SIMPLE_GET_FUNC
#undef SIMPLE_GET_FUNC_SMART_PTR

View File

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

View File

@ -17,18 +17,15 @@ BattleLibrary::BattleLibrary(const CreatureLib::Library::DataLibrary* staticLib,
BattleLibrary::~BattleLibrary() { BattleLibrary::~BattleLibrary() {
delete _staticLib; delete _staticLib;
delete _statCalculator;
delete _damageLibrary;
delete _experienceLibrary;
delete _scriptResolver;
delete _miscLibrary;
} }
const std::unique_ptr<const CreatureLib::Library::LibrarySettings>& BattleLibrary::GetSettings() const noexcept { const std::unique_ptr<const CreatureLib::Library::LibrarySettings>& BattleLibrary::GetSettings() const noexcept {
return _staticLib->GetSettings(); return _staticLib->GetSettings();
} }
const BattleStatCalculator* BattleLibrary::GetStatCalculator() const noexcept { return _statCalculator; } const std::unique_ptr<const BattleStatCalculator>& BattleLibrary::GetStatCalculator() const noexcept {
return _statCalculator;
}
const std::unique_ptr<const CreatureLib::Library::SpeciesLibrary>& BattleLibrary::GetSpeciesLibrary() const noexcept { const std::unique_ptr<const CreatureLib::Library::SpeciesLibrary>& BattleLibrary::GetSpeciesLibrary() const noexcept {
return _staticLib->GetSpeciesLibrary(); return _staticLib->GetSpeciesLibrary();
@ -46,9 +43,9 @@ const std::unique_ptr<const CreatureLib::Library::TypeLibrary>& BattleLibrary::G
return _staticLib->GetTypeLibrary(); return _staticLib->GetTypeLibrary();
} }
const DamageLibrary* BattleLibrary::GetDamageLibrary() const noexcept { return _damageLibrary; } const std::unique_ptr<const DamageLibrary>& BattleLibrary::GetDamageLibrary() const noexcept { return _damageLibrary; }
const MiscLibrary* BattleLibrary::GetMiscLibrary() const noexcept { return _miscLibrary; } const std::unique_ptr<const MiscLibrary>& BattleLibrary::GetMiscLibrary() const noexcept { return _miscLibrary; }
Script* BattleLibrary::LoadScript(ScriptCategory category, Script* BattleLibrary::LoadScript(ScriptCategory category,
const Arbutils::CaseInsensitiveConstString& scriptName) const { const Arbutils::CaseInsensitiveConstString& scriptName) const {

View File

@ -12,11 +12,11 @@ namespace CreatureLib::Battling {
class BattleLibrary { class BattleLibrary {
protected: protected:
const Library::DataLibrary* _staticLib = nullptr; const Library::DataLibrary* _staticLib = nullptr;
BattleStatCalculator* _statCalculator = nullptr; std::unique_ptr<const BattleStatCalculator> _statCalculator = nullptr;
DamageLibrary* _damageLibrary = nullptr; std::unique_ptr<const DamageLibrary> _damageLibrary = nullptr;
ExperienceLibrary* _experienceLibrary = nullptr; std::unique_ptr<const ExperienceLibrary> _experienceLibrary = nullptr;
ScriptResolver* _scriptResolver = nullptr; std::unique_ptr<const ScriptResolver> _scriptResolver = nullptr;
MiscLibrary* _miscLibrary = nullptr; std::unique_ptr<const MiscLibrary> _miscLibrary = nullptr;
public: public:
BattleLibrary(const Library::DataLibrary* staticLib, BattleStatCalculator* statCalculator, BattleLibrary(const Library::DataLibrary* staticLib, BattleStatCalculator* statCalculator,
@ -34,10 +34,12 @@ namespace CreatureLib::Battling {
return _staticLib->GetGrowthRates(); return _staticLib->GetGrowthRates();
} }
[[nodiscard]] const BattleStatCalculator* GetStatCalculator() const noexcept; [[nodiscard]] const std::unique_ptr<const BattleStatCalculator>& GetStatCalculator() const noexcept;
[[nodiscard]] const DamageLibrary* GetDamageLibrary() const noexcept; [[nodiscard]] const std::unique_ptr<const DamageLibrary>& GetDamageLibrary() const noexcept;
[[nodiscard]] const MiscLibrary* GetMiscLibrary() const noexcept; [[nodiscard]] const std::unique_ptr<const MiscLibrary>& GetMiscLibrary() const noexcept;
[[nodiscard]] const ExperienceLibrary* GetExperienceLibrary() const noexcept { return _experienceLibrary; } [[nodiscard]] const std::unique_ptr<const ExperienceLibrary>& GetExperienceLibrary() const noexcept {
return _experienceLibrary;
}
[[nodiscard]] Script* LoadScript(ScriptCategory category, [[nodiscard]] Script* LoadScript(ScriptCategory category,
const Arbutils::CaseInsensitiveConstString& scriptName) const; const Arbutils::CaseInsensitiveConstString& scriptName) const;

View File

@ -63,7 +63,7 @@ bool Battling::Creature::ChangeStatBoost(Library::Statistic stat, int8_t diffAmo
} }
void Battling::Creature::RecalculateFlatStats() { void Battling::Creature::RecalculateFlatStats() {
auto statCalc = this->_library->GetStatCalculator(); auto& statCalc = this->_library->GetStatCalculator();
this->_flatStats = statCalc->CalculateFlatStats(this); this->_flatStats = statCalc->CalculateFlatStats(this);
RecalculateBoostedStats(); RecalculateBoostedStats();
} }

View File

@ -14,7 +14,7 @@ namespace CreatureLib::Battling {
virtual ~ScriptResolver() = default; virtual ~ScriptResolver() = default;
virtual void Initialize(BattleLibrary* library){}; virtual void Initialize(BattleLibrary* library){};
virtual Script* LoadScript(ScriptCategory category, const ConstString& scriptName) { return nullptr; }; virtual Script* LoadScript(ScriptCategory category, const ConstString& scriptName) const { return nullptr; };
}; };
} }