/// <summary> /// Makes a random room, return false if it couldn't generate /// </summary> protected bool MakeRoom(Connector connection) { int roomWidth = 2 + rnd.Next(4); int roomHeight = 2 + rnd.Next(4); int x = rnd.Next(1, roomWidth - 1); int y = rnd.Next(1, roomHeight - 1); if (connection.dir == Direction.east) x = -1; else if (connection.dir == Direction.north) y = roomHeight; else if (connection.dir == Direction.south) y = -1; else if (connection.dir == Direction.west) x = roomWidth; int placeX = connection.posX - x; int placeY = connection.posY - y; Rectangle roomRect = new Rectangle(placeX, placeY, roomWidth, roomHeight); if (!AreaInBounds(roomRect) || CheckArea(roomRect, 0)) return false; // Carve out this rectangle of the map FillRect(placeX, placeY, placeX + roomWidth, placeY + roomHeight, 0); // Place the door if (!connection.noDoor) { SetTile(connection.posX, connection.posY, -3); Door newDoor = new Door(new Vector2(connection.posX + 0.5f, connection.posY + 0.5f), @"Walls\door", true, true); map.AddEntity(newDoor); } // Add some new connections int numCon = rnd.Next(2, 5); for (int i = 0; i < numCon; i++) { Connector newConnector = new Connector(); newConnector.dir = GetRandomDirection(); newConnector.posX = rnd.Next(placeX, placeX + roomWidth); newConnector.posY = rnd.Next(placeY, placeY + roomHeight); if (newConnector.dir == Direction.north) newConnector.posY = placeY - 1; else if (newConnector.dir == Direction.south) newConnector.posY = placeY + roomHeight; else if (newConnector.dir == Direction.east) newConnector.posX = placeX + roomWidth; else if (newConnector.dir == Direction.west) newConnector.posX = placeX - 1; available_conns.Add(newConnector); // Add some monsters, perhaps int genNum = rnd.Next(-2, 2); for (int m = 0; m < genNum; m++) { AddRandomMonster(roomRect); } } return true; }
public MapGenerator(MapType Type, Map Map) { // List of map connections available available_conns = new List<Connector>(); rnd = new Random(); type = Type; map = Map; width = 50; height = 50; map.mapData = new int[width, height]; map.Width = width; map.Height = height; map.entities = new List<Entity>(); SetTypeTextures(); // Fills map FillRandRect(0, 0, width - 1, height - 1, 1, 3); // Randomly place the first connector Connector connection = new Connector(); connection.posX = width / 2; connection.posY = height / 2; connection.dir = GetRandomDirection(); connection.noDoor = true; available_conns.Add(connection); // Generate some rooms int numGenerated = 0; int tries = 0; while (numGenerated < 20 && tries < 30 && available_conns.Count > 0) { // Attempt to make a room with the first connector Connector tryThis = available_conns.First(); bool didGenerate = MakeRoom(tryThis); if (didGenerate) { // This connection generated fine, remove the connector numGenerated++; available_conns.Remove(tryThis); } else { // Check to see if we've tried too many times already if (tries++ > 2) { tries = 0; available_conns.Remove(tryThis); } } } PlaceStairsUp(); PlaceStairsDown(); }
public MapGenerator(MapType Type, Map Map) { // List of map connections available available_conns = new List<Connector>(); rnd = new Random(); type = Type; map = Map; width = 50; height = 50; map.mapData = new int[width, height]; map.Width = width; map.Height = height; map.entities = new List<Entity>(); SetTypeTextures(); monsters = map.Gamestate.MonsterData.Where(i => i.level <= map.level).ToList(); weapons = map.Gamestate.WeaponData.Where(i => i.level <= map.level).ToList(); armor = map.Gamestate.ArmorData.Where(i => i.level <= map.level).ToList(); // Build the total chance of all weapons weapon_total_chance = 0; foreach (WeaponData d in weapons) weapon_total_chance += d.chance; // Build the total chance of all armor armor_total_chance = 0; foreach (ArmorData d in armor) armor_total_chance += d.chance; // Fills map FillRandRect(0, 0, width - 1, height - 1, 1, 3); // Randomly place the first connector Connector connection = new Connector(); connection.posX = width / 2; connection.posY = height / 2; connection.dir = GetRandomDirection(); connection.noDoor = true; available_conns.Add(connection); // Generate some rooms int numGenerated = 0; int tries = 0; while (numGenerated < 16 && tries < 30 && available_conns.Count > 0) { // Attempt to make a room with the first connector Connector tryThis = available_conns.First(); bool didGenerate = MakeRoom(tryThis); if (didGenerate) { // This connection generated fine, remove the connector numGenerated++; available_conns.Remove(tryThis); } else { // Check to see if we've tried too many times already if (tries++ > 2) { tries = 0; available_conns.Remove(tryThis); } } } PlaceStairsUp(); PlaceStairsDown(); }