From e50c67eb1aaa54f5fb31425f81616bea4e6b880a Mon Sep 17 00:00:00 2001 From: hop311 Date: Thu, 12 Oct 2023 20:19:00 +0100 Subject: Lots of accumulated changes --- src/openvic-simulation/economy/Good.hpp | 4 +- src/openvic-simulation/economy/ProductionType.cpp | 80 +++++++++++------------ src/openvic-simulation/economy/ProductionType.hpp | 6 +- 3 files changed, 43 insertions(+), 47 deletions(-) (limited to 'src/openvic-simulation/economy') diff --git a/src/openvic-simulation/economy/Good.hpp b/src/openvic-simulation/economy/Good.hpp index 7f020fe..1dce41f 100644 --- a/src/openvic-simulation/economy/Good.hpp +++ b/src/openvic-simulation/economy/Good.hpp @@ -66,11 +66,11 @@ namespace OpenVic { GoodManager(); bool add_good_category(std::string_view identifier); - IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(GoodCategory, good_category, good_categories) + IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(good_category, good_categories) bool add_good(std::string_view identifier, colour_t colour, GoodCategory const* category, Good::price_t base_price, bool available_from_start, bool tradeable, bool money, bool overseas_penalty); - IDENTIFIER_REGISTRY_ACCESSORS(Good, good) + IDENTIFIER_REGISTRY_ACCESSORS(good) void reset_to_defaults(); bool load_goods_file(ast::NodeCPtr root); diff --git a/src/openvic-simulation/economy/ProductionType.cpp b/src/openvic-simulation/economy/ProductionType.cpp index 2c7c431..b2d94c3 100644 --- a/src/openvic-simulation/economy/ProductionType.cpp +++ b/src/openvic-simulation/economy/ProductionType.cpp @@ -82,16 +82,22 @@ bool ProductionType::is_mine() const { ProductionTypeManager::ProductionTypeManager() : production_types { "production types" } {} -node_callback_t ProductionTypeManager::_expect_employed_pop(GoodManager& good_manager, PopManager& pop_manager, +node_callback_t ProductionTypeManager::_expect_employed_pop(GoodManager const& good_manager, PopManager const& pop_manager, callback_t cb) { return [this, &good_manager, &pop_manager, cb](ast::NodeCPtr node) -> bool { - std::string_view pop_type, effect; + std::string_view pop_type; + EmployedPop::effect_t effect; fixed_point_t effect_multiplier = 1, amount = 1; + using enum EmployedPop::effect_t; + static const string_map_t effect_map = { + { "input", INPUT }, { "output", OUTPUT }, { "throughput", THROUGHPUT } + }; + bool res = expect_dictionary_keys( "poptype", ONE_EXACTLY, expect_identifier(assign_variable_callback(pop_type)), - "effect", ONE_EXACTLY, expect_identifier(assign_variable_callback(effect)), + "effect", ONE_EXACTLY, expect_identifier(expect_mapped_string(effect_map, assign_variable_callback(effect))), "effect_multiplier", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(effect_multiplier)), "amount", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(amount)) )(node); @@ -107,20 +113,11 @@ node_callback_t ProductionTypeManager::_expect_employed_pop(GoodManager& good_ma } } - EmployedPop::effect_t found_effect; - if (effect == "input") found_effect = EmployedPop::effect_t::INPUT; - else if (effect == "output") found_effect = EmployedPop::effect_t::OUTPUT; - else if (effect == "throughput") found_effect = EmployedPop::effect_t::THROUGHPUT; - else { - Logger::error("Found invalid effect ", effect, " while parsing production types!"); - return false; - } - - return res & cb(EmployedPop { found_pop_type, artisan, found_effect, effect_multiplier, amount }); + return res & cb(EmployedPop { found_pop_type, artisan, effect, effect_multiplier, amount }); }; } -node_callback_t ProductionTypeManager::_expect_employed_pop_list(GoodManager& good_manager, PopManager& pop_manager, +node_callback_t ProductionTypeManager::_expect_employed_pop_list(GoodManager const& good_manager, PopManager const& pop_manager, callback_t> cb) { return [this, &good_manager, &pop_manager, cb](ast::NodeCPtr node) -> bool { @@ -174,19 +171,20 @@ bool ProductionTypeManager::add_production_type(PRODUCTION_TYPE_ARGS, GoodManage }); } -#define PARSE_NODE(target_node) expect_dictionary_keys(ALLOW_OTHER_KEYS, \ +#define PARSE_NODE expect_dictionary_keys_and_default( \ + key_value_success_callback, \ "owner", ZERO_OR_ONE, _expect_employed_pop(good_manager, pop_manager, move_variable_callback(owner)), \ "employees", ZERO_OR_ONE, _expect_employed_pop_list(good_manager, pop_manager, move_variable_callback(employees)), \ - "type", ZERO_OR_ONE, expect_identifier(assign_variable_callback(type)), \ - "workforce", ZERO_OR_ONE, expect_uint(assign_variable_callback_uint(workforce)), \ + "type", ZERO_OR_ONE, expect_identifier(expect_mapped_string(type_map, assign_variable_callback(type))), \ + "workforce", ZERO_OR_ONE, expect_uint(assign_variable_callback(workforce)), \ "input_goods", ZERO_OR_ONE, good_manager.expect_good_decimal_map(move_variable_callback(input_goods)), \ - "output_goods", ZERO_OR_ONE, good_manager.expect_good_identifier(assign_variable_callback_pointer(output_goods)), \ + "output_goods", ZERO_OR_ONE, expect_identifier(good_manager.expect_good_identifier(assign_variable_callback_pointer(output_goods))), \ "value", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(value)), \ "efficiency", ZERO_OR_ONE, good_manager.expect_good_decimal_map(move_variable_callback(efficiency)), \ "is_coastal", ZERO_OR_ONE, expect_bool(assign_variable_callback(coastal)), \ "farm", ZERO_OR_ONE, expect_bool(assign_variable_callback(farm)), \ "mine", ZERO_OR_ONE, expect_bool(assign_variable_callback(mine)) \ - )(target_node) + ) bool ProductionTypeManager::load_production_types_file(GoodManager& good_manager, PopManager& pop_manager, ast::NodeCPtr root) { size_t expected_types = 0; @@ -196,25 +194,27 @@ bool ProductionTypeManager::load_production_types_file(GoodManager& good_manager std::map template_target_map; bool ret = expect_dictionary( [this, &expected_types, &templates, &template_target_map](std::string_view key, ast::NodeCPtr value) -> bool { - std::string_view template_id = ""; - bool ret = expect_dictionary_keys(ALLOW_OTHER_KEYS, - "template", ZERO_OR_ONE, expect_identifier(assign_variable_callback(template_id)) - )(value); - - if (!template_id.empty()) { - templates.emplace(template_id); - template_target_map.emplace(key, template_id); - } - expected_types++; - return ret; + std::string_view template_id = ""; + bool found_template = false; + const bool ret = expect_key("template", expect_identifier(assign_variable_callback(template_id)), &found_template)(value); + if (found_template) { + if (ret) { + templates.emplace(template_id); + template_target_map.emplace(key, template_id); + } else { + Logger::error("Failed get template identifier for ", key); + return false; + } + } + return true; } )(root); // pass 2: create and populate the template map std::map template_node_map; - expect_dictionary( + ret &= expect_dictionary( [this, &expected_types, &templates, &template_node_map](std::string_view key, ast::NodeCPtr value) -> bool { if (templates.contains(key)) { template_node_map.emplace(key, value); @@ -233,7 +233,7 @@ bool ProductionTypeManager::load_production_types_file(GoodManager& good_manager EmployedPop owner; std::vector employees; - std::string_view type; + ProductionType::type_t type; Good const* output_goods = nullptr; Pop::pop_size_t workforce = 0; // 0 is a meaningless value -> unset std::map input_goods, efficiency; @@ -243,6 +243,11 @@ bool ProductionTypeManager::load_production_types_file(GoodManager& good_manager bool ret = true; + using enum ProductionType::type_t; + static const string_map_t type_map = { + { "factory", FACTORY }, { "rgo", RGO }, { "artisan", ARTISAN } + }; + // apply template first if (template_target_map.contains(key)) { std::string_view template_id = template_target_map[key]; @@ -254,17 +259,8 @@ bool ProductionTypeManager::load_production_types_file(GoodManager& good_manager ret &= PARSE_NODE(node); - ProductionType::type_t type_enum; - if (type == "factory") type_enum = ProductionType::type_t::FACTORY; - else if (type == "rgo") type_enum = ProductionType::type_t::RGO; - else if (type == "artisan") type_enum = ProductionType::type_t::ARTISAN; - else { - Logger::error("Invalid production type for ", key, ": ", type); - ret = false; - } - ret &= add_production_type( - key, owner, employees, type_enum, workforce, input_goods, output_goods, value, + key, owner, employees, type, workforce, input_goods, output_goods, value, bonuses, efficiency, coastal, farm, mine, good_manager ); return ret; diff --git a/src/openvic-simulation/economy/ProductionType.hpp b/src/openvic-simulation/economy/ProductionType.hpp index 755eda8..fdb0010 100644 --- a/src/openvic-simulation/economy/ProductionType.hpp +++ b/src/openvic-simulation/economy/ProductionType.hpp @@ -95,16 +95,16 @@ namespace OpenVic { private: IdentifierRegistry production_types; - NodeTools::node_callback_t _expect_employed_pop(GoodManager& good_manager, PopManager& pop_manager, + NodeTools::node_callback_t _expect_employed_pop(GoodManager const& good_manager, PopManager const& pop_manager, NodeTools::callback_t cb); - NodeTools::node_callback_t _expect_employed_pop_list(GoodManager& good_manager, PopManager& pop_manager, + NodeTools::node_callback_t _expect_employed_pop_list(GoodManager const& good_manager, PopManager const& pop_manager, NodeTools::callback_t> cb); public: ProductionTypeManager(); bool add_production_type(PRODUCTION_TYPE_ARGS, GoodManager& good_manager); - IDENTIFIER_REGISTRY_ACCESSORS(ProductionType, production_type) + IDENTIFIER_REGISTRY_ACCESSORS(production_type) bool load_production_types_file(GoodManager& good_manager, PopManager& pop_manager, ast::NodeCPtr root); }; -- cgit v1.2.3-56-ga3b1