using UnityEngine;
using System.Collections.Generic;
using Pathfinding.Serialization;
namespace Pathfinding {
///
/// Basic point graph.
/// \ingroup graphs
/// The point graph is the most basic graph structure, it consists of a number of interconnected points in space called nodes or waypoints.\n
/// The point graph takes a Transform object as "root", this Transform will be searched for child objects, every child object will be treated as a node.
/// If is enabled, it will also search the child objects of the children recursively.
/// It will then check if any connections between the nodes can be made, first it will check if the distance between the nodes isn't too large (
/// and then it will check if the axis aligned distance isn't too high. The axis aligned distance, named ,
/// is useful because usually an AI cannot climb very high, but linking nodes far away from each other,
/// but on the same Y level should still be possible. and are treated as being set to infinity if they are set to 0 (zero). \n
/// Lastly it will check if there are any obstructions between the nodes using
/// raycasting which can optionally be thick.\n
/// One thing to think about when using raycasting is to either place the nodes a small
/// distance above the ground in your scene or to make sure that the ground is not in the raycast mask to avoid the raycast from hitting the ground.\n
///
/// Alternatively, a tag can be used to search for nodes.
/// See: http://docs.unity3d.com/Manual/Tags.html
///
/// For larger graphs, it can take quite some time to scan the graph with the default settings.
/// If you have the pro version you can enable which will in most cases reduce the calculation times
/// drastically.
///
/// Note: Does not support linecast because of obvious reasons.
///
/// [Open online documentation to see images]
/// [Open online documentation to see images]
///
[JsonOptIn]
[Pathfinding.Util.Preserve]
public class PointGraph : NavGraph {
/// Childs of this transform are treated as nodes
[JsonMember]
public Transform root;
/// If no is set, all nodes with the tag is used as nodes
[JsonMember]
public string searchTag;
///
/// Max distance for a connection to be valid.
/// The value 0 (zero) will be read as infinity and thus all nodes not restricted by
/// other constraints will be added as connections.
///
/// A negative value will disable any neighbours to be added.
/// It will completely stop the connection processing to be done, so it can save you processing
/// power if you don't these connections.
///
[JsonMember]
public float maxDistance;
/// Max distance along the axis for a connection to be valid. 0 = infinity
[JsonMember]
public Vector3 limits;
/// Use raycasts to check connections
[JsonMember]
public bool raycast = true;
/// Use the 2D Physics API
[JsonMember]
public bool use2DPhysics;
/// Use thick raycast
[JsonMember]
public bool thickRaycast;
/// Thick raycast radius
[JsonMember]
public float thickRaycastRadius = 1;
/// Recursively search for child nodes to the
[JsonMember]
public bool recursive = true;
/// Layer mask to use for raycast
[JsonMember]
public LayerMask mask;
///
/// All nodes in this graph.
/// Note that only the first will be non-null.
///
/// You can also use the GetNodes method to get all nodes.
///
public PointNode[] nodes;
///
/// \copydoc Pathfinding::PointGraph::NodeDistanceMode
///
/// See:
///
/// If you enable this during runtime, you will need to call to make sure some cache data is properly recalculated.
/// If the graph doesn't have any nodes yet or if you are going to scan the graph afterwards then you do not need to do this.
///
[JsonMember]
public NodeDistanceMode nearestNodeDistanceMode;
/// Number of nodes in this graph
public int nodeCount { get; protected set; }
///
/// Distance query mode.
/// [Open online documentation to see images]
///
/// In the image above there are a few red nodes. Assume the agent is the orange circle. Using the Node mode the closest point on the graph that would be found would be the node at the bottom center which
/// may not be what you want. Using the %Connection mode it will find the closest point on the connection between the two nodes in the top half of the image.
///
/// When using the %Connection option you may also want to use the %Connection option for the Seeker's Start End Modifier snapping options.
/// This is not strictly necessary, but it most cases it is what you want.
///
/// See:
///
public enum NodeDistanceMode {
///
/// All nearest node queries find the closest node center.
/// This is the fastest option but it may not be what you want if you have long connections.
///
Node,
///
/// All nearest node queries find the closest point on edges between nodes.
/// This is useful if you have long connections where the agent might be closer to some unrelated node if it is standing on a long connection between two nodes.
/// This mode is however slower than the Node mode.
///
Connection,
}
public override int CountNodes () {
return nodeCount;
}
public override void GetNodes (System.Action action) {
if (nodes == null) return;
var count = nodeCount;
for (int i = 0; i < count; i++) action(nodes[i]);
}
public override NNInfoInternal GetNearest (Vector3 position, NNConstraint constraint, GraphNode hint) {
return GetNearestInternal(position, constraint, true);
}
public override NNInfoInternal GetNearestForce (Vector3 position, NNConstraint constraint) {
return GetNearestInternal(position, constraint, false);
}
NNInfoInternal GetNearestInternal (Vector3 position, NNConstraint constraint, bool fastCheck) {
if (nodes == null) return new NNInfoInternal();
var iposition = (Int3)position;
float maxDistSqr = constraint == null || constraint.constrainDistance ? AstarPath.active.maxNearestNodeDistanceSqr : float.PositiveInfinity;
maxDistSqr *= Int3.FloatPrecision * Int3.FloatPrecision;
var nnInfo = new NNInfoInternal(null);
long minDist = long.MaxValue;
long minConstDist = long.MaxValue;
for (int i = 0; i < nodeCount; i++) {
PointNode node = nodes[i];
long dist = (iposition - node.position).sqrMagnitudeLong;
if (dist < minDist) {
minDist = dist;
nnInfo.node = node;
}
if (dist < minConstDist && (float)dist < maxDistSqr && (constraint == null || constraint.Suitable(node))) {
minConstDist = dist;
nnInfo.constrainedNode = node;
}
}
if (!fastCheck) nnInfo.node = nnInfo.constrainedNode;
nnInfo.UpdateInfo();
return nnInfo;
}
NNInfoInternal FindClosestConnectionPoint (PointNode node, Vector3 position) {
var closestConnectionPoint = (Vector3)node.position;
var conns = node.connections;
var nodePos = (Vector3)node.position;
var bestDist = float.PositiveInfinity;
if (conns != null) {
for (int i = 0; i < conns.Length; i++) {
var connectionMidpoint = ((UnityEngine.Vector3)conns[i].node.position + nodePos) * 0.5f;
var closestPoint = VectorMath.ClosestPointOnSegment(nodePos, connectionMidpoint, position);
var dist = (closestPoint - position).sqrMagnitude;
if (dist < bestDist) {
bestDist = dist;
closestConnectionPoint = closestPoint;
}
}
}
var result = new NNInfoInternal();
result.node = node;
result.clampedPosition = closestConnectionPoint;
return result;
}
///
/// Add a node to the graph at the specified position.
/// Note: Vector3 can be casted to Int3 using (Int3)myVector.
///
/// Note: This needs to be called when it is safe to update nodes, which is
/// - when scanning
/// - during a graph update
/// - inside a callback registered using AstarPath.AddWorkItem
///
///
/// AstarPath.active.AddWorkItem(new AstarWorkItem(ctx => {
/// var graph = AstarPath.active.data.pointGraph;
/// // Add 2 nodes and connect them
/// var node1 = graph.AddNode((Int3)transform.position);
/// var node2 = graph.AddNode((Int3)(transform.position + Vector3.right));
/// var cost = (uint)(node2.position - node1.position).costMagnitude;
/// node1.AddConnection(node2, cost);
/// node2.AddConnection(node1, cost);
/// }));
///
///
/// See: runtime-graphs (view in online documentation for working links)
///
public PointNode AddNode (Int3 position) {
return AddNode(new PointNode(active), position);
}
///
/// Add a node with the specified type to the graph at the specified position.
///
/// Note: Vector3 can be casted to Int3 using (Int3)myVector.
///
/// Note: This needs to be called when it is safe to update nodes, which is
/// - when scanning
/// - during a graph update
/// - inside a callback registered using AstarPath.AddWorkItem
///
/// See:
/// See: runtime-graphs (view in online documentation for working links)
///
/// This must be a node created using T(AstarPath.active) right before the call to this method.
/// The node parameter is only there because there is no new(AstarPath) constraint on
/// generic type parameters.
/// The node will be set to this position.
public T AddNode(T node, Int3 position) where T : PointNode {
if (nodes == null || nodeCount == nodes.Length) {
var newNodes = new PointNode[nodes != null ? System.Math.Max(nodes.Length+4, nodes.Length*2) : 4];
if (nodes != null) nodes.CopyTo(newNodes, 0);
nodes = newNodes;
}
node.SetPosition(position);
node.GraphIndex = graphIndex;
node.Walkable = true;
nodes[nodeCount] = node;
nodeCount++;
return node;
}
/// Recursively counds children of a transform
protected static int CountChildren (Transform tr) {
int c = 0;
foreach (Transform child in tr) {
c++;
c += CountChildren(child);
}
return c;
}
/// Recursively adds childrens of a transform as nodes
protected void AddChildren (ref int c, Transform tr) {
foreach (Transform child in tr) {
nodes[c].position = (Int3)child.position;
nodes[c].Walkable = true;
nodes[c].gameObject = child.gameObject;
c++;
AddChildren(ref c, child);
}
}
///
/// Rebuilds the lookup structure for nodes.
///
/// This is used when is enabled.
///
/// You should call this method every time you move a node in the graph manually and
/// you are using , otherwise pathfinding might not work correctly.
///
/// You may also call this after you have added many nodes using the
/// method. When adding nodes using the method they
/// will be added to the lookup structure. The lookup structure will
/// rebalance itself when it gets too unbalanced however if you are
/// sure you won't be adding any more nodes in the short term, you can
/// make sure it is perfectly balanced and thus squeeze out the last
/// bit of performance by calling this method. This can improve the
/// performance of the method slightly. The improvements
/// are on the order of 10-20%.
///
public void RebuildNodeLookup () {
// A* Pathfinding Project Pro Only
}
/// Rebuilds a cache used when =
public void RebuildConnectionDistanceLookup () {
}
void AddToLookup (PointNode node) {
// A* Pathfinding Project Pro Only
}
///
/// Ensures the graph knows that there is a connection with this length.
/// This is used when the nearest node distance mode is set to ToConnection.
/// If you are modifying node connections yourself (i.e. manipulating the PointNode.connections array) then you must call this function
/// when you add any connections.
///
/// When using PointNode.AddConnection this is done automatically.
/// It is also done for all nodes when is called.
///
/// The length of the connection in squared Int3 units. This can be calculated using (node1.position - node2.position).sqrMagnitudeLong.
public void RegisterConnectionLength (long sqrLength) {
// A* Pathfinding Project Pro Only
}
protected virtual PointNode[] CreateNodes (int count) {
var nodes = new PointNode[count];
for (int i = 0; i < nodeCount; i++) nodes[i] = new PointNode(active);
return nodes;
}
protected override IEnumerable