From 46bbbb038e5fa21e25fa33c4fee84e4b14690885 Mon Sep 17 00:00:00 2001 From: Hop311 Date: Thu, 14 Sep 2023 20:09:10 +0100 Subject: Meta region loading --- src/openvic-simulation/GameManager.cpp | 2 +- src/openvic-simulation/map/Building.cpp | 2 +- src/openvic-simulation/map/Map.cpp | 65 ++++++++++++++++++--------------- src/openvic-simulation/map/Province.cpp | 8 +++- src/openvic-simulation/map/Province.hpp | 5 ++- src/openvic-simulation/map/Region.cpp | 17 +++++++-- src/openvic-simulation/map/Region.hpp | 19 ++++++++-- 7 files changed, 75 insertions(+), 43 deletions(-) diff --git a/src/openvic-simulation/GameManager.cpp b/src/openvic-simulation/GameManager.cpp index 0b42230..4c8b55f 100644 --- a/src/openvic-simulation/GameManager.cpp +++ b/src/openvic-simulation/GameManager.cpp @@ -63,7 +63,7 @@ bool GameManager::load_hardcoded_defines() { const std::vector mapmodes { { "mapmode_terrain", [](Map const&, Province const& province) -> colour_t { - return LOW_ALPHA_VALUE | (province.is_water() ? 0x4287F5 : 0x0D7017); + return LOW_ALPHA_VALUE | (province.get_water() ? 0x4287F5 : 0x0D7017); } }, { "mapmode_province", [](Map const&, Province const& province) -> colour_t { diff --git a/src/openvic-simulation/map/Building.cpp b/src/openvic-simulation/map/Building.cpp index 6e5cf18..eba2049 100644 --- a/src/openvic-simulation/map/Building.cpp +++ b/src/openvic-simulation/map/Building.cpp @@ -117,7 +117,7 @@ bool BuildingManager::generate_province_buildings(Province& province) const { return false; } bool ret = true; - if (!province.is_water()) { + if (!province.get_water()) { for (BuildingType const& type : building_types.get_items()) { ret &= province.add_building({ type }); } diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp index 74764d2..fa11f86 100644 --- a/src/openvic-simulation/map/Map.cpp +++ b/src/openvic-simulation/map/Map.cpp @@ -68,7 +68,7 @@ bool Map::set_water_province(const std::string_view identifier) { Logger::error("Unrecognised water province identifier: ", identifier); return false; } - if (province->is_water()) { + if (province->get_water()) { Logger::warning("Province ", identifier, " is already a water province!"); return true; } @@ -99,53 +99,60 @@ bool Map::add_region(const std::string_view identifier, std::vector(province->get_region()); - if (other_region_index != 0) { - other_region_index--; - if (other_region_index < regions.size()) - Logger::error("Cannot add province ", province_identifier, " to region ", identifier, " - it is already part of ", regions.get_item_by_index(other_region_index)->get_identifier()); - else - Logger::error("Cannot add province ", province_identifier, " to region ", identifier, " - it is already part of an unknown region with index ", other_region_index); - ret = false; - } else if (!new_region.add_province(province)) { - Logger::error("Failed to add province ", province_identifier, " to region ", identifier); - ret = false; + if (province->get_has_region()) { + meta = true; } + provinces.push_back(province); } } else { Logger::error("Invalid province identifier ", province_identifier, " for region ", identifier); ret = false; } } - new_region.lock(); - if (new_region.empty()) { - Logger::error("No valid provinces in list for ", identifier); - return false; + if (provinces.empty()) { + Logger::warning("No valid provinces in list for ", identifier); + return ret; } - // Used to detect provinces listed in multiple regions, will - // be corrected once regions is stable (i.e. lock_regions). - Region* tmp_region_index = reinterpret_cast(regions.size()); - for (Province* province : new_region.get_provinces()) - province->region = tmp_region_index; - ret &= regions.add_item(std::move(new_region)); + if (!meta) { + for (Province* province : provinces) { + province->has_region = true; + } + } + ret &= regions.add_item({ identifier, std::move(provinces), meta }); return ret; } void Map::lock_regions() { regions.lock(); - for (Region& region : regions.get_items()) - for (Province* province : region.get_provinces()) - province->region = ®ion; + for (Region& region : regions.get_items()) { + if (!region.meta) { + for (Province* province : region.get_provinces()) { + if (!province->get_has_region()) { + Logger::error("Province in non-meta region without has_region set: ", province->get_identifier()); + province->has_region = true; + } + province->region = ®ion; + } + } + } + for (Province& province : provinces.get_items()) { + const bool region_null = province.get_region() == nullptr; + if (province.get_has_region() == region_null) { + Logger::error("Province has_region / region mismatch: has_region = ", province.get_has_region(), ", region = ", province.get_region()); + province.has_region = !region_null; + } + } } size_t Map::get_province_count() const { @@ -421,7 +428,7 @@ bool Map::setup(GoodManager const& good_manager, BuildingManager const& building for (Province& province : provinces.get_items()) { province.clear_pops(); // 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) + if (!province.get_water() && !good_manager.get_good_count() > 0) province.rgo = good_manager.get_good_by_index(province.get_index() % good_manager.get_good_count()); ret &= building_manager.generate_province_buildings(province); } diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp index f53de3a..b4a80b5 100644 --- a/src/openvic-simulation/map/Province.cpp +++ b/src/openvic-simulation/map/Province.cpp @@ -22,7 +22,11 @@ Region* Province::get_region() const { return region; } -bool Province::is_water() const { +bool Province::get_has_region() const { + return has_region; +} + +bool Province::get_water() const { return water; } @@ -64,7 +68,7 @@ bool Province::load_pop_list(PopManager const& pop_manager, ast::NodeCPtr root) } bool Province::add_pop(Pop&& pop) { - if (!is_water()) { + if (!get_water()) { pops.push_back(std::move(pop)); return true; } else { diff --git a/src/openvic-simulation/map/Province.hpp b/src/openvic-simulation/map/Province.hpp index 67816ff..ca8138f 100644 --- a/src/openvic-simulation/map/Province.hpp +++ b/src/openvic-simulation/map/Province.hpp @@ -23,7 +23,7 @@ namespace OpenVic { private: const index_t index; Region* region = nullptr; - bool water = false; + bool has_region = false, water = false; life_rating_t life_rating = 0; IdentifierRegistry buildings; // TODO - change this into a factory-like structure @@ -40,7 +40,8 @@ namespace OpenVic { index_t get_index() const; Region* get_region() const; - bool is_water() const; + bool get_has_region() const; + bool get_water() const; life_rating_t get_life_rating() const; bool add_building(Building&& building); IDENTIFIER_REGISTRY_ACCESSORS(Building, building) diff --git a/src/openvic-simulation/map/Region.cpp b/src/openvic-simulation/map/Region.cpp index 33092c5..c0422de 100644 --- a/src/openvic-simulation/map/Region.cpp +++ b/src/openvic-simulation/map/Region.cpp @@ -2,6 +2,8 @@ using namespace OpenVic; +ProvinceSet::ProvinceSet(provinces_t&& new_provinces) : provinces { std::move(new_provinces) } {} + bool ProvinceSet::add_province(Province* province) { if (locked) { Logger::error("Cannot add province to province set - locked!"); @@ -57,13 +59,20 @@ bool ProvinceSet::contains_province(Province const* province) const { return province && std::find(provinces.begin(), provinces.end(), province) != provinces.end(); } -std::vector const& ProvinceSet::get_provinces() const { +ProvinceSet::provinces_t const& ProvinceSet::get_provinces() const { return provinces; } -Region::Region(const std::string_view new_identifier) : HasIdentifier { new_identifier } {} +Region::Region(const std::string_view new_identifier, provinces_t&& new_provinces, bool new_meta) + : HasIdentifier { new_identifier }, ProvinceSet { std::move(new_provinces) }, meta { new_meta } { + lock(); +} + +bool Region::get_meta() const { + return meta; +} colour_t Region::get_colour() const { - if (provinces.empty()) return FULL_COLOUR << 16; - return provinces.front()->get_colour(); + if (empty()) return FULL_COLOUR << 16; + return get_provinces().front()->get_colour(); } diff --git a/src/openvic-simulation/map/Region.hpp b/src/openvic-simulation/map/Region.hpp index 2fccf06..d68033b 100644 --- a/src/openvic-simulation/map/Region.hpp +++ b/src/openvic-simulation/map/Region.hpp @@ -5,11 +5,15 @@ namespace OpenVic { struct ProvinceSet { - protected: - std::vector provinces; + using provinces_t = std::vector; + + private: + provinces_t provinces; bool locked = false; public: + ProvinceSet(provinces_t&& new_provinces = {}); + bool add_province(Province* province); void lock(bool log = false); bool is_locked() const; @@ -18,7 +22,7 @@ namespace OpenVic { size_t size() const; void reserve(size_t size); bool contains_province(Province const* province) const; - std::vector const& get_provinces() const; + provinces_t const& get_provinces() const; }; /* REQUIREMENTS: @@ -28,11 +32,18 @@ namespace OpenVic { friend struct Map; private: - Region(const std::string_view new_identifier); + /* A meta region cannot be the template for a state. + * Any region containing a province already listed in a + * previously defined region is considered a meta region. + */ + const bool meta; + + Region(const std::string_view new_identifier, provinces_t&& new_provinces, bool new_meta); public: Region(Region&&) = default; + bool get_meta() const; colour_t get_colour() const; }; } -- cgit v1.2.3-56-ga3b1