Skip to content

Commit 896f9ab

Browse files
authored
Merge pull request #519 from OpenVicProject/pops_aggregate
Pops aggregate
2 parents 4a1efaf + b6dfc98 commit 896f9ab

File tree

12 files changed

+387
-626
lines changed

12 files changed

+387
-626
lines changed

src/openvic-simulation/InstanceManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,15 @@ bool InstanceManager::setup() {
161161
definition_manager.get_economy_manager().get_building_type_manager().get_building_types(),
162162
definition_manager.get_research_manager().get_technology_manager().get_technologies(),
163163
definition_manager.get_research_manager().get_invention_manager().get_inventions(),
164-
definition_manager.get_politics_manager().get_ideology_manager().get_ideologies(),
165164
definition_manager.get_politics_manager().get_issue_manager().get_reform_groups(),
166165
definition_manager.get_politics_manager().get_government_type_manager().get_government_types(),
167166
definition_manager.get_crime_manager().get_crime_modifiers(),
168-
definition_manager.get_pop_manager().get_pop_types(),
169167
good_instance_manager.get_good_instances(),
170168
definition_manager.get_military_manager().get_unit_type_manager().get_regiment_types(),
171169
definition_manager.get_military_manager().get_unit_type_manager().get_ship_types(),
172170
definition_manager.get_pop_manager().get_stratas(),
171+
definition_manager.get_pop_manager().get_pop_types(),
172+
definition_manager.get_politics_manager().get_ideology_manager().get_ideologies(),
173173
game_rules_manager,
174174
country_relation_manager,
175175
good_instance_manager,

src/openvic-simulation/country/CountryInstance.cpp

Lines changed: 21 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
#include "openvic-simulation/misc/GameRulesManager.hpp"
1616
#include "openvic-simulation/modifier/ModifierEffectCache.hpp"
1717
#include "openvic-simulation/modifier/StaticModifierCache.hpp"
18-
#include "openvic-simulation/politics/Ideology.hpp"
1918
#include "openvic-simulation/pop/Pop.hpp"
2019
#include "openvic-simulation/pop/PopType.hpp"
2120
#include "openvic-simulation/research/Invention.hpp"
2221
#include "openvic-simulation/research/Technology.hpp"
2322
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
2423
#include "openvic-simulation/types/IndexedFlatMap.hpp"
25-
#include "openvic-simulation/types/OrderedContainersMath.hpp"
2624
#include "openvic-simulation/types/PopSize.hpp"
2725
#include "openvic-simulation/types/SliderValue.hpp"
2826
#include "openvic-simulation/utility/Containers.hpp"
@@ -39,15 +37,15 @@ CountryInstance::CountryInstance(
3937
decltype(building_type_unlock_levels)::keys_span_type building_type_keys,
4038
decltype(technology_unlock_levels)::keys_span_type technology_keys,
4139
decltype(invention_unlock_levels)::keys_span_type invention_keys,
42-
decltype(upper_house_proportion_by_ideology)::keys_span_type ideology_keys,
4340
decltype(reforms)::keys_span_type reform_keys,
4441
decltype(flag_overrides_by_government_type)::keys_span_type government_type_keys,
4542
decltype(crime_unlock_levels)::keys_span_type crime_keys,
46-
decltype(population_by_type)::keys_span_type pop_type_keys,
4743
decltype(goods_data)::keys_span_type good_instances_keys,
4844
decltype(regiment_type_unlock_levels)::keys_span_type regiment_type_unlock_levels_keys,
4945
decltype(ship_type_unlock_levels)::keys_span_type ship_type_unlock_levels_keys,
50-
decltype(tax_rate_slider_value_by_strata)::keys_span_type strata_keys,
46+
utility::forwardable_span<const Strata> strata_keys,
47+
utility::forwardable_span<const PopType> pop_type_keys,
48+
utility::forwardable_span<const Ideology> ideology_keys,
5149
GameRulesManager const& new_game_rules_manager,
5250
CountryRelationManager& new_country_relations_manager,
5351
SharedCountryValues const& new_shared_country_values,
@@ -56,6 +54,7 @@ CountryInstance::CountryInstance(
5654
EconomyDefines const& new_economy_defines
5755
) : FlagStrings { "country" },
5856
HasIndex { new_index },
57+
PopsAggregate { strata_keys, pop_type_keys, ideology_keys },
5958
/* Main attributes */
6059
country_definition { new_country_definition },
6160
game_rules_manager { new_game_rules_manager },
@@ -84,16 +83,6 @@ CountryInstance::CountryInstance(
8483
flag_overrides_by_government_type { government_type_keys },
8584
crime_unlock_levels { crime_keys },
8685

87-
/* Population */
88-
population_by_strata { strata_keys },
89-
militancy_by_strata { strata_keys },
90-
life_needs_fulfilled_by_strata { strata_keys },
91-
everyday_needs_fulfilled_by_strata { strata_keys },
92-
luxury_needs_fulfilled_by_strata { strata_keys },
93-
population_by_type { pop_type_keys },
94-
unemployed_pops_by_type { pop_type_keys },
95-
supporter_equivalents_by_ideology { ideology_keys },
96-
9786
/* Trade */
9887
goods_data { good_instances_keys },
9988

@@ -179,13 +168,7 @@ fixed_point_t CountryInstance::get_tariff_efficiency() const {
179168
}
180169

181170
void CountryInstance::update_country_definition_based_attributes() {
182-
vote_equivalents_by_party.clear();
183-
auto view = country_definition->get_parties() | std::views::transform(
184-
[](CountryParty const& key) {
185-
return std::make_pair(&key, fixed_point_t::_0);
186-
}
187-
);
188-
vote_equivalents_by_party.insert(view.begin(), view.end());
171+
update_parties_for_votes(country_definition);
189172
}
190173

191174
bool CountryInstance::exists() const {
@@ -351,64 +334,6 @@ void CountryInstance::change_script_variable(memory::string const& variable_name
351334
script_variables[variable_name] += value;
352335
}
353336

354-
pop_size_t CountryInstance::get_population_by_type(PopType const& pop_type) const {
355-
return population_by_type.at(pop_type);
356-
}
357-
pop_size_t CountryInstance::get_unemployed_pops_by_type(PopType const& pop_type) const {
358-
return unemployed_pops_by_type.at(pop_type);
359-
}
360-
fixed_point_t CountryInstance::get_supporter_equivalents_by_ideology(Ideology const& ideology) const {
361-
return supporter_equivalents_by_ideology.at(ideology);
362-
}
363-
fixed_point_t CountryInstance::get_supporter_equivalents_by_issue(BaseIssue const& issue) const {
364-
const decltype(supporter_equivalents_by_issue)::const_iterator it = supporter_equivalents_by_issue.find(&issue);
365-
366-
if (it != supporter_equivalents_by_issue.end()) {
367-
return it->second;
368-
} else {
369-
return 0;
370-
}
371-
}
372-
fixed_point_t CountryInstance::get_vote_equivalents_by_party(CountryParty const& party) const {
373-
const decltype(vote_equivalents_by_party)::const_iterator it = vote_equivalents_by_party.find(&party);
374-
if (it == vote_equivalents_by_party.end()) {
375-
return 0;
376-
}
377-
return it.value();
378-
}
379-
fixed_point_t CountryInstance::get_population_by_culture(Culture const& culture) const {
380-
const decltype(population_by_culture)::const_iterator it = population_by_culture.find(&culture);
381-
382-
if (it != population_by_culture.end()) {
383-
return it->second;
384-
} else {
385-
return 0;
386-
}
387-
}
388-
fixed_point_t CountryInstance::get_population_by_religion(Religion const& religion) const {
389-
const decltype(population_by_religion)::const_iterator it = population_by_religion.find(&religion);
390-
391-
if (it != population_by_religion.end()) {
392-
return it->second;
393-
} else {
394-
return 0;
395-
}
396-
}
397-
pop_size_t CountryInstance::get_population_by_strata(Strata const& strata) const {
398-
return population_by_strata.at(strata);
399-
}
400-
fixed_point_t CountryInstance::get_militancy_by_strata(Strata const& strata) const {
401-
return militancy_by_strata.at(strata);
402-
}
403-
fixed_point_t CountryInstance::get_life_needs_fulfilled_by_strata(Strata const& strata) const {
404-
return life_needs_fulfilled_by_strata.at(strata);
405-
}
406-
fixed_point_t CountryInstance::get_everyday_needs_fulfilled_by_strata(Strata const& strata) const {
407-
return everyday_needs_fulfilled_by_strata.at(strata);
408-
}
409-
fixed_point_t CountryInstance::get_luxury_needs_fulfilled_by_strata(Strata const& strata) const {
410-
return luxury_needs_fulfilled_by_strata.at(strata);
411-
}
412337
fixed_point_t CountryInstance::get_taxable_income_by_strata(Strata const& strata) const {
413338
fixed_point_t running_total = 0;
414339
for (auto const& [pop_type, taxable_income] : taxable_income_by_pop_type) {
@@ -1259,13 +1184,13 @@ void CountryInstance::_update_budget() {
12591184
= 0;
12601185

12611186
const fixed_point_t corruption_cost_multiplier = get_corruption_cost_multiplier();
1262-
for (auto const& [pop_type, size] : population_by_type) {
1187+
for (auto const& [pop_type, size] : get_population_by_type()) {
12631188
SharedPopTypeValues const& pop_type_values = shared_country_values.get_shared_pop_type_values(pop_type);
12641189
projected_administration_spending_unscaled_by_slider += size * calculate_administration_salary_base(pop_type_values, corruption_cost_multiplier);
12651190
projected_education_spending_unscaled_by_slider += size * calculate_education_salary_base(pop_type_values, corruption_cost_multiplier);
12661191
projected_military_spending_unscaled_by_slider += size * calculate_military_salary_base(pop_type_values, corruption_cost_multiplier);
12671192
projected_pensions_spending_unscaled_by_slider += size * calculate_pensions_base(modifier_effect_cache, pop_type_values);
1268-
projected_unemployment_subsidies_spending_unscaled_by_slider += unemployed_pops_by_type.at(pop_type)
1193+
projected_unemployment_subsidies_spending_unscaled_by_slider += get_unemployed_pops_by_type(pop_type)
12691194
* calculate_unemployment_subsidies_base(modifier_effect_cache, pop_type_values);
12701195
}
12711196

@@ -1275,7 +1200,7 @@ void CountryInstance::_update_budget() {
12751200
projected_pensions_spending_unscaled_by_slider /= Pop::size_denominator;
12761201
projected_unemployment_subsidies_spending_unscaled_by_slider /= Pop::size_denominator;
12771202
projected_import_subsidies = has_import_subsidies()
1278-
? -effective_tariff_rate * yesterdays_import_value
1203+
? -effective_tariff_rate * get_yesterdays_import_value()
12791204
: fixed_point_t::_0;
12801205
}
12811206

@@ -1349,95 +1274,23 @@ void CountryInstance::_update_politics() {
13491274
}
13501275

13511276
void CountryInstance::_update_population() {
1352-
total_population = 0;
1353-
yesterdays_import_value = 0;
1354-
national_literacy = 0;
1355-
national_consciousness = 0;
1356-
national_militancy = 0;
1357-
1358-
population_by_strata.fill(0);
1359-
militancy_by_strata.fill(0);
1360-
life_needs_fulfilled_by_strata.fill(0);
1361-
everyday_needs_fulfilled_by_strata.fill(0);
1362-
luxury_needs_fulfilled_by_strata.fill(0);
1363-
1364-
population_by_type.fill(0);
1365-
unemployed_pops_by_type.fill(0);
1366-
supporter_equivalents_by_ideology.fill(0);
1367-
supporter_equivalents_by_issue.clear();
1368-
vote_equivalents_by_party.clear();
1369-
population_by_culture.clear();
1370-
population_by_religion.clear();
1277+
clear_pops_aggregate();
13711278

13721279
for (State const* state : states) {
1373-
total_population += state->get_total_population();
1374-
yesterdays_import_value += state->get_yesterdays_import_value();
1375-
1376-
// TODO - change casting if pop_size_t changes type
1377-
const fixed_point_t state_population = fixed_point_t::parse(state->get_total_population());
1378-
national_literacy += state->get_average_literacy() * state_population;
1379-
national_consciousness += state->get_average_consciousness() * state_population;
1380-
national_militancy += state->get_average_militancy() * state_population;
1381-
1382-
population_by_strata += state->get_population_by_strata();
1383-
militancy_by_strata.mul_add(state->get_militancy_by_strata(), state->get_population_by_strata());
1384-
life_needs_fulfilled_by_strata.mul_add(
1385-
state->get_life_needs_fulfilled_by_strata(), state->get_population_by_strata()
1386-
);
1387-
everyday_needs_fulfilled_by_strata.mul_add(
1388-
state->get_everyday_needs_fulfilled_by_strata(), state->get_population_by_strata()
1389-
);
1390-
luxury_needs_fulfilled_by_strata.mul_add(
1391-
state->get_luxury_needs_fulfilled_by_strata(), state->get_population_by_strata()
1392-
);
1393-
1394-
population_by_type += state->get_population_by_type();
1395-
unemployed_pops_by_type += state->get_unemployed_pops_by_type();
1396-
supporter_equivalents_by_ideology += state->get_supporter_equivalents_by_ideology();
1397-
supporter_equivalents_by_issue += state->get_supporter_equivalents_by_issue();
1398-
vote_equivalents_by_party += state->get_vote_equivalents_by_party();
1399-
population_by_culture += state->get_population_by_culture();
1400-
population_by_religion += state->get_population_by_religion();
1401-
}
1402-
1403-
if (total_population > 0) {
1404-
national_literacy /= total_population;
1405-
national_consciousness /= total_population;
1406-
national_militancy /= total_population;
1407-
1408-
static const fu2::function<fixed_point_t&(fixed_point_t&, pop_size_t const&)> handle_div_by_zero = [](
1409-
fixed_point_t& lhs,
1410-
pop_size_t const& rhs
1411-
)->fixed_point_t& {
1412-
return lhs = fixed_point_t::_0;
1413-
};
1414-
militancy_by_strata.divide_assign_handle_zero(
1415-
population_by_strata,
1416-
handle_div_by_zero
1417-
);
1418-
life_needs_fulfilled_by_strata.divide_assign_handle_zero(
1419-
population_by_strata,
1420-
handle_div_by_zero
1421-
);
1422-
everyday_needs_fulfilled_by_strata.divide_assign_handle_zero(
1423-
population_by_strata,
1424-
handle_div_by_zero
1425-
);
1426-
luxury_needs_fulfilled_by_strata.divide_assign_handle_zero(
1427-
population_by_strata,
1428-
handle_div_by_zero
1429-
);
1280+
add_pops_aggregate(*state);
14301281
}
14311282

1283+
normalise_pops_aggregate();
1284+
14321285
daily_research_points = 0;
14331286
monthly_leadership_points = 0;
14341287
research_points_from_pop_types.clear();
14351288
leadership_points_from_pop_types.clear();
14361289

1437-
for (auto const& [pop_type, pop_size] : population_by_type) {
1290+
for (auto const& [pop_type, pop_size] : get_population_by_type()) {
14381291
if (pop_type.get_research_leadership_optimum() > fixed_point_t::_0 && pop_size > 0) {
14391292
const fixed_point_t factor = std::min(
1440-
pop_size / (total_population * pop_type.get_research_leadership_optimum()), fixed_point_t::_1
1293+
pop_size / (get_total_population() * pop_type.get_research_leadership_optimum()), fixed_point_t::_1
14411294
);
14421295

14431296
if (pop_type.get_research_points() != fixed_point_t::_0) {
@@ -1497,14 +1350,9 @@ void CountryInstance::_update_military(
14971350
}
14981351
}
14991352

1500-
max_supported_regiment_count = 0;
1501-
for (State const* state : states) {
1502-
max_supported_regiment_count += state->get_max_supported_regiments();
1503-
}
1504-
15051353
supply_consumption = fixed_point_t::_1 + get_modifier_effect_value(*modifier_effect_cache.get_supply_consumption());
15061354

1507-
const size_t regular_army_size = std::min(4 * deployed_non_mobilised_regiments, max_supported_regiment_count);
1355+
const size_t regular_army_size = std::min(4 * deployed_non_mobilised_regiments, get_max_supported_regiment_count());
15081356

15091357
fixed_point_t sum_of_regiment_type_stats = 0;
15101358
for (RegimentType const& regiment_type : unit_type_manager.get_regiment_types()) {
@@ -1643,7 +1491,7 @@ void CountryInstance::update_modifier_sum(Date today, StaticModifierCache const&
16431491
}
16441492
modifier_sum.add_modifier(static_modifier_cache.get_war_exhaustion(), war_exhaustion);
16451493
modifier_sum.add_modifier(static_modifier_cache.get_infamy(), infamy);
1646-
modifier_sum.add_modifier(static_modifier_cache.get_literacy(), national_literacy);
1494+
modifier_sum.add_modifier(static_modifier_cache.get_literacy(), get_average_literacy());
16471495
modifier_sum.add_modifier(static_modifier_cache.get_plurality(), plurality);
16481496
modifier_sum.add_modifier(is_at_war() ? static_modifier_cache.get_war() : static_modifier_cache.get_peace());
16491497
// TODO - difficulty modifiers, debt_default_to, bad_debtor, generalised_debt_default,
@@ -2342,15 +2190,15 @@ bool CountryInstanceManager::generate_country_instances(
23422190
decltype(CountryInstance::building_type_unlock_levels)::keys_span_type building_type_keys,
23432191
decltype(CountryInstance::technology_unlock_levels)::keys_span_type technology_keys,
23442192
decltype(CountryInstance::invention_unlock_levels)::keys_span_type invention_keys,
2345-
decltype(CountryInstance::upper_house_proportion_by_ideology)::keys_span_type ideology_keys,
23462193
decltype(CountryInstance::reforms)::keys_span_type reform_keys,
23472194
decltype(CountryInstance::flag_overrides_by_government_type)::keys_span_type government_type_keys,
23482195
decltype(CountryInstance::crime_unlock_levels)::keys_span_type crime_keys,
2349-
decltype(CountryInstance::population_by_type)::keys_span_type pop_type_keys,
23502196
decltype(CountryInstance::goods_data)::keys_span_type good_instances_keys,
23512197
decltype(CountryInstance::regiment_type_unlock_levels)::keys_span_type regiment_type_unlock_levels_keys,
23522198
decltype(CountryInstance::ship_type_unlock_levels)::keys_span_type ship_type_unlock_levels_keys,
2353-
decltype(CountryInstance::tax_rate_slider_value_by_strata):: keys_span_type strata_keys,
2199+
utility::forwardable_span<const Strata> strata_keys,
2200+
utility::forwardable_span<const PopType> pop_type_keys,
2201+
utility::forwardable_span<const Ideology> ideology_keys,
23542202
GameRulesManager const& game_rules_manager,
23552203
CountryRelationManager& country_relations_manager,
23562204
GoodInstanceManager& good_instance_manager,
@@ -2369,15 +2217,15 @@ bool CountryInstanceManager::generate_country_instances(
23692217
building_type_keys,
23702218
technology_keys,
23712219
invention_keys,
2372-
ideology_keys,
23732220
reform_keys,
23742221
government_type_keys,
23752222
crime_keys,
2376-
pop_type_keys,
23772223
good_instances_keys,
23782224
regiment_type_unlock_levels_keys,
23792225
ship_type_unlock_levels_keys,
23802226
strata_keys,
2227+
pop_type_keys,
2228+
ideology_keys,
23812229
game_rules_manager,
23822230
country_relations_manager,
23832231
shared_country_values,

0 commit comments

Comments
 (0)