Esempio n. 1
0
        public JumpPointParam(BaseGrid iGrid, GridPos iStartPos, GridPos iEndPos, DiagonalMovement iDiagonalMovement = DiagonalMovement.Always, HeuristicMode iMode = HeuristicMode.EUCLIDEAN)
            : base(iGrid, iStartPos, iEndPos, iDiagonalMovement, iMode)
        {
            openList = new IntervalHeap <Node>();

            CurIterationType = IterationType.LOOP;
        }
Esempio n. 2
0
 public AStarParam(StaticGrid iGrid, DiagonalMovement iDiagonalMovement)
 {
     m_searchGrid     = iGrid;
     DiagonalMovement = iDiagonalMovement;
     m_startNode      = null;
     m_endNode        = null;
 }
Esempio n. 3
0
        protected ParamBase(BaseGrid grid, DiagonalMovement diagonalMovement, HeuristicMode mode)
        {
            SetHeuristic(mode);

            searchGrid       = grid;
            DiagonalMovement = diagonalMovement;
            startNode        = null;
            endNode          = null;
        }
Esempio n. 4
0
        protected ParamBase(BaseGrid iGrid, DiagonalMovement iDiagonalMovement, HeuristicMode iMode)
        {
            SetHeuristic(iMode);

            MsearchGrid      = iGrid;
            DiagonalMovement = iDiagonalMovement;
            MstartNode       = null;
            MendNode         = null;
        }
Esempio n. 5
0
        public ParamBase(BaseGrid iGrid, DiagonalMovement iDiagonalMovement, HeuristicMode iMode)
        {
            SetHeuristic(iMode);

            m_searchGrid     = iGrid;
            DiagonalMovement = iDiagonalMovement;
            m_startNode      = null;
            m_endNode        = null;
        }
Esempio n. 6
0
        public override IEnumerable <Square> GetAvailableMoves(Board board)
        {
            var availableMoves = new List <Square>();
            var currentSquare  = board.FindPiece(this);
            var player         = Player;

            availableMoves = DiagonalMovement.GetDiagonalMoves(currentSquare, board, player);

            return(availableMoves);
        }
Esempio n. 7
0
 public AStarParam(StaticGrid iGrid, Point iStartPos, Point iEndPos, DiagonalMovement iDiagonalMovement) : this(iGrid, iDiagonalMovement)
 {
     m_startNode = m_searchGrid.GetNodeAt(iStartPos.X, iStartPos.Y);
     m_endNode   = m_searchGrid.GetNodeAt(iEndPos.X, iEndPos.Y);
     if (m_startNode == null)
     {
         m_startNode = new Node(iStartPos.X, iStartPos.Y, 0);
     }
     if (m_endNode == null)
     {
         m_endNode = new Node(iEndPos.X, iEndPos.Y, 0);
     }
 }
Esempio n. 8
0
 public ParamBase(BaseGrid iGrid, GridPos iStartPos, GridPos iEndPos, DiagonalMovement iDiagonalMovement, HeuristicMode iMode) : this(iGrid, iDiagonalMovement, iMode)
 {
     m_startNode = m_searchGrid.GetNodeAt(iStartPos.x, iStartPos.y);
     m_endNode   = m_searchGrid.GetNodeAt(iEndPos.x, iEndPos.y);
     if (m_startNode == null)
     {
         m_startNode = new Node(iStartPos.x, iStartPos.y, true);
     }
     if (m_endNode == null)
     {
         m_endNode = new Node(iEndPos.x, iEndPos.y, true);
     }
 }
Esempio n. 9
0
 protected ParamBase(BaseGrid iGrid, GridPos iStartPos, GridPos iEndPos, DiagonalMovement iDiagonalMovement, HeuristicMode iMode) : this(iGrid, iDiagonalMovement, iMode)
 {
     MstartNode = MsearchGrid.GetNodeAt(iStartPos.X, iStartPos.Y);
     MendNode   = MsearchGrid.GetNodeAt(iEndPos.X, iEndPos.Y);
     if (MstartNode == null)
     {
         MstartNode = new Node(iStartPos.X, iStartPos.Y, true);
     }
     if (MendNode == null)
     {
         MendNode = new Node(iEndPos.X, iEndPos.Y, true);
     }
 }
