Skip to content

Commit 53441f5

Browse files
committed
Create basic condition script parsing and execution framework
1 parent 4669692 commit 53441f5

24 files changed

+4716
-959
lines changed

src/headless/main.cpp

Lines changed: 70 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -90,43 +90,56 @@ static bool run_headless(Dataloader::path_vector_t const& roots, bool run_tests)
9090
ret &= game_manager.update_clock();
9191

9292
// TODO - REMOVE TEST CODE
93-
Logger::info("===== Ranking system test... =====");
94-
if (game_manager.get_instance_manager()) {
95-
const auto print_ranking_list = [](std::string_view title, std::vector<CountryInstance*> const& countries) -> void {
96-
std::string text;
97-
for (CountryInstance const* country : countries) {
98-
text += StringUtils::append_string_views(
99-
"\n ", country->get_identifier(),
100-
" - Total #", std::to_string(country->get_total_rank()), " (", country->get_total_score().to_string(1),
101-
"), Prestige #", std::to_string(country->get_prestige_rank()), " (", country->get_prestige().to_string(1),
102-
"), Industry #", std::to_string(country->get_industrial_rank()), " (", country->get_industrial_power().to_string(1),
103-
"), Military #", std::to_string(country->get_military_rank()), " (", country->get_military_power().to_string(1), ")"
104-
);
105-
}
106-
Logger::info(title, ":", text);
107-
};
108-
109-
CountryInstanceManager const& country_instance_manager =
110-
game_manager.get_instance_manager()->get_country_instance_manager();
111-
112-
std::vector<CountryInstance*> const& great_powers = country_instance_manager.get_great_powers();
113-
print_ranking_list("Great Powers", great_powers);
114-
print_ranking_list("Secondary Powers", country_instance_manager.get_secondary_powers());
115-
print_ranking_list("All countries", country_instance_manager.get_total_ranking());
116-
117-
Logger::info("===== RGO test... =====");
118-
for (size_t i = 0; i < std::min<size_t>(3, great_powers.size()); ++i) {
119-
CountryInstance const& great_power = *great_powers[i];
120-
ProvinceInstance const* const capital_province = great_power.get_capital();
121-
if (capital_province == nullptr) {
122-
Logger::warning(great_power.get_identifier(), " has no capital ProvinceInstance set.");
123-
} else {
124-
print_rgo(*capital_province);
125-
}
126-
}
127-
} else {
128-
Logger::error("Instance manager not available!");
129-
ret = false;
93+
// Logger::info("===== Ranking system test... =====");
94+
// if (game_manager.get_instance_manager()) {
95+
// const auto print_ranking_list = [](std::string_view title, std::vector<CountryInstance*> const& countries) -> void {
96+
// std::string text;
97+
// for (CountryInstance const* country : countries) {
98+
// text += StringUtils::append_string_views(
99+
// "\n ", country->get_identifier(),
100+
// " - Total #", std::to_string(country->get_total_rank()), " (", country->get_total_score().to_string(1),
101+
// "), Prestige #", std::to_string(country->get_prestige_rank()), " (", country->get_prestige().to_string(1),
102+
// "), Industry #", std::to_string(country->get_industrial_rank()), " (", country->get_industrial_power().to_string(1),
103+
// "), Military #", std::to_string(country->get_military_rank()), " (", country->get_military_power().to_string(1), ")"
104+
// );
105+
// }
106+
// Logger::info(title, ":", text);
107+
// };
108+
109+
// CountryInstanceManager const& country_instance_manager =
110+
// game_manager.get_instance_manager()->get_country_instance_manager();
111+
112+
// std::vector<CountryInstance*> const& great_powers = country_instance_manager.get_great_powers();
113+
// print_ranking_list("Great Powers", great_powers);
114+
// print_ranking_list("Secondary Powers", country_instance_manager.get_secondary_powers());
115+
// print_ranking_list("All countries", country_instance_manager.get_total_ranking());
116+
117+
// Logger::info("===== RGO test... =====");
118+
// for (size_t i = 0; i < std::min<size_t>(3, great_powers.size()); ++i) {
119+
// CountryInstance const& great_power = *great_powers[i];
120+
// ProvinceInstance const* const capital_province = great_power.get_capital();
121+
// if (capital_province == nullptr) {
122+
// Logger::warning(great_power.get_identifier(), " has no capital ProvinceInstance set.");
123+
// } else {
124+
// print_rgo(*capital_province);
125+
// }
126+
// }
127+
// } else {
128+
// Logger::error("Instance manager not available!");
129+
// ret = false;
130+
// }
131+
132+
Pop const& pop = *game_manager.get_instance_manager()->get_map_instance().get_province_instance_by_index(300)->get_pops().begin();
133+
134+
Logger::info(
135+
"Pop has type ", pop.get_type()->get_identifier(),
136+
", size ", pop.get_size(),
137+
", culture ", pop.get_culture().get_identifier(),
138+
", religion ", pop.get_religion().get_identifier(),
139+
", ideology:"
140+
);
141+
for (auto [ideology, support] : pop.get_ideology_distribution()) {
142+
Logger::info(" ", ideology.get_identifier(), " - ", support);
130143
}
131144

132145
if (ret) {
@@ -139,6 +152,26 @@ static bool run_headless(Dataloader::path_vector_t const& roots, bool run_tests)
139152
Logger::info("Ran ", --ticks_passed, " ticks in ", end_time - start_time);
140153
}
141154

155+
ProvinceInstance const& province = *pop.get_location();
156+
Logger::info("Province ", province.get_identifier(), " has ", province.get_total_population(), " pops with ideology:");
157+
for (auto [ideology, support] : province.get_ideology_distribution()) {
158+
Logger::info(" ", ideology.get_identifier(), " - ", support);
159+
}
160+
161+
State const& state = *province.get_state();
162+
Logger::info("State ", state.get_identifier(), " has ", state.get_total_population(), " pops with ideology:");
163+
for (auto [ideology, support] : state.get_ideology_distribution()) {
164+
Logger::info(" ", ideology.get_identifier(), " - ", support);
165+
}
166+
167+
CountryInstance const& country = *state.get_owner();
168+
Logger::info("Country ", country.get_identifier(), " has ", country.get_total_population(), " pops with ideology:");
169+
for (auto [ideology, support] : country.get_ideology_distribution()) {
170+
Logger::info(" ", ideology.get_identifier(), " - ", support);
171+
}
172+
173+
Logger::info("National value of ", country.get_identifier(), " is ", country.get_national_value());
174+
142175
return ret;
143176
}
144177

