summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Graphs/PointGraph.cs
blob: b99b8746b2c45c15e03e49f9f80e3b6e0b943432 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
using UnityEngine;
using System.Collections.Generic;
using Pathfinding.Serialization;
using Pathfinding.Util;

namespace Pathfinding {
	using Pathfinding.Drawing;
	using Unity.Jobs;

	/// <summary>
	/// Basic point graph.
	///
	/// The point graph is the most basic graph structure, it consists of a number of interconnected points in space called nodes or waypoints.
	/// 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 <see cref="recursive"/> 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 (<see cref="maxDistance)"/>
	/// and then it will check if the axis aligned distance isn't too high. The axis aligned distance, named <see cref="limits"/>,
	/// 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. <see cref="limits"/> and <see cref="maxDistance"/> are treated as being set to infinity if they are set to 0 (zero).
	/// Lastly it will check if there are any obstructions between the nodes using
	/// <a href="http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html">raycasting</a> which can optionally be thick.
	/// 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.
	///
	/// 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.
	/// You can enable <see cref="optimizeForSparseGraph"/> 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]
	/// </summary>
	[JsonOptIn]
	[Pathfinding.Util.Preserve]
	public class PointGraph : NavGraph
		, IUpdatableGraph {
		/// <summary>Childs of this transform are treated as nodes</summary>
		[JsonMember]
		public Transform root;

		/// <summary>If no <see cref="root"/> is set, all nodes with the tag is used as nodes</summary>
		[JsonMember]
		public string searchTag;

		/// <summary>
		/// 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.
		/// </summary>
		[JsonMember]
		public float maxDistance;

		/// <summary>Max distance along the axis for a connection to be valid. 0 = infinity</summary>
		[JsonMember]
		public Vector3 limits;

		/// <summary>Use raycasts to check connections</summary>
		[JsonMember]
		public bool raycast = true;

		/// <summary>Use the 2D Physics API</summary>
		[JsonMember]
		public bool use2DPhysics;

		/// <summary>Use thick raycast</summary>
		[JsonMember]
		public bool thickRaycast;

		/// <summary>Thick raycast radius</summary>
		[JsonMember]
		public float thickRaycastRadius = 1;

		/// <summary>Recursively search for child nodes to the <see cref="root"/></summary>
		[JsonMember]
		public bool recursive = true;

		/// <summary>Layer mask to use for raycast</summary>
		[JsonMember]
		public LayerMask mask;

		/// <summary>
		/// Optimizes the graph for sparse graphs.
		///
		/// This can reduce calculation times for both scanning and for normal path requests by huge amounts.
		/// It reduces the number of node-node checks that need to be done during scan, and can also optimize getting the nearest node from the graph (such as when querying for a path).
		///
		/// Try enabling and disabling this option, check the scan times logged when you scan the graph to see if your graph is suited for this optimization
		/// or if it makes it slower.
		///
		/// The gain of using this optimization increases with larger graphs, the default scan algorithm is brute force and requires O(n^2) checks, this optimization
		/// along with a graph suited for it, requires only O(n) checks during scan (assuming the connection distance limits are reasonable).
		///
		/// Warning:
		/// When you have this enabled, you will not be able to move nodes around using scripting unless you recalculate the lookup structure at the same time.
		/// See: <see cref="RebuildNodeLookup"/>
		///
		/// If you enable this during runtime, you will need to call <see cref="RebuildNodeLookup"/> to make sure any existing nodes are added to the lookup structure.
		/// 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.
		/// </summary>
		[JsonMember]
		public bool optimizeForSparseGraph;

		PointKDTree lookupTree = new PointKDTree();

		/// <summary>
		/// Longest known connection.
		/// In squared Int3 units.
		///
		/// See: <see cref="RegisterConnectionLength"/>
		/// </summary>
		long maximumConnectionLength = 0;

		/// <summary>
		/// All nodes in this graph.
		/// Note that only the first <see cref="nodeCount"/> will be non-null.
		///
		/// You can also use the GetNodes method to get all nodes.
		/// </summary>
		public PointNode[] nodes;

		/// <summary>
		/// \copydoc Pathfinding::PointGraph::NodeDistanceMode
		///
		/// See: <see cref="NodeDistanceMode"/>
		///
		/// If you enable this during runtime, you will need to call <see cref="RebuildConnectionDistanceLookup"/> 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.
		/// </summary>
		[JsonMember]
		public NodeDistanceMode nearestNodeDistanceMode;

		/// <summary>Number of nodes in this graph</summary>
		public int nodeCount { get; protected set; }

		public override bool isScanned => nodes != null;

		/// <summary>
		/// 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: <see cref="Pathfinding.StartEndModifier.exactEndPoint"/>
		/// </summary>
		public enum NodeDistanceMode {
			/// <summary>
			/// 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.
			/// </summary>
			Node,
			/// <summary>
			/// 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.
			/// </summary>
			Connection,
		}

		public override int CountNodes () {
			return nodeCount;
		}

		public override void GetNodes (System.Action<GraphNode> action) {
			if (nodes == null) return;
			var count = nodeCount;
			for (int i = 0; i < count; i++) action(nodes[i]);
		}

		public override NNInfo GetNearest (Vector3 position, NNConstraint constraint, float maxDistanceSqr) {
			if (nodes == null) return NNInfo.Empty;
			var iposition = (Int3)position;

			if (optimizeForSparseGraph) {
				if (nearestNodeDistanceMode == NodeDistanceMode.Node) {
					var minDistSqr = maxDistanceSqr;
					var closestNode = lookupTree.GetNearest(iposition, constraint, ref minDistSqr);
					return new NNInfo(closestNode, (Vector3)closestNode.position, minDistSqr);
				} else {
					var closestNode = lookupTree.GetNearestConnection(iposition, constraint, maximumConnectionLength);
					if (closestNode == null) return NNInfo.Empty;

					return FindClosestConnectionPoint(closestNode as PointNode, position, maxDistanceSqr);
				}
			}

			PointNode minNode = null;
			long minDist = AstarMath.SaturatingConvertFloatToLong(maxDistanceSqr * Int3.FloatPrecision * Int3.FloatPrecision);

			for (int i = 0; i < nodeCount; i++) {
				PointNode node = nodes[i];
				long dist = (iposition - node.position).sqrMagnitudeLong;

				if (dist < minDist && (constraint == null || constraint.Suitable(node))) {
					minDist = dist;
					minNode = node;
				}
			}

			float distSqr = Int3.PrecisionFactor*Int3.PrecisionFactor*minDist;
			// Do a final distance check here just to make sure we don't exceed the max distance due to rounding errors when converting between longs and floats
			return distSqr < maxDistanceSqr && minNode != null ? new NNInfo(minNode, (Vector3)minNode.position, Int3.PrecisionFactor*Int3.PrecisionFactor*minDist) : NNInfo.Empty;
		}

		NNInfo FindClosestConnectionPoint (PointNode node, Vector3 position, float maxDistanceSqr) {
			var closestConnectionPoint = (Vector3)node.position;
			var conns = node.connections;
			var nodePos = (Vector3)node.position;

			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 < maxDistanceSqr) {
						maxDistanceSqr = dist;
						closestConnectionPoint = closestPoint;
					}
				}
			}

			return new NNInfo(node, closestConnectionPoint, maxDistanceSqr);
		}

		/// <summary>
		/// 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
		///
		/// <code>
		/// 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;
		///     GraphNode.Connect(node1, node2, cost);
		/// }));
		/// </code>
		///
		/// See: runtime-graphs (view in online documentation for working links)
		/// </summary>
		public PointNode AddNode (Int3 position) {
			return AddNode(new PointNode(active), position);
		}

		/// <summary>
		/// 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 cref="AstarPath.AddWorkItem"/>
		/// See: runtime-graphs (view in online documentation for working links)
		/// </summary>
		/// <param name="node">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.</param>
		/// <param name="position">The node will be set to this position.</param>
		public T AddNode<T>(T node, Int3 position) where T : PointNode {
			AssertSafeToUpdateGraph();
			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++;

			if (optimizeForSparseGraph) AddToLookup(node);

			return node;
		}

		/// <summary>Recursively counds children of a transform</summary>
		protected static int CountChildren (Transform tr) {
			int c = 0;

			foreach (Transform child in tr) {
				c++;
				c += CountChildren(child);
			}
			return c;
		}

		/// <summary>Recursively adds childrens of a transform as nodes</summary>
		protected static void AddChildren (PointNode[] nodes, 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(nodes, ref c, child);
			}
		}

		/// <summary>
		/// Rebuilds the lookup structure for nodes.
		///
		/// This is used when <see cref="optimizeForSparseGraph"/> is enabled.
		///
		/// You should call this method every time you move a node in the graph manually and
		/// you are using <see cref="optimizeForSparseGraph"/>, otherwise pathfinding might not work correctly.
		///
		/// You may also call this after you have added many nodes using the
		/// <see cref="AddNode"/> method. When adding nodes using the <see cref="AddNode"/> 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 <see cref="GetNearest"/> method slightly. The improvements
		/// are on the order of 10-20%.
		/// </summary>
		public void RebuildNodeLookup () {
			lookupTree = BuildNodeLookup(nodes, nodeCount, optimizeForSparseGraph);
			RebuildConnectionDistanceLookup();
		}

		static PointKDTree BuildNodeLookup (PointNode[] nodes, int nodeCount, bool optimizeForSparseGraph) {
			if (optimizeForSparseGraph && nodes != null) {
				var lookupTree = new PointKDTree();
				lookupTree.Rebuild(nodes, 0, nodeCount);
				return lookupTree;
			} else {
				return null;
			}
		}

		/// <summary>Rebuilds a cache used when <see cref="nearestNodeDistanceMode"/> = <see cref="NodeDistanceMode"/>.ToConnection</summary>
		public void RebuildConnectionDistanceLookup () {
			if (nearestNodeDistanceMode == NodeDistanceMode.Connection) {
				maximumConnectionLength = LongestConnectionLength(nodes, nodeCount);
			} else {
				maximumConnectionLength = 0;
			}
		}

		static long LongestConnectionLength (PointNode[] nodes, int nodeCount) {
			long maximumConnectionLength = 0;
			for (int j = 0; j < nodeCount; j++) {
				var node = nodes[j];
				var conns = node.connections;
				if (conns != null) {
					for (int i = 0; i < conns.Length; i++) {
						var distSqr = (node.position - conns[i].node.position).sqrMagnitudeLong;
						maximumConnectionLength = System.Math.Max(maximumConnectionLength, distSqr);
					}
				}
			}
			return maximumConnectionLength;
		}

		void AddToLookup (PointNode node) {
			lookupTree.Add(node);
		}

		/// <summary>
		/// 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 GraphNode.Connect this is done automatically.
		/// It is also done for all nodes when <see cref="RebuildNodeLookup"/> is called.
		/// </summary>
		/// <param name="sqrLength">The length of the connection in squared Int3 units. This can be calculated using (node1.position - node2.position).sqrMagnitudeLong.</param>
		public void RegisterConnectionLength (long sqrLength) {
			maximumConnectionLength = System.Math.Max(maximumConnectionLength, sqrLength);
		}

		protected virtual PointNode[] CreateNodes (int count) {
			var nodes = new PointNode[count];

			for (int i = 0; i < count; i++) nodes[i] = new PointNode(active);
			return nodes;
		}

		class PointGraphScanPromise : IGraphUpdatePromise {
			public PointGraph graph;
			PointKDTree lookupTree;
			PointNode[] nodes;

			public IEnumerator<JobHandle> Prepare () {
				var root = graph.root;
				if (root == null) {
					// If there is no root object, try to find nodes with the specified tag instead
					GameObject[] gos = graph.searchTag != null? GameObject.FindGameObjectsWithTag(graph.searchTag) : null;

					if (gos == null) {
						nodes = new PointNode[0];
					} else {
						// Create all the nodes
						nodes = graph.CreateNodes(gos.Length);

						for (int i = 0; i < gos.Length; i++) {
							var node = nodes[i];
							node.position = (Int3)gos[i].transform.position;
							node.Walkable = true;
							node.gameObject = gos[i].gameObject;
						}
					}
				} else {
					// Search the root for children and create nodes for them
					if (!graph.recursive) {
						var nodeCount = root.childCount;
						nodes = graph.CreateNodes(nodeCount);

						int c = 0;
						foreach (Transform child in root) {
							var node = nodes[c];
							node.position = (Int3)child.position;
							node.Walkable = true;
							node.gameObject = child.gameObject;
							c++;
						}
					} else {
						var nodeCount = CountChildren(root);
						nodes = graph.CreateNodes(nodeCount);

						int nodeIndex = 0;
						AddChildren(nodes, ref nodeIndex, root);
						UnityEngine.Assertions.Assert.AreEqual(nodeIndex, nodeCount);
					}
				}

				yield return default;
				lookupTree = BuildNodeLookup(nodes, nodes.Length, graph.optimizeForSparseGraph);

				foreach (var progress in ConnectNodesAsync(nodes, nodes.Length, lookupTree, graph.maxDistance, graph.limits, graph)) yield return default;
			}

			public void Apply (IGraphUpdateContext ctx) {
				// Destroy all previous nodes (if any)
				graph.DestroyAllNodes();
				// Assign the new node data
				graph.lookupTree = lookupTree;
				graph.nodes = nodes;
				graph.nodeCount = nodes.Length;
				graph.maximumConnectionLength = graph.nearestNodeDistanceMode == NodeDistanceMode.Connection ? LongestConnectionLength(nodes, nodes.Length) : 0;
			}
		}

		protected override IGraphUpdatePromise ScanInternal () => new PointGraphScanPromise { graph = this };

		/// <summary>
		/// Recalculates connections for all nodes in the graph.
		/// This is useful if you have created nodes manually using <see cref="AddNode"/> and then want to connect them in the same way as the point graph normally connects nodes.
		/// </summary>
		public void ConnectNodes () {
			AssertSafeToUpdateGraph();
			var ie = ConnectNodesAsync(nodes, nodeCount, lookupTree, maxDistance, limits, this).GetEnumerator();

			while (ie.MoveNext()) {}

			RebuildConnectionDistanceLookup();
		}

		/// <summary>
		/// Calculates connections for all nodes in the graph.
		/// This is an IEnumerable, you can iterate through it using e.g foreach to get progress information.
		/// </summary>
		static IEnumerable<float> ConnectNodesAsync (PointNode[] nodes, int nodeCount, PointKDTree lookupTree, float maxDistance, Vector3 limits, PointGraph graph) {
			if (maxDistance >= 0) {
				// To avoid too many allocations, these lists are reused for each node
				var connections = new List<Connection>();
				var candidateConnections = new List<GraphNode>();

				long maxSquaredRange;
				// Max possible squared length of a connection between two nodes
				// This is used to speed up the calculations by skipping a lot of nodes that do not need to be checked
				if (maxDistance == 0 && (limits.x == 0 || limits.y == 0 || limits.z == 0)) {
					maxSquaredRange = long.MaxValue;
				} else {
					maxSquaredRange = (long)(Mathf.Max(limits.x, Mathf.Max(limits.y, Mathf.Max(limits.z, maxDistance))) * Int3.Precision) + 1;
					maxSquaredRange *= maxSquaredRange;
				}

				// Report progress every N nodes
				const int YieldEveryNNodes = 512;

				// Loop through all nodes and add connections to other nodes
				for (int i = 0; i < nodeCount; i++) {
					if (i % YieldEveryNNodes == 0) {
						yield return i/(float)nodeCount;
					}

					connections.Clear();
					var node = nodes[i];
					if (lookupTree != null) {
						candidateConnections.Clear();
						lookupTree.GetInRange(node.position, maxSquaredRange, candidateConnections);
						for (int j = 0; j < candidateConnections.Count; j++) {
							var other = candidateConnections[j] as PointNode;
							if (other != node && graph.IsValidConnection(node, other, out var dist)) {
								connections.Add(new Connection(
									other,
									/// <summary>TODO: Is this equal to .costMagnitude</summary>
									(uint)Mathf.RoundToInt(dist*Int3.FloatPrecision),
									true,
									true
									));
							}
						}
					} else {
						// brute force
						for (int j = 0; j < nodeCount; j++) {
							if (i == j) continue;

							PointNode other = nodes[j];
							if (graph.IsValidConnection(node, other, out var dist)) {
								connections.Add(new Connection(
									other,
									/// <summary>TODO: Is this equal to .costMagnitude</summary>
									(uint)Mathf.RoundToInt(dist*Int3.FloatPrecision),
									true,
									true
									));
							}
						}
					}
					node.connections = connections.ToArray();
					node.SetConnectivityDirty();
				}
			}
		}

		/// <summary>
		/// Returns if the connection between a and b is valid.
		/// Checks for obstructions using raycasts (if enabled) and checks for height differences.
		/// As a bonus, it outputs the distance between the nodes too if the connection is valid.
		///
		/// Note: This is not the same as checking if node a is connected to node b.
		/// That should be done using a.ContainsOutgoingConnection(b)
		/// </summary>
		public virtual bool IsValidConnection (GraphNode a, GraphNode b, out float dist) {
			dist = 0;

			if (!a.Walkable || !b.Walkable) return false;

			var dir = (Vector3)(b.position-a.position);

			if (
				(!Mathf.Approximately(limits.x, 0) && Mathf.Abs(dir.x) > limits.x) ||
				(!Mathf.Approximately(limits.y, 0) && Mathf.Abs(dir.y) > limits.y) ||
				(!Mathf.Approximately(limits.z, 0) && Mathf.Abs(dir.z) > limits.z)) {
				return false;
			}

			dist = dir.magnitude;
			if (maxDistance == 0 || dist < maxDistance) {
				if (raycast) {
					var ray = new Ray((Vector3)a.position, dir);
					var invertRay = new Ray((Vector3)b.position, -dir);

					if (use2DPhysics) {
						if (thickRaycast) {
							return !Physics2D.CircleCast(ray.origin, thickRaycastRadius, ray.direction, dist, mask) && !Physics2D.CircleCast(invertRay.origin, thickRaycastRadius, invertRay.direction, dist, mask);
						} else {
							return !Physics2D.Linecast((Vector2)(Vector3)a.position, (Vector2)(Vector3)b.position, mask) && !Physics2D.Linecast((Vector2)(Vector3)b.position, (Vector2)(Vector3)a.position, mask);
						}
					} else {
						if (thickRaycast) {
							return !Physics.SphereCast(ray, thickRaycastRadius, dist, mask) && !Physics.SphereCast(invertRay, thickRaycastRadius, dist, mask);
						} else {
							return !Physics.Linecast((Vector3)a.position, (Vector3)b.position, mask) && !Physics.Linecast((Vector3)b.position, (Vector3)a.position, mask);
						}
					}
				} else {
					return true;
				}
			}
			return false;
		}

		class PointGraphUpdatePromise : IGraphUpdatePromise {
			public PointGraph graph;
			public List<GraphUpdateObject> graphUpdates;

			public void Apply (IGraphUpdateContext ctx) {
				var nodes = graph.nodes;
				for (int u = 0; u < graphUpdates.Count; u++) {
					var guo = graphUpdates[u];
					for (int i = 0; i < graph.nodeCount; i++) {
						var node = nodes[i];
						if (guo.bounds.Contains((Vector3)node.position)) {
							guo.WillUpdateNode(node);
							guo.Apply(node);
						}
					}

					if (guo.updatePhysics) {
						// Use a copy of the bounding box, we should not change the GUO's bounding box since it might be used for other graph updates
						Bounds bounds = guo.bounds;

						if (graph.thickRaycast) {
							// Expand the bounding box to account for the thick raycast
							bounds.Expand(graph.thickRaycastRadius*2);
						}

						// Create a temporary list used for holding connection data
						List<Connection> tmpList = Pathfinding.Util.ListPool<Connection>.Claim();

						for (int i = 0; i < graph.nodeCount; i++) {
							PointNode node = graph.nodes[i];
							var nodePos = (Vector3)node.position;

							List<Connection> conn = null;

							for (int j = 0; j < graph.nodeCount; j++) {
								if (j == i) continue;

								var otherNodePos = (Vector3)nodes[j].position;
								// Check if this connection intersects the bounding box.
								// If it does we need to recalculate that connection.
								if (VectorMath.SegmentIntersectsBounds(bounds, nodePos, otherNodePos)) {
									float dist;
									PointNode other = nodes[j];
									bool contains = node.ContainsOutgoingConnection(other);
									bool validConnection = graph.IsValidConnection(node, other, out dist);

									// Fill the 'conn' list when we need to change a connection
									if (conn == null && (contains != validConnection)) {
										tmpList.Clear();
										conn = tmpList;
										conn.AddRange(node.connections);
									}

									if (!contains && validConnection) {
										// A new connection should be added
										uint cost = (uint)Mathf.RoundToInt(dist*Int3.FloatPrecision);
										conn.Add(new Connection(other, cost, true, true));
										graph.RegisterConnectionLength((other.position - node.position).sqrMagnitudeLong);
									} else if (contains && !validConnection) {
										// A connection should be removed
										for (int q = 0; q < conn.Count; q++) {
											if (conn[q].node == other) {
												conn.RemoveAt(q);
												break;
											}
										}
									}
								}
							}

							// Save the new connections if any were changed
							if (conn != null) {
								node.connections = conn.ToArray();
								node.SetConnectivityDirty();
							}
						}

						// Release buffers back to the pool
						ListPool<Connection>.Release(ref tmpList);
						ctx.DirtyBounds(guo.bounds);
					}
				}

				ListPool<GraphUpdateObject>.Release(ref graphUpdates);
			}
		}

		/// <summary>
		/// Updates an area in the list graph.
		/// Recalculates possibly affected connections, i.e all connectionlines passing trough the bounds of the guo will be recalculated
		/// </summary>
		IGraphUpdatePromise IUpdatableGraph.ScheduleGraphUpdates (List<GraphUpdateObject> graphUpdates) {
			if (!isScanned) return null;

			return new PointGraphUpdatePromise {
					   graph = this,
					   graphUpdates = graphUpdates
			};
		}

