diff options
author | wvpm <24685035+wvpm@users.noreply.github.com> | 2023-12-13 21:21:10 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-13 21:21:10 +0100 |
commit | 2e4110b26281605dde3e10f2183572d22bf773fc (patch) | |
tree | 9ba33c2c8800a11ef59acbba711a7230dcf2dc05 | |
parent | e79ad08e28c07bac5d28d0653cbe374cd51e6bbe (diff) | |
parent | 2c383640d699680e99c79f0aa2601df9e0843f00 (diff) |
Merge pull request #96 from OpenVicProject/calculate_distances
Calculate distance between adjacencies
-rw-r--r-- | src/openvic-simulation/map/Map.cpp | 8 | ||||
-rw-r--r-- | src/openvic-simulation/map/Province.cpp | 14 | ||||
-rw-r--r-- | src/openvic-simulation/map/Province.hpp | 10 |
3 files changed, 25 insertions, 7 deletions
diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp index b272f1f..1e68cc6 100644 --- a/src/openvic-simulation/map/Map.cpp +++ b/src/openvic-simulation/map/Map.cpp @@ -612,10 +612,12 @@ bool Map::load_map_images(fs::path const& province_path, fs::path const& terrain bool Map::_generate_province_adjacencies() { bool changed = false; - auto generate_adjacency = [&](Province* cur, size_t x, size_t y) -> bool { + auto generate_adjacency = [&](Province* current, size_t x, size_t y) -> bool { Province* neighbour = get_province_by_index(province_shape_image[x + y * width].index); - if (neighbour != nullptr && cur != neighbour) { - return cur->add_adjacency(neighbour, 0, 0) | neighbour->add_adjacency(cur, 0, 0); + if (neighbour != nullptr && current != neighbour) { + const Province::distance_t distance = current->calculate_distance_to(neighbour); + const Province::flags_t flags = 0; + return current->add_adjacency(neighbour, distance, flags) | neighbour->add_adjacency(current, distance, flags); } return false; }; diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp index 8094463..717ef35 100644 --- a/src/openvic-simulation/map/Province.cpp +++ b/src/openvic-simulation/map/Province.cpp @@ -121,7 +121,7 @@ Province::adjacency_t::adjacency_t(Province const* province, distance_t distance assert(province != nullptr); } -bool Province::is_adjacent_to(Province const* province) { +bool Province::is_adjacent_to(Province const* province) const { for (adjacency_t adj : adjacencies) { if (adj.province == province) { return true; @@ -142,6 +142,18 @@ bool Province::add_adjacency(Province const* province, distance_t distance, flag return true; } +fvec2_t Province::get_unit_position() const { + return positions.unit.value_or(positions.center); +} + +Province::distance_t Province::calculate_distance_to(Province const* province) const { + const fvec2_t my_unit_position = get_unit_position(); + const fvec2_t other_unit_position = province->get_unit_position(); + const fvec2_t distance_vector = other_unit_position - my_unit_position; + const fixed_point_t distance = distance_vector.length_squared(); + return static_cast<Province::distance_t>(distance); +} + bool Province::reset(BuildingTypeManager const& building_type_manager) { terrain_type = default_terrain_type; life_rating = 0; diff --git a/src/openvic-simulation/map/Province.hpp b/src/openvic-simulation/map/Province.hpp index 37ecdaa..2f8068e 100644 --- a/src/openvic-simulation/map/Province.hpp +++ b/src/openvic-simulation/map/Province.hpp @@ -26,7 +26,7 @@ namespace OpenVic { using index_t = uint16_t; using life_rating_t = int8_t; - using distance_t = uint16_t; + using distance_t = fixed_point_t; using flags_t = uint16_t; enum struct colony_status_t : uint8_t { STATE, PROTECTORATE, COLONY }; @@ -47,7 +47,7 @@ namespace OpenVic { fvec2_t text; fixed_point_t text_rotation; fixed_point_t text_scale; - fvec2_t unit; + std::optional<fvec2_t> unit; fvec2_t city; fvec2_t factory; fvec2_t building_construction; @@ -90,6 +90,8 @@ namespace OpenVic { fixed_point_map_t<Culture const*> PROPERTY(culture_distribution); fixed_point_map_t<Religion const*> PROPERTY(religion_distribution); + fvec2_t get_unit_position() const; + Province(std::string_view new_identifier, colour_t new_colour, index_t new_index); public: @@ -109,9 +111,11 @@ namespace OpenVic { void update_state(Date today); void tick(Date today); - bool is_adjacent_to(Province const* province); + bool is_adjacent_to(Province const* province) const; bool add_adjacency(Province const* province, distance_t distance, flags_t flags); + distance_t calculate_distance_to(Province const* province) const; + bool reset(BuildingTypeManager const& building_type_manager); bool apply_history_to_province(ProvinceHistoryEntry const* entry); }; |