From 305eb5efb2b5c8a1fb441f8aa7e3429dab9fb503 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 8 May 2021 11:26:35 +0200 Subject: [PATCH] Cleanup for Creature class battle data. Signed-off-by: Deukhoofd --- src/Battling/Models/Creature.cpp | 60 ++++++++++++++++---------------- src/Battling/Models/Creature.hpp | 41 ++++++++++++---------- 2 files changed, 53 insertions(+), 48 deletions(-) diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index 92c2aa7..e3459f3 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -31,8 +31,8 @@ namespace CreatureLib::Battling { // If the creature is genderless, but it's new species is not, we want to set its gender if (_gender != CreatureLib::Library::Gender::Genderless && _species->GetGenderRate() != -1) { // If we are currently in battle, use the battle random so we can get predictable events. - if (_battle.HasValue()) { - _gender = _species->GetRandomGender(_battle.GetValue()->GetRandom()->GetRNG()); + if (_battleData.Battle.HasValue()) { + _gender = _species->GetRandomGender(_battleData.Battle.GetValue()->GetRandom()->GetRNG()); } // Else create a new random. else { @@ -43,8 +43,8 @@ namespace CreatureLib::Battling { else if (_species->GetGenderRate() == -1 && _gender != CreatureLib::Library::Gender::Genderless) { _gender = CreatureLib::Library::Gender::Genderless; } - if (_battle.HasValue()) { - _battle.GetValue()->TriggerEventListener(this, _species); + if (_battleData.Battle.HasValue()) { + _battleData.Battle.GetValue()->TriggerEventListener(this, _species); } ChangeVariant(variant); } @@ -73,8 +73,8 @@ namespace CreatureLib::Battling { // TODO: consider variant specific attacks? - if (_battle.HasValue()) { - _battle.GetValue()->TriggerEventListener(this, _variant); + if (_battleData.Battle.HasValue()) { + _battleData.Battle.GetValue()->TriggerEventListener(this, _variant); } } @@ -97,14 +97,13 @@ namespace CreatureLib::Battling { } void Creature::SetBattleData(const ArbUt::BorrowedPtr& battle, const ArbUt::BorrowedPtr& side) { - _battle = battle.GetRaw(); - _side = side.GetRaw(); + _battleData.Battle = battle.GetRaw(); + _battleData.Side = side.GetRaw(); this->ResetActiveScripts(); } void Creature::ClearBattleData() noexcept { - _battle = nullptr; - _side = nullptr; - _seenOpponents = {}; + _battleData = {}; + _battleData.SeenOpponents = {}; ResetActiveScripts(); } @@ -147,19 +146,20 @@ namespace CreatureLib::Battling { bool Creature::IsFainted() const noexcept { return this->_currentHealth == 0; } void Creature::OnFaint() { - EnsureNotNull(_battle) - EnsureNotNull(_side) + EnsureNotNull(_battleData.Battle) + EnsureNotNull(_battleData.Side) // HOOK: On Faint - if (_battle.HasValue()) { - _battle.GetValue()->TriggerEventListener(this); + if (_battleData.Battle.HasValue()) { + _battleData.Battle.GetValue()->TriggerEventListener(this); } - _library->GetExperienceLibrary()->HandleExperienceGain(this, _seenOpponents); - if (_battle.HasValue() && _side.HasValue()) { - auto sideIndex = _side.GetValue()->GetCreatureIndex(this); - if (!_battle.GetValue()->CanSlotBeFilled(_side.GetValue()->GetSideIndex(), sideIndex)) { - _side.GetValue()->MarkSlotAsUnfillable(this); + _library->GetExperienceLibrary()->HandleExperienceGain(this, _battleData.SeenOpponents); + if (_battleData.Battle.HasValue() && _battleData.Side.HasValue()) { + auto sideIndex = _battleData.Side.GetValue()->GetCreatureIndex(this); + if (!_battleData.Battle.GetValue()->CanSlotBeFilled(_battleData.Side.GetValue()->GetSideIndex(), + sideIndex)) { + _battleData.Side.GetValue()->MarkSlotAsUnfillable(this); } - _battle.GetValue()->ValidateBattleState(); + _battleData.Battle.GetValue()->ValidateBattleState(); } } @@ -223,8 +223,8 @@ namespace CreatureLib::Battling { size_t Creature::ScriptCount() const { auto c = 3; - if (_side.HasValue()) { - c += _side.GetValue()->ScriptCount(); + if (_battleData.Side.HasValue()) { + c += _battleData.Side.GetValue()->ScriptCount(); } return c; } @@ -233,8 +233,8 @@ namespace CreatureLib::Battling { scripts.Append(ScriptWrapper::FromScript(&_activeTalent)); scripts.Append(ScriptWrapper::FromScript(&_status)); scripts.Append(ScriptWrapper::FromSet(&_volatile)); - if (_side.HasValue()) { - _side.GetValue()->GetActiveScripts(scripts); + if (_battleData.Side.HasValue()) { + _battleData.Side.GetValue()->GetActiveScripts(scripts); } } void Creature::ClearVolatileScripts() { _volatile.Clear(); } @@ -248,8 +248,8 @@ namespace CreatureLib::Battling { if (level >= maxLevel) { exp = _library->GetGrowthRateLibrary()->CalculateExperience(this->GetSpecies()->GetGrowthRate(), maxLevel); } - if (_battle.HasValue()) { - _battle.GetValue()->TriggerEventListener(this, _experience, exp); + if (_battleData.Battle.HasValue()) { + _battleData.Battle.GetValue()->TriggerEventListener(this, _experience, exp); } _experience = exp; _level = level; @@ -350,9 +350,9 @@ namespace CreatureLib::Battling { c->_statBoost = _statBoost; c->_flatStats = _flatStats; c->_boostedStats = _boostedStats; - c->_battle = _battle; - c->_side = _side; - c->_onBattleField = _onBattleField; + c->_battleData.Battle = _battleData.Battle; + c->_battleData.Side = _battleData.Side; + c->_battleData.OnBattleField = _battleData.OnBattleField; if (_activeTalent != nullptr) { c->_activeTalent = std::unique_ptr(_activeTalent->Clone()); } diff --git a/src/Battling/Models/Creature.hpp b/src/Battling/Models/Creature.hpp index cf80250..fd6efc4 100644 --- a/src/Battling/Models/Creature.hpp +++ b/src/Battling/Models/Creature.hpp @@ -39,25 +39,28 @@ namespace CreatureLib::Battling { Library::StatisticSet _flatStats; Library::StatisticSet _boostedStats; - ArbUt::OptionalBorrowedPtr _battle = nullptr; - ArbUt::OptionalBorrowedPtr _side = nullptr; - bool _onBattleField = false; + struct BattleData { + ArbUt::OptionalBorrowedPtr Battle = nullptr; + ArbUt::OptionalBorrowedPtr Side = nullptr; + bool OnBattleField = false; + std::unordered_set> SeenOpponents; + }; + BattleData _battleData = {}; - std::string _nickname; - CreatureLib::Library::TalentIndex _talentIndex; - std::unique_ptr _activeTalent = nullptr; + std::string _nickname = {}; + CreatureLib::Library::TalentIndex _talentIndex = {}; + std::unique_ptr _activeTalent = {}; - bool _hasOverridenTalent; - ArbUt::StringView _overridenTalentName; - std::unordered_set> _seenOpponents; + bool _hasOverridenTalent = false; + ArbUt::StringView _overridenTalentName = {}; - ArbUt::OptionalUniquePtrList _attacks; - bool _allowedExperienceGain; + ArbUt::OptionalUniquePtrList _attacks = {}; + bool _allowedExperienceGain = {}; std::unique_ptr _status = nullptr; ScriptSet _volatile = {}; - std::vector _types; + std::vector _types = {}; private: void OnFaint(); @@ -106,10 +109,10 @@ namespace CreatureLib::Battling { void SetBattleData(const ArbUt::BorrowedPtr& battle, const ArbUt::BorrowedPtr& side); void ClearBattleData() noexcept; - inline const ArbUt::OptionalBorrowedPtr& GetBattle() const { return _battle; } - inline const ArbUt::OptionalBorrowedPtr& GetBattleSide() const { return _side; } - inline void SetOnBattleField(bool value) { _onBattleField = value; } - inline bool IsOnBattleField() const { return _onBattleField; } + inline const ArbUt::OptionalBorrowedPtr& GetBattle() const { return _battleData.Battle; } + inline const ArbUt::OptionalBorrowedPtr& GetBattleSide() const { return _battleData.Side; } + inline void SetOnBattleField(bool value) { _battleData.OnBattleField = value; } + inline bool IsOnBattleField() const { return _battleData.OnBattleField; } inline std::string_view GetNickname() const noexcept { return _nickname; } inline void SetNickname(std::string nickname) noexcept { _nickname = nickname; } @@ -128,8 +131,10 @@ namespace CreatureLib::Battling { void OverrideActiveTalent(const ArbUt::StringView& talent); void AddExperience(uint32_t amount); - void MarkOpponentAsSeen(ArbUt::BorrowedPtr creature) { _seenOpponents.insert(creature); } - const std::unordered_set>& GetSeenOpponents() const { return _seenOpponents; } + void MarkOpponentAsSeen(ArbUt::BorrowedPtr creature) { _battleData.SeenOpponents.insert(creature); } + const std::unordered_set>& GetSeenOpponents() const { + return _battleData.SeenOpponents; + } size_t ScriptCount() const override; void GetActiveScripts(ArbUt::List& scripts) override;