Example #1
0
        private Tile iterateAStar(Tile start, Tile goal)
        {
            if(DEBUG)
                Console.WriteLine("Called iterateAStar!, goal is at (" + goal.getMapX() + "," + goal.getMapY() + ")");

            Tile ret = null;
            List<Tile> bestpath = new List<Tile>();
            List<Tile> closed = new List<Tile>();
            start.addToTotalCost(Math.Abs(start.getMapX() - goal.getMapX()) + Math.Abs(start.getMapY() - goal.getMapY()));
            List<Tile> open = new List<Tile>();
            open.Add(start);
            while (open.Count > 0)
            {
                //get the lowest cost node...
                Tile cur = open.Min();
                if(DEBUG)
                    Console.WriteLine("Min retrieved a node with " + cur.getTotalCost() + " from the open list");

                if (cur.getMapX() == goal.getMapX() && cur.getMapY() == goal.getMapY())
                {
                    if(DEBUG)
                        Console.WriteLine("Found goal! cur is (" + cur.getMapX() + "," + cur.getMapY() + ")");

                    return cur;
                    //return addToBestPath(bestpath, cur, last);
                }
                else
                {
                    //Tile newlast = addToBestPath(bestpath, cur, last);
                    int curx = cur.getMapX();
                    int cury = cur.getMapY();
                    //last = newlast;
                    open.Remove(cur);
                    closed.Add(cur);
                    Tile up = map.getTileAt(curx, cury - 1);
                    Tile down = map.getTileAt(curx, cury + 1);
                    Tile left = map.getTileAt(curx - 1, cury);
                    Tile right = map.getTileAt(curx + 1, cury);

                    if (canAdd(up, open, closed))
                        addTile(up, cur, open, goal);

                    if (canAdd(down, open, closed))
                        addTile(down, cur, open, goal);

                    if (canAdd(left, open, closed))
                        addTile(left, cur, open, goal);

                    if (canAdd(right, open, closed))
                        addTile(right, cur, open, goal);
                }

            }
            if(DEBUG)
                Console.WriteLine("Failed to find a path!");

            return ret;
        }
Example #2
0
        private Tile addToBestPath(List<Tile> bestpath, Tile toadd, Tile prev)
        {
            int x = toadd.getX();
            int y = toadd.getY();
            int mapx = toadd.getMapX();
            int mapy = toadd.getMapY();
            int len = toadd.getLength();

            if(DEBUG)
            Console.WriteLine("Adding tile at (" + mapx + "," + mapy + ") to bestpath! Cost is " + toadd.getTotalCost());

            Tile newadd = new Tile(mapx, mapy, x, y, len, astarwaypoint, Color.White);
            newadd.setPrevious(prev);
            bestpath.Add(newadd);
            return newadd;
        }
Example #3
0
 public void setPlayerLocation(Tile newloc)
 {
     if (playertile != null)
     {
         playertile.setMapX(newloc.getMapX());
         playertile.setMapY(newloc.getMapY());
         playertile.setX(newloc.getX());
         playertile.setY(newloc.getY());
     }
     else
         Console.WriteLine("Player is null!!");
 }
Example #4
0
 private void addTile(Tile toadd, Tile prev, List<Tile> open, Tile goal)
 {
     toadd.setPrevious(prev);
     int distance = getCumulativeCost(toadd);
     toadd.addToTotalCost(distance + Math.Abs(toadd.getMapX() - goal.getMapX()) + Math.Abs(toadd.getMapY() - goal.getMapY()));
         if(DEBUG)
         Console.WriteLine("Adding node with cost " + toadd.getTotalCost() + " to open list");
     open.Add(toadd);
 }
Example #5
0
 public void setMonsterLocation(Tile newloc)
 {
     if (monstertile != null)
     {
         monstertile.setMapX(newloc.getMapX());
         monstertile.setMapY(newloc.getMapY());
         monstertile.setX(newloc.getX());
         monstertile.setY(newloc.getY());
     }
     else
         Console.WriteLine("Monster is null!!");
 }