2019-11-08 17:45:43 +00:00
|
|
|
#ifndef CREATURELIB_SCRIPTSET_HPP
|
|
|
|
#define CREATURELIB_SCRIPTSET_HPP
|
|
|
|
|
2021-10-30 12:24:38 +00:00
|
|
|
#include <Arbutils/Collections/Dictionary.hpp>
|
2019-11-08 17:45:43 +00:00
|
|
|
#include <any>
|
2021-03-07 09:26:41 +00:00
|
|
|
#include "BattleScript.hpp"
|
2019-11-08 17:45:43 +00:00
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
namespace CreatureLib::Battling {
|
|
|
|
class ScriptSet {
|
2021-03-07 09:26:41 +00:00
|
|
|
ArbUt::UniquePtrList<BattleScript> _scripts;
|
2022-03-23 12:56:45 +00:00
|
|
|
ArbUt::Dictionary<u32, size_t> _lookup;
|
2019-11-28 11:55:22 +00:00
|
|
|
|
2019-11-08 17:45:43 +00:00
|
|
|
public:
|
2020-06-02 13:03:31 +00:00
|
|
|
~ScriptSet() = default;
|
2020-02-05 13:52:50 +00:00
|
|
|
|
2020-04-25 09:33:25 +00:00
|
|
|
static constexpr size_t defaultCapacity = 8;
|
|
|
|
ScriptSet() : _scripts(defaultCapacity), _lookup(defaultCapacity){};
|
|
|
|
|
2021-10-29 19:55:49 +00:00
|
|
|
void Clone(const ArbUt::OptionalBorrowedPtr<void>& owner, ScriptSet& s) const {
|
2021-04-11 13:20:50 +00:00
|
|
|
for (auto* script : _scripts) {
|
2021-10-29 19:55:49 +00:00
|
|
|
s.Add(script->Clone(owner));
|
2021-04-11 13:20:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 17:30:35 +00:00
|
|
|
BattleScript* non_null Add(BattleScript* non_null script) {
|
2020-12-12 11:59:56 +00:00
|
|
|
auto v = _lookup.TryGet(script->GetName());
|
|
|
|
if (v.has_value()) {
|
|
|
|
_scripts[v.value()]->Stack();
|
2020-02-16 16:44:41 +00:00
|
|
|
delete script;
|
2021-03-27 15:36:20 +00:00
|
|
|
return _scripts[v.value()];
|
2019-11-08 17:45:43 +00:00
|
|
|
}
|
2020-03-25 18:07:36 +00:00
|
|
|
|
2020-03-22 18:21:40 +00:00
|
|
|
_scripts.Append(script);
|
2020-03-25 18:07:36 +00:00
|
|
|
_lookup.Insert(script->GetName(), _scripts.Count() - 1);
|
2021-03-27 15:36:20 +00:00
|
|
|
return script;
|
2019-11-08 17:45:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 19:20:49 +00:00
|
|
|
ArbUt::OptionalBorrowedPtr<BattleScript> Get(const ArbUt::BasicStringView& key) const {
|
2020-12-12 11:22:48 +00:00
|
|
|
return Get(key.GetHash());
|
|
|
|
}
|
2020-03-09 09:16:57 +00:00
|
|
|
|
2022-03-23 12:56:45 +00:00
|
|
|
ArbUt::OptionalBorrowedPtr<BattleScript> Get(u32 keyHash) const noexcept {
|
2020-12-12 11:59:56 +00:00
|
|
|
auto v = _lookup.TryGet(keyHash);
|
|
|
|
if (v.has_value()) {
|
2021-03-27 19:20:49 +00:00
|
|
|
return _scripts[v.value()].GetRaw();
|
2020-02-16 16:44:41 +00:00
|
|
|
}
|
2020-12-12 11:22:48 +00:00
|
|
|
return {};
|
2020-02-16 16:44:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 15:08:23 +00:00
|
|
|
void Remove(const ArbUt::BasicStringView& key) { Remove(key.GetHash()); }
|
2020-03-09 09:16:57 +00:00
|
|
|
|
2022-03-23 12:56:45 +00:00
|
|
|
void Remove(u32 keyHash) {
|
2020-12-12 11:59:56 +00:00
|
|
|
auto v = _lookup.TryGet(keyHash);
|
|
|
|
if (v.has_value()) {
|
2022-06-05 10:39:29 +00:00
|
|
|
auto index = v.value();
|
|
|
|
auto script = _scripts[index];
|
2020-02-05 13:52:50 +00:00
|
|
|
script->OnRemove();
|
2020-12-12 11:59:56 +00:00
|
|
|
_scripts.Remove(v.value());
|
2022-06-05 10:39:29 +00:00
|
|
|
// 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);
|
2019-11-12 17:38:09 +00:00
|
|
|
}
|
2019-11-08 17:45:43 +00:00
|
|
|
}
|
2019-11-09 11:15:45 +00:00
|
|
|
|
2019-12-14 11:15:30 +00:00
|
|
|
void Clear() {
|
2020-02-05 13:52:50 +00:00
|
|
|
for (auto s : _scripts) {
|
2020-03-25 18:07:36 +00:00
|
|
|
s->OnRemove();
|
2020-02-05 13:52:50 +00:00
|
|
|
}
|
2021-04-14 17:29:42 +00:00
|
|
|
_scripts.Clear();
|
2020-03-25 18:07:36 +00:00
|
|
|
_lookup.Clear();
|
2019-12-14 11:15:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 15:08:23 +00:00
|
|
|
bool Has(const ArbUt::BasicStringView& key) const { return _lookup.Has(key); }
|
2020-03-09 09:16:57 +00:00
|
|
|
|
2022-03-23 12:56:45 +00:00
|
|
|
bool Has(u32 keyHash) const { return _lookup.Has(keyHash); }
|
2020-02-23 15:17:57 +00:00
|
|
|
|
2020-04-23 21:23:58 +00:00
|
|
|
inline size_t Count() const { return _scripts.Count(); }
|
2019-11-09 11:15:45 +00:00
|
|
|
|
2021-03-07 09:26:41 +00:00
|
|
|
inline const ArbUt::UniquePtrList<BattleScript>& GetIterator() const { return _scripts; }
|
2019-11-08 17:45:43 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
#endif // CREATURELIB_SCRIPTSET_HPP
|