Store CreatureIndex on Creature, includes helper function to get a creatures party from a battle.
continuous-integration/drone/push Build is passing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2021-05-08 11:53:32 +02:00
parent 305eb5efb2
commit 188e88ab80
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
7 changed files with 35 additions and 7 deletions

View File

@ -209,3 +209,23 @@ Battle* Battle::Clone() const {
return battle; return battle;
} }
ArbUt::OptionalBorrowedPtr<BattleParty> Battle::FindPartyForCreature(const ArbUt::BorrowedPtr<Creature>& 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 {};
}

View File

@ -85,6 +85,8 @@ namespace CreatureLib::Battling {
inline const BattleResult& GetResult() const noexcept { return _battleResult; } inline const BattleResult& GetResult() const noexcept { return _battleResult; }
const ArbUt::UniquePtrList<BattleParty>& GetParties() const noexcept { return _parties; } const ArbUt::UniquePtrList<BattleParty>& GetParties() const noexcept { return _parties; }
ArbUt::OptionalBorrowedPtr<BattleParty> FindPartyForCreature(const ArbUt::BorrowedPtr<Creature>&);
const ArbUt::UniquePtrList<BattleSide>& GetSides() const noexcept { return _sides; } const ArbUt::UniquePtrList<BattleSide>& GetSides() const noexcept { return _sides; }
ArbUt::OptionalBorrowedPtr<BattleScript> GetVolatileScript(const ArbUt::StringView& key) const { ArbUt::OptionalBorrowedPtr<BattleScript> GetVolatileScript(const ArbUt::StringView& key) const {
return _volatile.Get(key); return _volatile.Get(key);

View File

@ -60,6 +60,7 @@ void BattleSide::SetCreature(ArbUt::OptionalBorrowedPtr<Creature> creature, uint
} }
creature.GetValue()->SetBattleData(_battle, this); creature.GetValue()->SetBattleData(_battle, this);
creature.GetValue()->SetOnBattleField(true); creature.GetValue()->SetOnBattleField(true);
creature.GetValue()->SetBattleIndex(CreatureIndex(_index, index));
for (auto* side : _battle->GetSides()) { for (auto* side : _battle->GetSides()) {
if (side == this) { if (side == this) {
continue; continue;
@ -124,8 +125,8 @@ bool BattleSide::SwapPositions(u8 a, u8 b) {
return true; return true;
} }
BattleSide *BattleSide::CloneWithoutCreatures(ArbUt::BorrowedPtr<Battle> battle) const { BattleSide* BattleSide::CloneWithoutCreatures(ArbUt::BorrowedPtr<Battle> battle) const {
auto *side = new BattleSide(_index, battle, _creaturesPerSide); auto* side = new BattleSide(_index, battle, _creaturesPerSide);
side->_choicesSet = _choicesSet; side->_choicesSet = _choicesSet;
_volatile.Clone(side->_volatile); _volatile.Clone(side->_volatile);
side->_hasFled = _hasFled; side->_hasFled = _hasFled;

View File

@ -353,6 +353,7 @@ namespace CreatureLib::Battling {
c->_battleData.Battle = _battleData.Battle; c->_battleData.Battle = _battleData.Battle;
c->_battleData.Side = _battleData.Side; c->_battleData.Side = _battleData.Side;
c->_battleData.OnBattleField = _battleData.OnBattleField; c->_battleData.OnBattleField = _battleData.OnBattleField;
c->_battleData.Index = _battleData.Index;
if (_activeTalent != nullptr) { if (_activeTalent != nullptr) {
c->_activeTalent = std::unique_ptr<BattleScript>(_activeTalent->Clone()); c->_activeTalent = std::unique_ptr<BattleScript>(_activeTalent->Clone());
} }

View File

@ -8,6 +8,7 @@
#include "../ScriptHandling/ScriptAggregator.hpp" #include "../ScriptHandling/ScriptAggregator.hpp"
#include "../ScriptHandling/ScriptSet.hpp" #include "../ScriptHandling/ScriptSet.hpp"
#include "../ScriptHandling/ScriptSource.hpp" #include "../ScriptHandling/ScriptSource.hpp"
#include "CreatureIndex.hpp"
#include "DamageSource.hpp" #include "DamageSource.hpp"
#include "LearnedAttack.hpp" #include "LearnedAttack.hpp"
@ -40,12 +41,12 @@ namespace CreatureLib::Battling {
Library::StatisticSet<uint32_t> _boostedStats; Library::StatisticSet<uint32_t> _boostedStats;
struct BattleData { struct BattleData {
bool OnBattleField = false;
ArbUt::OptionalBorrowedPtr<Battle> Battle = nullptr; ArbUt::OptionalBorrowedPtr<Battle> Battle = nullptr;
ArbUt::OptionalBorrowedPtr<BattleSide> Side = nullptr; ArbUt::OptionalBorrowedPtr<BattleSide> Side = nullptr;
bool OnBattleField = false; CreatureIndex Index = {};
std::unordered_set<ArbUt::BorrowedPtr<Creature>> SeenOpponents; std::unordered_set<ArbUt::BorrowedPtr<Creature>> SeenOpponents;
}; } _battleData = {};
BattleData _battleData = {};
std::string _nickname = {}; std::string _nickname = {};
CreatureLib::Library::TalentIndex _talentIndex = {}; CreatureLib::Library::TalentIndex _talentIndex = {};
@ -112,6 +113,8 @@ namespace CreatureLib::Battling {
inline const ArbUt::OptionalBorrowedPtr<Battle>& GetBattle() const { return _battleData.Battle; } inline const ArbUt::OptionalBorrowedPtr<Battle>& GetBattle() const { return _battleData.Battle; }
inline const ArbUt::OptionalBorrowedPtr<BattleSide>& GetBattleSide() const { return _battleData.Side; } inline const ArbUt::OptionalBorrowedPtr<BattleSide>& GetBattleSide() const { return _battleData.Side; }
inline void SetOnBattleField(bool value) { _battleData.OnBattleField = value; } 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 bool IsOnBattleField() const { return _battleData.OnBattleField; }
inline std::string_view GetNickname() const noexcept { return _nickname; } inline std::string_view GetNickname() const noexcept { return _nickname; }

View File

@ -1,8 +1,6 @@
#ifndef CREATURELIB_CREATUREINDEX_HPP #ifndef CREATURELIB_CREATUREINDEX_HPP
#define CREATURELIB_CREATUREINDEX_HPP #define CREATURELIB_CREATUREINDEX_HPP
#include "Creature.hpp"
namespace CreatureLib::Battling { namespace CreatureLib::Battling {
class CreatureIndex { class CreatureIndex {
uint8_t _side; uint8_t _side;

View File

@ -63,6 +63,9 @@ namespace CreatureLib::Battling {
} }
} }
} }
inline bool HasCreature(ArbUt::BorrowedPtr<Creature> creature) const noexcept {
return _party.Contains(creature);
}
virtual CreatureParty* Clone() const { virtual CreatureParty* Clone() const {
auto party = new CreatureParty(_party.Count()); auto party = new CreatureParty(_party.Count());