2
2
3
3
#include < cstdint>
4
4
5
+ #include " openvic-simulation/country/SharedCountryValues.hpp"
5
6
#include " openvic-simulation/country/CountryDefinition.hpp"
6
7
#include " openvic-simulation/defines/Define.hpp"
7
8
#include " openvic-simulation/DefinitionManager.hpp"
14
15
#include " openvic-simulation/modifier/ModifierEffectCache.hpp"
15
16
#include " openvic-simulation/modifier/StaticModifierCache.hpp"
16
17
#include " openvic-simulation/politics/Ideology.hpp"
18
+ #include " openvic-simulation/pop/Pop.hpp"
19
+ #include " openvic-simulation/pop/PopType.hpp"
20
+ #include " openvic-simulation/pop/PopNeedsMacro.hpp"
17
21
#include " openvic-simulation/research/Invention.hpp"
18
22
#include " openvic-simulation/research/Technology.hpp"
19
23
#include " openvic-simulation/types/fixed_point/FixedPoint.hpp"
24
+ #include " openvic-simulation/types/PopSize.hpp"
20
25
#include " openvic-simulation/types/SliderValue.hpp"
21
26
22
27
using namespace OpenVic ;
@@ -989,8 +994,9 @@ static inline constexpr fixed_point_t nonzero_or_one(fixed_point_t const& value)
989
994
return value == fixed_point_t::_0 () ? fixed_point_t::_1 () : value;
990
995
}
991
996
992
- void CountryInstance::_update_budget (DefineManager const & define_manager, ModifierEffectCache const & modifier_effect_cache) {
993
- CountryDefines const & country_defines = define_manager.get_country_defines ();
997
+ void CountryInstance::_update_budget (SharedCountryValues const & shared_country_values) {
998
+ CountryDefines const & country_defines = shared_country_values.get_country_defines ();
999
+ ModifierEffectCache const & modifier_effect_cache = shared_country_values.get_modifier_effect_cache ();
994
1000
995
1001
const fixed_point_t min_tax = get_modifier_effect_value (*modifier_effect_cache.get_min_tax ());
996
1002
const fixed_point_t max_tax = nonzero_or_one (get_modifier_effect_value (*modifier_effect_cache.get_max_tax ()));
@@ -1017,13 +1023,80 @@ void CountryInstance::_update_budget(DefineManager const& define_manager, Modifi
1017
1023
// TODO - make sure we properly update everything dependent on these sliders' values,
1018
1024
// as they might change if their sliders' bounds shrink past their previous values.
1019
1025
1020
- const fixed_point_t tax_efficiency = country_defines.get_base_country_tax_efficiency () +
1021
- get_modifier_effect_value (*modifier_effect_cache.get_tax_efficiency ()) +
1022
- get_modifier_effect_value (*modifier_effect_cache.get_tax_eff ()) / 100 ;
1026
+ const fixed_point_t tax_efficiency = country_defines.get_base_country_tax_efficiency ()
1027
+ + get_modifier_effect_value (*modifier_effect_cache.get_tax_efficiency ())
1028
+ + get_modifier_effect_value (*modifier_effect_cache.get_tax_eff ()) / 100 ;
1023
1029
1024
1030
for (auto const & [strata, tax_rate_slider_value] : tax_rate_slider_value_by_strata) {
1025
1031
effective_tax_rate_by_strata[strata] = tax_rate_slider_value.get_value () * tax_efficiency;
1026
1032
}
1033
+
1034
+ /*
1035
+ In Victoria 2, administration efficiency is updated in the UI immediately.
1036
+ However the corruption_cost_multiplier is only updated after 2 ticks.
1037
+
1038
+ OpenVic immediately updates both.
1039
+ */
1040
+
1041
+ if (total_population == 0 ) {
1042
+ administrative_efficiency = fixed_point_t::_0 ();
1043
+ } else {
1044
+ pop_size_t administrators = 0 ;
1045
+ for (auto const & [pop_type, size] : pop_type_distribution) {
1046
+ if (pop_type.get_is_administrator ()) {
1047
+ administrators += size;
1048
+ }
1049
+ }
1050
+
1051
+ administrative_efficiency = administrators
1052
+ * (fixed_point_t::_1 () + get_modifier_effect_value (*modifier_effect_cache.get_administrative_efficiency ()))
1053
+ * (fixed_point_t::_1 () + get_modifier_effect_value (*modifier_effect_cache.get_administrative_efficiency_modifier ()))
1054
+ / total_population;
1055
+ }
1056
+
1057
+ projected_administration_spending_unscaled_by_slider
1058
+ = projected_education_spending_unscaled_by_slider
1059
+ = projected_military_spending_unscaled_by_slider
1060
+ = projected_pensions_spending_unscaled_by_slider
1061
+ = projected_unemployment_subsidies_spending_unscaled_by_slider
1062
+ = fixed_point_t::_0 ();
1063
+
1064
+ for (auto const & [pop_type, size] : pop_type_distribution) {
1065
+ projected_administration_spending_unscaled_by_slider += size * calculate_administration_salary_base (pop_type, shared_country_values);
1066
+ projected_education_spending_unscaled_by_slider += size * calculate_education_salary_base (pop_type, shared_country_values);
1067
+ projected_military_spending_unscaled_by_slider += size * calculate_military_salary_base (pop_type, shared_country_values);
1068
+ projected_pensions_spending_unscaled_by_slider += size * calculate_pensions_base (pop_type, shared_country_values);
1069
+ projected_unemployment_subsidies_spending_unscaled_by_slider += pop_type_unemployed_count[pop_type]
1070
+ * calculate_unemployment_subsidies_base (pop_type, shared_country_values);
1071
+ }
1072
+
1073
+ projected_administration_spending_unscaled_by_slider /= Pop::size_denominator;
1074
+ projected_education_spending_unscaled_by_slider /= Pop::size_denominator;
1075
+ projected_military_spending_unscaled_by_slider /= Pop::size_denominator;
1076
+ projected_pensions_spending_unscaled_by_slider /= Pop::size_denominator;
1077
+ projected_unemployment_subsidies_spending_unscaled_by_slider /= Pop::size_denominator;
1078
+ }
1079
+
1080
+ fixed_point_t CountryInstance::calculate_pensions_base (
1081
+ PopType const & pop_type,
1082
+ SharedCountryValues const & shared_country_values
1083
+ ) const {
1084
+ return get_modifier_effect_value (*shared_country_values.get_modifier_effect_cache ().get_pension_level ())
1085
+ * calculate_social_income_form_base (pop_type, shared_country_values);
1086
+ }
1087
+ fixed_point_t CountryInstance::calculate_unemployment_subsidies_base (
1088
+ PopType const & pop_type,
1089
+ SharedCountryValues const & shared_country_values
1090
+ ) const {
1091
+ return get_modifier_effect_value (*shared_country_values.get_modifier_effect_cache ().get_unemployment_benefit ())
1092
+ * calculate_social_income_form_base (pop_type, shared_country_values);
1093
+ }
1094
+ fixed_point_t CountryInstance::calculate_minimum_wage_base (
1095
+ PopType const & pop_type,
1096
+ SharedCountryValues const & shared_country_values
1097
+ ) const {
1098
+ return get_modifier_effect_value (*shared_country_values.get_modifier_effect_cache ().get_minimum_wage ())
1099
+ * calculate_social_income_form_base (pop_type, shared_country_values);
1027
1100
}
1028
1101
1029
1102
void CountryInstance::_update_current_tech (InstanceManager const & instance_manager) {
@@ -1409,10 +1482,10 @@ fixed_point_t CountryInstance::get_modifier_effect_value(ModifierEffect const& e
1409
1482
return modifier_sum.get_modifier_effect_value (effect);
1410
1483
}
1411
1484
1412
- void CountryInstance::update_gamestate (InstanceManager& instance_manager) {
1485
+ void CountryInstance::update_gamestate (InstanceManager& instance_manager, SharedCountryValues const & shared_country_values ) {
1413
1486
DefinitionManager const & definition_manager = instance_manager.get_definition_manager ();
1414
1487
DefineManager const & define_manager = definition_manager.get_define_manager ();
1415
- ModifierEffectCache const & modifier_effect_cache = definition_manager. get_modifier_manager () .get_modifier_effect_cache ();
1488
+ ModifierEffectCache const & modifier_effect_cache = shared_country_values .get_modifier_effect_cache ();
1416
1489
1417
1490
if (is_civilised ()) {
1418
1491
civilisation_progress = fixed_point_t::_0 ();
@@ -1523,7 +1596,7 @@ void CountryInstance::update_gamestate(InstanceManager& instance_manager) {
1523
1596
definition_manager.get_military_manager ().get_unit_type_manager (),
1524
1597
modifier_effect_cache
1525
1598
);
1526
- _update_budget (define_manager, modifier_effect_cache );
1599
+ _update_budget (shared_country_values );
1527
1600
1528
1601
// These don't do anything yet
1529
1602
_update_politics ();
@@ -1552,15 +1625,73 @@ void CountryInstance::update_gamestate(InstanceManager& instance_manager) {
1552
1625
}
1553
1626
}
1554
1627
1555
- void CountryInstance::country_reset_before_tick () {
1556
- for (auto pair : goods_data) {
1557
- pair.second .clear_daily_recorded_data ();
1628
+ void CountryInstance::country_tick_before_map (InstanceManager& instance_manager) {
1629
+ // TODO AI sliders
1630
+ // TODO stockpile management
1631
+
1632
+ const fixed_point_t projected_administration_spending = administration_spending_slider_value.get_value () * projected_administration_spending_unscaled_by_slider;
1633
+ const fixed_point_t projected_education_spending = education_spending_slider_value.get_value () * projected_education_spending_unscaled_by_slider;
1634
+ const fixed_point_t projected_military_spending = military_spending_slider_value.get_value () * projected_military_spending_unscaled_by_slider;
1635
+ const fixed_point_t projected_social_spending = social_spending_slider_value.get_value () * (
1636
+ projected_pensions_spending_unscaled_by_slider
1637
+ + projected_unemployment_subsidies_spending_unscaled_by_slider
1638
+ );
1639
+
1640
+ const fixed_point_t projected_spending = projected_administration_spending
1641
+ + projected_education_spending
1642
+ + projected_military_spending
1643
+ + projected_social_spending;
1644
+ // + tariffs (if negative)
1645
+ // + reparations + war subsidies
1646
+ // + stockpile costs
1647
+ // + industrial subsidies
1648
+ // + loan interest
1649
+
1650
+ fixed_point_t available_funds = cash_stockpile.get_copy_of_value ();
1651
+ if (projected_spending <= available_funds) {
1652
+ actual_administration_spending = projected_administration_spending;
1653
+ actual_education_spending = projected_education_spending;
1654
+ actual_military_spending = projected_military_spending;
1655
+ actual_social_spending = projected_social_spending;
1656
+ } else {
1657
+ // TODO try take loan (callback?)
1658
+ // update available_funds with loan
1659
+
1660
+ if (available_funds < projected_education_spending) {
1661
+ actual_education_spending = available_funds;
1662
+ actual_administration_spending = actual_military_spending = actual_social_spending = fixed_point_t::_0 ();
1663
+ } else {
1664
+ available_funds -= projected_education_spending;
1665
+ actual_education_spending = projected_education_spending;
1666
+
1667
+ if (available_funds < projected_administration_spending) {
1668
+ actual_administration_spending = available_funds;
1669
+ actual_military_spending = actual_social_spending = fixed_point_t::_0 ();
1670
+ } else {
1671
+ available_funds -= projected_administration_spending;
1672
+ actual_administration_spending = projected_administration_spending;
1673
+
1674
+ if (available_funds < projected_social_spending) {
1675
+ actual_social_spending = available_funds;
1676
+ actual_military_spending = fixed_point_t::_0 ();
1677
+ } else {
1678
+ available_funds -= projected_social_spending;
1679
+ actual_social_spending = projected_social_spending;
1680
+
1681
+ actual_military_spending = std::min (available_funds, projected_military_spending);
1682
+ }
1683
+ }
1684
+ }
1685
+ }
1686
+
1687
+ for (auto const & [good_instance, data] : goods_data) {
1688
+ data.clear_daily_recorded_data ();
1558
1689
}
1559
1690
1560
1691
taxable_income_by_pop_type.fill (fixed_point_t::_0 ());
1561
1692
}
1562
1693
1563
- void CountryInstance::country_tick (InstanceManager& instance_manager) {
1694
+ void CountryInstance::country_tick_after_map (InstanceManager& instance_manager) {
1564
1695
DefinitionManager const & definition_manager = instance_manager.get_definition_manager ();
1565
1696
DefineManager const & define_manager = definition_manager.get_define_manager ();
1566
1697
@@ -1691,6 +1822,60 @@ void CountryInstance::report_output(ProductionType const& production_type, const
1691
1822
good_data.production_per_production_type [&production_type] += quantity;
1692
1823
}
1693
1824
1825
+ void CountryInstance::request_salaries_and_welfare (Pop& pop, SharedCountryValues const & shared_country_values) const {
1826
+ PopType const & pop_type = *pop.get_type ();
1827
+ const pop_size_t pop_size = pop.get_size ();
1828
+
1829
+ const fixed_point_t administration_salary = fixed_point_t::mul_div (
1830
+ pop_size * calculate_administration_salary_base (pop_type, shared_country_values),
1831
+ actual_administration_spending,
1832
+ projected_administration_spending_unscaled_by_slider
1833
+ );
1834
+ if (administration_salary > fixed_point_t::_0 ()) {
1835
+ pop.add_government_salary_administration (administration_salary);
1836
+ }
1837
+
1838
+ const fixed_point_t education_salary = fixed_point_t::mul_div (
1839
+ pop_size * calculate_education_salary_base (pop_type, shared_country_values),
1840
+ actual_education_spending,
1841
+ projected_education_spending_unscaled_by_slider
1842
+ );
1843
+ if (education_salary > fixed_point_t::_0 ()) {
1844
+ pop.add_government_salary_education (education_salary);
1845
+ }
1846
+
1847
+ const fixed_point_t military_salary = fixed_point_t::mul_div (
1848
+ pop_size * calculate_military_salary_base (pop_type, shared_country_values),
1849
+ actual_military_spending,
1850
+ projected_military_spending_unscaled_by_slider
1851
+ );
1852
+ if (military_salary > fixed_point_t::_0 ()) {
1853
+ pop.add_government_salary_military (military_salary);
1854
+ }
1855
+
1856
+ const fixed_point_t projected_social_spending_unscaled_by_slider =
1857
+ projected_pensions_spending_unscaled_by_slider
1858
+ + projected_unemployment_subsidies_spending_unscaled_by_slider;
1859
+
1860
+ const fixed_point_t pension_income = fixed_point_t::mul_div (
1861
+ pop_size * calculate_pensions_base (pop_type, shared_country_values),
1862
+ actual_social_spending,
1863
+ projected_social_spending_unscaled_by_slider
1864
+ );
1865
+ if (pension_income > fixed_point_t::_0 ()) {
1866
+ pop.add_pensions (pension_income);
1867
+ }
1868
+
1869
+ const fixed_point_t unemployment_subsidies = fixed_point_t::mul_div (
1870
+ pop.get_unemployed () * calculate_unemployment_subsidies_base (pop_type, shared_country_values),
1871
+ actual_social_spending,
1872
+ projected_social_spending_unscaled_by_slider
1873
+ );
1874
+ if (unemployment_subsidies > fixed_point_t::_0 ()) {
1875
+ pop.add_unemployment_subsidies (unemployment_subsidies);
1876
+ }
1877
+ }
1878
+
1694
1879
CountryInstance::good_data_t & CountryInstance::get_good_data (GoodInstance const & good_instance) {
1695
1880
return goods_data[good_instance];
1696
1881
}
@@ -1830,9 +2015,22 @@ void CountryInstanceManager::update_rankings(Date today, DefineManager const& de
1830
2015
}
1831
2016
}
1832
2017
1833
- CountryInstanceManager::CountryInstanceManager (CountryDefinitionManager const & new_country_definition_manager)
2018
+ CountryInstanceManager::CountryInstanceManager (
2019
+ CountryDefinitionManager const & new_country_definition_manager,
2020
+ ModifierEffectCache const & new_modifier_effect_cache,
2021
+ CountryDefines const & new_country_defines,
2022
+ PopsDefines const & new_pop_defines,
2023
+ std::vector<PopType> const & pop_type_keys
2024
+ )
1834
2025
: country_definition_manager { new_country_definition_manager },
1835
- country_definition_to_instance_map { &new_country_definition_manager.get_country_definitions () } {
2026
+ country_definition_to_instance_map { &new_country_definition_manager.get_country_definitions () },
2027
+ shared_country_values {
2028
+ new_modifier_effect_cache,
2029
+ new_country_defines,
2030
+ new_pop_defines,
2031
+ pop_type_keys
2032
+ }
2033
+ {
1836
2034
great_powers.reserve (16 );
1837
2035
secondary_powers.reserve (16 );
1838
2036
}
@@ -1999,7 +2197,7 @@ void CountryInstanceManager::update_modifier_sums(Date today, StaticModifierCach
1999
2197
2000
2198
void CountryInstanceManager::update_gamestate (InstanceManager& instance_manager) {
2001
2199
for (CountryInstance& country : country_instances.get_items ()) {
2002
- country.update_gamestate (instance_manager);
2200
+ country.update_gamestate (instance_manager, shared_country_values );
2003
2201
}
2004
2202
2005
2203
// TODO - work out how to have ranking effects applied (e.g. static modifiers) applied at game start
@@ -2009,14 +2207,21 @@ void CountryInstanceManager::update_gamestate(InstanceManager& instance_manager)
2009
2207
update_rankings (instance_manager.get_today (), instance_manager.get_definition_manager ().get_define_manager ());
2010
2208
}
2011
2209
2012
- void CountryInstanceManager::country_manager_reset_before_tick () {
2210
+ void CountryInstanceManager::country_manager_tick_before_map (InstanceManager& instance_manager) {
2211
+ // TODO parallellise?
2013
2212
for (CountryInstance& country : country_instances.get_items ()) {
2014
- country.country_reset_before_tick ( );
2213
+ country.country_tick_before_map (instance_manager );
2015
2214
}
2016
2215
}
2017
2216
2018
- void CountryInstanceManager::country_manager_tick (InstanceManager& instance_manager) {
2217
+ void CountryInstanceManager::country_manager_tick_after_map (InstanceManager& instance_manager) {
2218
+ // TODO parallellise?
2219
+ // TODO update and pay government salaries and social spending
2019
2220
for (CountryInstance& country : country_instances.get_items ()) {
2020
- country.country_tick (instance_manager);
2221
+ country.country_tick_after_map (instance_manager);
2021
2222
}
2223
+
2224
+ shared_country_values.update_costs (instance_manager.get_good_instance_manager ());
2022
2225
}
2226
+
2227
+ #undef DO_FOR_ALL_NEED_CATEGORIES
0 commit comments