Skip to content

Commit 6abcf83

Browse files
authored
Merge pull request #544 from OpenVicProject/update_states_and_allowed_regiment_cultures
Update states and allowed regiment cultures
2 parents 61ee374 + 4bdddf8 commit 6abcf83

File tree

4 files changed

+74
-17
lines changed

4 files changed

+74
-17
lines changed

src/openvic-simulation/InstanceManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ bool InstanceManager::load_bookmark(Bookmark const* new_bookmark) {
243243
OV_ERR_FAIL_COND_V_MSG(!all_has_state, false, "At least one land province has no state");
244244

245245
update_modifier_sums();
246-
country_instance_manager.update_gamestate(*this);
247246
map_instance.initialise_for_new_game(*this);
247+
country_instance_manager.update_gamestate(*this);
248248
market_instance.execute_orders();
249249

250250
return ret;

src/openvic-simulation/country/CountryInstance.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "openvic-simulation/map/Crime.hpp"
1717
#include "openvic-simulation/map/MapInstance.hpp"
1818
#include "openvic-simulation/misc/GameRulesManager.hpp"
19+
#include "openvic-simulation/military/UnitType.hpp"
1920
#include "openvic-simulation/modifier/ModifierEffectCache.hpp"
2021
#include "openvic-simulation/modifier/StaticModifierCache.hpp"
2122
#include "openvic-simulation/pop/Pop.hpp"
@@ -26,6 +27,7 @@
2627
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
2728
#include "openvic-simulation/types/IndexedFlatMap.hpp"
2829
#include "openvic-simulation/types/PopSize.hpp"
30+
#include "openvic-simulation/types/UnitBranchType.hpp"
2931
#include "openvic-simulation/utility/Containers.hpp"
3032
#include "openvic-simulation/utility/Utility.hpp"
3133

@@ -722,7 +724,35 @@ bool CountryInstance::modify_unit_type_unlock(UnitTypeBranched<Branch> const& un
722724
return false;
723725
}
724726

725-
unlock_level += unlock_level_change;
727+
if constexpr (Branch != unit_branch_t::LAND) {
728+
unlock_level += unlock_level_change;
729+
} else {
730+
bool was_unlocked = is_unit_type_unlocked(unit_type);
731+
unlock_level += unlock_level_change;
732+
bool is_unlocked = is_unit_type_unlocked(unit_type);
733+
734+
if (was_unlocked != is_unlocked) {
735+
if (was_unlocked) {
736+
//recalculate entirely
737+
allowed_regiment_cultures = regiment_allowed_cultures_t::NO_CULTURES;
738+
for (RegimentType const& regiment_type : unlocked_unit_types.get_keys()) {
739+
if (!is_unit_type_unlocked(regiment_type)) {
740+
continue;
741+
}
742+
743+
allowed_regiment_cultures = RegimentType::allowed_cultures_get_most_permissive(
744+
allowed_regiment_cultures,
745+
regiment_type.get_allowed_cultures()
746+
);
747+
}
748+
} else {
749+
allowed_regiment_cultures = RegimentType::allowed_cultures_get_most_permissive(
750+
allowed_regiment_cultures,
751+
unit_type.get_allowed_cultures()
752+
);
753+
}
754+
}
755+
}
726756

727757
return true;
728758
}

src/openvic-simulation/map/State.cpp

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ using namespace OpenVic;
1616

