/** Add a connection from this node to the specified node. * \param node Node to add a connection to * \param cost Cost of traversing the connection. A cost of 1000 corresponds approximately to the cost of moving 1 world unit. * \param shapeEdge Which edge on the shape of this node to use or -1 if no edge is used. \see Pathfinding.Connection.edge * * If the connection already exists, the cost will simply be updated and * no extra connection added. * * \note Only adds a one-way connection. Consider calling the same function on the other node * to get a two-way connection. */ public void AddConnection(GraphNode node, uint cost, int shapeEdge) { if (node == null) { throw new System.ArgumentNullException(); } // Check if we already have a connection to the node if (connections != null) { for (int i = 0; i < connections.Length; i++) { if (connections[i].node == node) { // Just update the cost for the existing connection connections[i].cost = cost; // Update edge only if it was a definite edge, otherwise reuse the existing one // This makes it possible to use the AddConnection(node,cost) overload to only update the cost // without changing the edge which is required for backwards compatibility. connections[i].shapeEdge = shapeEdge >= 0 ? (byte)shapeEdge : connections[i].shapeEdge; return; } } } // Create new arrays which include the new connection 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; }
/** Add a connection from this node to the specified node. * If the connection already exists, the cost will simply be updated and * no extra connection added. * * \note Only adds a one-way connection. Consider calling the same function on the other node * to get a two-way connection. */ public override void AddConnection(GraphNode node, uint cost) { if (node == null) { throw new System.ArgumentNullException(); } // Check if we already have a connection to the node if (connections != null) { for (int i = 0; i < connections.Length; i++) { if (connections[i].node == node) { // Just update the cost for the existing connection connections[i].cost = cost; return; } } } // Create new arrays which include the new connection 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 = node, cost = cost }; if (connections != null) { ArrayPool <Connection> .Release(ref connections, true); } connections = newconns; }
public override void DeserializeReferences(GraphSerializationContext ctx) { int num = ctx.reader.ReadInt32(); if (num == -1) { this.connections = null; return; } this.connections = ArrayPool <Connection> .ClaimWithExactLength(num); for (int i = 0; i < num; i++) { this.connections[i] = new Connection { node = ctx.DeserializeNodeReference(), cost = ctx.reader.ReadUInt32() }; } }
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 { node = ctx.DeserializeNodeReference(), cost = ctx.reader.ReadUInt32() }; } } }
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() ); } } }
/// <summary> /// Removes any connection from this node to the specified node. /// If no such connection exists, nothing will be done. /// /// Note: This only removes the connection from this node to the other node. /// You may want to call the same function on the other node to remove its eventual connection /// to this node. /// </summary> public override void RemoveConnection(GraphNode node) { if (connections == null) { return; } // Iterate through all connections and check if there are any to the node for (int i = 0; i < connections.Length; i++) { if (connections[i].node == node) { // Create new arrays which have the specified node removed 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 override void AddConnection(GraphNode node, uint cost) { if (node == null) { throw new ArgumentNullException(); } if (this.connections != null) { for (int i = 0; i < this.connections.Length; i++) { if (this.connections[i].node == node) { this.connections[i].cost = cost; return; } } } int num = (this.connections == null) ? 0 : this.connections.Length; Connection[] array = ArrayPool <Connection> .ClaimWithExactLength(num + 1); for (int j = 0; j < num; j++) { array[j] = this.connections[j]; } array[num] = new Connection { node = node, cost = cost }; if (this.connections != null) { ArrayPool <Connection> .Release(ref this.connections, true); } this.connections = array; }