From a7f125a5f276e2951d1236fe88e32c5c08271504 Mon Sep 17 00:00:00 2001 From: hop311 Date: Sun, 14 Apr 2024 23:55:48 +0100 Subject: Province port adjacency calculation --- src/openvic-simulation/map/Province.cpp | 35 +++++++++++++++++++++++++++++---- src/openvic-simulation/map/Province.hpp | 1 + 2 files changed, 32 insertions(+), 4 deletions(-) (limited to 'src/openvic-simulation/map') diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp index 2d1c2d1..89ddd39 100644 --- a/src/openvic-simulation/map/Province.cpp +++ b/src/openvic-simulation/map/Province.cpp @@ -11,9 +11,10 @@ Province::Province( std::string_view new_identifier, colour_t new_colour, index_t new_index ) : HasIdentifierAndColour { new_identifier, new_colour, true }, index { new_index }, region { nullptr }, climate { nullptr }, continent { nullptr }, on_map { false }, has_region { false }, water { false }, coastal { false }, - port { false }, default_terrain_type { nullptr }, positions {}, terrain_type { nullptr }, life_rating { 0 }, - colony_status { colony_status_t::STATE }, state { nullptr }, owner { nullptr }, controller { nullptr }, slave { false }, - crime { nullptr }, rgo { nullptr }, buildings { "buildings", false }, total_population { 0 } { + port { false }, port_adjacent_province { nullptr }, default_terrain_type { nullptr }, positions {}, + terrain_type { nullptr }, life_rating { 0 }, colony_status { colony_status_t::STATE }, state { nullptr }, + owner { nullptr }, controller { nullptr }, slave { false }, crime { nullptr }, rgo { nullptr }, + buildings { "buildings", false }, total_population { 0 } { assert(index != NULL_INDEX); } @@ -62,7 +63,33 @@ bool Province::load_positions(Map const& map, BuildingTypeManager const& buildin "building_nudge", ZERO_OR_ONE, success_callback )(root); - port = coastal && positions.building_position.contains(building_type_manager.get_port_building_type()); + if (coastal) { + fvec2_t const* port_position = get_building_position(building_type_manager.get_port_building_type()); + if (port_position != nullptr) { + const fixed_point_t rotation = get_building_rotation(building_type_manager.get_port_building_type()); + + /* At 0 rotation the port faces west, as rotation increases the port rotates anti-clockwise. */ + const fvec2_t port_dir { -rotation.cos(), rotation.sin() }; + const ivec2_t port_facing_position = static_cast(*port_position + port_dir / 4); + + Province const* province = map.get_province_at(port_facing_position); + + if (province != nullptr) { + if (province->is_water() && is_adjacent_to(province)) { + port = true; + port_adjacent_province = province; + } else { + /* Expected provinces with invalid ports: 39, 296, 1047, 1406, 2044 */ + Logger::warning( + "Invalid port for province ", get_identifier(), ": facing province ", province, + " which has: water = ", province->is_water(), ", adjacent = ", is_adjacent_to(province) + ); + } + } else { + Logger::warning("Invalid port for province ", get_identifier(), ": facing null province!"); + } + } + } return ret; } diff --git a/src/openvic-simulation/map/Province.hpp b/src/openvic-simulation/map/Province.hpp index d5b5ad6..cfe5ed6 100644 --- a/src/openvic-simulation/map/Province.hpp +++ b/src/openvic-simulation/map/Province.hpp @@ -96,6 +96,7 @@ namespace OpenVic { bool PROPERTY_CUSTOM_PREFIX(water, is); bool PROPERTY_CUSTOM_PREFIX(coastal, is); bool PROPERTY_CUSTOM_PREFIX(port, has); + Province const* PROPERTY(port_adjacent_province); /* Terrain type calculated from terrain image */ TerrainType const* PROPERTY(default_terrain_type); -- cgit v1.2.3-56-ga3b1