aboutsummaryrefslogtreecommitdiff
path: root/src/openvic/map
diff options
context:
space:
mode:
author Hop311 <Hop3114@gmail.com>2023-05-22 17:54:15 +0200
committer GitHub <noreply@github.com>2023-05-22 17:54:15 +0200
commit08ec6fe5fbf52d814d88c235aac84bb95ad4e322 (patch)
tree136a221eb5e7c895c8219778b3d206f2ed9e8e7f /src/openvic/map
parent15e960f93ced8c94a6a45ebb2b44d0705ff7f8f6 (diff)
parent7874702f30d5855319faf197b10aed31f07f5e27 (diff)
Merge pull request #5 from OpenVicProject/bmp
BMP palette parser + code style cleanup
Diffstat (limited to 'src/openvic/map')
-rw-r--r--src/openvic/map/Building.cpp14
-rw-r--r--src/openvic/map/Building.hpp14
-rw-r--r--src/openvic/map/Map.cpp35
-rw-r--r--src/openvic/map/Map.hpp11
-rw-r--r--src/openvic/map/Province.cpp10
-rw-r--r--src/openvic/map/Province.hpp1
-rw-r--r--src/openvic/map/Region.cpp2
-rw-r--r--src/openvic/map/Region.hpp3
8 files changed, 65 insertions, 25 deletions
diff --git a/src/openvic/map/Building.cpp b/src/openvic/map/Building.cpp
index ccd5ad7..317ccdf 100644
--- a/src/openvic/map/Building.cpp
+++ b/src/openvic/map/Building.cpp
@@ -2,12 +2,14 @@
#include <cassert>
-#include "../Logger.hpp"
+#include "../utility/Logger.hpp"
#include "Province.hpp"
using namespace OpenVic;
-Building::Building(BuildingType const& new_type) : HasIdentifier{ new_type.get_identifier() }, type{ new_type } {}
+Building::Building(BuildingType const& new_type)
+ : HasIdentifier { new_type.get_identifier() },
+ type { new_type } {}
bool Building::_can_expand() const {
return level < type.get_max_level();
@@ -74,8 +76,10 @@ void Building::tick(Date const& today) {
}
}
-BuildingType::BuildingType(std::string const& new_identifier, Building::level_t new_max_level, Timespan new_build_time) :
- HasIdentifier{ new_identifier }, max_level{ new_max_level }, build_time{ new_build_time } {
+BuildingType::BuildingType(std::string const& new_identifier, Building::level_t new_max_level, Timespan new_build_time)
+ : HasIdentifier { new_identifier },
+ max_level { new_max_level },
+ build_time { new_build_time } {
assert(max_level >= 0);
assert(build_time >= 0);
}
@@ -88,7 +92,7 @@ Timespan BuildingType::get_build_time() const {
return build_time;
}
-BuildingManager::BuildingManager() : building_types{ "building types" } {}
+BuildingManager::BuildingManager() : building_types { "building types" } {}
return_t BuildingManager::add_building_type(std::string const& identifier, Building::level_t max_level, Timespan build_time) {
if (identifier.empty()) {
diff --git a/src/openvic/map/Building.hpp b/src/openvic/map/Building.hpp
index ca5c196..98c3991 100644
--- a/src/openvic/map/Building.hpp
+++ b/src/openvic/map/Building.hpp
@@ -2,8 +2,8 @@
#include <vector>
-#include "../Types.hpp"
#include "../Date.hpp"
+#include "../Types.hpp"
namespace OpenVic {
struct Province;
@@ -19,7 +19,13 @@ namespace OpenVic {
using level_t = int8_t;
- enum class ExpansionState { CannotExpand, CanExpand, Preparing, Expanding };
+ enum class ExpansionState {
+ CannotExpand,
+ CanExpand,
+ Preparing,
+ Expanding
+ };
+
private:
BuildingType const& type;
level_t level = 0;
@@ -30,6 +36,7 @@ namespace OpenVic {
Building(BuildingType const& new_type);
bool _can_expand() const;
+
public:
Building(Building&&) = default;
@@ -49,11 +56,13 @@ namespace OpenVic {
struct BuildingType : HasIdentifier {
friend struct BuildingManager;
+
private:
const Building::level_t max_level;
const Timespan build_time;
BuildingType(std::string const& new_identifier, Building::level_t new_max_level, Timespan new_build_time);
+
public:
BuildingType(BuildingType&&) = default;
@@ -64,6 +73,7 @@ namespace OpenVic {
struct BuildingManager {
private:
IdentifierRegistry<BuildingType> building_types;
+
public:
BuildingManager();
diff --git a/src/openvic/map/Map.cpp b/src/openvic/map/Map.cpp
index af1ea8a..2efee32 100644
--- a/src/openvic/map/Map.cpp
+++ b/src/openvic/map/Map.cpp
@@ -3,16 +3,20 @@
#include <cassert>
#include <unordered_set>
-#include "../Logger.hpp"
#include "../economy/Good.hpp"
+#include "../utility/Logger.hpp"
using namespace OpenVic;
Mapmode::Mapmode(index_t new_index, std::string const& new_identifier, colour_func_t new_colour_func)
- : HasIdentifier{ new_identifier }, index{ new_index }, colour_func{ new_colour_func } {
+ : HasIdentifier { new_identifier },
+ index { new_index },
+ colour_func { new_colour_func } {
assert(colour_func != nullptr);
}
+const Mapmode Mapmode::ERROR_MAPMODE { 0, "mapmode_error", [](Map const& map, Province const& province) -> colour_t { return 0xFFFF0000; } };
+
Mapmode::index_t Mapmode::get_index() const {
return index;
}
@@ -21,7 +25,9 @@ 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" } {}
+Map::Map() : provinces { "provinces" },
+ regions { "regions" },
+ mapmodes { "mapmodes" } {}
return_t Map::add_province(std::string const& identifier, colour_t colour) {
if (provinces.get_item_count() >= MAX_INDEX) {
@@ -36,7 +42,7 @@ return_t Map::add_province(std::string const& identifier, colour_t colour) {
Logger::error("Invalid province colour: ", Province::colour_to_hex_string(colour));
return FAILURE;
}
- Province new_province{ static_cast<index_t>(provinces.get_item_count() + 1), identifier, colour };
+ 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());
@@ -79,7 +85,7 @@ return_t Map::add_region(std::string const& identifier, std::vector<std::string>
Logger::error("Invalid region identifier - empty!");
return FAILURE;
}
- Region new_region{ identifier };
+ Region new_region { identifier };
return_t ret = SUCCESS;
for (std::string const& province_identifier : province_identifiers) {
Province* province = get_province_by_identifier(province_identifier);
@@ -176,7 +182,8 @@ Region const* Map::get_region_by_identifier(std::string const& identifier) const
}
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];
+ idx *= 3;
+ return (colour_data[idx] << 16) | (colour_data[idx + 1] << 8) | colour_data[idx + 2];
}
return_t Map::generate_province_shape_image(size_t new_width, size_t new_height, uint8_t const* colour_data,
@@ -309,10 +316,17 @@ return_t Map::generate_mapmode_colours(Mapmode::index_t index, uint8_t* target)
Logger::error("Mapmode colour target pointer is null!");
return FAILURE;
}
+ return_t ret = SUCCESS;
Mapmode const* mapmode = mapmodes.get_item_by_index(index);
if (mapmode == nullptr) {
- Logger::error("Invalid mapmode index: ", index);
- return FAILURE;
+ // Not an error if mapmodes haven't yet been loaded,
+ // e.g. if we want to allocate the province colour
+ // texture before mapmodes are loaded.
+ if (!(mapmodes.get_item_count() == 0 && index == 0)) {
+ Logger::error("Invalid mapmode index: ", index);
+ ret = FAILURE;
+ }
+ mapmode = &Mapmode::ERROR_MAPMODE;
}
// Skip past Province::NULL_INDEX
for (size_t i = 0; i < MAPMODE_COLOUR_SIZE; ++i)
@@ -324,13 +338,14 @@ return_t Map::generate_mapmode_colours(Mapmode::index_t index, uint8_t* target)
*target++ = colour & FULL_COLOUR;
*target++ = (colour >> 24) & FULL_COLOUR;
}
- return SUCCESS;
+ return ret;
}
return_t Map::setup(GoodManager const& good_manager, BuildingManager const& building_manager) {
return_t ret = SUCCESS;
for (Province& province : provinces.get_items()) {
- if (!province.is_water()) // Set all land provinces to have an RGO based on their index to test them
+ // Set all land provinces to have an RGO based on their index to test them
+ if (!province.is_water() && good_manager.get_good_count() > 0)
province.rgo = good_manager.get_good_by_index(province.get_index() % good_manager.get_good_count());
if (building_manager.generate_province_buildings(province) != SUCCESS) ret = FAILURE;
}
diff --git a/src/openvic/map/Map.hpp b/src/openvic/map/Map.hpp
index 39c37c0..3533a14 100644
--- a/src/openvic/map/Map.hpp
+++ b/src/openvic/map/Map.hpp
@@ -9,14 +9,18 @@ namespace OpenVic {
struct Mapmode : HasIdentifier {
friend struct Map;
- using colour_func_t = std::function<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;
const colour_func_t colour_func;
Mapmode(index_t new_index, std::string const& new_identifier, colour_func_t new_colour_func);
+
public:
+ static const Mapmode ERROR_MAPMODE;
+
index_t get_index() const;
colour_t get_colour(Map const& map, Province const& province) const;
};
@@ -30,12 +34,12 @@ namespace OpenVic {
using terrain_t = uint8_t;
using terrain_variant_map_t = std::map<colour_t, terrain_t>;
- #pragma pack(push, 1)
+#pragma pack(push, 1)
struct shape_pixel_t {
index_t index;
terrain_t terrain;
};
- #pragma pack(pop)
+#pragma pack(pop)
private:
using colour_index_map_t = std::map<colour_t, index_t>;
@@ -51,6 +55,7 @@ namespace OpenVic {
index_t selected_province = NULL_INDEX;
index_t get_index_from_colour(colour_t colour) const;
+
public:
Map();
diff --git a/src/openvic/map/Province.cpp b/src/openvic/map/Province.cpp
index b169021..d2a5ecf 100644
--- a/src/openvic/map/Province.cpp
+++ b/src/openvic/map/Province.cpp
@@ -1,13 +1,16 @@
#include "Province.hpp"
#include <cassert>
-#include <sstream>
#include <iomanip>
+#include <sstream>
using namespace OpenVic;
-Province::Province(index_t new_index, std::string const& new_identifier, colour_t new_colour) :
- HasIdentifier{ new_identifier }, HasColour{ new_colour }, index{ new_index }, buildings{ "buildings" } {
+Province::Province(index_t new_index, std::string const& new_identifier, colour_t new_colour)
+ : HasIdentifier { new_identifier },
+ HasColour { new_colour },
+ index { new_index },
+ buildings { "buildings" } {
assert(index != NULL_INDEX);
}
@@ -66,7 +69,6 @@ std::string Province::to_string() const {
void Province::update_state(Date const& today) {
for (Building& building : buildings.get_items())
building.update_state(today);
-
}
void Province::tick(Date const& today) {
diff --git a/src/openvic/map/Province.hpp b/src/openvic/map/Province.hpp
index cc11046..f1f87a2 100644
--- a/src/openvic/map/Province.hpp
+++ b/src/openvic/map/Province.hpp
@@ -25,6 +25,7 @@ namespace OpenVic {
Good const* rgo = nullptr;
Province(index_t new_index, std::string const& new_identifier, colour_t new_colour);
+
public:
Province(Province&&) = default;
diff --git a/src/openvic/map/Region.cpp b/src/openvic/map/Region.cpp
index d546ff9..b83f556 100644
--- a/src/openvic/map/Region.cpp
+++ b/src/openvic/map/Region.cpp
@@ -16,7 +16,7 @@ std::vector<Province*> const& ProvinceSet::get_provinces() const {
return provinces;
}
-Region::Region(std::string const& new_identifier) : HasIdentifier{ new_identifier } {}
+Region::Region(std::string const& new_identifier) : HasIdentifier { new_identifier } {}
colour_t Region::get_colour() const {
if (provinces.empty()) return FULL_COLOUR << 16;
diff --git a/src/openvic/map/Region.hpp b/src/openvic/map/Region.hpp
index 953a3cc..331d883 100644
--- a/src/openvic/map/Region.hpp
+++ b/src/openvic/map/Region.hpp
@@ -7,6 +7,7 @@ namespace OpenVic {
struct ProvinceSet {
protected:
std::vector<Province*> provinces;
+
public:
size_t get_province_count() const;
bool contains_province(Province const* province) const;
@@ -18,8 +19,10 @@ namespace OpenVic {
*/
struct Region : HasIdentifier, ProvinceSet {
friend struct Map;
+
private:
Region(std::string const& new_identifier);
+
public:
Region(Region&&) = default;