diff --git a/lib/nw/kernel/Rules.cpp b/lib/nw/kernel/Rules.cpp index a484d1042..29552d704 100644 --- a/lib/nw/kernel/Rules.cpp +++ b/lib/nw/kernel/Rules.cpp @@ -12,6 +12,19 @@ const std::type_index Rules::type_index{typeid(Rules)}; Rules::Rules(MemoryResource* scope) : Service(scope) + , baseitems{allocator()} + , classes{allocator()} + , feats{allocator()} + , races{allocator()} + , spells{allocator()} + , spellschools{allocator()} + , skills{allocator()} + , master_feats{allocator()} + , modifiers{allocator()} + , phenotypes{allocator()} + , appearances{allocator()} + , placeables{allocator()} + , traps{allocator()} { } @@ -20,22 +33,6 @@ Rules::~Rules() clear(); } -void Rules::clear() -{ - modifiers.clear(); - baseitems.clear(); - classes.clear(); - feats.clear(); - races.clear(); - spells.clear(); - skills.clear(); - master_feats.clear(); - appearances.clear(); - phenotypes.clear(); - traps.clear(); - placeables.clear(); -} - void Rules::initialize(ServiceInitTime time) { if (time != ServiceInitTime::kernel_start && time != ServiceInitTime::module_post_load) { diff --git a/lib/nw/kernel/Rules.hpp b/lib/nw/kernel/Rules.hpp index 3450bc261..dcd076bb1 100644 --- a/lib/nw/kernel/Rules.hpp +++ b/lib/nw/kernel/Rules.hpp @@ -29,9 +29,6 @@ struct Rules : public Service { /// Initializes rules system virtual void initialize(ServiceInitTime time) override; - /// Clears rules system of all rules and cached 2da files - virtual void clear() override; - /// Gets attack functions [[nodiscard]] const AttackFuncs& attack_functions() const noexcept; diff --git a/lib/nw/rules/Class.hpp b/lib/nw/rules/Class.hpp index 1d92650dd..e465f7127 100644 --- a/lib/nw/rules/Class.hpp +++ b/lib/nw/rules/Class.hpp @@ -92,7 +92,14 @@ struct ClassArray { InternedString, Class, InternedStringHash, - InternedStringEq>; + InternedStringEq, + Allocator>>; + + ClassArray(MemoryResource* allocator = kernel::global_allocator()) + : entries(allocator) + , constant_to_index(allocator) + { + } const ClassInfo* get(Class class_) const noexcept; bool is_valid(Class class_) const noexcept; @@ -120,7 +127,7 @@ struct ClassArray { std::set> attack_tables; Vector stat_gain_tables; - Vector entries; + PVector entries; map_type constant_to_index; }; diff --git a/lib/nw/rules/feats.cpp b/lib/nw/rules/feats.cpp index 6bcfb391b..326926f36 100644 --- a/lib/nw/rules/feats.cpp +++ b/lib/nw/rules/feats.cpp @@ -52,6 +52,11 @@ FeatInfo::FeatInfo(const TwoDARowView& tda) // -- Master Feats ------------------------------------------------------------ // ---------------------------------------------------------------------------- +MasterFeatRegistry::MasterFeatRegistry(MemoryResource* allocator) + : allocator_(allocator) +{ +} + const ModifierVariant& MasterFeatRegistry::get_bonus(MasterFeat mfeat) const { return bonuses_[*mfeat]; diff --git a/lib/nw/rules/feats.hpp b/lib/nw/rules/feats.hpp index b74488efa..a714fb02d 100644 --- a/lib/nw/rules/feats.hpp +++ b/lib/nw/rules/feats.hpp @@ -86,6 +86,8 @@ inline bool operator==(const MasterFeatEntry& lhs, const MasterFeatEntry& rhs) } struct MasterFeatRegistry { + MasterFeatRegistry(MemoryResource* allocator = nw::kernel::global_allocator()); + template void add(T type, MasterFeat mfeat, Feat feat); void clear() noexcept; @@ -100,6 +102,7 @@ struct MasterFeatRegistry { private: Vector entries_; Vector bonuses_; + MemoryResource* allocator_ = nullptr; }; template diff --git a/lib/nw/rules/rule_type.hpp b/lib/nw/rules/rule_type.hpp index a10338818..6ed5f3a2e 100644 --- a/lib/nw/rules/rule_type.hpp +++ b/lib/nw/rules/rule_type.hpp @@ -2,6 +2,7 @@ #include "../util/InternedString.hpp" +#include "../kernel/Kernel.hpp" #include #include @@ -155,7 +156,14 @@ struct RuleTypeArray { InternedString, RuleType, InternedStringHash, - InternedStringEq>; + InternedStringEq, + Allocator>>; + + RuleTypeArray(MemoryResource* allocator = kernel::global_allocator()) + : entries(allocator) + , constant_to_index(allocator) + { + } const RuleTypeInfo* get(RuleType type) const noexcept { @@ -187,7 +195,7 @@ struct RuleTypeArray { } } - Vector entries; + PVector entries; map_type constant_to_index; }; diff --git a/lib/nw/rules/system.cpp b/lib/nw/rules/system.cpp index 25fd40585..9cdbcdeba 100644 --- a/lib/nw/rules/system.cpp +++ b/lib/nw/rules/system.cpp @@ -142,6 +142,11 @@ nw::Modifier make_modifier(nw::ModifierType type, nw::ModifierVariant value, }; } +ModifierRegistry::ModifierRegistry(MemoryResource* allocator) + : allocator_(allocator) +{ +} + void ModifierRegistry::add(Modifier mod) { auto it = std::lower_bound(std::begin(entries_), std::end(entries_), mod); diff --git a/lib/nw/rules/system.hpp b/lib/nw/rules/system.hpp index b92320d25..2bb647a23 100644 --- a/lib/nw/rules/system.hpp +++ b/lib/nw/rules/system.hpp @@ -9,13 +9,9 @@ #include "rule_type.hpp" #include "system.hpp" +#include #include -#include -#include -#include -#include - namespace nw { struct Class; @@ -91,7 +87,7 @@ enum struct ModifierSource { }; struct ModifierResult { - ModifierResult() { }; + ModifierResult() {}; ModifierResult(int value); ModifierResult(float value); ModifierResult(DamageRoll value); @@ -187,6 +183,8 @@ struct ModifierRegistry { using iterator = Storage::iterator; using const_iterator = Storage::const_iterator; + ModifierRegistry(MemoryResource* allocator = nw::kernel::global_allocator()); + /// Adds a modifier to the system void add(Modifier mod); @@ -232,6 +230,7 @@ struct ModifierRegistry { private: Storage entries_; + MemoryResource* allocator_ = nullptr; }; } // namespace nw