1
0
mirror of https://github.com/OpenTTD/OpenTTD.git synced 2025-08-31 18:39:10 +00:00

Compare commits

...

2 Commits

3 changed files with 21 additions and 33 deletions

View File

@@ -19,10 +19,11 @@
#include "../ship.h"
#include "../debug.h"
#include "../3rdparty/fmt/ranges.h"
#include "../core/convertible_through_base.hpp"
#include "../safeguards.h"
using TWaterRegionTraversabilityBits = uint16_t;
constexpr TWaterRegionPatchLabel FIRST_REGION_LABEL = 1;
constexpr TWaterRegionPatchLabel FIRST_REGION_LABEL{1};
static_assert(sizeof(TWaterRegionTraversabilityBits) * 8 == WATER_REGION_EDGE_LENGTH);
static_assert(sizeof(TWaterRegionPatchLabel) == sizeof(uint8_t)); // Important for the hash calculation.
@@ -36,7 +37,7 @@ static inline int GetWaterRegionY(TileIndex tile) { return TileY(tile) / WATER_R
static inline int GetWaterRegionMapSizeX() { return Map::SizeX() / WATER_REGION_EDGE_LENGTH; }
static inline int GetWaterRegionMapSizeY() { return Map::SizeY() / WATER_REGION_EDGE_LENGTH; }
static inline TWaterRegionIndex GetWaterRegionIndex(int region_x, int region_y) { return GetWaterRegionMapSizeX() * region_y + region_x; }
static inline TWaterRegionIndex GetWaterRegionIndex(int region_x, int region_y) { return TWaterRegionIndex(GetWaterRegionMapSizeX() * region_y + region_x); }
static inline TWaterRegionIndex GetWaterRegionIndex(TileIndex tile) { return GetWaterRegionIndex(GetWaterRegionX(tile), GetWaterRegionY(tile)); }
using TWaterRegionPatchLabelArray = std::array<TWaterRegionPatchLabel, WATER_REGION_NUMBER_OF_TILES>;
@@ -50,7 +51,7 @@ class WaterRegionData {
std::array<TWaterRegionTraversabilityBits, DIAGDIR_END> edge_traversability_bits{};
std::unique_ptr<TWaterRegionPatchLabelArray> tile_patch_labels; // Tile patch labels, this may be nullptr in the following trivial cases: region is invalid, region is only land (0 patches), region is only water (1 patch)
bool has_cross_region_aqueducts = false;
TWaterRegionPatchLabel number_of_patches = 0; // 0 = no water, 1 = one single patch of water, etc...
TWaterRegionPatchLabel::BaseType number_of_patches{0}; // 0 = no water, 1 = one single patch of water, etc...
};
/**
@@ -183,7 +184,7 @@ public:
if (increase_label) current_label++;
}
this->data.number_of_patches = highest_assigned_label;
this->data.number_of_patches = highest_assigned_label.base();
if (this->NumberOfPatches() == 0 || (this->NumberOfPatches() == 1 &&
std::all_of(this->data.tile_patch_labels->begin(), this->data.tile_patch_labels->end(), [](TWaterRegionPatchLabel label) { return label == FIRST_REGION_LABEL; }))) {
@@ -221,8 +222,8 @@ public:
}
};
std::vector<WaterRegionData> _water_region_data;
std::vector<bool> _is_water_region_valid;
ReferenceThroughBaseContainer<std::vector<WaterRegionData>> _water_region_data;
ReferenceThroughBaseContainer<std::vector<bool>> _is_water_region_valid;
static TileIndex GetTileIndexFromLocalCoordinate(int region_x, int region_y, int local_x, int local_y)
{
@@ -274,7 +275,7 @@ static TWaterRegionIndex GetWaterRegionIndex(const WaterRegionDesc &water_region
*/
int CalculateWaterRegionPatchHash(const WaterRegionPatchDesc &water_region_patch)
{
return water_region_patch.label | GetWaterRegionIndex(water_region_patch) << 8;
return water_region_patch.label.base() | GetWaterRegionIndex(water_region_patch).base() << 8;
}
/**

View File

@@ -10,15 +10,16 @@
#ifndef WATER_REGIONS_H
#define WATER_REGIONS_H
#include "../core/strong_typedef_type.hpp"
#include "../tile_type.h"
#include "../map_func.h"
using TWaterRegionPatchLabel = uint8_t;
using TWaterRegionIndex = uint;
using TWaterRegionIndex = StrongType::Typedef<uint, struct TWaterRegionIndexTag, StrongType::Compare>;
using TWaterRegionPatchLabel = StrongType::Typedef<uint8_t, struct TWaterRegionPatchLabelTag, StrongType::Compare, StrongType::Integer>;
constexpr int WATER_REGION_EDGE_LENGTH = 16;
constexpr int WATER_REGION_NUMBER_OF_TILES = WATER_REGION_EDGE_LENGTH * WATER_REGION_EDGE_LENGTH;
constexpr TWaterRegionPatchLabel INVALID_WATER_REGION_PATCH = 0;
constexpr TWaterRegionPatchLabel INVALID_WATER_REGION_PATCH{0};
/**
* Describes a single interconnected patch of water within a particular water region.

View File

@@ -89,17 +89,10 @@ static int ScriptOrderPositionToRealOrderPosition(VehicleID vehicle_id, ScriptOr
assert(ScriptOrder::IsValidVehicleOrder(vehicle_id, order_position));
int pos = (int)order_position;
int res = (int)order_position;
for (const Order &order : v->Orders()) {
if (order.IsType(OT_IMPLICIT)) {
++res;
} else {
if (pos == 0) break;
--pos;
}
}
return res;
auto orders = v->Orders();
auto real_orders = orders | std::views::filter([](const Order &order) { return !order.IsType(OT_IMPLICIT); });
auto it = std::ranges::next(std::begin(real_orders), order_position, std::end(real_orders));
return static_cast<int>(std::distance(std::begin(orders), it.base()));
}
/**
@@ -111,18 +104,11 @@ static ScriptOrder::OrderPosition RealOrderPositionToScriptOrderPosition(Vehicle
{
const Vehicle *v = ::Vehicle::Get(vehicle_id);
int num_implicit_orders = 0;
int pos = order_position;
for (const Order &order : v->Orders()) {
if (order.IsType(OT_IMPLICIT)) {
++num_implicit_orders;
} else {
if (pos == 0) break;
--pos;
}
}
return static_cast<ScriptOrder::OrderPosition>(order_position - num_implicit_orders);
auto orders = v->Orders();
auto first = std::begin(orders);
auto last = std::ranges::next(first, order_position, std::end(orders));
int num_implicit = static_cast<int>(std::count_if(first, last, [](const Order &order) { return order.IsType(OT_IMPLICIT); }));
return static_cast<ScriptOrder::OrderPosition>(order_position - num_implicit);
}
/* static */ bool ScriptOrder::IsGotoStationOrder(VehicleID vehicle_id, OrderPosition order_position)