Example #1
0
        // Read through lists to find the path.
        public void FindPath(AiBotBase bot, Player plr, Level level)
        {
            if (data.Count == 0)
                return;
            path = new List<Coord2>();
            int lowest = int.MaxValue;

            Coord2 currentPos = new Coord2(-1, -1);
            Coord2 nextPos = new Coord2(bot.GridPosition.X, bot.GridPosition.Y);

            while (Math.Abs(plr.GridPosition.X - currentPos.X) > 1 || Math.Abs(plr.GridPosition.Y - currentPos.Y) > 1)//(new Coord2(plr.GridPosition.X, plr.GridPosition.Y-1) != currentPos)
            {
                currentPos = nextPos;
                if (data[(plr.GridPosition.X) * level.GridSizeX + plr.GridPosition.Y].ElementAt((currentPos.X) * level.GridSizeY + (currentPos.Y)) != 0 ||
                data[(plr.GridPosition.X) * level.GridSizeX + plr.GridPosition.Y].ElementAt((currentPos.X) * level.GridSizeY + (currentPos.Y)) < int.MaxValue)
                {
                    for (int i = -1; i <= 1; i++)
                        for (int j = -1; j <= 1; j++)
                            if (level.ValidPosition(new Coord2(currentPos.X + i, currentPos.Y + j)))
                                if (data[plr.GridPosition.X * 40 + plr.GridPosition.Y].ElementAt((currentPos.X + i) * 40 + (currentPos.Y + j)) < lowest)
                                    //if (i == 0 || j == 0)
                                        if (new Coord2(currentPos.X + i, currentPos.Y + j) != new Coord2(currentPos.X, currentPos.Y))
                                        {
                                            lowest = data[plr.GridPosition.X * 40 + plr.GridPosition.Y].ElementAt((currentPos.X + i) * 40 + (currentPos.Y + j));
                                            nextPos = new Coord2(currentPos.X + i, currentPos.Y + j);
                                        }
                }

                if (lowest == int.MaxValue)
                    break;

                path.Add(nextPos);

            }
        }
Example #2
0
        protected override void ChooseNextGridLocation(Level level, Player plr)
        {
            //your code will go here.
            Coord2 position;
            position = GridPosition;

            if(plr.GridPosition.X < position.X)//if player position X less than bot position X move back on the X axis
            {
                //position.X -= 1;
            }
            if (plr.GridPosition.X > position.X)//if player position X greater then bot position X move forward on the X axis.
            {
                //position.X += 1;
            }
            if (plr.GridPosition.Y < position.Y)//if player position Y less than bot position Y move down on the Y axis.
            {
                //position.Y -= 1;
            }
            if (plr.GridPosition.Y > position.Y)//if player position Y greater than bot position Y move up on the Y axis.
            {
                //position.Y += 1;
            }
            SetNextGridPosition(position, level);//sets position of bot.
            level.ValidPosition(position);//checks for walls.
        }
Example #3
0
 protected override void ChooseNextGridLocation(Level level, Player plr)
 {
     bool ok = false;
     Coord2 pos;// = new Coord2();
     while (!ok)
     {
         pos = GridPosition;
         int x = rnd.Next(0,4);
         switch (x)
         {
             case (0):
                 pos.X += 1;
                 break;
             case (1):
                 pos.X -= 1;
                 break;
             case (2):
                 pos.Y += 1;
                 break;
             case (3):
                 pos.Y -= 1;
                 break;
         }
         ok = SetNextGridPosition(pos, level);
     }
 }
        protected override void ChooseNextGridLocation(Level level, Player plr)
        {
            double currentDistance = GetDistance(plr.GridPosition, GridPosition);

            Coord2 testPosition = new Coord2(GridPosition.X, GridPosition.Y);

            for (int i = -1; i <= 1; i++)
            {
                for (int j = -1; j <= 1; j++)
                {
                    if (i == 0 || j == 0)
                    {
                        testPosition = new Coord2(GridPosition.X + i, GridPosition.Y + j);
                        if (currentDistance > GetDistance(plr.GridPosition, testPosition))
                        {
                            currentDistance = GetDistance(plr.GridPosition, testPosition);
                            SetNextGridPosition(testPosition, level);
                            currentDirection = testPosition - GridPosition;
                        }
                    }
                }
            }

            if (!level.ValidPosition(GridPosition + currentDirection))
            {
                currentDirection = new Coord2(currentDirection.Y, currentDirection.X);
                SetNextGridPosition(GridPosition + currentDirection, level);
            }
        }
