From d8baf57d46539da9edba5952f73089bf9a54bdaf Mon Sep 17 00:00:00 2001 From: hop311 Date: Thu, 18 Jul 2024 23:25:38 +0100 Subject: Unit position/country/leader handling + more use of unit branch templates --- src/openvic-simulation/map/ProvinceInstance.cpp | 41 +++++++++++-------------- src/openvic-simulation/map/ProvinceInstance.hpp | 27 +++------------- 2 files changed, 22 insertions(+), 46 deletions(-) (limited to 'src/openvic-simulation/map') diff --git a/src/openvic-simulation/map/ProvinceInstance.cpp b/src/openvic-simulation/map/ProvinceInstance.cpp index 1792fe0..6b20cd2 100644 --- a/src/openvic-simulation/map/ProvinceInstance.cpp +++ b/src/openvic-simulation/map/ProvinceInstance.cpp @@ -104,41 +104,36 @@ void ProvinceInstance::tick(Date today) { } } -bool ProvinceInstance::add_army(ArmyInstance& army) { - if (armies.emplace(&army).second) { +template +bool ProvinceInstance::add_unit_instance_group(UnitInstanceGroup& group) { + if (get_unit_instance_groups().emplace(static_cast*>(&group)).second) { return true; } else { - Logger::error("Trying to add already-existing army ", army.get_name(), " to province ", get_identifier()); + Logger::error( + "Trying to add already-existing ", Branch == UnitType::branch_t::LAND ? "army" : "navy", " ", + group.get_name(), " to province ", get_identifier() + ); return false; } } -bool ProvinceInstance::remove_army(ArmyInstance& army) { - if (armies.erase(&army) > 0) { +template +bool ProvinceInstance::remove_unit_instance_group(UnitInstanceGroup& group) { + if (get_unit_instance_groups().erase(static_cast*>(&group)) > 0) { return true; } else { - Logger::error("Trying to remove non-existent army ", army.get_name(), " from province ", get_identifier()); + Logger::error( + "Trying to remove non-existent ", Branch == UnitType::branch_t::LAND ? "army" : "navy", " ", + group.get_name(), " from province ", get_identifier() + ); return false; } } -bool ProvinceInstance::add_navy(NavyInstance& navy) { - if (navies.emplace(&navy).second) { - return true; - } else { - Logger::error("Trying to add already-existing navy ", navy.get_name(), " to province ", get_identifier()); - return false; - } -} - -bool ProvinceInstance::remove_navy(NavyInstance& navy) { - if (navies.erase(&navy) > 0) { - return true; - } else { - Logger::error("Trying to remove non-existent navy ", navy.get_name(), " from province ", get_identifier()); - return false; - } -} +template bool ProvinceInstance::add_unit_instance_group(UnitInstanceGroup&); +template bool ProvinceInstance::add_unit_instance_group(UnitInstanceGroup&); +template bool ProvinceInstance::remove_unit_instance_group(UnitInstanceGroup&); +template bool ProvinceInstance::remove_unit_instance_group(UnitInstanceGroup&); bool ProvinceInstance::setup(BuildingTypeManager const& building_type_manager) { if (buildings_are_locked()) { diff --git a/src/openvic-simulation/map/ProvinceInstance.hpp b/src/openvic-simulation/map/ProvinceInstance.hpp index 4398cd4..ec06e0c 100644 --- a/src/openvic-simulation/map/ProvinceInstance.hpp +++ b/src/openvic-simulation/map/ProvinceInstance.hpp @@ -58,6 +58,8 @@ namespace OpenVic { ordered_set PROPERTY(armies); ordered_set PROPERTY(navies); + UNIT_BRANCHED_GETTER(get_unit_instance_groups, armies, navies); + std::vector PROPERTY(pops); Pop::pop_size_t PROPERTY(total_population); IndexedMap PROPERTY(pop_type_distribution); @@ -89,31 +91,10 @@ namespace OpenVic { void update_gamestate(Date today); void tick(Date today); - bool add_army(ArmyInstance& army); - bool remove_army(ArmyInstance& army); - bool add_navy(NavyInstance& navy); - bool remove_navy(NavyInstance& navy); - template - bool add_unit_instance_group(UnitInstanceGroup& group) { - if constexpr (Branch == UnitType::branch_t::LAND) { - return add_army(static_cast(group)); - } else if constexpr (Branch == UnitType::branch_t::NAVAL) { - return add_navy(static_cast(group)); - } else { - OpenVic::utility::unreachable(); - } - } + bool add_unit_instance_group(UnitInstanceGroup& group); template - bool remove_unit_instance_group(UnitInstanceGroup& group) { - if constexpr (Branch == UnitType::branch_t::LAND) { - return remove_army(static_cast(group)); - } else if constexpr (Branch == UnitType::branch_t::NAVAL) { - return remove_navy(static_cast(group)); - } else { - OpenVic::utility::unreachable(); - } - } + bool remove_unit_instance_group(UnitInstanceGroup& group); bool setup(BuildingTypeManager const& building_type_manager); bool apply_history_to_province(ProvinceHistoryEntry const* entry); -- cgit v1.2.3-56-ga3b1