#if UNITY_EDITOR
		public override void OnDrawGizmos (DrawingData gizmos, bool drawNodes, RedrawScope redrawScope) {
			base.OnDrawGizmos(gizmos, drawNodes, redrawScope);

			if (!drawNodes) return;

			using (var draw = gizmos.GetBuilder()) {
				using (draw.WithColor(new Color(0.161f, 0.341f, 1f, 0.5f))) {
					if (root != null) {
						DrawChildren(draw, this, root);
					} else if (!string.IsNullOrEmpty(searchTag)) {
						GameObject[] gos = GameObject.FindGameObjectsWithTag(searchTag);
						for (int i = 0; i < gos.Length; i++) {
							draw.SolidBox(gos[i].transform.position, Vector3.one*UnityEditor.HandleUtility.GetHandleSize(gos[i].transform.position)*0.1F);
						}
					}
				}
			}
		}

		static void DrawChildren (CommandBuilder draw, PointGraph graph, Transform tr) {
			foreach (Transform child in tr) {
				draw.SolidBox(child.position, Vector3.one*UnityEditor.HandleUtility.GetHandleSize(child.position)*0.1F);
				if (graph.recursive) DrawChildren(draw, graph, child);
			}
		}
#endif

		protected override void PostDeserialization (GraphSerializationContext ctx) {
			RebuildNodeLookup();
		}

		public override void RelocateNodes (Matrix4x4 deltaMatrix) {
			base.RelocateNodes(deltaMatrix);
			RebuildNodeLookup();
		}

		protected override void SerializeExtraInfo (GraphSerializationContext ctx) {
			// Serialize node data

			if (nodes == null) ctx.writer.Write(-1);

			// Length prefixed array of nodes
			ctx.writer.Write(nodeCount);
			for (int i = 0; i < nodeCount; i++) {
				// -1 indicates a null field
				if (nodes[i] == null) ctx.writer.Write(-1);
				else {
					ctx.writer.Write(0);
					nodes[i].SerializeNode(ctx);
				}
			}
		}

		protected override void DeserializeExtraInfo (GraphSerializationContext ctx) {
			int count = ctx.reader.ReadInt32();

			if (count == -1) {
				nodes = null;
				return;
			}

			nodes = new PointNode[count];
			nodeCount = count;

			for (int i = 0; i < nodes.Length; i++) {
				if (ctx.reader.ReadInt32() == -1) continue;
				nodes[i] = new PointNode(active);
				nodes[i].DeserializeNode(ctx);
			}
		}
	}
}