From e91bd6c3322a73c3ac6707329e81b6f2edc0b8be Mon Sep 17 00:00:00 2001 From: wvpm <24685035+wvpm@users.noreply.github.com> Date: Sun, 3 Nov 2024 22:23:39 +0100 Subject: ModifierEffectCache & location to fields --- .../production/ResourceGatheringOperation.cpp | 53 ++++++++++++++-------- .../production/ResourceGatheringOperation.hpp | 26 +++++++---- 2 files changed, 50 insertions(+), 29 deletions(-) (limited to 'src/openvic-simulation/economy/production') diff --git a/src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp b/src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp index 7f030ab..8db74c1 100644 --- a/src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp +++ b/src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp @@ -14,6 +14,7 @@ using namespace OpenVic; ResourceGatheringOperation::ResourceGatheringOperation( + ModifierEffectCache const& new_modifier_effect_cache, ProductionType const* new_production_type_nullable, fixed_point_t new_size_multiplier, fixed_point_t new_revenue_yesterday, @@ -21,7 +22,9 @@ ResourceGatheringOperation::ResourceGatheringOperation( fixed_point_t new_unsold_quantity_yesterday, std::vector&& new_employees, decltype(employee_count_per_type_cache)::keys_t const& pop_type_keys -) : production_type_nullable { new_production_type_nullable }, +) : modifier_effect_cache { new_modifier_effect_cache }, + location_ptr { nullptr }, + production_type_nullable { new_production_type_nullable }, revenue_yesterday { new_revenue_yesterday }, output_quantity_yesterday { new_output_quantity_yesterday }, unsold_quantity_yesterday { new_unsold_quantity_yesterday }, @@ -35,22 +38,32 @@ ResourceGatheringOperation::ResourceGatheringOperation( employee_count_per_type_cache { &pop_type_keys } { } -ResourceGatheringOperation::ResourceGatheringOperation(decltype(employee_count_per_type_cache)::keys_t const& pop_type_keys) : ResourceGatheringOperation { +ResourceGatheringOperation::ResourceGatheringOperation( + ModifierEffectCache const& new_modifier_effect_cache, + decltype(employee_count_per_type_cache)::keys_t const& pop_type_keys +) : ResourceGatheringOperation { + new_modifier_effect_cache, nullptr, fixed_point_t::_0(), fixed_point_t::_0(), fixed_point_t::_0(), fixed_point_t::_0(), {}, pop_type_keys } {} -void ResourceGatheringOperation::initialise_rgo_size_multiplier( - ProvinceInstance& location, - ModifierEffectCache const& modifier_effect_cache -) { +void ResourceGatheringOperation::setup_location_ptr(ProvinceInstance& location) { + if (location_ptr != nullptr) { + Logger::error("RGO already has a location_ptr pointing to province ", location_ptr->get_identifier()); + } + + location_ptr = &location; +} + +void ResourceGatheringOperation::initialise_rgo_size_multiplier() { if (production_type_nullable == nullptr) { size_multiplier = fixed_point_t::_0(); max_employee_count_cache = fixed_point_t::_0(); return; } + ProvinceInstance& location = *location_ptr; ProductionType const& production_type = *production_type_nullable; std::vector const& jobs = production_type.get_jobs(); IndexedMap const& province_pop_type_distribution = location.get_pop_type_distribution(); @@ -75,14 +88,15 @@ void ResourceGatheringOperation::initialise_rgo_size_multiplier( size_multiplier = ((total_worker_count_in_province / (base_size_modifier * base_workforce_size)).ceil() * fixed_point_t::_1_50()).floor(); } - const fixed_point_t size_modifier = calculate_size_modifier(location, modifier_effect_cache); + const fixed_point_t size_modifier = calculate_size_modifier(); max_employee_count_cache = (size_modifier * size_multiplier * base_workforce_size).floor(); } -fixed_point_t ResourceGatheringOperation::calculate_size_modifier(ProvinceInstance const& location, ModifierEffectCache const& modifier_effect_cache) const { +fixed_point_t ResourceGatheringOperation::calculate_size_modifier() const { if (production_type_nullable == nullptr) { return fixed_point_t::_1(); } + ProvinceInstance& location = *location_ptr; ProductionType const& production_type = *production_type_nullable; @@ -100,7 +114,8 @@ fixed_point_t ResourceGatheringOperation::calculate_size_modifier(ProvinceInstan return size_modifier > fixed_point_t::_0() ? size_modifier : fixed_point_t::_0(); } -void ResourceGatheringOperation::rgo_tick(ProvinceInstance& location, ModifierEffectCache const& modifier_effect_cache) { +void ResourceGatheringOperation::rgo_tick() { + ProvinceInstance& location = *location_ptr; if (production_type_nullable == nullptr | location.get_owner() == nullptr) { output_quantity_yesterday = 0; revenue_yesterday = 0; @@ -116,7 +131,7 @@ void ResourceGatheringOperation::rgo_tick(ProvinceInstance& location, ModifierEf total_worker_count_in_province += province_pop_type_distribution[*job.get_pop_type()]; } - hire(location, total_worker_count_in_province); + hire(total_worker_count_in_province); Pop::pop_size_t total_owner_count_in_state_cache = 0; std::vector const* owner_pops_cache = nullptr; @@ -128,16 +143,13 @@ void ResourceGatheringOperation::rgo_tick(ProvinceInstance& location, ModifierEf } output_quantity_yesterday = produce( - location, owner_pops_cache, - total_owner_count_in_state_cache, - modifier_effect_cache + total_owner_count_in_state_cache ); revenue_yesterday = output_quantity_yesterday * production_type.get_output_good().get_base_price(); //TODO sell on market pay_employees( - location, revenue_yesterday, total_worker_count_in_province, owner_pops_cache, @@ -145,7 +157,7 @@ void ResourceGatheringOperation::rgo_tick(ProvinceInstance& location, ModifierEf ); } -void ResourceGatheringOperation::hire(ProvinceInstance& location, Pop::pop_size_t available_worker_count) { +void ResourceGatheringOperation::hire(const Pop::pop_size_t available_worker_count) { total_employees_count_cache = 0; total_paid_employees_count_cache=0; if (production_type_nullable == nullptr) { @@ -153,6 +165,7 @@ void ResourceGatheringOperation::hire(ProvinceInstance& location, Pop::pop_size_ employee_count_per_type_cache.fill(fixed_point_t::_0()); return; } + ProvinceInstance& location = *location_ptr; ProductionType const& production_type = *production_type_nullable; if (max_employee_count_cache <= 0) { return; } @@ -187,12 +200,10 @@ void ResourceGatheringOperation::hire(ProvinceInstance& location, Pop::pop_size_ } fixed_point_t ResourceGatheringOperation::produce( - ProvinceInstance& location, std::vector const* const owner_pops_cache, - const Pop::pop_size_t total_owner_count_in_state_cache, - ModifierEffectCache const& modifier_effect_cache + const Pop::pop_size_t total_owner_count_in_state_cache ) { - const fixed_point_t size_modifier = calculate_size_modifier(location, modifier_effect_cache); + const fixed_point_t size_modifier = calculate_size_modifier(); if (size_modifier == fixed_point_t::_0()){ return fixed_point_t::_0(); } @@ -200,6 +211,7 @@ fixed_point_t ResourceGatheringOperation::produce( if (production_type_nullable == nullptr || max_employee_count_cache <= 0) { return fixed_point_t::_0(); } + ProvinceInstance& location = *location_ptr; ProductionType const& production_type = *production_type_nullable; fixed_point_t throughput_multiplier = fixed_point_t::_1(); @@ -290,12 +302,13 @@ fixed_point_t ResourceGatheringOperation::produce( } void ResourceGatheringOperation::pay_employees( - ProvinceInstance& location, const fixed_point_t revenue, const Pop::pop_size_t total_worker_count_in_province, std::vector const* const owner_pops_cache, const Pop::pop_size_t total_owner_count_in_state_cache ) { + ProvinceInstance& location = *location_ptr; + total_owner_income_cache = 0; total_employee_income_cache = 0; if (revenue <= 0 || total_worker_count_in_province <= 0) { diff --git a/src/openvic-simulation/economy/production/ResourceGatheringOperation.hpp b/src/openvic-simulation/economy/production/ResourceGatheringOperation.hpp index 5079603..878041e 100644 --- a/src/openvic-simulation/economy/production/ResourceGatheringOperation.hpp +++ b/src/openvic-simulation/economy/production/ResourceGatheringOperation.hpp @@ -8,8 +8,12 @@ #include "openvic-simulation/utility/Getters.hpp" namespace OpenVic { + struct ProvinceInstance; + struct ResourceGatheringOperation { private: + ModifierEffectCache const& modifier_effect_cache; + ProvinceInstance* location_ptr; ProductionType const* PROPERTY_RW(production_type_nullable); fixed_point_t PROPERTY(revenue_yesterday); fixed_point_t PROPERTY(output_quantity_yesterday); @@ -23,16 +27,13 @@ namespace OpenVic { fixed_point_t PROPERTY(total_employee_income_cache); IndexedMap PROPERTY(employee_count_per_type_cache); - fixed_point_t calculate_size_modifier(ProvinceInstance const& location, ModifierEffectCache const& modifier_effect_cache) const; - void hire(ProvinceInstance& location, const Pop::pop_size_t available_worker_count); + fixed_point_t calculate_size_modifier() const; + void hire(const Pop::pop_size_t available_worker_count); fixed_point_t produce( - ProvinceInstance& location, std::vector const* const owner_pops_cache, - const Pop::pop_size_t total_owner_count_in_state_cache, - ModifierEffectCache const& modifier_effect_cache + const Pop::pop_size_t total_owner_count_in_state_cache ); void pay_employees( - ProvinceInstance& location, const fixed_point_t revenue, const Pop::pop_size_t total_worker_count_in_province, std::vector const* const owner_pops_cache, @@ -41,6 +42,7 @@ namespace OpenVic { public: ResourceGatheringOperation( + ModifierEffectCache const& new_modifier_effect_cache, ProductionType const* new_production_type_nullable, fixed_point_t new_size_multiplier, fixed_point_t new_revenue_yesterday, @@ -49,11 +51,17 @@ namespace OpenVic { std::vector&& new_employees, decltype(employee_count_per_type_cache)::keys_t const& pop_type_keys ); - ResourceGatheringOperation(decltype(employee_count_per_type_cache)::keys_t const& pop_type_keys); + + ResourceGatheringOperation( + ModifierEffectCache const& new_modifier_effect_cache, + decltype(employee_count_per_type_cache)::keys_t const& pop_type_keys + ); + constexpr bool is_valid() const { return production_type_nullable != nullptr; } - void initialise_rgo_size_multiplier(ProvinceInstance& location, ModifierEffectCache const& modifier_effect_cache); - void rgo_tick(ProvinceInstance& location, ModifierEffectCache const& modifier_effect_cache); + void setup_location_ptr(ProvinceInstance& location); + void initialise_rgo_size_multiplier(); + void rgo_tick(); }; } -- cgit v1.2.3-56-ga3b1