diff options
author | hop311 <hop3114@gmail.com> | 2024-07-19 00:25:38 +0200 |
---|---|---|
committer | hop311 <hop3114@gmail.com> | 2024-07-19 00:26:27 +0200 |
commit | d8baf57d46539da9edba5952f73089bf9a54bdaf (patch) | |
tree | 6d2de5221d2b1945a065e0abba8198d8a052a92a /src/openvic-simulation/military/UnitInstanceGroup.hpp | |
parent | a673f89bb2705826b1c646365eab1775727372b7 (diff) |
Unit position/country/leader handling + more use of unit branch templatesunit-work
Diffstat (limited to 'src/openvic-simulation/military/UnitInstanceGroup.hpp')
-rw-r--r-- | src/openvic-simulation/military/UnitInstanceGroup.hpp | 151 |
1 files changed, 35 insertions, 116 deletions
diff --git a/src/openvic-simulation/military/UnitInstanceGroup.hpp b/src/openvic-simulation/military/UnitInstanceGroup.hpp index 54aac3d..4fe0cba 100644 --- a/src/openvic-simulation/military/UnitInstanceGroup.hpp +++ b/src/openvic-simulation/military/UnitInstanceGroup.hpp @@ -6,11 +6,10 @@ #include <plf_colony.h> -#include "openvic-simulation/map/ProvinceInstance.hpp" #include "openvic-simulation/military/UnitInstance.hpp" +#include "openvic-simulation/military/UnitType.hpp" #include "openvic-simulation/types/fixed_point/FixedPoint.hpp" #include "openvic-simulation/utility/Getters.hpp" -#include "openvic-simulation/utility/Utility.hpp" namespace OpenVic { struct ProvinceInstance; @@ -31,9 +30,6 @@ namespace OpenVic { struct CountryInstance; - template<UnitType::branch_t> - struct UnitInstanceGroupBranched; - template<UnitType::branch_t Branch> struct UnitInstanceGroup { using _UnitInstance = UnitInstanceBranched<Branch>; @@ -52,110 +48,27 @@ namespace OpenVic { UnitInstanceGroup( std::string_view new_name, - std::vector<_UnitInstance*>&& new_units, - _Leader* new_leader, - CountryInstance* new_country - ) : name { new_name }, - units { std::move(new_units) }, - leader { nullptr }, - position { nullptr }, - country { new_country } { - set_leader(new_leader); - } + std::vector<_UnitInstance*>&& new_units + ); public: UnitInstanceGroup(UnitInstanceGroup&&) = default; UnitInstanceGroup(UnitInstanceGroup const&) = delete; - void set_name(std::string_view new_name) { - name = new_name; - } - - size_t get_unit_count() const { - return units.size(); - } - - bool empty() const { - return units.empty(); - } - - size_t get_unit_category_count(UnitType::unit_category_t unit_category) const { - return std::count_if(units.begin(), units.end(), [unit_category](_UnitInstance const* unit) { - return unit->unit_type.get_unit_category() == unit_category; - }); - } - - UnitType const* get_display_unit_type() const { - if (units.empty()) { - return nullptr; - } - - fixed_point_map_t<UnitType const*> weighted_unit_types; - - for (_UnitInstance const* unit : units) { - UnitType const& unit_type = unit->get_unit_type(); - weighted_unit_types[&unit_type] += unit_type.get_weighted_value(); - } - - return get_largest_item_tie_break( - weighted_unit_types, - [](UnitType const* lhs, UnitType const* rhs) -> bool { - return lhs->get_weighted_value() < rhs->get_weighted_value(); - } - )->first; - } - - void set_position(ProvinceInstance* new_position) { - if (position != new_position) { - if (position != nullptr) { - position->remove_unit_instance_group(*this); - } - - position = new_position; - - if (position != nullptr) { - position->add_unit_instance_group(*this); - } - } - } - - bool set_leader(_Leader* new_leader) { - bool ret = true; - - if (leader != new_leader) { - if (leader != nullptr) { - if (leader->unit_instance_group == this) { - leader->unit_instance_group = nullptr; - } else { - Logger::error( - "Mismatch between leader and unit instance group: group ", name, " has leader ", - leader->get_name(), " but the leader has group ", leader->get_unit_instance_group() != nullptr - ? leader->get_unit_instance_group()->get_name() : "NULL" - ); - ret = false; - } - } - - leader = new_leader; - - if (leader != nullptr) { - if (leader->unit_instance_group != nullptr) { - if (leader->unit_instance_group != this) { - ret &= leader->unit_instance_group->set_leader(nullptr); - } else { - Logger::error("Leader ", leader->get_name(), " already leads group ", name, "!"); - ret = false; - } - } - - leader->unit_instance_group = static_cast<UnitInstanceGroupBranched<Branch>*>(this); - } - } - - return ret; - } + size_t get_unit_count() const; + bool empty() const; + size_t get_unit_category_count(UnitType::unit_category_t unit_category) const; + UnitType const* get_display_unit_type() const; + + void set_name(std::string_view new_name); + bool set_position(ProvinceInstance* new_position); + bool set_country(CountryInstance* new_country); + bool set_leader(_Leader* new_leader); }; + template<UnitType::branch_t> + struct UnitInstanceGroupBranched; + template<> struct UnitInstanceGroupBranched<UnitType::branch_t::LAND> : UnitInstanceGroup<UnitType::branch_t::LAND> { friend struct UnitInstanceManager; @@ -163,9 +76,7 @@ namespace OpenVic { private: UnitInstanceGroupBranched( std::string_view new_name, - std::vector<RegimentInstance*>&& new_units, - _Leader* new_leader, - CountryInstance* new_country + std::vector<RegimentInstance*>&& new_units ); public: @@ -183,9 +94,7 @@ namespace OpenVic { UnitInstanceGroupBranched( std::string_view new_name, - std::vector<ShipInstance*>&& new_ships, - _Leader* new_leader, - CountryInstance* new_country + std::vector<ShipInstance*>&& new_ships ); public: @@ -194,11 +103,13 @@ namespace OpenVic { using NavyInstance = UnitInstanceGroupBranched<UnitType::branch_t::NAVAL>; - struct RegimentDeployment; - struct ShipDeployment; + template<UnitType::branch_t> + struct UnitDeployment; + + template<UnitType::branch_t> + struct UnitDeploymentGroup; + struct MapInstance; - struct ArmyDeployment; - struct NavyDeployment; struct Deployment; struct UnitInstanceManager { @@ -206,13 +117,21 @@ namespace OpenVic { plf::colony<RegimentInstance> PROPERTY(regiments); plf::colony<ShipInstance> PROPERTY(ships); + UNIT_BRANCHED_GETTER(get_unit_instances, regiments, ships); + plf::colony<ArmyInstance> PROPERTY(armies); plf::colony<NavyInstance> PROPERTY(navies); - bool generate_regiment(RegimentDeployment const& regiment_deployment, RegimentInstance*& regiment); - bool generate_ship(ShipDeployment const& ship_deployment, ShipInstance*& ship); - bool generate_army(MapInstance& map_instance, CountryInstance& country, ArmyDeployment const& army_deployment); - bool generate_navy(MapInstance& map_instance, CountryInstance& country, NavyDeployment const& navy_deployment); + UNIT_BRANCHED_GETTER(get_unit_instance_groups, armies, navies); + + template<UnitType::branch_t Branch> + bool generate_unit_instance( + UnitDeployment<Branch> const& unit_deployment, UnitInstanceBranched<Branch>*& unit_instance + ); + template<UnitType::branch_t Branch> + bool generate_unit_instance_group( + MapInstance& map_instance, CountryInstance& country, UnitDeploymentGroup<Branch> const& unit_deployment_group + ); public: bool generate_deployment(MapInstance& map_instance, CountryInstance& country, Deployment const* deployment); |