aboutsummaryrefslogtreecommitdiff
path: root/extension
diff options
context:
space:
mode:
Diffstat (limited to 'extension')
-rw-r--r--extension/src/GameSingleton.cpp268
-rw-r--r--extension/src/GameSingleton.hpp59
-rw-r--r--extension/src/MapMesh.cpp18
-rw-r--r--extension/src/MapMesh.hpp10
-rw-r--r--extension/src/openvic2/Date.cpp4
-rw-r--r--extension/src/openvic2/GameAdvancementHook.cpp2
-rw-r--r--extension/src/openvic2/GameManager.cpp6
-rw-r--r--extension/src/openvic2/GameManager.hpp6
-rw-r--r--extension/src/openvic2/Good.cpp3
-rw-r--r--extension/src/openvic2/Good.hpp21
-rw-r--r--extension/src/openvic2/Logger.cpp2
-rw-r--r--extension/src/openvic2/Types.cpp20
-rw-r--r--extension/src/openvic2/Types.hpp57
-rw-r--r--extension/src/openvic2/map/Building.cpp6
-rw-r--r--extension/src/openvic2/map/Building.hpp4
-rw-r--r--extension/src/openvic2/map/Map.cpp120
-rw-r--r--extension/src/openvic2/map/Map.hpp38
-rw-r--r--extension/src/openvic2/map/Province.cpp20
-rw-r--r--extension/src/openvic2/map/Province.hpp12
-rw-r--r--extension/src/openvic2/map/Region.cpp8
-rw-r--r--extension/src/openvic2/map/Region.hpp4
21 files changed, 461 insertions, 227 deletions
diff --git a/extension/src/GameSingleton.cpp b/extension/src/GameSingleton.cpp
index 3811dea..3476ac5 100644
--- a/extension/src/GameSingleton.cpp
+++ b/extension/src/GameSingleton.cpp
@@ -17,15 +17,19 @@ void GameSingleton::_bind_methods() {
ClassDB::bind_method(D_METHOD("load_province_identifier_file", "file_path"), &GameSingleton::load_province_identifier_file);
ClassDB::bind_method(D_METHOD("load_water_province_file", "file_path"), &GameSingleton::load_water_province_file);
ClassDB::bind_method(D_METHOD("load_region_file", "file_path"), &GameSingleton::load_region_file);
- ClassDB::bind_method(D_METHOD("load_province_shape_file", "file_path"), &GameSingleton::load_province_shape_file);
+ ClassDB::bind_method(D_METHOD("load_terrain_file", "file_path"), &GameSingleton::load_terrain_file);
+ ClassDB::bind_method(D_METHOD("load_map_images", "province_image_path", "terrain_image_path"), &GameSingleton::load_map_images);
ClassDB::bind_method(D_METHOD("setup"), &GameSingleton::setup);
ClassDB::bind_method(D_METHOD("get_province_index_from_uv_coords", "coords"), &GameSingleton::get_province_index_from_uv_coords);
ClassDB::bind_method(D_METHOD("get_province_info_from_index", "index"), &GameSingleton::get_province_info_from_index);
ClassDB::bind_method(D_METHOD("get_width"), &GameSingleton::get_width);
ClassDB::bind_method(D_METHOD("get_height"), &GameSingleton::get_height);
- ClassDB::bind_method(D_METHOD("get_province_index_images"), &GameSingleton::get_province_index_images);
- ClassDB::bind_method(D_METHOD("get_province_colour_image"), &GameSingleton::get_province_colour_image);
+ ClassDB::bind_method(D_METHOD("get_aspect_ratio"), &GameSingleton::get_aspect_ratio);
+ ClassDB::bind_method(D_METHOD("get_terrain_texture"), &GameSingleton::get_terrain_texture);
+ ClassDB::bind_method(D_METHOD("get_province_shape_image_subdivisions"), &GameSingleton::get_province_shape_image_subdivisions);
+ ClassDB::bind_method(D_METHOD("get_province_shape_texture"), &GameSingleton::get_province_shape_texture);
+ ClassDB::bind_method(D_METHOD("get_province_colour_texture"), &GameSingleton::get_province_colour_texture);
ClassDB::bind_method(D_METHOD("update_colour_image"), &GameSingleton::update_colour_image);
ClassDB::bind_method(D_METHOD("get_mapmode_count"), &GameSingleton::get_mapmode_count);
@@ -54,27 +58,31 @@ GameSingleton* GameSingleton::get_singleton() {
/* REQUIREMENTS:
* MAP-21, MAP-25
*/
-GameSingleton::GameSingleton() : game_manager{ [this]() { emit_signal("state_updated"); } } {
+GameSingleton::GameSingleton() : game_manager{ [this]() { emit_signal("state_updated"); } }, terrain_variants{ "terrain variants" } {
ERR_FAIL_COND(singleton != nullptr);
singleton = this;
Logger::set_info_func([](std::string&& str) { UtilityFunctions::print(str.c_str()); });
Logger::set_error_func([](std::string&& str) { UtilityFunctions::push_error(str.c_str()); });
+ static constexpr colour_t HIGH_ALPHA_VALUE = to_alpha_value(0.5f);
+ static constexpr colour_t LOW_ALPHA_VALUE = to_alpha_value(0.2f);
using mapmode_t = std::pair<std::string, Mapmode::colour_func_t>;
const std::vector<mapmode_t> mapmodes = {
- { "mapmode_province", [](Map const&, Province const& province) -> Province::colour_t { return province.get_colour(); } },
- { "mapmode_region", [](Map const&, Province const& province) -> Province::colour_t {
- Region const* region = province.get_region();
- if (region != nullptr) return region->get_colour();
- return province.get_colour();
+ { "mapmode_terrain", [](Map const&, Province const& province) -> colour_t {
+ return LOW_ALPHA_VALUE | (province.is_water() ? 0x4287F5 : 0x0D7017);
+ } },
+ { "mapmode_province", [](Map const&, Province const& province) -> colour_t {
+ return HIGH_ALPHA_VALUE | province.get_colour();
} },
- { "mapmode_terrain", [](Map const&, Province const& province) -> Province::colour_t {
- return province.is_water() ? 0x4287F5 : 0x0D7017;
+ { "mapmode_region", [](Map const&, Province const& province) -> colour_t {
+ Region const* region = province.get_region();
+ if (region != nullptr) return (0xCC << 24) | region->get_colour();
+ return NULL_COLOUR;
} },
- { "mapmode_index", [](Map const& map, Province const& province) -> Province::colour_t {
+ { "mapmode_index", [](Map const& map, Province const& province) -> colour_t {
const uint8_t f = static_cast<float>(province.get_index()) / static_cast<float>(map.get_province_count()) * 255.0f;
- return (f << 16) | (f << 8) | f;
+ return HIGH_ALPHA_VALUE | (f << 16) | (f << 8) | f;
} }
};
for (mapmode_t const& mapmode : mapmodes)
@@ -96,7 +104,7 @@ GameSingleton::~GameSingleton() {
singleton = nullptr;
}
-static Error load_json_file(String const& file_description, String const& file_path, Variant& result) {
+static Error _load_json_file(String const& file_description, String const& file_path, Variant& result) {
result.clear();
UtilityFunctions::print("Loading ", file_description, " file: ", file_path);
const Ref<FileAccess> file = FileAccess::open(file_path, FileAccess::ModeFlags::READ);
@@ -119,10 +127,10 @@ static Error load_json_file(String const& file_description, String const& file_p
using parse_json_entry_func_t = std::function<godot::Error (godot::String const&, godot::Variant const&)>;
-static Error parse_json_dictionary_file(String const& file_description, String const& file_path,
+static Error _parse_json_dictionary_file(String const& file_description, String const& file_path,
String const& identifier_prefix, parse_json_entry_func_t parse_entry) {
Variant json_var;
- Error err = load_json_file(file_description, file_path, json_var);
+ Error err = _load_json_file(file_description, file_path, json_var);
if (err != OK) return err;
const Variant::Type type = json_var.get_type();
if (type != Variant::DICTIONARY) {
@@ -147,40 +155,38 @@ static Error parse_json_dictionary_file(String const& file_description, String c
return err;
}
-Error GameSingleton::_parse_province_identifier_entry(String const& identifier, Variant const& entry) {
- const Variant::Type type = entry.get_type();
- Province::colour_t colour = Province::NULL_COLOUR;
+static colour_t _parse_colour(Variant const& var) {
+ const Variant::Type type = var.get_type();
if (type == Variant::ARRAY) {
- Array const& colour_array = entry;
+ Array const& colour_array = var;
if (colour_array.size() == 3) {
+ colour_t colour = NULL_COLOUR;
for (int jdx = 0; jdx < 3; ++jdx) {
Variant const& var = colour_array[jdx];
- if (var.get_type() != Variant::FLOAT) {
- colour = Province::NULL_COLOUR;
- break;
- }
+ if (var.get_type() != Variant::FLOAT) return NULL_COLOUR;
const double colour_double = var;
- if (std::trunc(colour_double) != colour_double) {
- colour = Province::NULL_COLOUR;
- break;
- }
+ if (std::trunc(colour_double) != colour_double) return NULL_COLOUR;
const int64_t colour_int = static_cast<int64_t>(colour_double);
- if (colour_int < 0 || colour_int > 255) {
- colour = Province::NULL_COLOUR;
- break;
- }
+ if (colour_int < 0 || colour_int > 255) return NULL_COLOUR;
colour = (colour << 8) | colour_int;
}
+ return colour;
}
- } else if (type == Variant::STRING) {
- String const& colour_string = entry;
+ }
+ else if (type == Variant::STRING) {
+ String const& colour_string = var;
if (colour_string.is_valid_hex_number()) {
const int64_t colour_int = colour_string.hex_to_int();
- if (0 <= colour_int && colour_int <= 0xFFFFFF)
- colour = colour_int;
+ if (colour_int != NULL_COLOUR && colour_int <= MAX_COLOUR_RGB)
+ return colour_int;
}
}
- if (colour == Province::NULL_COLOUR) {
+ return NULL_COLOUR;
+}
+
+Error GameSingleton::_parse_province_identifier_entry(String const& identifier, Variant const& entry) {
+ const colour_t colour = _parse_colour(entry);
+ if (colour == NULL_COLOUR) {
UtilityFunctions::push_error("Invalid colour for province identifier \"", identifier, "\": ", entry);
return FAILED;
}
@@ -188,7 +194,7 @@ Error GameSingleton::_parse_province_identifier_entry(String const& identifier,
}
Error GameSingleton::load_province_identifier_file(String const& file_path) {
- const Error err = parse_json_dictionary_file("province identifier", file_path, "prov_",
+ const Error err = _parse_json_dictionary_file("province identifier", file_path, "prov_",
[this](String const& identifier, Variant const& entry) -> Error {
return _parse_province_identifier_entry(identifier, entry);
});
@@ -208,7 +214,8 @@ Error GameSingleton::_parse_region_entry(String const& identifier, Variant const
if (type == Variant::STRING) {
String const& province_string = province_var;
province_identifiers.push_back(province_string.utf8().get_data());
- } else {
+ }
+ else {
UtilityFunctions::push_error("Invalid province identifier for region \"", identifier, "\": ", entry);
err = FAILED;
}
@@ -222,7 +229,7 @@ Error GameSingleton::_parse_region_entry(String const& identifier, Variant const
}
Error GameSingleton::load_region_file(String const& file_path) {
- const Error err = parse_json_dictionary_file("region", file_path, "region_",
+ const Error err = _parse_json_dictionary_file("region", file_path, "region_",
[this](String const& identifier, Variant const& entry) -> Error {
return _parse_region_entry(identifier, entry);
});
@@ -230,53 +237,134 @@ Error GameSingleton::load_region_file(String const& file_path) {
return err;
}
-Error GameSingleton::load_province_shape_file(String const& file_path) {
- if (province_index_image[0].is_valid()) {
- UtilityFunctions::push_error("Province shape file has already been loaded, cannot load: ", file_path);
+TerrainVariant::TerrainVariant(std::string const& new_identfier, colour_t new_colour, Ref<Image> const& new_image)
+ : HasIdentifier(new_identfier), HasColour(new_colour), image(new_image) {}
+
+Ref<Image> TerrainVariant::get_image() const { return image; }
+
+Error GameSingleton::_parse_terrain_entry(String const& identifier, Variant const& entry) {
+ const colour_t colour = _parse_colour(entry);
+ if (colour == NULL_COLOUR) {
+ UtilityFunctions::push_error("Invalid colour for terrain texture \"", identifier, "\": ", entry);
return FAILED;
}
- Ref<Image> province_shape_image;
- province_shape_image.instantiate();
- Error err = province_shape_image->load(file_path);
+ static const String terrain_folder = "res://art/terrain/";
+ const String terrain_path = terrain_folder + identifier;
+ Ref<Image> terrain_image;
+ terrain_image.instantiate();
+ const Error err = terrain_image->load(terrain_path);
if (err != OK) {
- UtilityFunctions::push_error("Failed to load province shape file: ", file_path);
+ UtilityFunctions::push_error("Failed to load terrain image: ", terrain_path);
return err;
}
- const int32_t width = province_shape_image->get_width();
- const int32_t height = province_shape_image->get_height();
- if (width < 1 || height < 1) {
- UtilityFunctions::push_error("Invalid dimensions (", width, "x", height, ") for province shape file: ", file_path);
+ return ERR(terrain_variants.add_item({ identifier.utf8().get_data(), colour, terrain_image }));
+}
+
+Error GameSingleton::load_terrain_file(String const& file_path) {
+ Error parse_err = _parse_json_dictionary_file("terrain variants", file_path, "",
+ [this](String const& identifier, Variant const& entry) -> Error {
+ return _parse_terrain_entry(identifier, entry);
+ });
+ terrain_variants.lock();
+ if (terrain_variants.get_item_count() == 0) parse_err = FAILED;
+
+ Array terrain_images;
+ for (TerrainVariant const& var : terrain_variants.get_items()) {
+ terrain_variant_map[var.get_colour()] = terrain_images.size();
+ terrain_images.append(var.get_image());
+ }
+
+ terrain_texture.instantiate();
+ const Error texturearray_err = terrain_texture->create_from_images(terrain_images);
+ if (texturearray_err != OK) {
+ UtilityFunctions::push_error("Failed to create terrain texture array!");
+ return texturearray_err;
+ }
+ return parse_err;
+}
+
+Error GameSingleton::load_map_images(String const& province_image_path, String const& terrain_image_path) {
+ if (province_shape_texture.is_valid()) {
+ UtilityFunctions::push_error("Map images have already been loaded, cannot load: ", province_image_path, " and ", terrain_image_path);
+ return FAILED;
+ }
+
+ // Load images
+ Ref<Image> province_image, terrain_image;
+ province_image.instantiate();
+ terrain_image.instantiate();
+ Error err = province_image->load(province_image_path);
+ if (err != OK) {
+ UtilityFunctions::push_error("Failed to load province image: ", province_image_path);
+ return err;
+ }
+ err = terrain_image->load(terrain_image_path);
+ if (err != OK) {
+ UtilityFunctions::push_error("Failed to load terrain image: ", terrain_image_path);
+ return err;
+ }
+
+ // Validate dimensions and format
+ const Vector2i province_dims = province_image->get_size(), terrain_dims = terrain_image->get_size();
+ if (province_dims.x < 1 || province_dims.y < 1) {
+ UtilityFunctions::push_error("Invalid dimensions (", province_dims.x, "x", province_dims.y, ") for province image: ", province_image_path);
err = FAILED;
}
- if (width % image_width_divide != 0) {
- UtilityFunctions::push_error("Invalid width ", width, " (must be divisible by ", image_width_divide, ") for province shape file: ", file_path);
+ if (province_dims != terrain_dims) {
+ UtilityFunctions::push_error("Invalid dimensions (", terrain_dims.x, "x", terrain_dims.y, ") for terrain image: ",
+ terrain_image_path, " (must match province image: (", province_dims.x, "x", province_dims.x, "))");
err = FAILED;
}
static constexpr Image::Format expected_format = Image::FORMAT_RGB8;
- const Image::Format format = province_shape_image->get_format();
- if (format != expected_format) {
- UtilityFunctions::push_error("Invalid format (", format, ", should be ", expected_format, ") for province shape file: ", file_path);
+ const Image::Format province_format = province_image->get_format(), terrain_format = terrain_image->get_format();
+ if (province_format != expected_format) {
+ UtilityFunctions::push_error("Invalid format (", province_format, ", should be ", expected_format, ") for province image: ", province_image_path);
+ err = FAILED;
+ }
+ if (terrain_format != expected_format) {
+ UtilityFunctions::push_error("Invalid format (", terrain_format, ", should be ", expected_format, ") for terrain image: ", terrain_image_path);
err = FAILED;
}
if (err != OK) return err;
- err = ERR(game_manager.map.generate_province_index_image(width, height, province_shape_image->get_data().ptr()));
-
- std::vector<Province::index_t> const& province_index_data = game_manager.map.get_province_index_image();
- const int32_t divided_width = width / image_width_divide;
- for (int32_t i = 0; i < image_width_divide; ++i) {
- PackedByteArray index_data_array;
- index_data_array.resize(divided_width * height * sizeof(Province::index_t));
- for (int32_t y = 0; y < height; ++y)
- memcpy(index_data_array.ptrw() + y * divided_width * sizeof(Province::index_t),
- province_index_data.data() + y * width + i * divided_width,
- divided_width * sizeof(Province::index_t));
- province_index_image[i] = Image::create_from_data(divided_width, height, false, Image::FORMAT_RG8, index_data_array);
- if (province_index_image[i].is_null()) {
- UtilityFunctions::push_error("Failed to create province ID image #", i);
- err = FAILED;
+
+ // Generate interleaved province and terrain ID image
+ if (game_manager.map.generate_province_shape_image(province_dims.x, province_dims.y, province_image->get_data().ptr(),
+ terrain_image->get_data().ptr(), terrain_variant_map) != SUCCESS) return FAILED;
+
+ static constexpr int32_t GPU_DIM_LIMIT = 0x3FFF;
+ // For each dimension of the image, this finds the small number of equal subdivisions required get the individual texture dims under GPU_DIM_LIMIT
+ for (int i = 0; i < 2; ++i) for (image_subdivisions[i] = 1;
+ province_dims[i] / image_subdivisions[i] > GPU_DIM_LIMIT || province_dims[i] % image_subdivisions[i] != 0; ++image_subdivisions[i]);
+
+ Map::shape_pixel_t const* province_shape_data = game_manager.map.get_province_shape_image().data();
+ const Vector2i divided_dims = province_dims / image_subdivisions;
+ Array province_shape_images;
+ province_shape_images.resize(image_subdivisions.x * image_subdivisions.y);
+ for (int32_t v = 0; v < image_subdivisions.y; ++v) {
+ for (int32_t u = 0; u < image_subdivisions.x; ++u) {
+ PackedByteArray index_data_array;
+ index_data_array.resize(divided_dims.x * divided_dims.y * sizeof(Map::shape_pixel_t));
+
+ for (int32_t y = 0; y < divided_dims.y; ++y)
+ memcpy(index_data_array.ptrw() + y * divided_dims.x * sizeof(Map::shape_pixel_t),
+ province_shape_data + (v * divided_dims.y + y) * province_dims.x + u * divided_dims.x,
+ divided_dims.x * sizeof(Map::shape_pixel_t));
+
+ const Ref<Image> province_shape_subimage = Image::create_from_data(divided_dims.x, divided_dims.y, false, Image::FORMAT_RGB8, index_data_array);
+ if (province_shape_subimage.is_null()) {
+ UtilityFunctions::push_error("Failed to create province shape image (", u, ", ", v, ")");
+ err = FAILED;
+ }
+ province_shape_images[u + v * image_subdivisions.x] = province_shape_subimage;
}
}
+ province_shape_texture.instantiate();
+ if (province_shape_texture->create_from_images(province_shape_images) != OK) {
+ UtilityFunctions::push_error("");
+ err = FAILED;
+ }
+
if (update_colour_image() != OK) err = FAILED;
return err;
@@ -288,14 +376,15 @@ godot::Error GameSingleton::setup() {
Error GameSingleton::load_water_province_file(String const& file_path) {
Variant json_var;
- Error err = load_json_file("water province", file_path, json_var);
+ Error err = _load_json_file("water province", file_path, json_var);
if (err != OK) return err;
Variant::Type type = json_var.get_type();
if (type != Variant::ARRAY) {
UtilityFunctions::push_error("Invalid water province JSON: root has type ",
Variant::get_type_name(type), " (expected Array)");
err = FAILED;
- } else {
+ }
+ else {
Array const& array = json_var;
for (int64_t idx = 0; idx < array.size(); ++idx) {
Variant const& entry = array[idx];
@@ -340,7 +429,7 @@ Dictionary GameSingleton::get_province_info_from_index(int32_t index) const {
buildings_array.resize(buildings.size());
for (size_t idx = 0; idx < buildings.size(); ++idx) {
KEY(building) KEY(level) KEY(expansion_state) KEY(start_date) KEY(end_date) KEY(expansion_progress)
-
+
Dictionary building_dict;
Building const& building = buildings[idx];
building_dict[building_key] = building.get_identifier().c_str();
@@ -366,27 +455,36 @@ int32_t GameSingleton::get_height() const {
return game_manager.map.get_height();
}
-Array GameSingleton::get_province_index_images() const {
- Array ret;
- for (int i = 0; i < image_width_divide; ++i)
- ret.append(province_index_image[i]);
- return ret;
+float GameSingleton::get_aspect_ratio() const {
+ return static_cast<float>(get_width()) / static_cast<float>(get_height());
+}
+
+Ref<Texture> GameSingleton::get_terrain_texture() const {
+ return terrain_texture;
+}
+
+Vector2i GameSingleton::get_province_shape_image_subdivisions() const {
+ return image_subdivisions;
+}
+
+Ref<Texture> GameSingleton::get_province_shape_texture() const {
+ return province_shape_texture;
}
-Ref<Image> GameSingleton::get_province_colour_image() const {
- return province_colour_image;
+Ref<Texture> GameSingleton::get_province_colour_texture() const {
+ return province_colour_texture;
}
Error GameSingleton::update_colour_image() {
static PackedByteArray colour_data_array;
- static constexpr int64_t colour_data_array_size = (Province::MAX_INDEX + 1) * 4;
+ static constexpr int64_t colour_data_array_size = (MAX_INDEX + 1) * Map::MAPMODE_COLOUR_SIZE;
colour_data_array.resize(colour_data_array_size);
Error err = OK;
if (game_manager.map.generate_mapmode_colours(mapmode_index, colour_data_array.ptrw()) != SUCCESS)
err = FAILED;
- static constexpr int32_t PROVINCE_INDEX_SQRT = 1 << (sizeof(Province::index_t) * 4);
+ static constexpr int32_t PROVINCE_INDEX_SQRT = 1 << (sizeof(index_t) * 4);
if (province_colour_image.is_null())
province_colour_image.instantiate();
province_colour_image->set_data(PROVINCE_INDEX_SQRT, PROVINCE_INDEX_SQRT,
@@ -395,6 +493,10 @@ Error GameSingleton::update_colour_image() {
UtilityFunctions::push_error("Failed to update province colour image");
return FAILED;
}
+ if (province_colour_texture.is_null())
+ province_colour_texture = ImageTexture::create_from_image(province_colour_image);
+ else
+ province_colour_texture->update(province_colour_image);
return err;
}
diff --git a/extension/src/GameSingleton.hpp b/extension/src/GameSingleton.hpp
index d9879ef..80b2d95 100644
--- a/extension/src/GameSingleton.hpp
+++ b/extension/src/GameSingleton.hpp
@@ -1,12 +1,21 @@
#pragma once
-#include <functional>
-
-#include <godot_cpp/classes/image.hpp>
+#include <godot_cpp/classes/image_texture.hpp>
+#include <godot_cpp/classes/texture2d_array.hpp>
#include "openvic2/GameManager.hpp"
namespace OpenVic2 {
+ struct TerrainVariant : HasIdentifier, HasColour {
+ private:
+ const godot::Ref<godot::Image> image;
+ public:
+ TerrainVariant(std::string const& new_identfier, colour_t new_colour,
+ godot::Ref<godot::Image> const& new_image);
+ TerrainVariant(TerrainVariant&&) = default;
+
+ godot::Ref<godot::Image> get_image() const;
+ };
class GameSingleton : public godot::Object {
GDCLASS(GameSingleton, godot::Object)
@@ -14,12 +23,18 @@ namespace OpenVic2 {
GameManager game_manager;
- static constexpr int image_width_divide = 2;
- godot::Ref<godot::Image> province_index_image[image_width_divide], province_colour_image;
+ godot::Vector2i image_subdivisions;
+ godot::Ref<godot::Texture2DArray> province_shape_texture;
+ godot::Ref<godot::Image> province_colour_image;
+ godot::Ref<godot::ImageTexture> province_colour_texture;
Mapmode::index_t mapmode_index = 0;
+ IdentifierRegistry<TerrainVariant> terrain_variants;
+ Map::terrain_variant_map_t terrain_variant_map;
+ godot::Ref<godot::Texture2DArray> terrain_texture;
godot::Error _parse_province_identifier_entry(godot::String const& identifier, godot::Variant const& entry);
godot::Error _parse_region_entry(godot::String const& identifier, godot::Variant const& entry);
+ godot::Error _parse_terrain_entry(godot::String const& identifier, godot::Variant const& entry);
void _tick();
protected:
static void _bind_methods();
@@ -33,17 +48,45 @@ namespace OpenVic2 {
godot::Error load_province_identifier_file(godot::String const& file_path);
godot::Error load_water_province_file(godot::String const& file_path);
godot::Error load_region_file(godot::String const& file_path);
- godot::Error load_province_shape_file(godot::String const& file_path);
+ godot::Error load_terrain_file(godot::String const& file_path);
+ godot::Error load_map_images(godot::String const& province_image_path, godot::String const& terrain_image_path);
+
+ /* Post-load/restart game setup - reset the game to post-load state
+ * and (re)generate starting data, e.g. buildings.
+ */
godot::Error setup();
int32_t get_province_index_from_uv_coords(godot::Vector2 const& coords) const;
godot::Dictionary get_province_info_from_index(int32_t index) const;
int32_t get_width() const;
int32_t get_height() const;
- godot::Array get_province_index_images() const;
- godot::Ref<godot::Image> get_province_colour_image() const;
+ float get_aspect_ratio() const;
+ /* The cosmetic terrain textures stored in a Texture2DArray.
+ */
+ godot::Ref<godot::Texture> get_terrain_texture() const;
+
+ /* Number of (vertical, horizontal) subdivisions the province shape image
+ * was split into when making the province_shape_texture to ensure no
+ * piece had a dimension greater than 16383.
+ */
+ godot::Vector2i get_province_shape_image_subdivisions() const;
+
+ /* The map, encoded in RGB8 with RG representing province index and B representing terrain texture.
+ * To support a wider range of GPUs, the image is divided so that no piece has a dimension
+ * greater than 16383 and the pieces are stored in a Texture2DArray.
+ */
+ godot::Ref<godot::Texture> get_province_shape_texture() const;
+
+ /* The colour each province should be tinted, arranged in
+ * index order into a 256x256 RGB8 texture.
+ */
+ godot::Ref<godot::Texture> get_province_colour_texture() const;
+
+ /* Generate the province_colour_texture from the current mapmode.
+ */
godot::Error update_colour_image();
+
int32_t get_mapmode_count() const;
godot::String get_mapmode_identifier(int32_t index) const;
godot::Error set_mapmode(godot::String const& identifier);
diff --git a/extension/src/MapMesh.cpp b/extension/src/MapMesh.cpp
index 91c7611..6d94973 100644
--- a/extension/src/MapMesh.cpp
+++ b/extension/src/MapMesh.cpp
@@ -50,21 +50,21 @@ float MapMesh::get_repeat_proportion() const {
return repeat_proportion;
}
-void MapMesh::set_subdivide_width(const int divisions) {
+void MapMesh::set_subdivide_width(const int32_t divisions) {
subdivide_w = divisions > 0 ? divisions : 0;
_request_update();
}
-int MapMesh::get_subdivide_width() const {
+int32_t MapMesh::get_subdivide_width() const {
return subdivide_w;
}
-void MapMesh::set_subdivide_depth(const int divisions) {
+void MapMesh::set_subdivide_depth(const int32_t divisions) {
subdivide_d = divisions > 0 ? divisions : 0;
_request_update();
}
-int MapMesh::get_subdivide_depth() const {
+int32_t MapMesh::get_subdivide_depth() const {
return subdivide_d;
}
@@ -81,8 +81,8 @@ Array MapMesh::_create_mesh_array() const {
Array arr;
arr.resize(Mesh::ARRAY_MAX);
- const int vertex_count = (subdivide_w + 2) * (subdivide_d + 2);
- const int indice_count = (subdivide_w + 1) * (subdivide_d + 1) * 6;
+ const int32_t vertex_count = (subdivide_w + 2) * (subdivide_d + 2);
+ const int32_t indice_count = (subdivide_w + 1) * (subdivide_d + 1) * 6;
PackedVector3Array points;
PackedVector3Array normals;
@@ -100,17 +100,17 @@ Array MapMesh::_create_mesh_array() const {
const Size2 uv_size{ 1.0f + 2.0f * repeat_proportion, 1.0f };
const Size2 size{ aspect_ratio * uv_size.x, uv_size.y }, start_pos = size * -0.5f;
- int point_index = 0, thisrow = 0, prevrow = 0, indice_index = 0;
+ int32_t point_index = 0, thisrow = 0, prevrow = 0, indice_index = 0;
Vector2 subdivide_step{ 1.0f / (subdivide_w + 1.0f) , 1.0f / (subdivide_d + 1.0f) };
Vector3 point{ 0.0f, 0.0f, start_pos.y };
Vector2 point_step = subdivide_step * size;
Vector2 uv{}, uv_step = subdivide_step * uv_size;
- for (int j = 0; j <= subdivide_d + 1; ++j) {
+ for (int32_t j = 0; j <= subdivide_d + 1; ++j) {
point.x = start_pos.x;
uv.x = -repeat_proportion;
- for (int i = 0; i <= subdivide_w + 1; ++i) {
+ for (int32_t i = 0; i <= subdivide_w + 1; ++i) {
points[point_index] = point;
normals[point_index] = normal;
tangents[point_index * 4 + 0] = 1.0f;
diff --git a/extension/src/MapMesh.hpp b/extension/src/MapMesh.hpp
index d8727cf..89c0147 100644
--- a/extension/src/MapMesh.hpp
+++ b/extension/src/MapMesh.hpp
@@ -7,7 +7,7 @@ namespace OpenVic2 {
GDCLASS(MapMesh, godot::PrimitiveMesh)
float aspect_ratio = 2.0f, repeat_proportion = 0.5f;
- int subdivide_w = 0, subdivide_d = 0;
+ int32_t subdivide_w = 0, subdivide_d = 0;
protected:
static void _bind_methods();
@@ -20,11 +20,11 @@ namespace OpenVic2 {
void set_repeat_proportion(const float proportion);
float get_repeat_proportion() const;
- void set_subdivide_width(const int divisions);
- int get_subdivide_width() const;
+ void set_subdivide_width(const int32_t divisions);
+ int32_t get_subdivide_width() const;
- void set_subdivide_depth(const int divisions);
- int get_subdivide_depth() const;
+ void set_subdivide_depth(const int32_t divisions);
+ int32_t get_subdivide_depth() const;
godot::AABB get_core_aabb() const;
bool is_valid_uv_coord(godot::Vector2 const& uv) const;
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;
};
}