@@ -15,6 +15,9 @@ namespace SubnauticaRandomiser.Patches
1515 [ HarmonyPatch ]
1616 internal static class EntitySlotsTrackerPatcher
1717 {
18+ private const float AdjustedSlotMult = 1.25f ;
19+ private const float SlotEmergencyThreshold = 0.95f ;
20+
1821 private static EntitySlotsTrackerSaveData _saveData ;
1922 private static ILogHandler _log = PrefixLogHandler . Get ( "[SlotTrackerPatcher]" ) ;
2023 private static IRandomHandler _rng = new RandomHandler ( ) ;
@@ -127,17 +130,25 @@ private static EntitySlot.Filler GetForcedSpawn(BiomeType biome, EntitySlotData.
127130 return default ;
128131 // Pretend that more slots have spawned than really did so that we reach guaranteed 100% of entities spawned
129132 // *before* we run out of slots to do so.
130- slotProgress *= 1.25f ; // Works out to 100% fragment saturation after 80% of slots have been processed.
131-
132- if ( slotProgress < entityCounts . NextCheckThreshold )
133+ var adjustedProgress = slotProgress * AdjustedSlotMult ;
134+ if ( adjustedProgress < entityCounts . NextCheckThreshold )
133135 return default ;
134136
135- var techType = entityCounts . GetForcedSpawn ( slotProgress ) ;
137+ var techType = entityCounts . GetForcedSpawn ( adjustedProgress ) ;
136138 if ( techType == TechType . None )
137139 return default ;
138140
139141 _log . Debug ( $ "Forcing spawn of '{ techType } ' in biome '{ biome } '") ;
140- return new EntitySlot . Filler { classId = _saveData . Spawnables [ techType ] . GetRandomPrefab ( _rng ) , count = 1 } ;
142+ int spawnCount = 1 ;
143+ // If almost no slots are left do an emergency spawn where several entities are layered on top of each
144+ // other. It's not pretty, but it prevents getting stuck / softlocked.
145+ if ( slotProgress >= SlotEmergencyThreshold )
146+ {
147+ spawnCount = entityCounts . GetNumMissingSpawns ( techType ) ;
148+ _log . Warn ( $ "Forcing emergency spawn of { spawnCount } { techType } in { biome } !") ;
149+ }
150+
151+ return new EntitySlot . Filler { classId = _saveData . Spawnables [ techType ] . GetRandomPrefab ( _rng ) , count = spawnCount } ;
141152 }
142153 }
143154}
0 commit comments