Skip to content

Commit 54dd939

Browse files
committed
Try filling shapes with rooms a few times before giving up
1 parent 4cee4f7 commit 54dd939

1 file changed

Lines changed: 25 additions & 10 deletions

File tree

RandomizerCore/Sidescroll/ShapeFirstCoordinatePalaceGenerator.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace Z2Randomizer.RandomizerCore.Sidescroll;
1212

1313
public abstract class ShapeFirstCoordinatePalaceGenerator() : CoordinatePalaceGenerator()
1414
{
15+
public const int FILL_SHAPE_TRIES = 10;
1516

1617
internal override async Task<Palace> GeneratePalace(RandomizerProperties props, RoomPool rooms, Random r, int roomCount, int palaceNumber)
1718
{
@@ -72,6 +73,22 @@ internal override async Task<Palace> GeneratePalace(RandomizerProperties props,
7273
}
7374

7475
//Add rooms
76+
bool success = false;
77+
for (int i = 0; i < FILL_SHAPE_TRIES; i++)
78+
{
79+
if (await FillShape())
80+
{
81+
success = true;
82+
break;
83+
}
84+
}
85+
if (!success)
86+
{
87+
palace.IsValid = false;
88+
return palace;
89+
}
90+
async Task<bool> FillShape()
91+
{
7592
roomsByExitType = roomPool.CategorizeNormalRoomExits(true);
7693
Dictionary<RoomExitType, bool> stubOnlyExitTypes = new();
7794
foreach (KeyValuePair<Coord, RoomExitType> item in shape.OrderBy(i => i.Key.X).ThenByDescending(i => i.Key.Y))
@@ -128,14 +145,12 @@ internal override async Task<Palace> GeneratePalace(RandomizerProperties props,
128145
{
129146
//We need to use a drop zone stub but one does not (and cannot) exist so this graph is doomed.
130147
//Debug.WriteLine(GetLayoutDebug(walkGraph, false));
131-
palace.IsValid = false;
132-
return palace;
148+
return false;
133149
}
134150
}
135151
if (newRoom == null)
136152
{
137-
palace.IsValid = false;
138-
return palace;
153+
return false;
139154
}
140155
else
141156
{
@@ -214,15 +229,13 @@ internal override async Task<Palace> GeneratePalace(RandomizerProperties props,
214229
//unreachable because up was the only way to get there.
215230
if (!palace.AllReachable())
216231
{
217-
palace.IsValid = false;
218-
return palace;
232+
return false;
219233
}
220234

221235

222236
if (!AddSpecialRoomsByReplacement(palace, roomPool, r, props, itemRoomSelector))
223237
{
224-
palace.IsValid = false;
225-
return palace;
238+
return false;
226239
}
227240

228241
if (palace.AllRooms.Count(i => i.Enabled) != roomCount)
@@ -232,8 +245,10 @@ internal override async Task<Palace> GeneratePalace(RandomizerProperties props,
232245

233246
if (palace.HasDisallowedDrop(props.BossRoomsExitToPalace[palace.Number - 1], props.PalaceDropStyle, r))
234247
{
235-
palace.IsValid = false;
236-
return palace;
248+
return false;
249+
}
250+
251+
return true;
237252
}
238253

239254
palace.AllRooms.ForEach(i => i.PalaceNumber = palaceNumber);

0 commit comments

Comments
 (0)