Skip to content

Commit

Permalink
Refactor floor data control (LostArtefacts#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahm86 authored May 2, 2024
1 parent 9d5c39b commit e7159ef
Show file tree
Hide file tree
Showing 170 changed files with 4,288 additions and 4,523 deletions.
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,3 @@ MigrationBackup/

# Ionide (cross platform F# VS Code tools) working folder
.ionide/
*.TR2
/TRMOD
/Item Locations
/TR2Randomizer/TexturePacks
Binary file modified Deps/TRGE.Coord.dll
Binary file not shown.
17 changes: 3 additions & 14 deletions LocationExport/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Newtonsoft.Json;
using TRFDControl;
using TRFDControl.Utilities;
using TRLevelControl;
using TRLevelControl.Helpers;
using TRLevelControl.Model;
Expand Down Expand Up @@ -163,8 +161,6 @@ private static TRFileVersion DetectVersion(string path)
private static List<Location> ExportTR1Locations(string lvl)
{
TR1Level level = _reader1.Read(lvl);
FDControl floorData = new();
floorData.ParseFromLevel(level);
List<Location> exclusions = new();
if (_allTR1Exclusions.ContainsKey(lvl))
{
Expand All @@ -175,8 +171,7 @@ private static List<Location> ExportTR1Locations(string lvl)
{
if (!TR1TypeUtilities.CanSharePickupSpace(entity.TypeID))
{
exclusions.Add(entity.GetFloorLocation(loc =>
FDUtilities.GetRoomSector(loc.X, loc.Y, loc.Z, (short)loc.Room, level, floorData)));
exclusions.Add(entity.GetFloorLocation(loc => level.GetRoomSector(loc)));
}
}

Expand All @@ -187,8 +182,6 @@ private static List<Location> ExportTR1Locations(string lvl)
private static List<Location> ExportTR2Locations(string lvl)
{
TR2Level level = _reader2.Read(lvl);
FDControl floorData = new();
floorData.ParseFromLevel(level);
List<Location> exclusions = new();
if (_allTR2Exclusions.ContainsKey(lvl))
{
Expand All @@ -199,8 +192,7 @@ private static List<Location> ExportTR2Locations(string lvl)
{
if (!TR2TypeUtilities.CanSharePickupSpace(entity.TypeID))
{
exclusions.Add(entity.GetFloorLocation(loc =>
FDUtilities.GetRoomSector(loc.X, loc.Y, loc.Z, (short)loc.Room, level, floorData)));
exclusions.Add(entity.GetFloorLocation(loc => level.GetRoomSector(loc)));
}
}

Expand All @@ -211,8 +203,6 @@ private static List<Location> ExportTR2Locations(string lvl)
private static List<Location> ExportTR3Locations(string lvl)
{
TR3Level level = _reader3.Read(lvl);
FDControl floorData = new();
floorData.ParseFromLevel(level);
List<Location> exclusions = new();
if (_allTR3Exclusions.ContainsKey(lvl))
{
Expand All @@ -223,8 +213,7 @@ private static List<Location> ExportTR3Locations(string lvl)
{
if (!TR3TypeUtilities.CanSharePickupSpace(entity.TypeID))
{
exclusions.Add(entity.GetFloorLocation(loc =>
FDUtilities.GetRoomSector(loc.X, loc.Y, loc.Z, (short)loc.Room, level, floorData)));
exclusions.Add(entity.GetFloorLocation(loc => level.GetRoomSector(loc)));
}
}

Expand Down
22 changes: 4 additions & 18 deletions TREnvironmentEditor/Helpers/EMEntityFinder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using TRFDControl;
using TRFDControl.Utilities;
using TRLevelControl.Helpers;
using TRLevelControl.Helpers;
using TRLevelControl.Model;

namespace TREnvironmentEditor.Helpers;
Expand All @@ -13,9 +11,6 @@ public class EMEntityFinder

public int GetEntity(TR1Level level)
{
FDControl floorData = new();
floorData.ParseFromLevel(level);

EMLevelData data = EMLevelData.GetData(level);

List<TR1Type> types = new();
Expand All @@ -32,15 +27,11 @@ public int GetEntity(TR1Level level)
types.AddRange(Types.Select(t => (TR1Type)t));
}

return GetEntity(level.Entities, types, data,
l => FDUtilities.GetRoomSector(l.X, l.Y, l.Z, l.Room, level, floorData));
return GetEntity(level.Entities, types, data, l => level.GetRoomSector(l));
}

public int GetEntity(TR2Level level)
{
FDControl floorData = new();
floorData.ParseFromLevel(level);

EMLevelData data = EMLevelData.GetData(level);

List<TR2Type> types = new();
Expand All @@ -57,15 +48,11 @@ public int GetEntity(TR2Level level)
types.AddRange(Types.Select(t => (TR2Type)t));
}

return GetEntity(level.Entities, types, data,
l => FDUtilities.GetRoomSector(l.X, l.Y, l.Z, l.Room, level, floorData));
return GetEntity(level.Entities, types, data, l => level.GetRoomSector(l));
}

public int GetEntity(TR3Level level)
{
FDControl floorData = new();
floorData.ParseFromLevel(level);

EMLevelData data = EMLevelData.GetData(level);

List<TR3Type> types = new();
Expand All @@ -82,8 +69,7 @@ public int GetEntity(TR3Level level)
types.AddRange(Types.Select(t => (TR3Type)t));
}

return GetEntity(level.Entities, types, data,
l => FDUtilities.GetRoomSector(l.X, l.Y, l.Z, l.Room, level, floorData));
return GetEntity(level.Entities, types, data, l => level.GetRoomSector(l));
}

public int GetEntity<E, T>(List<E> entities, List<T> types, EMLevelData data, Func<EMLocation, TRRoomSector> sectorFunc)
Expand Down
12 changes: 12 additions & 0 deletions TREnvironmentEditor/Helpers/EMLevelData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ public short ConvertRoom(int room)
return Convert(room, NumRooms);
}

public EMLocation ConvertLocation(EMLocation location)
{
return location.Room >= 0 ? location : new()
{
X = location.X,
Y = location.Y,
Z = location.Z,
Room = ConvertRoom(location.Room),
Angle = location.Angle
};
}

public static short Convert(int itemIndex, long numItems)
{
return (short)(itemIndex < 0 ? numItems + itemIndex : itemIndex);
Expand Down
6 changes: 4 additions & 2 deletions TREnvironmentEditor/Helpers/EMLocation.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace TREnvironmentEditor.Helpers;
using TRLevelControl.Model;

public class EMLocation
namespace TREnvironmentEditor.Helpers;

public class EMLocation : ITRLocatable
{
public int X { get; set; }
public int Y { get; set; }
Expand Down
32 changes: 13 additions & 19 deletions TREnvironmentEditor/Helpers/EMLocationUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,36 @@
using TRFDControl;
using TRFDControl.FDEntryTypes;
using TRFDControl.Utilities;
using TRLevelControl.Helpers;
using TRLevelControl.Helpers;
using TRLevelControl.Model;

namespace TREnvironmentEditor.Helpers;

public static class EMLocationUtilities
{
public static int GetContainedSecretEntity(this EMLocation location, TR1Level level, FDControl floorData)
public static int GetContainedSecretEntity(this EMLocation location, TR1Level level)
{
TRRoomSector sector = FDUtilities.GetRoomSector(location.X, location.Y, location.Z, location.Room, level, floorData);
return GetSectorSecretEntity(sector, floorData);
TRRoomSector sector = level.GetRoomSector(location);
return GetSectorSecretEntity(sector, level.FloorData);
}

public static int GetContainedSecretEntity(this EMLocation location, TR2Level level, FDControl floorData)
public static int GetContainedSecretEntity(this EMLocation location, TR2Level level)
{
TRRoomSector sector = FDUtilities.GetRoomSector(location.X, location.Y, location.Z, location.Room, level, floorData);
return level.Entities.FindIndex(e =>
TR2TypeUtilities.IsSecretType(e.TypeID)
&& FDUtilities.GetRoomSector(e.X, e.Y, e.Z, e.Room, level, floorData) == sector
);
TRRoomSector sector = level.GetRoomSector(location);
return level.Entities.FindIndex(e => TR2TypeUtilities.IsSecretType(e.TypeID) && level.GetRoomSector(e) == sector);
}

public static int GetContainedSecretEntity(this EMLocation location, TR3Level level, FDControl floorData)
public static int GetContainedSecretEntity(this EMLocation location, TR3Level level)
{
TRRoomSector sector = FDUtilities.GetRoomSector(location.X, location.Y, location.Z, location.Room, level, floorData);
return GetSectorSecretEntity(sector, floorData);
TRRoomSector sector = level.GetRoomSector(location);
return GetSectorSecretEntity(sector, level.FloorData);
}

private static int GetSectorSecretEntity(TRRoomSector sector, FDControl floorData)
{
if (sector.FDIndex != 0)
{
if (floorData.Entries[sector.FDIndex].Find(e => e is FDTriggerEntry) is FDTriggerEntry trigger
if (floorData[sector.FDIndex].Find(e => e is FDTriggerEntry) is FDTriggerEntry trigger
&& trigger.TrigType == FDTrigType.Pickup
&& trigger.TrigActionList.Find(a => a.TrigAction == FDTrigAction.SecretFound) != null
&& trigger.TrigActionList.Find(a => a.TrigAction == FDTrigAction.Object) is FDActionItem action)
&& trigger.Actions.Find(a => a.Action == FDTrigAction.SecretFound) != null
&& trigger.Actions.Find(a => a.Action == FDTrigAction.Object) is FDActionItem action)
{
return action.Parameter;
}
Expand Down
2 changes: 1 addition & 1 deletion TREnvironmentEditor/Helpers/EMRoomDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using TRFDControl;
using TRLevelControl.Model;

namespace TREnvironmentEditor.Helpers;

Expand Down
32 changes: 14 additions & 18 deletions TREnvironmentEditor/Helpers/EMTrigger.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using TRFDControl;
using TRFDControl.FDEntryTypes;
using TRLevelControl.Model;

namespace TREnvironmentEditor.Helpers;

// This is redundant now - to be eliminated
public class EMTrigger
{
private static readonly byte _defaultMask = 31;
Expand All @@ -23,21 +23,17 @@ public FDTriggerEntry ToFDEntry(EMLevelData levelData)
{
FDTriggerEntry entry = new()
{
Setup = new FDSetup(FDFunction.Trigger),
TrigType = TrigType,
SwitchOrKeyRef = (ushort)levelData.ConvertEntity(SwitchOrKeyRef),
TrigSetup = new FDTrigSetup
{
OneShot = OneShot,
Mask = Mask,
Timer = Timer
},
TrigActionList = new List<FDActionItem>()
SwitchOrKeyRef = levelData.ConvertEntity(SwitchOrKeyRef),
OneShot = OneShot,
Mask = Mask,
Timer = Timer,
Actions = new()
};

foreach (EMTriggerAction action in Actions)
{
entry.TrigActionList.Add(action.ToFDAction(levelData));
entry.Actions.Add(action.ToFDAction(levelData));
}

return entry;
Expand All @@ -48,14 +44,14 @@ public static EMTrigger FromFDEntry(FDTriggerEntry entry)
EMTrigger trigger = new()
{
TrigType = entry.TrigType,
OneShot = entry.TrigSetup.OneShot,
Mask = entry.TrigSetup.Mask,
Timer = entry.TrigSetup.Timer,
SwitchOrKeyRef = (short)entry.SwitchOrKeyRef,
Actions = new List<EMTriggerAction>()
OneShot = entry.OneShot,
Mask = entry.Mask,
Timer = entry.Timer,
SwitchOrKeyRef = entry.SwitchOrKeyRef,
Actions = new()
};

foreach (FDActionItem action in entry.TrigActionList)
foreach (FDActionItem action in entry.Actions)
{
trigger.Actions.Add(EMTriggerAction.FromFDAction(action));
}
Expand Down
16 changes: 8 additions & 8 deletions TREnvironmentEditor/Helpers/EMTriggerAction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using TRFDControl;
using TRLevelControl.Model;

namespace TREnvironmentEditor.Helpers;

Expand All @@ -9,34 +9,34 @@ public class EMTriggerAction

public FDActionItem ToFDAction(EMLevelData levelData)
{
ushort parameter = (ushort)Parameter;
short parameter = Parameter;
if (Parameter < 0)
{
switch (Action)
{
case FDTrigAction.Camera:
parameter = (ushort)(levelData.NumCameras + Parameter);
parameter = (short)(levelData.NumCameras + Parameter);
break;
case FDTrigAction.LookAtItem:
case FDTrigAction.Object:
parameter = (ushort)(levelData.NumEntities + Parameter);
parameter = (short)(levelData.NumEntities + Parameter);
break;
}
}

return new FDActionItem
{
TrigAction = Action,
Action = Action,
Parameter = parameter
};
}

public static EMTriggerAction FromFDAction(FDActionItem action)
{
return new EMTriggerAction
return new()
{
Action = action.TrigAction,
Parameter = (short)action.Parameter
Action = action.Action,
Parameter = action.Parameter
};
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using TRFDControl;
using TRFDControl.FDEntryTypes;
using TRFDControl.Utilities;
using TRLevelControl.Helpers;
using TRLevelControl.Helpers;
using TRLevelControl.Model;

namespace TREnvironmentEditor.Model.Conditions;
Expand All @@ -13,17 +10,14 @@ public class EMSecretRoomCondition : BaseEMCondition

protected override bool Evaluate(TR1Level level)
{
FDControl floorData = new();
floorData.ParseFromLevel(level);

foreach (TRRoomSector sector in level.Rooms[RoomIndex].Sectors)
{
if (sector.FDIndex == 0)
{
continue;
}

if (floorData.Entries[sector.FDIndex].Find(e => e is FDTriggerEntry) is FDTriggerEntry trigger && trigger.TrigActionList.Find(a => a.TrigAction == FDTrigAction.SecretFound) != null)
if (level.FloorData[sector.FDIndex].Find(e => e is FDTriggerEntry) is FDTriggerEntry trigger && trigger.Actions.Any(a => a.Action == FDTrigAction.SecretFound))
{
return true;
}
Expand All @@ -45,27 +39,24 @@ protected override bool Evaluate(TR3Level level)
// It's difficult to tell if a particular model is being used for secret pickups,
// so instead we check the FD under each entity in the room to see if it triggers
// a secret found.
FDControl floorData = new();
floorData.ParseFromLevel(level);

Predicate<FDEntry> pred = new(
e =>
e is FDTriggerEntry trig && trig.TrigType == FDTrigType.Pickup
&& trig.TrigActionList.Count > 1
&& trig.TrigActionList[0].TrigAction == FDTrigAction.Object
&& trig.TrigActionList[1].TrigAction == FDTrigAction.SecretFound
&& trig.Actions.Count > 1
&& trig.Actions[0].Action == FDTrigAction.Object
&& trig.Actions[1].Action == FDTrigAction.SecretFound
);

foreach (TR3Entity entity in roomEntities)
{
TRRoomSector sector = FDUtilities.GetRoomSector(entity.X, entity.Y, entity.Z, RoomIndex, level, floorData);
TRRoomSector sector = level.GetRoomSector(entity);
if (sector.FDIndex == 0)
{
continue;
}

ushort entityIndex = (ushort)level.Entities.IndexOf(entity);
if (floorData.Entries[sector.FDIndex].Find(pred) is FDTriggerEntry trigger && trigger.TrigActionList[0].Parameter == entityIndex)
if (level.FloorData[sector.FDIndex].Find(pred) is FDTriggerEntry trigger && trigger.Actions[0].Parameter == entityIndex)
{
return true;
}
Expand Down
Loading

0 comments on commit e7159ef

Please sign in to comment.