From 7f9a9a8241ba81be9213e6606b8be4a48f1cbaab Mon Sep 17 00:00:00 2001 From: Hop311 Date: Fri, 8 Sep 2023 00:34:47 +0100 Subject: Remove return_t, use & instead of if(x != SUCCESS) --- src/openvic/map/Building.cpp | 24 ++++---- src/openvic/map/Building.hpp | 6 +- src/openvic/map/Map.cpp | 134 +++++++++++++++++++++---------------------- src/openvic/map/Map.hpp | 20 +++---- src/openvic/map/Province.cpp | 16 +++--- src/openvic/map/Province.hpp | 8 +-- src/openvic/map/Region.cpp | 14 +++-- src/openvic/map/Region.hpp | 3 +- 8 files changed, 113 insertions(+), 112 deletions(-) (limited to 'src/openvic/map') diff --git a/src/openvic/map/Building.cpp b/src/openvic/map/Building.cpp index 00e0135..72bccc1 100644 --- a/src/openvic/map/Building.cpp +++ b/src/openvic/map/Building.cpp @@ -39,13 +39,13 @@ float Building::get_expansion_progress() const { return expansion_progress; } -return_t Building::expand() { +bool Building::expand() { if (expansion_state == ExpansionState::CanExpand) { expansion_state = ExpansionState::Preparing; expansion_progress = 0.0f; - return SUCCESS; + return true; } - return FAILURE; + return false; } /* REQUIREMENTS: @@ -94,18 +94,18 @@ Timespan BuildingType::get_build_time() const { BuildingManager::BuildingManager() : building_types { "building types" } {} -return_t BuildingManager::add_building_type(const std::string_view identifier, Building::level_t max_level, Timespan build_time) { +bool BuildingManager::add_building_type(const std::string_view identifier, Building::level_t max_level, Timespan build_time) { if (identifier.empty()) { Logger::error("Invalid building type identifier - empty!"); - return FAILURE; + return false; } if (max_level < 0) { Logger::error("Invalid building type max level for ", identifier, ": ", max_level); - return FAILURE; + return false; } if (build_time < 0) { Logger::error("Invalid building type build time for ", identifier, ": ", build_time); - return FAILURE; + return false; } return building_types.add_item({ identifier, max_level, build_time }); } @@ -118,18 +118,16 @@ BuildingType const* BuildingManager::get_building_type_by_identifier(const std:: return building_types.get_item_by_identifier(identifier); } -return_t BuildingManager::generate_province_buildings(Province& province) const { +bool BuildingManager::generate_province_buildings(Province& province) const { province.reset_buildings(); if (!building_types.is_locked()) { Logger::error("Cannot generate buildings until building types are locked!"); - return FAILURE; + return false; } - return_t ret = SUCCESS; + bool ret = true; if (!province.is_water()) { for (BuildingType const& type : building_types.get_items()) { - if (province.add_building({ type }) != SUCCESS) { - ret = FAILURE; - } + ret &= province.add_building({ type }); } } province.lock_buildings(); diff --git a/src/openvic/map/Building.hpp b/src/openvic/map/Building.hpp index 2ff9409..d1c0348 100644 --- a/src/openvic/map/Building.hpp +++ b/src/openvic/map/Building.hpp @@ -48,7 +48,7 @@ namespace OpenVic { Date const& get_end_date() const; float get_expansion_progress() const; - return_t expand(); + bool expand(); void update_state(Date const& today); void tick(Date const& today); }; @@ -78,9 +78,9 @@ namespace OpenVic { public: BuildingManager(); - return_t add_building_type(const std::string_view identifier, Building::level_t max_level, Timespan build_time); + bool add_building_type(const std::string_view identifier, Building::level_t max_level, Timespan build_time); void lock_building_types(); BuildingType const* get_building_type_by_identifier(const std::string_view identifier) const; - return_t generate_province_buildings(Province& province) const; + bool generate_province_buildings(Province& province) const; }; } 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(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 const& list) { - return_t ret = SUCCESS; +bool Map::set_water_province_list(std::vector 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 const& province_identifiers) { +bool Map::add_region(const std::string_view identifier, std::vector 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(province->get_region()); if (other_region_index != 0) { @@ -116,21 +114,21 @@ return_t Map::add_region(const std::string_view identifier, std::vectorget_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(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 province_checklist(provinces.size()); - return_t ret = SUCCESS; + bool ret = true; std::unordered_set 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 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 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 components) { - return_t ret = SUCCESS; +static bool parse_province_colour(colour_t& colour, std::array 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 const& lines) { +bool Map::load_province_definitions(std::vector 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); } } ); diff --git a/src/openvic/map/Map.hpp b/src/openvic/map/Map.hpp index 0a2c7c6..4684226 100644 --- a/src/openvic/map/Map.hpp +++ b/src/openvic/map/Map.hpp @@ -65,12 +65,12 @@ namespace OpenVic { public: Map(); - return_t add_province(const std::string_view identifier, colour_t colour); + bool add_province(const std::string_view identifier, colour_t colour); void lock_provinces(); - return_t set_water_province(const std::string_view identifier); - return_t set_water_province_list(std::vector const& list); + bool set_water_province(const std::string_view identifier); + bool set_water_province_list(std::vector const& list); void lock_water_provinces(); - return_t add_region(const std::string_view identifier, std::vector const& province_identifiers); + bool add_region(const std::string_view identifier, std::vector const& province_identifiers); void lock_regions(); size_t get_province_count() const; @@ -80,7 +80,7 @@ namespace OpenVic { Province* get_province_by_identifier(const std::string_view identifier); Province const* get_province_by_identifier(const std::string_view identifier) const; Province::index_t get_province_index_at(size_t x, size_t y) const; - return_t set_max_provinces(Province::index_t new_max_provinces); + bool set_max_provinces(Province::index_t new_max_provinces); Province::index_t get_max_provinces() const; void set_selected_province(Province::index_t index); Province::index_t get_selected_province_index() const; @@ -91,22 +91,22 @@ namespace OpenVic { size_t get_region_count() const; std::vector const& get_regions() const; - return_t generate_province_shape_image(size_t new_width, size_t new_height, uint8_t const* colour_data, + bool 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); size_t get_width() const; size_t get_height() const; std::vector const& get_province_shape_image() const; - return_t add_mapmode(const std::string_view identifier, Mapmode::colour_func_t colour_func); + bool add_mapmode(const std::string_view identifier, Mapmode::colour_func_t colour_func); void lock_mapmodes(); size_t get_mapmode_count() const; std::vector const& get_mapmodes() const; Mapmode const* get_mapmode_by_index(Mapmode::index_t index) const; Mapmode const* get_mapmode_by_identifier(const std::string_view identifier) const; static constexpr size_t MAPMODE_COLOUR_SIZE = 4; - return_t generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) const; + bool generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) const; - return_t setup(GoodManager const& good_manager, BuildingManager const& building_manager, PopManager const& pop_manager); + bool setup(GoodManager const& good_manager, BuildingManager const& building_manager, PopManager const& pop_manager); void update_highest_province_population(); Pop::pop_size_t get_highest_province_population() const; @@ -116,6 +116,6 @@ namespace OpenVic { void update_state(Date const& today); void tick(Date const& today); - return_t load_province_definitions(std::vector const& lines); + bool load_province_definitions(std::vector const& lines); }; } diff --git a/src/openvic/map/Province.cpp b/src/openvic/map/Province.cpp index 7d03604..cfdc263 100644 --- a/src/openvic/map/Province.cpp +++ b/src/openvic/map/Province.cpp @@ -30,7 +30,7 @@ Province::life_rating_t Province::get_life_rating() const { return life_rating; } -return_t Province::add_building(Building&& building) { +bool Province::add_building(Building&& building) { return buildings.add_item(std::move(building)); } @@ -54,9 +54,9 @@ std::vector const& Province::get_buildings() const { return buildings.get_items(); } -return_t Province::expand_building(const std::string_view building_type_identifier) { +bool Province::expand_building(const std::string_view building_type_identifier) { Building* building = buildings.get_item_by_identifier(building_type_identifier); - if (building == nullptr) return FAILURE; + if (building == nullptr) return false; return building->expand(); } @@ -70,22 +70,22 @@ std::string Province::to_string() const { return stream.str(); } -return_t Province::load_pop_list(PopManager const& pop_manager, ast::NodeCPtr root) { +bool Province::load_pop_list(PopManager const& pop_manager, ast::NodeCPtr root) { return expect_list_reserve_length( pops, - [this, &pop_manager](ast::NodeCPtr pop_node) -> return_t { + [this, &pop_manager](ast::NodeCPtr pop_node) -> bool { return pop_manager.load_pop_into_province(*this, pop_node); } )(root); } -return_t Province::add_pop(Pop&& pop) { +bool Province::add_pop(Pop&& pop) { if (!is_water()) { pops.push_back(std::move(pop)); - return SUCCESS; + return true; } else { Logger::error("Trying to add pop to water province ", get_identifier()); - return FAILURE; + return false; } } diff --git a/src/openvic/map/Province.hpp b/src/openvic/map/Province.hpp index 364bbf8..f64bca2 100644 --- a/src/openvic/map/Province.hpp +++ b/src/openvic/map/Province.hpp @@ -41,18 +41,18 @@ namespace OpenVic { Region* get_region() const; bool is_water() const; life_rating_t get_life_rating() const; - return_t add_building(Building&& building); + bool add_building(Building&& building); void lock_buildings(); void reset_buildings(); Building const* get_building_by_identifier(const std::string_view identifier) const; size_t get_building_count() const; std::vector const& get_buildings() const; - return_t expand_building(const std::string_view building_type_identifier); + bool expand_building(const std::string_view building_type_identifier); Good const* get_rgo() const; std::string to_string() const; - return_t load_pop_list(PopManager const& pop_manager, ast::NodeCPtr root); - return_t add_pop(Pop&& pop); + bool load_pop_list(PopManager const& pop_manager, ast::NodeCPtr root); + bool add_pop(Pop&& pop); void clear_pops(); size_t get_pop_count() const; std::vector const& get_pops() const; diff --git a/src/openvic/map/Region.cpp b/src/openvic/map/Region.cpp index 6372e15..33092c5 100644 --- a/src/openvic/map/Region.cpp +++ b/src/openvic/map/Region.cpp @@ -2,21 +2,21 @@ using namespace OpenVic; -return_t ProvinceSet::add_province(Province* province) { +bool ProvinceSet::add_province(Province* province) { if (locked) { Logger::error("Cannot add province to province set - locked!"); - return FAILURE; + return false; } if (province == nullptr) { Logger::error("Cannot add province to province set - null province!"); - return FAILURE; + return false; } if (contains_province(province)) { Logger::error("Cannot add province ", province->get_identifier(), " to province set - already in the set!"); - return FAILURE; + return false; } provinces.push_back(province); - return SUCCESS; + return true; } void ProvinceSet::lock(bool log) { @@ -37,6 +37,10 @@ void ProvinceSet::reset() { locked = false; } +bool ProvinceSet::empty() const { + return provinces.empty(); +} + size_t ProvinceSet::size() const { return provinces.size(); } diff --git a/src/openvic/map/Region.hpp b/src/openvic/map/Region.hpp index e67edaa..a3640ca 100644 --- a/src/openvic/map/Region.hpp +++ b/src/openvic/map/Region.hpp @@ -10,10 +10,11 @@ namespace OpenVic { bool locked = false; public: - return_t add_province(Province* province); + bool add_province(Province* province); void lock(bool log = false); bool is_locked() const; void reset(); + bool empty() const; size_t size() const; void reserve(size_t size); bool contains_province(Province const* province) const; -- cgit v1.2.3-56-ga3b1