示例#1
0
    public PathObject(Grid2DObject grid, Coord2DObject point1, Coord2DObject point2, int thickness)
    {
        this.grid      = grid;
        joints         = new List <Coord2DObject>();
        this.thickness = thickness;

        populateBestPath(point1, point2);
    }
    public Grid2DObject(Grid2DObject other)
    {
        this.dimensions = new Coord2DObject(other.dimensions);

        grid = new TileObject[dimensions.x, dimensions.y];

        for (int i = 0; i < dimensions.x; i++)
        {
            for (int j = 0; j < dimensions.y; j++)
            {
                Coord2DObject thisCoord2D   = new Coord2DObject(i, j);
                TileObject    thisOtherTile = other.getTile(thisCoord2D);
                setTile(thisOtherTile.type, thisCoord2D);
            }
        }
    }
示例#3
0
 public PathObject(Grid2DObject grid)
 {
     this.grid      = grid;
     joints         = new List <Coord2DObject>();
     this.thickness = 0;
 }
示例#4
0
 public PathObject(Grid2DObject grid, List <Coord2DObject> joints, int thickness)
 {
     this.grid      = grid;
     this.joints    = new List <Coord2DObject>(joints);
     this.thickness = thickness;
 }
示例#5
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));
        }
    }
 public GameGrid2DObject(Grid2DObject other)
     : base(other)
 {
     init(4, 8, 8);
 }