Exemple #1
0
 //constructor initialises the grid array
 public Level()
 {
     path_count = new int();
     dijkstra = new Dijkstra();
     astar = new AStar();
     scentmap = new Scentmap();
     tiles = new int[gridSize, gridSize];
     for (int i = 0; i < gridSize; i++)
         for (int j = 0; j < gridSize; j++)
             tiles[i,j] = 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;
        }
Exemple #3
0
        public static List <Point> GetPath(List <Area> areas,
                                           List <Section> horizontalSections,
                                           List <Section> verticalSections,
                                           int offset,
                                           int intersectionWeight,
                                           Vertex start,
                                           Vertex end,
                                           int orientation)
        {
            if (start.X == end.X && start.Y == end.Y || offset <= 0)
            {
                return(new List <Point>());
            }

            DoubleToInt(areas,
                        horizontalSections,
                        verticalSections,
                        offset,
                        out List <Rectangle> rectangles,
                        out List <Corridor> horizontalCorridors,
                        out List <Corridor> verticalCorridors);

            List <List <Vertex> > outlines = GetOutlines(rectangles);

            //outlines = CorrectOutlines(outlines, start, end);

            CorrectCorridorsAndGaps(start,
                                    end,
                                    outlines,
                                    horizontalCorridors,
                                    verticalCorridors);

            GetCoordinates(start,
                           end,
                           outlines,
                           horizontalCorridors,
                           verticalCorridors,
                           out HashSet <int> unsortedXs,
                           out HashSet <int> unsortedYs);

            List <int> xs = unsortedXs.ToList();

            xs.Sort();

            List <int> ys = unsortedYs.ToList();

            ys.Sort();

            GetSides(outlines,
                     out Dictionary <int, List <Side> > horizontalSides,
                     out Dictionary <int, List <Side> > verticalSides);

            var graph = new Graph(start,
                                  end,
                                  horizontalSides,
                                  verticalSides,
                                  horizontalCorridors,
                                  verticalCorridors,
                                  xs,
                                  ys,
                                  intersectionWeight,
                                  orientation);

            Node startNode;
            {
                int a = graph.AbscissaRedirects[start.X];
                int o = graph.OrdinateRedirects[start.Y];

                if (a >= 0 && o >= 0)
                {
                    startNode = graph[a, o];
                }
                else
                {
                    startNode = null;
                }
            }

            Node endNode;
            {
                int a = graph.AbscissaRedirects[end.X];
                int o = graph.OrdinateRedirects[end.Y];

                if (a >= 0 && o >= 0)
                {
                    endNode = graph[a, o];
                }
                else
                {
                    endNode = null;
                }
            }

            {
                var points = new List <Point>();
                if (startNode != null && endNode != null)
                {
                    Dijkstra.FindShortPath(graph,
                                           startNode,
                                           endNode,
                                           out List <Node> nodePath,
                                           out List <Edge> edgePath);

                    SmoothPath(nodePath, edgePath, out List <Node> resultNodePath);

                    var vertices = new List <Vertex>();
                    if (resultNodePath != null && resultNodePath.Count > 0)
                    {
                        foreach (Node node in resultNodePath)
                        {
                            int x = xs[node.A];
                            int y = ys[node.O];

                            var vertex = new Vertex(x, y);
                            vertices.Add(vertex);
                        }

                        ClearIntermediateVertices(vertices);

                        foreach (Vertex vertex in vertices)
                        {
                            points.Add(new Point(vertex.X, vertex.Y));
                        }
                    }
                }
                return(points);
            }
        }
Exemple #4
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
        }