From 84b5ee7a7749e2dbfeb214b4cedd16d5522f4197 Mon Sep 17 00:00:00 2001 From: Hop311 Date: Thu, 28 Sep 2023 21:03:14 +0100 Subject: Removed unnecessary `const std::string_view`s --- src/openvic-simulation/types/IdentifierRegistry.hpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/openvic-simulation/types/IdentifierRegistry.hpp') diff --git a/src/openvic-simulation/types/IdentifierRegistry.hpp b/src/openvic-simulation/types/IdentifierRegistry.hpp index 41e4c6b..482e917 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.hpp +++ b/src/openvic-simulation/types/IdentifierRegistry.hpp @@ -16,7 +16,7 @@ namespace OpenVic { const std::string identifier; protected: - HasIdentifier(const std::string_view new_identifier); + HasIdentifier(std::string_view new_identifier); public: HasIdentifier(HasIdentifier const&) = delete; @@ -55,7 +55,7 @@ namespace OpenVic { */ class HasIdentifierAndColour : public HasIdentifier, public HasColour { protected: - HasIdentifierAndColour(const std::string_view new_identifier, const colour_t new_colour, bool can_be_null, bool can_have_alpha); + HasIdentifierAndColour(std::string_view new_identifier, const colour_t new_colour, bool can_be_null, bool can_have_alpha); public: HasIdentifierAndColour(HasIdentifierAndColour const&) = delete; @@ -86,7 +86,7 @@ namespace OpenVic { identifier_index_map_t identifier_index_map; public: - IdentifierRegistry(const std::string_view new_name, bool new_log_lock = true) + IdentifierRegistry(std::string_view new_name, bool new_log_lock = true) : name { new_name }, log_lock { new_log_lock } {} std::string const& get_name() const { @@ -143,13 +143,13 @@ namespace OpenVic { } } - T* get_item_by_identifier(const std::string_view identifier) { + T* get_item_by_identifier(std::string_view identifier) { const identifier_index_map_t::const_iterator it = identifier_index_map.find(identifier); if (it != identifier_index_map.end()) return &items[it->second]; return nullptr; } - T const* get_item_by_identifier(const std::string_view identifier) const { + T const* get_item_by_identifier(std::string_view identifier) const { const identifier_index_map_t::const_iterator it = identifier_index_map.find(identifier); if (it != identifier_index_map.end()) return &items[it->second]; return nullptr; @@ -242,7 +242,7 @@ namespace OpenVic { #define IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(type, singular, plural) \ void lock_##plural() { plural.lock(); } \ bool plural##_are_locked() const { return plural.is_locked(); } \ - type const* get_##singular##_by_identifier(const std::string_view identifier) const { \ + type const* get_##singular##_by_identifier(std::string_view identifier) const { \ return plural.get_item_by_identifier(identifier); } \ size_t get_##singular##_count() const { \ return plural.size(); } \ @@ -258,7 +258,7 @@ namespace OpenVic { return plural.expect_item_decimal_map(callback); } #define IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_CUSTOM_PLURAL(type, singular, plural) \ - type* get_##singular##_by_identifier(const std::string_view identifier) { \ + type* get_##singular##_by_identifier(std::string_view identifier) { \ return plural.get_item_by_identifier(identifier); } \ NodeTools::node_callback_t expect_##singular##_identifier(NodeTools::callback_t callback) { \ return plural.expect_item_identifier(callback); } \ -- cgit v1.2.3-56-ga3b1 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/GameManager.cpp | 2 +- src/openvic-simulation/GameManager.hpp | 5 +- src/openvic-simulation/dataloader/Dataloader.cpp | 7 ++ src/openvic-simulation/dataloader/Dataloader.hpp | 7 -- src/openvic-simulation/dataloader/NodeTools.cpp | 27 ++++++- src/openvic-simulation/dataloader/NodeTools.hpp | 17 +++-- src/openvic-simulation/economy/ProductionType.cpp | 49 ++++++------ src/openvic-simulation/economy/ProductionType.hpp | 12 +-- src/openvic-simulation/map/Building.cpp | 89 ++++++++++++---------- src/openvic-simulation/map/Building.hpp | 32 ++++---- src/openvic-simulation/map/TerrainType.cpp | 4 +- src/openvic-simulation/pop/Pop.cpp | 8 +- src/openvic-simulation/pop/Religion.cpp | 2 +- .../types/IdentifierRegistry.hpp | 10 +++ src/openvic-simulation/units/Unit.cpp | 89 ++++++++++++---------- src/openvic-simulation/units/Unit.hpp | 46 +++++------ 16 files changed, 227 insertions(+), 179 deletions(-) (limited to 'src/openvic-simulation/types/IdentifierRegistry.hpp') diff --git a/src/openvic-simulation/GameManager.cpp b/src/openvic-simulation/GameManager.cpp index b0bb0a3..6eeecac 100644 --- a/src/openvic-simulation/GameManager.cpp +++ b/src/openvic-simulation/GameManager.cpp @@ -74,7 +74,7 @@ ModifierManager& GameManager::get_modifier_manager() { return modifier_manager; } -ModifierManager const& GameManager::get_modifier_manager() const{ +ModifierManager const& GameManager::get_modifier_manager() const { return modifier_manager; } diff --git a/src/openvic-simulation/GameManager.hpp b/src/openvic-simulation/GameManager.hpp index 2494dd8..99e2fd1 100644 --- a/src/openvic-simulation/GameManager.hpp +++ b/src/openvic-simulation/GameManager.hpp @@ -2,14 +2,11 @@ #include "openvic-simulation/GameAdvancementHook.hpp" #include "openvic-simulation/economy/Good.hpp" +#include "openvic-simulation/economy/ProductionType.hpp" #include "openvic-simulation/map/Map.hpp" #include "openvic-simulation/politics/Ideology.hpp" #include "openvic-simulation/politics/Issue.hpp" -#include "openvic-simulation/GameAdvancementHook.hpp" -#include "openvic-simulation/economy/Good.hpp" -#include "openvic-simulation/map/Map.hpp" #include "openvic-simulation/units/Unit.hpp" -#include "openvic-simulation/economy/ProductionType.hpp" namespace OpenVic { struct GameManager { diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index c21c6da..4a98bca 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -1,5 +1,12 @@ #include "Dataloader.hpp" +#include +#include +#include + +#include "openvic-simulation/GameManager.hpp" +#include "openvic-simulation/utility/Logger.hpp" + using namespace OpenVic; using namespace OpenVic::NodeTools; using namespace ovdl; diff --git a/src/openvic-simulation/dataloader/Dataloader.hpp b/src/openvic-simulation/dataloader/Dataloader.hpp index e1a2614..f833a2c 100644 --- a/src/openvic-simulation/dataloader/Dataloader.hpp +++ b/src/openvic-simulation/dataloader/Dataloader.hpp @@ -1,15 +1,8 @@ #pragma once #include -#include -#include #include "openvic-simulation/dataloader/NodeTools.hpp" -#include -#include -#include -#include "openvic-simulation/GameManager.hpp" -#include "openvic-simulation/utility/Logger.hpp" namespace OpenVic { namespace fs = std::filesystem; diff --git a/src/openvic-simulation/dataloader/NodeTools.cpp b/src/openvic-simulation/dataloader/NodeTools.cpp index 4deffe1..5ef09d1 100644 --- a/src/openvic-simulation/dataloader/NodeTools.cpp +++ b/src/openvic-simulation/dataloader/NodeTools.cpp @@ -134,6 +134,10 @@ node_callback_t NodeTools::expect_colour(callback_t callback) { }; } +node_callback_t NodeTools::expect_timespan(callback_t callback) { + return expect_int(callback); +} + node_callback_t NodeTools::expect_date(callback_t callback) { return expect_identifier( [callback](std::string_view identifier) -> bool { @@ -234,6 +238,22 @@ node_callback_t NodeTools::expect_length(callback_t callback) { }; } +node_callback_t NodeTools::expect_key(std::string_view key, node_callback_t callback) { + return _expect_type( + [key, callback](ast::AbstractListNode const& list_node) -> bool { + std::vector const& list = list_node._statements; + for (ast::NodeUPtr const& sub_node : list_node._statements) { + ast::AssignNode const* assign_node = sub_node->cast_to(); + if (assign_node != nullptr && assign_node->_name == key) { + return callback(&*assign_node->_initializer); + } + } + Logger::error("Failed to find expected key: ", key); + return false; + } + ); +} + node_callback_t NodeTools::expect_dictionary_and_length(length_callback_t length_callback, key_value_callback_t callback) { return expect_list_and_length(length_callback, expect_assign(callback)); } @@ -267,14 +287,15 @@ key_value_callback_t NodeTools::dictionary_keys_callback(key_map_t& key_map, boo }; } -bool NodeTools::check_key_map_counts(key_map_t const& key_map) { +bool NodeTools::check_key_map_counts(key_map_t& key_map) { bool ret = true; - for (key_map_t::value_type const& key_entry : key_map) { - dictionary_entry_t const& entry = key_entry.second; + for (key_map_t::value_type& key_entry : key_map) { + dictionary_entry_t& entry = key_entry.second; if (entry.must_appear() && entry.count < 1) { Logger::error("Mandatory dictionary key not present: ", key_entry.first); ret = false; } + entry.count = 0; } return ret; } diff --git a/src/openvic-simulation/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp index 20c8dbd..5ba9d63 100644 --- a/src/openvic-simulation/dataloader/NodeTools.hpp +++ b/src/openvic-simulation/dataloader/NodeTools.hpp @@ -30,6 +30,7 @@ namespace OpenVic { node_callback_t expect_uint(callback_t callback); node_callback_t expect_fixed_point(callback_t callback); node_callback_t expect_colour(callback_t callback); + node_callback_t expect_timespan(callback_t callback); node_callback_t expect_date(callback_t callback); node_callback_t expect_ivec2(callback_t callback); node_callback_t expect_fvec2(callback_t callback); @@ -43,6 +44,8 @@ namespace OpenVic { node_callback_t expect_list(node_callback_t callback); node_callback_t expect_length(callback_t callback); + node_callback_t expect_key(std::string_view key, node_callback_t callback); + node_callback_t expect_dictionary_and_length(length_callback_t length_callback, key_value_callback_t callback); node_callback_t expect_dictionary(key_value_callback_t callback); @@ -74,7 +77,7 @@ namespace OpenVic { void add_key_map_entry(key_map_t& key_map, std::string_view key, dictionary_entry_t::expected_count_t expected_count, node_callback_t callback); key_value_callback_t dictionary_keys_callback(key_map_t& key_map, bool allow_other_keys); - bool check_key_map_counts(key_map_t const& key_map); + bool check_key_map_counts(key_map_t& key_map); constexpr struct allow_other_keys_t {} ALLOW_OTHER_KEYS; @@ -169,13 +172,13 @@ namespace OpenVic { template requires(std::integral) - callback_t assign_variable_callback_uint(std::string_view name, T& var) { - return [&var, name](uint64_t val) -> bool { + callback_t assign_variable_callback_uint(T& var) { + return [&var](uint64_t val) -> bool { if (val <= static_cast(std::numeric_limits::max())) { var = val; return true; } - Logger::error("Invalid ", name, ": ", val, " (valid range: [0, ", + Logger::error("Invalid uint: ", val, " (valid range: [0, ", static_cast(std::numeric_limits::max()), "])"); return false; }; @@ -183,13 +186,13 @@ namespace OpenVic { template requires(std::signed_integral) - callback_t assign_variable_callback_int(std::string_view name, T& var) { - return [&var, name](int64_t val) -> bool { + callback_t assign_variable_callback_int(T& var) { + return [&var](int64_t val) -> bool { if (static_cast(std::numeric_limits::lowest()) <= val && val <= static_cast(std::numeric_limits::max())) { var = val; return true; } - Logger::error("Invalid ", name, ": ", val, " (valid range: [", + Logger::error("Invalid int: ", val, " (valid range: [", static_cast(std::numeric_limits::lowest()), ", ", static_cast(std::numeric_limits::max()), "])"); return false; diff --git a/src/openvic-simulation/economy/ProductionType.cpp b/src/openvic-simulation/economy/ProductionType.cpp index b6f3ffa..2c7c431 100644 --- a/src/openvic-simulation/economy/ProductionType.cpp +++ b/src/openvic-simulation/economy/ProductionType.cpp @@ -28,7 +28,7 @@ fixed_point_t EmployedPop::get_amount() { return amount; } -ProductionType::ProductionType(PRODUCTION_TYPE_ARGS(type_t, Good const*)) : HasIdentifier { identifier }, owner { owner }, +ProductionType::ProductionType(PRODUCTION_TYPE_ARGS) : HasIdentifier { identifier }, owner { owner }, employees { employees }, type { type }, workforce { workforce }, input_goods { input_goods }, output_goods { output_goods }, value { value }, bonuses { bonuses }, efficiency { efficiency }, coastal { coastal }, farm { farm }, mine { mine } {} @@ -141,46 +141,36 @@ node_callback_t ProductionTypeManager::_expect_employed_pop_list(GoodManager& go return false; \ } -bool ProductionTypeManager::add_production_type(PRODUCTION_TYPE_ARGS(std::string_view, std::string_view), GoodManager& good_manager) { +bool ProductionTypeManager::add_production_type(PRODUCTION_TYPE_ARGS, GoodManager& good_manager) { if (identifier.empty()) { Logger::error("Invalid production type identifier - empty!"); return false; } - 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("Bad type ", type, " for production type ", identifier, "!"); - return false; - } - - if (workforce == 0) { + if (workforce <= 0) { Logger::error("Workforce for production type ", identifier, " was 0 or unset!"); return false; } - if (value == 0) { + if (value <= 0) { Logger::error("Value for production type ", identifier, " was 0 or unset!"); return false; } POPTYPE_CHECK(owner) - for (int i = 0; i < employees.size(); i++) { - POPTYPE_CHECK(employees[i]) + for (EmployedPop const& ep : employees) { + POPTYPE_CHECK(ep) } - Good const* output = good_manager.get_good_by_identifier(output_goods); - if (output == nullptr) { - Logger::error("Invalid output ", output_goods, " for production type ", identifier, "!"); + if (output_goods == nullptr) { + Logger::error("Output good for production type ", identifier, " was null!"); return false; } return production_types.add_item({ - identifier, owner, employees, type_enum, workforce, input_goods, - output, value, bonuses, efficiency, coastal, farm, mine + identifier, owner, employees, type, workforce, input_goods, + output_goods, value, bonuses, efficiency, coastal, farm, mine }); } @@ -188,9 +178,9 @@ bool ProductionTypeManager::add_production_type(PRODUCTION_TYPE_ARGS(std::string "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", workforce)), \ + "workforce", ZERO_OR_ONE, expect_uint(assign_variable_callback_uint(workforce)), \ "input_goods", ZERO_OR_ONE, good_manager.expect_good_decimal_map(move_variable_callback(input_goods)), \ - "output_goods", ZERO_OR_ONE, expect_identifier(assign_variable_callback(output_goods)), \ + "output_goods", ZERO_OR_ONE, 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)), \ @@ -243,7 +233,8 @@ bool ProductionTypeManager::load_production_types_file(GoodManager& good_manager EmployedPop owner; std::vector employees; - std::string_view type, output_goods; + std::string_view type; + Good const* output_goods = nullptr; Pop::pop_size_t workforce = 0; // 0 is a meaningless value -> unset std::map input_goods, efficiency; fixed_point_t value = 0; // 0 is a meaningless value -> unset @@ -262,8 +253,18 @@ 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, workforce, input_goods, output_goods, value, + key, owner, employees, type_enum, 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 57d04ea..755eda8 100644 --- a/src/openvic-simulation/economy/ProductionType.hpp +++ b/src/openvic-simulation/economy/ProductionType.hpp @@ -5,9 +5,9 @@ #include "openvic-simulation/types/IdentifierRegistry.hpp" #include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#define PRODUCTION_TYPE_ARGS(enum_type, output) \ - std::string_view identifier, EmployedPop owner, std::vector employees, enum_type type, \ - Pop::pop_size_t workforce, std::map input_goods, output output_goods, \ +#define PRODUCTION_TYPE_ARGS \ + std::string_view identifier, EmployedPop owner, std::vector employees, ProductionType::type_t type, \ + Pop::pop_size_t workforce, std::map input_goods, Good const* output_goods, \ fixed_point_t value, std::vector bonuses, std::map efficiency, \ bool coastal, bool farm, bool mine @@ -69,7 +69,7 @@ namespace OpenVic { const bool farm; const bool mine; - ProductionType(PRODUCTION_TYPE_ARGS(type_t, Good const*)); + ProductionType(PRODUCTION_TYPE_ARGS); public: ProductionType(ProductionType&&) = default; @@ -80,7 +80,7 @@ namespace OpenVic { Pop::pop_size_t get_workforce() const; std::map const& get_input_goods(); - const Good* get_output_goods() const; + Good const* get_output_goods() const; fixed_point_t get_value() const; std::vector const& get_bonuses(); @@ -103,7 +103,7 @@ namespace OpenVic { public: ProductionTypeManager(); - bool add_production_type(PRODUCTION_TYPE_ARGS(std::string_view, std::string_view), GoodManager& good_manager); + bool add_production_type(PRODUCTION_TYPE_ARGS, GoodManager& good_manager); IDENTIFIER_REGISTRY_ACCESSORS(ProductionType, production_type) bool load_production_types_file(GoodManager& good_manager, PopManager& pop_manager, ast::NodeCPtr root); 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; diff --git a/src/openvic-simulation/map/Building.hpp b/src/openvic-simulation/map/Building.hpp index 1c2ded0..9b445bf 100644 --- a/src/openvic-simulation/map/Building.hpp +++ b/src/openvic-simulation/map/Building.hpp @@ -7,10 +7,10 @@ #include "openvic-simulation/economy/ProductionType.hpp" #include "openvic-simulation/Modifier.hpp" -#define ARGS BuildingType const& type, std::string_view on_completion, fixed_point_t completion_size, level_t max_level, \ - std::map goods_cost, fixed_point_t cost, Timespan build_time, bool visibility, bool on_map, bool default_enabled, \ +#define ARGS std::string_view on_completion, fixed_point_t completion_size, level_t max_level, \ + std::map goods_cost, fixed_point_t cost, Timespan build_time, bool visibility, bool on_map, bool default_enabled, \ ProductionType const* production_type, bool pop_build_factory, bool strategic_factory, bool advanced_factory, level_t fort_level, \ - uint64_t naval_capacity, std::vector colonial_points, bool in_province, bool one_per_state, fixed_point_t colonial_range, \ + uint64_t naval_capacity, std::vector colonial_points, bool in_province, bool one_per_state, fixed_point_t colonial_range, \ fixed_point_t infrastructure, fixed_point_t movement_cost, fixed_point_t local_ship_build, bool spawn_railway_track, bool sail, bool steam, \ bool capital, bool port, ModifierValue&& modifiers @@ -27,19 +27,19 @@ namespace OpenVic { struct Building : HasIdentifier, ModifierValue { friend struct BuildingManager; - using level_t = uint8_t; + using level_t = int8_t; private: BuildingType const& type; - const std::string_view on_completion; //probably sound played on completion + const std::string on_completion; //probably sound played on completion const fixed_point_t completion_size; const level_t max_level; - const std::map goods_cost; + const std::map goods_cost; const fixed_point_t cost; const Timespan build_time; //time const bool visibility; const bool on_map; //onmap - + const bool default_enabled; ProductionType const* production_type; const bool pop_build_factory; @@ -49,7 +49,7 @@ namespace OpenVic { const level_t fort_level; //probably the step-per-level const uint64_t naval_capacity; - const std::vector colonial_points; + const std::vector colonial_points; const bool in_province; //province const bool one_per_state; const fixed_point_t colonial_range; @@ -64,16 +64,16 @@ namespace OpenVic { const bool capital; //only in naval base const bool port; //only in naval base - Building(std::string_view identifier, ARGS); + Building(std::string_view identifier, BuildingType const& type, ARGS); public: Building(Building&&) = default; BuildingType const& get_type() const; - std::string_view get_on_completion() const; + std::string const& get_on_completion() const; fixed_point_t get_completion_size() const; level_t get_max_level() const; - std::map const& get_goods_cost() const; + std::map const& get_goods_cost() const; fixed_point_t get_cost() const; Timespan get_build_time() const; bool has_visibility() const; @@ -84,15 +84,15 @@ namespace OpenVic { bool is_pop_built_factory() const; bool is_strategic_factory() const; bool is_advanced_factory() const; - + level_t get_fort_level() const; uint64_t get_naval_capacity() const; - std::vector const& get_colonial_points() const; + std::vector const& get_colonial_points() const; bool is_in_province() const; bool is_one_per_state() const; fixed_point_t get_colonial_range() const; - + fixed_point_t get_infrastructure() const; fixed_point_t get_movement_cost() const; fixed_point_t get_local_ship_build() const; @@ -119,7 +119,7 @@ namespace OpenVic { struct BuildingInstance : HasIdentifier { //used in the actual game friend struct BuildingManager; using level_t = Building::level_t; - + private: Building const& building; @@ -164,7 +164,7 @@ namespace OpenVic { bool add_building_type(std::string_view identifier); IDENTIFIER_REGISTRY_ACCESSORS(BuildingType, building_type) - bool add_building(std::string_view identifier, ARGS); + bool add_building(std::string_view identifier, BuildingType const* type, ARGS); IDENTIFIER_REGISTRY_ACCESSORS(Building, building) bool load_buildings_file(GoodManager const& good_manager, ProductionTypeManager const& production_type_manager, ModifierManager const& modifier_manager, ast::NodeCPtr root); diff --git a/src/openvic-simulation/map/TerrainType.cpp b/src/openvic-simulation/map/TerrainType.cpp index 24cfb7d..2438df6 100644 --- a/src/openvic-simulation/map/TerrainType.cpp +++ b/src/openvic-simulation/map/TerrainType.cpp @@ -117,7 +117,7 @@ bool TerrainTypeManager::_load_terrain_type_mapping(std::string_view mapping_key return false; } )), - "priority", ZERO_OR_ONE, expect_uint(assign_variable_callback_uint("terrain type mapping priority", priority)), + "priority", ZERO_OR_ONE, expect_uint(assign_variable_callback_uint(priority)), "has_texture", ZERO_OR_ONE, expect_bool(assign_variable_callback(has_texture)) )(mapping_value); if (has_texture) { @@ -153,7 +153,7 @@ bool TerrainTypeManager::load_terrain_types(ModifierManager const& modifier_mana if (key == "terrain") { if (!terrain) { terrain = true; - return expect_uint(assign_variable_callback_uint("terrain texture limit", terrain_texture_limit))(value); + return expect_uint(assign_variable_callback_uint(terrain_texture_limit))(value); } else { Logger::error("Duplicate terrain key!"); return false; diff --git a/src/openvic-simulation/pop/Pop.cpp b/src/openvic-simulation/pop/Pop.cpp index 17bacbf..38cd883 100644 --- a/src/openvic-simulation/pop/Pop.cpp +++ b/src/openvic-simulation/pop/Pop.cpp @@ -152,11 +152,11 @@ bool PopManager::load_pop_type_file(std::string_view filestem, ast::NodeCPtr roo bool state_capital_only = false, is_artisan = false, is_slave = false, demote_migrant = false; Pop::pop_size_t max_size = 0, merge_max_size = 0; bool ret = expect_dictionary_keys( - "sprite", ONE_EXACTLY, expect_uint(assign_variable_callback_uint("poptype sprite", sprite)), + "sprite", ONE_EXACTLY, expect_uint(assign_variable_callback_uint(sprite)), "color", ONE_EXACTLY, expect_colour(assign_variable_callback(colour)), "is_artisan", ZERO_OR_ONE, expect_bool(assign_variable_callback(is_artisan)), - "max_size", ZERO_OR_ONE, expect_uint(assign_variable_callback_uint("poptype max_size", max_size)), - "merge_max_size", ZERO_OR_ONE, expect_uint(assign_variable_callback_uint("poptype merge_max_size", merge_max_size)), + "max_size", ZERO_OR_ONE, expect_uint(assign_variable_callback_uint(max_size)), + "merge_max_size", ZERO_OR_ONE, expect_uint(assign_variable_callback_uint(merge_max_size)), "strata", ONE_EXACTLY, expect_identifier( [&strata](std::string_view identifier) -> bool { using strata_map_t = std::map>; @@ -218,7 +218,7 @@ bool PopManager::load_pop_into_province(Province& province, std::string_view pop bool ret = expect_dictionary_keys( "culture", ONE_EXACTLY, culture_manager.expect_culture_identifier(assign_variable_callback_pointer(culture)), "religion", ONE_EXACTLY, religion_manager.expect_religion_identifier(assign_variable_callback_pointer(religion)), - "size", ONE_EXACTLY, expect_uint(assign_variable_callback_uint("pop size", size)), + "size", ONE_EXACTLY, expect_uint(assign_variable_callback_uint(size)), "militancy", ZERO_OR_ONE, success_callback, "rebel_type", ZERO_OR_ONE, success_callback )(pop_node); diff --git a/src/openvic-simulation/pop/Religion.cpp b/src/openvic-simulation/pop/Religion.cpp index c679e00..32a3219 100644 --- a/src/openvic-simulation/pop/Religion.cpp +++ b/src/openvic-simulation/pop/Religion.cpp @@ -91,7 +91,7 @@ bool ReligionManager::load_religion_file(ast::NodeCPtr root) { bool pagan = false; bool ret = expect_dictionary_keys( - "icon", ONE_EXACTLY, expect_uint(assign_variable_callback_uint("religion icon", icon)), + "icon", ONE_EXACTLY, expect_uint(assign_variable_callback_uint(icon)), "color", ONE_EXACTLY, expect_colour(assign_variable_callback(colour)), "pagan", ZERO_OR_ONE, expect_bool(assign_variable_callback(pagan)) )(value); diff --git a/src/openvic-simulation/types/IdentifierRegistry.hpp b/src/openvic-simulation/types/IdentifierRegistry.hpp index 482e917..7fe2656 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.hpp +++ b/src/openvic-simulation/types/IdentifierRegistry.hpp @@ -155,6 +155,10 @@ namespace OpenVic { return nullptr; } + bool has_identifier(std::string_view identifier) const { + return get_item_by_identifier(identifier) != nullptr; + } + T* get_item_by_index(size_t index) { return index < items.size() ? &items[index] : nullptr; } @@ -163,6 +167,10 @@ namespace OpenVic { return index < items.size() ? &items[index] : nullptr; } + bool has_index(size_t index) const { + return get_item_by_index(index) != nullptr; + } + std::vector& get_items() { return items; } @@ -244,6 +252,8 @@ namespace OpenVic { bool plural##_are_locked() const { return plural.is_locked(); } \ type const* get_##singular##_by_identifier(std::string_view identifier) const { \ return plural.get_item_by_identifier(identifier); } \ + bool has_##singular##_identifier(std::string_view identifier) const { \ + return plural.has_identifier(identifier); } \ size_t get_##singular##_count() const { \ return plural.size(); } \ std::vector const& get_##plural() const { \ diff --git a/src/openvic-simulation/units/Unit.cpp b/src/openvic-simulation/units/Unit.cpp index 16719e6..8d519f1 100644 --- a/src/openvic-simulation/units/Unit.cpp +++ b/src/openvic-simulation/units/Unit.cpp @@ -1,6 +1,6 @@ #include "Unit.hpp" -#define UNIT_ARGS icon, sprite, active, type, floating_flag, priority, max_strength, \ +#define UNIT_ARGS icon, sprite, active, unit_type, floating_flag, priority, max_strength, \ default_organisation, maximum_speed, weighted_value, build_time, build_cost, supply_consumption, \ supply_cost #define LAND_ARGS reconnaissance, attack, defence, discipline, support, maneuver, siege @@ -10,8 +10,8 @@ using namespace OpenVic; using namespace OpenVic::NodeTools; -Unit::Unit(std::string_view identifier, std::string_view category, UNIT_PARAMS) : HasIdentifier { identifier }, - icon { icon }, category { category }, sprite { sprite }, active { active }, type { type }, +Unit::Unit(std::string_view identifier, type_t type, UNIT_PARAMS) : HasIdentifier { identifier }, + icon { icon }, type { type }, sprite { sprite }, active { active }, unit_type { unit_type }, floating_flag { floating_flag }, priority { priority }, max_strength { max_strength }, default_organisation { default_organisation }, maximum_speed { maximum_speed }, weighted_value { weighted_value }, build_time { build_time }, build_cost { build_cost }, supply_consumption { supply_consumption }, supply_cost { supply_cost } {} @@ -20,7 +20,11 @@ Unit::icon_t Unit::get_icon() const { return icon; } -Unit::sprite_t Unit::get_sprite() const { +Unit::type_t Unit::get_type() const { + return type; +} + +std::string const& Unit::get_sprite() const { return sprite; } @@ -28,8 +32,8 @@ bool Unit::is_active() const { return active; } -std::string_view Unit::get_type() const { - return type; +std::string const& Unit::get_unit_type() const { + return unit_type; } bool Unit::has_floating_flag() const { @@ -60,7 +64,7 @@ fixed_point_t Unit::get_weighted_value() const { return weighted_value; } -std::map const& Unit::get_build_cost() const { +std::map const& Unit::get_build_cost() const { return build_cost; } @@ -68,11 +72,11 @@ fixed_point_t Unit::get_supply_consumption() const { return supply_consumption; } -std::map const& Unit::get_supply_cost() const { +std::map const& Unit::get_supply_cost() const { return supply_cost; } -LandUnit::LandUnit(std::string_view identifier, UNIT_PARAMS, LAND_PARAMS) : Unit { identifier, "land", UNIT_ARGS }, +LandUnit::LandUnit(std::string_view identifier, UNIT_PARAMS, LAND_PARAMS) : Unit { identifier, type_t::LAND, UNIT_ARGS }, reconnaissance { reconnaissance }, attack { attack }, defence { defence }, discipline { discipline }, support { support }, maneuver { maneuver }, siege { siege } {} @@ -104,7 +108,7 @@ fixed_point_t LandUnit::get_siege() const { return siege; } -NavalUnit::NavalUnit(std::string_view identifier, UNIT_PARAMS, NAVY_PARAMS) : Unit { identifier, "naval", UNIT_ARGS }, +NavalUnit::NavalUnit(std::string_view identifier, UNIT_PARAMS, NAVY_PARAMS) : Unit { identifier, type_t::NAVAL, UNIT_ARGS }, naval_icon { naval_icon }, sail { sail }, transport { transport }, capital { capital }, move_sound { move_sound }, select_sound { select_sound }, colonial_points { colonial_points }, build_overseas { build_overseas }, min_port_level { min_port_level }, limit_per_port { limit_per_port }, supply_consumption_score { supply_consumption_score }, @@ -122,11 +126,11 @@ bool NavalUnit::is_transport() const { return transport; } -NavalUnit::sound_t NavalUnit::get_move_sound() const { +std::string const& NavalUnit::get_move_sound() const { return move_sound; } -NavalUnit::sound_t NavalUnit::get_select_sound() const { +std::string const& NavalUnit::get_select_sound() const { return select_sound; } @@ -183,7 +187,7 @@ bool UnitManager::_check_shared_parameters(std::string_view identifier, UNIT_PAR return false; } - if (type.empty()) { + if (unit_type.empty()) { Logger::error("Invalid unit type - empty!"); return false; } @@ -212,38 +216,36 @@ bool UnitManager::add_naval_unit(std::string_view identifier, UNIT_PARAMS, NAVY_ } bool UnitManager::load_unit_file(GoodManager const& good_manager, ast::NodeCPtr root) { - return NodeTools::expect_dictionary([this, &good_manager](std::string_view key, ast::NodeCPtr value) -> bool { - Unit::icon_t icon; - std::string_view category, type; - Unit::sprite_t sprite; - bool active = true, floating_flag; - uint32_t priority, build_time_days; - fixed_point_t maximum_speed, max_strength, default_organisation, weighted_value, supply_consumption; - std::map build_cost, supply_cost; + return expect_dictionary([this, &good_manager](std::string_view key, ast::NodeCPtr value) -> bool { + Unit::icon_t icon = 0; + std::string_view type, unit_type, sprite; + bool active = true, floating_flag = false; + uint32_t priority = 0; + Timespan build_time; + fixed_point_t maximum_speed = 0, max_strength = 0, default_organisation = 0, weighted_value = 0, supply_consumption = 0; + std::map build_cost, supply_cost; //shared bool ret = expect_dictionary_keys(ALLOW_OTHER_KEYS, - "icon", ONE_EXACTLY, expect_uint(assign_variable_callback_uint("unit icon", icon)), - "type", ONE_EXACTLY, expect_identifier(assign_variable_callback(category)), + "icon", ONE_EXACTLY, expect_uint(assign_variable_callback_uint(icon)), + "type", ONE_EXACTLY, expect_identifier(assign_variable_callback(type)), "sprite", ONE_EXACTLY, expect_identifier(assign_variable_callback(sprite)), "active", ZERO_OR_ONE, expect_bool(assign_variable_callback(active)), - "unit_type", ONE_EXACTLY, expect_identifier(assign_variable_callback(type)), + "unit_type", ONE_EXACTLY, expect_identifier(assign_variable_callback(unit_type)), "floating_flag", ONE_EXACTLY, expect_bool(assign_variable_callback(floating_flag)), - "priority", ONE_EXACTLY, expect_uint(assign_variable_callback_uint("unit priority", priority)), + "priority", ONE_EXACTLY, expect_uint(assign_variable_callback_uint(priority)), "max_strength", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(max_strength)), "default_organisation", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(default_organisation)), "maximum_speed", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(maximum_speed)), "weighted_value", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(weighted_value)), - "build_time", ONE_EXACTLY, expect_uint(assign_variable_callback_uint("unit build time", build_time_days)), - "build_cost", ONE_EXACTLY, good_manager.expect_good_decimal_map(assign_variable_callback(build_cost)), - "supply_consumption", ONE_EXACTLY, expect_fixed_point(move_variable_callback(supply_consumption)), + "build_time", ONE_EXACTLY, expect_timespan(assign_variable_callback(build_time)), + "build_cost", ONE_EXACTLY, good_manager.expect_good_decimal_map(move_variable_callback(build_cost)), + "supply_consumption", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(supply_consumption)), "supply_cost", ONE_EXACTLY, good_manager.expect_good_decimal_map(move_variable_callback(supply_cost)) )(value); - Timespan build_time = { build_time_days }; - - if (category == "land") { - fixed_point_t reconnaissance, attack, defence, discipline, support, maneuver, siege; + if (type == "land") { + fixed_point_t reconnaissance = 0, attack = 0, defence = 0, discipline = 0, support = 0, maneuver = 0, siege = 0; ret &= expect_dictionary_keys(ALLOW_OTHER_KEYS, "reconnaissance", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(reconnaissance)), @@ -258,16 +260,16 @@ bool UnitManager::load_unit_file(GoodManager const& good_manager, ast::NodeCPtr ret &= add_land_unit(key, UNIT_ARGS, LAND_ARGS); return ret; - } else if (category == "naval") { - Unit::icon_t naval_icon; + } else if (type == "naval") { + Unit::icon_t naval_icon = 0; bool sail = false, transport = false, capital = false, build_overseas = false; - Unit::sound_t move_sound, select_sound; //TODO defaults for both - uint32_t min_port_level; - int32_t limit_per_port; - fixed_point_t fire_range, evasion, supply_consumption_score, hull, gun_power, colonial_points = 0, torpedo_attack = 0; + std::string_view move_sound, select_sound; //TODO defaults for both + uint32_t min_port_level = 0; + int32_t limit_per_port = 0; + fixed_point_t fire_range = 0, evasion = 0, supply_consumption_score = 0, hull = 0, gun_power = 0, colonial_points = 0, torpedo_attack = 0; ret &= expect_dictionary_keys(ALLOW_OTHER_KEYS, - "naval_icon", ONE_EXACTLY, expect_uint(assign_variable_callback_uint("unit naval icon", naval_icon)), + "naval_icon", ONE_EXACTLY, expect_uint(assign_variable_callback_uint(naval_icon)), "sail", ZERO_OR_ONE, expect_bool(assign_variable_callback(sail)), "transport", ZERO_OR_ONE, expect_bool(assign_variable_callback(transport)), "capital", ZERO_OR_ONE, expect_bool(assign_variable_callback(capital)), @@ -275,8 +277,8 @@ bool UnitManager::load_unit_file(GoodManager const& good_manager, ast::NodeCPtr "select_sound", ZERO_OR_ONE, expect_identifier(assign_variable_callback(select_sound)), "colonial_points", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(colonial_points)), "can_build_overseas", ZERO_OR_ONE, expect_bool(assign_variable_callback(build_overseas)), - "min_port_level", ONE_EXACTLY, expect_uint(assign_variable_callback_uint("unit min port level", min_port_level)), - "limit_per_port", ONE_EXACTLY, expect_int(assign_variable_callback_int("unit limit per port", limit_per_port)), + "min_port_level", ONE_EXACTLY, expect_uint(assign_variable_callback_uint(min_port_level)), + "limit_per_port", ONE_EXACTLY, expect_int(assign_variable_callback_int(limit_per_port)), "supply_consumption_score", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(supply_consumption_score)), "hull", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(hull)), "gun_power", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(gun_power)), @@ -288,6 +290,9 @@ bool UnitManager::load_unit_file(GoodManager const& good_manager, ast::NodeCPtr ret &= add_naval_unit(key, UNIT_ARGS, NAVY_ARGS); return ret; - } else return false; + } else { + Logger::error("Invalid type for unit ", key, ": ", type); + return false; + } })(root); } \ No newline at end of file diff --git a/src/openvic-simulation/units/Unit.hpp b/src/openvic-simulation/units/Unit.hpp index 979ae70..3555cce 100644 --- a/src/openvic-simulation/units/Unit.hpp +++ b/src/openvic-simulation/units/Unit.hpp @@ -9,30 +9,32 @@ #include "openvic-simulation/economy/Good.hpp" #include "openvic-simulation/types/Date.hpp" -#define UNIT_PARAMS Unit::icon_t icon, Unit::sprite_t sprite, bool active, std::string_view type, \ +#define UNIT_PARAMS Unit::icon_t icon, std::string_view sprite, bool active, std::string_view unit_type, \ bool floating_flag, uint32_t priority, fixed_point_t max_strength, fixed_point_t default_organisation, \ fixed_point_t maximum_speed, fixed_point_t weighted_value, Timespan build_time, \ - std::map build_cost, fixed_point_t supply_consumption, \ - std::map supply_cost + std::map build_cost, fixed_point_t supply_consumption, \ + std::map supply_cost #define LAND_PARAMS fixed_point_t reconnaissance, fixed_point_t attack, fixed_point_t defence, fixed_point_t discipline, \ fixed_point_t support, fixed_point_t maneuver, fixed_point_t siege -#define NAVY_PARAMS Unit::icon_t naval_icon, bool sail, bool transport, bool capital, Unit::sound_t move_sound, \ - Unit::sound_t select_sound, fixed_point_t colonial_points, bool build_overseas, uint32_t min_port_level, \ +#define NAVY_PARAMS Unit::icon_t naval_icon, bool sail, bool transport, bool capital, std::string_view move_sound, \ + std::string_view select_sound, fixed_point_t colonial_points, bool build_overseas, uint32_t min_port_level, \ int32_t limit_per_port, fixed_point_t supply_consumption_score, fixed_point_t hull, fixed_point_t gun_power, \ fixed_point_t fire_range, fixed_point_t evasion, fixed_point_t torpedo_attack namespace OpenVic { struct Unit : HasIdentifier { using icon_t = uint32_t; - using sprite_t = std::string_view; - using sound_t = std::string_view; + + enum struct type_t { + LAND, NAVAL + }; private: - const std::string_view category; + const type_t type; const icon_t icon; - const sprite_t sprite; + const std::string sprite; const bool active; - const std::string_view type; + const std::string unit_type; const bool floating_flag; const uint32_t priority; @@ -42,21 +44,21 @@ namespace OpenVic { const fixed_point_t weighted_value; const Timespan build_time; - const std::map build_cost; + const std::map build_cost; const fixed_point_t supply_consumption; - const std::map supply_cost; + const std::map supply_cost; protected: - Unit(std::string_view identifier, std::string_view category, UNIT_PARAMS); + Unit(std::string_view identifier, type_t type, UNIT_PARAMS); public: Unit(Unit&&) = default; icon_t get_icon() const; - std::string_view get_category() const; - sprite_t get_sprite() const; + type_t get_type() const; + std::string const& get_sprite() const; bool is_active() const; - std::string_view get_type() const; + std::string const& get_unit_type() const; bool has_floating_flag() const; uint32_t get_priority() const; @@ -66,9 +68,9 @@ namespace OpenVic { fixed_point_t get_weighted_value() const; Timespan get_build_time() const; - std::map const& get_build_cost() const; + std::map const& get_build_cost() const; fixed_point_t get_supply_consumption() const; - std::map const& get_supply_cost() const; + std::map const& get_supply_cost() const; }; struct LandUnit : Unit { @@ -105,8 +107,8 @@ namespace OpenVic { const bool sail; const bool transport; const bool capital; - const sound_t move_sound; - const sound_t select_sound; + const std::string move_sound; + const std::string select_sound; const fixed_point_t colonial_points; const bool build_overseas; const uint32_t min_port_level; @@ -128,8 +130,8 @@ namespace OpenVic { bool can_sail() const; bool is_transport() const; bool is_capital() const; - sound_t get_move_sound() const; - sound_t get_select_sound() const; + std::string const& get_move_sound() const; + std::string const& get_select_sound() const; fixed_point_t get_colonial_points() const; bool can_build_overseas() const; uint32_t get_min_port_level() const; -- cgit v1.2.3-56-ga3b1