From 6b3d2315225468e03bef30e942263fe9b8fc06fa Mon Sep 17 00:00:00 2001 From: hop311 Date: Thu, 12 Oct 2023 23:05:49 +0100 Subject: Further cleanup and rearrangement --- src/openvic-simulation/military/Unit.cpp | 338 +++++++++++++++++++++++++++++++ 1 file changed, 338 insertions(+) create mode 100644 src/openvic-simulation/military/Unit.cpp (limited to 'src/openvic-simulation/military/Unit.cpp') diff --git a/src/openvic-simulation/military/Unit.cpp b/src/openvic-simulation/military/Unit.cpp new file mode 100644 index 0000000..3da24da --- /dev/null +++ b/src/openvic-simulation/military/Unit.cpp @@ -0,0 +1,338 @@ +#include "Unit.hpp" + +#include + +#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 primary_culture, sprite_override, sprite_mount, sprite_mount_attach_node, \ + reconnaissance, attack, defence, discipline, support, maneuver, siege +#define NAVY_ARGS naval_icon, sail, transport, capital, move_sound, select_sound, colonial_points, build_overseas, min_port_level, \ + limit_per_port, supply_consumption_score, hull, gun_power, fire_range, evasion, torpedo_attack + +using namespace OpenVic; +using namespace OpenVic::NodeTools; + +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 } {} + +Unit::icon_t Unit::get_icon() const { + return icon; +} + +Unit::type_t Unit::get_type() const { + return type; +} + +std::string const& Unit::get_sprite() const { + return sprite; +} + +bool Unit::is_active() const { + return active; +} + +std::string const& Unit::get_unit_type() const { + return unit_type; +} + +bool Unit::has_floating_flag() const { + return floating_flag; +} + +uint32_t Unit::get_priority() const { + return priority; +} + +fixed_point_t Unit::get_max_strength() const { + return max_strength; +} + +fixed_point_t Unit::get_default_organisation() const { + return default_organisation; +} + +fixed_point_t Unit::get_maximum_speed() const { + return maximum_speed; +} + +Timespan Unit::get_build_time() const { + return build_time; +} + +fixed_point_t Unit::get_weighted_value() const { + return weighted_value; +} + +std::map const& Unit::get_build_cost() const { + return build_cost; +} + +fixed_point_t Unit::get_supply_consumption() const { + return supply_consumption; +} + +std::map const& Unit::get_supply_cost() const { + return supply_cost; +} + +LandUnit::LandUnit(std::string_view identifier, UNIT_PARAMS, LAND_PARAMS) + : Unit { identifier, type_t::LAND, UNIT_ARGS }, primary_culture { primary_culture }, sprite_override { sprite_override }, + sprite_mount { sprite_mount }, sprite_mount_attach_node { sprite_mount_attach_node }, reconnaissance { reconnaissance }, + attack { attack }, defence { defence }, discipline { discipline }, support { support }, maneuver { maneuver }, siege { siege } {} + +bool LandUnit::get_primary_culture() const { + return primary_culture; +} + +std::string const& LandUnit::get_sprite_override() const { + return sprite_override; +} + +std::string const& LandUnit::get_sprite_mount() const { + return sprite_mount; +} + +std::string const& LandUnit::get_sprite_mount_attach_node() const { + return sprite_mount_attach_node; +} + +fixed_point_t LandUnit::get_reconnaissance() const { + return reconnaissance; +} + +fixed_point_t LandUnit::get_attack() const { + return attack; +} + +fixed_point_t LandUnit::get_defence() const { + return defence; +} + +fixed_point_t LandUnit::get_discipline() const { + return discipline; +} + +fixed_point_t LandUnit::get_support() const { + return support; +} + +fixed_point_t LandUnit::get_maneuver() const { + return maneuver; +} + +fixed_point_t LandUnit::get_siege() const { + return siege; +} + +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 }, + hull { hull }, gun_power { gun_power }, fire_range { fire_range }, evasion { evasion }, torpedo_attack { torpedo_attack } {}; + +NavalUnit::icon_t NavalUnit::get_naval_icon() const { + return naval_icon; +} + +bool NavalUnit::can_sail() const { + return sail; +} + +bool NavalUnit::is_transport() const { + return transport; +} + +std::string const& NavalUnit::get_move_sound() const { + return move_sound; +} + +std::string const& NavalUnit::get_select_sound() const { + return select_sound; +} + +fixed_point_t NavalUnit::get_colonial_points() const { + return colonial_points; +} + +bool NavalUnit::can_build_overseas() const { + return build_overseas; +} + +uint32_t NavalUnit::get_min_port_level() const { + return min_port_level; +} + +int32_t NavalUnit::get_limit_per_port() const { + return limit_per_port; +} + +fixed_point_t NavalUnit::get_supply_consumption_score() const { + return supply_consumption_score; +} + +fixed_point_t NavalUnit::get_hull() const { + return hull; +} + +fixed_point_t NavalUnit::get_gun_power() const { + return gun_power; +} + +fixed_point_t NavalUnit::get_fire_range() const { + return fire_range; +} + +fixed_point_t NavalUnit::get_evasion() const { + return evasion; +} + +fixed_point_t NavalUnit::get_torpedo_attack() const { + return torpedo_attack; +} + +UnitManager::UnitManager() : units { "units" } {} + +bool UnitManager::_check_shared_parameters(std::string_view identifier, UNIT_PARAMS) { + if (identifier.empty()) { + Logger::error("Invalid religion identifier - empty!"); + return false; + } + + if (sprite.empty()) { + Logger::error("Invalid sprite identifier - empty!"); + return false; + } + + if (unit_type.empty()) { + Logger::error("Invalid unit type - empty!"); + return false; + } + + //TODO check that icon and sprite exist + + return true; +} + +bool UnitManager::add_land_unit(std::string_view identifier, UNIT_PARAMS, LAND_PARAMS) { + if (!_check_shared_parameters(identifier, UNIT_ARGS)) { + return false; + } + + return units.add_item(LandUnit { identifier, UNIT_ARGS, LAND_ARGS }); +} + +bool UnitManager::add_naval_unit(std::string_view identifier, UNIT_PARAMS, NAVY_PARAMS) { + if (!_check_shared_parameters(identifier, UNIT_ARGS)) { + return false; + } + + //TODO: check that icon and sounds exist + + return units.add_item(NavalUnit { identifier, UNIT_ARGS, NAVY_ARGS }); +} + +static bool shared_keys_callback(std::string_view key, ast::NodeCPtr) { + static const std::set> reserved_keys = { + "icon", "type", "sprite", "active", "unit_type", "floating_flag", "priority", + "max_strength", "default_organisation", "maximum_speed", "weighted_value", + "build_time", "build_cost", "supply_consumption", "supply_cost" + }; + if (reserved_keys.contains(key)) return true; + Logger::error("Invalid key: ", key); + return false; +}; + +bool UnitManager::load_unit_file(GoodManager const& good_manager, ast::NodeCPtr root) { + 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_and_default( + key_value_success_callback, + "icon", ONE_EXACTLY, expect_uint(assign_variable_callback(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(unit_type)), + "floating_flag", ONE_EXACTLY, expect_bool(assign_variable_callback(floating_flag)), + "priority", ONE_EXACTLY, expect_uint(assign_variable_callback(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_days(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); + + if (type == "land") { + bool primary_culture = false; + std::string_view sprite_override, sprite_mount, sprite_mount_attach_node; + fixed_point_t reconnaissance = 0, attack = 0, defence = 0, discipline = 0, support = 0, maneuver = 0, siege = 0; + + ret &= expect_dictionary_keys_and_default( + shared_keys_callback, + "primary_culture", ZERO_OR_ONE, expect_bool(assign_variable_callback(primary_culture)), + "sprite_override", ZERO_OR_ONE, expect_identifier(assign_variable_callback(sprite_override)), + "sprite_mount", ZERO_OR_ONE, expect_identifier(assign_variable_callback(sprite_mount)), + "sprite_mount_attach_node", ZERO_OR_ONE, expect_identifier(assign_variable_callback(sprite_mount_attach_node)), + "reconnaissance", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(reconnaissance)), + "attack", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(attack)), + "defence", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(defence)), + "discipline", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(discipline)), + "support", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(support)), + "maneuver", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(maneuver)), + "siege", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(siege)) + )(value); + + ret &= add_land_unit(key, UNIT_ARGS, LAND_ARGS); + + return ret; + } else if (type == "naval") { + Unit::icon_t naval_icon = 0; + bool sail = false, transport = false, capital = false, build_overseas = false; + 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_and_default( + shared_keys_callback, + "naval_icon", ONE_EXACTLY, expect_uint(assign_variable_callback(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)), + "move_sound", ZERO_OR_ONE, expect_identifier(assign_variable_callback(move_sound)), + "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(min_port_level)), + "limit_per_port", ONE_EXACTLY, expect_int(assign_variable_callback(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)), + "fire_range", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(fire_range)), + "evasion", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(evasion)), + "torpedo_attack", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(torpedo_attack)) + )(value); + + ret &= add_naval_unit(key, UNIT_ARGS, NAVY_ARGS); + + return ret; + } else { + Logger::error("Invalid type for unit ", key, ": ", type); + return false; + } + })(root); +} \ No newline at end of file -- cgit v1.2.3-56-ga3b1 From d95c3c9da75018e3e959e5493ebd9c520e00bf7a Mon Sep 17 00:00:00 2001 From: hop311 Date: Thu, 12 Oct 2023 23:43:10 +0100 Subject: Changed return types to std::string_view --- src/openvic-simulation/economy/Building.cpp | 2 +- src/openvic-simulation/economy/Building.hpp | 2 +- src/openvic-simulation/military/Unit.cpp | 14 +++++++------- src/openvic-simulation/military/Unit.hpp | 14 +++++++------- src/openvic-simulation/politics/Government.cpp | 2 +- src/openvic-simulation/politics/Government.hpp | 2 +- src/openvic-simulation/pop/Culture.cpp | 2 +- src/openvic-simulation/pop/Culture.hpp | 2 +- src/openvic-simulation/types/IdentifierRegistry.cpp | 2 +- src/openvic-simulation/types/IdentifierRegistry.hpp | 20 ++++++++++++++------ 10 files changed, 35 insertions(+), 27 deletions(-) (limited to 'src/openvic-simulation/military/Unit.cpp') diff --git a/src/openvic-simulation/economy/Building.cpp b/src/openvic-simulation/economy/Building.cpp index d27c91a..23bd04c 100644 --- a/src/openvic-simulation/economy/Building.cpp +++ b/src/openvic-simulation/economy/Building.cpp @@ -17,7 +17,7 @@ BuildingType const& Building::get_type() const { return type; } -std::string const& Building::get_on_completion() const { +std::string_view Building::get_on_completion() const { return on_completion; } diff --git a/src/openvic-simulation/economy/Building.hpp b/src/openvic-simulation/economy/Building.hpp index 3f74c3d..3d1e24b 100644 --- a/src/openvic-simulation/economy/Building.hpp +++ b/src/openvic-simulation/economy/Building.hpp @@ -70,7 +70,7 @@ namespace OpenVic { Building(Building&&) = default; BuildingType const& get_type() const; - std::string const& get_on_completion() const; + std::string_view get_on_completion() const; fixed_point_t get_completion_size() const; level_t get_max_level() const; std::map const& get_goods_cost() const; diff --git a/src/openvic-simulation/military/Unit.cpp b/src/openvic-simulation/military/Unit.cpp index 3da24da..63ece91 100644 --- a/src/openvic-simulation/military/Unit.cpp +++ b/src/openvic-simulation/military/Unit.cpp @@ -27,7 +27,7 @@ Unit::type_t Unit::get_type() const { return type; } -std::string const& Unit::get_sprite() const { +std::string_view Unit::get_sprite() const { return sprite; } @@ -35,7 +35,7 @@ bool Unit::is_active() const { return active; } -std::string const& Unit::get_unit_type() const { +std::string_view Unit::get_unit_type() const { return unit_type; } @@ -88,15 +88,15 @@ bool LandUnit::get_primary_culture() const { return primary_culture; } -std::string const& LandUnit::get_sprite_override() const { +std::string_view LandUnit::get_sprite_override() const { return sprite_override; } -std::string const& LandUnit::get_sprite_mount() const { +std::string_view LandUnit::get_sprite_mount() const { return sprite_mount; } -std::string const& LandUnit::get_sprite_mount_attach_node() const { +std::string_view LandUnit::get_sprite_mount_attach_node() const { return sprite_mount_attach_node; } @@ -146,11 +146,11 @@ bool NavalUnit::is_transport() const { return transport; } -std::string const& NavalUnit::get_move_sound() const { +std::string_view NavalUnit::get_move_sound() const { return move_sound; } -std::string const& NavalUnit::get_select_sound() const { +std::string_view NavalUnit::get_select_sound() const { return select_sound; } diff --git a/src/openvic-simulation/military/Unit.hpp b/src/openvic-simulation/military/Unit.hpp index 65e6259..de30763 100644 --- a/src/openvic-simulation/military/Unit.hpp +++ b/src/openvic-simulation/military/Unit.hpp @@ -57,9 +57,9 @@ namespace OpenVic { icon_t get_icon() const; type_t get_type() const; - std::string const& get_sprite() const; + std::string_view get_sprite() const; bool is_active() const; - std::string const& get_unit_type() const; + std::string_view get_unit_type() const; bool has_floating_flag() const; uint32_t get_priority() const; @@ -94,9 +94,9 @@ namespace OpenVic { LandUnit(LandUnit&&) = default; bool get_primary_culture() const; - std::string const& get_sprite_override() const; - std::string const& get_sprite_mount() const; - std::string const& get_sprite_mount_attach_node() const; + std::string_view get_sprite_override() const; + std::string_view get_sprite_mount() const; + std::string_view get_sprite_mount_attach_node() const; fixed_point_t get_reconnaissance() const; fixed_point_t get_attack() const; @@ -138,8 +138,8 @@ namespace OpenVic { bool can_sail() const; bool is_transport() const; bool is_capital() const; - std::string const& get_move_sound() const; - std::string const& get_select_sound() const; + std::string_view get_move_sound() const; + std::string_view get_select_sound() const; fixed_point_t get_colonial_points() const; bool can_build_overseas() const; uint32_t get_min_port_level() const; diff --git a/src/openvic-simulation/politics/Government.cpp b/src/openvic-simulation/politics/Government.cpp index 1a48806..16b3bc4 100644 --- a/src/openvic-simulation/politics/Government.cpp +++ b/src/openvic-simulation/politics/Government.cpp @@ -30,7 +30,7 @@ Timespan GovernmentType::get_term_duration() const { return term_duration; } -std::string const& GovernmentType::get_flag_type() const { +std::string_view GovernmentType::get_flag_type() const { return flag_type_identifier; } diff --git a/src/openvic-simulation/politics/Government.hpp b/src/openvic-simulation/politics/Government.hpp index c72dae6..ba8496f 100644 --- a/src/openvic-simulation/politics/Government.hpp +++ b/src/openvic-simulation/politics/Government.hpp @@ -24,7 +24,7 @@ namespace OpenVic { bool holds_elections() const; bool can_appoint_ruling_party() const; Timespan get_term_duration() const; - std::string const& get_flag_type() const; + std::string_view get_flag_type() const; }; struct GovernmentTypeManager { diff --git a/src/openvic-simulation/pop/Culture.cpp b/src/openvic-simulation/pop/Culture.cpp index 261ad4c..e3ba5dd 100644 --- a/src/openvic-simulation/pop/Culture.cpp +++ b/src/openvic-simulation/pop/Culture.cpp @@ -15,7 +15,7 @@ CultureGroup::CultureGroup(std::string_view new_identifier, std::string_view new unit_graphical_culture_type { new_unit_graphical_culture_type }, is_overseas { new_is_overseas } {} -std::string const& CultureGroup::get_leader() const { +std::string_view CultureGroup::get_leader() const { return leader; } diff --git a/src/openvic-simulation/pop/Culture.hpp b/src/openvic-simulation/pop/Culture.hpp index b6b4952..d36a90b 100644 --- a/src/openvic-simulation/pop/Culture.hpp +++ b/src/openvic-simulation/pop/Culture.hpp @@ -31,7 +31,7 @@ namespace OpenVic { public: CultureGroup(CultureGroup&&) = default; - std::string const& get_leader() const; + std::string_view get_leader() const; GraphicalCultureType const& get_unit_graphical_culture_type() const; bool get_is_overseas() const; }; diff --git a/src/openvic-simulation/types/IdentifierRegistry.cpp b/src/openvic-simulation/types/IdentifierRegistry.cpp index d6afd3a..8e93cb1 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.cpp +++ b/src/openvic-simulation/types/IdentifierRegistry.cpp @@ -9,7 +9,7 @@ HasIdentifier::HasIdentifier(std::string_view new_identifier) assert(!identifier.empty()); } -std::string const& HasIdentifier::get_identifier() const { +std::string_view HasIdentifier::get_identifier() const { return identifier; } diff --git a/src/openvic-simulation/types/IdentifierRegistry.hpp b/src/openvic-simulation/types/IdentifierRegistry.hpp index aea5728..3ba7fc4 100644 --- a/src/openvic-simulation/types/IdentifierRegistry.hpp +++ b/src/openvic-simulation/types/IdentifierRegistry.hpp @@ -28,7 +28,7 @@ namespace OpenVic { HasIdentifier& operator=(HasIdentifier const&) = delete; HasIdentifier& operator=(HasIdentifier&&) = delete; - std::string const& get_identifier() const; + std::string_view get_identifier() const; }; std::ostream& operator<<(std::ostream& stream, HasIdentifier const& obj); @@ -73,7 +73,7 @@ namespace OpenVic { distribution_t::value_type get_largest_item(distribution_t const& dist); template - using get_identifier_func_t = std::string const&(T::*)(void) const; + using get_identifier_func_t = std::string_view(T::*)(void) const; template _Type, get_identifier_func_t<_Base> get_identifier, typename _Storage, _Type* (*get_ptr)(_Storage&), _Type const* (*get_cptr)(_Storage const&)> @@ -92,7 +92,7 @@ namespace OpenVic { UniqueKeyRegistry(std::string_view new_name, bool new_log_lock = true) : name { new_name }, log_lock { new_log_lock } {} - std::string const& get_name() const { + std::string_view get_name() const { return name; } @@ -102,7 +102,7 @@ namespace OpenVic { return false; } value_type const* new_item = (*get_cptr)(item); - std::string const& new_identifier = (new_item->*get_identifier)(); + const std::string_view new_identifier = (new_item->*get_identifier)(); value_type const* old_item = get_item_by_identifier(new_identifier); if (old_item != nullptr) { #define DUPLICATE_MESSAGE "Cannot add item to the ", name, " registry - an item with the identifier \"", new_identifier, "\" already exists!" @@ -115,7 +115,7 @@ namespace OpenVic { } #undef DUPLICATE_MESSAGE } - identifier_index_map[new_identifier] = items.size(); + identifier_index_map.emplace(new_identifier, items.size()); items.push_back(std::move(item)); return true; } @@ -249,8 +249,16 @@ namespace OpenVic { } }; + template + [[nodiscard]] inline constexpr T* _addressof(T& v) noexcept { + return std::addressof(v); + } + + template + const T* _addressof(const T&&) = delete; + template _Type, get_identifier_func_t<_Base> get_identifier> - using ValueRegistry = UniqueKeyRegistry<_Base, _Type, get_identifier, _Type, std::addressof<_Type>, std::addressof>; + using ValueRegistry = UniqueKeyRegistry<_Base, _Type, get_identifier, _Type, _addressof<_Type>, _addressof>; template constexpr _Type* get_ptr(std::unique_ptr<_Type>& storage) { -- cgit v1.2.3-56-ga3b1