Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 10 additions & 4 deletions src/openvic-simulation/InstanceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ InstanceManager::InstanceManager(
DefinitionManager const& new_definition_manager,
gamestate_updated_func_t gamestate_updated_callback,
SimulationClock::state_changed_function_t clock_state_changed_callback
) : game_rules_manager { new_game_rules_manager },
) : definition_manager { new_definition_manager },
game_rules_manager { new_game_rules_manager },
good_instance_manager { new_definition_manager.get_economy_manager().get_good_definition_manager() },
market_instance { good_instance_manager },
artisanal_producer_factory_pattern {
market_instance,
new_definition_manager.get_modifier_manager().get_modifier_effect_cache(),
new_definition_manager.get_economy_manager().get_production_type_manager()
},
definition_manager { new_definition_manager },
global_flags { "global" },
country_instance_manager { new_definition_manager.get_country_definition_manager() },
politics_instance_manager { *this },
Expand Down Expand Up @@ -71,6 +72,10 @@ void InstanceManager::tick() {
unit_instance_manager.tick();
market_instance.execute_orders();

if (today.is_month_start()) {
market_instance.record_price_history();
}

set_gamestate_needs_update();
}

Expand All @@ -81,7 +86,6 @@ bool InstanceManager::setup() {
}

bool ret = good_instance_manager.setup_goods(
definition_manager.get_economy_manager().get_good_definition_manager(),
game_rules_manager
);
ret &= map_instance.setup(
Expand All @@ -101,9 +105,11 @@ bool InstanceManager::setup() {
definition_manager.get_politics_manager().get_government_type_manager().get_government_types(),
definition_manager.get_crime_manager().get_crime_modifiers(),
definition_manager.get_pop_manager().get_pop_types(),
good_instance_manager.get_good_instances(),
definition_manager.get_military_manager().get_unit_type_manager().get_regiment_types(),
definition_manager.get_military_manager().get_unit_type_manager().get_ship_types(),
definition_manager.get_pop_manager().get_stratas()
definition_manager.get_pop_manager().get_stratas(),
good_instance_manager
);

game_instance_setup = true;
Expand Down
3 changes: 2 additions & 1 deletion src/openvic-simulation/InstanceManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ namespace OpenVic {
using gamestate_updated_func_t = std::function<void()>;

private:
DefinitionManager const& PROPERTY(definition_manager);

GameRulesManager const& game_rules_manager;
GoodInstanceManager PROPERTY_REF(good_instance_manager);
MarketInstance PROPERTY_REF(market_instance);
ArtisanalProducerFactoryPattern artisanal_producer_factory_pattern;
DefinitionManager const& PROPERTY(definition_manager);

FlagStrings PROPERTY_REF(global_flags);

Expand Down
69 changes: 47 additions & 22 deletions src/openvic-simulation/country/CountryInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ CountryInstance::CountryInstance(
decltype(government_flag_overrides)::keys_type const& government_type_keys,
decltype(crime_unlock_levels)::keys_type const& crime_keys,
decltype(pop_type_distribution)::keys_type const& pop_type_keys,
decltype(goods_data)::keys_type const& good_instances_keys,
decltype(regiment_type_unlock_levels)::keys_type const& regiment_type_unlock_levels_keys,
decltype(ship_type_unlock_levels)::keys_type const& ship_type_unlock_levels_keys,
decltype(tax_rate_by_strata)::keys_type const& strata_keys
decltype(tax_rate_by_strata)::keys_type const& strata_keys,
GoodInstanceManager& good_instance_manager
) : FlagStrings { "country" },
/* Main attributes */
country_definition { new_country_definition },
Expand Down Expand Up @@ -68,6 +70,7 @@ CountryInstance::CountryInstance(
vote_distribution { nullptr },

/* Trade */
goods_data { &good_instances_keys },

/* Diplomacy */

Expand All @@ -82,7 +85,7 @@ CountryInstance::CountryInstance(

for (BuildingType const& building_type : *building_type_unlock_levels.get_keys()) {
if (building_type.is_default_enabled()) {
unlock_building_type(building_type);
unlock_building_type(building_type, good_instance_manager);
}
}

Expand Down Expand Up @@ -448,7 +451,9 @@ bool CountryInstance::is_unit_type_unlocked(UnitType const& unit_type) const {
}
}

bool CountryInstance::modify_building_type_unlock(BuildingType const& building_type, unlock_level_t unlock_level_change) {
bool CountryInstance::modify_building_type_unlock(
BuildingType const& building_type, unlock_level_t unlock_level_change, GoodInstanceManager& good_instance_manager
) {
decltype(building_type_unlock_levels)::value_ref_type unlock_level = building_type_unlock_levels[building_type];

// This catches subtracting below 0 or adding above the int types maximum value
Expand All @@ -464,11 +469,15 @@ bool CountryInstance::modify_building_type_unlock(BuildingType const& building_t

unlock_level += unlock_level_change;

if (building_type.get_production_type() != nullptr) {
good_instance_manager.enable_good(building_type.get_production_type()->get_output_good());
}

return true;
}

bool CountryInstance::unlock_building_type(BuildingType const& building_type) {
return modify_building_type_unlock(building_type, 1);
bool CountryInstance::unlock_building_type(BuildingType const& building_type, GoodInstanceManager& good_instance_manager) {
return modify_building_type_unlock(building_type, 1, good_instance_manager);
}

bool CountryInstance::is_building_type_unlocked(BuildingType const& building_type) const {
Expand Down Expand Up @@ -594,7 +603,9 @@ CountryInstance::unit_variant_t CountryInstance::get_max_unlocked_unit_variant()
return unit_variant_unlock_levels.size();
}

bool CountryInstance::modify_technology_unlock(Technology const& technology, unlock_level_t unlock_level_change) {
bool CountryInstance::modify_technology_unlock(
Technology const& technology, unlock_level_t unlock_level_change, GoodInstanceManager& good_instance_manager
) {
decltype(technology_unlock_levels)::value_ref_type unlock_level = technology_unlock_levels[technology];

// This catches subtracting below 0 or adding above the int types maximum value
Expand All @@ -621,26 +632,30 @@ bool CountryInstance::modify_technology_unlock(Technology const& technology, unl
ret &= modify_unit_type_unlock(*unit, unlock_level_change);
}
for (BuildingType const* building : technology.get_activated_buildings()) {
ret &= modify_building_type_unlock(*building, unlock_level_change);
ret &= modify_building_type_unlock(*building, unlock_level_change, good_instance_manager);
}

return ret;
}

bool CountryInstance::set_technology_unlock_level(Technology const& technology, unlock_level_t unlock_level) {
bool CountryInstance::set_technology_unlock_level(
Technology const& technology, unlock_level_t unlock_level, GoodInstanceManager& good_instance_manager
) {
const unlock_level_t unlock_level_change = unlock_level - technology_unlock_levels[technology];
return unlock_level_change != 0 ? modify_technology_unlock(technology, unlock_level_change) : true;
return unlock_level_change != 0 ? modify_technology_unlock(technology, unlock_level_change, good_instance_manager) : true;
}

bool CountryInstance::unlock_technology(Technology const& technology) {
return modify_technology_unlock(technology, 1);
bool CountryInstance::unlock_technology(Technology const& technology, GoodInstanceManager& good_instance_manager) {
return modify_technology_unlock(technology, 1, good_instance_manager);
}

bool CountryInstance::is_technology_unlocked(Technology const& technology) const {
return technology_unlock_levels[technology] > 0;
}

bool CountryInstance::modify_invention_unlock(Invention const& invention, unlock_level_t unlock_level_change) {
bool CountryInstance::modify_invention_unlock(
Invention const& invention, unlock_level_t unlock_level_change, GoodInstanceManager& good_instance_manager
) {
decltype(invention_unlock_levels)::value_ref_type unlock_level = invention_unlock_levels[invention];

// This catches subtracting below 0 or adding above the int types maximum value
Expand All @@ -664,7 +679,7 @@ bool CountryInstance::modify_invention_unlock(Invention const& invention, unlock
ret &= modify_unit_type_unlock(*unit, unlock_level_change);
}
for (BuildingType const* building : invention.get_activated_buildings()) {
ret &= modify_building_type_unlock(*building, unlock_level_change);
ret &= modify_building_type_unlock(*building, unlock_level_change, good_instance_manager);
}
for (Crime const* crime : invention.get_enabled_crimes()) {
ret &= modify_crime_unlock(*crime, unlock_level_change);
Expand All @@ -679,13 +694,15 @@ bool CountryInstance::modify_invention_unlock(Invention const& invention, unlock
return ret;
}

bool CountryInstance::set_invention_unlock_level(Invention const& invention, unlock_level_t unlock_level) {
bool CountryInstance::set_invention_unlock_level(
Invention const& invention, unlock_level_t unlock_level, GoodInstanceManager& good_instance_manager
) {
const unlock_level_t unlock_level_change = unlock_level - invention_unlock_levels[invention];
return unlock_level_change != 0 ? modify_invention_unlock(invention, unlock_level_change) : true;
return unlock_level_change != 0 ? modify_invention_unlock(invention, unlock_level_change, good_instance_manager) : true;
}

bool CountryInstance::unlock_invention(Invention const& invention) {
return modify_invention_unlock(invention, 1);
bool CountryInstance::unlock_invention(Invention const& invention, GoodInstanceManager& good_instance_manager) {
return modify_invention_unlock(invention, 1, good_instance_manager);
}

bool CountryInstance::is_invention_unlocked(Invention const& invention) const {
Expand Down Expand Up @@ -798,12 +815,16 @@ bool CountryInstance::apply_history_to_country(CountryHistoryEntry const& entry,
target[*key] = value;
}
};

GoodInstanceManager& good_instance_manager = instance_manager.get_good_instance_manager();

for (auto const& [technology, level] : entry.get_technologies()) {
ret &= set_technology_unlock_level(*technology, level);
ret &= set_technology_unlock_level(*technology, level, good_instance_manager);
}
for (auto const& [invention, activated] : entry.get_inventions()) {
ret &= set_invention_unlock_level(*invention, activated ? 1 : 0);
ret &= set_invention_unlock_level(*invention, activated ? 1 : 0, good_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 @@ -1333,7 +1354,7 @@ void CountryInstance::tick(InstanceManager& instance_manager) {
invested_research_points += research_points_spent;

if (invested_research_points >= current_research_cost) {
unlock_technology(*current_research);
unlock_technology(*current_research, instance_manager.get_good_instance_manager());
current_research = nullptr;
invested_research_points = fixed_point_t::_0();
current_research_cost = fixed_point_t::_0();
Expand Down Expand Up @@ -1502,9 +1523,11 @@ bool CountryInstanceManager::generate_country_instances(
decltype(CountryInstance::government_flag_overrides)::keys_type const& government_type_keys,
decltype(CountryInstance::crime_unlock_levels)::keys_type const& crime_keys,
decltype(CountryInstance::pop_type_distribution)::keys_type const& pop_type_keys,
decltype(CountryInstance::goods_data)::keys_type const& good_instances_keys,
decltype(CountryInstance::regiment_type_unlock_levels)::keys_type const& regiment_type_unlock_levels_keys,
decltype(CountryInstance::ship_type_unlock_levels)::keys_type const& ship_type_unlock_levels_keys,
decltype(CountryInstance::tax_rate_by_strata):: keys_type const& strata_keys
decltype(CountryInstance::tax_rate_by_strata):: keys_type const& strata_keys,
GoodInstanceManager& good_instance_manager
) {
reserve_more(country_instances, country_definition_manager.get_country_definition_count());

Expand All @@ -1521,9 +1544,11 @@ bool CountryInstanceManager::generate_country_instances(
government_type_keys,
crime_keys,
pop_type_keys,
good_instances_keys,
regiment_type_unlock_levels_keys,
ship_type_unlock_levels_keys,
strata_keys
strata_keys,
good_instance_manager
})) {
// We need to update the country's ModifierSum's source here as the country's address is finally stable
// after changing between its constructor call and now due to being std::move'd into the registry.
Expand Down
60 changes: 49 additions & 11 deletions src/openvic-simulation/country/CountryInstance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace OpenVic {
struct Culture;
struct Religion;
struct BuildingType;
struct GoodInstance;
struct GoodInstanceManager;
struct CountryHistoryEntry;
struct DefineManager;
struct ModifierEffectCache;
Expand Down Expand Up @@ -186,7 +188,29 @@ namespace OpenVic {
// TODO - national foci

/* Trade */
// TODO - total amount of each good exported and imported
public:
struct good_data_t {
fixed_point_t stockpile_amount;
fixed_point_t stockpile_change_yesterday; // positive if we bought, negative if we sold

bool is_automated = true;
bool is_selling = false; // buying if false
fixed_point_t stockpile_cutoff;

fixed_point_t exported_amount; // negative if net importing

fixed_point_t government_needs;
fixed_point_t army_needs;
fixed_point_t navy_needs;
fixed_point_t production_needs;
fixed_point_t overseas_needs;
fixed_point_t factory_needs;
fixed_point_t pop_needs;
fixed_point_t available_amount;
};

private:
IndexedMap<GoodInstance, good_data_t> PROPERTY(goods_data);

/* Diplomacy */
fixed_point_t PROPERTY(prestige);
Expand Down Expand Up @@ -253,9 +277,11 @@ namespace OpenVic {
decltype(government_flag_overrides)::keys_type const& government_type_keys,
decltype(crime_unlock_levels)::keys_type const& crime_keys,
decltype(pop_type_distribution)::keys_type const& pop_type_keys,
decltype(goods_data)::keys_type const& good_instances_keys,
decltype(regiment_type_unlock_levels)::keys_type const& regiment_type_unlock_levels_keys,
decltype(ship_type_unlock_levels)::keys_type const& ship_type_unlock_levels_keys,
decltype(tax_rate_by_strata)::keys_type const& strata_keys
decltype(tax_rate_by_strata)::keys_type const& strata_keys,
GoodInstanceManager& good_instance_manager
);

public:
Expand Down Expand Up @@ -399,8 +425,10 @@ namespace OpenVic {
bool unlock_unit_type(UnitType const& unit_type);
bool is_unit_type_unlocked(UnitType const& unit_type) const;

bool modify_building_type_unlock(BuildingType const& building_type, unlock_level_t unlock_level_change);
bool unlock_building_type(BuildingType const& building_type);
bool modify_building_type_unlock(
BuildingType const& building_type, unlock_level_t unlock_level_change, GoodInstanceManager& good_instance_manager
);
bool unlock_building_type(BuildingType const& building_type, GoodInstanceManager& good_instance_manager);
bool is_building_type_unlocked(BuildingType const& building_type) const;

bool modify_crime_unlock(Crime const& crime, unlock_level_t unlock_level_change);
Expand All @@ -419,14 +447,22 @@ namespace OpenVic {
bool unlock_unit_variant(unit_variant_t unit_variant);
unit_variant_t get_max_unlocked_unit_variant() const;

bool modify_technology_unlock(Technology const& technology, unlock_level_t unlock_level_change);
bool set_technology_unlock_level(Technology const& technology, unlock_level_t unlock_level);
bool unlock_technology(Technology const& technology);
bool modify_technology_unlock(
Technology const& technology, unlock_level_t unlock_level_change, GoodInstanceManager& good_instance_manager
);
bool set_technology_unlock_level(
Technology const& technology, unlock_level_t unlock_level, GoodInstanceManager& good_instance_manager
);
bool unlock_technology(Technology const& technology, GoodInstanceManager& good_instance_manager);
bool is_technology_unlocked(Technology const& technology) const;

bool modify_invention_unlock(Invention const& invention, unlock_level_t unlock_level_change);
bool set_invention_unlock_level(Invention const& invention, unlock_level_t unlock_level);
bool unlock_invention(Invention const& invention);
bool modify_invention_unlock(
Invention const& invention, unlock_level_t unlock_level_change, GoodInstanceManager& good_instance_manager
);
bool set_invention_unlock_level(
Invention const& invention, unlock_level_t unlock_level, GoodInstanceManager& good_instance_manager
);
bool unlock_invention(Invention const& invention, GoodInstanceManager& good_instance_manager);
bool is_invention_unlocked(Invention const& invention) const;

bool is_primary_culture(Culture const& culture) const;
Expand Down Expand Up @@ -517,9 +553,11 @@ namespace OpenVic {
decltype(CountryInstance::government_flag_overrides)::keys_type const& government_type_keys,
decltype(CountryInstance::crime_unlock_levels)::keys_type const& crime_keys,
decltype(CountryInstance::pop_type_distribution)::keys_type const& pop_type_keys,
decltype(CountryInstance::goods_data)::keys_type const& good_instances_keys,
decltype(CountryInstance::regiment_type_unlock_levels)::keys_type const& regiment_type_unlock_levels_keys,
decltype(CountryInstance::ship_type_unlock_levels)::keys_type const& ship_type_unlock_levels_keys,
decltype(CountryInstance::tax_rate_by_strata):: keys_type const& strata_keys
decltype(CountryInstance::tax_rate_by_strata):: keys_type const& strata_keys,
GoodInstanceManager& good_instance_manager
);

bool apply_history_to_countries(CountryHistoryManager const& history_manager, InstanceManager& instance_manager);
Expand Down
Loading
Loading