2019-11-28 11:55:22 +00:00
|
|
|
#include "Creature.hpp"
|
2019-11-05 13:31:54 +00:00
|
|
|
#include <algorithm>
|
2019-11-10 16:08:42 +00:00
|
|
|
#include <utility>
|
2019-10-24 11:37:55 +00:00
|
|
|
#include "../Models/Battle.hpp"
|
2020-02-19 14:32:43 +00:00
|
|
|
#include "../ScriptHandling/ScriptMacros.hpp"
|
2019-10-24 11:37:55 +00:00
|
|
|
|
|
|
|
using namespace CreatureLib;
|
|
|
|
|
2020-06-02 14:20:47 +00:00
|
|
|
Battling::Creature::Creature(ArbUt::BorrowedPtr<const BattleLibrary> library,
|
2020-05-26 16:31:06 +00:00
|
|
|
const ArbUt::BorrowedPtr<const Library::CreatureSpecies>& species,
|
|
|
|
const ArbUt::BorrowedPtr<const Library::SpeciesVariant>& variant, uint8_t level,
|
2020-05-26 12:35:18 +00:00
|
|
|
uint32_t experience, uint32_t uid, Library::Gender gender, uint8_t coloring,
|
2020-07-19 10:15:14 +00:00
|
|
|
const ArbUt::BorrowedPtr<const Library::Item> heldItem, const std::string& nickname,
|
2020-05-31 15:26:39 +00:00
|
|
|
const Library::TalentIndex& talent, const std::vector<LearnedAttack*>& attacks,
|
2020-05-26 12:35:18 +00:00
|
|
|
bool allowedExperienceGain)
|
2020-01-12 16:04:42 +00:00
|
|
|
: _library(library), _species(species), _variant(variant), _level(level), _experience(experience),
|
|
|
|
_uniqueIdentifier(uid), _gender(gender), _coloring(coloring), _heldItem(heldItem), _nickname(std::move(nickname)),
|
2020-05-20 13:18:27 +00:00
|
|
|
_talentIndex(talent), _hasOverridenTalent(false), _attacks(attacks),
|
|
|
|
_allowedExperienceGain(allowedExperienceGain) {
|
2020-03-22 12:42:26 +00:00
|
|
|
AssertNotNull(library)
|
|
|
|
AssertNotNull(species)
|
|
|
|
AssertNotNull(variant)
|
2020-02-27 17:23:23 +00:00
|
|
|
|
2020-06-02 11:43:44 +00:00
|
|
|
_activeTalent = std::unique_ptr<Script>(_library->LoadScript(ScriptCategory::Talent, GetActiveTalent()));
|
2020-02-27 17:23:23 +00:00
|
|
|
if (_nickname.empty()) {
|
|
|
|
_nickname = species->GetName().std_str();
|
|
|
|
}
|
2020-06-05 14:38:27 +00:00
|
|
|
for (auto t : _variant->GetTypes()) {
|
|
|
|
_types.insert(t);
|
|
|
|
}
|
2019-11-18 17:41:55 +00:00
|
|
|
}
|
2019-10-24 11:37:55 +00:00
|
|
|
|
2020-06-10 12:39:20 +00:00
|
|
|
void Battling::Creature::ChangeVariant(ArbUt::BorrowedPtr<const Library::SpeciesVariant> variant) {
|
|
|
|
AssertNotNull(variant)
|
|
|
|
_variant = variant;
|
|
|
|
|
|
|
|
// Set the types to the new variant.
|
|
|
|
_types.clear();
|
|
|
|
for (auto t : variant->GetTypes()) {
|
|
|
|
_types.insert(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the new active talent.
|
|
|
|
_activeTalent =
|
|
|
|
std::unique_ptr<CreatureLib::Battling::Script>(_library->LoadScript(ScriptCategory::Talent, GetActiveTalent()));
|
|
|
|
|
|
|
|
// We modify the health of the creature by the change in its max health.
|
|
|
|
auto prevHealth = GetBoostedStat(CreatureLib::Library::Statistic::Health);
|
|
|
|
RecalculateFlatStats();
|
|
|
|
int32_t diffHealth = GetBoostedStat(CreatureLib::Library::Statistic::Health) - prevHealth;
|
2020-06-10 12:46:45 +00:00
|
|
|
if (_currentHealth < static_cast<uint32_t>(INT32_MAX) && static_cast<int32_t>(_currentHealth) < -diffHealth) {
|
2020-06-10 12:39:20 +00:00
|
|
|
_currentHealth = 0;
|
|
|
|
} else {
|
|
|
|
_currentHealth += diffHealth;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: consider variant specific attacks?
|
|
|
|
}
|
|
|
|
|
2020-03-05 10:25:41 +00:00
|
|
|
void Battling::Creature::ChangeLevelBy(int8_t amount) {
|
2020-03-25 18:07:36 +00:00
|
|
|
auto level = _level + amount;
|
|
|
|
if (level > _library->GetSettings()->GetMaximalLevel())
|
|
|
|
level = _library->GetSettings()->GetMaximalLevel();
|
|
|
|
if (level < 1)
|
|
|
|
level = 1;
|
|
|
|
_level = level;
|
2020-03-05 10:25:41 +00:00
|
|
|
_experience = _library->GetGrowthRateLibrary()->CalculateExperience(_species->GetGrowthRate(), _level);
|
2019-10-24 11:37:55 +00:00
|
|
|
RecalculateFlatStats();
|
|
|
|
}
|
|
|
|
|
2020-06-26 15:08:23 +00:00
|
|
|
const ArbUt::StringView& Battling::Creature::GetActiveTalent() const {
|
2019-11-28 11:55:22 +00:00
|
|
|
if (_hasOverridenTalent) {
|
2019-11-18 17:41:55 +00:00
|
|
|
return _overridenTalentName;
|
|
|
|
}
|
2020-01-12 16:04:42 +00:00
|
|
|
return _variant->GetTalent(_talentIndex);
|
2019-10-24 11:37:55 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 16:02:37 +00:00
|
|
|
void Battling::Creature::SetBattleData(ArbUt::BorrowedPtr<Battle> battle, ArbUt::BorrowedPtr<BattleSide> side) {
|
2019-10-29 10:19:25 +00:00
|
|
|
_battle = battle;
|
|
|
|
_side = side;
|
2020-02-17 09:05:32 +00:00
|
|
|
this->ResetActiveScripts();
|
2019-10-29 10:19:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
// region Stat APIs
|
2019-10-24 11:37:55 +00:00
|
|
|
|
2020-04-22 19:48:00 +00:00
|
|
|
bool Battling::Creature::ChangeStatBoost(Library::Statistic stat, int8_t diffAmount) {
|
|
|
|
bool changed = false;
|
2019-10-24 11:37:55 +00:00
|
|
|
if (diffAmount > 0)
|
2020-04-22 19:48:00 +00:00
|
|
|
changed = this->_statBoost.IncreaseStatBy(stat, diffAmount);
|
2019-10-24 11:37:55 +00:00
|
|
|
else
|
2020-04-22 19:48:00 +00:00
|
|
|
changed = this->_statBoost.DecreaseStatBy(stat, -diffAmount);
|
2019-10-24 11:37:55 +00:00
|
|
|
this->RecalculateBoostedStat(stat);
|
2020-04-22 19:48:00 +00:00
|
|
|
return changed;
|
2019-10-24 11:37:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Battling::Creature::RecalculateFlatStats() {
|
2020-05-26 13:11:04 +00:00
|
|
|
auto& statCalc = this->_library->GetStatCalculator();
|
2019-12-05 11:56:41 +00:00
|
|
|
this->_flatStats = statCalc->CalculateFlatStats(this);
|
2019-10-24 11:37:55 +00:00
|
|
|
RecalculateBoostedStats();
|
|
|
|
}
|
|
|
|
void Battling::Creature::RecalculateBoostedStats() {
|
|
|
|
this->_boostedStats = this->_library->GetStatCalculator()->CalculateFlatStats(this);
|
|
|
|
}
|
|
|
|
|
2020-03-22 09:11:53 +00:00
|
|
|
void Battling::Creature::RecalculateFlatStat(Library::Statistic stat) {
|
2019-10-24 11:37:55 +00:00
|
|
|
auto s = this->_library->GetStatCalculator()->CalculateFlatStat(this, stat);
|
|
|
|
this->_flatStats.SetStat(stat, s);
|
|
|
|
RecalculateBoostedStat(stat);
|
|
|
|
}
|
|
|
|
|
2020-03-22 09:11:53 +00:00
|
|
|
void Battling::Creature::RecalculateBoostedStat(Library::Statistic stat) {
|
2019-10-24 11:37:55 +00:00
|
|
|
auto s = this->_library->GetStatCalculator()->CalculateBoostedStat(this, stat);
|
|
|
|
this->_boostedStats.SetStat(stat, s);
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
// endregion
|
2019-11-02 12:57:43 +00:00
|
|
|
|
2020-06-02 11:06:24 +00:00
|
|
|
const ArbUt::BorrowedPtr<CreatureLib::Battling::Battle>& Battling::Creature::GetBattle() const { return _battle; }
|
2019-10-29 10:19:25 +00:00
|
|
|
|
2020-06-02 14:20:47 +00:00
|
|
|
const ArbUt::BorrowedPtr<CreatureLib::Battling::BattleSide>& Battling::Creature::GetBattleSide() const { return _side; }
|
2019-10-29 10:19:25 +00:00
|
|
|
|
2020-04-22 19:20:07 +00:00
|
|
|
bool Battling::Creature::IsFainted() const noexcept { return this->_currentHealth == 0; }
|
2019-11-03 12:47:50 +00:00
|
|
|
|
2019-12-14 12:28:23 +00:00
|
|
|
void Battling::Creature::OnFaint() {
|
2020-08-02 07:52:11 +00:00
|
|
|
AssertNotNull(_battle)
|
|
|
|
AssertNotNull(_side)
|
2019-12-14 12:28:23 +00:00
|
|
|
// HOOK: On Faint
|
2020-03-09 15:29:41 +00:00
|
|
|
if (_battle != nullptr) {
|
2020-07-31 14:19:39 +00:00
|
|
|
_battle->TriggerEventListener<FaintEvent>(this);
|
2020-03-09 15:29:41 +00:00
|
|
|
}
|
2019-12-14 12:28:23 +00:00
|
|
|
_library->GetExperienceLibrary()->HandleExperienceGain(this, _seenOpponents);
|
2020-04-04 15:03:06 +00:00
|
|
|
auto sideIndex = _side->GetCreatureIndex(this);
|
|
|
|
if (!_battle->CanSlotBeFilled(_side->GetSideIndex(), sideIndex)) {
|
2019-12-14 12:28:23 +00:00
|
|
|
_side->MarkSlotAsUnfillable(this);
|
|
|
|
}
|
|
|
|
_battle->ValidateBattleState();
|
|
|
|
}
|
|
|
|
|
2019-11-03 12:47:50 +00:00
|
|
|
void Battling::Creature::Damage(uint32_t damage, Battling::DamageSource source) {
|
2020-01-12 16:04:42 +00:00
|
|
|
if (damage > _currentHealth) {
|
|
|
|
damage = _currentHealth;
|
2019-11-03 12:47:50 +00:00
|
|
|
}
|
2020-04-22 19:20:07 +00:00
|
|
|
if (damage == 0)
|
|
|
|
return;
|
2019-11-03 12:47:50 +00:00
|
|
|
// HOOK: On Damage
|
2020-01-12 16:04:42 +00:00
|
|
|
auto newHealth = _currentHealth - damage;
|
2020-02-02 11:34:02 +00:00
|
|
|
auto battle = this->GetBattle();
|
|
|
|
if (battle != nullptr) {
|
2020-07-31 14:19:39 +00:00
|
|
|
battle->TriggerEventListener<DamageEvent>(this, source, _currentHealth, newHealth);
|
2020-02-02 11:34:02 +00:00
|
|
|
}
|
2020-01-12 16:04:42 +00:00
|
|
|
_currentHealth = newHealth;
|
2019-12-07 20:56:29 +00:00
|
|
|
|
2020-08-02 07:52:11 +00:00
|
|
|
if (IsFainted() && damage > 0 && battle != nullptr) {
|
2019-12-14 12:28:23 +00:00
|
|
|
OnFaint();
|
2019-12-07 20:56:29 +00:00
|
|
|
}
|
2019-11-03 12:47:50 +00:00
|
|
|
}
|
2019-11-05 07:06:12 +00:00
|
|
|
|
2020-03-09 15:29:41 +00:00
|
|
|
void Battling::Creature::Heal(uint32_t amount, bool canRevive) {
|
|
|
|
if (_currentHealth == 0 && !canRevive) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-13 13:48:09 +00:00
|
|
|
if (amount > GetMaxHealth() - _currentHealth) {
|
|
|
|
amount = GetMaxHealth() - _currentHealth;
|
|
|
|
}
|
|
|
|
// HOOK: On Heal
|
|
|
|
auto newHealth = _currentHealth + amount;
|
|
|
|
auto battle = this->GetBattle();
|
|
|
|
if (battle != nullptr) {
|
2020-07-31 14:19:39 +00:00
|
|
|
battle->TriggerEventListener<HealEvent>(this, _currentHealth, newHealth);
|
2020-02-13 13:48:09 +00:00
|
|
|
}
|
|
|
|
_currentHealth = newHealth;
|
|
|
|
}
|
|
|
|
|
2020-06-26 15:08:23 +00:00
|
|
|
void Battling::Creature::OverrideActiveTalent(const ArbUt::StringView& talent) {
|
2019-11-18 17:41:55 +00:00
|
|
|
_hasOverridenTalent = true;
|
2020-08-01 08:27:46 +00:00
|
|
|
if (_activeTalent != nullptr) {
|
|
|
|
_activeTalent->OnRemove();
|
|
|
|
_activeTalent.reset(this->_library->LoadScript(ScriptCategory::Talent, talent));
|
|
|
|
}
|
2019-11-18 17:41:55 +00:00
|
|
|
_overridenTalentName = talent;
|
|
|
|
}
|
|
|
|
|
2020-06-05 14:38:27 +00:00
|
|
|
const std::unordered_set<uint8_t>& Battling::Creature::GetTypes() const noexcept { return _types; }
|
2019-11-05 13:31:54 +00:00
|
|
|
|
2020-03-22 12:42:26 +00:00
|
|
|
bool Battling::Creature::HasType(uint8_t type) const noexcept {
|
2020-06-05 14:38:27 +00:00
|
|
|
return std::find(_types.begin(), _types.end(), type) != _types.end();
|
2019-11-05 13:31:54 +00:00
|
|
|
}
|
2019-11-10 13:32:05 +00:00
|
|
|
|
2020-04-25 09:33:25 +00:00
|
|
|
size_t Battling::Creature::ScriptCount() const {
|
|
|
|
auto c = 3;
|
|
|
|
if (_side != nullptr) {
|
|
|
|
c += _side->ScriptCount();
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2020-05-26 16:31:06 +00:00
|
|
|
void Battling::Creature::GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) {
|
2020-04-23 21:23:58 +00:00
|
|
|
scripts.Append(ScriptWrapper::FromScript(&_activeTalent));
|
|
|
|
scripts.Append(ScriptWrapper::FromScript(&_status));
|
|
|
|
scripts.Append(ScriptWrapper::FromSet(&_volatile));
|
2020-02-17 08:57:15 +00:00
|
|
|
if (_side != nullptr) {
|
|
|
|
_side->GetActiveScripts(scripts);
|
|
|
|
}
|
2019-11-10 13:32:05 +00:00
|
|
|
}
|
2019-12-14 11:15:30 +00:00
|
|
|
void Battling::Creature::ClearVolatileScripts() { _volatile.Clear(); }
|
2019-12-14 12:28:23 +00:00
|
|
|
void Battling::Creature::AddExperience(uint32_t amount) {
|
2020-02-01 12:30:51 +00:00
|
|
|
auto maxLevel = _library->GetSettings()->GetMaximalLevel();
|
2020-01-12 16:04:42 +00:00
|
|
|
if (_level >= maxLevel) {
|
2019-12-14 12:28:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-01-12 16:04:42 +00:00
|
|
|
auto exp = _experience + amount;
|
2019-12-14 12:28:23 +00:00
|
|
|
auto level = _library->GetGrowthRateLibrary()->CalculateLevel(this->GetSpecies()->GetGrowthRate(), exp);
|
|
|
|
if (level >= maxLevel) {
|
|
|
|
exp = _library->GetGrowthRateLibrary()->CalculateExperience(this->GetSpecies()->GetGrowthRate(), maxLevel);
|
|
|
|
}
|
2020-01-12 16:04:42 +00:00
|
|
|
_experience = exp;
|
|
|
|
_level = level;
|
2019-12-14 12:28:23 +00:00
|
|
|
}
|
2020-05-26 16:31:06 +00:00
|
|
|
ArbUt::BorrowedPtr<const Library::CreatureSpecies> Battling::Creature::GetDisplaySpecies() const noexcept {
|
2020-02-03 15:49:28 +00:00
|
|
|
auto species = _displaySpecies;
|
|
|
|
if (species == nullptr)
|
|
|
|
species = _species;
|
2019-12-21 13:32:45 +00:00
|
|
|
return species;
|
|
|
|
}
|
2020-05-26 16:31:06 +00:00
|
|
|
ArbUt::BorrowedPtr<const Library::SpeciesVariant> Battling::Creature::GetDisplayVariant() const noexcept {
|
2020-02-03 15:49:28 +00:00
|
|
|
auto variant = _displayVariant;
|
|
|
|
if (variant == nullptr)
|
|
|
|
variant = _variant;
|
2019-12-21 13:32:45 +00:00
|
|
|
return variant;
|
|
|
|
}
|
2020-06-26 15:08:23 +00:00
|
|
|
void Battling::Creature::SetHeldItem(const ArbUt::BasicStringView& itemName) {
|
2020-05-26 16:31:06 +00:00
|
|
|
ArbUt::BorrowedPtr<const Library::Item> item;
|
2020-05-24 18:57:22 +00:00
|
|
|
if (!_library->GetItemLibrary()->TryGet(itemName.GetHash(), item)) {
|
2020-07-26 15:41:11 +00:00
|
|
|
THROW_CREATURE("Item not found '" << itemName.c_str() << "'.");
|
2020-02-17 16:16:28 +00:00
|
|
|
}
|
|
|
|
_heldItem = item;
|
|
|
|
}
|
2020-03-05 10:25:41 +00:00
|
|
|
void Battling::Creature::SetHeldItem(uint32_t itemNameHash) {
|
2020-05-26 16:31:06 +00:00
|
|
|
ArbUt::BorrowedPtr<const Library::Item> item;
|
2020-03-05 10:25:41 +00:00
|
|
|
if (!_library->GetItemLibrary()->TryGet(itemNameHash, item)) {
|
2020-07-26 15:41:11 +00:00
|
|
|
THROW_CREATURE("Item not found.");
|
2020-03-05 10:25:41 +00:00
|
|
|
}
|
|
|
|
_heldItem = item;
|
|
|
|
}
|
|
|
|
|
2020-06-26 16:23:40 +00:00
|
|
|
void Battling::Creature::AddVolatileScript(const ArbUt::StringView& name) {
|
2020-02-22 15:01:01 +00:00
|
|
|
auto script = _volatile.Get(name);
|
|
|
|
if (script != nullptr) {
|
|
|
|
script->Stack();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
script = this->_library->LoadScript(ScriptCategory::Creature, name);
|
2020-04-10 22:22:08 +00:00
|
|
|
if (script == nullptr) {
|
2020-07-26 15:41:11 +00:00
|
|
|
THROW_CREATURE("Invalid volatile script requested for creature: '" << name.c_str() << "'.");
|
2020-04-10 22:22:08 +00:00
|
|
|
}
|
2020-06-02 13:03:31 +00:00
|
|
|
_volatile.Add(script.GetRaw());
|
2020-02-22 14:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Battling::Creature::AddVolatileScript(Script* script) { _volatile.Add(script); }
|
2020-06-26 15:08:23 +00:00
|
|
|
void Battling::Creature::RemoveVolatileScript(const ArbUt::BasicStringView& name) { _volatile.Remove(name); }
|
2020-02-23 10:11:47 +00:00
|
|
|
void Battling::Creature::RemoveVolatileScript(Battling::Script* script) { _volatile.Remove(script->GetName()); }
|
2020-06-26 15:08:23 +00:00
|
|
|
bool Battling::Creature::HasVolatileScript(const ArbUt::BasicStringView& name) const { return _volatile.Has(name); }
|