| | | 1 | | using FixedMathSharp; |
| | | 2 | | using SwiftCollections; |
| | | 3 | | using System; |
| | | 4 | | |
| | | 5 | | namespace Trailblazer.Heightmaps; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Context-owned API for registering and sampling deterministic heightmap layers. |
| | | 9 | | /// </summary> |
| | | 10 | | public sealed class TrailblazerHeightmapService |
| | | 11 | | { |
| | | 12 | | private readonly TrailblazerWorldContext _context; |
| | | 13 | | |
| | 961 | 14 | | internal TrailblazerHeightmapService(TrailblazerWorldContext context) |
| | | 15 | | { |
| | 961 | 16 | | _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | 961 | 17 | | State = new HeightmapWorldState(); |
| | 961 | 18 | | } |
| | | 19 | | |
| | | 20 | | internal HeightmapWorldState State { get; } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Registers a heightmap layer for this context. |
| | | 24 | | /// </summary> |
| | | 25 | | public bool Register( |
| | | 26 | | HeightmapSurface surface, |
| | | 27 | | Fixed64 minSelectionY, |
| | | 28 | | Fixed64 maxSelectionY, |
| | | 29 | | int priority = 0) |
| | | 30 | | { |
| | 37 | 31 | | EnsureUsable(); |
| | 37 | 32 | | if (surface == null) |
| | 1 | 33 | | throw new ArgumentNullException(nameof(surface)); |
| | 36 | 34 | | if (maxSelectionY <= minSelectionY) |
| | 1 | 35 | | throw new ArgumentOutOfRangeException(nameof(maxSelectionY), "Maximum selection Y must be greater than minim |
| | 35 | 36 | | if (State.LayersByName.ContainsKey(surface.Name)) |
| | 1 | 37 | | return false; |
| | | 38 | | |
| | 34 | 39 | | var registration = new HeightmapLayerRegistration( |
| | 34 | 40 | | surface, |
| | 34 | 41 | | minSelectionY, |
| | 34 | 42 | | maxSelectionY, |
| | 34 | 43 | | priority, |
| | 34 | 44 | | State.NextRegistrationOrder++); |
| | 34 | 45 | | State.LayersByName[surface.Name] = registration; |
| | 34 | 46 | | return true; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Unregisters a heightmap layer by name. |
| | | 51 | | /// </summary> |
| | | 52 | | public bool Unregister(string layerName) |
| | | 53 | | { |
| | 3 | 54 | | EnsureUsable(); |
| | 3 | 55 | | if (string.IsNullOrWhiteSpace(layerName)) |
| | 1 | 56 | | return false; |
| | | 57 | | |
| | 2 | 58 | | return State.LayersByName.Remove(layerName); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Returns true when a layer with the supplied name is registered. |
| | | 63 | | /// </summary> |
| | | 64 | | public bool IsRegistered(string layerName) |
| | | 65 | | { |
| | 8 | 66 | | EnsureUsable(); |
| | 6 | 67 | | return !string.IsNullOrWhiteSpace(layerName) && State.LayersByName.ContainsKey(layerName); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Attempts to retrieve context-local registration metadata for a layer. |
| | | 72 | | /// </summary> |
| | | 73 | | public bool TryGetRegistration(string layerName, out HeightmapLayerRegistration registration) |
| | | 74 | | { |
| | 2 | 75 | | EnsureUsable(); |
| | 2 | 76 | | if (!string.IsNullOrWhiteSpace(layerName) |
| | 2 | 77 | | && State.LayersByName.TryGetValue(layerName, out HeightmapLayerRegistration found)) |
| | | 78 | | { |
| | 1 | 79 | | registration = found; |
| | 1 | 80 | | return true; |
| | | 81 | | } |
| | | 82 | | |
| | 1 | 83 | | registration = null!; |
| | 1 | 84 | | return false; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Attempts to sample the best deterministic registered layer that contains the query X/Z and contact Y. |
| | | 89 | | /// </summary> |
| | | 90 | | public bool TrySampleGround(Vector3d worldPosition, out HeightmapSample sample) |
| | | 91 | | { |
| | 8 | 92 | | return TrySampleGround(worldPosition, null, out sample); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Attempts to sample the preferred layer when valid, then falls back to deterministic candidate selection. |
| | | 97 | | /// </summary> |
| | | 98 | | public bool TrySampleGround(Vector3d worldPosition, string? preferredLayerName, out HeightmapSample sample) |
| | | 99 | | { |
| | 22 | 100 | | EnsureUsable(); |
| | | 101 | | |
| | 22 | 102 | | if (!string.IsNullOrWhiteSpace(preferredLayerName) |
| | 22 | 103 | | && State.LayersByName.TryGetValue(preferredLayerName, out HeightmapLayerRegistration preferredRegistration) |
| | 22 | 104 | | && TrySampleRegistration(preferredRegistration, worldPosition, out Fixed64 preferredGroundY, out Fixed64 pre |
| | | 105 | | { |
| | 3 | 106 | | sample = CreateSample(preferredRegistration, worldPosition, preferredGroundY, preferredDistance); |
| | 3 | 107 | | return true; |
| | | 108 | | } |
| | | 109 | | |
| | 19 | 110 | | bool hasBest = false; |
| | 19 | 111 | | HeightmapLayerRegistration? bestRegistration = null; |
| | 19 | 112 | | Fixed64 bestGroundY = Fixed64.Zero; |
| | 19 | 113 | | Fixed64 bestDistance = Fixed64.Zero; |
| | 88 | 114 | | foreach (HeightmapLayerRegistration registration in State.LayersByName.Values) |
| | | 115 | | { |
| | 25 | 116 | | if (!TrySampleRegistration(registration, worldPosition, out Fixed64 groundY, out Fixed64 distance)) |
| | | 117 | | continue; |
| | | 118 | | |
| | 20 | 119 | | if (hasBest && !IsBetterCandidate(registration, distance, bestRegistration!, bestDistance)) |
| | | 120 | | continue; |
| | | 121 | | |
| | 18 | 122 | | hasBest = true; |
| | 18 | 123 | | bestRegistration = registration; |
| | 18 | 124 | | bestGroundY = groundY; |
| | 18 | 125 | | bestDistance = distance; |
| | | 126 | | } |
| | | 127 | | |
| | 19 | 128 | | if (!hasBest || bestRegistration == null) |
| | | 129 | | { |
| | 2 | 130 | | sample = default; |
| | 2 | 131 | | return false; |
| | | 132 | | } |
| | | 133 | | |
| | 17 | 134 | | sample = CreateSample(bestRegistration, worldPosition, bestGroundY, bestDistance); |
| | 17 | 135 | | return true; |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Clears all heightmap layers registered to this context. |
| | | 140 | | /// </summary> |
| | | 141 | | public void Reset() |
| | | 142 | | { |
| | 25 | 143 | | EnsureUsable(); |
| | 25 | 144 | | State.Reset(); |
| | 25 | 145 | | } |
| | | 146 | | |
| | | 147 | | internal void Dispose() |
| | | 148 | | { |
| | 961 | 149 | | State.Reset(); |
| | 961 | 150 | | } |
| | | 151 | | |
| | | 152 | | private static bool TrySampleRegistration( |
| | | 153 | | HeightmapLayerRegistration registration, |
| | | 154 | | Vector3d worldPosition, |
| | | 155 | | out Fixed64 groundY, |
| | | 156 | | out Fixed64 distance) |
| | | 157 | | { |
| | 30 | 158 | | if (!registration.ContainsSelectionY(worldPosition.y) |
| | 30 | 159 | | || !registration.Surface.TrySampleGround(worldPosition, out groundY)) |
| | | 160 | | { |
| | 7 | 161 | | groundY = Fixed64.Zero; |
| | 7 | 162 | | distance = Fixed64.Zero; |
| | 7 | 163 | | return false; |
| | | 164 | | } |
| | | 165 | | |
| | 23 | 166 | | distance = (worldPosition.y - groundY).Abs(); |
| | 23 | 167 | | return true; |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | private static HeightmapSample CreateSample( |
| | | 171 | | HeightmapLayerRegistration registration, |
| | | 172 | | Vector3d worldPosition, |
| | | 173 | | Fixed64 groundY, |
| | | 174 | | Fixed64 distance) |
| | | 175 | | { |
| | 20 | 176 | | return new HeightmapSample( |
| | 20 | 177 | | registration.LayerName, |
| | 20 | 178 | | worldPosition, |
| | 20 | 179 | | groundY, |
| | 20 | 180 | | distance); |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | private static bool IsBetterCandidate( |
| | | 184 | | HeightmapLayerRegistration candidate, |
| | | 185 | | Fixed64 candidateDistance, |
| | | 186 | | HeightmapLayerRegistration current, |
| | | 187 | | Fixed64 currentDistance) |
| | | 188 | | { |
| | 3 | 189 | | if (candidateDistance != currentDistance) |
| | 1 | 190 | | return candidateDistance < currentDistance; |
| | | 191 | | |
| | 2 | 192 | | if (candidate.Priority != current.Priority) |
| | 1 | 193 | | return candidate.Priority > current.Priority; |
| | | 194 | | |
| | 1 | 195 | | return candidate.RegistrationOrder < current.RegistrationOrder; |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | private void EnsureUsable() |
| | | 199 | | { |
| | 97 | 200 | | SwiftThrowHelper.ThrowIfDisposed(_context.IsDisposed, nameof(TrailblazerWorldContext)); |
| | 96 | 201 | | if (!_context.World.IsActive) |
| | 1 | 202 | | throw new InvalidOperationException("TrailblazerHeightmapService is bound to an inactive GridWorld."); |
| | 95 | 203 | | } |
| | | 204 | | } |