Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. void WritePaths()
  2.     {
  3.         if(File.Exists(filepath))
  4.             File.Delete (filepath);
  5.        
  6.         Debug.Log ("writing paths");
  7.        
  8.         nodes = GameObject.FindGameObjectsWithTag("Node");
  9.         if(nodes.Length <= 0)
  10.         {
  11.             Debug.LogWarning ("no nodes");
  12.             return;
  13.         }
  14.         int x = 0;
  15.         for(x = 0; x < nodes.Length; x++)
  16.         {
  17.             list.Add(x.ToString () + " " + nodes[x].transform.position.x.ToString() + " " +         nodes[x].transform.position.y.ToString() + " " + nodes[x].transform.position.z.ToString());
  18.         }
  19.        
  20.         list.Add ("paths");
  21.        
  22.        
  23.         //calculate paths
  24.        
  25.        
  26.         List<Node> openlist = new List<Node>();
  27.         List<Node> closedlist = new List<Node>();
  28.         List<int> temp_path = new List<int>();
  29.         Node current;
  30.         Node next = null;
  31.         bool path_found;
  32.         float lowest_f;
  33.         //string asd;
  34.        
  35.         foreach(Node start in nodelist)
  36.         {
  37.             foreach(Node end in nodelist)
  38.             {
  39.                 if(start != end)
  40.                 {
  41.                     openlist.Clear();
  42.                     closedlist.Clear();
  43.                     temp_path.Clear ();
  44.                     restartValues();
  45.                     start.open = true;
  46.                     openlist.Add (start);
  47.                     current = start;
  48.                     path_found = false;
  49.                     start.g = 0;
  50.                     start.h = Vector3.Distance (nodedict[start.id],nodedict[end.id]);
  51.                     start.f = start.h;
  52.                    
  53.                     while(path_found == false)
  54.                     {
  55.                         Debug.Log (current.id);
  56.                        
  57.                         if(current == end)
  58.                         {
  59.                             path_found = true;
  60.                             Debug.Log ("Path from "+ start.id +" to "+ end.id +":");
  61.                             while(current != start)
  62.                             {
  63.                                 //Debug.Log (current.id);
  64.                                 temp_path.Add (current.id);
  65.                                 current = current.parent;
  66.                             }
  67.                             temp_path.Add (start.id);
  68.                             temp_path.Reverse ();
  69.                             //Debug.Log ("Path from "+ start.id +" to "+ end.id +":");
  70.                             //asd = "";
  71.                             //foreach(int i in temp_path)
  72.                             //{
  73.                                 //asd += i.ToString ();
  74.                                 //asd += " ";
  75.                             //}
  76.                             //Debug.Log (asd);
  77.                             paths[start.id,end.id] = temp_path.ToArray ();
  78.                         }
  79.                         foreach(Node nb in current.neighbours)
  80.                         {
  81.                             if(!nb.closed)
  82.                             {
  83.                                 if(nb.open == false)
  84.                                 {
  85.                                     nb.open = true;
  86.                                     openlist.Add (nb);
  87.                                     nb.h = Vector3.Distance (nodedict[nb.id],nodedict[end.id]);
  88.                                     nb.g = current.g + Vector3.Distance (nodedict[current.id],nodedict[nb.id]);
  89.                                     nb.f = nb.h + nb.g;
  90.                                     nb.parent = current;
  91.                                 }
  92.                                 else
  93.                                 {
  94.                                     if(current.g + Vector3.Distance (nodedict[current.id],nodedict[nb.id]) < nb.g)
  95.                                     {
  96.                                         nb.g = current.g + Vector3.Distance (nodedict[current.id],nodedict[nb.id]);
  97.                                         nb.f = nb.g + nb.h;
  98.                                         nb.parent = current;
  99.                                     }
  100.                                 }
  101.                             }
  102.                         }
  103.                         current.open = false;
  104.                         openlist.Remove (current);
  105.                         current.closed = true;
  106.                         closedlist.Add (current);
  107.                        
  108.                         lowest_f = 100000;
  109.                        
  110.                         if(openlist.Count <= 0)
  111.                         {
  112.                             Debug.Log ("can't find path");
  113.                             return;
  114.                         }
  115.                        
  116.                         foreach(Node potential in openlist)
  117.                         {
  118.                             if(potential.f < lowest_f)
  119.                             {
  120.                                 next = potential;
  121.                                 lowest_f = next.f;
  122.                             }
  123.                         }
  124.                         current = next;
  125.                     }
  126.                 }
  127.             }
  128.         }
  129.        
  130.         //int[,][] paths = new int[3,3][];
  131.         /*paths[0,2] = new int[3] {0,1,2};
  132.         paths[0,1] = new int[3] {0,2,1};
  133.         paths[1,0] = new int[3] {1,2,0};
  134.         paths[1,2] = new int[3] {1,0,2};
  135.         paths[2,1] = new int[3] {2,0,1};
  136.         paths[2,0] = new int[3] {2,1,0};*/
  137.        
  138.    
  139.        
  140.        
  141.         int y,p;
  142.        
  143.         string temp;//, temp2;
  144.        
  145.         for(x = 0; x < nodelist.Count; x++)
  146.         {
  147.             for(y = 0; y < nodelist.Count; y++)
  148.             {
  149.                 if(x != y)
  150.                 {
  151.                     temp = x.ToString()+" "+y.ToString()+" ";
  152.                    
  153.                     for(p = 0; p < paths[x,y].Length; p++)
  154.                     {
  155.                         temp += paths[x,y][p].ToString () + " ";
  156.                     }
  157.                     list.Add (temp);
  158.                 }
  159.                
  160.             }
  161.         }
  162.        
  163.         string[] text = list.ToArray();
  164.         File.WriteAllLines (filepath, text);
  165.        
  166.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement