123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920 |
- using System.Collections.Generic;
- using Pathfinding.Serialization;
- using UnityEngine;
- namespace Pathfinding {
-
- public class GridNode : GridNodeBase {
- public GridNode (AstarPath astar) : base(astar) {
- }
- #if !ASTAR_NO_GRID_GRAPH
- private static GridGraph[] _gridGraphs = new GridGraph[0];
- public static GridGraph GetGridGraph (uint graphIndex) { return _gridGraphs[(int)graphIndex]; }
- public static void SetGridGraph (int graphIndex, GridGraph graph) {
- if (_gridGraphs.Length <= graphIndex) {
- var gg = new GridGraph[graphIndex+1];
- for (int i = 0; i < _gridGraphs.Length; i++) gg[i] = _gridGraphs[i];
- _gridGraphs = gg;
- }
- _gridGraphs[graphIndex] = graph;
- }
- public static void ClearGridGraph (int graphIndex, GridGraph graph) {
- if (graphIndex < _gridGraphs.Length && _gridGraphs[graphIndex] == graph) {
- _gridGraphs[graphIndex] = null;
- }
- }
-
- internal ushort InternalGridFlags {
- get { return gridFlags; }
- set { gridFlags = value; }
- }
- const int GridFlagsConnectionOffset = 0;
- const int GridFlagsConnectionBit0 = 1 << GridFlagsConnectionOffset;
- const int GridFlagsConnectionMask = 0xFF << GridFlagsConnectionOffset;
- const int GridFlagsEdgeNodeOffset = 10;
- const int GridFlagsEdgeNodeMask = 1 << GridFlagsEdgeNodeOffset;
- public override bool HasConnectionsToAllEightNeighbours {
- get {
- return (InternalGridFlags & GridFlagsConnectionMask) == GridFlagsConnectionMask;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public override bool HasConnectionInDirection (int dir) {
- return (gridFlags >> dir & GridFlagsConnectionBit0) != 0;
- }
-
-
-
-
- [System.Obsolete("Use HasConnectionInDirection")]
- public bool GetConnectionInternal (int dir) {
- return HasConnectionInDirection(dir);
- }
-
-
-
-
- public void SetConnectionInternal (int dir, bool value) {
-
- unchecked { gridFlags = (ushort)(gridFlags & ~((ushort)1 << GridFlagsConnectionOffset << dir) | (value ? (ushort)1 : (ushort)0) << GridFlagsConnectionOffset << dir); }
- AstarPath.active.hierarchicalGraph.AddDirtyNode(this);
- }
-
-
-
-
-
-
- public void SetAllConnectionInternal (int connections) {
- unchecked { gridFlags = (ushort)((gridFlags & ~GridFlagsConnectionMask) | (connections << GridFlagsConnectionOffset)); }
- AstarPath.active.hierarchicalGraph.AddDirtyNode(this);
- }
-
-
-
-
-
- public void ResetConnectionsInternal () {
- unchecked {
- gridFlags = (ushort)(gridFlags & ~GridFlagsConnectionMask);
- }
- AstarPath.active.hierarchicalGraph.AddDirtyNode(this);
- }
-
-
-
-
- public bool EdgeNode {
- get {
- return (gridFlags & GridFlagsEdgeNodeMask) != 0;
- }
- set {
- unchecked { gridFlags = (ushort)(gridFlags & ~GridFlagsEdgeNodeMask | (value ? GridFlagsEdgeNodeMask : 0)); }
- }
- }
- public override GridNodeBase GetNeighbourAlongDirection (int direction) {
- if (HasConnectionInDirection(direction)) {
- GridGraph gg = GetGridGraph(GraphIndex);
- return gg.nodes[NodeInGridIndex+gg.neighbourOffsets[direction]];
- }
- return null;
- }
- public override void ClearConnections (bool alsoReverse) {
- if (alsoReverse) {
-
-
- for (int i = 0; i < 8; i++) {
- var other = GetNeighbourAlongDirection(i) as GridNode;
- if (other != null) {
-
- other.SetConnectionInternal(i < 4 ? ((i + 2) % 4) : (((i-2) % 4) + 4), false);
- }
- }
- }
- ResetConnectionsInternal();
- #if !ASTAR_GRID_NO_CUSTOM_CONNECTIONS
- base.ClearConnections(alsoReverse);
- #endif
- }
- public override void GetConnections (System.Action<GraphNode> action) {
- GridGraph gg = GetGridGraph(GraphIndex);
- int[] neighbourOffsets = gg.neighbourOffsets;
- var nodes = gg.nodes;
- for (int i = 0; i < 8; i++) {
- if (HasConnectionInDirection(i)) {
- var other = nodes[NodeInGridIndex + neighbourOffsets[i]];
- if (other != null) action(other);
- }
- }
- #if !ASTAR_GRID_NO_CUSTOM_CONNECTIONS
- base.GetConnections(action);
- #endif
- }
- public override Vector3 ClosestPointOnNode (Vector3 p) {
- var gg = GetGridGraph(GraphIndex);
-
- p = gg.transform.InverseTransform(p);
-
- int x = NodeInGridIndex % gg.width;
- int z = NodeInGridIndex / gg.width;
-
- float y = gg.transform.InverseTransform((Vector3)position).y;
- var closestInGraphSpace = new Vector3(Mathf.Clamp(p.x, x, x+1f), y, Mathf.Clamp(p.z, z, z+1f));
-
- return gg.transform.Transform(closestInGraphSpace);
- }
- public override bool GetPortal (GraphNode other, List<Vector3> left, List<Vector3> right, bool backwards) {
- if (backwards) return true;
- GridGraph gg = GetGridGraph(GraphIndex);
- int[] neighbourOffsets = gg.neighbourOffsets;
- var nodes = gg.nodes;
- for (int i = 0; i < 4; i++) {
- if (HasConnectionInDirection(i) && other == nodes[NodeInGridIndex + neighbourOffsets[i]]) {
- Vector3 middle = ((Vector3)(position + other.position))*0.5f;
- Vector3 cross = Vector3.Cross(gg.collision.up, (Vector3)(other.position-position));
- cross.Normalize();
- cross *= gg.nodeSize*0.5f;
- left.Add(middle - cross);
- right.Add(middle + cross);
- return true;
- }
- }
- for (int i = 4; i < 8; i++) {
- if (HasConnectionInDirection(i) && other == nodes[NodeInGridIndex + neighbourOffsets[i]]) {
- bool rClear = false;
- bool lClear = false;
- if (HasConnectionInDirection(i-4)) {
- var n2 = nodes[NodeInGridIndex + neighbourOffsets[i-4]];
- if (n2.Walkable && n2.HasConnectionInDirection((i-4+1)%4)) {
- rClear = true;
- }
- }
- if (HasConnectionInDirection((i-4+1)%4)) {
- var n2 = nodes[NodeInGridIndex + neighbourOffsets[(i-4+1)%4]];
- if (n2.Walkable && n2.HasConnectionInDirection(i-4)) {
- lClear = true;
- }
- }
- Vector3 middle = ((Vector3)(position + other.position))*0.5f;
- Vector3 cross = Vector3.Cross(gg.collision.up, (Vector3)(other.position-position));
- cross.Normalize();
- cross *= gg.nodeSize*1.4142f;
- left.Add(middle - (lClear ? cross : Vector3.zero));
- right.Add(middle + (rClear ? cross : Vector3.zero));
- return true;
- }
- }
- return false;
- }
- public override void UpdateRecursiveG (Path path, PathNode pathNode, PathHandler handler) {
- GridGraph gg = GetGridGraph(GraphIndex);
- int[] neighbourOffsets = gg.neighbourOffsets;
- var nodes = gg.nodes;
- pathNode.UpdateG(path);
- handler.heap.Add(pathNode);
- ushort pid = handler.PathID;
- var index = NodeInGridIndex;
- for (int i = 0; i < 8; i++) {
- if (HasConnectionInDirection(i)) {
- var other = nodes[index + neighbourOffsets[i]];
- PathNode otherPN = handler.GetPathNode(other);
- if (otherPN.parent == pathNode && otherPN.pathID == pid) other.UpdateRecursiveG(path, otherPN, handler);
- }
- }
- #if !ASTAR_GRID_NO_CUSTOM_CONNECTIONS
- base.UpdateRecursiveG(path, pathNode, handler);
- #endif
- }
- #if ASTAR_JPS
- static byte[] JPSForced = {
- 0xFF,
- 0,
- (1<<3),
- 0,
- 0,
- 0,
- (1<<5),
- 0
- };
- static byte[] JPSForcedDiagonal = {
- 0xFF,
- (1<<2),
- 0,
- 0,
- 0,
- 0,
- 0,
- (1<<6)
- };
-
-
-
-
- static int[] JPSCyclic = {
- 6,
- 4,
- 2,
- 0,
- 5,
- 3,
- 1,
- 7
- };
-
- static int[] JPSInverseCyclic = {
- 3,
- 6,
- 2,
- 5,
- 1,
- 4,
- 0,
- 7
- };
- const int JPSNaturalStraightNeighbours = 1 << 4;
- const int JPSNaturalDiagonalNeighbours = (1 << 5) | (1 << 4) | (1 << 3);
-
- GridNodeBase[] JPSCache;
-
-
-
-
-
- byte[] JPSDead;
- ushort[] JPSLastCacheID;
-
-
-
-
- static GridNodeBase JPSJumpStraight (GridNode node, Path path, PathHandler handler, int parentDir, int depth = 0) {
- GridGraph gg = GetGridGraph(node.GraphIndex);
- int[] neighbourOffsets = gg.neighbourOffsets;
- var nodes = gg.nodes;
- var origin = node;
-
-
-
- int threadID = handler.threadID;
- int threadOffset = 8*handler.threadID;
- int cyclicParentDir = JPSCyclic[parentDir];
- GridNodeBase result = null;
-
- const int forwardDir = 4;
- int forwardOffset = neighbourOffsets[JPSInverseCyclic[(forwardDir + cyclicParentDir) % 8]];
-
-
-
-
-
-
-
-
-
- while (true) {
-
-
-
- if (node.JPSLastCacheID == null || node.JPSLastCacheID.Length < handler.totalThreadCount) {
- lock (node) {
-
- if (node.JPSLastCacheID == null || node.JPSLastCacheID.Length < handler.totalThreadCount) {
- node.JPSCache = new GridNode[8*handler.totalThreadCount];
- node.JPSDead = new byte[handler.totalThreadCount];
- node.JPSLastCacheID = new ushort[handler.totalThreadCount];
- }
- }
- }
- if (node.JPSLastCacheID[threadID] != path.pathID) {
- for (int i = 0; i < 8; i++) node.JPSCache[i + threadOffset] = null;
- node.JPSLastCacheID[threadID] = path.pathID;
- node.JPSDead[threadID] = 0;
- }
-
-
-
- var cachedResult = node.JPSCache[parentDir + threadOffset];
- if (cachedResult != null) {
- result = cachedResult;
- break;
- }
- if (((node.JPSDead[threadID] >> parentDir)&1) != 0) return null;
-
- if (handler.GetPathNode(node).flag2) {
-
-
- result = node;
- break;
- }
- #if !ASTAR_GRID_NO_CUSTOM_CONNECTIONS
-
- if (node.connections != null && node.connections.Length > 0) {
- result = node;
- break;
- }
- #endif
-
- int noncyclic = node.gridFlags;
- int cyclic = 0;
- for (int i = 0; i < 8; i++) cyclic |= ((noncyclic >> i)&0x1) << JPSCyclic[i];
- int forced = 0;
-
- cyclic = ((cyclic >> cyclicParentDir) | ((cyclic << 8) >> cyclicParentDir)) & 0xFF;
-
- if ((cyclic & (1 << 2)) == 0) forced |= (1<<3);
- if ((cyclic & (1 << 6)) == 0) forced |= (1<<5);
- int natural = JPSNaturalStraightNeighbours;
-
-
- if ((forced & (~natural) & cyclic) != 0) {
-
- result = node;
- break;
- }
-
- if ((cyclic & (1 << forwardDir)) != 0) {
- node = nodes[node.nodeInGridIndex + forwardOffset] as GridNode;
-
- } else {
- result = null;
- break;
- }
- }
- if (result == null) {
- while (origin != node) {
- origin.JPSDead[threadID] |= (byte)(1 << parentDir);
- origin = nodes[origin.nodeInGridIndex + forwardOffset] as GridNode;
- }
- } else {
- while (origin != node) {
- origin.JPSCache[parentDir + threadOffset] = result;
- origin = nodes[origin.nodeInGridIndex + forwardOffset] as GridNode;
- }
- }
- return result;
- }
-
-
-
-
- GridNodeBase JPSJumpDiagonal (Path path, PathHandler handler, int parentDir, int depth = 0) {
-
-
-
- int threadID = handler.threadID;
- int threadOffset = 8*handler.threadID;
-
-
-
- if (JPSLastCacheID == null || JPSLastCacheID.Length < handler.totalThreadCount) {
- lock (this) {
-
- if (JPSLastCacheID == null || JPSLastCacheID.Length < handler.totalThreadCount) {
- JPSCache = new GridNode[8*handler.totalThreadCount];
- JPSDead = new byte[handler.totalThreadCount];
- JPSLastCacheID = new ushort[handler.totalThreadCount];
- }
- }
- }
- if (JPSLastCacheID[threadID] != path.pathID) {
- for (int i = 0; i < 8; i++) JPSCache[i + threadOffset] = null;
- JPSLastCacheID[threadID] = path.pathID;
- JPSDead[threadID] = 0;
- }
-
-
-
- var cachedResult = JPSCache[parentDir + threadOffset];
- if (cachedResult != null) {
-
- }
-
-
- if (handler.GetPathNode(this).flag2) {
-
-
- JPSCache[parentDir + threadOffset] = this;
- return this;
- }
- #if !ASTAR_GRID_NO_CUSTOM_CONNECTIONS
-
- if (connections != null && connections.Length > 0) {
- JPSCache[parentDir] = this;
- return this;
- }
- #endif
- int noncyclic = gridFlags;
- int cyclic = 0;
- for (int i = 0; i < 8; i++) cyclic |= ((noncyclic >> i)&0x1) << JPSCyclic[i];
- int forced = 0;
- int cyclicParentDir = JPSCyclic[parentDir];
-
- cyclic = ((cyclic >> cyclicParentDir) | ((cyclic << 8) >> cyclicParentDir)) & 0xFF;
- int natural;
- for (int i = 0; i < 8; i++) if (((cyclic >> i)&1) == 0) forced |= JPSForcedDiagonal[i];
- natural = JPSNaturalDiagonalNeighbours;
-
-
- forced &= cyclic;
- natural &= cyclic;
- if ((forced & (~natural)) != 0) {
-
- JPSCache[parentDir+threadOffset] = this;
- return this;
- }
- int forwardDir;
- GridGraph gg = GetGridGraph(GraphIndex);
- int[] neighbourOffsets = gg.neighbourOffsets;
- var nodes = gg.nodes;
- {
-
- forwardDir = 3;
- if (((cyclic >> forwardDir)&1) != 0) {
- int oi = JPSInverseCyclic[(forwardDir + cyclicParentDir) % 8];
- var other = nodes[nodeInGridIndex + neighbourOffsets[oi]];
-
- GridNodeBase v;
- if (oi < 4) {
- v = JPSJumpStraight(other as GridNode, path, handler, JPSInverseCyclic[(cyclicParentDir-1+8)%8], depth+1);
- } else {
- v = (other as GridNode).JPSJumpDiagonal(path, handler, JPSInverseCyclic[(cyclicParentDir-1+8)%8], depth+1);
- }
- if (v != null) {
- JPSCache[parentDir+threadOffset] = this;
- return this;
- }
- }
-
- forwardDir = 5;
- if (((cyclic >> forwardDir)&1) != 0) {
- int oi = JPSInverseCyclic[(forwardDir + cyclicParentDir) % 8];
- var other = nodes[nodeInGridIndex + neighbourOffsets[oi]];
-
- GridNodeBase v;
- if (oi < 4) {
- v = JPSJumpStraight(other as GridNode, path, handler, JPSInverseCyclic[(cyclicParentDir+1+8)%8], depth+1);
- } else {
- v = (other as GridNode).JPSJumpDiagonal(path, handler, JPSInverseCyclic[(cyclicParentDir+1+8)%8], depth+1);
- }
- if (v != null) {
- JPSCache[parentDir+threadOffset] = this;
- return this;
- }
- }
- }
-
- forwardDir = 4;
- if (((cyclic >> forwardDir)&1) != 0) {
- int oi = JPSInverseCyclic[(forwardDir + cyclicParentDir) % 8];
- var other = nodes[nodeInGridIndex + neighbourOffsets[oi]] as GridNode;
-
- var v = other.JPSJumpDiagonal(path, handler, parentDir, depth+1);
- if (v != null) {
- JPSCache[parentDir+threadOffset] = v;
- return v;
- }
- }
- JPSDead[threadID] |= (byte)(1 << parentDir);
- return null;
- }
-
-
-
-
- public void JPSOpen (Path path, PathNode pathNode, PathHandler handler) {
- GridGraph gg = GetGridGraph(GraphIndex);
- int[] neighbourOffsets = gg.neighbourOffsets;
- var nodes = gg.nodes;
- ushort pid = handler.PathID;
- int noncyclic = gridFlags & 0xFF;
- int cyclic = 0;
- for (int i = 0; i < 8; i++) cyclic |= ((noncyclic >> i)&0x1) << JPSCyclic[i];
- var parent = pathNode.parent != null ? pathNode.parent.node as GridNode : null;
- int parentDir = -1;
- if (parent != null) {
- int diff = parent != null ? parent.nodeInGridIndex - nodeInGridIndex : 0;
- int x2 = nodeInGridIndex % gg.width;
- int x1 = parent.nodeInGridIndex % gg.width;
- if (diff < 0) {
- if (x1 == x2) {
- parentDir = 0;
- } else if (x1 < x2) {
- parentDir = 7;
- } else {
- parentDir = 4;
- }
- } else {
- if (x1 == x2) {
- parentDir = 1;
- } else if (x1 < x2) {
- parentDir = 6;
- } else {
- parentDir = 5;
- }
- }
- }
- int cyclicParentDir = 0;
-
- int forced = 0;
- if (parentDir != -1) {
- cyclicParentDir = JPSCyclic[parentDir];
-
- cyclic = ((cyclic >> cyclicParentDir) | ((cyclic << 8) >> cyclicParentDir)) & 0xFF;
- } else {
- forced = 0xFF;
-
- }
- bool diagonal = parentDir >= 4;
- int natural;
- if (diagonal) {
- for (int i = 0; i < 8; i++) if (((cyclic >> i)&1) == 0) forced |= JPSForcedDiagonal[i];
- natural = JPSNaturalDiagonalNeighbours;
- } else {
- for (int i = 0; i < 8; i++) if (((cyclic >> i)&1) == 0) forced |= JPSForced[i];
- natural = JPSNaturalStraightNeighbours;
- }
-
- forced &= cyclic;
- natural &= cyclic;
- int nb = forced | natural;
-
- for (int i = 0; i < 8; i++) {
- if (((nb >> i)&1) != 0) {
- int oi = JPSInverseCyclic[(i + cyclicParentDir) % 8];
- var other = nodes[nodeInGridIndex + neighbourOffsets[oi]];
- #if ASTARDEBUG
- if (((forced >> i)&1) != 0) {
- Debug.DrawLine((Vector3)position, Vector3.Lerp((Vector3)other.position, (Vector3)position, 0.6f), Color.red);
- }
- if (((natural >> i)&1) != 0) {
- Debug.DrawLine((Vector3)position + Vector3.up*0.2f, Vector3.Lerp((Vector3)other.position, (Vector3)position, 0.6f) + Vector3.up*0.2f, Color.green);
- }
- #endif
- if (oi < 4) {
- other = JPSJumpStraight(other as GridNode, path, handler, JPSInverseCyclic[(i + 4 + cyclicParentDir) % 8]);
- } else {
- other = (other as GridNode).JPSJumpDiagonal(path, handler, JPSInverseCyclic[(i + 4 + cyclicParentDir) % 8]);
- }
- if (other != null) {
-
-
-
-
- PathNode otherPN = handler.GetPathNode(other);
- if (otherPN.pathID != pid) {
- otherPN.parent = pathNode;
- otherPN.pathID = pid;
- otherPN.cost = (uint)(other.position - position).costMagnitude;
- otherPN.H = path.CalculateHScore(other);
- otherPN.UpdateG(path);
-
- handler.heap.Add(otherPN);
-
- } else {
-
- uint tmpCost = (uint)(other.position - position).costMagnitude;
- if (pathNode.G+tmpCost+path.GetTraversalCost(other) < otherPN.G) {
-
- otherPN.cost = tmpCost;
- otherPN.parent = pathNode;
- other.UpdateRecursiveG(path, otherPN, handler);
- }
- }
- }
- }
- #if ASTARDEBUG
- if (i == 0 && parentDir != -1 && this.nodeInGridIndex > 10) {
- int oi = JPSInverseCyclic[(i + cyclicParentDir) % 8];
- if (nodeInGridIndex + neighbourOffsets[oi] < 0 || nodeInGridIndex + neighbourOffsets[oi] >= nodes.Length) {
-
-
- } else {
- var other = nodes[nodeInGridIndex + neighbourOffsets[oi]];
- Debug.DrawLine((Vector3)position - Vector3.up*0.2f, Vector3.Lerp((Vector3)other.position, (Vector3)position, 0.6f) - Vector3.up*0.2f, Color.blue);
- }
- }
- #endif
- }
- }
- #endif
- public override void Open (Path path, PathNode pathNode, PathHandler handler) {
- GridGraph gg = GetGridGraph(GraphIndex);
- ushort pid = handler.PathID;
- #if ASTAR_JPS
- if (gg.useJumpPointSearch && !path.FloodingPath) {
- JPSOpen(path, pathNode, handler);
- } else
- #endif
- {
- int[] neighbourOffsets = gg.neighbourOffsets;
- uint[] neighbourCosts = gg.neighbourCosts;
- GridNodeBase[] nodes = gg.nodes;
- var index = NodeInGridIndex;
- for (int i = 0; i < 8; i++) {
- if (HasConnectionInDirection(i)) {
- GridNodeBase other = nodes[index + neighbourOffsets[i]];
- if (!path.CanTraverse(other)) continue;
- PathNode otherPN = handler.GetPathNode(other);
- uint tmpCost = neighbourCosts[i];
-
- 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);
- }
- }
- }
- }
- }
- #if !ASTAR_GRID_NO_CUSTOM_CONNECTIONS
- base.Open(path, pathNode, handler);
- #endif
- }
- public override void SerializeNode (GraphSerializationContext ctx) {
- base.SerializeNode(ctx);
- ctx.SerializeInt3(position);
- ctx.writer.Write(gridFlags);
- }
- public override void DeserializeNode (GraphSerializationContext ctx) {
- base.DeserializeNode(ctx);
- position = ctx.DeserializeInt3();
- gridFlags = ctx.reader.ReadUInt16();
- }
- public override void AddConnection (GraphNode node, uint cost) {
-
-
-
- if (node is GridNode gn && gn.GraphIndex == GraphIndex) {
- RemoveGridConnection(gn);
- }
- base.AddConnection(node, cost);
- }
- public override void RemoveConnection (GraphNode node) {
- base.RemoveConnection(node);
-
- if (node is GridNode gn && gn.GraphIndex == GraphIndex) {
- RemoveGridConnection(gn);
- }
- }
-
-
-
-
- protected void RemoveGridConnection (GridNode node) {
- var nodeIndex = NodeInGridIndex;
- var gg = GetGridGraph(GraphIndex);
- for (int i = 0; i < 8; i++) {
- if (nodeIndex + gg.neighbourOffsets[i] == node.NodeInGridIndex && GetNeighbourAlongDirection(i) == node) {
- SetConnectionInternal(i, false);
- break;
- }
- }
- }
- #else
- public override void AddConnection (GraphNode node, uint cost) {
- throw new System.NotImplementedException();
- }
- public override void ClearConnections (bool alsoReverse) {
- throw new System.NotImplementedException();
- }
- public override void GetConnections (GraphNodeDelegate del) {
- throw new System.NotImplementedException();
- }
- public override void Open (Path path, PathNode pathNode, PathHandler handler) {
- throw new System.NotImplementedException();
- }
- public override void RemoveConnection (GraphNode node) {
- throw new System.NotImplementedException();
- }
- #endif
- }
- }
|