Advertisement
Guest User

Untitled

a guest
Oct 12th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1.     NodeElement _start, _goal, closestPossible;
  2.     PriorityQueue frontier;
  3.     bool searching = false;
  4.     Dictionary<NodeElement, NodeElement> camefrom = new Dictionary<NodeElement, NodeElement>();
  5.     Dictionary<NodeElement, double> costsofar = new Dictionary<NodeElement, double>();
  6.     public IEnumerator MultiThreadAStar(NodeElement goal)
  7.     {
  8.         if (searching == false)
  9.         {
  10.             //Debug.LogError("new search");
  11.            // double tim = Time.time;
  12.             searching = true;
  13.             SquareGrid graph = PathfindingMap.Grid();
  14.             int count = 0;
  15.             frontier = new PriorityQueue();
  16.             frontier.Enqueue(curNode, 0);
  17.             _start = curNode;
  18.             _goal = goal;
  19.             camefrom[_start] = _start;
  20.             costsofar[_start] = 0;
  21.             closestPossible = _start;
  22.             while (frontier.Count > 0)
  23.             {
  24.                 NodeElement current = frontier.Dequeue();
  25.                 if (current.Equals(goal))
  26.                 {
  27.                     break;
  28.                 }
  29.  
  30.                 foreach (var next in graph.Neighbors(current))
  31.                 {
  32.                     double newCost = costsofar[current] + graph.Cost(current, next);
  33.                     // double newCost = current.costSofar + graph.Cost(current, next);
  34.                     if (!costsofar.ContainsKey(next)
  35.                       //  || newCost < next.costSofar)
  36.                       || newCost < costsofar[next])
  37.                     {
  38.                         count++;
  39.                         if (count > 250)
  40.                         {
  41.                             yield return null;
  42.                             count = 0;
  43.                         }
  44.                         double h = Heuristic(next, goal);
  45.                         double h2 = Heuristic(closestPossible, goal);
  46.                         double priority = newCost + Heuristic(next, goal);
  47.  
  48.                         frontier.Enqueue(next, priority);
  49.                         //next.cameFrom = current;
  50.                         //next.costSofar = newCost;
  51.                         camefrom[next] = current;
  52.                         costsofar[next] = newCost;
  53.                         if (h2 > h)
  54.                         {
  55.                             closestPossible = next;
  56.                         }
  57.                     }
  58.                 }
  59.             }
  60.            // Debug.LogError("finished search in " + (Time.time - tim) + " seconds");
  61.             FindShortestPath();
  62.             searching = false;
  63.         }
  64.     }
  65.     public void FindShortestPath()
  66.     {
  67.         path.Clear();
  68.         NodeElement next = _goal, start;
  69.         if (!camefrom.ContainsKey(next))
  70.         //if (next.cameFrom == null)
  71.         {
  72.             next = closestPossible;
  73.             start = closestPossible;
  74.         }
  75.         else
  76.         {
  77.             start = _goal;
  78.         }
  79.         path.Add(next);
  80.         for (int i = 0; i < camefrom.Count; i++)
  81.         //  for (int i = 0; i < start.costSofar; i++)
  82.         {
  83.             next = camefrom[next];
  84.             // next = next.cameFrom;
  85.             path.Add(next);
  86.             if (next == _start)
  87.             {
  88.                 path.Reverse();
  89.                 break;
  90.             }
  91.         }/*
  92.         for (int i = 0; i < frontier.elementstoremove.Count; i++)
  93.         {
  94.  
  95.             NodeElement node = frontier.elementstoremove[i];
  96.             node.passed = false;
  97.             node.cameFrom = null;
  98.             node.costSofar = 0;
  99.         }*/
  100.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement