/** * Fill the dungeon's space with rooms and doors (some locked). * Keys are not inserted at this point. * * @param levels the keyLevel -> room-set mapping to update * @ if it fails * @see KeyLevelRoomMapping */ protected void PlaceRooms(KeyLevelRoomMapping levels, int roomsPerLock) { // keyLevel: the number of keys required to Get to the new room int keyLevel = 0; MZSymbol latestKey = null; // condition that must hold true for the player to reach the new room // (the set of keys they must have). MZCondition cond = new MZCondition(); // Loop to place rooms and link them while (dungeon.RoomCount() < constraints.GetMaxRooms()) { bool doLock = false; // Decide whether we need to place a new lock // (Don't place the last lock, since that's reserved for the boss) if (ShouldAddNewLock(keyLevel, levels.GetRooms(keyLevel).Count, roomsPerLock)) { latestKey = new MZSymbol(keyLevel++); cond = cond.And(latestKey); doLock = true; } // Find an existing room with a free edge: MZRoom parentRoom = null; if (!doLock && UnityEngine.Random.Range(0, 10) > 0) { parentRoom = ChooseRoomWithFreeEdge(levels.GetRooms(keyLevel), keyLevel); } if (parentRoom == null) { parentRoom = ChooseRoomWithFreeEdge(new List <MZRoom>(dungeon.GetRooms()), keyLevel); doLock = true; } if (parentRoom == null) { throw new OutOfRoomsException(); } // Decide which direction to put the new room in relative to the // parent int nextId = ChooseFreeEdge(parentRoom, keyLevel); List <Vector2Int> coords = constraints.GetCoords(nextId); MZRoom room = new MZRoom(nextId, coords, parentRoom, null, cond); // Add the room to the dungeon dungeon.Add(room); parentRoom.AddChild(room); dungeon.Link(parentRoom, room, doLock ? latestKey : null); levels.AddRoom(keyLevel, room); } }
/** * Computes the 'intensity' of each {@link MZRoom}. MZRooms generally Get more * intense the deeper they are into the dungeon. * * @param levels the keyLevel -> room-set mapping to update * @ if it fails * @see KeyLevelRoomMapping * @see MZRoom */ protected void ComputeIntensity(KeyLevelRoomMapping levels) { double nextLevelBaseIntensity = 0.0; for (int level = 0; level < levels.KeyCount(); ++level) { double intensity = nextLevelBaseIntensity * (1.0 - intensityEaseOff); foreach (MZRoom room in levels.GetRooms(level)) { if (room.GetParent() == null || !room.GetParent().GetPrecond(). Implies(room.GetPrecond())) { nextLevelBaseIntensity = Math.Max( nextLevelBaseIntensity, ApplyIntensity(room, intensity)); } } } NormalizeIntensity(); dungeon.FindBoss().SetIntensity(1.0); MZRoom goalRoom = dungeon.FindGoal(); if (goalRoom != null) { goalRoom.SetIntensity(0.0); } }
/** * Places keys within the dungeon in such a way that the dungeon is * guaranteed to be solvable. * * @param levels the keyLevel -> room-set mapping to use * @ if it fails * @see KeyLevelRoomMapping */ protected void PlaceKeys(KeyLevelRoomMapping levels) { // Now place the keys. For every key-level but the last one, place a // key for the next level in it, preferring rooms with fewest links // (dead end rooms). for (int key = 0; key < levels.KeyCount() - 1; ++key) { List <MZRoom> rooms = levels.GetRooms(key); Shuffle(rooms); // Collections.sort is stable: it doesn't reorder "equal" elements, // which means the shuffling we just did is still useful. rooms.Sort(new MZRoomIntensityComparer()); // Alternatively, use the EDGE_COUNT_COMPARATOR to put keys at // 'dead end' rooms. MZSymbol keySym = new MZSymbol(key); bool placedKey = false; foreach (MZRoom room in rooms) { if (room.GetItem() == null && constraints.RoomCanFitItem(room.id, keySym)) { room.SetItem(keySym); placedKey = true; break; } } if (!placedKey) { // there were no rooms into which the key would fit throw new RetryException(); } } }
/** * Places the BOSS and GOAL rooms within the dungeon, in existing rooms. * These rooms are moved into the next keyLevel. * * @param levels the keyLevel -> room-set mapping to update * @ if it fails * @see KeyLevelRoomMapping */ protected void PlaceBossGoalRooms(KeyLevelRoomMapping levels) { List <MZRoom> possibleGoalRooms = new List <MZRoom>(dungeon.RoomCount()); MZSymbol goalSym = new MZSymbol((int)MZSymbol.MZSymbolValue.Goal), bossSym = new MZSymbol((int)MZSymbol.MZSymbolValue.Boss); foreach (MZRoom room in dungeon.GetRooms()) { if (room.GetChildren().Count > 0 || room.GetItem() != null) { continue; } MZRoom parent = room.GetParent(); if (parent == null) { continue; } if (IsGenerateGoal() && (parent.GetChildren().Count != 1 || !parent.GetPrecond().Implies(room.GetPrecond()))) { continue; } if (IsGenerateGoal()) { if (!constraints.RoomCanFitItem(room.id, goalSym) || !constraints.RoomCanFitItem(parent.id, bossSym)) { continue; } } else { if (!constraints.RoomCanFitItem(room.id, bossSym)) { continue; } } possibleGoalRooms.Add(room); } if (possibleGoalRooms.Count == 0) { throw new RetryException(); } MZRoom goalRoom = possibleGoalRooms[UnityEngine.Random.Range(0, possibleGoalRooms.Count)], bossRoom = goalRoom.GetParent(); if (!IsGenerateGoal()) { bossRoom = goalRoom; goalRoom = null; } if (goalRoom != null) { goalRoom.SetItem(goalSym); } bossRoom.SetItem(bossSym); int oldKeyLevel = bossRoom.GetPrecond().GetKeyLevel(), newKeyLevel = Math.Min(levels.KeyCount(), constraints.GetMaxKeys()); if (oldKeyLevel != newKeyLevel) { List <MZRoom> oklRooms = levels.GetRooms(oldKeyLevel); if (goalRoom != null) { oklRooms.Remove(goalRoom); } oklRooms.Remove(bossRoom); if (goalRoom != null) { levels.AddRoom(newKeyLevel, goalRoom); } levels.AddRoom(newKeyLevel, bossRoom); MZSymbol bossKey = new MZSymbol(newKeyLevel - 1); MZCondition precond = bossRoom.GetPrecond().And(bossKey); bossRoom.SetPrecond(precond); if (goalRoom != null) { goalRoom.SetPrecond(precond); } if (newKeyLevel == 0) { dungeon.Link(bossRoom.GetParent(), bossRoom); } else { dungeon.Link(bossRoom.GetParent(), bossRoom, bossKey); } if (goalRoom != null) { dungeon.Link(bossRoom, goalRoom); } } }