aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/map
diff options
context:
space:
mode:
author hop311 <hop3114@gmail.com>2023-12-25 01:22:01 +0100
committer hop311 <hop3114@gmail.com>2023-12-25 01:22:01 +0100
commit48204950cd8d48e6df26ed6892f57b3d9da823de (patch)
tree99822c48f16eb1495f7ea9fe669bcb43e7a6001c /src/openvic-simulation/map
parentbf4e7368600bb425b6612231fbb84de34ec99a27 (diff)
Province building scaffolding
Diffstat (limited to 'src/openvic-simulation/map')
-rw-r--r--src/openvic-simulation/map/Map.cpp24
-rw-r--r--src/openvic-simulation/map/Map.hpp5
-rw-r--r--src/openvic-simulation/map/Province.cpp11
-rw-r--r--src/openvic-simulation/map/Province.hpp2
4 files changed, 24 insertions, 18 deletions
diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp
index aa8ed5e..fde3b3a 100644
--- a/src/openvic-simulation/map/Map.cpp
+++ b/src/openvic-simulation/map/Map.cpp
@@ -30,7 +30,7 @@ Mapmode::base_stripe_t Mapmode::get_base_stripe_colours(Map const& map, Province
}
Map::Map()
- : width { 0 }, height { 0 }, max_provinces { Province::MAX_INDEX }, selected_province_index { Province::NULL_INDEX },
+ : width { 0 }, height { 0 }, max_provinces { Province::MAX_INDEX }, selected_province { nullptr },
highest_province_population { 0 }, total_map_population { 0 } {}
bool Map::add_province(std::string_view identifier, colour_t colour) {
@@ -181,18 +181,24 @@ bool Map::set_max_provinces(Province::index_t new_max_provinces) {
}
void Map::set_selected_province(Province::index_t index) {
- if (index > get_province_count()) {
- Logger::error(
- "Trying to set selected province to an invalid index ", index, " (max index is ", get_province_count(), ")"
- );
- selected_province_index = Province::NULL_INDEX;
+ if (index == Province::NULL_INDEX) {
+ selected_province = nullptr;
} else {
- selected_province_index = index;
+ selected_province = get_province_by_index(index);
+ if (selected_province == nullptr) {
+ Logger::error(
+ "Trying to set selected province to an invalid index ", index, " (max index is ", get_province_count(), ")"
+ );
+ }
}
}
-Province const* Map::get_selected_province() const {
- return get_province_by_index(get_selected_province_index());
+Province* Map::get_selected_province() {
+ return selected_province;
+}
+
+Province::index_t Map::get_selected_province_index() const {
+ return selected_province != nullptr ? selected_province->get_index() : Province::NULL_INDEX;
}
bool Map::add_mapmode(std::string_view identifier, Mapmode::colour_func_t colour_func) {
diff --git a/src/openvic-simulation/map/Map.hpp b/src/openvic-simulation/map/Map.hpp
index 9cee86b..e477b38 100644
--- a/src/openvic-simulation/map/Map.hpp
+++ b/src/openvic-simulation/map/Map.hpp
@@ -72,7 +72,7 @@ namespace OpenVic {
colour_index_map_t colour_index_map;
Province::index_t PROPERTY(max_provinces);
- Province::index_t PROPERTY(selected_province_index);
+ Province* PROPERTY(selected_province);
Pop::pop_size_t PROPERTY(highest_province_population)
Pop::pop_size_t PROPERTY(total_map_population);
@@ -94,7 +94,8 @@ namespace OpenVic {
Province::index_t get_province_index_at(size_t x, size_t y) const;
bool set_max_provinces(Province::index_t new_max_provinces);
void set_selected_province(Province::index_t index);
- Province const* get_selected_province() const;
+ Province* get_selected_province();
+ Province::index_t get_selected_province_index() const;
bool add_region(std::string_view identifier, std::vector<std::string_view> const& province_identifiers);
IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS(region)
diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp
index 79a6202..cb9095b 100644
--- a/src/openvic-simulation/map/Province.cpp
+++ b/src/openvic-simulation/map/Province.cpp
@@ -58,9 +58,10 @@ bool Province::load_positions(BuildingTypeManager const& building_type_manager,
return ret;
}
-bool Province::expand_building(std::string_view building_type_identifier) {
- BuildingInstance* building = buildings.get_item_by_identifier(building_type_identifier);
+bool Province::expand_building(size_t building_index) {
+ BuildingInstance* building = buildings.get_item_by_index(building_index);
if (building == nullptr) {
+ Logger::error("Trying to expand non-existent building index ", building_index, " in province ", get_identifier());
return false;
}
return building->expand();
@@ -360,10 +361,8 @@ bool Province::reset(BuildingTypeManager const& building_type_manager) {
bool ret = true;
if (!is_water()) {
if (building_type_manager.building_types_are_locked()) {
- for (BuildingType const& building_type : building_type_manager.get_building_types()) {
- if (building_type.get_in_province()) {
- ret &= buildings.add_item({ building_type });
- }
+ for (BuildingType const* building_type : building_type_manager.get_province_building_types()) {
+ ret &= buildings.add_item({ *building_type });
}
} else {
Logger::error("Cannot generate buildings until building types are locked!");
diff --git a/src/openvic-simulation/map/Province.hpp b/src/openvic-simulation/map/Province.hpp
index 844ca63..8a9f9e9 100644
--- a/src/openvic-simulation/map/Province.hpp
+++ b/src/openvic-simulation/map/Province.hpp
@@ -130,7 +130,7 @@ namespace OpenVic {
bool load_positions(BuildingTypeManager const& building_type_manager, ast::NodeCPtr root);
- bool expand_building(std::string_view building_type_identifier);
+ bool expand_building(size_t building_index);
bool add_pop(Pop&& pop);
bool add_pop_vec(std::vector<Pop> const& pop_vec);