< Summary

Information
Class: Trailblazer.Pathing.VolumeMediumRules
Assembly: Trailblazer
File(s): /home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/VolumeRules/VolumeMediumRules.cs
Line coverage
98%
Covered lines: 61
Uncovered lines: 1
Coverable lines: 62
Total lines: 159
Line coverage: 98.3%
Branch coverage
93%
Covered branches: 30
Total branches: 32
Branch coverage: 93.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_State()100%11100%
get_HasGasVoxelRule()100%11100%
get_HasLiquidVoxelRule()100%11100%
get_RegistryVersion()100%11100%
SetGasVoxelPartition()50%22100%
SetGasVoxelRule(...)100%11100%
ClearGasVoxelRule()100%11100%
SetLiquidVoxelPartition()100%22100%
SetLiquidVoxelRule(...)100%11100%
ClearLiquidVoxelRule()100%11100%
Reset()50%2283.33%
IsConfigured(...)100%11100%
IsConfigured(...)100%88100%
Matches(...)100%11100%
Matches(...)100%1818100%
InvalidateRuleConfiguration()100%11100%

File(s)

/home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Pathing/VolumeRules/VolumeMediumRules.cs

#LineLine coverage
 1using GridForge.Grids;
 2using GridForge.Spatial;
 3using System.Runtime.CompilerServices;
 4
 5namespace Trailblazer.Pathing;
 6
 7/// <summary>
 8/// Evaluates whether a host-defined voxel belongs to a raw-volume traversal medium.
 9/// </summary>
 10/// <param name="voxel">The voxel to evaluate.</param>
 11/// <returns>true when the voxel belongs to the configured medium; otherwise, false.</returns>
 12public delegate bool VolumeVoxelRule(Voxel voxel);
 13
 14/// <summary>
 15/// Implements authored and host-configured membership rules for raw volume traversal media.
 16/// </summary>
 17internal static class VolumeMediumRules
 18{
 516519    private static VolumeMediumRulesState State => PathManager.ActiveState.VolumeRulesState;
 20
 21    /// <summary>
 22    /// Indicates whether a gas rule is currently configured.
 23    /// </summary>
 1324    public static bool HasGasVoxelRule => State.GasVoxelRule != null;
 25
 26    /// <summary>
 27    /// Indicates whether a liquid rule is currently configured.
 28    /// </summary>
 1029    public static bool HasLiquidVoxelRule => State.LiquidVoxelRule != null;
 30
 631    internal static int RegistryVersion => State.RegistryVersion;
 32
 33    /// <summary>
 34    /// Uses a host-defined voxel partition type to add gas membership on eligible voxels.
 35    /// </summary>
 36    public static void SetGasVoxelPartition<TPartition>()
 37        where TPartition : class, IVoxelPartition
 38    {
 239        SetGasVoxelRule(static voxel =>
 240            voxel != null
 241            && voxel.HasPartition<TPartition>());
 242    }
 43
 44    /// <summary>
 45    /// Sets a host-defined rule that adds gas membership on eligible voxels.
 46    /// </summary>
 47    public static void SetGasVoxelRule(VolumeVoxelRule rule)
 48    {
 1949        State.GasVoxelRule = rule;
 1950        InvalidateRuleConfiguration();
 1951    }
 52
 53    /// <summary>
 54    /// Clears any previously configured gas voxel rule.
 55    /// </summary>
 56    public static void ClearGasVoxelRule()
 57    {
 4858        State.GasVoxelRule = null;
 4859        InvalidateRuleConfiguration();
 4860    }
 61
 62    /// <summary>
 63    /// Uses a host-defined voxel partition type to add liquid membership on eligible voxels.
 64    /// </summary>
 65    public static void SetLiquidVoxelPartition<TPartition>()
 66        where TPartition : class, IVoxelPartition
 67    {
 268        SetLiquidVoxelRule(static voxel =>
 269            voxel != null
 270            && voxel.HasPartition<TPartition>());
 271    }
 72
 73    /// <summary>
 74    /// Sets a host-defined rule that adds liquid membership on eligible voxels.
 75    /// </summary>
 76    public static void SetLiquidVoxelRule(VolumeVoxelRule rule)
 77    {
 878        State.LiquidVoxelRule = rule;
 879        InvalidateRuleConfiguration();
 880    }
 81
 82    /// <summary>
 83    /// Clears any previously configured liquid voxel rule.
 84    /// </summary>
 85    public static void ClearLiquidVoxelRule()
 86    {
 4587        State.LiquidVoxelRule = null;
 4588        InvalidateRuleConfiguration();
 4589    }
 90
 91    internal static void Reset()
 92    {
 163293        if (!PathManager.TryGetActiveState(out _))
 094            return;
 95
 163296        State.GasVoxelRule = null;
 163297        State.LiquidVoxelRule = null;
 163298        InvalidateRuleConfiguration();
 163299    }
 100
 101    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 102    internal static bool IsConfigured(TraversalMedium medium)
 103    {
 8104        return IsConfigured(PathManager.ActiveState, medium);
 105    }
 106
 107    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 108    internal static bool IsConfigured(PathingWorldState pathingState, TraversalMedium medium)
 109    {
 477110        VolumeMediumRulesState state = pathingState.VolumeRulesState;
 477111        return medium switch
 477112        {
 278113            TraversalMedium.Gas => state.GasVoxelRule != null || PathManager.HasAuthoredVolumeMedium(pathingState, Trave
 198114            TraversalMedium.Liquid => state.LiquidVoxelRule != null || PathManager.HasAuthoredVolumeMedium(pathingState,
 1115            _ => false
 477116        };
 117    }
 118
 119    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 120    internal static bool Matches(Voxel voxel, TraversalMedium medium)
 121    {
 180122        return Matches(PathManager.ActiveState, voxel, medium);
 123    }
 124
 125    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 126    internal static bool Matches(PathingWorldState pathingState, Voxel voxel, TraversalMedium medium)
 127    {
 4494128        if (voxel == null)
 1129            return false;
 130
 4493131        bool hasTrailblazerPartition = voxel.HasPartition<SolidChartPartition>();
 4493132        bool hasAuthoredVolumeChartPartition = voxel.TryGetPartition(out VolumeChartPartition? volumeChartPartition)
 4493133            && volumeChartPartition != null;
 4493134        hasTrailblazerPartition |= hasAuthoredVolumeChartPartition;
 4493135        if (!hasTrailblazerPartition)
 44136            return false;
 137
 4449138        VolumeMediumRulesState state = pathingState.VolumeRulesState;
 4449139        bool hostGasMatch = state.GasVoxelRule?.Invoke(voxel) == true;
 4449140        bool hostLiquidMatch = state.LiquidVoxelRule?.Invoke(voxel) == true;
 4449141        bool authoredGasMatch = volumeChartPartition?.SupportsMedium(TraversalMedium.Gas) == true;
 4449142        bool authoredLiquidMatch = volumeChartPartition?.SupportsMedium(TraversalMedium.Liquid) == true;
 143
 144        // Host rules only add medium membership; they do not suppress authored media.
 4449145        return medium switch
 4449146        {
 2622147            TraversalMedium.Gas => authoredGasMatch || hostGasMatch,
 1826148            TraversalMedium.Liquid => authoredLiquidMatch || hostLiquidMatch,
 1149            _ => false
 4449150        };
 151    }
 152
 153    private static void InvalidateRuleConfiguration()
 154    {
 1752155        State.IncrementRegistryVersion();
 1752156        TraversalTransitionRegistry.RefreshManagedManualTransitions();
 1752157        PathGuideFactory.InvalidateVolumeCache();
 1752158    }
 159}