Exemplo n.º 1
0
 public void ShouldFindStraightLine()
 {
     var surface = SurfaceFactory.CreateSurface(10, 10);
      var astar = new AStarPathFinder(surface);
      Path path = astar.FindPath(new Point(5, 5), new Point(7, 7));
      Assert.AreEqual(3, path.Count);
 }
Exemplo n.º 2
0
        public void TestAStarLinearPath()
        {
            var world = new TrueCraft.World.World("default", new FlatlandGenerator());
            var astar = new AStarPathFinder();

            var watch = new Stopwatch();

            watch.Start();
            var path = astar.FindPath(world, new BoundingBox(),
                                      new Coordinates3D(0, 4, 0), new Coordinates3D(5, 4, 0));

            watch.Stop();
            DrawGrid(path, world);
            Console.WriteLine(watch.ElapsedMilliseconds + "ms");

            var expected = new[]
            {
                new Coordinates3D(0, 4, 0),
                new Coordinates3D(1, 4, 0),
                new Coordinates3D(2, 4, 0),
                new Coordinates3D(3, 4, 0),
                new Coordinates3D(4, 4, 0),
                new Coordinates3D(5, 4, 0)
            };

            for (var i = 0; i < path.Waypoints.Count; i++)
            {
                Assert.AreEqual(expected[i], path.Waypoints[i]);
            }
        }
Exemplo n.º 3
0
        public void FindPath()
        {
            Stopwatch watch = Stopwatch.StartNew();

            bool[,] boolMap = SearchHelpers.GetSearchBoolMap(theGameStatus.TheMap);
            Point            startLocation        = new Point(100, 100);
            Point            endLocation          = new Point(300, 300);
            SearchParameters tempSearchParameters = new SearchParameters(startLocation, endLocation,
                                                                         boolMap);

            AStarPathFinder tempAStarPathFinder = new AStarPathFinder(tempSearchParameters);
            List <Point>    tempPath            = tempAStarPathFinder.FindPath();

            if (tempPath.Count == 0)
            {
                TheGameCore.RaiseMessage("No path found: " + startLocation + "-" + endLocation);
            }
            else
            {
                tempPath.Insert(0, startLocation);
                Vector3[] tempVect3 = SearchHelpers.PathToVector3Array(theGameStatus.TheMap, tempPath, 0.1f);
                RenderLayerBase.TheSceneManager.TheRenderLayerGame.AddPath(tempVect3);
            }
            watch.Stop();
            TheGameCore.RaiseMessage("FindPath() took " + watch.ElapsedMilliseconds + "ms,: " + startLocation + "-" + endLocation);
        }
Exemplo n.º 4
0
 public void ShouldAvoidPixel()
 {
     var surface = SurfaceFactory.CreateSurface(10, 10);
      var astar = new AStarPathFinder(surface);
      surface.SetPixel(5, 5, Color.Black);
      var path = astar.FindPath(new Point(3, 3), new Point(7, 7));
      Assert.AreEqual(8, path.Count);
 }
Exemplo n.º 5
0
        private static void Main()
        {
            IPathFinder pathFinder = new AStarPathFinder(HeuristicFunc);

            data = GetData1();
            IEnumerable <IConnection> conns =
                pathFinder.FindPath(data.Item1, 0, 5);

            ShowPath(conns);
        }
Exemplo n.º 6
0
    public void StartPathfinding()
    {
        if (_startNode == null || _endNode == null)
        {
            return;
        }
        var path = _pathFinder.FindPath(_startNode, _endNode);

        PathfindingFinished(path);
    }
Exemplo n.º 7
0
 public void ShouldGoAround()
 {
     var surface = SurfaceFactory.CreateSurface(10, 10);
      surface.SetPixel(4, 4, Color.Black);
      surface.SetPixel(5, 4, Color.Black);
      surface.SetPixel(6, 4, Color.Black);
      var astar = new AStarPathFinder(surface);
      Path path = astar.FindPath(new Point(5, 7), new Point(5, 2));
      Assert.AreEqual(9, path.Count);
 }
Exemplo n.º 8
0
 public void ShouldGoAroundWithoutTwisting()
 {
     var surface = SurfaceFactory.CreateSurface(400, 400);
      for (int y = 156; y <= 165; y++)
      {
     surface.SetPixel(174, y, Color.Black);
      }
      var astar = new AStarPathFinder(surface);
      Path path = astar.FindPath(new Point(179, 161), new Point(169, 161));
      Assert.AreEqual(15, path.Count);
 }
