< Summary

Information
Class: Trailblazer.Pathing.HybridGuide
Assembly: Trailblazer
File(s): /home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Search/Hybrid/HybridGuide.cs
Line coverage
96%
Covered lines: 51
Uncovered lines: 2
Coverable lines: 53
Total lines: 153
Line coverage: 96.2%
Branch coverage
75%
Covered branches: 30
Total branches: 40
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Initialize(...)100%44100%
GetIndex(...)83.33%66100%
AdvanceWaypoint()100%11100%
TryGetMovementDirection(...)66.66%6687.5%
GetCurrentWaypointDirection(...)75%88100%
TryGetFallbackDirection(...)70%101093.75%
TryGetWaypointAt(...)66.66%66100%

File(s)

/home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Search/Hybrid/HybridGuide.cs

#LineLine coverage
 1using FixedMathSharp;
 2using System;
 3
 4namespace Trailblazer.Pathing;
 5
 6/// <summary>
 7/// Internal waypoint guide produced from a staged transition-aware route.
 8/// </summary>
 9internal sealed class HybridGuide : IWaypointGuide
 10{
 11    /// <summary>
 12    /// The active waypoints for this guide, which may be generated from either A* or flow field segments depending on t
 13    /// This allows the guide to provide consistent waypoint-based navigation regardless of the underlying pathfinding s
 14    /// </summary>
 15    public AStarWaypoint[] ActiveWaypoints { get; private set; } = Array.Empty<AStarWaypoint>();
 16
 17    /// <summary>
 18    /// Returns the index of the current waypoint being pursued.
 19    /// This is used to track progression through the waypoints of the current stage in the plan, and it allows the guid
 20    /// The index is updated as the agent reaches each waypoint, and it helps ensure that the guide provides directions 
 21    /// </summary>
 22    public int CurrentWaypointIndex { get; private set; }
 23
 24    /// <summary>
 25    /// Tracks the last waypoint index that was used to provide a fallback direction.
 26    /// This helps ensure that fallback directions are provided in a forward progression along the path, rather than rep
 27    /// By updating this index each time a fallback direction is provided, the guide can offer more dynamic and contextu
 28    /// </summary>
 29    private int _lastTriedIndex;
 30
 31    /// <summary>
 32    /// Initializes the guide with the given waypoints for the current stage of the plan.
 33    /// </summary>
 34    /// <param name="waypoints">The waypoints to initialize the guide with.</param>
 35    /// <returns>True if the guide was successfully initialized; otherwise, false.</returns>
 36    public bool Initialize(AStarWaypoint[] waypoints)
 37    {
 838        if (waypoints == null || waypoints.Length == 0)
 239            return false;
 40
 641        ActiveWaypoints = waypoints;
 642        CurrentWaypointIndex = waypoints.Length > 1 ? 1 : 0;
 643        _lastTriedIndex = CurrentWaypointIndex;
 644        return true;
 45    }
 46
 47    /// <inheritdoc/>
 48    public int GetIndex(Vector3d from)
 49    {
 250        Fixed64 minDistSq = Fixed64.MAX_VALUE;
 251        int bestIndex = -1;
 1252        for (int i = 0; i < ActiveWaypoints.Length; i++)
 53        {
 554            Fixed64 distSq = (from - ActiveWaypoints[i].Position).SqrMagnitude;
 555            if (distSq < minDistSq)
 56            {
 557                minDistSq = distSq;
 558                bestIndex = i;
 59            }
 60
 561            if (minDistSq <= Fixed64.Epsilon)
 62                break;
 63        }
 64
 265        return bestIndex;
 66    }
 67
 68    /// <inheritdoc/>
 569    public void AdvanceWaypoint() => CurrentWaypointIndex++;
 70
 71    /// <inheritdoc/>
 72    public bool TryGetMovementDirection(Vector3d origin, out Vector3d direction)
 73    {
 274        direction = Vector3d.Zero;
 75
 276        if (ActiveWaypoints == null || ActiveWaypoints.Length == 0)
 177            return false;
 78
 179        int closestIndex = GetIndex(origin);
 180        if (closestIndex == -1)
 081            return false;
 82
 183        direction = (ActiveWaypoints[closestIndex].Position - origin).Normalize();
 184        return true;
 85    }
 86
 87    /// <inheritdoc/>
 88    public Vector3d GetCurrentWaypointDirection(Vector3d origin)
 89    {
 490        if (ActiveWaypoints == null
 491            || CurrentWaypointIndex < 0
 492            || CurrentWaypointIndex >= ActiveWaypoints.Length)
 93        {
 294            return Vector3d.Zero;
 95        }
 96
 297        Vector3d waypoint = ActiveWaypoints[CurrentWaypointIndex].Position;
 298        if (waypoint == Vector3d.Zero)
 199            return Vector3d.Zero;
 100
 1101        return (waypoint - origin).Normal;
 102    }
 103
 104    /// <inheritdoc/>
 105    public bool TryGetFallbackDirection(Vector3d from, out Vector3d fallbackDirection)
 106    {
 2107        fallbackDirection = Vector3d.Zero;
 108
 2109        if (ActiveWaypoints == null || ActiveWaypoints.Length == 0)
 1110            return false;
 111
 1112        int searchStart = FixedMath.Clamp(_lastTriedIndex, 0, ActiveWaypoints.Length - 1);
 1113        Fixed64 minDistSq = Fixed64.MAX_VALUE;
 1114        int bestIndex = -1;
 115
 6116        for (int i = searchStart; i < ActiveWaypoints.Length; i++)
 117        {
 2118            Fixed64 distSq = (from - ActiveWaypoints[i].Position).SqrMagnitude;
 2119            if (distSq < minDistSq)
 120            {
 2121                minDistSq = distSq;
 2122                bestIndex = i;
 123            }
 124        }
 125
 1126        if (bestIndex < 0)
 0127            return false;
 128
 1129        fallbackDirection = (ActiveWaypoints[bestIndex].Position - from).Normal;
 1130        _lastTriedIndex = bestIndex;
 1131        return true;
 132    }
 133
 134    /// <summary>
 135    /// Attempts to get the waypoint at the specified index.
 136    /// This can be used to retrieve specific waypoints for debugging, visualization, or advanced navigation logic that 
 137    /// By providing a method to access waypoints by index, the guide allows for greater flexibility and control over ho
 138    /// </summary>
 139    /// <param name="index">The index of the waypoint to retrieve.</param>
 140    /// <param name="waypoint">When this method returns, contains the waypoint at the specified index, if the index is v
 141    /// <returns>True if the waypoint was successfully retrieved; otherwise, false.</returns>
 142    public bool TryGetWaypointAt(int index, out AStarWaypoint waypoint)
 143    {
 3144        if (ActiveWaypoints == null || index < 0 || index >= ActiveWaypoints.Length)
 145        {
 1146            waypoint = default;
 1147            return false;
 148        }
 149
 2150        waypoint = ActiveWaypoints[index];
 2151        return true;
 152    }
 153}