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 delegate void UpdateUIHandler();
public event UpdateUIHandler UpdateUIEvent; 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; [Required] public EngineTheme theme;
public int NumberOfRooms => _rooms.Count; public int NumberOfRooms => _rooms.Count;
@@ -68,22 +70,39 @@ namespace EscapeRoomEngine.Engine.Runtime
private void GenerateSpace(Room room, Passage entrance) 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 do
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++)
{ {
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); room.AddSpace(space, exit);

View File

@@ -1619,6 +1619,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 9a9b6b8b557abbb4ab172444615ebf23, type: 3} m_Script: {fileID: 11500000, guid: 9a9b6b8b557abbb4ab172444615ebf23, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
maxSpaceGenerationTries: 10
theme: {fileID: 11400000, guid: 568d9a7d70f3edb4cb6db66a0010f105, type: 2} theme: {fileID: 11400000, guid: 568d9a7d70f3edb4cb6db66a0010f105, type: 2}
--- !u!4 &1568048335 --- !u!4 &1568048335
Transform: Transform: