From 188e88ab80a8604e1193676e9e7711af133825e2 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 8 May 2021 11:53:32 +0200 Subject: [PATCH] Store CreatureIndex on Creature, includes helper function to get a creatures party from a battle. Signed-off-by: Deukhoofd --- src/Battling/Models/Battle.cpp | 20 ++++++++++++++++++++ src/Battling/Models/Battle.hpp | 2 ++ src/Battling/Models/BattleSide.cpp | 5 +++-- src/Battling/Models/Creature.cpp | 1 + src/Battling/Models/Creature.hpp | 9 ++++++--- src/Battling/Models/CreatureIndex.hpp | 2 -- src/Battling/Models/CreatureParty.hpp | 3 +++ 7 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/Battling/Models/Battle.cpp b/src/Battling/Models/Battle.cpp index 8074d52..432f226 100644 --- a/src/Battling/Models/Battle.cpp +++ b/src/Battling/Models/Battle.cpp @@ -209,3 +209,23 @@ Battle* Battle::Clone() const { return battle; } +ArbUt::OptionalBorrowedPtr Battle::FindPartyForCreature(const ArbUt::BorrowedPtr& creature) { + if (creature->IsOnBattleField()) { + auto index = creature->GetBattleIndex(); + for (auto& party : _parties) { + if (!party->IsResponsibleForIndex(index)) { + continue; + } + if (party->GetParty()->HasCreature(creature)) { + return party; + } + } + } else { + for (auto& party : _parties) { + if (party->GetParty()->HasCreature(creature)) { + return party; + } + } + } + return {}; +} \ No newline at end of file diff --git a/src/Battling/Models/Battle.hpp b/src/Battling/Models/Battle.hpp index 7e509c6..a7ab720 100644 --- a/src/Battling/Models/Battle.hpp +++ b/src/Battling/Models/Battle.hpp @@ -85,6 +85,8 @@ namespace CreatureLib::Battling { inline const BattleResult& GetResult() const noexcept { return _battleResult; } const ArbUt::UniquePtrList& GetParties() const noexcept { return _parties; } + ArbUt::OptionalBorrowedPtr FindPartyForCreature(const ArbUt::BorrowedPtr&); + const ArbUt::UniquePtrList& GetSides() const noexcept { return _sides; } ArbUt::OptionalBorrowedPtr GetVolatileScript(const ArbUt::StringView& key) const { return _volatile.Get(key); diff --git a/src/Battling/Models/BattleSide.cpp b/src/Battling/Models/BattleSide.cpp index 894712f..f9543ac 100644 --- a/src/Battling/Models/BattleSide.cpp +++ b/src/Battling/Models/BattleSide.cpp @@ -60,6 +60,7 @@ void BattleSide::SetCreature(ArbUt::OptionalBorrowedPtr creature, uint } creature.GetValue()->SetBattleData(_battle, this); creature.GetValue()->SetOnBattleField(true); + creature.GetValue()->SetBattleIndex(CreatureIndex(_index, index)); for (auto* side : _battle->GetSides()) { if (side == this) { continue; @@ -124,8 +125,8 @@ bool BattleSide::SwapPositions(u8 a, u8 b) { return true; } -BattleSide *BattleSide::CloneWithoutCreatures(ArbUt::BorrowedPtr battle) const { - auto *side = new BattleSide(_index, battle, _creaturesPerSide); +BattleSide* BattleSide::CloneWithoutCreatures(ArbUt::BorrowedPtr battle) const { + auto* side = new BattleSide(_index, battle, _creaturesPerSide); side->_choicesSet = _choicesSet; _volatile.Clone(side->_volatile); side->_hasFled = _hasFled; diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index e3459f3..70675f5 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -353,6 +353,7 @@ namespace CreatureLib::Battling { c->_battleData.Battle = _battleData.Battle; c->_battleData.Side = _battleData.Side; c->_battleData.OnBattleField = _battleData.OnBattleField; + c->_battleData.Index = _battleData.Index; 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 fd6efc4..5b466d4 100644 --- a/src/Battling/Models/Creature.hpp +++ b/src/Battling/Models/Creature.hpp @@ -8,6 +8,7 @@ #include "../ScriptHandling/ScriptAggregator.hpp" #include "../ScriptHandling/ScriptSet.hpp" #include "../ScriptHandling/ScriptSource.hpp" +#include "CreatureIndex.hpp" #include "DamageSource.hpp" #include "LearnedAttack.hpp" @@ -40,12 +41,12 @@ namespace CreatureLib::Battling { Library::StatisticSet _boostedStats; struct BattleData { + bool OnBattleField = false; ArbUt::OptionalBorrowedPtr Battle = nullptr; ArbUt::OptionalBorrowedPtr Side = nullptr; - bool OnBattleField = false; + CreatureIndex Index = {}; std::unordered_set> SeenOpponents; - }; - BattleData _battleData = {}; + } _battleData = {}; std::string _nickname = {}; CreatureLib::Library::TalentIndex _talentIndex = {}; @@ -112,6 +113,8 @@ namespace CreatureLib::Battling { 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 void SetBattleIndex(const CreatureIndex& index) { _battleData.Index = index; } + inline const CreatureIndex& GetBattleIndex() const noexcept { return _battleData.Index; } inline bool IsOnBattleField() const { return _battleData.OnBattleField; } inline std::string_view GetNickname() const noexcept { return _nickname; } diff --git a/src/Battling/Models/CreatureIndex.hpp b/src/Battling/Models/CreatureIndex.hpp index 404049d..fcdb394 100644 --- a/src/Battling/Models/CreatureIndex.hpp +++ b/src/Battling/Models/CreatureIndex.hpp @@ -1,8 +1,6 @@ #ifndef CREATURELIB_CREATUREINDEX_HPP #define CREATURELIB_CREATUREINDEX_HPP -#include "Creature.hpp" - namespace CreatureLib::Battling { class CreatureIndex { uint8_t _side; diff --git a/src/Battling/Models/CreatureParty.hpp b/src/Battling/Models/CreatureParty.hpp index 156b4f9..0d71f59 100644 --- a/src/Battling/Models/CreatureParty.hpp +++ b/src/Battling/Models/CreatureParty.hpp @@ -63,6 +63,9 @@ namespace CreatureLib::Battling { } } } + inline bool HasCreature(ArbUt::BorrowedPtr creature) const noexcept { + return _party.Contains(creature); + } virtual CreatureParty* Clone() const { auto party = new CreatureParty(_party.Count());