Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void WritePaths()
- {
- if(File.Exists(filepath))
- File.Delete (filepath);
- Debug.Log ("writing paths");
- nodes = GameObject.FindGameObjectsWithTag("Node");
- if(nodes.Length <= 0)
- {
- Debug.LogWarning ("no nodes");
- return;
- }
- int x = 0;
- for(x = 0; x < nodes.Length; x++)
- {
- list.Add(x.ToString () + " " + nodes[x].transform.position.x.ToString() + " " + nodes[x].transform.position.y.ToString() + " " + nodes[x].transform.position.z.ToString());
- }
- list.Add ("paths");
- //calculate paths
- List<Node> openlist = new List<Node>();
- List<Node> closedlist = new List<Node>();
- List<int> temp_path = new List<int>();
- Node current;
- Node next = null;
- bool path_found;
- float lowest_f;
- //string asd;
- foreach(Node start in nodelist)
- {
- foreach(Node end in nodelist)
- {
- if(start != end)
- {
- openlist.Clear();
- closedlist.Clear();
- temp_path.Clear ();
- restartValues();
- start.open = true;
- openlist.Add (start);
- current = start;
- path_found = false;
- start.g = 0;
- start.h = Vector3.Distance (nodedict[start.id],nodedict[end.id]);
- start.f = start.h;
- while(path_found == false)
- {
- Debug.Log (current.id);
- if(current == end)
- {
- path_found = true;
- Debug.Log ("Path from "+ start.id +" to "+ end.id +":");
- while(current != start)
- {
- //Debug.Log (current.id);
- temp_path.Add (current.id);
- current = current.parent;
- }
- temp_path.Add (start.id);
- temp_path.Reverse ();
- //Debug.Log ("Path from "+ start.id +" to "+ end.id +":");
- //asd = "";
- //foreach(int i in temp_path)
- //{
- //asd += i.ToString ();
- //asd += " ";
- //}
- //Debug.Log (asd);
- paths[start.id,end.id] = temp_path.ToArray ();
- }
- foreach(Node nb in current.neighbours)
- {
- if(!nb.closed)
- {
- if(nb.open == false)
- {
- nb.open = true;
- openlist.Add (nb);
- nb.h = Vector3.Distance (nodedict[nb.id],nodedict[end.id]);
- nb.g = current.g + Vector3.Distance (nodedict[current.id],nodedict[nb.id]);
- nb.f = nb.h + nb.g;
- nb.parent = current;
- }
- else
- {
- if(current.g + Vector3.Distance (nodedict[current.id],nodedict[nb.id]) < nb.g)
- {
- nb.g = current.g + Vector3.Distance (nodedict[current.id],nodedict[nb.id]);
- nb.f = nb.g + nb.h;
- nb.parent = current;
- }
- }
- }
- }
- current.open = false;
- openlist.Remove (current);
- current.closed = true;
- closedlist.Add (current);
- lowest_f = 100000;
- if(openlist.Count <= 0)
- {
- Debug.Log ("can't find path");
- return;
- }
- foreach(Node potential in openlist)
- {
- if(potential.f < lowest_f)
- {
- next = potential;
- lowest_f = next.f;
- }
- }
- current = next;
- }
- }
- }
- }
- //int[,][] paths = new int[3,3][];
- /*paths[0,2] = new int[3] {0,1,2};
- paths[0,1] = new int[3] {0,2,1};
- paths[1,0] = new int[3] {1,2,0};
- paths[1,2] = new int[3] {1,0,2};
- paths[2,1] = new int[3] {2,0,1};
- paths[2,0] = new int[3] {2,1,0};*/
- int y,p;
- string temp;//, temp2;
- for(x = 0; x < nodelist.Count; x++)
- {
- for(y = 0; y < nodelist.Count; y++)
- {
- if(x != y)
- {
- temp = x.ToString()+" "+y.ToString()+" ";
- for(p = 0; p < paths[x,y].Length; p++)
- {
- temp += paths[x,y][p].ToString () + " ";
- }
- list.Add (temp);
- }
- }
- }
- string[] text = list.ToArray();
- File.WriteAllLines (filepath, text);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement