Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Condition scripts #247

Draft
wants to merge 27 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0c6bf19
Create basic condition script parsing and execution framework
Hop311 Nov 1, 2024
341238c
(WIP) Register conditions with parse and execute callbacks
Hop311 Nov 1, 2024
b48614d
tmp
Hop311 Nov 4, 2024
80a4771
tmp
Hop311 Nov 4, 2024
6338fb7
more condition parsing work, skipped some which need a multi type hel…
Hop311 Nov 5, 2024
317f0f0
Add more condition parsing callbacks
Hop311 Nov 6, 2024
a1e3fcb
Remove STATE scope and start implementing special condition parsing c…
Hop311 Nov 8, 2024
5bfb79b
Finish condition parse callbacks (apart from scope checks)
Hop311 Nov 9, 2024
395742d
Add ConditionNode printing function
Hop311 Nov 12, 2024
1eb9458
tmp
Hop311 Nov 16, 2024
8710612
Make scope_t not const& + implement more execution callbacks
Hop311 Nov 29, 2024
4af4ac4
more condition execution callbacks
Hop311 Nov 29, 2024
d96e86a
More execution callbacks, including simplifying some
Hop311 Dec 4, 2024
1379608
Fix const& scope argument issue
Hop311 Dec 15, 2024
190c700
Fix lambda function pointer and add IsValidArgument
Hop311 Dec 16, 2024
09766c9
Fix has_building parse special callback on Linux
Hop311 Dec 17, 2024
121bd68
Rework ConditionalWeight to use addition/multiplication correctly
Hop311 Dec 19, 2024
d2ad0f8
Check starting invention limits with new condition system
Hop311 Dec 19, 2024
2ea9387
Add missing conditions + some comments
Hop311 Dec 21, 2024
a3388e2
Add more condition execution callbacks
Hop311 Dec 21, 2024
6e0a7b8
Further invention chance condition stuff (even for those with limits …
Hop311 Dec 25, 2024
ae04d91
Further THIS | FROM execution infrastructure
Hop311 Dec 26, 2024
a3e82dd
More THIS/FROM callbacks and visitor cleanup
Hop311 Dec 27, 2024
7189b1a
Calculate pop ideology distributions
Hop311 Dec 28, 2024
048631e
More ideology generation work
Hop311 Dec 30, 2024
6a9e542
Country script variables
Hop311 Dec 31, 2024
fdd8107
tmp
Hop311 Jan 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 70 additions & 37 deletions src/headless/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,45 +90,78 @@ static bool run_headless(Dataloader::path_vector_t const& roots, bool run_tests)
ret &= game_manager.update_clock();

// TODO - REMOVE TEST CODE
Logger::info("===== Ranking system test... =====");
if (game_manager.get_instance_manager()) {
const auto print_ranking_list = [](std::string_view title, std::vector<CountryInstance*> const& countries) -> void {
std::string text;
for (CountryInstance const* country : countries) {
text += StringUtils::append_string_views(
"\n ", country->get_identifier(),
" - Total #", std::to_string(country->get_total_rank()), " (", country->get_total_score().to_string(1),
"), Prestige #", std::to_string(country->get_prestige_rank()), " (", country->get_prestige().to_string(1),
"), Industry #", std::to_string(country->get_industrial_rank()), " (", country->get_industrial_power().to_string(1),
"), Military #", std::to_string(country->get_military_rank()), " (", country->get_military_power().to_string(1), ")"
);
}
Logger::info(title, ":", text);
};

CountryInstanceManager const& country_instance_manager =
game_manager.get_instance_manager()->get_country_instance_manager();

std::vector<CountryInstance*> const& great_powers = country_instance_manager.get_great_powers();
print_ranking_list("Great Powers", great_powers);
print_ranking_list("Secondary Powers", country_instance_manager.get_secondary_powers());
print_ranking_list("All countries", country_instance_manager.get_total_ranking());

Logger::info("===== RGO test... =====");
for (size_t i = 0; i < std::min<size_t>(3, great_powers.size()); ++i) {
CountryInstance const& great_power = *great_powers[i];
ProvinceInstance const* const capital_province = great_power.get_capital();
if (capital_province == nullptr) {
Logger::warning(great_power.get_identifier(), " has no capital ProvinceInstance set.");
} else {
print_rgo(*capital_province);
}
}
} else {
Logger::error("Instance manager not available!");
ret = false;
// Logger::info("===== Ranking system test... =====");
// if (game_manager.get_instance_manager()) {
// const auto print_ranking_list = [](std::string_view title, std::vector<CountryInstance*> const& countries) -> void {
// std::string text;
// for (CountryInstance const* country : countries) {
// text += StringUtils::append_string_views(
// "\n ", country->get_identifier(),
// " - Total #", std::to_string(country->get_total_rank()), " (", country->get_total_score().to_string(1),
// "), Prestige #", std::to_string(country->get_prestige_rank()), " (", country->get_prestige().to_string(1),
// "), Industry #", std::to_string(country->get_industrial_rank()), " (", country->get_industrial_power().to_string(1),
// "), Military #", std::to_string(country->get_military_rank()), " (", country->get_military_power().to_string(1), ")"
// );
// }
// Logger::info(title, ":", text);
// };

// CountryInstanceManager const& country_instance_manager =
// game_manager.get_instance_manager()->get_country_instance_manager();

// std::vector<CountryInstance*> const& great_powers = country_instance_manager.get_great_powers();
// print_ranking_list("Great Powers", great_powers);
// print_ranking_list("Secondary Powers", country_instance_manager.get_secondary_powers());
// print_ranking_list("All countries", country_instance_manager.get_total_ranking());

// Logger::info("===== RGO test... =====");
// for (size_t i = 0; i < std::min<size_t>(3, great_powers.size()); ++i) {
// CountryInstance const& great_power = *great_powers[i];
// ProvinceInstance const* const capital_province = great_power.get_capital();
// if (capital_province == nullptr) {
// Logger::warning(great_power.get_identifier(), " has no capital ProvinceInstance set.");
// } else {
// print_rgo(*capital_province);
// }
// }
// } else {
// Logger::error("Instance manager not available!");
// ret = false;
// }

Pop const& pop = *game_manager.get_instance_manager()->get_map_instance().get_province_instance_by_index(300)->get_pops().begin();

Logger::info(
"Pop has type ", pop.get_type()->get_identifier(),
", size ", pop.get_size(),
", culture ", pop.get_culture().get_identifier(),
", religion ", pop.get_religion().get_identifier(),
", ideology:"
);
for (auto [ideology, support] : pop.get_ideology_distribution()) {
Logger::info(" ", ideology.get_identifier(), " - ", support);
}

ProvinceInstance const& province = *pop.get_location();
Logger::info("Province ", province.get_identifier(), " has ", province.get_total_population(), " pops with ideology:");
for (auto [ideology, support] : province.get_ideology_distribution()) {
Logger::info(" ", ideology.get_identifier(), " - ", support);
}

State const& state = *province.get_state();
Logger::info("State ", state.get_identifier(), " has ", state.get_total_population(), " pops with ideology:");
for (auto [ideology, support] : state.get_ideology_distribution()) {
Logger::info(" ", ideology.get_identifier(), " - ", support);
}

CountryInstance const& country = *state.get_owner();
Logger::info("Country ", country.get_identifier(), " has ", country.get_total_population(), " pops with ideology:");
for (auto [ideology, support] : country.get_ideology_distribution()) {
Logger::info(" ", ideology.get_identifier(), " - ", support);
}

Logger::info("National value of ", country.get_identifier(), " is ", country.get_national_value());

return ret;
}

Expand Down
11 changes: 3 additions & 8 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 @@ -141,9 +141,7 @@ bool InstanceManager::load_bookmark(Bookmark const* new_bookmark) {
// It is important that province history is applied before country history as province history includes
// generating pops which then have stats like literacy and consciousness set by country history.

ret &= country_instance_manager.apply_history_to_countries(
definition_manager.get_history_manager().get_country_manager(), today, unit_instance_manager, map_instance
);
ret &= country_instance_manager.apply_history_to_countries(*this);

ret &= map_instance.get_state_manager().generate_states(
map_instance,
Expand All @@ -154,10 +152,7 @@ bool InstanceManager::load_bookmark(Bookmark const* new_bookmark) {

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
42 changes: 31 additions & 11 deletions src/openvic-simulation/country/CountryInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ CountryInstance::CountryInstance(
country_definition { new_country_definition },
colour { ERROR_COLOUR },
capital { nullptr },
ai { true },
releasable_vassal { true },
owns_colonial_province { false },
country_status { COUNTRY_STATUS_UNCIVILISED },
Expand Down Expand Up @@ -216,6 +217,11 @@ bool CountryInstance::is_secondary_power() const {
return country_status == COUNTRY_STATUS_SECONDARY_POWER;
}

bool CountryInstance::is_at_war() const {
// TODO - implement this properly once we have wars
return false;
}

bool CountryInstance::is_neighbour(CountryInstance const& country) const {
return neighbouring_countries.contains(&country);
}
Expand Down Expand Up @@ -786,9 +792,7 @@ void CountryInstance::apply_foreign_investments(
}
}

bool CountryInstance::apply_history_to_country(
CountryHistoryEntry const& entry, MapInstance& map_instance, CountryInstanceManager const& country_instance_manager
) {
bool CountryInstance::apply_history_to_country(CountryHistoryEntry const& entry, InstanceManager const& instance_manager) {
constexpr auto set_optional = []<typename T>(T& target, std::optional<T> const& source) {
if (source) {
target = *source;
Expand All @@ -812,7 +816,7 @@ bool CountryInstance::apply_history_to_country(
set_optional(last_election, entry.get_last_election());
ret &= upper_house.copy(entry.get_upper_house());
if (entry.get_capital()) {
capital = &map_instance.get_province_instance_from_definition(**entry.get_capital());
capital = &instance_manager.get_map_instance().get_province_instance_from_definition(**entry.get_capital());
}
set_optional(government_type, entry.get_government_type());
set_optional(plurality, entry.get_plurality());
Expand All @@ -834,10 +838,23 @@ bool CountryInstance::apply_history_to_country(
for (auto const& [technology, level] : entry.get_technologies()) {
ret &= set_technology_unlock_level(*technology, level);
}

for (
Invention const& invention :
instance_manager.get_definition_manager().get_research_manager().get_invention_manager().get_inventions()
) {
if (
invention.get_limit().execute(instance_manager, this, this) &&
invention.get_chance().execute(instance_manager, this, this) > 0
) {
ret &= unlock_invention(invention);
}
}

for (auto const& [invention, activated] : entry.get_inventions()) {
ret &= set_invention_unlock_level(*invention, activated ? 1 : 0);
}
apply_foreign_investments(entry.get_foreign_investment(), country_instance_manager);
apply_foreign_investments(entry.get_foreign_investment(), instance_manager.get_country_instance_manager());

set_optional(releasable_vassal, entry.is_releasable_vassal());

Expand Down Expand Up @@ -1428,10 +1445,13 @@ bool CountryInstanceManager::generate_country_instances(
return ret;
}

bool CountryInstanceManager::apply_history_to_countries(
CountryHistoryManager const& history_manager, Date date, UnitInstanceManager& unit_instance_manager,
MapInstance& map_instance
) {
bool CountryInstanceManager::apply_history_to_countries(InstanceManager& instance_manager) {
CountryHistoryManager const& history_manager =
instance_manager.get_definition_manager().get_history_manager().get_country_manager();
const Date today = instance_manager.get_today();
UnitInstanceManager& unit_instance_manager = instance_manager.get_unit_instance_manager();
MapInstance& map_instance = instance_manager.get_map_instance();

bool ret = true;

for (CountryInstance& country_instance : country_instances.get_items()) {
Expand All @@ -1448,8 +1468,8 @@ bool CountryInstanceManager::apply_history_to_countries(
std::optional<fixed_point_t> nonstate_culture_literacy;

for (auto const& [entry_date, entry] : history_map->get_entries()) {
if (entry_date <= date) {
ret &= country_instance.apply_history_to_country(*entry, map_instance, *this);
if (entry_date <= today) {
ret &= country_instance.apply_history_to_country(*entry, instance_manager);

if (entry->get_inital_oob().has_value()) {
oob_history_entry = entry.get();
Expand Down
11 changes: 4 additions & 7 deletions src/openvic-simulation/country/CountryInstance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ namespace OpenVic {
CountryDefinition const* PROPERTY(country_definition);
colour_t PROPERTY(colour); // Cached to avoid searching government overrides for every province
ProvinceInstance const* PROPERTY(capital);
bool PROPERTY_CUSTOM_PREFIX(ai, is);
bool PROPERTY_CUSTOM_PREFIX(releasable_vassal, is);
bool PROPERTY(owns_colonial_province);

Expand Down Expand Up @@ -248,6 +249,7 @@ namespace OpenVic {
bool can_colonise() const;
bool is_great_power() const;
bool is_secondary_power() const;
bool is_at_war() const;
bool is_neighbour(CountryInstance const& country) const;

// These functions take "std::string const&" rather than "std::string_view" as they're only used with script arguments
Expand Down Expand Up @@ -380,9 +382,7 @@ namespace OpenVic {
CountryInstanceManager const& country_instance_manager
);

bool apply_history_to_country(
CountryHistoryEntry const& entry, MapInstance& map_instance, CountryInstanceManager const& country_instance_manager
);
bool apply_history_to_country(CountryHistoryEntry const& entry, InstanceManager const& instance_manager);

private:
void _update_production(DefineManager const& define_manager);
Expand Down Expand Up @@ -451,10 +451,7 @@ namespace OpenVic {
decltype(CountryInstance::tax_rate_by_strata):: keys_type const& strata_keys
);

bool apply_history_to_countries(
CountryHistoryManager const& history_manager, Date date, UnitInstanceManager& unit_instance_manager,
MapInstance& map_instance
);
bool apply_history_to_countries(InstanceManager& instance_manager);

void update_modifier_sums(Date today, StaticModifierCache const& static_modifier_cache);
void update_gamestate(InstanceManager& instance_manager);
Expand Down
15 changes: 8 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 @@ -144,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 @@ -168,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
4 changes: 2 additions & 2 deletions src/openvic-simulation/map/MapInstance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,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);
};
}
15 changes: 10 additions & 5 deletions src/openvic-simulation/map/ProvinceInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

#include "openvic-simulation/country/CountryInstance.hpp"
#include "openvic-simulation/defines/Define.hpp"
#include "openvic-simulation/DefinitionManager.hpp"
#include "openvic-simulation/economy/production/ProductionType.hpp"
#include "openvic-simulation/economy/production/ResourceGatheringOperation.hpp"
#include "openvic-simulation/history/ProvinceHistory.hpp"
#include "openvic-simulation/InstanceManager.hpp"
#include "openvic-simulation/map/Crime.hpp"
#include "openvic-simulation/map/ProvinceDefinition.hpp"
#include "openvic-simulation/map/Region.hpp"
Expand Down Expand Up @@ -226,7 +228,7 @@ size_t ProvinceInstance::get_pop_count() const {
/* REQUIREMENTS:
* MAP-65, MAP-68, MAP-70, MAP-234
*/
void ProvinceInstance::_update_pops(DefineManager const& define_manager) {
void ProvinceInstance::_update_pops(InstanceManager const& instance_manager) {
total_population = 0;
average_literacy = 0;
average_consciousness = 0;
Expand All @@ -251,7 +253,8 @@ void ProvinceInstance::_update_pops(DefineManager const& define_manager) {

max_supported_regiments = 0;

MilitaryDefines const& military_defines = define_manager.get_military_defines();
MilitaryDefines const& military_defines =
instance_manager.get_definition_manager().get_define_manager().get_military_defines();

using enum colony_status_t;

Expand All @@ -261,7 +264,7 @@ void ProvinceInstance::_update_pops(DefineManager const& define_manager) {
: is_owner_core() ? fixed_point_t::_1() : military_defines.get_pop_size_per_regiment_non_core_multiplier();

for (Pop& pop : pops) {
pop.update_gamestate(define_manager, owner, pop_size_per_regiment_multiplier);
pop.update_gamestate(instance_manager, owner, pop_size_per_regiment_multiplier);

const pop_size_t pop_size_s = pop.get_size();
// TODO - change casting if pop_size_t changes type
Expand Down Expand Up @@ -443,7 +446,7 @@ bool ProvinceInstance::convert_rgo_worker_pops_to_equivalent(ProductionType cons
return is_valid_operation;
}

void ProvinceInstance::update_gamestate(const Date today, DefineManager const& define_manager) {
void ProvinceInstance::update_gamestate(InstanceManager const& instance_manager) {
land_regiment_count = 0;
for (ArmyInstance const* army : armies) {
land_regiment_count += army->get_unit_count();
Expand All @@ -454,10 +457,12 @@ void ProvinceInstance::update_gamestate(const Date today, DefineManager const& d
}
}

const Date today = instance_manager.get_today();

for (BuildingInstance& building : buildings.get_items()) {
building.update_gamestate(today);
}
_update_pops(define_manager);
_update_pops(instance_manager);
}

void ProvinceInstance::province_tick(const Date today) {
Expand Down
Loading
Loading