From 6f4a6c77c6f2613e65a403c3a2964d5041a538c7 Mon Sep 17 00:00:00 2001 From: Hop311 Date: Thu, 17 Aug 2023 20:18:28 +0100 Subject: Moved index_t back to Province --- src/openvic/GameManager.cpp | 2 +- src/openvic/GameManager.hpp | 2 +- src/openvic/Types.hpp | 3 --- src/openvic/map/Map.cpp | 38 +++++++++++++++++++------------------- src/openvic/map/Map.hpp | 18 +++++++++--------- src/openvic/map/Province.cpp | 2 +- src/openvic/map/Province.hpp | 3 +++ 7 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/openvic/GameManager.cpp b/src/openvic/GameManager.cpp index 580dc4b..ce56afc 100644 --- a/src/openvic/GameManager.cpp +++ b/src/openvic/GameManager.cpp @@ -45,7 +45,7 @@ Date const& GameManager::get_today() const { return today; } -return_t GameManager::expand_building(index_t province_index, const std::string_view building_type_identifier) { +return_t GameManager::expand_building(Province::index_t province_index, const std::string_view building_type_identifier) { set_needs_update(); Province* province = map.get_province_by_index(province_index); if (province == nullptr) return FAILURE; diff --git a/src/openvic/GameManager.hpp b/src/openvic/GameManager.hpp index 52de06e..b06c228 100644 --- a/src/openvic/GameManager.hpp +++ b/src/openvic/GameManager.hpp @@ -30,6 +30,6 @@ namespace OpenVic { return_t setup(); Date const& get_today() const; - return_t expand_building(index_t province_index, const std::string_view building_type_identifier); + return_t expand_building(Province::index_t province_index, const std::string_view building_type_identifier); }; } diff --git a/src/openvic/Types.hpp b/src/openvic/Types.hpp index dab455f..3e7ac1d 100644 --- a/src/openvic/Types.hpp +++ b/src/openvic/Types.hpp @@ -30,9 +30,6 @@ namespace OpenVic { return std::clamp(static_cast(colour) / 255.0f, 0.0f, 1.0f); } - using index_t = uint16_t; - static constexpr index_t NULL_INDEX = 0, MAX_INDEX = 0xFFFF; - // TODO: price_t must be changed to a fixed-point numeric type before multiplayer using price_t = double; static constexpr price_t NULL_PRICE = 0.0; diff --git a/src/openvic/map/Map.cpp b/src/openvic/map/Map.cpp index 44c393d..d5cfdd2 100644 --- a/src/openvic/map/Map.cpp +++ b/src/openvic/map/Map.cpp @@ -31,8 +31,8 @@ Map::Map() : provinces { "provinces" }, mapmodes { "mapmodes" } {} return_t Map::add_province(const std::string_view identifier, colour_t colour) { - if (provinces.get_item_count() >= MAX_INDEX) { - Logger::error("The map's province list is full - there can be at most ", MAX_INDEX, " provinces"); + if (provinces.get_item_count() >= Province::MAX_INDEX) { + Logger::error("The map's province list is full - there can be at most ", Province::MAX_INDEX, " provinces"); return FAILURE; } if (identifier.empty()) { @@ -43,9 +43,9 @@ return_t Map::add_province(const std::string_view identifier, colour_t colour) { Logger::error("Invalid province colour for ", identifier, ": ", Province::colour_to_hex_string(colour)); return FAILURE; } - Province new_province { identifier, colour, static_cast(provinces.get_item_count() + 1) }; - const index_t index = get_index_from_colour(colour); - if (index != NULL_INDEX) { + Province new_province { identifier, colour, static_cast(provinces.get_item_count() + 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; } @@ -142,12 +142,12 @@ size_t Map::get_province_count() const { return provinces.get_item_count(); } -Province* Map::get_province_by_index(index_t index) { - return index != NULL_INDEX ? provinces.get_item_by_index(index - 1) : nullptr; +Province* Map::get_province_by_index(Province::index_t index) { + return index != Province::NULL_INDEX ? provinces.get_item_by_index(index - 1) : nullptr; } -Province const* Map::get_province_by_index(index_t index) const { - return index != NULL_INDEX ? provinces.get_item_by_index(index - 1) : nullptr; +Province const* Map::get_province_by_index(Province::index_t index) const { + return index != Province::NULL_INDEX ? provinces.get_item_by_index(index - 1) : nullptr; } Province* Map::get_province_by_identifier(const std::string_view identifier) { @@ -158,27 +158,27 @@ Province const* Map::get_province_by_identifier(const std::string_view identifie return provinces.get_item_by_identifier(identifier); } -index_t Map::get_index_from_colour(colour_t colour) const { +Province::index_t Map::get_index_from_colour(colour_t colour) const { const colour_index_map_t::const_iterator it = colour_index_map.find(colour); if (it != colour_index_map.end()) return it->second; - return NULL_INDEX; + return Province::NULL_INDEX; } -index_t Map::get_province_index_at(size_t x, size_t y) const { +Province::index_t Map::get_province_index_at(size_t x, size_t y) const { if (x < width && y < height) return province_shape_image[x + y * width].index; - return NULL_INDEX; + return Province::NULL_INDEX; } -void Map::set_selected_province(index_t index) { +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 = NULL_INDEX; + selected_province = Province::NULL_INDEX; } else { selected_province = index; } } -index_t Map::get_selected_province_index() const { +Province::index_t Map::get_selected_province_index() const { return selected_province; } @@ -262,8 +262,8 @@ return_t Map::generate_province_shape_image(size_t new_width, size_t new_height, continue; } } - const index_t index = get_index_from_colour(province_colour); - if (index != NULL_INDEX) { + const Province::index_t index = get_index_from_colour(province_colour); + if (index != Province::NULL_INDEX) { province_checklist[index - 1] = true; province_shape_image[idx].index = index; continue; @@ -275,7 +275,7 @@ return_t Map::generate_province_shape_image(size_t new_width, size_t new_height, " at (", x, ", ", y, ")"); } } - province_shape_image[idx].index = NULL_INDEX; + province_shape_image[idx].index = Province::NULL_INDEX; } } if (!unrecognised_province_colours.empty()) { diff --git a/src/openvic/map/Map.hpp b/src/openvic/map/Map.hpp index 64678c8..1b21f5d 100644 --- a/src/openvic/map/Map.hpp +++ b/src/openvic/map/Map.hpp @@ -36,12 +36,12 @@ namespace OpenVic { #pragma pack(push, 1) struct shape_pixel_t { - index_t index; + Province::index_t index; terrain_t terrain; }; #pragma pack(pop) private: - using colour_index_map_t = std::map; + using colour_index_map_t = std::map; IdentifierRegistry provinces; IdentifierRegistry regions; @@ -51,11 +51,11 @@ namespace OpenVic { size_t width = 0, height = 0; std::vector province_shape_image; colour_index_map_t colour_index_map; - index_t selected_province = NULL_INDEX; + Province::index_t selected_province = Province::NULL_INDEX; Pop::pop_size_t highest_province_population, total_map_population; - index_t get_index_from_colour(colour_t colour) const; + Province::index_t get_index_from_colour(colour_t colour) const; public: Map(); @@ -68,13 +68,13 @@ namespace OpenVic { void lock_regions(); size_t get_province_count() const; - Province* get_province_by_index(index_t index); - Province const* get_province_by_index(index_t index) const; + Province* get_province_by_index(Province::index_t index); + Province const* get_province_by_index(Province::index_t index) const; Province* get_province_by_identifier(const std::string_view identifier); Province const* get_province_by_identifier(const std::string_view identifier) const; - index_t get_province_index_at(size_t x, size_t y) const; - void set_selected_province(index_t index); - index_t get_selected_province_index() const; + Province::index_t get_province_index_at(size_t x, size_t y) const; + void set_selected_province(Province::index_t index); + Province::index_t get_selected_province_index() const; Province const* get_selected_province() const; Region* get_region_by_identifier(const std::string_view identifier); diff --git a/src/openvic/map/Province.cpp b/src/openvic/map/Province.cpp index ea284b6..3de7263 100644 --- a/src/openvic/map/Province.cpp +++ b/src/openvic/map/Province.cpp @@ -13,7 +13,7 @@ Province::Province(const std::string_view new_identifier, colour_t new_colour, i assert(index != NULL_INDEX); } -index_t Province::get_index() const { +Province::index_t Province::get_index() const { return index; } diff --git a/src/openvic/map/Province.hpp b/src/openvic/map/Province.hpp index 20c5870..c2a8ac9 100644 --- a/src/openvic/map/Province.hpp +++ b/src/openvic/map/Province.hpp @@ -14,8 +14,11 @@ namespace OpenVic { struct Province : HasIdentifierAndColour { friend struct Map; + using index_t = uint16_t; using life_rating_t = int8_t; + static constexpr index_t NULL_INDEX = 0, MAX_INDEX = (1 << (8 * sizeof(index_t))) - 1; + private: const index_t index; Region* region = nullptr; -- cgit v1.2.3-56-ga3b1