Skip to content

Commit

Permalink
Fix key item issues
Browse files Browse the repository at this point in the history
Consider enemies 71/72 in Wreck - if they are allocated either of the first two keys, it's a softlock. While the room is valid en-route, the enemies' trigger is beyond the keyhole. For shuffled mode, it's too complicated to re-allocate, so we just disable key drops in that scenario. For normal mode, the triggers are taken into consideration.
  • Loading branch information
lahm86 committed Jun 17, 2024
1 parent 500b2d0 commit 3cdee22
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 21 deletions.
20 changes: 20 additions & 0 deletions TRLevelControl/Model/Common/FloorData/FDControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,26 @@ public void RemoveEntityTriggers(int entityIndex)
}
}

public List<short> GetTriggerRooms<R>(int entityIndex, List<R> rooms)
where R : TRRoom
{
List<FDTriggerEntry> triggers = GetEntityTriggers(entityIndex);
List<short> triggerRooms = new();
for (short i = 0; i < rooms.Count; i++)
{
foreach (TRRoomSector sector in rooms[i].Sectors.Where(s => s.FDIndex != 0))
{
if (triggers.Any(_entries[sector.FDIndex].Contains))
{
triggerRooms.Add(i);
break;
}
}
}

return triggerRooms;
}

public TRRoomSector GetRoomSector<R>(int x, int y, int z, short roomNumber, List<R> rooms)
where R : TRRoom
{
Expand Down
5 changes: 1 addition & 4 deletions TRRandomizerCore/Randomizers/Shared/ItemAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,7 @@ public void ApplyItemSwaps(string levelName, List<E> items)
}
}

if (!Settings.AllowEnemyKeyDrops)
{
ExcludeEnemyKeyDrops(items);
}
ExcludeEnemyKeyDrops(items);
}

protected List<E> GetPickups(string levelName, List<E> items, bool isUnarmed)
Expand Down
4 changes: 2 additions & 2 deletions TRRandomizerCore/Randomizers/Shared/LocationPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class LocationPicker : IRouteManager
private Random _generator;

public Func<Location, bool> TriggerTestAction { get; set; }
public Func<Location, bool, bool> KeyItemTestAction { get; set; }
public Func<Location, bool, List<short>, bool> KeyItemTestAction { get; set; }
public List<ExtRoomInfo> RoomInfos { get; set; }
public int LevelSize { get; private set; }

Expand Down Expand Up @@ -138,7 +138,7 @@ public Location GetKeyItemLocation<T>(int keyItemID, TREntity<T> entity, bool ha
continue;
}

if (KeyItemTestAction != null && !KeyItemTestAction(newLocation, hasPickupTrigger))
if (KeyItemTestAction != null && !KeyItemTestAction(newLocation, hasPickupTrigger, roomPool))
{
continue;
}
Expand Down
11 changes: 5 additions & 6 deletions TRRandomizerCore/Randomizers/TR1/Classic/TR1ItemRandomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,11 @@ public void FinalizeRandomization()
else
{
_allocator.RandomizeKeyItems(_levelInstance.Name, _levelInstance.Data, _levelInstance.Script.OriginalSequence);
}

if (Settings.AllowEnemyKeyDrops)
{
UpdateEnemyItemDrops(_levelInstance, _levelInstance.Data.Entities
.Where(e => TR1TypeUtilities.IsKeyItemType(e.TypeID)));
if (Settings.AllowEnemyKeyDrops)
{
UpdateEnemyItemDrops(_levelInstance, _levelInstance.Data.Entities
.Where(e => TR1TypeUtilities.IsKeyItemType(e.TypeID)));
}
}

