diff options
Diffstat (limited to 'extension/src/openvic2')
-rw-r--r-- | extension/src/openvic2/Date.cpp | 4 | ||||
-rw-r--r-- | extension/src/openvic2/GameAdvancementHook.cpp | 2 | ||||
-rw-r--r-- | extension/src/openvic2/GameManager.cpp | 6 | ||||
-rw-r--r-- | extension/src/openvic2/GameManager.hpp | 6 | ||||
-rw-r--r-- | extension/src/openvic2/Good.cpp | 3 | ||||
-rw-r--r-- | extension/src/openvic2/Good.hpp | 21 | ||||
-rw-r--r-- | extension/src/openvic2/Logger.cpp | 2 | ||||
-rw-r--r-- | extension/src/openvic2/Types.cpp | 20 | ||||
-rw-r--r-- | extension/src/openvic2/Types.hpp | 57 | ||||
-rw-r--r-- | extension/src/openvic2/map/Building.cpp | 6 | ||||
-rw-r--r-- | extension/src/openvic2/map/Building.hpp | 4 | ||||
-rw-r--r-- | extension/src/openvic2/map/Map.cpp | 120 | ||||
-rw-r--r-- | extension/src/openvic2/map/Map.hpp | 38 | ||||
-rw-r--r-- | extension/src/openvic2/map/Province.cpp | 20 | ||||
-rw-r--r-- | extension/src/openvic2/map/Province.hpp | 12 | ||||
-rw-r--r-- | extension/src/openvic2/map/Region.cpp | 8 | ||||
-rw-r--r-- | extension/src/openvic2/map/Region.hpp | 4 |
17 files changed, 211 insertions, 122 deletions
diff --git a/extension/src/openvic2/Date.cpp b/extension/src/openvic2/Date.cpp index bb891fd..ed800d5 100644 --- a/extension/src/openvic2/Date.cpp +++ b/extension/src/openvic2/Date.cpp @@ -1,9 +1,9 @@ -#include "openvic2/Date.hpp" +#include "Date.hpp" #include <cctype> #include <algorithm> -#include "openvic2/Logger.hpp" +#include "Logger.hpp" using namespace OpenVic2; diff --git a/extension/src/openvic2/GameAdvancementHook.cpp b/extension/src/openvic2/GameAdvancementHook.cpp index 4b9bc25..d90b7b1 100644 --- a/extension/src/openvic2/GameAdvancementHook.cpp +++ b/extension/src/openvic2/GameAdvancementHook.cpp @@ -1,4 +1,4 @@ -#include "openvic2/GameAdvancementHook.hpp" +#include "GameAdvancementHook.hpp" using namespace OpenVic2; diff --git a/extension/src/openvic2/GameManager.cpp b/extension/src/openvic2/GameManager.cpp index 78992f1..58e40bb 100644 --- a/extension/src/openvic2/GameManager.cpp +++ b/extension/src/openvic2/GameManager.cpp @@ -1,6 +1,6 @@ -#include "openvic2/GameManager.hpp" +#include "GameManager.hpp" -#include "openvic2/Logger.hpp" +#include "Logger.hpp" using namespace OpenVic2; @@ -38,7 +38,7 @@ Date const& GameManager::get_today() const { return today; } -return_t GameManager::expand_building(Province::index_t province_index, std::string const& building_type_identifier) { +return_t GameManager::expand_building(index_t province_index, std::string const& building_type_identifier) { set_needs_update(); Province* province = map.get_province_by_index(province_index); if (province == nullptr) return FAILURE; diff --git a/extension/src/openvic2/GameManager.hpp b/extension/src/openvic2/GameManager.hpp index 65cd566..70e98bd 100644 --- a/extension/src/openvic2/GameManager.hpp +++ b/extension/src/openvic2/GameManager.hpp @@ -1,7 +1,7 @@ #pragma once -#include "openvic2/GameAdvancementHook.hpp" -#include "openvic2/map/Map.hpp" +#include "GameAdvancementHook.hpp" +#include "map/Map.hpp" namespace OpenVic2 { struct GameManager { @@ -24,6 +24,6 @@ namespace OpenVic2 { return_t setup(); Date const& get_today() const; - return_t expand_building(Province::index_t province_index, std::string const& building_type_identifier); + return_t expand_building(index_t province_index, std::string const& building_type_identifier); }; } diff --git a/extension/src/openvic2/Good.cpp b/extension/src/openvic2/Good.cpp new file mode 100644 index 0000000..e3389de --- /dev/null +++ b/extension/src/openvic2/Good.cpp @@ -0,0 +1,3 @@ +#include "Good.hpp" + +using namespace OpenVic2; diff --git a/extension/src/openvic2/Good.hpp b/extension/src/openvic2/Good.hpp new file mode 100644 index 0000000..378bbc4 --- /dev/null +++ b/extension/src/openvic2/Good.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "Types.hpp" + +namespace OpenVic2 { + class Good : HasIdentifier { + public: + std::string category; + price_t cost; + std::string colour; + bool isAvailable; + bool isTradable; + bool isMoney; + bool hasOverseasPenalty; + + Good(Good&&) = default; + Good(std::string const& identifier,std::string const& category, price_t cost, std::string const& colour, + bool isAvailable, bool isTradable, bool isMoney, bool hasOverseasPenalty) : HasIdentifier(identifier), + category(category), cost(cost), colour(colour), isAvailable(isAvailable), isMoney(isMoney), hasOverseasPenalty(hasOverseasPenalty) {}; + }; +} diff --git a/extension/src/openvic2/Logger.cpp b/extension/src/openvic2/Logger.cpp index 56d74ab..4d7378e 100644 --- a/extension/src/openvic2/Logger.cpp +++ b/extension/src/openvic2/Logger.cpp @@ -1,4 +1,4 @@ -#include "openvic2/Logger.hpp" +#include "Logger.hpp" #include <iostream> diff --git a/extension/src/openvic2/Types.cpp b/extension/src/openvic2/Types.cpp index 861ab50..f51ad7c 100644 --- a/extension/src/openvic2/Types.cpp +++ b/extension/src/openvic2/Types.cpp @@ -1,6 +1,8 @@ -#include "openvic2/Types.hpp" +#include "Types.hpp" #include <cassert> +#include <sstream> +#include <iomanip> using namespace OpenVic2; @@ -11,3 +13,19 @@ HasIdentifier::HasIdentifier(std::string const& new_identifier) : identifier{ ne std::string const& HasIdentifier::get_identifier() const { return identifier; } + +HasColour::HasColour(colour_t const new_colour) : colour(new_colour) { + assert(colour != NULL_COLOUR && colour <= MAX_COLOUR_RGB); +} + +colour_t HasColour::get_colour() const { return colour; } + +std::string OpenVic2::HasColour::colour_to_hex_string(colour_t const colour) { + std::ostringstream stream; + stream << std::hex << std::setfill('0') << std::setw(6) << colour; + return stream.str(); +} + +std::string HasColour::colour_to_hex_string() const { + return colour_to_hex_string(colour); +} diff --git a/extension/src/openvic2/Types.hpp b/extension/src/openvic2/Types.hpp index 98e92ce..e4a0e2d 100644 --- a/extension/src/openvic2/Types.hpp +++ b/extension/src/openvic2/Types.hpp @@ -1,12 +1,32 @@ #pragma once -#include <string> #include <vector> +#include <cstdint> +#include <algorithm> +#include <map> -#include "openvic2/Logger.hpp" +#include "Logger.hpp" namespace OpenVic2 { + // Represents a 24-bit RGB integer OR a 32-bit ARGB integer + using colour_t = uint32_t; + /* When colour_t is used as an identifier, NULL_COLOUR is disallowed + * and should be reserved as an error value. + * When colour_t is used in a purely graphical context, NULL_COLOUR + * should be allowed. + */ + static constexpr colour_t NULL_COLOUR = 0, MAX_COLOUR_RGB = 0xFFFFFF; + constexpr colour_t to_alpha_value(float a) { + return static_cast<colour_t>(std::clamp(a, 0.0f, 1.0f) * 255.0f) << 24; + } + + 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; using return_t = bool; + // This mirrors godot::Error, where `OK = 0` and `FAILED = 1`. static constexpr return_t SUCCESS = false, FAILURE = true; @@ -29,6 +49,24 @@ namespace OpenVic2 { }; /* + * Base class for objects with associated colour information + */ + class HasColour { + const colour_t colour; + protected: + HasColour(colour_t const new_colour); + public: + HasColour(HasColour const&) = delete; + HasColour(HasColour&&) = default; + HasColour& operator=(HasColour const&) = delete; + HasColour& operator=(HasColour&&) = delete; + + colour_t get_colour() const; + std::string colour_to_hex_string() const; + static std::string colour_to_hex_string(colour_t const colour); + }; + + /* * Template for a list of objects with unique string identifiers that can * be locked to prevent any further additions. The template argument T is * the type of object that the registry will store, and the second part ensures @@ -36,9 +74,12 @@ namespace OpenVic2 { */ template<class T, typename std::enable_if<std::is_base_of<HasIdentifier, T>::value>::type* = nullptr> class IdentifierRegistry { + using identifier_index_map_t = std::map<std::string, size_t>; + const std::string name; std::vector<T> items; bool locked = false; + identifier_index_map_t identifier_index_map; public: IdentifierRegistry(std::string const& new_name) : name(new_name) {} return_t add_item(T&& item) { @@ -51,6 +92,7 @@ namespace OpenVic2 { Logger::error("Cannot add item to the ", name, " registry - an item with the identifier \"", item.get_identifier(), "\" already exists!"); return FAILURE; } + identifier_index_map[item.get_identifier()] = items.size(); items.push_back(std::move(item)); return SUCCESS; } @@ -66,6 +108,7 @@ namespace OpenVic2 { return locked; } void reset() { + identifier_index_map.clear(); items.clear(); locked = false; } @@ -73,15 +116,13 @@ namespace OpenVic2 { return items.size(); } T* get_item_by_identifier(std::string const& identifier) { - if (!identifier.empty()) - for (T& item : items) - if (item.get_identifier() == identifier) return &item; + const identifier_index_map_t::const_iterator it = identifier_index_map.find(identifier); + if (it != identifier_index_map.end()) return &items[it->second]; return nullptr; } T const* get_item_by_identifier(std::string const& identifier) const { - if (!identifier.empty()) - for (T const& item : items) - if (item.get_identifier() == identifier) return &item; + const identifier_index_map_t::const_iterator it = identifier_index_map.find(identifier); + if (it != identifier_index_map.end()) return &items[it->second]; return nullptr; } T* get_item_by_index(size_t index) { diff --git a/extension/src/openvic2/map/Building.cpp b/extension/src/openvic2/map/Building.cpp index 00e121b..1e26873 100644 --- a/extension/src/openvic2/map/Building.cpp +++ b/extension/src/openvic2/map/Building.cpp @@ -1,9 +1,9 @@ -#include "openvic2/map/Building.hpp" +#include "Building.hpp" #include <cassert> -#include "openvic2/Logger.hpp" -#include "openvic2/map/Province.hpp" +#include "../Logger.hpp" +#include "Province.hpp" using namespace OpenVic2; diff --git a/extension/src/openvic2/map/Building.hpp b/extension/src/openvic2/map/Building.hpp index 1305014..78d08ae 100644 --- a/extension/src/openvic2/map/Building.hpp +++ b/extension/src/openvic2/map/Building.hpp @@ -2,8 +2,8 @@ #include <vector> -#include "openvic2/Types.hpp" -#include "openvic2/Date.hpp" +#include "../Types.hpp" +#include "../Date.hpp" namespace OpenVic2 { struct Province; diff --git a/extension/src/openvic2/map/Map.cpp b/extension/src/openvic2/map/Map.cpp index b5cf144..1f44c43 100644 --- a/extension/src/openvic2/map/Map.cpp +++ b/extension/src/openvic2/map/Map.cpp @@ -1,9 +1,9 @@ -#include "openvic2/map/Map.hpp" +#include "Map.hpp" #include <cassert> #include <unordered_set> -#include "openvic2/Logger.hpp" +#include "../Logger.hpp" using namespace OpenVic2; @@ -16,31 +16,32 @@ 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_RGB) { 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 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()); + Province new_province{ static_cast<index_t>(provinces.get_item_count() + 1), identifier, colour }; + const index_t index = get_index_from_colour(colour); + if (index != NULL_INDEX) { + Logger::error("Duplicate province colours: ", get_province_by_index(index)->to_string(), " and ", new_province.to_string()); return FAILURE; } + colour_index_map[new_province.get_colour()] = new_province.get_index(); return provinces.add_item(std::move(new_province)); } @@ -126,12 +127,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 +143,15 @@ 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) - for (Province& province : provinces.get_items()) - if (province.get_colour() == colour) return &province; - return nullptr; +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; } -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; - return nullptr; -} - -Province::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; +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; } Region* Map::get_region_by_identifier(std::string const& identifier) { @@ -169,12 +162,13 @@ 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]; } -return_t Map::generate_province_index_image(size_t new_width, size_t new_height, uint8_t const* colour_data) { - if (!province_index_image.empty()) { +return_t 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) { + if (!province_shape_image.empty()) { Logger::error("Province index image has already been generated!"); return FAILURE; } @@ -190,45 +184,61 @@ return_t Map::generate_province_index_image(size_t new_width, size_t new_height, Logger::error("Province colour data pointer is null!"); return FAILURE; } + if (terrain_data == nullptr) { + Logger::error("Province terrain data pointer is null!"); + return FAILURE; + } width = new_width; height = new_height; - province_index_image.resize(width * height); + province_shape_image.resize(width * 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_province_colours, unrecognised_terrain_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 terrain_colour = colour_at(terrain_data, idx); + const terrain_variant_map_t::const_iterator it = terrain_variant_map.find(terrain_colour); + if (it != terrain_variant_map.end()) province_shape_image[idx].terrain = it->second; + else { + if (unrecognised_terrain_colours.find(terrain_colour) == unrecognised_terrain_colours.end()) { + unrecognised_terrain_colours.insert(terrain_colour); + Logger::error("Unrecognised terrain colour ", Province::colour_to_hex_string(terrain_colour), " at (", x, ", ", y, ")"); + ret = FAILURE; + } + province_shape_image[idx].terrain = 0; + } + + const colour_t province_colour = colour_at(colour_data, idx); if (x > 0) { const int32_t jdx = idx - 1; - if (colour_at(colour_data, jdx) == colour) { - province_index_image[idx] = province_index_image[jdx]; + if (colour_at(colour_data, jdx) == province_colour) { + province_shape_image[idx].index = province_shape_image[jdx].index; continue; } } if (y > 0) { const int32_t jdx = idx - width; - if (colour_at(colour_data, jdx) == colour) { - province_index_image[idx] = province_index_image[jdx]; + if (colour_at(colour_data, jdx) == province_colour) { + province_shape_image[idx].index = province_shape_image[jdx].index; continue; } } - Province const* province = get_province_by_colour(colour); - if (province != nullptr) { - const Province::index_t index = province->get_index(); - province_index_image[idx] = index; + const index_t index = get_index_from_colour(province_colour); + if (index != NULL_INDEX) { province_checklist[index - 1] = true; + province_shape_image[idx].index = index; continue; } - if (unrecognised_colours.find(colour) == unrecognised_colours.end()) { - unrecognised_colours.insert(colour); - Logger::error("Unrecognised province colour ", Province::colour_to_hex_string(colour), " at (", x, ", ", y, ")"); + if (unrecognised_province_colours.find(province_colour) == unrecognised_province_colours.end()) { + unrecognised_province_colours.insert(province_colour); + Logger::error("Unrecognised province colour ", Province::colour_to_hex_string(province_colour), " at (", x, ", ", y, ")"); ret = FAILURE; } - province_index_image[idx] = Province::NULL_INDEX; + province_shape_image[idx].index = NULL_INDEX; } } @@ -249,8 +259,8 @@ size_t Map::get_height() const { return height; } -std::vector<Province::index_t> const& Map::get_province_index_image() const { - return province_index_image; +std::vector<Map::shape_pixel_t> const& Map::get_province_shape_image() const { + return province_shape_image; } return_t Map::add_mapmode(std::string const& identifier, Mapmode::colour_func_t colour_func) { @@ -291,13 +301,15 @@ return_t Map::generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) Logger::error("Invalid mapmode index: ", index); return FAILURE; } - target += 4; // Skip past Province::NULL_INDEX + // Skip past Province::NULL_INDEX + for (size_t i = 0; i < MAPMODE_COLOUR_SIZE; ++i) + *target++ = 0; 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; - *target++ = province.is_water() ? 0 : 255; + *target++ = (colour >> 24) & 0xFF; } return SUCCESS; } diff --git a/extension/src/openvic2/map/Map.hpp b/extension/src/openvic2/map/Map.hpp index ebc23be..cb8dcb1 100644 --- a/extension/src/openvic2/map/Map.hpp +++ b/extension/src/openvic2/map/Map.hpp @@ -2,14 +2,14 @@ #include <functional> -#include "openvic2/map/Region.hpp" +#include "Region.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,14 +18,25 @@ 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: * MAP-4 */ struct Map { + using terrain_t = uint8_t; + using terrain_variant_map_t = std::map<colour_t, terrain_t>; + + #pragma pack(push, 1) + struct shape_pixel_t { + index_t index; + terrain_t terrain; + }; + #pragma pack(pop) private: + using colour_index_map_t = std::map<colour_t, index_t>; + IdentifierRegistry<Province> provinces; IdentifierRegistry<Region> regions; IdentifierRegistry<Mapmode> mapmodes; @@ -33,11 +44,14 @@ namespace OpenVic2 { size_t water_province_count = 0; size_t width = 0, height = 0; - std::vector<Province::index_t> province_index_image; + std::vector<shape_pixel_t> province_shape_image; + colour_index_map_t colour_index_map; + + index_t get_index_from_colour(colour_t colour) const; 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,27 +59,27 @@ 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; + 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; - return_t generate_province_index_image(size_t new_width, size_t new_height, uint8_t const* colour_data); + return_t 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); size_t get_width() const; size_t get_height() const; - std::vector<Province::index_t> const& get_province_index_image() const; + std::vector<shape_pixel_t> const& get_province_shape_image() const; return_t add_mapmode(std::string const& identifier, Mapmode::colour_func_t colour_func); void lock_mapmodes(); size_t get_mapmode_count() const; Mapmode const* get_mapmode_by_index(Mapmode::index_t index) const; Mapmode const* get_mapmode_by_identifier(std::string const& identifier) const; + static constexpr size_t MAPMODE_COLOUR_SIZE = 4; return_t generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) const; return_t generate_province_buildings(BuildingManager const& manager); diff --git a/extension/src/openvic2/map/Province.cpp b/extension/src/openvic2/map/Province.cpp index 4360bce..b3d455b 100644 --- a/extension/src/openvic2/map/Province.cpp +++ b/extension/src/openvic2/map/Province.cpp @@ -1,4 +1,4 @@ -#include "openvic2/map/Province.hpp" +#include "Province.hpp" #include <cassert> #include <sstream> @@ -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..9b07fc1 100644 --- a/extension/src/openvic2/map/Province.hpp +++ b/extension/src/openvic2/map/Province.hpp @@ -1,6 +1,6 @@ #pragma once -#include "openvic2/map/Building.hpp" +#include "Building.hpp" namespace OpenVic2 { struct Map; @@ -9,18 +9,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 +23,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..da0dfdd 100644 --- a/extension/src/openvic2/map/Region.cpp +++ b/extension/src/openvic2/map/Region.cpp @@ -1,7 +1,6 @@ -#include "openvic2/map/Region.hpp" +#include "Region.hpp" #include <cassert> -#include <algorithm> using namespace OpenVic2; @@ -19,8 +18,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..3920dfc 100644 --- a/extension/src/openvic2/map/Region.hpp +++ b/extension/src/openvic2/map/Region.hpp @@ -2,7 +2,7 @@ #include <set> -#include "openvic2/map/Province.hpp" +#include "Province.hpp" namespace OpenVic2 { @@ -25,6 +25,6 @@ namespace OpenVic2 { public: Region(Region&&) = default; - Province::colour_t get_colour() const; + colour_t get_colour() const; }; } |