123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- using System;
- using UnityEngine;
- using System.Collections.Generic;
- namespace Pathfinding {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class FloodPath : Path {
- public Vector3 originalStartPoint;
- public Vector3 startPoint;
- public GraphNode startNode;
-
-
-
-
- public bool saveParents = true;
- protected Dictionary<GraphNode, GraphNode> parents;
- public override bool FloodingPath {
- get {
- return true;
- }
- }
- public bool HasPathTo (GraphNode node) {
- return parents != null && parents.ContainsKey(node);
- }
- public GraphNode GetParent (GraphNode node) {
- return parents[node];
- }
-
-
-
-
- public FloodPath () {}
- public static FloodPath Construct (Vector3 start, OnPathDelegate callback = null) {
- var p = PathPool.GetPath<FloodPath>();
- p.Setup(start, callback);
- return p;
- }
- public static FloodPath Construct (GraphNode start, OnPathDelegate callback = null) {
- if (start == null) throw new ArgumentNullException("start");
- var p = PathPool.GetPath<FloodPath>();
- p.Setup(start, callback);
- return p;
- }
- protected void Setup (Vector3 start, OnPathDelegate callback) {
- this.callback = callback;
- originalStartPoint = start;
- startPoint = start;
- heuristic = Heuristic.None;
- }
- protected void Setup (GraphNode start, OnPathDelegate callback) {
- this.callback = callback;
- originalStartPoint = (Vector3)start.position;
- startNode = start;
- startPoint = (Vector3)start.position;
- heuristic = Heuristic.None;
- }
- protected override void Reset () {
- base.Reset();
- originalStartPoint = Vector3.zero;
- startPoint = Vector3.zero;
- startNode = null;
-
- parents = new Dictionary<GraphNode, GraphNode>();
- saveParents = true;
- }
- protected override void Prepare () {
- AstarProfiler.StartProfile("Get Nearest");
- if (startNode == null) {
-
- nnConstraint.tags = enabledTags;
- var startNNInfo = AstarPath.active.GetNearest(originalStartPoint, nnConstraint);
- startPoint = startNNInfo.position;
- startNode = startNNInfo.node;
- } else if (startNode.Destroyed) {
- FailWithError("Start node has been destroyed");
- return;
- } else {
- startPoint = (Vector3)startNode.position;
- }
- AstarProfiler.EndProfile();
- #if ASTARDEBUG
- Debug.DrawLine((Vector3)startNode.position, startPoint, Color.blue);
- #endif
- if (startNode == null) {
- FailWithError("Couldn't find a close node to the start point");
- return;
- }
- if (!CanTraverse(startNode)) {
- FailWithError("The node closest to the start point could not be traversed");
- return;
- }
- }
- protected override void Initialize () {
- PathNode startRNode = pathHandler.GetPathNode(startNode);
- startRNode.node = startNode;
- startRNode.pathID = pathHandler.PathID;
- startRNode.parent = null;
- startRNode.cost = 0;
- startRNode.G = GetTraversalCost(startNode);
- startRNode.H = CalculateHScore(startNode);
- parents[startNode] = null;
- startNode.Open(this, startRNode, pathHandler);
- searchedNodes++;
-
- if (pathHandler.heap.isEmpty) {
- CompleteState = PathCompleteState.Complete;
- return;
- }
- currentR = pathHandler.heap.Remove();
- }
-
- protected override void CalculateStep (long targetTick) {
- int counter = 0;
-
- while (CompleteState == PathCompleteState.NotCalculated) {
- searchedNodes++;
- AstarProfiler.StartFastProfile(4);
-
-
- currentR.node.Open(this, currentR, pathHandler);
-
- if (saveParents) parents[currentR.node] = currentR.parent.node;
- AstarProfiler.EndFastProfile(4);
-
- if (pathHandler.heap.isEmpty) {
- CompleteState = PathCompleteState.Complete;
- break;
- }
-
- AstarProfiler.StartFastProfile(7);
- currentR = pathHandler.heap.Remove();
- AstarProfiler.EndFastProfile(7);
-
- if (counter > 500) {
-
- if (DateTime.UtcNow.Ticks >= targetTick) {
-
- return;
- }
- counter = 0;
- if (searchedNodes > 1000000) {
- throw new Exception("Probable infinite loop. Over 1,000,000 nodes searched");
- }
- }
- counter++;
- }
- }
- }
- }
|