Class AdjacencySystem
ENGINE LAYER: Province adjacency system
Stores which provinces are adjacent (share a border) for movement validation. Populated from FastAdjacencyScanner during map initialization.
Architecture:
- Dictionary{ushort, HashSet{ushort}}: provinceID → set of neighbor IDs
- Bidirectional: if A is adjacent to B, then B is adjacent to A
- Read-only after initialization
Usage:
- Initialize with SetAdjacencies(adjacencyData)
- Query with IsAdjacent(province1, province2)
- Get all neighbors with GetNeighbors(provinceID)
Performance:
- IsAdjacent: O(1) HashSet lookup
- GetNeighbors: O(1) dictionary lookup
- Memory: ~6 neighbors × 13,350 provinces × 2 bytes = ~160 KB
public class AdjacencySystem
- Inheritance
-
objectAdjacencySystem
Constructors
AdjacencySystem()
public AdjacencySystem()
Properties
IsInitialized
Check if system is initialized
public bool IsInitialized { get; }
Property Value
- bool
ProvinceCount
Get total province count
public int ProvinceCount { get; }
Property Value
- int
TotalAdjacencyPairs
Get total adjacency pair count
public int TotalAdjacencyPairs { get; }
Property Value
- int
Methods
Dispose()
Dispose native allocations
public void Dispose()
FindBridgeProvinces(IEnumerable<ushort>, Func<ushort, bool>, List<ushort>)
Find all bridge provinces within a region. These are strategic choke points where losing control would split territory.
public void FindBridgeProvinces(IEnumerable<ushort> regionProvinces, Func<ushort, bool> regionPredicate, List<ushort> resultBuffer)
Parameters
regionProvincesIEnumerable<ushort>Set of provinces to check
regionPredicateFunc<ushort, bool>Predicate defining what's in the region
resultBufferList<ushort>Pre-allocated list to fill with bridge provinces
GetConnectedRegion(ushort, Func<ushort, bool>)
Get all provinces connected to a starting province (allocating version).
public HashSet<ushort> GetConnectedRegion(ushort startProvince, Func<ushort, bool> predicate)
Parameters
startProvinceushortpredicateFunc<ushort, bool>
Returns
- HashSet<ushort>
GetConnectedRegion(ushort, Func<ushort, bool>, HashSet<ushort>)
Get all provinces connected to a starting province that match a predicate. Uses BFS flood fill. Returns the connected region including the start province.
Example: Get all provinces owned by a country connected to capitalProvince GetConnectedRegion(capital, id => provinceSystem.GetOwner(id) == countryId)
public void GetConnectedRegion(ushort startProvince, Func<ushort, bool> predicate, HashSet<ushort> resultBuffer)
Parameters
startProvinceushortStarting province for flood fill
predicateFunc<ushort, bool>Filter - only provinces passing this are included and traversed
resultBufferHashSet<ushort>Pre-allocated set to fill (cleared before use)
GetNativeData()
Get read-only native adjacency data for Burst jobs.
public NativeAdjacencyData GetNativeData()
Returns
GetNeighborCount(ushort)
Get neighbor count for a province
public int GetNeighborCount(ushort provinceId)
Parameters
provinceIdushort
Returns
- int
GetNeighbors(ushort, Allocator)
Get all neighbors for a province Returns empty array if province has no neighbors or doesn't exist Caller must dispose the returned NativeArray
public NativeArray<ushort> GetNeighbors(ushort provinceId, Allocator allocator = Allocator.TempJob)
Parameters
provinceIdushortallocatorAllocator
Returns
- NativeArray<ushort>
GetNeighbors(ushort, NativeList<ushort>)
Get all neighbors for a province, filling an existing NativeList (zero allocations) Clears the list before filling it with neighbors Used by PathfindingSystem for allocation-free pathfinding
public void GetNeighbors(ushort provinceId, NativeList<ushort> resultBuffer)
Parameters
provinceIdushortresultBufferNativeList<ushort>
GetNeighborsWhere(ushort, Func<ushort, bool>)
Get neighbors that match a predicate (allocating version). Prefer the buffer version for hot paths.
public List<ushort> GetNeighborsWhere(ushort provinceId, Func<ushort, bool> predicate)
Parameters
provinceIdushortpredicateFunc<ushort, bool>
Returns
- List<ushort>
GetNeighborsWhere(ushort, Func<ushort, bool>, List<ushort>)
Get neighbors that match a predicate. Example: GetNeighborsWhere(provinceId, id => provinceSystem.GetOwner(id) == enemyCountry)
public void GetNeighborsWhere(ushort provinceId, Func<ushort, bool> predicate, List<ushort> resultBuffer)
Parameters
provinceIdushortProvince to get neighbors for
predicateFunc<ushort, bool>Filter function - returns true for neighbors to include
resultBufferList<ushort>Pre-allocated list to fill (cleared before use)
GetSharedBorderProvinces(IEnumerable<ushort>, HashSet<ushort>)
Get provinces where two countries share a border (allocating version).
public List<ushort> GetSharedBorderProvinces(IEnumerable<ushort> ownedProvinces, HashSet<ushort> foreignProvinces)
Parameters
ownedProvincesIEnumerable<ushort>foreignProvincesHashSet<ushort>
Returns
- List<ushort>
GetSharedBorderProvinces(IEnumerable<ushort>, HashSet<ushort>, List<ushort>)
Get provinces where two countries share a border. Returns provinces from country1 that are adjacent to any province in country2.
Example: Find where France borders Germany GetSharedBorderProvinces(frenchProvinces, germanProvinces, borderBuffer)
public void GetSharedBorderProvinces(IEnumerable<ushort> ownedProvinces, HashSet<ushort> foreignProvinces, List<ushort> resultBuffer)
Parameters
ownedProvincesIEnumerable<ushort>Provinces belonging to first country
foreignProvincesHashSet<ushort>Provinces belonging to second country
resultBufferList<ushort>Pre-allocated list to fill with border provinces from ownedProvinces
GetStatistics()
Get adjacency statistics for debugging (string format). Prefer GetStats() for programmatic access.
public string GetStatistics()
Returns
- string
GetStats()
Get queryable adjacency statistics.
public AdjacencyStats GetStats()
Returns
IsAdjacent(ushort, ushort)
Check if two provinces are adjacent (share a border)
public bool IsAdjacent(ushort province1, ushort province2)
Parameters
province1ushortprovince2ushort
Returns
- bool
IsBridgeProvince(ushort, Func<ushort, bool>)
Check if a province is a "bridge" - removing it would disconnect the region.
A bridge province has the property that the region becomes disconnected if it's removed. Useful for strategic AI (critical choke points).
Algorithm: Check if any two neighbors of the province can still reach each other without going through this province.
public bool IsBridgeProvince(ushort province, Func<ushort, bool> regionPredicate)
Parameters
provinceushortProvince to check
regionPredicateFunc<ushort, bool>Predicate defining the region (e.g., same owner)
Returns
- bool
True if removing this province would disconnect the region
SetAdjacencies(Dictionary<int, HashSet<int>>)
Initialize adjacency data from FastAdjacencyScanner results Converts from Dictionary{int, HashSet{int}} to Dictionary{ushort, HashSet{ushort}} Also builds native MultiHashMap for Burst job compatibility
public void SetAdjacencies(Dictionary<int, HashSet<int>> scanResults)
Parameters
scanResultsDictionary<int, HashSet<int>>