#ifndef CREATURELIB_SCRIPTAGGREGATOR_HPP #define CREATURELIB_SCRIPTAGGREGATOR_HPP #include "BattleScript.hpp" #include "ScriptSet.hpp" #include "ScriptWrapper.hpp" namespace CreatureLib::Battling { class ScriptAggregator { const ScriptWrapper* nullable _scripts; i32 _size; i32 _index = -1; i32 _setIndex = -1; inline bool IncrementToNextNotNull() { if (_index != -1 && _scripts[_index].IsSet()) { _setIndex++; if (_setIndex >= (i32)_scripts[_index].GetScriptSet()->Count()) { _setIndex = -1; } else { return true; } } for (_index++; _index < _size; ++_index) { if (_scripts[_index].HasValue()) { if (_scripts[_index].IsSet()) { _setIndex = 0; } return true; } } return false; } public: ScriptAggregator(){}; explicit ScriptAggregator(const ArbUt::List& scripts) : _scripts(scripts.RawData()), _size(scripts.Count()){}; inline void Reset() { _index = -1; _setIndex = -1; } bool GetNext(ArbUt::BorrowedPtr& out) { if (!IncrementToNextNotNull()) { return false; } auto& current = _scripts[_index]; if (!current.IsSet()) { auto s = current.GetScript(); out = s->GetValue(); return true; } else { auto& set = current.GetScriptSet()->GetIterator(); auto v = set[_setIndex]; out = v; return true; } } }; } #endif // CREATURELIB_SCRIPTAGGREGATOR_HPP