aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/modifier
diff options
context:
space:
mode:
author hop311 <hop3114@gmail.com>2024-10-19 12:39:08 +0200
committer hop311 <hop3114@gmail.com>2024-10-19 12:39:08 +0200
commit572ac597d8b43c4c97be4b68aa40de0e7ae6bfe0 (patch)
treec529d60fe49fbcd167522c0fb21ca10139cb5cc7 /src/openvic-simulation/modifier
parent35909d6e79d524f19f9b69dffd02fcf162be5093 (diff)
Require modifier type when parsing ModifierValues
Diffstat (limited to 'src/openvic-simulation/modifier')
-rw-r--r--src/openvic-simulation/modifier/Modifier.cpp27
-rw-r--r--src/openvic-simulation/modifier/Modifier.hpp9
-rw-r--r--src/openvic-simulation/modifier/ModifierManager.cpp92
-rw-r--r--src/openvic-simulation/modifier/ModifierManager.hpp50
4 files changed, 130 insertions, 48 deletions
diff --git a/src/openvic-simulation/modifier/Modifier.cpp b/src/openvic-simulation/modifier/Modifier.cpp
index 962d2d4..3cec7df 100644
--- a/src/openvic-simulation/modifier/Modifier.cpp
+++ b/src/openvic-simulation/modifier/Modifier.cpp
@@ -2,6 +2,33 @@
using namespace OpenVic;
+std::string_view Modifier::modifier_type_to_string(modifier_type_t type) {
+ using enum Modifier::modifier_type_t;
+
+ switch (type) {
+#define _CASE(X) case X: return #X;
+ _CASE(EVENT)
+ _CASE(STATIC)
+ _CASE(TRIGGERED)
+ _CASE(CRIME)
+ _CASE(TERRAIN)
+ _CASE(CLIMATE)
+ _CASE(CONTINENT)
+ _CASE(BUILDING)
+ _CASE(LEADER)
+ _CASE(UNIT_TERRAIN)
+ _CASE(NATIONAL_VALUE)
+ _CASE(NATIONAL_FOCUS)
+ _CASE(ISSUE)
+ _CASE(REFORM)
+ _CASE(TECHNOLOGY)
+ _CASE(INVENTION)
+ _CASE(TECH_SCHOOL)
+#undef _CASE
+ default: return "INVALID MODIFIER TYPE";
+ }
+}
+
Modifier::Modifier(std::string_view new_identifier, ModifierValue&& new_values, modifier_type_t new_type)
: HasIdentifier { new_identifier }, ModifierValue { std::move(new_values) }, type { new_type } {}
diff --git a/src/openvic-simulation/modifier/Modifier.hpp b/src/openvic-simulation/modifier/Modifier.hpp
index 16ccd21..1277c76 100644
--- a/src/openvic-simulation/modifier/Modifier.hpp
+++ b/src/openvic-simulation/modifier/Modifier.hpp
@@ -8,14 +8,19 @@
#include "openvic-simulation/utility/Getters.hpp"
namespace OpenVic {
+ struct UnitType;
+
struct Modifier : HasIdentifier, ModifierValue {
friend struct ModifierManager;
+ friend struct UnitType;
enum struct modifier_type_t : uint8_t {
- EVENT, STATIC, TRIGGERED, CRIME, TERRAIN, CLIMATE, CONTINENT, BUILDING, LEADER, NATIONAL_VALUE, NATIONAL_FOCUS,
- ISSUE, REFORM, TECHNOLOGY, INVENTION, TECH_SCHOOL
+ EVENT, STATIC, TRIGGERED, CRIME, TERRAIN, CLIMATE, CONTINENT, BUILDING, LEADER, UNIT_TERRAIN,
+ NATIONAL_VALUE, NATIONAL_FOCUS, ISSUE, REFORM, TECHNOLOGY, INVENTION, TECH_SCHOOL
};
+ static std::string_view modifier_type_to_string(modifier_type_t type);
+
private:
const modifier_type_t PROPERTY(type);
diff --git a/src/openvic-simulation/modifier/ModifierManager.cpp b/src/openvic-simulation/modifier/ModifierManager.cpp
index f21712d..5aacbe6 100644
--- a/src/openvic-simulation/modifier/ModifierManager.cpp
+++ b/src/openvic-simulation/modifier/ModifierManager.cpp
@@ -460,13 +460,15 @@ std::string ModifierManager::get_flat_identifier(
}
bool ModifierManager::add_event_modifier(std::string_view identifier, ModifierValue&& values, IconModifier::icon_t icon) {
+ using enum Modifier::modifier_type_t;
+
if (identifier.empty()) {
Logger::error("Invalid event modifier effect identifier - empty!");
return false;
}
return event_modifiers.add_item(
- { identifier, std::move(values), Modifier::modifier_type_t::EVENT, icon }, duplicate_warning_callback
+ { identifier, std::move(values), EVENT, icon }, duplicate_warning_callback
);
}
@@ -474,13 +476,19 @@ bool ModifierManager::load_event_modifiers(ast::NodeCPtr root) {
const bool ret = expect_dictionary_reserve_length(
event_modifiers,
[this](std::string_view key, ast::NodeCPtr value) -> bool {
+ using enum Modifier::modifier_type_t;
+
ModifierValue modifier_value;
IconModifier::icon_t icon = 0;
+
bool ret = expect_modifier_value_and_keys(
move_variable_callback(modifier_value),
+ EVENT,
"icon", ZERO_OR_ONE, expect_uint(assign_variable_callback(icon))
)(value);
+
ret &= add_event_modifier(key, std::move(modifier_value), icon);
+
return ret;
}
)(root);
@@ -491,13 +499,15 @@ bool ModifierManager::load_event_modifiers(ast::NodeCPtr root) {
}
bool ModifierManager::add_static_modifier(std::string_view identifier, ModifierValue&& values) {
+ using enum Modifier::modifier_type_t;
+
if (identifier.empty()) {
Logger::error("Invalid static modifier effect identifier - empty!");
return false;
}
return static_modifiers.add_item(
- { identifier, std::move(values), Modifier::modifier_type_t::STATIC }, duplicate_warning_callback
+ { identifier, std::move(values), STATIC }, duplicate_warning_callback
);
}
@@ -505,9 +515,14 @@ bool ModifierManager::load_static_modifiers(ast::NodeCPtr root) {
bool ret = expect_dictionary_reserve_length(
static_modifiers,
[this](std::string_view key, ast::NodeCPtr value) -> bool {
+ using enum Modifier::modifier_type_t;
+
ModifierValue modifier_value;
- bool ret = expect_modifier_value(move_variable_callback(modifier_value))(value);
+
+ bool ret = expect_modifier_value(move_variable_callback(modifier_value), STATIC)(value);
+
ret &= add_static_modifier(key, std::move(modifier_value));
+
return ret;
}
)(root);
@@ -522,13 +537,15 @@ bool ModifierManager::load_static_modifiers(ast::NodeCPtr root) {
bool ModifierManager::add_triggered_modifier(
std::string_view identifier, ModifierValue&& values, IconModifier::icon_t icon, ConditionScript&& trigger
) {
+ using enum Modifier::modifier_type_t;
+
if (identifier.empty()) {
Logger::error("Invalid triggered modifier effect identifier - empty!");
return false;
}
return triggered_modifiers.add_item(
- { identifier, std::move(values), Modifier::modifier_type_t::TRIGGERED, icon, std::move(trigger) },
+ { identifier, std::move(values), TRIGGERED, icon, std::move(trigger) },
duplicate_warning_callback
);
}
@@ -537,16 +554,21 @@ bool ModifierManager::load_triggered_modifiers(ast::NodeCPtr root) {
const bool ret = expect_dictionary_reserve_length(
triggered_modifiers,
[this](std::string_view key, ast::NodeCPtr value) -> bool {
+ using enum Modifier::modifier_type_t;
+
ModifierValue modifier_value;
IconModifier::icon_t icon = 0;
ConditionScript trigger { scope_t::COUNTRY, scope_t::COUNTRY, scope_t::NO_SCOPE };
bool ret = expect_modifier_value_and_keys(
move_variable_callback(modifier_value),
+ TRIGGERED,
"icon", ZERO_OR_ONE, expect_uint(assign_variable_callback(icon)),
"trigger", ONE_EXACTLY, trigger.expect_script()
)(value);
+
ret &= add_triggered_modifier(key, std::move(modifier_value), icon, std::move(trigger));
+
return ret;
}
)(root);
@@ -567,7 +589,8 @@ bool ModifierManager::parse_scripts(DefinitionManager const& definition_manager)
}
key_value_callback_t ModifierManager::_modifier_effect_callback(
- ModifierValue& modifier, key_value_callback_t default_callback, ModifierEffectValidator auto effect_validator
+ ModifierValue& modifier, Modifier::modifier_type_t type, key_value_callback_t default_callback,
+ ModifierEffectValidator auto effect_validator
) const {
const auto add_modifier_cb = [this, &modifier, effect_validator](
ModifierEffect const* effect, ast::NodeCPtr value
@@ -578,9 +601,11 @@ key_value_callback_t ModifierManager::_modifier_effect_callback(
"local_artisan_output", "artisan_input", "artisan_throughput", "artisan_output",
"import_cost", "unciv_economic_modifier", "unciv_military_modifier"
};
+
if (no_effect_modifiers.contains(effect->get_identifier())) {
Logger::warning("This modifier does nothing: ", effect->get_identifier());
}
+
return expect_fixed_point(map_callback(modifier.values, effect))(value);
} else {
Logger::error("Failed to validate modifier effect: ", effect->get_identifier());
@@ -630,43 +655,52 @@ key_value_callback_t ModifierManager::_modifier_effect_callback(
}
node_callback_t ModifierManager::expect_validated_modifier_value_and_default(
- callback_t<ModifierValue&&> modifier_callback, key_value_callback_t default_callback,
+ callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type, key_value_callback_t default_callback,
ModifierEffectValidator auto effect_validator
) const {
- return [this, modifier_callback, default_callback, effect_validator](ast::NodeCPtr root) -> bool {
+ return [this, modifier_callback, type, default_callback, effect_validator](ast::NodeCPtr root) -> bool {
ModifierValue modifier;
+
bool ret = expect_dictionary_reserve_length(
modifier.values,
- _modifier_effect_callback(modifier, default_callback, effect_validator)
+ _modifier_effect_callback(modifier, type, default_callback, effect_validator)
)(root);
+
ret &= modifier_callback(std::move(modifier));
+
return ret;
};
}
node_callback_t ModifierManager::expect_validated_modifier_value(
- callback_t<ModifierValue&&> modifier_callback, ModifierEffectValidator auto effect_validator
+ callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type,
+ ModifierEffectValidator auto effect_validator
) const {
- return expect_validated_modifier_value_and_default(modifier_callback, key_value_invalid_callback, effect_validator);
+ return expect_validated_modifier_value_and_default(modifier_callback, type, key_value_invalid_callback, effect_validator);
}
node_callback_t ModifierManager::expect_modifier_value_and_default(
- callback_t<ModifierValue&&> modifier_callback, key_value_callback_t default_callback
+ callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type, key_value_callback_t default_callback
) const {
- return expect_validated_modifier_value_and_default(modifier_callback, default_callback, [](ModifierEffect const&) -> bool {
- return true;
- });
+ return expect_validated_modifier_value_and_default(
+ modifier_callback, type, default_callback, [](ModifierEffect const&) -> bool {
+ return true;
+ }
+ );
}
-node_callback_t ModifierManager::expect_modifier_value(callback_t<ModifierValue&&> modifier_callback) const {
- return expect_modifier_value_and_default(modifier_callback, key_value_invalid_callback);
+node_callback_t ModifierManager::expect_modifier_value(
+ callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type
+) const {
+ return expect_modifier_value_and_default(modifier_callback, type, key_value_invalid_callback);
}
node_callback_t ModifierManager::expect_whitelisted_modifier_value_and_default(
- callback_t<ModifierValue&&> modifier_callback, string_set_t const& whitelist, key_value_callback_t default_callback
+ callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type, string_set_t const& whitelist,
+ key_value_callback_t default_callback
) const {
return expect_validated_modifier_value_and_default(
- modifier_callback, default_callback,
+ modifier_callback, type, default_callback,
[&whitelist](ModifierEffect const& effect) -> bool {
return whitelist.contains(effect.get_identifier());
}
@@ -674,26 +708,32 @@ node_callback_t ModifierManager::expect_whitelisted_modifier_value_and_default(
}
node_callback_t ModifierManager::expect_whitelisted_modifier_value(
- callback_t<ModifierValue&&> modifier_callback, string_set_t const& whitelist
+ callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type, string_set_t const& whitelist
) const {
- return expect_whitelisted_modifier_value_and_default(modifier_callback, whitelist, key_value_invalid_callback);
+ return expect_whitelisted_modifier_value_and_default(modifier_callback, type, whitelist, key_value_invalid_callback);
}
node_callback_t ModifierManager::expect_modifier_value_and_key_map_and_default(
- callback_t<ModifierValue&&> modifier_callback, key_value_callback_t default_callback, key_map_t&& key_map
+ callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type, key_value_callback_t default_callback,
+ key_map_t&& key_map
) const {
- return [this, modifier_callback, default_callback, key_map = std::move(key_map)](ast::NodeCPtr node) mutable -> bool {
+ return [this, modifier_callback, type, default_callback, key_map = std::move(key_map)](
+ ast::NodeCPtr node
+ ) mutable -> bool {
bool ret = expect_modifier_value_and_default(
- modifier_callback,
- dictionary_keys_callback(key_map, default_callback)
+ modifier_callback, type, dictionary_keys_callback(key_map, default_callback)
)(node);
+
ret &= check_key_map_counts(key_map);
+
return ret;
};
}
node_callback_t ModifierManager::expect_modifier_value_and_key_map(
- callback_t<ModifierValue&&> modifier_callback, key_map_t&& key_map
+ callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type, key_map_t&& key_map
) const {
- return expect_modifier_value_and_key_map_and_default(modifier_callback, key_value_invalid_callback, std::move(key_map));
+ return expect_modifier_value_and_key_map_and_default(
+ modifier_callback, type, key_value_invalid_callback, std::move(key_map)
+ );
}
diff --git a/src/openvic-simulation/modifier/ModifierManager.hpp b/src/openvic-simulation/modifier/ModifierManager.hpp
index 73d974d..f3673b0 100644
--- a/src/openvic-simulation/modifier/ModifierManager.hpp
+++ b/src/openvic-simulation/modifier/ModifierManager.hpp
@@ -36,7 +36,7 @@ namespace OpenVic {
/* effect_validator takes in ModifierEffect const& */
NodeTools::key_value_callback_t _modifier_effect_callback(
- ModifierValue& modifier, NodeTools::key_value_callback_t default_callback,
+ ModifierValue& modifier, Modifier::modifier_type_t type, NodeTools::key_value_callback_t default_callback,
ModifierEffectValidator auto effect_validator
) const;
@@ -71,56 +71,66 @@ namespace OpenVic {
bool parse_scripts(DefinitionManager const& definition_manager);
NodeTools::node_callback_t expect_validated_modifier_value_and_default(
- NodeTools::callback_t<ModifierValue&&> modifier_callback, NodeTools::key_value_callback_t default_callback,
- ModifierEffectValidator auto effect_validator
+ NodeTools::callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type,
+ NodeTools::key_value_callback_t default_callback, ModifierEffectValidator auto effect_validator
) const;
NodeTools::node_callback_t expect_validated_modifier_value(
- NodeTools::callback_t<ModifierValue&&> modifier_callback, ModifierEffectValidator auto effect_validator
+ NodeTools::callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type,
+ ModifierEffectValidator auto effect_validator
) const;
NodeTools::node_callback_t expect_modifier_value_and_default(
- NodeTools::callback_t<ModifierValue&&> modifier_callback, NodeTools::key_value_callback_t default_callback
+ NodeTools::callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type,
+ NodeTools::key_value_callback_t default_callback
+ ) const;
+ NodeTools::node_callback_t expect_modifier_value(
+ NodeTools::callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type
) const;
- NodeTools::node_callback_t expect_modifier_value(NodeTools::callback_t<ModifierValue&&> modifier_callback) const;
NodeTools::node_callback_t expect_whitelisted_modifier_value_and_default(
- NodeTools::callback_t<ModifierValue&&> modifier_callback, string_set_t const& whitelist,
- NodeTools::key_value_callback_t default_callback
+ NodeTools::callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type,
+ string_set_t const& whitelist, NodeTools::key_value_callback_t default_callback
) const;
NodeTools::node_callback_t expect_whitelisted_modifier_value(
- NodeTools::callback_t<ModifierValue&&> modifier_callback, string_set_t const& whitelist
+ NodeTools::callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type,
+ string_set_t const& whitelist
) const;
+ // In the functions below, key_map refers to a map from identifier strings to NodeTools::dictionary_entry_t,
+ // allowing any non-modifier effect keys found to be parsed in a custom way, similar to expect_dictionary_keys.
NodeTools::node_callback_t expect_modifier_value_and_key_map_and_default(
- NodeTools::callback_t<ModifierValue&&> modifier_callback, NodeTools::key_value_callback_t default_callback,
- NodeTools::key_map_t&& key_map
+ NodeTools::callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type,
+ NodeTools::key_value_callback_t default_callback, NodeTools::key_map_t&& key_map
) const;
NodeTools::node_callback_t expect_modifier_value_and_key_map(
- NodeTools::callback_t<ModifierValue&&> modifier_callback, NodeTools::key_map_t&& key_map
+ NodeTools::callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type,
+ NodeTools::key_map_t&& key_map
) const;
template<typename... Args>
NodeTools::node_callback_t expect_modifier_value_and_key_map_and_default(
- NodeTools::callback_t<ModifierValue&&> modifier_callback, NodeTools::key_value_callback_t default_callback,
- NodeTools::key_map_t&& key_map, Args... args
+ NodeTools::callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type,
+ NodeTools::key_value_callback_t default_callback, NodeTools::key_map_t&& key_map, Args... args
) const {
NodeTools::add_key_map_entries(key_map, args...);
- return expect_modifier_value_and_key_map_and_default(modifier_callback, default_callback, std::move(key_map));
+ return expect_modifier_value_and_key_map_and_default(
+ modifier_callback, type, default_callback, std::move(key_map)
+ );
}
template<typename... Args>
NodeTools::node_callback_t expect_modifier_value_and_keys_and_default(
- NodeTools::callback_t<ModifierValue&&> modifier_callback, NodeTools::key_value_callback_t default_callback,
- Args... args
+ NodeTools::callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type,
+ NodeTools::key_value_callback_t default_callback, Args... args
) const {
- return expect_modifier_value_and_key_map_and_default(modifier_callback, default_callback, {}, args...);
+ return expect_modifier_value_and_key_map_and_default(modifier_callback, type, default_callback, {}, args...);
}
template<typename... Args>
NodeTools::node_callback_t expect_modifier_value_and_keys(
- NodeTools::callback_t<ModifierValue&&> modifier_callback, Args... args
+ NodeTools::callback_t<ModifierValue&&> modifier_callback, Modifier::modifier_type_t type, Args... args
) const {
return expect_modifier_value_and_key_map_and_default(
- modifier_callback, NodeTools::key_value_invalid_callback, {}, args...
+ modifier_callback, type, NodeTools::key_value_invalid_callback, {}, args...
);
}
};