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 (_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<ChangeSpeciesEvent>(this, _species);
if (_battleData.Battle.HasValue()) {
_battleData.Battle.GetValue()->TriggerEventListener<ChangeSpeciesEvent>(this, _species);
}
ChangeVariant(variant);
}
@ -73,8 +73,8 @@ namespace CreatureLib::Battling {
// TODO: consider variant specific attacks?
if (_battle.HasValue()) {
_battle.GetValue()->TriggerEventListener<ChangeVariantEvent>(this, _variant);
if (_battleData.Battle.HasValue()) {
_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) {
_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<FaintEvent>(this);
if (_battleData.Battle.HasValue()) {
_battleData.Battle.GetValue()->TriggerEventListener<FaintEvent>(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<ExperienceGainEvent>(this, _experience, exp);
if (_battleData.Battle.HasValue()) {
_battleData.Battle.GetValue()->TriggerEventListener<ExperienceGainEvent>(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<BattleScript>(_activeTalent->Clone());
}

View File

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