Make LearnedAttack of Creature a smart pointer.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-05-31 17:26:39 +02:00
parent a7069a5960
commit ff181204ae
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
8 changed files with 25 additions and 21 deletions

View File

@ -9,7 +9,7 @@ export uint8_t CreatureLib_Creature_Construct(Creature*& out, const BattleLibrar
uint8_t coloring, const CreatureLib::Library::Item* heldItem,
const char* nickname, bool secretTalent, uint8_t talent,
LearnedAttack* attacks[], size_t attacksNum, bool allowedExperienceGain) {
Try(auto attacksVec = ArbUt::List<LearnedAttack*>(attacks, attacks + attacksNum);
Try(auto attacksVec = std::vector<LearnedAttack*>(attacks, attacks + (attacksNum * sizeof(LearnedAttack*)));
out = new Creature(library, ArbUt::BorrowedPtr<const CreatureLib::Library::CreatureSpecies>(species), variant,
level, experience, uid, gender, coloring,
ArbUt::BorrowedPtr<const CreatureLib::Library::Item>(heldItem), nickname,
@ -81,7 +81,9 @@ export bool CreatureLib_Creature_HasVolatileScript(Creature* p, const char* scri
return p->HasVolatileScript(ArbUt::CaseInsensitiveConstString(scriptName));
}
export size_t CreatureLib_Creature_GetAttacksCount(Creature* p) { return p->GetAttacks().Count(); }
export LearnedAttack* const* CreatureLib_Creature_GetAttacks(Creature* p) { return p->GetAttacks().RawData(); }
export LearnedAttack* const* CreatureLib_Creature_GetAttack(Creature* p, size_t index) {
return p->GetAttacks().RawData();
}
SIMPLE_GET_FUNC_SMART_PTR(Creature, GetDisplaySpecies, const CreatureLib::Library::CreatureSpecies*);
SIMPLE_GET_FUNC_SMART_PTR(Creature, GetDisplayVariant, const CreatureLib::Library::SpeciesVariant*);
export void CreatureLib_Creature_SetDisplaySpecies(Creature* p, const CreatureLib::Library::CreatureSpecies* species) {

View File

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

View File

@ -19,11 +19,13 @@ export void CreatureLib_BaseTurnChoice_Destruct(const BaseTurnChoice* p) { delet
#define SIMPLE_GET_FUNC(type, name, returnType) \
export returnType CreatureLib_##type##_##name(const type* p) { return p->name(); }
#define SIMPLE_GET_FUNC_SMART_PTR(type, name, returnType) \
export returnType CreatureLib_##type##_##name(const type* p) { return p->name().operator->(); }
SIMPLE_GET_FUNC(BaseTurnChoice, GetKind, TurnChoiceKind)
SIMPLE_GET_FUNC(BaseTurnChoice, GetUser, Creature*)
SIMPLE_GET_FUNC(AttackTurnChoice, GetAttack, LearnedAttack*)
SIMPLE_GET_FUNC_SMART_PTR(AttackTurnChoice, GetAttack, LearnedAttack*)
SIMPLE_GET_FUNC(AttackTurnChoice, GetKind, TurnChoiceKind)
export uint8_t CreatureLib_BaseTurnChoice_GetPriority(int8_t& out, AttackTurnChoice* p) { Try(out = p->GetPriority()); }
@ -38,4 +40,5 @@ export uint8_t CreatureLib_BaseTurnChoice_GetTargetCreatureIndex(const AttackTur
SIMPLE_GET_FUNC(SwitchTurnChoice, GetNewCreature, Creature*)
#undef SIMPLE_GET_FUNC
#undef SIMPLE_GET_FUNC
#undef SIMPLE_GET_FUNC_SMART_PTR

View File

@ -56,10 +56,10 @@ Creature* CreateCreature::Create() {
}
auto experience = _library->GetGrowthRateLibrary()->CalculateExperience(species->GetGrowthRate(), _level);
auto attacks = ArbUt::List<LearnedAttack*>(_attacks.Count());
auto attacks = std::vector<LearnedAttack*>(_attacks.Count());
for (size_t i = 0; i < _attacks.Count(); i++) {
auto kv = _attacks[i];
attacks.Append(new LearnedAttack(std::get<0>(kv), std::get<1>(kv)));
attacks[i] = new LearnedAttack(std::get<0>(kv), std::get<1>(kv));
}
auto c = new Creature(_library, species, variant, _level, experience, identifier, gender, _coloring, heldItem,
_nickname, talent, attacks);

View File

@ -11,7 +11,7 @@ Battling::Creature::Creature(const BattleLibrary* library,
const ArbUt::BorrowedPtr<const Library::SpeciesVariant>& variant, uint8_t level,
uint32_t experience, uint32_t uid, Library::Gender gender, uint8_t coloring,
const ArbUt::BorrowedPtr<const Library::Item> heldItem, std::string nickname,
const Library::TalentIndex& talent, const ArbUt::List<LearnedAttack*>& attacks,
const Library::TalentIndex& talent, const std::vector<LearnedAttack*>& attacks,
bool allowedExperienceGain)
: _library(library), _species(species), _variant(variant), _level(level), _experience(experience),
_uniqueIdentifier(uid), _gender(gender), _coloring(coloring), _heldItem(heldItem), _nickname(std::move(nickname)),

View File

@ -3,6 +3,7 @@
#include <Arbutils/Collections/List.hpp>
#include <Arbutils/Memory/BorrowedPtr.hpp>
#include <Arbutils/Memory/UniquePtrList.hpp>
#include "../../Library/ClampedStatisticSet.hpp"
#include "../../Library/CreatureData/CreatureSpecies.hpp"
#include "../../Library/Items/Item.hpp"
@ -52,7 +53,7 @@ namespace CreatureLib::Battling {
ArbUt::CaseInsensitiveConstString _overridenTalentName = ""_cnc;
std::unordered_set<Creature*> _seenOpponents = {};
ArbUt::List<LearnedAttack*> _attacks;
ArbUt::UniquePtrList<LearnedAttack> _attacks;
bool _allowedExperienceGain;
Script* _status = nullptr;
@ -66,13 +67,10 @@ namespace CreatureLib::Battling {
const ArbUt::BorrowedPtr<const Library::SpeciesVariant>& variant, uint8_t level, uint32_t experience,
uint32_t uid, Library::Gender gender, uint8_t coloring,
const ArbUt::BorrowedPtr<const Library::Item> heldItem, std::string nickname,
const Library::TalentIndex& talent, const ArbUt::List<LearnedAttack*>& attacks,
const Library::TalentIndex& talent, const std::vector<LearnedAttack*>& attacks,
bool allowedExperienceGain = true);
virtual ~Creature() {
for (auto attack : _attacks) {
delete attack;
}
delete _activeTalent;
delete _status;
};
@ -135,7 +133,7 @@ namespace CreatureLib::Battling {
void RemoveVolatileScript(Script* script);
bool HasVolatileScript(const ArbUt::CaseInsensitiveConstString& name) const;
const ArbUt::List<LearnedAttack*>& GetAttacks() noexcept { return _attacks; }
const ArbUt::UniquePtrList<LearnedAttack>& GetAttacks() noexcept { return _attacks; }
ArbUt::BorrowedPtr<const Library::CreatureSpecies> GetDisplaySpecies() const noexcept;
ArbUt::BorrowedPtr<const Library::SpeciesVariant> GetDisplayVariant() const noexcept;

View File

@ -40,12 +40,12 @@ namespace CreatureLib::Battling {
uint8_t _numberHits;
HitData* _hits;
Creature* _user;
LearnedAttack* _attack;
ArbUt::BorrowedPtr<LearnedAttack> _attack;
Script* _script;
public:
ExecutingAttack(const ArbUt::List<Creature*>& targets, uint8_t numberHits, Creature* user,
LearnedAttack* attack, Script* script)
const ArbUt::BorrowedPtr<LearnedAttack>& attack, Script* script)
: _targets(targets.Count()), _numberHits(numberHits), _hits(new HitData[targets.Count() * numberHits]),
_user(user), _attack(attack), _script(script) {
AssertNotNull(user)
@ -87,7 +87,7 @@ namespace CreatureLib::Battling {
Creature* GetUser() noexcept { return _user; }
LearnedAttack* GetAttack() noexcept { return _attack; }
const ArbUt::BorrowedPtr<LearnedAttack>& GetAttack() noexcept { return _attack; }
size_t ScriptCount() const override { return _user->ScriptCount() + 1; }
protected:

View File

@ -9,7 +9,7 @@
namespace CreatureLib::Battling {
class AttackTurnChoice : public BaseTurnChoice {
LearnedAttack* _attack;
ArbUt::BorrowedPtr<LearnedAttack> _attack;
CreatureIndex _target;
Script* _attackScript = nullptr;
bool _scriptIsTaken = false;
@ -34,12 +34,13 @@ namespace CreatureLib::Battling {
}
public:
AttackTurnChoice(Creature* user, LearnedAttack* attack, const CreatureIndex& target)
AttackTurnChoice(Creature* user, const ArbUt::BorrowedPtr<LearnedAttack>& attack, const CreatureIndex& target)
: BaseTurnChoice(user), _attack(attack), _target(target) {
AssertNotNull(attack)
ResolveScript();
}
AttackTurnChoice(Creature* user, LearnedAttack* attack, const CreatureIndex& target, Script* script) noexcept
AttackTurnChoice(Creature* user, const ArbUt::BorrowedPtr<LearnedAttack>& attack, const CreatureIndex& target,
Script* script) noexcept
: BaseTurnChoice(user), _attack(attack), _target(target), _attackScript(script) {}
~AttackTurnChoice() {
@ -48,7 +49,7 @@ namespace CreatureLib::Battling {
}
}
inline LearnedAttack* GetAttack() const noexcept { return _attack; }
inline const ArbUt::BorrowedPtr<LearnedAttack>& GetAttack() const noexcept { return _attack; }
TurnChoiceKind GetKind() const noexcept override { return TurnChoiceKind ::Attack; }