mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Use strong types for water regions. (#14289)
parent
0455627d16
commit
180ec6505b
|
@ -19,10 +19,11 @@
|
||||||
#include "../ship.h"
|
#include "../ship.h"
|
||||||
#include "../debug.h"
|
#include "../debug.h"
|
||||||
#include "../3rdparty/fmt/ranges.h"
|
#include "../3rdparty/fmt/ranges.h"
|
||||||
|
#include "../core/convertible_through_base.hpp"
|
||||||
#include "../safeguards.h"
|
#include "../safeguards.h"
|
||||||
|
|
||||||
using TWaterRegionTraversabilityBits = uint16_t;
|
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(TWaterRegionTraversabilityBits) * 8 == WATER_REGION_EDGE_LENGTH);
|
||||||
static_assert(sizeof(TWaterRegionPatchLabel) == sizeof(uint8_t)); // Important for the hash calculation.
|
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 GetWaterRegionMapSizeX() { return Map::SizeX() / WATER_REGION_EDGE_LENGTH; }
|
||||||
static inline int GetWaterRegionMapSizeY() { return Map::SizeY() / 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)); }
|
static inline TWaterRegionIndex GetWaterRegionIndex(TileIndex tile) { return GetWaterRegionIndex(GetWaterRegionX(tile), GetWaterRegionY(tile)); }
|
||||||
|
|
||||||
using TWaterRegionPatchLabelArray = std::array<TWaterRegionPatchLabel, WATER_REGION_NUMBER_OF_TILES>;
|
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::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)
|
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;
|
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++;
|
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 &&
|
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; }))) {
|
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;
|
ReferenceThroughBaseContainer<std::vector<WaterRegionData>> _water_region_data;
|
||||||
std::vector<bool> _is_water_region_valid;
|
ReferenceThroughBaseContainer<std::vector<bool>> _is_water_region_valid;
|
||||||
|
|
||||||
static TileIndex GetTileIndexFromLocalCoordinate(int region_x, int region_y, int local_x, int local_y)
|
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)
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -10,15 +10,16 @@
|
||||||
#ifndef WATER_REGIONS_H
|
#ifndef WATER_REGIONS_H
|
||||||
#define WATER_REGIONS_H
|
#define WATER_REGIONS_H
|
||||||
|
|
||||||
|
#include "../core/strong_typedef_type.hpp"
|
||||||
#include "../tile_type.h"
|
#include "../tile_type.h"
|
||||||
#include "../map_func.h"
|
#include "../map_func.h"
|
||||||
|
|
||||||
using TWaterRegionPatchLabel = uint8_t;
|
using TWaterRegionIndex = StrongType::Typedef<uint, struct TWaterRegionIndexTag, StrongType::Compare>;
|
||||||
using TWaterRegionIndex = uint;
|
using TWaterRegionPatchLabel = StrongType::Typedef<uint8_t, struct TWaterRegionPatchLabelTag, StrongType::Compare, StrongType::Integer>;
|
||||||
|
|
||||||
constexpr int WATER_REGION_EDGE_LENGTH = 16;
|
constexpr int WATER_REGION_EDGE_LENGTH = 16;
|
||||||
constexpr int WATER_REGION_NUMBER_OF_TILES = WATER_REGION_EDGE_LENGTH * WATER_REGION_EDGE_LENGTH;
|
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.
|
* Describes a single interconnected patch of water within a particular water region.
|
||||||
|
|
Loading…
Reference in New Issue