Increased desired chance of success for lock population

This commit is contained in:
Max 2025-02-18 11:20:54 +01:00
parent 03f0f7859e
commit fd1700d650
13 changed files with 34 additions and 15 deletions

View file

@ -7,11 +7,11 @@ namespace DungeonMapGenerator
{
public static class DungeonLockPopulator
{
private const float ACCEPTABLE_ERROR = .1319f;
private const float ACCEPTABLE_ERROR = .02f;
public static void PopulateLocksOfDungeon(DungeonMap dungeon)
{
Random random = new Random();
float desiredSuccessChance = .80f;
float desiredSuccessChance = .995f;
List<Room> currentRooms = dungeon.GetEntranceRooms();
HashSet<Room> seenRooms = new HashSet<Room>(currentRooms);
@ -21,12 +21,14 @@ namespace DungeonMapGenerator
int entranceRoomLock = 1;
foreach (Room entranceRoom in currentRooms)
{
entranceRoom.Lock = new Lock((entranceRoomLock + 1.ToString()));
entranceRoomLock += 1;
entranceRoom.Lock = new Lock((entranceRoomLock.ToString()));
}
while (currentRooms.Count > 0)
{
List<Room> locklessRooms = new List<Room>();
List<Room> alreadyLockedRooms = new List<Room>();
List<Room> adjacentRooms = new List<Room>();
// Get all adjacent rooms that haven't been locked at
@ -42,24 +44,30 @@ namespace DungeonMapGenerator
}
}
// Get the odds of locks already set.
// Sort locked and not locked rooms
foreach (Room adjacentRoom in adjacentRooms.ToList())
{
List<float> odds = new List<float>();
if (adjacentRoom.Lock != null)
{
odds.Add(adjacentRoom.Lock.GetOdds());
alreadyLockedRooms.Add(adjacentRoom);
}
else
{
locklessRooms.Add(adjacentRoom);
}
}
// Get locks of locked rooms
List<Lock> pregeneratedLocks = new List<Lock>();
foreach (Room room in alreadyLockedRooms)
{
pregeneratedLocks.Add(room.Lock);
}
if (locklessRooms.Count > 0)
{
// Add locks to the rooms with odds adding to within the difficulty range
List<Lock> locks = GenerateLocks(locklessRooms.Count, desiredSuccessChance);
List<Lock> locks = GenerateLocks(pregeneratedLocks, locklessRooms.Count, desiredSuccessChance);
for (int i = 0; i < locklessRooms.Count; i++)
{
locklessRooms[i].Lock = locks[i];
@ -70,10 +78,10 @@ namespace DungeonMapGenerator
}
}
private static List<Lock> GenerateLocks(int numLocks, float successChance)
private static List<Lock> GenerateLocks(List<Lock> pregeneratedLocks, int numLocks, float successChance)
{
List<Lock> locks = new List<Lock>();
if (FindCombination(locks, numLocks, successChance, Lock.PossibleLocks))
if (FindCombination(pregeneratedLocks, locks, numLocks, successChance, Lock.PossibleLocks))
{
return locks;
}
@ -81,11 +89,13 @@ namespace DungeonMapGenerator
return null;
}
private static bool FindCombination(List<Lock> generatedLocks, int numLocks, float successChance, List<Lock> lockOptions)
private static bool FindCombination(List<Lock> pregeneratedLocks, List<Lock> generatedLocks, int numLocks, float successChance, List<Lock> lockOptions)
{
if (generatedLocks.Count == numLocks)
{
List<string> locksAsStrings = generatedLocks.Select(_lock => _lock.GetLock()).ToList();
List<Lock> allLocks = new List<Lock>(pregeneratedLocks);
allLocks.AddRange(generatedLocks);
List<string> locksAsStrings = allLocks.Select(_lock => _lock.GetLock()).ToList();
// Return true if given the current locks the chance of being able to open one is close to difficulty
float probalityOfAtleastOneUnlocking = RollFourSumTwo.GetProbalityOfAtleastOneUnlocking(locksAsStrings);
@ -94,7 +104,7 @@ namespace DungeonMapGenerator
}
Shuffle(lockOptions);
foreach (Lock _lock in lockOptions.ToList())
{
// if adding the lock won't bring us over the threshold difficulty.
@ -103,7 +113,7 @@ namespace DungeonMapGenerator
if (RollFourSumTwo.GetProbalityOfAtleastOneUnlocking(locksAsStrings) <= successChance)
{
generatedLocks.Add(new Lock(_lock.GetLock()));
if (FindCombination(generatedLocks, numLocks, successChance, lockOptions))
if (FindCombination(pregeneratedLocks, generatedLocks, numLocks, successChance, lockOptions))
{
return true;
}