| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationFlowFieldPayload.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 System.Runtime.CompilerServices; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | |
| | | 12 | | namespace Trailblazer.Pathing; |
| | | 13 | | |
| | | 14 | | /// <summary>Stores one immutable stable-address flow-field prefix.</summary> |
| | | 15 | | internal sealed class NavigationFlowFieldPayload |
| | | 16 | | { |
| | | 17 | | private const long ObjectHeaderBytes = 16L; |
| | | 18 | | private const long ArrayHeaderBytes = 24L; |
| | | 19 | | private const long ReferenceSlotBytes = 8L; |
| | | 20 | | private const long Int32Bytes = 4L; |
| | | 21 | | private const long NullableUInt64Bytes = 16L; |
| | | 22 | | private const long BooleanBytes = 1L; |
| | 1 | 23 | | private static readonly long BaseRetainedBytes = Align8( |
| | 1 | 24 | | ObjectHeaderBytes |
| | 1 | 25 | | + Unsafe.SizeOf<NavigationFlowFieldPayloadKey>() |
| | 1 | 26 | | + (4L * ReferenceSlotBytes) |
| | 1 | 27 | | + NullableUInt64Bytes |
| | 1 | 28 | | + BooleanBytes); |
| | | 29 | | |
| | 767 | 30 | | internal NavigationFlowFieldPayload( |
| | 767 | 31 | | NavigationFlowFieldPayloadKey key, |
| | 767 | 32 | | NavigationFlowFieldNode[] nodes, |
| | 767 | 33 | | int[] addressLookupOrdinals, |
| | 767 | 34 | | NavigationTransitionInstruction[] transitionInstructions, |
| | 767 | 35 | | GraphDependencyStamp dependencies, |
| | 767 | 36 | | bool isComplete, |
| | 767 | 37 | | ulong? worldChangeSequence) |
| | | 38 | | { |
| | 767 | 39 | | SwiftThrowHelper.ThrowIfNull(nodes, nameof(nodes)); |
| | 767 | 40 | | SwiftThrowHelper.ThrowIfNull(addressLookupOrdinals, nameof(addressLookupOrdinals)); |
| | 767 | 41 | | SwiftThrowHelper.ThrowIfNull( |
| | 767 | 42 | | transitionInstructions, |
| | 767 | 43 | | nameof(transitionInstructions)); |
| | 767 | 44 | | SwiftThrowHelper.ThrowIfNull(dependencies, nameof(dependencies)); |
| | 767 | 45 | | SwiftThrowHelper.ThrowIfArgument( |
| | 767 | 46 | | nodes.Length == 0 || addressLookupOrdinals.Length != nodes.Length, |
| | 767 | 47 | | paramName: null, |
| | 767 | 48 | | message: "Flow payload arrays must be non-empty and aligned."); |
| | 765 | 49 | | Key = key; |
| | 765 | 50 | | Nodes = nodes; |
| | 765 | 51 | | AddressLookupOrdinals = addressLookupOrdinals; |
| | 765 | 52 | | TransitionInstructions = transitionInstructions; |
| | 765 | 53 | | Dependencies = dependencies; |
| | 765 | 54 | | IsComplete = isComplete; |
| | 765 | 55 | | WorldChangeSequence = worldChangeSequence; |
| | 765 | 56 | | } |
| | | 57 | | |
| | | 58 | | internal NavigationFlowFieldPayloadKey Key { get; } |
| | | 59 | | internal NavigationFlowFieldNode[] Nodes { get; } |
| | | 60 | | internal int[] AddressLookupOrdinals { get; } |
| | | 61 | | internal NavigationTransitionInstruction[] TransitionInstructions { get; } |
| | | 62 | | internal GraphDependencyStamp Dependencies { get; } |
| | | 63 | | internal bool IsComplete { get; } |
| | | 64 | | internal ulong? WorldChangeSequence { get; } |
| | 1476 | 65 | | internal Fixed64 LastSettledCost => Nodes[Nodes.Length - 1].IntegrationCost; |
| | 2 | 66 | | internal NavigationCellAddress LastSettledAddress => Nodes[Nodes.Length - 1].Address; |
| | | 67 | | |
| | 6842 | 68 | | internal long RetainedBytes => GetRetainedBytes( |
| | 6842 | 69 | | Nodes.Length, |
| | 6842 | 70 | | TransitionInstructions.Length, |
| | 6842 | 71 | | Dependencies); |
| | | 72 | | |
| | | 73 | | internal bool TryGetNode( |
| | | 74 | | NavigationCellAddress address, |
| | | 75 | | TraversalMedium medium, |
| | | 76 | | out NavigationFlowFieldNode node) |
| | | 77 | | { |
| | 2878 | 78 | | int low = 0; |
| | 2878 | 79 | | int high = AddressLookupOrdinals.Length - 1; |
| | 8262 | 80 | | while (low <= high) |
| | | 81 | | { |
| | 8243 | 82 | | int middle = low + ((high - low) >> 1); |
| | 8243 | 83 | | NavigationFlowFieldNode candidate = Nodes[AddressLookupOrdinals[middle]]; |
| | 8243 | 84 | | int comparison = candidate.Address.CompareTo(address); |
| | 8243 | 85 | | if (comparison == 0) |
| | 2860 | 86 | | comparison = ((int)candidate.Medium).CompareTo((int)medium); |
| | 8243 | 87 | | if (comparison == 0) |
| | | 88 | | { |
| | 2859 | 89 | | node = candidate; |
| | 2859 | 90 | | return true; |
| | | 91 | | } |
| | 5384 | 92 | | if (comparison < 0) |
| | 5289 | 93 | | low = middle + 1; |
| | | 94 | | else |
| | 95 | 95 | | high = middle - 1; |
| | | 96 | | } |
| | 19 | 97 | | node = default; |
| | 19 | 98 | | return false; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | internal static long GetRetainedBytes( |
| | | 102 | | int nodeCount, |
| | | 103 | | int transitionInstructionCount, |
| | | 104 | | GraphDependencyStamp dependencies) |
| | | 105 | | { |
| | 6842 | 106 | | SwiftThrowHelper.ThrowIfNegative(nodeCount, nameof(nodeCount)); |
| | 6842 | 107 | | SwiftThrowHelper.ThrowIfNegative( |
| | 6842 | 108 | | transitionInstructionCount, |
| | 6842 | 109 | | nameof(transitionInstructionCount)); |
| | 6842 | 110 | | SwiftThrowHelper.ThrowIfNull(dependencies, nameof(dependencies)); |
| | 6842 | 111 | | return checked( |
| | 6842 | 112 | | BaseRetainedBytes |
| | 6842 | 113 | | + GetArrayRetainedBytes( |
| | 6842 | 114 | | nodeCount, |
| | 6842 | 115 | | Unsafe.SizeOf<NavigationFlowFieldNode>()) |
| | 6842 | 116 | | + GetArrayRetainedBytes(nodeCount, Int32Bytes) |
| | 6842 | 117 | | + GetArrayRetainedBytes( |
| | 6842 | 118 | | transitionInstructionCount, |
| | 6842 | 119 | | Unsafe.SizeOf<NavigationTransitionInstruction>()) |
| | 6842 | 120 | | + dependencies.RetainedBytes); |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | internal static long GetMaximumRetainedBytes( |
| | | 124 | | int nodeCount, |
| | | 125 | | int transitionInstructionCount, |
| | | 126 | | int componentCount, |
| | | 127 | | int pageCount) |
| | | 128 | | { |
| | 762 | 129 | | SwiftThrowHelper.ThrowIfNegative(nodeCount, nameof(nodeCount)); |
| | 761 | 130 | | SwiftThrowHelper.ThrowIfNegative( |
| | 761 | 131 | | transitionInstructionCount, |
| | 761 | 132 | | nameof(transitionInstructionCount)); |
| | 760 | 133 | | SwiftThrowHelper.ThrowIfNegative(componentCount, nameof(componentCount)); |
| | 759 | 134 | | SwiftThrowHelper.ThrowIfNegative(pageCount, nameof(pageCount)); |
| | 758 | 135 | | return checked( |
| | 758 | 136 | | BaseRetainedBytes |
| | 758 | 137 | | + GetArrayRetainedBytes( |
| | 758 | 138 | | nodeCount, |
| | 758 | 139 | | Unsafe.SizeOf<NavigationFlowFieldNode>()) |
| | 758 | 140 | | + GetArrayRetainedBytes(nodeCount, Int32Bytes) |
| | 758 | 141 | | + GetArrayRetainedBytes( |
| | 758 | 142 | | transitionInstructionCount, |
| | 758 | 143 | | Unsafe.SizeOf<NavigationTransitionInstruction>()) |
| | 758 | 144 | | + GraphDependencyStamp.GetRetainedBytes(componentCount, pageCount)); |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | private static long GetArrayRetainedBytes(int length, long elementBytes) => |
| | 22800 | 148 | | length == 0 |
| | 22800 | 149 | | ? 0L |
| | 22800 | 150 | | : Align8(checked(ArrayHeaderBytes + ((long)length * elementBytes))); |
| | | 151 | | |
| | 15455 | 152 | | private static long Align8(long value) => checked((value + 7L) & ~7L); |
| | | 153 | | } |