Advertisement
DCSquid

Pathfinding

Mar 25th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System;
  4.  
  5.  
  6.  
  7. public class PathFinding{
  8.  
  9.     public static List<Node> reconstruct_path(Dictionary<Node, Node> cameFrom, Node current){
  10.         List<Node> total_path=new List<Node>();
  11.         total_path.Add(current);
  12.         while (cameFrom.ContainsKey(current)){
  13.             current=cameFrom[current];
  14.             total_path.Add(current);
  15.         }
  16.         total_path.Reverse();
  17.         return total_path;
  18.     }
  19.  
  20.  
  21.     public static List<Node> Astar(Node start, Node goal, Func<Node,Node,double> costEstimate){
  22.         // The set of nodes already evaluated
  23.         List<Node> closedSet=new List<Node>();
  24.         // The set of currently discovered nodes still to be evaluated.
  25.         // Initially, only the start node is known.
  26.         List<Node> openSet=new List<Node>();
  27.         openSet.Add(start);
  28.         // For each node, which node it can most efficiently be reached from.
  29.         // If a node can be reached from many nodes, cameFrom will eventually contain the
  30.         // most efficient previous step.
  31.         Dictionary<Node, Node> cameFrom = new Dictionary<Node,Node>();
  32.         // For each node, the cost of getting from the start node to that node.
  33.         Dictionary<Node, Double> gScore=new Dictionary<Node, double>();
  34.         double DefaultScore=Double.PositiveInfinity;
  35.         gScore[start]=0;
  36.         // For each node, the total cost of getting from the start node to the goal
  37.         // by passing by that node. That value is partly known, partly heuristic.
  38.         Dictionary<Node, double> fScore=new Dictionary<Node, double>();
  39.         //for the first node, that value is completely heuristic
  40.         fScore[start]=costEstimate(start,goal);
  41.         while (openSet.Count!=0){
  42.             //the current node is the one in openSet with the lowest fScore value
  43.             Node current=start;
  44.             Double minScore=DefaultScore;
  45.             foreach (Node n in openSet){
  46.                 Double fValue=DefaultScore;
  47.                 fScore.TryGetValue(n,out fValue);
  48.                 if (fValue<minScore){
  49.                     current=n;
  50.                     minScore=fValue;
  51.                 }
  52.             }
  53.             if (current==goal){
  54.                 List<Node> path=reconstruct_path(cameFrom,current);
  55.                 return path;
  56.             }
  57.             openSet.Remove(current);
  58.             closedSet.Add(current);
  59.             foreach (Node neighbour in current.neighbours()){
  60.                 if(neighbour==null){
  61.                     continue;
  62.                 }
  63.                 if (closedSet.Contains(neighbour)){
  64.                     continue;
  65.                     // Ignore the neighbor which is already evaluated.
  66.                 }
  67.                 // The distance from start to a neighbor
  68.                 double tentative_gScore = gScore[current] + current.weight(neighbour);
  69.                 if (!openSet.Contains(neighbour)){
  70.                     openSet.Add(neighbour);
  71.                 }else if (tentative_gScore >= gScore[neighbour]){
  72.                     continue;       // This is not a better path.
  73.                 }
  74.                 // This path is the best until now. Record it!
  75.                 cameFrom[neighbour] = current;
  76.                 gScore[neighbour] = tentative_gScore;
  77.                 fScore[neighbour] = gScore[neighbour] + costEstimate(neighbour, goal);
  78.             }
  79.         }
  80.         //failure
  81.         return new List<Node>();
  82.     }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement