示例#1
0
    private void createPath(Ground_Tile end, char color)
    {
        //Debug.Log("path found");
        //Debug.Log("end tile location =" + end.tileY + "" + end.tileX);
        Ground_Tile curentTile = end;

        /*if (color == 'W')
         * {
         *      //whiteMoves.Push(curentTile.Tile);
         *      whiteMoves.Push(GameObject.FindGameObjectWithTag("Tile_" + curentTile.tileY + "" + curentTile.tileX));
         * }
         * else
         * {
         *      //blackMoves.Push(curentTile.Tile);
         *      blackMoves.Push(GameObject.FindGameObjectWithTag("Tile_" + curentTile.tileY + "" + curentTile.tileX));
         * }*/


        while (curentTile.parent != null)
        {
            //Debug.Log("previose tile location in path =" + curentTile.tileY + "" + curentTile.tileX);
            if (color == 'W')
            {
                //whiteMoves.Push(curentTile.Tile);
                whiteMoves.Push(GameObject.FindGameObjectWithTag("Tile_" + curentTile.tileY + "" + curentTile.tileX));
            }
            else
            {
                //blackMoves.Push(curentTile.Tile);
                blackMoves.Push(GameObject.FindGameObjectWithTag("Tile_" + curentTile.tileY + "" + curentTile.tileX));
            }
            curentTile = curentTile.parent;
        }
    }
    public A_Star_Controler(Vector3 startLocaiton, Vector3 endLocaiton)
    {
        //Debug.Log("A* constructer with passed \n startLocaiton ="+ startLocaiton+ "\n endLocation = "+ endLocaiton);
        //setting the start and end points on the baord
        Board.setStartEndPoints(startLocaiton, endLocaiton);

        //adding the start tile to the oppen list and setting it as the curent Tile
        curentTile    = Board.worldToTile(startLocaiton);
        curentTileObj = Board.board[curentTile[0], curentTile[1]];
        oppenList.Insert(curentTileObj);

        //looping thrue untill hitting the end point
        while (oppenList.Count > 0)
        {
            //setting the curent tile to the next one in the oppen list
            curentTileObj = oppenList.ExtractMin();
            curentTile[0] = curentTileObj.tileX;
            curentTile[1] = curentTileObj.tileZ;

            //cheking to see if you are at the end
            if (curentTileObj.end)
            {
                createPath(curentTileObj);
                break;
            }

            //else creating the tiles adjacent to curent tile
            createNeighbors();
            //adding curent tile to closed list
            //Debug.Log("adding Ground_Tile at Board Position \n" + curentTile[0] + " "+ curentTile[1]+  " \n To closed list");
            closedList.Add(curentTile[0] * Board.board.GetLength(1) + curentTile[1], curentTileObj);
        }
    }
示例#3
0
    public void Insert(T o)
    {
        data.Add(o);

        int i = data.Count - 1;

        while (i > 0)
        {
            int j = (i + 1) / 2 - 1;

            // Check if the invariant holds for the element in data[i]
            Ground_Tile v = data[j];
            if (v.CompareTo(data[i]) < 0 || v.CompareTo(data[i]) == 0)
            {
                break;
            }

            // Swap the elements
            Ground_Tile tmp = data[i];
            data[i] = data[j];
            data[j] = tmp;

            i = j;
        }
    }
示例#4
0
    private void MinHeapify(int i)
    {
        int smallest;
        int l = 2 * (i + 1) - 1;
        int r = 2 * (i + 1) - 1 + 1;

        if (l < data.Count && (data[l].CompareTo(data[i]) < 0))
        {
            smallest = l;
        }
        else
        {
            smallest = i;
        }

        if (r < data.Count && (data[r].CompareTo(data[smallest]) < 0))
        {
            smallest = r;
        }

        if (smallest != i)
        {
            Ground_Tile tmp = data[i];
            data[i]        = data[smallest];
            data[smallest] = tmp;
            this.MinHeapify(smallest);
        }
    }
示例#5
0
    private void chekNaborsViablity(Ground_Tile lookTile, int[] lookLocation)
    {
        int newG = moveCost;

        //cheking to see if the tile is pathable or if it is in the closed list
        if (lookTile.pathable == true && !closedList.ContainsKey(lookLocation[0] * Board.board.GetLength(1) + lookLocation[1]))
        {
            //Debug.Log("neighbor "+ i +" "+j +" is pathable and not in closed list");
            //setting its G equal to the cost of its position from the curent point added to the curent points G
            newG += curentTileObj.G;
            //cheking if the new tile is in the oppen list
            if (oppenList.contains(lookTile))
            {
                //Debug.Log("" + lookTile.tileY + " " + lookTile.tileX + " is in open list");
                //check to see if its g is smaller or greater then the curent one
                if (lookTile.G < oppenList.Peek(lookTile).G)
                {
                    int heapIndexOfLook = oppenList.PeekL(lookTile);
                    //change the G value and Parent
                    oppenList.changeValuesAt(heapIndexOfLook, newG, curentTileObj);
                }
            }
            else
            {
                //Debug.Log(i + " " + j + " is new");
                //setting the tiles values and puting it in the oppen list
                lookTile.updateValues(newG);
                lookTile.parent = curentTileObj;
                oppenList.Insert(lookTile);
            }
        }
    }
示例#6
0
 public bool contains(Ground_Tile altered)
 {
     for (int i = 0; i < data.Count; i++)
     {
         if (altered.equals(data[i]))
         {
             return(true);
         }
     }
     return(false);
 }
示例#7
0
 public int PeekL(Ground_Tile looking)
 {
     for (int i = 0; i < data.Count; i++)
     {
         if (looking.equals(data[i]))
         {
             return(i);
         }
     }
     return(0);
 }
示例#8
0
 public Ground_Tile Peek(Ground_Tile looking)
 {
     for (int i = 0; i < data.Count; i++)
     {
         if (looking.equals(data[i]))
         {
             return(data[i]);
         }
     }
     return(data[0]);
 }
示例#9
0
 // Use this for initialization
 public Tile_Board()
 {
     //create the tiles in there correct locations
     for (int Y = 0; Y < numTileRow; Y++)
     {
         for (int X = 0; X < numbTileColumn; X++)
         {
             //creating a Gound_Tile out of the tile at the correct matrix locaiton
             board[X, Y] = new Ground_Tile(GameObject.FindGameObjectWithTag("Tile_" + X + "" + Y));
         }
     }
 }
示例#10
0
    public Ground_Tile ExtractMin()
    {
        if (data.Count < 0)
        {
            Debug.Log("MinHeap is empty");
        }

        Ground_Tile min = data[0];

        data[0] = data[data.Count - 1];
        data.RemoveAt(data.Count - 1);
        this.MinHeapify(0);
        return(min);
    }
示例#11
0
    public int CompareTo(Ground_Tile other)
    {
        int result = 1;

        if (this.F == other.F)
        {
            result = 0;
        }
        else if (this.F < other.F)
        {
            result = -1;
        }
        return(result);
    }
    private void createPath(Ground_Tile end)
    {
        Debug.Log("path found");
        Debug.Log("end tile location =" + end.tileX + "" + end.tileZ);
        Ground_Tile curentTile = end;

        while (curentTile.parent != null)
        {
            Debug.Log("previose tile location in path =" + curentTile.tileX + "" + curentTile.tileZ);
            movesToMake.Push(curentTile.tileLocation);
            curentTile = curentTile.parent;
        }
        if (curentTile.start)
        {
            Debug.Log("start tile location =" + curentTile.tileX + "" + curentTile.tileZ);
            movesToMake.Push(curentTile.tileLocation);
        }
    }
示例#13
0
    // Use this for initialization
    void Start()
    {
        //getting the board hight and with
        Mesh mesh = GetComponent <MeshFilter>().mesh;

        boardHight = mesh.bounds.size.x * transform.lossyScale.x;
        boardWidth = mesh.bounds.size.z * transform.lossyScale.z;

        //getting the size the tiles will be
        tileHight = boardHight / numbTileColumn;
        tileWidth = boardWidth / numTileRow;

        //determoning the offfset to get the cordonent in the center of the tile from the left top edge
        offsetX = tileHight / 2;
        offsetZ = tileWidth / 2;

        //the world cordonents of the first tile
        sTileX = ((boardHight / 2) * -1) + offsetX + transform.position.x;
        sTileZ = ((boardWidth / 2) * -1) + offsetZ + transform.position.z;

        //the world cordonents of the curent tile
        float tileX;
        float tileZ;

        //create the tiles in there correct locations
        for (int x = 0; x < numTileRow; x++)
        {
            //shifting curent tile down by the Highth of the tiles
            tileX = sTileX + (tileHight * x);
            for (int z = 0; z < numbTileColumn; z++)
            {
                //shifting curent tile location right by the length of the tiles
                tileZ = sTileZ + (tileHight * z);
                //creating a tile
                board[x, z] = new Ground_Tile(new Vector3(tileX, transform.position.y + boardYOffset, tileZ), x, z);
                //Instantiate(Resources.Load("Obstacle"), new Vector3(tileX, transform.position.y + boardYOffset, tileZ), new Quaternion(0, 0, 0, 1));
            }
        }

        //create empty board of tile objects
    }
