Make all individual scripts smart pointers.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -62,7 +62,6 @@ void TurnHandler::ExecuteAttackChoice(ArbUt::BorrowedPtr<AttackTurnChoice> choic
|
||||
ArbUt::List<ArbUt::BorrowedPtr<Creature>> targets = {target};
|
||||
|
||||
auto attack = ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript());
|
||||
choice->MarkScriptAsTaken();
|
||||
bool prevented = false;
|
||||
HOOK_LOCAL(PreventAttack, attack, &attack, &prevented);
|
||||
if (prevented) {
|
||||
|
||||
@@ -21,7 +21,7 @@ Battling::Creature::Creature(const BattleLibrary* library,
|
||||
AssertNotNull(species)
|
||||
AssertNotNull(variant)
|
||||
|
||||
_activeTalent = _library->LoadScript(ScriptCategory::Talent, GetActiveTalent());
|
||||
_activeTalent = std::unique_ptr<Script>(_library->LoadScript(ScriptCategory::Talent, GetActiveTalent()));
|
||||
if (_nickname.empty()) {
|
||||
_nickname = species->GetName().std_str();
|
||||
}
|
||||
@@ -142,9 +142,8 @@ void Battling::Creature::Heal(uint32_t amount, bool canRevive) {
|
||||
void Battling::Creature::OverrideActiveTalent(const ConstString& talent) {
|
||||
_hasOverridenTalent = true;
|
||||
_activeTalent->OnRemove();
|
||||
delete _activeTalent;
|
||||
_overridenTalentName = talent;
|
||||
_activeTalent = this->_library->LoadScript(ScriptCategory::Talent, talent);
|
||||
_activeTalent.reset(this->_library->LoadScript(ScriptCategory::Talent, talent));
|
||||
}
|
||||
|
||||
const ArbUt::List<uint8_t>& Battling::Creature::GetTypes() const noexcept {
|
||||
|
||||
@@ -47,7 +47,7 @@ namespace CreatureLib::Battling {
|
||||
|
||||
std::string _nickname = "";
|
||||
CreatureLib::Library::TalentIndex _talentIndex;
|
||||
Script* _activeTalent = nullptr;
|
||||
std::unique_ptr<Script> _activeTalent = nullptr;
|
||||
|
||||
bool _hasOverridenTalent;
|
||||
ArbUt::CaseInsensitiveConstString _overridenTalentName = ""_cnc;
|
||||
@@ -56,7 +56,7 @@ namespace CreatureLib::Battling {
|
||||
ArbUt::UniquePtrList<LearnedAttack> _attacks;
|
||||
bool _allowedExperienceGain;
|
||||
|
||||
Script* _status = nullptr;
|
||||
std::unique_ptr<Script> _status = nullptr;
|
||||
ScriptSet _volatile = {};
|
||||
|
||||
private:
|
||||
@@ -70,10 +70,7 @@ namespace CreatureLib::Battling {
|
||||
const Library::TalentIndex& talent, const std::vector<LearnedAttack*>& attacks,
|
||||
bool allowedExperienceGain = true);
|
||||
|
||||
virtual ~Creature() {
|
||||
delete _activeTalent;
|
||||
delete _status;
|
||||
};
|
||||
virtual ~Creature() = default;
|
||||
|
||||
virtual void Initialize() {
|
||||
RecalculateFlatStats();
|
||||
|
||||
@@ -41,24 +41,25 @@ namespace CreatureLib::Battling {
|
||||
HitData* _hits;
|
||||
Creature* _user;
|
||||
ArbUt::BorrowedPtr<LearnedAttack> _attack;
|
||||
Script* _script;
|
||||
std::unique_ptr<Script> _script = nullptr;
|
||||
|
||||
public:
|
||||
ExecutingAttack(const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& targets, uint8_t numberHits, Creature* user,
|
||||
const ArbUt::BorrowedPtr<LearnedAttack>& attack, Script* script)
|
||||
const ArbUt::BorrowedPtr<LearnedAttack>& attack, const std::unique_ptr<Script>& script)
|
||||
: _targets(targets.Count()), _numberHits(numberHits), _hits(new HitData[targets.Count() * numberHits]),
|
||||
_user(user), _attack(attack), _script(script) {
|
||||
_user(user), _attack(attack) {
|
||||
AssertNotNull(user)
|
||||
AssertNotNull(attack)
|
||||
for (auto target : targets) {
|
||||
_targets.Append(target);
|
||||
}
|
||||
// Take ownership of the script of the attack choice, and give attack choice our initial nullptr.
|
||||
_script.swap(const_cast<std::unique_ptr<Script>&>(script));
|
||||
}
|
||||
ExecutingAttack(const ExecutingAttack&) = delete;
|
||||
ExecutingAttack& operator=(const ExecutingAttack&) = delete;
|
||||
|
||||
virtual ~ExecutingAttack() noexcept {
|
||||
delete _script;
|
||||
delete[] _hits;
|
||||
};
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace CreatureLib::Battling {
|
||||
auto s = current.GetScript();
|
||||
if (s == nullptr)
|
||||
return nullptr;
|
||||
return *s;
|
||||
return (*s).get();
|
||||
} else {
|
||||
auto& set = *current.GetScriptSet()->GetIterator();
|
||||
auto count = set.Count();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef CREATURELIB_SCRIPTWRAPPER_HPP
|
||||
#define CREATURELIB_SCRIPTWRAPPER_HPP
|
||||
|
||||
#include <variant>
|
||||
#include <memory>
|
||||
#include "Script.hpp"
|
||||
#include "ScriptSet.hpp"
|
||||
|
||||
@@ -10,20 +10,20 @@ namespace CreatureLib::Battling {
|
||||
bool _isSet;
|
||||
|
||||
union {
|
||||
Script* const* _script;
|
||||
std::unique_ptr<Script> const* _script;
|
||||
const ScriptSet* _scriptSet;
|
||||
};
|
||||
|
||||
ScriptWrapper(Script** s, bool isSet) : _isSet(isSet), _script(s){};
|
||||
ScriptWrapper(std::unique_ptr<Script>* s, bool isSet) : _isSet(isSet), _script(s){};
|
||||
ScriptWrapper(ScriptSet* s, bool isSet) : _isSet(isSet), _scriptSet(s){};
|
||||
|
||||
public:
|
||||
static inline ScriptWrapper FromScript(Script** s) { return ScriptWrapper(s, false); }
|
||||
static inline ScriptWrapper FromScript(std::unique_ptr<Script>* s) { return ScriptWrapper(s, false); }
|
||||
static inline ScriptWrapper FromSet(ScriptSet* s) { return ScriptWrapper(s, true); }
|
||||
|
||||
bool IsSet() const { return _isSet; }
|
||||
|
||||
inline Script* const* GetScript() const { return _script; }
|
||||
inline const std::unique_ptr<Script>* GetScript() const { return _script; }
|
||||
inline const ScriptSet* GetScriptSet() const { return _scriptSet; }
|
||||
};
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace CreatureLib::Battling {
|
||||
class AttackTurnChoice : public BaseTurnChoice {
|
||||
ArbUt::BorrowedPtr<LearnedAttack> _attack;
|
||||
CreatureIndex _target;
|
||||
Script* _attackScript = nullptr;
|
||||
std::unique_ptr<Script> _attackScript = nullptr;
|
||||
bool _scriptIsTaken = false;
|
||||
|
||||
void ResolveScript() {
|
||||
@@ -25,7 +25,8 @@ namespace CreatureLib::Battling {
|
||||
if (_attack->GetAttack()->HasSecondaryEffect()) {
|
||||
auto library = battle->GetLibrary();
|
||||
auto& effect = _attack->GetAttack()->GetSecondaryEffect();
|
||||
_attackScript = library->LoadScript(ScriptCategory::Attack, effect->GetEffectName());
|
||||
_attackScript =
|
||||
std::unique_ptr<Script>(library->LoadScript(ScriptCategory::Attack, effect->GetEffectName()));
|
||||
if (_attackScript != nullptr) {
|
||||
_attackScript->OnInitialize(effect->GetParameters());
|
||||
}
|
||||
@@ -45,7 +46,6 @@ namespace CreatureLib::Battling {
|
||||
|
||||
~AttackTurnChoice() {
|
||||
if (!_scriptIsTaken) {
|
||||
delete _attackScript;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,11 +58,9 @@ namespace CreatureLib::Battling {
|
||||
return _attack->GetAttack()->GetPriority();
|
||||
}
|
||||
|
||||
void MarkScriptAsTaken() { _scriptIsTaken = true; }
|
||||
|
||||
const CreatureIndex& GetTarget() const noexcept { return _target; }
|
||||
|
||||
Script* GetAttackScript() const noexcept { return _attackScript; }
|
||||
const std::unique_ptr<Script>& GetAttackScript() const noexcept { return _attackScript; }
|
||||
size_t ScriptCount() const override { return 1 + GetUser()->ScriptCount(); }
|
||||
|
||||
protected:
|
||||
|
||||
Reference in New Issue
Block a user