Esempio n. 10
0
 public List<GridPos> findPath(BaseGrid grid, DiagonalMovement move, GridPos startPos, GridPos endPos){
     JumpPointParam jpParam = new JumpPointParam(
         grid, 
         startPos, 
         endPos, 
         true,
         move, 
         HeuristicMode.EUCLIDEAN
     );
     List<GridPos> result = JumpPointFinder.FindPath(jpParam); 
     if (debug) Console.WriteLine("found path");
     return result;
 }
Esempio n. 11
0
        public IMap DefineMap(DiagonalMovement diagonal, int width, int height, int seed, int minPathLength)
        {
            var  IsAGoodMap = false;
            IMap ret        = null;

            var        aStar = FinderFactory.GetAStarImplementation();
            IHeuristic heuristic;

            heuristic = diagonal == DiagonalMovement.Never ?
                        HeuristicFactory.GetManhattamImplementation() :
                        HeuristicFactory.GetOctileImplementation();


            while (!IsAGoodMap)
            {
                var nodes = new List <Node>();
                var _map  = new Map(diagonal, width, height);

                var size = Convert.ToInt32((width * height) * (seed / 100M));
                var rand = new Random();

                while (size > 0)
                {
                    var p = RandNode(rand, width, height, true);
                    GridMap.Add(p);
                    size--;
                }
                _map.DefineAllNodes(GridMap);
                _map.StartNode = RandNode(rand, width, height, false);
                _map.EndNode   = RandNode(rand, width, height, false);
                if (!_map.ValidMap())
                {
                    throw new Exception("Invalid map configuration");
                }
                if (aStar.Find(_map, heuristic)) // verifica se o mapa possui um caminho
                {
                    var path = _map.GetPath();
                    if (path.Max(e => e.G) >= minPathLength) // verifica se o caminho sastifaz o tamanho minimo
                    {
                        IsAGoodMap = true;
                        ret        = _map;
                    }
                }
                GridMap = new List <Node>();
            }
            ret.Clear();
            ret.MapType = MapGeneratorEnum.Random;
            return(ret);
        }
Esempio n. 12
0
        protected ParamBase(BaseGrid grid, GridPos startPos, GridPos endPos, DiagonalMovement diagonalMovement, HeuristicMode mode) : this(grid, diagonalMovement, mode)
        {
            startNode = searchGrid.GetNodeAt(startPos.X, startPos.Y);
            endNode   = searchGrid.GetNodeAt(endPos.X, endPos.Y);

            if (startNode == null)
            {
                startNode = new Node(startPos.X, startPos.Y, true);
            }

            if (endNode == null)
            {
                endNode = new Node(endPos.X, endPos.Y, true);
            }
        }
Esempio n. 13
0
 private void Setup(DiagonalMovement diagonal, int width, int height, int startNodeX, int startNodeY, int endNodeX, int endNodeY)
 {
     Diagonal = diagonal;
     Width    = width;
     Height   = height;
     Nodes    = new Node[height, width];
     DefineAllNodes();
     StartNode = Nodes[startNodeY, startNodeX];
     EndNode   = Nodes[endNodeY, endNodeX];
     for (int i = 0; i < width; i++)
     {
         for (int j = 0; j < height; j++)
         {
             Nodes[j, i].X        = i;
             Nodes[j, i].Y        = j;
             Nodes[j, i].Walkable = true;
         }
     }
 }
Esempio n. 14
0
        public List <Node> RouteFinding(DiagonalMovement diagonal)
        {
            var rand     = RandomFactory.Rand;
            var listnode = new List <Node>();
            var run      = true;
            var node     = new Node(Map.StartNode);

            while (run)
            {
                if (!listnode.Exists(i => i.EqualsAll(node)))
                {
                    listnode.Add(node);
                }
                var list    = Map.GetNeighbors(node, diagonal, false, false);
                var ind     = rand.Next(0, list.Count);
                var newnode = list[ind];
                run = newnode != null && !listnode.Exists(i => i.EqualsAll(newnode));
                if (newnode != null)
                {
                    node = new Node(newnode, node, newnode.Direction);
                }
            }
            return(listnode);
        }
