From 1e40569a49ab0d557a2a43ee900f4f28d5c81cd3 Mon Sep 17 00:00:00 2001 From: Hop311 Date: Thu, 28 Sep 2023 23:33:46 +0100 Subject: Data types, defaults, callback cleanup --- src/openvic-simulation/map/Building.cpp | 89 ++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 40 deletions(-) (limited to 'src/openvic-simulation/map/Building.cpp') diff --git a/src/openvic-simulation/map/Building.cpp b/src/openvic-simulation/map/Building.cpp index 4e26ab9..6f4c099 100644 --- a/src/openvic-simulation/map/Building.cpp +++ b/src/openvic-simulation/map/Building.cpp @@ -5,7 +5,7 @@ using namespace OpenVic; using namespace OpenVic::NodeTools; -Building::Building(std::string_view identifier, ARGS) : HasIdentifier { identifier }, ModifierValue { std::move(modifiers) }, type { type }, +Building::Building(std::string_view identifier, BuildingType const& type, ARGS) : HasIdentifier { identifier }, ModifierValue { std::move(modifiers) }, type { type }, on_completion { on_completion }, completion_size { completion_size }, max_level { max_level }, goods_cost { goods_cost }, cost { cost }, build_time { build_time }, visibility { visibility }, on_map { on_map }, default_enabled { default_enabled }, production_type { production_type }, pop_build_factory { pop_build_factory }, strategic_factory { strategic_factory }, advanced_factory { advanced_factory }, fort_level { fort_level }, naval_capacity { naval_capacity }, @@ -17,8 +17,8 @@ BuildingType const& Building::get_type() const { return type; } -std::string_view Building::get_on_completion() const { - return on_completion; +std::string const& Building::get_on_completion() const { + return on_completion; } fixed_point_t Building::get_completion_size() const { @@ -29,7 +29,7 @@ Building::level_t Building::get_max_level() const { return max_level; } -std::map const& Building::get_goods_cost() const { +std::map const& Building::get_goods_cost() const { return goods_cost; } @@ -77,7 +77,7 @@ uint64_t Building::get_naval_capacity() const { return naval_capacity; } -std::vector const& Building::get_colonial_points() const { +std::vector const& Building::get_colonial_points() const { return colonial_points; } @@ -184,52 +184,72 @@ bool BuildingManager::add_building_type(std::string_view identifier) { return building_types.add_item({ identifier }); } -bool BuildingManager::add_building(std::string_view identifier, ARGS) { +bool BuildingManager::add_building(std::string_view identifier, BuildingType const* type, ARGS) { if (identifier.empty()) { Logger::error("Invalid building identifier - empty!"); return false; } + if (type == nullptr) { + Logger::error("Invalid building type for ", identifier, ": null"); + return false; + } return buildings.add_item({ - identifier, type, on_completion, completion_size, max_level, goods_cost, cost, build_time, visibility, on_map, default_enabled, - production_type, pop_build_factory, strategic_factory, advanced_factory, fort_level, naval_capacity, colonial_points, in_province, one_per_state, + identifier, *type, on_completion, completion_size, max_level, goods_cost, cost, build_time, visibility, on_map, default_enabled, + production_type, pop_build_factory, strategic_factory, advanced_factory, fort_level, naval_capacity, colonial_points, in_province, one_per_state, colonial_range, infrastructure, movement_cost, local_ship_build, spawn_railway_track, sail, steam, capital, port, std::move(modifiers) }); } bool BuildingManager::load_buildings_file(GoodManager const& good_manager, ProductionTypeManager const& production_type_manager, ModifierManager const& modifier_manager, ast::NodeCPtr root) { - bool ret = expect_dictionary([this, &good_manager, &production_type_manager, &modifier_manager](std::string_view key, ast::NodeCPtr value) -> bool { - std::string_view type, on_completion = "", production_type; + bool ret = expect_dictionary_reserve_length(buildings, [this](std::string_view, ast::NodeCPtr value) -> bool { + return expect_key("type", expect_identifier( + [this](std::string_view identifier) -> bool { + if (!building_types.has_identifier(identifier)) { + return building_types.add_item({ identifier }); + } + return true; + } + ))(value); + })(root); + lock_building_types(); + + ret &= expect_dictionary([this, &good_manager, &production_type_manager, &modifier_manager](std::string_view key, ast::NodeCPtr value) -> bool { + BuildingType const* type = nullptr; + ProductionType const* production_type = nullptr; + std::string_view on_completion; fixed_point_t completion_size = 0, cost = 0, infrastructure = 0, movement_cost = 0, colonial_range = 0, local_ship_build = 0; - Building::level_t max_level, fort_level = 0; - std::map goods_cost; - uint32_t build_days; - bool visibility, on_map, default_enabled, pop_build_factory, strategic_factory, advanced_factory, in_province, one_per_state, spawn_railway_track, sail, steam, capital, port; - default_enabled = pop_build_factory = strategic_factory = advanced_factory = in_province = one_per_state = spawn_railway_track = sail = steam = capital = port = false; + Building::level_t max_level = 0, fort_level = 0; + std::map goods_cost; + Timespan build_time; + bool visibility = false, on_map = false, default_enabled = false, pop_build_factory = false, strategic_factory = false, advanced_factory = false; + bool in_province = false, one_per_state = false, spawn_railway_track = false, sail = false, steam = false, capital = false, port = false; uint64_t naval_capacity = 0; - std::vector colonial_points; + std::vector colonial_points; ModifierValue modifiers; - + bool ret = modifier_manager.expect_modifier_value_and_keys(move_variable_callback(modifiers), - "type", ONE_EXACTLY, expect_identifier(assign_variable_callback(type)), + "type", ONE_EXACTLY, expect_building_type_identifier(assign_variable_callback_pointer(type)), "on_completion", ZERO_OR_ONE, expect_identifier(assign_variable_callback(on_completion)), "completion_size", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(completion_size)), - "max_level", ONE_EXACTLY, expect_uint(assign_variable_callback(max_level)), - "goods_cost", ONE_EXACTLY, good_manager.expect_good_decimal_map(assign_variable_callback(goods_cost)), + "max_level", ONE_EXACTLY, expect_uint(assign_variable_callback_uint(max_level)), + "goods_cost", ONE_EXACTLY, good_manager.expect_good_decimal_map(move_variable_callback(goods_cost)), "cost", ZERO_OR_MORE, expect_fixed_point(assign_variable_callback(cost)), - "time", ONE_EXACTLY, expect_uint(assign_variable_callback_uint("building build time", build_days)), + "time", ONE_EXACTLY, expect_timespan(assign_variable_callback(build_time)), "visibility", ONE_EXACTLY, expect_bool(assign_variable_callback(visibility)), "onmap", ONE_EXACTLY, expect_bool(assign_variable_callback(on_map)), "default_enabled", ZERO_OR_ONE, expect_bool(assign_variable_callback(default_enabled)), - "production_type", ZERO_OR_ONE, expect_identifier(assign_variable_callback(production_type)), + "production_type", ZERO_OR_ONE, production_type_manager.expect_production_type_identifier(assign_variable_callback_pointer(production_type)), "pop_build_factory", ZERO_OR_ONE, expect_bool(assign_variable_callback(pop_build_factory)), "strategic_factory", ZERO_OR_ONE, expect_bool(assign_variable_callback(strategic_factory)), "advanced_factory", ZERO_OR_ONE, expect_bool(assign_variable_callback(advanced_factory)), - "fort_level", ZERO_OR_ONE, expect_uint(assign_variable_callback_uint("building fort level", fort_level)), - "naval_capacity", ZERO_OR_ONE, expect_uint(assign_variable_callback_uint("building naval capacity", naval_capacity)), - "colonial_points", ZERO_OR_ONE, expect_list(expect_uint([&colonial_points](uint64_t points) -> bool { - return colonial_points.emplace_back(points); - })), "province", ZERO_OR_ONE, expect_bool(assign_variable_callback(in_province)), + "fort_level", ZERO_OR_ONE, expect_uint(assign_variable_callback_uint(fort_level)), + "naval_capacity", ZERO_OR_ONE, expect_uint(assign_variable_callback_uint(naval_capacity)), + "colonial_points", ZERO_OR_ONE, expect_list(expect_fixed_point([&colonial_points](fixed_point_t points) -> bool { + colonial_points.push_back(points); + return true; + })), + "province", ZERO_OR_ONE, expect_bool(assign_variable_callback(in_province)), "one_per_state", ZERO_OR_ONE, expect_bool(assign_variable_callback(one_per_state)), "colonial_range", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(colonial_range)), "infrastructure", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(infrastructure)), @@ -242,25 +262,14 @@ bool BuildingManager::load_buildings_file(GoodManager const& good_manager, Produ "port", ZERO_OR_ONE, expect_bool(assign_variable_callback(port)) )(value); - BuildingType const* type_ref = building_types.get_item_by_identifier(type); - if (type_ref == nullptr) { - building_types.add_item({ type }); - type_ref = building_types.get_item_by_identifier(type); - } - - Timespan build_time = Timespan(build_days); - - ProductionType const* production_type_ref = production_type_manager.get_production_type_by_identifier(production_type); - ret &= add_building( - key, *type_ref, on_completion, completion_size, max_level, goods_cost, cost, build_time, visibility, on_map, default_enabled, - production_type_ref, pop_build_factory, strategic_factory, advanced_factory, fort_level, naval_capacity, colonial_points, in_province, + key, type, on_completion, completion_size, max_level, goods_cost, cost, build_time, visibility, on_map, default_enabled, + production_type, pop_build_factory, strategic_factory, advanced_factory, fort_level, naval_capacity, colonial_points, in_province, one_per_state, colonial_range, infrastructure, movement_cost, local_ship_build, spawn_railway_track, sail, steam, capital, port, std::move(modifiers) ); return ret; })(root); - lock_building_types(); lock_buildings(); return ret; -- cgit v1.2.3-56-ga3b1