| | | 1 | | //======================================================================= |
| | | 2 | | // GridDiagnosticChange.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 | | |
| | | 8 | | using FixedMathSharp; |
| | | 9 | | using GridForge.Spatial; |
| | | 10 | | using SwiftCollections.Utility; |
| | | 11 | | using System; |
| | | 12 | | |
| | | 13 | | namespace GridForge.Diagnostics; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Immutable descriptor for one diagnostic dirty change captured by a session. |
| | | 17 | | /// </summary> |
| | | 18 | | public readonly struct GridDiagnosticChange : |
| | | 19 | | IEquatable<GridDiagnosticChange>, |
| | | 20 | | IComparable<GridDiagnosticChange> |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Kind flags coalesced for this dirty change. |
| | | 24 | | /// </summary> |
| | | 25 | | public readonly GridDiagnosticChangeKind Kind; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Runtime token of the world that produced the change. |
| | | 29 | | /// </summary> |
| | | 30 | | public readonly int WorldSpawnToken; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// World-local grid slot affected by the change, or |
| | | 34 | | /// <see cref="ushort.MaxValue"/> for world-scoped changes. |
| | | 35 | | /// </summary> |
| | | 36 | | public readonly ushort GridIndex; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Runtime token of the grid instance affected by the change. |
| | | 40 | | /// </summary> |
| | | 41 | | public readonly int GridSpawnToken; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// World-scoped voxel identity for cell-scoped changes. |
| | | 45 | | /// </summary> |
| | | 46 | | public readonly WorldVoxelIndex WorldIndex; |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Topology-local voxel index for cell or sparse address-range changes. |
| | | 50 | | /// </summary> |
| | | 51 | | public readonly VoxelIndex VoxelIndex; |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Minimum world-space bounds affected by grid or range-scoped changes. |
| | | 55 | | /// </summary> |
| | | 56 | | public readonly Vector3d BoundsMin; |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Maximum world-space bounds affected by grid or range-scoped changes. |
| | | 60 | | /// </summary> |
| | | 61 | | public readonly Vector3d BoundsMax; |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Initializes a diagnostic dirty-change descriptor. |
| | | 65 | | /// </summary> |
| | | 66 | | public GridDiagnosticChange( |
| | | 67 | | GridDiagnosticChangeKind kind, |
| | | 68 | | int worldSpawnToken, |
| | | 69 | | ushort gridIndex, |
| | | 70 | | int gridSpawnToken, |
| | | 71 | | WorldVoxelIndex worldIndex, |
| | | 72 | | VoxelIndex voxelIndex, |
| | | 73 | | Vector3d boundsMin, |
| | | 74 | | Vector3d boundsMax) |
| | | 75 | | { |
| | 23 | 76 | | Kind = kind; |
| | 23 | 77 | | WorldSpawnToken = worldSpawnToken; |
| | 23 | 78 | | GridIndex = gridIndex; |
| | 23 | 79 | | GridSpawnToken = gridSpawnToken; |
| | 23 | 80 | | WorldIndex = worldIndex; |
| | 23 | 81 | | VoxelIndex = voxelIndex; |
| | 23 | 82 | | BoundsMin = boundsMin; |
| | 23 | 83 | | BoundsMax = boundsMax; |
| | 23 | 84 | | } |
| | | 85 | | |
| | | 86 | | internal GridDiagnosticChange WithKind(GridDiagnosticChangeKind kind) => |
| | 4 | 87 | | new( |
| | 4 | 88 | | kind, |
| | 4 | 89 | | WorldSpawnToken, |
| | 4 | 90 | | GridIndex, |
| | 4 | 91 | | GridSpawnToken, |
| | 4 | 92 | | WorldIndex, |
| | 4 | 93 | | VoxelIndex, |
| | 4 | 94 | | BoundsMin, |
| | 4 | 95 | | BoundsMax); |
| | | 96 | | |
| | | 97 | | /// <inheritdoc/> |
| | | 98 | | public int CompareTo(GridDiagnosticChange other) |
| | | 99 | | { |
| | 4 | 100 | | int result = WorldSpawnToken.CompareTo(other.WorldSpawnToken); |
| | 4 | 101 | | if (result != 0) |
| | 0 | 102 | | return result; |
| | | 103 | | |
| | 4 | 104 | | result = GetScopeOrder(this).CompareTo(GetScopeOrder(other)); |
| | 4 | 105 | | if (result != 0) |
| | 3 | 106 | | return result; |
| | | 107 | | |
| | 1 | 108 | | result = GridIndex.CompareTo(other.GridIndex); |
| | 1 | 109 | | if (result != 0) |
| | 0 | 110 | | return result; |
| | | 111 | | |
| | 1 | 112 | | result = GridSpawnToken.CompareTo(other.GridSpawnToken); |
| | 1 | 113 | | if (result != 0) |
| | 0 | 114 | | return result; |
| | | 115 | | |
| | 1 | 116 | | result = VoxelIndex.CompareTo(other.VoxelIndex); |
| | 1 | 117 | | if (result != 0) |
| | 1 | 118 | | return result; |
| | | 119 | | |
| | 0 | 120 | | result = CompareVector(BoundsMin, other.BoundsMin); |
| | 0 | 121 | | if (result != 0) |
| | 0 | 122 | | return result; |
| | | 123 | | |
| | 0 | 124 | | result = CompareVector(BoundsMax, other.BoundsMax); |
| | 0 | 125 | | if (result != 0) |
| | 0 | 126 | | return result; |
| | | 127 | | |
| | 0 | 128 | | return ((int)Kind).CompareTo((int)other.Kind); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <inheritdoc/> |
| | | 132 | | public bool Equals(GridDiagnosticChange other) => |
| | 0 | 133 | | Kind == other.Kind |
| | 0 | 134 | | && WorldSpawnToken == other.WorldSpawnToken |
| | 0 | 135 | | && GridIndex == other.GridIndex |
| | 0 | 136 | | && GridSpawnToken == other.GridSpawnToken |
| | 0 | 137 | | && WorldIndex.Equals(other.WorldIndex) |
| | 0 | 138 | | && VoxelIndex.Equals(other.VoxelIndex) |
| | 0 | 139 | | && BoundsMin.Equals(other.BoundsMin) |
| | 0 | 140 | | && BoundsMax.Equals(other.BoundsMax); |
| | | 141 | | |
| | | 142 | | /// <inheritdoc/> |
| | 0 | 143 | | public override bool Equals(object? obj) => obj is GridDiagnosticChange other && Equals(other); |
| | | 144 | | |
| | | 145 | | /// <inheritdoc/> |
| | | 146 | | public override int GetHashCode() |
| | | 147 | | { |
| | 0 | 148 | | int hash = SwiftHashTools.CombineHashCodes((int)Kind, WorldSpawnToken); |
| | 0 | 149 | | hash = SwiftHashTools.CombineHashCodes(hash, GridIndex); |
| | 0 | 150 | | hash = SwiftHashTools.CombineHashCodes(hash, GridSpawnToken); |
| | 0 | 151 | | hash = SwiftHashTools.CombineHashCodes(hash, WorldIndex.GetHashCode()); |
| | 0 | 152 | | hash = SwiftHashTools.CombineHashCodes(hash, VoxelIndex.GetHashCode()); |
| | 0 | 153 | | hash = SwiftHashTools.CombineHashCodes(hash, BoundsMin.GetHashCode()); |
| | 0 | 154 | | return SwiftHashTools.CombineHashCodes(hash, BoundsMax.GetHashCode()); |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | private static int GetScopeOrder(GridDiagnosticChange change) |
| | | 158 | | { |
| | 8 | 159 | | if ((change.Kind & GridDiagnosticChangeKind.WorldReset) != 0) |
| | 0 | 160 | | return 0; |
| | | 161 | | |
| | 8 | 162 | | if (change.WorldIndex.WorldSpawnToken != 0 || change.WorldIndex.VoxelIndex.IsAllocated) |
| | 5 | 163 | | return 2; |
| | | 164 | | |
| | 3 | 165 | | if ((change.Kind & GridDiagnosticChangeKind.SparseAddressChanged) != 0) |
| | 2 | 166 | | return 3; |
| | | 167 | | |
| | 1 | 168 | | return 1; |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | private static int CompareVector(Vector3d left, Vector3d right) |
| | | 172 | | { |
| | 0 | 173 | | int result = CompareFixed(left.X, right.X); |
| | 0 | 174 | | if (result != 0) |
| | 0 | 175 | | return result; |
| | | 176 | | |
| | 0 | 177 | | result = CompareFixed(left.Y, right.Y); |
| | 0 | 178 | | return result != 0 ? result : CompareFixed(left.Z, right.Z); |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | private static int CompareFixed(Fixed64 left, Fixed64 right) |
| | | 182 | | { |
| | 0 | 183 | | if (left < right) |
| | 0 | 184 | | return -1; |
| | | 185 | | |
| | 0 | 186 | | return left > right ? 1 : 0; |
| | | 187 | | } |
| | | 188 | | } |