Exemplo n.º 9
0
        public void GoesAroundObstacles()
        {
            var map = new MapContext(5, 3);

            map.StartingPoint = new Point(0, 1);
            map.TargetPoint   = new Point(4, 1);
            map.Obstacles.Add(new Point(2, 2));
            map.Obstacles.Add(new Point(2, 1));

            var subject = new AStarPathFinder();
            var path    = subject.FindPath(map);

            Check.That(path.Steps).Not.Contains(new Point(2, 1));
        }
Exemplo n.º 10
0
        // Если осталось мало противников, то может быть сложно их найти. Поэтому они сами нас найдут.
        private IEnumerator UpdateRandomEnemyPath()
        {
            while (true)
            {
                yield return(new WaitForSecondsRealtime(10));

                var activeEnemy = m_Enemies.First(x => x.activeInHierarchy)?.GetComponent <EnemyController>();
                if (activeEnemy != null)
                {
                    activeEnemy.Path = AStarPathFinder.FindPath(
                        activeEnemy.gameObject.transform.position,
                        m_Player.transform.position);
                }
            }
        }
Exemplo n.º 11
0
        public void NoPathReturnsNull()
        {
            var map = new MapContext(5, 3);

            map.StartingPoint = new Point(0, 0);
            map.TargetPoint   = new Point(4, 1);
            map.AddObstacle(0, 1);
            map.AddObstacle(1, 0);
            map.AddObstacle(1, 1);

            var subject = new AStarPathFinder();
            var path    = subject.FindPath(map);

            Check.That(path).IsNull();
        }
Exemplo n.º 12
0
        public static List <Pos> FindPath(this Actor a, Pos dst)
        {
            var path = new List <Pos>();

            PathFinder.KeepInLayer = a.KeepInLayer;
            var pts = PathFinder.FindPath(a.Pos.x, a.Pos.y, dst.x, dst.y);

            Utils.For(pts.Length / 2, (n) =>
            {
                var x = pts[n * 2];
                var y = pts[n * 2 + 1];
                path.Add(new Pos(x, y));
            });

            return(path);
        }
Exemplo n.º 13
0
        public void FindsStraightPath()
        {
            var map = new MapContext(5, 3);

            map.StartingPoint = new Point(0, 1);
            map.TargetPoint   = new Point(4, 1);

            var subject = new AStarPathFinder();
            var path    = subject.FindPath(map);

            Check.That(path.Steps).ContainsExactly(
                new Point(1, 1),
                new Point(2, 1),
                new Point(3, 1),
                new Point(4, 1));
        }
Exemplo n.º 14
0
        public override void Handle(IRemoteClient client, string alias, string[] arguments)
        {
            if (arguments.Length != 1)
            {
                Help(client, alias, arguments);
                return;
            }

            int Id;

            if (!int.TryParse(arguments[0], out Id))
            {
                Help(client, alias, arguments);
                return;
            }

            var manager = client.Server.GetEntityManagerForWorld(client.World);
            var entity  = manager.GetEntityById(Id) as MobEntity;

            if (entity == null)
            {
                client.SendMessage(ChatColor.Red + "An entity with that Id does not exist in this world.");
                return;
            }

            Task.Factory.StartNew(() =>
            {
                var astar = new AStarPathFinder();
                var path  = astar.FindPath(client.World, entity.BoundingBox, (Coordinates3D)entity.Position,
                                           (Coordinates3D)client.Entity.Position);
                if (path == null)
                {
                    client.SendMessage(ChatColor.Red + "It is impossible for this entity to reach you.");
                }
                else
                {
                    client.SendMessage(string.Format(ChatColor.Blue
                                                     + "Executing path with {0} waypoints", path.Waypoints.Count()));
                    entity.CurrentPath = path;
                }
            });
        }
Exemplo n.º 15
0
        public void TestAStarDiagonalPath()
        {
            var world = new TrueCraft.World.World("default", new FlatlandGenerator());
            var astar = new AStarPathFinder();
            var start = new Coordinates3D(0, 4, 0);
            var end   = new Coordinates3D(5, 4, 5);

            var watch = new Stopwatch();

            watch.Start();
            var path = astar.FindPath(world, new BoundingBox(), start, end);

            watch.Stop();
            DrawGrid(path, world);
            Console.WriteLine(watch.ElapsedMilliseconds + "ms");

            // Just test the start and end, the exact results need to be eyeballed
            Assert.AreEqual(start, path.Waypoints[0]);
            Assert.AreEqual(end, path.Waypoints[path.Waypoints.Count - 1]);
        }
