Skip to content

Commit

Permalink
ModifierEffectCache & location to fields
Browse files Browse the repository at this point in the history
  • Loading branch information
wvpm committed Nov 13, 2024
1 parent 55eb6aa commit e91bd6c
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 53 deletions.
6 changes: 3 additions & 3 deletions src/openvic-simulation/InstanceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void InstanceManager::tick() {
Logger::info("Tick: ", today);

// Tick...
map_instance.map_tick(today, definition_manager.get_modifier_manager().get_modifier_effect_cache());
map_instance.map_tick(today);

set_gamestate_needs_update();
}
Expand All @@ -75,6 +75,7 @@ bool InstanceManager::setup() {
bool ret = good_instance_manager.setup(definition_manager.get_economy_manager().get_good_definition_manager());
ret &= map_instance.setup(
definition_manager.get_economy_manager().get_building_type_manager(),
definition_manager.get_modifier_manager().get_modifier_effect_cache(),
definition_manager.get_pop_manager().get_pop_types(),
definition_manager.get_politics_manager().get_ideology_manager().get_ideologies()
);
Expand Down Expand Up @@ -142,8 +143,7 @@ bool InstanceManager::load_bookmark(Bookmark const* new_bookmark) {
update_modifier_sums();
map_instance.initialise_for_new_game(
today,
definition_manager.get_define_manager(),
definition_manager.get_modifier_manager().get_modifier_effect_cache()
definition_manager.get_define_manager()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
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,
fixed_point_t new_output_quantity_yesterday,
fixed_point_t new_unsold_quantity_yesterday,
std::vector<Employee>&& 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 },
Expand All @@ -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<Job> const& jobs = production_type.get_jobs();
IndexedMap<PopType, Pop::pop_size_t> const& province_pop_type_distribution = location.get_pop_type_distribution();
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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<Pop*> const* owner_pops_cache = nullptr;
Expand All @@ -128,31 +143,29 @@ 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,
total_owner_count_in_state_cache
);
}

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) {
employees.clear();
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; }
Expand Down Expand Up @@ -187,19 +200,18 @@ void ResourceGatheringOperation::hire(ProvinceInstance& location, Pop::pop_size_
}

