Esempio n. 1
0
    void ResolveAStar(GridItem argStart, GridItem argDest)
    {
        if (null == argStart || null == argDest)
        {
            return;
        }

        List <GridItem> lsOpenItems = new List <GridItem> {
            argStart
        };
        HashSet <GridItem> lsClosedItems = new HashSet <GridItem>();

        argStart.g = 0;
        argStart.h = GetDistance(argStart, argDest);
        argStart.fCost();

        GridItem lsCurrent = lsOpenItems[0];

        while (lsOpenItems.Count > 0)
        {
            if (lsCurrent == argDest)
            {
                TracePath(argStart, argDest);
            }

            lsCurrent = lsOpenItems[0];
            for (int i = 1; i < lsOpenItems.Count; i++)
            {
                if (lsOpenItems[i].GetFCost() < lsCurrent.GetFCost())
                {
                    lsCurrent = lsOpenItems[i];
                }
            }

            lsOpenItems.Remove(lsCurrent);
            lsClosedItems.Add(lsCurrent);

            foreach (GridItem neighbour in GetNeighbours(lsCurrent))
            {
                if (lsClosedItems.Contains(neighbour))
                {
                    continue;
                }
                if (neighbour.Blocked || neighbour.Occupied || !neighbour.Active)
                {
                    continue;
                }
                double lsPredictedGCost = lsCurrent.g + GetDistance(lsCurrent, neighbour);
                if (lsPredictedGCost < neighbour.g)
                {
                    neighbour.Parent = lsCurrent;
                    neighbour.g      = lsPredictedGCost;
                    neighbour.h      = GetDistance(neighbour, argDest);
                    neighbour.fCost();

                    if (!lsOpenItems.Contains(neighbour))
                    {
                        lsOpenItems.Add(neighbour);
                    }
                }
            }
        }

        //nopath
    }
Esempio n. 2
0
    void ResolveAStar(Vector3 argStartPos, Vector3 argDestPos)
    {
        GridItem lsStart = GetTileByPosition(argStartPos);
        GridItem lsDest  = GetTileByPosition(argDestPos);

        if (null == lsStart || null == lsDest)
        {
            return;
        }

        List <GridItem> lsOpenItems = new List <GridItem> {
            lsStart
        };
        HashSet <GridItem> lsClosedItems = new HashSet <GridItem>();

        //initialising costs of path to default val
        for (int i = 0; i < m_Grid.x; i++)
        {
            for (int j = 0; j < m_Grid.y; j++)
            {
                m_Grid.Tiles[i, 0, j].g      = int.MaxValue;
                m_Grid.Tiles[i, 0, j].Parent = null;
            }
        }

        lsStart.g = 0;
        lsStart.h = GetDistance(lsStart, lsDest);
        lsStart.fCost();

        GridItem lsCurrent = lsOpenItems[0];

        while (lsOpenItems.Count > 0)
        {
            for (int i = 1; i < lsOpenItems.Count; i++)
            {
                if (lsOpenItems[i].GetFCost() < lsCurrent.GetFCost() || lsCurrent.GetFCost() == lsOpenItems[i].GetFCost() && lsOpenItems[i].h < lsCurrent.h)
                {
                    lsCurrent = lsOpenItems[i];
                }
            }

            lsOpenItems.Remove(lsCurrent);
            lsClosedItems.Add(lsCurrent);

            //get closest neighbour based on g cost
            foreach (GridItem neighbour in GetNeighbours(lsCurrent))
            {
                if (lsClosedItems.Contains(neighbour))
                {
                    continue;
                }
                if (neighbour.Blocked || neighbour.Occupied || !neighbour.Active)
                {
                    continue;
                }
                double lsPredictedGCost = lsCurrent.g + GetDistance(lsCurrent, neighbour);
                if (lsPredictedGCost < neighbour.g)
                {
                    neighbour.Parent = lsCurrent;
                    neighbour.g      = lsPredictedGCost;
                    neighbour.h      = GetDistance(neighbour, lsDest);
                    neighbour.fCost();

                    //if the neighbour hasnt been added
                    if (!lsOpenItems.Contains(neighbour))
                    {
                        lsOpenItems.Add(neighbour);
                    }
                }
            }

            if (lsCurrent == lsDest)
            {
                TracePath(lsStart, lsDest);
                return;
            }
        }
    }