public char[] Pathfind(int dest_x, int dest_y) { int[,] paths = new int[grid.GetXSize(), grid.GetYSize()]; Queue <GridSquare> queue = new Queue <GridSquare>(); paths[(int)x_pos, (int)z_pos] = 1; queue.Enqueue(curr_pos); int x = (int)x_pos; int y = (int)z_pos; bool found = false; int step = 0; while (!found) { GridSquare curr_square = queue.Dequeue(); curr_square.GetPos(ref x, ref y); step = paths[x, y]; bool no_moves = true; //Debug.Log(x + ", " + y + ", " + paths[x, y]); y += 1; if (y >= 0 && y < 20 && Move('n', true, x, y)) { if (paths[x, y] == 0) { no_moves = false; paths[x, y] = step + 1; queue.Enqueue(grid.squares[x, y]); if (x == dest_x && y == dest_y) { found = true; continue; } } } y -= 1; y -= 1; if (y >= 0 && y < 20 && Move('s', true, x, y)) { if (paths[x, y] == 0) { no_moves = false; paths[x, y] = step + 1; queue.Enqueue(grid.squares[x, y]); if (x == dest_x && y == dest_y) { found = true; continue; } } } y += 1; x -= 1; if (x >= 0 && x < 20 && Move('w', true, x, y)) { if (paths[x, y] == 0) { no_moves = false; paths[x, y] = step + 1; queue.Enqueue(grid.squares[x, y]); if (x == dest_x && y == dest_y) { found = true; continue; } } } x += 1; x += 1; if (x >= 0 && x < 20 && Move('e', true, x, y)) { if (paths[x, y] == 0) { no_moves = false; paths[x, y] = step + 1; queue.Enqueue(grid.squares[x, y]); if (x == dest_x && y == dest_y) { found = true; continue; } } } x -= 1; if (no_moves && queue.Count == 0) { return(null); } } /*for (int i = 5; i >= 0; i--) { * Debug.Log(paths[0, i] + ", " + paths[1, i] + ", " + paths[2, i] + ", " + paths[3, i] + ", " + paths[4, i] + ", " + paths[5, i]); * }*/ char[] directions = new char[paths[dest_x, dest_y]]; //Debug.Log(paths[dest_x, dest_y]); step = paths[dest_x, dest_y]; x = dest_x; y = dest_y; int min = 10000000; char direction = '_'; while (x != (int)x_pos || y != (int)z_pos) { if (x - 1 >= 0 && min >= paths[x - 1, y] && paths[x - 1, y] != 0) { min = paths[x - 1, y]; direction = 'e'; } if (x + 1 < 20 && min >= paths[x + 1, y] && paths[x + 1, y] != 0) { min = paths[x + 1, y]; direction = 'w'; } if (y - 1 >= 0 && min >= paths[x, y - 1] && paths[x, y - 1] != 0) { min = paths[x, y - 1]; direction = 'n'; } if (y + 1 < 20 && min >= paths[x, y + 1] && paths[x, y + 1] != 0) { min = paths[x, y + 1]; direction = 's'; } step--; directions[step] = direction; if (direction == 'e') { x--; } else if (direction == 'w') { x++; } else if (direction == 'n') { y--; } else if (direction == 's') { y++; } //Debug.Log("(" + x + ", " + y + ")" + ", " + direction); } return(directions); }