From ce84886cb931975f622134d6c8d32a69c675d975 Mon Sep 17 00:00:00 2001 From: hop311 Date: Sat, 11 Nov 2023 20:49:52 +0000 Subject: Directory lookup refactor+suppressed wargoal error --- src/openvic-simulation/military/Wargoal.cpp | 165 +++++++++------------------- src/openvic-simulation/military/Wargoal.hpp | 50 ++++----- 2 files changed, 68 insertions(+), 147 deletions(-) (limited to 'src/openvic-simulation/military') diff --git a/src/openvic-simulation/military/Wargoal.cpp b/src/openvic-simulation/military/Wargoal.cpp index 8bc4446..b493f22 100644 --- a/src/openvic-simulation/military/Wargoal.cpp +++ b/src/openvic-simulation/military/Wargoal.cpp @@ -33,54 +33,6 @@ WargoalType::WargoalType( modifiers { std::move(new_modifiers) }, peace_options { new_peace_options } {} -std::string_view WargoalType::get_sprite() const { - return sprite; -} - -std::string_view WargoalType::get_war_name() const { - return war_name; -} - -const Timespan WargoalType::get_available_length() const { - return available_length; -} - -const Timespan WargoalType::get_truce_length() const { - return truce_length; -} - -const bool WargoalType::is_triggered_only() const { - return triggered_only; -} - -const bool WargoalType::is_civil_war() const { - return civil_war; -} - -const bool WargoalType::is_constructing() const { - return constructing; -} - -const bool WargoalType::is_crisis() const { - return crisis; -} - -const bool WargoalType::is_great_war() const { - return great_war; -} - -const bool WargoalType::is_mutual() const { - return mutual; -} - -WargoalType::peace_modifiers_t const& WargoalType::get_modifiers() const { - return modifiers; -} - -const peace_options_t WargoalType::get_peace_options() const { - return peace_options; -} - WargoalTypeManager::WargoalTypeManager() : wargoal_types { "wargoal types" } {} const std::vector& WargoalTypeManager::get_peace_priority_list() const { @@ -106,7 +58,7 @@ bool WargoalTypeManager::add_wargoal_type( Logger::error("Invalid wargoal identifier - empty!"); return false; } - + if (sprite.empty()) { Logger::error("Invalid sprite for wargoal ", identifier, " - empty!"); return false; @@ -117,72 +69,47 @@ bool WargoalTypeManager::add_wargoal_type( return false; } - return wargoal_types.add_item({ identifier, sprite, war_name, available_length, truce_length, triggered_only, civil_war, constructing, crisis, great_war, mutual, std::move(modifiers), peace_options }); + return wargoal_types.add_item({ + identifier, sprite, war_name, available_length, truce_length, triggered_only, civil_war, constructing, crisis, + great_war, mutual, std::move(modifiers), peace_options + }); } bool WargoalTypeManager::load_wargoal_file(ast::NodeCPtr root) { bool ret = expect_dictionary( [this](std::string_view identifier, ast::NodeCPtr value) -> bool { if (identifier == "peace_order") return true; - + std::string_view sprite, war_name; Timespan available, truce; - bool triggered_only = false, civil_war = false, constructing = false, crisis = false, great_war = false, mutual = false; - peace_options_t peace_options; + bool triggered_only = false, civil_war = false, constructing = true, crisis = true, great_war = false, + mutual = false; + peace_options_t peace_options {}; WargoalType::peace_modifiers_t modifiers; bool ret = expect_dictionary_keys_and_default( [&modifiers, &identifier](std::string_view key, ast::NodeCPtr value) -> bool { - fixed_point_t modifier; - expect_fixed_point(assign_variable_callback(modifier))(value); - - if (key == "badboy_factor") { - modifiers[WargoalType::PEACE_MODIFIERS::BADBOY_FACTOR] += modifier; - return true; - } - if (key == "prestige_factor") { - modifiers[WargoalType::PEACE_MODIFIERS::PRESTIGE_FACTOR] += modifier; - return true; - } - if (key == "peace_cost_factor") { - modifiers[WargoalType::PEACE_MODIFIERS::PEACE_COST_FACTOR] += modifier; - return true; - } - if (key == "penalty_factor") { - modifiers[WargoalType::PEACE_MODIFIERS::PENALTY_FACTOR] += modifier; - return true; - } - if (key == "break_truce_prestige_factor") { - modifiers[WargoalType::PEACE_MODIFIERS::BREAK_TRUCE_PRESTIGE_FACTOR] += modifier; - return true; - } - if (key == "break_truce_infamy_factor") { - modifiers[WargoalType::PEACE_MODIFIERS::BREAK_TRUCE_INFAMY_FACTOR] += modifier; - return true; - } - if (key == "break_truce_militancy_factor") { - modifiers[WargoalType::PEACE_MODIFIERS::BREAK_TRUCE_MILITANCY_FACTOR] += modifier; - return true; - } - if (key == "good_relation_prestige_factor") { - modifiers[WargoalType::PEACE_MODIFIERS::GOOD_RELATION_PRESTIGE_FACTOR] += modifier; - return true; - } - if (key == "good_relation_infamy_factor") { - modifiers[WargoalType::PEACE_MODIFIERS::GOOD_RELATION_INFAMY_FACTOR] += modifier; - return true; - } - if (key == "good_relation_militancy_factor") { - modifiers[WargoalType::PEACE_MODIFIERS::GOOD_RELATION_MILITANCY_FACTOR] += modifier; - return true; - } - if (key == "tws_battle_factor") { - modifiers[WargoalType::PEACE_MODIFIERS::WAR_SCORE_BATTLE_FACTOR] += modifier; - return true; - } - if (key == "construction_speed") { - modifiers[WargoalType::PEACE_MODIFIERS::CONSTRUCTION_SPEED] += modifier; - return true; + using enum WargoalType::PEACE_MODIFIERS; + static const string_map_t peace_modifier_map { + { "badboy_factor", BADBOY_FACTOR }, + { "prestige_factor", PRESTIGE_FACTOR }, + { "peace_cost_factor", PEACE_COST_FACTOR }, + { "penalty_factor", PENALTY_FACTOR }, + { "break_truce_prestige_factor", BREAK_TRUCE_PRESTIGE_FACTOR }, + { "break_truce_infamy_factor", BREAK_TRUCE_INFAMY_FACTOR }, + { "break_truce_militancy_factor", BREAK_TRUCE_MILITANCY_FACTOR }, + { "good_relation_prestige_factor", GOOD_RELATION_PRESTIGE_FACTOR }, + { "good_relation_infamy_factor", GOOD_RELATION_INFAMY_FACTOR }, + { "good_relation_militancy_factor", GOOD_RELATION_MILITANCY_FACTOR }, + { "tws_battle_factor", WAR_SCORE_BATTLE_FACTOR }, + { "construction_speed", CONSTRUCTION_SPEED } + }; + const decltype(peace_modifier_map)::const_iterator it = peace_modifier_map.find(key); + if (it != peace_modifier_map.end()) { + return expect_fixed_point([&modifiers, peace_modifier = it->second](fixed_point_t val) -> bool { + modifiers[peace_modifier] = val; + return true; + })(value); } Logger::error("Modifier ", key, " in wargoal ", identifier, " is invalid."); @@ -284,24 +211,30 @@ bool WargoalTypeManager::load_wargoal_file(ast::NodeCPtr root) { "always", ZERO_OR_ONE, success_callback // usage unknown / quirk )(value); - add_wargoal_type(identifier, sprite, war_name, available, truce, triggered_only, civil_war, constructing, crisis, great_war, mutual, std::move(modifiers), peace_options); + add_wargoal_type( + identifier, sprite, war_name, available, truce, triggered_only, civil_war, constructing, crisis, great_war, + mutual, std::move(modifiers), peace_options + ); return ret; } )(root); /* load order in which CBs are prioritised by AI */ - ret &= expect_dictionary_keys_and_default( - key_value_success_callback, - "peace_order", ONE_EXACTLY, expect_list([this](ast::NodeCPtr value) -> bool { - WargoalType const* wargoal; - bool ret = expect_wargoal_type_identifier(assign_variable_callback_pointer(wargoal))(value); - if (ret) { - peace_priorities.push_back(wargoal); - } else { - Logger::warning("Attempted to add invalid wargoal type to AI peace order!"); - } - return true; - }) + ret &= expect_key( + "peace_order", + expect_list( + expect_wargoal_type_identifier( + [this](WargoalType const& wargoal) -> bool { + if (std::find(peace_priorities.begin(), peace_priorities.end(), &wargoal) == peace_priorities.end()) { + peace_priorities.push_back(&wargoal); + } else { + Logger::warning("Wargoal ", wargoal.get_identifier(), " is already in the peace priority list!"); + } + return true; + }, + true // warn instead of error + ) + ) )(root); lock_wargoal_types(); diff --git a/src/openvic-simulation/military/Wargoal.hpp b/src/openvic-simulation/military/Wargoal.hpp index dbb8d67..3700347 100644 --- a/src/openvic-simulation/military/Wargoal.hpp +++ b/src/openvic-simulation/military/Wargoal.hpp @@ -1,8 +1,9 @@ #pragma once -#include "openvic-simulation/types/IdentifierRegistry.hpp" -#include "openvic-simulation/types/EnumBitfield.hpp" #include "openvic-simulation/Modifier.hpp" +#include "openvic-simulation/types/EnumBitfield.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include "openvic-simulation/utility/Getters.hpp" namespace OpenVic { struct WargoalTypeManager; @@ -46,22 +47,22 @@ namespace OpenVic { WAR_SCORE_BATTLE_FACTOR, CONSTRUCTION_SPEED }; - using peace_modifiers_t = std::map; - + using peace_modifiers_t = decimal_map_t; + private: - const std::string sprite; - const std::string war_name; - const Timespan available_length; - const Timespan truce_length; - const bool triggered_only; // only able to be added via effects (or within the code) - const bool civil_war; - const bool constructing; // can be added to existing wars or justified - const bool crisis; // able to be added to crises - const bool great_war; // automatically add to great wars - const bool mutual; // attacked and defender share wargoal - const peace_modifiers_t modifiers; - const peace_options_t peace_options; - + const std::string PROPERTY(sprite); + const std::string PROPERTY(war_name); + const Timespan PROPERTY(available_length); + const Timespan PROPERTY(truce_length); + const bool PROPERTY(triggered_only); // only able to be added via effects (or within the code) + const bool PROPERTY(civil_war); + const bool PROPERTY(constructing); // can be added to existing wars or justified + const bool PROPERTY(crisis); // able to be added to crises + const bool PROPERTY(great_war); // automatically add to great wars + const bool PROPERTY(mutual); // attacked and defender share wargoal + const peace_modifiers_t PROPERTY(modifiers); + const peace_options_t PROPERTY(peace_options); + // TODO: can_use, prerequisites, on_add, on_po_accepted WargoalType( @@ -82,25 +83,12 @@ namespace OpenVic { public: WargoalType(WargoalType&&) = default; - - std::string_view get_sprite() const; - std::string_view get_war_name() const; - const Timespan get_available_length() const; - const Timespan get_truce_length() const; - const bool is_triggered_only() const; - const bool is_civil_war() const; - const bool is_constructing() const; - const bool is_crisis() const; - const bool is_great_war() const; - const bool is_mutual() const; - peace_modifiers_t const& get_modifiers() const; - const peace_options_t get_peace_options() const; }; struct WargoalTypeManager { private: IdentifierRegistry wargoal_types; - std::vector peace_priorities; + std::vector PROPERTY(peace_priorities); public: WargoalTypeManager(); -- cgit v1.2.3-56-ga3b1