Rework ScriptIterator to jump to first value on reset.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2020-06-05 18:06:45 +02:00
parent 9e7607338f
commit fddf2cabab
5 changed files with 51 additions and 37 deletions

View File

@@ -14,6 +14,25 @@ namespace CreatureLib::Battling {
size_t _index = 0;
size_t _setIndex = 0;
inline void IncrementToNextNotNull(bool initialIncrement = true) {
if (_scripts[_index].IsSet()) {
_setIndex++;
if (_setIndex >= _scripts[_index].GetScriptSet()->Count()) {
_setIndex = 0;
} else {
return;
}
}
if (initialIncrement)
_index++;
while (HasNext()) {
if (_scripts[_index].HasValue()) {
return;
}
_index++;
}
}
public:
ScriptAggregator(){};
explicit ScriptAggregator(const ArbUt::List<ScriptWrapper>& scripts)
@@ -21,11 +40,12 @@ namespace CreatureLib::Battling {
inline void Reset() {
_index = 0;
_setIndex = 0;
_setIndex = -1;
IncrementToNextNotNull(false);
}
inline bool HasNext() { return _index < _size; }
Script* GetNextNotNull() {
ArbUt::BorrowedPtr<Script> GetNextNotNull() {
while (HasNext()) {
auto s = GetNext();
if (s != nullptr) {
@@ -35,28 +55,17 @@ namespace CreatureLib::Battling {
return nullptr;
}
Script* GetNext() {
ArbUt::BorrowedPtr<Script> GetNext() {
auto& current = _scripts[_index];
if (!current.IsSet()) {
_index++;
auto s = current.GetScript();
if (s == nullptr)
return nullptr;
return (*s).get();
IncrementToNextNotNull();
return (*s);
} else {
auto& set = current.GetScriptSet()->GetIterator();
auto count = set.Count();
if (_setIndex >= count) {
_index++;
_setIndex = 0;
return nullptr;
}
auto v = set[_setIndex++];
if (_setIndex >= count) {
_index++;
_setIndex = 0;
}
return v.GetRaw();
auto v = set[_setIndex];
IncrementToNextNotNull();
return v;
}
}
};

View File

@@ -68,7 +68,7 @@ namespace CreatureLib::Battling {
inline size_t Count() const { return _scripts.Count(); }
const ArbUt::UniquePtrList<Script>& GetIterator() const { return _scripts; }
inline const ArbUt::UniquePtrList<Script>& GetIterator() const { return _scripts; }
};
}

View File

@@ -21,10 +21,17 @@ namespace CreatureLib::Battling {
static inline ScriptWrapper FromScript(std::unique_ptr<Script>* s) { return ScriptWrapper(s, false); }
static inline ScriptWrapper FromSet(ScriptSet* s) { return ScriptWrapper(s, true); }
bool IsSet() const { return _isSet; }
inline bool IsSet() const noexcept { return _isSet; }
inline const std::unique_ptr<Script>* GetScript() const { return _script; }
inline const ScriptSet* GetScriptSet() const { return _scriptSet; }
inline const std::unique_ptr<Script>* GetScript() const noexcept { return _script; }
inline const ScriptSet* GetScriptSet() const noexcept { return _scriptSet; }
inline bool HasValue() const noexcept {
if (_isSet)
return _scriptSet->Count() > 0;
else
return *_script != nullptr;
}
};
}