Esempio n. 15
0
        public List <Node> GetNeighbors(Node iNode, DiagonalMovement diagonalMovement)
        {
            int         tX = iNode.x;
            int         tY = iNode.y;
            List <Node> neighbors = new List <Node>();
            bool        tS0 = false, tD0 = false,
                        tS1 = false, tD1 = false,
                        tS2 = false, tD2 = false,
                        tS3 = false, tD3 = false;

            GridPos pos = new GridPos();

            if (IsWalkableAt(pos.Set(tX, tY - 1)))
            {
                neighbors.Add(GetNodeAt(pos));
                tS0 = true;
            }
            if (IsWalkableAt(pos.Set(tX + 1, tY)))
            {
                neighbors.Add(GetNodeAt(pos));
                tS1 = true;
            }
            if (IsWalkableAt(pos.Set(tX, tY + 1)))
            {
                neighbors.Add(GetNodeAt(pos));
                tS2 = true;
            }
            if (IsWalkableAt(pos.Set(tX - 1, tY)))
            {
                neighbors.Add(GetNodeAt(pos));
                tS3 = true;
            }

            switch (diagonalMovement)
            {
            case DiagonalMovement.Always:
                tD0 = true;
                tD1 = true;
                tD2 = true;
                tD3 = true;
                break;

            case DiagonalMovement.Never:
                break;

            case DiagonalMovement.IfAtLeastOneWalkable:
                tD0 = tS3 || tS0;
                tD1 = tS0 || tS1;
                tD2 = tS1 || tS2;
                tD3 = tS2 || tS3;
                break;

            case DiagonalMovement.OnlyWhenNoObstacles:
                tD0 = tS3 && tS0;
                tD1 = tS0 && tS1;
                tD2 = tS1 && tS2;
                tD3 = tS2 && tS3;
                break;

            default:
                break;
            }

            if (tD0 && IsWalkableAt(pos.Set(tX - 1, tY - 1)))
            {
                neighbors.Add(GetNodeAt(pos));
            }
            if (tD1 && IsWalkableAt(pos.Set(tX + 1, tY - 1)))
            {
                neighbors.Add(GetNodeAt(pos));
            }
            if (tD2 && IsWalkableAt(pos.Set(tX + 1, tY + 1)))
            {
                neighbors.Add(GetNodeAt(pos));
            }
            if (tD3 && IsWalkableAt(pos.Set(tX - 1, tY + 1)))
            {
                neighbors.Add(GetNodeAt(pos));
            }
            return(neighbors);
        }
Esempio n. 16
0
 public AStarParam(BaseGrid iGrid, float iweight, DiagonalMovement iDiagonalMovement = DiagonalMovement.Always, HeuristicMode iMode = HeuristicMode.EUCLIDEAN)
     : base(iGrid, iDiagonalMovement, iMode)
 {
     Weight = iweight;
 }
Esempio n. 17
0
		public AStarParam(BaseGrid grid, GridPos startPos, GridPos endPos, float weight, DiagonalMovement diagonalMovement = DiagonalMovement.Always, HeuristicMode mode = HeuristicMode.Euclidean)
			: base(grid, startPos, endPos, diagonalMovement, mode)
			=> Weight = weight;
Esempio n. 18
0
 public AStarParam(BaseGrid iGrid, GridPos iStartPos, GridPos iEndPos, float iweight, DiagonalMovement iDiagonalMovement = DiagonalMovement.Always, HeuristicMode iMode = HeuristicMode.Euclidean)
     : base(iGrid,iStartPos,iEndPos, iDiagonalMovement,iMode)
 {
     Weight = iweight;
 }
