Beispiel #1
0
        public List <Tile> FindPath(Entity source, Entity target, double distance)
        {
            try
            {
                //new EmptyBlockedProvider(), // Instance of: IBockedProvider

                var navigator = new TileNavigator(
                    new LevelNavigator(source, source.Level, distance, _blockCache),
                    new BlockDiagonalNeighborProvider(source.Level, (int)source.KnownPosition.Y, _blockCache), // Instance of: INeighborProvider
                    new BlockPythagorasAlgorithm(_blockCache),                                                 // Instance of: IDistanceAlgorithm
                    new ManhattanHeuristicAlgorithm()                                                          // Instance of: IDistanceAlgorithm
                    );

                BlockCoordinates targetPos = (BlockCoordinates)target.KnownPosition;
                BlockCoordinates sourcePos = (BlockCoordinates)source.KnownPosition;
                var from = new Tile(sourcePos.X, sourcePos.Z);
                var to   = new Tile(targetPos.X, targetPos.Z);

                return(navigator.Navigate(from, to)?.ToList() ?? new List <Tile>());
            }
            catch (Exception e)
            {
                Log.Error("Navigate", e);
            }

            return(new List <Tile>());
        }
Beispiel #2
0
        public Path FindPath(Entity source, BlockCoordinates target, double distance)
        {
            try
            {
                var blockAccess = new CachedBlockAccess(source.Level);

                var navigator = new TileNavigator(
                    new LevelNavigator(source, blockAccess, distance, _blockCache),
                    new BlockDiagonalNeighborProvider(blockAccess, (int)Math.Truncate(source.KnownPosition.Y), _blockCache, source), // Instance of: INeighborProvider
                    new BlockDistanceAlgorithm(_blockCache),                                                                         // Instance of: IDistanceAlgorithm
                    new ManhattanHeuristicAlgorithm()                                                                                // Instance of: IDistanceAlgorithm
                    );

                BlockCoordinates targetPos = target;
                BlockCoordinates sourcePos = (BlockCoordinates)source.KnownPosition;
                var from = new Tile(sourcePos.X, sourcePos.Z);
                var to   = new Tile(targetPos.X, targetPos.Z);

                var path = navigator.Navigate(from, to, 200)?.ToList() ?? new List <Tile>();

                //Log.Debug($"{source.GetType()} finding path within {distance} blocks. Did {blockAccess.NumberOfBlockGet} block-gets");

                return(new Path(_blockCache)
                {
                    Current = path
                });
            }
            catch (Exception e)
            {
                Log.Error("Navigate", e);
            }

            return(new Path());
        }
Beispiel #3
0
        public void TestPathFinder()
        {
            var navigator = new TileNavigator(
                new EmptyBlockedProvider(),                // Instance of: IBockedProvider
                new DiagonalNeighborProvider(),            // Instance of: INeighborProvider
                new PythagorasAlgorithm(),                 // Instance of: IDistanceAlgorithm
                new ManhattanHeuristicAlgorithm()          // Instance of: IDistanceAlgorithm
                );

            var from = new Tile(-100.5, -102.5);
            var to   = new Tile(120.5, 122.5);

            navigator.Navigate(from, to);
        }