SaveLevelInstance();
Expand Down
10 changes: 6 additions & 4 deletions TRRandomizerCore/Randomizers/TR2/Shared/TR2ItemAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private void InitialisePicker(string levelName, TR2Level level, LocationMode loc
? location => LocationUtilities.HasAnyTrigger(location, level)
: null;
_picker.KeyItemTestAction = locationMode == LocationMode.KeyItems
? (location, hasPickupTrigger) => TestKeyItemLocation(location, hasPickupTrigger, levelName, level)
? (location, hasPickupTrigger, roomPool) => TestKeyItemLocation(location, hasPickupTrigger, roomPool, levelName, level)
: null;
_picker.RoomInfos = new(level.Rooms.Select(r => new ExtRoomInfo(r)));

Expand All @@ -99,9 +99,10 @@ private void InitialisePicker(string levelName, TR2Level level, LocationMode loc
_picker.Initialise(levelName, pool, Settings, Generator);
}

private bool TestKeyItemLocation(Location location, bool hasPickupTrigger, string levelName, TR2Level level)
private bool TestKeyItemLocation(Location location, bool hasPickupTrigger, List<short> roomPool, string levelName, TR2Level level)
{
// Make sure if we're placing on the same tile as an enemy, that the enemy can drop the item.
// Make sure if we're placing on the same tile as an enemy, that the enemy can drop the item. Ensure too that the enemy
// can be triggered from within the key item's room pool and not beyond.
TR2Entity enemy = level.Entities
.FindAll(e => TR2TypeUtilities.IsEnemyType(e.TypeID))
.Find(e => e.GetLocation().IsEquivalent(location));
Expand All @@ -111,7 +112,8 @@ private bool TestKeyItemLocation(Location location, bool hasPickupTrigger, strin
TR2TypeUtilities.GetAliasForLevel(levelName, enemy.TypeID),
Settings.RandomizeEnemies && !Settings.ProtectMonks,
Settings.RandomizeEnemies && Settings.UnconditionalChickens
));
)
&& level.FloorData.GetTriggerRooms(level.Entities.IndexOf(enemy), level.Rooms).Any(roomPool.Contains));
}

private List<Location> GetItemLocationPool(string levelName, TR2Level level, bool keyItemMode)
Expand Down
7 changes: 4 additions & 3 deletions TRRandomizerCore/Randomizers/TR3/Shared/TR3ItemAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void InitialisePicker(string levelName, TR3Level level, LocationMode loc
? location => LocationUtilities.HasAnyTrigger(location, level)
: null;
_picker.KeyItemTestAction = locationMode == LocationMode.KeyItems
? (location, hasPickupTrigger) => TestKeyItemLocation(location, hasPickupTrigger, levelName, level)
? (location, hasPickupTrigger, roomPool) => TestKeyItemLocation(location, hasPickupTrigger, roomPool, levelName, level)
: null;
_picker.RoomInfos = new(level.Rooms.Select(r => new ExtRoomInfo(r)));

Expand All @@ -94,7 +94,7 @@ private void InitialisePicker(string levelName, TR3Level level, LocationMode loc
_picker.Initialise(levelName, pool, Settings, Generator);
}

private bool TestKeyItemLocation(Location location, bool hasPickupTrigger, string levelName, TR3Level level)
private bool TestKeyItemLocation(Location location, bool hasPickupTrigger, List<short> roomPool, string levelName, TR3Level level)
{
// Make sure if we're placing on the same tile as an enemy, that the enemy can drop the item.
TR3Entity enemy = level.Entities
Expand All @@ -105,7 +105,8 @@ private bool TestKeyItemLocation(Location location, bool hasPickupTrigger, strin
(
TR3TypeUtilities.GetAliasForLevel(levelName, enemy.TypeID),
!Settings.RandomizeEnemies || Settings.ProtectMonks
));
)
&& level.FloorData.GetTriggerRooms(level.Entities.IndexOf(enemy), level.Rooms).Any(roomPool.Contains));
}

private List<Location> GetItemLocationPool(string levelName, TR3Level level, bool keyItemMode, bool isCold)
Expand Down
4 changes: 2 additions & 2 deletions TRRandomizerView/Model/ControllerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ private void UpdateItemMode()
MaintainKeyContinuity.IsActive = defaultMode;

AllowReturnPathLocations.IsActive = !defaultMode || IncludeKeyItems.Value;
AllowEnemyKeyDrops.IsActive = !defaultMode || IncludeKeyItems.Value;
AllowEnemyKeyDrops.IsActive = defaultMode && IncludeKeyItems.Value;

FirePropertyChanged(nameof(WeaponDifficultyAvailable));
FirePropertyChanged(nameof(IncludeKeyItemsImplied));
Expand Down Expand Up @@ -3535,7 +3535,7 @@ private void IncludeKeyItems_PropertyChanged(object sender, PropertyChangedEvent
{
bool defaultMode = ItemMode == ItemMode.Default;
AllowReturnPathLocations.IsActive = !defaultMode || IncludeKeyItems.Value;
AllowEnemyKeyDrops.IsActive = !defaultMode || IncludeKeyItems.Value;
AllowEnemyKeyDrops.IsActive = defaultMode && IncludeKeyItems.Value;
FirePropertyChanged(nameof(IncludeKeyItemsImplied));
}

Expand Down

0 comments on commit 3cdee22

Please sign in to comment.