123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813 |
- using UnityEngine;
- using System.Collections.Generic;
- using Pathfinding.Serialization;
- namespace Pathfinding {
- using Pathfinding.Util;
-
- public struct Connection {
-
- public GraphNode node;
-
-
-
-
- public uint cost;
-
-
-
-
-
-
-
-
-
-
-
-
-
- public byte shapeEdge;
- public const byte NoSharedEdge = 0xFF;
- public Connection (GraphNode node, uint cost, byte shapeEdge = NoSharedEdge) {
- this.node = node;
- this.cost = cost;
- this.shapeEdge = shapeEdge;
- }
- public override int GetHashCode () {
- return node.GetHashCode() ^ (int)cost;
- }
- public override bool Equals (object obj) {
- if (obj == null) return false;
- var conn = (Connection)obj;
- return conn.node == node && conn.cost == cost && conn.shapeEdge == shapeEdge;
- }
- }
-
- public abstract class GraphNode {
-
- private int nodeIndex;
-
-
-
-
-
-
-
- protected uint flags;
- #if !ASTAR_NO_PENALTY
-
-
-
-
-
-
-
-
- private uint penalty;
- #endif
-
-
-
-
-
-
-
-
-
-
-
- public NavGraph Graph {
- get {
- return Destroyed ? null : AstarData.GetGraph(this);
- }
- }
-
- protected GraphNode (AstarPath astar) {
- if (!System.Object.ReferenceEquals(astar, null)) {
- this.nodeIndex = astar.GetNewNodeIndex();
- astar.InitializeNode(this);
- } else {
- throw new System.Exception("No active AstarPath object to bind to");
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public void Destroy () {
- if (Destroyed) return;
- ClearConnections(true);
- if (AstarPath.active != null) {
- AstarPath.active.DestroyNode(this);
- }
- NodeIndex = DestroyedNodeIndex;
- }
- public bool Destroyed {
- get {
- return NodeIndex == DestroyedNodeIndex;
- }
- }
-
- const int NodeIndexMask = 0xFFFFFFF;
- const int DestroyedNodeIndex = NodeIndexMask - 1;
- const int TemporaryFlag1Mask = 0x10000000;
- const int TemporaryFlag2Mask = 0x20000000;
-
-
-
-
-
- public int NodeIndex { get { return nodeIndex & NodeIndexMask; } private set { nodeIndex = (nodeIndex & ~NodeIndexMask) | value; } }
-
-
-
-
- internal bool TemporaryFlag1 { get { return (nodeIndex & TemporaryFlag1Mask) != 0; } set { nodeIndex = (nodeIndex & ~TemporaryFlag1Mask) | (value ? TemporaryFlag1Mask : 0); } }
-
-
-
-
- internal bool TemporaryFlag2 { get { return (nodeIndex & TemporaryFlag2Mask) != 0; } set { nodeIndex = (nodeIndex & ~TemporaryFlag2Mask) | (value ? TemporaryFlag2Mask : 0); } }
-
-
-
-
-
-
- public Int3 position;
- #region Constants
-
- const int FlagsWalkableOffset = 0;
-
- const uint FlagsWalkableMask = 1 << FlagsWalkableOffset;
-
- const int FlagsHierarchicalIndexOffset = 1;
-
- const uint HierarchicalIndexMask = (131072-1) << FlagsHierarchicalIndexOffset;
-
- const int HierarchicalDirtyOffset = 18;
-
- const uint HierarchicalDirtyMask = 1 << HierarchicalDirtyOffset;
-
- const int FlagsGraphOffset = 24;
-
- const uint FlagsGraphMask = (256u-1) << FlagsGraphOffset;
- public const uint MaxHierarchicalNodeIndex = HierarchicalIndexMask >> FlagsHierarchicalIndexOffset;
-
- public const uint MaxGraphIndex = FlagsGraphMask >> FlagsGraphOffset;
-
- const int FlagsTagOffset = 19;
-
- const uint FlagsTagMask = (32-1) << FlagsTagOffset;
- #endregion
- #region Properties
-
-
-
-
-
-
-
-
-
-
-
- public uint Flags {
- get {
- return flags;
- }
- set {
- flags = value;
- }
- }
-
-
-
-
-
-
-
- public uint Penalty {
- #if !ASTAR_NO_PENALTY
- get {
- return penalty;
- }
- set {
- if (value > 0xFFFFFF)
- Debug.LogWarning("Very high penalty applied. Are you sure negative values haven't underflowed?\n" +
- "Penalty values this high could with long paths cause overflows and in some cases infinity loops because of that.\n" +
- "Penalty value applied: "+value);
- penalty = value;
- }
- #else
- get { return 0U; }
- set {}
- #endif
- }
-
-
-
-
-
- public bool Walkable {
- get {
- return (flags & FlagsWalkableMask) != 0;
- }
- set {
- flags = flags & ~FlagsWalkableMask | (value ? 1U : 0U) << FlagsWalkableOffset;
- AstarPath.active.hierarchicalGraph.AddDirtyNode(this);
- }
- }
-
-
-
-
-
-
-
-
-
- internal int HierarchicalNodeIndex {
- get {
- return (int)((flags & HierarchicalIndexMask) >> FlagsHierarchicalIndexOffset);
- }
- set {
- flags = (flags & ~HierarchicalIndexMask) | (uint)(value << FlagsHierarchicalIndexOffset);
- }
- }
-
- internal bool IsHierarchicalNodeDirty {
- get {
- return (flags & HierarchicalDirtyMask) != 0;
- }
- set {
- flags = flags & ~HierarchicalDirtyMask | (value ? 1U : 0U) << HierarchicalDirtyOffset;
- }
- }
-
-
-
-
-
-
-
-
- public uint Area {
- get {
- return AstarPath.active.hierarchicalGraph.GetConnectedComponent(HierarchicalNodeIndex);
- }
- }
-
-
-
-
-
- public uint GraphIndex {
- get {
- return (flags & FlagsGraphMask) >> FlagsGraphOffset;
- }
- set {
- flags = flags & ~FlagsGraphMask | value << FlagsGraphOffset;
- }
- }
-
-
-
-
-
- public uint Tag {
- get {
- return (flags & FlagsTagMask) >> FlagsTagOffset;
- }
- set {
- flags = flags & ~FlagsTagMask | ((value << FlagsTagOffset) & FlagsTagMask);
- }
- }
- #endregion
-
-
-
-
-
-
-
-
-
- public void SetConnectivityDirty () {
- AstarPath.active.hierarchicalGraph.AddDirtyNode(this);
- }
-
-
-
-
- [System.Obsolete("This method is deprecated because it never did anything, you can safely remove any calls to this method")]
- public void RecalculateConnectionCosts () {
- }
- public virtual void UpdateRecursiveG (Path path, PathNode pathNode, PathHandler handler) {
-
- pathNode.UpdateG(path);
- handler.heap.Add(pathNode);
- GetConnections((GraphNode other) => {
- PathNode otherPN = handler.GetPathNode(other);
- if (otherPN.parent == pathNode && otherPN.pathID == handler.PathID) other.UpdateRecursiveG(path, otherPN, handler);
- });
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public abstract void GetConnections(System.Action<GraphNode> action);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public abstract void AddConnection(GraphNode node, uint cost);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public abstract void RemoveConnection(GraphNode node);
-
-
- public abstract void ClearConnections(bool alsoReverse);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public virtual bool ContainsConnection (GraphNode node) {
-
- bool contains = false;
- GetConnections(neighbour => {
- contains |= neighbour == node;
- });
- return contains;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public virtual bool GetPortal (GraphNode other, List<Vector3> left, List<Vector3> right, bool backwards) {
- return false;
- }
-
-
-
-
- public abstract void Open(Path path, PathNode pathNode, PathHandler handler);
-
- public virtual float SurfaceArea () {
- return 0;
- }
-
-
-
-
- public virtual Vector3 RandomPointOnSurface () {
- return (Vector3)position;
- }
-
- public abstract Vector3 ClosestPointOnNode(Vector3 p);
-
-
-
-
- public virtual int GetGizmoHashCode () {
-
- return position.GetHashCode() ^ (19 * (int)Penalty) ^ (41 * (int)(flags & ~(HierarchicalIndexMask | HierarchicalDirtyMask)));
- }
-
- public virtual void SerializeNode (GraphSerializationContext ctx) {
-
- ctx.writer.Write(Penalty);
-
- ctx.writer.Write(Flags & ~(HierarchicalIndexMask | HierarchicalDirtyMask));
- }
-
- public virtual void DeserializeNode (GraphSerializationContext ctx) {
- Penalty = ctx.reader.ReadUInt32();
-
-
- Flags = (ctx.reader.ReadUInt32() & ~(HierarchicalIndexMask | HierarchicalDirtyMask)) | (Flags & (HierarchicalIndexMask | HierarchicalDirtyMask));
-
- GraphIndex = ctx.graphIndex;
- }
-
-
-
-
-
-
-
-
-
-
- public virtual void SerializeReferences (GraphSerializationContext ctx) {
- }
-
-
-
-
-
-
-
-
-
-
- public virtual void DeserializeReferences (GraphSerializationContext ctx) {
- }
- }
- public abstract class MeshNode : GraphNode {
- protected MeshNode (AstarPath astar) : base(astar) {
- }
-
-
-
-
-
-
-
- public Connection[] connections;
-
-
- public abstract Int3 GetVertex(int i);
-
-
-
-
- public abstract int GetVertexCount();
-
-
-
-
-
-
- public abstract Vector3 ClosestPointOnNodeXZ(Vector3 p);
- public override void ClearConnections (bool alsoReverse) {
-
- if (alsoReverse && connections != null) {
- for (int i = 0; i < connections.Length; i++) {
-
-
-
- if (connections[i].node != null) {
- connections[i].node.RemoveConnection(this);
- }
- }
- }
- ArrayPool<Connection>.Release(ref connections, true);
- AstarPath.active.hierarchicalGraph.AddDirtyNode(this);
- }
- public override void GetConnections (System.Action<GraphNode> action) {
- if (connections == null) return;
- for (int i = 0; i < connections.Length; i++) action(connections[i].node);
- }
- public override bool ContainsConnection (GraphNode node) {
- for (int i = 0; i < connections.Length; i++) if (connections[i].node == node) return true;
- return false;
- }
- public override void UpdateRecursiveG (Path path, PathNode pathNode, PathHandler handler) {
- pathNode.UpdateG(path);
- handler.heap.Add(pathNode);
- for (int i = 0; i < connections.Length; i++) {
- GraphNode other = connections[i].node;
- PathNode otherPN = handler.GetPathNode(other);
- if (otherPN.parent == pathNode && otherPN.pathID == handler.PathID) {
- other.UpdateRecursiveG(path, otherPN, handler);
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
- public override void AddConnection (GraphNode node, uint cost) {
- AddConnection(node, cost, Connection.NoSharedEdge);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void AddConnection (GraphNode node, uint cost, byte shapeEdge) {
- if (node == null) throw new System.ArgumentNullException();
-
- if (connections != null) {
- for (int i = 0; i < connections.Length; i++) {
- if (connections[i].node == node) {
-
- connections[i].cost = cost;
-
-
-
- connections[i].shapeEdge = shapeEdge != Connection.NoSharedEdge ? shapeEdge : connections[i].shapeEdge;
- return;
- }
- }
- }
-
- int connLength = connections != null ? connections.Length : 0;
- var newconns = ArrayPool<Connection>.ClaimWithExactLength(connLength+1);
- for (int i = 0; i < connLength; i++) {
- newconns[i] = connections[i];
- }
- newconns[connLength] = new Connection(node, cost, (byte)shapeEdge);
- if (connections != null) {
- ArrayPool<Connection>.Release(ref connections, true);
- }
- connections = newconns;
- AstarPath.active.hierarchicalGraph.AddDirtyNode(this);
- }
-
-
-
-
-
-
-
-
- public override void RemoveConnection (GraphNode node) {
- if (connections == null) return;
-
- for (int i = 0; i < connections.Length; i++) {
- if (connections[i].node == node) {
-
- int connLength = connections.Length;
- var newconns = ArrayPool<Connection>.ClaimWithExactLength(connLength-1);
- for (int j = 0; j < i; j++) {
- newconns[j] = connections[j];
- }
- for (int j = i+1; j < connLength; j++) {
- newconns[j-1] = connections[j];
- }
- if (connections != null) {
- ArrayPool<Connection>.Release(ref connections, true);
- }
- connections = newconns;
- AstarPath.active.hierarchicalGraph.AddDirtyNode(this);
- return;
- }
- }
- }
-
- public virtual bool ContainsPoint (Int3 point) {
- return ContainsPoint((Vector3)point);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public abstract bool ContainsPoint(Vector3 point);
-
-
-
-
-
-
- public abstract bool ContainsPointInGraphSpace(Int3 point);
- public override int GetGizmoHashCode () {
- var hash = base.GetGizmoHashCode();
- if (connections != null) {
- for (int i = 0; i < connections.Length; i++) {
- hash ^= 17 * connections[i].GetHashCode();
- }
- }
- return hash;
- }
- public override void SerializeReferences (GraphSerializationContext ctx) {
- if (connections == null) {
- ctx.writer.Write(-1);
- } else {
- ctx.writer.Write(connections.Length);
- for (int i = 0; i < connections.Length; i++) {
- ctx.SerializeNodeReference(connections[i].node);
- ctx.writer.Write(connections[i].cost);
- ctx.writer.Write(connections[i].shapeEdge);
- }
- }
- }
- public override void DeserializeReferences (GraphSerializationContext ctx) {
- int count = ctx.reader.ReadInt32();
- if (count == -1) {
- connections = null;
- } else {
- connections = ArrayPool<Connection>.ClaimWithExactLength(count);
- for (int i = 0; i < count; i++) {
- connections[i] = new Connection(
- ctx.DeserializeNodeReference(),
- ctx.reader.ReadUInt32(),
- ctx.meta.version < AstarSerializer.V4_1_0 ? (byte)0xFF : ctx.reader.ReadByte()
- );
- }
- }
- }
- }
- }
|