| | | 1 | | using FixedMathSharp; |
| | | 2 | | using GridForge.Grids; |
| | | 3 | | using System; |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | |
| | | 7 | | namespace Trailblazer.Pathing; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// A pathfinding request used for flow field generation. Contains configuration for |
| | | 11 | | /// destination targeting, dynamic agent sizing, and walkability override. |
| | | 12 | | /// Implements value-based equality for guide pooling. |
| | | 13 | | /// </summary> |
| | | 14 | | public class FlowFieldPathRequest : PathRequest, IEquatable<FlowFieldPathRequest> |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Specifies the default value for the extra flood range used in calculations or operations that require an |
| | | 18 | | /// additional range parameter. |
| | | 19 | | /// </summary> |
| | | 20 | | public const int DefaultExtraFloodRange = 10; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// The maximum Y-axis height delta a unit can step or climb per voxel while the field is built. |
| | | 24 | | /// Voxels exceeding this are ignored even if walkable and adjacent. |
| | | 25 | | /// </summary> |
| | | 26 | | public Fixed64 MaxClimbHeight { get; set; } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Limits how much extra distance the flood will expand after the target is reached. |
| | | 30 | | /// </summary> |
| | | 31 | | public int ExtraFloodRange { get; set; } |
| | | 32 | | |
| | 582 | 33 | | private FlowFieldPathRequest() { } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Attempts to create a new context-bound flow field path request using the specified origin, destination, and unit |
| | | 37 | | /// </summary> |
| | | 38 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 39 | | public static bool TryCreateWithSize( |
| | | 40 | | TrailblazerWorldContext context, |
| | | 41 | | Vector3d origin, |
| | | 42 | | Vector3d destination, |
| | | 43 | | Fixed64 unitSize, |
| | | 44 | | [NotNullWhen(true)] out FlowFieldPathRequest? request) |
| | | 45 | | { |
| | 48 | 46 | | request = Create(context, origin, destination, unitSize); |
| | 48 | 47 | | if (request == null) |
| | 3 | 48 | | return false; |
| | 45 | 49 | | return true; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Attempts to create a new context-bound flow field path request using the context's voxel size. |
| | | 54 | | /// </summary> |
| | | 55 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 56 | | public static bool TryCreate( |
| | | 57 | | TrailblazerWorldContext context, |
| | | 58 | | Vector3d origin, |
| | | 59 | | Vector3d destination, |
| | | 60 | | [NotNullWhen(true)] out FlowFieldPathRequest? request) => |
| | 47 | 61 | | TryCreateWithSize(context, origin, destination, context.VoxelSize, out request); |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Creates a context-bound flow field path request. |
| | | 65 | | /// </summary> |
| | | 66 | | public static FlowFieldPathRequest? Create( |
| | | 67 | | TrailblazerWorldContext context, |
| | | 68 | | Vector3d origin, |
| | | 69 | | Vector3d destination, |
| | | 70 | | Fixed64 unitSize, |
| | | 71 | | bool allowUnwalkableEndpoints = false, |
| | | 72 | | bool allowTraversalTransitions = false) |
| | | 73 | | { |
| | 301 | 74 | | PathRequestContextResolver.ThrowIfUnusable(context); |
| | 301 | 75 | | if (!SolidVoxelFinder.TryGetPathEdgeVoxels( |
| | 301 | 76 | | context, |
| | 301 | 77 | | origin, |
| | 301 | 78 | | destination, |
| | 301 | 79 | | out Voxel? startNode, |
| | 301 | 80 | | out Voxel? endNode, |
| | 301 | 81 | | unitSize, |
| | 301 | 82 | | allowUnwalkableEndpoints)) |
| | | 83 | | { |
| | 10 | 84 | | return null; |
| | | 85 | | } |
| | | 86 | | |
| | 291 | 87 | | if (startNode == null || endNode == null) |
| | 0 | 88 | | return null; |
| | | 89 | | |
| | 291 | 90 | | FlowFieldPathRequest request = new() |
| | 291 | 91 | | { |
| | 291 | 92 | | Context = context, |
| | 291 | 93 | | Origin = origin, |
| | 291 | 94 | | StartNode = startNode, |
| | 291 | 95 | | TargetPosition = destination, |
| | 291 | 96 | | EndNode = endNode, |
| | 291 | 97 | | UnitSize = unitSize, |
| | 291 | 98 | | AllowUnwalkableEndpoints = allowUnwalkableEndpoints, |
| | 291 | 99 | | AllowTraversalTransitions = allowTraversalTransitions, |
| | 291 | 100 | | MaxClimbHeight = context.VoxelSize, |
| | 291 | 101 | | ExtraFloodRange = DefaultExtraFloodRange |
| | 291 | 102 | | }; |
| | | 103 | | |
| | 291 | 104 | | if (context.Pathing.TryGetMaxSearchSize(startNode, endNode, out int searchSize)) |
| | 291 | 105 | | request.MaxPathSearchRange = searchSize; |
| | | 106 | | |
| | 291 | 107 | | return request; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <inheritdoc/> |
| | | 111 | | public override bool Equals(object? obj) => |
| | 1 | 112 | | obj is FlowFieldPathRequest other && Equals(other); |
| | | 113 | | |
| | | 114 | | /// <inheritdoc/> |
| | | 115 | | public bool Equals(FlowFieldPathRequest? other) => |
| | 2 | 116 | | other != null && RequestCacheKey == other.RequestCacheKey; |
| | | 117 | | |
| | | 118 | | /// <inheritdoc/> |
| | | 119 | | public override int GetHashCode() |
| | | 120 | | { |
| | | 121 | | // Note: For FlowFields we don't care about the start voxel (only that the FlowField contains it) |
| | 2777 | 122 | | PathRequestHashBuilder hash = PathRequestHashBuilder.Create(); |
| | 2777 | 123 | | hash.Add(EndNode?.SpawnToken ?? 0); |
| | 2777 | 124 | | hash.Add(UnitSize.GetHashCode()); |
| | 2777 | 125 | | hash.Add(AllowUnwalkableEndpoints); |
| | 2777 | 126 | | hash.Add(AllowTraversalTransitions); |
| | 2777 | 127 | | hash.Add(MaxClimbHeight.GetHashCode()); |
| | 2777 | 128 | | hash.Add(ExtraFloodRange); |
| | 2777 | 129 | | hash.Add(MaxPathSearchRange); |
| | 2777 | 130 | | hash.Add(AllowTraversalTransitions ? Context.Pathing.State.TransitionRegistryState.RegistryVersion : 0); |
| | 2777 | 131 | | return hash.ToHashCode(); |
| | | 132 | | } |
| | | 133 | | } |