Skip to content

Commit 855d37f

Browse files
committed
Automatic dependency tracking
1 parent afdff8d commit 855d37f

22 files changed

+805
-368
lines changed

src/headless/main.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,13 @@ static bool run_headless(fs::path const& root, memory::vector<memory::string>& m
179179
if (game_manager.get_instance_manager()) {
180180
const auto print_ranking_list = [](std::string_view title, OpenVic::utility::forwardable_span<CountryInstance* const> countries) -> void {
181181
memory::string text;
182-
for (CountryInstance const* country : countries) {
182+
for (CountryInstance* country : countries) {
183183
text += StringUtils::append_string_views(
184184
"\n ", country->get_identifier(),
185-
" - Total #", std::to_string(country->get_total_rank()), " (", country->get_total_score().to_string(1),
186-
"), Prestige #", std::to_string(country->get_prestige_rank()), " (", country->get_prestige().to_string(1),
187-
"), Industry #", std::to_string(country->get_industrial_rank()), " (", country->get_industrial_power().to_string(1),
188-
"), Military #", std::to_string(country->get_military_rank()), " (", country->get_military_power().to_string(1), ")"
185+
" - Total #", std::to_string(country->get_total_rank()), " (", country->total_score.get_untracked().to_string(1),
186+
"), Prestige #", std::to_string(country->get_prestige_rank()), " (", country->get_prestige_untracked().to_string(1),
187+
"), Industry #", std::to_string(country->get_industrial_rank()), " (", country->get_industrial_power_untracked().to_string(1),
188+
"), Military #", std::to_string(country->get_military_rank()), " (", country->military_power.get_untracked().to_string(1), ")"
189189
);
190190
}
191191
Logger::info(title, ":", text);

src/openvic-simulation/country/CountryInstance.cpp

Lines changed: 329 additions & 230 deletions
Large diffs are not rendered by default.

src/openvic-simulation/country/CountryInstance.hpp

Lines changed: 74 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "openvic-simulation/types/UnitVariant.hpp"
2222
#include "openvic-simulation/utility/Getters.hpp"
2323
#include "openvic-simulation/utility/Containers.hpp"
24+
#include "openvic-simulation/utility/reactive/DerivedState.hpp"
25+
#include "openvic-simulation/utility/reactive/MutableState.hpp"
2426

2527
#include "openvic-simulation/types/IndexedFlatMapMacro.hpp" //below other imports that undef the macros
2628

@@ -100,7 +102,7 @@ namespace OpenVic {
100102
GameRulesManager const& game_rules_manager;
101103
CountryRelationManager& country_relations_manager;
102104
CountryDefines const& country_defines;
103-
SharedCountryValues const& shared_country_values;
105+
SharedCountryValues& shared_country_values;
104106

105107
colour_t PROPERTY(colour); // Cached to avoid searching government overrides for every province
106108
ProvinceInstance* PROPERTY_PTR(capital, nullptr);
@@ -115,7 +117,6 @@ namespace OpenVic {
115117
country_status_t PROPERTY(country_status, country_status_t::COUNTRY_STATUS_UNCIVILISED);
116118
fixed_point_t PROPERTY(civilisation_progress);
117119
Date PROPERTY(lose_great_power_date);
118-
fixed_point_t PROPERTY(total_score);
119120
size_t PROPERTY(total_rank, 0);
120121

121122
ordered_set<ProvinceInstance*> PROPERTY(owned_provinces);
@@ -132,7 +133,7 @@ namespace OpenVic {
132133
memory::vector<ModifierInstance> PROPERTY(event_modifiers);
133134

134135
/* Production */
135-
fixed_point_t PROPERTY(industrial_power);
136+
STATE_PROPERTY(fixed_point_t, industrial_power);
136137
memory::vector<std::pair<State const*, fixed_point_t>> PROPERTY(industrial_power_from_states);
137138
memory::vector<std::pair<CountryInstance const*, fixed_point_t>> PROPERTY(industrial_power_from_investments);
138139
size_t PROPERTY(industrial_rank, 0);
@@ -142,20 +143,19 @@ namespace OpenVic {
142143

143144
/* Budget */
144145
// TODO - cash stockpile change over last 30 days
145-
fixed_point_t PROPERTY(gold_income);
146+
STATE_PROPERTY(fixed_point_t, gold_income);
146147
atomic_fixed_point_t PROPERTY(cash_stockpile);
147148
std::mutex taxable_income_mutex;
148149
IndexedFlatMap_PROPERTY(PopType, fixed_point_t, taxable_income_by_pop_type);
149-
fixed_point_t PROPERTY(tax_efficiency);
150-
IndexedFlatMap_PROPERTY(Strata, fixed_point_t, effective_tax_rate_by_strata);
150+
STATE_PROPERTY(fixed_point_t, tax_efficiency);
151+
IndexedFlatMap<Strata, DerivedState<fixed_point_t>> PROPERTY(effective_tax_rate_by_strata);
152+
public:
153+
DerivedState<fixed_point_t>& get_effective_tax_rate_by_strata(Strata const& strata);
154+
private:
151155
IndexedFlatMap_PROPERTY(Strata, SliderValue, tax_rate_slider_value_by_strata);
152156

153-
fixed_point_t PROPERTY(administrative_efficiency_from_administrators);
154-
constexpr fixed_point_t get_corruption_cost_multiplier() const {
155-
return 2 - administrative_efficiency_from_administrators;
156-
}
157-
fixed_point_t PROPERTY(administrator_percentage);
158-
fixed_point_t PROPERTY(desired_administrator_percentage);
157+
STATE_PROPERTY(fixed_point_t, administrative_efficiency_from_administrators);
158+
STATE_PROPERTY(fixed_point_t, administrator_percentage);
159159

160160
//store per slider per good: desired, bought & cost
161161
//store purchase record from last tick and prediction next tick
@@ -164,64 +164,64 @@ namespace OpenVic {
164164
SliderValue PROPERTY(construction_spending_slider_value);
165165

166166
SliderValue PROPERTY(administration_spending_slider_value);
167-
fixed_point_t PROPERTY(actual_administration_spending);
168-
fixed_point_t PROPERTY(projected_administration_spending_unscaled_by_slider);
167+
STATE_PROPERTY(fixed_point_t, projected_administration_spending_unscaled_by_slider);
168+
STATE_PROPERTY(fixed_point_t, actual_administration_spending);
169169

170170
SliderValue PROPERTY(education_spending_slider_value);
171-
fixed_point_t PROPERTY(actual_education_spending);
172-
fixed_point_t PROPERTY(projected_education_spending_unscaled_by_slider);
171+
STATE_PROPERTY(fixed_point_t, projected_education_spending_unscaled_by_slider);
172+
STATE_PROPERTY(fixed_point_t, actual_education_spending);
173173

174174
SliderValue PROPERTY(military_spending_slider_value);
175-
fixed_point_t PROPERTY(actual_military_spending);
176-
fixed_point_t PROPERTY(projected_military_spending_unscaled_by_slider);
175+
STATE_PROPERTY(fixed_point_t, projected_military_spending_unscaled_by_slider);
176+
STATE_PROPERTY(fixed_point_t, actual_military_spending);
177177

178178
SliderValue PROPERTY(social_spending_slider_value);
179-
fixed_point_t PROPERTY(actual_social_spending);
180-
fixed_point_t PROPERTY(projected_pensions_spending_unscaled_by_slider);
181-
fixed_point_t PROPERTY(projected_unemployment_subsidies_spending_unscaled_by_slider);
179+
STATE_PROPERTY(fixed_point_t, projected_pensions_spending_unscaled_by_slider);
180+
STATE_PROPERTY(fixed_point_t, projected_unemployment_subsidies_spending_unscaled_by_slider);
181+
STATE_PROPERTY(fixed_point_t, actual_social_spending);
182+
183+
//base here means not scaled by slider or pop size
184+
IndexedFlatMap<PopType, DerivedState<fixed_point_t>> administration_salary_base_by_pop_type;
185+
IndexedFlatMap<PopType, DerivedState<fixed_point_t>> education_salary_base_by_pop_type;
186+
IndexedFlatMap<PopType, DerivedState<fixed_point_t>> military_salary_base_by_pop_type;
187+
IndexedFlatMap<PopType, DerivedState<fixed_point_t>> social_income_variant_base_by_pop_type;
182188

183189
SliderValue PROPERTY(tariff_rate_slider_value);
184-
fixed_point_t PROPERTY(effective_tariff_rate);
185190
std::mutex actual_net_tariffs_mutex;
186-
fixed_point_t PROPERTY(projected_import_subsidies);
187-
fixed_point_t PROPERTY(actual_net_tariffs);
188-
constexpr bool has_import_subsidies() const {
189-
return effective_tariff_rate < fixed_point_t::_0;
190-
}
191+
MutableState<fixed_point_t> actual_net_tariffs;
191192

192193
//TODO actual factory subsidies
193194
//projected cost is UI only and lists the different factories
194195

195196
/* Technology */
196197
IndexedFlatMap_PROPERTY(Technology, technology_unlock_level_t, technology_unlock_levels);
197198
IndexedFlatMap_PROPERTY(Invention, technology_unlock_level_t, invention_unlock_levels);
198-
int32_t PROPERTY(inventions_count, 0);
199-
Technology const* PROPERTY(current_research, nullptr);
200-
fixed_point_t PROPERTY(invested_research_points);
201-
fixed_point_t PROPERTY(current_research_cost);
202-
Date PROPERTY(expected_research_completion_date);
203-
fixed_point_t PROPERTY(research_point_stockpile);
204-
fixed_point_t PROPERTY(daily_research_points);
199+
STATE_PROPERTY(int32_t, inventions_count);
200+
STATE_PROPERTY(Technology const*, current_research);
201+
STATE_PROPERTY(fixed_point_t, invested_research_points);
202+
STATE_PROPERTY(fixed_point_t, current_research_cost);
203+
STATE_PROPERTY(Date, expected_research_completion_date);
204+
STATE_PROPERTY(fixed_point_t, research_point_stockpile);
205+
STATE_PROPERTY(fixed_point_t, daily_research_points);
205206
fixed_point_map_t<PopType const*> PROPERTY(research_points_from_pop_types);
206-
TechnologySchool const* PROPERTY(tech_school, nullptr);
207+
STATE_PROPERTY(TechnologySchool const*, tech_school);
207208
// TODO - cached possible inventions with %age chance
208209

209210
/* Politics */
210-
NationalValue const* PROPERTY(national_value, nullptr);
211-
GovernmentType const* PROPERTY(government_type, nullptr);
211+
STATE_PROPERTY(NationalValue const*, national_value);
212+
STATE_PROPERTY(GovernmentType const*, government_type);
212213
Date PROPERTY(last_election);
213-
CountryParty const* PROPERTY(ruling_party, nullptr);
214+
STATE_PROPERTY(CountryParty const*, ruling_party);
214215
IndexedFlatMap_PROPERTY(Ideology, fixed_point_t, upper_house_proportion_by_ideology);
215216
IndexedFlatMap_PROPERTY(ReformGroup, Reform const*, reforms);
216-
fixed_point_t PROPERTY(total_administrative_multiplier);
217+
STATE_PROPERTY(fixed_point_t, total_administrative_multiplier);
217218
RuleSet PROPERTY(rule_set);
218219
// TODO - national issue support distribution (for just voters and for everyone)
219220
IndexedFlatMap_PROPERTY(GovernmentType, GovernmentType const*, flag_overrides_by_government_type);
220-
GovernmentType const* PROPERTY(flag_government_type, nullptr);
221-
fixed_point_t PROPERTY(suppression_points);
222-
fixed_point_t PROPERTY(infamy); // in 0-25+ range
223-
fixed_point_t PROPERTY(plurality); // in 0-100 range
224-
fixed_point_t PROPERTY(revanchism);
221+
STATE_PROPERTY(fixed_point_t, suppression_points);
222+
STATE_PROPERTY(fixed_point_t, infamy); // in 0-25+ range
223+
STATE_PROPERTY(fixed_point_t, plurality); // in 0-100 range
224+
STATE_PROPERTY(fixed_point_t, revanchism);
225225
IndexedFlatMap_PROPERTY(Crime, technology_unlock_level_t, crime_unlock_levels);
226226
// TODO - rebel movements
227227

@@ -233,6 +233,23 @@ namespace OpenVic {
233233
// TODO - population change over last 30 days
234234

235235
public:
236+
DerivedState<GovernmentType const*> flag_government_type;
237+
DerivedState<fixed_point_t> total_score;
238+
DerivedState<fixed_point_t> military_power;
239+
DerivedState<fixed_point_t> research_progress;
240+
DerivedState<fixed_point_t> desired_administrator_percentage;
241+
DerivedState<fixed_point_t> corruption_cost_multiplier;
242+
DerivedState<fixed_point_t> tariff_efficiency;
243+
DerivedState<fixed_point_t> effective_tariff_rate;
244+
DerivedState<fixed_point_t> projected_administration_spending;
245+
DerivedState<fixed_point_t> projected_education_spending;
246+
DerivedState<fixed_point_t> projected_military_spending;
247+
DerivedState<fixed_point_t> projected_social_spending;
248+
DerivedState<fixed_point_t> projected_social_spending_unscaled_by_slider;
249+
DerivedState<fixed_point_t> projected_import_subsidies;
250+
DerivedState<fixed_point_t> projected_spending;
251+
DerivedState<bool> has_import_subsidies;
252+
236253
fixed_point_t get_taxable_income_by_strata(Strata const& strata) const;
237254
// TODO - national foci
238255

@@ -272,7 +289,7 @@ namespace OpenVic {
272289
IndexedFlatMap_PROPERTY(GoodInstance, good_data_t, goods_data);
273290

274291
/* Diplomacy */
275-
fixed_point_t PROPERTY(prestige);
292+
STATE_PROPERTY(fixed_point_t, prestige);
276293
size_t PROPERTY(prestige_rank, 0);
277294
fixed_point_t PROPERTY(diplomatic_points);
278295
// The last time this country lost a war, i.e. accepted a peace offer sent from their offer tab or the enemy's demand
@@ -282,10 +299,9 @@ namespace OpenVic {
282299
// TODO - colonial power, current wars
283300

284301
/* Military */
285-
fixed_point_t PROPERTY(military_power);
286-
fixed_point_t PROPERTY(military_power_from_land);
287-
fixed_point_t PROPERTY(military_power_from_sea);
288-
fixed_point_t PROPERTY(military_power_from_leaders);
302+
STATE_PROPERTY(fixed_point_t, military_power_from_land);
303+
STATE_PROPERTY(fixed_point_t, military_power_from_sea);
304+
STATE_PROPERTY(fixed_point_t, military_power_from_leaders);
289305
size_t PROPERTY(military_rank, 0);
290306
memory::vector<LeaderInstance*> SPAN_PROPERTY(generals);
291307
memory::vector<LeaderInstance*> SPAN_PROPERTY(admirals);
@@ -347,7 +363,7 @@ namespace OpenVic {
347363
utility::forwardable_span<const Ideology> ideology_keys,
348364
GameRulesManager const* new_game_rules_manager,
349365
CountryRelationManager* new_country_relations_manager,
350-
SharedCountryValues const* new_shared_country_values,
366+
SharedCountryValues* new_shared_country_values,
351367
GoodInstanceManager* new_good_instance_manager,
352368
CountryDefines const* new_country_defines,
353369
EconomyDefines const* new_economy_defines
@@ -558,7 +574,6 @@ namespace OpenVic {
558574
fixed_point_t calculate_research_cost(
559575
Technology const& technology, ModifierEffectCache const& modifier_effect_cache
560576
) const;
561-
fixed_point_t get_research_progress() const;
562577
bool can_research_tech(Technology const& technology, Date today) const;
563578
void start_research(Technology const& technology, InstanceManager const& instance_manager);
564579

@@ -572,45 +587,21 @@ namespace OpenVic {
572587

573588
private:
574589
void _update_production(DefineManager const& define_manager);
575-
void _update_effective_tax_rate_by_strata(Strata const& strata);
576590
void _update_budget();
577591

578592
//base here means not scaled by slider or pop size
579-
constexpr fixed_point_t calculate_administration_salary_base(
580-
SharedPopTypeValues const& pop_type_values,
581-
const fixed_point_t corruption_cost_multiplier
582-
) const {
583-
return pop_type_values.get_administration_salary_base() * corruption_cost_multiplier;
584-
}
585-
constexpr fixed_point_t calculate_education_salary_base(
586-
SharedPopTypeValues const& pop_type_values,
587-
const fixed_point_t corruption_cost_multiplier
588-
) const {
589-
return pop_type_values.get_education_salary_base() * corruption_cost_multiplier;
590-
}
591-
constexpr fixed_point_t calculate_military_salary_base(
592-
SharedPopTypeValues const& pop_type_values,
593-
const fixed_point_t corruption_cost_multiplier
594-
) const {
595-
return pop_type_values.get_military_salary_base() * corruption_cost_multiplier;
596-
}
597593
fixed_point_t calculate_pensions_base(
598594
ModifierEffectCache const& modifier_effect_cache,
599-
SharedPopTypeValues const& pop_type_values
600-
) const;
595+
PopType const& pop_type
596+
);
601597
fixed_point_t calculate_unemployment_subsidies_base(
602598
ModifierEffectCache const& modifier_effect_cache,
603-
SharedPopTypeValues const& pop_type_values
604-
) const;
599+
PopType const& pop_type
600+
);
605601
fixed_point_t calculate_minimum_wage_base(
606602
ModifierEffectCache const& modifier_effect_cache,
607-
SharedPopTypeValues const& pop_type_values
608-
) const;
609-
constexpr fixed_point_t calculate_social_income_variant_base(
610-
SharedPopTypeValues const& pop_type_values
611-
) const {
612-
return administrative_efficiency_from_administrators * pop_type_values.get_social_income_variant_base();
613-
}
603+
PopType const& pop_type
604+
);
614605

615606
// Expects current_research to be non-null
616607
void _update_current_tech(InstanceManager const& instance_manager);
@@ -652,8 +643,8 @@ namespace OpenVic {
652643
void report_input_consumption(ProductionType const& production_type, GoodDefinition const& good, const fixed_point_t quantity);
653644
void report_input_demand(ProductionType const& production_type, GoodDefinition const& good, const fixed_point_t quantity);
654645
void report_output(ProductionType const& production_type, const fixed_point_t quantity);
655-
void request_salaries_and_welfare_and_import_subsidies(Pop& pop) const;
656-
fixed_point_t calculate_minimum_wage_base(PopType const& pop_type) const;
646+
void request_salaries_and_welfare_and_import_subsidies(Pop& pop);
647+
fixed_point_t calculate_minimum_wage_base(PopType const& pop_type);
657648
fixed_point_t apply_tariff(const fixed_point_t money_spent_on_imports);
658649
};
659650

src/openvic-simulation/country/SharedCountryValues.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@ SharedCountryValues::SharedCountryValues(
1919
shared_pop_type_values { pop_type_keys }
2020
{}
2121

22-
SharedPopTypeValues const& SharedCountryValues::get_shared_pop_type_values(PopType const& pop_type) const {
22+
SharedPopTypeValues& SharedCountryValues::get_shared_pop_type_values(PopType const& pop_type) {
2323
return shared_pop_type_values.at(pop_type);
2424
}
2525

2626
void SharedCountryValues::update_costs(GoodInstanceManager const& good_instance_manager) {
27-
for (auto [pop_type, value] : shared_pop_type_values) {
28-
value.update_costs(pop_type, pop_defines, good_instance_manager);
27+
for (SharedPopTypeValues& value : shared_pop_type_values.get_values()) {
28+
value.update_costs(pop_defines, good_instance_manager);
2929
}
3030
}
3131

32-
void SharedPopTypeValues::update_costs(PopType const& pop_type, PopsDefines const& pop_defines, GoodInstanceManager const& good_instance_manager) {
33-
administration_salary_base = education_salary_base = military_salary_base = 0;
32+
void SharedPopTypeValues::update_costs(PopsDefines const& pop_defines, GoodInstanceManager const& good_instance_manager) {
33+
fixed_point_t administration_salary_base_running_total = 0;
34+
fixed_point_t education_salary_base_running_total = 0;
35+
fixed_point_t military_salary_base_running_total = 0;
3436
using enum PopType::income_type_t;
3537

3638
#define UPDATE_NEED_COSTS(need_category) \
@@ -41,17 +43,22 @@ void SharedPopTypeValues::update_costs(PopType const& pop_type, PopsDefines cons
4143
} \
4244
base_##need_category##_need_costs *= pop_defines.get_base_goods_demand(); \
4345
if ((pop_type.get_##need_category##_needs_income_types() & ADMINISTRATION) == ADMINISTRATION) { \
44-
administration_salary_base += base_##need_category##_need_costs; \
46+
administration_salary_base_running_total += base_##need_category##_need_costs; \
4547
} \
4648
if ((pop_type.get_##need_category##_needs_income_types() & EDUCATION) == EDUCATION) { \
47-
education_salary_base += base_##need_category##_need_costs; \
49+
education_salary_base_running_total += base_##need_category##_need_costs; \
4850
} \
4951
if ((pop_type.get_##need_category##_needs_income_types() & MILITARY) == MILITARY) { \
50-
military_salary_base += base_##need_category##_need_costs; \
52+
military_salary_base_running_total += base_##need_category##_need_costs; \
5153
}
5254

5355
DO_FOR_ALL_NEED_CATEGORIES(UPDATE_NEED_COSTS)
5456
#undef UPDATE_NEED_COSTS
57+
58+
administration_salary_base.set(administration_salary_base_running_total);
59+
education_salary_base.set(education_salary_base_running_total);
60+
military_salary_base.set(military_salary_base_running_total);
61+
social_income_variant_base.set(2 * base_life_need_costs);
5562
}
5663

5764
#undef DO_FOR_ALL_NEED_CATEGORIES

0 commit comments

Comments
 (0)