123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- using UnityEngine;
- namespace Pathfinding {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class RandomPath : ABPath {
-
-
-
-
- public int searchLength;
-
-
-
-
-
- public int spread = 5000;
-
- public float aimStrength;
-
- PathNode chosenNodeR;
-
-
-
-
- PathNode maxGScoreNodeR;
-
- int maxGScore;
-
-
-
-
-
-
- public Vector3 aim;
- int nodesEvaluatedRep;
-
- readonly System.Random rnd = new System.Random();
- public override bool FloodingPath {
- get {
- return true;
- }
- }
- protected override bool hasEndPoint {
- get {
- return false;
- }
- }
- protected override void Reset () {
- base.Reset();
- searchLength = 5000;
- spread = 5000;
- aimStrength = 0.0f;
- chosenNodeR = null;
- maxGScoreNodeR = null;
- maxGScore = 0;
- aim = Vector3.zero;
- nodesEvaluatedRep = 0;
- }
- public RandomPath () {}
- public static RandomPath Construct (Vector3 start, int length, OnPathDelegate callback = null) {
- var p = PathPool.GetPath<RandomPath>();
- p.Setup(start, length, callback);
- return p;
- }
- protected RandomPath Setup (Vector3 start, int length, OnPathDelegate callback) {
- this.callback = callback;
- searchLength = length;
- originalStartPoint = start;
- originalEndPoint = Vector3.zero;
- startPoint = start;
- endPoint = Vector3.zero;
- startIntPoint = (Int3)start;
- return this;
- }
-
-
-
-
- protected override void ReturnPath () {
- if (path != null && path.Count > 0) {
- endNode = path[path.Count-1];
- endPoint = (Vector3)endNode.position;
- originalEndPoint = endPoint;
- hTarget = endNode.position;
- }
- if (callback != null) {
- callback(this);
- }
- }
- protected override void Prepare () {
- nnConstraint.tags = enabledTags;
- var startNNInfo = AstarPath.active.GetNearest(startPoint, nnConstraint);
- startPoint = startNNInfo.position;
- endPoint = startPoint;
- startIntPoint = (Int3)startPoint;
- hTarget = (Int3)aim;
- startNode = startNNInfo.node;
- endNode = startNode;
- #if ASTARDEBUG
- Debug.DrawLine((Vector3)startNode.position, startPoint, Color.blue);
- Debug.DrawLine((Vector3)endNode.position, endPoint, Color.blue);
- #endif
- if (startNode == null || endNode == null) {
- FailWithError("Couldn't find close nodes to the start point");
- return;
- }
- if (!CanTraverse(startNode)) {
- FailWithError("The node closest to the start point could not be traversed");
- return;
- }
- heuristicScale = aimStrength;
- }
- protected override void Initialize () {
-
-
-
- PathNode startRNode = pathHandler.GetPathNode(startNode);
- startRNode.node = startNode;
- if (searchLength+spread <= 0) {
- CompleteState = PathCompleteState.Complete;
- Trace(startRNode);
- return;
- }
- startRNode.pathID = pathID;
- startRNode.parent = null;
- startRNode.cost = 0;
- startRNode.G = GetTraversalCost(startNode);
- startRNode.H = CalculateHScore(startNode);
-
- startNode.Open(this, startRNode, pathHandler);
-
- searchedNodes++;
-
- if (pathHandler.heap.isEmpty) {
- FailWithError("No open points, the start node didn't open any nodes");
- return;
- }
- currentR = pathHandler.heap.Remove();
- }
- protected override void CalculateStep (long targetTick) {
- int counter = 0;
-
- while (CompleteState == PathCompleteState.NotCalculated) {
- searchedNodes++;
-
- if (currentR.G >= searchLength) {
- if (currentR.G <= searchLength+spread) {
- nodesEvaluatedRep++;
- if (rnd.NextDouble() <= 1.0f/nodesEvaluatedRep) {
- chosenNodeR = currentR;
- }
- } else {
-
- if (chosenNodeR == null) chosenNodeR = currentR;
- CompleteState = PathCompleteState.Complete;
- break;
- }
- } else if (currentR.G > maxGScore) {
- maxGScore = (int)currentR.G;
- maxGScoreNodeR = currentR;
- }
-
- currentR.node.Open(this, currentR, pathHandler);
-
- if (pathHandler.heap.isEmpty) {
- if (chosenNodeR != null) {
- CompleteState = PathCompleteState.Complete;
- } else if (maxGScoreNodeR != null) {
- chosenNodeR = maxGScoreNodeR;
- CompleteState = PathCompleteState.Complete;
- } else {
- FailWithError("Not a single node found to search");
- }
- break;
- }
-
- currentR = pathHandler.heap.Remove();
-
- if (counter > 500) {
-
- if (System.DateTime.UtcNow.Ticks >= targetTick) {
-
- return;
- }
- counter = 0;
- if (searchedNodes > 1000000) {
- throw new System.Exception("Probable infinite loop. Over 1,000,000 nodes searched");
- }
- }
- counter++;
- }
- if (CompleteState == PathCompleteState.Complete) {
- Trace(chosenNodeR);
- }
- }
- }
- }
|