123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- using UnityEngine;
- using Pathfinding.Serialization;
- namespace Pathfinding {
-
- public abstract class GridNodeBase : GraphNode {
- protected GridNodeBase (AstarPath astar) : base(astar) {
- }
- const int GridFlagsWalkableErosionOffset = 8;
- const int GridFlagsWalkableErosionMask = 1 << GridFlagsWalkableErosionOffset;
- const int GridFlagsWalkableTmpOffset = 9;
- const int GridFlagsWalkableTmpMask = 1 << GridFlagsWalkableTmpOffset;
- protected const int NodeInGridIndexLayerOffset = 24;
- protected const int NodeInGridIndexMask = 0xFFFFFF;
-
-
-
-
- protected int nodeInGridIndex;
- protected ushort gridFlags;
- #if !ASTAR_GRID_NO_CUSTOM_CONNECTIONS
-
-
-
-
-
-
-
-
-
-
-
- public Connection[] connections;
- #endif
-
-
-
-
-
-
-
-
-
-
-
- public int NodeInGridIndex { get { return nodeInGridIndex & NodeInGridIndexMask; } set { nodeInGridIndex = (nodeInGridIndex & ~NodeInGridIndexMask) | value; } }
-
-
-
-
-
-
-
- public int XCoordinateInGrid {
- get {
- return NodeInGridIndex % GridNode.GetGridGraph(GraphIndex).width;
- }
- }
-
-
-
-
-
-
-
- public int ZCoordinateInGrid {
- get {
- return NodeInGridIndex / GridNode.GetGridGraph(GraphIndex).width;
- }
- }
-
-
-
-
- public bool WalkableErosion {
- get {
- return (gridFlags & GridFlagsWalkableErosionMask) != 0;
- }
- set {
- unchecked { gridFlags = (ushort)(gridFlags & ~GridFlagsWalkableErosionMask | (value ? (ushort)GridFlagsWalkableErosionMask : (ushort)0)); }
- }
- }
-
- public bool TmpWalkable {
- get {
- return (gridFlags & GridFlagsWalkableTmpMask) != 0;
- }
- set {
- unchecked { gridFlags = (ushort)(gridFlags & ~GridFlagsWalkableTmpMask | (value ? (ushort)GridFlagsWalkableTmpMask : (ushort)0)); }
- }
- }
-
-
-
-
-
- public abstract bool HasConnectionsToAllEightNeighbours { get; }
- public override float SurfaceArea () {
- GridGraph gg = GridNode.GetGridGraph(GraphIndex);
- return gg.nodeSize*gg.nodeSize;
- }
- public override Vector3 RandomPointOnSurface () {
- GridGraph gg = GridNode.GetGridGraph(GraphIndex);
- var graphSpacePosition = gg.transform.InverseTransform((Vector3)position);
- return gg.transform.Transform(graphSpacePosition + new Vector3(Random.value - 0.5f, 0, Random.value - 0.5f));
- }
-
-
-
-
-
-
- public Vector2 NormalizePoint (Vector3 worldPoint) {
- GridGraph gg = GridNode.GetGridGraph(GraphIndex);
- var graphSpacePosition = gg.transform.InverseTransform(worldPoint);
- return new Vector2(graphSpacePosition.x - this.XCoordinateInGrid, graphSpacePosition.z - this.ZCoordinateInGrid);
- }
-
-
-
-
-
-
- public Vector3 UnNormalizePoint (Vector2 normalizedPointOnSurface) {
- GridGraph gg = GridNode.GetGridGraph(GraphIndex);
- return (Vector3)this.position + gg.transform.TransformVector(new Vector3(normalizedPointOnSurface.x - 0.5f, 0, normalizedPointOnSurface.y - 0.5f));
- }
- public override int GetGizmoHashCode () {
- var hash = base.GetGizmoHashCode();
- #if !ASTAR_GRID_NO_CUSTOM_CONNECTIONS
- if (connections != null) {
- for (int i = 0; i < connections.Length; i++) {
- hash ^= 17 * connections[i].GetHashCode();
- }
- }
- #endif
- hash ^= 109 * gridFlags;
- return hash;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public abstract GridNodeBase GetNeighbourAlongDirection(int direction);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public virtual bool HasConnectionInDirection (int direction) {
-
- return GetNeighbourAlongDirection(direction) != null;
- }
- public override bool ContainsConnection (GraphNode node) {
- #if !ASTAR_GRID_NO_CUSTOM_CONNECTIONS
- if (connections != null) {
- for (int i = 0; i < connections.Length; i++) {
- if (connections[i].node == node) {
- return true;
- }
- }
- }
- #endif
- for (int i = 0; i < 8; i++) {
- if (node == GetNeighbourAlongDirection(i)) {
- return true;
- }
- }
- return false;
- }
- #if ASTAR_GRID_NO_CUSTOM_CONNECTIONS
- public override void AddConnection (GraphNode node, uint cost) {
- throw new System.NotImplementedException("GridNodes do not have support for adding manual connections with your current settings."+
- "\nPlease disable ASTAR_GRID_NO_CUSTOM_CONNECTIONS in the Optimizations tab in the A* Inspector");
- }
- public override void RemoveConnection (GraphNode node) {
- throw new System.NotImplementedException("GridNodes do not have support for adding manual connections with your current settings."+
- "\nPlease disable ASTAR_GRID_NO_CUSTOM_CONNECTIONS in the Optimizations tab in the A* Inspector");
- }
- public void ClearCustomConnections (bool alsoReverse) {
- }
- #else
-
- public void ClearCustomConnections (bool alsoReverse) {
- if (connections != null) for (int i = 0; i < connections.Length; i++) connections[i].node.RemoveConnection(this);
- connections = null;
- AstarPath.active.hierarchicalGraph.AddDirtyNode(this);
- }
- public override void ClearConnections (bool alsoReverse) {
- ClearCustomConnections(alsoReverse);
- }
- public override void GetConnections (System.Action<GraphNode> action) {
- if (connections != null) for (int i = 0; i < connections.Length; i++) action(connections[i].node);
- }
- public override void UpdateRecursiveG (Path path, PathNode pathNode, PathHandler handler) {
- ushort pid = handler.PathID;
- if (connections != null) for (int i = 0; i < connections.Length; i++) {
- GraphNode other = connections[i].node;
- PathNode otherPN = handler.GetPathNode(other);
- if (otherPN.parent == pathNode && otherPN.pathID == pid) other.UpdateRecursiveG(path, otherPN, handler);
- }
- }
- public override void Open (Path path, PathNode pathNode, PathHandler handler) {
- ushort pid = handler.PathID;
- if (connections != null) for (int i = 0; i < connections.Length; i++) {
- GraphNode other = connections[i].node;
- if (!path.CanTraverse(other)) continue;
- PathNode otherPN = handler.GetPathNode(other);
- uint tmpCost = connections[i].cost;
- if (otherPN.pathID != pid) {
- otherPN.parent = pathNode;
- otherPN.pathID = pid;
- otherPN.cost = tmpCost;
- otherPN.H = path.CalculateHScore(other);
- otherPN.UpdateG(path);
-
- handler.heap.Add(otherPN);
-
- } else {
-
-
- #if ASTAR_NO_TRAVERSAL_COST
- if (pathNode.G+tmpCost < otherPN.G)
- #else
- if (pathNode.G+tmpCost+path.GetTraversalCost(other) < otherPN.G)
- #endif
- {
-
- otherPN.cost = tmpCost;
- otherPN.parent = pathNode;
- other.UpdateRecursiveG(path, otherPN, handler);
- }
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public override void AddConnection (GraphNode node, uint cost) {
- 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;
- return;
- }
- }
- }
- int connLength = connections != null ? connections.Length : 0;
- var newconns = new Connection[connLength+1];
- for (int i = 0; i < connLength; i++) {
- newconns[i] = connections[i];
- }
- newconns[connLength] = new Connection(node, cost);
- 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 = new Connection[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];
- }
- connections = newconns;
- AstarPath.active.hierarchicalGraph.AddDirtyNode(this);
- return;
- }
- }
- }
- 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);
- }
- }
- }
- public override void DeserializeReferences (GraphSerializationContext ctx) {
-
- if (ctx.meta.version < AstarSerializer.V3_8_3)
- return;
- int count = ctx.reader.ReadInt32();
- if (count == -1) {
- connections = null;
- } else {
- connections = new Connection[count];
- for (int i = 0; i < count; i++) {
- connections[i] = new Connection(ctx.DeserializeNodeReference(), ctx.reader.ReadUInt32());
- }
- }
- }
- #endif
- }
- }
|