diff options
author | Hop311 <Hop3114@gmail.com> | 2023-07-28 00:10:46 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-28 00:10:46 +0200 |
commit | 8a08be3e7e8477973e243716d431ad7117acfa43 (patch) | |
tree | a91587787bf6742168279ac41f77b9ebabe0dffe /src/openvic/map | |
parent | 420c2dce47e74c01ff46be991058d543e0c70a6b (diff) | |
parent | 532c9be36ca03ee13c92ca7d895aaf5b42eeb034 (diff) |
Merge pull request #9 from OpenVicProject/pops
Pops and other bits
Diffstat (limited to 'src/openvic/map')
-rw-r--r-- | src/openvic/map/Building.cpp | 6 | ||||
-rw-r--r-- | src/openvic/map/Building.hpp | 9 | ||||
-rw-r--r-- | src/openvic/map/Map.cpp | 40 | ||||
-rw-r--r-- | src/openvic/map/Map.hpp | 9 | ||||
-rw-r--r-- | src/openvic/map/Province.cpp | 25 | ||||
-rw-r--r-- | src/openvic/map/Province.hpp | 10 |
6 files changed, 80 insertions, 19 deletions
diff --git a/src/openvic/map/Building.cpp b/src/openvic/map/Building.cpp index 317ccdf..81532c4 100644 --- a/src/openvic/map/Building.cpp +++ b/src/openvic/map/Building.cpp @@ -100,11 +100,11 @@ return_t BuildingManager::add_building_type(std::string const& identifier, Build return FAILURE; } if (max_level < 0) { - Logger::error("Invalid building type max level: ", max_level); + Logger::error("Invalid building type max level for ", identifier, ": ", max_level); return FAILURE; } if (build_time < 0) { - Logger::error("Invalid building type build time: ", build_time); + Logger::error("Invalid building type build time for ", identifier, ": ", build_time); return FAILURE; } return building_types.add_item({ identifier, max_level, build_time }); @@ -127,7 +127,7 @@ return_t BuildingManager::generate_province_buildings(Province& province) const if (province.is_water()) return SUCCESS; return_t ret = SUCCESS; for (BuildingType const& type : building_types.get_items()) - if (province.add_building(type) != SUCCESS) ret = FAILURE; + if (province.add_building({ type }) != SUCCESS) ret = FAILURE; province.lock_buildings(); return ret; } diff --git a/src/openvic/map/Building.hpp b/src/openvic/map/Building.hpp index 98c3991..c4f8950 100644 --- a/src/openvic/map/Building.hpp +++ b/src/openvic/map/Building.hpp @@ -6,7 +6,8 @@ #include "../Types.hpp" namespace OpenVic { - struct Province; + + struct BuildingManager; struct BuildingType; /* REQUIREMENTS: @@ -15,7 +16,7 @@ namespace OpenVic { * MAP-13, MAP-78, MAP-79 */ struct Building : HasIdentifier { - friend struct Province; + friend struct BuildingManager; using level_t = int8_t; @@ -52,8 +53,6 @@ namespace OpenVic { void tick(Date const& today); }; - struct BuildingManager; - struct BuildingType : HasIdentifier { friend struct BuildingManager; @@ -70,6 +69,8 @@ namespace OpenVic { Timespan get_build_time() const; }; + struct Province; + struct BuildingManager { private: IdentifierRegistry<BuildingType> building_types; diff --git a/src/openvic/map/Map.cpp b/src/openvic/map/Map.cpp index 2efee32..0f4ed46 100644 --- a/src/openvic/map/Map.cpp +++ b/src/openvic/map/Map.cpp @@ -39,7 +39,7 @@ return_t Map::add_province(std::string const& identifier, colour_t colour) { return FAILURE; } if (colour == NULL_COLOUR || colour > MAX_COLOUR_RGB) { - Logger::error("Invalid province colour: ", Province::colour_to_hex_string(colour)); + Logger::error("Invalid province colour for ", identifier, ": ", Province::colour_to_hex_string(colour)); return FAILURE; } Province new_province { static_cast<index_t>(provinces.get_item_count() + 1), identifier, colour }; @@ -91,26 +91,26 @@ return_t Map::add_region(std::string const& identifier, std::vector<std::string> Province* province = get_province_by_identifier(province_identifier); if (province != nullptr) { if (new_region.contains_province(province)) { - Logger::error("Duplicate province identifier ", province_identifier); + Logger::error("Duplicate province identifier ", province_identifier, " in region ", identifier); ret = FAILURE; } else { size_t other_region_index = reinterpret_cast<size_t>(province->get_region()); if (other_region_index != 0) { other_region_index--; if (other_region_index < regions.get_item_count()) - Logger::error("Province ", province_identifier, " is already part of ", regions.get_item_by_index(other_region_index)->get_identifier()); + 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("Province ", province_identifier, " is already part of an unknown region with index ", other_region_index); + Logger::error("Cannot add province ", province_identifier, " to region ", identifier, " - it is already part of an unknown region with index ", other_region_index); ret = FAILURE; } else new_region.provinces.push_back(province); } } else { - Logger::error("Invalid province identifier ", province_identifier); + Logger::error("Invalid province identifier ", province_identifier, " for region ", identifier); ret = FAILURE; } } if (!new_region.get_province_count()) { - Logger::error("No valid provinces in region's list"); + Logger::error("No valid provinces in list for ", identifier); return FAILURE; } @@ -341,13 +341,37 @@ return_t Map::generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) return ret; } -return_t Map::setup(GoodManager const& good_manager, BuildingManager const& building_manager) { +void Map::update_highest_province_population() { + highest_province_population = 0; + for (Province const& province : provinces.get_items()) { + highest_province_population = std::max(highest_province_population, province.get_total_population()); + } +} + +Pop::pop_size_t Map::get_highest_province_population() const { + return highest_province_population; +} + +void Map::update_total_map_population() { + total_map_population = 0; + for (Province const& province : provinces.get_items()) { + total_map_population += province.get_total_population(); + } +} + +Pop::pop_size_t Map::get_total_map_population() const { + return total_map_population; +} + +return_t Map::setup(GoodManager const& good_manager, BuildingManager const& building_manager, PopManager const& pop_manager) { return_t ret = SUCCESS; for (Province& province : provinces.get_items()) { // 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; + // Add some pops to the province (for testing purposes) + pop_manager.generate_test_pops(province); } return ret; } @@ -355,6 +379,8 @@ return_t Map::setup(GoodManager const& good_manager, BuildingManager const& buil void Map::update_state(Date const& today) { for (Province& province : provinces.get_items()) province.update_state(today); + update_highest_province_population(); + update_total_map_population(); } void Map::tick(Date const& today) { diff --git a/src/openvic/map/Map.hpp b/src/openvic/map/Map.hpp index 3533a14..3e1370b 100644 --- a/src/openvic/map/Map.hpp +++ b/src/openvic/map/Map.hpp @@ -54,6 +54,8 @@ namespace OpenVic { colour_index_map_t colour_index_map; index_t selected_province = NULL_INDEX; + Pop::pop_size_t highest_province_population, total_map_population; + index_t get_index_from_colour(colour_t colour) const; public: @@ -93,7 +95,12 @@ namespace OpenVic { static constexpr size_t MAPMODE_COLOUR_SIZE = 4; return_t generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) const; - return_t setup(GoodManager const& good_manager, BuildingManager const& building_manager); + return_t setup(GoodManager const& good_manager, BuildingManager const& building_manager, PopManager const& pop_manager); + + void update_highest_province_population(); + Pop::pop_size_t get_highest_province_population() const; + void update_total_map_population(); + Pop::pop_size_t get_total_map_population() const; void update_state(Date const& today); void tick(Date const& today); diff --git a/src/openvic/map/Province.cpp b/src/openvic/map/Province.cpp index d2a5ecf..75612ad 100644 --- a/src/openvic/map/Province.cpp +++ b/src/openvic/map/Province.cpp @@ -8,7 +8,7 @@ using namespace OpenVic; Province::Province(index_t new_index, std::string const& new_identifier, colour_t new_colour) : HasIdentifier { new_identifier }, - HasColour { new_colour }, + HasColour { new_colour, false }, index { new_index }, buildings { "buildings" } { assert(index != NULL_INDEX); @@ -30,8 +30,8 @@ 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 }); +return_t Province::add_building(Building&& building) { + return buildings.add_item(std::move(building)); } void Province::lock_buildings() { @@ -66,9 +66,28 @@ std::string Province::to_string() const { return stream.str(); } +void Province::add_pop(Pop&& pop) { + pops.push_back(std::move(pop)); +} + +/* REQUIREMENTS: + * MAP-65 + */ +void Province::update_total_population() { + total_population = 0; + for (Pop const& pop : pops) { + total_population += pop.get_size(); + } +} + +Pop::pop_size_t Province::get_total_population() const { + return total_population; +} + void Province::update_state(Date const& today) { for (Building& building : buildings.get_items()) building.update_state(today); + update_total_population(); } void Province::tick(Date const& today) { diff --git a/src/openvic/map/Province.hpp b/src/openvic/map/Province.hpp index f1f87a2..cd90f7d 100644 --- a/src/openvic/map/Province.hpp +++ b/src/openvic/map/Province.hpp @@ -1,5 +1,6 @@ #pragma once +#include "../pop/Pop.hpp" #include "Building.hpp" namespace OpenVic { @@ -24,6 +25,9 @@ namespace OpenVic { // TODO - change this into a factory-like structure Good const* rgo = nullptr; + std::vector<Pop> pops; + Pop::pop_size_t total_population; + Province(index_t new_index, std::string const& new_identifier, colour_t new_colour); public: @@ -33,7 +37,7 @@ namespace OpenVic { Region* get_region() const; bool is_water() const; life_rating_t get_life_rating() const; - return_t add_building(BuildingType const& type); + return_t add_building(Building&& building); void lock_buildings(); void reset_buildings(); Building const* get_building_by_identifier(std::string const& identifier) const; @@ -42,6 +46,10 @@ namespace OpenVic { Good const* get_rgo() const; std::string to_string() const; + void add_pop(Pop&& pop); + void update_total_population(); + Pop::pop_size_t get_total_population() const; + void update_state(Date const& today); void tick(Date const& today); }; |