Stack <Vector3> BreadthFirstSearch(Vector3 start, Vector3 end, VoxelChunk vc) { Stack <Vector3> waypoints = new Stack <Vector3>(); Dictionary <Vector3, Vector3> visitedParent = new Dictionary <Vector3, Vector3>(); Queue <Vector3> q = new Queue <Vector3>(); bool found = false; Vector3 current = start; q.Enqueue(start); while (q.Count > 0 && !found) { current = q.Dequeue(); if (current != end) { List <Vector3> neighbourList = new List <Vector3>(); neighbourList.Add(current + new Vector3(1, 0, 0)); neighbourList.Add(current + new Vector3(-1, 0, 0)); neighbourList.Add(current + new Vector3(0, 0, 1)); neighbourList.Add(current + new Vector3(0, 0, -1)); foreach (Vector3 n in neighbourList) { if (n.x >= 0 && n.x < vc.GetChunkSize() && n.z >= 0 && n.z < vc.GetChunkSize()) { if (vc.isTraversable(n)) { if (!visitedParent.ContainsKey(n)) { visitedParent[n] = current; q.Enqueue(n); } } } } } else { found = true; } } if (found) { while (current != start) { waypoints.Push(current + offset); current = visitedParent[current]; } waypoints.Push(start + offset); } return(waypoints); }
//dijkstra method that allow finding the shortest path depending on the cost of the blocks******************************************************* Stack <Vector3> Dijkstra(Vector3 start, Vector3 end, VoxelChunk vc) { Stack <Vector3> waypoints = new Stack <Vector3> (); Dictionary <Vector3, int> distance = new Dictionary <Vector3, int> (); Dictionary <Vector3, Vector3> visitedParent = new Dictionary <Vector3, Vector3> (); bool found = false; Stack <Vector3> q = new Stack <Vector3> (); List <Vector3> l = new List <Vector3>(); int newDist = 0; int length = 0; int tempHolder; //Vector3 tempDKHolder; start += new Vector3(0, -1, 0); end += new Vector3(0, -1, 0); Vector3 current = start; foreach (Vector3 n in vc.GetTerrainList()) { distance[n] = int.MaxValue; l.Add(n); //Debug.Log (n); } distance [start] = 0; while (l.Count > 0 && !found) { //set tempHolder to inf* tempHolder = int.MaxValue; //loop through each of the items in the list to find the smallest value foreach (Vector3 v in l) { //if the item is in the distance dictionary if (distance.ContainsKey(v)) { //the tempholder will be always greater than the first value because its inf //but after it will equal to the first value //which will campare to each value of the list to find the smallest value again if (tempHolder > distance[v]) { tempHolder = distance[v]; //once it found the smaller value, set the current equal to that vector current = v; } } } //once the smallest value confirmed, remove the item from the list l.Remove(current); if (current != end) { //our adjacent nodes are x+1, x-1, z+1 and z-1 List <Vector3> neighbourList = new List <Vector3>(); neighbourList.Add(current + new Vector3(1, 0, 0)); //x+1 neighbourList.Add(current + new Vector3(-1, 0, 0)); //x-1 neighbourList.Add(current + new Vector3(0, 0, 1)); //z+1 neighbourList.Add(current + new Vector3(0, 0, -1)); //z-1 foreach (Vector3 n in neighbourList) { if (l.Contains(n)) { //check if n is within range if ((n.x >= 0 && n.x < vc.GetChunkSize()) && n.z >= 0 && n.z < vc.GetChunkSize()) { //check if we can traverse over this if (vc.IsTraversableForDijkstra(n)) { if (vc.isStone(n)) { length = 1; } else { length = 3; } newDist = distance[current] + length; if (newDist < distance[n] || !distance.ContainsKey(n)) { // //check if node is alread processed // if(!visitedParent.ContainsKey(n)) // { distance[n] = newDist; visitedParent[n] = current; // } } } } } } } else { found = true; } } if (found) { while (current != start) { //put the current location into the waypoint waypoints.Push(current + offset + new Vector3(0, 1, 0)); //find the parent of the current and replace the current location current = visitedParent[current]; } waypoints.Push(start + offset + new Vector3(0, 1, 0)); } //Debug.Log (waypoints.Count); return(waypoints); }
Stack <Vector3> DepthFirstSearch (Vector3 start, Vector3 end, VoxelChunk vc) { //stack not usually used with BFS //now we use this to build a waypoint list by traver through parents Stack <Vector3> waypoints = new Stack <Vector3> (); Dictionary <Vector3, Vector3> visitedParent = new Dictionary <Vector3, Vector3> (); Stack <Vector3> q = new Stack <Vector3> (); bool found = false; Vector3 current = start; q.Push(start); while (q.Count > 0 && !found) { current = q.Pop(); if (current != end) { //our adjacent nodes are x+1, x-1, z+1 and z-1 List <Vector3> neighbourList = new List <Vector3>(); neighbourList.Add(current + new Vector3(1, 0, 0)); //x+1 neighbourList.Add(current + new Vector3(-1, 0, 0)); //x-1 neighbourList.Add(current + new Vector3(0, 0, 1)); //z+1 neighbourList.Add(current + new Vector3(0, 0, -1)); //z-1 foreach (Vector3 n in neighbourList) { //check if n is within range if ((n.x >= 0 && n.x < vc.GetChunkSize()) && n.z >= 0 && n.z < vc.GetChunkSize()) { //check if we can traverse over this if (vc.IsTraversable(n)) { //check if node is alread processed if (!visitedParent.ContainsKey(n)) { visitedParent[n] = current; q.Push(n); } } } } } else { found = true; } } if (found) { while (current != start) { //put the current location into the waypoint waypoints.Push(current + offset); //find the parent of the current and replace the current location current = visitedParent[current]; } waypoints.Push(start + offset); } //Debug.Log (waypoints.Count); return(waypoints); }
Stack <Vector3> Dijkstra(Vector3 start, Vector3 end, VoxelChunk vc) { Stack <Vector3> waypoints = new Stack <Vector3>(); List <Vector3> l = new List <Vector3>(); disDictionary = new Dictionary <Vector3, int>(); Dictionary <Vector3, Vector3> visitedParent = new Dictionary <Vector3, Vector3>(); bool found = false; Vector3 current = start; int newDist; disDictionary[start] = 0; if (vc.isTraversable(current)) { l.Add(current); } while (l.Count > 0 && !found) { int minDistance = disDictionary[l[0]]; current = l[0]; for (int i = 0; i < l.Count; i++) { if (minDistance > disDictionary[l[i]]) { current = l[i]; minDistance = vc.GetDistanceCost(l[i]); } } l.Remove(current); if (current != end) { List <Vector3> neighbourList = new List <Vector3>(); neighbourList.Add(current + new Vector3(1, 0, 0)); neighbourList.Add(current + new Vector3(-1, 0, 0)); neighbourList.Add(current + new Vector3(0, 0, 1)); neighbourList.Add(current + new Vector3(0, 0, -1)); foreach (Vector3 n in neighbourList) { if (n.x >= 0 && n.x < vc.GetChunkSize() && n.z >= 0 && n.z < vc.GetChunkSize() && vc.isTraversable(n)) { newDist = disDictionary[current] + vc.GetDistanceCost(n); if (!visitedParent.ContainsKey(n) || newDist < disDictionary[n]) { disDictionary[n] = newDist; visitedParent[n] = current; l.Add(n); } } } } else { found = true; } } if (found) { Debug.Log("The path cost is: " + disDictionary[current]); while (current != start) { waypoints.Push(current + offset); current = visitedParent[current]; } waypoints.Push(start + offset); } return(waypoints); }