< Summary

Information
Class: Trailblazer.Pathing.SolidVoxelFinder
Assembly: Trailblazer
File(s): /home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Search/VoxelResolution/SolidVoxelFinder.cs
Line coverage
98%
Covered lines: 80
Uncovered lines: 1
Coverable lines: 81
Total lines: 268
Line coverage: 98.7%
Branch coverage
81%
Covered branches: 31
Total branches: 38
Branch coverage: 81.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TryGetPathEdgeVoxels(...)100%44100%
GetEndVoxel(...)100%22100%
GetStartVoxel(...)100%22100%
StarCast(...)100%11100%
StarCast(...)100%44100%
TryGetClosestWalkableVoxel(...)100%22100%
GetClosestVoxelForSize(...)100%11100%
TryGetEndpointVoxel(...)100%11100%
IsChartTraversable(...)83.33%66100%
IsBaseChartTraversable(...)50%44100%
RequiresSizeFallback(...)75%88100%
.ctor(...)100%11100%
CanResolve()100%11100%
TryAcceptDirectVoxel(...)100%11100%
RequiresSizeFallback(...)100%11100%
IsTraversable(...)100%11100%
TryGetFinalFallbackVoxel(...)100%11100%
StarCast(...)66.66%6690%

File(s)

/home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Search/VoxelResolution/SolidVoxelFinder.cs