Example #5
0
 //Handles animation moving from current grid position (gridLocation) to next grid position (targetLocation)
 //When target location is reached, sets grid location to targetLocation, and then calls ChooseNextGridLocation
 //and resets animation timer
 public void Update(GameTime gameTime, Level level, Player plr)
 {
     timerMs -= gameTime.ElapsedGameTime.Milliseconds;
     if (timerMs <= 0)
     {
         gridPosition = targetPosition;
         ChooseNextGridLocation(level, plr);
         timerMs = moveTime;
     }
     //calculate screen position
     screenPosition = (gridPosition * 15) + ((((targetPosition * 15) - (gridPosition * 15)) * (moveTime - timerMs)) / moveTime);
 }
Example #6
0
        protected override void ChooseNextGridLocation(Level level, Player plr)
        {
            Coord2 position;
            position = GridPosition;

            if (plr.GridPosition.X < position.X)
            {
                position.X -= 1;
            }
            if (plr.GridPosition.X > position.X)
            {
                position.X += 1;
            }
            if (plr.GridPosition.Y < position.Y)
            {
                position.Y -= 1;
            }
            if (plr.GridPosition.Y > position.Y)
            {
                position.Y += 1;
            }

            if (!level.ValidPosition(position))
            {
                if (plr.GridPosition.Y < position.Y)
                {
                    position = GridPosition;
                    position.Y -= 1;
                    SetNextGridPosition(position, level);
                }
                if (plr.GridPosition.Y > position.Y)
                {
                    position = GridPosition;
                    position.Y += 1;
                    SetNextGridPosition(position, level);
                }
                if (plr.GridPosition.X > position.X)
                {
                    position = GridPosition;
                    position.X -= 1;
                    SetNextGridPosition(position, level);
                }
                if (plr.GridPosition.X < position.X)
                {
                    position = GridPosition;
                    position.X += 1;
                    SetNextGridPosition(position, level);
                }
            }
            SetNextGridPosition(position, level);//sets position of bot.
        }
