diff --git a/src/Battling/ScriptHandling/ScriptSet.hpp b/src/Battling/ScriptHandling/ScriptSet.hpp index 134962c..6e66618 100644 --- a/src/Battling/ScriptHandling/ScriptSet.hpp +++ b/src/Battling/ScriptHandling/ScriptSet.hpp @@ -52,10 +52,19 @@ namespace CreatureLib::Battling { void Remove(u32 keyHash) { auto v = _lookup.TryGet(keyHash); if (v.has_value()) { - auto script = _scripts[v.value()]; + auto index = v.value(); + auto script = _scripts[index]; script->OnRemove(); _scripts.Remove(v.value()); - _lookup.Remove(keyHash); + // This is extremely inefficient. We need to do something better for this. + ResetLookup(); + } + } + + void ResetLookup() { + _lookup.Clear(); + for (size_t i = 0; i < _scripts.Count(); ++i) { + _lookup.Insert(_scripts[i]->GetName(), i); } } diff --git a/tests/BattleTests/ScriptTests/ScriptSetTests.cpp b/tests/BattleTests/ScriptTests/ScriptSetTests.cpp index f1bd61d..f462578 100644 --- a/tests/BattleTests/ScriptTests/ScriptSetTests.cpp +++ b/tests/BattleTests/ScriptTests/ScriptSetTests.cpp @@ -84,4 +84,17 @@ TEST_CASE("Add two scripts to script set, then remove them") { REQUIRE(it.At(0)->GetName() == "foobar2"); } +TEST_CASE("Add two scripts to script set, remove the first, get second by name") { + auto set = ScriptSet(); + auto s = new TestScript("foobar"); + auto s2 = new TestScript("foobar2"); + set.Add(s); + set.Add(s2); + REQUIRE(set.Count() == 2); + set.Remove("foobar"_cnc.GetHash()); + REQUIRE(set.Count() == 1); + auto retrieved = set.Get("foobar2"_cnc); + REQUIRE(retrieved == s2); +} + #endif