Cleanup for Creature class battle data.

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

View File

@ -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 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 (_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 we are currently in battle, use the battle random so we can get predictable events.
if (_battle.HasValue()) { if (_battleData.Battle.HasValue()) {
_gender = _species->GetRandomGender(_battle.GetValue()->GetRandom()->GetRNG()); _gender = _species->GetRandomGender(_battleData.Battle.GetValue()->GetRandom()->GetRNG());
} }
// Else create a new random. // Else create a new random.
else { else {
@ -43,8 +43,8 @@ namespace CreatureLib::Battling {
else if (_species->GetGenderRate() == -1 && _gender != CreatureLib::Library::Gender::Genderless) { else if (_species->GetGenderRate() == -1 && _gender != CreatureLib::Library::Gender::Genderless) {
_gender = CreatureLib::Library::Gender::Genderless; _gender = CreatureLib::Library::Gender::Genderless;
} }
if (_battle.HasValue()) { if (_battleData.Battle.HasValue()) {
_battle.GetValue()->TriggerEventListener<ChangeSpeciesEvent>(this, _species); _battleData.Battle.GetValue()->TriggerEventListener<ChangeSpeciesEvent>(this, _species);
} }
ChangeVariant(variant); ChangeVariant(variant);
} }
@ -73,8 +73,8 @@ namespace CreatureLib::Battling {
// TODO: consider variant specific attacks? // TODO: consider variant specific attacks?
if (_battle.HasValue()) { if (_battleData.Battle.HasValue()) {
_battle.GetValue()->TriggerEventListener<ChangeVariantEvent>(this, _variant); _battleData.Battle.GetValue()->TriggerEventListener<ChangeVariantEvent>(this, _variant);
} }
} }
@ -97,14 +97,13 @@ namespace CreatureLib::Battling {
} }
void Creature::SetBattleData(const ArbUt::BorrowedPtr<Battle>& battle, const ArbUt::BorrowedPtr<BattleSide>& side) { void Creature::SetBattleData(const ArbUt::BorrowedPtr<Battle>& battle, const ArbUt::BorrowedPtr<BattleSide>& side) {
_battle = battle.GetRaw(); _battleData.Battle = battle.GetRaw();
_side = side.GetRaw(); _battleData.Side = side.GetRaw();
this->ResetActiveScripts(); this->ResetActiveScripts();
} }
void Creature::ClearBattleData() noexcept { void Creature::ClearBattleData() noexcept {
_battle = nullptr; _battleData = {};
_side = nullptr; _battleData.SeenOpponents = {};
_seenOpponents = {};
ResetActiveScripts(); ResetActiveScripts();
} }
@ -147,19 +146,20 @@ namespace CreatureLib::Battling {
bool Creature::IsFainted() const noexcept { return this->_currentHealth == 0; } bool Creature::IsFainted() const noexcept { return this->_currentHealth == 0; }
void Creature::OnFaint() { void Creature::OnFaint() {
EnsureNotNull(_battle) EnsureNotNull(_battleData.Battle)
EnsureNotNull(_side) EnsureNotNull(_battleData.Side)
// HOOK: On Faint // HOOK: On Faint
if (_battle.HasValue()) { if (_battleData.Battle.HasValue()) {
_battle.GetValue()->TriggerEventListener<FaintEvent>(this); _battleData.Battle.GetValue()->TriggerEventListener<FaintEvent>(this);
} }
_library->GetExperienceLibrary()->HandleExperienceGain(this, _seenOpponents); _library->GetExperienceLibrary()->HandleExperienceGain(this, _battleData.SeenOpponents);
if (_battle.HasValue() && _side.HasValue()) { if (_battleData.Battle.HasValue() && _battleData.Side.HasValue()) {
auto sideIndex = _side.GetValue()->GetCreatureIndex(this); auto sideIndex = _battleData.Side.GetValue()->GetCreatureIndex(this);
if (!_battle.GetValue()->CanSlotBeFilled(_side.GetValue()->GetSideIndex(), sideIndex)) { if (!_battleData.Battle.GetValue()->CanSlotBeFilled(_battleData.Side.GetValue()->GetSideIndex(),
_side.GetValue()->MarkSlotAsUnfillable(this); 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 { size_t Creature::ScriptCount() const {
auto c = 3; auto c = 3;
if (_side.HasValue()) { if (_battleData.Side.HasValue()) {
c += _side.GetValue()->ScriptCount(); c += _battleData.Side.GetValue()->ScriptCount();
} }
return c; return c;
} }
@ -233,8 +233,8 @@ namespace CreatureLib::Battling {
scripts.Append(ScriptWrapper::FromScript(&_activeTalent)); scripts.Append(ScriptWrapper::FromScript(&_activeTalent));
scripts.Append(ScriptWrapper::FromScript(&_status)); scripts.Append(ScriptWrapper::FromScript(&_status));
scripts.Append(ScriptWrapper::FromSet(&_volatile)); scripts.Append(ScriptWrapper::FromSet(&_volatile));
if (_side.HasValue()) { if (_battleData.Side.HasValue()) {
_side.GetValue()->GetActiveScripts(scripts); _battleData.Side.GetValue()->GetActiveScripts(scripts);
} }
} }
void Creature::ClearVolatileScripts() { _volatile.Clear(); } void Creature::ClearVolatileScripts() { _volatile.Clear(); }
@ -248,8 +248,8 @@ namespace CreatureLib::Battling {
if (level >= maxLevel) { if (level >= maxLevel) {
exp = _library->GetGrowthRateLibrary()->CalculateExperience(this->GetSpecies()->GetGrowthRate(), maxLevel); exp = _library->GetGrowthRateLibrary()->CalculateExperience(this->GetSpecies()->GetGrowthRate(), maxLevel);
} }
if (_battle.HasValue()) { if (_battleData.Battle.HasValue()) {
_battle.GetValue()->TriggerEventListener<ExperienceGainEvent>(this, _experience, exp); _battleData.Battle.GetValue()->TriggerEventListener<ExperienceGainEvent>(this, _experience, exp);
} }
_experience = exp; _experience = exp;
_level = level; _level = level;
@ -350,9 +350,9 @@ namespace CreatureLib::Battling {
c->_statBoost = _statBoost; c->_statBoost = _statBoost;
c->_flatStats = _flatStats; c->_flatStats = _flatStats;
c->_boostedStats = _boostedStats; c->_boostedStats = _boostedStats;
c->_battle = _battle; c->_battleData.Battle = _battleData.Battle;
c->_side = _side; c->_battleData.Side = _battleData.Side;
c->_onBattleField = _onBattleField; c->_battleData.OnBattleField = _battleData.OnBattleField;
if (_activeTalent != nullptr) { if (_activeTalent != nullptr) {
c->_activeTalent = std::unique_ptr<BattleScript>(_activeTalent->Clone()); c->_activeTalent = std::unique_ptr<BattleScript>(_activeTalent->Clone());
} }

View File

@ -39,25 +39,28 @@ namespace CreatureLib::Battling {
Library::StatisticSet<uint32_t> _flatStats; Library::StatisticSet<uint32_t> _flatStats;
Library::StatisticSet<uint32_t> _boostedStats; Library::StatisticSet<uint32_t> _boostedStats;
ArbUt::OptionalBorrowedPtr<Battle> _battle = nullptr; struct BattleData {
ArbUt::OptionalBorrowedPtr<BattleSide> _side = nullptr; ArbUt::OptionalBorrowedPtr<Battle> Battle = nullptr;
bool _onBattleField = false; ArbUt::OptionalBorrowedPtr<BattleSide> Side = nullptr;
bool OnBattleField = false;
std::unordered_set<ArbUt::BorrowedPtr<Creature>> SeenOpponents;
};
BattleData _battleData = {};
std::string _nickname; std::string _nickname = {};
CreatureLib::Library::TalentIndex _talentIndex; CreatureLib::Library::TalentIndex _talentIndex = {};
std::unique_ptr<BattleScript> _activeTalent = nullptr; std::unique_ptr<BattleScript> _activeTalent = {};
bool _hasOverridenTalent; bool _hasOverridenTalent = false;
ArbUt::StringView _overridenTalentName; ArbUt::StringView _overridenTalentName = {};
std::unordered_set<ArbUt::BorrowedPtr<Creature>> _seenOpponents;
ArbUt::OptionalUniquePtrList<LearnedAttack> _attacks; ArbUt::OptionalUniquePtrList<LearnedAttack> _attacks = {};
bool _allowedExperienceGain; bool _allowedExperienceGain = {};
std::unique_ptr<BattleScript> _status = nullptr; std::unique_ptr<BattleScript> _status = nullptr;
ScriptSet _volatile = {}; ScriptSet _volatile = {};
std::vector<u8> _types; std::vector<u8> _types = {};
private: private:
void OnFaint(); void OnFaint();
@ -106,10 +109,10 @@ namespace CreatureLib::Battling {
void SetBattleData(const ArbUt::BorrowedPtr<Battle>& battle, const ArbUt::BorrowedPtr<BattleSide>& side); void SetBattleData(const ArbUt::BorrowedPtr<Battle>& battle, const ArbUt::BorrowedPtr<BattleSide>& side);
void ClearBattleData() noexcept; void ClearBattleData() noexcept;
inline const ArbUt::OptionalBorrowedPtr<Battle>& GetBattle() const { return _battle; } inline const ArbUt::OptionalBorrowedPtr<Battle>& GetBattle() const { return _battleData.Battle; }
inline const ArbUt::OptionalBorrowedPtr<BattleSide>& GetBattleSide() const { return _side; } inline const ArbUt::OptionalBorrowedPtr<BattleSide>& GetBattleSide() const { return _battleData.Side; }
inline void SetOnBattleField(bool value) { _onBattleField = value; } inline void SetOnBattleField(bool value) { _battleData.OnBattleField = value; }
inline bool IsOnBattleField() const { return _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; }
inline void SetNickname(std::string nickname) noexcept { _nickname = nickname; } inline void SetNickname(std::string nickname) noexcept { _nickname = nickname; }
@ -128,8 +131,10 @@ namespace CreatureLib::Battling {
void OverrideActiveTalent(const ArbUt::StringView& talent); void OverrideActiveTalent(const ArbUt::StringView& talent);
void AddExperience(uint32_t amount); void AddExperience(uint32_t amount);
void MarkOpponentAsSeen(ArbUt::BorrowedPtr<Creature> creature) { _seenOpponents.insert(creature); } void MarkOpponentAsSeen(ArbUt::BorrowedPtr<Creature> creature) { _battleData.SeenOpponents.insert(creature); }
const std::unordered_set<ArbUt::BorrowedPtr<Creature>>& GetSeenOpponents() const { return _seenOpponents; } const std::unordered_set<ArbUt::BorrowedPtr<Creature>>& GetSeenOpponents() const {
return _battleData.SeenOpponents;
}
size_t ScriptCount() const override; size_t ScriptCount() const override;
void GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) override; void GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) override;