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); }
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> Djikstras(Vector3 start, Vector3 end, VoxelChunk vc) { Stack <Vector3> waypoint = new Stack <Vector3>(); Dictionary <Vector3, int> d = new Dictionary <Vector3, int>(); Dictionary <Vector3, Vector3> p = new Dictionary <Vector3, Vector3>(); bool found = false; int newDist = 0; Vector3 current = new Vector3(0, 0, 0); d[start] = 0; List <Vector3> l = new List <Vector3>(); for (int x = 0; x < vc.terrainArray.GetLength(0); x++) { for (int y = 1; y < vc.terrainArray.GetLength(1); y++) { for (int z = 0; z < vc.terrainArray.GetLength(2); z++) { Vector3 n = new Vector3(x, y, z); if (vc.IsTraversable(n)) { l.Add(n); } } } } while (l.Count > 0 && !found) { int smallestDistance = int.MaxValue; foreach (Vector3 n in l) { if (d.ContainsKey(n)) { if (d[n] < smallestDistance) { smallestDistance = d[n]; current = n; } } } 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 (l.Contains(n)) { if (vc.terrainArray[(int)n.x, (int)n.y - 1, (int)n.z] == 3) { newDist = 1; } if (vc.terrainArray[(int)n.x, (int)n.y - 1, (int)n.z] == 2) { newDist = 3; } if (!d.ContainsKey(n) || newDist < d[n]) { d[n] = newDist; p[n] = current; } } } } else { found = true; } } if (found) { while (current != start) { waypoint.Push(current + offset); current = p[current]; } waypoint.Push(current + offset); } return(waypoint); }