Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TR2 secret issues #730

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
- fixed docile bird monsters causing multiple Laras to spawn in remastered levels (#723)
- fixed the incomplete skidoo model in TR2R when it appears anywhere other than Tibetan Foothills (#721)
- fixed secrets on triangle portals not triggering in TR3 (#727)
- fixed secrets in 40 Fathoms all generally appearing too close to the start of the level (#729)
- fixed the Jade secret appearing before the Stone in TR2R Floating Islands (#729)
- improved data integrity checks when opening a folder and prior to randomization (#719)

## [V1.9.1](https://github.com/LostArtefacts/TR-Rando/compare/V1.9.0...V1.9.1) - 2024-06-23
Expand Down
11 changes: 11 additions & 0 deletions TRRandomizerCore/Randomizers/Shared/LocationPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,17 @@ public int GetRoutePosition(Location location)
return _currentRoute.FindIndex(l => l.Room == location.Room);
}

public List<short> GetDemarkedZone(int zoneID)
{
List<short> rooms = new();
int demarker = _currentRoute.FindIndex(l => l.TargetType == zoneID);
if (demarker != -1)
{
rooms.AddRange(_currentRoute.GetRange(0, demarker).Select(l => l.Room));
}
return rooms;
}

public List<short> GetRouteRooms()
{
return _currentRoute
Expand Down
6 changes: 6 additions & 0 deletions TRRandomizerCore/Randomizers/Shared/SecretPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class SecretPicker<T>
public IRouteManager RouteManager { get; set; }
public Func<Location, TRRoomSector> SectorAction { get; set; }
public Func<Location, bool> PlacementTestAction { get; set; }
public Func<Location, List<Location>, bool> ProximityTestAction { get; set; }

public List<Location> GetLocations(List<Location> allLocations, bool isMirrored, int totalCount)
{
Expand Down Expand Up @@ -184,6 +185,11 @@ public bool TestLocation(Location location, List<Location> usedLocations, bool i
return true;
}

if (!ProximityTestAction?.Invoke(location, usedLocations) ?? false)
{
return false;
}

_proxEvaluationCount++;
if (_retryTolerances.Contains(_proxEvaluationCount) || _proxEvaluationCount > _retryTolerances.Max())
{
Expand Down
22 changes: 22 additions & 0 deletions TRRandomizerCore/Randomizers/TR2/Classic/TR2SecretRandomizer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using TRGE.Core;
using TRLevelControl.Helpers;
using TRLevelControl.Model;
using TRRandomizerCore.Helpers;
using TRRandomizerCore.Levels;
Expand All @@ -8,6 +9,13 @@ namespace TRRandomizerCore.Randomizers;

public class TR2SecretRandomizer : BaseTR2Randomizer
{
private static readonly List<string> _textureFixLevels = new()
{
TR2LevelNames.FLOATER,
TR2LevelNames.LAIR,
TR2LevelNames.HOME
};

public IMirrorControl Mirrorer { get; set; }
public ItemFactory<TR2Entity> ItemFactory { get; set; }

Expand Down Expand Up @@ -35,6 +43,8 @@ public override void Randomize(int seed)
AddDamageControl(_levelInstance, pickedLocations);
}

FixSecretTextures(_levelInstance.Name, _levelInstance.Data);

SaveLevelInstance();
if (!TriggerProgress())
{
Expand Down Expand Up @@ -79,4 +89,16 @@ private static void AddDamageControl(TR2CombinedLevel level, List<Location> loca
level.Script.AddStartInventoryItem(ItemUtilities.ConvertToScriptItem(TR2Type.LargeMed_S_P), hardDamageCount);
level.Script.AddStartInventoryItem(ItemUtilities.ConvertToScriptItem(TR2Type.SmallMed_S_P), easyDamageCount);
}

private static void FixSecretTextures(string levelName, TR2Level level)
{
if (!_textureFixLevels.Contains(levelName))
{
return;
}

// Swap Stone and Jade textures - OG has them the wrong way around.
(level.Sprites[TR2Type.JadeSecret_S_P].Textures, level.Sprites[TR2Type.StoneSecret_S_P].Textures)
= (level.Sprites[TR2Type.StoneSecret_S_P].Textures, level.Sprites[TR2Type.JadeSecret_S_P].Textures);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Newtonsoft.Json;
using TRGE.Core;
using TRLevelControl.Helpers;
using TRLevelControl.Model;
using TRRandomizerCore.Helpers;
using TRRandomizerCore.Levels;
Expand Down Expand Up @@ -38,6 +39,7 @@ public override void Randomize(int seed)
{
List<Location> pickedLocations = allocator.RandomizeSecrets(_levelInstance.Name, _levelInstance.Data);
AddDamageControl(_levelInstance, pickedLocations);
FixSecretOrder(_levelInstance);
}

SaveLevelInstance();
Expand All @@ -60,4 +62,20 @@ private void AddDamageControl(TR2RCombinedLevel level, List<Location> locations)
medi.TypeID = TR2Type.LargeMed_S_P;
}
}

private static void FixSecretOrder(TR2RCombinedLevel level)
{
if (!level.Is(TR2LevelNames.FLOATER))
{
return;
}

TR2Entity stone = level.Data.Entities.Find(e => e.TypeID == TR2Type.StoneSecret_S_P);
TR2Entity jade = level.Data.Entities.Find(e => e.TypeID == TR2Type.JadeSecret_S_P);
if (stone != null && jade != null)
{
stone.TypeID = TR2Type.JadeSecret_S_P;
jade.TypeID = TR2Type.StoneSecret_S_P;
}
}
}
32 changes: 8 additions & 24 deletions TRRandomizerCore/Randomizers/TR2/Shared/TR2SecretAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ public class TR2SecretAllocator : ISecretRandomizer
{
private static readonly int _levelSecretCount = 3;

private static readonly List<string> _textureFixLevels = new()
{
TR2LevelNames.FLOATER,
TR2LevelNames.LAIR,
TR2LevelNames.HOME
};

private readonly Dictionary<string, List<Location>> _locations;
private readonly LocationPicker _routePicker;
private SecretPicker<TR2Entity> _secretPicker;
Expand Down Expand Up @@ -75,8 +68,6 @@ public void PlaceAllSecrets(string levelName, TR2Level level)
int zone = zones.FindIndex(z => z.Contains(location.Room));
PlaceSecret(entity, secretTypes[zone], location);
}

FixSecretTextures(levelName, level);
}

public List<Location> RandomizeSecrets(string levelName, TR2Level level)
Expand All @@ -99,6 +90,7 @@ public List<Location> RandomizeSecrets(string levelName, TR2Level level)
locations.Shuffle(Generator);

_secretPicker.SectorAction = loc => level.GetRoomSector(loc);
_secretPicker.ProximityTestAction = (loc, usedLocs) => TestSecretPlacement(loc, usedLocs);
_routePicker.RoomInfos = new(level.Rooms.Select(r => new ExtRoomInfo(r)));

_routePicker.Initialise(levelName, locations, Settings, Generator);
Expand All @@ -117,9 +109,6 @@ public List<Location> RandomizeSecrets(string levelName, TR2Level level)
}

_secretPicker.FinaliseSecretPool(pickedLocations, levelName, itemIndex => GetDependentLockedItems(level, itemIndex));

FixSecretTextures(levelName, level);

return pickedLocations;
}

Expand All @@ -141,6 +130,13 @@ private static List<int> GetDependentLockedItems(TR2Level level, int itemIndex)
return items;
}

private bool TestSecretPlacement(Location location, List<Location> usedLocations)
{
// This is in effect a hard-coded zone for 40F because the starting area has too much weight.
List<short> stoneRooms = _routePicker.GetDemarkedZone((int)TR2Type.StoneSecret_S_P);
return !stoneRooms.Contains(location.Room) || !usedLocations.Any(l => stoneRooms.Contains(l.Room));
}

private void PlaceSecret(TR2Entity entity, TR2Type type, Location location)
{
_routePicker.SetLocation(entity, location);
Expand All @@ -149,16 +145,4 @@ private void PlaceSecret(TR2Entity entity, TR2Type type, Location location)
entity.Intensity2 = -1;
entity.Flags = 0;
}

private static void FixSecretTextures(string levelName, TR2Level level)
{
if (!_textureFixLevels.Contains(levelName))
{
return;
}

// Swap Stone and Jade textures - OG has them the wrong way around.
(level.Sprites[TR2Type.JadeSecret_S_P].Textures, level.Sprites[TR2Type.StoneSecret_S_P].Textures)
= (level.Sprites[TR2Type.StoneSecret_S_P].Textures, level.Sprites[TR2Type.JadeSecret_S_P].Textures);
}
}
3 changes: 2 additions & 1 deletion TRRandomizerCore/Resources/TR2/Locations/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -5219,7 +5219,8 @@
"X": 66048,
"Y": -3584,
"Z": 75264,
"Room": 21
"Room": 21,
"TargetType": 192
},
{
"X": 67072,
Expand Down
Loading