/// <summary>
    /// Sets all the tiles in a specified line on this grid to the specified type.
    /// Note that point1 and point2 MUST lie on the same line
    /// (i.e. either their x coordinates are equal or their y coordinates are equal),
    /// or I'm going to crash your program.
    /// If prioritize is false,
    /// this function will only mark tiles which are currently empty.
    /// </summary>
    /// <param name="point1">An endpoint</param>
    /// <param name="point2">Another endpoint</param>
    /// <param name="type">Tile type to set this line to</param>
    /// <param name="prioritize">Whether this method should override existing non-empty tiles</param>
    public void setTypeLine(Coord2DObject point1, Coord2DObject point2,
                            TileObject.TileType type, bool prioritize)
    {
        assertBounds(point1); assertBounds(point2);

        Assert.raiseExceptions = true;
        Assert.IsTrue(point1.x == point2.x || point1.y == point2.y,
                      "point1 " + point1.ToString() + " and point2 " + point2.ToString() + " must lie on a straight line");

        if (point1.Equals(point2) &&
            (prioritize || getTile(point1).type == TileObject.TileType.EMPTY))
        {
            getTile(point1).type = type;
            return;
        }

        // If on the same row
        if (point1.y == point2.y)
        {
            // Iterate from least x to greater x,
            // whichever is which
            int biggerX  = (point1.x > point2.x ? point1.x : point2.x);
            int smallerX = (point1.x < point2.x ? point1.x : point2.x);

            for (int i = smallerX; i <= biggerX; i++)
            {
                TileObject thisTile = getTile(new Coord2DObject(i, point1.y));

                if (thisTile == null)
                {
                    Debug.Log("i is " + i + ", point1.y is " + point1.y);
                }

                if (prioritize || thisTile.type == TileObject.TileType.EMPTY)
                {
                    thisTile.type = type;
                }
            }
        }

        // Else, they're on the same column
        else
        {
            // Iterate from least y to greatest y,
            // whichever is which
            int biggerY  = (point1.y > point2.y ? point1.y : point2.y);
            int smallerY = (point1.y < point2.y ? point1.y : point2.y);

            for (int i = smallerY; i <= biggerY; i++)
            {
                TileObject thisTile = getTile(new Coord2DObject(point1.x, i));

                if (prioritize || thisTile.type == TileObject.TileType.EMPTY)
                {
                    thisTile.type = type;
                }
            }
        }
    }
    /// <summary>
    /// Returns a list of random Coord2DObjects of specified size.
    /// As long as p1UpRight and p2LowLeft are initialized,
    /// you are guaranteed that this list will have no duplicate values,
    /// and will also not contain the values p1UpRight or p2LowLeft.
    /// Additionally, none of these points shall fall within the bases.
    /// </summary>
    /// <param name="amount">Number of random points to generate</param>
    /// <returns>List of distinct Coord2DObject's</returns>
    private List <Coord2DObject> getDistinctRandomPoints(int amount)
    {
        HashSet <Coord2DObject> pointsSet = new HashSet <Coord2DObject>();

        // Use a while loop instead of a for loop
        // because there's a small chance
        // that we could accidentally generate duplicate Coord2D's
        while (pointsSet.Count < amount)
        {
            Coord2DObject randCoord = getRandomNonBase();

            // These two will populate pointsSet later,
            // so check for duplicates now
            if (!randCoord.Equals(p1UpRight) && !randCoord.Equals(p2LowLeft))
            {
                pointsSet.Add(randCoord);
            }
        }

        // As far as this function is concerned,
        // order does not matter,
        // so we can clumsily return a list from our set
        return(new List <Coord2DObject>(pointsSet));
    }
Exemple #3
0
    /// <summary>
    /// Populates this Path's "joints" list with the best path between point1 and point2.
    /// This is accomplished by implementing Dijkstra's algorithm.
    /// This path's grid and joints MUST be initialized before this method is called.
    /// Algorithm adopted from the pseudocode found here:
    /// https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Pseudocode
    /// </summary>
    /// <param name="src">The coordinate that the path should start from</param>
    /// <param name="dest">The coordinate that the path should start from</param>
    private void populateBestPath(Coord2DObject src, Coord2DObject dest)
    {
        Grid2DObject tempGrid = new Grid2DObject(grid); // generate copy, maintaining tile types

        TileObject srcTile  = tempGrid.getTile(src);
        TileObject destTile = tempGrid.getTile(dest);

        srcTile.distance = 0;

        // Do some error checking
        Assert.raiseExceptions = true;
        Assert.IsTrue(!src.Equals(dest), "Attempted autopath to the same tile " + src.ToString());
        Assert.IsTrue(srcTile.type != TileObject.TileType.NON_TRAVERSABLE,
                      "Path attempted on srcTile  non-traversable tile " + srcTile.ToString() + " to destTile " + destTile.ToString());
        Assert.IsTrue(destTile.type != TileObject.TileType.NON_TRAVERSABLE,
                      "Path attempted on destTile non-traversable tile " + destTile.ToString() + " from srcTile " + srcTile.ToString());

        // Populate set Q
        HashSet <TileObject> setQ = new HashSet <TileObject>();

        foreach (TileObject t in tempGrid)
        {
            if (t.type != TileObject.TileType.NON_TRAVERSABLE)
            {
                setQ.Add(t);
            }
        }
        List <TileObject> listQ = new List <TileObject>(setQ);

        shuffleList <TileObject>(listQ);

        TileObject uTile = null;

        while (listQ.Count > 0)
        {
            // Get tile with minimum distance from setQ
            int runningMin = System.Int32.MaxValue;
            foreach (TileObject t in listQ)
            {
                if (t.distance < runningMin)
                {
                    runningMin = t.distance;
                    uTile      = t;
                }
            }

            // Make sure uTile is properly set,
            // then remove it from setQ
            Assert.IsTrue(uTile != null, "Minimum distance tile uTile not properly set");
            Assert.IsTrue(listQ.Contains(uTile), "setQ doesn't contain uTile " + uTile.ToString());
            listQ.Remove(uTile);

            // Break out if we've reached the destination,
            // we now need to construct the path via reverse iteration
            if (uTile == destTile)  // check for identity, not just equivalence
            {
                break;
            }

            // Update distances of all uTile's current neighbors
            HashSet <TileObject> uNeighbors = tempGrid.getTraversableNeighbors(uTile.location);

            foreach (TileObject thisNeighbor in uNeighbors)
            {
                int currentDist = uTile.distance + 1;

                if (currentDist < thisNeighbor.distance)
                {
                    thisNeighbor.distance = currentDist;
                    thisNeighbor.prev     = uTile;
                }
            }
        }



        // Ensure that uTile is actually usable
        Assert.IsTrue(uTile.prev != null || uTile == srcTile,
                      "Condition specified by Dijkstra's not met");

        // Populate joints by backtracing
        while (uTile != null)
        {
            joints.Add(uTile.location);
            uTile = uTile.prev;

            // Make sure if we're about to break out,
            // that we have enough joints to do so
            // (i.e. that we have at least 2 joints)
            Assert.IsTrue(!(uTile == null && joints.Count < 2),
                          "Not enough prev's? For sure not enough joints\n"
                          + "Perhaps src and dest are the same?\n"
                          + "src:  " + srcTile.ToString() + '\n'
                          + "dest: " + destTile.ToString() + '\n'
                          + "src.equals(dest)? " + src.Equals(dest));
        }
    }