Example #7
0
        protected override void ChooseNextGridLocation(Level level, Player plr)
        {              
            Coord2 CurrentPos;
            if(level.build_astar == true)
            {
                CurrentPos = GridPosition;

                //sets new position for the bot.
                if (GridPosition != plr.GridPosition)
                {
                    CurrentPos = level.astar.astar_path[index];
                }
                SetNextGridPosition(CurrentPos, level);
                index++;
            }
        }
        protected override void ChooseNextGridLocation(Map level, Player plr)
        {
            try
            {
                if (level.pathfinder.GetAlgorithm() != PathfinderAlgorithm.ScentMap)
                    throw new Exception("Wrong bot used for pathfinding algorithm.");

                // Get a copy of the scent map
                ScentMap map = (level.pathfinder as ScentMap);

                // Get neighbouring coordinates
                Coord2[] neighbours = GetNeighbours();

                Coord2 highest = new Coord2(0, 0);
                bool validFound = false;
                for (int i = 0; i < neighbours.Length; i++) // Ensures a valid position is initially selected
                {
                    if (level.ValidPosition(neighbours[i]))
                    {
                        highest = neighbours[i];
                        validFound = true;
                        break;
                    }
                }

                if (validFound) // Only update if at least one neighbouring positon is in fact valid on the map
                {
                    // Find the highest neighbouring coorindate
                    for (int i = 0; i < neighbours.Length; i++)
                    {
                        if (level.ValidPosition(neighbours[i]))
                        {
                            if (map.Scents.data[neighbours[i].X, neighbours[i].Y] > map.Scents.data[highest.X, highest.Y])
                                highest = neighbours[i];
                        }
                    }

                    SetNextGridPosition(highest, level);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Bot Exception: " + e.Message);
            }
        }
Example #9
0
        // Create a text file by flooding Dijkstra's for every cell.
        public void Calculate(Level level)
        {
            int firstTile = level.tiles[0][0];
            level.tiles[0][0] = 1;
            Dijkstra dijkstra = new Dijkstra(level);
            AiBotSimple bot = new AiBotSimple(0,0);
            Player player = new Player(level.GridSizeX-1, level.GridSizeY-1);
            Coord2 current = new Coord2(-1, -1);
            dijkstra.Build(level, bot, player);
            StreamWriter writer = new StreamWriter("precompute.txt", true);

            for (int x = 0; x < level.GridSize; x++)
            {
                for (int y = 0; y < level.GridSize; y++)
                {
                    for (int i = 0; i < level.GridSize; i++)
                    {
                        for (int j = 0; j < level.GridSize; j++)
                        {
                            if (new Coord2(i, j) == player.GridPosition)
                                writer.Write(0);
                            else if (level.ValidPosition(new Coord2(i, j)) && dijkstra.cost[i,j].ToString() != "2.147484E+09")
                                writer.Write(dijkstra.cost[i, j].ToString());
                            else
                                writer.Write("-");

                            string tab = "\t";
                            writer.Write(tab);
                        }
                    }

                    writer.WriteLine();
                    bot = new AiBotSimple(x, y);
                    player = new Player(0, 0);
                    dijkstra = new Dijkstra(level);
                    dijkstra.Build(level, bot, player);
                }
            }

            writer.Close();
            level.tiles[0][0] = firstTile;
        }
        // Read base class's input class and follow through.
        protected override void ChooseNextGridLocation(Level level, Player plr)
        {
            if (path == null)
            {
                if (dijkstra != null)
                    path = dijkstra.path;
                else if (aStar != null)
                    path = aStar.path;
                else if (level.precompute != null)
                {

                    path = level.precompute.path;
                    path.Reverse();
                }
            }
            else if (path.Count > 0)
            {
                SetNextGridPosition(path[path.Count-1], level);
                path.RemoveAt(path.Count - 1);
            }
        }
Example #11
0
        protected override void ChooseNextGridLocation(Map level, Player plr)
        {
            //Coord2[] neighbours = GetNeighbours();

            //int cVal = 0;
            //Coord2 cPos = new Coord2(0, 0);
            //for (int i = 0; i < neighbours.Length; i++)
            //{
            //    if(level.ValidPosition(neighbours[i]))
            //    {
            //        int lVal = level.scentMap.Buffer1.data[neighbours[i].X, neighbours[i].Y];
            //        if (lVal > cVal)
            //        {
            //            cVal = lVal;
            //            cPos = new Coord2(neighbours[i].X, neighbours[i].Y);
            //        }
            //    }
            //}

            //SetNextGridPosition(cPos, level);
        }
        //this function is filled in by a derived class: must use SetNextGridLocation to actually move the bot
        protected override void ChooseNextGridLocation(Map level, Player plr)
        {
            try
            {
                if (level.pathfinder.GetAlgorithm() != PathfinderAlgorithm.AStar && level.pathfinder.GetAlgorithm() != PathfinderAlgorithm.Dijkstra)
                    throw new Exception("Wrong bot used for pathfinding algorithm.");

                List<Coord2> path = level.pathfinder.GetPath();
                if (path.Count > 0 && currentPathIndex != path.Count - 1)
                {
                    SetNextGridPosition(path[currentPathIndex], level);
                    currentPathIndex++;
                }
                else
                    currentPathIndex = 0;
            }
            catch(Exception e)
            {
                Console.WriteLine("Bot Exception: " + e.Message);
            }
        }
Example #13
0
        // Follow the highest adjacent scent value.
        protected override void ChooseNextGridLocation(Level level, Player plr)
        {
            if (GridPosition == plr.GridPosition)
                return;
            Coord2 next = new Coord2(-1, -1);
            int highest = 0;
            for (int i = -1; i <= 1; i++)
            {
                for (int j = -1; j <= 1; j++)
                {
                    if (level.ValidPosition(new Coord2(GridPosition.X+i, GridPosition.Y+j)) &&
                        plr.GetScent(GridPosition.X + i, GridPosition.Y + j) > highest && (i == 0 || j == 0))
                    {
                        if (i == 0 && j == 0)
                            continue;
                        highest = plr.GetScent(GridPosition.X + i, GridPosition.Y + j);
                        next = new Coord2(GridPosition.X + i, GridPosition.Y + j);
                    }
                }
            }

            SetNextGridPosition(next, level);
        }
Example #14
0
        protected override void ChooseNextGridLocation(Level level, Player plr)
        {
            Coord2 CurrentPos;
            CurrentPos = GridPosition;

            //checks bot surrounding nodes.
            if(GridPosition != plr.GridPosition)
            {
                for (int f = -1; f <= 1; f++)
                {
                    for (int g = -1; g <= 1; g++)
                    {
                        //changes bot position.
                        if (level.scentmap.buffer1[GridPosition.X + f, GridPosition.Y + g] > level.scentmap.buffer1[GridPosition.X, GridPosition.Y])
                        {
                            CurrentPos = new Coord2(GridPosition.X + f, GridPosition.Y + g);
                            level.path_count ++;
                        }
                        SetNextGridPosition(CurrentPos, level);
                    }
                }
            }
        }
Example #15
0
        public void Update(Level level, Player plr)
        {
            try
            {
                sourceValue++;
            }
            catch
            {
                throw new Exception("A billion years passed and this somehow overflowed.");
            }

            // Make buffers identical.
            Array.Copy(bufferOne, bufferTwo, bufferTwo.Length);
            bufferOne[plr.GridPosition.X, plr.GridPosition.Y] = sourceValue;
            bufferTwo[plr.GridPosition.X, plr.GridPosition.Y] = sourceValue;

            // Iterate through cells, using one buffer for comparison and another for updating.
            for (int x = 0; x < level.GridSize; x++)
            {
                for (int y = 0; y < level.GridSize; y++)
                {
                    for (int i = -1; i <= 1; i++)
                    {
                        for (int j = -1; j <= 1; j++)
                        {
                            if (level.ValidPosition(new Coord2(x + i, y + j))
                                && (level.ValidPosition(new Coord2(x+i, y)) && level.ValidPosition(new Coord2(x, y+j))))
                            {
                                if ((bufferOne[x, y] < bufferTwo[x + i, y + j]))
                                    bufferOne[x, y] = bufferTwo[x+i,y+j] - 1;
                            }
                        }
                    }
                }
            }
        }
Example #16
0
 //this function is filled in by a derived class: must use SetNextGridLocation to actually move the bot
 protected abstract void ChooseNextGridLocation(Level level, Player plr);
Example #17
0
 protected override void ChooseNextGridLocation(Level level, Player plr)
 {
 }
Example #18
0
        public Game1()
        {
            //program settings.
            Window.Title = "GAL11266257_CGP3001M_Assignment 2";
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferHeight = BackBufferHeight;
            graphics.PreferredBackBufferWidth = BackBufferWidth;          
            Content.RootDirectory = "../../../Content";
            TargetElapsedTime = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / TargetFrameRate);
           
            //loads map.
            level = new Level();
            level.Loadmap("../../../Content/0.txt");

            //loads player.
            player = new Player(125, 67);          
        }
