< Summary

Information
Class: Trailblazer.Pathing.EndpointVoxelResolver
Assembly: Trailblazer
File(s): /home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Search/VoxelResolution/EndpointVoxelResolver.cs
Line coverage
94%
Covered lines: 51
Uncovered lines: 3
Coverable lines: 54
Total lines: 156
Line coverage: 94.4%
Branch coverage
94%
Covered branches: 47
Total branches: 50
Branch coverage: 94%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TryGetEndpointVoxel(...)95.83%242493.1%
TryGetClosestTraversableVoxel(...)90%202094.11%
TryTraceToClosestTraversableVoxel(...)100%66100%

File(s)

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

#LineLine coverage
 1using FixedMathSharp;
 2using GridForge;
 3using GridForge.Grids;
 4using GridForge.Spatial;
 5using GridForge.Utility;
 6using System.Diagnostics.CodeAnalysis;
 7
 8namespace Trailblazer.Pathing;
 9
 10internal interface IVoxelEndpointResolutionPolicy
 11{
 12    bool CanResolve();
 13
 14    bool TryAcceptDirectVoxel(
 15        Voxel voxel,
 16        Fixed64 unitSize,
 17        bool allowUnwalkableEndpoints);
 18
 19    bool RequiresSizeFallback(Voxel voxel, Fixed64 unitSize);
 20
 21    bool IsTraversable(Voxel voxel, Fixed64 unitSize);
 22
 23    bool TryGetFinalFallbackVoxel(
 24        Vector3d position,
 25        Voxel directVoxel,
 26        Fixed64 unitSize,
 27        [MaybeNullWhen(false)] out Voxel voxel);
 28}
 29
 30internal static class EndpointVoxelResolver
 31{
 32    public static bool TryGetEndpointVoxel<TPolicy>(
 33        TrailblazerWorldContext context,
 34        Vector3d position,
 35        Vector3d traceToward,
 36        [MaybeNullWhen(false)] out Voxel voxel,
 37        bool allowUnwalkableEndpoints,
 38        Fixed64 unitSize,
 39        TPolicy policy)
 40        where TPolicy : struct, IVoxelEndpointResolutionPolicy
 41    {
 279442        PathRequestContextResolver.ThrowIfUnusable(context);
 279443        GridWorld world = context.World;
 279444        if (!policy.CanResolve())
 45        {
 846            voxel = null;
 847            return false;
 48        }
 49
 278650        voxel = null;
 278651        bool shouldRelaxEndpoint = allowUnwalkableEndpoints;
 52
 278653        if (world.TryGetVoxel(position, out Voxel? directVoxel)
 278654            && directVoxel != null)
 55        {
 276456            if (policy.TryAcceptDirectVoxel(directVoxel, unitSize, allowUnwalkableEndpoints))
 57            {
 266958                voxel = directVoxel;
 266959                return true;
 60            }
 61
 9562            shouldRelaxEndpoint = shouldRelaxEndpoint || policy.RequiresSizeFallback(directVoxel, unitSize);
 9563            if (shouldRelaxEndpoint
 9564                && TryGetClosestTraversableVoxel(context, directVoxel, out Voxel? closestNeighbor, unitSize, policy)
 9565                && closestNeighbor != null)
 66            {
 5367                voxel = closestNeighbor;
 5368                return true;
 69            }
 70
 4271            if (!shouldRelaxEndpoint)
 3372                return false;
 73
 974            if (TryTraceToClosestTraversableVoxel(context, position, traceToward, unitSize, out voxel, policy))
 375                return true;
 76
 677            return policy.TryGetFinalFallbackVoxel(position, directVoxel, unitSize, out voxel);
 78        }
 79
 2280        if (!shouldRelaxEndpoint)
 2081            return false;
 82
 283        if (TryTraceToClosestTraversableVoxel(context, position, traceToward, unitSize, out voxel, policy))
 284            return true;
 85
 086        voxel = null;
 087        return false;
 88    }
 89
 90    public static bool TryGetClosestTraversableVoxel<TPolicy>(
 91        TrailblazerWorldContext context,
 92        Voxel voxel,
 93        [MaybeNullWhen(false)] out Voxel closestNeighbor,
 94        Fixed64 unitSize,
 95        TPolicy policy)
 96        where TPolicy : struct, IVoxelEndpointResolutionPolicy
 97    {
 7098        PathRequestContextResolver.ThrowIfUnusable(context);
 7099        closestNeighbor = null;
 70100        if (voxel == null || !context.World.TryGetGrid(voxel.WorldIndex.GridIndex, out VoxelGrid? grid))
 0101            return false;
 102
 568103        foreach (SpatialDirection dir in SpatialAwareness.PerpendicularDirections)
 104        {
 238105            if (!voxel.TryGetNeighborFromDirection(grid!, dir, out Voxel? candidate)
 238106                || candidate == null
 238107                || !policy.IsTraversable(candidate, unitSize))
 108            {
 109                continue;
 110            }
 111
 48112            closestNeighbor = candidate;
 48113            return true;
 114        }
 115
 644116        foreach (SpatialDirection dir in SpatialAwareness.DiagonalDirections)
 117        {
 304118            if (!voxel.TryGetNeighborFromDirection(grid!, dir, out Voxel? candidate)
 304119                || candidate == null
 304120                || !policy.IsTraversable(candidate, unitSize))
 121            {
 122                continue;
 123            }
 124
 8125            closestNeighbor = candidate;
 8126            return true;
 127        }
 128
 14129        return false;
 130    }
 131
 132    private static bool TryTraceToClosestTraversableVoxel<TPolicy>(
 133        TrailblazerWorldContext context,
 134        Vector3d position,
 135        Vector3d traceToward,
 136        Fixed64 unitSize,
 137        [MaybeNullWhen(false)] out Voxel voxel,
 138        TPolicy policy)
 139        where TPolicy : struct, IVoxelEndpointResolutionPolicy
 140    {
 39141        foreach (GridVoxelSet gridVoxelSet in GridTracer.TraceLine(context.World, position, traceToward))
 142        {
 127143            foreach (Voxel current in gridVoxelSet.Voxels)
 144            {
 55145                if (!policy.IsTraversable(current, unitSize))
 146                    continue;
 147
 5148                voxel = current;
 5149                return true;
 150            }
 151        }
 152
 6153        voxel = null;
 6154        return false;
 5155    }
 156}