| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationGridChangeIngress.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 System; |
| | | 9 | | using GridForge.Grids; |
| | | 10 | | using GridForge.Spatial; |
| | | 11 | | using SwiftCollections; |
| | | 12 | | using SwiftCollections.Utility; |
| | | 13 | | |
| | | 14 | | namespace Trailblazer.Pathing; |
| | | 15 | | |
| | | 16 | | /// <summary>Buffers the single GridForge committed final-state feed without running graph work in callbacks.</summary> |
| | | 17 | | internal sealed unsafe class NavigationGridChangeIngress |
| | | 18 | | { |
| | | 19 | | private const int BaseRetainedBytes = 384; |
| | 1 | 20 | | private static readonly int EventSlotBytes = sizeof(GridEventInfo) + (2 * sizeof(int)); |
| | 1 | 21 | | private static readonly int DictionaryEntryBytes = AlignToEight( |
| | 1 | 22 | | sizeof(EventKey) + (2 * sizeof(int)) + sizeof(bool)); |
| | 1 | 23 | | private static readonly int ScopeEntryBytes = AlignToEight( |
| | 1 | 24 | | sizeof(NavigationGridChangeScope) + sizeof(int)); |
| | 1214 | 25 | | private readonly object _sync = new(); |
| | | 26 | | private readonly GridEventInfo[] _events; |
| | | 27 | | private readonly int[] _next; |
| | | 28 | | private readonly int[] _previous; |
| | | 29 | | private readonly SwiftDictionary<EventKey, int> _coalescedIndexes; |
| | | 30 | | private readonly ScopeEntry[] _scopes; |
| | 1214 | 31 | | private int _head = -1; |
| | 1214 | 32 | | private int _tail = -1; |
| | | 33 | | private int _freeHead; |
| | | 34 | | private int _count; |
| | | 35 | | private int _scopeCount; |
| | | 36 | | private int _topologyLifecycleCount; |
| | | 37 | | private bool _scopeTrackingAll; |
| | | 38 | | private bool _overflowed; |
| | | 39 | | private bool _overflowedTopologyLifecycle; |
| | | 40 | | private bool _disposed; |
| | | 41 | | |
| | 1214 | 42 | | internal NavigationGridChangeIngress(int capacity, int maximumScopes = 16) |
| | | 43 | | { |
| | 1214 | 44 | | _events = new GridEventInfo[capacity]; |
| | 1214 | 45 | | _next = new int[capacity]; |
| | 1214 | 46 | | _previous = new int[capacity]; |
| | 1214 | 47 | | int indexCapacity = checked(((capacity * 100) / 82) + 1); |
| | 1214 | 48 | | _coalescedIndexes = new SwiftDictionary<EventKey, int>(indexCapacity); |
| | 1214 | 49 | | _scopes = new ScopeEntry[maximumScopes]; |
| | 1214 | 50 | | ResetSlots(); |
| | 1214 | 51 | | } |
| | | 52 | | |
| | 3 | 53 | | internal int IndexCapacity => _coalescedIndexes.Capacity; |
| | | 54 | | |
| | | 55 | | internal bool HasPendingWork |
| | | 56 | | { |
| | | 57 | | get |
| | | 58 | | { |
| | 6 | 59 | | lock (_sync) |
| | 6 | 60 | | return _overflowed || _count != 0; |
| | 6 | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | 1 | 64 | | internal long GetRetainedBytes() => GetRetainedBytes(_events.Length, _scopes.Length); |
| | | 65 | | |
| | | 66 | | internal static int GetMaximumCapacity( |
| | | 67 | | int maximumEntries, |
| | | 68 | | long maximumBytes, |
| | | 69 | | int maximumScopes = 16) |
| | | 70 | | { |
| | 1199 | 71 | | int low = 0; |
| | 1199 | 72 | | int high = maximumEntries; |
| | 17739 | 73 | | while (low < high) |
| | | 74 | | { |
| | 16540 | 75 | | int middle = low + ((high - low + 1) >> 1); |
| | 16540 | 76 | | if (GetRetainedBytes(middle, maximumScopes) <= maximumBytes) |
| | 9419 | 77 | | low = middle; |
| | | 78 | | else |
| | 7121 | 79 | | high = middle - 1; |
| | | 80 | | } |
| | 1199 | 81 | | return low; |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | internal static long GetRetainedBytes(int capacity, int maximumScopes = 16) |
| | | 85 | | { |
| | 16542 | 86 | | int indexCapacity = GetIndexCapacity(capacity); |
| | 16542 | 87 | | return checked( |
| | 16542 | 88 | | BaseRetainedBytes |
| | 16542 | 89 | | + ((long)capacity * EventSlotBytes) |
| | 16542 | 90 | | + ((long)indexCapacity * DictionaryEntryBytes) |
| | 16542 | 91 | | + ((long)maximumScopes * ScopeEntryBytes)); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | internal void Enqueue(GridEventInfo eventInfo) |
| | | 95 | | { |
| | 1292 | 96 | | lock (_sync) |
| | | 97 | | { |
| | 1292 | 98 | | if (_disposed) |
| | 1 | 99 | | return; |
| | 1291 | 100 | | if (_overflowed) |
| | | 101 | | { |
| | 3 | 102 | | TrackScope(eventInfo); |
| | 3 | 103 | | _overflowedTopologyLifecycle |= IsTopologyLifecycle(eventInfo); |
| | 3 | 104 | | return; |
| | | 105 | | } |
| | 1288 | 106 | | if (EventKey.TryCreate(eventInfo, out EventKey key) |
| | 1288 | 107 | | && _coalescedIndexes.TryGetValue(key, out int existing)) |
| | | 108 | | { |
| | 3 | 109 | | Unlink(existing); |
| | 3 | 110 | | _events[existing] = eventInfo; |
| | 3 | 111 | | Append(existing); |
| | 3 | 112 | | return; |
| | | 113 | | } |
| | 1285 | 114 | | if (_count == _events.Length) |
| | | 115 | | { |
| | 11 | 116 | | _overflowed = true; |
| | 11 | 117 | | TrackScope(eventInfo); |
| | 11 | 118 | | _overflowedTopologyLifecycle = IsTopologyLifecycle(eventInfo); |
| | 11 | 119 | | return; |
| | | 120 | | } |
| | 1274 | 121 | | int insertion = _freeHead; |
| | 1274 | 122 | | _freeHead = _next[insertion]; |
| | 1274 | 123 | | _events[insertion] = eventInfo; |
| | 1274 | 124 | | Append(insertion); |
| | 1274 | 125 | | _count++; |
| | 1274 | 126 | | if (IsTopologyLifecycle(eventInfo)) |
| | 1108 | 127 | | _topologyLifecycleCount++; |
| | 1274 | 128 | | if (key.IsValid) |
| | 162 | 129 | | _coalescedIndexes[key] = insertion; |
| | 1274 | 130 | | TrackScope(eventInfo); |
| | 1274 | 131 | | } |
| | 1292 | 132 | | } |
| | | 133 | | |
| | | 134 | | internal int DetachInto( |
| | | 135 | | Span<GridEventInfo> destination, |
| | | 136 | | Span<NavigationGridChangeScope> blockedScopes, |
| | | 137 | | out int blockedScopeCount, |
| | 15 | 138 | | out bool blockAll) => DetachInto( |
| | 15 | 139 | | destination, |
| | 15 | 140 | | blockedScopes, |
| | 15 | 141 | | out blockedScopeCount, |
| | 15 | 142 | | out blockAll, |
| | 15 | 143 | | out _); |
| | | 144 | | |
| | | 145 | | internal int DetachInto( |
| | | 146 | | Span<GridEventInfo> destination, |
| | | 147 | | Span<NavigationGridChangeScope> blockedScopes, |
| | | 148 | | out int blockedScopeCount, |
| | | 149 | | out bool blockAll, |
| | | 150 | | out bool topologyLifecycleCoverageLost) |
| | | 151 | | { |
| | 20285 | 152 | | lock (_sync) |
| | | 153 | | { |
| | 20285 | 154 | | if (_overflowed) |
| | | 155 | | { |
| | 43 | 156 | | topologyLifecycleCoverageLost = _topologyLifecycleCount != 0 |
| | 43 | 157 | | || _overflowedTopologyLifecycle; |
| | 43 | 158 | | blockedScopeCount = CopyBlockedScopes(blockedScopes, out blockAll); |
| | 43 | 159 | | _coalescedIndexes.Clear(); |
| | 43 | 160 | | ResetSlots(); |
| | 43 | 161 | | return 0; |
| | | 162 | | } |
| | | 163 | | |
| | 20242 | 164 | | int count = _count < destination.Length ? _count : destination.Length; |
| | 42642 | 165 | | for (int i = 0; i < count; i++) |
| | | 166 | | { |
| | 1079 | 167 | | int slot = _head; |
| | 1079 | 168 | | GridEventInfo eventInfo = _events[slot]; |
| | 1079 | 169 | | destination[i] = eventInfo; |
| | 1079 | 170 | | Unlink(slot); |
| | 1079 | 171 | | if (EventKey.TryCreate(eventInfo, out EventKey key) |
| | 1079 | 172 | | && _coalescedIndexes.TryGetValue(key, out int indexedSlot) |
| | 1079 | 173 | | && indexedSlot == slot) |
| | | 174 | | { |
| | 148 | 175 | | _coalescedIndexes.Remove(key); |
| | | 176 | | } |
| | 1079 | 177 | | UntrackScope(eventInfo); |
| | 1079 | 178 | | if (IsTopologyLifecycle(eventInfo)) |
| | 928 | 179 | | _topologyLifecycleCount--; |
| | 1079 | 180 | | _next[slot] = _freeHead; |
| | 1079 | 181 | | _previous[slot] = -1; |
| | 1079 | 182 | | _freeHead = slot; |
| | | 183 | | } |
| | 20242 | 184 | | _count -= count; |
| | 20242 | 185 | | topologyLifecycleCoverageLost = false; |
| | 20242 | 186 | | blockAll = false; |
| | 20242 | 187 | | blockedScopeCount = _count > 0 |
| | 20242 | 188 | | ? CopyBlockedScopes(blockedScopes, out blockAll) |
| | 20242 | 189 | | : 0; |
| | 20242 | 190 | | if (_count == 0) |
| | | 191 | | { |
| | 20237 | 192 | | blockAll = false; |
| | 20237 | 193 | | _scopeCount = 0; |
| | 20237 | 194 | | _scopeTrackingAll = false; |
| | | 195 | | } |
| | 20242 | 196 | | return count; |
| | | 197 | | } |
| | 20285 | 198 | | } |
| | | 199 | | |
| | | 200 | | /// <summary> |
| | | 201 | | /// Restores an unpublished committed prefix ahead of changes that arrived after detachment. |
| | | 202 | | /// Newer same-address final states win without moving backward in committed sequence order. |
| | | 203 | | /// </summary> |
| | | 204 | | internal void RequeuePrefix(ReadOnlySpan<GridEventInfo> prefix) |
| | | 205 | | { |
| | 10866 | 206 | | if (prefix.IsEmpty) |
| | 10778 | 207 | | return; |
| | 88 | 208 | | lock (_sync) |
| | | 209 | | { |
| | 88 | 210 | | if (_disposed) |
| | 1 | 211 | | return; |
| | 87 | 212 | | if (_overflowed || !IsOrderedBeforeCurrent(prefix)) |
| | | 213 | | { |
| | 22 | 214 | | for (int i = 0; i < prefix.Length; i++) |
| | | 215 | | { |
| | 6 | 216 | | TrackScope(prefix[i]); |
| | 6 | 217 | | _overflowedTopologyLifecycle |= IsTopologyLifecycle(prefix[i]); |
| | | 218 | | } |
| | 5 | 219 | | _overflowed = true; |
| | 5 | 220 | | return; |
| | | 221 | | } |
| | | 222 | | |
| | 82 | 223 | | int insertionCount = 0; |
| | 372 | 224 | | for (int i = 0; i < prefix.Length; i++) |
| | | 225 | | { |
| | 104 | 226 | | if (!EventKey.TryCreate(prefix[i], out EventKey key) |
| | 104 | 227 | | || !_coalescedIndexes.ContainsKey(key)) |
| | | 228 | | { |
| | 103 | 229 | | insertionCount++; |
| | | 230 | | continue; |
| | | 231 | | } |
| | | 232 | | } |
| | 82 | 233 | | if (insertionCount > _events.Length - _count) |
| | | 234 | | { |
| | 6 | 235 | | for (int i = 0; i < prefix.Length; i++) |
| | | 236 | | { |
| | 2 | 237 | | TrackScope(prefix[i]); |
| | 2 | 238 | | _overflowedTopologyLifecycle |= IsTopologyLifecycle(prefix[i]); |
| | | 239 | | } |
| | 1 | 240 | | _overflowed = true; |
| | 1 | 241 | | return; |
| | | 242 | | } |
| | | 243 | | |
| | 366 | 244 | | for (int i = prefix.Length - 1; i >= 0; i--) |
| | | 245 | | { |
| | 102 | 246 | | GridEventInfo eventInfo = prefix[i]; |
| | 102 | 247 | | bool keyed = EventKey.TryCreate(eventInfo, out EventKey key); |
| | 102 | 248 | | if (keyed && _coalescedIndexes.ContainsKey(key)) |
| | | 249 | | continue; |
| | 101 | 250 | | int insertion = _freeHead; |
| | 101 | 251 | | _freeHead = _next[insertion]; |
| | 101 | 252 | | _events[insertion] = eventInfo; |
| | 101 | 253 | | Prepend(insertion); |
| | 101 | 254 | | _count++; |
| | 101 | 255 | | if (IsTopologyLifecycle(eventInfo)) |
| | 27 | 256 | | _topologyLifecycleCount++; |
| | 101 | 257 | | if (keyed) |
| | 73 | 258 | | _coalescedIndexes[key] = insertion; |
| | 101 | 259 | | TrackScope(eventInfo); |
| | | 260 | | } |
| | 81 | 261 | | } |
| | 88 | 262 | | } |
| | | 263 | | |
| | | 264 | | internal void MarkResnapshotRequired() |
| | | 265 | | { |
| | 31 | 266 | | lock (_sync) |
| | | 267 | | { |
| | 31 | 268 | | _scopeTrackingAll = true; |
| | 31 | 269 | | _overflowed = true; |
| | 31 | 270 | | } |
| | 31 | 271 | | } |
| | | 272 | | |
| | | 273 | | internal void Reset() |
| | | 274 | | { |
| | 24 | 275 | | lock (_sync) |
| | | 276 | | { |
| | 24 | 277 | | _coalescedIndexes.Clear(); |
| | 24 | 278 | | ResetSlots(); |
| | 24 | 279 | | } |
| | 24 | 280 | | } |
| | | 281 | | |
| | | 282 | | internal void Dispose() |
| | | 283 | | { |
| | 1198 | 284 | | lock (_sync) |
| | | 285 | | { |
| | 1198 | 286 | | _disposed = true; |
| | 1198 | 287 | | _coalescedIndexes.Clear(); |
| | 1198 | 288 | | ResetSlots(); |
| | 1198 | 289 | | } |
| | 1198 | 290 | | } |
| | | 291 | | |
| | | 292 | | private void Append(int slot) |
| | | 293 | | { |
| | 1277 | 294 | | _previous[slot] = _tail; |
| | 1277 | 295 | | _next[slot] = -1; |
| | 1277 | 296 | | if (_tail >= 0) |
| | 310 | 297 | | _next[_tail] = slot; |
| | | 298 | | else |
| | 967 | 299 | | _head = slot; |
| | 1277 | 300 | | _tail = slot; |
| | 1277 | 301 | | } |
| | | 302 | | |
| | | 303 | | private void Prepend(int slot) |
| | | 304 | | { |
| | 101 | 305 | | _previous[slot] = -1; |
| | 101 | 306 | | _next[slot] = _head; |
| | 101 | 307 | | if (_head >= 0) |
| | 22 | 308 | | _previous[_head] = slot; |
| | | 309 | | else |
| | 79 | 310 | | _tail = slot; |
| | 101 | 311 | | _head = slot; |
| | 101 | 312 | | } |
| | | 313 | | |
| | | 314 | | private bool IsOrderedBeforeCurrent(ReadOnlySpan<GridEventInfo> prefix) |
| | | 315 | | { |
| | 210 | 316 | | for (int i = 1; i < prefix.Length; i++) |
| | | 317 | | { |
| | 23 | 318 | | if (prefix[i].ChangeSequence <= prefix[i - 1].ChangeSequence) |
| | 1 | 319 | | return false; |
| | | 320 | | } |
| | 82 | 321 | | return _head < 0 || prefix[prefix.Length - 1].ChangeSequence < _events[_head].ChangeSequence; |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | private void Unlink(int slot) |
| | | 325 | | { |
| | 1082 | 326 | | int previous = _previous[slot]; |
| | 1082 | 327 | | int next = _next[slot]; |
| | 1082 | 328 | | if (previous >= 0) |
| | 1 | 329 | | _next[previous] = next; |
| | | 330 | | else |
| | 1081 | 331 | | _head = next; |
| | 1082 | 332 | | if (next >= 0) |
| | 262 | 333 | | _previous[next] = previous; |
| | | 334 | | else |
| | 820 | 335 | | _tail = previous; |
| | 820 | 336 | | } |
| | | 337 | | |
| | | 338 | | private void ResetSlots() |
| | | 339 | | { |
| | 2479 | 340 | | _head = -1; |
| | 2479 | 341 | | _tail = -1; |
| | 2479 | 342 | | _count = 0; |
| | 2479 | 343 | | _scopeCount = 0; |
| | 2479 | 344 | | _topologyLifecycleCount = 0; |
| | 2479 | 345 | | _scopeTrackingAll = false; |
| | 2479 | 346 | | _overflowed = false; |
| | 2479 | 347 | | _overflowedTopologyLifecycle = false; |
| | 2479 | 348 | | _freeHead = _events.Length == 0 ? -1 : 0; |
| | 64006660 | 349 | | for (int i = 0; i < _events.Length; i++) |
| | | 350 | | { |
| | 32000851 | 351 | | _previous[i] = -1; |
| | 32000851 | 352 | | _next[i] = i + 1 < _events.Length ? i + 1 : -1; |
| | | 353 | | } |
| | 2479 | 354 | | } |
| | | 355 | | |
| | | 356 | | private static bool IsTopologyLifecycle(in GridEventInfo eventInfo) => |
| | 2476 | 357 | | eventInfo.ChangeKind == GridEventKind.GridAdded |
| | 2476 | 358 | | || eventInfo.ChangeKind == GridEventKind.GridRemoved |
| | 2476 | 359 | | || eventInfo.ChangeKind == GridEventKind.WorldReset; |
| | | 360 | | |
| | | 361 | | private static int GetIndexCapacity(int capacity) |
| | | 362 | | { |
| | 16542 | 363 | | int requested = checked(((capacity * 100) / 82) + 1); |
| | 16542 | 364 | | int result = SwiftDictionary<EventKey, int>.DefaultCapacity; |
| | 204380 | 365 | | while (result < requested) |
| | 187838 | 366 | | result = checked(result * 2); |
| | 16542 | 367 | | return result; |
| | | 368 | | } |
| | | 369 | | |
| | 2 | 370 | | private static int AlignToEight(int value) => (value + 7) & ~7; |
| | | 371 | | |
| | | 372 | | private void TrackScope(in GridEventInfo eventInfo) |
| | | 373 | | { |
| | 1397 | 374 | | if (_scopeTrackingAll) |
| | 5 | 375 | | return; |
| | 1392 | 376 | | if (eventInfo.ChangeKind == GridEventKind.WorldReset) |
| | | 377 | | { |
| | 7 | 378 | | _scopeTrackingAll = true; |
| | 7 | 379 | | return; |
| | | 380 | | } |
| | | 381 | | |
| | 1385 | 382 | | var scope = new NavigationGridChangeScope(eventInfo); |
| | 3604 | 383 | | for (int i = 0; i < _scopeCount; i++) |
| | | 384 | | { |
| | 518 | 385 | | if (!_scopes[i].Scope.Equals(scope)) |
| | | 386 | | continue; |
| | 101 | 387 | | _scopes[i].Scope = scope; |
| | 101 | 388 | | _scopes[i].Count++; |
| | 101 | 389 | | return; |
| | | 390 | | } |
| | 1284 | 391 | | if (_scopeCount == _scopes.Length) |
| | | 392 | | { |
| | 1 | 393 | | _scopeTrackingAll = true; |
| | 1 | 394 | | return; |
| | | 395 | | } |
| | 1283 | 396 | | _scopes[_scopeCount++] = new ScopeEntry(scope, 1); |
| | 1283 | 397 | | } |
| | | 398 | | |
| | | 399 | | private void UntrackScope(in GridEventInfo eventInfo) |
| | | 400 | | { |
| | 1079 | 401 | | if (_scopeTrackingAll || eventInfo.ChangeKind == GridEventKind.WorldReset) |
| | 2 | 402 | | return; |
| | 1077 | 403 | | var scope = new NavigationGridChangeScope(eventInfo); |
| | 1077 | 404 | | int matchingScopeIndex = _scopes.Length; |
| | 1077 | 405 | | for (int i = 0; |
| | 2285 | 406 | | i < _scopeCount && matchingScopeIndex == _scopes.Length; |
| | 1208 | 407 | | i++) |
| | | 408 | | { |
| | 1208 | 409 | | if (_scopes[i].Scope.Equals(scope)) |
| | 1077 | 410 | | matchingScopeIndex = i; |
| | | 411 | | } |
| | | 412 | | System.Diagnostics.Debug.Assert( |
| | | 413 | | matchingScopeIndex < _scopeCount, |
| | | 414 | | "Every retained non-global event owns one matching tracked scope."); |
| | 1077 | 415 | | if (--_scopes[matchingScopeIndex].Count > 0) |
| | 23 | 416 | | return; |
| | 1054 | 417 | | _scopes[matchingScopeIndex] = _scopes[--_scopeCount]; |
| | 1054 | 418 | | _scopes[_scopeCount] = default; |
| | 1054 | 419 | | } |
| | | 420 | | |
| | | 421 | | private int CopyBlockedScopes( |
| | | 422 | | Span<NavigationGridChangeScope> destination, |
| | | 423 | | out bool blockAll) |
| | | 424 | | { |
| | 48 | 425 | | blockAll = _scopeTrackingAll || _scopeCount > destination.Length; |
| | 48 | 426 | | if (blockAll) |
| | 32 | 427 | | return 0; |
| | 74 | 428 | | for (int i = 0; i < _scopeCount; i++) |
| | 21 | 429 | | destination[i] = _scopes[i].Scope; |
| | 16 | 430 | | return _scopeCount; |
| | | 431 | | } |
| | | 432 | | |
| | | 433 | | private struct ScopeEntry |
| | | 434 | | { |
| | | 435 | | internal ScopeEntry(NavigationGridChangeScope scope, int count) |
| | | 436 | | { |
| | 1283 | 437 | | Scope = scope; |
| | 1283 | 438 | | Count = count; |
| | 1283 | 439 | | } |
| | | 440 | | |
| | | 441 | | internal NavigationGridChangeScope Scope; |
| | | 442 | | internal int Count; |
| | | 443 | | } |
| | | 444 | | |
| | | 445 | | private readonly struct EventKey : IEquatable<EventKey> |
| | | 446 | | { |
| | | 447 | | private EventKey( |
| | | 448 | | long worldSpawnToken, |
| | | 449 | | ushort gridIndex, |
| | | 450 | | long gridSpawnToken, |
| | | 451 | | VoxelIndex index) |
| | | 452 | | { |
| | 468 | 453 | | WorldSpawnToken = worldSpawnToken; |
| | 468 | 454 | | GridIndex = gridIndex; |
| | 468 | 455 | | GridSpawnToken = gridSpawnToken; |
| | 468 | 456 | | Index = index; |
| | 468 | 457 | | IsValid = true; |
| | 468 | 458 | | } |
| | | 459 | | |
| | | 460 | | private long WorldSpawnToken { get; } |
| | | 461 | | private ushort GridIndex { get; } |
| | | 462 | | private long GridSpawnToken { get; } |
| | | 463 | | private VoxelIndex Index { get; } |
| | | 464 | | internal bool IsValid { get; } |
| | | 465 | | |
| | | 466 | | internal static bool TryCreate(GridEventInfo eventInfo, out EventKey key) |
| | | 467 | | { |
| | 2573 | 468 | | if (!eventInfo.HasVoxelState) |
| | | 469 | | { |
| | 2105 | 470 | | key = default; |
| | 2105 | 471 | | return false; |
| | | 472 | | } |
| | 468 | 473 | | key = new EventKey( |
| | 468 | 474 | | eventInfo.WorldSpawnToken, |
| | 468 | 475 | | eventInfo.GridIndex, |
| | 468 | 476 | | eventInfo.GridSpawnToken, |
| | 468 | 477 | | eventInfo.VoxelIndex); |
| | 468 | 478 | | return true; |
| | | 479 | | } |
| | | 480 | | |
| | | 481 | | public bool Equals(EventKey other) => |
| | 310 | 482 | | WorldSpawnToken == other.WorldSpawnToken |
| | 310 | 483 | | && GridIndex == other.GridIndex |
| | 310 | 484 | | && GridSpawnToken == other.GridSpawnToken |
| | 310 | 485 | | && Index.Equals(other.Index); |
| | | 486 | | |
| | | 487 | | public override int GetHashCode() |
| | | 488 | | { |
| | 1086 | 489 | | int hash = SwiftHashTools.CombineHashCodes( |
| | 1086 | 490 | | WorldSpawnToken.GetHashCode(), |
| | 1086 | 491 | | GridSpawnToken.GetHashCode()); |
| | 1086 | 492 | | hash = SwiftHashTools.CombineHashCodes(hash, GridIndex.GetHashCode()); |
| | 1086 | 493 | | return SwiftHashTools.CombineHashCodes(hash, Index.GetHashCode()); |
| | | 494 | | } |
| | | 495 | | } |
| | | 496 | | } |