Exemplo n.º 16
0
        // ReSharper disable once UnusedParameter.Local
        static void Main(string[] args)
        {
            ConsoleKeyInfo key;

            do
            {
                Console.Clear();
                var map = new MapContext(24, 12);

                //SetUpSimpleTest(map);
                SetUpRandomTest(map);

                var searcher = new AStarPathFinder(diagonalMoves: false);
                var path     = searcher.FindPath(map);

                DrawMap(map, path);

                key = Console.ReadKey(intercept: true);
            } while (key.Key != ConsoleKey.Q);
        }
Exemplo n.º 17
0
        public void Simple()
        {
            var grid = new GridGenerator(false, 3, 2).GenerateGraph();

            foreach (var node in grid.Nodes)
            {
                var coordinates = node.Name.Split(',');
                int x = int.Parse(coordinates[0]);
                int y = int.Parse(coordinates[1]);
                node.UserData[LocationProperty] = new Point(x, y);
            }
            
            foreach (var edge in grid.Edges)
                edge.UserData[DistanceProperty] = 1;

            grid.Edges["0,0", "1,0"].UserData[DistanceProperty]
                = grid.Edges["1,0", "2,0"].UserData[DistanceProperty]
                = 3;

            // Perfect heuristic should result in the best path.
            var finder = new AStarPathFinder(DistanceProperty, DefaultHeuristic);
            var path = finder.FindPath(grid.Nodes["0,0"], grid.Nodes["2,0"]).ToArray();
            Assert.Equal(new[]
            {
                grid.Nodes["0,0"],
                grid.Nodes["0,1"],
                grid.Nodes["1,1"],
                grid.Nodes["2,1"],
                grid.Nodes["2,0"]
            }, path);
            
            // Over estimating the heuristic should result in the more costly path.
            var finder2 = new AStarPathFinder(DistanceProperty, (node, node1) => 5*DefaultHeuristic(node, node1));
            var path2 = finder2.FindPath(grid.Nodes["0,0"], grid.Nodes["2,0"]).ToArray();
            Assert.Equal(new[]
            {
                grid.Nodes["0,0"],
                grid.Nodes["1,0"],
                grid.Nodes["2,0"]
            }, path2);
        }
Exemplo n.º 18
0
        public void TestAStarImpossible()
        {
            var world = new TrueCraft.World.World("default", new FlatlandGenerator());
            var astar = new AStarPathFinder();
            var start = new Coordinates3D(0, 4, 0);
            var end   = new Coordinates3D(5, 4, 0);

            world.SetBlockId(start + Coordinates3D.East, 1);
            world.SetBlockId(start + Coordinates3D.West, 1);
            world.SetBlockId(start + Coordinates3D.North, 1);
            world.SetBlockId(start + Coordinates3D.South, 1);

            var watch = new Stopwatch();

            watch.Start();
            var path = astar.FindPath(world, new BoundingBox(), start, end);

            watch.Stop();
            Console.WriteLine(watch.ElapsedMilliseconds + "ms");

            Assert.IsNull(path);
        }
Exemplo n.º 19
0
        public void TestAStarAvoidRoom()
        {
            var world = new TrueCraft.World.World("default", new FlatlandGenerator());
            var astar = new AStarPathFinder();
            var start = new Coordinates3D(-5, 4, 0);
            var end   = new Coordinates3D(5, 4, 0);

            // North wall
            for (var x = -4; x < 4; x++)
            {
                world.SetBlockId(new Coordinates3D(x, 4, -4), 1);
            }
            // East wall
            for (var z = -4; z < 4; z++)
            {
                world.SetBlockId(new Coordinates3D(3, 4, z), 1);
            }
            // South wall
            for (var x = -4; x < 4; x++)
            {
                world.SetBlockId(new Coordinates3D(x, 4, 4), 1);
            }

            var watch = new Stopwatch();

            watch.Start();
            var path = astar.FindPath(world, new BoundingBox(), start, end);

            watch.Stop();
            DrawGrid(path, world);
            Console.WriteLine(watch.ElapsedMilliseconds + "ms");

            // Just test the start and end, the exact results need to be eyeballed
            Assert.AreEqual(start, path.Waypoints[0]);
            Assert.AreEqual(end, path.Waypoints[path.Waypoints.Count - 1]);
        }
Exemplo n.º 20
0
 void findPath()
 {
     _path = AStarPathFinder.FindPath(_grid, _grid.grid[1, 1], _grid.grid[mazeSize.x - 2, mazeSize.y - 2], false);
 }