Skip to content

Commit 14e8fa1

Browse files
authored
Merge pull request #696 from OpenVicProject/refactor_province_buildings
Refactor province buildings
2 parents 0bb087a + b1b8bf8 commit 14e8fa1

14 files changed

Lines changed: 185 additions & 93 deletions
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "openvic-simulation/DefinitionManager.hpp"
22

3+
#include "openvic-simulation/history/HistoryManager.hpp"
34
#include "openvic-simulation/interface/UI.hpp"
45

56
using namespace OpenVic;
67

7-
DefinitionManager::DefinitionManager() : ui_manager(*this) {}
8+
DefinitionManager::DefinitionManager() : ui_manager(*this), history_manager(*this) {}

src/openvic-simulation/dataloader/Dataloader.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -965,10 +965,6 @@ bool Dataloader::load_defines(
965965

966966
bool ret = true;
967967

968-
if (!definition_manager.get_mapmode_manager().setup_mapmodes(definition_manager.get_map_definition())) {
969-
spdlog::critical_s("Failed to set up mapmodes!");
970-
ret = false;
971-
}
972968
if (!_load_sound_effect_defines(definition_manager)) {
973969
spdlog::critical_s("Failed to load sound effect defines");
974970
ret = false;
@@ -1033,6 +1029,13 @@ bool Dataloader::load_defines(
10331029
spdlog::critical_s("Failed to load buildings!");
10341030
ret = false;
10351031
}
1032+
if (!definition_manager.get_mapmode_manager().setup_mapmodes(
1033+
definition_manager.get_map_definition(),
1034+
definition_manager.get_economy_manager().get_building_type_manager()
1035+
)) {
1036+
spdlog::critical_s("Failed to set up mapmodes!");
1037+
ret = false;
1038+
}
10361039
if (!_load_map_dir(definition_manager)) {
10371040
spdlog::critical_s("Failed to load map!");
10381041
ret = false;

src/openvic-simulation/economy/BuildingType.cpp

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
#include "BuildingType.hpp"
22

3+
#include <optional>
4+
35
#include "openvic-simulation/economy/GoodDefinition.hpp"
46
#include "openvic-simulation/economy/production/ProductionType.hpp"
57
#include "openvic-simulation/modifier/ModifierEffectCache.hpp"
68
#include "openvic-simulation/modifier/ModifierManager.hpp"
9+
#include "openvic-simulation/types/TypedIndices.hpp"
710

811
using namespace OpenVic;
912
using namespace OpenVic::NodeTools;
1013

1114
BuildingType::BuildingType(
1215
index_t new_index,
13-
std::string_view identifier,
16+
std::optional<province_building_index_t> new_province_building_index,
17+
std::string_view new_identifier,
1418
building_type_args_t& building_type_args
1519
) : HasIndex { new_index },
16-
Modifier { identifier, std::move(building_type_args.modifier), modifier_type_t::BUILDING },
20+
Modifier { new_identifier, std::move(building_type_args.modifier), modifier_type_t::BUILDING },
21+
province_building_index { new_province_building_index },
1722
type { building_type_args.type },
1823
on_completion { building_type_args.on_completion },
1924
completion_size { building_type_args.completion_size },
@@ -40,7 +45,7 @@ BuildingType::BuildingType(
4045
capital { building_type_args.capital },
4146
port { building_type_args.port } {}
4247

43-
BuildingTypeManager::BuildingTypeManager() : port_building_type { nullptr } {}
48+
BuildingTypeManager::BuildingTypeManager() : infrastructure_building_type { nullptr }, port_building_type { nullptr } {}
4449

4550
bool BuildingTypeManager::add_building_type(
4651
std::string_view identifier, BuildingType::building_type_args_t& building_type_args
@@ -49,14 +54,21 @@ bool BuildingTypeManager::add_building_type(
4954
spdlog::error_s("Invalid building identifier - empty!");
5055
return false;
5156
}
57+
58+
std::optional<province_building_index_t> province_building_index = building_type_args.in_province
59+
? std::make_optional<province_building_index_t>(province_building_types.size())
60+
: std::nullopt;
5261

5362
const bool ret = building_types.emplace_item(
5463
identifier,
55-
BuildingType::index_t { get_building_type_count() }, identifier, building_type_args
64+
BuildingType::index_t { get_building_type_count() }, province_building_index, identifier, building_type_args
5665
);
5766

5867
if (ret) {
5968
building_type_types.emplace(building_type_args.type);
69+
if (province_building_index.has_value()) {
70+
province_building_types.emplace_back(&building_types.back());
71+
}
6072
}
6173

6274
return ret;
@@ -146,13 +158,8 @@ bool BuildingTypeManager::load_buildings_file(
146158
this_building_type_effects.min_level, append_string_views(min_prefix, building_type.get_identifier()),
147159
FORMAT_x1_0DP_NEG
148160
);
149-
150161
if (building_type.is_in_province()) {
151-
province_building_types.emplace_back(&building_type);
152-
}
153-
154-
if (building_type.is_port()) {
155-
if (building_type.is_in_province()) {
162+
if (building_type.is_port()) {
156163
if (port_building_type == nullptr) {
157164
port_building_type = &building_type;
158165
} else {
@@ -162,7 +169,19 @@ bool BuildingTypeManager::load_buildings_file(
162169
);
163170
ret = false;
164171
}
165-
} else {
172+
} else if (building_type.get_type() == "infrastructure") {
173+
if (infrastructure_building_type == nullptr) {
174+
infrastructure_building_type = &building_type;
175+
} else {
176+
spdlog::error_s(
177+
"Building type {} is marked as a infrastructure, but we are already using {} as the infrastructure building type!",
178+
building_type, *infrastructure_building_type
179+
);
180+
ret = false;
181+
}
182+
}
183+
} else {
184+
if (building_type.is_port()) {
166185
spdlog::error_s(
167186
"Building type {} is marked as a port, but is not a province building!", building_type
168187
);
@@ -171,6 +190,11 @@ bool BuildingTypeManager::load_buildings_file(
171190
}
172191
}
173192

193+
if (infrastructure_building_type == nullptr) {
194+
spdlog::error_s("No infrastructure building type found!");
195+
ret = false;
196+
}
197+
174198
if (port_building_type == nullptr) {
175199
spdlog::error_s("No port building type found!");
176200
ret = false;

src/openvic-simulation/economy/BuildingType.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <optional>
4+
35
#include "openvic-simulation/modifier/Modifier.hpp"
46
#include "openvic-simulation/economy/BuildingLevel.hpp"
57
#include "openvic-simulation/types/Date.hpp"
@@ -72,9 +74,15 @@ namespace OpenVic {
7274
bool PROPERTY(steam); // only in steamer shipyard
7375
bool PROPERTY(capital); // only in naval base
7476
bool PROPERTY_CUSTOM_PREFIX(port, is); // only in naval base
77+
std::optional<province_building_index_t> PROPERTY(province_building_index);
7578

7679
public:
77-
BuildingType(index_t new_index, std::string_view identifier, building_type_args_t& building_type_args);
80+
BuildingType(
81+
index_t new_index,
82+
std::optional<province_building_index_t> new_province_building_index,
83+
std::string_view new_identifier,
84+
building_type_args_t& building_type_args
85+
);
7886
BuildingType(BuildingType&&) = default;
7987
};
8088

@@ -83,6 +91,7 @@ namespace OpenVic {
8391
IdentifierRegistry<BuildingType> IDENTIFIER_REGISTRY(building_type);
8492
string_set_t PROPERTY(building_type_types);
8593
memory::vector<BuildingType const*> SPAN_PROPERTY(province_building_types);
94+
BuildingType const* PROPERTY(infrastructure_building_type);
8695
BuildingType const* PROPERTY(port_building_type);
8796

8897
public:

src/openvic-simulation/history/HistoryManager.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
#include "openvic-simulation/history/ProvinceHistory.hpp"
77

88
namespace OpenVic {
9+
struct DefinitionManager;
10+
911
struct HistoryManager {
1012
private:
1113
BookmarkManager PROPERTY_REF(bookmark_manager);
1214
CountryHistoryManager PROPERTY_REF(country_manager);
1315
ProvinceHistoryManager PROPERTY_REF(province_manager);
1416
DiplomaticHistoryManager PROPERTY_REF(diplomacy_manager);
17+
public:
18+
HistoryManager(DefinitionManager const& definition_manager) : province_manager { definition_manager } {}
1519
};
1620
}

src/openvic-simulation/history/ProvinceHistory.cpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "openvic-simulation/dataloader/NodeTools.hpp"
66
#include "openvic-simulation/DefinitionManager.hpp"
7+
#include "openvic-simulation/economy/BuildingLevel.hpp"
78
#include "openvic-simulation/economy/GoodDefinition.hpp"
89
#include "openvic-simulation/map/ProvinceDefinition.hpp"
910
#include "openvic-simulation/utility/Logger.hpp"
@@ -12,13 +13,26 @@
1213
using namespace OpenVic;
1314
using namespace OpenVic::NodeTools;
1415

15-
ProvinceHistoryEntry::ProvinceHistoryEntry(ProvinceDefinition const& new_province, Date new_date)
16-
: HistoryEntry { new_date }, province { new_province } {}
16+
ProvinceHistoryEntry::ProvinceHistoryEntry(
17+
BuildingTypeManager const& building_type_manager,
18+
ProvinceDefinition const& new_province,
19+
Date new_date
20+
) : HistoryEntry { new_date },
21+
province { new_province },
22+
_province_building_levels(
23+
building_type_manager.get_province_building_types().size(),
24+
building_level_t(0)
25+
),
26+
province_building_levels(_province_building_levels)
27+
{}
1728

18-
ProvinceHistoryMap::ProvinceHistoryMap(ProvinceDefinition const& new_province) : province { new_province } {}
29+
ProvinceHistoryMap::ProvinceHistoryMap(ProvinceDefinition const& new_province, BuildingTypeManager const& new_building_type_manager)
30+
: province { new_province },
31+
building_type_manager { new_building_type_manager }
32+
{}
1933

2034
memory::unique_ptr<ProvinceHistoryEntry> ProvinceHistoryMap::_make_entry(Date date) const {
21-
return memory::make_unique<ProvinceHistoryEntry>(province, date);
35+
return memory::make_unique<ProvinceHistoryEntry>(building_type_manager, province, date);
2236
}
2337

2438
bool ProvinceHistoryMap::_load_history_entry(
@@ -85,7 +99,17 @@ bool ProvinceHistoryMap::_load_history_entry(
8599
return expect_strong_typedef<building_level_t>(
86100
/* This is set to warn to prevent vanilla from always having errors because
87101
* of a duplicate railroad entry in the 1861.1.1 history of Manchester (278). */
88-
map_callback(entry.province_buildings, building_type, true)
102+
[
103+
&entry,
104+
optional_index = building_type->get_province_building_index()
105+
](const building_level_t level) -> bool {
106+
if (!optional_index.has_value()) {
107+
return false;
108+
}
109+
110+
entry.province_building_levels[optional_index.value()] = level;
111+
return true;
112+
}
89113
)(value);
90114
} else {
91115
spdlog::error_s(
@@ -167,7 +191,7 @@ bool ProvinceHistoryMap::_load_history_entry(
167191
)(node);
168192
if (building_type != nullptr) {
169193
if (!building_type->is_in_province()) {
170-
ret &= map_callback(entry.state_buildings, building_type, true)(level);
194+
ret &= map_callback(entry.state_buildings, building_type->index, true)(level);
171195
} else {
172196
spdlog::error_s(
173197
"Attempted to add province building \"{}\" to state building list of province history for {}",
@@ -241,7 +265,13 @@ ProvinceHistoryMap* ProvinceHistoryManager::_get_or_make_province_history(Provin
241265
decltype(province_histories)::iterator it = province_histories.find(&province);
242266
if (it == province_histories.end()) {
243267
const std::pair<decltype(province_histories)::iterator, bool> result =
244-
province_histories.emplace(&province, ProvinceHistoryMap { province });
268+
province_histories.emplace(
269+
&province,
270+
ProvinceHistoryMap {
271+
province,
272+
definition_manager.get_economy_manager().get_building_type_manager()
273+
}
274+
);
245275
if (result.second) {
246276
it = result.first;
247277
} else {

src/openvic-simulation/history/ProvinceHistory.hpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
#include "openvic-simulation/economy/BuildingLevel.hpp"
99
#include "openvic-simulation/types/ColonyStatus.hpp"
1010
#include "openvic-simulation/types/Date.hpp"
11-
#include "openvic-simulation/types/OrderedContainers.hpp"
1211
#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp"
12+
#include "openvic-simulation/types/FixedVector.hpp"
13+
#include "openvic-simulation/types/OrderedContainers.hpp"
14+
#include "openvic-simulation/types/TypedSpan.hpp"
15+
#include "openvic-simulation/types/TypedIndices.hpp"
1316
#include "openvic-simulation/map/LifeRating.hpp"
1417
#include "openvic-simulation/utility/Containers.hpp"
1518
#include "openvic-simulation/utility/Getters.hpp"
@@ -26,7 +29,11 @@ namespace OpenVic {
2629
struct ProvinceHistoryEntry : HistoryEntry {
2730
friend struct ProvinceHistoryMap;
2831

29-
ProvinceHistoryEntry(ProvinceDefinition const& new_province, Date new_date);
32+
ProvinceHistoryEntry(
33+
BuildingTypeManager const& building_type_manager,
34+
ProvinceDefinition const& new_province,
35+
Date new_date
36+
);
3037
private:
3138
std::optional<CountryDefinition const*> PROPERTY(owner);
3239
std::optional<CountryDefinition const*> PROPERTY(controller);
@@ -36,8 +43,14 @@ namespace OpenVic {
3643
std::optional<ProductionType const*> PROPERTY(rgo_production_type_nullable);
3744
std::optional<life_rating_t> PROPERTY(life_rating);
3845
std::optional<TerrainType const*> PROPERTY(terrain_type);
39-
ordered_map<BuildingType const*, building_level_t> PROPERTY(province_buildings);
40-
ordered_map<BuildingType const*, building_level_t> PROPERTY(state_buildings);
46+
memory::FixedVector<building_level_t> _province_building_levels;
47+
TypedSpan<province_building_index_t, building_level_t> province_building_levels;
48+
public:
49+
constexpr TypedSpan<province_building_index_t, const building_level_t> get_province_building_levels() const {
50+
return province_building_levels;
51+
}
52+
private:
53+
ordered_map<building_type_index_t, building_level_t> PROPERTY(state_buildings);
4154
fixed_point_map_t<Ideology const*> PROPERTY(party_loyalties);
4255
memory::vector<PopBase> SPAN_PROPERTY(pops);
4356

@@ -55,9 +68,10 @@ namespace OpenVic {
5568

5669
private:
5770
ProvinceDefinition const& province;
71+
BuildingTypeManager const& building_type_manager;
5872

5973
protected:
60-
ProvinceHistoryMap(ProvinceDefinition const& new_province);
74+
ProvinceHistoryMap(ProvinceDefinition const& new_province, BuildingTypeManager const& new_building_type_manager);
6175

6276
memory::unique_ptr<ProvinceHistoryEntry> _make_entry(Date date) const override;
6377
bool _load_history_entry(DefinitionManager const& definition_manager, ProvinceHistoryEntry& entry, ast::NodeCPtr root)
@@ -73,13 +87,14 @@ namespace OpenVic {
7387

7488
struct ProvinceHistoryManager {
7589
private:
90+
DefinitionManager const& definition_manager;
7691
ordered_map<ProvinceDefinition const*, ProvinceHistoryMap> province_histories;
7792
bool locked = false;
7893

7994
ProvinceHistoryMap* _get_or_make_province_history(ProvinceDefinition const& province);
8095

8196
public:
82-
ProvinceHistoryManager() {};
97+
ProvinceHistoryManager(DefinitionManager const& new_definition_manager) : definition_manager { new_definition_manager } {};
8398

8499
void reserve_more_province_histories(size_t size);
85100
void lock_province_histories(MapDefinition const& map_definition, bool detailed_errors);

0 commit comments

Comments
 (0)