Example #19
0
        //generates path.
        public void Build(Level level, AiBotBase bot, Player plr)
        {
            //starts timing the algorithm.
            var watch = Stopwatch.StartNew();

            astar_is_built = true;
            astar_built = true;

            //fill nodes with default value.
            for (int i = 0; i < map_size; i++)
            {
                for (int j = 0; j < map_size; j++)
                {
                    closed[i, j] = false;
                    cost[i, j] = 999999;
                    link[i, j] = new Coord2(-1, -1);
                    inPath[i, j] = false;
                }
            }
            open_locations.Add(bot.GridPosition);

            Coord2 lowest_location = new Coord2(-1, -1);
            float lowest_cost = 999999;
            float newcost = 999999;

            closed[bot.GridPosition.X, bot.GridPosition.Y] = false;
            cost[bot.GridPosition.X, bot.GridPosition.Y] = 0;

            //heuristic calculation
            for (int i = 0; i < map_size; i++)
            {
                for (int j = 0; j < map_size; j++)
                {
                    if (level.manhatten == true)
                    {
                        heuristic[i, j] = Math.Abs(i - plr.GridPosition.X) + Math.Abs(j - plr.GridPosition.Y);//manhatten.                       
                    }
                    else if (level.diagonal == true)
                    {
                        heuristic[i, j] = Math.Max(Math.Abs(i - plr.GridPosition.X), Math.Abs(j - plr.GridPosition.Y));//diagonal.
                    }
                    else if (level.euclidian == true)
                    {
                        heuristic[i, j] = (int)Math.Sqrt(Math.Pow(i - plr.GridPosition.X, 2) + Math.Pow(j - plr.GridPosition.Y, 2));//euclidian.
                    }
                    else
                    {
                        heuristic[i, j] = Math.Abs(i - plr.GridPosition.X) + Math.Abs(j - plr.GridPosition.Y);//manhatten is the default heuristic.
                    }
                }
            }
            while (!closed[plr.GridPosition.X, plr.GridPosition.Y])
            {
                lowest_cost = 999999;

                //unoptomised astar.
                if (level.astar_unoptomised_chosen == true)
                {
                    for (int x = 0; x < map_size; x++)
                    {
                        for (int y = 0; y < map_size; y++)
                        {
                            if (cost[x, y] + heuristic[x, y] < lowest_cost && !closed[x, y])
                            {
                                lowest_cost = cost[x, y] + heuristic[x, y];
                                lowest_location = new Coord2(x, y);
                            }
                        }
                    }
                }
                else
                {
                    //optomised astar, only checks open locations.
                    for (int x = 0; x < open_locations.Count(); x++)
                    {
                        if ((cost[open_locations[x].X, open_locations[x].Y] + heuristic[open_locations[x].X, open_locations[x].Y]) < lowest_cost && !closed[open_locations[x].X, open_locations[x].Y])
                        {
                            lowest_cost = cost[open_locations[x].X, open_locations[x].Y] + heuristic[open_locations[x].X, open_locations[x].Y];
                            lowest_location = new Coord2(open_locations[x].X, open_locations[x].Y);
                        }
                    }
                }
                closed[lowest_location.X, lowest_location.Y] = true;
                open_locations.Remove(lowest_location);

                //counts nodes closed.
                if (closed[lowest_location.X, lowest_location.Y] == true)
                {
                    isClosed ++;
                }

                for (int f = -1; f <= 1; f++)
                {
                    for (int g = -1; g <= 1; g++)
                    {
                        //assigns a cost to each location surrounding the current path node.
                        if (level.ValidPosition(new Coord2(lowest_location.X + f, lowest_location.Y + g)))
                        {
                            newcost = 999999;

                            Coord2 neighbour_location;
                            neighbour_location = new Coord2(lowest_location.X + f, lowest_location.Y + g);
                          
                            if (f != 0 ^ g != 0)
                            {
                                //horizontal cost.
                                newcost = 1f;
                            }                            
                            else if (f != 0 && g != 0)
                            {
                                //diagonal cost.
                                newcost = 1.4f;
                            }
                            if (!open_locations.Contains(neighbour_location) && neighbour_location != bot.GridPosition)
                            {
                                open_locations.Add(neighbour_location);
                            }

                            if (newcost < cost[lowest_location.X + f, lowest_location.Y + g] && level.ValidPosition(new Coord2(lowest_location.X + f, lowest_location.Y + g)) && !closed[lowest_location.X + f, lowest_location.Y + g])
                            {
                                cost[lowest_location.X + f, lowest_location.Y + g] = newcost;//assigns current cost to the new cost.
                                link[lowest_location.X + f, lowest_location.Y + g] = new Coord2(lowest_location.X, lowest_location.Y);//assigns current link to lowest location.
                            }
                        }
                    }
                }
            }

            bool done = false;
            Coord2 nextclosed = plr.GridPosition;
            while (done == false)
            {
                //adds next step to path.
                astar_path.Add(nextclosed);
                inPath[nextclosed.X, nextclosed.Y] = true;
                nextclosed = link[nextclosed.X, nextclosed.Y];
                if (nextclosed == bot.GridPosition)
                {
                    astar_path.Add(bot.GridPosition);//adds path to the bot.
                    done = true;
                }
            }
            astar_path.Reverse();//reverse path for reading.
            watch.Stop();

            elapsedMS = watch.ElapsedMilliseconds;
        }
