Beispiel #1
0
    public static List <TilePosition> CalculateAStarPahtTile(IGCalculator characterCalculator, TilePosition startTile, TilePosition endTile)
    {
        List <AStarPathNode> rawPath = CalculatePath(characterCalculator, startTile.ConvertToAStarPathNode(), endTile.ConvertToAStarPathNode());
        List <TilePosition>  result  = new List <TilePosition>();

        for (int i = 0; i < rawPath.Count; i++)
        {
            result.Add(rawPath[i].ConvertToTilePosition());
        }
        return(result);
    }
Beispiel #2
0
 public static List <TilePosition> CalculatePathTile(IGCalculator calculator, bool[,] obstacleMap,
                                                     TilePosition startTile, TilePosition endTile, out List <TilePosition> aStarPath)
 {
     aStarPath = CalculateAStarPahtTile(calculator, startTile, endTile);
     return(CalculateLinePath(aStarPath, obstacleMap));
 }
Beispiel #3
0
    private static List <AStarPathNode> CalculatePath(IGCalculator calculator, AStarPathNode startPathNode, AStarPathNode endPathNode)
    {
        bool existPath = false;

        /*
         * Debug.Log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
         * Debug.Log(startPathNode.Row + "," + startPathNode.Column);
         */
        startPathNode.GCalculator = calculator;
        s_OpenList.Add(startPathNode.ValueF, new List <AStarPathNode>()
        {
            startPathNode
        });
        s_OpenDictionary.Add(startPathNode.Column + (startPathNode.Row << 16), startPathNode);
        RecycleAllNode();
        while (s_OpenList.Count != 0)
        {
            SortedDictionary <int, List <AStarPathNode> > .Enumerator enumerator = s_OpenList.GetEnumerator();
            enumerator.MoveNext();
            AStarPathNode currentPathNode = enumerator.Current.Value[0];
            enumerator.Current.Value.RemoveAt(0);
            if (enumerator.Current.Value.Count == 0)
            {
                s_OpenList.Remove(enumerator.Current.Key);
            }
            s_OpenDictionary.Remove(currentPathNode.Column + (currentPathNode.Row << 16));
            if (endPathNode.Column == currentPathNode.Column && endPathNode.Row == currentPathNode.Row)
            {
                existPath   = true;
                endPathNode = currentPathNode;
                break;
            }
            s_CloseList.Add(currentPathNode);
            s_CloseDictionary.Add(currentPathNode.Column + (currentPathNode.Row << 16), currentPathNode);
            //leftbottom
            AStarPathNode leftBottomPathNode = GetNewNodeFromPool(); //new AStarPathNode();
            leftBottomPathNode.Row         = currentPathNode.Row + 1;
            leftBottomPathNode.Column      = currentPathNode.Column - 1;
            leftBottomPathNode.GCalculator = calculator;
            DealNeighbourPathNode(currentPathNode, leftBottomPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary);

            //lefttop
            AStarPathNode leftTopPathNode = GetNewNodeFromPool();//new AStarPathNode();
            leftTopPathNode.Row         = currentPathNode.Row - 1;
            leftTopPathNode.Column      = currentPathNode.Column - 1;
            leftTopPathNode.GCalculator = calculator;
            DealNeighbourPathNode(currentPathNode, leftTopPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary);
            //rightbottom
            AStarPathNode rightBottomPathNode = GetNewNodeFromPool();//new AStarPathNode();
            rightBottomPathNode.Row         = currentPathNode.Row + 1;
            rightBottomPathNode.Column      = currentPathNode.Column + 1;
            rightBottomPathNode.GCalculator = calculator;
            DealNeighbourPathNode(currentPathNode, rightBottomPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary);
            //righttop
            AStarPathNode rightTopPathNode = GetNewNodeFromPool();//new AStarPathNode();
            rightTopPathNode.Row         = currentPathNode.Row - 1;
            rightTopPathNode.Column      = currentPathNode.Column + 1;
            rightTopPathNode.GCalculator = calculator;
            DealNeighbourPathNode(currentPathNode, rightTopPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary);

            //bottom
            AStarPathNode bottomPathNode = GetNewNodeFromPool();//new AStarPathNode();
            bottomPathNode.Row         = currentPathNode.Row + 1;
            bottomPathNode.Column      = currentPathNode.Column;
            bottomPathNode.GCalculator = calculator;
            DealNeighbourPathNode(currentPathNode, bottomPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary);
            //left
            AStarPathNode leftPathNode = GetNewNodeFromPool();//new AStarPathNode();
            leftPathNode.Row         = currentPathNode.Row;
            leftPathNode.Column      = currentPathNode.Column - 1;
            leftPathNode.GCalculator = calculator;
            DealNeighbourPathNode(currentPathNode, leftPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary);
            //right
            AStarPathNode rightPathNode = GetNewNodeFromPool();//new AStarPathNode();
            rightPathNode.Row         = currentPathNode.Row;
            rightPathNode.Column      = currentPathNode.Column + 1;
            rightPathNode.GCalculator = calculator;
            DealNeighbourPathNode(currentPathNode, rightPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary);
            //top
            AStarPathNode topPathNode = GetNewNodeFromPool();//new AStarPathNode();
            topPathNode.Row         = currentPathNode.Row - 1;
            topPathNode.Column      = currentPathNode.Column;
            topPathNode.GCalculator = calculator;
            DealNeighbourPathNode(currentPathNode, topPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary);
        }

        s_OpenList.Clear();
        s_CloseList.Clear();
        s_OpenDictionary.Clear();
        s_CloseDictionary.Clear();

        if (existPath)
        {
            List <AStarPathNode> path     = new List <AStarPathNode>();
            AStarPathNode        pathNode = endPathNode;
            while (pathNode.Column != startPathNode.Column || pathNode.Row != startPathNode.Row)
            {
                path.Add(pathNode);
                pathNode = pathNode.ParentPathNode;
            }
            path.Add(startPathNode);
            path.Reverse();
            return(path);
        }
        return(null);
    }