示例#14
0
    public void AStarFromStart(int startX, int startY)
    {
        //creating the oppen and closed list
        oppenList  = new MinHeap <Ground_Tile>();
        closedList = new Dictionary <int, Ground_Tile> ();

        //adding the start tile to the oppen list and setting it as the curent Tile
        oppenList.Insert(Board.board[startY, startX]);
        //oppenList.Insert(curentTileObj);

        int numTilesLookedAt = 0;

        //looping thrue untill hitting the end point
        while (oppenList.Count > 0)
        {
            //setting the curent tile to the next one in the oppen list
            curentTileObj = oppenList.ExtractMin();
            numTilesLookedAt++;
            //Debug.Log ("Tile curently located = " + curentTileObj.tileY + "" + curentTileObj.tileX);
            // Debug.Log("Tile is pathable = "+ curentTileObj.pathable);
            //curentTileL[0] = curentTileObj.tileY;
            //curentTileL[1] = curentTileObj.tileX;
            //Debug.Log ("Number of tiles looked at = " + numTilesLookedAt);

            //cheking to see if you are at the end
            if (curentTileObj.end)
            {
                break;
            }

            //else creating the tiles adjacent to curent tile
            createNeighbors();
            //adding curent tile to closed list
            //Debug.Log("adding Ground_Tile at Board Position \n" + curentTileObj.tileY + " "+ curentTileObj.tileX+  " \n To closed list");
            closedList.Add(curentTileObj.tileY * Board.board.GetLength(1) + curentTileObj.tileX, curentTileObj);
        }
    }
示例#15
0
 public void changeValuesAt(int i, int g, Ground_Tile newParent)
 {
     data[i].updateValues(g);
     data[i].parent = newParent;
     MinHeapify(i);
 }
示例#16
0
 public bool equals(Ground_Tile other)
 {
     return(this.tileLocation.Equals(other.tileLocation));
 }
示例#17
0
 public bool equals(Ground_Tile other)
 {
     return(this.Tile.tag.Equals(other.Tile.tag));
 }
    private void createNeighbors()
    {
        int startI = curentTile[0] - 1;
        int maxI   = curentTile[0] + 1;
        int startJ = curentTile[1] - 1;
        int maxJ   = curentTile[1] + 1;

        //cheking to see if you are at the edge of the board array
        if (curentTile[0] == 0)
        {
            startI += 1;
        }
        else if (curentTile[0] == Board.board.GetLength(1))
        {
            maxI -= 1;
        }
        if (curentTile[1] == 0)
        {
            startJ += 1;
        }
        else if (curentTile[1] == Board.board.GetLength(0))
        {
            maxJ -= 1;
        }

        //Debug.Log("Creating Neibors for tile " + curentTile[0] + " " + curentTile[1]);
        //looping thru adjacent tiles
        for (int i = startI; i <= maxI; i++)
        {
            //Debug.Log("maxI = " + maxI);
            //Debug.Log("curent i = " + i);
            for (int j = startJ; j <= maxJ; j++)
            {
                //skipping over curent tile
                if (i == curentTile[0] && j == curentTile[1])
                {
                    j++;
                }
                //Debug.Log("maxJ = " + maxJ);
                //Debug.Log("curent j = " + j);
                int[] lookLocation = new int[2];
                lookLocation[0] = i;
                lookLocation[1] = j;
                int         newG     = strMoveCost;
                Ground_Tile lookTile = Board.board[i, j];
                //setting the move increace to diagonal or streate values
                if (i != curentTile[0] && j != curentTile[1])
                {
                    newG = diagMoveCost;
                }

                //Debug.Log("neighbor " + i + " " + j + " is pathable ="+ lookTile.pathable);
                //Debug.Log("neighbor " + i + " " + j + " is in closed list =" + closedList.ContainsKey(lookLocation[0] + "" + lookLocation[1]));
                //cheking to see if the tile is pathable or if it is in the closed list
                if (lookTile.pathable == true && !closedList.ContainsKey(lookLocation[0] * Board.board.GetLength(1) + lookLocation[1]))
                {
                    //Debug.Log("neighbor "+ i +" "+j +" is pathable and not in closed list");
                    //setting its G equal to the cost of its position from the curent point added to the curent points G
                    newG += curentTileObj.G;
                    //cheking if the new tile is in the oppen list
                    if (oppenList.contains(lookTile))
                    {
                        //Debug.Log(i + " " + j + " is in oppen list");
                        //check to see if its g is smaller or greater then the curent one
                        if (lookTile.G < oppenList.Peek(lookTile).G)
                        {
                            int heapIndexOfLook = oppenList.PeekL(lookTile);
                            //change the G value and Parent
                            oppenList.changeValuesAt(heapIndexOfLook, newG, curentTileObj);
                        }
                    }
                    else
                    {
                        //Debug.Log(i + " " + j + " is new");
                        //setting the tiles values and puting it in the oppen list
                        lookTile.updateValues(newG);
                        lookTile.parent = curentTileObj;
                        oppenList.Insert(lookTile);
                    }
                }
            }
        }
    }