aboutsummaryrefslogtreecommitdiff
path: root/src/openvic/map/Map.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/openvic/map/Map.cpp')
-rw-r--r--src/openvic/map/Map.cpp134
1 files changed, 66 insertions, 68 deletions
diff --git a/src/openvic/map/Map.cpp b/src/openvic/map/Map.cpp
index 439338b..6375691 100644
--- a/src/openvic/map/Map.cpp
+++ b/src/openvic/map/Map.cpp
@@ -30,24 +30,24 @@ Map::Map() : provinces { "provinces" },
regions { "regions" },
mapmodes { "mapmodes" } {}
-return_t Map::add_province(const std::string_view identifier, colour_t colour) {
+bool Map::add_province(const std::string_view identifier, colour_t colour) {
if (provinces.size() >= max_provinces) {
Logger::error("The map's province list is full - maximum number of provinces is ", max_provinces, " (this can be at most ", Province::MAX_INDEX, ")");
- return FAILURE;
+ return false;
}
if (identifier.empty()) {
Logger::error("Invalid province identifier - empty!");
- return FAILURE;
+ return false;
}
if (colour == NULL_COLOUR || colour > MAX_COLOUR_RGB) {
Logger::error("Invalid province colour for ", identifier, ": ", colour_to_hex_string(colour));
- return FAILURE;
+ return false;
}
Province new_province { identifier, colour, static_cast<Province::index_t>(provinces.size() + 1) };
const Province::index_t index = get_index_from_colour(colour);
if (index != Province::NULL_INDEX) {
Logger::error("Duplicate province colours: ", get_province_by_index(index)->to_string(), " and ", new_province.to_string());
- return FAILURE;
+ return false;
}
colour_index_map[new_province.get_colour()] = new_province.get_index();
return provinces.add_item(std::move(new_province));
@@ -57,35 +57,33 @@ void Map::lock_provinces() {
provinces.lock();
}
-return_t Map::set_water_province(const std::string_view identifier) {
+bool Map::set_water_province(const std::string_view identifier) {
if (water_provinces.is_locked()) {
Logger::error("The map's water provinces have already been locked!");
- return FAILURE;
+ return false;
}
Province* province = get_province_by_identifier(identifier);
if (province == nullptr) {
Logger::error("Unrecognised water province identifier: ", identifier);
- return FAILURE;
+ return false;
}
if (province->is_water()) {
Logger::error("Province ", identifier, " is already a water province!");
- return FAILURE;
+ return false;
}
- if (water_provinces.add_province(province) != SUCCESS) {
+ if (!water_provinces.add_province(province)) {
Logger::error("Failed to add province ", identifier, " to water province set!");
- return FAILURE;
+ return false;
}
province->water = true;
- return SUCCESS;
+ return true;
}
-return_t Map::set_water_province_list(std::vector<std::string_view> const& list) {
- return_t ret = SUCCESS;
+bool Map::set_water_province_list(std::vector<std::string_view> const& list) {
+ bool ret = true;
water_provinces.reserve(water_provinces.size() + list.size());
for (std::string_view const& identifier : list) {
- if (set_water_province(identifier) != SUCCESS) {
- ret = FAILURE;
- }
+ ret &= set_water_province(identifier);
}
return ret;
}
@@ -95,19 +93,19 @@ void Map::lock_water_provinces() {
Logger::info("Locked water provinces after registering ", water_provinces.size());
}
-return_t Map::add_region(const std::string_view identifier, std::vector<std::string_view> const& province_identifiers) {
+bool Map::add_region(const std::string_view identifier, std::vector<std::string_view> const& province_identifiers) {
if (identifier.empty()) {
Logger::error("Invalid region identifier - empty!");
- return FAILURE;
+ return false;
}
Region new_region { identifier };
- return_t ret = SUCCESS;
+ bool ret = true;
for (const std::string_view province_identifier : province_identifiers) {
Province* province = get_province_by_identifier(province_identifier);
if (province != nullptr) {
if (new_region.contains_province(province)) {
Logger::error("Duplicate province identifier ", province_identifier, " in region ", identifier);
- ret = FAILURE;
+ ret = false;
} else {
size_t other_region_index = reinterpret_cast<size_t>(province->get_region());
if (other_region_index != 0) {
@@ -116,21 +114,21 @@ return_t Map::add_region(const std::string_view identifier, std::vector<std::str
Logger::error("Cannot add province ", province_identifier, " to region ", identifier, " - it is already part of ", regions.get_item_by_index(other_region_index)->get_identifier());
else
Logger::error("Cannot add province ", province_identifier, " to region ", identifier, " - it is already part of an unknown region with index ", other_region_index);
- ret = FAILURE;
- } else if (new_region.add_province(province) != SUCCESS) {
+ ret = false;
+ } else if (!new_region.add_province(province)) {
Logger::error("Failed to add province ", province_identifier, " to region ", identifier);
- ret = FAILURE;
+ ret = false;
}
}
} else {
Logger::error("Invalid province identifier ", province_identifier, " for region ", identifier);
- ret = FAILURE;
+ ret = false;
}
}
new_region.lock();
- if (!new_region.size()) {
+ if (new_region.empty()) {
Logger::error("No valid provinces in list for ", identifier);
- return FAILURE;
+ return false;
}
// Used to detect provinces listed in multiple regions, will
@@ -138,7 +136,7 @@ return_t Map::add_region(const std::string_view identifier, std::vector<std::str
Region* tmp_region_index = reinterpret_cast<Region*>(regions.size());
for (Province* province : new_region.get_provinces())
province->region = tmp_region_index;
- if (regions.add_item(std::move(new_region)) != SUCCESS) ret = FAILURE;
+ ret &= regions.add_item(std::move(new_region));
return ret;
}
@@ -184,17 +182,17 @@ Province::index_t Map::get_province_index_at(size_t x, size_t y) const {
return Province::NULL_INDEX;
}
-return_t Map::set_max_provinces(Province::index_t new_max_provinces) {
+bool Map::set_max_provinces(Province::index_t new_max_provinces) {
if (new_max_provinces <= Province::NULL_INDEX) {
Logger::error("Trying to set max province count to an invalid value ", new_max_provinces, " (must be greater than ", Province::NULL_INDEX, ")");
- return FAILURE;
+ return false;
}
if (!provinces.empty() || provinces.is_locked()) {
Logger::error("Trying to set max province count to ", new_max_provinces, " after provinces have already been added and/or locked");
- return FAILURE;
+ return false;
}
max_provinces = new_max_provinces;
- return SUCCESS;
+ return true;
}
Province::index_t Map::get_max_provinces() const {
@@ -239,34 +237,34 @@ static colour_t colour_at(uint8_t const* colour_data, int32_t idx) {
return (colour_data[idx] << 16) | (colour_data[idx + 1] << 8) | colour_data[idx + 2];
}
-return_t Map::generate_province_shape_image(size_t new_width, size_t new_height, uint8_t const* colour_data,
+bool Map::generate_province_shape_image(size_t new_width, size_t new_height, uint8_t const* colour_data,
uint8_t const* terrain_data, terrain_variant_map_t const& terrain_variant_map, bool detailed_errors) {
if (!province_shape_image.empty()) {
Logger::error("Province index image has already been generated!");
- return FAILURE;
+ return false;
}
if (!provinces.is_locked()) {
Logger::error("Province index image cannot be generated until after provinces are locked!");
- return FAILURE;
+ return false;
}
if (new_width < 1 || new_height < 1) {
Logger::error("Invalid province image dimensions: ", new_width, "x", new_height);
- return FAILURE;
+ return false;
}
if (colour_data == nullptr) {
Logger::error("Province colour data pointer is null!");
- return FAILURE;
+ return false;
}
if (terrain_data == nullptr) {
Logger::error("Province terrain data pointer is null!");
- return FAILURE;
+ return false;
}
width = new_width;
height = new_height;
province_shape_image.resize(width * height);
std::vector<bool> province_checklist(provinces.size());
- return_t ret = SUCCESS;
+ bool ret = true;
std::unordered_set<colour_t> unrecognised_province_colours, unrecognised_terrain_colours;
for (int32_t y = 0; y < height; ++y) {
@@ -320,11 +318,11 @@ return_t Map::generate_province_shape_image(size_t new_width, size_t new_height,
}
if (!unrecognised_province_colours.empty()) {
Logger::error("Province image contains ", unrecognised_province_colours.size(), " unrecognised province colours");
- ret = FAILURE;
+ ret = false;
}
if (!unrecognised_terrain_colours.empty()) {
Logger::error("Terrain image contains ", unrecognised_terrain_colours.size(), " unrecognised terrain colours");
- ret = FAILURE;
+ ret = false;
}
size_t missing = 0;
@@ -338,7 +336,7 @@ return_t Map::generate_province_shape_image(size_t new_width, size_t new_height,
}
if (missing > 0) {
Logger::error("Province image is missing ", missing, " province colours");
- ret = FAILURE;
+ ret = false;
}
return ret;
}
@@ -355,14 +353,14 @@ std::vector<Map::shape_pixel_t> const& Map::get_province_shape_image() const {
return province_shape_image;
}
-return_t Map::add_mapmode(const std::string_view identifier, Mapmode::colour_func_t colour_func) {
+bool Map::add_mapmode(const std::string_view identifier, Mapmode::colour_func_t colour_func) {
if (identifier.empty()) {
Logger::error("Invalid mapmode identifier - empty!");
- return FAILURE;
+ return false;
}
if (colour_func == nullptr) {
Logger::error("Mapmode colour function is null for identifier: ", identifier);
- return FAILURE;
+ return false;
}
return mapmodes.add_item({ identifier, mapmodes.size(), colour_func });
}
@@ -387,20 +385,20 @@ Mapmode const* Map::get_mapmode_by_identifier(const std::string_view identifier)
return mapmodes.get_item_by_identifier(identifier);
}
-return_t Map::generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) const {
+bool Map::generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) const {
if (target == nullptr) {
Logger::error("Mapmode colour target pointer is null!");
- return FAILURE;
+ return false;
}
- return_t ret = SUCCESS;
+ bool ret = true;
Mapmode const* mapmode = mapmodes.get_item_by_index(index);
if (mapmode == nullptr) {
// Not an error if mapmodes haven't yet been loaded,
// e.g. if we want to allocate the province colour
// texture before mapmodes are loaded.
- if (!(mapmodes.size() == 0 && index == 0)) {
+ if (!(mapmodes.empty() && index == 0)) {
Logger::error("Invalid mapmode index: ", index);
- ret = FAILURE;
+ ret = false;
}
mapmode = &Mapmode::ERROR_MAPMODE;
}
@@ -439,14 +437,14 @@ Pop::pop_size_t Map::get_total_map_population() const {
return total_map_population;
}
-return_t Map::setup(GoodManager const& good_manager, BuildingManager const& building_manager, PopManager const& pop_manager) {
- return_t ret = SUCCESS;
+bool Map::setup(GoodManager const& good_manager, BuildingManager const& building_manager, PopManager const& pop_manager) {
+ bool ret = true;
for (Province& province : provinces.get_items()) {
province.clear_pops();
// Set all land provinces to have an RGO based on their index to test them
if (!province.is_water() && good_manager.get_good_count() > 0)
province.rgo = good_manager.get_good_by_index(province.get_index() % good_manager.get_good_count());
- if (building_manager.generate_province_buildings(province) != SUCCESS) ret = FAILURE;
+ ret &= building_manager.generate_province_buildings(province);
}
return ret;
}
@@ -465,18 +463,18 @@ void Map::tick(Date const& today) {
using namespace ovdl::csv;
-static return_t validate_province_definitions_header(LineObject const& header) {
+static bool validate_province_definitions_header(LineObject const& header) {
static const std::vector<std::string> standard_header { "province", "red", "green", "blue" };
for (size_t i = 0; i < standard_header.size(); ++i) {
const std::string_view val = header.get_value_for(i);
if (i == 0 && val.empty()) break;
- if (val != standard_header[i]) return FAILURE;
+ if (val != standard_header[i]) return false;
}
- return SUCCESS;
+ return true;
}
-static return_t parse_province_colour(colour_t& colour, std::array<std::string_view, 3> components) {
- return_t ret = SUCCESS;
+static bool parse_province_colour(colour_t& colour, std::array<std::string_view, 3> components) {
+ bool ret = true;
colour = NULL_COLOUR;
for (std::string_view& c : components) {
colour <<= 8;
@@ -486,43 +484,43 @@ static return_t parse_province_colour(colour_t& colour, std::array<std::string_v
if (successful && val <= 255) {
colour |= val;
} else {
- ret = FAILURE;
+ ret = false;
}
}
return ret;
}
-return_t Map::load_province_definitions(std::vector<LineObject> const& lines) {
+bool Map::load_province_definitions(std::vector<LineObject> const& lines) {
if (lines.empty()) {
Logger::error("No header or entries in province definition file!");
- return FAILURE;
+ return false;
}
{
LineObject const& header = lines.front();
- if (validate_province_definitions_header(header) != SUCCESS) {
+ if (!validate_province_definitions_header(header)) {
Logger::error("Non-standard province definition file header - make sure this is not a province definition: ", header);
}
}
if (lines.size() <= 1) {
Logger::error("No entries in province definition file!");
- return FAILURE;
+ return false;
}
provinces.reserve(lines.size() - 1);
- return_t ret = SUCCESS;
+ bool ret = true;
std::for_each(lines.begin() + 1, lines.end(),
[this, &ret](LineObject const& line) -> void {
const std::string_view identifier = line.get_value_for(0);
if (!identifier.empty()) {
colour_t colour;
- if (parse_province_colour(colour, {
+ if (!parse_province_colour(colour, {
line.get_value_for(1),
line.get_value_for(2),
line.get_value_for(3)
- }) != SUCCESS) {
+ })) {
Logger::error("Error reading colour in province definition: ", line);
- ret = FAILURE;
+ ret = false;
}
- if (add_province(identifier, colour) != SUCCESS) ret = FAILURE;
+ ret &= add_province(identifier, colour);
}
}
);