diff options
author | ClarkeCode <clarke.john.robert@gmail.com> | 2023-04-28 23:34:45 +0200 |
---|---|---|
committer | ClarkeCode <clarke.john.robert@gmail.com> | 2023-04-28 23:34:45 +0200 |
commit | 1b35c3a4434873b98f8e3aa7770f0edd37ec053c (patch) | |
tree | d15169c42d04a09d6d1d74593fe21458cdb9f6df /extension/src/openvic2/map | |
parent | 92d0f35b989b0164e2dd8c9a32213c6038e5d57f (diff) |
Moved several type aliases to Types.hpp; Added HasColour class for items which need associated colours
Diffstat (limited to 'extension/src/openvic2/map')
-rw-r--r-- | extension/src/openvic2/map/Map.cpp | 54 | ||||
-rw-r--r-- | extension/src/openvic2/map/Map.hpp | 21 | ||||
-rw-r--r-- | extension/src/openvic2/map/Province.cpp | 18 | ||||
-rw-r--r-- | extension/src/openvic2/map/Province.hpp | 11 | ||||
-rw-r--r-- | extension/src/openvic2/map/Region.cpp | 5 | ||||
-rw-r--r-- | extension/src/openvic2/map/Region.hpp | 3 |
6 files changed, 49 insertions, 63 deletions
diff --git a/extension/src/openvic2/map/Map.cpp b/extension/src/openvic2/map/Map.cpp index b5cf144..90f2420 100644 --- a/extension/src/openvic2/map/Map.cpp +++ b/extension/src/openvic2/map/Map.cpp @@ -16,26 +16,26 @@ Mapmode::index_t Mapmode::get_index() const { return index; } -Province::colour_t Mapmode::get_colour(Map const& map, Province const& province) const { - return colour_func ? colour_func(map, province) : Province::NULL_COLOUR; +colour_t Mapmode::get_colour(Map const& map, Province const& province) const { + return colour_func ? colour_func(map, province) : NULL_COLOUR; } Map::Map() : provinces{ "provinces" }, regions{ "regions" }, mapmodes{ "mapmodes" } {} -return_t Map::add_province(std::string const& identifier, Province::colour_t colour) { - 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_t Map::add_province(std::string const& 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"); return FAILURE; } if (identifier.empty()) { Logger::error("Invalid province identifier - empty!"); return FAILURE; } - if (colour == Province::NULL_COLOUR || colour > Province::MAX_COLOUR) { + if (colour == NULL_COLOUR || colour > MAX_COLOUR) { Logger::error("Invalid province colour: ", Province::colour_to_hex_string(colour)); return FAILURE; } - Province new_province{ static_cast<Province::index_t>(provinces.get_item_count() + 1), identifier, colour }; + Province new_province{ static_cast<index_t>(provinces.get_item_count() + 1), identifier, colour }; Province const* old_province = get_province_by_colour(colour); if (old_province != nullptr) { Logger::error("Duplicate province colours: ", old_province->to_string(), " and ", new_province.to_string()); @@ -126,12 +126,12 @@ size_t Map::get_province_count() const { return provinces.get_item_count(); } -Province* Map::get_province_by_index(Province::index_t index) { - return index != Province::NULL_INDEX ? provinces.get_item_by_index(index - 1) : nullptr; +Province* Map::get_province_by_index(index_t index) { + 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 const* Map::get_province_by_index(index_t index) const { + return index != NULL_INDEX ? provinces.get_item_by_index(index - 1) : nullptr; } Province* Map::get_province_by_identifier(std::string const& identifier) { @@ -142,23 +142,25 @@ Province const* Map::get_province_by_identifier(std::string const& identifier) c return provinces.get_item_by_identifier(identifier); } -Province* Map::get_province_by_colour(Province::colour_t colour) { - if (colour != Province::NULL_COLOUR) +Province* Map::get_province_by_colour(colour_t colour) { + if (colour != NULL_COLOUR) for (Province& province : provinces.get_items()) if (province.get_colour() == colour) return &province; return nullptr; } -Province const* Map::get_province_by_colour(Province::colour_t colour) const { - if (colour != Province::NULL_COLOUR) - for (Province const& province : provinces.get_items()) - if (province.get_colour() == colour) return &province; +Province const* Map::get_province_by_colour(colour_t colour) const { + if (colour == NULL_COLOUR) { return nullptr; } + for (Province const& province : provinces.get_items()) { + if (province.get_colour() == colour) + return &province; + } return nullptr; } -Province::index_t Map::get_province_index_at(size_t x, size_t y) const { +index_t Map::get_province_index_at(size_t x, size_t y) const { if (x < width && y < height) return province_index_image[x + y * width]; - return Province::NULL_INDEX; + return NULL_INDEX; } Region* Map::get_region_by_identifier(std::string const& identifier) { @@ -169,7 +171,7 @@ Region const* Map::get_region_by_identifier(std::string const& identifier) const return regions.get_item_by_identifier(identifier); } -static Province::colour_t colour_at(uint8_t const* colour_data, int32_t idx) { +static colour_t colour_at(uint8_t const* colour_data, int32_t idx) { return (colour_data[idx * 3] << 16) | (colour_data[idx * 3 + 1] << 8) | colour_data[idx * 3 + 2]; } @@ -196,12 +198,12 @@ return_t Map::generate_province_index_image(size_t new_width, size_t new_height, std::vector<bool> province_checklist(provinces.get_item_count()); return_t ret = SUCCESS; - std::unordered_set<Province::colour_t> unrecognised_colours; + std::unordered_set<colour_t> unrecognised_colours; for (int32_t y = 0; y < height; ++y) { for (int32_t x = 0; x < width; ++x) { const int32_t idx = x + y * width; - const Province::colour_t colour = colour_at(colour_data, idx); + const colour_t colour = colour_at(colour_data, idx); if (x > 0) { const int32_t jdx = idx - 1; if (colour_at(colour_data, jdx) == colour) { @@ -218,7 +220,7 @@ return_t Map::generate_province_index_image(size_t new_width, size_t new_height, } Province const* province = get_province_by_colour(colour); if (province != nullptr) { - const Province::index_t index = province->get_index(); + const index_t index = province->get_index(); province_index_image[idx] = index; province_checklist[index - 1] = true; continue; @@ -228,7 +230,7 @@ return_t Map::generate_province_index_image(size_t new_width, size_t new_height, Logger::error("Unrecognised province colour ", Province::colour_to_hex_string(colour), " at (", x, ", ", y, ")"); ret = FAILURE; } - province_index_image[idx] = Province::NULL_INDEX; + province_index_image[idx] = NULL_INDEX; } } @@ -249,7 +251,7 @@ size_t Map::get_height() const { return height; } -std::vector<Province::index_t> const& Map::get_province_index_image() const { +std::vector<index_t> const& Map::get_province_index_image() const { return province_index_image; } @@ -293,7 +295,7 @@ return_t Map::generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) } target += 4; // Skip past Province::NULL_INDEX for (Province const& province : provinces.get_items()) { - const Province::colour_t colour = mapmode->get_colour(*this, province); + const colour_t colour = mapmode->get_colour(*this, province); *target++ = (colour >> 16) & 0xFF; *target++ = (colour >> 8) & 0xFF; *target++ = colour & 0xFF; diff --git a/extension/src/openvic2/map/Map.hpp b/extension/src/openvic2/map/Map.hpp index ebc23be..9150772 100644 --- a/extension/src/openvic2/map/Map.hpp +++ b/extension/src/openvic2/map/Map.hpp @@ -3,13 +3,14 @@ #include <functional> #include "openvic2/map/Region.hpp" +#include "openvic2/Types.hpp" namespace OpenVic2 { struct Mapmode : HasIdentifier { friend struct Map; - using colour_func_t = std::function<Province::colour_t (Map const&, Province const&)>; + using colour_func_t = std::function<colour_t (Map const&, Province const&)>; using index_t = size_t; private: const index_t index; @@ -18,7 +19,7 @@ namespace OpenVic2 { Mapmode(index_t new_index, std::string const& new_identifier, colour_func_t new_colour_func); public: index_t get_index() const; - Province::colour_t get_colour(Map const& map, Province const& province) const; + colour_t get_colour(Map const& map, Province const& province) const; }; /* REQUIREMENTS: @@ -33,11 +34,11 @@ namespace OpenVic2 { size_t water_province_count = 0; size_t width = 0, height = 0; - std::vector<Province::index_t> province_index_image; + std::vector<index_t> province_index_image; public: Map(); - return_t add_province(std::string const& identifier, Province::colour_t colour); + return_t add_province(std::string const& identifier, colour_t colour); void lock_provinces(); return_t set_water_province(std::string const& identifier); void lock_water_provinces(); @@ -45,13 +46,13 @@ namespace OpenVic2 { void lock_regions(); size_t get_province_count() 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_index(index_t index); + Province const* get_province_by_index(index_t index) const; Province* get_province_by_identifier(std::string const& identifier); Province const* get_province_by_identifier(std::string const& identifier) const; - Province* get_province_by_colour(Province::colour_t colour); - Province const* get_province_by_colour(Province::colour_t colour) const; - Province::index_t get_province_index_at(size_t x, size_t y) const; + Province* get_province_by_colour(colour_t colour); + Province const* get_province_by_colour(colour_t colour) const; + index_t get_province_index_at(size_t x, size_t y) const; Region* get_region_by_identifier(std::string const& identifier); Region const* get_region_by_identifier(std::string const& identifier) const; @@ -59,7 +60,7 @@ namespace OpenVic2 { return_t generate_province_index_image(size_t new_width, size_t new_height, uint8_t const* colour_data); size_t get_width() const; size_t get_height() const; - std::vector<Province::index_t> const& get_province_index_image() const; + std::vector<index_t> const& get_province_index_image() const; return_t add_mapmode(std::string const& identifier, Mapmode::colour_func_t colour_func); void lock_mapmodes(); diff --git a/extension/src/openvic2/map/Province.cpp b/extension/src/openvic2/map/Province.cpp index 4360bce..472cccb 100644 --- a/extension/src/openvic2/map/Province.cpp +++ b/extension/src/openvic2/map/Province.cpp @@ -7,25 +7,15 @@ using namespace OpenVic2; Province::Province(index_t new_index, std::string const& new_identifier, colour_t new_colour) : - HasIdentifier{ new_identifier }, index{ new_index }, colour{ new_colour }, buildings{ "buildings" } { + HasIdentifier{ new_identifier }, HasColour{ new_colour }, index{ new_index }, buildings{ "buildings" } { assert(index != NULL_INDEX); - assert(colour != NULL_COLOUR); + assert(new_colour != NULL_COLOUR); } -std::string Province::colour_to_hex_string(colour_t colour) { - std::ostringstream stream; - stream << std::hex << std::setfill('0') << std::setw(6) << colour; - return stream.str(); -} - -Province::index_t Province::get_index() const { +index_t Province::get_index() const { return index; } -Province::colour_t Province::get_colour() const { - return colour; -} - Region* Province::get_region() const { return region; } @@ -62,7 +52,7 @@ return_t Province::expand_building(std::string const& building_type_identifier) std::string Province::to_string() const { std::stringstream stream; - stream << "(#" << std::to_string(index) << ", " << get_identifier() << ", 0x" << colour_to_hex_string(colour) << ")"; + stream << "(#" << std::to_string(index) << ", " << get_identifier() << ", 0x" << colour_to_hex_string() << ")"; return stream.str(); } diff --git a/extension/src/openvic2/map/Province.hpp b/extension/src/openvic2/map/Province.hpp index aa0329c..65eaa09 100644 --- a/extension/src/openvic2/map/Province.hpp +++ b/extension/src/openvic2/map/Province.hpp @@ -1,6 +1,7 @@ #pragma once #include "openvic2/map/Building.hpp" +#include "openvic2/Types.hpp" namespace OpenVic2 { struct Map; @@ -9,18 +10,13 @@ namespace OpenVic2 { /* REQUIREMENTS: * MAP-5, MAP-8, MAP-43, MAP-47 */ - struct Province : HasIdentifier { + struct Province : HasIdentifier, HasColour { friend struct Map; - using colour_t = uint32_t; - using index_t = uint16_t; using life_rating_t = int8_t; - static constexpr colour_t NULL_COLOUR = 0, MAX_COLOUR = 0xFFFFFF; - static constexpr index_t NULL_INDEX = 0, MAX_INDEX = 0xFFFF; private: const index_t index; - const colour_t colour; Region* region = nullptr; bool water = false; life_rating_t life_rating = 0; @@ -28,12 +24,9 @@ namespace OpenVic2 { Province(index_t new_index, std::string const& new_identifier, colour_t new_colour); public: - static std::string colour_to_hex_string(colour_t colour); - Province(Province&&) = default; index_t get_index() const; - colour_t get_colour() const; Region* get_region() const; bool is_water() const; life_rating_t get_life_rating() const; diff --git a/extension/src/openvic2/map/Region.cpp b/extension/src/openvic2/map/Region.cpp index 3e5bee7..6839a10 100644 --- a/extension/src/openvic2/map/Region.cpp +++ b/extension/src/openvic2/map/Region.cpp @@ -19,8 +19,7 @@ std::set<Province*> const& ProvinceSet::get_provinces() const { Region::Region(std::string const& new_identifier) : HasIdentifier{ new_identifier } {} -Province::colour_t Region::get_colour() const { +colour_t Region::get_colour() const { if (provinces.empty()) return 0xFF0000; - Province const* province = *provinces.cbegin(); - return province->get_colour(); + return (*provinces.cbegin())->get_colour(); } diff --git a/extension/src/openvic2/map/Region.hpp b/extension/src/openvic2/map/Region.hpp index 04564fc..58fad31 100644 --- a/extension/src/openvic2/map/Region.hpp +++ b/extension/src/openvic2/map/Region.hpp @@ -3,6 +3,7 @@ #include <set> #include "openvic2/map/Province.hpp" +#include "openvic2/Types.hpp" namespace OpenVic2 { @@ -25,6 +26,6 @@ namespace OpenVic2 { public: Region(Region&&) = default; - Province::colour_t get_colour() const; + colour_t get_colour() const; }; } |