diff options
author | Hop311 <Hop3114@gmail.com> | 2024-06-13 00:52:45 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-13 00:52:45 +0200 |
commit | a49e4d0975e2b5dfd13ae50aa716f1a0307e618d (patch) | |
tree | f681dd2e76d489ba6e7beea44ae6ebd59e9d51f3 /src/openvic-simulation | |
parent | f5d173e88a49a1a9556860063aef1aa287925cfd (diff) | |
parent | 1198a780916e65cae048dd3478f614c1d18db846 (diff) |
Merge pull request #162 from OpenVicProject/map-instance
Map instance
Diffstat (limited to 'src/openvic-simulation')
25 files changed, 706 insertions, 601 deletions
diff --git a/src/openvic-simulation/GameManager.cpp b/src/openvic-simulation/GameManager.cpp index 3f8220c..fff806c 100644 --- a/src/openvic-simulation/GameManager.cpp +++ b/src/openvic-simulation/GameManager.cpp @@ -1,16 +1,14 @@ #include "GameManager.hpp" -#include "openvic-simulation/types/Colour.hpp" - using namespace OpenVic; -using namespace OpenVic::colour_literals; GameManager::GameManager( gamestate_updated_func_t gamestate_updated_callback, SimulationClock::state_changed_function_t clock_state_changed_callback ) : simulation_clock { std::bind(&GameManager::tick, this), std::bind(&GameManager::update_gamestate, this), clock_state_changed_callback }, gamestate_updated { gamestate_updated_callback ? std::move(gamestate_updated_callback) : []() {} }, - session_start { 0 }, today {}, gamestate_needs_update { false }, currently_updating_gamestate { false } {} + session_start { 0 }, today {}, gamestate_needs_update { false }, currently_updating_gamestate { false }, + map_instance { map_definition } {} void GameManager::set_gamestate_needs_update() { if (!currently_updating_gamestate) { @@ -26,7 +24,7 @@ void GameManager::update_gamestate() { } currently_updating_gamestate = true; Logger::info("Update: ", today); - map.update_gamestate(today); + map_instance.update_gamestate(today); gamestate_updated(); gamestate_needs_update = false; currently_updating_gamestate = false; @@ -38,7 +36,7 @@ void GameManager::update_gamestate() { void GameManager::tick() { today++; Logger::info("Tick: ", today); - map.tick(today); + map_instance.tick(today); set_gamestate_needs_update(); } @@ -47,7 +45,7 @@ bool GameManager::reset() { simulation_clock.reset(); today = {}; economy_manager.get_good_manager().reset_to_defaults(); - bool ret = map.reset(economy_manager.get_building_type_manager()); + bool ret = map_instance.reset(economy_manager.get_building_type_manager()); set_gamestate_needs_update(); return ret; } @@ -69,15 +67,15 @@ bool GameManager::load_bookmark(Bookmark const* new_bookmark) { today = bookmark->get_date(); - ret &= map.apply_history_to_provinces( + ret &= map_instance.apply_history_to_provinces( history_manager.get_province_manager(), today, politics_manager.get_ideology_manager(), politics_manager.get_issue_manager(), *country_manager.get_country_by_identifier("ENG") ); - ret &= map.get_state_manager().generate_states(map); + ret &= map_instance.get_state_manager().generate_states(map_instance); ret &= country_instance_manager.generate_country_instances(country_manager); ret &= country_instance_manager.apply_history_to_countries( - history_manager.get_country_manager(), today, military_manager.get_unit_instance_manager(), map + history_manager.get_country_manager(), today, military_manager.get_unit_instance_manager(), map_instance ); return ret; @@ -85,7 +83,7 @@ bool GameManager::load_bookmark(Bookmark const* new_bookmark) { bool GameManager::expand_selected_province_building(size_t building_index) { set_gamestate_needs_update(); - ProvinceInstance* province = map.get_selected_province(); + ProvinceInstance* province = map_instance.get_selected_province(); if (province == nullptr) { Logger::error("Cannot expand building index ", building_index, " - no province selected!"); return false; @@ -96,210 +94,3 @@ bool GameManager::expand_selected_province_building(size_t building_index) { } return province->expand_building(building_index); } - -static constexpr colour_argb_t::value_type ALPHA_VALUE = colour_argb_t::max_value; -/* White default colour, used in mapmodes including political, revolt risk and party loyaly. */ -static constexpr colour_argb_t DEFAULT_COLOUR_WHITE = (0xFFFFFF_argb).with_alpha(ALPHA_VALUE); -/* Grey default colour, used in mapmodes including diplomatic, administrative and colonial, recruitment, - * national focus, RGO, population density, sphere of influence, ranking and migration. */ -static constexpr colour_argb_t DEFAULT_COLOUR_GREY = (0x7F7F7F_argb).with_alpha(ALPHA_VALUE); - -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 { - ProvinceDefinition const& province_definition = province.get_province_definition(); - - T const* item = [&province, &province_definition, get_item]() -> T const* { - if constexpr (std::same_as<P, ProvinceDefinition>) { - return (province_definition.*get_item)(); - } else { - return (province.*get_item)(); - } - }(); - - if (item != nullptr) { - return colour_argb_t { item->get_colour(), ALPHA_VALUE }; - } else if (!province_definition.is_water()) { - return DEFAULT_COLOUR_WHITE; - } else { - return colour_argb_t::null(); - } - }; -} - -template<utility::is_derived_from_specialization_of<_HasColour> T> -static constexpr Mapmode::base_stripe_t shaded_mapmode(fixed_point_map_t<T const*> const& map) { - const std::pair<fixed_point_map_const_iterator_t<T const*>, fixed_point_map_const_iterator_t<T const*>> largest = - get_largest_two_items(map); - if (largest.first != map.end()) { - const colour_argb_t base_colour = colour_argb_t { largest.first->first->get_colour(), ALPHA_VALUE }; - if (largest.second != map.end()) { - /* If second largest is at least a third... */ - if (largest.second->second * 3 >= get_total(map)) { - const colour_argb_t stripe_colour = colour_argb_t { largest.second->first->get_colour(), ALPHA_VALUE }; - return { base_colour, stripe_colour }; - } - } - return base_colour; - } - return colour_argb_t::null(); -} - -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 shaded_mapmode((province.*get_map)()); - }; -} - -bool GameManager::load_hardcoded_defines() { - bool ret = true; - - using mapmode_t = std::pair<std::string, Mapmode::colour_func_t>; - const std::vector<mapmode_t> mapmodes { - { - "mapmode_terrain", - [](Map const&, ProvinceInstance const& province) -> Mapmode::base_stripe_t { - return colour_argb_t::null(); - } - }, - { - "mapmode_political", get_colour_mapmode(&ProvinceInstance::get_owner) - }, - { - /* TEST MAPMODE, TO BE REMOVED */ - "mapmode_province", - [](Map const&, ProvinceInstance const& province) -> Mapmode::base_stripe_t { - return colour_argb_t { province.get_province_definition().get_colour(), ALPHA_VALUE }; - } - }, - { - "mapmode_region", get_colour_mapmode(&ProvinceDefinition::get_region) - }, - { - /* TEST MAPMODE, TO BE REMOVED */ - "mapmode_index", - [](Map const& map, 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 - ); - return colour_argb_t::fill_as(f).with_alpha(ALPHA_VALUE); - } - }, - { - /* Non-vanilla mapmode, still of use in game. */ - "mapmode_terrain_type", get_colour_mapmode(&ProvinceInstance::get_terrain_type) - }, - { - "mapmode_rgo", get_colour_mapmode(&ProvinceInstance::get_rgo) - }, - { - "mapmode_infrastructure", - [](Map const& map, 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( - railroad->get_level(), railroad->get_building_type().get_max_level() + 1, 0.5f, 1.0f - ); - switch (railroad->get_expansion_state()) { - case BuildingInstance::ExpansionState::CannotExpand: - return colour_argb_t { val, 0, 0, ALPHA_VALUE }; - case BuildingInstance::ExpansionState::CanExpand: - return colour_argb_t { 0, 0, val, ALPHA_VALUE }; - default: - return colour_argb_t { 0, val, 0, ALPHA_VALUE }; - } - } - return colour_argb_t::null(); - } - }, - { - "mapmode_population", - [](Map const& map, 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 - ); - return colour_argb_t { 0, val, 0, ALPHA_VALUE }; - } else { - return colour_argb_t::null(); - } - } - }, - { - "mapmode_culture", shaded_mapmode(&ProvinceInstance::get_culture_distribution) - }, - { - /* Non-vanilla mapmode, still of use in game. */ - "mapmode_religion", shaded_mapmode(&ProvinceInstance::get_religion_distribution) - }, - { - /* 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(); - - if (selected_province != nullptr) { - ProvinceDefinition const& selected_province_definition = selected_province->get_province_definition(); - - if (selected_province == &province) { - return (0xFFFFFF_argb).with_alpha(ALPHA_VALUE); - } - - ProvinceDefinition const* province_definition = &province.get_province_definition(); - - colour_argb_t base = colour_argb_t::null(), stripe = colour_argb_t::null(); - ProvinceDefinition::adjacency_t const* adj = - selected_province_definition.get_adjacency_to(province_definition); - - if (adj != nullptr) { - colour_argb_t::integer_type base_int; - switch (adj->get_type()) { - using enum ProvinceDefinition::adjacency_t::type_t; - case LAND: base_int = 0x00FF00; break; - case WATER: base_int = 0x0000FF; break; - case COASTAL: base_int = 0xF9D199; break; - case IMPASSABLE: base_int = 0x8B4513; break; - case STRAIT: base_int = 0x00FFFF; break; - case CANAL: base_int = 0x888888; break; - default: base_int = 0xFF0000; break; - } - base = colour_argb_t::from_integer(base_int).with_alpha(ALPHA_VALUE); - stripe = base; - } - - if (selected_province_definition.has_adjacency_going_through(province_definition)) { - stripe = (0xFFFF00_argb).with_alpha(ALPHA_VALUE); - } - - return { base, stripe }; - } - - return colour_argb_t::null(); - } - }, - { - "mapmode_port", [](Map const& map, ProvinceInstance const& province) -> Mapmode::base_stripe_t { - ProvinceDefinition const& province_definition = province.get_province_definition(); - - if (province_definition.has_port()) { - return (0xFFFFFF_argb).with_alpha(ALPHA_VALUE); - } else if (!province_definition.is_water()) { - return (0x333333_argb).with_alpha(ALPHA_VALUE); - } else { - return colour_argb_t::null(); - } - } - } - }; - - for (mapmode_t const& mapmode : mapmodes) { - ret &= map.add_mapmode(mapmode.first, mapmode.second); - } - map.lock_mapmodes(); - - return ret; -} diff --git a/src/openvic-simulation/GameManager.hpp b/src/openvic-simulation/GameManager.hpp index 8e0bd00..55ba37c 100644 --- a/src/openvic-simulation/GameManager.hpp +++ b/src/openvic-simulation/GameManager.hpp @@ -6,7 +6,9 @@ #include "openvic-simulation/history/HistoryManager.hpp" #include "openvic-simulation/interface/UI.hpp" #include "openvic-simulation/map/Crime.hpp" -#include "openvic-simulation/map/Map.hpp" +#include "openvic-simulation/map/MapDefinition.hpp" +#include "openvic-simulation/map/MapInstance.hpp" +#include "openvic-simulation/map/Mapmode.hpp" #include "openvic-simulation/military/MilitaryManager.hpp" #include "openvic-simulation/misc/Decision.hpp" #include "openvic-simulation/misc/Define.hpp" @@ -40,7 +42,9 @@ namespace OpenVic { DiplomacyManager PROPERTY_REF(diplomacy_manager); /* Near the end so it is freed after other managers that may depend on it, * e.g. if we want to remove military units from the province they're in when they're destructed. */ - Map PROPERTY_REF(map); + MapDefinition PROPERTY_REF(map_definition); + MapInstance PROPERTY_REF(map_instance); + MapmodeManager PROPERTY_REF(mapmode_manager); ScriptManager PROPERTY_REF(script_manager); SimulationClock PROPERTY_REF(simulation_clock); @@ -64,10 +68,5 @@ namespace OpenVic { bool load_bookmark(Bookmark const* new_bookmark); bool expand_selected_province_building(size_t building_index); - - /* Hardcoded data for defining things for which parsing from files has - * not been implemented, currently mapmodes and building types. - */ - bool load_hardcoded_defines(); }; } diff --git a/src/openvic-simulation/country/CountryInstance.cpp b/src/openvic-simulation/country/CountryInstance.cpp index 58e5232..25f40ed 100644 --- a/src/openvic-simulation/country/CountryInstance.cpp +++ b/src/openvic-simulation/country/CountryInstance.cpp @@ -109,7 +109,8 @@ bool CountryInstanceManager::generate_country_instances(CountryManager const& co } bool CountryInstanceManager::apply_history_to_countries( - CountryHistoryManager const& history_manager, Date date, UnitInstanceManager& unit_instance_manager, Map& map + CountryHistoryManager const& history_manager, Date date, UnitInstanceManager& unit_instance_manager, + MapInstance& map_instance ) { bool ret = true; @@ -129,7 +130,9 @@ bool CountryInstanceManager::apply_history_to_countries( } if (oob_history_entry != nullptr) { - unit_instance_manager.generate_deployment(map, country_instance, *oob_history_entry->get_inital_oob()); + unit_instance_manager.generate_deployment( + map_instance, country_instance, *oob_history_entry->get_inital_oob() + ); } } } diff --git a/src/openvic-simulation/country/CountryInstance.hpp b/src/openvic-simulation/country/CountryInstance.hpp index 33cee61..68d2bb2 100644 --- a/src/openvic-simulation/country/CountryInstance.hpp +++ b/src/openvic-simulation/country/CountryInstance.hpp @@ -59,7 +59,7 @@ namespace OpenVic { struct CountryManager; struct CountryHistoryManager; struct UnitInstanceManager; - struct Map; + struct MapInstance; struct CountryInstanceManager { private: @@ -69,7 +69,8 @@ namespace OpenVic { bool generate_country_instances(CountryManager const& country_manager); bool apply_history_to_countries( - CountryHistoryManager const& history_manager, Date date, UnitInstanceManager& unit_instance_manager, Map& map + CountryHistoryManager const& history_manager, Date date, UnitInstanceManager& unit_instance_manager, + MapInstance& map_instance ); }; } diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index ee0eaf4..ae115d3 100644 --- a/src/openvic-simulation/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -368,7 +368,7 @@ bool Dataloader::_load_units(GameManager& game_manager) const { [&game_manager, &unit_type_manager](fs::path const& file) -> bool { return unit_type_manager.load_unit_type_file( game_manager.get_economy_manager().get_good_manager(), - game_manager.get_map().get_terrain_type_manager(), + game_manager.get_map_definition().get_terrain_type_manager(), game_manager.get_modifier_manager(), parse_defines(file).get_file_node() ); @@ -552,7 +552,7 @@ bool Dataloader::_load_history(GameManager& game_manager, bool unused_history_fi { /* Province History */ ProvinceHistoryManager& province_history_manager = game_manager.get_history_manager().get_province_manager(); - Map const& map = game_manager.get_map(); + MapDefinition const& map_definition = game_manager.get_map_definition(); static constexpr std::string_view province_history_directory = "history/provinces"; const path_vector_t province_history_files = @@ -562,13 +562,13 @@ bool Dataloader::_load_history(GameManager& game_manager, bool unused_history_fi ret &= apply_to_files( province_history_files, - [this, &game_manager, &province_history_manager, &map, unused_history_file_warnings]( + [this, &game_manager, &province_history_manager, &map_definition, unused_history_file_warnings]( fs::path const& file ) -> bool { const std::string filename = file.stem().string(); const std::string_view province_id = extract_basic_identifier_prefix(filename); - ProvinceDefinition const* province = map.get_province_definition_by_identifier(province_id); + ProvinceDefinition const* province = map_definition.get_province_definition_by_identifier(province_id); if (province == nullptr) { if (unused_history_file_warnings) { Logger::warning("Found history file for non-existent province: ", province_id); @@ -610,7 +610,7 @@ bool Dataloader::_load_history(GameManager& game_manager, bool unused_history_fi } } - province_history_manager.lock_province_histories(map, false); + province_history_manager.lock_province_histories(map_definition, false); } { @@ -667,7 +667,7 @@ bool Dataloader::_load_events(GameManager& game_manager) { bool Dataloader::_load_map_dir(GameManager& game_manager) const { static constexpr std::string_view map_directory = "map/"; - Map& map = game_manager.get_map(); + MapDefinition& map_definition = game_manager.get_map_definition(); static constexpr std::string_view defaults_filename = "default.map"; static constexpr std::string_view default_definitions = "definition.csv"; @@ -711,7 +711,7 @@ bool Dataloader::_load_map_dir(GameManager& game_manager) const { bool ret = expect_dictionary_keys( "max_provinces", ONE_EXACTLY, - expect_uint<ProvinceDefinition::index_t>(std::bind_front(&Map::set_max_provinces, &map)), + expect_uint<ProvinceDefinition::index_t>(std::bind_front(&MapDefinition::set_max_provinces, &map_definition)), "sea_starts", ONE_EXACTLY, expect_list_reserve_length( water_province_identifiers, expect_identifier(vector_callback(water_province_identifiers)) @@ -733,14 +733,16 @@ bool Dataloader::_load_map_dir(GameManager& game_manager) const { Logger::error("Failed to load map default file!"); } - if (!map.load_province_definitions(parse_csv(lookup_file(append_string_views(map_directory, definitions))).get_lines())) { + if (!map_definition.load_province_definitions( + parse_csv(lookup_file(append_string_views(map_directory, definitions))).get_lines() + )) { Logger::error("Failed to load province definitions file!"); ret = false; } { std::vector<colour_t> colours; - if (!Map::load_region_colours(parse_defines(lookup_file(region_colours)).get_file_node(), colours)) { + if (!MapDefinition::load_region_colours(parse_defines(lookup_file(region_colours)).get_file_node(), colours)) { Logger::error("Failed to load region colours file!"); ret = false; } @@ -748,19 +750,19 @@ bool Dataloader::_load_map_dir(GameManager& game_manager) const { using namespace OpenVic::colour_literals; colours.push_back(0xFFFFFF_rgb); /* This ensures there is always at least one region colour. */ - if (!map.load_region_file( + if (!map_definition.load_region_file( parse_defines(lookup_file(append_string_views(map_directory, region))).get_file_node(), colours)) { Logger::error("Failed to load region file!"); ret = false; } } - if (!map.set_water_province_list(water_province_identifiers)) { + if (!map_definition.set_water_province_list(water_province_identifiers)) { Logger::error("Failed to set water provinces!"); ret = false; } - if (!map.get_terrain_type_manager().load_terrain_types( + if (!map_definition.get_terrain_type_manager().load_terrain_types( game_manager.get_modifier_manager(), parse_defines(lookup_file(append_string_views(map_directory, terrain_definition))).get_file_node() )) { @@ -768,7 +770,7 @@ bool Dataloader::_load_map_dir(GameManager& game_manager) const { ret = false; } - if (!map.load_map_images( + if (!map_definition.load_map_images( lookup_file(append_string_views(map_directory, provinces)), lookup_file(append_string_views(map_directory, terrain)), false )) { @@ -776,7 +778,7 @@ bool Dataloader::_load_map_dir(GameManager& game_manager) const { ret = false; } - if (map.generate_and_load_province_adjacencies( + if (map_definition.generate_and_load_province_adjacencies( parse_csv(lookup_file(append_string_views(map_directory, adjacencies))).get_lines() )) { Logger::info("Successfully generated and loaded province adjacencies!"); @@ -786,7 +788,7 @@ bool Dataloader::_load_map_dir(GameManager& game_manager) const { } /* Must be loaded after adjacencies so we know what provinces are coastal, and so can have a port */ - if (!map.load_province_positions( + if (!map_definition.load_province_positions( game_manager.get_economy_manager().get_building_type_manager(), parse_defines(lookup_file(append_string_views(map_directory, positions))).get_file_node() )) { @@ -794,7 +796,7 @@ bool Dataloader::_load_map_dir(GameManager& game_manager) const { ret = false; } - if (!map.load_climate_file( + if (!map_definition.load_climate_file( game_manager.get_modifier_manager(), parse_defines(lookup_file(append_string_views(map_directory, climate_file))).get_file_node() )) { @@ -802,7 +804,7 @@ bool Dataloader::_load_map_dir(GameManager& game_manager) const { ret = false; } - if (!map.load_continent_file( + if (!map_definition.load_continent_file( game_manager.get_modifier_manager(), parse_defines(lookup_file(append_string_views(map_directory, continent))).get_file_node() )) { @@ -843,6 +845,10 @@ bool Dataloader::load_defines(GameManager& game_manager) { bool ret = true; + if (!game_manager.get_mapmode_manager().setup_mapmodes()) { + Logger::error("Failed to set up mapmodes!"); + ret = false; + } if (!_load_interface_files(game_manager.get_ui_manager())) { Logger::error("Failed to load interface files!"); ret = false; diff --git a/src/openvic-simulation/history/CountryHistory.cpp b/src/openvic-simulation/history/CountryHistory.cpp index 90fe995..9ef230e 100644 --- a/src/openvic-simulation/history/CountryHistory.cpp +++ b/src/openvic-simulation/history/CountryHistory.cpp @@ -67,8 +67,9 @@ bool CountryHistoryMap::_load_history_entry( game_manager, dataloader, deployment_manager, entry.get_date(), value, key, value ); }, - "capital", ZERO_OR_ONE, - game_manager.get_map().expect_province_definition_identifier(assign_variable_callback_pointer_opt(entry.capital)), + "capital", ZERO_OR_ONE, game_manager.get_map_definition().expect_province_definition_identifier( + assign_variable_callback_pointer_opt(entry.capital) + ), "primary_culture", ZERO_OR_ONE, culture_manager.expect_culture_identifier(assign_variable_callback_pointer_opt(entry.primary_culture)), "culture", ZERO_OR_MORE, culture_manager.expect_culture_identifier( diff --git a/src/openvic-simulation/history/DiplomaticHistory.cpp b/src/openvic-simulation/history/DiplomaticHistory.cpp index f56b3dc..c8aa934 100644 --- a/src/openvic-simulation/history/DiplomaticHistory.cpp +++ b/src/openvic-simulation/history/DiplomaticHistory.cpp @@ -322,7 +322,7 @@ bool DiplomaticHistoryManager::load_war_history_file(GameManager const& game_man assign_variable_callback_pointer(*third_party) ), "state_province_id", ZERO_OR_ONE, - game_manager.get_map().expect_province_definition_identifier( + game_manager.get_map_definition().expect_province_definition_identifier( assign_variable_callback_pointer(*target) ) )(value); diff --git a/src/openvic-simulation/history/ProvinceHistory.cpp b/src/openvic-simulation/history/ProvinceHistory.cpp index 645cf8d..87d0713 100644 --- a/src/openvic-simulation/history/ProvinceHistory.cpp +++ b/src/openvic-simulation/history/ProvinceHistory.cpp @@ -22,7 +22,7 @@ bool ProvinceHistoryMap::_load_history_entry( CountryManager const& country_manager = game_manager.get_country_manager(); GoodManager const& good_manager = game_manager.get_economy_manager().get_good_manager(); IdeologyManager const& ideology_manager = game_manager.get_politics_manager().get_ideology_manager(); - TerrainTypeManager const& terrain_type_manager = game_manager.get_map().get_terrain_type_manager(); + TerrainTypeManager const& terrain_type_manager = game_manager.get_map_definition().get_terrain_type_manager(); using enum ProvinceInstance::colony_status_t; static const string_map_t<ProvinceInstance::colony_status_t> colony_status_map { @@ -120,8 +120,8 @@ void ProvinceHistoryManager::reserve_more_province_histories(size_t size) { } } -void ProvinceHistoryManager::lock_province_histories(Map const& map, bool detailed_errors) { - std::vector<ProvinceDefinition> const& provinces = map.get_province_definitions(); +void ProvinceHistoryManager::lock_province_histories(MapDefinition const& map_definition, bool detailed_errors) { + std::vector<ProvinceDefinition> const& provinces = map_definition.get_province_definitions(); std::vector<bool> province_checklist(provinces.size()); for (decltype(province_histories)::value_type const& entry : province_histories) { @@ -231,7 +231,7 @@ bool ProvinceHistoryManager::load_pop_history_file( Logger::error("Attempted to load pop history file after province history registry was locked!"); return false; } - return game_manager.get_map().expect_province_definition_dictionary( + return game_manager.get_map_definition().expect_province_definition_dictionary( [this, &game_manager, date, non_integer_size](ProvinceDefinition const& province, ast::NodeCPtr node) -> bool { ProvinceHistoryMap* province_history = _get_or_make_province_history(province); if (province_history != nullptr) { diff --git a/src/openvic-simulation/history/ProvinceHistory.hpp b/src/openvic-simulation/history/ProvinceHistory.hpp index 27d744d..c4cbd49 100644 --- a/src/openvic-simulation/history/ProvinceHistory.hpp +++ b/src/openvic-simulation/history/ProvinceHistory.hpp @@ -68,7 +68,7 @@ namespace OpenVic { ); }; - struct Map; + struct MapDefinition; struct ProvinceHistoryManager { private: @@ -81,7 +81,7 @@ namespace OpenVic { ProvinceHistoryManager() = default; void reserve_more_province_histories(size_t size); - void lock_province_histories(Map const& map, bool detailed_errors); + void lock_province_histories(MapDefinition const& map_definition, bool detailed_errors); bool is_locked() const; ProvinceHistoryMap const* get_province_history(ProvinceDefinition const* province) const; diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/MapDefinition.cpp index 8f6471e..f3bb619 100644 --- a/src/openvic-simulation/map/Map.cpp +++ b/src/openvic-simulation/map/MapDefinition.cpp @@ -1,10 +1,7 @@ -#include "Map.hpp" +#include "MapDefinition.hpp" -#include <cassert> -#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,29 +9,10 @@ using namespace OpenVic; using namespace OpenVic::NodeTools; -using namespace OpenVic::colour_literals; -Mapmode::Mapmode( - std::string_view new_identifier, index_t new_index, colour_func_t new_colour_func -) : HasIdentifier { new_identifier }, index { new_index }, colour_func { new_colour_func } { - assert(colour_func != nullptr); -} - -const Mapmode Mapmode::ERROR_MAPMODE { - "mapmode_error", 0, [](Map const& map, ProvinceInstance const& province) -> base_stripe_t { - return { 0xFFFF0000_argb, colour_argb_t::null() }; - } -}; +MapDefinition::MapDefinition() : dims { 0, 0 }, max_provinces { ProvinceDefinition::MAX_INDEX } {} -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(); -} - -Map::Map() - : dims { 0, 0 }, max_provinces { ProvinceDefinition::MAX_INDEX }, selected_province { nullptr }, - highest_province_population { 0 }, total_map_population { 0 } {} - -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 ", @@ -71,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(); @@ -108,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; } @@ -147,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 { @@ -275,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; @@ -303,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; @@ -317,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; @@ -363,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; @@ -371,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 ", @@ -404,175 +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::add_mapmode(std::string_view identifier, Mapmode::colour_func_t colour_func) { - if (identifier.empty()) { - Logger::error("Invalid mapmode identifier - empty!"); - return false; - } - if (colour_func == nullptr) { - Logger::error("Mapmode colour function is null for identifier: ", identifier); - return false; - } - return mapmodes.add_item({ identifier, mapmodes.size(), colour_func }); -} - -bool Map::generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) const { - if (target == nullptr) { - Logger::error("Mapmode colour target pointer is null!"); - return false; - } - - bool ret = true; - Mapmode const* mapmode = mapmodes.get_item_by_index(index); - if (mapmode == nullptr) { - // Not an error if mapmodes haven't yet been loaded, - // e.g. if we want to allocate the province colour - // texture before mapmodes are loaded. - if (!(mapmodes.empty() && index == 0)) { - Logger::error("Invalid mapmode index: ", index); - ret = false; - } - mapmode = &Mapmode::ERROR_MAPMODE; - } - - Mapmode::base_stripe_t* target_stripes = reinterpret_cast<Mapmode::base_stripe_t*>(target); - - target_stripes[ProvinceDefinition::NULL_INDEX] = colour_argb_t::null(); - - if (province_instances_are_locked()) { - for (ProvinceInstance const& province : province_instances.get_items()) { - target_stripes[province.get_province_definition().get_index()] = mapmode->get_base_stripe_colours(*this, province); - } - } else { - for (size_t index = ProvinceDefinition::NULL_INDEX + 1; index <= get_province_definition_count(); ++index) { - target_stripes[index] = colour_argb_t::null(); - } - } - - return ret; -} - -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) { @@ -607,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; @@ -647,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); @@ -655,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 { @@ -667,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 { @@ -697,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; @@ -850,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 { @@ -875,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!"); @@ -937,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 { @@ -990,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 d42b3fb..9ec4367 100644 --- a/src/openvic-simulation/map/Map.hpp +++ b/src/openvic-simulation/map/MapDefinition.hpp @@ -1,57 +1,30 @@ #pragma once #include <filesystem> -#include <functional> +#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 Mapmode : HasIdentifier { - friend struct Map; - - /* Bottom 32 bits are the base colour, top 32 are the stripe colour, both in ARGB format with the alpha channels - * controlling interpolation with the terrain colour (0 = all terrain, 255 = all corresponding RGB) */ - struct base_stripe_t { - colour_argb_t base_colour; - colour_argb_t stripe_colour; - constexpr base_stripe_t(colour_argb_t base, colour_argb_t stripe) - : 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 index_t = size_t; - - private: - const index_t PROPERTY(index); - const colour_func_t colour_func; - - Mapmode(std::string_view new_identifier, index_t new_index, colour_func_t new_colour_func); - - public: - static const Mapmode ERROR_MAPMODE; - - Mapmode(Mapmode&&) = default; - - base_stripe_t get_base_stripe_colours(Map const& map, ProvinceInstance const& province) const; - }; - - 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 { @@ -59,13 +32,12 @@ 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<Mapmode> IDENTIFIER_REGISTRY(mapmode); IdentifierRegistry<Climate> IDENTIFIER_REGISTRY(climate); IdentifierRegistry<Continent> IDENTIFIER_REGISTRY(continent); ProvinceSet water_provinces; @@ -76,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; @@ -136,30 +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 add_mapmode(std::string_view identifier, Mapmode::colour_func_t colour_func); - - /* The mapmode colour image contains of a list of base colours and stripe colours. Each colour is four bytes - * in RGBA format, with the alpha value being used to interpolate with the terrain colour, so A = 0 is fully terrain - * 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(Mapmode::index_t index, uint8_t* target) 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); - 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 new file mode 100644 index 0000000..6f8e606 --- /dev/null +++ b/src/openvic-simulation/map/Mapmode.cpp @@ -0,0 +1,293 @@ +#include "Mapmode.hpp" + +#include <cassert> + +#include "openvic-simulation/country/Country.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; +using namespace OpenVic::colour_literals; + +Mapmode::Mapmode( + std::string_view new_identifier, index_t new_index, colour_func_t new_colour_func +) : HasIdentifier { new_identifier }, index { new_index }, colour_func { new_colour_func } { + assert(colour_func != nullptr); +} + +const Mapmode Mapmode::ERROR_MAPMODE { + "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( + 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) { + if (identifier.empty()) { + Logger::error("Invalid mapmode identifier - empty!"); + return false; + } + if (colour_func == nullptr) { + Logger::error("Mapmode colour function is null for identifier: ", identifier); + return false; + } + return mapmodes.add_item({ identifier, mapmodes.size(), colour_func }); +} + +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; + } + + bool ret = true; + Mapmode const* mapmode = get_mapmode_by_index(index); + if (mapmode == nullptr) { + // Not an error if mapmodes haven't yet been loaded, + // e.g. if we want to allocate the province colour + // texture before mapmodes are loaded. + if (!(mapmodes_empty() && index == 0)) { + Logger::error("Invalid mapmode index: ", index); + ret = false; + } + mapmode = &Mapmode::ERROR_MAPMODE; + } + + Mapmode::base_stripe_t* target_stripes = reinterpret_cast<Mapmode::base_stripe_t*>(target); + + target_stripes[ProvinceDefinition::NULL_INDEX] = colour_argb_t::null(); + + 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_instance.get_map_definition().get_province_definition_count(); + ++index + ) { + target_stripes[index] = colour_argb_t::null(); + } + } + + return ret; +} + +static constexpr colour_argb_t::value_type ALPHA_VALUE = colour_argb_t::max_value; +/* White default colour, used in mapmodes including political, revolt risk and party loyaly. */ +static constexpr colour_argb_t DEFAULT_COLOUR_WHITE = (0xFFFFFF_argb).with_alpha(ALPHA_VALUE); +/* Grey default colour, used in mapmodes including diplomatic, administrative and colonial, recruitment, + * national focus, RGO, population density, sphere of influence, ranking and migration. */ +static constexpr colour_argb_t DEFAULT_COLOUR_GREY = (0x7F7F7F_argb).with_alpha(ALPHA_VALUE); + +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](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& { + if constexpr (std::same_as<P, ProvinceDefinition>) { + return province_definition; + } else { + return province; + } + }().*get_item)(); + + if (item != nullptr) { + return colour_argb_t { item->get_colour(), ALPHA_VALUE }; + } else if (!province_definition.is_water()) { + return DEFAULT_COLOUR_WHITE; + } else { + return colour_argb_t::null(); + } + }; +} + +template<utility::is_derived_from_specialization_of<_HasColour> T> +static constexpr Mapmode::base_stripe_t shaded_mapmode(fixed_point_map_t<T const*> const& map) { + const std::pair<fixed_point_map_const_iterator_t<T const*>, fixed_point_map_const_iterator_t<T const*>> largest = + get_largest_two_items(map); + if (largest.first != map.end()) { + const colour_argb_t base_colour = colour_argb_t { largest.first->first->get_colour(), ALPHA_VALUE }; + if (largest.second != map.end()) { + /* If second largest is at least a third... */ + if (largest.second->second * 3 >= get_total(map)) { + const colour_argb_t stripe_colour = colour_argb_t { largest.second->first->get_colour(), ALPHA_VALUE }; + return { base_colour, stripe_colour }; + } + } + return base_colour; + } + return colour_argb_t::null(); +} + +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](MapInstance const&, ProvinceInstance const& province) -> Mapmode::base_stripe_t { + return shaded_mapmode((province.*get_map)()); + }; +} + +bool MapmodeManager::setup_mapmodes() { + bool ret = true; + + using mapmode_t = std::pair<std::string, Mapmode::colour_func_t>; + const std::vector<mapmode_t> mapmodes { + { + "mapmode_terrain", + [](MapInstance const&, ProvinceInstance const& province) -> Mapmode::base_stripe_t { + return colour_argb_t::null(); + } + }, + { + "mapmode_political", get_colour_mapmode(&ProvinceInstance::get_owner) + }, + { + /* TEST MAPMODE, TO BE REMOVED */ + "mapmode_province", + [](MapInstance const&, ProvinceInstance const& province) -> Mapmode::base_stripe_t { + return colour_argb_t { province.get_province_definition().get_colour(), ALPHA_VALUE }; + } + }, + { + "mapmode_region", get_colour_mapmode(&ProvinceDefinition::get_region) + }, + { + /* TEST MAPMODE, TO BE REMOVED */ + "mapmode_index", + [](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_instance.get_map_definition().get_province_definition_count() + 1 + ); + return colour_argb_t::fill_as(f).with_alpha(ALPHA_VALUE); + } + }, + { + /* Non-vanilla mapmode, still of use in game. */ + "mapmode_terrain_type", get_colour_mapmode(&ProvinceInstance::get_terrain_type) + }, + { + "mapmode_rgo", get_colour_mapmode(&ProvinceInstance::get_rgo) + }, + { + "mapmode_infrastructure", + [](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( + railroad->get_level(), railroad->get_building_type().get_max_level() + 1, 0.5f, 1.0f + ); + switch (railroad->get_expansion_state()) { + case BuildingInstance::ExpansionState::CannotExpand: + return colour_argb_t { val, 0, 0, ALPHA_VALUE }; + case BuildingInstance::ExpansionState::CanExpand: + return colour_argb_t { 0, 0, val, ALPHA_VALUE }; + default: + return colour_argb_t { 0, val, 0, ALPHA_VALUE }; + } + } + return colour_argb_t::null(); + } + }, + { + "mapmode_population", + [](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_instance.get_highest_province_population() + 1, 0.1f, 1.0f + ); + return colour_argb_t { 0, val, 0, ALPHA_VALUE }; + } else { + return colour_argb_t::null(); + } + } + }, + { + "mapmode_culture", shaded_mapmode(&ProvinceInstance::get_culture_distribution) + }, + { + /* Non-vanilla mapmode, still of use in game. */ + "mapmode_religion", shaded_mapmode(&ProvinceInstance::get_religion_distribution) + }, + { + /* TEST MAPMODE, TO BE REMOVED */ + "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(); + + if (selected_province == &province) { + return (0xFFFFFF_argb).with_alpha(ALPHA_VALUE); + } + + ProvinceDefinition const* province_definition = &province.get_province_definition(); + + colour_argb_t base = colour_argb_t::null(), stripe = colour_argb_t::null(); + ProvinceDefinition::adjacency_t const* adj = + selected_province_definition.get_adjacency_to(province_definition); + + if (adj != nullptr) { + colour_argb_t::integer_type base_int; + switch (adj->get_type()) { + using enum ProvinceDefinition::adjacency_t::type_t; + case LAND: base_int = 0x00FF00; break; + case WATER: base_int = 0x0000FF; break; + case COASTAL: base_int = 0xF9D199; break; + case IMPASSABLE: base_int = 0x8B4513; break; + case STRAIT: base_int = 0x00FFFF; break; + case CANAL: base_int = 0x888888; break; + default: base_int = 0xFF0000; break; + } + base = colour_argb_t::from_integer(base_int).with_alpha(ALPHA_VALUE); + stripe = base; + } + + if (selected_province_definition.has_adjacency_going_through(province_definition)) { + stripe = (0xFFFF00_argb).with_alpha(ALPHA_VALUE); + } + + return { base, stripe }; + } + + return colour_argb_t::null(); + } + }, + { + "mapmode_port", [](MapInstance const&, ProvinceInstance const& province) -> Mapmode::base_stripe_t { + ProvinceDefinition const& province_definition = province.get_province_definition(); + + if (province_definition.has_port()) { + return (0xFFFFFF_argb).with_alpha(ALPHA_VALUE); + } else if (!province_definition.is_water()) { + return (0x333333_argb).with_alpha(ALPHA_VALUE); + } else { + return colour_argb_t::null(); + } + } + } + }; + + for (mapmode_t const& mapmode : mapmodes) { + ret &= add_mapmode(mapmode.first, mapmode.second); + } + lock_mapmodes(); + + return ret; +} diff --git a/src/openvic-simulation/map/Mapmode.hpp b/src/openvic-simulation/map/Mapmode.hpp new file mode 100644 index 0000000..d514f7a --- /dev/null +++ b/src/openvic-simulation/map/Mapmode.hpp @@ -0,0 +1,61 @@ +#pragma once + +#include <functional> + +#include "openvic-simulation/types/Colour.hpp" +#include "openvic-simulation/types/HasIdentifier.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" + +namespace OpenVic { + struct MapmodeManager; + struct MapInstance; + struct ProvinceInstance; + + struct Mapmode : HasIdentifier { + friend struct MapmodeManager; + + /* Bottom 32 bits are the base colour, top 32 are the stripe colour, both in ARGB format with the alpha channels + * controlling interpolation with the terrain colour (0 = all terrain, 255 = all corresponding RGB) */ + struct base_stripe_t { + colour_argb_t base_colour; + colour_argb_t stripe_colour; + constexpr base_stripe_t(colour_argb_t base, colour_argb_t stripe) + : 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(MapInstance const&, ProvinceInstance const&)>; + using index_t = size_t; + + private: + const index_t PROPERTY(index); + const colour_func_t colour_func; + + Mapmode(std::string_view new_identifier, index_t new_index, colour_func_t new_colour_func); + + public: + static const Mapmode ERROR_MAPMODE; + + Mapmode(Mapmode&&) = default; + + base_stripe_t get_base_stripe_colours(MapInstance const& map_instance, ProvinceInstance const& province) const; + }; + + struct MapmodeManager { + private: + IdentifierRegistry<Mapmode> IDENTIFIER_REGISTRY(mapmode); + + public: + MapmodeManager() = default; + + bool add_mapmode(std::string_view identifier, Mapmode::colour_func_t colour_func); + + /* The mapmode colour image contains of a list of base colours and stripe colours. Each colour is four bytes + * in RGBA format, with the alpha value being used to interpolate with the terrain colour, so A = 0 is fully terrain + * 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(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(); diff --git a/src/openvic-simulation/military/Deployment.cpp b/src/openvic-simulation/military/Deployment.cpp index 4550108..813cd94 100644 --- a/src/openvic-simulation/military/Deployment.cpp +++ b/src/openvic-simulation/military/Deployment.cpp @@ -126,7 +126,7 @@ bool DeploymentManager::load_oob_file( const bool ret = expect_dictionary_keys( "name", ONE_EXACTLY, expect_string(assign_variable_callback(army_name)), - "location", ONE_EXACTLY, game_manager.get_map().expect_province_definition_identifier( + "location", ONE_EXACTLY, game_manager.get_map_definition().expect_province_definition_identifier( assign_variable_callback_pointer(army_location) ), "regiment", ONE_OR_MORE, [&game_manager, &army_regiments](ast::NodeCPtr node) -> bool { @@ -138,7 +138,7 @@ bool DeploymentManager::load_oob_file( "name", ONE_EXACTLY, expect_string(assign_variable_callback(regiment_name)), "type", ONE_EXACTLY, game_manager.get_military_manager().get_unit_type_manager() .expect_regiment_type_identifier(assign_variable_callback_pointer(regiment_type)), - "home", ZERO_OR_ONE, game_manager.get_map() + "home", ZERO_OR_ONE, game_manager.get_map_definition() .expect_province_definition_identifier(assign_variable_callback_pointer(regiment_home)) )(node); @@ -170,7 +170,7 @@ bool DeploymentManager::load_oob_file( const bool ret = expect_dictionary_keys( "name", ONE_EXACTLY, expect_string(assign_variable_callback(navy_name)), - "location", ONE_EXACTLY, game_manager.get_map().expect_province_definition_identifier( + "location", ONE_EXACTLY, game_manager.get_map_definition().expect_province_definition_identifier( assign_variable_callback_pointer(navy_location) ), "ship", ONE_OR_MORE, [&game_manager, &navy_ships](ast::NodeCPtr node) -> bool { diff --git a/src/openvic-simulation/military/UnitInstance.cpp b/src/openvic-simulation/military/UnitInstance.cpp index 1cd0813..ac9c194 100644 --- a/src/openvic-simulation/military/UnitInstance.cpp +++ b/src/openvic-simulation/military/UnitInstance.cpp @@ -3,7 +3,7 @@ #include <vector> #include "openvic-simulation/country/CountryInstance.hpp" -#include "openvic-simulation/map/Map.hpp" +#include "openvic-simulation/map/MapInstance.hpp" #include "openvic-simulation/map/ProvinceInstance.hpp" #include "openvic-simulation/military/Deployment.hpp" @@ -76,7 +76,9 @@ bool UnitInstanceManager::generate_ship(ShipDeployment const& ship_deployment, S return true; } -bool UnitInstanceManager::generate_army(Map& map, CountryInstance& country, ArmyDeployment const& army_deployment) { +bool UnitInstanceManager::generate_army( + MapInstance& map_instance, CountryInstance& country, ArmyDeployment const& army_deployment +) { if (army_deployment.get_regiments().empty()) { Logger::error( "Trying to generate army \"", army_deployment.get_name(), "\" with no regiments for country \"", @@ -117,12 +119,14 @@ bool UnitInstanceManager::generate_army(Map& map, CountryInstance& country, Army armies.push_back({ army_deployment.get_name(), std::move(army_regiments), nullptr, &country }); - armies.back().set_position(map.get_province_instance_from_const(army_deployment.get_location())); + armies.back().set_position(map_instance.get_province_instance_from_const(army_deployment.get_location())); return ret; } -bool UnitInstanceManager::generate_navy(Map& map, CountryInstance& country, NavyDeployment const& navy_deployment) { +bool UnitInstanceManager::generate_navy( + MapInstance& map_instance, CountryInstance& country, NavyDeployment const& navy_deployment +) { if (navy_deployment.get_ships().empty()) { Logger::error( "Trying to generate navy \"", navy_deployment.get_name(), "\" with no ships for country \"", @@ -163,12 +167,14 @@ bool UnitInstanceManager::generate_navy(Map& map, CountryInstance& country, Navy navies.push_back({ navy_deployment.get_name(), std::move(navy_ships), nullptr, &country }); - navies.back().set_position(map.get_province_instance_from_const(navy_deployment.get_location())); + navies.back().set_position(map_instance.get_province_instance_from_const(navy_deployment.get_location())); return ret; } -bool UnitInstanceManager::generate_deployment(Map& map, CountryInstance& country, Deployment const* deployment) { +bool UnitInstanceManager::generate_deployment( + MapInstance& map_instance, CountryInstance& country, Deployment const* deployment +) { if (deployment == nullptr) { Logger::error("Trying to generate null deployment for ", country.get_identifier()); return false; @@ -179,11 +185,11 @@ bool UnitInstanceManager::generate_deployment(Map& map, CountryInstance& country bool ret = true; for (ArmyDeployment const& army_deployment : deployment->get_armies()) { - ret &= generate_army(map, country, army_deployment); + ret &= generate_army(map_instance, country, army_deployment); } for (NavyDeployment const& navy_deployment : deployment->get_navies()) { - ret &= generate_navy(map, country, navy_deployment); + ret &= generate_navy(map_instance, country, navy_deployment); } return ret; diff --git a/src/openvic-simulation/military/UnitInstance.hpp b/src/openvic-simulation/military/UnitInstance.hpp index a3a53d0..a1437ef 100644 --- a/src/openvic-simulation/military/UnitInstance.hpp +++ b/src/openvic-simulation/military/UnitInstance.hpp @@ -186,7 +186,7 @@ namespace OpenVic { struct RegimentDeployment; struct ShipDeployment; - struct Map; + struct MapInstance; struct ArmyDeployment; struct NavyDeployment; struct Deployment; @@ -201,10 +201,10 @@ namespace OpenVic { bool generate_regiment(RegimentDeployment const& regiment_deployment, RegimentInstance*& regiment); bool generate_ship(ShipDeployment const& ship_deployment, ShipInstance*& ship); - bool generate_army(Map& map, CountryInstance& country, ArmyDeployment const& army_deployment); - bool generate_navy(Map& map, CountryInstance& country, NavyDeployment const& navy_deployment); + bool generate_army(MapInstance& map_instance, CountryInstance& country, ArmyDeployment const& army_deployment); + bool generate_navy(MapInstance& map_instance, CountryInstance& country, NavyDeployment const& navy_deployment); public: - bool generate_deployment(Map& map, CountryInstance& country, Deployment const* deployment); + bool generate_deployment(MapInstance& map_instance, CountryInstance& country, Deployment const* deployment); }; } diff --git a/src/openvic-simulation/scripts/Condition.cpp b/src/openvic-simulation/scripts/Condition.cpp index 5bf4067..238d0f8 100644 --- a/src/openvic-simulation/scripts/Condition.cpp +++ b/src/openvic-simulation/scripts/Condition.cpp @@ -361,7 +361,7 @@ bool ConditionManager::setup_conditions(GameManager const& game_manager) { ); import_identifiers( - game_manager.get_map().get_region_identifiers(), + game_manager.get_map_definition().get_region_identifiers(), GROUP, COUNTRY, STATE, @@ -370,7 +370,7 @@ bool ConditionManager::setup_conditions(GameManager const& game_manager) { ); import_identifiers( - game_manager.get_map().get_province_definition_identifiers(), + game_manager.get_map_definition().get_province_definition_identifiers(), GROUP, COUNTRY, PROVINCE, @@ -485,8 +485,8 @@ callback_t<std::string_view> ConditionManager::expect_parse_identifier( EXPECT_CALL_PLACEHOLDER(COUNTRY_FLAG); EXPECT_CALL_PLACEHOLDER(PROVINCE_FLAG); EXPECT_CALL(COUNTRY_TAG, country, game_manager.get_country_manager(), "THIS", "FROM", "OWNER"); - EXPECT_CALL(PROVINCE_ID, province_definition, game_manager.get_map(), "THIS", "FROM"); - EXPECT_CALL(REGION, region, game_manager.get_map()); + EXPECT_CALL(PROVINCE_ID, province_definition, game_manager.get_map_definition(), "THIS", "FROM"); + EXPECT_CALL(REGION, region, game_manager.get_map_definition()); EXPECT_CALL(IDEOLOGY, ideology, game_manager.get_politics_manager().get_ideology_manager()); EXPECT_CALL(REFORM_GROUP, reform_group, game_manager.get_politics_manager().get_issue_manager()); EXPECT_CALL(REFORM, reform, game_manager.get_politics_manager().get_issue_manager()); @@ -508,9 +508,9 @@ callback_t<std::string_view> ConditionManager::expect_parse_identifier( EXPECT_CALL(MODIFIER, static_modifier, game_manager.get_modifier_manager()); EXPECT_CALL(NATIONAL_VALUE, national_value, game_manager.get_politics_manager().get_national_value_manager()); EXPECT_CALL(CULTURE_UNION, country, game_manager.get_country_manager(), "THIS", "FROM", "THIS_UNION"); - EXPECT_CALL(CONTINENT, continent, game_manager.get_map()); + EXPECT_CALL(CONTINENT, continent, game_manager.get_map_definition()); EXPECT_CALL(CRIME, crime_modifier, game_manager.get_crime_manager()); - EXPECT_CALL(TERRAIN, terrain_type, game_manager.get_map().get_terrain_type_manager()); + EXPECT_CALL(TERRAIN, terrain_type, game_manager.get_map_definition().get_terrain_type_manager()); #undef EXPECT_CALL #undef EXPECT_CALL_PLACEHOLDER |