Exemplo n.º 1
0
        /// <inheritdoc/>
        public PathfindingResponse FindJumpPointsWithSpatialAstar(Position startPoint, Position targetPoint)
        {
            if (!_ready)
            {
                InitializeAlgorithms(_contextManager.Current);
            }

            Point startPointPoint  = _contextManager.Current.PathfindingData.PositionToZeroBasedPoint(startPoint);
            Point targetPointPoint = _contextManager.Current.PathfindingData.PositionToZeroBasedPoint(targetPoint);

            int nodesOpen, nodesClosed;
            LinkedList <MyPathNode> path = _spatialAStar.Search(startPointPoint, targetPointPoint, Position.Zero, out nodesOpen, out nodesClosed);

            //Debug.Log($"{(path==null?"Failed " : "")} Path from {startPoint} to {targetPoint} with {path?.Count} steps, {nodesOpen} open nodes, {nodesClosed} closed nodes.");

            if (path == null || !path.Any())
            {
                return(new PathfindingResponse(PathfindingResult.FailureTargetUnreachable));
            }

            List <Position> resultJumpPointsInGrid = path.Select(p => p.Position).ToList();

            List <Position> naturalJumpPoints = _naturalLineCalculator.GetNaturalJumpPoints(resultJumpPointsInGrid).ToList();

            return(new PathfindingResponse(naturalJumpPoints));
        }
        public IEnumerable <WalkableNode> GetPath(Vector3 destination)
        {
            if (m_Solver == null)
            {
                CreateSolver();
            }

            return(m_Solver.Search(
                       (Vector2)m_CurrentCell,
                       (Vector2)m_DestinationCell,
                       null));
        }
Exemplo n.º 3
0
        public static MovementPath AStar(Point start, Point end, Stage stage = null)
        {
            MovementPath newPath = new MovementPath(null);
            if (stage == null)
            {
                stage = Stage.CurrentStage;
            }

            // setup grid with walls
            //MyPathNode[,] grid = new MyPathNode[(int)stage.Width, (int)stage.Height];
            MyPathNode[][] grid = new MyPathNode[(int)stage.Width][];

            for (int x = 0; x < (int)stage.Width; x++)
            {
                grid[x] = new MyPathNode[(int) stage.Height];
                for (int y = 0; y < (int)stage.Height; y++)
                {

                    grid[x][y] = new MyPathNode()
                    {
                        IsWall = stage.GetTileAt(x, y).GetWalkable(),
                        X = x,
                        Y = y,
                    };
                }
            }

            SpatialAStar<MyPathNode, Object> aStar = new SpatialAStar<MyPathNode, Object>(grid);

            LinkedList<MyPathNode> path = aStar.Search(new Point(0, 0),
               new Point(stage.Width - 2, stage.Height - 2), null);
            return null;
        }
Exemplo n.º 4
0
    void Awake()
    {
        // setup grid with walls
        MyPathNode[,] grid = new MyPathNode[Width, Height];
        for (int x = 0; x < Width; x++)
        {
            for (int y = 0; y < Height; y++)
            {
                bool isWall = ((y % 2) != 0) && (Random.Range(0, 10) != 8);
                grid[x, y] = new MyPathNode()
                {
                    IsWall = isWall,
                    X      = x,
                    Y      = y,
                };
            }
        }

        // second generic parameter should be the argument type for IsWalkable (I'm using GameObject)
        SpatialAStar <MyPathNode, Object> aStar = new SpatialAStar <MyPathNode, Object>(grid);

        // start point, desired end point,
        // the third parameter is the value passed to IsWalkable (eg, the GameObject of the NPC doing the walking)
        // if not path is found, null is returned
        LinkedList <MyPathNode> path = aStar.Search(new Point(0, 0),
                                                    new Point(Width - 2, Height - 2), null);
    }
Exemplo n.º 5
0
        public void CalculatePath()
        {
            Vector2Int pos    = new Vector2Int(Mathf.FloorToInt(transform.position.x), Mathf.FloorToInt(transform.position.y));
            Vector2Int target = new Vector2Int(Mathf.FloorToInt(destination.x), Mathf.FloorToInt(destination.y));

            path = aStar.Search(pos.x, pos.y, target.x, target.y, null);
        }
Exemplo n.º 6
0
        private LinkedList <Cell> TestIfMapIsSolveable()
        {
            SpatialAStar <Cell, Object> aStar = new SpatialAStar <Cell, Object>(grid.Cells);
            LinkedList <Cell>           path  = aStar.Search(new Point((int)player.Position.X / 20, (int)player.Position.Y / 20),
                                                             new Point((int)Goal.Position.X / 20, (int)Goal.Position.Y / 20), null);

            return(path);
        }
