diff options
author | Hop311 <hop3114@gmail.com> | 2023-04-25 22:35:59 +0200 |
---|---|---|
committer | Hop311 <hop3114@gmail.com> | 2023-04-25 22:35:59 +0200 |
commit | 50327abf33078c44fef85c62ce3d90e23056fb34 (patch) | |
tree | f39e8fe64c024ecfde01d2bc4faacb7cf0dddc2c /extension/src/openvic2/map | |
parent | 8fba1c8a02f8680e0d80279b8b6451fea4a40a62 (diff) |
Further cleanup + reset on return to main menu
Diffstat (limited to 'extension/src/openvic2/map')
-rw-r--r-- | extension/src/openvic2/map/Building.cpp | 15 | ||||
-rw-r--r-- | extension/src/openvic2/map/Building.hpp | 21 | ||||
-rw-r--r-- | extension/src/openvic2/map/Map.cpp | 22 | ||||
-rw-r--r-- | extension/src/openvic2/map/Map.hpp | 15 | ||||
-rw-r--r-- | extension/src/openvic2/map/Province.cpp | 16 | ||||
-rw-r--r-- | extension/src/openvic2/map/Province.hpp | 12 | ||||
-rw-r--r-- | extension/src/openvic2/map/Region.hpp | 2 |
7 files changed, 75 insertions, 28 deletions
diff --git a/extension/src/openvic2/map/Building.cpp b/extension/src/openvic2/map/Building.cpp index 3643b4e..00e121b 100644 --- a/extension/src/openvic2/map/Building.cpp +++ b/extension/src/openvic2/map/Building.cpp @@ -3,6 +3,7 @@ #include <cassert> #include "openvic2/Logger.hpp" +#include "openvic2/map/Province.hpp" using namespace OpenVic2; @@ -87,9 +88,13 @@ Timespan BuildingType::get_build_time() const { return build_time; } -const char BuildingManager::building_types_name[] = "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()) { + Logger::error("Invalid building type identifier - empty!"); + return FAILURE; + } if (max_level < 0) { Logger::error("Invalid building type max level: ", max_level); return FAILURE; @@ -109,7 +114,11 @@ BuildingType const* BuildingManager::get_building_type_by_identifier(std::string return building_types.get_item_by_identifier(identifier); } -void BuildingManager::generate_province_buildings(std::vector<Building>& buildings) const { +return_t BuildingManager::generate_province_buildings(Province& province) const { + return_t ret = SUCCESS; + province.reset_buildings(); for (BuildingType const& type : building_types.get_items()) - buildings.push_back(Building{ type }); + if (province.add_building(type) != SUCCESS) ret = FAILURE; + province.lock_buildings(); + return ret; } diff --git a/extension/src/openvic2/map/Building.hpp b/extension/src/openvic2/map/Building.hpp index 08ede3a..1305014 100644 --- a/extension/src/openvic2/map/Building.hpp +++ b/extension/src/openvic2/map/Building.hpp @@ -6,7 +6,7 @@ #include "openvic2/Date.hpp" namespace OpenVic2 { - struct BuildingManager; + struct Province; struct BuildingType; /* REQUIREMENTS: @@ -15,7 +15,7 @@ namespace OpenVic2 { * MAP-13, MAP-78, MAP-79 */ struct Building : HasIdentifier { - friend struct BuildingManager; + friend struct Province; using level_t = int8_t; @@ -31,6 +31,8 @@ namespace OpenVic2 { bool _can_expand() const; public: + Building(Building&&) = default; + BuildingType const& get_type() const; level_t get_level() const; ExpansionState get_expansion_state() const; @@ -43,26 +45,31 @@ namespace OpenVic2 { void tick(Date const& today); }; + struct BuildingManager; + struct BuildingType : HasIdentifier { friend struct BuildingManager; private: - Building::level_t max_level; - Timespan build_time; + 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; + Building::level_t get_max_level() const; Timespan get_build_time() const; }; struct BuildingManager { private: - static const char building_types_name[]; - IdentifierRegistry<BuildingType, building_types_name> building_types; + IdentifierRegistry<BuildingType> building_types; public: + BuildingManager(); + return_t add_building_type(std::string const& identifier, Building::level_t max_level, Timespan build_time); void lock_building_types(); BuildingType const* get_building_type_by_identifier(std::string const& identifier) const; - void generate_province_buildings(std::vector<Building>& buildings) const; + return_t generate_province_buildings(Province& province) const; }; } diff --git a/extension/src/openvic2/map/Map.cpp b/extension/src/openvic2/map/Map.cpp index a440f67..b5cf144 100644 --- a/extension/src/openvic2/map/Map.cpp +++ b/extension/src/openvic2/map/Map.cpp @@ -20,13 +20,17 @@ Province::colour_t Mapmode::get_colour(Map const& map, Province const& province) return colour_func ? colour_func(map, province) : Province::NULL_COLOUR; } -const char Map::provinces_name[] = "provinces", Map::regions_name[] = "regions", Map::mapmodes_name[] = "mapmodes"; +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 FAILURE; } + if (identifier.empty()) { + Logger::error("Invalid province identifier - empty!"); + return FAILURE; + } if (colour == Province::NULL_COLOUR || colour > Province::MAX_COLOUR) { Logger::error("Invalid province colour: ", Province::colour_to_hex_string(colour)); return FAILURE; @@ -69,8 +73,12 @@ void Map::lock_water_provinces() { } return_t Map::add_region(std::string const& identifier, std::vector<std::string> const& province_identifiers) { - return_t ret = SUCCESS; + if (identifier.empty()) { + Logger::error("Invalid region identifier - empty!"); + return FAILURE; + } Region new_region{ identifier }; + return_t ret = SUCCESS; for (std::string const& province_identifier : province_identifiers) { Province* province = get_province_by_identifier(province_identifier); if (province != nullptr) { @@ -246,6 +254,10 @@ std::vector<Province::index_t> const& Map::get_province_index_image() const { } return_t Map::add_mapmode(std::string const& identifier, Mapmode::colour_func_t colour_func) { + if (identifier.empty()) { + Logger::error("Invalid mapmode identifier - empty!"); + return FAILURE; + } if (colour_func == nullptr) { Logger::error("Mapmode colour function is null for identifier: ", identifier); return FAILURE; @@ -290,9 +302,11 @@ return_t Map::generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) return SUCCESS; } -void Map::generate_province_buildings(BuildingManager const& manager) { +return_t Map::generate_province_buildings(BuildingManager const& manager) { + return_t ret = SUCCESS; for (Province& province : provinces.get_items()) - manager.generate_province_buildings(province.buildings.get_items()); + if (manager.generate_province_buildings(province) != SUCCESS) ret = FAILURE; + return ret; } void Map::update_state(Date const& today) { diff --git a/extension/src/openvic2/map/Map.hpp b/extension/src/openvic2/map/Map.hpp index ed63912..ebc23be 100644 --- a/extension/src/openvic2/map/Map.hpp +++ b/extension/src/openvic2/map/Map.hpp @@ -12,8 +12,8 @@ namespace OpenVic2 { using colour_func_t = std::function<Province::colour_t (Map const&, Province const&)>; using index_t = size_t; private: - index_t index; - colour_func_t colour_func; + 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: @@ -26,16 +26,17 @@ namespace OpenVic2 { */ struct Map { private: - static const char provinces_name[], regions_name[], mapmodes_name[]; - IdentifierRegistry<Province, provinces_name> provinces; - IdentifierRegistry<Region, regions_name> regions; - IdentifierRegistry<Mapmode, mapmodes_name> mapmodes; + IdentifierRegistry<Province> provinces; + IdentifierRegistry<Region> regions; + IdentifierRegistry<Mapmode> mapmodes; bool water_provinces_locked = false; size_t water_province_count = 0; size_t width = 0, height = 0; std::vector<Province::index_t> province_index_image; public: + Map(); + return_t add_province(std::string const& identifier, Province::colour_t colour); void lock_provinces(); return_t set_water_province(std::string const& identifier); @@ -67,7 +68,7 @@ namespace OpenVic2 { Mapmode const* get_mapmode_by_identifier(std::string const& identifier) const; return_t generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) const; - void generate_province_buildings(BuildingManager const& manager); + return_t generate_province_buildings(BuildingManager const& manager); void update_state(Date const& today); void tick(Date const& today); diff --git a/extension/src/openvic2/map/Province.cpp b/extension/src/openvic2/map/Province.cpp index 08711af..4360bce 100644 --- a/extension/src/openvic2/map/Province.cpp +++ b/extension/src/openvic2/map/Province.cpp @@ -6,10 +6,8 @@ using namespace OpenVic2; -const char Province::buildings_name[] = "buildings"; - Province::Province(index_t new_index, std::string const& new_identifier, colour_t new_colour) : - HasIdentifier{ new_identifier }, index{ new_index }, colour{ new_colour } { + HasIdentifier{ new_identifier }, index{ new_index }, colour{ new_colour }, buildings{ "buildings" } { assert(index != NULL_INDEX); assert(colour != NULL_COLOUR); } @@ -40,6 +38,18 @@ Province::life_rating_t Province::get_life_rating() const { return life_rating; } +return_t Province::add_building(BuildingType const& type) { + return buildings.add_item({ type }); +} + +void Province::lock_buildings() { + buildings.lock(false); +} + +void Province::reset_buildings() { + buildings.reset(); +} + std::vector<Building> const& Province::get_buildings() const { return buildings.get_items(); } diff --git a/extension/src/openvic2/map/Province.hpp b/extension/src/openvic2/map/Province.hpp index 0b7cd4c..aa0329c 100644 --- a/extension/src/openvic2/map/Province.hpp +++ b/extension/src/openvic2/map/Province.hpp @@ -19,23 +19,27 @@ namespace OpenVic2 { static constexpr colour_t NULL_COLOUR = 0, MAX_COLOUR = 0xFFFFFF; static constexpr index_t NULL_INDEX = 0, MAX_INDEX = 0xFFFF; private: - index_t index; - colour_t colour; + const index_t index; + const colour_t colour; Region* region = nullptr; bool water = false; life_rating_t life_rating = 0; - static const char buildings_name[]; - IdentifierRegistry<Building, buildings_name> buildings; + IdentifierRegistry<Building> buildings; 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; + return_t add_building(BuildingType const& type); + void lock_buildings(); + void reset_buildings(); std::vector<Building> const& get_buildings() const; return_t expand_building(std::string const& building_type_identifier); std::string to_string() const; diff --git a/extension/src/openvic2/map/Region.hpp b/extension/src/openvic2/map/Region.hpp index 2eec1cd..04564fc 100644 --- a/extension/src/openvic2/map/Region.hpp +++ b/extension/src/openvic2/map/Region.hpp @@ -23,6 +23,8 @@ namespace OpenVic2 { private: Region(std::string const& new_identifier); public: + Region(Region&&) = default; + Province::colour_t get_colour() const; }; } |