diff options
Diffstat (limited to 'src/openvic-simulation/map')
-rw-r--r-- | src/openvic-simulation/map/MapDefinition.cpp (renamed from src/openvic-simulation/map/Map.cpp) | 189 | ||||
-rw-r--r-- | src/openvic-simulation/map/MapDefinition.hpp (renamed from src/openvic-simulation/map/Map.hpp) | 45 | ||||
-rw-r--r-- | src/openvic-simulation/map/MapInstance.cpp | 148 | ||||
-rw-r--r-- | src/openvic-simulation/map/MapInstance.hpp | 56 | ||||
-rw-r--r-- | src/openvic-simulation/map/Mapmode.cpp | 54 | ||||
-rw-r--r-- | src/openvic-simulation/map/Mapmode.hpp | 8 | ||||
-rw-r--r-- | src/openvic-simulation/map/ProvinceDefinition.cpp | 10 | ||||
-rw-r--r-- | src/openvic-simulation/map/ProvinceDefinition.hpp | 8 | ||||
-rw-r--r-- | src/openvic-simulation/map/ProvinceInstance.hpp | 5 | ||||
-rw-r--r-- | src/openvic-simulation/map/Region.hpp | 4 | ||||
-rw-r--r-- | src/openvic-simulation/map/State.cpp | 17 | ||||
-rw-r--r-- | src/openvic-simulation/map/State.hpp | 6 |
12 files changed, 306 insertions, 244 deletions
diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/MapDefinition.cpp index 11b940f..f3bb619 100644 --- a/src/openvic-simulation/map/Map.cpp +++ b/src/openvic-simulation/map/MapDefinition.cpp @@ -1,9 +1,7 @@ -#include "Map.hpp" +#include "MapDefinition.hpp" -#include <cstddef> #include <vector> -#include "openvic-simulation/history/ProvinceHistory.hpp" #include "openvic-simulation/types/Colour.hpp" #include "openvic-simulation/types/OrderedContainers.hpp" #include "openvic-simulation/utility/BMP.hpp" @@ -12,11 +10,9 @@ using namespace OpenVic; using namespace OpenVic::NodeTools; -Map::Map() - : dims { 0, 0 }, max_provinces { ProvinceDefinition::MAX_INDEX }, selected_province { nullptr }, - highest_province_population { 0 }, total_map_population { 0 } {} +MapDefinition::MapDefinition() : dims { 0, 0 }, max_provinces { ProvinceDefinition::MAX_INDEX } {} -bool Map::add_province_definition(std::string_view identifier, colour_t colour) { +bool MapDefinition::add_province_definition(std::string_view identifier, colour_t colour) { if (province_definitions.size() >= max_provinces) { Logger::error( "The map's province list is full - maximum number of provinces is ", max_provinces, " (this can be at most ", @@ -53,23 +49,7 @@ bool Map::add_province_definition(std::string_view identifier, colour_t colour) return province_definitions.add_item(std::move(new_province)); } -ProvinceInstance* Map::get_province_instance_from_const(ProvinceDefinition const* province) { - if (province != nullptr) { - return get_province_instance_by_index(province->get_index()); - } else { - return nullptr; - } -} - -ProvinceInstance const* Map::get_province_instance_from_const(ProvinceDefinition const* province) const { - if (province != nullptr) { - return get_province_instance_by_index(province->get_index()); - } else { - return nullptr; - } -} - -ProvinceDefinition::distance_t Map::calculate_distance_between( +ProvinceDefinition::distance_t MapDefinition::calculate_distance_between( ProvinceDefinition const& from, ProvinceDefinition const& to ) const { const fvec2_t to_pos = to.get_unit_position(); @@ -90,7 +70,7 @@ using adjacency_t = ProvinceDefinition::adjacency_t; /* This is called for all adjacent pixel pairs and returns whether or not a new adjacency was add, * hence the lack of error messages in the false return cases. */ -bool Map::add_standard_adjacency(ProvinceDefinition& from, ProvinceDefinition& to) const { +bool MapDefinition::add_standard_adjacency(ProvinceDefinition& from, ProvinceDefinition& to) const { if (from == to) { return false; } @@ -129,7 +109,7 @@ bool Map::add_standard_adjacency(ProvinceDefinition& from, ProvinceDefinition& t return true; } -bool Map::add_special_adjacency( +bool MapDefinition::add_special_adjacency( ProvinceDefinition& from, ProvinceDefinition& to, adjacency_t::type_t type, ProvinceDefinition const* through, adjacency_t::data_t data ) const { @@ -257,7 +237,7 @@ bool Map::add_special_adjacency( return add_adjacency(from, to) & add_adjacency(to, from); } -bool Map::set_water_province(std::string_view identifier) { +bool MapDefinition::set_water_province(std::string_view identifier) { if (water_provinces.is_locked()) { Logger::error("The map's water provinces have already been locked!"); return false; @@ -285,7 +265,7 @@ bool Map::set_water_province(std::string_view identifier) { return true; } -bool Map::set_water_province_list(std::vector<std::string_view> const& list) { +bool MapDefinition::set_water_province_list(std::vector<std::string_view> const& list) { if (water_provinces.is_locked()) { Logger::error("The map's water provinces have already been locked!"); return false; @@ -299,12 +279,12 @@ bool Map::set_water_province_list(std::vector<std::string_view> const& list) { return ret; } -void Map::lock_water_provinces() { +void MapDefinition::lock_water_provinces() { water_provinces.lock(); Logger::info("Locked water provinces after registering ", water_provinces.size()); } -bool Map::add_region(std::string_view identifier, std::vector<ProvinceDefinition const*>&& provinces, colour_t colour) { +bool MapDefinition::add_region(std::string_view identifier, std::vector<ProvinceDefinition const*>&& provinces, colour_t colour) { if (identifier.empty()) { Logger::error("Invalid region identifier - empty!"); return false; @@ -345,7 +325,7 @@ bool Map::add_region(std::string_view identifier, std::vector<ProvinceDefinition return ret; } -ProvinceDefinition::index_t Map::get_index_from_colour(colour_t colour) const { +ProvinceDefinition::index_t MapDefinition::get_index_from_colour(colour_t colour) const { const colour_index_map_t::const_iterator it = colour_index_map.find(colour); if (it != colour_index_map.end()) { return it->second; @@ -353,22 +333,22 @@ ProvinceDefinition::index_t Map::get_index_from_colour(colour_t colour) const { return ProvinceDefinition::NULL_INDEX; } -ProvinceDefinition::index_t Map::get_province_index_at(ivec2_t pos) const { +ProvinceDefinition::index_t MapDefinition::get_province_index_at(ivec2_t pos) const { if (pos.nonnegative() && pos.less_than(dims)) { return province_shape_image[get_pixel_index_from_pos(pos)].index; } return ProvinceDefinition::NULL_INDEX; } -ProvinceDefinition* Map::get_province_definition_at(ivec2_t pos) { +ProvinceDefinition* MapDefinition::get_province_definition_at(ivec2_t pos) { return get_province_definition_by_index(get_province_index_at(pos)); } -ProvinceDefinition const* Map::get_province_definition_at(ivec2_t pos) const { +ProvinceDefinition const* MapDefinition::get_province_definition_at(ivec2_t pos) const { return get_province_definition_by_index(get_province_index_at(pos)); } -bool Map::set_max_provinces(ProvinceDefinition::index_t new_max_provinces) { +bool MapDefinition::set_max_provinces(ProvinceDefinition::index_t new_max_provinces) { if (new_max_provinces <= ProvinceDefinition::NULL_INDEX) { Logger::error( "Trying to set max province count to an invalid value ", new_max_provinces, " (must be greater than ", @@ -386,127 +366,6 @@ bool Map::set_max_provinces(ProvinceDefinition::index_t new_max_provinces) { return true; } -void Map::set_selected_province(ProvinceDefinition::index_t index) { - if (index == ProvinceDefinition::NULL_INDEX) { - selected_province = nullptr; - } else { - selected_province = get_province_instance_by_index(index); - if (selected_province == nullptr) { - Logger::error( - "Trying to set selected province to an invalid index ", index, " (max index is ", - get_province_instance_count(), ")" - ); - } - } -} - -ProvinceInstance* Map::get_selected_province() { - return selected_province; -} - -ProvinceDefinition::index_t Map::get_selected_province_index() const { - return selected_province != nullptr ? selected_province->get_province_definition().get_index() - : ProvinceDefinition::NULL_INDEX; -} - -bool Map::reset(BuildingTypeManager const& building_type_manager) { - if (!province_definitions_are_locked()) { - Logger::error("Cannot reset map - province consts are not locked!"); - return false; - } - - bool ret = true; - - // TODO - ensure all references to old ProvinceInstances are safely cleared - state_manager.reset(); - selected_province = nullptr; - - province_instances.reset(); - - province_instances.reserve(province_definitions.size()); - - for (ProvinceDefinition const& province : province_definitions.get_items()) { - ret &= province_instances.add_item({ province }); - } - - province_instances.lock(); - - for (ProvinceInstance& province : province_instances.get_items()) { - ret &= province.reset(building_type_manager); - } - - if (get_province_instance_count() != get_province_definition_count()) { - Logger::error( - "ProvinceInstance count (", get_province_instance_count(), ") does not match ProvinceDefinition count (", - get_province_definition_count(), ")!" - ); - return false; - } - - return ret; -} - -bool Map::apply_history_to_provinces( - ProvinceHistoryManager const& history_manager, Date date, IdeologyManager const& ideology_manager, - IssueManager const& issue_manager, Country const& country -) { - bool ret = true; - - for (ProvinceInstance& province : province_instances.get_items()) { - ProvinceDefinition const& province_definition = province.get_province_definition(); - if (!province_definition.is_water()) { - ProvinceHistoryMap const* history_map = history_manager.get_province_history(&province_definition); - - if (history_map != nullptr) { - ProvinceHistoryEntry const* pop_history_entry = nullptr; - - for (ProvinceHistoryEntry const* entry : history_map->get_entries_up_to(date)) { - province.apply_history_to_province(entry); - - if (!entry->get_pops().empty()) { - pop_history_entry = entry; - } - } - - if (pop_history_entry != nullptr) { - province.add_pop_vec(pop_history_entry->get_pops()); - - province.setup_pop_test_values(ideology_manager, issue_manager, country); - } - } - } - } - - return ret; -} - -void Map::update_gamestate(Date today) { - for (ProvinceInstance& province : province_instances.get_items()) { - province.update_gamestate(today); - } - state_manager.update_gamestate(); - - // Update population stats - highest_province_population = 0; - total_map_population = 0; - - for (ProvinceInstance const& province : province_instances.get_items()) { - const Pop::pop_size_t province_population = province.get_total_population(); - - if (highest_province_population < province_population) { - highest_province_population = province_population; - } - - total_map_population += province_population; - } -} - -void Map::tick(Date today) { - for (ProvinceInstance& province : province_instances.get_items()) { - province.tick(today); - } -} - using namespace ovdl::csv; static bool _validate_province_definitions_header(LineObject const& header) { @@ -541,7 +400,7 @@ static bool _parse_province_colour(colour_t& colour, std::array<std::string_view return ret; } -bool Map::load_province_definitions(std::vector<LineObject> const& lines) { +bool MapDefinition::load_province_definitions(std::vector<LineObject> const& lines) { if (lines.empty()) { Logger::error("No header or entries in province definition file!"); return false; @@ -581,7 +440,7 @@ bool Map::load_province_definitions(std::vector<LineObject> const& lines) { return ret; } -bool Map::load_province_positions(BuildingTypeManager const& building_type_manager, ast::NodeCPtr root) { +bool MapDefinition::load_province_positions(BuildingTypeManager const& building_type_manager, ast::NodeCPtr root) { return expect_province_definition_dictionary( [this, &building_type_manager](ProvinceDefinition& province, ast::NodeCPtr node) -> bool { return province.load_positions(*this, building_type_manager, node); @@ -589,7 +448,7 @@ bool Map::load_province_positions(BuildingTypeManager const& building_type_manag )(root); } -bool Map::load_region_colours(ast::NodeCPtr root, std::vector<colour_t>& colours) { +bool MapDefinition::load_region_colours(ast::NodeCPtr root, std::vector<colour_t>& colours) { return expect_dictionary_reserve_length( colours, [&colours](std::string_view key, ast::NodeCPtr value) -> bool { @@ -601,7 +460,7 @@ bool Map::load_region_colours(ast::NodeCPtr root, std::vector<colour_t>& colours })(root); } -bool Map::load_region_file(ast::NodeCPtr root, std::vector<colour_t> const& colours) { +bool MapDefinition::load_region_file(ast::NodeCPtr root, std::vector<colour_t> const& colours) { const bool ret = expect_dictionary_reserve_length( regions, [this, &colours](std::string_view region_identifier, ast::NodeCPtr region_node) -> bool { @@ -631,7 +490,7 @@ static constexpr colour_t colour_at(uint8_t const* colour_data, int32_t idx) { return { colour_data[idx + 2], colour_data[idx + 1], colour_data[idx] }; } -bool Map::load_map_images(fs::path const& province_path, fs::path const& terrain_path, bool detailed_errors) { +bool MapDefinition::load_map_images(fs::path const& province_path, fs::path const& terrain_path, bool detailed_errors) { if (!province_definitions_are_locked()) { Logger::error("Province index image cannot be generated until after provinces are locked!"); return false; @@ -784,7 +643,7 @@ bool Map::load_map_images(fs::path const& province_path, fs::path const& terrain /* REQUIREMENTS: * MAP-19, MAP-84 */ -bool Map::_generate_standard_province_adjacencies() { +bool MapDefinition::_generate_standard_province_adjacencies() { bool changed = false; const auto generate_adjacency = [this](ProvinceDefinition* current, ivec2_t pos) -> bool { @@ -809,7 +668,7 @@ bool Map::_generate_standard_province_adjacencies() { return changed; } -bool Map::generate_and_load_province_adjacencies(std::vector<LineObject> const& additional_adjacencies) { +bool MapDefinition::generate_and_load_province_adjacencies(std::vector<LineObject> const& additional_adjacencies) { bool ret = _generate_standard_province_adjacencies(); if (!ret) { Logger::error("Failed to generate standard province adjacencies!"); @@ -871,7 +730,7 @@ bool Map::generate_and_load_province_adjacencies(std::vector<LineObject> const& return ret; } -bool Map::load_climate_file(ModifierManager const& modifier_manager, ast::NodeCPtr root) { +bool MapDefinition::load_climate_file(ModifierManager const& modifier_manager, ast::NodeCPtr root) { bool ret = expect_dictionary_reserve_length( climates, [this, &modifier_manager](std::string_view identifier, ast::NodeCPtr node) -> bool { @@ -924,7 +783,7 @@ bool Map::load_climate_file(ModifierManager const& modifier_manager, ast::NodeCP return ret; } -bool Map::load_continent_file(ModifierManager const& modifier_manager, ast::NodeCPtr root) { +bool MapDefinition::load_continent_file(ModifierManager const& modifier_manager, ast::NodeCPtr root) { bool ret = expect_dictionary_reserve_length( continents, [this, &modifier_manager](std::string_view identifier, ast::NodeCPtr node) -> bool { diff --git a/src/openvic-simulation/map/Map.hpp b/src/openvic-simulation/map/MapDefinition.hpp index 12c9de4..9ec4367 100644 --- a/src/openvic-simulation/map/Map.hpp +++ b/src/openvic-simulation/map/MapDefinition.hpp @@ -1,27 +1,30 @@ #pragma once #include <filesystem> +#include <string_view> +#include <vector> #include <openvic-dataloader/csv/LineObject.hpp> #include "openvic-simulation/map/ProvinceDefinition.hpp" -#include "openvic-simulation/map/ProvinceInstance.hpp" #include "openvic-simulation/map/Region.hpp" -#include "openvic-simulation/map/State.hpp" #include "openvic-simulation/map/TerrainType.hpp" #include "openvic-simulation/types/Colour.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" #include "openvic-simulation/types/OrderedContainers.hpp" +#include "openvic-simulation/types/Vector.hpp" namespace OpenVic { namespace fs = std::filesystem; - struct GoodManager; - struct ProvinceHistoryManager; + struct BuildingTypeManager; + struct ModifierManager; /* REQUIREMENTS: * MAP-4 */ - struct Map { + struct MapDefinition { + #pragma pack(push, 1) /* Used to represent tightly packed 3-byte integer pixel information. */ struct shape_pixel_t { @@ -29,11 +32,11 @@ namespace OpenVic { TerrainTypeMapping::index_t terrain; }; #pragma pack(pop) + private: using colour_index_map_t = ordered_map<colour_t, ProvinceDefinition::index_t>; IdentifierRegistry<ProvinceDefinition> IDENTIFIER_REGISTRY_CUSTOM_INDEX_OFFSET(province_definition, 1); - IdentifierRegistry<ProvinceInstance> IDENTIFIER_REGISTRY_CUSTOM_INDEX_OFFSET(province_instance, 1); IdentifierRegistry<Region> IDENTIFIER_REGISTRY(region); IdentifierRegistry<Climate> IDENTIFIER_REGISTRY(climate); IdentifierRegistry<Continent> IDENTIFIER_REGISTRY(continent); @@ -45,36 +48,24 @@ namespace OpenVic { colour_index_map_t colour_index_map; ProvinceDefinition::index_t PROPERTY(max_provinces); - ProvinceInstance* PROPERTY(selected_province); // is it right for this to be mutable? how about using an index instead? - Pop::pop_size_t PROPERTY(highest_province_population); - Pop::pop_size_t PROPERTY(total_map_population); ProvinceDefinition::index_t get_index_from_colour(colour_t colour) const; bool _generate_standard_province_adjacencies(); - StateManager PROPERTY_REF(state_manager); - inline constexpr int32_t get_pixel_index_from_pos(ivec2_t pos) const { return pos.x + pos.y * dims.x; } + IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_CUSTOM_INDEX_OFFSET(province_definition, 1); + public: - Map(); + MapDefinition(); inline constexpr int32_t get_width() const { return dims.x; } inline constexpr int32_t get_height() const { return dims.y; } bool add_province_definition(std::string_view identifier, colour_t colour); - private: - IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_CUSTOM_INDEX_OFFSET(province_definition, 1); - - public: - IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_CUSTOM_INDEX_OFFSET(province_instance, 1); - - ProvinceInstance* get_province_instance_from_const(ProvinceDefinition const* province); - ProvinceInstance const* get_province_instance_from_const(ProvinceDefinition const* province) const; - ProvinceDefinition::distance_t calculate_distance_between( ProvinceDefinition const& from, ProvinceDefinition const& to ) const; @@ -105,21 +96,9 @@ namespace OpenVic { public: ProvinceDefinition const* get_province_definition_at(ivec2_t pos) const; bool set_max_provinces(ProvinceDefinition::index_t new_max_provinces); - void set_selected_province(ProvinceDefinition::index_t index); - ProvinceInstance* get_selected_province(); - ProvinceDefinition::index_t get_selected_province_index() const; bool add_region(std::string_view identifier, std::vector<ProvinceDefinition const*>&& provinces, colour_t colour); - bool reset(BuildingTypeManager const& building_type_manager); - bool apply_history_to_provinces( - ProvinceHistoryManager const& history_manager, Date date, IdeologyManager const& ideology_manager, - IssueManager const& issue_manager, Country const& country - ); - - void update_gamestate(Date today); - void tick(Date today); - bool load_province_definitions(std::vector<ovdl::csv::LineObject> const& lines); /* Must be loaded after adjacencies so we know what provinces are coastal, and so can have a port */ bool load_province_positions(BuildingTypeManager const& building_type_manager, ast::NodeCPtr root); diff --git a/src/openvic-simulation/map/MapInstance.cpp b/src/openvic-simulation/map/MapInstance.cpp new file mode 100644 index 0000000..23fe6a3 --- /dev/null +++ b/src/openvic-simulation/map/MapInstance.cpp @@ -0,0 +1,148 @@ +#include "MapInstance.hpp" + +#include "openvic-simulation/history/ProvinceHistory.hpp" +#include "openvic-simulation/map/MapDefinition.hpp" +#include "openvic-simulation/utility/Logger.hpp" + +using namespace OpenVic; + +MapInstance::MapInstance(MapDefinition const& new_map_definition) + : map_definition { new_map_definition }, selected_province { nullptr }, highest_province_population { 0 }, + total_map_population { 0 } {} + +ProvinceInstance* MapInstance::get_province_instance_from_const(ProvinceDefinition const* province) { + if (province != nullptr) { + return get_province_instance_by_index(province->get_index()); + } else { + return nullptr; + } +} + +ProvinceInstance const* MapInstance::get_province_instance_from_const(ProvinceDefinition const* province) const { + if (province != nullptr) { + return get_province_instance_by_index(province->get_index()); + } else { + return nullptr; + } +} + +void MapInstance::set_selected_province(ProvinceDefinition::index_t index) { + if (index == ProvinceDefinition::NULL_INDEX) { + selected_province = nullptr; + } else { + selected_province = get_province_instance_by_index(index); + if (selected_province == nullptr) { + Logger::error( + "Trying to set selected province to an invalid index ", index, " (max index is ", + get_province_instance_count(), ")" + ); + } + } +} + +ProvinceInstance* MapInstance::get_selected_province() { + return selected_province; +} + +ProvinceDefinition::index_t MapInstance::get_selected_province_index() const { + return selected_province != nullptr ? selected_province->get_province_definition().get_index() + : ProvinceDefinition::NULL_INDEX; +} + +bool MapInstance::reset(BuildingTypeManager const& building_type_manager) { + if (!map_definition.province_definitions_are_locked()) { + Logger::error("Cannot reset map - province consts are not locked!"); + return false; + } + + bool ret = true; + + // TODO - ensure all references to old ProvinceInstances are safely cleared + state_manager.reset(); + selected_province = nullptr; + + province_instances.reset(); + + province_instances.reserve(map_definition.get_province_definition_count()); + + for (ProvinceDefinition const& province : map_definition.get_province_definitions()) { + ret &= province_instances.add_item({ province }); + } + + province_instances.lock(); + + for (ProvinceInstance& province : province_instances.get_items()) { + ret &= province.reset(building_type_manager); + } + + if (get_province_instance_count() != map_definition.get_province_definition_count()) { + Logger::error( + "ProvinceInstance count (", get_province_instance_count(), ") does not match ProvinceDefinition count (", + map_definition.get_province_definition_count(), ")!" + ); + return false; + } + + return ret; +} + +bool MapInstance::apply_history_to_provinces( + ProvinceHistoryManager const& history_manager, Date date, IdeologyManager const& ideology_manager, + IssueManager const& issue_manager, Country const& country +) { + bool ret = true; + + for (ProvinceInstance& province : province_instances.get_items()) { + ProvinceDefinition const& province_definition = province.get_province_definition(); + if (!province_definition.is_water()) { + ProvinceHistoryMap const* history_map = history_manager.get_province_history(&province_definition); + + if (history_map != nullptr) { + ProvinceHistoryEntry const* pop_history_entry = nullptr; + + for (ProvinceHistoryEntry const* entry : history_map->get_entries_up_to(date)) { + province.apply_history_to_province(entry); + + if (!entry->get_pops().empty()) { + pop_history_entry = entry; + } + } + + if (pop_history_entry != nullptr) { + province.add_pop_vec(pop_history_entry->get_pops()); + + province.setup_pop_test_values(ideology_manager, issue_manager, country); + } + } + } + } + + return ret; +} + +void MapInstance::update_gamestate(Date today) { + for (ProvinceInstance& province : province_instances.get_items()) { + province.update_gamestate(today); + } + state_manager.update_gamestate(); + + // Update population stats + highest_province_population = 0; + total_map_population = 0; + + for (ProvinceInstance const& province : province_instances.get_items()) { + const Pop::pop_size_t province_population = province.get_total_population(); + + if (highest_province_population < province_population) { + highest_province_population = province_population; + } + + total_map_population += province_population; + } +} + +void MapInstance::tick(Date today) { + for (ProvinceInstance& province : province_instances.get_items()) { + province.tick(today); + } +} diff --git a/src/openvic-simulation/map/MapInstance.hpp b/src/openvic-simulation/map/MapInstance.hpp new file mode 100644 index 0000000..1f87326 --- /dev/null +++ b/src/openvic-simulation/map/MapInstance.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include "openvic-simulation/map/ProvinceDefinition.hpp" +#include "openvic-simulation/map/ProvinceInstance.hpp" +#include "openvic-simulation/map/State.hpp" +#include "openvic-simulation/types/Date.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" + +namespace OpenVic { + struct MapDefinition; + struct BuildingTypeManager; + struct ProvinceHistoryManager; + struct IdeologyManager; + struct IssueManager; + struct Country; + + /* REQUIREMENTS: + * MAP-4 + */ + struct MapInstance { + MapDefinition const& PROPERTY(map_definition); + + IdentifierRegistry<ProvinceInstance> IDENTIFIER_REGISTRY_CUSTOM_INDEX_OFFSET(province_instance, 1); + + ProvinceInstance* PROPERTY(selected_province); // is it right for this to be mutable? how about using an index instead? + Pop::pop_size_t PROPERTY(highest_province_population); + Pop::pop_size_t PROPERTY(total_map_population); + + StateManager PROPERTY_REF(state_manager); + + public: + MapInstance(MapDefinition const& new_map_definition); + + inline explicit constexpr operator MapDefinition const&() const { + return map_definition; + } + + IDENTIFIER_REGISTRY_NON_CONST_ACCESSORS_CUSTOM_INDEX_OFFSET(province_instance, 1); + + ProvinceInstance* get_province_instance_from_const(ProvinceDefinition const* province); + ProvinceInstance const* get_province_instance_from_const(ProvinceDefinition const* province) const; + + void set_selected_province(ProvinceDefinition::index_t index); + ProvinceInstance* get_selected_province(); + ProvinceDefinition::index_t get_selected_province_index() const; + + bool reset(BuildingTypeManager const& building_type_manager); + bool apply_history_to_provinces( + ProvinceHistoryManager const& history_manager, Date date, IdeologyManager const& ideology_manager, + IssueManager const& issue_manager, Country const& country + ); + + void update_gamestate(Date today); + void tick(Date today); + }; +} diff --git a/src/openvic-simulation/map/Mapmode.cpp b/src/openvic-simulation/map/Mapmode.cpp index d2acd84..6f8e606 100644 --- a/src/openvic-simulation/map/Mapmode.cpp +++ b/src/openvic-simulation/map/Mapmode.cpp @@ -3,7 +3,10 @@ #include <cassert> #include "openvic-simulation/country/Country.hpp" -#include "openvic-simulation/map/Map.hpp" +#include "openvic-simulation/map/MapDefinition.hpp" +#include "openvic-simulation/map/MapInstance.hpp" +#include "openvic-simulation/map/ProvinceDefinition.hpp" +#include "openvic-simulation/map/ProvinceInstance.hpp" #include "openvic-simulation/utility/Utility.hpp" using namespace OpenVic; @@ -16,13 +19,15 @@ Mapmode::Mapmode( } const Mapmode Mapmode::ERROR_MAPMODE { - "mapmode_error", 0, [](Map const& map, ProvinceInstance const& province) -> base_stripe_t { + "mapmode_error", 0, [](MapInstance const&, ProvinceInstance const& province) -> base_stripe_t { return { 0xFFFF0000_argb, colour_argb_t::null() }; } }; -Mapmode::base_stripe_t Mapmode::get_base_stripe_colours(Map const& map, ProvinceInstance const& province) const { - return colour_func ? colour_func(map, province) : colour_argb_t::null(); +Mapmode::base_stripe_t Mapmode::get_base_stripe_colours( + MapInstance const& map_instance, ProvinceInstance const& province +) const { + return colour_func ? colour_func(map_instance, province) : colour_argb_t::null(); } bool MapmodeManager::add_mapmode(std::string_view identifier, Mapmode::colour_func_t colour_func) { @@ -37,7 +42,7 @@ bool MapmodeManager::add_mapmode(std::string_view identifier, Mapmode::colour_fu return mapmodes.add_item({ identifier, mapmodes.size(), colour_func }); } -bool MapmodeManager::generate_mapmode_colours(Map const& map, Mapmode::index_t index, uint8_t* target) const { +bool MapmodeManager::generate_mapmode_colours(MapInstance const& map_instance, Mapmode::index_t index, uint8_t* target) const { if (target == nullptr) { Logger::error("Mapmode colour target pointer is null!"); return false; @@ -60,12 +65,17 @@ bool MapmodeManager::generate_mapmode_colours(Map const& map, Mapmode::index_t i target_stripes[ProvinceDefinition::NULL_INDEX] = colour_argb_t::null(); - if (map.province_instances_are_locked()) { - for (ProvinceInstance const& province : map.get_province_instances()) { - target_stripes[province.get_province_definition().get_index()] = mapmode->get_base_stripe_colours(map, province); + if (map_instance.province_instances_are_locked()) { + for (ProvinceInstance const& province : map_instance.get_province_instances()) { + target_stripes[province.get_province_definition().get_index()] = + mapmode->get_base_stripe_colours(map_instance, province); } } else { - for (size_t index = ProvinceDefinition::NULL_INDEX + 1; index <= map.get_province_definition_count(); ++index) { + for ( + size_t index = ProvinceDefinition::NULL_INDEX + 1; + index <= map_instance.get_map_definition().get_province_definition_count(); + ++index + ) { target_stripes[index] = colour_argb_t::null(); } } @@ -83,7 +93,7 @@ static constexpr colour_argb_t DEFAULT_COLOUR_GREY = (0x7F7F7F_argb).with_alpha( template<utility::is_derived_from_specialization_of<_HasColour> T, typename P> requires(std::same_as<P, ProvinceDefinition> || std::same_as<P, ProvinceInstance>) static constexpr auto get_colour_mapmode(T const*(P::*get_item)() const) { - return [get_item](Map const& map, ProvinceInstance const& province) -> Mapmode::base_stripe_t { + return [get_item](MapInstance const&, ProvinceInstance const& province) -> Mapmode::base_stripe_t { ProvinceDefinition const& province_definition = province.get_province_definition(); T const* item = ([&province, &province_definition]() -> P const& { @@ -124,7 +134,7 @@ static constexpr Mapmode::base_stripe_t shaded_mapmode(fixed_point_map_t<T const template<utility::is_derived_from_specialization_of<_HasColour> T> static constexpr auto shaded_mapmode(fixed_point_map_t<T const*> const&(ProvinceInstance::*get_map)() const) { - return [get_map](Map const& map, ProvinceInstance const& province) -> Mapmode::base_stripe_t { + return [get_map](MapInstance const&, ProvinceInstance const& province) -> Mapmode::base_stripe_t { return shaded_mapmode((province.*get_map)()); }; } @@ -136,7 +146,7 @@ bool MapmodeManager::setup_mapmodes() { const std::vector<mapmode_t> mapmodes { { "mapmode_terrain", - [](Map const&, ProvinceInstance const& province) -> Mapmode::base_stripe_t { + [](MapInstance const&, ProvinceInstance const& province) -> Mapmode::base_stripe_t { return colour_argb_t::null(); } }, @@ -146,7 +156,7 @@ bool MapmodeManager::setup_mapmodes() { { /* TEST MAPMODE, TO BE REMOVED */ "mapmode_province", - [](Map const&, ProvinceInstance const& province) -> Mapmode::base_stripe_t { + [](MapInstance const&, ProvinceInstance const& province) -> Mapmode::base_stripe_t { return colour_argb_t { province.get_province_definition().get_colour(), ALPHA_VALUE }; } }, @@ -156,9 +166,10 @@ bool MapmodeManager::setup_mapmodes() { { /* TEST MAPMODE, TO BE REMOVED */ "mapmode_index", - [](Map const& map, ProvinceInstance const& province) -> Mapmode::base_stripe_t { + [](MapInstance const& map_instance, ProvinceInstance const& province) -> Mapmode::base_stripe_t { const colour_argb_t::value_type f = colour_argb_t::colour_traits::component_from_fraction( - province.get_province_definition().get_index(), map.get_province_definition_count() + 1 + province.get_province_definition().get_index(), + map_instance.get_map_definition().get_province_definition_count() + 1 ); return colour_argb_t::fill_as(f).with_alpha(ALPHA_VALUE); } @@ -172,7 +183,7 @@ bool MapmodeManager::setup_mapmodes() { }, { "mapmode_infrastructure", - [](Map const& map, ProvinceInstance const& province) -> Mapmode::base_stripe_t { + [](MapInstance const&, ProvinceInstance const& province) -> Mapmode::base_stripe_t { BuildingInstance const* railroad = province.get_building_by_identifier("railroad"); if (railroad != nullptr) { const colour_argb_t::value_type val = colour_argb_t::colour_traits::component_from_fraction( @@ -192,13 +203,13 @@ bool MapmodeManager::setup_mapmodes() { }, { "mapmode_population", - [](Map const& map, ProvinceInstance const& province) -> Mapmode::base_stripe_t { + [](MapInstance const& map_instance, ProvinceInstance const& province) -> Mapmode::base_stripe_t { // TODO - explore non-linear scaling to have more variation among non-massive provinces // TODO - when selecting a province, only show the population of provinces controlled (or owned?) // by the same country, relative to the most populous province in that set of provinces if (!province.get_province_definition().is_water()) { const colour_argb_t::value_type val = colour_argb_t::colour_traits::component_from_fraction( - province.get_total_population(), map.get_highest_province_population() + 1, 0.1f, 1.0f + province.get_total_population(), map_instance.get_highest_province_population() + 1, 0.1f, 1.0f ); return colour_argb_t { 0, val, 0, ALPHA_VALUE }; } else { @@ -215,8 +226,9 @@ bool MapmodeManager::setup_mapmodes() { }, { /* TEST MAPMODE, TO BE REMOVED */ - "mapmode_adjacencies", [](Map const& map, ProvinceInstance const& province) -> Mapmode::base_stripe_t { - ProvinceInstance const* selected_province = map.get_selected_province(); + "mapmode_adjacencies", + [](MapInstance const& map_instance, ProvinceInstance const& province) -> Mapmode::base_stripe_t { + ProvinceInstance const* selected_province = map_instance.get_selected_province(); if (selected_province != nullptr) { ProvinceDefinition const& selected_province_definition = selected_province->get_province_definition(); @@ -258,7 +270,7 @@ bool MapmodeManager::setup_mapmodes() { } }, { - "mapmode_port", [](Map const& map, ProvinceInstance const& province) -> Mapmode::base_stripe_t { + "mapmode_port", [](MapInstance const&, ProvinceInstance const& province) -> Mapmode::base_stripe_t { ProvinceDefinition const& province_definition = province.get_province_definition(); if (province_definition.has_port()) { diff --git a/src/openvic-simulation/map/Mapmode.hpp b/src/openvic-simulation/map/Mapmode.hpp index fa306e9..d514f7a 100644 --- a/src/openvic-simulation/map/Mapmode.hpp +++ b/src/openvic-simulation/map/Mapmode.hpp @@ -8,7 +8,7 @@ namespace OpenVic { struct MapmodeManager; - struct Map; + struct MapInstance; struct ProvinceInstance; struct Mapmode : HasIdentifier { @@ -23,7 +23,7 @@ namespace OpenVic { : base_colour { base }, stripe_colour { stripe } {} constexpr base_stripe_t(colour_argb_t both) : base_stripe_t { both, both } {} }; - using colour_func_t = std::function<base_stripe_t(Map const&, ProvinceInstance const&)>; + using colour_func_t = std::function<base_stripe_t(MapInstance const&, ProvinceInstance const&)>; using index_t = size_t; private: @@ -37,7 +37,7 @@ namespace OpenVic { Mapmode(Mapmode&&) = default; - base_stripe_t get_base_stripe_colours(Map const& map, ProvinceInstance const& province) const; + base_stripe_t get_base_stripe_colours(MapInstance const& map_instance, ProvinceInstance const& province) const; }; struct MapmodeManager { @@ -54,7 +54,7 @@ namespace OpenVic { * and A = 255 is fully the RGB colour packaged with A. The base and stripe colours for each province are packed * together adjacently, so each province's entry is 8 bytes long. The list contains ProvinceDefinition::MAX_INDEX + 1 * entries, that is the maximum allowed number of provinces plus one for the index-zero "null province". */ - bool generate_mapmode_colours(Map const& map, Mapmode::index_t index, uint8_t* target) const; + bool generate_mapmode_colours(MapInstance const& map_instance, Mapmode::index_t index, uint8_t* target) const; bool setup_mapmodes(); }; diff --git a/src/openvic-simulation/map/ProvinceDefinition.cpp b/src/openvic-simulation/map/ProvinceDefinition.cpp index bb8ad59..c40f8f0 100644 --- a/src/openvic-simulation/map/ProvinceDefinition.cpp +++ b/src/openvic-simulation/map/ProvinceDefinition.cpp @@ -2,7 +2,7 @@ #include "openvic-simulation/dataloader/NodeTools.hpp" #include "openvic-simulation/economy/BuildingType.hpp" -#include "openvic-simulation/map/Map.hpp" +#include "openvic-simulation/map/MapDefinition.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -26,8 +26,10 @@ std::string ProvinceDefinition::to_string() const { return stream.str(); } -bool ProvinceDefinition::load_positions(Map const& map, BuildingTypeManager const& building_type_manager, ast::NodeCPtr root) { - const fixed_point_t map_height = map.get_height(); +bool ProvinceDefinition::load_positions( + MapDefinition const& map_definition, BuildingTypeManager const& building_type_manager, ast::NodeCPtr root +) { + const fixed_point_t map_height = map_definition.get_height(); const bool ret = expect_dictionary_keys( "text_position", ZERO_OR_ONE, @@ -70,7 +72,7 @@ bool ProvinceDefinition::load_positions(Map const& map, BuildingTypeManager cons const fvec2_t port_dir { -rotation.cos(), rotation.sin() }; const ivec2_t port_facing_position = static_cast<ivec2_t>(*port_position + port_dir / 4); - ProvinceDefinition const* province = map.get_province_definition_at(port_facing_position); + ProvinceDefinition const* province = map_definition.get_province_definition_at(port_facing_position); if (province != nullptr) { if (province->is_water() && is_adjacent_to(province)) { diff --git a/src/openvic-simulation/map/ProvinceDefinition.hpp b/src/openvic-simulation/map/ProvinceDefinition.hpp index a4076fe..c66323c 100644 --- a/src/openvic-simulation/map/ProvinceDefinition.hpp +++ b/src/openvic-simulation/map/ProvinceDefinition.hpp @@ -11,7 +11,7 @@ namespace OpenVic { - struct Map; + struct MapDefinition; struct Region; struct TerrainType; struct ProvinceSetModifier; @@ -25,7 +25,7 @@ namespace OpenVic { * POP-22 */ struct ProvinceDefinition : HasIdentifierAndColour { - friend struct Map; + friend struct MapDefinition; using index_t = uint16_t; using distance_t = fixed_point_t; // should this go inside adjacency_t? @@ -114,7 +114,9 @@ namespace OpenVic { } /* The positions' y coordinates need to be inverted. */ - bool load_positions(Map const& map, BuildingTypeManager const& building_type_manager, ast::NodeCPtr root); + bool load_positions( + MapDefinition const& map_definition, BuildingTypeManager const& building_type_manager, ast::NodeCPtr root + ); fvec2_t get_text_position() const; fixed_point_t get_text_rotation() const; diff --git a/src/openvic-simulation/map/ProvinceInstance.hpp b/src/openvic-simulation/map/ProvinceInstance.hpp index 2dbc4e3..3292575 100644 --- a/src/openvic-simulation/map/ProvinceInstance.hpp +++ b/src/openvic-simulation/map/ProvinceInstance.hpp @@ -7,6 +7,7 @@ #include "openvic-simulation/types/OrderedContainers.hpp" namespace OpenVic { + struct MapInstance; struct ProvinceDefinition; struct TerrainType; struct State; @@ -24,7 +25,7 @@ namespace OpenVic { struct IssueManager; struct ProvinceInstance : HasIdentifier { - friend struct Map; + friend struct MapInstance; using life_rating_t = int8_t; @@ -63,7 +64,7 @@ namespace OpenVic { public: ProvinceInstance(ProvinceInstance&&) = default; - inline constexpr operator ProvinceDefinition const&() const { + inline explicit constexpr operator ProvinceDefinition const&() const { return province_definition; } diff --git a/src/openvic-simulation/map/Region.hpp b/src/openvic-simulation/map/Region.hpp index f532400..1152dab 100644 --- a/src/openvic-simulation/map/Region.hpp +++ b/src/openvic-simulation/map/Region.hpp @@ -46,7 +46,7 @@ namespace OpenVic { }; struct ProvinceSetModifier : Modifier, ProvinceSet { - friend struct Map; + friend struct MapDefinition; private: ProvinceSetModifier(std::string_view new_identifier, ModifierValue&& new_values); public: @@ -57,7 +57,7 @@ namespace OpenVic { * MAP-6, MAP-44, MAP-48 */ struct Region : HasIdentifierAndColour, ProvinceSet { - friend struct Map; + friend struct MapDefinition; private: /* A meta region cannot be the template for a state. diff --git a/src/openvic-simulation/map/State.cpp b/src/openvic-simulation/map/State.cpp index 1c49ed7..46ff11d 100644 --- a/src/openvic-simulation/map/State.cpp +++ b/src/openvic-simulation/map/State.cpp @@ -1,7 +1,8 @@ #include "State.hpp" #include "openvic-simulation/country/Country.hpp" -#include "openvic-simulation/map/Map.hpp" +#include "openvic-simulation/map/MapDefinition.hpp" +#include "openvic-simulation/map/MapInstance.hpp" #include "openvic-simulation/map/ProvinceInstance.hpp" #include "openvic-simulation/map/Region.hpp" @@ -39,7 +40,7 @@ void StateSet::update_gamestate() { } } -bool StateManager::add_state_set(Map& map, Region const& region) { +bool StateManager::add_state_set(MapInstance& map_instance, Region const& region) { if (region.get_meta()) { Logger::error("Cannot use meta region \"", region.get_identifier(), "\" as state template!"); return false; @@ -54,7 +55,7 @@ bool StateManager::add_state_set(Map& map, Region const& region) { for (ProvinceDefinition const* province : region.get_provinces()) { - ProvinceInstance* province_instance = map.get_province_instance_from_const(province); + ProvinceInstance* province_instance = map_instance.get_province_instance_from_const(province); // add to existing state if shared owner & status... for (std::vector<ProvinceInstance*>& provinces : temp_provinces) { @@ -98,16 +99,18 @@ bool StateManager::add_state_set(Map& map, Region const& region) { return true; } -bool StateManager::generate_states(Map& map) { +bool StateManager::generate_states(MapInstance& map_instance) { + MapDefinition const& map_definition = map_instance.get_map_definition(); + state_sets.clear(); - state_sets.reserve(map.get_region_count()); + state_sets.reserve(map_definition.get_region_count()); bool ret = true; size_t state_count = 0; - for (Region const& region : map.get_regions()) { + for (Region const& region : map_definition.get_regions()) { if (!region.get_meta()) { - if (add_state_set(map, region)) { + if (add_state_set(map_instance, region)) { state_count += state_sets.back().get_state_count(); } else { ret = false; diff --git a/src/openvic-simulation/map/State.hpp b/src/openvic-simulation/map/State.hpp index e030a0b..ff8acfa 100644 --- a/src/openvic-simulation/map/State.hpp +++ b/src/openvic-simulation/map/State.hpp @@ -53,20 +53,20 @@ namespace OpenVic { void update_gamestate(); }; - struct Map; + struct MapInstance; /* Contains all current states.*/ struct StateManager { private: std::vector<StateSet> PROPERTY(state_sets); - bool add_state_set(Map& map, Region const& region); + bool add_state_set(MapInstance& map_instance, Region const& region); public: /* Creates states from current province gamestate & regions, sets province state value. * After this function, the `regions` property is unmanaged and must be carefully updated and * validated by functions that modify it. */ - bool generate_states(Map& map); + bool generate_states(MapInstance& map_instance); void reset(); |