fixed_point_t ResourceGatheringOperation::produce(
ProvinceInstance& location,
std::vector<Pop*> 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();
}

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();
Expand Down Expand Up @@ -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<Pop*> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -23,16 +27,13 @@ namespace OpenVic {
fixed_point_t PROPERTY(total_employee_income_cache);
IndexedMap<PopType, Pop::pop_size_t> 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<Pop*> 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<Pop*> const* const owner_pops_cache,
Expand All @@ -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,
Expand All @@ -49,11 +51,17 @@ namespace OpenVic {
std::vector<Employee>&& 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();
};
}
19 changes: 12 additions & 7 deletions src/openvic-simulation/map/MapInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ ProvinceDefinition::index_t MapInstance::get_selected_province_index() const {

bool MapInstance::setup(
BuildingTypeManager const& building_type_manager,
ModifierEffectCache const& modifier_effect_cache,
decltype(ProvinceInstance::pop_type_distribution)::keys_t const& pop_type_keys,
decltype(ProvinceInstance::ideology_distribution)::keys_t const& ideology_keys
) {
Expand All @@ -60,7 +61,12 @@ bool MapInstance::setup(
province_instances.reserve(map_definition.get_province_definition_count());

for (ProvinceDefinition const& province : map_definition.get_province_definitions()) {
ret &= province_instances.add_item({ province, pop_type_keys, ideology_keys });
ret &= province_instances.add_item({
modifier_effect_cache,
province,
pop_type_keys,
ideology_keys
});
}

province_instances.lock();
Expand Down Expand Up @@ -152,20 +158,19 @@ void MapInstance::update_gamestate(const Date today, DefineManager const& define
state_manager.update_gamestate();
}

void MapInstance::map_tick(const Date today, ModifierEffectCache const& modifier_effect_cache) {
void MapInstance::map_tick(const Date today) {
for (ProvinceInstance& province : province_instances.get_items()) {
province.province_tick(today, modifier_effect_cache);
province.province_tick(today);
}
}

void MapInstance::initialise_for_new_game(
const Date today,
DefineManager const& define_manager,
ModifierEffectCache const& modifier_effect_cache
DefineManager const& define_manager
) {
update_gamestate(today, define_manager);
for (ProvinceInstance& province : province_instances.get_items()) {
province.initialise_rgo(modifier_effect_cache);
province.province_tick(today, modifier_effect_cache);
province.initialise_rgo();
province.province_tick(today);
}
}
5 changes: 3 additions & 2 deletions src/openvic-simulation/map/MapInstance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace OpenVic {

bool setup(
BuildingTypeManager const& building_type_manager,
ModifierEffectCache const& modifier_effect_cache,
decltype(ProvinceInstance::pop_type_distribution)::keys_t const& pop_type_keys,
decltype(ProvinceInstance::ideology_distribution)::keys_t const& ideology_keys
);
Expand All @@ -54,7 +55,7 @@ namespace OpenVic {

void update_modifier_sums(const Date today, StaticModifierCache const& static_modifier_cache);
void update_gamestate(const Date today, DefineManager const& define_manager);
void map_tick(const Date today, ModifierEffectCache const& modifier_effect_cache);
void initialise_for_new_game(const Date today, DefineManager const& define_manager, ModifierEffectCache const& modifier_effect_cache);
void map_tick(const Date today);
void initialise_for_new_game(const Date today, DefineManager const& define_manager);
};
}
19 changes: 10 additions & 9 deletions src/openvic-simulation/map/ProvinceInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
using namespace OpenVic;

ProvinceInstance::ProvinceInstance(
ProvinceDefinition const& new_province_definition, decltype(pop_type_distribution)::keys_t const& pop_type_keys,
ModifierEffectCache const& new_modifier_effect_cache,
ProvinceDefinition const& new_province_definition,
decltype(pop_type_distribution)::keys_t const& pop_type_keys,
decltype(ideology_distribution)::keys_t const& ideology_keys
) : HasIdentifierAndColour { new_province_definition },
province_definition { new_province_definition },
Expand All @@ -33,7 +35,7 @@ ProvinceInstance::ProvinceInstance(
event_modifiers {},
slave { false },
crime { nullptr },
rgo { pop_type_keys },
rgo { new_modifier_effect_cache, pop_type_keys },
buildings { "buildings", false },
armies {},
navies {},
Expand Down Expand Up @@ -368,14 +370,11 @@ void ProvinceInstance::update_gamestate(const Date today, DefineManager const& d
_update_pops(define_manager);
}

void ProvinceInstance::province_tick(const Date today, ModifierEffectCache const& modifier_effect_cache) {
void ProvinceInstance::province_tick(const Date today) {
for (BuildingInstance& building : buildings.get_items()) {
building.tick(today);
}
rgo.rgo_tick(
*this,
modifier_effect_cache
);
rgo.rgo_tick();
}

template<UnitType::branch_t Branch>
Expand Down Expand Up @@ -415,6 +414,8 @@ bool ProvinceInstance::setup(BuildingTypeManager const& building_type_manager) {
return false;
}

rgo.setup_location_ptr(*this);

bool ret = true;

if (!province_definition.is_water()) {
Expand Down Expand Up @@ -479,8 +480,8 @@ bool ProvinceInstance::apply_history_to_province(ProvinceHistoryEntry const& ent
return ret;
}

void ProvinceInstance::initialise_rgo(ModifierEffectCache const& modifier_effect_cache) {
rgo.initialise_rgo_size_multiplier(*this, modifier_effect_cache);
void ProvinceInstance::initialise_rgo() {
rgo.initialise_rgo_size_multiplier();
}

void ProvinceInstance::setup_pop_test_values(IssueManager const& issue_manager) {
Expand Down
Loading

0 comments on commit e91bd6c

Please sign in to comment.