Esempio n. 19
0
        /*
        private class NodeComparer : IComparer<Node>
        {
            public int Compare(Node x, Node y)
            {
                var result = (x.heuristicStartToEndLen - y.heuristicStartToEndLen);
                if (result < 0) return -1;
                else
                if (result > 0) return 1;
                else
                {
                    return 0;
                }
            }
        }
        */
        public static List<GridPos> FindPath(AStarParam iParam)
        {
            var lo = new object();
            //var openList = new IntervalHeap<Node>(new NodeComparer());
            var openList = new IntervalHeap<Node>();
            Node startNode = iParam.StartNode;
            Node endNode = iParam.EndNode;
            HeuristicDelegate heuristic = iParam.HeuristicFunc;
            BaseGrid grid = iParam.SearchGrid;
            DiagonalMovement diagonalMovement = iParam.DiagonalMovement;
            float weight = iParam.Weight;


            startNode.StartToCurNodeLen = 0;
            startNode.HeuristicStartToEndLen = 0;

            openList.Add(startNode);
            startNode.IsOpened = true;

            while (openList.Count != 0)
            {
                Node node = openList.DeleteMin();
                node.IsClosed = true;

                if (node == endNode)
                {
                    return Node.Backtrace(endNode);
                }

                List<Node> neighbors = grid.GetNeighbors(node, diagonalMovement);

#if (UNITY)
                foreach(var neighbor in neighbors)
#else
                Parallel.ForEach(neighbors, neighbor =>
#endif
                {
#if (UNITY)
                    if (neighbor.isClosed) continue;
#else
                    if (neighbor.IsClosed) return;
#endif
                    int x = neighbor.X;
                    int y = neighbor.Y;
                    float ng = node.StartToCurNodeLen + (float)((x - node.X == 0 || y - node.Y == 0) ? 1 : Math.Sqrt(2));

                    if (!neighbor.IsOpened || ng < neighbor.StartToCurNodeLen)
                    {
                        neighbor.StartToCurNodeLen = ng;
                        if (neighbor.HeuristicCurNodeToEndLen == null) neighbor.HeuristicCurNodeToEndLen = weight * heuristic(Math.Abs(x - endNode.X), Math.Abs(y - endNode.Y));
                        neighbor.HeuristicStartToEndLen = neighbor.StartToCurNodeLen + neighbor.HeuristicCurNodeToEndLen.Value;
                        neighbor.Parent = node;
                        if (!neighbor.IsOpened)
                        {
                            lock (lo)
                            {
                                openList.Add(neighbor);
                            }
                            neighbor.IsOpened = true;
                        }
                        else
                        {

                        }
                    }
                }
#if (!UNITY)
                );
#endif
            }
            return new List<GridPos>();

        }
Esempio n. 20
0
        public JumpPointParam(BaseGrid iGrid, GridPos iStartPos, GridPos iEndPos, EndNodeUnWalkableTreatment iAllowEndNodeUnWalkable = EndNodeUnWalkableTreatment.Allow, DiagonalMovement iDiagonalMovement = DiagonalMovement.Always, HeuristicMode iMode = HeuristicMode.Euclidean)
            : base(iGrid, iStartPos, iEndPos, iDiagonalMovement, iMode)
        {
            CurEndNodeUnWalkableTreatment = iAllowEndNodeUnWalkable;
            openList = new IntervalHeap <Node>();

            CurIterationType = IterationType.Loop;
        }
 public IMap DefineMap(DiagonalMovement diagonal, int width, int height, int seed, int minPathLength)
 {
     return(DefineMap());
 }
