123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- using UnityEngine;
- using System;
- using System.Collections.Generic;
- namespace Pathfinding {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class ConstantPath : Path {
- public GraphNode startNode;
- public Vector3 startPoint;
- public Vector3 originalStartPoint;
-
-
-
-
- public List<GraphNode> allNodes;
-
-
-
-
-
-
- public PathEndingCondition endingCondition;
- public override bool FloodingPath {
- get {
- return true;
- }
- }
-
-
-
-
-
-
-
-
-
- public static ConstantPath Construct (Vector3 start, int maxGScore, OnPathDelegate callback = null) {
- var p = PathPool.GetPath<ConstantPath>();
- p.Setup(start, maxGScore, callback);
- return p;
- }
-
- protected void Setup (Vector3 start, int maxGScore, OnPathDelegate callback) {
- this.callback = callback;
- startPoint = start;
- originalStartPoint = startPoint;
- endingCondition = new EndingConditionDistance(this, maxGScore);
- }
- protected override void OnEnterPool () {
- base.OnEnterPool();
- if (allNodes != null) Util.ListPool<GraphNode>.Release(ref allNodes);
- }
-
-
-
-
-
-
-
- protected override void Reset () {
- base.Reset();
- allNodes = Util.ListPool<GraphNode>.Claim();
- endingCondition = null;
- originalStartPoint = Vector3.zero;
- startPoint = Vector3.zero;
- startNode = null;
- heuristic = Heuristic.None;
- }
- protected override void Prepare () {
- nnConstraint.tags = enabledTags;
- var startNNInfo = AstarPath.active.GetNearest(startPoint, nnConstraint);
- startNode = startNNInfo.node;
- if (startNode == null) {
- FailWithError("Could not find close node to the start point");
- 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);
- startNode.Open(this, startRNode, pathHandler);
- searchedNodes++;
- startRNode.flag1 = true;
- allNodes.Add(startNode);
-
- if (pathHandler.heap.isEmpty) {
- CompleteState = PathCompleteState.Complete;
- return;
- }
- currentR = pathHandler.heap.Remove();
- }
- protected override void Cleanup () {
- int c = allNodes.Count;
- for (int i = 0; i < c; i++) pathHandler.GetPathNode(allNodes[i]).flag1 = false;
- }
- protected override void CalculateStep (long targetTick) {
- int counter = 0;
-
- while (CompleteState == PathCompleteState.NotCalculated) {
- searchedNodes++;
-
- if (endingCondition.TargetFound(currentR)) {
- CompleteState = PathCompleteState.Complete;
- break;
- }
- if (!currentR.flag1) {
-
- allNodes.Add(currentR.node);
- currentR.flag1 = true;
- }
- #if ASTARDEBUG
- Debug.DrawRay((Vector3)currentR.node.position, Vector3.up*5, Color.cyan);
- #endif
- AstarProfiler.StartFastProfile(4);
-
-
- currentR.node.Open(this, currentR, pathHandler);
- 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++;
- }
- }
- }
-
-
-
-
-
-
-
-
- public class EndingConditionDistance : PathEndingCondition {
-
- public int maxGScore = 100;
-
- public EndingConditionDistance (Path p, int maxGScore) : base(p) {
- this.maxGScore = maxGScore;
- }
- public override bool TargetFound (PathNode node) {
- return node.G >= maxGScore;
- }
- }
- }
|