Skip to content

Commit

Permalink
Merge pull request #151 from lahm86/master
Browse files Browse the repository at this point in the history
#146, #149 Dragon triggers and entity limits
  • Loading branch information
DanzaG authored Jun 6, 2021
2 parents 5069184 + 4ece1f8 commit 3e6086f
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 13 deletions.
8 changes: 8 additions & 0 deletions TR2RandomizerCore/Randomizers/EnemyRandomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,9 @@ private void RandomizeEnemies(TR2CombinedLevel level, EnemyRandomizationCollecti

targetEntity.TypeID = (short)TR2EntityUtilities.TranslateEntityAlias(entity);

// #146 Ensure OneShot triggers are set for this enemy if needed
EnemyUtilities.SetEntityTriggers(level.Data, targetEntity);

// Remove the target entity so it doesn't get replaced
enemyEntities.Remove(targetEntity);
}
Expand Down Expand Up @@ -565,6 +568,11 @@ private void RandomizeEnemies(TR2CombinedLevel level, EnemyRandomizationCollecti

// Make sure to convert BengalTiger, StickWieldingGoonBandana etc back to their actual types
currentEntity.TypeID = (short)TR2EntityUtilities.TranslateEntityAlias(newEntityType);

// #146 Ensure OneShot triggers are set for this enemy if needed. This currently only applies
// to the dragon, which will be handled above in defined rooms, but the check should be made
// here in case this needs to be extended later.
EnemyUtilities.SetEntityTriggers(level.Data, currentEntity);
}

