diff options
author | hop311 <hop3114@gmail.com> | 2023-10-20 08:39:37 +0200 |
---|---|---|
committer | hop311 <hop3114@gmail.com> | 2023-10-20 08:39:37 +0200 |
commit | ab696e8469b1417b69d6b3aa5d46a69d370613a0 (patch) | |
tree | cdfdb2d6b500b7709fa381a3fd19369f21a52006 /src/openvic-simulation/military | |
parent | 1484dd3aa73489cc66572f1c431e08c610e201af (diff) |
Minor cleanup
Diffstat (limited to 'src/openvic-simulation/military')
-rw-r--r-- | src/openvic-simulation/military/LeaderTrait.cpp | 71 | ||||
-rw-r--r-- | src/openvic-simulation/military/LeaderTrait.hpp | 104 | ||||
-rw-r--r-- | src/openvic-simulation/military/Unit.cpp | 2 |
3 files changed, 84 insertions, 93 deletions
diff --git a/src/openvic-simulation/military/LeaderTrait.cpp b/src/openvic-simulation/military/LeaderTrait.cpp index 27f81c5..b90a399 100644 --- a/src/openvic-simulation/military/LeaderTrait.cpp +++ b/src/openvic-simulation/military/LeaderTrait.cpp @@ -3,58 +3,51 @@ using namespace OpenVic; using namespace OpenVic::NodeTools; -LeaderTrait::LeaderTrait(std::string_view new_identifier, trait_type_t new_type, ModifierValue new_modifiers) - : HasIdentifier { new_identifier }, type { new_type }, modifiers { new_modifiers } {} +LeaderTrait::LeaderTrait(std::string_view new_identifier, trait_type_t new_type, ModifierValue&& new_modifiers) + : HasIdentifier { new_identifier }, type { new_type }, modifiers { std::move(new_modifiers) } {} -trait_type_t LeaderTrait::get_trait_type() const { - return type; +LeaderTrait::trait_type_t LeaderTrait::get_trait_type() const { + return type; } bool LeaderTrait::is_personality_trait() const { - return type == trait_type_t::PERSONALITY; + return type == trait_type_t::PERSONALITY; } bool LeaderTrait::is_background_trait() const { - return type == trait_type_t::BACKGROUND; + return type == trait_type_t::BACKGROUND; } -ModifierValue LeaderTrait::get_modifiers() const { - return modifiers; +ModifierValue const& LeaderTrait::get_modifiers() const { + return modifiers; } LeaderTraitManager::LeaderTraitManager() : leader_traits { "leader_traits" } {} -bool LeaderTraitManager::add_leader_trait(std::string_view identifier, trait_type_t type, ModifierValue modifiers) { - if (identifier.empty()) { - Logger::error("Invalid leader trait identifier - empty!"); - return false; - } +bool LeaderTraitManager::add_leader_trait(std::string_view identifier, LeaderTrait::trait_type_t type, ModifierValue&& modifiers) { + if (identifier.empty()) { + Logger::error("Invalid leader trait identifier - empty!"); + return false; + } - return leader_traits.add_item({ identifier, type, modifiers }); + return leader_traits.add_item({ identifier, type, std::move(modifiers) }); } -bool LeaderTraitManager::load_leader_traits_file(ModifierManager& modifier_manager, ast::NodeCPtr root) { - bool ret = expect_dictionary_keys( - "personality", ONE_EXACTLY, expect_dictionary( - [this, &modifier_manager](std::string_view trait_identifier, ast::NodeCPtr value) -> bool { - ModifierValue modifiers; - - bool ret = modifier_manager.expect_whitelisted_modifier_value(move_variable_callback(modifiers), allowed_modifiers)(value); - ret &= add_leader_trait(trait_identifier, trait_type_t::PERSONALITY, modifiers); - return ret; - } - ), - "background", ONE_EXACTLY, expect_dictionary( - [this, &modifier_manager](std::string_view trait_identifier, ast::NodeCPtr value) -> bool { - ModifierValue modifiers; - - bool ret = modifier_manager.expect_whitelisted_modifier_value(move_variable_callback(modifiers), allowed_modifiers)(value); - ret &= add_leader_trait(trait_identifier, trait_type_t::BACKGROUND, modifiers); - return ret; - } - ) - )(root); - lock_leader_traits(); - - return ret; -}
\ No newline at end of file +bool LeaderTraitManager::load_leader_traits_file(ModifierManager const& modifier_manager, ast::NodeCPtr root) { + using enum LeaderTrait::trait_type_t; + const auto trait_callback = [this, &modifier_manager](LeaderTrait::trait_type_t type) -> key_value_callback_t { + return [this, &modifier_manager, type](std::string_view trait_identifier, ast::NodeCPtr value) -> bool { + ModifierValue modifiers; + bool ret = modifier_manager.expect_whitelisted_modifier_value(move_variable_callback(modifiers), allowed_modifiers)(value); + ret &= add_leader_trait(trait_identifier, type, std::move(modifiers)); + return ret; + }; + }; + const bool ret = expect_dictionary_keys( + "personality", ONE_EXACTLY, expect_dictionary(trait_callback(PERSONALITY)), + "background", ONE_EXACTLY, expect_dictionary(trait_callback(BACKGROUND)) + )(root); + lock_leader_traits(); + + return ret; +} diff --git a/src/openvic-simulation/military/LeaderTrait.hpp b/src/openvic-simulation/military/LeaderTrait.hpp index f4445f6..e525e23 100644 --- a/src/openvic-simulation/military/LeaderTrait.hpp +++ b/src/openvic-simulation/military/LeaderTrait.hpp @@ -8,65 +8,65 @@ #include "openvic-simulation/Modifier.hpp" namespace OpenVic { - struct LeaderTraitManager; + struct LeaderTraitManager; - enum class trait_type_t { - PERSONALITY, - BACKGROUND - }; + struct LeaderTrait : HasIdentifier { + friend struct LeaderTraitManager; - struct LeaderTrait : HasIdentifier { - friend struct LeaderTraitManager; + enum class trait_type_t { + PERSONALITY, + BACKGROUND + }; - private: + private: - const trait_type_t type; - /* - * Allowed modifiers for leaders: - * attack - integer - * defence - integer - * morale - % - * organisation - % - * reconnaissance - % - * speed - % - * attrition - %, negative is good - * experience - % - * reliability - decimal, mil gain or loss for associated POPs - */ - const ModifierValue modifiers; + const trait_type_t type; + /* + * Allowed modifiers for leaders: + * attack - integer + * defence - integer + * morale - % + * organisation - % + * reconnaissance - % + * speed - % + * attrition - %, negative is good + * experience - % + * reliability - decimal, mil gain or loss for associated POPs + */ + const ModifierValue modifiers; - LeaderTrait(std::string_view new_identifier, trait_type_t new_type, ModifierValue new_modifiers); - - public: - LeaderTrait(LeaderTrait&&) = default; + LeaderTrait(std::string_view new_identifier, trait_type_t new_type, ModifierValue&& new_modifiers); - trait_type_t get_trait_type() const; - bool is_personality_trait() const; - bool is_background_trait() const; - ModifierValue get_modifiers() const; - }; + public: + LeaderTrait(LeaderTrait&&) = default; - struct LeaderTraitManager { - private: - IdentifierRegistry<LeaderTrait> leader_traits; - const std::set<std::string, std::less<void>> allowed_modifiers = { - "attack", - "defence", - "morale", - "organisation", - "reconnaissance", - "speed", - "attrition", - "experience", - "reliability" - }; - - public: - LeaderTraitManager(); + trait_type_t get_trait_type() const; + bool is_personality_trait() const; + bool is_background_trait() const; + ModifierValue const& get_modifiers() const; + }; - bool add_leader_trait(std::string_view identifier, trait_type_t type, ModifierValue modifiers); - IDENTIFIER_REGISTRY_ACCESSORS(leader_trait) + struct LeaderTraitManager { + private: + IdentifierRegistry<LeaderTrait> leader_traits; + inline static const string_set_t allowed_modifiers = { + "attack", + "defence", + "morale", + "organisation", + "reconnaissance", + "speed", + "attrition", + "experience", + "reliability" + }; - bool load_leader_traits_file(ModifierManager& modifier_manager, ast::NodeCPtr root); - }; + public: + LeaderTraitManager(); + + bool add_leader_trait(std::string_view identifier, LeaderTrait::trait_type_t type, ModifierValue&& modifiers); + IDENTIFIER_REGISTRY_ACCESSORS(leader_trait) + + bool load_leader_traits_file(ModifierManager const& modifier_manager, ast::NodeCPtr root); + }; } // namespace OpenVic diff --git a/src/openvic-simulation/military/Unit.cpp b/src/openvic-simulation/military/Unit.cpp index 8b010e3..294c77f 100644 --- a/src/openvic-simulation/military/Unit.cpp +++ b/src/openvic-simulation/military/Unit.cpp @@ -1,7 +1,5 @@ #include "Unit.hpp" -#include <set> - #define UNIT_ARGS \ icon, sprite, active, unit_type, floating_flag, priority, max_strength, default_organisation, maximum_speed, \ weighted_value, move_sound, select_sound, build_time, std::move(build_cost), supply_consumption, std::move(supply_cost) |