public static void TopDoor(Room r, TileType fillType) { for (int x = TB_DOOR_X; x < TB_DOOR_X + TB_DOOR_WIDTH; x++){ for (int y = r.Height - TB_DOOR_HEIGHT; y < r.Height; y++){ r[x,y] = new Tile(fillType); } } }
public static void LeftDoor(Room r, TileType fillType) { for (int x = 0; x < LR_DOOR_WIDTH; x++){ for (int y = LR_DOOR_Y; y < LR_DOOR_Y + LR_DOOR_HEIGHT; y++){ r[x,y] = new Tile(fillType); } } }
public static void RightDoor(Room r, TileType fillType) { for (int x = r.Width - LR_DOOR_WIDTH; x < r.Width; x++){ for (int y = LR_DOOR_Y; y < LR_DOOR_Y + LR_DOOR_HEIGHT; y++){ r[x,y] = new Tile(fillType); } } }
private Point getRandomPointOnTheSquareRadiusFor(Room outer_Room,Room inner_Room) { Point ret = null; Point start = inner_Room.ActualLocation; int lSqOuter = outer_Room.getMaxWidth(); if ( outer_Room.getMaxLength() > lSqOuter ) lSqOuter = outer_Room.getMaxLength(); int lSqInner = inner_Room.getMaxWidth(); if ( inner_Room.getMaxLength() > lSqInner ) lSqInner = inner_Room.getMaxLength(); int radius = (lSqInner+2)+(lSqOuter+2)+4; //start = new Point (start.x +inner_Room.getMaxWidth ()+1, start.y + inner_Room.getMaxWidth ()+1); start = new Point (start.x-lSqOuter-4, start.y-lSqOuter-4); //UnityEngine.Debug.Log("Start: "+start.x+","+start.y); int randomPoint = rng.Next (radius * 4); int rLine = randomPoint / radius; int rPos = randomPoint % radius; switch (rLine) { case 0: ret = new Point(start.x+rPos,start.y); break; case 1: ret = new Point(start.x+radius,start.y+rPos); break; case 2: ret = new Point(start.x+radius-rPos,start.y+radius); break; case 3: ret = new Point(start.x,start.y+radius-rPos); break; default: // WTF How did i get here? ret = new Point(-1,-1); break; } return ret; }
public static void BottomDoor(Room r, TileType fillType) { for (int x = TB_DOOR_X; x < TB_DOOR_X + TB_DOOR_WIDTH; x++){ for (int y = 0; y < TB_DOOR_HEIGHT; y++){ r[x,y] = new Tile(fillType); } } }
/// <summary> /// /// </summary> /// <param name="m"> /// A <see cref="Maze"/> /// </param> /// <param name="rand"> /// A <see cref="System.Random"/> /// </param> /// <returns> /// A <see cref="Room"/> /// </returns> public static Room ChambersFromMaze(Maze m, ChamberMap cm, System.Random rand, coords StartPosition, coords EndPosition) { // LEFTOFF: Here! Room ret = new Room(m.Width * Chambers.CHAMBER_WIDTH, m.Height * Chambers.CHAMBER_HEIGHT); for (int x = 0; x < m.Width; x++){ for (int y = 0; y < m.Height; y++){ // Select a random chamber type ChamberType ct; if (new coords(x,y) == EndPosition){ ct = ChamberType.TYPE_BOSS; } else{ ct = (ChamberType)(rand.Next() % (int)ChamberType.NumElements); if (ct == ChamberType.TYPE_BOSS){ ct = ChamberType.TYPE_5; } } cm[x,y] = ct; Room r = Chambers.Rooms(ct).Clone() as Room; switch(m[x,y]){ case MazeTileType.SPACE: // Handle the door carving // Left and Right if (x > 0){ if (m[x-1,y] == MazeTileType.SPACE){ Chambers.LeftDoor(r, TileType.SNOW); } } if (x < m.Width-1){ if (m[x+1,y] == MazeTileType.SPACE){ Chambers.RightDoor(r, TileType.SNOW); } } // Bottom and Top if (y > 0){ if (m[x,y-1] == MazeTileType.SPACE){ Chambers.BottomDoor(r, TileType.SNOW); } } if (y < m.Height-1){ if (m[x,y+1] == MazeTileType.SPACE){ Chambers.TopDoor(r, TileType.SNOW); } } ret.CopyRoom(r, x * Chambers.CHAMBER_WIDTH, y * Chambers.CHAMBER_HEIGHT); break; } } } return ret; }
private void buildHallway(Room r1, Room r2) { int mpX1, mpX2, mpY1, mpY2; bool stillInRoom, reEnteredRoom; // Find the mid points of both rectangles mpX1 = r1.ActualLocation.x + (r1.getMaxWidth () / 2 + 1); mpY1 = r1.ActualLocation.y + (r1.getMaxLength () / 2 + 1); mpX2 = r2.ActualLocation.x + (r2.getMaxWidth () / 2 + 1); mpY2 = r2.ActualLocation.y + (r2.getMaxLength () / 2 + 1); int cX, cY; bool emptyCheck = true; // Try Cross-point #1 cX = mpX1; cY = mpY2; int delX = -1,delY = -1; int stX = mpX1,stY = mpY1; if ( cX > mpX1 || cX > mpX2 ) delX = 1; if ( cY > mpY1 || cY > mpY2 ) delY = 1; if ( cX == mpX1 ) stX = mpX2; if ( cY == mpY1 ) stY = mpY2; stillInRoom = true; reEnteredRoom = false; if ( delX == 1 ) { for ( int i = stX; i <= cX; i += delX ) { if ( this[i,cY] == 99 ) { if ( reEnteredRoom ) emptyCheck = false; stillInRoom = false; } else { if ( !stillInRoom ) { reEnteredRoom = true; } } //UnityEngine.Debug.Log ("["+i+","+cY+"] -> sIR: "+stillInRoom+" eC: "+emptyCheck); } } else { for ( int i = stX; i >= cX; i += delX ) { if ( this[i,cY] == 99 ) { if ( reEnteredRoom ) emptyCheck = false; stillInRoom = false; } else { if ( !stillInRoom ) { reEnteredRoom = true; } } //UnityEngine.Debug.Log ("["+i+","+cY+"] -> sIR: "+stillInRoom+" eC: "+emptyCheck); } } stillInRoom = true; reEnteredRoom = false; if ( delY == 1 ) { for ( int i = stY; i <= cY; i += delY ) { if ( this[cX,i] == 99 ) { if ( reEnteredRoom ) emptyCheck = false; stillInRoom = false; } else { if ( !stillInRoom ) { reEnteredRoom = true; } } //UnityEngine.Debug.Log ("["+cX+","+i+"] -> sIR: "+stillInRoom+" eC: "+emptyCheck); } } else { for ( int i = stY; i >= cY; i += delY ) { if ( this[cX,i] == 99 ) { if ( reEnteredRoom ) emptyCheck = false; stillInRoom = false; } else { if ( !stillInRoom ) { reEnteredRoom = true; } } //UnityEngine.Debug.Log ("["+cX+","+i+"] -> sIR: "+stillInRoom+" eC: "+emptyCheck); } } //emptyCheck = false; if ( emptyCheck ) { //UnityEngine.Debug.Log("EC1 -- > stX: "+stX+" stY: "+stY+" delX: "+delX+" cX: "+cX+" cY: "+cY+" delY: "+delY); // This path is clear, so build it! if ( delX == 1 ) { for ( int i = stX; i <= cX; i += delX ) if ( this[i,cY] >= 98 ) placeHallAt(i,cY); } else { for ( int i = stX; i >= cX; i += delX ) if ( this[i,cY] >= 98 ) placeHallAt(i,cY); } if ( delY == 1 ) { for ( int i = stY; i <= cY; i += delY ) if ( this[cX,i] >= 98 ) placeHallAt(cX,i); } else { for ( int i = stY; i >= cY; i += delY ) if ( this[cX,i] >= 98 ) placeHallAt(cX,i); } } else { // Try Cross-point #2 cX = mpX2; cY = mpY1; delX = -1; delY = -1; stX = mpX1; stY = mpY1; if ( cX > mpX1 || cX > mpX2 ) delX = 1; if ( cY > mpY1 || cY > mpY2 ) delY = 1; if ( cX == mpX1 ) stX = mpX2; if ( cY == mpY1 ) stY = mpY2; emptyCheck = true; stillInRoom = true; reEnteredRoom = false; if ( delX == 1 ) { for ( int i = stX; i <= cX; i += delX ) { if ( this[i,cY] == 99 ) { if ( reEnteredRoom ) emptyCheck = false; stillInRoom = false; } else { if ( !stillInRoom ) { reEnteredRoom = true; } } UnityEngine.Debug.Log ("["+i+","+cY+"] -> sIR: "+stillInRoom+" eC: "+emptyCheck); } } else { for ( int i = stX; i >= cX; i += delX ) { if ( this[i,cY] == 99 ) { if ( reEnteredRoom ) emptyCheck = false; stillInRoom = false; } else { if ( !stillInRoom ) { reEnteredRoom = true; } } UnityEngine.Debug.Log ("["+i+","+cY+"] -> sIR: "+stillInRoom+" eC: "+emptyCheck); } } stillInRoom = true; reEnteredRoom = false; if ( delY == 1 ) { for ( int i = stY; i <= cY; i += delY ) { if ( this[cX,i] == 99 ) { if ( reEnteredRoom ) emptyCheck = false; stillInRoom = false; } else { if ( !stillInRoom ) { reEnteredRoom = true; } } UnityEngine.Debug.Log ("["+cX+","+i+"] -> sIR: "+stillInRoom+" eC: "+emptyCheck); } } else { for ( int i = stY; i >= cY; i += delY ) { if ( this[cX,i] == 99 ) { if ( reEnteredRoom ) emptyCheck = false; stillInRoom = false; } else { if ( !stillInRoom ) { reEnteredRoom = true; } } UnityEngine.Debug.Log ("["+cX+","+i+"] -> sIR: "+stillInRoom+" eC: "+emptyCheck); } } if ( emptyCheck ) { //UnityEngine.Debug.Log("EC2 -- > stX: "+stX+" stY: "+stY+" delX: "+delX+" cX: "+cX+" cY: "+cY+" delY: "+delY); // This path is clear, so build it! if ( delX == 1 ) { for ( int i = stX; i <= cX; i += delX ) if ( this[i,cY] >= 98 ) placeHallAt(i,cY); } else { for ( int i = stX; i >= cX; i += delX ) if ( this[i,cY] >= 98 ) placeHallAt(i,cY); } if ( delY == 1 ) { for ( int i = stY; i <= cY; i += delY ) if ( this[cX,i] >= 98 ) placeHallAt(cX,i); } else { for ( int i = stY; i >= cY; i += delY ) if ( this[cX,i] >= 98 ) placeHallAt(cX,i); } } else { UnityEngine.Debug.Log("Err -- >"+ " mp1: ["+mpX1+","+mpY1+"]"+ " mp2: ["+mpX2+","+mpY2+"]" ); UnityEngine.Debug.Log("Unable to draw a hallway!!"); } } // End Else -> Cross-Point #2 }
public void CopyRoom(Room source, int x, int y) { int xLim = Math.Min(Width, x + source.Width); int yLim = Math.Min(Height, y + source.Height); for (int xx = x; xx < xLim; xx++){ for (int yy = y; yy < yLim; yy++){ tiles[xx, yy] = source[xx - x, yy - y]; } } }
/// <summary> /// Get a sub-room in the specified rectangle. /// </summary> /// <param name='x'> /// X. /// </param> /// <param name='y'> /// Y. /// </param> /// <param name='width'> /// Width. /// </param> /// <param name='height'> /// Height. /// </param> public Room Slice(int x, int y, int width, int height) { if (x < 0) x = 0; if (x > Width-1) x = Width-1; if (y < 0) y = 0; if (y > Height-1) y = Height-1; if (width < 1) width = 1; if (x + width > Width) width = Width-x; if (height < 1) height = 1; if (y + height > Height) height = Height-y; Room ret = new Room(width, height); // Do the copying! for (int xx = 0; xx < width; xx++){ for (int yy = 0; yy < height; yy++){ ret[xx,yy] = this[xx+x, yy+y]; } } return ret; }
public object Clone() { Room ret = new Room(0, 0); ret.tiles = (Tile[,]) tiles.Clone(); return ret; }
public static Room RoomFromMaze(Maze m) { Room ret = new Room(m.Width, m.Height); for (int x = 0; x < ret.Width; x++){ for (int y = 0; y < ret.Height; y++){ switch(m[x,y]){ case MazeTileType.WALL: ret[x,y] = new Tile(TileType.ROCK); break; case MazeTileType.SPACE: ret[x,y] = new Tile(TileType.BLANK); break; default: ret[x,y] = new Tile(TileType.BLANK); break; } } } return ret; }
/// <summary> /// Totally random room generation /// </summary> /// <param name="width"> /// A <see cref="System.Int32"/> /// </param> /// <param name="height"> /// A <see cref="System.Int32"/> /// </param> /// <returns> /// A <see cref="Room"/> /// </returns> public static Room RandomRoom(int width, int height) { System.Random r = new System.Random(); Room ret = new Room(width, height); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { TileType t = TileType.BLANK; switch (r.Next() % 3) { case 0: t = TileType.BLANK; break; case 1: t = TileType.ROCK; break; case 2: t = TileType.SNOW; break; } ret[x, y] = new Tile(t); } } return ret; }
/// <summary> /// Read the room from a string representation. /// </summary> /// <param name="s"> /// A <see cref="System.String"/> /// </param> /// <returns> /// A <see cref="Room"/> /// </returns> public static Room FromString(string s) { var lines = (from str in s.Split('\r','\n') where str.Length > 0 select str).ToArray(); int height = lines.Length; if (height == 0){ throw new RoomFormatException("Zero lines in input."); } int width = lines[0].Length; Room ret = new Room(width, height); for (int y = height-1; y >= 0; y--){ string currentLine = lines[height - y - 1]; if (currentLine.Length != width){ Debug.Log("Width: "+width+" doesn't match line: "+currentLine.Length); throw new RoomFormatException("Inconsistent line width ("+width+" vs. "+currentLine.Length+") at y = "+y); } for (int x = 0; x < width; x++){ ret[x,y] = Tile.FromChar(currentLine[x]); } } return ret; }
/// <summary> /// Crappy, naive random generation algorithm. /// </summary> /// <param name="width"> /// A <see cref="System.Int32"/> /// </param> /// <param name="height"> /// A <see cref="System.Int32"/> /// </param> /// <param name="numRooms"> /// A <see cref="System.Int32"/> /// </param> /// <returns> /// A <see cref="Room"/> /// </returns> public static Room ChambersGen0(int width, int height, int numRooms) { Room ret = new Room(width, height); for (int x = 0; x < width; x++){ for (int y = 0; y < width; y++){ ret.tiles[x,y] = new Tile(TileType.SNOW); } } System.Random r = new System.Random(); for (int i = 0; i < numRooms; i++){ // Randomly select the index ChamberType cIdx = (ChamberType)(r.Next() % (int)ChamberType.NumElements); Room rm = Chambers.Rooms(cIdx); int x = r.Next() % width; int y = r.Next() % height; ret.CopyRoom(rm, x, y); } return ret; }