try generating spaces multiple times if they don't contain any puzzle modules
This commit is contained in:
@@ -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;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
tries++;
|
||||||
|
Logger.Log($"Generating space{(tries > 1 ? $" (try {tries})" : "")}...", LogType.RoomGeneration);
|
||||||
|
|
||||||
// create space
|
// create space
|
||||||
var space = new Space(room, entrance);
|
space = new Space(room, entrance);
|
||||||
|
|
||||||
// add exit
|
// add exit
|
||||||
var exitDoor = new DoorModule(space, theme.exitDoorTypes.RandomElement());
|
var exitDoor = new DoorModule(space, theme.exitDoorTypes.RandomElement());
|
||||||
if (!space.AddModuleWithRequirements(exitDoor))
|
if (!space.AddModuleWithRequirements(exitDoor))
|
||||||
throw new EngineException("Could not satisfy requirements for exit door.");
|
throw new EngineException("Could not satisfy requirements for exit door.");
|
||||||
var exit = new Passage(exitDoor);
|
exit = new Passage(exitDoor);
|
||||||
|
|
||||||
// add puzzles
|
// add puzzles
|
||||||
var puzzleCount = Utilities.Utilities.RandomInclusive(theme.puzzleCount.x, theme.puzzleCount.y);
|
var puzzleCount = Utilities.Utilities.RandomInclusive(theme.puzzleCount.x, theme.puzzleCount.y);
|
||||||
for (var i = 0; i < puzzleCount; i++)
|
for (var i = 0; i < puzzleCount; i++)
|
||||||
{
|
{
|
||||||
space.AddModuleWithRequirements(Module.CreateModuleByType(space, theme.puzzleTypes.RandomElement()));
|
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);
|
||||||
|
|||||||
@@ -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:
|
||||||
|
|||||||
Reference in New Issue
Block a user