#LineLine coverage
 1using FixedMathSharp;
 2using GridForge.Grids;
 3using System.Diagnostics.CodeAnalysis;
 4using System.Runtime.CompilerServices;
 5
 6namespace Trailblazer.Pathing;
 7
 8/// <summary>
 9/// Utility for resolving valid start and end voxels for pathfinding based on world positions,
 10/// with optional size consideration and walkability fallback.
 11/// </summary>
 12public static class SolidVoxelFinder
 13{
 14    /// <summary>
 15    /// Specifies the maximum allowable test distance.
 16    /// </summary>
 17    public const int MaxTestDistance = 3;
 18
 19    /// <summary>
 20    /// Attempts to get valid start and end voxels from one explicit context.
 21    /// </summary>
 22    public static bool TryGetPathEdgeVoxels(
 23        TrailblazerWorldContext context,
 24        Vector3d origin,
 25        Vector3d target,
 26        [MaybeNullWhen(false)] out Voxel originVoxel,
 27        [MaybeNullWhen(false)] out Voxel targetVoxel,
 28        Fixed64? unitSize = null,
 29        bool allowUnwalkableEndpoints = false)
 30    {
 113831        PathRequestContextResolver.ThrowIfUnusable(context);
 113832        Fixed64 resolvedUnitSize = unitSize ?? context.VoxelSize;
 113833        targetVoxel = null;
 113834        if (!GetStartVoxel(context, origin, target, out originVoxel, allowUnwalkableEndpoints, resolvedUnitSize))
 2535            return false;
 36
 111337        return GetEndVoxel(context, origin, target, out targetVoxel, allowUnwalkableEndpoints, resolvedUnitSize);
 38    }
 39
 40
 41    /// <summary>
 42    /// Finds a closest valid end voxel in one explicit context.
 43    /// </summary>
 44    public static bool GetEndVoxel(
 45        TrailblazerWorldContext context,
 46        Vector3d origin,
 47        Vector3d target,
 48        [MaybeNullWhen(false)] out Voxel targetVoxel,
 49        bool allowUnwalkableEndpoints = false,
 50        Fixed64? unitSize = null)
 51    {
 112152        return TryGetEndpointVoxel(
 112153            context,
 112154            target,
 112155            origin,
 112156            out targetVoxel,
 112157            allowUnwalkableEndpoints,
 112158            unitSize ?? context.VoxelSize);
 59    }
 60
 61    /// <summary>
 62    /// Finds a closest valid start voxel in one explicit context.
 63    /// </summary>
 64    public static bool GetStartVoxel(
 65        TrailblazerWorldContext context,
 66        Vector3d origin,
 67        Vector3d target,
 68        [MaybeNullWhen(false)] out Voxel originVoxel,
 69        bool allowUnwalkableEndpoints = false,
 70        Fixed64? unitSize = null)
 71    {
 121772        return TryGetEndpointVoxel(
 121773            context,
 121774            origin,
 121775            target,
 121776            out originVoxel,
 121777            allowUnwalkableEndpoints,
 121778            unitSize ?? context.VoxelSize);
 79    }
 80
 81
 82    /// <summary>
 83    /// Performs a bounded same-layer star search in one explicit context.
 84    /// </summary>
 85    public static bool StarCast(
 86        TrailblazerWorldContext context,
 87        Vector3d target,
 88        [MaybeNullWhen(false)] out Voxel targetVoxel) =>
 289        StarCast(context, target, out targetVoxel, context.VoxelSize);
 90
 91    /// <summary>
 92    /// Performs a bounded same-layer star search in one explicit context.
 93    /// </summary>
 94    public static bool StarCast(
 95        TrailblazerWorldContext context,
 96        Vector3d target,
 97        [MaybeNullWhen(false)] out Voxel targetVoxel,
 98        Fixed64 unitSize)
 99    {
 3100        PathRequestContextResolver.ThrowIfUnusable(context);
 3101        if (!context.World.TryGetVoxel(target, out Voxel? directVoxel)
 3102            || directVoxel == null)
 103        {
 1104            targetVoxel = null;
 1105            return false;
 106        }
 107
 2108        return StarCast(context, target, directVoxel, out targetVoxel, unitSize);
 109    }
 110
 111    /// <summary>
 112    /// Finds the closest valid neighboring solid voxel in one explicit context.
 113    /// </summary>
 114    public static bool TryGetClosestWalkableVoxel(
 115        TrailblazerWorldContext context,
 116        Voxel voxel,
 117        [MaybeNullWhen(false)] out Voxel closestNeighbor,
 118        Fixed64? unitSize = null)
 119    {
 4120        return EndpointVoxelResolver.TryGetClosestTraversableVoxel(
 4121            context,
 4122            voxel,
 4123            out closestNeighbor,
 4124            unitSize ?? context.VoxelSize,
 4125            new SolidEndpointPolicy(context));
 126    }
 127
 128    /// <summary>
 129    /// Finds the closest valid endpoint voxel for a unit size in one explicit context.
 130    /// </summary>
 131    public static bool GetClosestVoxelForSize(
 132        TrailblazerWorldContext context,
 133        Vector3d origin,
 134        Vector3d target,
 135        Fixed64 unitSize,
 136        [MaybeNullWhen(false)] out Voxel targetVoxel,
 137        bool allowUnwalkableEndpoints = false)
 138    {
 2139        return EndpointVoxelResolver.TryGetEndpointVoxel(
 2140            context,
 2141            target,
 2142            origin,
 2143            out targetVoxel,
 2144            allowUnwalkableEndpoints,
 2145            unitSize,
 2146            new SolidEndpointPolicy(context));
 147    }
 148
 149    private static bool TryGetEndpointVoxel(
 150        TrailblazerWorldContext context,
 151        Vector3d position,
 152        Vector3d traceToward,
 153        [MaybeNullWhen(false)] out Voxel voxel,
 154        bool allowUnwalkableEndpoints,
 155        Fixed64 unitSize)
 156    {
 2338157        return EndpointVoxelResolver.TryGetEndpointVoxel(
 2338158            context,
 2338159            position,
 2338160            traceToward,
 2338161            out voxel,
 2338162            allowUnwalkableEndpoints,
 2338163            unitSize,
 2338164            new SolidEndpointPolicy(context));
 165    }
 166
 167    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 168    private static bool IsChartTraversable(Voxel voxel, Fixed64 unitSize, Fixed64 voxelSize)
 169    {
 2608170        if (!IsBaseChartTraversable(voxel))
 234171            return false;
 172
 2374173        voxel.TryGetPartition(out SolidChartPartition? partition);
 2374174        return unitSize == voxelSize
 2374175            || (partition != null && !partition.IsImpassable(unitSize));
 176    }
 177
 178    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 179    private static bool IsBaseChartTraversable(Voxel voxel) =>
 2629180        voxel != null
 2629181        && !voxel.IsBlocked
 2629182        && voxel.HasPartition<SolidChartPartition>();
 183
 184    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 185    private static bool RequiresSizeFallback(Voxel voxel, Fixed64 unitSize, Fixed64 voxelSize)
 186    {
 35187        if (unitSize == voxelSize
 35188            || !IsBaseChartTraversable(voxel)
 35189            || !voxel.TryGetPartition(out SolidChartPartition? partition)
 35190            || partition == null)
 191        {
 14192            return false;
 193        }
 194
 21195        return partition.IsImpassable(unitSize);
 196    }
 197
 198    private readonly struct SolidEndpointPolicy : IVoxelEndpointResolutionPolicy
 199    {
 200        private readonly TrailblazerWorldContext _context;
 201        private readonly Fixed64 _voxelSize;
 202
 203        public SolidEndpointPolicy(TrailblazerWorldContext context)
 204        {
 2344205            _context = context;
 2344206            _voxelSize = context.VoxelSize;
 2344207        }
 208
 209        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 2340210        public bool CanResolve() => true;
 211
 212        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 213        public bool TryAcceptDirectVoxel(
 214            Voxel voxel,
 215            Fixed64 unitSize,
 216            bool allowUnwalkableEndpoints)
 217        {
 2324218            return IsChartTraversable(voxel, unitSize, _voxelSize);
 219        }
 220
 221        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 222        public bool RequiresSizeFallback(Voxel voxel, Fixed64 unitSize)
 223        {
 35224            return SolidVoxelFinder.RequiresSizeFallback(voxel, unitSize, _voxelSize);
 225        }
 226
 227        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 228        public bool IsTraversable(Voxel voxel, Fixed64 unitSize)
 229        {
 280230            return IsChartTraversable(voxel, unitSize, _voxelSize);
 231        }
 232
 233        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 234        public bool TryGetFinalFallbackVoxel(
 235            Vector3d position,
 236            Voxel directVoxel,
 237            Fixed64 unitSize,
 238            [MaybeNullWhen(false)] out Voxel voxel)
 239        {
 2240            return StarCast(_context, position, directVoxel, out voxel, unitSize);
 241        }
 242    }
 243
 244    private static bool StarCast(
 245        TrailblazerWorldContext context,
 246        Vector3d target,
 247        Voxel directVoxel,
 248        [MaybeNullWhen(false)] out Voxel targetVoxel,
 249        Fixed64 unitSize)
 250    {
 4251        targetVoxel = null;
 252
 4253        AlternativeVoxelFinder finder = context.Pathing.State.AlternativeVoxelFinder;
 4254        finder.SetQuery(context, target, directVoxel, MaxTestDistance);
 255
 4256        if (!finder.GetVoxel(out Voxel? candidateVoxel)
 4257            || candidateVoxel == null)
 0258            return false;
 259
 4260        if (IsChartTraversable(candidateVoxel, unitSize, context.VoxelSize))
 261        {
 1262            targetVoxel = candidateVoxel;
 1263            return true;
 264        }
 265
 3266        return TryGetClosestWalkableVoxel(context, candidateVoxel, out targetVoxel, unitSize);
 267    }
 268}

