diff --git a/src/Battling/Models/Battle.cpp b/src/Battling/Models/Battle.cpp index 0b0a4cd..56eedce 100644 --- a/src/Battling/Models/Battle.cpp +++ b/src/Battling/Models/Battle.cpp @@ -85,6 +85,7 @@ bool Battle::CreatureInField(const Creature* creature) const { void Battle::ForceRecall(uint8_t side, uint8_t index) { _sides[side]->SetCreature(nullptr, index); } +size_t Battle::ScriptCount() const { return 1; } void Battle::GetActiveScripts(Arbutils::Collections::List& scripts) { scripts.Append(ScriptWrapper::FromSet(&_volatile)); } diff --git a/src/Battling/Models/Battle.hpp b/src/Battling/Models/Battle.hpp index b876118..4099380 100644 --- a/src/Battling/Models/Battle.hpp +++ b/src/Battling/Models/Battle.hpp @@ -81,6 +81,7 @@ namespace CreatureLib::Battling { void SwitchCreature(uint8_t side, uint8_t index, Creature* c); bool CanSlotBeFilled(uint8_t side, uint8_t index) const; + size_t ScriptCount() const override; void GetActiveScripts(Arbutils::Collections::List& scripts) override; void ValidateBattleState(); diff --git a/src/Battling/Models/BattleSide.cpp b/src/Battling/Models/BattleSide.cpp index 4d4b507..7b6dbc2 100644 --- a/src/Battling/Models/BattleSide.cpp +++ b/src/Battling/Models/BattleSide.cpp @@ -73,6 +73,8 @@ void BattleSide::GetActiveScripts(Arbutils::Collections::List& sc scripts.Append(ScriptWrapper::FromSet(&_volatile)); _battle->GetActiveScripts(scripts); } +size_t BattleSide::ScriptCount() const { return _battle->ScriptCount() + 1; } + uint8_t BattleSide::GetRandomCreatureIndex() { // TODO: Consider adding parameter to only get index for available creatures. AssertNotNull(_battle) diff --git a/src/Battling/Models/BattleSide.hpp b/src/Battling/Models/BattleSide.hpp index 859992d..dae3aa9 100644 --- a/src/Battling/Models/BattleSide.hpp +++ b/src/Battling/Models/BattleSide.hpp @@ -48,6 +48,7 @@ namespace CreatureLib::Battling { Creature* GetCreature(uint8_t index) const; bool CreatureOnSide(const Creature* creature) const; + size_t ScriptCount() const override; void GetActiveScripts(Arbutils::Collections::List& scripts) override; const List& GetCreatures() { return _creatures; } diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index fd5875e..7fcc807 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -166,6 +166,14 @@ bool Battling::Creature::HasType(uint8_t type) const noexcept { return std::find(t.begin(), t.end(), type) != t.end(); } +size_t Battling::Creature::ScriptCount() const { + auto c = 3; + if (_side != nullptr) { + c += _side->ScriptCount(); + } + return c; +} + void Battling::Creature::GetActiveScripts(Arbutils::Collections::List& scripts) { scripts.Append(ScriptWrapper::FromScript(&_activeTalent)); scripts.Append(ScriptWrapper::FromScript(&_status)); diff --git a/src/Battling/Models/Creature.hpp b/src/Battling/Models/Creature.hpp index 798a9df..b6baa18 100644 --- a/src/Battling/Models/Creature.hpp +++ b/src/Battling/Models/Creature.hpp @@ -122,6 +122,7 @@ namespace CreatureLib::Battling { void MarkOpponentAsSeen(Creature* creature) { _seenOpponents.insert(creature); } const std::unordered_set& GetSeenOpponents() const { return _seenOpponents; } + size_t ScriptCount() const override; void GetActiveScripts(Arbutils::Collections::List& scripts) override; void ClearVolatileScripts(); void AddVolatileScript(const ConstString& name); diff --git a/src/Battling/Models/ExecutingAttack.hpp b/src/Battling/Models/ExecutingAttack.hpp index abdd623..e923ebf 100644 --- a/src/Battling/Models/ExecutingAttack.hpp +++ b/src/Battling/Models/ExecutingAttack.hpp @@ -80,6 +80,7 @@ namespace CreatureLib::Battling { Creature* GetUser() noexcept { return _user; } LearnedAttack* GetAttack() noexcept { return _attack; } + size_t ScriptCount() const override { return _user->ScriptCount() + 1; } protected: void GetActiveScripts(Arbutils::Collections::List& scripts) override { diff --git a/src/Battling/ScriptHandling/ScriptSet.hpp b/src/Battling/ScriptHandling/ScriptSet.hpp index 6e70ac1..88dd0c7 100644 --- a/src/Battling/ScriptHandling/ScriptSet.hpp +++ b/src/Battling/ScriptHandling/ScriptSet.hpp @@ -18,6 +18,9 @@ namespace CreatureLib::Battling { } } + static constexpr size_t defaultCapacity = 8; + ScriptSet() : _scripts(defaultCapacity), _lookup(defaultCapacity){}; + void Add(Script* script) { size_t v; if (_lookup.TryGet(script->GetName(), v)) { diff --git a/src/Battling/ScriptHandling/ScriptSource.hpp b/src/Battling/ScriptHandling/ScriptSource.hpp index 7a456b7..49a2809 100644 --- a/src/Battling/ScriptHandling/ScriptSource.hpp +++ b/src/Battling/ScriptHandling/ScriptSource.hpp @@ -16,8 +16,10 @@ namespace CreatureLib::Battling { void ResetActiveScripts() { _areScriptsInitialized = false; } public: + virtual size_t ScriptCount() const = 0; const ScriptAggregator& GetScriptIterator() { if (!_areScriptsInitialized) { + _scripts.Reserve(ScriptCount()); GetActiveScripts(_scripts); _scriptsIterator = ScriptAggregator(_scripts); _areScriptsInitialized = true; diff --git a/src/Battling/TurnChoices/AttackTurnChoice.hpp b/src/Battling/TurnChoices/AttackTurnChoice.hpp index 689bb71..e5d7248 100644 --- a/src/Battling/TurnChoices/AttackTurnChoice.hpp +++ b/src/Battling/TurnChoices/AttackTurnChoice.hpp @@ -53,6 +53,7 @@ namespace CreatureLib::Battling { const CreatureIndex& GetTarget() const noexcept { return _target; } Script* GetAttackScript() const noexcept { return _attackScript; } + size_t ScriptCount() const override { return 1 + GetUser()->ScriptCount(); } protected: void GetActiveScripts(Arbutils::Collections::List& scripts) override { diff --git a/src/Battling/TurnChoices/FleeTurnChoice.hpp b/src/Battling/TurnChoices/FleeTurnChoice.hpp index 076ea5a..749df8b 100644 --- a/src/Battling/TurnChoices/FleeTurnChoice.hpp +++ b/src/Battling/TurnChoices/FleeTurnChoice.hpp @@ -10,6 +10,7 @@ namespace CreatureLib::Battling { FleeTurnChoice(Creature* user) : BaseTurnChoice(user) {} TurnChoiceKind GetKind() const noexcept override { return TurnChoiceKind ::Flee; } + size_t ScriptCount() const override { return GetUser()->ScriptCount(); } protected: void GetActiveScripts(Arbutils::Collections::List& scripts) override { diff --git a/src/Battling/TurnChoices/PassTurnChoice.hpp b/src/Battling/TurnChoices/PassTurnChoice.hpp index f762f50..5b67c10 100644 --- a/src/Battling/TurnChoices/PassTurnChoice.hpp +++ b/src/Battling/TurnChoices/PassTurnChoice.hpp @@ -9,6 +9,7 @@ namespace CreatureLib::Battling { PassTurnChoice(Creature* c) : BaseTurnChoice(c) {} TurnChoiceKind GetKind() const noexcept override { return TurnChoiceKind ::Pass; } + size_t ScriptCount() const override { return GetUser()->ScriptCount(); } protected: void GetActiveScripts(Arbutils::Collections::List& scripts) override { diff --git a/src/Battling/TurnChoices/SwitchTurnChoice.hpp b/src/Battling/TurnChoices/SwitchTurnChoice.hpp index 2da647e..23e2316 100644 --- a/src/Battling/TurnChoices/SwitchTurnChoice.hpp +++ b/src/Battling/TurnChoices/SwitchTurnChoice.hpp @@ -13,6 +13,7 @@ namespace CreatureLib::Battling { TurnChoiceKind GetKind() const noexcept final { return TurnChoiceKind::Switch; } inline Creature* GetNewCreature() const noexcept { return _newCreature; } + size_t ScriptCount() const override { return GetUser()->ScriptCount(); } protected: void GetActiveScripts(Arbutils::Collections::List& scripts) override { diff --git a/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp b/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp index eddbf54..e877f2b 100644 --- a/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp +++ b/tests/BattleTests/ScriptTests/ScriptSourceTest.cpp @@ -24,6 +24,7 @@ public: Script* ScriptPtr = nullptr; protected: + size_t ScriptCount() const override { return 1; } void GetActiveScripts(Arbutils::Collections::List& scripts) override { scripts.Append(ScriptWrapper::FromScript(&ScriptPtr)); } @@ -34,6 +35,7 @@ public: ScriptSet Set; protected: + size_t ScriptCount() const override { return 1; } void GetActiveScripts(Arbutils::Collections::List& scripts) override { scripts.Append(ScriptWrapper::FromSet(&Set)); }