Make Creature use smart pointers.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-06-02 16:20:47 +02:00
parent 1ef50fd3a6
commit 62583ecb17
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
6 changed files with 19 additions and 14 deletions

View File

@ -44,7 +44,7 @@ export void CreatureLib_Creature_SetHeldItemFromItem(Creature* p, const Creature
} }
SIMPLE_GET_FUNC(Creature, GetCurrentHealth, uint32_t); SIMPLE_GET_FUNC(Creature, GetCurrentHealth, uint32_t);
SIMPLE_GET_FUNC_SMART_PTR(Creature, GetBattle, Battle*); SIMPLE_GET_FUNC_SMART_PTR(Creature, GetBattle, Battle*);
SIMPLE_GET_FUNC(Creature, GetBattleSide, BattleSide*); SIMPLE_GET_FUNC_SMART_PTR(Creature, GetBattleSide, BattleSide*);
SIMPLE_GET_FUNC(Creature, IsOnBattleField, bool); SIMPLE_GET_FUNC(Creature, IsOnBattleField, bool);
export const char* CreatureLib_Creature_GetNickname(Creature* p) { return p->GetNickname().c_str(); } export const char* CreatureLib_Creature_GetNickname(Creature* p) { return p->GetNickname().c_str(); }
export size_t CreatureLib_Creature_GetTypesCount(Creature* p) { return p->GetTypes().Count(); } export size_t CreatureLib_Creature_GetTypesCount(Creature* p) { return p->GetTypes().Count(); }

View File