Esempio n. 22
0
        public IList <Node> GetNeighbors(Node node, DiagonalMovement diag, bool ByRef = true, bool valid = true)
        {
            Node newnode;
            var  neighbors = new List <Node>();
            var  s0        = false;
            var  d0        = false;
            var  s1        = false;
            var  d1        = false;
            var  s2        = false;
            var  d2        = false;
            var  s3        = false;
            var  d3        = false;

            newnode = GetDirectionNode(node, DirectionMovement.Up, ByRef, valid);
            if (newnode != null)
            {
                neighbors.Add(newnode);
                s0 = true;
            }
            newnode = GetDirectionNode(node, DirectionMovement.Down, ByRef, valid);
            if (newnode != null)
            {
                neighbors.Add(newnode);
                s2 = true;
            }
            newnode = GetDirectionNode(node, DirectionMovement.Left, ByRef, valid);
            if (newnode != null)
            {
                neighbors.Add(newnode);
                s1 = true;
            }
            newnode = GetDirectionNode(node, DirectionMovement.Right, ByRef, valid);
            if (newnode != null)
            {
                neighbors.Add(newnode);
                s3 = true;
            }
            if (diag == DiagonalMovement.Never)
            {
                return(neighbors);
            }
            switch (diag)
            {
            case DiagonalMovement.OnlyWhenNoObstacles:
                d0 = s3 && s0;
                d1 = s0 && s1;
                d2 = s1 && s2;
                d3 = s2 && s3;
                break;

            case DiagonalMovement.IfAtMostOneObstacle:
                d0 = s3 || s0;
                d1 = s0 || s1;
                d2 = s1 || s2;
                d3 = s2 || s3;
                break;

            case DiagonalMovement.Always:
                d0 = true;
                d1 = true;
                d2 = true;
                d3 = true;
                break;

            default:
                throw new Exception("Incorrect value of diagonalMovement");
            }
            newnode = GetDirectionNode(node, DirectionMovement.UpRight, ByRef, valid);
            if (d0 && newnode != null)
            {
                neighbors.Add(newnode);
            }
            newnode = GetDirectionNode(node, DirectionMovement.UpLeft, ByRef, valid);
            if (d1 && newnode != null)
            {
                neighbors.Add(newnode);
            }
            newnode = GetDirectionNode(node, DirectionMovement.DownLeft, ByRef, valid);
            if (d2 && newnode != null)
            {
                neighbors.Add(newnode);
            }
            newnode = GetDirectionNode(node, DirectionMovement.DownRight, ByRef, valid);
            if (d3 && newnode != null)
            {
                neighbors.Add(newnode);
            }
            //if (neighbors.Any(e => e == null || !e.Walkable ))
            //    throw new Exception("NO!!");
            return(neighbors);
        }
Esempio n. 23
0
 public Map(DiagonalMovement diagonal, int width, int height, int startNodeX, int startNodeY, int endNodeX, int endNodeY)
 {
     Setup(diagonal, width, height, startNodeX, startNodeY, endNodeX, endNodeY);
 }
Esempio n. 24
0
 public AStarParam(BaseGrid iGrid, float iweight, DiagonalMovement iDiagonalMovement = DiagonalMovement.IfAtLeastOneWalkable, HeuristicMode iMode = HeuristicMode.Euclidean)
     : base(iGrid, iDiagonalMovement, iMode)
 {
     Weight = iweight;
 }
Esempio n. 25
0
 public AStar(Grid grid, DiagonalMovement diagonalMovement, HeuristicType heuristictType)
 {
     this.grid             = grid;
     this.diagonalMovement = diagonalMovement;
 }
        public IMap DefineMap(DiagonalMovement diagonal, int width, int height, int seed, int minPathLength)
        {
            int _GDC(int a, int b) => (b == 0 || a == 0) ? a | b : _GDC(Min(a, b), Max(a, b) % Min(a, b));

            var  blocksize  = Blocksize > 0 ? Blocksize : _GDC(width, height);
            var  IsAGoodMap = false;
            IMap ret        = null;

            // finder para valida se o mapa é passavel
            var        aStar = FinderFactory.GetAStarImplementation();
            IHeuristic heuristic;

            heuristic = diagonal == DiagonalMovement.Never ?
                        HeuristicFactory.GetManhattamImplementation() :
                        HeuristicFactory.GetOctileImplementation();

            var subgrid = new List <Node>();

            while (!IsAGoodMap)
            {
                var nodes = new List <Node>();
                var _map  = new Map(diagonal, width, height);

                var size = Convert.ToInt32(blocksize * blocksize * (seed / 100M));
                var rand = new Random();
                while (size > 0)
                {
                    var p = RandNode(rand, blocksize, blocksize, true);
                    subgrid.Add(p);
                    size--;
                }
                for (int i = 0; i < width; i += blocksize)
                {
                    for (int j = 0; j < height; j += blocksize)
                    {
                        foreach (var item in subgrid)
                        {
                            var x = item.X + i;
                            var y = item.Y + j;
                            if (x < width && y < height)
                            {
                                var node = new Node(x, y, false);
                                GridMap.Add(node);
                            }
                        }
                    }
                }
                _map.DefineAllNodes(GridMap);
                _map.StartNode = RandNode(rand, width, height, false);
                _map.EndNode   = RandNode(rand, width, height, false);
                if (!_map.ValidMap())
                {
                    throw new Exception("Invalid map configuration");
                }
                if (aStar.Find(_map, heuristic)) // verifica se o mapa possui um caminho
                {
                    var path = _map.GetPath();
                    if (path.Max(e => e.G) >= minPathLength) // verifica se o caminho sastifaz o tamanho minimo
                    {
                        IsAGoodMap = true;
                        _map.Clear();
                        ret = _map;
                    }
                }
                GridMap = new List <Node>();
                subgrid = new List <Node>();
            }

            ret.Clear();
            ret.MapType = MapGeneratorEnum.WithPattern;
            return(ret);
        }
