diff --git a/CInterface/Battling/Creature.cpp b/CInterface/Battling/Creature.cpp index 01c5eaf..4004cb2 100644 --- a/CInterface/Battling/Creature.cpp +++ b/CInterface/Battling/Creature.cpp @@ -111,4 +111,10 @@ export uint32_t CreatureLib_Creature_GetBaseStat(Creature* p, CreatureLib::Libra } export int8_t CreatureLib_Creature_GetStatBoost(Creature* p, CreatureLib::Library::Statistic stat) { return p->GetStatBoost(stat); +} + +SIMPLE_GET_FUNC(Creature, GetAvailableAttackSlot, uint8_t); +export uint8_t CreatureLib_Creature_AddAttack(Creature* p, LearnedAttack* attack) { Try(p->AddAttack(attack);) } +export uint8_t CreatureLib_Creature_ReplaceAttack(Creature* p, size_t index, LearnedAttack* attack) { + Try(p->ReplaceAttack(index, attack);) } \ No newline at end of file diff --git a/CInterface/Library/LibrarySettings.cpp b/CInterface/Library/LibrarySettings.cpp index 5b8a9fd..66aab1f 100644 --- a/CInterface/Library/LibrarySettings.cpp +++ b/CInterface/Library/LibrarySettings.cpp @@ -9,4 +9,4 @@ export const LibrarySettings* CreatureLib_LibrarySettings_Construct(uint8_t maxi export void CreatureLib_LibrarySettings_Destruct(const LibrarySettings* p) { delete p; } SIMPLE_GET_FUNC(LibrarySettings, GetMaximalLevel, uint8_t); -SIMPLE_GET_FUNC(LibrarySettings, GetMaximalMoves, uint8_t); \ No newline at end of file +SIMPLE_GET_FUNC(LibrarySettings, GetMaximalAttacks, uint8_t); \ No newline at end of file diff --git a/src/Battling/Models/CreateCreature.cpp b/src/Battling/Models/CreateCreature.cpp index 2c5f59a..23da5cd 100644 --- a/src/Battling/Models/CreateCreature.cpp +++ b/src/Battling/Models/CreateCreature.cpp @@ -22,7 +22,7 @@ CreateCreature CreateCreature::WithGender(Library::Gender gender) { } CreateCreature CreateCreature::WithAttack(const ArbUt::StringView& attackName, AttackLearnMethod learnMethod) { - if (_attacks.Count() >= _library->GetSettings()->GetMaximalMoves()) { + if (_attacks.Count() >= _library->GetSettings()->GetMaximalAttacks()) { THROW_CREATURE("You have already set the maximum amount of allowed moves."); } diff --git a/src/Battling/Models/CreateCreature.hpp b/src/Battling/Models/CreateCreature.hpp index 7b2c816..fe69c73 100644 --- a/src/Battling/Models/CreateCreature.hpp +++ b/src/Battling/Models/CreateCreature.hpp @@ -23,8 +23,8 @@ namespace CreatureLib::Battling { public: CreateCreature(ArbUt::BorrowedPtr library, const ArbUt::StringView& species, uint8_t level) - : _library(library), _species(species), _level(level), _attacks(library->GetSettings()->GetMaximalMoves()) { - } + : _library(library), _species(species), _level(level), + _attacks(library->GetSettings()->GetMaximalAttacks()) {} CreateCreature WithVariant(const ArbUt::StringView& variant); CreateCreature WithNickname(std::string nickname); diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index 5ad7eb1..1706049 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -271,3 +271,36 @@ void Battling::Creature::AddVolatileScript(Script* script) { _volatile.Add(scrip void Battling::Creature::RemoveVolatileScript(const ArbUt::BasicStringView& name) { _volatile.Remove(name); } void Battling::Creature::RemoveVolatileScript(Battling::Script* script) { _volatile.Remove(script->GetName()); } bool Battling::Creature::HasVolatileScript(const ArbUt::BasicStringView& name) const { return _volatile.Has(name); } +void Battling::Creature::AddAttack(Battling::LearnedAttack* attack) { + for (size_t i = 0; i < _attacks.Count(); i++) { + if (_attacks[i] == nullptr) { + _attacks.Set(i, attack); + return; + } + } + if (_attacks.Count() < _library->GetStaticLib()->GetSettings()->GetMaximalAttacks()) { + _attacks.Append(attack); + } + THROW_CREATURE("Can't add attack. The creature already has the maximum amount of attacks."); +} +uint8_t Battling::Creature::GetAvailableAttackSlot() const noexcept { + for (uint8_t i = 0; i < (uint8_t)_attacks.Count(); i++) { + if (_attacks[i] == nullptr) { + return i; + } + } + if (_attacks.Count() < _library->GetStaticLib()->GetSettings()->GetMaximalAttacks()) { + return _attacks.Count(); + } + return -1; +} +void Battling::Creature::ReplaceAttack(size_t index, Battling::LearnedAttack* attack) { + if (_attacks.Count() <= index) { + if (_attacks.Count() < _library->GetStaticLib()->GetSettings()->GetMaximalAttacks()) { + _attacks.Append(attack); + } + THROW_CREATURE("Can't replace attack at index " << index << ". Number of attacks is " << _attacks.Count() + << "."); + } + _attacks.Set(index, attack); +} diff --git a/src/Battling/Models/Creature.hpp b/src/Battling/Models/Creature.hpp index 2647ce8..d21b768 100644 --- a/src/Battling/Models/Creature.hpp +++ b/src/Battling/Models/Creature.hpp @@ -161,6 +161,10 @@ namespace CreatureLib::Battling { inline bool AllowedExperienceGain() const noexcept { return _allowedExperienceGain; } inline void SetAllowedExperienceGain(bool allowed) noexcept { _allowedExperienceGain = allowed; } + uint8_t GetAvailableAttackSlot() const noexcept; + void AddAttack(LearnedAttack* attack); + void ReplaceAttack(size_t index, LearnedAttack* attack); + // region Stat APIs bool ChangeStatBoost(Library::Statistic stat, int8_t diffAmount); diff --git a/src/Library/LibrarySettings.hpp b/src/Library/LibrarySettings.hpp index 6dbf264..4434f95 100644 --- a/src/Library/LibrarySettings.hpp +++ b/src/Library/LibrarySettings.hpp @@ -6,15 +6,15 @@ namespace CreatureLib::Library { class LibrarySettings { uint8_t _maximalLevel; - uint8_t _maximalMoves; + uint8_t _maximalAttacks; public: - LibrarySettings(uint8_t maximalLevel, uint8_t maximalMoves) - : _maximalLevel(maximalLevel), _maximalMoves(maximalMoves) {} + LibrarySettings(uint8_t maximalLevel, uint8_t maximalAttacks) + : _maximalLevel(maximalLevel), _maximalAttacks(maximalAttacks) {} inline uint8_t GetMaximalLevel() const noexcept { return _maximalLevel; } - inline uint8_t GetMaximalMoves() const noexcept { return _maximalMoves; } + inline uint8_t GetMaximalAttacks() const noexcept { return _maximalAttacks; } }; }