Changeset - r28781:ee8d01cc7e15
[Not reviewed]
master
0 3 0
Koen Bussemaker - 2 months ago 2024-02-17 19:15:43
koen_bussemaker@hotmail.com
Codechange: Skip non-water water region patches in neigbor search
3 files changed with 7 insertions and 1 deletions:
0 comments (0 inline, 0 general)
src/pathfinder/water_regions.cpp
Show inline comments
 
@@ -19,13 +19,12 @@
 
#include "follow_track.hpp"
 
#include "ship.h"
 
#include "debug.h"
 

	
 
using TWaterRegionTraversabilityBits = uint16_t;
 
constexpr TWaterRegionPatchLabel FIRST_REGION_LABEL = 1;
 
constexpr TWaterRegionPatchLabel INVALID_WATER_REGION_PATCH = 0;
 

	
 
static_assert(sizeof(TWaterRegionTraversabilityBits) * 8 == WATER_REGION_EDGE_LENGTH);
 
static_assert(sizeof(TWaterRegionPatchLabel) == sizeof(byte)); // Important for the hash calculation.
 

	
 
static inline TrackBits GetWaterTracks(TileIndex tile) { return TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0)); }
 
static inline bool IsAqueductTile(TileIndex tile) { return IsBridgeTile(tile) && GetTunnelBridgeTransportType(tile) == TRANSPORT_WATER; }
 
@@ -318,12 +317,14 @@ void InvalidateWaterRegion(TileIndex til
 
 * @param water_region_patch Water patch within the water region to start searching from
 
 * @param side Side of the water region to look for neigboring patches of water
 
 * @param callback The function that will be called for each neighbor that is found
 
 */
 
static inline void VisitAdjacentWaterRegionPatchNeighbors(const WaterRegionPatchDesc &water_region_patch, DiagDirection side, TVisitWaterRegionPatchCallBack &func)
 
{
 
	if (water_region_patch.label == INVALID_WATER_REGION_PATCH) return;
 

	
 
	const WaterRegion &current_region = GetUpdatedWaterRegion(water_region_patch.x, water_region_patch.y);
 

	
 
	const TileIndexDiffC offset = TileIndexDiffCByDiagDir(side);
 
	const int nx = water_region_patch.x + offset.x;
 
	const int ny = water_region_patch.y + offset.y;
 

	
 
@@ -351,12 +352,13 @@ static inline void VisitAdjacentWaterReg
 
		const TileIndex current_edge_tile = GetEdgeTileCoordinate(water_region_patch.x, water_region_patch.y, side, x_or_y);
 
		const TWaterRegionPatchLabel current_label = current_region.GetLabel(current_edge_tile);
 
		if (current_label != water_region_patch.label) continue;
 

	
 
		const TileIndex neighbor_edge_tile = GetEdgeTileCoordinate(nx, ny, opposite_side, x_or_y);
 
		const TWaterRegionPatchLabel neighbor_label = neighboring_region.GetLabel(neighbor_edge_tile);
 
		assert(neighbor_label != INVALID_WATER_REGION_PATCH);
 
		if (std::find(unique_labels.begin(), unique_labels.end(), neighbor_label) == unique_labels.end()) unique_labels.push_back(neighbor_label);
 
	}
 
	for (TWaterRegionPatchLabel unique_label : unique_labels) func(WaterRegionPatchDesc{ nx, ny, unique_label });
 
}
 

	
 
/**
 
@@ -364,12 +366,14 @@ static inline void VisitAdjacentWaterReg
 
 * each cardinal direction, plus any others that are reachable via aqueducts.
 
 * @param water_region_patch Water patch within the water region to start searching from
 
 * @param callback The function that will be called for each accessible water patch that is found
 
 */
 
void VisitWaterRegionPatchNeighbors(const WaterRegionPatchDesc &water_region_patch, TVisitWaterRegionPatchCallBack &callback)
 
{
 
	if (water_region_patch.label == INVALID_WATER_REGION_PATCH) return;
 

	
 
	const WaterRegion &current_region = GetUpdatedWaterRegion(water_region_patch.x, water_region_patch.y);
 

	
 
	/* Visit adjacent water region patches in each cardinal direction */
 
	for (DiagDirection side = DIAGDIR_BEGIN; side < DIAGDIR_END; side++) VisitAdjacentWaterRegionPatchNeighbors(water_region_patch, side, callback);
 

	
 
	/* Visit neigboring water patches accessible via cross-region aqueducts */
src/pathfinder/water_regions.h
Show inline comments
 
@@ -15,12 +15,13 @@
 

	
 
using TWaterRegionPatchLabel = uint8_t;
 
using TWaterRegionIndex = uint;
 

	
 
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;
 

	
 
/**
 
 * Describes a single interconnected patch of water within a particular water region.
 
 */
 
struct WaterRegionPatchDesc
 
{
src/pathfinder/yapf/yapf_ship_regions.cpp
Show inline comments
 
@@ -99,12 +99,13 @@ protected:
 
private:
 
	std::vector<CYapfRegionPatchNodeKey> m_origin_keys;
 

	
 
public:
 
	void AddOrigin(const WaterRegionPatchDesc &water_region_patch)
 
	{
 
		if (water_region_patch.label == INVALID_WATER_REGION_PATCH) return;
 
		if (!HasOrigin(water_region_patch)) m_origin_keys.push_back(CYapfRegionPatchNodeKey{ water_region_patch });
 
	}
 

	
 
	bool HasOrigin(const WaterRegionPatchDesc &water_region_patch)
 
	{
 
		return std::find(m_origin_keys.begin(), m_origin_keys.end(), CYapfRegionPatchNodeKey{ water_region_patch }) != m_origin_keys.end();
0 comments (0 inline, 0 general)