@ -8,6 +8,7 @@ export void CreatureLib_ExperienceLibrary_Destruct(const ExperienceLibrary* p) {
export uint8_t CreatureLib_ExperienceLibrary_HandleExperienceGain(const ExperienceLibrary* p, Creature* faintedMon, export uint8_t CreatureLib_ExperienceLibrary_HandleExperienceGain(const ExperienceLibrary* p, Creature* faintedMon,
Creature* opponents[], size_t opponentsCount) { Creature* opponents[], size_t opponentsCount) {
Try(auto set = std::unordered_set<Creature*>(opponents, opponents + opponentsCount); Try(auto set = std::unordered_set<ArbUt::BorrowedPtr<Creature>>(opponentsCount);
p->HandleExperienceGain(faintedMon, set);) for (size_t i = 0; i < opponentsCount;
i++) { set.insert(opponents[i]); } p->HandleExperienceGain(faintedMon, set);)
} }

View File

@ -2,7 +2,8 @@
#include "../Models/Creature.hpp" #include "../Models/Creature.hpp"
void CreatureLib::Battling::ExperienceLibrary::HandleExperienceGain( void CreatureLib::Battling::ExperienceLibrary::HandleExperienceGain(
CreatureLib::Battling::Creature* faintedMon, const std::unordered_set<Creature*>& opponents) const { CreatureLib::Battling::Creature* faintedMon,
const std::unordered_set<ArbUt::BorrowedPtr<Creature>>& opponents) const {
for (auto opponent : opponents) { for (auto opponent : opponents) {
if (opponent == nullptr) if (opponent == nullptr)
continue; continue;

View File

@ -1,5 +1,6 @@
#ifndef CREATURELIB_EXPERIENCELIBRARY_HPP #ifndef CREATURELIB_EXPERIENCELIBRARY_HPP
#define CREATURELIB_EXPERIENCELIBRARY_HPP #define CREATURELIB_EXPERIENCELIBRARY_HPP
#include <Arbutils/Memory/BorrowedPtr.hpp>
#include <unordered_set> #include <unordered_set>
namespace CreatureLib::Battling { namespace CreatureLib::Battling {
@ -9,7 +10,8 @@ namespace CreatureLib::Battling {
public: public:
virtual ~ExperienceLibrary() = default; virtual ~ExperienceLibrary() = default;
virtual void HandleExperienceGain(Creature* faintedMon, const std::unordered_set<Creature*>& opponents) const; virtual void HandleExperienceGain(Creature* faintedMon,
const std::unordered_set<ArbUt::BorrowedPtr<Creature>>& opponents) const;
}; };
} }

View File

@ -6,7 +6,7 @@
using namespace CreatureLib; using namespace CreatureLib;
Battling::Creature::Creature(const BattleLibrary* library, Battling::Creature::Creature(ArbUt::BorrowedPtr<const BattleLibrary> library,
const ArbUt::BorrowedPtr<const Library::CreatureSpecies>& species, const ArbUt::BorrowedPtr<const Library::CreatureSpecies>& species,
const ArbUt::BorrowedPtr<const Library::SpeciesVariant>& variant, uint8_t level, const ArbUt::BorrowedPtr<const Library::SpeciesVariant>& variant, uint8_t level,
uint32_t experience, uint32_t uid, Library::Gender gender, uint8_t coloring, uint32_t experience, uint32_t uid, Library::Gender gender, uint8_t coloring,
@ -87,7 +87,7 @@ void Battling::Creature::RecalculateBoostedStat(Library::Statistic stat) {
const ArbUt::BorrowedPtr<CreatureLib::Battling::Battle>& Battling::Creature::GetBattle() const { return _battle; } const ArbUt::BorrowedPtr<CreatureLib::Battling::Battle>& Battling::Creature::GetBattle() const { return _battle; }
Battling::BattleSide* Battling::Creature::GetBattleSide() const { return _side; } const ArbUt::BorrowedPtr<CreatureLib::Battling::BattleSide>& Battling::Creature::GetBattleSide() const { return _side; }
bool Battling::Creature::IsFainted() const noexcept { return this->_currentHealth == 0; } bool Battling::Creature::IsFainted() const noexcept { return this->_currentHealth == 0; }

View File

@ -21,7 +21,7 @@ namespace CreatureLib::Battling {
class Creature : public ScriptSource { class Creature : public ScriptSource {
protected: protected:
const BattleLibrary* _library; ArbUt::BorrowedPtr<const BattleLibrary> _library;
ArbUt::BorrowedPtr<const Library::CreatureSpecies> _species; ArbUt::BorrowedPtr<const Library::CreatureSpecies> _species;
ArbUt::BorrowedPtr<const Library::SpeciesVariant> _variant; ArbUt::BorrowedPtr<const Library::SpeciesVariant> _variant;
@ -42,7 +42,7 @@ namespace CreatureLib::Battling {
Library::StatisticSet<uint32_t> _boostedStats; Library::StatisticSet<uint32_t> _boostedStats;
ArbUt::BorrowedPtr<Battle> _battle = nullptr; ArbUt::BorrowedPtr<Battle> _battle = nullptr;
BattleSide* _side = nullptr; ArbUt::BorrowedPtr<BattleSide> _side = nullptr;
bool _onBattleField = false; bool _onBattleField = false;
std::string _nickname = ""; std::string _nickname = "";
@ -51,7 +51,7 @@ namespace CreatureLib::Battling {
bool _hasOverridenTalent; bool _hasOverridenTalent;
ArbUt::CaseInsensitiveConstString _overridenTalentName = ""_cnc; ArbUt::CaseInsensitiveConstString _overridenTalentName = ""_cnc;
std::unordered_set<Creature*> _seenOpponents = {}; std::unordered_set<ArbUt::BorrowedPtr<Creature>> _seenOpponents;
ArbUt::UniquePtrList<LearnedAttack> _attacks; ArbUt::UniquePtrList<LearnedAttack> _attacks;
bool _allowedExperienceGain; bool _allowedExperienceGain;
@ -63,7 +63,8 @@ namespace CreatureLib::Battling {
void OnFaint(); void OnFaint();
public: public:
Creature(const BattleLibrary* library, const ArbUt::BorrowedPtr<const Library::CreatureSpecies>& species, Creature(ArbUt::BorrowedPtr<const BattleLibrary> library,
const ArbUt::BorrowedPtr<const Library::CreatureSpecies>& species,
const ArbUt::BorrowedPtr<const Library::SpeciesVariant>& variant, uint8_t level, uint32_t experience, const ArbUt::BorrowedPtr<const Library::SpeciesVariant>& variant, uint8_t level, uint32_t experience,
uint32_t uid, Library::Gender gender, uint8_t coloring, uint32_t uid, Library::Gender gender, uint8_t coloring,
const ArbUt::BorrowedPtr<const Library::Item> heldItem, std::string nickname, const ArbUt::BorrowedPtr<const Library::Item> heldItem, std::string nickname,
@ -100,7 +101,7 @@ namespace CreatureLib::Battling {
void SetBattleData(ArbUt::BorrowedPtr<Battle> battle, BattleSide* side); void SetBattleData(ArbUt::BorrowedPtr<Battle> battle, BattleSide* side);
const ArbUt::BorrowedPtr<Battle>& GetBattle() const; const ArbUt::BorrowedPtr<Battle>& GetBattle() const;
BattleSide* GetBattleSide() const; const ArbUt::BorrowedPtr<BattleSide>& GetBattleSide() const;
void SetOnBattleField(bool value) { _onBattleField = value; } void SetOnBattleField(bool value) { _onBattleField = value; }
bool IsOnBattleField() const { return _onBattleField; } bool IsOnBattleField() const { return _onBattleField; }
@ -118,8 +119,8 @@ namespace CreatureLib::Battling {
void OverrideActiveTalent(const ArbUt::CaseInsensitiveConstString& talent); void OverrideActiveTalent(const ArbUt::CaseInsensitiveConstString& talent);
void AddExperience(uint32_t amount); void AddExperience(uint32_t amount);
void MarkOpponentAsSeen(Creature* creature) { _seenOpponents.insert(creature); } void MarkOpponentAsSeen(ArbUt::BorrowedPtr<Creature> creature) { _seenOpponents.insert(creature); }
const std::unordered_set<Creature*>& GetSeenOpponents() const { return _seenOpponents; } const std::unordered_set<ArbUt::BorrowedPtr<Creature>>& GetSeenOpponents() const { return _seenOpponents; }
size_t ScriptCount() const override; size_t ScriptCount() const override;
void GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) override; void GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) override;