// MercSnowMobDriver relies on RedSnowmobile so it will be available in the model list
Expand Down
24 changes: 14 additions & 10 deletions TR2RandomizerCore/Randomizers/ItemRandomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ private void FindUnarmedPistolsLocation()
{TR2Entities.Shotgun_S_P, 8},
{TR2Entities.Automags_S_P, 4},
{TR2Entities.Uzi_S_P, 4},
{TR2Entities.Harpoon_S_P, 24},
{TR2Entities.Harpoon_S_P, 4}, // #149 Agreed that a low number of harpoons will be given for unarmed levels, but pistols will also be included
{TR2Entities.M16_S_P, 2},
{TR2Entities.GrenadeLauncher_S_P, 4},
};
Expand Down Expand Up @@ -451,8 +451,9 @@ private void RandomizeORPistol()
if (Weap != TR2Entities.Pistols_S_P)
{
// If we haven't decided to add the pistols (i.e. for enemy difficulty)
// add a 1/3 chance of getting them anyway.
if (addPistols || _generator.Next(0, 3) == 0)
// add a 1/3 chance of getting them anyway. #149 If the harpoon is being
// given, the pistols will be included.
if (addPistols || Weap == TR2Entities.Harpoon_S_P || _generator.Next(0, 3) == 0)
{
CopyEntity(unarmedLevelWeapons, TR2Entities.Pistols_S_P);
}
Expand All @@ -472,11 +473,14 @@ private void RandomizeORPistol()
private void CopyEntity(TR2Entity entity, TR2Entities newType)
{
List<TR2Entity> ents = _levelInstance.Data.Entities.ToList();
TR2Entity copy = entity.Clone();
copy.TypeID = (short)newType;
ents.Add(copy);
_levelInstance.Data.NumEntities++;
_levelInstance.Data.Entities = ents.ToArray();
if (ents.Count < 256)
{
TR2Entity copy = entity.Clone();
copy.TypeID = (short)newType;
ents.Add(copy);
_levelInstance.Data.NumEntities++;
_levelInstance.Data.Entities = ents.ToArray();
}
}

private TR2Entities GetWeaponAmmo(TR2Entities weapon)
Expand Down Expand Up @@ -504,7 +508,7 @@ private void AddORAmmo(TR2Entities ammoType, uint count, TR2Entity weapon)
{
List<TR2Entity> ents = _levelInstance.Data.Entities.ToList();

for (uint i = 0; i < count; i++)
for (uint i = 0; i < count && ents.Count < 256; i++)
{
TR2Entity ammo = weapon.Clone();

Expand All @@ -513,7 +517,7 @@ private void AddORAmmo(TR2Entities ammoType, uint count, TR2Entity weapon)
ents.Add(ammo);
};

_levelInstance.Data.NumEntities += count;
_levelInstance.Data.NumEntities = (uint)ents.Count;
_levelInstance.Data.Entities = ents.ToArray();
}

Expand Down
2 changes: 1 addition & 1 deletion TR2RandomizerCore/Resources/Models/MarcoBartoli/Data.json

Large diffs are not rendered by default.

Binary file modified TR2RandomizerCore/Resources/Models/MarcoBartoli/Segments.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions TR2RandomizerCore/Resources/enemy_restrictions.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"WALL.TR2": {
"40": [
2,
21,
63,
79
],
"52": [
Expand All @@ -26,7 +26,7 @@
},
"OPERA.TR2": {
"40": [
2,
12,
72
],
"52": [
Expand Down
4 changes: 4 additions & 0 deletions TR2RandomizerCore/TR2RandomizerCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
<Compile Include="Zones\LevelZones.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TRFDControl\TRFDControl.csproj">
<Project>{09ABCF27-6777-4866-8740-3EAD3F6B86FC}</Project>
<Name>TRFDControl</Name>
</ProjectReference>
<ProjectReference Include="..\TRLevelReader\TRLevelReader.csproj">
<Project>{226c4a3a-4fbb-4c15-aed4-c9248eb3a144}</Project>
<Name>TRLevelReader</Name>
Expand Down
28 changes: 28 additions & 0 deletions TR2RandomizerCore/Utilities/EnemyUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using System.IO;
using System.Linq;
using TR2RandomizerCore.Helpers;
using TRFDControl;
using TRFDControl.FDEntryTypes;
using TRFDControl.Utilities;
using TRLevelReader.Helpers;
using TRLevelReader.Model;
using TRLevelReader.Model.Enums;
Expand Down Expand Up @@ -361,5 +364,30 @@ public static List<TR2Entities> GetEnemyGuisers(TR2Entities entity)
TR2Entities.MonkWithKnifeStick, TR2Entities.MonkWithLongStick
}
};

// #146 Ensure Marco is spawned only once
private static readonly List<TR2Entities> _oneShotEnemies = new List<TR2Entities>
{
TR2Entities.MarcoBartoli
};

public static void SetEntityTriggers(TR2Level level, TR2Entity entity)
{
if (_oneShotEnemies.Contains((TR2Entities)entity.TypeID))
{
int entityID = level.Entities.ToList().IndexOf(entity);

FDControl fdControl = new FDControl();
fdControl.ParseFromLevel(level);

List<FDTriggerEntry> triggers = FDUtilities.GetEntityTriggers(fdControl, entityID);
foreach (FDTriggerEntry trigger in triggers)
{
trigger.TrigSetup.SetOneShot();
}

fdControl.WriteToLevel(level);
}
}
}
}
1 change: 1 addition & 0 deletions TRFDControl/TRFDControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Compile Include="FDTrigSetup.cs" />
<Compile Include="FDTrigType.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utilities\FDUtilities.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TRLevelReader\TRLevelReader.csproj">
Expand Down
34 changes: 34 additions & 0 deletions TRFDControl/Utilities/FDUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Generic;
using TRFDControl.FDEntryTypes;

