Skip to content

Commit

Permalink
More ideology generation work
Browse files Browse the repository at this point in the history
  • Loading branch information
Hop311 committed Dec 30, 2024
1 parent e867291 commit 5b3cd6f
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 15 deletions.
33 changes: 33 additions & 0 deletions src/headless/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,39 @@ static bool run_headless(Dataloader::path_vector_t const& roots, bool run_tests)
// 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
3 changes: 3 additions & 0 deletions src/openvic-simulation/InstanceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ bool InstanceManager::load_bookmark(Bookmark const* new_bookmark) {
definition_manager.get_politics_manager().get_issue_manager()
);

// 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(*this);

ret &= map_instance.get_state_manager().generate_states(
Expand Down
51 changes: 43 additions & 8 deletions src/openvic-simulation/country/CountryInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,14 +837,10 @@ bool CountryInstance::apply_history_to_country(CountryHistoryEntry const& entry,
}
apply_foreign_investments(entry.get_foreign_investment(), instance_manager.get_country_instance_manager());

// These need to be applied to pops
// entry.get_consciousness();
// entry.get_nonstate_consciousness();
// entry.get_literacy();
// entry.get_nonstate_culture_literacy();

set_optional(releasable_vassal, entry.is_releasable_vassal());
// entry.get_colonial_points();

// TODO - entry.get_colonial_points();

for (std::string const& flag : entry.get_country_flags()) {
ret &= set_flag(flag, true);
}
Expand Down Expand Up @@ -1445,15 +1441,32 @@ bool CountryInstanceManager::apply_history_to_countries(InstanceManager& instanc
history_manager.get_country_history(country_instance.get_country_definition());

if (history_map != nullptr) {
static constexpr fixed_point_t DEFAULT_STATE_CULTURE_LITERACY = fixed_point_t::_0_50();
CountryHistoryEntry const* oob_history_entry = nullptr;
std::optional<fixed_point_t> state_culture_consciousness;
std::optional<fixed_point_t> nonstate_culture_consciousness;
fixed_point_t state_culture_literacy = DEFAULT_STATE_CULTURE_LITERACY;
std::optional<fixed_point_t> nonstate_culture_literacy;

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

if (entry->get_inital_oob()) {
if (entry->get_inital_oob().has_value()) {
oob_history_entry = entry.get();
}
if (entry->get_consciousness().has_value()) {
state_culture_consciousness = entry->get_consciousness();
}
if (entry->get_nonstate_consciousness().has_value()) {
nonstate_culture_consciousness = entry->get_nonstate_consciousness();
}
if (entry->get_literacy().has_value()) {
state_culture_literacy = *entry->get_literacy();
}
if (entry->get_nonstate_culture_literacy().has_value()) {
nonstate_culture_literacy = entry->get_nonstate_culture_literacy();
}
} else {
// All foreign investments are applied regardless of the bookmark's date
country_instance.apply_foreign_investments(entry->get_foreign_investment(), *this);
Expand All @@ -1465,6 +1478,28 @@ bool CountryInstanceManager::apply_history_to_countries(InstanceManager& instanc
map_instance, country_instance, *oob_history_entry->get_inital_oob()
);
}

// TODO - check if better to do "if"s then "for"s, so looping multiple times rather than having lots of
// redundant "if" statements?
for (ProvinceInstance* province : country_instance.get_owned_provinces()) {
for (Pop& pop : province->get_mutable_pops()) {
if (country_instance.is_primary_or_accepted_culture(pop.get_culture())) {
pop.set_literacy(state_culture_literacy);

if (state_culture_consciousness.has_value()) {
pop.set_consciousness(*state_culture_consciousness);
}
} else {
if (nonstate_culture_literacy.has_value()) {
pop.set_literacy(*nonstate_culture_literacy);
}

if (nonstate_culture_consciousness.has_value()) {
pop.set_consciousness(*nonstate_culture_consciousness);
}
}
}
}
} else {
Logger::error("Country ", country_instance.get_identifier(), " has no history!");
ret = false;
Expand Down
6 changes: 3 additions & 3 deletions src/openvic-simulation/map/MapInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ bool MapInstance::apply_history_to_provinces(
}

if (pop_history_entry == nullptr) {
Logger::warning("No pop history entry for province ",province.get_identifier(), " for date ", date.to_string());
Logger::warning("No pop history entry for province ", province.get_identifier(), " for date ", date);
} else {
province.add_pop_vec(pop_history_entry->get_pops());
ret &= province.add_pop_vec(pop_history_entry->get_pops());
province.setup_pop_test_values(issue_manager);
}

ret&=province.set_rgo_production_type_nullable(rgo_production_type_nullable);
ret &= province.set_rgo_production_type_nullable(rgo_production_type_nullable);
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/openvic-simulation/pop/Pop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

using namespace OpenVic;

static constexpr fixed_point_t DEFAULT_POP_LITERACY = fixed_point_t::_0_10();

PopBase::PopBase(
PopType const& new_type, Culture const& new_culture, Religion const& new_religion, pop_size_t new_size,
fixed_point_t new_militancy, fixed_point_t new_consciousness, RebelType const* new_rebel_type
Expand All @@ -23,6 +25,7 @@ Pop::Pop(PopBase const& pop_base, decltype(ideology_distribution)::keys_type con
num_migrated_internal { 0 },
num_migrated_external { 0 },
num_migrated_colonial { 0 },
literacy { DEFAULT_POP_LITERACY },
ideology_distribution { &ideology_keys },
issue_distribution {},
vote_distribution { nullptr },
Expand Down Expand Up @@ -183,6 +186,17 @@ fixed_point_t Pop::get_party_support(CountryParty const& party) const {
void Pop::update_gamestate(
InstanceManager const& instance_manager, CountryInstance const* owner, const fixed_point_t pop_size_per_regiment_multiplier
) {
static constexpr fixed_point_t MIN_MILITANCY = fixed_point_t::_0();
static constexpr fixed_point_t MAX_MILITANCY = fixed_point_t::_10();
static constexpr fixed_point_t MIN_CONSCIOUSNESS = fixed_point_t::_0();
static constexpr fixed_point_t MAX_CONSCIOUSNESS = fixed_point_t::_10();
static constexpr fixed_point_t MIN_LITERACY = fixed_point_t::_0_01();
static constexpr fixed_point_t MAX_LITERACY = fixed_point_t::_1();

militancy = std::clamp(militancy, MIN_MILITANCY, MAX_MILITANCY);
consciousness = std::clamp(consciousness, MIN_CONSCIOUSNESS, MAX_CONSCIOUSNESS);
literacy = std::clamp(literacy, MIN_LITERACY, MAX_LITERACY);

if (type->get_can_be_recruited()) {
MilitaryDefines const& military_defines =
instance_manager.get_definition_manager().get_define_manager().get_military_defines();
Expand Down
6 changes: 3 additions & 3 deletions src/openvic-simulation/pop/Pop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace OpenVic {
Culture const& PROPERTY_ACCESS(culture, protected);
Religion const& PROPERTY_ACCESS(religion, protected);
pop_size_t PROPERTY_ACCESS(size, protected);
fixed_point_t PROPERTY_ACCESS(militancy, protected);
fixed_point_t PROPERTY_ACCESS(consciousness, protected);
fixed_point_t PROPERTY_RW_ACCESS(militancy, protected);
fixed_point_t PROPERTY_RW_ACCESS(consciousness, protected);
RebelType const* PROPERTY_ACCESS(rebel_type, protected);

PopBase(
Expand Down Expand Up @@ -66,7 +66,7 @@ namespace OpenVic {
pop_size_t PROPERTY(num_migrated_external);
pop_size_t PROPERTY(num_migrated_colonial);

fixed_point_t PROPERTY(literacy);
fixed_point_t PROPERTY_RW(literacy);

// All of these should have a total size equal to the pop size, allowing the distributions from different pops to be
// added together with automatic weighting based on their relative sizes. Similarly, the province, state and country
Expand Down
7 changes: 7 additions & 0 deletions src/openvic-simulation/scripts/Condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ static bool _parse_condition_node_value_callback(
ret = definition_manager.get_politics_manager().get_issue_manager().expect_reform_identifier_or_string(
assign_variable_callback_pointer(value)
)(node);
} else if constexpr (std::same_as<T, NationalValue const*>) {
ret = definition_manager.get_politics_manager().get_national_value_manager().expect_national_value_identifier_or_string(
assign_variable_callback_pointer(value)
)(node);
} else if constexpr (std::same_as<T, Invention const*>) {
ret = definition_manager.get_research_manager().get_invention_manager().expect_invention_identifier_or_string(
assign_variable_callback_pointer(value)
Expand Down Expand Up @@ -589,6 +593,9 @@ static bool _parse_condition_node_value_callback(
"which", ONE_EXACTLY, expect_identifier_or_string(assign_variable_callback_string(value.first)),
"value", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(value.second))
)(node);
} else {
Logger::error("Cannot parse condition \"", condition.get_identifier(), "\": unknown value type!");
ret = false;
}

if (ret) {
Expand Down
14 changes: 13 additions & 1 deletion src/openvic-simulation/types/fixed_point/FixedPoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace OpenVic {
#include "openvic-simulation/types/fixed_point/FixedPointLUT_sin.hpp"

static_assert(SIN_LUT_PRECISION == PRECISION);

// Doesn't account for sign, so -n.abc -> 1 - 0.abc
constexpr fixed_point_t get_frac() const {
return value & FRAC_MASK;
Expand Down Expand Up @@ -83,10 +83,22 @@ namespace OpenVic {
return 4;
}

static constexpr fixed_point_t _10() {
return 10;
}

static constexpr fixed_point_t _100() {
return 100;
}

static constexpr fixed_point_t _0_01() {
return _1() / 100;
}

static constexpr fixed_point_t _0_10() {
return _1() / 10;
}

static constexpr fixed_point_t _0_20() {
return _1() / 5;
}
Expand Down

0 comments on commit 5b3cd6f

Please sign in to comment.