From 2f5706fd5ef6bbdee54c82860e03f00a96751693 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Tue, 2 Jan 2024 15:24:06 +0100 Subject: feat: calculate province distance using new sqrt functions --- src/openvic-simulation/map/Province.cpp | 156 -------------------------------- 1 file changed, 156 deletions(-) (limited to 'src/openvic-simulation/map/Province.cpp') diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp index 1285b0d..a9bf329 100644 --- a/src/openvic-simulation/map/Province.cpp +++ b/src/openvic-simulation/map/Province.cpp @@ -185,166 +185,10 @@ bool Province::has_adjacency_going_through(Province const* province) const { ); } -/* 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 Province::add_standard_adjacency(Province& from, Province& to) { - if (from == to) { - return false; - } - - const bool from_needs_adjacency = !from.is_adjacent_to(&to); - const bool to_needs_adjacency = !to.is_adjacent_to(&from); - - if (!from_needs_adjacency && !to_needs_adjacency) { - return false; - } - - const distance_t distance = calculate_distance_between(from, to); - - using enum adjacency_t::type_t; - - /* Default land-to-land adjacency */ - adjacency_t::type_t type = LAND; - if (from.is_water() != to.is_water()) { - /* Land-to-water adjacency */ - type = COASTAL; - - /* Mark the land province as coastal */ - from.coastal = !from.is_water(); - to.coastal = !to.is_water(); - } else if (from.is_water()) { - /* Water-to-water adjacency */ - type = WATER; - } - - if (from_needs_adjacency) { - from.adjacencies.emplace_back(&to, distance, type, nullptr, 0); - } - if (to_needs_adjacency) { - to.adjacencies.emplace_back(&from, distance, type, nullptr, 0); - } - return true; -} - -bool Province::add_special_adjacency( - Province& from, Province& to, adjacency_t::type_t type, Province const* through, adjacency_t::data_t data -) { - if (from == to) { - Logger::error("Trying to add ", adjacency_t::get_type_name(type), " adjacency from province ", from, " to itself!"); - return false; - } - - using enum adjacency_t::type_t; - - /* Check end points */ - switch (type) { - case LAND: - case IMPASSABLE: - case STRAIT: - if (from.is_water() || to.is_water()) { - Logger::error(adjacency_t::get_type_name(type), " adjacency from ", from, " to ", to, " has water endpoint(s)!"); - return false; - } - break; - case WATER: - case CANAL: - if (!from.is_water() || !to.is_water()) { - Logger::error(adjacency_t::get_type_name(type), " adjacency from ", from, " to ", to, " has land endpoint(s)!"); - return false; - } - break; - case COASTAL: - if (from.is_water() == to.is_water()) { - Logger::error("Coastal adjacency from ", from, " to ", to, " has both land or water endpoints!"); - return false; - } - break; - default: - Logger::error("Invalid adjacency type ", static_cast(type)); - return false; - } - - /* Check through province */ - if (type == STRAIT || type == CANAL) { - const bool water_expected = type == STRAIT; - if (through == nullptr || through->is_water() != water_expected) { - Logger::error( - adjacency_t::get_type_name(type), " adjacency from ", from, " to ", to, " has a ", - (through == nullptr ? "null" : water_expected ? "land" : "water"), " through province ", through - ); - return false; - } - } else if (through != nullptr) { - Logger::warning( - adjacency_t::get_type_name(type), " adjacency from ", from, " to ", to, " has a non-null through province ", - through - ); - through = nullptr; - } - - /* Check canal data */ - if (data != adjacency_t::NO_CANAL && type != CANAL) { - Logger::warning( - adjacency_t::get_type_name(type), " adjacency from ", from, " to ", to, " has invalid data ", - static_cast(data) - ); - data = adjacency_t::NO_CANAL; - } - - const distance_t distance = calculate_distance_between(from, to); - - const auto add_adjacency = [distance, type, through, data](Province& from, Province const& to) -> bool { - adjacency_t* existing_adjacency = from.get_adjacency_to(&to); - if (existing_adjacency != nullptr) { - if (type == existing_adjacency->get_type()) { - Logger::warning( - "Adjacency from ", from, " to ", to, " already has type ", adjacency_t::get_type_name(type), "!" - ); - if (type != STRAIT && type != CANAL) { - /* Straits and canals might change through or data, otherwise we can exit early */ - return true; - } - } - if (type != IMPASSABLE && type != STRAIT && type != CANAL) { - Logger::error( - "Provinces ", from, " and ", to, " already have an existing ", - adjacency_t::get_type_name(existing_adjacency->get_type()), " adjacency, cannot create a ", - adjacency_t::get_type_name(type), " adjacency!" - ); - return false; - } - if (type != existing_adjacency->get_type() && existing_adjacency->get_type() != (type == CANAL ? WATER : LAND)) { - Logger::error( - "Cannot convert ", adjacency_t::get_type_name(existing_adjacency->get_type()), " adjacency from ", from, - " to ", to, " to type ", adjacency_t::get_type_name(type), "!" - ); - return false; - } - *existing_adjacency = { &to, distance, type, through, data }; - return true; - } else if (type == IMPASSABLE) { - Logger::warning( - "Provinces ", from, " and ", to, " do not have an existing adjacency to make impassable!" - ); - return true; - } else { - from.adjacencies.emplace_back(&to, distance, type, through, data); - return true; - } - }; - - return add_adjacency(from, to) & add_adjacency(to, from); -} - fvec2_t Province::get_unit_position() const { return positions.unit.value_or(positions.centre); } -Province::distance_t Province::calculate_distance_between(Province const& from, Province const& to) { - const fvec2_t distance_vector = to.get_unit_position() - from.get_unit_position(); - return distance_vector.length_squared(); // TODO - replace with length using deterministic fixed point square root -} - bool Province::reset(BuildingTypeManager const& building_type_manager) { terrain_type = default_terrain_type; life_rating = 0; -- cgit v1.2.3-56-ga3b1