diff options
Diffstat (limited to 'src/openvic-simulation/map')
-rw-r--r-- | src/openvic-simulation/map/Map.cpp | 23 | ||||
-rw-r--r-- | src/openvic-simulation/map/Map.hpp | 4 | ||||
-rw-r--r-- | src/openvic-simulation/map/Province.cpp | 201 | ||||
-rw-r--r-- | src/openvic-simulation/map/Province.hpp | 79 |
4 files changed, 131 insertions, 176 deletions
diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp index 261a13e..dad07a6 100644 --- a/src/openvic-simulation/map/Map.cpp +++ b/src/openvic-simulation/map/Map.cpp @@ -4,6 +4,7 @@ #include <unordered_set> #include "openvic-simulation/economy/Good.hpp" +#include "openvic-simulation/history/ProvinceHistory.hpp" #include "openvic-simulation/utility/BMP.hpp" #include "openvic-simulation/utility/Logger.hpp" @@ -297,11 +298,25 @@ Pop::pop_size_t Map::get_total_map_population() const { return total_map_population; } -bool Map::setup(BuildingManager const& building_manager, PopManager const& pop_manager) { +bool Map::reset(BuildingManager const& building_manager) { bool ret = true; for (Province& province : provinces.get_items()) { - province.clear_pops(); - ret &= building_manager.generate_province_buildings(province); + ret &= province.reset(building_manager); + } + return ret; +} + +bool Map::apply_history_to_provinces(ProvinceHistoryManager const& history_manager, Date date) { + bool ret = true; + for (Province& province : provinces.get_items()) { + if (!province.get_water()) { + ProvinceHistoryMap const* history_map = history_manager.get_province_history(&province); + if (history_map != nullptr) { + for (ProvinceHistoryEntry const* entry : history_map->get_entries_up_to(date)) { + province.apply_history_to_province(entry); + } + } + } } return ret; } @@ -562,7 +577,7 @@ bool Map::load_map_images(fs::path const& province_path, fs::path const& terrain for (size_t idx = 0; idx < province_checklist.size(); ++idx) { Province* province = provinces.get_item_by_index(idx); const fixed_point_map_const_iterator_t<TerrainType const*> largest = get_largest_item(terrain_type_pixels_list[idx]); - province->_set_terrain_type(largest != terrain_type_pixels_list[idx].end() ? largest->first : nullptr); + province->default_terrain_type = largest != terrain_type_pixels_list[idx].end() ? largest->first : nullptr; province->on_map = province_checklist[idx]; if (!province->on_map) { if (detailed_errors) { diff --git a/src/openvic-simulation/map/Map.hpp b/src/openvic-simulation/map/Map.hpp index db67390..189713c 100644 --- a/src/openvic-simulation/map/Map.hpp +++ b/src/openvic-simulation/map/Map.hpp @@ -36,6 +36,7 @@ namespace OpenVic { }; struct GoodManager; + struct ProvinceHistoryManager; /* REQUIREMENTS: * MAP-4 @@ -109,7 +110,8 @@ namespace OpenVic { * that is the maximum allowed number of provinces plus one for the index-zero "null province". */ bool generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) const; - bool setup(BuildingManager const& building_manager, PopManager const& pop_manager); + bool reset(BuildingManager const& building_manager); + bool apply_history_to_provinces(ProvinceHistoryManager const& history_manager, Date date); void update_highest_province_population(); Pop::pop_size_t get_highest_province_population() const; diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp index 1b47dea..97e5192 100644 --- a/src/openvic-simulation/map/Province.cpp +++ b/src/openvic-simulation/map/Province.cpp @@ -8,56 +8,16 @@ using namespace OpenVic::NodeTools; Province::Province( std::string_view new_identifier, colour_t new_colour, index_t new_index ) : HasIdentifierAndColour { new_identifier, new_colour, true, false }, index { new_index }, - buildings { "buildings", false } { + region { nullptr }, on_map { false }, has_region { false }, water { false }, default_terrain_type { nullptr }, + terrain_type { nullptr }, life_rating { 0 }, colony_status { colony_status_t::STATE }, owner { nullptr }, + controller { nullptr }, slave { false }, buildings { "buildings", false }, rgo { nullptr }, total_population { 0 } { assert(index != NULL_INDEX); } -Province::index_t Province::get_index() const { - return index; -} - -Region const* Province::get_region() const { - return region; -} - -bool Province::get_on_map() const { - return on_map; -} - -bool Province::get_has_region() const { - return has_region; -} - -bool Province::get_water() const { - return water; -} - -TerrainType const* Province::get_terrain_type() const { - return terrain_type; -} - -Province::life_rating_t Province::get_life_rating() const { - return life_rating; -} - -Province::colony_status_t Province::get_colony_status() const { - return colony_status; -} - -Country const* Province::get_owner() const { - return owner; -} - -Country const* Province::get_controller() const { - return controller; -} - -std::vector<Country const*> const& Province::get_cores() const { - return cores; -} - -bool Province::is_slave() const { - return slave; +std::string Province::to_string() const { + std::stringstream stream; + stream << "(#" << std::to_string(index) << ", " << get_identifier() << ", 0x" << colour_to_hex_string() << ")"; + return stream.str(); } bool Province::load_positions(BuildingManager const& building_manager, ast::NodeCPtr root) { @@ -89,14 +49,6 @@ bool Province::load_positions(BuildingManager const& building_manager, ast::Node )(root); } -bool Province::add_building(BuildingInstance&& building_instance) { - return buildings.add_item(std::move(building_instance)); -} - -void Province::reset_buildings() { - buildings.reset(); -} - bool Province::expand_building(std::string_view building_type_identifier) { BuildingInstance* building = buildings.get_item_by_identifier(building_type_identifier); if (building == nullptr) { @@ -105,16 +57,6 @@ bool Province::expand_building(std::string_view building_type_identifier) { return building->expand(); } -Good const* Province::get_rgo() const { - return rgo; -} - -std::string Province::to_string() const { - std::stringstream stream; - stream << "(#" << std::to_string(index) << ", " << get_identifier() << ", 0x" << colour_to_hex_string() << ")"; - return stream.str(); -} - bool Province::load_pop_list(PopManager const& pop_manager, ast::NodeCPtr root) { return expect_dictionary_reserve_length(pops, [this, &pop_manager](std::string_view pop_type_identifier, ast::NodeCPtr pop_node) -> bool { @@ -133,22 +75,10 @@ bool Province::add_pop(Pop&& pop) { } } -void Province::clear_pops() { - pops.clear(); -} - size_t Province::get_pop_count() const { return pops.size(); } -std::vector<Pop> const& Province::get_pops() const { - return pops; -} - -Pop::pop_size_t Province::get_total_population() const { - return total_population; -} - /* REQUIREMENTS: * MAP-65, MAP-68, MAP-70, MAP-234 */ @@ -185,14 +115,6 @@ Province::adjacency_t::adjacency_t(Province const* province, distance_t distance assert(province != nullptr); } -Province::distance_t Province::adjacency_t::get_distance() const { - return distance; -} - -Province::flags_t Province::adjacency_t::get_flags() const { - return flags; -} - bool Province::is_adjacent_to(Province const* province) { for (adjacency_t adj : adjacencies) { if (adj.province == province) { @@ -207,7 +129,6 @@ bool Province::add_adjacency(Province const* province, distance_t distance, flag Logger::error("Tried to create null adjacency province for province ", get_identifier(), "!"); return false; } - if (is_adjacent_to(province)) { return false; } @@ -215,46 +136,84 @@ bool Province::add_adjacency(Province const* province, distance_t distance, flag return true; } -std::vector<Province::adjacency_t> const& Province::get_adjacencies() const { - return adjacencies; -} +bool Province::reset(BuildingManager const& building_manager) { + terrain_type = default_terrain_type; + life_rating = 0; + colony_status = colony_status_t::STATE; + owner = nullptr; + controller = nullptr; + cores.clear(); + slave = false; + rgo = nullptr; -void Province::_set_terrain_type(TerrainType const* type) { - terrain_type = type; -} + buildings.reset(); + bool ret = true; + if (!get_water()) { + if (building_manager.building_types_are_locked() && building_manager.buildings_are_locked()) { + for (Building const& building : building_manager.get_buildings()) { + if (building.get_in_province()) { + ret &= buildings.add_item({ building }); + } + } + } else { + Logger::error("Cannot generate buildings until building types are locked!"); + ret = false; + } + } + lock_buildings(); + + pops.clear(); + update_pops(); -void Province::apply_history_to_province(ProvinceHistoryMap const& history, Date date) { - auto entries = history.get_entries(date); - - reset_buildings(); + return ret; +} - for (ProvinceHistoryEntry const* entry : entries) { - if (entry->get_life_rating()) life_rating = *entry->get_life_rating(); - if (entry->get_colonial()) colony_status = *entry->get_colonial(); - if (entry->get_rgo()) rgo = *entry->get_rgo(); - if (entry->get_terrain_type()) terrain_type = *entry->get_terrain_type(); - if (entry->get_owner()) owner = *entry->get_owner(); - if (entry->get_controller()) controller = *entry->get_controller(); - if (entry->get_slave()) slave = *entry->get_slave(); - for (const auto& core : entry->get_remove_cores()) { - const auto existing_core = std::find(cores.begin(), cores.end(), core); - if (existing_core != cores.end()) cores.erase(existing_core); +bool Province::apply_history_to_province(ProvinceHistoryEntry const* entry) { + if (entry == nullptr) { + Logger::error("Trying to apply null province history to ", get_identifier()); + return false; + } + if (entry->get_life_rating()) life_rating = *entry->get_life_rating(); + if (entry->get_colonial()) colony_status = *entry->get_colonial(); + if (entry->get_rgo()) rgo = *entry->get_rgo(); + if (entry->get_terrain_type()) terrain_type = *entry->get_terrain_type(); + if (entry->get_owner()) owner = *entry->get_owner(); + if (entry->get_controller()) controller = *entry->get_controller(); + if (entry->get_slave()) slave = *entry->get_slave(); + for (Country const* core : entry->get_remove_cores()) { + const typename decltype(cores)::iterator existing_core = std::find(cores.begin(), cores.end(), core); + if (existing_core != cores.end()) { + cores.erase(existing_core); + } else { + Logger::warning( + "Trying to remove non-existent core ", core->get_identifier(), " from province ", get_identifier() + ); } - for (const auto& core : entry->get_add_cores()) { - const auto existing_core = std::find(cores.begin(), cores.end(), core); - if (existing_core == cores.end()) cores.push_back(core); + } + for (Country const* core : entry->get_add_cores()) { + const typename decltype(cores)::iterator existing_core = std::find(cores.begin(), cores.end(), core); + if (existing_core == cores.end()) { + cores.push_back(core); + } else { + Logger::warning( + "Trying to add already-existing core ", core->get_identifier(), " to province ", get_identifier() + ); } - // TODO: rework province buildings - for (const auto& building : entry->get_buildings()) { - BuildingInstance* existing_entry = buildings.get_item_by_identifier(building.first->get_identifier()); - if (existing_entry != nullptr) { - existing_entry->set_level(building.second); - } else { - BuildingInstance instance = { *building.first }; - instance.set_level(building.second); - add_building(std::move(instance)); - } + } + bool ret = true; + for (auto const& [building, level] : entry->get_province_buildings()) { + BuildingInstance* existing_entry = buildings.get_item_by_identifier(building->get_identifier()); + if (existing_entry != nullptr) { + existing_entry->set_level(level); + } else { + Logger::error( + "Trying to set level of non-existent province building ", building->get_identifier(), " to ", level, + " in province ", get_identifier() + ); + ret = false; } - // TODO: party loyalties for each POP when implemented on POP side } + // TODO: load state buildings + // TODO: party loyalties for each POP when implemented on POP side# + return ret; } diff --git a/src/openvic-simulation/map/Province.hpp b/src/openvic-simulation/map/Province.hpp index 21a974f..0b7a5d9 100644 --- a/src/openvic-simulation/map/Province.hpp +++ b/src/openvic-simulation/map/Province.hpp @@ -13,7 +13,7 @@ namespace OpenVic { struct Good; struct TerrainType; struct TerrainTypeMapping; - struct ProvinceHistoryMap; + struct ProvinceHistoryEntry; /* REQUIREMENTS: * MAP-5, MAP-7, MAP-8, MAP-43, MAP-47 @@ -34,14 +34,10 @@ namespace OpenVic { private: Province const* const province; - const distance_t distance; - flags_t flags; + const distance_t PROPERTY(distance); + flags_t PROPERTY(flags); adjacency_t(Province const* province, distance_t distance, flags_t flags); - - public: - distance_t get_distance() const; - flags_t get_flags() const; }; struct province_positions_t { @@ -64,62 +60,50 @@ namespace OpenVic { static constexpr index_t NULL_INDEX = 0, MAX_INDEX = std::numeric_limits<index_t>::max(); private: - const index_t index; - Region const* region = nullptr; - bool on_map = false, has_region = false, water = false; - life_rating_t life_rating = 0; - colony_status_t colony_status = colony_status_t::STATE; - IdentifierRegistry<BuildingInstance> buildings; + const index_t PROPERTY(index); + Region* PROPERTY(region); + bool PROPERTY(on_map); + bool PROPERTY(has_region); + bool PROPERTY(water); + /* Terrain type calculated from terrain image */ + TerrainType const* PROPERTY(default_terrain_type); + + std::vector<adjacency_t> PROPERTY(adjacencies); + province_positions_t positions; + + TerrainType const* PROPERTY(terrain_type); + life_rating_t PROPERTY(life_rating); + colony_status_t PROPERTY(colony_status); + Country const* PROPERTY(owner); + Country const* PROPERTY(controller); + std::vector<Country const*> PROPERTY(cores); + bool PROPERTY(slave); // TODO - change this into a factory-like structure - Good const* rgo = nullptr; + Good const* PROPERTY(rgo); + IdentifierRegistry<BuildingInstance> buildings; - std::vector<Pop> pops; - Pop::pop_size_t total_population; + std::vector<Pop> PROPERTY(pops); + Pop::pop_size_t PROPERTY(total_population); fixed_point_map_t<PopType const*> PROPERTY(pop_type_distribution); fixed_point_map_t<Ideology const*> PROPERTY(ideology_distribution); fixed_point_map_t<Culture const*> PROPERTY(culture_distribution); fixed_point_map_t<Religion const*> PROPERTY(religion_distribution); - std::vector<adjacency_t> adjacencies; - province_positions_t positions; - - TerrainType const* terrain_type = nullptr; - - void _set_terrain_type(TerrainType const* type); - - Country const* owner = nullptr; - Country const* controller = nullptr; - std::vector<Country const*> cores; - bool slave = false; - Province(std::string_view new_identifier, colour_t new_colour, index_t new_index); public: Province(Province&&) = default; - index_t get_index() const; - Region const* get_region() const; - bool get_on_map() const; - bool get_has_region() const; - bool get_water() const; - TerrainType const* get_terrain_type() const; - life_rating_t get_life_rating() const; - colony_status_t get_colony_status() const; + std::string to_string() const; + bool load_positions(BuildingManager const& building_manager, ast::NodeCPtr root); - bool add_building(BuildingInstance&& building_instance); IDENTIFIER_REGISTRY_ACCESSORS(building) - void reset_buildings(); bool expand_building(std::string_view building_type_identifier); - Good const* get_rgo() const; - std::string to_string() const; bool load_pop_list(PopManager const& pop_manager, ast::NodeCPtr root); bool add_pop(Pop&& pop); - void clear_pops(); size_t get_pop_count() const; - std::vector<Pop> const& get_pops() const; - Pop::pop_size_t get_total_population() const; void update_pops(); void update_state(Date today); @@ -127,13 +111,8 @@ namespace OpenVic { bool is_adjacent_to(Province const* province); bool add_adjacency(Province const* province, distance_t distance, flags_t flags); - std::vector<adjacency_t> const& get_adjacencies() const; - - Country const* get_owner() const; - Country const* get_controller() const; - std::vector<Country const*> const& get_cores() const; - bool is_slave() const; - void apply_history_to_province(ProvinceHistoryMap const& history, Date date); + bool reset(BuildingManager const& building_manager); + bool apply_history_to_province(ProvinceHistoryEntry const* entry); }; } |