/// <summary> /// Sets all the tiles in a specified rectangle on this grid to the specified type. /// Note that lowerLeft and upperRight MUST have this relationship /// (for example, upperRight.y cannot be lower than lowerLeft.y), /// or I'm going to crash your program. /// If prioritize is false, /// this function will only mark tiles which are currently empty. /// Also, this function technically can mark single lines, /// but it's recommended that you use setTypeLine() instead for that. /// </summary> /// <param name="lowerLeft">Lower leftmost point of the desired rectangle</param> /// <param name="upperRight">Upper rightmost point of the desired rectangle</param> /// <param name="type">Tile type to set this rectangle to</param> /// <param name="prioritize">Whether this action should override existing non-empty tiles</param> public void setTypeRect(Coord2DObject lowerLeft, Coord2DObject upperRight, TileObject.TileType type, bool prioritize) { assertBounds(lowerLeft); assertBounds(upperRight); Assert.IsTrue(lowerLeft.x <= upperRight.x && lowerLeft.y <= upperRight.y, "Invalid argument order. " + "lowerLeft should be " + upperRight.ToString() + "and upperRight should be " + lowerLeft.ToString()); // Just setTypeLine if they gave us a line instead of a rectangle if (lowerLeft.x == upperRight.x || lowerLeft.y == upperRight.y) { setTypeLine(lowerLeft, upperRight, type, prioritize); return; } // If we're here, then we're marking a non-line rectangle, // and the arguments were provided in correct order for (int thisY = lowerLeft.y; thisY <= upperRight.y; thisY++) { // Go row by row Coord2DObject thisRowLeft = new Coord2DObject(lowerLeft.x, thisY); Coord2DObject thisRowRight = new Coord2DObject(upperRight.x, thisY); setTypeLine(thisRowLeft, thisRowRight, type, prioritize); } }
/// <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> /// Sets all the tiles in a specified line on this grid to the specified type. /// This overload adds the "thickness" parameter. /// A single straight line of Tiles is considered a row with layers = 0 (total thickness 1). /// layers = 1 surrounds the line on each side with additional rows, for a total thickness of 3. /// All other parameters remain the same as the other overload for this method, /// including that part about me crashing your program /// if point1 and point2 aren't on the same line. /// </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="layers">Number of layers on each side of this line</param> /// <param name="prioritize">Whether this method should override existing non-empty tiles</param> public void setTypeLine(Coord2DObject point1, Coord2DObject point2, TileObject.TileType type, int layers, bool prioritize) { for (int thisLayer = 0; thisLayer <= layers; thisLayer++) { // Rows (horizontal) if (point1.y == point2.y) { // Do row on top, offset by thisLevel Coord2DObject point1Layered = new Coord2DObject(point1.x, point1.y + thisLayer); Coord2DObject point2Layered = new Coord2DObject(point2.x, point2.y + thisLayer); if (checkBounds(point1Layered) && checkBounds(point2Layered)) { setTypeLine(point1Layered, point2Layered, type, prioritize); } // Do row on bot, offset by thisLevel point1Layered = new Coord2DObject(point1.x, point1.y - thisLayer); point2Layered = new Coord2DObject(point2.x, point2.y - thisLayer); if (checkBounds(point1Layered) && checkBounds(point2Layered)) { setTypeLine(point1Layered, point2Layered, type, prioritize); } } // Columns (vertical) else if (point1.x == point2.x) { // Do column on left, offset by thisLevel Coord2DObject point1Layered = new Coord2DObject(point1.x - thisLayer, point1.y); Coord2DObject point2Layered = new Coord2DObject(point2.x - thisLayer, point2.y); if (checkBounds(point1Layered) && checkBounds(point2Layered)) { setTypeLine(point1Layered, point2Layered, type, prioritize); } // Do column on right, offset by thisLevel point1Layered = new Coord2DObject(point1.x + thisLayer, point1.y); point2Layered = new Coord2DObject(point2.x + thisLayer, point2.y); if (checkBounds(point1Layered) && checkBounds(point2Layered)) { setTypeLine(point1Layered, point2Layered, type, prioritize); } } else { Assert.IsTrue(false, "point1" + point1.ToString() + " and point2 " + point2.ToString() + " not on a line"); } } }
/// <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 void assertBounds(Coord2DObject location) { Assert.raiseExceptions = true; Assert.IsTrue(checkBounds(location), "Invalid coordinate " + location.ToString()); }