Beispiel #4
0
        public IActionResult Move([FromBody] NetSnake.Model.Request request)
        {
            // Hitta närmsta mat
            // Undvik ormar
            GameBoard = new GameBoard(request.you, request.board);
            GameBoard.Update(request.you, request.board);

            var           head      = request.you.body.First();
            var           heuristic = new ManhattanHeuristicAlgorithm();
            TileNavigator navigator = new TileNavigator(
                GameBoard,
                GameBoard,
                new PythagorasAlgorithm(),
                heuristic);

            int dist = Int32.MaxValue;
            //Tile? foodTile = null;
            //bool foodFound = false;
            var sortedFood = new List <SortableFood>();

            foreach (var food in request.board.food)
            {
                var newDist    = heuristic.Calculate(ToTile(head), ToTile(food));
                var pathToFood = navigator.Navigate(ToTile(head), ToTile(food)) ?? new List <Tile>();

                sortedFood.Add(new SortableFood {
                    Food       = ToTile(food),
                    Distance   = (int)newDist,
                    Neighbours = GameBoard.GetNeighbors(ToTile(food)).Where(t => !GameBoard.IsBlocked(t)).ToList()
                });

/*
 *              if (newDist < dist && pathToFood.Any() &&
 *                  GameBoard.GetNeighbors(ToTile(food)).Count(t => !GameBoard.IsBlocked(t)) > 1)
 *              {
 *                  dist = (int)newDist;
 *                  foodTile = ToTile(food);
 *                  foodFound = true;
 *              }*/
            }


            var foodTile = SelectBestFood(sortedFood);


            if (foodTile != null)
            {
                var pathToFood = (navigator.Navigate(ToTile(head), foodTile.Value) ?? new List <Tile>()).ToList();
                var startPath  = pathToFood.FirstOrDefault();
                if (pathToFood.Any() && GetMovement(startPath, head, out var foodMove))
                {
                    return(Ok(new Move {
                        Direction = foodMove
                    }));
                }
            }


            // Undvik väggar


            var tiles = GameBoard.GetNeighbors(new Tile(head.x, head.y)).Where(x => !GameBoard.IsBlocked(x)).ToList();

            if (tiles.Count == 0)
            {
                return(Ok(new Move {
                    Taunt = "Scheisse!!"
                }));
            }

            var tile = GetBestTile(tiles);


            if (GetMovement(tile, head, out var dir))
            {
                return(Ok(new Move {
                    Direction = dir
                }));
            }

            return(Ok(new Move()));
        }
        // public const int gridSize = 100;
        // public static Grid grid = new Grid(gridSize, gridSize);
        //public List<Point> FindPath(Point start, Point destination, IWorldProvider world, NamelessGame game)
        //{

        //    var gridOffset = new Point(start.X- (gridSize/2), start.Y- (gridSize/2));

        //    var gridStart = WorldToGrid(gridOffset, start);
        //    var gridEnd = WorldToGrid(gridOffset, destination);

        //    if (gridEnd.X < 0 || gridEnd.Y < 0 || gridEnd.X >= gridSize || gridEnd.Y >= gridSize)
        //    {
        //        return new List<Point>();
        //    }

        //    for (int x = 0; x < 100; x++)
        //    {
        //        for (int y = 0; y < 100; y++)
        //        {
        //            var point = GridToWorld(gridOffset, new Point(x, y));
        //            var tile = world.GetTile(point.X, point.Y);
        //            grid.UnblockCell(new Position(x, y));
        //            if (!tile.IsPassable())
        //            {
        //                grid.BlockCell(new Position(x, y));
        //            }
        //        }
        //    }


        //    //unblock start and end
        //    grid.UnblockCell(new Position(gridStart.X, gridStart.Y));
        //    grid.UnblockCell(new Position(gridEnd.X, gridEnd.Y));

        //    var path = grid.GetPath(new Position(gridStart.X, gridStart.Y), new Position(gridEnd.X, gridEnd.Y));

        //    List<Point> resultPoints = new List<Point>();
        //    foreach (var position in path)
        //    {

        //        var point = new Point(position.X, position.Y);
        //        resultPoints.Add(GridToWorld(gridOffset,point));
        //    }

        //    return resultPoints;

        //}


        //public List<Point> FindPath(Point start, Point destination, IWorldProvider world, NamelessGame game)
        //{

        //    Func<Tile, IEnumerable<Tile>> getNeighbors = delegate(Tile p)
        //    {
        //        return new[]
        //        {
        //            world.GetTile(p.GetCoordinate().X - 1, p.GetCoordinate().Y + 0), // L
        //            world.GetTile(p.GetCoordinate().X + 1, p.GetCoordinate().Y + 0), // R
        //            world.GetTile(p.GetCoordinate().X, p.GetCoordinate().Y + 1), // B
        //            world.GetTile(p.GetCoordinate().X, p.GetCoordinate().Y - 0), // T
        //        };
        //    };

        //    Func<Tile, Tile, double> getScoreBetween = (p1, p2) =>
        //    {
        //        // Manhatten Distance
        //        return Math.Abs(p1.GetCoordinate().X - p2.GetCoordinate().X) + Math.Abs(p1.GetCoordinate().Y - p2.GetCoordinate().Y);
        //    };

        //    var rand = new Random(42);

        //    Func<Tile, double> getHeuristicScore = p =>
        //    {
        //        if (p.IsPassable())
        //        {
        //            return Math.Abs(p.GetCoordinate().X - destination.X) + Math.Abs(p.GetCoordinate().Y - destination.Y);
        //        }
        //        else if( p.GetCoordinate().X<0||p.GetCoordinate().Y<0)
        //        {
        //            return double.MaxValue;
        //        }
        //        return double.MaxValue;
        //    };

        //    Console.WriteLine("Going from {0} to {1}", start, destination);

        //    var sw = Stopwatch.StartNew();
        //    double distance = 0;
        //    var results = AStarUtilities.FindMinimalPath(world.GetTile(start.X,start.Y), world.GetTile(destination.X,destination.Y), getNeighbors,
        //        getScoreBetween, getHeuristicScore).Result;
        //    sw.Stop();
        //    return results.Select(x=>x.GetCoordinate()).ToList();
        //}



        public List <Point> FindPath(Point start, Point destination, IWorldProvider world, NamelessGame game)
        {
            var from = new AStarNavigator.Tile(start.X, start.Y);

            var trimmedDestination = new Point(destination.X, destination.Y);


            //var differenceInX = Math.Abs(destination.X - start.X);

            //var areaSize = 5;

            //if (differenceInX > areaSize)
            //{
            //    trimmedDestination.X = destination.X > start.X
            //        ? destination.X - (differenceInX - areaSize)
            //        : destination.X + (differenceInX - areaSize);
            //}

            //var differenceInY = Math.Abs(destination.Y - start.Y);

            //if (differenceInY > areaSize)
            //{
            //    trimmedDestination.Y = destination.Y > start.Y
            //        ? destination.Y - (differenceInY - areaSize)
            //        : destination.Y + (differenceInY - areaSize);
            //}


            var navigator = new TileNavigator(
                new BlockedProvider(world, trimmedDestination, start), // Instance of: IBockedProvider
                new DiagonalNeighborProvider(),                        // Instance of: INeighborProvider
                new PythagorasAlgorithm(),                             // Instance of: IDistanceAlgorithm
                new ManhattanHeuristicAlgorithm()                      // Instance of: IDistanceAlgorithm
                );

            //if (destination.X - start.X <= -5)
            //{
            //    trimmedDestination.X = destination.X + Math.Abs(destination.X - start.X + 5);
            //}
            //else if (destination.X - start.X >= 5)
            //{
            //    trimmedDestination.X = destination.X - Math.Abs(destination.X - start.X + 5); ;
            //}


            //if (destination.Y - start.Y <= -5)
            //{
            //    trimmedDestination.Y = destination.Y + Math.Abs(destination.Y - start.Y + 5);
            //}
            //else if (destination.Y - start.Y >= 5)
            //{
            //    trimmedDestination.Y = destination.Y - Math.Abs(destination.Y - start.Y + 5);
            //}

            var to = new AStarNavigator.Tile(trimmedDestination.X, trimmedDestination.Y);

            var result = navigator.Navigate(from, to);

            if (result == null)
            {
                return(new List <Point>());
            }

            return(result.Select(x => new Point((int)x.X, (int)x.Y)).ToList());
        }