Skip to content

Commit

Permalink
Calculate pop ideology distributions
Browse files Browse the repository at this point in the history
  • Loading branch information
Hop311 committed Dec 30, 2024
1 parent a691b6d commit e867291
Show file tree
Hide file tree
Showing 20 changed files with 640 additions and 119 deletions.
11 changes: 6 additions & 5 deletions src/openvic-simulation/InstanceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void InstanceManager::update_gamestate() {
Logger::info("Update: ", today);
update_modifier_sums();
// Update gamestate...
map_instance.update_gamestate(today, definition_manager.get_define_manager());
map_instance.update_gamestate(*this);
country_instance_manager.update_gamestate(*this);

gamestate_updated();
Expand Down Expand Up @@ -79,6 +79,7 @@ bool InstanceManager::setup() {
definition_manager.get_economy_manager().get_building_type_manager(),
market_instance,
definition_manager.get_modifier_manager().get_modifier_effect_cache(),
definition_manager.get_pop_manager().get_stratas(),
definition_manager.get_pop_manager().get_pop_types(),
definition_manager.get_politics_manager().get_ideology_manager().get_ideologies()
);
Expand Down Expand Up @@ -128,6 +129,8 @@ bool InstanceManager::load_bookmark(Bookmark const* new_bookmark) {

today = bookmark->get_date();

politics_instance_manager.setup_starting_ideologies();

bool ret = map_instance.apply_history_to_provinces(
definition_manager.get_history_manager().get_province_manager(), today,
country_instance_manager,
Expand All @@ -139,16 +142,14 @@ bool InstanceManager::load_bookmark(Bookmark const* new_bookmark) {

ret &= map_instance.get_state_manager().generate_states(
map_instance,
definition_manager.get_pop_manager().get_stratas(),
definition_manager.get_pop_manager().get_pop_types(),
definition_manager.get_politics_manager().get_ideology_manager().get_ideologies()
);

if (ret) {
update_modifier_sums();
map_instance.initialise_for_new_game(
today,
definition_manager.get_define_manager()
);
map_instance.initialise_for_new_game(*this);
market_instance.execute_orders();
}

Expand Down
37 changes: 29 additions & 8 deletions src/openvic-simulation/country/CountryInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ CountryInstance::CountryInstance(
total_population { 0 },
national_consciousness { 0 },
national_militancy { 0 },
population_by_strata { &strata_keys },
militancy_by_strata { &strata_keys },
life_needs_fulfilled_by_strata { &strata_keys },
everyday_needs_fulfilled_by_strata { &strata_keys },
luxury_needs_fulfilled_by_strata { &strata_keys },
pop_type_distribution { &pop_type_keys },
ideology_distribution { &ideology_keys },
issue_distribution {},
Expand Down Expand Up @@ -220,14 +225,6 @@ bool CountryInstance::is_neighbour(CountryInstance const& country) const {
return neighbouring_countries.contains(&country);
}

fixed_point_t CountryInstance::get_pop_type_proportion(PopType const& pop_type) const {
return pop_type_distribution[pop_type];
}

fixed_point_t CountryInstance::get_ideology_support(Ideology const& ideology) const {
return ideology_distribution[ideology];
}

fixed_point_t CountryInstance::get_issue_support(Issue const& issue) const {
const decltype(issue_distribution)::const_iterator it = issue_distribution.find(&issue);

Expand Down Expand Up @@ -915,6 +912,13 @@ void CountryInstance::_update_population() {
national_literacy = 0;
national_consciousness = 0;
national_militancy = 0;

population_by_strata.clear();
militancy_by_strata.clear();
life_needs_fulfilled_by_strata.clear();
everyday_needs_fulfilled_by_strata.clear();
luxury_needs_fulfilled_by_strata.clear();

pop_type_distribution.clear();
ideology_distribution.clear();
issue_distribution.clear();
Expand All @@ -931,6 +935,18 @@ void CountryInstance::_update_population() {
national_consciousness += state->get_average_consciousness() * state_population;
national_militancy += state->get_average_militancy() * state_population;

population_by_strata += state->get_population_by_strata();
militancy_by_strata.mul_add(state->get_militancy_by_strata(), state->get_population_by_strata());
life_needs_fulfilled_by_strata.mul_add(
state->get_life_needs_fulfilled_by_strata(), state->get_population_by_strata()
);
everyday_needs_fulfilled_by_strata.mul_add(
state->get_everyday_needs_fulfilled_by_strata(), state->get_population_by_strata()
);
luxury_needs_fulfilled_by_strata.mul_add(
state->get_luxury_needs_fulfilled_by_strata(), state->get_population_by_strata()
);

pop_type_distribution += state->get_pop_type_distribution();
ideology_distribution += state->get_ideology_distribution();
issue_distribution += state->get_issue_distribution();
Expand All @@ -943,6 +959,11 @@ void CountryInstance::_update_population() {
national_literacy /= total_population;
national_consciousness /= total_population;
national_militancy /= total_population;

militancy_by_strata /= population_by_strata;
life_needs_fulfilled_by_strata /= population_by_strata;
everyday_needs_fulfilled_by_strata /= population_by_strata;
luxury_needs_fulfilled_by_strata /= population_by_strata;
}

// TODO - update national focus capacity
Expand Down
31 changes: 29 additions & 2 deletions src/openvic-simulation/country/CountryInstance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "openvic-simulation/military/Leader.hpp"
#include "openvic-simulation/military/UnitInstanceGroup.hpp"
#include "openvic-simulation/modifier/ModifierSum.hpp"
#include "openvic-simulation/politics/Ideology.hpp"
#include "openvic-simulation/politics/Rule.hpp"
#include "openvic-simulation/pop/PopType.hpp"
#include "openvic-simulation/types/Date.hpp"
Expand Down Expand Up @@ -161,6 +162,13 @@ namespace OpenVic {
// TODO - population change over last 30 days
fixed_point_t PROPERTY(national_consciousness);
fixed_point_t PROPERTY(national_militancy);

IndexedMap<Strata, pop_size_t> PROPERTY(population_by_strata);
IndexedMap<Strata, fixed_point_t> PROPERTY(militancy_by_strata);
IndexedMap<Strata, fixed_point_t> PROPERTY(life_needs_fulfilled_by_strata);
IndexedMap<Strata, fixed_point_t> PROPERTY(everyday_needs_fulfilled_by_strata);
IndexedMap<Strata, fixed_point_t> PROPERTY(luxury_needs_fulfilled_by_strata);

IndexedMap<PopType, pop_size_t> PROPERTY(pop_type_distribution);
IndexedMap<Ideology, fixed_point_t> PROPERTY(ideology_distribution);
fixed_point_map_t<Issue const*> PROPERTY(issue_distribution);
Expand Down Expand Up @@ -244,12 +252,31 @@ namespace OpenVic {

// The values returned by these functions are scaled by population size, so they must be divided by population size
// to get the support as a proportion of 1.0
fixed_point_t get_pop_type_proportion(PopType const& pop_type) const;
fixed_point_t get_ideology_support(Ideology const& ideology) const;
constexpr fixed_point_t get_pop_type_proportion(PopType const& pop_type) const {
return pop_type_distribution[pop_type];
}
constexpr fixed_point_t get_ideology_support(Ideology const& ideology) const {
return ideology_distribution[ideology];
}
fixed_point_t get_issue_support(Issue const& issue) const;
fixed_point_t get_party_support(CountryParty const& party) const;
fixed_point_t get_culture_proportion(Culture const& culture) const;
fixed_point_t get_religion_proportion(Religion const& religion) const;
constexpr pop_size_t get_strata_population(Strata const& strata) const {
return population_by_strata[strata];
}
constexpr fixed_point_t get_strata_militancy(Strata const& strata) const {
return militancy_by_strata[strata];
}
constexpr fixed_point_t get_strata_life_needs_fulfilled(Strata const& strata) const {
return life_needs_fulfilled_by_strata[strata];
}
constexpr fixed_point_t get_strata_everyday_needs_fulfilled(Strata const& strata) const {
return everyday_needs_fulfilled_by_strata[strata];
}
constexpr fixed_point_t get_strata_luxury_needs_fulfilled(Strata const& strata) const {
return luxury_needs_fulfilled_by_strata[strata];
}

bool add_owned_province(ProvinceInstance& new_province);
bool remove_owned_province(ProvinceInstance const& province_to_remove);
Expand Down
17 changes: 10 additions & 7 deletions src/openvic-simulation/map/MapInstance.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "MapInstance.hpp"

#include "openvic-simulation/history/ProvinceHistory.hpp"
#include "openvic-simulation/InstanceManager.hpp"
#include "openvic-simulation/map/MapDefinition.hpp"
#include "openvic-simulation/utility/Logger.hpp"

Expand Down Expand Up @@ -45,6 +46,7 @@ bool MapInstance::setup(
BuildingTypeManager const& building_type_manager,
MarketInstance& market_instance,
ModifierEffectCache const& modifier_effect_cache,
decltype(ProvinceInstance::population_by_strata)::keys_type const& strata_keys,
decltype(ProvinceInstance::pop_type_distribution)::keys_type const& pop_type_keys,
decltype(ProvinceInstance::ideology_distribution)::keys_type const& ideology_keys
) {
Expand All @@ -66,6 +68,7 @@ bool MapInstance::setup(
market_instance,
modifier_effect_cache,
province,
strata_keys,
pop_type_keys,
ideology_keys
});
Expand Down Expand Up @@ -142,12 +145,12 @@ void MapInstance::update_modifier_sums(const Date today, StaticModifierCache con
}
}

void MapInstance::update_gamestate(const Date today, DefineManager const& define_manager) {
void MapInstance::update_gamestate(InstanceManager const& instance_manager) {
highest_province_population = 0;
total_map_population = 0;

for (ProvinceInstance& province : province_instances.get_items()) {
province.update_gamestate(today, define_manager);
province.update_gamestate(instance_manager);

// Update population stats
const pop_size_t province_population = province.get_total_population();
Expand All @@ -166,11 +169,11 @@ void MapInstance::map_tick(const Date today) {
}
}

void MapInstance::initialise_for_new_game(
const Date today,
DefineManager const& define_manager
) {
update_gamestate(today, define_manager);
void MapInstance::initialise_for_new_game(InstanceManager const& instance_manager) {
update_gamestate(instance_manager);

const Date today = instance_manager.get_today();

for (ProvinceInstance& province : province_instances.get_items()) {
province.initialise_rgo();
province.province_tick(today);
Expand Down
5 changes: 3 additions & 2 deletions src/openvic-simulation/map/MapInstance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ namespace OpenVic {
BuildingTypeManager const& building_type_manager,
MarketInstance& market_instance,
ModifierEffectCache const& modifier_effect_cache,
decltype(ProvinceInstance::population_by_strata)::keys_type const& strata_keys,
decltype(ProvinceInstance::pop_type_distribution)::keys_type const& pop_type_keys,
decltype(ProvinceInstance::ideology_distribution)::keys_type const& ideology_keys
);
Expand All @@ -56,8 +57,8 @@ 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 update_gamestate(InstanceManager const& instance_manager);
void map_tick(const Date today);
void initialise_for_new_game(const Date today, DefineManager const& define_manager);
void initialise_for_new_game(InstanceManager const& instance_manager);
};
}
Loading

0 comments on commit e867291

Please sign in to comment.