Overhaul memory model to new Arbutils memory.
Some checks failed
continuous-integration/drone/push Build is failing

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
2020-12-12 12:22:48 +01:00
parent 1dc3aafd33
commit 5c39694f19
33 changed files with 279 additions and 211 deletions

View File

@@ -17,10 +17,6 @@ namespace CreatureLib::Battling {
_uniqueIdentifier(uid), _gender(gender), _coloring(coloring), _heldItem(heldItem),
_nickname(std::move(nickname)), _talentIndex(talent), _hasOverridenTalent(false), _attacks(attacks),
_allowedExperienceGain(allowedExperienceGain) {
AssertNotNull(library)
AssertNotNull(species)
AssertNotNull(variant)
_activeTalent = std::unique_ptr<Script>(_library->LoadScript(ScriptCategory::Talent, GetActiveTalent()));
for (auto t : _variant->GetTypes()) {
_types.push_back(t);
@@ -36,8 +32,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.IsNull()) {
_gender = _species->GetRandomGender(_battle->GetRandom()->GetRNG());
if (_battle.HasValue()) {
_gender = _species->GetRandomGender(_battle.GetValue()->GetRandom()->GetRNG());
}
// Else create a new random.
else {
@@ -48,8 +44,8 @@ namespace CreatureLib::Battling {
else if (_species->GetGenderRate() == -1 && _gender != CreatureLib::Library::Gender::Genderless) {
_gender = CreatureLib::Library::Gender::Genderless;
}
if (_battle != nullptr) {
_battle->TriggerEventListener<ChangeSpeciesEvent>(this, _species);
if (_battle.HasValue()) {
_battle.GetValue()->TriggerEventListener<ChangeSpeciesEvent>(this, _species);
}
ChangeVariant(variant);
}
@@ -79,8 +75,8 @@ namespace CreatureLib::Battling {
// TODO: consider variant specific attacks?
if (_battle != nullptr) {
_battle->TriggerEventListener<ChangeVariantEvent>(this, _variant);
if (_battle.HasValue()) {
_battle.GetValue()->TriggerEventListener<ChangeVariantEvent>(this, _variant);
}
}
@@ -102,9 +98,9 @@ namespace CreatureLib::Battling {
return _variant->GetTalent(_talentIndex);
}
void Creature::SetBattleData(ArbUt::BorrowedPtr<Battle> battle, ArbUt::BorrowedPtr<BattleSide> side) {
_battle = battle;
_side = side;
void Creature::SetBattleData(const ArbUt::BorrowedPtr<Battle>& battle, const ArbUt::BorrowedPtr<BattleSide>& side) {
_battle = battle.GetRaw();
_side = side.GetRaw();
this->ResetActiveScripts();
}
@@ -142,9 +138,9 @@ namespace CreatureLib::Battling {
// endregion
const ArbUt::BorrowedPtr<Battle>& Creature::GetBattle() const { return _battle; }
const ArbUt::OptionalBorrowedPtr<Battle>& Creature::GetBattle() const { return _battle; }
const ArbUt::BorrowedPtr<BattleSide>& Creature::GetBattleSide() const { return _side; }
const ArbUt::OptionalBorrowedPtr<BattleSide>& Creature::GetBattleSide() const { return _side; }
bool Creature::IsFainted() const noexcept { return this->_currentHealth == 0; }
@@ -152,15 +148,17 @@ namespace CreatureLib::Battling {
AssertNotNull(_battle)
AssertNotNull(_side)
// HOOK: On Faint
if (_battle != nullptr) {
_battle->TriggerEventListener<FaintEvent>(this);
if (_battle.HasValue()) {
_battle.GetValue()->TriggerEventListener<FaintEvent>(this);
}
_library->GetExperienceLibrary()->HandleExperienceGain(this, _seenOpponents);
auto sideIndex = _side->GetCreatureIndex(this);
if (!_battle->CanSlotBeFilled(_side->GetSideIndex(), sideIndex)) {
_side->MarkSlotAsUnfillable(this);
if (_battle.HasValue() && _side.HasValue()) {
auto sideIndex = _side.GetValue()->GetCreatureIndex(this);
if (!_battle.GetValue()->CanSlotBeFilled(_side.GetValue()->GetSideIndex(), sideIndex)) {
_side.GetValue()->MarkSlotAsUnfillable(this);
}
_battle.GetValue()->ValidateBattleState();
}
_battle->ValidateBattleState();
}
void Creature::Damage(uint32_t damage, DamageSource source) {
@@ -172,8 +170,8 @@ namespace CreatureLib::Battling {
// HOOK: On Damage
auto newHealth = _currentHealth - damage;
auto battle = this->GetBattle();
if (battle != nullptr) {
battle->TriggerEventListener<DamageEvent>(this, source, _currentHealth, newHealth);
if (battle.HasValue()) {
battle.GetValue()->TriggerEventListener<DamageEvent>(this, source, _currentHealth, newHealth);
}
_currentHealth = newHealth;
@@ -192,8 +190,8 @@ namespace CreatureLib::Battling {
// HOOK: On Heal
auto newHealth = _currentHealth + amount;
auto battle = this->GetBattle();
if (battle != nullptr) {
battle->TriggerEventListener<HealEvent>(this, _currentHealth, newHealth);
if (battle.HasValue()) {
battle.GetValue()->TriggerEventListener<HealEvent>(this, _currentHealth, newHealth);
}
_currentHealth = newHealth;
}
@@ -223,8 +221,8 @@ namespace CreatureLib::Battling {
size_t Creature::ScriptCount() const {
auto c = 3;
if (_side != nullptr) {
c += _side->ScriptCount();
if (_side.HasValue()) {
c += _side.GetValue()->ScriptCount();
}
return c;
}
@@ -233,8 +231,8 @@ namespace CreatureLib::Battling {
scripts.Append(ScriptWrapper::FromScript(&_activeTalent));
scripts.Append(ScriptWrapper::FromScript(&_status));
scripts.Append(ScriptWrapper::FromSet(&_volatile));
if (_side != nullptr) {
_side->GetActiveScripts(scripts);
if (_side.HasValue()) {
_side.GetValue()->GetActiveScripts(scripts);
}
}
void Creature::ClearVolatileScripts() { _volatile.Clear(); }
@@ -248,22 +246,22 @@ namespace CreatureLib::Battling {
if (level >= maxLevel) {
exp = _library->GetGrowthRateLibrary()->CalculateExperience(this->GetSpecies()->GetGrowthRate(), maxLevel);
}
if (_battle != nullptr) {
_battle->TriggerEventListener<ExperienceGainEvent>(this, _experience, exp);
if (_battle.HasValue()) {
_battle.GetValue()->TriggerEventListener<ExperienceGainEvent>(this, _experience, exp);
}
_experience = exp;
_level = level;
}
ArbUt::BorrowedPtr<const Library::CreatureSpecies> Creature::GetDisplaySpecies() const noexcept {
ArbUt::OptionalBorrowedPtr<const Library::CreatureSpecies> Creature::GetDisplaySpecies() const noexcept {
auto species = _displaySpecies;
if (species == nullptr)
species = _species;
if (!species.HasValue())
species = _species.GetRaw();
return species;
}
ArbUt::BorrowedPtr<const Library::SpeciesVariant> Creature::GetDisplayVariant() const noexcept {
ArbUt::OptionalBorrowedPtr<const Library::SpeciesVariant> Creature::GetDisplayVariant() const noexcept {
auto variant = _displayVariant;
if (variant == nullptr)
variant = _variant;
if (!variant.HasValue())
variant = _variant.GetRaw();
return variant;
}
void Creature::SetHeldItem(const ArbUt::BasicStringView& itemName) {
@@ -283,15 +281,15 @@ namespace CreatureLib::Battling {
void Creature::AddVolatileScript(const ArbUt::StringView& name) {
auto script = _volatile.Get(name);
if (script != nullptr) {
script->Stack();
if (script.has_value()) {
script.value()->Stack();
return;
}
script = this->_library->LoadScript(ScriptCategory::Creature, name);
if (script == nullptr) {
if (!script.has_value()) {
THROW("Invalid volatile script requested for creature: '" << name.c_str() << "'.");
}
_volatile.Add(script.GetRaw());
_volatile.Add(script.value().GetRaw());
}
void Creature::AddVolatileScript(Script* script) { _volatile.Add(script); }