Make ExecutingAttack use smart pointers.

This commit is contained in:
Deukhoofd 2020-06-02 18:02:37 +02:00
parent a5a613ba5c
commit 7262ae9e8b
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
5 changed files with 15 additions and 14 deletions

View File

@ -21,7 +21,7 @@ export bool CreatureLib_ExecutingAttack_IsCreatureTarget(ExecutingAttack* p, Cre
return p->IsCreatureTarget(target); return p->IsCreatureTarget(target);
} }
export Creature* CreatureLib_ExecutingAttack_GetUser(ExecutingAttack* p) { return p->GetUser(); } export Creature* CreatureLib_ExecutingAttack_GetUser(ExecutingAttack* p) { return p->GetUser().GetRaw(); }
export LearnedAttack* CreatureLib_ExecutingAttack_GetAttack(ExecutingAttack* p) { return p->GetAttack().operator->(); } export LearnedAttack* CreatureLib_ExecutingAttack_GetAttack(ExecutingAttack* p) { return p->GetAttack().operator->(); }
#define HITDATA_GET_FUNC(name, returnType) \ #define HITDATA_GET_FUNC(name, returnType) \

View File

@ -30,7 +30,7 @@ float DamageLibrary::GetStatModifier(ExecutingAttack* attack, Creature* target,
const ExecutingAttack::HitData& hitData) const { const ExecutingAttack::HitData& hitData) const {
AssertNotNull(attack) AssertNotNull(attack)
AssertNotNull(target) AssertNotNull(target)
auto user = attack->GetUser(); auto user = attack->GetUser().GetRaw();
AssertNotNull(user) AssertNotNull(user)
HOOK(ChangeDamageStatsUser, attack, attack, target, hitIndex, &user); HOOK(ChangeDamageStatsUser, attack, attack, target, hitIndex, &user);
Library::Statistic offensiveStat; Library::Statistic offensiveStat;

View File

@ -45,7 +45,7 @@ const ConstString& Battling::Creature::GetActiveTalent() const {
return _variant->GetTalent(_talentIndex); return _variant->GetTalent(_talentIndex);
} }
void Battling::Creature::SetBattleData(ArbUt::BorrowedPtr<Battle> battle, BattleSide* side) { void Battling::Creature::SetBattleData(ArbUt::BorrowedPtr<Battle> battle, ArbUt::BorrowedPtr<BattleSide> side) {
_battle = battle; _battle = battle;
_side = side; _side = side;
this->ResetActiveScripts(); this->ResetActiveScripts();

View File

@ -35,7 +35,7 @@ namespace CreatureLib::Battling {
Library::Gender _gender; Library::Gender _gender;
uint8_t _coloring; uint8_t _coloring;
ArbUt::BorrowedPtr<const Library::Item> _heldItem; ArbUt::BorrowedPtr<const Library::Item> _heldItem;
uint32_t _currentHealth; uint32_t _currentHealth = -1;
Library::ClampedStatisticSet<int8_t, -6, 6> _statBoost; Library::ClampedStatisticSet<int8_t, -6, 6> _statBoost;
Library::StatisticSet<uint32_t> _flatStats; Library::StatisticSet<uint32_t> _flatStats;
@ -99,7 +99,7 @@ namespace CreatureLib::Battling {
inline uint32_t GetCurrentHealth() const noexcept { return _currentHealth; } inline uint32_t GetCurrentHealth() const noexcept { return _currentHealth; }
void SetBattleData(ArbUt::BorrowedPtr<Battle> battle, BattleSide* side); void SetBattleData(ArbUt::BorrowedPtr<Battle> battle, ArbUt::BorrowedPtr<BattleSide> side);
const ArbUt::BorrowedPtr<Battle>& GetBattle() const; const ArbUt::BorrowedPtr<Battle>& GetBattle() const;
const ArbUt::BorrowedPtr<BattleSide>& GetBattleSide() const; const ArbUt::BorrowedPtr<BattleSide>& GetBattleSide() const;
void SetOnBattleField(bool value) { _onBattleField = value; } void SetOnBattleField(bool value) { _onBattleField = value; }
@ -139,7 +139,9 @@ namespace CreatureLib::Battling {
void SetDisplaySpecies(const ArbUt::BorrowedPtr<const Library::CreatureSpecies>& species) noexcept { void SetDisplaySpecies(const ArbUt::BorrowedPtr<const Library::CreatureSpecies>& species) noexcept {
_displaySpecies = species; _displaySpecies = species;
} }
void SetDisplayVariant(const Library::SpeciesVariant* variant) noexcept { _displayVariant = variant; }; void SetDisplayVariant(ArbUt::BorrowedPtr<const Library::SpeciesVariant> variant) noexcept {
_displayVariant = variant;
};
inline bool AllowedExperienceGain() const noexcept { return _allowedExperienceGain; } inline bool AllowedExperienceGain() const noexcept { return _allowedExperienceGain; }
inline void SetAllowedExperienceGain(bool allowed) noexcept { _allowedExperienceGain = allowed; } inline void SetAllowedExperienceGain(bool allowed) noexcept { _allowedExperienceGain = allowed; }

View File

@ -38,16 +38,16 @@ namespace CreatureLib::Battling {
private: private:
ArbUt::List<ArbUt::BorrowedPtr<Creature>> _targets; ArbUt::List<ArbUt::BorrowedPtr<Creature>> _targets;
uint8_t _numberHits; uint8_t _numberHits;
HitData* _hits; std::unique_ptr<HitData[]> _hits;
Creature* _user; ArbUt::BorrowedPtr<Creature> _user;
ArbUt::BorrowedPtr<LearnedAttack> _attack; ArbUt::BorrowedPtr<LearnedAttack> _attack;
std::unique_ptr<Script> _script = nullptr; std::unique_ptr<Script> _script = nullptr;
public: public:
ExecutingAttack(const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& targets, uint8_t numberHits, Creature* user, ExecutingAttack(const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& targets, uint8_t numberHits, Creature* user,
const ArbUt::BorrowedPtr<LearnedAttack>& attack, const std::unique_ptr<Script>& script) const ArbUt::BorrowedPtr<LearnedAttack>& attack, const std::unique_ptr<Script>& script)
: _targets(targets.Count()), _numberHits(numberHits), _hits(new HitData[targets.Count() * numberHits]), : _targets(targets.Count()), _numberHits(numberHits),
_user(user), _attack(attack) { _hits(std::make_unique<HitData[]>(targets.Count() * numberHits)), _user(user), _attack(attack) {
AssertNotNull(user) AssertNotNull(user)
AssertNotNull(attack) AssertNotNull(attack)
for (auto target : targets) { for (auto target : targets) {
@ -59,7 +59,7 @@ namespace CreatureLib::Battling {
ExecutingAttack(const ExecutingAttack&) = delete; ExecutingAttack(const ExecutingAttack&) = delete;
ExecutingAttack& operator=(const ExecutingAttack&) = delete; ExecutingAttack& operator=(const ExecutingAttack&) = delete;
virtual ~ExecutingAttack() noexcept { delete[] _hits; }; virtual ~ExecutingAttack() noexcept = default;
HitData& GetHitData(Creature* creature, uint8_t hit) { HitData& GetHitData(Creature* creature, uint8_t hit) {
for (size_t i = 0; i < _targets.Count(); i++) { for (size_t i = 0; i < _targets.Count(); i++) {
@ -73,8 +73,7 @@ namespace CreatureLib::Battling {
HitData* GetTargetIteratorBegin(Creature* creature) { HitData* GetTargetIteratorBegin(Creature* creature) {
for (size_t i = 0; i < _targets.Count(); i++) { for (size_t i = 0; i < _targets.Count(); i++) {
if (_targets[i] == creature) { if (_targets[i] == creature) {
auto v = _hits + (i * _numberHits * sizeof(HitData)); return &_hits[i * _numberHits * sizeof(HitData)];
return v;
} }
} }
throw CreatureException("Invalid target requested."); throw CreatureException("Invalid target requested.");
@ -84,7 +83,7 @@ namespace CreatureLib::Battling {
const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& GetTargets() noexcept { return _targets; } const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& GetTargets() noexcept { return _targets; }
uint8_t GetNumberOfHits() const noexcept { return _numberHits; } uint8_t GetNumberOfHits() const noexcept { return _numberHits; }
Creature* GetUser() noexcept { return _user; } ArbUt::BorrowedPtr<Creature> GetUser() noexcept { return _user; }
const ArbUt::BorrowedPtr<LearnedAttack>& GetAttack() noexcept { return _attack; } const ArbUt::BorrowedPtr<LearnedAttack>& GetAttack() noexcept { return _attack; }
size_t ScriptCount() const override { return _user->ScriptCount() + 1; } size_t ScriptCount() const override { return _user->ScriptCount() + 1; }