Example #20
0
 public Level(Map map, Player plr, AiBotBase bot)
 {
     this.map = map;
     this.player = plr;
     this.bot = bot;
 }
Example #21
0
 protected override void ChooseNextGridLocation(Map level, Player plr)
 {
     SetNextGridPosition(gridPosition, level);
 }
Example #22
0
        // Generate the path.
        public void Build(Level level, AiBotBase bot, Player plr)
        {
            cost[bot.GridPosition.X, bot.GridPosition.Y] = 0;
            Coord2 currentPosition = bot.GridPosition;
            closed[bot.GridPosition.X, bot.GridPosition.Y] = true;
            while (!closed[plr.GridPosition.X, plr.GridPosition.Y])
            {
                // Open adjacent cells.
                for (int i = -1; i <= 1; i++)
                {
                    for (int j = -1; j <= 1; j++)
                    {
                        if (level.ValidPosition(new Coord2(currentPosition.X + i, currentPosition.Y + j)) && !closed[currentPosition.X + i, currentPosition.Y + j]
                            && (i != 0 || j != 0) && (currentPosition.X + i < level.GridSize && currentPosition.Y + j < level.GridSize && currentPosition.X + i >= 0 && currentPosition.Y + j >= 0)
                            && (level.ValidPosition(new Coord2(currentPosition.X + i, currentPosition.Y)) && level.ValidPosition(new Coord2(currentPosition.X, currentPosition.Y + j))))
                        {
                            if (i == 0 || j == 0)
                                SetCost(currentPosition, level, i, j, false);
                            else
                                SetCost(currentPosition, level, i, j, true);
                        }
                    }
                }

                float lowestCost = int.MaxValue;
                Coord2 nextPosition = currentPosition;

                // Iterate through the list to check open cells for valid movements.
                for (int i = 0; i < level.GridSize; i++)
                {
                    for (int j = 0; j < level.GridSize; j++)
                    {
                        if (!closed[i, j] && level.ValidPosition(new Coord2(i, j)) && cost[i, j] < lowestCost)
                        {
                            lowestCost = cost[i, j];
                            nextPosition = new Coord2(i, j);
                        }
                    }
                }
                if (currentPosition == nextPosition)
                    break;
                closed[nextPosition.X, nextPosition.Y] = true;
                currentPosition = nextPosition;
            }

            bool done = false;
            Coord2 nextClose = plr.GridPosition;
            while (!done)
            {
                if (nextClose.X == -1)
                    break;
                if (nextClose == bot.GridPosition)
                {
                    playerPos = plr.GridPosition;
                    done = true;
                }
                inPath[nextClose.X, nextClose.Y] = true;
                path.Add(new Coord2(nextClose.X, nextClose.Y));
                nextClose = link[nextClose.X, nextClose.Y];

            }
        }
