Пример #1
0
    Path DownPath(Path currentPath, int pathInt, Vector2Int lastTime)
    {
        pathInt++;

        if (pathInt > MaxPath)
        {
            return(null);
        }
        if (Branches >= MaxBranch)
        {
            return(null);
        }

        Vector2Int coord   = currentPath.Current.gridCoord;
        Square     current = currentPath.Current;

        if (coord == endSquare.gridCoord)
        {
            if (pathInt < LowestTotalPath)
            {
                MaxPath         = (int)(pathInt * 1.2f);
                LowestTotalPath = pathInt;
            }
            Branches++;
            return(currentPath);
        }

        Vector2Int difCoord = coord - endSquare.gridCoord;
        int        diff     = Mathf.Abs(difCoord.x) + Mathf.Abs(difCoord.y);

        if (pathInt + diff > MaxPath)
        {
            return(null);
        }

        PathIds.Add(GridUtilities.TwoToOne(coord));

        //squares on the edge of the map should either be boxes or
        //enterances/exits so there's no point in us checking them
        //if the straight distance to the exit will make out path too
        //long we shouldn't bother checking it either
        if (GridUtilities.IsOnEdge(current))
        {
            return(null);
        }

        if (RightIsBetter(current.gridCoord, endSquare.gridCoord, right))
        {
            Vector2Int temp = right;
            right = left;
            left  = temp;

            temp      = diagRight;
            diagRight = diagLeft;
            diagLeft  = temp;
        }

        Square forwardSquare       = GridUtilities.GetNextSquare(current, forward);
        Square leftSquare          = GridUtilities.GetNextSquare(current, left);
        Square diagLeftSquare      = GridUtilities.GetNextSquare(current, diagLeft);
        Square diagBackLeftSquare  = GridUtilities.GetNextSquare(current, -diagRight);
        Square rightSquare         = GridUtilities.GetNextSquare(current, right);
        Square diagRightSquare     = GridUtilities.GetNextSquare(current, diagRight);
        Square diagBackRightSquare = GridUtilities.GetNextSquare(current, -diagLeft);
        Square backSquare          = GridUtilities.GetNextSquare(current, back);

        currentPath.FutureSquares = new List <Path>();

        //FORWARD
        if (back != lastTime)
        {
            CheckNextPath(ref currentPath, forwardSquare, pathInt, forward);
        }
        //LEFT
        if (CheckDirection(left, forwardSquare, diagLeftSquare, lastTime, coord))
        {
            CheckNextPath(ref currentPath, leftSquare, pathInt, left);
        }
        //RIGHT
        if (CheckDirection(right, forwardSquare, diagRightSquare, lastTime, coord))
        {
            CheckNextPath(ref currentPath, rightSquare, pathInt, right);
        }
        //BACK
        if (forward != lastTime &&
            ((leftSquare != null && leftSquare.hasBox) ||
             (rightSquare != null && rightSquare.hasBox) ||
             (diagBackLeftSquare != null && diagBackLeftSquare.hasBox) ||
             (diagBackRightSquare != null && diagBackRightSquare.hasBox)))
        {
            CheckNextPath(ref currentPath, backSquare, pathInt, back);
        }

        PathIds.Remove(GridUtilities.TwoToOne(coord));

        if (currentPath.FutureSquares.Count > 0)
        {
            return(currentPath);
        }

        return(null);
    }