void CreateRoomsAndCorridors() { // Get Div and Mod of hit_power to adjust amount of traps // mod_power adjusts the exponential amount of traps mod_power = dungeon_stats.trap_difficulty % 10; // div_power adjusts the starting amount of traps. Caps at 20. div_power = dungeon_stats.trap_difficulty / 10; if (div_power > 20) { div_power = 20; } // Function creates odds of getting a trap. FORMULA DOES NOT WORK VERY WELL. ): // At hit_power 20, mod_power 9, There is a 95.14% chance of getting a trap. trap_chance = Mathf.Pow(mod_power, (0.04f * mod_power) + 1.0f) + Mathf.Pow(div_power, (0.02f * div_power) + 1.0f) + 9.0f; // Create the rooms array with a random size. int ran = dungeon_stats.numRooms.Random; // Save random number for further use. rooms = new Room[ran]; Debug.Log("Num rooms: " + ran); // There should be one less corridor than there is rooms. corridors = new Corridor[rooms.Length - 1]; loopable_corridors = new List <Corridor>(); // Determine number of hidden corridors. for (int i = 0; i < ran; i++) { // 1 in 10 chance for every room that there will be a hidden room. if ((UnityEngine.Random.Range(1, 11)) == 10) { num_hidden++; } } Debug.Log("Hidden corridors: " + num_hidden); hidden_corridors = new HiddenCorridor[num_hidden]; // Create the first room and corridor. rooms[0] = new Room(); corridors[0] = new Corridor(); // Setup the first room, there is no previous corridor so we do not use one. rooms[0].SetupRoom(dungeon_stats.roomWidth, dungeon_stats.roomHeight, dungeon_stats.columns, dungeon_stats.rows); // Setup the first corridor using the first room. corridors[0].SetupCorridor(rooms[0], dungeon_stats.corridorLength, dungeon_stats.roomWidth, dungeon_stats.roomHeight, dungeon_stats.columns, dungeon_stats.rows, true); for (int i = 1; i < rooms.Length; i++) { // Create a room. rooms[i] = new Room(); // Setup the room based on the previous corridor. rooms[i].SetupRoom(dungeon_stats.roomWidth, dungeon_stats.roomHeight, dungeon_stats.columns, dungeon_stats.rows, corridors[i - 1], dungeon_stats.reward_chance, dungeon_stats.people_chance); // If we haven't reached the end of the corridors array... if (i < corridors.Length) { int x = i; int y = 1; // ... create a corridor. corridors[i] = new Corridor(); // Setup the corridor based on the room that was just created. corridors[i].SetupCorridor(rooms[i], dungeon_stats.corridorLength, dungeon_stats.roomWidth, dungeon_stats.roomHeight, dungeon_stats.columns, dungeon_stats.rows, false); // Check if corridor goes into an existing room // If it does, then add it into "looping" corridors and make a new corridor to try again. while (CheckCorridors(x)) { corridors[i].ReSetupCorridor(rooms[x], dungeon_stats.corridorLength, dungeon_stats.roomWidth, dungeon_stats.roomHeight, dungeon_stats.columns, dungeon_stats.rows, y); y++; // If we have done a full rotation, choose a different room to branch from. if (y == 4) { y = 1; if (x == i) { x = Mathf.FloorToInt(x / (Random.Range(1, i))); } } } } } // Set up hidden rooms. // Hidden rooms are basically just corridors with a chest at the end. for (int i = 0; i < hidden_corridors.Length; i++) { int y = 1; // Used for changing the direction the corridor faces. // Create a room. hidden_corridors[i] = new HiddenCorridor(); // Choose random exisiting room to branch off of int branch_from = UnityEngine.Random.Range(0, ran); // Setup the corridor based on the room chosen hidden_corridors[i].SetupCorridor(rooms[branch_from], dungeon_stats.corridorLength, dungeon_stats.roomWidth, dungeon_stats.roomHeight, dungeon_stats.columns, dungeon_stats.rows, false); // Check if corridor goes into an existing room // If it does, then add it into "looping" corridors and make a new corridor to try again. while (CheckHiddenCorridors(i)) { hidden_corridors[i].ReSetupCorridor(rooms[branch_from], dungeon_stats.corridorLength, dungeon_stats.roomWidth, dungeon_stats.roomHeight, dungeon_stats.columns, dungeon_stats.rows, y); y++; // If we have done a full rotation, choose a different room to branch from. if (y == 4) { y = 1; branch_from = UnityEngine.Random.Range(0, ran); } } } //Forces to have at least 1 treasure or 1 villager in each dungeon for (int i = 1; i < rooms.Length; i++) { if (rooms[i].has_people == true || rooms[i].has_treasure == true) { return; } } IntRange room_choose = new IntRange(1, rooms.Length - 1); IntRange reward_choose = new IntRange(1, 2); rooms[room_choose.Random].ForceReward(reward_choose.Random); }
void SetTilesValuesForCorridors() { // Go through every corridor... // Regular corridors for (int i = 0; i < corridors.Length; i++) { Corridor currentCorridor = corridors[i]; // and go through it's length. for (int j = 0; j < currentCorridor.corridorLength; j++) { // Start the coordinates at the start of the corridor. int xCoord = currentCorridor.startXPos; int yCoord = currentCorridor.startYPos; // Depending on the direction, add or subtract from the appropriate // coordinate based on how far through the length the loop is. switch (currentCorridor.direction) { case Direction.North: yCoord += j; break; case Direction.East: xCoord += j; break; case Direction.South: yCoord -= j; break; case Direction.West: xCoord -= j; break; } // If rolls a trap, set tile at coordinates to Trap. if (Random.Range(0.0f, 100.0f) <= trap_chance) { tiles[xCoord][yCoord] = TileType.Trap; } // Else set the tile at these coordinates to Floor. else { tiles[xCoord][yCoord] = TileType.Floor; } } } // Looping corridors for (int i = 0; i < loopable_corridors.Count; i++) { Corridor currentCorridor = loopable_corridors[i]; for (int j = 0; j < currentCorridor.corridorLength; j++) { int xCoord = currentCorridor.startXPos; int yCoord = currentCorridor.startYPos; switch (currentCorridor.direction) { case Direction.North: yCoord += j; break; case Direction.East: xCoord += j; break; case Direction.South: yCoord -= j; break; case Direction.West: xCoord -= j; break; } if (Random.Range(0.0f, 100.0f) <= trap_chance) { tiles[xCoord][yCoord] = TileType.Trap; } else { tiles[xCoord][yCoord] = TileType.Floor; } } } // Hidden corridors for (int i = 0; i < hidden_corridors.Length; i++) { HiddenCorridor currentCorridor = hidden_corridors[i]; for (int j = 0; j < currentCorridor.corridorLength; j++) { int xCoord = currentCorridor.startXPos; int yCoord = currentCorridor.startYPos; switch (currentCorridor.direction) { case Direction.North: yCoord += j; break; case Direction.East: xCoord += j; break; case Direction.South: yCoord -= j; break; case Direction.West: xCoord -= j; break; } // At the end of hidden corridor, put treasure chest. if (xCoord == currentCorridor.EndPositionX && yCoord == currentCorridor.EndPositionY) { tiles[xCoord][yCoord] = TileType.HiddenReward; Debug.Log("Hidden X: " + xCoord + " Hidden Y: " + yCoord); } // Much higher chance of having a trap. else if (Random.Range(0.0f, 100.0f) <= 80.0f) { tiles[xCoord][yCoord] = TileType.HiddenTrap; } else { tiles[xCoord][yCoord] = TileType.HiddenFloor; } } } }