namespace TRFDControl.Utilities
{
public static class FDUtilities
{
public static List<FDTriggerEntry> GetEntityTriggers(FDControl control, int entityIndex)
{
List<FDTriggerEntry> entries = new List<FDTriggerEntry>();

foreach (List<FDEntry> entryList in control.Entries.Values)
{
foreach (FDEntry entry in entryList)
{
if (entry is FDTriggerEntry triggerEntry)
{
int itemIndex = triggerEntry.TrigActionList.FindIndex
(
i =>
i.TrigAction == FDTrigAction.Object && i.Parameter == entityIndex
);
if (itemIndex != -1)
{
entries.Add(triggerEntry);
}
}
}
}

return entries;
}
}
}
74 changes: 74 additions & 0 deletions TRLevelReaderUnitTests/TR2Level_UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

using TRFDControl;
using TRFDControl.FDEntryTypes;
using TRFDControl.Utilities;

namespace TRLevelReaderUnitTests
{
Expand Down Expand Up @@ -474,5 +475,78 @@ public void FloorData_ReadWriteTest()

Assert.IsTrue(isPortal);
}

[TestMethod]
public void FloorData_ReadWriteOneShotTest()
{
//Read GW data
TR2LevelReader reader = new TR2LevelReader();
TR2Level lvl = reader.ReadLevel("wall.tr2");

//Parse the floordata using FDControl
FDControl fdataReader = new FDControl();
fdataReader.ParseFromLevel(lvl);

//Get all triggers for entity ID 18
List<FDTriggerEntry> triggers = FDUtilities.GetEntityTriggers(fdataReader, 18);

//There should be 3
Assert.AreEqual(triggers.Count, 3);

//Verify none of the triggers has OneShot set
foreach (FDTriggerEntry trigger in triggers)
{
Assert.IsFalse(trigger.TrigSetup.OneShot);
}

//Set OneShot on each trigger
foreach (FDTriggerEntry trigger in triggers)
{
trigger.TrigSetup.SetOneShot();
}

fdataReader.WriteToLevel(lvl);

//Save it and read it back in
TR2LevelWriter writer = new TR2LevelWriter();
writer.WriteLevelToFile(lvl, "TEST.tr2");
lvl = reader.ReadLevel("TEST.tr2");

fdataReader = new FDControl();
fdataReader.ParseFromLevel(lvl);

//Get the triggers again afresh
triggers = FDUtilities.GetEntityTriggers(fdataReader, 18);

//Verify that they now have OneShot set
foreach (FDTriggerEntry trigger in triggers)
{
Assert.IsTrue(trigger.TrigSetup.OneShot);
}

//Switch it off again
foreach (FDTriggerEntry trigger in triggers)
{
trigger.TrigSetup.ClearOneShot();
}

fdataReader.WriteToLevel(lvl);

//Save it and read it back in
writer.WriteLevelToFile(lvl, "TEST.tr2");
lvl = reader.ReadLevel("TEST.tr2");

fdataReader = new FDControl();
fdataReader.ParseFromLevel(lvl);

//Get the triggers again afresh
triggers = FDUtilities.GetEntityTriggers(fdataReader, 18);

//Verify that they now once again do not have OneShot set
foreach (FDTriggerEntry trigger in triggers)
{
Assert.IsFalse(trigger.TrigSetup.OneShot);
}
}
}
}
1 change: 1 addition & 0 deletions TRModelTransporter/Handlers/TextureTransportHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class TextureTransportHandler : AbstractTransportHandler
private static readonly Dictionary<TR2Entities, List<TR2Entities>> _entitySpriteDependencies = new Dictionary<TR2Entities, List<TR2Entities>>
{
[TR2Entities.FlamethrowerGoon] = new List<TR2Entities> { TR2Entities.Flame_S_H },
[TR2Entities.MarcoBartoli] = new List<TR2Entities> { TR2Entities.Flame_S_H },
[TR2Entities.RedSnowmobile] = new List<TR2Entities> { TR2Entities.SnowmobileWake_S_H },
[TR2Entities.XianGuardSword] = new List<TR2Entities> { TR2Entities.XianGuardSparkles_S_H }
};
Expand Down

0 comments on commit 3e6086f

Please sign in to comment.