From 13d652813e2a662875b0364ec0d1bfe99e9004d6 Mon Sep 17 00:00:00 2001 From: hop311 Date: Tue, 23 Jan 2024 00:17:38 +0000 Subject: Reworked Units and added IdentifierPointerRegistry --- src/openvic-simulation/military/Unit.hpp | 132 +++++++++++++++++++++---------- 1 file changed, 92 insertions(+), 40 deletions(-) (limited to 'src/openvic-simulation/military/Unit.hpp') diff --git a/src/openvic-simulation/military/Unit.hpp b/src/openvic-simulation/military/Unit.hpp index c791aca..d1ee3e3 100644 --- a/src/openvic-simulation/military/Unit.hpp +++ b/src/openvic-simulation/military/Unit.hpp @@ -10,34 +10,42 @@ #include "openvic-simulation/types/IdentifierRegistry.hpp" #include "openvic-simulation/types/fixed_point/FixedPoint.hpp" -#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, std::string_view move_sound, std::string_view select_sound, Timespan build_time, \ - Good::good_map_t&& build_cost, fixed_point_t supply_consumption, Good::good_map_t&& supply_cost - -#define LAND_PARAMS \ - LandUnit::allowed_cultures_t allowed_cultures, std::string_view sprite_override, std::string_view sprite_mount, \ - std::string_view sprite_mount_attach_node, 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, 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 TerrainType; + struct TerrainTypeManager; + struct Unit : HasIdentifier { using icon_t = uint32_t; - - enum struct type_t { LAND, NAVAL }; + using terrain_modifiers_t = ordered_map; + + enum struct branch_t : uint8_t { INVALID_BRANCH, LAND, NAVAL }; + enum struct unit_type_t : uint8_t { + INVALID_UNIT_TYPE, INFANTRY, CAVALRY, SUPPORT, SPECIAL, BIG_SHIP, LIGHT_SHIP, TRANSPORT + }; + + struct unit_args_t { + icon_t icon = 0; + unit_type_t unit_type = unit_type_t::INVALID_UNIT_TYPE; + // TODO defaults for move_sound and select_sound + std::string_view sprite, move_sound, select_sound; + bool active = true, floating_flag = false; + uint32_t priority = 0; + fixed_point_t max_strength = 0, default_organisation = 0, maximum_speed = 0, weighted_value = 0, + supply_consumption = 0; + Timespan build_time; + Good::good_map_t build_cost, supply_cost; + terrain_modifiers_t terrain_modifiers; + + unit_args_t() = default; + unit_args_t(unit_args_t&&) = default; + }; private: - const type_t PROPERTY(type); + const branch_t PROPERTY(branch); /* type in defines */ const icon_t PROPERTY(icon); - const std::string PROPERTY(sprite); + std::string PROPERTY(sprite); const bool PROPERTY_CUSTOM_PREFIX(active, is); - const std::string PROPERTY(unit_type); + unit_type_t PROPERTY(unit_type); const bool PROPERTY_CUSTOM_PREFIX(floating_flag, has); const uint32_t PROPERTY(priority); @@ -46,16 +54,19 @@ namespace OpenVic { const fixed_point_t PROPERTY(maximum_speed); const fixed_point_t PROPERTY(weighted_value); - const std::string PROPERTY(move_sound); - const std::string PROPERTY(select_sound); + std::string PROPERTY(move_sound); + std::string PROPERTY(select_sound); const Timespan PROPERTY(build_time); - const Good::good_map_t PROPERTY(build_cost); + Good::good_map_t PROPERTY(build_cost); const fixed_point_t PROPERTY(supply_consumption); - const Good::good_map_t PROPERTY(supply_cost); + Good::good_map_t PROPERTY(supply_cost); + + terrain_modifiers_t PROPERTY(terrain_modifiers); protected: - Unit(std::string_view identifier, type_t type, UNIT_PARAMS); + /* Non-const reference unit_args so variables can be moved from it. */ + Unit(std::string_view new_identifier, branch_t new_branch, unit_args_t& unit_args); public: Unit(Unit&&) = default; @@ -66,11 +77,23 @@ namespace OpenVic { enum struct allowed_cultures_t { ALL_CULTURES, ACCEPTED_CULTURES, PRIMARY_CULTURE }; + struct land_unit_args_t { + allowed_cultures_t allowed_cultures = allowed_cultures_t::ALL_CULTURES; + std::string_view sprite_override, sprite_mount, sprite_mount_attach_node; + // TODO - represent these as modifier effects, so that they can be combined with tech, inventions, + // leader bonuses, etc. and applied to unit instances all in one go (same for NavalUnits below) + fixed_point_t reconnaissance = 0, attack = 0, defence = 0, discipline = 0, support = 0, maneuver = 0, + siege = 0; + + land_unit_args_t() = default; + land_unit_args_t(land_unit_args_t&&) = default; + }; + private: const allowed_cultures_t PROPERTY(allowed_cultures); - const std::string PROPERTY(sprite_override); - const std::string PROPERTY(sprite_mount); - const std::string PROPERTY(sprite_mount_attach_node); + std::string PROPERTY(sprite_override); + std::string PROPERTY(sprite_mount); + std::string PROPERTY(sprite_mount_attach_node); const fixed_point_t PROPERTY(reconnaissance); const fixed_point_t PROPERTY(attack); const fixed_point_t PROPERTY(defence); @@ -79,7 +102,7 @@ namespace OpenVic { const fixed_point_t PROPERTY(maneuver); const fixed_point_t PROPERTY(siege); - LandUnit(std::string_view identifier, UNIT_PARAMS, LAND_PARAMS); + LandUnit(std::string_view new_identifier, unit_args_t& unit_args, land_unit_args_t const& land_unit_args); public: LandUnit(LandUnit&&) = default; @@ -88,6 +111,18 @@ namespace OpenVic { struct NavalUnit : Unit { friend struct UnitManager; + struct naval_unit_args_t { + Unit::icon_t naval_icon = 0; + bool sail = false, transport = false, capital = false, build_overseas = false; + uint32_t min_port_level = 0; + int32_t limit_per_port = 0; + fixed_point_t colonial_points = 0, supply_consumption_score = 0, hull = 0, gun_power = 0, fire_range = 0, + evasion = 0, torpedo_attack = 0; + + naval_unit_args_t() = default; + naval_unit_args_t(naval_unit_args_t&&) = default; + }; + private: const icon_t PROPERTY(naval_icon); const bool PROPERTY_CUSTOM_PREFIX(sail, can); @@ -105,7 +140,7 @@ namespace OpenVic { const fixed_point_t PROPERTY(evasion); const fixed_point_t PROPERTY(torpedo_attack); - NavalUnit(std::string_view identifier, UNIT_PARAMS, NAVY_PARAMS); + NavalUnit(std::string_view new_identifier, unit_args_t& unit_args, naval_unit_args_t const& naval_unit_args); public: NavalUnit(NavalUnit&&) = default; @@ -113,17 +148,34 @@ namespace OpenVic { struct UnitManager { private: - IdentifierRegistry IDENTIFIER_REGISTRY(unit); - - bool _check_shared_parameters(std::string_view identifier, UNIT_PARAMS); + IdentifierPointerRegistry IDENTIFIER_REGISTRY(unit); + IdentifierRegistry IDENTIFIER_REGISTRY(land_unit); + IdentifierRegistry IDENTIFIER_REGISTRY(naval_unit); public: - bool add_land_unit(std::string_view identifier, UNIT_PARAMS, LAND_PARAMS); - bool add_naval_unit(std::string_view identifier, UNIT_PARAMS, NAVY_PARAMS); - - static NodeTools::callback_t expect_type_str(NodeTools::Callback auto callback); - - bool load_unit_file(GoodManager const& good_manager, ast::NodeCPtr root); + void reserve_all_units(size_t size); + void lock_all_units(); + + bool add_land_unit( + std::string_view identifier, Unit::unit_args_t& unit_args, LandUnit::land_unit_args_t const& land_unit_args + ); + bool add_naval_unit( + std::string_view identifier, Unit::unit_args_t& unit_args, NavalUnit::naval_unit_args_t const& naval_unit_args + ); + + static NodeTools::Callback auto expect_branch_str(NodeTools::Callback auto callback) { + using enum Unit::branch_t; + static const string_map_t branch_map { { "land", LAND }, { "naval", NAVAL }, { "sea", NAVAL } }; + return NodeTools::expect_mapped_string(branch_map, callback); + } + static NodeTools::NodeCallback auto expect_branch_identifier(NodeTools::Callback auto callback) { + return NodeTools::expect_identifier(expect_branch_str(callback)); + } + + bool load_unit_file( + GoodManager const& good_manager, TerrainTypeManager const& terrain_type_manager, + ModifierManager const& modifier_manager, ast::NodeCPtr root + ); bool generate_modifiers(ModifierManager& modifier_manager) const; }; } -- cgit v1.2.3-56-ga3b1