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
811using namespace OpenVic ;
912using namespace OpenVic ::NodeTools;
1013
1114BuildingType::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
4550bool 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 ;
0 commit comments