| | | 1 | | using GridForge.Grids; |
| | | 2 | | using GridForge.Spatial; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | |
| | | 5 | | namespace 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> |
| | | 12 | | public delegate bool VolumeVoxelRule(Voxel voxel); |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Implements authored and host-configured membership rules for raw volume traversal media. |
| | | 16 | | /// </summary> |
| | | 17 | | internal static class VolumeMediumRules |
| | | 18 | | { |
| | 5165 | 19 | | private static VolumeMediumRulesState State => PathManager.ActiveState.VolumeRulesState; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Indicates whether a gas rule is currently configured. |
| | | 23 | | /// </summary> |
| | 13 | 24 | | public static bool HasGasVoxelRule => State.GasVoxelRule != null; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Indicates whether a liquid rule is currently configured. |
| | | 28 | | /// </summary> |
| | 10 | 29 | | public static bool HasLiquidVoxelRule => State.LiquidVoxelRule != null; |
| | | 30 | | |
| | 6 | 31 | | 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 | | { |
| | 2 | 39 | | SetGasVoxelRule(static voxel => |
| | 2 | 40 | | voxel != null |
| | 2 | 41 | | && voxel.HasPartition<TPartition>()); |
| | 2 | 42 | | } |
| | | 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 | | { |
| | 19 | 49 | | State.GasVoxelRule = rule; |
| | 19 | 50 | | InvalidateRuleConfiguration(); |
| | 19 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Clears any previously configured gas voxel rule. |
| | | 55 | | /// </summary> |
| | | 56 | | public static void ClearGasVoxelRule() |
| | | 57 | | { |
| | 48 | 58 | | State.GasVoxelRule = null; |
| | 48 | 59 | | InvalidateRuleConfiguration(); |
| | 48 | 60 | | } |
| | | 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 | | { |
| | 2 | 68 | | SetLiquidVoxelRule(static voxel => |
| | 2 | 69 | | voxel != null |
| | 2 | 70 | | && voxel.HasPartition<TPartition>()); |
| | 2 | 71 | | } |
| | | 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 | | { |
| | 8 | 78 | | State.LiquidVoxelRule = rule; |
| | 8 | 79 | | InvalidateRuleConfiguration(); |
| | 8 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Clears any previously configured liquid voxel rule. |
| | | 84 | | /// </summary> |
| | | 85 | | public static void ClearLiquidVoxelRule() |
| | | 86 | | { |
| | 45 | 87 | | State.LiquidVoxelRule = null; |
| | 45 | 88 | | InvalidateRuleConfiguration(); |
| | 45 | 89 | | } |
| | | 90 | | |
| | | 91 | | internal static void Reset() |
| | | 92 | | { |
| | 1632 | 93 | | if (!PathManager.TryGetActiveState(out _)) |
| | 0 | 94 | | return; |
| | | 95 | | |
| | 1632 | 96 | | State.GasVoxelRule = null; |
| | 1632 | 97 | | State.LiquidVoxelRule = null; |
| | 1632 | 98 | | InvalidateRuleConfiguration(); |
| | 1632 | 99 | | } |
| | | 100 | | |
| | | 101 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 102 | | internal static bool IsConfigured(TraversalMedium medium) |
| | | 103 | | { |
| | 8 | 104 | | return IsConfigured(PathManager.ActiveState, medium); |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 108 | | internal static bool IsConfigured(PathingWorldState pathingState, TraversalMedium medium) |
| | | 109 | | { |
| | 477 | 110 | | VolumeMediumRulesState state = pathingState.VolumeRulesState; |
| | 477 | 111 | | return medium switch |
| | 477 | 112 | | { |
| | 278 | 113 | | TraversalMedium.Gas => state.GasVoxelRule != null || PathManager.HasAuthoredVolumeMedium(pathingState, Trave |
| | 198 | 114 | | TraversalMedium.Liquid => state.LiquidVoxelRule != null || PathManager.HasAuthoredVolumeMedium(pathingState, |
| | 1 | 115 | | _ => false |
| | 477 | 116 | | }; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 120 | | internal static bool Matches(Voxel voxel, TraversalMedium medium) |
| | | 121 | | { |
| | 180 | 122 | | return Matches(PathManager.ActiveState, voxel, medium); |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 126 | | internal static bool Matches(PathingWorldState pathingState, Voxel voxel, TraversalMedium medium) |
| | | 127 | | { |
| | 4494 | 128 | | if (voxel == null) |
| | 1 | 129 | | return false; |
| | | 130 | | |
| | 4493 | 131 | | bool hasTrailblazerPartition = voxel.HasPartition<SolidChartPartition>(); |
| | 4493 | 132 | | bool hasAuthoredVolumeChartPartition = voxel.TryGetPartition(out VolumeChartPartition? volumeChartPartition) |
| | 4493 | 133 | | && volumeChartPartition != null; |
| | 4493 | 134 | | hasTrailblazerPartition |= hasAuthoredVolumeChartPartition; |
| | 4493 | 135 | | if (!hasTrailblazerPartition) |
| | 44 | 136 | | return false; |
| | | 137 | | |
| | 4449 | 138 | | VolumeMediumRulesState state = pathingState.VolumeRulesState; |
| | 4449 | 139 | | bool hostGasMatch = state.GasVoxelRule?.Invoke(voxel) == true; |
| | 4449 | 140 | | bool hostLiquidMatch = state.LiquidVoxelRule?.Invoke(voxel) == true; |
| | 4449 | 141 | | bool authoredGasMatch = volumeChartPartition?.SupportsMedium(TraversalMedium.Gas) == true; |
| | 4449 | 142 | | bool authoredLiquidMatch = volumeChartPartition?.SupportsMedium(TraversalMedium.Liquid) == true; |
| | | 143 | | |
| | | 144 | | // Host rules only add medium membership; they do not suppress authored media. |
| | 4449 | 145 | | return medium switch |
| | 4449 | 146 | | { |
| | 2622 | 147 | | TraversalMedium.Gas => authoredGasMatch || hostGasMatch, |
| | 1826 | 148 | | TraversalMedium.Liquid => authoredLiquidMatch || hostLiquidMatch, |
| | 1 | 149 | | _ => false |
| | 4449 | 150 | | }; |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | private static void InvalidateRuleConfiguration() |
| | | 154 | | { |
| | 1752 | 155 | | State.IncrementRegistryVersion(); |
| | 1752 | 156 | | TraversalTransitionRegistry.RefreshManagedManualTransitions(); |
| | 1752 | 157 | | PathGuideFactory.InvalidateVolumeCache(); |
| | 1752 | 158 | | } |
| | | 159 | | } |