Example #23
0
        public Game1()
        {
            //constructor
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferHeight = BackBufferHeight;
            graphics.PreferredBackBufferWidth = BackBufferWidth;
            Window.Title = "Pathfinder";
            Content.RootDirectory = "Content";
            this.IsMouseVisible = true;
            this.IsFixedTimeStep = false;
            elapsedTime = 0;
            frameCounter = 0;
            FPS = 60;
            mouseState = Mouse.GetState();
            lastMouseState = mouseState;
            mouseClickPos = new Coord2(-1, -1);
            scrollOffset = Vector2.Zero;
            textColour = Color.BlueViolet;
            //set frame rate
            TargetElapsedTime = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / TargetFrameRate);
            //load level map
            level = new Level();
            string mapName = mapNumber.ToString();
            //mapName = "eighty";
            level.Loadmap("Content/" + mapName + ".txt");
            //instantiate bot and player objects

            BackBufferWidth = level.gridSquareSize * level.GridSizeX;
            BackBufferHeight = level.GridSizeY * level.GridSquareSize;
            graphics.PreferredBackBufferHeight = BackBufferHeight;
            graphics.PreferredBackBufferWidth = BackBufferWidth;
            graphics.ApplyChanges();
            graphics.GraphicsDevice.Reset();

            SetCharacters();
            //bots = new List<AiBotBase>();

            #if ASTARTEST
            string heuristic = "Euclidean";
            StreamWriter r = new StreamWriter("astarTest-" + mapName + "-" + heuristic + ".txt");
            r.WriteLine("A Star Test values with " + heuristic);
            r.WriteLine("Map size: {0}x{1}", level.GridSizeX, level.GridSizeY);
            r.WriteLine("Times per build, in ms");

            AStar aStar = new AStar(level);
            aStar = new AStar(level);

            Stopwatch timer = new Stopwatch();

            Coord2 start = new Coord2(1, 1);
            Coord2 end = new Coord2(level.GridSizeX-1, level.GridSizeY-1);
            for (int i = 0; i < 1000; i++)
            {

                timer.Restart();
                aStar.Build(level, start, end, false, heuristic);
                timer.Stop();
                r.WriteLine("{0:N3}", timer.Elapsed.TotalMilliseconds);
            }
            r.Close();

            #endif

            #if DIJKSTRASTEST
            StreamWriter r = new StreamWriter("dijkstrasTest-" + mapName + ".txt");

            string mapDescription = "";
            r.WriteLine("Dijkstra's Test values");
            r.WriteLine("Map size: {0}x{1}", level.GridSizeX, level.GridSizeY);
            r.WriteLine(mapDescription);
            r.WriteLine("Times per build, in ms");

            Dijkstra dijkstras = new Dijkstra(level);
            dijkstras = new Dijkstra(level);

            Stopwatch timer = new Stopwatch();
            AiBotAlgorithm bot = new AiBotAlgorithm(1, 1);
            Player player = new Player(level.GridSizeX - 1, level.GridSizeY - 1);
            for (int i = 0; i < 1000; i++)
            {

                timer.Restart();
                dijkstras.Build(level, bot, player);
                timer.Stop();
                r.WriteLine("{0:N3}", timer.Elapsed.TotalMilliseconds);
            }
            r.Close();
            #endif

            #if PRECOMPUTETEST
            StreamWriter r = new StreamWriter("precomputeTest-" + mapName + ".txt");

            string mapDescription = "";
            r.WriteLine("Precompute's Test values");
            r.WriteLine("Map size: {0}x{1}", level.GridSizeX, level.GridSizeY);
            r.WriteLine(mapDescription);
            r.WriteLine("Times per build, in ms");

            Precompute precompute = new Precompute(mapName);
            precompute = new Precompute(mapName);

            Stopwatch timer = new Stopwatch();
            AiBotAlgorithm bot = new AiBotAlgorithm(1, 1);
            Player player = new Player(level.GridSizeX - 1, level.GridSizeY - 1);
            for (int i = 0; i < 1; i++)
            {

                timer.Restart();
                precompute.Calculate(level);
                timer.Stop();
                r.WriteLine("{0:N3}", timer.Elapsed.TotalMilliseconds);
            }
            r.Close();
            #endif
        }
