aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic2/map
diff options
context:
space:
mode:
author Hop311 <hop3114@gmail.com>2023-05-01 00:59:54 +0200
committer Hop311 <hop3114@gmail.com>2023-05-01 00:59:54 +0200
commit2fec521cc6bbe7b2cda0eef3b830acbfc8b68333 (patch)
treee18e9cb252030e9b40ff91463ed117dd00a65789 /extension/src/openvic2/map
parentbce925ad8efa7bbf508e79cab2110416a71cb8ee (diff)
Hashmaps instead of linear + better province hover
Diffstat (limited to 'extension/src/openvic2/map')
-rw-r--r--extension/src/openvic2/map/Map.cpp30
-rw-r--r--extension/src/openvic2/map/Map.hpp11
2 files changed, 17 insertions, 24 deletions
diff --git a/extension/src/openvic2/map/Map.cpp b/extension/src/openvic2/map/Map.cpp
index ab035ad..1f44c43 100644
--- a/extension/src/openvic2/map/Map.cpp
+++ b/extension/src/openvic2/map/Map.cpp
@@ -36,11 +36,12 @@ return_t Map::add_province(std::string const& identifier, colour_t colour) {
return FAILURE;
}
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());
+ 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));
}
@@ -142,20 +143,10 @@ 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(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(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;
+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;
}
index_t Map::get_province_index_at(size_t x, size_t y) const {
@@ -236,9 +227,8 @@ return_t Map::generate_province_shape_image(size_t new_width, size_t new_height,
continue;
}
}
- Province const* province = get_province_by_colour(province_colour);
- if (province != nullptr) {
- const index_t index = province->get_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;
diff --git a/extension/src/openvic2/map/Map.hpp b/extension/src/openvic2/map/Map.hpp
index a075505..cb8dcb1 100644
--- a/extension/src/openvic2/map/Map.hpp
+++ b/extension/src/openvic2/map/Map.hpp
@@ -1,7 +1,6 @@
#pragma once
#include <functional>
-#include <unordered_map>
#include "Region.hpp"
@@ -27,7 +26,8 @@ namespace OpenVic2 {
*/
struct Map {
using terrain_t = uint8_t;
- using terrain_variant_map_t = std::unordered_map<colour_t, terrain_t>;
+ using terrain_variant_map_t = std::map<colour_t, terrain_t>;
+
#pragma pack(push, 1)
struct shape_pixel_t {
index_t index;
@@ -35,6 +35,8 @@ namespace OpenVic2 {
};
#pragma pack(pop)
private:
+ using colour_index_map_t = std::map<colour_t, index_t>;
+
IdentifierRegistry<Province> provinces;
IdentifierRegistry<Region> regions;
IdentifierRegistry<Mapmode> mapmodes;
@@ -43,6 +45,9 @@ namespace OpenVic2 {
size_t width = 0, height = 0;
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();
@@ -58,8 +63,6 @@ namespace OpenVic2 {
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(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);