1
0
Fork 0

Codechange: Use strong types for water regions. (#14289)

pull/13289/head
Kuhnovic 2025-05-23 20:54:42 +02:00 committed by GitHub
parent 0455627d16
commit 180ec6505b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 10 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.