Beispiel #1
0
 public void Reset()
 {
     for (int i = 0; i < NodeCost.Length; i++)
     {
         NodeCost[i]   = 0;
         ParentNode[i] = -1;
     }
     OpenList.Clear();
     ClosedList.Clear();
 }
Beispiel #2
0
        public Stack <PathNode> FindPath(State state, Piece startPiece, Piece endPiece)
        {
            openList.Clear();
            closedHashSet.Clear();

            openList.Add(new PathNode(startPiece.position));

            while (openList.Count > 0)
            {
                PathNode currentNode = openList.Pop();

                if (currentNode.Position == endPiece.position)
                {
                    return(ReconstructPath(currentNode, endPiece));
                }

                closedHashSet.Add(currentNode);

                foreach (Vector2i neighbor in GetNeighbors(state, endPiece, currentNode.Position))
                {
                    if (closedHashSet.Contains(neighbor))
                    {
                        continue;
                    }

                    float gValue = currentNode.g + neighbor.Distance(currentNode.Position);

                    if (openList.Update(neighbor, gValue))
                    {
                        continue;
                    }

                    float hValue = GetH(neighbor, endPiece.position);

                    PathNode currentNeighbor = new PathNode(neighbor)
                    {
                        g      = gValue,
                        h      = hValue,
                        f      = gValue + hValue,
                        Parent = currentNode
                    };

                    openList.Add(currentNeighbor);
                }
            }

            return(null);
        }
Beispiel #3
0
    public void init(ref INode[,] map)
    {
        this.map = map;

        foreach (INode node in map)
        {
            if (node.property == nodeProp.START)
            {
                start = node;
            }
            if (node.property == nodeProp.GOAL)
            {
                goal = node;
            }
        }

        curTile       = null;
        isCalculating = false;

        if (OpenList == null)
        {
            OpenList = new List <INode>();
        }
        else
        {
            OpenList.Clear();
        }

        if (CloseList == null)
        {
            CloseList = new List <INode>();
        }
        else
        {
            CloseList.Clear();
        }

        if (PathList == null)
        {
            PathList = new List <INode>();
        }
        else
        {
            PathList.Clear();
        }
    }
Beispiel #4
0
        /// <summary>
        /// 重置
        /// </summary>
        public void ReSet()
        {
            if (StartCell != null)
            {
                MapBmpFillRectangle(StartCell, this._whiteSmoke);
                StartCell = null;
            }
            if (GoalCell != null)
            {
                MapBmpFillRectangle(GoalCell, this._whiteSmoke);
                GoalCell = null;
            }

            MapBmpFillRectangles(StoneCells, this._whiteSmoke);
            StoneCells.Clear();

            MapBmpFillRectangles(ClosedList, this._whiteSmoke);
            ClosedList.Clear();

            MapBmpFillRectangles(OpenList, this._whiteSmoke);
            OpenList.Clear();
        }
Beispiel #5
0
    public override ArrayList FindPaths(int StartPos, int InEndPos, int InID)
    {
        ArrayList Paths = new ArrayList();

        if (!IsValidPosIndex(StartPos) || !IsValidPosIndex(InEndPos))
        {
            return(Paths);
        }

        CTimeCheck.Start(InID);
        EndPos = InEndPos;

        ClosedList.Clear();
        OpenList.Clear();
        foreach (Tile tile in Tiles)
        {
            if (tile.bBlock)
            {
                ClosedList.Add(tile.Index);
            }

            tile.Reset();
        }

        ClosedList.Add(StartPos);
        int FindNextPath         = StartPos;
        int path_make_start_time = Environment.TickCount;

        while (true)
        {
            //if (100 <= Environment.TickCount - path_make_start_time)
            //    return Paths;

            ArrayList MakedOpenList = MakeOpenList(FindNextPath);

            if (OpenList.Count == 0)
            {
                CDebugLog.Log(ELogType.AStar, "OpenList.Count == 0");
            }

            //FindNextPath = FindNextPathIndex(ref MakedOpenList);
            ////FindNextPath = FindNextPathIndexFromOpenList();

            //if (-1 == FindNextPath)
            //{
            FindNextPath = FindNextPathIndexFromOpenList();
            //}

            if (-1 == FindNextPath)
            {
                CTimeCheck.End(ELogType.AStar, "Failed FindPaths");
                return(Paths);
            }

            if (FindNextPath == EndPos)
            {
                break;
            }

            OpenList.Remove(FindNextPath);
            ClosedList.Add(FindNextPath);
        }

        int path_index = EndPos;

        Paths.Add(EndPos);
        while (true)
        {
            if (path_index == StartPos)
            {
                break;
            }

            Paths.Add(((Tile)Tiles[path_index]).ParentTile);
            path_index = ((Tile)Tiles[path_index]).ParentTile;
        }
        Paths.Reverse();


        CTimeCheck.End(ELogType.AStar, "Success FindPaths");
        return(Paths);
    }
Beispiel #6
0
        public bool Compute(int originX, int originY, int destX, int destY)
        {
            OriginX      = originX;
            OriginY      = originY;
            DestinationX = destX;
            DestinationY = destY;
            Nodes.Clear();
            OpenList.Clear();

            if (originX == destX && originY == destY)
            {
                return(true);
            }

            var originOutsideMap = !IsValid(OriginX, OriginY);
            var destOutsideMap   = !IsValid(DestinationX, DestinationY);

            if (originOutsideMap || destOutsideMap)
            {
                return(false);
            }

            if (!Map.Tiles[OriginX, OriginY].Walkable || !Map.Tiles[DestinationX, DestinationY].Walkable)
            {
                return(false);
            }

            InitializeGrid(ClosedList, false, Width, Height);
            InitializeCellDetails();

            OpenList.Add(new NodeScore(0, new Point(OriginX, OriginY)));

            while (OpenList.Count != 0)
            {
                var nodeScore = OpenList[0];
                OpenList.RemoveAt(0);

                var x = nodeScore.Location.X;
                var y = nodeScore.Location.Y;
                ClosedList[x, y] = true;

                if (ProcessCell(x, y, NorthEast, DiagonalCost))
                {
                    break;
                }

                if (ProcessCell(x, y, North, 1))
                {
                    break;
                }

                if (ProcessCell(x, y, NorthWest, DiagonalCost))
                {
                    break;
                }

                if (ProcessCell(x, y, East, 1))
                {
                    break;
                }

                if (ProcessCell(x, y, West, 1))
                {
                    break;
                }

                if (ProcessCell(x, y, SouthEast, DiagonalCost))
                {
                    break;
                }

                if (ProcessCell(x, y, South, 1))
                {
                    break;
                }

                if (ProcessCell(x, y, SouthWest, DiagonalCost))
                {
                    break;
                }
            }

            return(true);
        }