Methods/Properties

TryGetPathEdgeVoxels(Trailblazer.TrailblazerWorldContext,FixedMathSharp.Vector3d,FixedMathSharp.Vector3d,GridForge.Grids.Voxel&,GridForge.Grids.Voxel&,System.Nullable`1<FixedMathSharp.Fixed64>,System.Boolean)
GetEndVoxel(Trailblazer.TrailblazerWorldContext,FixedMathSharp.Vector3d,FixedMathSharp.Vector3d,GridForge.Grids.Voxel&,System.Boolean,System.Nullable`1<FixedMathSharp.Fixed64>)
GetStartVoxel(Trailblazer.TrailblazerWorldContext,FixedMathSharp.Vector3d,FixedMathSharp.Vector3d,GridForge.Grids.Voxel&,System.Boolean,System.Nullable`1<FixedMathSharp.Fixed64>)
StarCast(Trailblazer.TrailblazerWorldContext,FixedMathSharp.Vector3d,GridForge.Grids.Voxel&)
StarCast(Trailblazer.TrailblazerWorldContext,FixedMathSharp.Vector3d,GridForge.Grids.Voxel&,FixedMathSharp.Fixed64)
TryGetClosestWalkableVoxel(Trailblazer.TrailblazerWorldContext,GridForge.Grids.Voxel,GridForge.Grids.Voxel&,System.Nullable`1<FixedMathSharp.Fixed64>)
GetClosestVoxelForSize(Trailblazer.TrailblazerWorldContext,FixedMathSharp.Vector3d,FixedMathSharp.Vector3d,FixedMathSharp.Fixed64,GridForge.Grids.Voxel&,System.Boolean)
TryGetEndpointVoxel(Trailblazer.TrailblazerWorldContext,FixedMathSharp.Vector3d,FixedMathSharp.Vector3d,GridForge.Grids.Voxel&,System.Boolean,FixedMathSharp.Fixed64)
IsChartTraversable(GridForge.Grids.Voxel,FixedMathSharp.Fixed64,FixedMathSharp.Fixed64)
IsBaseChartTraversable(GridForge.Grids.Voxel)
RequiresSizeFallback(GridForge.Grids.Voxel,FixedMathSharp.Fixed64,FixedMathSharp.Fixed64)
.ctor(Trailblazer.TrailblazerWorldContext)
CanResolve()
TryAcceptDirectVoxel(GridForge.Grids.Voxel,FixedMathSharp.Fixed64,System.Boolean)
RequiresSizeFallback(GridForge.Grids.Voxel,FixedMathSharp.Fixed64)
IsTraversable(GridForge.Grids.Voxel,FixedMathSharp.Fixed64)
TryGetFinalFallbackVoxel(FixedMathSharp.Vector3d,GridForge.Grids.Voxel,FixedMathSharp.Fixed64,GridForge.Grids.Voxel&)
StarCast(Trailblazer.TrailblazerWorldContext,FixedMathSharp.Vector3d,GridForge.Grids.Voxel,GridForge.Grids.Voxel&,FixedMathSharp.Fixed64)