try generating spaces multiple times if they don't contain any puzzle modules

This commit is contained in:
2022-11-24 11:30:53 +01:00
parent 5e5eaa2f0e
commit f13ba4cd95
2 changed files with 34 additions and 14 deletions

View File

@@ -27,6 +27,8 @@ namespace EscapeRoomEngine.Engine.Runtime
public delegate void UpdateUIHandler();
public event UpdateUIHandler UpdateUIEvent;
[InfoBox("If a space was generated without any puzzles in it, the engine will try generating another new space. To prevent infinite loops, the amount of retries is bound.")]
public int maxSpaceGenerationTries;
[Required] public EngineTheme theme;
public int NumberOfRooms => _rooms.Count;
@@ -68,22 +70,39 @@ namespace EscapeRoomEngine.Engine.Runtime
private void GenerateSpace(Room room, Passage entrance)
{
Logger.Log("Generating space...", LogType.RoomGeneration);
var puzzlesAdded = 0;
var tries = 0;
Space space;
Passage exit;
// create space
var space = new Space(room, entrance);
// add exit
var exitDoor = new DoorModule(space, theme.exitDoorTypes.RandomElement());
if (!space.AddModuleWithRequirements(exitDoor))
throw new EngineException("Could not satisfy requirements for exit door.");
var exit = new Passage(exitDoor);
// add puzzles
var puzzleCount = Utilities.Utilities.RandomInclusive(theme.puzzleCount.x, theme.puzzleCount.y);
for (var i = 0; i < puzzleCount; i++)
do
{
space.AddModuleWithRequirements(Module.CreateModuleByType(space, theme.puzzleTypes.RandomElement()));
tries++;
Logger.Log($"Generating space{(tries > 1 ? $" (try {tries})" : "")}...", LogType.RoomGeneration);
// create space
space = new Space(room, entrance);
// add exit
var exitDoor = new DoorModule(space, theme.exitDoorTypes.RandomElement());
if (!space.AddModuleWithRequirements(exitDoor))
throw new EngineException("Could not satisfy requirements for exit door.");
exit = new Passage(exitDoor);
// add puzzles
var puzzleCount = Utilities.Utilities.RandomInclusive(theme.puzzleCount.x, theme.puzzleCount.y);
for (var i = 0; i < puzzleCount; i++)
{
if (space.AddModuleWithRequirements(Module.CreateModuleByType(space, theme.puzzleTypes.RandomElement())))
{
puzzlesAdded++;
}
}
} while (puzzlesAdded == 0 && tries < maxSpaceGenerationTries);
if (puzzlesAdded == 0)
{
Logger.Log($"Unable to create space with puzzles after {tries} tries", LogType.Important);
}
room.AddSpace(space, exit);