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/country/CountryInstance.cpp | 106 ++++++++------------- src/openvic-simulation/country/CountryInstance.hpp | 19 ++-- 2 files changed, 53 insertions(+), 72 deletions(-) (limited to 'src/openvic-simulation/country') diff --git a/src/openvic-simulation/country/CountryInstance.cpp b/src/openvic-simulation/country/CountryInstance.cpp index 3e21bab..8d2955b 100644 --- a/src/openvic-simulation/country/CountryInstance.cpp +++ b/src/openvic-simulation/country/CountryInstance.cpp @@ -88,89 +88,63 @@ bool CountryInstance::remove_reform(Reform const* reform_to_remove) { return true; } -void CountryInstance::add_general(General&& new_general) { - generals.emplace(std::move(new_general)); -} - -bool CountryInstance::remove_general(General const* general_to_remove) { - const auto it = generals.get_iterator(general_to_remove); - if (it != generals.end()) { - generals.erase(it); +template +bool CountryInstance::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 ", Branch == UnitType::branch_t::LAND ? "army" : "navy", " ", + group.get_name(), " to country ", get_identifier() + ); + return false; } - - Logger::error( - "Trying to remove non-existent general ", general_to_remove != nullptr ? general_to_remove->get_name() : "NULL", - " from country ", get_identifier() - ); - return false; } -void CountryInstance::add_admiral(Admiral&& new_admiral) { - admirals.emplace(std::move(new_admiral)); -} - -bool CountryInstance::remove_admiral(Admiral const* admiral_to_remove) { - const auto it = admirals.get_iterator(admiral_to_remove); - if (it != admirals.end()) { - admirals.erase(it); +template +bool CountryInstance::remove_unit_instance_group(UnitInstanceGroup& group) { + if (get_unit_instance_groups().erase(static_cast*>(&group)) > 0) { return true; - } - - Logger::error( - "Trying to remove non-existent admiral ", admiral_to_remove != nullptr ? admiral_to_remove->get_name() : "NULL", - " from country ", get_identifier() - ); - return false; -} - -bool CountryInstance::add_leader(LeaderBase const& new_leader) { - using enum UnitType::branch_t; - - switch (new_leader.get_branch()) { - case LAND: - add_general({ new_leader }); - return true; - - case NAVAL: - add_admiral({ new_leader }); - return true; - - default: + } else { Logger::error( - "Trying to add leader ", new_leader.get_name(), " to country ", get_identifier(), " with invalid branch ", - static_cast(new_leader.get_branch()) + "Trying to remove non-existent ", Branch == UnitType::branch_t::LAND ? "army" : "navy", " ", + group.get_name(), " from country ", get_identifier() ); return false; } } -bool CountryInstance::remove_leader(LeaderBase const* leader_to_remove) { - if (leader_to_remove == nullptr) { - Logger::error("Trying to remvoe null leader from country ", get_identifier()); - return false; - } - - using enum UnitType::branch_t; +template bool CountryInstance::add_unit_instance_group(UnitInstanceGroup&); +template bool CountryInstance::add_unit_instance_group(UnitInstanceGroup&); +template bool CountryInstance::remove_unit_instance_group(UnitInstanceGroup&); +template bool CountryInstance::remove_unit_instance_group(UnitInstanceGroup&); - switch (leader_to_remove->get_branch()) { - case LAND: - remove_general(static_cast(leader_to_remove)); - return true; +template +void CountryInstance::add_leader(LeaderBranched&& leader) { + get_leaders().emplace(std::move(leader)); +} - case NAVAL: - remove_admiral(static_cast(leader_to_remove)); +template +bool CountryInstance::remove_leader(LeaderBranched const* leader) { + plf::colony>& leaders = get_leaders(); + const auto it = leaders.get_iterator(leader); + if (it != leaders.end()) { + leaders.erase(it); return true; - - default: - Logger::error( - "Trying to add leader ", leader_to_remove->get_name(), " to country ", get_identifier(), " with invalid branch ", - static_cast(leader_to_remove->get_branch()) - ); - return false; } + + Logger::error( + "Trying to remove non-existent ", Branch == UnitType::branch_t::LAND ? "general" : "admiral", " ", + leader != nullptr ? leader->get_name() : "NULL", " from country ", get_identifier() + ); + return false; } +template void CountryInstance::add_leader(LeaderBranched&&); +template void CountryInstance::add_leader(LeaderBranched&&); +template bool CountryInstance::remove_leader(LeaderBranched const*); +template bool CountryInstance::remove_leader(LeaderBranched const*); + bool CountryInstance::apply_history_to_country(CountryHistoryEntry const* entry) { if (entry == nullptr) { Logger::error("Trying to apply null country history to ", get_identifier()); diff --git a/src/openvic-simulation/country/CountryInstance.hpp b/src/openvic-simulation/country/CountryInstance.hpp index 885a5fd..1eaf398 100644 --- a/src/openvic-simulation/country/CountryInstance.hpp +++ b/src/openvic-simulation/country/CountryInstance.hpp @@ -54,6 +54,11 @@ namespace OpenVic { plf::colony PROPERTY(generals); plf::colony PROPERTY(admirals); + ordered_set PROPERTY(armies); + ordered_set PROPERTY(navies); + + UNIT_BRANCHED_GETTER(get_unit_instance_groups, armies, navies); + UNIT_BRANCHED_GETTER(get_leaders, generals, admirals); CountryInstance( CountryDefinition const* new_country_definition, decltype(technologies)::keys_t const& technology_keys, @@ -70,13 +75,15 @@ namespace OpenVic { bool add_reform(Reform const* new_reform); bool remove_reform(Reform const* reform_to_remove); - void add_general(General&& new_general); - bool remove_general(General const* general_to_remove); - void add_admiral(Admiral&& new_admiral); - bool remove_admiral(Admiral const* admiral_to_remove); + template + bool add_unit_instance_group(UnitInstanceGroup& group); + template + bool remove_unit_instance_group(UnitInstanceGroup& group); - bool add_leader(LeaderBase const& new_leader); - bool remove_leader(LeaderBase const* leader_to_remove); + template + void add_leader(LeaderBranched&& leader); + template + bool remove_leader(LeaderBranched const* leader); bool apply_history_to_country(CountryHistoryEntry const* entry); }; -- cgit v1.2.3-56-ga3b1