1717
State::State(
1818
StateSet const& new_state_set,
19-
CountryInstance* new_owner,
2019
ProvinceInstance* new_capital,
2120
memory::vector<ProvinceInstance*>&& new_provinces,
2221
colony_status_t new_colony_status,
@@ -25,21 +24,33 @@ State::State(
2524
utility::forwardable_span<const Ideology> ideology_keys
2625
) : PopsAggregate { strata_keys, pop_type_keys, ideology_keys },
2726
state_set { new_state_set },
28-
owner { new_owner },
2927
capital { new_capital },
3028
provinces { std::move(new_provinces) },
3129
colony_status { new_colony_status },
32-
pops_cache_by_type { pop_type_keys } {
33-
update_parties_for_votes(new_owner);
34-
}
30+
pops_cache_by_type { pop_type_keys }
31+
{
32+
_update_country();
33+
}
3534

3635
memory::string State::get_identifier() const {
36+
CountryInstance const* const owner_ptr = get_owner();
3737
return StringUtils::append_string_views(
38-
state_set.get_region().get_identifier(), "_", owner->get_identifier(), "_",
38+
state_set.get_region().get_identifier(),
39+
"_",
40+
owner_ptr == nullptr
41+
? "NoCountry"
42+
: owner_ptr->get_identifier(),
43+
"_",
3944
ProvinceInstance::get_colony_status_string(colony_status)
4045
);
4146
}
4247

48+
CountryInstance* State::get_owner() const {
49+
return capital == nullptr
50+
? static_cast<CountryInstance*>(nullptr)
51+
: capital->get_owner();
52+
}
53+
4354
memory::vector<Pop*> const& State::get_pops_cache_by_type(PopType const& pop_type) const {
4455
return pops_cache_by_type.at(pop_type);
4556
}
@@ -84,6 +95,25 @@ void State::update_gamestate() {
8495
}
8596

8697
industrial_power = total_factory_levels_in_state * workforce_scalar;
98+
_update_country();
99+
}
100+
101+
void State::_update_country() {
102+
CountryInstance* const owner_ptr = get_owner();
103+
if (owner_ptr == previous_country_ptr) {
104+
return;
105+
}
106+
107+
update_parties_for_votes(owner_ptr);
108+
if (previous_country_ptr != nullptr) {
109+
previous_country_ptr->remove_state(*this);
110+
}
111+
112+
if (owner_ptr != nullptr) {
113+
owner_ptr->add_state(*this);
114+
}
115+
116+
previous_country_ptr = owner_ptr;
87117
}
88118

89119
/* Whether two provinces in the same region should be grouped into the same state or not.
@@ -146,22 +176,16 @@ bool StateManager::add_state_set(
146176
for (memory::vector<ProvinceInstance*>& provinces : temp_provinces) {
147177
ProvinceInstance* capital = provinces.front();
148178

149-
CountryInstance* owner = capital->get_owner();
150-
151179
State& state = *state_set.states.emplace(
152180
/* TODO: capital province logic */
153-
state_set, owner, capital,
181+
state_set, capital,
154182
std::move(provinces), capital->get_colony_status(),
155183
strata_keys, pop_type_keys, ideology_keys
156184
);
157185

158186
for (ProvinceInstance* province : state.get_provinces()) {
159187
province->set_state(&state);
160188
}
161-
162-
if (owner != nullptr) {
163-
owner->add_state(state);
164-
}
165189
}
166190

167191
return true;

src/openvic-simulation/map/State.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,21 @@ namespace OpenVic {
2828
friend struct StateManager;
2929

3030
private:
31+
CountryInstance* previous_country_ptr = nullptr;
32+
3133
StateSet const& PROPERTY(state_set);
32-
CountryInstance* PROPERTY_PTR(owner);
3334
ProvinceInstance* PROPERTY_PTR(capital);
3435
memory::vector<ProvinceInstance*> SPAN_PROPERTY(provinces);
3536
colony_status_t PROPERTY(colony_status);
3637
fixed_point_t PROPERTY(industrial_power);
3738

3839
IndexedFlatMap_PROPERTY(PopType, memory::vector<Pop*>, pops_cache_by_type);
3940

41+
void _update_country();
42+
4043
public:
4144
State(
4245
StateSet const& new_state_set,
43-
CountryInstance* new_owner,
4446
ProvinceInstance* new_capital,
4547
memory::vector<ProvinceInstance*>&& new_provinces,
4648
colony_status_t new_colony_status,
@@ -54,6 +56,7 @@ namespace OpenVic {
5456
State& operator=(State const&) = delete;
5557

5658
memory::string get_identifier() const;
59+
CountryInstance* get_owner() const;
5760

5861
constexpr bool is_colonial_state() const {
5962
return is_colonial(colony_status);

0 commit comments

Comments
 (0)