< Summary

Information
Class: Trailblazer.Pathing.VolumeGuide
Assembly: Trailblazer
File(s): /home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Search/Volume/VolumeGuide.cs
Line coverage
96%
Covered lines: 58
Uncovered lines: 2
Coverable lines: 60
Total lines: 160
Line coverage: 96.6%
Branch coverage
80%
Covered branches: 37
Total branches: 46
Branch coverage: 80.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_ActiveWaypoints()50%44100%
Initialize(...)100%22100%
GetIndex(...)83.33%66100%
AdvanceWaypoint()100%11100%
TryGetMovementDirection(...)83.33%6687.5%
GetCurrentWaypointDirection(...)80%1010100%
TryGetFallbackDirection(...)80%101093.75%
TryGetWaypointAt(...)87.5%88100%
ResetForReuse()100%11100%

File(s)

/home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Search/Volume/VolumeGuide.cs

#LineLine coverage
 1using FixedMathSharp;
 2using System;
 3
 4namespace Trailblazer.Pathing;
 5
 6/// <summary>
 7/// Waypoint guide used for raw voxel volume detours.
 8/// </summary>
 9public sealed class VolumeGuide : IWaypointGuide
 10{
 11    /// <summary>
 12    /// Gets the trail map result for the volume survey, if available.
 13    /// </summary>
 14    public VolumeSurveyResult? TrailMap { get; private set; }
 15
 16    /// <inheritdoc/>
 17    public int CurrentWaypointIndex { get; private set; }
 18
 19    private int _lastTriedIndex;
 20
 21    /// <summary>
 22    /// Gets the collection of currently active waypoints used for pathfinding.
 23    /// </summary>
 24    /// <remarks>Returns an empty array if no trail map is available or if there are no active waypoints.</remarks>
 7925    public AStarWaypoint[] ActiveWaypoints => TrailMap?.Waypoints ?? Array.Empty<AStarWaypoint>();
 26
 27    /// <summary>
 28    /// Initializes the navigation state using the specified survey result.
 29    /// </summary>
 30    /// <param name="surveyResult">The survey result containing the path and waypoints to initialize navigation. Must ha
 31    /// <returns>true if initialization succeeds and a valid path is present; otherwise, false.</returns>
 32    public bool Initialize(VolumeSurveyResult surveyResult)
 33    {
 29434        if (!surveyResult.HasPath)
 235            return false;
 36
 29237        AStarWaypoint[] waypoints = surveyResult.Waypoints!;
 29238        TrailMap = surveyResult;
 29239        CurrentWaypointIndex = waypoints.Length > 1 ? 1 : 0;
 29240        _lastTriedIndex = CurrentWaypointIndex;
 29241        return true;
 42    }
 43
 44    /// <inheritdoc/>
 45    public int GetIndex(Vector3d from)
 46    {
 247        Fixed64 minDistSq = Fixed64.MAX_VALUE;
 248        int bestIndex = -1;
 1249        for (int i = 0; i < ActiveWaypoints.Length; i++)
 50        {
 551            Fixed64 distSq = (from - ActiveWaypoints[i].Position).SqrMagnitude;
 552            if (distSq < minDistSq)
 53            {
 554                minDistSq = distSq;
 555                bestIndex = i;
 56            }
 57
 558            if (minDistSq <= Fixed64.Epsilon)
 59                break;
 60        }
 61
 262        return bestIndex;
 63    }
 64
 65    /// <inheritdoc/>
 2266    public void AdvanceWaypoint() => CurrentWaypointIndex++;
 67
 68    /// <inheritdoc/>
 69    public bool TryGetMovementDirection(Vector3d origin, out Vector3d direction)
 70    {
 271        direction = Vector3d.Zero;
 72
 273        if (TrailMap == null || !TrailMap.HasPath)
 174            return false;
 75
 176        int closestIndex = GetIndex(origin);
 177        if (closestIndex == -1)
 078            return false;
 79
 180        direction = (ActiveWaypoints[closestIndex].Position - origin).Normalize();
 181        return true;
 82    }
 83
 84    /// <inheritdoc/>
 85    public Vector3d GetCurrentWaypointDirection(Vector3d origin)
 86    {
 1587        if (TrailMap == null
 1588            || !TrailMap.HasPath
 1589            || CurrentWaypointIndex < 0
 1590            || CurrentWaypointIndex >= ActiveWaypoints.Length)
 91        {
 292            return Vector3d.Zero;
 93        }
 94
 1395        Vector3d waypoint = ActiveWaypoints[CurrentWaypointIndex].Position;
 1396        if (waypoint == Vector3d.Zero)
 197            return Vector3d.Zero;
 98
 1299        return (waypoint - origin).Normal;
 100    }
 101
 102    /// <inheritdoc/>
 103    public bool TryGetFallbackDirection(Vector3d from, out Vector3d fallbackDirection)
 104    {
 2105        fallbackDirection = Vector3d.Zero;
 106
 2107        if (TrailMap == null || ActiveWaypoints.Length == 0)
 1108            return false;
 109
 1110        int searchStart = FixedMath.Clamp(_lastTriedIndex, 0, ActiveWaypoints.Length - 1);
 1111        Fixed64 minDistSq = Fixed64.MAX_VALUE;
 1112        int bestIndex = -1;
 113
 6114        for (int i = searchStart; i < ActiveWaypoints.Length; i++)
 115        {
 2116            Fixed64 distSq = (from - ActiveWaypoints[i].Position).SqrMagnitude;
 2117            if (distSq < minDistSq)
 118            {
 2119                minDistSq = distSq;
 2120                bestIndex = i;
 121            }
 122        }
 123
 1124        if (bestIndex < 0)
 0125            return false;
 126
 1127        fallbackDirection = (ActiveWaypoints[bestIndex].Position - from).Normal;
 1128        _lastTriedIndex = bestIndex;
 1129        return true;
 130    }
 131
 132    /// <summary>
 133    /// Attempts to retrieve the waypoint at the specified index in the active waypoints collection.
 134    /// </summary>
 135    /// <remarks>Returns false if the trail map is null, does not have a path, or if the index is out of range.</remarks
 136    /// <param name="index">The zero-based index of the waypoint to retrieve. Must be within the bounds of the active wa
 137    /// <param name="waypoint">
 138    /// When this method returns, contains the waypoint at the specified index if the operation succeeds; otherwise,
 139    /// the default value for <see cref="AStarWaypoint"/>.
 140    /// </param>
 141    /// <returns>true if the waypoint at the specified index was successfully retrieved; otherwise, false.</returns>
 142    public bool TryGetWaypointAt(int index, out AStarWaypoint waypoint)
 143    {
 12144        if (TrailMap == null || !TrailMap.HasPath || index < 0 || index >= ActiveWaypoints.Length)
 145        {
 2146            waypoint = default;
 2147            return false;
 148        }
 149
 10150        waypoint = ActiveWaypoints[index];
 10151        return true;
 152    }
 153
 154    internal void ResetForReuse()
 155    {
 279156        TrailMap = null;
 279157        CurrentWaypointIndex = 0;
 279158        _lastTriedIndex = 0;
 279159    }
 160}