< Summary

Information
Class: Trailblazer.Pathing.TraversalTransitionDefinition
Assembly: Trailblazer
File(s): /home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Map/TraversalTransitionDefinition.cs
Line coverage
100%
Covered lines: 81
Uncovered lines: 0
Coverable lines: 81
Total lines: 212
Line coverage: 100%
Branch coverage
100%
Covered branches: 38
Total branches: 38
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%44100%
Equals(...)100%2424100%
Equals(...)100%22100%
GetHashCode()100%22100%
IsKnownMedium(...)100%44100%
IsKnownType(...)100%22100%
op_Equality(...)100%11100%
op_Inequality(...)100%11100%

File(s)

/home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/Map/TraversalTransitionDefinition.cs

#LineLine coverage
 1//=======================================================================
 2// TraversalTransitionDefinition.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2024-present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8using System;
 9using FixedMathSharp;
 10using GridForge.Spatial;
 11using SwiftCollections.Utility;
 12
 13namespace Trailblazer.Pathing;
 14
 15/// <summary>
 16/// Describes one directed, source-map-owned semantic traversal action.
 17/// </summary>
 18public readonly struct TraversalTransitionDefinition : IEquatable<TraversalTransitionDefinition>
 19{
 20    /// <summary>
 21    /// The stable map-local transition identifier.
 22    /// </summary>
 23    public string Id { get; }
 24
 25    /// <summary>
 26    /// The semantic action performed by this transition.
 27    /// </summary>
 28    public TraversalTransitionType Type { get; }
 29
 30    /// <summary>
 31    /// The local source-cell index in the owning map.
 32    /// </summary>
 33    public VoxelIndex SourceIndex { get; }
 34
 35    /// <summary>
 36    /// The traversal medium used at the source endpoint.
 37    /// </summary>
 38    public TraversalMedium SourceMedium { get; }
 39
 40    /// <summary>
 41    /// The durable destination-cell address.
 42    /// </summary>
 43    public NavigationCellAddress Destination { get; }
 44
 45    /// <summary>
 46    /// The traversal medium used at the destination endpoint.
 47    /// </summary>
 48    public TraversalMedium DestinationMedium { get; }
 49
 50    /// <summary>
 51    /// Whether the source endpoint overrides its cell anchor.
 52    /// </summary>
 53    public bool HasSourcePointOverride { get; }
 54
 55    /// <summary>
 56    /// The exact source endpoint when <see cref="HasSourcePointOverride"/> is true.
 57    /// </summary>
 58    public Vector3d SourcePointOverride { get; }
 59
 60    /// <summary>
 61    /// Whether the destination endpoint overrides its cell anchor.
 62    /// </summary>
 63    public bool HasDestinationPointOverride { get; }
 64
 65    /// <summary>
 66    /// The exact destination endpoint when <see cref="HasDestinationPointOverride"/> is true.
 67    /// </summary>
 68    public Vector3d DestinationPointOverride { get; }
 69
 70    /// <summary>
 71    /// The complete set of capabilities required to use this transition.
 72    /// </summary>
 73    public TraversalCapability RequiredCapabilities { get; }
 74
 75    /// <summary>
 76    /// The non-negative surcharge for performing this semantic action.
 77    /// </summary>
 78    public Fixed64 ActionCost { get; }
 79
 80    /// <summary>
 81    /// The authored built-in locomotion intent for this semantic action.
 82    /// </summary>
 83    public TraversalTransitionLocomotionHints LocomotionHints { get; }
 84
 85    /// <summary>
 86    /// Creates one complete directed semantic transition definition.
 87    /// </summary>
 88    public TraversalTransitionDefinition(
 89        string id,
 90        TraversalTransitionType type,
 91        VoxelIndex sourceIndex,
 92        TraversalMedium sourceMedium,
 93        NavigationCellAddress destination,
 94        TraversalMedium destinationMedium,
 95        TraversalCapability requiredCapabilities = TraversalCapability.None,
 96        Fixed64 actionCost = default,
 97        TraversalTransitionLocomotionHints locomotionHints = TraversalTransitionLocomotionHints.None,
 98        Vector3d sourcePointOverride = default,
 99        bool hasSourcePointOverride = false,
 100        Vector3d destinationPointOverride = default,
 101        bool hasDestinationPointOverride = false)
 102    {
 4297103        SwiftThrowHelper.ThrowIfArgument(
 4297104            string.IsNullOrWhiteSpace(id),
 4297105            nameof(id),
 4297106            "Transition ID cannot be null, empty, or whitespace.");
 4297107        SwiftThrowHelper.ThrowIfArgument(
 4297108            !IsKnownType(type),
 4297109            nameof(type),
 4297110            "Transition type is unknown.");
 4296111        SwiftThrowHelper.ThrowIfArgument(
 4296112            !IsKnownMedium(sourceMedium),
 4296113            nameof(sourceMedium),
 4296114            "Source medium must identify one known traversal medium.");
 4296115        SwiftThrowHelper.ThrowIfArgument(
 4296116            !IsKnownMedium(destinationMedium),
 4296117            nameof(destinationMedium),
 4296118            "Destination medium must identify one known traversal medium.");
 4296119        SwiftThrowHelper.ThrowIfArgument(
 4296120            (requiredCapabilities & ~NavigationCell.KnownCapabilities) != 0,
 4296121            nameof(requiredCapabilities),
 4296122            "Required capabilities contain an unknown bit.");
 4296123        SwiftThrowHelper.ThrowIfArgument(
 4296124            actionCost < Fixed64.Zero,
 4296125            nameof(actionCost),
 4296126            "Action cost must be non-negative.");
 4296127        SwiftThrowHelper.ThrowIfArgument(
 4296128            (locomotionHints & ~TraversalTransitionRule.KnownLocomotionHints) != 0,
 4296129            nameof(locomotionHints),
 4296130            "Locomotion hints contain an unknown bit.");
 4296131        SwiftThrowHelper.ThrowIfArgument(
 4296132            string.IsNullOrWhiteSpace(destination.MapId),
 4296133            nameof(destination),
 4296134            "Destination must contain a valid map ID.");
 135
 4296136        Id = id;
 4296137        Type = type;
 4296138        SourceIndex = sourceIndex;
 4296139        SourceMedium = sourceMedium;
 4296140        Destination = destination;
 4296141        DestinationMedium = destinationMedium;
 4296142        RequiredCapabilities = requiredCapabilities;
 4296143        ActionCost = actionCost;
 4296144        LocomotionHints = locomotionHints;
 4296145        SourcePointOverride = hasSourcePointOverride ? sourcePointOverride : default;
 4296146        HasSourcePointOverride = hasSourcePointOverride;
 4296147        DestinationPointOverride = hasDestinationPointOverride ? destinationPointOverride : default;
 4296148        HasDestinationPointOverride = hasDestinationPointOverride;
 4296149    }
 150
 151    /// <inheritdoc/>
 152    public bool Equals(TraversalTransitionDefinition other) =>
 4215153        string.Equals(Id, other.Id, StringComparison.Ordinal)
 4215154        && Type == other.Type
 4215155        && SourceIndex.Equals(other.SourceIndex)
 4215156        && SourceMedium == other.SourceMedium
 4215157        && Destination.Equals(other.Destination)
 4215158        && DestinationMedium == other.DestinationMedium
 4215159        && HasSourcePointOverride == other.HasSourcePointOverride
 4215160        && SourcePointOverride == other.SourcePointOverride
 4215161        && HasDestinationPointOverride == other.HasDestinationPointOverride
 4215162        && DestinationPointOverride == other.DestinationPointOverride
 4215163        && RequiredCapabilities == other.RequiredCapabilities
 4215164        && ActionCost == other.ActionCost
 4215165        && LocomotionHints == other.LocomotionHints;
 166
 167    /// <inheritdoc/>
 168    public override bool Equals(object? obj) =>
 14169        obj is TraversalTransitionDefinition other && Equals(other);
 170
 171    /// <inheritdoc/>
 172    public override int GetHashCode()
 173    {
 15174        int idHash = Id == null
 15175            ? 0
 15176            : SwiftHashTools.GetDeterministicStringEqualityComparer().GetHashCode(Id);
 15177        int hash = SwiftHashTools.CombineHashCodes(idHash, (int)Type);
 15178        hash = SwiftHashTools.CombineHashCodes(hash, SourceIndex.GetHashCode());
 15179        hash = SwiftHashTools.CombineHashCodes(hash, (int)SourceMedium);
 15180        hash = SwiftHashTools.CombineHashCodes(hash, Destination.GetHashCode());
 15181        hash = SwiftHashTools.CombineHashCodes(hash, (int)DestinationMedium);
 15182        hash = SwiftHashTools.CombineHashCodes(hash, HasSourcePointOverride ? 1 : 0);
 15183        hash = SwiftHashTools.CombineHashCodes(hash, SourcePointOverride.GetHashCode());
 15184        hash = SwiftHashTools.CombineHashCodes(hash, HasDestinationPointOverride ? 1 : 0);
 15185        hash = SwiftHashTools.CombineHashCodes(hash, DestinationPointOverride.GetHashCode());
 15186        hash = SwiftHashTools.CombineHashCodes(hash, (int)RequiredCapabilities);
 15187        hash = SwiftHashTools.CombineHashCodes(hash, ActionCost.GetHashCode());
 15188        return SwiftHashTools.CombineHashCodes(hash, (int)LocomotionHints);
 189    }
 190
 191    internal static bool IsKnownMedium(TraversalMedium medium) =>
 12239192        medium == TraversalMedium.Solid
 12239193        || medium == TraversalMedium.Gas
 12239194        || medium == TraversalMedium.Liquid;
 195
 196    internal static bool IsKnownType(TraversalTransitionType type) =>
 4467197        type >= TraversalTransitionType.Custom && type <= TraversalTransitionType.Climb;
 198
 199    /// <summary>
 200    /// Tests two definitions for value equality.
 201    /// </summary>
 202    public static bool operator ==(
 203        TraversalTransitionDefinition left,
 1204        TraversalTransitionDefinition right) => left.Equals(right);
 205
 206    /// <summary>
 207    /// Tests two definitions for value inequality.
 208    /// </summary>
 209    public static bool operator !=(
 210        TraversalTransitionDefinition left,
 1211        TraversalTransitionDefinition right) => !left.Equals(right);
 212}