Example #24
0
        private void SetCharacters(int bX = 1, int bY = 1, int pX = 10, int pY = 10)
        {
            bots = new List<AiBotBase>();
            //bots[0] = new AiBotStill(bX, bY);
            bots.Add(new AiBotStill(bX, bY));
            bots[0].dijkstra = null;
            bots[0].aStar = null;

            player = new Player(pX, pY);
            methodText = "None";
        }
Example #25
0
        //generates path.
        public void Build(Level level, AiBotBase bot, Player plr)
        {
            //starts timing the algorithm.
            var watch = Stopwatch.StartNew();

            dijkstra_is_built = true;
            dijkstra_built = true;

            //fill nodes with default value.
            for (int i = 0; i < map_size; i++)
            {
                for (int j = 0; j < map_size; j++)
                {
                    closed[i, j] = false;
                    cost[i, j] = float.MaxValue;
                    link[i, j] = new Coord2(-1, -1);
                    inPath[i, j] = false;
                }
            }

            Coord2 lowest_location = new Coord2 (-1, -1);
            float lowest_cost = float.MaxValue;

            closed[bot.GridPosition.X, bot.GridPosition.Y] = false;
            cost[bot.GridPosition.X, bot.GridPosition.Y] = 0;

            while (!closed[plr.GridPosition.X, plr.GridPosition.Y])
            {
                lowest_cost = float.MaxValue;
                for (int x = 0; x < map_size; x++)
                {
                    for (int y = 0; y < map_size; y++)
                    {
                        //sets new lowest cost.
                        if(cost[x,y] <= lowest_cost && !closed[x, y])
                        {
                            lowest_cost = cost[x, y];
                            lowest_location = new Coord2(x, y);                   
                        }
                    }
                }
                //closes that node.
                closed[lowest_location.X, lowest_location.Y] = true;

                //counts nodes closed.
                if(closed[lowest_location.X, lowest_location.Y] == true)
                {
                    isClosed ++;
                }

                for (int f = -1; f <= 1; f++)
                {
                    for (int g = -1; g <= 1; g++)
                    {
                        //assigns a cost to each location surrounding the current path node.
                        if(level.ValidPosition(new Coord2(lowest_location.X + f, lowest_location.Y + g)))
                        {
                            float newcost = float.MaxValue;

                            if(f != 0 ^ g != 0)
                            {
                                //horizontal cost.
                                newcost = lowest_cost + 1f;
                            }
                            else if (f != 0 && g != 0)
                            {
                                //diagonal cost.
                                newcost = lowest_cost + 1.4f;
                            }
                            if(newcost < cost[lowest_location.X + f, lowest_location.Y + g] && level.ValidPosition(new Coord2(lowest_location.X + f, lowest_location.Y + g)))
                            {
                                cost[lowest_location.X + f, lowest_location.Y + g] = newcost;//assigns current cost to the new cost.
                                link[lowest_location.X + f, lowest_location.Y + g] = new Coord2(lowest_location.X, lowest_location.Y);//assigns current link to lowest location.
                            }
                        }
                    }
                }
            }

            bool done = false;
            nextclosed = plr.GridPosition;
            while (done == false)
            {
                //adds next step to path.
                dijkstra_path.Add(nextclosed);
                inPath[nextclosed.X, nextclosed.Y] = true;
                nextclosed = link[nextclosed.X, nextclosed.Y];
                if (nextclosed == bot.GridPosition)
                {
                    dijkstra_path.Add(bot.GridPosition);//adds path to the bot.
                    done = true;
                }                   
            }
            dijkstra_path.Reverse();//reverse path for reading.

            elapsedMS = watch.ElapsedMilliseconds;
        }