| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationAStarPayload.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 fixed-point A* result independent of graph slots.</summary> |
| | | 15 | | internal sealed class NavigationAStarPayload |
| | | 16 | | { |
| | | 17 | | private const long ObjectHeaderBytes = 16L; |
| | | 18 | | private const long ArrayHeaderBytes = 24L; |
| | | 19 | | private const long ReferenceSlotBytes = 8L; |
| | | 20 | | private const long Int64Bytes = 8L; |
| | | 21 | | private const long ByteBytes = 1L; |
| | | 22 | | private const long Fixed64Bytes = Int64Bytes; |
| | | 23 | | private const long NullableUInt64Bytes = 16L; |
| | 1 | 24 | | private static readonly long BaseRetainedBytes = Align8( |
| | 1 | 25 | | ObjectHeaderBytes |
| | 1 | 26 | | + Unsafe.SizeOf<NavigationAStarPayloadKey>() |
| | 1 | 27 | | + (3L * ReferenceSlotBytes) |
| | 1 | 28 | | + Fixed64Bytes |
| | 1 | 29 | | + NullableUInt64Bytes |
| | 1 | 30 | | + ByteBytes); |
| | | 31 | | |
| | 293 | 32 | | internal NavigationAStarPayload( |
| | 293 | 33 | | NavigationAStarPayloadKey key, |
| | 293 | 34 | | NavigationAStarGuidePoint[] guidePoints, |
| | 293 | 35 | | NavigationTransitionInstruction[] transitionInstructions, |
| | 293 | 36 | | Fixed64 cost, |
| | 293 | 37 | | GraphDependencyStamp dependencies, |
| | 293 | 38 | | ulong? worldChangeSequence, |
| | 293 | 39 | | NavigationSurfaceAStarStatus status) |
| | | 40 | | { |
| | 293 | 41 | | SwiftThrowHelper.ThrowIfNull(guidePoints, nameof(guidePoints)); |
| | 293 | 42 | | SwiftThrowHelper.ThrowIfNull( |
| | 293 | 43 | | transitionInstructions, |
| | 293 | 44 | | nameof(transitionInstructions)); |
| | 293 | 45 | | SwiftThrowHelper.ThrowIfNull(dependencies, nameof(dependencies)); |
| | 293 | 46 | | SwiftThrowHelper.ThrowIfArgument( |
| | 293 | 47 | | !IsReusableResult(status, guidePoints.Length), |
| | 293 | 48 | | paramName: nameof(status), |
| | 293 | 49 | | message: "Only successful paths and exact no-path results are reusable."); |
| | 292 | 50 | | Key = key; |
| | 292 | 51 | | GuidePoints = guidePoints; |
| | 292 | 52 | | TransitionInstructions = transitionInstructions; |
| | 292 | 53 | | Cost = cost; |
| | 292 | 54 | | Dependencies = dependencies; |
| | 292 | 55 | | WorldChangeSequence = worldChangeSequence; |
| | 292 | 56 | | Status = status; |
| | 292 | 57 | | } |
| | | 58 | | |
| | | 59 | | internal NavigationAStarPayloadKey Key { get; } |
| | | 60 | | |
| | | 61 | | internal NavigationAStarGuidePoint[] GuidePoints { get; } |
| | | 62 | | |
| | | 63 | | internal NavigationTransitionInstruction[] TransitionInstructions { get; } |
| | | 64 | | |
| | 634 | 65 | | internal bool HasPath => GuidePoints.Length != 0; |
| | | 66 | | |
| | | 67 | | internal Fixed64 Cost { get; } |
| | | 68 | | |
| | | 69 | | internal GraphDependencyStamp Dependencies { get; } |
| | | 70 | | |
| | | 71 | | internal ulong? WorldChangeSequence { get; } |
| | | 72 | | |
| | | 73 | | internal NavigationSurfaceAStarStatus Status { get; } |
| | | 74 | | |
| | 4295 | 75 | | internal long RetainedBytes => GetRetainedBytes( |
| | 4295 | 76 | | GuidePoints.Length, |
| | 4295 | 77 | | TransitionInstructions.Length, |
| | 4295 | 78 | | Dependencies); |
| | | 79 | | |
| | | 80 | | internal static long GetRetainedBytes( |
| | | 81 | | int guidePointCount, |
| | | 82 | | int transitionInstructionCount, |
| | | 83 | | GraphDependencyStamp dependencies) |
| | | 84 | | { |
| | 4509 | 85 | | SwiftThrowHelper.ThrowIfNegative(guidePointCount, nameof(guidePointCount)); |
| | 4509 | 86 | | SwiftThrowHelper.ThrowIfNegative( |
| | 4509 | 87 | | transitionInstructionCount, |
| | 4509 | 88 | | nameof(transitionInstructionCount)); |
| | 4509 | 89 | | SwiftThrowHelper.ThrowIfNull(dependencies, nameof(dependencies)); |
| | 4509 | 90 | | return checked( |
| | 4509 | 91 | | BaseRetainedBytes |
| | 4509 | 92 | | + GetGuidePointArrayRetainedBytes(guidePointCount) |
| | 4509 | 93 | | + GetTransitionInstructionArrayRetainedBytes(transitionInstructionCount) |
| | 4509 | 94 | | + dependencies.RetainedBytes); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | internal static long GetMaximumRetainedBytes( |
| | | 98 | | int guidePointCount, |
| | | 99 | | int transitionInstructionCount, |
| | | 100 | | int componentCount, |
| | | 101 | | int pageCount) |
| | | 102 | | { |
| | 440 | 103 | | SwiftThrowHelper.ThrowIfNegative(guidePointCount, nameof(guidePointCount)); |
| | 440 | 104 | | SwiftThrowHelper.ThrowIfNegative( |
| | 440 | 105 | | transitionInstructionCount, |
| | 440 | 106 | | nameof(transitionInstructionCount)); |
| | 440 | 107 | | return checked( |
| | 440 | 108 | | BaseRetainedBytes |
| | 440 | 109 | | + GetGuidePointArrayRetainedBytes(guidePointCount) |
| | 440 | 110 | | + GetTransitionInstructionArrayRetainedBytes(transitionInstructionCount) |
| | 440 | 111 | | + GraphDependencyStamp.GetRetainedBytes(componentCount, pageCount)); |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | internal static bool IsReusableResult( |
| | | 115 | | NavigationSurfaceAStarStatus status, |
| | | 116 | | int guidePointCount) => |
| | 299 | 117 | | (status == NavigationSurfaceAStarStatus.Success && guidePointCount > 0) |
| | 299 | 118 | | || (status == NavigationSurfaceAStarStatus.NoPath && guidePointCount == 0); |
| | | 119 | | |
| | | 120 | | private static long GetGuidePointArrayRetainedBytes(int length) => |
| | 4949 | 121 | | length == 0 |
| | 4949 | 122 | | ? 0L |
| | 4949 | 123 | | : Align8(checked( |
| | 4949 | 124 | | ArrayHeaderBytes |
| | 4949 | 125 | | + ((long)length * Unsafe.SizeOf<NavigationAStarGuidePoint>()))); |
| | | 126 | | |
| | | 127 | | private static long GetTransitionInstructionArrayRetainedBytes(int length) => |
| | 4949 | 128 | | length == 0 |
| | 4949 | 129 | | ? 0L |
| | 4949 | 130 | | : Align8(checked( |
| | 4949 | 131 | | ArrayHeaderBytes |
| | 4949 | 132 | | + ((long)length * Unsafe.SizeOf<NavigationTransitionInstruction>()))); |
| | | 133 | | |
| | 5131 | 134 | | private static long Align8(long value) => checked((value + 7L) & ~7L); |
| | | 135 | | } |