Skip to content

Commit

Permalink
Add TR3R reward support
Browse files Browse the repository at this point in the history
Shared logic for TR3 separated out.
  • Loading branch information
lahm86 committed May 21, 2024
1 parent d92b322 commit bed56b4
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 32 deletions.
19 changes: 19 additions & 0 deletions TRRandomizerCore/Editors/TR3RemasteredEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ protected override int GetSaveTarget(int numLevels)
}
}

if (Settings.RandomizeSecretRewardsPhysical)
{
target += numLevels;
}

if (Settings.RandomizeStartPosition)
{
target += numLevels;
Expand Down Expand Up @@ -120,6 +125,20 @@ protected override void SaveImpl(AbstractTRScriptEditor scriptEditor, TRSaveMoni
itemRandomizer.Randomize(Settings.ItemSeed);
}

if (!monitor.IsCancelled && Settings.RandomizeSecretRewardsPhysical)
{
monitor.FireSaveStateBeginning(TRSaveCategory.Custom, "Randomizing secret rewards");
new TR3RSecretRewardRandomizer
{
ScriptEditor = scriptEditor,
Levels = levels,
BasePath = wipDirectory,
BackupPath = backupDirectory,
SaveMonitor = monitor,
Settings = Settings
}.Randomize(Settings.SecretRewardsPhysicalSeed);
}

if (!monitor.IsCancelled && Settings.RandomizeStartPosition)
{
monitor.FireSaveStateBeginning(TRSaveCategory.Custom, "Randomizing start positions");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using TRGE.Core;
using TRLevelControl.Helpers;
using TRLevelControl.Model;
using TRRandomizerCore.Levels;
using TRRandomizerCore.Secrets;

namespace TRRandomizerCore.Randomizers;
Expand All @@ -10,45 +7,26 @@ public class TR3SecretRewardRandomizer : BaseTR3Randomizer
{
public override void Randomize(int seed)
{
_generator = new Random(seed);
_generator = new(seed);
TR3SecretRewardAllocator allocator = new()
{
Generator = _generator
};

foreach (TR3ScriptedLevel lvl in Levels)
{
LoadLevelInstance(lvl);

RandomizeRewards(_levelInstance);
TR3SecretMapping mapping = TR3SecretMapping.Get(_levelInstance);
if (mapping != null)
{
allocator.RandomizeRewards(_levelInstance.Data, mapping.RewardEntities);
}

SaveLevelInstance();

if (!TriggerProgress())
{
break;
}
}
}

private void RandomizeRewards(TR3CombinedLevel level)
{
if (level.IsAssault)
{
return;
}

TR3SecretMapping secretMapping = TR3SecretMapping.Get(level);

List<TR3Type> stdItemTypes = TR3TypeUtilities.GetStandardPickupTypes();
// A bit cruel as rewards?
stdItemTypes.Remove(TR3Type.PistolAmmo_P);
stdItemTypes.Remove(TR3Type.Pistols_P);

foreach (int rewardIndex in secretMapping.RewardEntities)
{
if (level.Data.Entities[rewardIndex].TypeID == TR3Type.SaveCrystal_P)
{
continue;
}

level.Data.Entities[rewardIndex].TypeID = stdItemTypes[_generator.Next(0, stdItemTypes.Count)];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using TRGE.Core;
using TRLevelControl.Model;
using TRRandomizerCore.Secrets;

namespace TRRandomizerCore.Randomizers;

public class TR3RSecretRewardRandomizer : BaseTR3RRandomizer
{
public override void Randomize(int seed)
{
_generator = new(seed);
TR3SecretRewardAllocator allocator = new()
{
Generator = _generator
};

foreach (TRRScriptedLevel lvl in Levels)
{
LoadLevelInstance(lvl);
TRSecretMapping<TR3Entity> mapping = TRSecretMapping<TR3Entity>.Get($@"Resources\TR3\SecretMapping\{_levelInstance.Name}-SecretMapping.json");
if (mapping != null)
{
allocator.RandomizeRewards(_levelInstance.Data, mapping.RewardEntities);
}

SaveLevelInstance();
if (!TriggerProgress())
{
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using TRLevelControl.Helpers;
using TRLevelControl.Model;

namespace TRRandomizerCore.Randomizers;

public class TR3SecretRewardAllocator
{
public Random Generator { get; set; }

public void RandomizeRewards(TR3Level level, List<int> rewardEntities)
{
List<TR3Type> stdItemTypes = TR3TypeUtilities.GetStandardPickupTypes();
stdItemTypes.Remove(TR3Type.PistolAmmo_P);
stdItemTypes.Remove(TR3Type.Pistols_P);

foreach (int rewardIndex in rewardEntities)
{
if (level.Entities[rewardIndex].TypeID == TR3Type.SaveCrystal_P)
{
continue;
}

level.Entities[rewardIndex].TypeID = stdItemTypes[Generator.Next(0, stdItemTypes.Count)];
}
}
}
1 change: 1 addition & 0 deletions TRRandomizerCore/TRVersionSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ internal class TRVersionSupport
TRRandomizerType.KeyItems,
TRRandomizerType.Secret,
TRRandomizerType.SecretAudio,
TRRandomizerType.SecretReward,
TRRandomizerType.SFX,
TRRandomizerType.StartPosition,
};
Expand Down

0 comments on commit bed56b4

Please sign in to comment.