Beispiel #4
0
    private static List <AStarPathNode> CalculatePath(IGCalculator calculator, AStarPathNode startPathNode, AStarPathNode endPathNode)
    {
        bool existPath = false;

        startPathNode.GCalculator = calculator;
        s_OpenList.Add(startPathNode);
        s_OpenDictionary.Add(startPathNode.Column + (startPathNode.Row << 16), startPathNode);
        //int p = 0;
        RecycleAllNode();
        while (s_OpenList.Count != 0)
        {
            s_OpenList.Sort();
            AStarPathNode currentPathNode = s_OpenList[0];
            s_OpenList.RemoveAt(0);
            s_OpenDictionary.Remove(currentPathNode.Column + (currentPathNode.Row << 16));

            if (endPathNode.Column == currentPathNode.Column && endPathNode.Row == currentPathNode.Row)
            {
                existPath   = true;
                endPathNode = currentPathNode;
                break;
            }
            s_CloseList.Add(currentPathNode);
            s_CloseDictionary.Add(currentPathNode.Column + (currentPathNode.Row << 16), currentPathNode);
            //leftbottom
            AStarPathNode leftBottomPathNode = GetNewNodeFromPool(); //new AStarPathNode();
            leftBottomPathNode.Row         = currentPathNode.Row + 1;
            leftBottomPathNode.Column      = currentPathNode.Column - 1;
            leftBottomPathNode.GCalculator = calculator;
            DealNeighbourPathNode(currentPathNode, leftBottomPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary);

            //lefttop
            AStarPathNode leftTopPathNode = GetNewNodeFromPool();//new AStarPathNode();
            leftTopPathNode.Row         = currentPathNode.Row - 1;
            leftTopPathNode.Column      = currentPathNode.Column - 1;
            leftTopPathNode.GCalculator = calculator;
            DealNeighbourPathNode(currentPathNode, leftTopPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary);
            //rightbottom
            AStarPathNode rightBottomPathNode = GetNewNodeFromPool();//new AStarPathNode();
            rightBottomPathNode.Row         = currentPathNode.Row + 1;
            rightBottomPathNode.Column      = currentPathNode.Column + 1;
            rightBottomPathNode.GCalculator = calculator;
            DealNeighbourPathNode(currentPathNode, rightBottomPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary);
            //righttop
            AStarPathNode rightTopPathNode = GetNewNodeFromPool();//new AStarPathNode();
            rightTopPathNode.Row         = currentPathNode.Row - 1;
            rightTopPathNode.Column      = currentPathNode.Column + 1;
            rightTopPathNode.GCalculator = calculator;
            DealNeighbourPathNode(currentPathNode, rightTopPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary);

            //bottom
            AStarPathNode bottomPathNode = GetNewNodeFromPool();//new AStarPathNode();
            bottomPathNode.Row         = currentPathNode.Row + 1;
            bottomPathNode.Column      = currentPathNode.Column;
            bottomPathNode.GCalculator = calculator;
            DealNeighbourPathNode(currentPathNode, bottomPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary);
            //left
            AStarPathNode leftPathNode = GetNewNodeFromPool();//new AStarPathNode();
            leftPathNode.Row         = currentPathNode.Row;
            leftPathNode.Column      = currentPathNode.Column - 1;
            leftPathNode.GCalculator = calculator;
            DealNeighbourPathNode(currentPathNode, leftPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary);
            //right
            AStarPathNode rightPathNode = GetNewNodeFromPool();//new AStarPathNode();
            rightPathNode.Row         = currentPathNode.Row;
            rightPathNode.Column      = currentPathNode.Column + 1;
            rightPathNode.GCalculator = calculator;
            DealNeighbourPathNode(currentPathNode, rightPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary);
            //top
            AStarPathNode topPathNode = GetNewNodeFromPool();//new AStarPathNode();
            topPathNode.Row         = currentPathNode.Row - 1;
            topPathNode.Column      = currentPathNode.Column;
            topPathNode.GCalculator = calculator;
            DealNeighbourPathNode(currentPathNode, topPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary);

            //p += 8;
        }

        //Debug.Log(p);

        s_OpenList.Clear();
        s_CloseList.Clear();
        s_OpenDictionary.Clear();
        s_CloseDictionary.Clear();

        if (existPath)
        {
            List <AStarPathNode> path     = new List <AStarPathNode>();
            AStarPathNode        pathNode = endPathNode;
            while (pathNode.Column != startPathNode.Column || pathNode.Row != startPathNode.Row)
            {
                path.Add(pathNode);
                pathNode = pathNode.ParentPathNode;
            }
            path.Add(startPathNode);
            path.Reverse();
            return(path);
        }
        return(null);
    }