src/openvic-simulation/DefinitionManager.cpp

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/openvic-simulation/DefinitionManager.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ namespace OpenVic {
4545
SoundEffectManager PROPERTY_REF(sound_effect_manager);
4646

4747
public:
48-
DefinitionManager();
48+
DefinitionManager() : ui_manager { *this }, script_manager { *this } {}
4949
};
5050
}

src/openvic-simulation/InstanceManager.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void InstanceManager::update_gamestate() {
7474
update_modifier_sums();
7575

7676
// Update gamestate...
77-
map_instance.update_gamestate(today, definition_manager.get_define_manager());
77+
map_instance.update_gamestate(*this);
7878
country_instance_manager.update_gamestate(*this);
7979
unit_instance_manager.update_gamestate();
8080

@@ -221,9 +221,7 @@ bool InstanceManager::load_bookmark(Bookmark const* new_bookmark) {
221221
// It is important that province history is applied before country history as province history includes
222222
// generating pops which then have stats like literacy and consciousness set by country history.
223223

224-
ret &= country_instance_manager.apply_history_to_countries(
225-
definition_manager.get_history_manager().get_country_manager(), *this
226-
);
224+
ret &= country_instance_manager.apply_history_to_countries(*this);
227225

228226
ret &= map_instance.get_state_manager().generate_states(
229227
map_instance,
@@ -234,10 +232,7 @@ bool InstanceManager::load_bookmark(Bookmark const* new_bookmark) {
234232

235233
update_modifier_sums();
236234
country_instance_manager.update_gamestate(*this);
237-
map_instance.initialise_for_new_game(
238-
today,
239-
definition_manager.get_define_manager()
240-
);
235+
map_instance.initialise_for_new_game(*this);
241236
market_instance.execute_orders();
242237

243238
return ret;

src/openvic-simulation/country/CountryInstance.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,19 @@ bool CountryInstance::apply_history_to_country(CountryHistoryEntry const& entry,
925925
for (auto const& [technology, level] : entry.get_technologies()) {
926926
ret &= set_technology_unlock_level(*technology, level, good_instance_manager);
927927
}
928+
929+
for (
930+
Invention const& invention :
931+
instance_manager.get_definition_manager().get_research_manager().get_invention_manager().get_inventions()
932+
) {
933+
if (
934+
invention.get_limit().execute(instance_manager, this, this) &&
935+
invention.get_chance().execute(instance_manager, this, this) > 0
936+
) {
937+
ret &= unlock_invention(invention, good_instance_manager);
938+
}
939+
}
940+
928941
for (auto const& [invention, activated] : entry.get_inventions()) {
929942
ret &= set_invention_unlock_level(*invention, activated ? 1 : 0, good_instance_manager);
930943
}
@@ -1822,9 +1835,10 @@ bool CountryInstanceManager::generate_country_instances(
18221835
return ret;
18231836
}
18241837

1825-
bool CountryInstanceManager::apply_history_to_countries(
1826-
CountryHistoryManager const& history_manager, InstanceManager& instance_manager
1827-
) {
1838+
bool CountryInstanceManager::apply_history_to_countries(InstanceManager& instance_manager) {
1839+
CountryHistoryManager const& history_manager =
1840+
instance_manager.get_definition_manager().get_history_manager().get_country_manager();
1841+
18281842
bool ret = true;
18291843

18301844
const Date today = instance_manager.get_today();

src/openvic-simulation/country/CountryInstance.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ namespace OpenVic {
601601
EconomyDefines const& economy_defines
602602
);
603603

604-
bool apply_history_to_countries(CountryHistoryManager const& history_manager, InstanceManager& instance_manager);
604+
bool apply_history_to_countries(InstanceManager& instance_manager);
605605

606606
void update_modifier_sums(Date today, StaticModifierCache const& static_modifier_cache);
607607
void update_gamestate(InstanceManager& instance_manager);

src/openvic-simulation/dataloader/Dataloader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ bool Dataloader::load_defines(
11131113
Logger::error("Failed to load diplomatic actions!");
11141114
ret = false;
11151115
}
1116-
if (!definition_manager.get_script_manager().get_condition_manager().setup_conditions(definition_manager)) {
1116+
if (!definition_manager.get_script_manager().get_condition_manager().setup_conditions()) {
11171117
Logger::error("Failed to set up conditions!");
11181118
ret = false;
11191119
}

src/openvic-simulation/map/MapInstance.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ void MapInstance::update_modifier_sums(const Date today, StaticModifierCache con
137137
}
138138
}
139139

140-
void MapInstance::update_gamestate(const Date today, DefineManager const& define_manager) {
140+
void MapInstance::update_gamestate(InstanceManager const& instance_manager) {
141141
highest_province_population = 0;
142142
total_map_population = 0;
143143

144144
for (ProvinceInstance& province : province_instances.get_items()) {
145-
province.update_gamestate(today, define_manager);
145+
province.update_gamestate(instance_manager);
146146

147147
// Update population stats
148148
const pop_size_t province_population = province.get_total_population();
@@ -162,10 +162,7 @@ void MapInstance::map_tick() {
162162
//state tick will update pop employment via factories
163163
}
164164

165-
void MapInstance::initialise_for_new_game(
166-
const Date today,
167-
DefineManager const& define_manager
168-
) {
169-
update_gamestate(today, define_manager);
165+
void MapInstance::initialise_for_new_game(InstanceManager const& instance_manager) {
166+
update_gamestate(instance_manager);
170167
thread_pool.process_province_initialise_for_new_game();
171168
}

src/openvic-simulation/map/MapInstance.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ namespace OpenVic {
6666
);
6767

6868
void update_modifier_sums(const Date today, StaticModifierCache const& static_modifier_cache);
69-
void update_gamestate(const Date today, DefineManager const& define_manager);
69+
void update_gamestate(InstanceManager const& instance_manager);
7070
void map_tick();
71-
void initialise_for_new_game(const Date today, DefineManager const& define_manager);
71+
void initialise_for_new_game(InstanceManager const& instance_manager);
7272
};
7373
}

src/openvic-simulation/map/ProvinceInstance.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
#include "openvic-simulation/country/CountryInstance.hpp"
44
#include "openvic-simulation/defines/Define.hpp"
5+
#include "openvic-simulation/DefinitionManager.hpp"
56
#include "openvic-simulation/economy/production/ProductionType.hpp"
67
#include "openvic-simulation/economy/production/ResourceGatheringOperation.hpp"
78
#include "openvic-simulation/history/ProvinceHistory.hpp"
9+
#include "openvic-simulation/InstanceManager.hpp"
810
#include "openvic-simulation/map/Crime.hpp"
911
#include "openvic-simulation/map/ProvinceDefinition.hpp"
1012
#include "openvic-simulation/map/Region.hpp"
@@ -244,7 +246,7 @@ size_t ProvinceInstance::get_pop_count() const {
244246
/* REQUIREMENTS:
245247
* MAP-65, MAP-68, MAP-70, MAP-234
246248
*/
247-
void ProvinceInstance::_update_pops(DefineManager const& define_manager) {
249+
void ProvinceInstance::_update_pops(InstanceManager const& instance_manager) {
248250
total_population = 0;
249251
average_literacy = 0;
250252
average_consciousness = 0;
@@ -269,7 +271,8 @@ void ProvinceInstance::_update_pops(DefineManager const& define_manager) {
269271

270272
max_supported_regiments = 0;
271273

272-
MilitaryDefines const& military_defines = define_manager.get_military_defines();
274+
MilitaryDefines const& military_defines =
275+
instance_manager.get_definition_manager().get_define_manager().get_military_defines();
273276

274277
using enum colony_status_t;
275278

@@ -279,7 +282,7 @@ void ProvinceInstance::_update_pops(DefineManager const& define_manager) {
279282
: is_owner_core() ? fixed_point_t::_1() : military_defines.get_pop_size_per_regiment_non_core_multiplier();
280283

281284
for (Pop& pop : pops) {
282-
pop.update_gamestate(define_manager, owner, pop_size_per_regiment_multiplier);
285+
pop.update_gamestate(instance_manager, owner, pop_size_per_regiment_multiplier);
283286

284287
const pop_size_t pop_size_s = pop.get_size();
285288
// TODO - change casting if pop_size_t changes type
@@ -413,7 +416,7 @@ bool ProvinceInstance::convert_rgo_worker_pops_to_equivalent(ProductionType cons
413416
return is_valid_operation;
414417
}
415418

416-
void ProvinceInstance::update_gamestate(const Date today, DefineManager const& define_manager) {
419+
void ProvinceInstance::update_gamestate(InstanceManager const& instance_manager) {
417420
land_regiment_count = 0;
418421
for (ArmyInstance const* army : armies) {
419422
land_regiment_count += army->get_unit_count();
@@ -424,10 +427,12 @@ void ProvinceInstance::update_gamestate(const Date today, DefineManager const& d
424427
}
425428
}
426429

430+
const Date today = instance_manager.get_today();
431+
427432
for (BuildingInstance& building : buildings.get_items()) {
428433
building.update_gamestate(today);
429434
}
430-
_update_pops(define_manager);
435+
_update_pops(instance_manager);
431436
}
432437

433438
void ProvinceInstance::province_tick(const Date today, PopValuesFromProvince& reusable_pop_values) {

0 commit comments

Comments
 (0)