From 27288563cdd560f854f02edf420a77aa4ca07e1a Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 22 Mar 2020 19:21:40 +0100 Subject: [PATCH] Replace most collections with Arbutils collections for more safety. --- CInterface/Battling/Battle.cpp | 6 ++-- CInterface/Battling/BattleParty.cpp | 2 +- CInterface/Battling/Creature.cpp | 13 ++++---- CInterface/Battling/CreatureParty.cpp | 4 +-- CInterface/Library/GrowthRate.cpp | 2 +- CInterface/Library/LearnableAttacks.cpp | 4 +-- CInterface/Library/SpeciesVariant.cpp | 6 ++-- CInterface/Library/TypeLibrary.cpp | 2 +- src/Battling/Flow/TurnHandler.cpp | 2 +- src/Battling/Models/Battle.cpp | 4 +-- src/Battling/Models/Battle.hpp | 19 ++++++------ src/Battling/Models/BattleParty.hpp | 16 +++++----- src/Battling/Models/BattleSide.cpp | 8 ++--- src/Battling/Models/BattleSide.hpp | 19 +++++++----- src/Battling/Models/CreateCreature.cpp | 8 ++--- src/Battling/Models/CreateCreature.hpp | 5 ++- src/Battling/Models/Creature.cpp | 13 ++++---- src/Battling/Models/Creature.hpp | 13 +++++--- src/Battling/Models/CreatureParty.hpp | 12 +++---- src/Battling/Models/ExecutingAttack.hpp | 27 +++++++++------- .../ScriptHandling/ScriptAggregator.hpp | 21 +++++++------ src/Battling/ScriptHandling/ScriptSet.hpp | 14 ++++----- src/Battling/ScriptHandling/ScriptSource.hpp | 6 ++-- src/Battling/TurnChoices/AttackTurnChoice.hpp | 4 +-- src/Battling/TurnChoices/FleeTurnChoice.hpp | 4 ++- src/Battling/TurnChoices/PassTurnChoice.hpp | 4 ++- src/Battling/TurnChoices/SwitchTurnChoice.hpp | 4 ++- src/Library/BaseLibrary.hpp | 31 ++++++++----------- src/Library/CreatureData/CreatureSpecies.cpp | 21 ++++--------- src/Library/CreatureData/CreatureSpecies.hpp | 9 ++++-- src/Library/CreatureData/LearnableAttacks.cpp | 14 +++++---- src/Library/CreatureData/LearnableAttacks.hpp | 11 ++++--- src/Library/CreatureData/SpeciesVariant.cpp | 19 ++++++------ src/Library/CreatureData/SpeciesVariant.hpp | 30 +++++++++--------- src/Library/GrowthRates/LookupGrowthRate.hpp | 16 +++++----- src/Library/TypeLibrary.cpp | 26 +++++++--------- src/Library/TypeLibrary.hpp | 11 ++++--- .../ScriptTests/ScriptAggregatorTests.cpp | 16 +++++----- .../ScriptTests/ScriptSetTests.cpp | 10 +++--- .../ScriptTests/ScriptSourceTest.cpp | 4 +-- 40 files changed, 234 insertions(+), 226 deletions(-) diff --git a/CInterface/Battling/Battle.cpp b/CInterface/Battling/Battle.cpp index aba7a69..26cca7a 100644 --- a/CInterface/Battling/Battle.cpp +++ b/CInterface/Battling/Battle.cpp @@ -5,7 +5,7 @@ using namespace CreatureLib::Battling; export Battle* CreatureLib_Battle_Construct(const BattleLibrary* library, BattleParty* partyArr[], size_t numberOfParties, bool canFlee, uint8_t numberOfSides, uint8_t creaturesPerSide) { - std::vector parties(partyArr, partyArr + numberOfParties); + List parties(partyArr, partyArr + numberOfParties); return new Battle(library, parties, canFlee, numberOfSides, creaturesPerSide); } @@ -39,8 +39,8 @@ export bool CreatureLib_Battle_HasEnded(const Battle* p) { return p->HasEnded(); export bool CreatureLib_Battle_HasConclusiveResult(const Battle* p) { return p->GetResult().IsConclusiveResult(); } export bool CreatureLib_Battle_GetWinningSide(const Battle* p) { return p->GetResult().GetWinningSide(); } -export size_t CreatureLib_Battle_GetSidesCount(const Battle* p) { return p->GetSides().size(); } -export BattleSide* const* CreatureLib_Battle_GetSides(const Battle* p) { return p->GetSides().data(); } +export size_t CreatureLib_Battle_GetSidesCount(const Battle* p) { return p->GetSides().Count(); } +export BattleSide* const* CreatureLib_Battle_GetSides(const Battle* p) { return p->GetSides().RawData(); } export Script* CreatureLib_Battle_GetVolatileScript(Battle* p, const char* key) { return p->GetVolatileScript(ConstString::GetHash(key)); diff --git a/CInterface/Battling/BattleParty.cpp b/CInterface/Battling/BattleParty.cpp index e6ea5bd..f4d8f14 100644 --- a/CInterface/Battling/BattleParty.cpp +++ b/CInterface/Battling/BattleParty.cpp @@ -6,7 +6,7 @@ using namespace CreatureLib::Battling; // side, one after the other. export BattleParty* CreatureLib_BattleParty_Construct(CreatureParty* p, uint8_t creatureIndices[], size_t numberOfIndices) { - std::vector indices(numberOfIndices); + Arbutils::Collections::List indices(numberOfIndices); for (size_t i = 0; i < numberOfIndices; i++) { indices[i] = CreatureIndex(creatureIndices[i * 2], creatureIndices[i * 2 + 1]); } diff --git a/CInterface/Battling/Creature.cpp b/CInterface/Battling/Creature.cpp index be8c4ca..2c51bc9 100644 --- a/CInterface/Battling/Creature.cpp +++ b/CInterface/Battling/Creature.cpp @@ -10,7 +10,7 @@ export Creature* CreatureLib_Creature_Construct(const BattleLibrary* library, uint8_t coloring, const CreatureLib::Library::Item* heldItem, std::string nickname, bool secretTalent, const uint8_t talent, LearnedAttack* attacks[], size_t attacksNum) { - auto attacksVec = std::vector(attacks, attacks + attacksNum); + auto attacksVec = List(attacks, attacks + attacksNum); return new Creature(library, species, variant, level, experience, uid, gender, coloring, heldItem, nickname, CreatureLib::Library::TalentIndex(secretTalent, talent), attacksVec); }; @@ -44,8 +44,8 @@ SIMPLE_GET_FUNC(Creature, GetBattle, Battle*); SIMPLE_GET_FUNC(Creature, GetBattleSide, BattleSide*); SIMPLE_GET_FUNC(Creature, IsOnBattleField, bool); export const char* CreatureLib_Creature_GetNickname(Creature* p) { return p->GetNickname().c_str(); } -export size_t CreatureLib_Creature_GetTypesCount(Creature* p) { return p->GetTypes().size(); } -export const uint8_t* CreatureLib_Creature_GetTypes(Creature* p) { return p->GetTypes().data(); } +export size_t CreatureLib_Creature_GetTypesCount(Creature* p) { return p->GetTypes().Count(); } +export const uint8_t* CreatureLib_Creature_GetTypes(Creature* p) { return p->GetTypes().RawData(); } export bool CreatureLib_Creature_HasType(Creature* p, uint8_t type) { return p->HasType(type); } SIMPLE_GET_FUNC(Creature, GetMaxHealth, uint32_t); export void CreatureLib_Creature_ChangeLevelBy(Creature* p, int8_t level) { return p->ChangeLevelBy(level); } @@ -64,8 +64,7 @@ export void CreatureLib_Creature_AddVolatileScriptByName(Creature* p, const char return p->AddVolatileScript(ConstString(scriptName)); } export void CreatureLib_Creature_AddVolatileScript(Creature* p, Script* script) { - return p->AddVolatileScript(script); -} + return p->AddVolatileScript(script); } export void CreatureLib_Creature_RemoveVolatileScriptByName(Creature* p, const char* scriptName) { return p->RemoveVolatileScript(ConstString(scriptName)); } @@ -75,8 +74,8 @@ export void CreatureLib_Creature_RemoveVolatileScript(Creature* p, Script* scrip export bool CreatureLib_Creature_HasVolatileScript(Creature* p, const char* scriptName) { return p->HasVolatileScript(ConstString(scriptName)); } -export size_t CreatureLib_Creature_GetAttacksCount(Creature* p) { return p->GetAttacks().size(); } -export LearnedAttack** CreatureLib_Creature_GetAttacks(Creature* p) { return p->GetAttacks().data(); } +export size_t CreatureLib_Creature_GetAttacksCount(Creature* p) { return p->GetAttacks().Count(); } +export LearnedAttack* const* CreatureLib_Creature_GetAttacks(Creature* p) { return p->GetAttacks().RawData(); } SIMPLE_GET_FUNC(Creature, GetDisplaySpecies, const CreatureLib::Library::CreatureSpecies*); SIMPLE_GET_FUNC(Creature, GetDisplayVariant, const CreatureLib::Library::SpeciesVariant*); export void CreatureLib_Creature_SetDisplaySpecies(Creature* p, const CreatureLib::Library::CreatureSpecies* species) { diff --git a/CInterface/Battling/CreatureParty.cpp b/CInterface/Battling/CreatureParty.cpp index fbddfdf..a3d3b6f 100644 --- a/CInterface/Battling/CreatureParty.cpp +++ b/CInterface/Battling/CreatureParty.cpp @@ -4,7 +4,7 @@ using namespace CreatureLib::Battling; export CreatureParty* CreatureLib_CreatureParty_ConstructWithSize(size_t size) { return new CreatureParty(size); } export CreatureParty* CreatureLib_CreatureParty_ConstructFromArray(Creature* creatures[], size_t size) { - return new CreatureParty(std::vector(creatures, creatures + size)); + return new CreatureParty(Arbutils::Collections::List(creatures, creatures + size)); } export void CreatureLib_CreatureParty_Destruct(const CreatureParty* p) { delete p; } @@ -21,4 +21,4 @@ export bool CreatureLib_CreatureParty_HasAvailableCreatures(const CreatureParty* export size_t CreatureLib_CreatureParty_GetLength(const CreatureParty* p) { return p->GetLength(); } -export Creature** CreatureLib_CreatureParty_GetParty(CreatureParty* p) { return p->GetParty().data(); } +export Creature* const* CreatureLib_CreatureParty_GetParty(CreatureParty* p) { return p->GetParty().RawData(); } diff --git a/CInterface/Library/GrowthRate.cpp b/CInterface/Library/GrowthRate.cpp index ebb0065..dc4d329 100644 --- a/CInterface/Library/GrowthRate.cpp +++ b/CInterface/Library/GrowthRate.cpp @@ -5,7 +5,7 @@ using namespace CreatureLib::Library; export GrowthRate* CreatureLib_LookupGrowthRate_Construct(uint32_t experiencePerLevel[], size_t count) { - std::vector exp(experiencePerLevel, experiencePerLevel + count); + Arbutils::Collections::List exp(experiencePerLevel, experiencePerLevel + count); return new LookupGrowthRate(exp); }; diff --git a/CInterface/Library/LearnableAttacks.cpp b/CInterface/Library/LearnableAttacks.cpp index 1995642..a3391b8 100644 --- a/CInterface/Library/LearnableAttacks.cpp +++ b/CInterface/Library/LearnableAttacks.cpp @@ -13,8 +13,8 @@ export void CreatureLib_LearnableAttacks_AddLevelMove(LearnableAttacks* p, uint8 } export const AttackData* const* CreatureLib_LearnableAttacks_GetAttacksForLevel(LearnableAttacks* p, uint8_t level) { - return p->GetAttacksForLevel(level).data(); + return p->GetAttacksForLevel(level).RawData(); } export size_t CreatureLib_LearnableAttacks_GetAttacksForLevelCount(LearnableAttacks* p, uint8_t level) { - return p->GetAttacksForLevel(level).size(); + return p->GetAttacksForLevel(level).Count(); } \ No newline at end of file diff --git a/CInterface/Library/SpeciesVariant.cpp b/CInterface/Library/SpeciesVariant.cpp index 17d4b16..3593024 100644 --- a/CInterface/Library/SpeciesVariant.cpp +++ b/CInterface/Library/SpeciesVariant.cpp @@ -10,17 +10,17 @@ export SpeciesVariant* CreatureLib_SpeciesVariant_Construct( uint16_t baseMagicalDefense, uint8_t baseSpeed, const char* talents[], size_t talentsLength, const char* secretTalents[], size_t secretTalentsLength, const LearnableAttacks* attacks) { - auto talentsWrapped = std::vector(talentsLength); + auto talentsWrapped = Arbutils::Collections::List(talentsLength); for (size_t i = 0; i < talentsLength; i++) { talentsWrapped[i] = ConstString(talents[i]); } - auto secretTalentsWrapped = std::vector(secretTalentsLength); + auto secretTalentsWrapped = Arbutils::Collections::List(secretTalentsLength); for (size_t i = 0; i < secretTalentsLength; i++) { secretTalentsWrapped[i] = ConstString(secretTalents[i]); } return new SpeciesVariant(Arbutils::CaseInsensitiveConstString(name), height, weight, baseExperience, - std::vector(types, types + typeLength), + Arbutils::Collections::List(types, types + typeLength), CreatureLib::Library::StatisticSet(baseHealth, baseAttack, baseDefense, baseMagicalAttack, baseMagicalDefense, baseSpeed), diff --git a/CInterface/Library/TypeLibrary.cpp b/CInterface/Library/TypeLibrary.cpp index 7203324..33dde15 100644 --- a/CInterface/Library/TypeLibrary.cpp +++ b/CInterface/Library/TypeLibrary.cpp @@ -33,5 +33,5 @@ export float CreatureLib_TypeLibrary_GetSingleEffectiveness(TypeLibrary* p, uint export float CreatureLib_TypeLibrary_GetEffectiveness(TypeLibrary* p, uint8_t attacking, uint8_t defensive[], size_t defensiveCount) { - return p->GetEffectiveness(attacking, std::vector(defensive, defensive + defensiveCount)); + return p->GetEffectiveness(attacking, List(defensive, defensive + defensiveCount)); } diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index fd1320c..e502ee3 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -62,7 +62,7 @@ void TurnHandler::ExecuteAttackChoice(AttackTurnChoice* choice) { // FIXME: Resolve all targets auto target = choice->GetUser()->GetBattle()->GetCreature(choice->GetTarget()); - std::vector targets = {target}; + Arbutils::Collections::List targets = {target}; auto attack = new ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript()); bool prevented = false; diff --git a/src/Battling/Models/Battle.cpp b/src/Battling/Models/Battle.cpp index cfff52c..9302ea5 100644 --- a/src/Battling/Models/Battle.cpp +++ b/src/Battling/Models/Battle.cpp @@ -84,7 +84,7 @@ bool Battle::CreatureInField(const Creature* creature) const { void Battle::ForceRecall(uint8_t side, uint8_t index) { _sides[side]->SetCreature(nullptr, index); } -void Battle::GetActiveScripts(std::vector& scripts) { scripts.emplace_back(&_volatile); } +void Battle::GetActiveScripts(Arbutils::Collections::List& scripts) { scripts.Append(&_volatile); } void Battle::SwitchCreature(uint8_t sideIndex, uint8_t index, Creature* c) { AssertNotNull(c) @@ -105,7 +105,7 @@ bool Battle::CanSlotBeFilled(uint8_t side, uint8_t index) const { void Battle::ValidateBattleState() { bool survivingSideExists = false; uint8_t winningSide = 0; - for (uint8_t i = 0; i < _sides.size(); i++) { + for (uint8_t i = 0; i < _sides.Count(); i++) { auto side = _sides[i]; if (side->HasFled()) { this->_battleResult = BattleResult::Inconclusive(); diff --git a/src/Battling/Models/Battle.hpp b/src/Battling/Models/Battle.hpp index 803e9d8..307503f 100644 --- a/src/Battling/Models/Battle.hpp +++ b/src/Battling/Models/Battle.hpp @@ -2,7 +2,7 @@ #define CREATURELIB_BATTLE_HPP #include -#include +#include #include "../EventHooks/EventHook.hpp" #include "../Flow/ChoiceQueue.hpp" #include "../Library/BattleLibrary.hpp" @@ -13,14 +13,16 @@ #include "BattleSide.hpp" #include "CreatureIndex.hpp" +using namespace Arbutils::Collections; + namespace CreatureLib::Battling { class Battle : public ScriptSource { const BattleLibrary* _library; - std::vector _parties; + List _parties; bool _canFlee; uint8_t _numberOfSides; uint8_t _creaturesPerSide; - std::vector _sides; + List _sides; BattleRandom _random; ChoiceQueue* _currentTurnQueue = nullptr; bool _hasEnded = false; @@ -31,15 +33,15 @@ namespace CreatureLib::Battling { ScriptSet _volatile; public: - Battle(const BattleLibrary* library, std::vector parties, bool canFlee = true, - uint8_t numberOfSides = 2, uint8_t creaturesPerSide = 1) + Battle(const BattleLibrary* library, List parties, bool canFlee = true, uint8_t numberOfSides = 2, + uint8_t creaturesPerSide = 1) : _library(library), _parties(std::move(parties)), _canFlee(canFlee), _numberOfSides(numberOfSides), _creaturesPerSide(creaturesPerSide) { AssertNotNull(_library) for (auto p : parties) AssertNotNull(p) - _sides = std::vector(numberOfSides); + _sides = List(numberOfSides); for (size_t i = 0; i < numberOfSides; i++) { _sides[i] = new BattleSide(i, this, creaturesPerSide); } @@ -71,7 +73,6 @@ namespace CreatureLib::Battling { bool CreatureInField(const Creature* creature) const; Creature* GetCreature(const CreatureIndex& target) const { - Assert(target.GetSideIndex() < _sides.size()) return _sides[target.GetSideIndex()]->GetCreature(target.GetCreatureIndex()); } Creature* GetCreature(uint8_t side, uint8_t target) const { return _sides[side]->GetCreature(target); } @@ -80,13 +81,13 @@ namespace CreatureLib::Battling { void SwitchCreature(uint8_t side, uint8_t index, Creature* c); bool CanSlotBeFilled(uint8_t side, uint8_t index) const; - void GetActiveScripts(std::vector& scripts) override; + void GetActiveScripts(Arbutils::Collections::List& scripts) override; void ValidateBattleState(); inline bool HasEnded() const { return _hasEnded; } inline const BattleResult& GetResult() const { return _battleResult; } - const std::vector& GetSides() const { return _sides; } + const List& GetSides() const { return _sides; } Script* GetVolatileScript(const ConstString& key) const { return _volatile.Get(key); } Script* GetVolatileScript(uint32_t keyHash) const { return _volatile.Get(keyHash); } void AddVolatileScript(const ConstString& key); diff --git a/src/Battling/Models/BattleParty.hpp b/src/Battling/Models/BattleParty.hpp index f072360..402d84e 100644 --- a/src/Battling/Models/BattleParty.hpp +++ b/src/Battling/Models/BattleParty.hpp @@ -2,28 +2,28 @@ #define CREATURELIB_BATTLEPARTY_HPP #include +#include #include "CreatureIndex.hpp" #include "CreatureParty.hpp" + +using namespace Arbutils::Collections; + namespace CreatureLib::Battling { class BattleParty { CreatureParty* _party; - std::vector _responsibleIndices; + List _responsibleIndices; public: - BattleParty(CreatureParty* party, std::vector responsibleIndices) + BattleParty(CreatureParty* party, const List& responsibleIndices) : _party(party), _responsibleIndices(responsibleIndices) { AssertNotNull(_party) } inline CreatureParty* GetParty() const { return _party; } - inline const std::vector& GetResponsibleIndices() const { return _responsibleIndices; } + inline const List& GetResponsibleIndices() const { return _responsibleIndices; } inline bool IsResponsibleForIndex(const CreatureIndex& index) const { - for (const auto& i : _responsibleIndices) { - if (i == index) - return true; - } - return false; + return _responsibleIndices.Contains(index); } inline bool IsResponsibleForIndex(uint8_t side, uint8_t index) const { diff --git a/src/Battling/Models/BattleSide.cpp b/src/Battling/Models/BattleSide.cpp index d2d2f86..5dd4eec 100644 --- a/src/Battling/Models/BattleSide.cpp +++ b/src/Battling/Models/BattleSide.cpp @@ -9,7 +9,7 @@ bool BattleSide::AllChoicesSet() const { return _choicesSet == _creaturesPerSide bool BattleSide::AllPossibleSlotsFilled() const { AssertNotNull(_battle) - for (size_t i = 0; i < _creatures.size(); i++) { + for (size_t i = 0; i < _creatures.Count(); i++) { auto c = _creatures[i]; if (c == nullptr || c->IsFainted()) { if (_battle->CanSlotBeFilled(_index, i)) @@ -26,7 +26,7 @@ void BattleSide::ResetChoices() { } } -const std::vector& BattleSide::GetChoices() const { return _choices; } +const List& BattleSide::GetChoices() const { return _choices; } void BattleSide::SetChoice(BaseTurnChoice* choice) { AssertNotNull(choice) @@ -68,8 +68,8 @@ bool BattleSide::CreatureOnSide(const Creature* creature) const { Creature* BattleSide::GetCreature(uint8_t index) const { return _creatures[index]; } -void BattleSide::GetActiveScripts(std::vector& scripts) { - scripts.emplace_back(&_volatile); +void BattleSide::GetActiveScripts(Arbutils::Collections::List& scripts) { + scripts.Append(&_volatile); _battle->GetActiveScripts(scripts); } uint8_t BattleSide::GetRandomCreatureIndex() { diff --git a/src/Battling/Models/BattleSide.hpp b/src/Battling/Models/BattleSide.hpp index 428b604..914343d 100644 --- a/src/Battling/Models/BattleSide.hpp +++ b/src/Battling/Models/BattleSide.hpp @@ -2,17 +2,20 @@ #define CREATURELIB_BATTLESIDE_HPP #include +#include #include #include "../TurnChoices/BaseTurnChoice.hpp" #include "Creature.hpp" +using namespace Arbutils::Collections; + namespace CreatureLib::Battling { class BattleSide : public ScriptSource { uint8_t _index; uint8_t _creaturesPerSide; - std::vector _creatures; - std::vector _choices; - std::vector _fillableSlots; + List _creatures; + List _choices; + List _fillableSlots; uint8_t _choicesSet = 0; ScriptSet _volatile; Battle* _battle; @@ -33,7 +36,7 @@ namespace CreatureLib::Battling { virtual ~BattleSide() = default; [[nodiscard]] bool AllChoicesSet() const; - [[nodiscard]] const std::vector& GetChoices() const; + [[nodiscard]] const List& GetChoices() const; [[nodiscard]] bool AllPossibleSlotsFilled() const; @@ -45,13 +48,13 @@ namespace CreatureLib::Battling { Creature* GetCreature(uint8_t index) const; bool CreatureOnSide(const Creature* creature) const; - void GetActiveScripts(std::vector& scripts) final; + void GetActiveScripts(Arbutils::Collections::List& scripts) final; - const std::vector& GetCreatures() { return _creatures; } + const List& GetCreatures() { return _creatures; } uint8_t GetSideIndex() { return _index; } uint8_t GetCreatureIndex(Creature* c) { - for (size_t i = 0; i < _creatures.size(); i++) { + for (size_t i = 0; i < _creatures.Count(); i++) { if (c == _creatures[i]) return i; } @@ -61,7 +64,7 @@ namespace CreatureLib::Battling { void MarkSlotAsUnfillable(Creature* creature) { for (uint8_t i = 0; i < _creaturesPerSide; i++) { if (_creatures[i] == creature) { - _fillableSlots[i] = false; + _fillableSlots.At(i) = false; return; } } diff --git a/src/Battling/Models/CreateCreature.cpp b/src/Battling/Models/CreateCreature.cpp index e264071..178d283 100644 --- a/src/Battling/Models/CreateCreature.cpp +++ b/src/Battling/Models/CreateCreature.cpp @@ -22,11 +22,11 @@ CreateCreature* CreateCreature::WithGender(Library::Gender gender) { CreateCreature* CreateCreature::WithAttack(const Arbutils::CaseInsensitiveConstString& attackName, AttackLearnMethod learnMethod) { - if (_attacks.size() >= _library->GetSettings()->GetMaximalMoves()) + if (_attacks.Count() >= _library->GetSettings()->GetMaximalMoves()) throw CreatureException("You have already set the maximum amount of allowed moves."); auto attackData = _library->GetAttackLibrary()->Get(attackName); - _attacks.emplace_back(attackData, learnMethod); + _attacks.Append(std::tuple(attackData, learnMethod)); return this; } @@ -56,8 +56,8 @@ Creature* CreateCreature::Create() { } auto experience = _library->GetGrowthRateLibrary()->CalculateExperience(species->GetGrowthRate(), _level); - auto attacks = std::vector(_attacks.size()); - for (size_t i = 0; i < attacks.size(); i++) { + auto attacks = List(_attacks.Count()); + for (size_t i = 0; i < attacks.Count(); i++) { auto kv = _attacks[i]; attacks[i] = new LearnedAttack(std::get<0>(kv), std::get<1>(kv)); } diff --git a/src/Battling/Models/CreateCreature.hpp b/src/Battling/Models/CreateCreature.hpp index 824870b..d9a2830 100644 --- a/src/Battling/Models/CreateCreature.hpp +++ b/src/Battling/Models/CreateCreature.hpp @@ -1,9 +1,12 @@ #ifndef CREATURELIB_CREATECREATURE_HPP #define CREATURELIB_CREATECREATURE_HPP +#include #include "../../Library/DataLibrary.hpp" #include "Creature.hpp" +using namespace Arbutils::Collections; + namespace CreatureLib::Battling { class CreateCreature { const BattleLibrary* _library; @@ -17,7 +20,7 @@ namespace CreatureLib::Battling { uint8_t _coloring = 0; Arbutils::CaseInsensitiveConstString _heldItem = ""_cnc; uint32_t _identifier = 0; - std::vector> _attacks = {}; + List> _attacks = {}; public: CreateCreature(const BattleLibrary* library, std::string species, uint8_t level) diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index 80af431..5e2add3 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -9,8 +9,7 @@ using namespace CreatureLib; Battling::Creature::Creature(const BattleLibrary* library, const Library::CreatureSpecies* species, const Library::SpeciesVariant* variant, uint8_t level, uint32_t experience, uint32_t uid, Library::Gender gender, uint8_t coloring, const Library::Item* heldItem, - std::string nickname, const Library::TalentIndex& talent, - std::vector attacks) + std::string nickname, const Library::TalentIndex& talent, List attacks) : _library(library), _species(species), _variant(variant), _level(level), _experience(experience), _uniqueIdentifier(uid), _gender(gender), _coloring(coloring), _heldItem(heldItem), _nickname(std::move(nickname)), _talentIndex(talent), _hasOverridenTalent(false), _attacks(std::move(attacks)) { @@ -143,7 +142,7 @@ void Battling::Creature::OverrideActiveTalent(const ConstString& talent) { _activeTalent = this->_library->LoadScript(ScriptCategory::Talent, talent); } -const std::vector& Battling::Creature::GetTypes() const noexcept { +const List& Battling::Creature::GetTypes() const noexcept { // HOOK: override types. return this->_variant->GetTypes(); } @@ -153,10 +152,10 @@ bool Battling::Creature::HasType(uint8_t type) const noexcept { return std::find(t.begin(), t.end(), type) != t.end(); } -void Battling::Creature::GetActiveScripts(std::vector& scripts) { - scripts.emplace_back(&_activeTalent); - scripts.emplace_back(&_status); - scripts.emplace_back(&_volatile); +void Battling::Creature::GetActiveScripts(Arbutils::Collections::List& scripts) { + scripts.Append(&_activeTalent); + scripts.Append(&_status); + scripts.Append(&_volatile); if (_side != nullptr) { _side->GetActiveScripts(scripts); } diff --git a/src/Battling/Models/Creature.hpp b/src/Battling/Models/Creature.hpp index db53bbb..f713f4a 100644 --- a/src/Battling/Models/Creature.hpp +++ b/src/Battling/Models/Creature.hpp @@ -1,6 +1,7 @@ #ifndef CREATURELIB_BATTLECREATURE_HPP #define CREATURELIB_BATTLECREATURE_HPP +#include #include "../../Library/CreatureData/CreatureSpecies.hpp" #include "../../Library/Items/Item.hpp" #include "../ScriptHandling/ScriptAggregator.hpp" @@ -9,6 +10,8 @@ #include "DamageSource.hpp" #include "LearnedAttack.hpp" +using namespace Arbutils::Collections; + namespace CreatureLib::Battling { // Forward declare battle class class Battle; @@ -49,7 +52,7 @@ namespace CreatureLib::Battling { ConstString _overridenTalentName = ""_cnc; std::unordered_set _seenOpponents = {}; - std::vector _attacks; + List _attacks; Script* _status = nullptr; ScriptSet _volatile = {}; @@ -61,7 +64,7 @@ namespace CreatureLib::Battling { Creature(const BattleLibrary* library, const Library::CreatureSpecies* species, const Library::SpeciesVariant* variant, uint8_t level, uint32_t experience, uint32_t uid, Library::Gender gender, uint8_t coloring, const Library::Item* heldItem, std::string nickname, - const Library::TalentIndex& talent, std::vector attacks); + const Library::TalentIndex& talent, List attacks); virtual ~Creature() { for (auto attack : _attacks) { @@ -105,7 +108,7 @@ namespace CreatureLib::Battling { const ConstString& GetActiveTalent() const; [[nodiscard]] bool IsFainted() const noexcept; - [[nodiscard]] const std::vector& GetTypes() const noexcept; + [[nodiscard]] const List& GetTypes() const noexcept; [[nodiscard]] bool HasType(uint8_t type) const noexcept; uint32_t GetMaxHealth() const noexcept { return _boostedStats.GetHealth(); } @@ -118,7 +121,7 @@ namespace CreatureLib::Battling { void MarkOpponentAsSeen(Creature* creature) { _seenOpponents.insert(creature); } const std::unordered_set& GetSeenOpponents() const { return _seenOpponents; } - void GetActiveScripts(std::vector& scripts) override; + void GetActiveScripts(Arbutils::Collections::List& scripts) override; void ClearVolatileScripts(); void AddVolatileScript(const ConstString& name); void AddVolatileScript(Script* script); @@ -126,7 +129,7 @@ namespace CreatureLib::Battling { void RemoveVolatileScript(Script* script); bool HasVolatileScript(const ConstString& name) const; - std::vector& GetAttacks() { return _attacks; } + List& GetAttacks() { return _attacks; } const Library::CreatureSpecies* GetDisplaySpecies() const; const Library::SpeciesVariant* GetDisplayVariant() const; diff --git a/src/Battling/Models/CreatureParty.hpp b/src/Battling/Models/CreatureParty.hpp index a999d5b..5982085 100644 --- a/src/Battling/Models/CreatureParty.hpp +++ b/src/Battling/Models/CreatureParty.hpp @@ -1,16 +1,16 @@ #ifndef CREATURELIB_CREATUREPARTY_HPP #define CREATURELIB_CREATUREPARTY_HPP -#include +#include #include "Creature.hpp" namespace CreatureLib::Battling { class CreatureParty { - std::vector _party; + Arbutils::Collections::List _party; public: CreatureParty(size_t size) : _party(size) {} - CreatureParty(std::vector party) : _party(party) {} + CreatureParty(Arbutils::Collections::List party) : _party(party) {} CreatureParty(std::initializer_list party) : _party(party) {} virtual ~CreatureParty() { @@ -38,10 +38,10 @@ namespace CreatureLib::Battling { return false; } - std::vector& GetParty() { return _party; } - const std::vector& GetParty() const { return _party; } + Arbutils::Collections::List& GetParty() { return _party; } + const Arbutils::Collections::List& GetParty() const { return _party; } - size_t GetLength() const { return _party.size(); } + size_t GetLength() const { return _party.Count(); } }; } diff --git a/src/Battling/Models/ExecutingAttack.hpp b/src/Battling/Models/ExecutingAttack.hpp index 70eef51..5b5718a 100644 --- a/src/Battling/Models/ExecutingAttack.hpp +++ b/src/Battling/Models/ExecutingAttack.hpp @@ -2,11 +2,15 @@ #define CREATURELIB_EXECUTINGATTACK_HPP #include +#include +#include #include #include #include #include "Creature.hpp" +using namespace Arbutils::Collections; + namespace CreatureLib::Battling { class ExecutingAttack : public ScriptSource { public: @@ -35,7 +39,7 @@ namespace CreatureLib::Battling { class TargetData { bool _isHit = true; - std::vector _hits; + List _hits; public: explicit TargetData(uint8_t numberOfHits) : _hits(numberOfHits) { @@ -47,26 +51,25 @@ namespace CreatureLib::Battling { HitData* GetHit(uint8_t index) { return &_hits[index]; } - uint8_t GetNumberOfHits() const { return _hits.size(); } + uint8_t GetNumberOfHits() const { return _hits.Count(); } bool IsHit() const { return _isHit; } }; private: - std::unordered_map _targets; + Dictionary _targets; Creature* _user; LearnedAttack* _attack; Script* _script; public: - ExecutingAttack(const std::vector& targets, uint8_t numberHits, Creature* user, - LearnedAttack* attack, Script* script) - : _user(user), _attack(attack), _script(script) { + ExecutingAttack(const List& targets, uint8_t numberHits, Creature* user, LearnedAttack* attack, + Script* script) + : _targets(targets.Count()), _user(user), _attack(attack), _script(script) { AssertNotNull(user) AssertNotNull(attack) - _targets.reserve(targets.size()); for (auto target : targets) { - _targets.insert({target, TargetData(numberHits)}); + _targets.Insert(target, TargetData(numberHits)); } } @@ -74,17 +77,17 @@ namespace CreatureLib::Battling { TargetData* GetAttackDataForTarget(Creature* creature) { return &_targets[creature]; } - bool IsCreatureTarget(Creature* creature) { return _targets.find(creature) != _targets.end(); } + bool IsCreatureTarget(Creature* creature) { return _targets.Has(creature); } - std::unordered_map& GetTargets() { return _targets; } + Dictionary& GetTargets() { return _targets; } Creature* GetUser() { return _user; } LearnedAttack* GetAttack() { return _attack; } protected: - void GetActiveScripts(std::vector& scripts) override { - scripts.emplace_back(&_script); + void GetActiveScripts(Arbutils::Collections::List& scripts) override { + scripts.Append(&_script); _user->GetActiveScripts(scripts); } }; diff --git a/src/Battling/ScriptHandling/ScriptAggregator.hpp b/src/Battling/ScriptHandling/ScriptAggregator.hpp index ee0a8be..57a683a 100644 --- a/src/Battling/ScriptHandling/ScriptAggregator.hpp +++ b/src/Battling/ScriptHandling/ScriptAggregator.hpp @@ -1,41 +1,42 @@ #ifndef CREATURELIB_SCRIPTAGGREGATOR_HPP #define CREATURELIB_SCRIPTAGGREGATOR_HPP -#include -#include +#include #include "../../Library/Exceptions/NotReachableException.hpp" #include "Script.hpp" #include "ScriptSet.hpp" #include "ScriptWrapper.hpp" +using namespace Arbutils::Collections; + namespace CreatureLib::Battling { class ScriptAggregator { - std::vector _scripts; + List _scripts; size_t _index = 0; bool _isSetSet = false; - const std::vector* _setScripts; + const List* _setScripts; size_t _setIndex; public: - ScriptAggregator(std::vector scripts) : _scripts(std::move(scripts)){}; + ScriptAggregator(List scripts) : _scripts(std::move(scripts)){}; - bool HasNext() { return _index < _scripts.size() || (_isSetSet && _setIndex < _setScripts->size()); } + bool HasNext() { return _index < _scripts.Count() || (_isSetSet && _setIndex < _setScripts->Count()); } Script* GetNext() { // We can probably do this in a cleaner version once C++ 20 drops with Coroutine support. if (_isSetSet) { - if (_setIndex >= _setScripts->size()) { + if (_setIndex >= _setScripts->Count()) { _isSetSet = false; return GetNext(); } - auto s = _setScripts->at(_setIndex); + auto s = _setScripts->At(_setIndex); _setIndex++; - if (_setIndex >= _setScripts->size()) { + if (_setIndex >= _setScripts->Count()) { _isSetSet = false; } return s; } - if (_index >= _scripts.size()) + if (_index >= _scripts.Count()) return nullptr; auto next = _scripts[_index]; _index++; diff --git a/src/Battling/ScriptHandling/ScriptSet.hpp b/src/Battling/ScriptHandling/ScriptSet.hpp index 4038980..c0f9aa9 100644 --- a/src/Battling/ScriptHandling/ScriptSet.hpp +++ b/src/Battling/ScriptHandling/ScriptSet.hpp @@ -7,7 +7,7 @@ namespace CreatureLib::Battling { class ScriptSet { - std::vector _scripts; + Arbutils::Collections::List _scripts; std::unordered_map _lookup; public: @@ -24,8 +24,8 @@ namespace CreatureLib::Battling { delete script; return; } - _scripts.push_back(script); - _lookup.insert({script->GetName(), _scripts.size() - 1}); + _scripts.Append(script); + _lookup.insert({script->GetName(), _scripts.Count() - 1}); } Script* Get(const ConstString& key) const { return Get(key.GetHash()); } @@ -46,7 +46,7 @@ namespace CreatureLib::Battling { auto script = _scripts[find->second]; script->OnRemove(); delete script; - _scripts.erase(_scripts.begin() + find.operator*().second); + _scripts.Remove(find.operator*().second); _lookup.erase(keyHash); } } @@ -55,7 +55,7 @@ namespace CreatureLib::Battling { for (auto s : _scripts) { delete s; } - _scripts.clear(); + _scripts.Clear(); _lookup.clear(); } @@ -63,9 +63,9 @@ namespace CreatureLib::Battling { bool Has(uint32_t keyHash) const { return _lookup.find(keyHash) != _lookup.end(); } - size_t Count() const { return _scripts.size(); } + size_t Count() const { return _scripts.Count(); } - const std::vector* GetIterator() const { return &_scripts; } + const Arbutils::Collections::List* GetIterator() const { return &_scripts; } }; } diff --git a/src/Battling/ScriptHandling/ScriptSource.hpp b/src/Battling/ScriptHandling/ScriptSource.hpp index 0403cb7..a1aa195 100644 --- a/src/Battling/ScriptHandling/ScriptSource.hpp +++ b/src/Battling/ScriptHandling/ScriptSource.hpp @@ -8,13 +8,13 @@ namespace CreatureLib::Battling { class ScriptSource { bool _areScriptsInitialized = false; - std::vector _scripts; + Arbutils::Collections::List _scripts; protected: - virtual void GetActiveScripts(std::vector& scripts) = 0; + virtual void GetActiveScripts(Arbutils::Collections::List& scripts) = 0; void ResetActiveScripts() { _areScriptsInitialized = false; - _scripts.clear(); + _scripts.Clear(); } public: diff --git a/src/Battling/TurnChoices/AttackTurnChoice.hpp b/src/Battling/TurnChoices/AttackTurnChoice.hpp index 964f4da..dd9ce1e 100644 --- a/src/Battling/TurnChoices/AttackTurnChoice.hpp +++ b/src/Battling/TurnChoices/AttackTurnChoice.hpp @@ -49,8 +49,8 @@ namespace CreatureLib::Battling { Script* GetAttackScript() const { return _attackScript; } protected: - void GetActiveScripts(std::vector& scripts) override { - scripts.emplace_back(&_attackScript); + void GetActiveScripts(Arbutils::Collections::List& scripts) override { + scripts.Append(&_attackScript); GetUser()->GetActiveScripts(scripts); } }; diff --git a/src/Battling/TurnChoices/FleeTurnChoice.hpp b/src/Battling/TurnChoices/FleeTurnChoice.hpp index 6d7823e..7f975c7 100644 --- a/src/Battling/TurnChoices/FleeTurnChoice.hpp +++ b/src/Battling/TurnChoices/FleeTurnChoice.hpp @@ -12,7 +12,9 @@ namespace CreatureLib::Battling { TurnChoiceKind GetKind() const override { return TurnChoiceKind ::Flee; } protected: - void GetActiveScripts(std::vector& scripts) override { GetUser()->GetActiveScripts(scripts); } + void GetActiveScripts(Arbutils::Collections::List& scripts) override { + GetUser()->GetActiveScripts(scripts); + } }; } diff --git a/src/Battling/TurnChoices/PassTurnChoice.hpp b/src/Battling/TurnChoices/PassTurnChoice.hpp index c569dcb..a8cceb9 100644 --- a/src/Battling/TurnChoices/PassTurnChoice.hpp +++ b/src/Battling/TurnChoices/PassTurnChoice.hpp @@ -11,7 +11,9 @@ namespace CreatureLib::Battling { TurnChoiceKind GetKind() const override { return TurnChoiceKind ::Pass; } protected: - void GetActiveScripts(std::vector& scripts) override { GetUser()->GetActiveScripts(scripts); } + void GetActiveScripts(Arbutils::Collections::List& scripts) override { + GetUser()->GetActiveScripts(scripts); + } }; } diff --git a/src/Battling/TurnChoices/SwitchTurnChoice.hpp b/src/Battling/TurnChoices/SwitchTurnChoice.hpp index 90b931a..2203edf 100644 --- a/src/Battling/TurnChoices/SwitchTurnChoice.hpp +++ b/src/Battling/TurnChoices/SwitchTurnChoice.hpp @@ -16,7 +16,9 @@ namespace CreatureLib::Battling { inline Creature* GetNewCreature() const { return _newCreature; } protected: - void GetActiveScripts(std::vector& scripts) override { GetUser()->GetActiveScripts(scripts); } + void GetActiveScripts(Arbutils::Collections::List& scripts) override { + GetUser()->GetActiveScripts(scripts); + } }; } diff --git a/src/Library/BaseLibrary.hpp b/src/Library/BaseLibrary.hpp index 951ab73..7a66aac 100644 --- a/src/Library/BaseLibrary.hpp +++ b/src/Library/BaseLibrary.hpp @@ -2,6 +2,7 @@ #define CREATURELIB_BASELIBRARY_HPP #include +#include #include #include #include @@ -9,7 +10,7 @@ namespace CreatureLib::Library { template class BaseLibrary { - std::unordered_map _values; + Arbutils::Collections::Dictionary _values; public: BaseLibrary(size_t initialCapacity = 32) : _values(initialCapacity) {} @@ -18,46 +19,40 @@ namespace CreatureLib::Library { for (const auto& v : _values) { delete v.second; } - _values.clear(); + _values.Clear(); } inline void Insert(const Arbutils::CaseInsensitiveConstString& key, const T* value) { AssertNotNull(value) - _values.insert({key.GetHash(), value}); + _values.Insert(key.GetHash(), value); } inline void Insert(uint32_t hashedKey, const T* value) { AssertNotNull(value) - _values.insert({hashedKey, value}); + _values.Insert(hashedKey, value); } inline void Delete(const Arbutils::CaseInsensitiveConstString& key) { _values.erase(key.GetHash()); } - inline void Delete(uint32_t hashedKey) { _values.erase({hashedKey}); } + inline void Delete(uint32_t hashedKey) { _values.Remove(hashedKey); } bool TryGet(const Arbutils::CaseInsensitiveConstString& name, const T*& out) const { return TryGet(name.GetHash(), out); } - bool TryGet(uint32_t hashedKey, const T*& out) const { - auto find = this->_values.find(hashedKey); - if (find == this->_values.end()) { - out = nullptr; - return false; - } - out = find->second; - return true; - } + bool TryGet(uint32_t hashedKey, const T*& out) const { return _values.TryGet(hashedKey, out); } [[nodiscard]] inline const T* Get(const Arbutils::CaseInsensitiveConstString& name) const { - return _values.at(name.GetHash()); + return _values.Get(name.GetHash()); } - [[nodiscard]] inline const T* Get(uint32_t hashedKey) const { return _values.at(hashedKey); } + [[nodiscard]] inline const T* Get(uint32_t hashedKey) const { return _values.Get(hashedKey); } [[nodiscard]] inline const T* operator[](const Arbutils::CaseInsensitiveConstString& name) const { return Get(name); } [[nodiscard]] inline const T* operator[](uint32_t hashedKey) const { return Get(hashedKey); } - [[nodiscard]] inline const std::unordered_map& GetIterator() const { return _values; } + [[nodiscard]] inline const Arbutils::Collections::Dictionary& GetIterator() const { + return _values; + } - [[nodiscard]] size_t GetCount() const { return _values.size(); } + [[nodiscard]] size_t GetCount() const { return _values.Count(); } }; } diff --git a/src/Library/CreatureData/CreatureSpecies.cpp b/src/Library/CreatureData/CreatureSpecies.cpp index e2d3ec5..69e2e4e 100644 --- a/src/Library/CreatureData/CreatureSpecies.cpp +++ b/src/Library/CreatureData/CreatureSpecies.cpp @@ -10,30 +10,21 @@ CreatureSpecies::CreatureSpecies(uint16_t id, const ConstString& name, const Spe AssertNotNull(defaultVariant) } -bool CreatureSpecies::HasVariant(const ConstString& name) const { return _variants.find(name) != _variants.end(); } +bool CreatureSpecies::HasVariant(const ConstString& name) const { return _variants.Has(name); } bool CreatureSpecies::TryGetVariant(const ConstString& name, const SpeciesVariant*& out) const { return TryGetVariant(name.GetHash(), out); } bool CreatureSpecies::TryGetVariant(uint32_t hash, const SpeciesVariant*& out) const { - auto find = _variants.find(hash); - if (find != _variants.end()) { - out = find->second; - return true; - } - return false; + return _variants.TryGet(hash, out); } -const SpeciesVariant* CreatureSpecies::GetVariant(const ConstString& name) const { return _variants.at(name); } -const SpeciesVariant* CreatureSpecies::GetVariant(uint32_t key) const { return _variants.at(key); } -bool CreatureSpecies::HasVariant(uint32_t hash) const { return _variants.at(hash); } +const SpeciesVariant* CreatureSpecies::GetVariant(const ConstString& name) const { return _variants.Get(name); } +const SpeciesVariant* CreatureSpecies::GetVariant(uint32_t key) const { return _variants.Get(key); } +bool CreatureSpecies::HasVariant(uint32_t hash) const { return _variants.Has(hash); } void CreatureSpecies::SetVariant(const ConstString& name, const SpeciesVariant* variant) { - auto find = _variants.find(name); - if (find != _variants.end()) { - delete find->second; - } - _variants[name] = variant; + _variants.Insert(name, variant); } Gender CreatureSpecies::GetRandomGender(Arbutils::Random& rand) const { diff --git a/src/Library/CreatureData/CreatureSpecies.hpp b/src/Library/CreatureData/CreatureSpecies.hpp index 04600dd..caa4b4a 100644 --- a/src/Library/CreatureData/CreatureSpecies.hpp +++ b/src/Library/CreatureData/CreatureSpecies.hpp @@ -1,6 +1,7 @@ #ifndef CREATURELIB_CREATURESPECIES_HPP #define CREATURELIB_CREATURESPECIES_HPP +#include #include #include #include @@ -20,7 +21,7 @@ namespace CreatureLib::Library { float _genderRate; const ConstString _growthRate; uint8_t _captureRate; - std::unordered_map _variants; + Arbutils::Collections::Dictionary _variants; public: CreatureSpecies(uint16_t id, const ConstString& name, const SpeciesVariant* defaultVariant, float genderRatio, @@ -29,7 +30,7 @@ namespace CreatureLib::Library { virtual ~CreatureSpecies() { for (auto v : _variants) delete v.second; - _variants.clear(); + _variants.Clear(); } inline uint16_t GetId() const { return _id; } @@ -48,7 +49,9 @@ namespace CreatureLib::Library { void SetVariant(const ConstString& name, const SpeciesVariant* variant); - const std::unordered_map& GetVariantsIterator() const { return _variants; } + const Arbutils::Collections::Dictionary& GetVariantsIterator() const { + return _variants; + } }; } diff --git a/src/Library/CreatureData/LearnableAttacks.cpp b/src/Library/CreatureData/LearnableAttacks.cpp index 6362e04..a5587f7 100644 --- a/src/Library/CreatureData/LearnableAttacks.cpp +++ b/src/Library/CreatureData/LearnableAttacks.cpp @@ -3,14 +3,16 @@ using namespace CreatureLib::Library; void LearnableAttacks::AddLevelMove(uint8_t level, const AttackData* attack) { - auto find = _learnedByLevel.find(level); - if (find != _learnedByLevel.end()) { - find->second.push_back(attack); + + List levelData; + if (_learnedByLevel.TryGet(level, levelData)) { + levelData.Append(attack); } else { - _learnedByLevel.insert({level, {attack}}); + levelData = {attack}; + _learnedByLevel.Insert(level, levelData); } } -const std::vector& LearnableAttacks::GetAttacksForLevel(uint8_t level) const { - return _learnedByLevel.at(level); +const List& LearnableAttacks::GetAttacksForLevel(uint8_t level) const { + return _learnedByLevel.Get(level); } diff --git a/src/Library/CreatureData/LearnableAttacks.hpp b/src/Library/CreatureData/LearnableAttacks.hpp index b242b22..eb92180 100644 --- a/src/Library/CreatureData/LearnableAttacks.hpp +++ b/src/Library/CreatureData/LearnableAttacks.hpp @@ -2,17 +2,20 @@ #define CREATURELIB_LEARNABLEATTACKS_HPP #include +#include +#include #include -#include #include "../Attacks/AttackData.hpp" +using namespace Arbutils::Collections; + namespace CreatureLib::Library { class LearnableAttacks { - std::unordered_map> _learnedByLevel; + Dictionary> _learnedByLevel; public: LearnableAttacks(size_t levelAttackCapacity) - : _learnedByLevel(std::unordered_map>(levelAttackCapacity)) { + : _learnedByLevel(Dictionary>(levelAttackCapacity)) { for (auto kv : _learnedByLevel) { for (auto attack : kv.second) AssertNotNull(attack) @@ -21,7 +24,7 @@ namespace CreatureLib::Library { void AddLevelMove(uint8_t level, const AttackData* attack); - const std::vector& GetAttacksForLevel(uint8_t level) const; + const List& GetAttacksForLevel(uint8_t level) const; }; } diff --git a/src/Library/CreatureData/SpeciesVariant.cpp b/src/Library/CreatureData/SpeciesVariant.cpp index 9abe62a..4c9d546 100644 --- a/src/Library/CreatureData/SpeciesVariant.cpp +++ b/src/Library/CreatureData/SpeciesVariant.cpp @@ -2,9 +2,9 @@ #include #include -const std::vector& CreatureLib::Library::SpeciesVariant::GetTypes() const { return _types; } +const List& CreatureLib::Library::SpeciesVariant::GetTypes() const { return _types; } -size_t CreatureLib::Library::SpeciesVariant::GetTypeCount() const { return _types.size(); } +size_t CreatureLib::Library::SpeciesVariant::GetTypeCount() const { return _types.Count(); } uint8_t CreatureLib::Library::SpeciesVariant::GetType(size_t index) const { return _types[index]; } @@ -14,13 +14,13 @@ uint32_t CreatureLib::Library::SpeciesVariant::GetStatistic(CreatureLib::Library const CreatureLib::Library::TalentIndex CreatureLib::Library::SpeciesVariant::GetTalentIndex(const ConstString& talent) const { - for (size_t i = 0; i < _talents.size(); i++) { - if (_talents.at(i) == talent) { + for (size_t i = 0; i < _talents.Count(); i++) { + if (_talents.At(i) == talent) { return TalentIndex(false, i); } } - for (size_t i = 0; i < _secretTalents.size(); i++) { - if (_secretTalents.at(i) == talent) { + for (size_t i = 0; i < _secretTalents.Count(); i++) { + if (_secretTalents.At(i) == talent) { return TalentIndex(true, i); } } @@ -28,7 +28,7 @@ CreatureLib::Library::SpeciesVariant::GetTalentIndex(const ConstString& talent) } CreatureLib::Library::TalentIndex CreatureLib::Library::SpeciesVariant::GetRandomTalent(Arbutils::Random* rand) const { - return TalentIndex(false, rand->Get(_talents.size())); + return TalentIndex(false, rand->Get(_talents.Count())); } const CreatureLib::Library::LearnableAttacks* CreatureLib::Library::SpeciesVariant::GetLearnableAttacks() const { @@ -36,10 +36,9 @@ const CreatureLib::Library::LearnableAttacks* CreatureLib::Library::SpeciesVaria } CreatureLib::Library::SpeciesVariant::SpeciesVariant(ConstString name, float height, float weight, - uint32_t baseExperience, std::vector types, + uint32_t baseExperience, List types, CreatureLib::Library::StatisticSet baseStats, - std::vector talents, - std::vector secretTalents, + List talents, List secretTalents, const LearnableAttacks* attacks) : _name(std::move(name)), _height(height), _weight(weight), _baseExperience(baseExperience), _types(std::move(types)), _baseStatistics(baseStats), _talents(std::move(talents)), diff --git a/src/Library/CreatureData/SpeciesVariant.hpp b/src/Library/CreatureData/SpeciesVariant.hpp index e708c08..1742aed 100644 --- a/src/Library/CreatureData/SpeciesVariant.hpp +++ b/src/Library/CreatureData/SpeciesVariant.hpp @@ -1,14 +1,15 @@ #ifndef CREATURELIB_SPECIESVARIANT_HPP #define CREATURELIB_SPECIESVARIANT_HPP +#include #include #include -#include #include "../StatisticSet.hpp" #include "CreatureMoves.hpp" #include "LearnableAttacks.hpp" #include "TalentIndex.hpp" using ConstString = Arbutils::CaseInsensitiveConstString; +using namespace Arbutils::Collections; namespace CreatureLib::Library { /*! @@ -22,17 +23,16 @@ namespace CreatureLib::Library { uint32_t _baseExperience; private: - std::vector _types; + List _types; Library::StatisticSet _baseStatistics; - std::vector _talents; - std::vector _secretTalents; + List _talents; + List _secretTalents; const LearnableAttacks* _attacks; public: - SpeciesVariant(ConstString name, float height, float weight, uint32_t baseExperience, - std::vector types, Library::StatisticSet baseStats, - std::vector talents, std::vector secretTalents, - const LearnableAttacks* attacks); + SpeciesVariant(ConstString name, float height, float weight, uint32_t baseExperience, List types, + Library::StatisticSet baseStats, List talents, + List secretTalents, const LearnableAttacks* attacks); virtual ~SpeciesVariant(); @@ -43,21 +43,21 @@ namespace CreatureLib::Library { [[nodiscard]] size_t GetTypeCount() const; [[nodiscard]] uint8_t GetType(size_t index) const; - [[nodiscard]] const std::vector& GetTypes() const; + [[nodiscard]] const List& GetTypes() const; [[nodiscard]] uint32_t GetStatistic(Library::Statistic stat) const; - [[nodiscard]] const size_t GetTalentCount() const { return _talents.size(); } - [[nodiscard]] const size_t GetSecretTalentCount() const { return _secretTalents.size(); } + [[nodiscard]] const size_t GetTalentCount() const { return _talents.Count(); } + [[nodiscard]] const size_t GetSecretTalentCount() const { return _secretTalents.Count(); } [[nodiscard]] const ConstString& GetTalent(const TalentIndex& index) const { if (index.IsSecret()) - return _secretTalents.at(index.GetIndex()); - return _talents.at(index.GetIndex()); + return _secretTalents.At(index.GetIndex()); + return _talents.At(index.GetIndex()); } [[nodiscard]] const TalentIndex GetTalentIndex(const ConstString& talent) const; [[nodiscard]] const LearnableAttacks* GetLearnableAttacks() const; [[nodiscard]] TalentIndex GetRandomTalent(Arbutils::Random* rand) const; - [[nodiscard]] inline const std::vector& GetTalents() const { return _talents; } - [[nodiscard]] inline const std::vector& GetSecretTalents() const { return _secretTalents; } + [[nodiscard]] inline const List& GetTalents() const { return _talents; } + [[nodiscard]] inline const List& GetSecretTalents() const { return _secretTalents; } }; } diff --git a/src/Library/GrowthRates/LookupGrowthRate.hpp b/src/Library/GrowthRates/LookupGrowthRate.hpp index caf38d2..732993b 100644 --- a/src/Library/GrowthRates/LookupGrowthRate.hpp +++ b/src/Library/GrowthRates/LookupGrowthRate.hpp @@ -1,29 +1,27 @@ #ifndef CREATURELIB_LOOKUPGROWTHRATE_HPP #define CREATURELIB_LOOKUPGROWTHRATE_HPP -#include +#include #include "GrowthRate.hpp" + namespace CreatureLib::Library { class LookupGrowthRate : public GrowthRate { protected: - std::vector _experience; + Arbutils::Collections::List _experience; public: - LookupGrowthRate(const std::vector& experience) : _experience(experience) {} + LookupGrowthRate(const Arbutils::Collections::List& experience) : _experience(experience) {} uint8_t CalculateLevel(uint32_t experience) const override { - for (uint8_t i = 0; i < _experience.size(); i++) { + for (uint8_t i = 0; i < _experience.Count(); i++) { if (_experience[i] > experience) { return i; } } - return _experience[_experience.size() - 1]; + return _experience[_experience.Count() - 1]; } - uint32_t CalculateExperience(uint8_t level) const override { - Assert(level <= _experience.size()) - return _experience[level - 1]; - } + uint32_t CalculateExperience(uint8_t level) const override { return _experience[level - 1]; } }; } diff --git a/src/Library/TypeLibrary.cpp b/src/Library/TypeLibrary.cpp index 07d08bb..b4ee41c 100644 --- a/src/Library/TypeLibrary.cpp +++ b/src/Library/TypeLibrary.cpp @@ -3,7 +3,7 @@ using namespace CreatureLib::Library; -float TypeLibrary::GetEffectiveness(uint8_t attacking, const std::vector& defensive) const { +float TypeLibrary::GetEffectiveness(uint8_t attacking, const List& defensive) const { auto eff = 1; for (auto def : defensive) { eff *= GetSingleEffectiveness(attacking, def); @@ -12,33 +12,29 @@ float TypeLibrary::GetEffectiveness(uint8_t attacking, const std::vector +#include #include #include #include using ConstString = Arbutils::CaseInsensitiveConstString; +using namespace Arbutils::Collections; namespace CreatureLib::Library { class TypeLibrary { - std::unordered_map _types; - std::vector> _effectiveness; + Dictionary _types; + List> _effectiveness; public: - TypeLibrary(size_t initialCapacity = 20) : _types(std::unordered_map(initialCapacity)) {} + TypeLibrary(size_t initialCapacity = 20) : _types(Dictionary(initialCapacity)) {} uint8_t GetTypeId(const ConstString& s) const; uint8_t GetTypeId(uint32_t s) const; [[nodiscard]] float GetSingleEffectiveness(uint8_t attacking, uint8_t defensive) const; - [[nodiscard]] float GetEffectiveness(uint8_t attacking, const std::vector& defensive) const; + [[nodiscard]] float GetEffectiveness(uint8_t attacking, const List& defensive) const; uint8_t RegisterType(const ConstString& typeName); uint8_t RegisterType(uint32_t typeHash); diff --git a/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp b/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp index 6a26cea..b3b8ef4 100644 --- a/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp +++ b/tests/BattleTests/ScriptTests/ScriptAggregatorTests.cpp @@ -21,7 +21,7 @@ public: TEST_CASE("Script Aggregator properly iterates containing script.", "[Battling, Scripting]") { Script* script = new TestScript("test"); auto ran = 0; - auto vec = std::vector{&script}; + auto vec = Arbutils::Collections::List{&script}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { auto next = aggr.GetNext(); @@ -37,7 +37,7 @@ TEST_CASE("Script Aggregator properly iterates multiple scripts.", "[Battling, S Script* script2 = new TestScript("test2"); Script* script3 = new TestScript("test3"); auto ran = 0; - auto vec = std::vector{&script, &script2, &script3}; + auto vec = Arbutils::Collections::List{&script, &script2, &script3}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { auto next = aggr.GetNext(); @@ -59,7 +59,7 @@ TEST_CASE("Script Aggregator properly iterates Script Set.", "[Battling, Scripti set.Add(script); set.Add(script2); set.Add(script3); - auto vec = std::vector{&set}; + auto vec = Arbutils::Collections::List{&set}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { auto next = aggr.GetNext(); @@ -77,7 +77,7 @@ TEST_CASE("Script Aggregator properly iterates data of Script Set and Script.", auto set = ScriptSet(); set.Add(script2); set.Add(script3); - auto vec = std::vector{&set, &script}; + auto vec = Arbutils::Collections::List{&set, &script}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { auto next = aggr.GetNext(); @@ -96,7 +96,7 @@ TEST_CASE("Script Aggregator properly iterates data of Script and Script Set.", auto set = ScriptSet(); set.Add(script2); set.Add(script3); - auto vec = std::vector{&script, &set}; + auto vec = Arbutils::Collections::List{&script, &set}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { auto next = aggr.GetNext(); @@ -116,7 +116,7 @@ TEST_CASE("Script Aggregator properly iterates data of Script, Script Set and Sc auto set = ScriptSet(); set.Add(script2); set.Add(script3); - auto vec = std::vector{&script, &set, &script4}; + auto vec = Arbutils::Collections::List{&script, &set, &script4}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { auto next = aggr.GetNext(); @@ -130,7 +130,7 @@ TEST_CASE("Script Aggregator properly iterates data of Script, Script Set and Sc TEST_CASE("Script Aggregator properly iterates when empty.", "[Battling, Scripting]") { auto ran = 0; - auto vec = std::vector{}; + auto vec = Arbutils::Collections::List{}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { throw CreatureException("Aggregator returned a script, but should have been empty."); @@ -141,7 +141,7 @@ TEST_CASE("Script Aggregator properly iterates when empty.", "[Battling, Scripti TEST_CASE("Script Aggregator properly iterates empty Script Set.", "[Battling, Scripting]") { auto ran = 0; auto set = ScriptSet(); - auto vec = std::vector{&set}; + auto vec = Arbutils::Collections::List{&set}; auto aggr = ScriptAggregator(vec); while (aggr.HasNext()) { auto next = aggr.GetNext(); diff --git a/tests/BattleTests/ScriptTests/ScriptSetTests.cpp b/tests/BattleTests/ScriptTests/ScriptSetTests.cpp index 63c309b..40cab8a 100644 --- a/tests/BattleTests/ScriptTests/ScriptSetTests.cpp +++ b/tests/BattleTests/ScriptTests/ScriptSetTests.cpp @@ -33,7 +33,7 @@ TEST_CASE("Add script to script set, then retrieve it", "[Battling, Scripting]") auto s = new TestScript("foobar"); set.Add(s); REQUIRE(set.Count() == 1); - auto get = set.GetIterator()->at(0); + auto get = set.GetIterator()->At(0); REQUIRE(get->GetName() == "foobar"); } @@ -53,8 +53,8 @@ TEST_CASE("Add two scripts to script set, then retrieve them", "[Battling, Scrip set.Add(s); set.Add(s2); REQUIRE(set.Count() == 2); - auto get1 = set.GetIterator()->at(0); - auto get2 = set.GetIterator()->at(1); + auto get1 = set.GetIterator()->At(0); + auto get2 = set.GetIterator()->At(1); REQUIRE(get1->GetName() == "foobar"); REQUIRE(get2->GetName() == "foobar2"); } @@ -67,7 +67,7 @@ TEST_CASE("Add script to script set, then remove it", "[Battling, Scripting]") { set.Remove("foobar"_cnc); REQUIRE(set.Count() == 0); auto it = set.GetIterator(); - REQUIRE(it->empty()); + REQUIRE(it->Count() == 0); } TEST_CASE("Add two scripts to script set, then remove them", "[Battling, Scripting]") { @@ -80,7 +80,7 @@ TEST_CASE("Add two scripts to script set, then remove them", "[Battling, Scripti set.Remove("foobar"_cnc); REQUIRE(set.Count() == 1); auto it = set.GetIterator(); - REQUIRE(it->at(0)->GetName() == "foobar2"); + REQUIRE(it->At(0)->GetName() == "foobar2"); } #endif diff --git a/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp b/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp index bc3c7fd..810bb81 100644 --- a/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp +++ b/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp @@ -24,7 +24,7 @@ public: Script* ScriptPtr = nullptr; protected: - void GetActiveScripts(std::vector& scripts) override { scripts.emplace_back(&ScriptPtr); } + void GetActiveScripts(Arbutils::Collections::List& scripts) override { scripts.Append(&ScriptPtr); } }; class ScriptSourceWithScriptSet : public ScriptSource { @@ -32,7 +32,7 @@ public: ScriptSet Set; protected: - void GetActiveScripts(std::vector& scripts) override { scripts.emplace_back(&Set); } + void GetActiveScripts(Arbutils::Collections::List& scripts) override { scripts.Append(&Set); } }; TEST_CASE("Script source with unset script ptr.", "[Battling, Scripting]") {