Esempio n. 27
0
 public AStarParam(BaseGrid iGrid, GridPos iStartPos, GridPos iEndPos, float iweight, DiagonalMovement iDiagonalMovement = DiagonalMovement.IfAtLeastOneWalkable, HeuristicMode iMode = HeuristicMode.EUCLIDEAN)
     : base(iGrid, iStartPos, iEndPos, iDiagonalMovement, iMode)
 {
     Weight = iweight;
 }
Esempio n. 28
0
        public List <Node> find(Node node, DiagonalMovement diagonalMovement)
        {
            List <Node> list = new List <Node>();
            int         x    = node.x;
            int         y    = node.y;
            bool        flag4;
            bool        flag3;
            bool        flag2;
            bool        flag = flag2 = (flag3 = (flag4 = false));
            bool        flag8;
            bool        flag7;
            bool        flag6;
            bool        flag5 = flag6 = (flag7 = (flag8 = false));

            if (this.isWalk(x, y + 1))
            {
                list.Add(this.getNode(x, y + 1));
                flag2 = true;
            }
            if (this.isWalk(x + 1, y))
            {
                list.Add(this.getNode(x + 1, y));
                flag = true;
            }
            if (this.isWalk(x, y - 1))
            {
                list.Add(this.getNode(x, y - 1));
                flag3 = true;
            }
            if (this.isWalk(x - 1, y))
            {
                list.Add(this.getNode(x - 1, y));
                flag4 = true;
            }
            switch (diagonalMovement)
            {
            case DiagonalMovement.ALWAYS:
                flag5 = (flag6 = (flag7 = (flag8 = true)));
                break;

            case DiagonalMovement.NEVER:
                return(list);

            case DiagonalMovement.IFATMOSTONEOBSTACLE:
                flag6 = (flag4 | flag2);
                flag5 = (flag2 | flag);
                flag7 = (flag | flag3);
                flag8 = (flag3 | flag4);
                break;

            case DiagonalMovement.ONLYWHENNOOBSTACLES:
                flag6 = (flag4 & flag2);
                flag5 = (flag2 & flag);
                flag7 = (flag & flag3);
                flag8 = (flag3 & flag4);
                break;
            }
            if (flag6 && this.isWalk(x - 1, y + 1))
            {
                list.Add(this.getNode(x - 1, y + 1));
            }
            if (flag5 && this.isWalk(x + 1, y + 1))
            {
                list.Add(this.getNode(x + 1, y + 1));
            }
            if (flag7 && this.isWalk(x + 1, y - 1))
            {
                list.Add(this.getNode(x + 1, y - 1));
            }
            if (flag8 && this.isWalk(x - 1, y - 1))
            {
                list.Add(this.getNode(x - 1, y - 1));
            }
            return(list);
        }
Esempio n. 29
0
        public JumpPointParam(BaseGrid grid, EndNodeUnWalkableTreatment allowEndNodeUnWalkable = EndNodeUnWalkableTreatment.Allow, DiagonalMovement diagonalMovement = DiagonalMovement.Always, HeuristicMode mode = HeuristicMode.Euclidean)
            : base(grid, diagonalMovement, mode)
        {
            CurEndNodeUnWalkableTreatment = allowEndNodeUnWalkable;

            OpenList         = new IntervalHeap <Node>();
            CurIterationType = IterationType.Loop;
        }
Esempio n. 30
0
 public Map(DiagonalMovement diagonal, int width, int height)
 {
     Setup(diagonal, width, height, 0, 0, width - 1, height - 1);
 }