Exemplo n.º 7
0
        public LinkedList <ITile> Find(IBuilding building, IPoint placingPointOnMap)
        {
            _buildingOnMapLocator.BlockBuildingArea(_map, building, placingPointOnMap);

            var closestStreet       = _closestStreetFinder.Find(_map, placingPointOnMap);
            var pathToNearestStreet = _astar.Search(
                new AStarAlgorithm.Point(placingPointOnMap.X, placingPointOnMap.Y),
                new AStarAlgorithm.Point(closestStreet.X, closestStreet.Y),
                NeighbourClassification.ByWall);

            _map.UnblockAllTemporarilyBlockedTiles();

            return(pathToNearestStreet);
        }
		public IEnumerable<ILocation> GetWalkPoints (ILocation from, ILocation to)
		{
			if (_pathMask == null) yield break;

			int fromX = (int)from.X;
			int fromY = (int)from.Y;
			int toX = (int)to.X;
			int toY = (int)to.Y;
			SpatialAStar<PathNode, object> finder = new SpatialAStar<PathNode, object> (_pathMask);
			var paths = finder.Search (new Point (fromX, fromY), 
				new Point ((int)to.X, (int)to.Y), null);
			if (paths == null)
				yield break;
		    if (!SmoothPath) 
			{
				foreach (var node in paths) 
				{
					yield return new AGSLocation (node.X, node.Y, to.Z);
				}
			}
			int currentDirX = paths.First.Value.X - fromX;
			int currentDirY = paths.First.Value.Y - fromY;
			PathNode prevNode = null;
			PathNode prevAcceptedNode = paths.First.Value;
			foreach (var node in paths) 
			{
				if (prevNode != null) 
				{
					int dirX = node.X - prevNode.X;
					int dirY = node.Y - prevNode.Y;
					if (dirX != currentDirX || dirY != currentDirY) 
					{
						if (Math.Abs (prevAcceptedNode.X - node.X) <= 10
						    && Math.Abs (prevAcceptedNode.Y - node.Y) <= 10
							&& (node.X != toX || node.Y != toY))
							continue; //smoothing the path
						currentDirX = dirX;
						currentDirY = dirY;
						prevAcceptedNode = node;
						yield return new AGSLocation (node.X, node.Y, to.Z);
					}
				}
				prevNode = node;
			}
		}
Exemplo n.º 9
0
        // 寻路
        public List <int> FindPath(int fx, int fy, int tx, int ty, int maxSteps, TileType tileMask)
        {
            var path = new List <int>();

            var nodes = pathFinder.Search(fx, fy, tx, ty, new KeyValuePair <int, int>(maxSteps, (int)tileMask), true);

            if (nodes != null)
            {
                nodes.RemoveFirst(); // remove the src node
                FC.ForEach(nodes, (i, n) =>
                {
                    path.Add((int)n.Pos.x);
                    path.Add((int)n.Pos.y);
                }, () => path.Count < maxSteps * 2);
            }

            return(path);
        }
Exemplo n.º 10
0
    private void setupWalls()
    {
        if (isEntrance)
        {
            placeExteriorWallsAndInitializeGrid();
            placeStairWalls();
            return;
        }

        // Create rooms until a valid one is complete
        bool allPathsArePossible = true;

        do
        {
            placeExteriorWallsAndInitializeGrid();
            placeStairWalls();
            placeInteriorWalls();

            SpatialAStar <Tile, System.Object> aStar = new SpatialAStar <Tile, System.Object>(grid);
            allPathsArePossible = true;

            // Check to make sure all paths are possible
            RoomConnection connection = roomConnections[0]; // if all can connect to this connection, then they can all connect to each other
            foreach (RoomConnection otherConnection in roomConnections)
            {
                if (connection == otherConnection)
                {
                    continue;
                }

                LinkedList <Tile> path = aStar.Search(PositionOfRoomConnection(connection), PositionOfRoomConnection(otherConnection), null);
                bool pathIsPossible    = path != null;
                if (!pathIsPossible)
                {
                    allPathsArePossible = false;
                }
            }
        } while (!allPathsArePossible);
    }
Exemplo n.º 11
0
    public bool MoveToTile(GameManager.SpecialPathNode tile)
    {
        path = aStar.Search(TileX, TileY, tile.X, tile.Y, null);
        //Debug.Log("Enemy: " + TileX + ", " + TileY + "; Player: " + tile.X + ", " + tile.Y);
        if (path != null && path.Count > 0)
        {
            IsMoving        = true;
            GoalPos         = path.First.Value.tile.transform.position;
            CurrentGoalNode = path.First;
            LinkedListNode <GameManager.SpecialPathNode> next = path.First;

            /*
             * string pathStr = "";
             * while (next != null)
             * {
             *  pathStr += "(" + next.Value.X + ", " + next.Value.Y + "); ";
             *  next = next.Next;
             * }
             * //Debug.Log(pathStr);*/
            return(true);
        }
        return(false);
    }
Exemplo n.º 12
0
        // 寻路
        public List <Vec2> FindPath(Vec2 src, Vec2 dst, int radius, params T[] ignoreUIDs)
        {
            var path = new List <Vec2>();

            var dx = (int)dst.x;
            var dy = (int)dst.y;
            //if (!CheckSpareSpace(dx, dy, radius)
            //    && !FindNearestSpareSpace(dx, dy, radius, 1, out dx, out dy))
            //    return path;

            var nodes = pathFinder.Search((int)src.x, (int)src.y, dx, dy, new KeyValuePair <int, T[]>(radius, ignoreUIDs), 5);

            if (nodes != null)
            {
                nodes.RemoveFirst(); // remove the src node
                foreach (var n in nodes)
                {
                    path.Add(n.Pos);
                }
            }

            return(path);
        }
Exemplo n.º 13
0
        /// <summary>
        /// 経路検索
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        public List <PathNode> Search(Vector2Int from, Vector2Int to)
        {
            var res = aStar.Search(from, to, null);

            return((res != null) ? res.ToList() : null);
        }
Exemplo n.º 14
0
        private IEnumerable <PathNode> PathFinding(Point a, Point b)
        {
            var aStar = new SpatialAStar <PathNode, Object>(_court.ToWallGrid());

            return(aStar.Search(a, b, null));
        }