From 0d861669d9e1f5d487c810ae01be50f142790f1e Mon Sep 17 00:00:00 2001 From: wvpm <24685035+wvpm@users.noreply.github.com> Date: Sun, 22 Sep 2024 01:07:51 +0200 Subject: Implement rgo for new game Map province history rgo to production type for province instance output_goods back to ZERO_OR_ONE Link trade_goods in history to RGO instance for province. Other producer types as structs instead of classes Convert pops to equivalents & calculate rgo size Also convert pops when changing rgo Clean up Refactored RGO into part of ProvinceInstance ProductionType const& output_good Remove unused imports Clean up unused imports Restore constructor for ResourceGatheringOperation to initialise from savegame Move rgo size calculation to rgo Use terrain modifiers to calculate rgo size (placeholder code) Clean up Basic production & sales for rgo when initialising new game Use mutable pops Paychecks for owners, workers and slaves Clean up Simplify rgo instantiation Co-authored-by: Hop311 Simplify good_to_rgo_production_type assignment Co-authored-by: Hop311 Fix import Co-authored-by: Hop311 min(3, great_powers.size()) Co-authored-by: Hop311 Fix import Co-authored-by: Hop311 Apply comments Log errors and return result when applying history Cleanup --- src/openvic-simulation/map/MapInstance.cpp | 13 ++++++ src/openvic-simulation/map/MapInstance.hpp | 1 + src/openvic-simulation/map/Mapmode.cpp | 2 +- src/openvic-simulation/map/ProvinceInstance.cpp | 55 +++++++++++++++++++++++-- src/openvic-simulation/map/ProvinceInstance.hpp | 15 +++++-- src/openvic-simulation/map/State.cpp | 17 ++++---- src/openvic-simulation/map/State.hpp | 2 +- 7 files changed, 89 insertions(+), 16 deletions(-) (limited to 'src/openvic-simulation/map') diff --git a/src/openvic-simulation/map/MapInstance.cpp b/src/openvic-simulation/map/MapInstance.cpp index 47ae40e..4a4a1e5 100644 --- a/src/openvic-simulation/map/MapInstance.cpp +++ b/src/openvic-simulation/map/MapInstance.cpp @@ -93,6 +93,7 @@ bool MapInstance::apply_history_to_provinces( if (history_map != nullptr) { ProvinceHistoryEntry const* pop_history_entry = nullptr; + ProductionType const* rgo_production_type_nullable = nullptr; for (auto const& [entry_date, entry] : history_map->get_entries()) { if(entry_date > date) { @@ -101,6 +102,10 @@ bool MapInstance::apply_history_to_provinces( } } else { province.apply_history_to_province(*entry, country_manager); + std::optional const& rgo_production_type_nullable_optional = entry->get_rgo_production_type_nullable(); + if (rgo_production_type_nullable_optional.has_value()) { + rgo_production_type_nullable = rgo_production_type_nullable_optional.value(); + } } if (!entry->get_pops().empty()) { @@ -114,6 +119,8 @@ bool MapInstance::apply_history_to_provinces( province.add_pop_vec(pop_history_entry->get_pops()); province.setup_pop_test_values(issue_manager); } + + ret&=province.set_rgo_production_type_nullable(rgo_production_type_nullable); } } } @@ -153,3 +160,9 @@ void MapInstance::tick(Date today) { province.tick(today); } } + +void MapInstance::initialise_for_new_game(ModifierEffectCache const& modifier_effect_cache){ + for (ProvinceInstance& province : province_instances.get_items()) { + province.initialise_for_new_game(modifier_effect_cache); + } +} \ No newline at end of file diff --git a/src/openvic-simulation/map/MapInstance.hpp b/src/openvic-simulation/map/MapInstance.hpp index e7c623e..a580c55 100644 --- a/src/openvic-simulation/map/MapInstance.hpp +++ b/src/openvic-simulation/map/MapInstance.hpp @@ -55,5 +55,6 @@ namespace OpenVic { void update_modifier_sums(Date today, StaticModifierCache const& static_modifier_cache); void update_gamestate(Date today, DefineManager const& define_manager); void tick(Date today); + void initialise_for_new_game(ModifierEffectCache const& modifier_effect_cache); }; } diff --git a/src/openvic-simulation/map/Mapmode.cpp b/src/openvic-simulation/map/Mapmode.cpp index f951a05..88eb2eb 100644 --- a/src/openvic-simulation/map/Mapmode.cpp +++ b/src/openvic-simulation/map/Mapmode.cpp @@ -179,7 +179,7 @@ bool MapmodeManager::setup_mapmodes() { "mapmode_terrain_type", get_colour_mapmode(&ProvinceInstance::get_terrain_type) }, { - "mapmode_rgo", get_colour_mapmode(&ProvinceInstance::get_rgo) + "mapmode_rgo", get_colour_mapmode(&ProvinceInstance::get_rgo_good) }, { "mapmode_infrastructure", diff --git a/src/openvic-simulation/map/ProvinceInstance.cpp b/src/openvic-simulation/map/ProvinceInstance.cpp index cbb23bd..64a104b 100644 --- a/src/openvic-simulation/map/ProvinceInstance.cpp +++ b/src/openvic-simulation/map/ProvinceInstance.cpp @@ -1,6 +1,8 @@ #include "ProvinceInstance.hpp" #include "openvic-simulation/country/CountryInstance.hpp" +#include "openvic-simulation/economy/production/ProductionType.hpp" +#include "openvic-simulation/economy/production/ResourceGatheringOperation.hpp" #include "openvic-simulation/history/ProvinceHistory.hpp" #include "openvic-simulation/map/Crime.hpp" #include "openvic-simulation/map/ProvinceDefinition.hpp" @@ -10,6 +12,8 @@ #include "openvic-simulation/misc/Define.hpp" #include "openvic-simulation/modifier/StaticModifierCache.hpp" #include "openvic-simulation/politics/Ideology.hpp" +#include "openvic-simulation/pop/Pop.hpp" +#include "openvic-simulation/utility/Logger.hpp" using namespace OpenVic; @@ -29,7 +33,7 @@ ProvinceInstance::ProvinceInstance( event_modifiers {}, slave { false }, crime { nullptr }, - rgo { nullptr }, + rgo { pop_type_keys }, buildings { "buildings", false }, armies {}, navies {}, @@ -41,6 +45,25 @@ ProvinceInstance::ProvinceInstance( religion_distribution {}, max_supported_regiments { 0 } {} +GoodDefinition const* ProvinceInstance::get_rgo_good() const { + if (!rgo.is_valid()) { return nullptr; } + return &(rgo.get_production_type_nullable()->get_output_good()); +} +bool ProvinceInstance::set_rgo_production_type_nullable(ProductionType const* rgo_production_type_nullable) { + bool is_valid_operation = true; + if (rgo_production_type_nullable != nullptr) { + ProductionType const& rgo_production_type = *rgo_production_type_nullable; + if (rgo_production_type.get_template_type() != ProductionType::template_type_t::RGO) { + Logger::error("Tried setting province ", get_identifier(), " rgo to ", rgo_production_type.get_identifier(), " which is not of template_type RGO."); + is_valid_operation = false; + } + is_valid_operation&=convert_rgo_worker_pops_to_equivalent(rgo_production_type); + } + + rgo.set_production_type_nullable(rgo_production_type_nullable); + return is_valid_operation; +} + bool ProvinceInstance::set_owner(CountryInstance* new_owner) { bool ret = true; @@ -176,7 +199,7 @@ void ProvinceInstance::_update_pops(DefineManager const& define_manager) { average_consciousness += pop.get_consciousness(); average_militancy += pop.get_militancy(); - pop_type_distribution[pop.get_type()] += pop.get_size(); + pop_type_distribution[*pop.get_type()] += pop.get_size(); ideology_distribution += pop.get_ideologies(); culture_distribution[&pop.get_culture()] += pop.get_size(); religion_distribution[&pop.get_religion()] += pop.get_size(); @@ -311,6 +334,24 @@ std::vector ProvinceInstance::get_contributing_mo } } +bool ProvinceInstance::convert_rgo_worker_pops_to_equivalent(ProductionType const& production_type) { + bool is_valid_operation = true; + std::vector const& jobs = production_type.get_jobs(); + for(Pop& pop : pops) { + for(Job const& job : jobs) { + PopType const* const job_pop_type = job.get_pop_type(); + PopType const* old_pop_type = pop.get_type(); + if (job_pop_type != old_pop_type) { + PopType const* const equivalent = old_pop_type->get_equivalent(); + if (job_pop_type == equivalent) { + is_valid_operation&=pop.convert_to_equivalent(); + } + } + } + } + return is_valid_operation; +} + void ProvinceInstance::update_gamestate(Date today, DefineManager const& define_manager) { for (BuildingInstance& building : buildings.get_items()) { building.update_gamestate(today); @@ -405,7 +446,7 @@ bool ProvinceInstance::apply_history_to_province(ProvinceHistoryEntry const& ent ret &= remove_core(country_manager.get_country_instance_from_definition(*country)); } } - set_optional(rgo, entry.get_rgo()); + set_optional(life_rating, entry.get_life_rating()); set_optional(terrain_type, entry.get_terrain_type()); for (auto const& [building, level] : entry.get_province_buildings()) { @@ -425,8 +466,16 @@ bool ProvinceInstance::apply_history_to_province(ProvinceHistoryEntry const& ent return ret; } +void ProvinceInstance::initialise_for_new_game(ModifierEffectCache const& modifier_effect_cache) { + rgo.initialise_for_new_game(*this, modifier_effect_cache); +} + void ProvinceInstance::setup_pop_test_values(IssueManager const& issue_manager) { for (Pop& pop : pops) { pop.setup_pop_test_values(issue_manager); } } + +plf::colony& ProvinceInstance::get_mutable_pops() { + return pops; +} \ No newline at end of file diff --git a/src/openvic-simulation/map/ProvinceInstance.hpp b/src/openvic-simulation/map/ProvinceInstance.hpp index e7c0326..ba800a9 100644 --- a/src/openvic-simulation/map/ProvinceInstance.hpp +++ b/src/openvic-simulation/map/ProvinceInstance.hpp @@ -3,11 +3,12 @@ #include #include "openvic-simulation/economy/BuildingInstance.hpp" +#include "openvic-simulation/economy/production/ProductionType.hpp" +#include "openvic-simulation/economy/production/ResourceGatheringOperation.hpp" #include "openvic-simulation/military/UnitInstance.hpp" #include "openvic-simulation/military/UnitType.hpp" #include "openvic-simulation/modifier/ModifierSum.hpp" #include "openvic-simulation/pop/Pop.hpp" -#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp" #include "openvic-simulation/types/HasIdentifier.hpp" #include "openvic-simulation/types/OrderedContainers.hpp" @@ -81,8 +82,7 @@ namespace OpenVic { bool PROPERTY(slave); Crime const* PROPERTY_RW(crime); - // TODO - change this into a factory-like structure - GoodDefinition const* PROPERTY(rgo); + ResourceGatheringOperation PROPERTY(rgo); IdentifierRegistry IDENTIFIER_REGISTRY(building); ordered_set PROPERTY(armies); ordered_set PROPERTY(navies); @@ -95,7 +95,7 @@ namespace OpenVic { fixed_point_t PROPERTY(average_literacy); fixed_point_t PROPERTY(average_consciousness); fixed_point_t PROPERTY(average_militancy); - IndexedMap PROPERTY(pop_type_distribution); + IndexedMap PROPERTY(pop_type_distribution); IndexedMap PROPERTY(ideology_distribution); fixed_point_map_t PROPERTY(culture_distribution); fixed_point_map_t PROPERTY(religion_distribution); @@ -108,6 +108,7 @@ namespace OpenVic { void _add_pop(Pop&& pop); void _update_pops(DefineManager const& define_manager); + bool convert_rgo_worker_pops_to_equivalent(ProductionType const& production_type); public: ProvinceInstance(ProvinceInstance&&) = default; @@ -123,6 +124,9 @@ namespace OpenVic { return controller; } + GoodDefinition const* get_rgo_good() const; + bool set_rgo_production_type_nullable(ProductionType const* rgo_production_type_nullable); + bool set_owner(CountryInstance* new_owner); bool set_controller(CountryInstance* new_controller); bool add_core(CountryInstance& new_core); @@ -155,6 +159,9 @@ namespace OpenVic { bool setup(BuildingTypeManager const& building_type_manager); bool apply_history_to_province(ProvinceHistoryEntry const& entry, CountryInstanceManager& country_manager); + void initialise_for_new_game(ModifierEffectCache const& modifier_effect_cache); + void setup_pop_test_values(IssueManager const& issue_manager); + plf::colony& get_mutable_pops(); }; } diff --git a/src/openvic-simulation/map/State.cpp b/src/openvic-simulation/map/State.cpp index 619f97b..31ea05f 100644 --- a/src/openvic-simulation/map/State.cpp +++ b/src/openvic-simulation/map/State.cpp @@ -66,16 +66,19 @@ void State::update_gamestate() { const int32_t potential_workforce_in_state = 0; // sum of worker pops, regardless of employment const int32_t potential_employment_in_state = 0; // sum of (factory level * production method base_workforce_size) - fixed_point_t unclamped_score_per_factory = fixed_point_t::_0(); - - if (potential_employment_in_state > 0) { - unclamped_score_per_factory = (fixed_point_t { potential_workforce_in_state } / fixed_point_t::_100()).floor() * 400 / potential_employment_in_state; + fixed_point_t workforce_scalar; + constexpr fixed_point_t min_workforce_scalar = fixed_point_t::_0_20(); + constexpr fixed_point_t max_workforce_scalar = fixed_point_t::_4(); + if (potential_employment_in_state <= 0) { + workforce_scalar = min_workforce_scalar; } else { - industrial_power = total_factory_levels_in_state * std::clamp( - unclamped_score_per_factory, - fixed_point_t::_0_20(), fixed_point_t::_4() + workforce_scalar = std::clamp( + (fixed_point_t { potential_workforce_in_state } / 100).floor() * 400 / potential_employment_in_state, + min_workforce_scalar, max_workforce_scalar ); } + + industrial_power = total_factory_levels_in_state * workforce_scalar; } /* Whether two provinces in the same region should be grouped into the same state or not. diff --git a/src/openvic-simulation/map/State.hpp b/src/openvic-simulation/map/State.hpp index 596206a..a39eea6 100644 --- a/src/openvic-simulation/map/State.hpp +++ b/src/openvic-simulation/map/State.hpp @@ -29,7 +29,7 @@ namespace OpenVic { fixed_point_t PROPERTY(average_literacy); fixed_point_t PROPERTY(average_consciousness); fixed_point_t PROPERTY(average_militancy); - IndexedMap PROPERTY(pop_type_distribution); + IndexedMap PROPERTY(pop_type_distribution); fixed_point_t PROPERTY(industrial_power); -- cgit v1.2.3-56-ga3b1