Skip to content

Commit

Permalink
Refactor room-related collections (LostArtefacts#617)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahm86 authored Apr 15, 2024
1 parent b7074dc commit 4e109d6
Show file tree
Hide file tree
Showing 56 changed files with 471 additions and 1,007 deletions.
Binary file modified Deps/TRGE.Coord.dll
Binary file not shown.
22 changes: 11 additions & 11 deletions TREnvironmentEditor/Helpers/EMLevelData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace TREnvironmentEditor.Helpers;

public class EMLevelData
{
public uint NumCameras { get; set; }
public int NumCameras { get; set; }
public int NumEntities { get; set; }
public uint NumRooms { get; set; }
public int NumRooms { get; set; }

/// <summary>
/// Negative values will imply a backwards search against NumCameras.
Expand Down Expand Up @@ -42,31 +42,31 @@ public static short Convert(int itemIndex, long numItems)

public static EMLevelData GetData(TR1Level level)
{
return new EMLevelData
return new()
{
NumCameras = level.NumCameras,
NumCameras = level.Cameras.Count,
NumEntities = level.Entities.Count,
NumRooms = level.NumRooms
NumRooms = level.Rooms.Count
};
}

public static EMLevelData GetData(TR2Level level)
{
return new EMLevelData
return new()
{
NumCameras = level.NumCameras,
NumCameras = level.Cameras.Count,
NumEntities = level.Entities.Count,
NumRooms = level.NumRooms
NumRooms = level.Rooms.Count
};
}

public static EMLevelData GetData(TR3Level level)
{
return new EMLevelData
return new()
{
NumCameras = level.NumCameras,
NumCameras = level.Cameras.Count,
NumEntities = level.Entities.Count,
NumRooms = level.NumRooms
NumRooms = level.Rooms.Count
};
}
}
6 changes: 3 additions & 3 deletions TREnvironmentEditor/Model/Types/Mirroring/EMMirrorFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ private void MirrorBoxes(TR3Level level)
MirrorBoxes(level.Boxes);
}

private void MirrorBoxes(TRBox[] boxes)
private void MirrorBoxes(List<TRBox> boxes)
{
// TR1 boxes are in world coordinate values
foreach (TRBox box in boxes)
Expand All @@ -543,7 +543,7 @@ private void MirrorBoxes(TRBox[] boxes)
}
}

private void MirrorBoxes(TR2Box[] boxes)
private void MirrorBoxes(List<TR2Box> boxes)
{
// Boxes do not necessarily cover only one sector and several sectors can point
// to the same box. So we need to work out the smallest new X position for shared
Expand Down Expand Up @@ -583,7 +583,7 @@ private static void MirrorStaticMeshes(TR3Level level)
});
}

private static void MirrorStaticMeshes(TRStaticMesh[] staticMeshes, Func<TRStaticMesh, TRMesh> meshFunc)
private static void MirrorStaticMeshes(List<TRStaticMesh> staticMeshes, Func<TRStaticMesh, TRMesh> meshFunc)
{
foreach (TRStaticMesh staticMesh in staticMeshes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class EMVisibilityPortalFunction : BaseEMFunction

public override void ApplyToLevel(TR1Level level)
{
EMLevelData data = new() { NumRooms = level.NumRooms };
EMLevelData data = GetData(level);

foreach (EMVisibilityPortal emPortal in Portals)
{
Expand All @@ -24,7 +24,7 @@ public override void ApplyToLevel(TR1Level level)

public override void ApplyToLevel(TR2Level level)
{
EMLevelData data = new() { NumRooms = level.NumRooms };
EMLevelData data = GetData(level);

foreach (EMVisibilityPortal emPortal in Portals)
{
Expand All @@ -39,7 +39,7 @@ public override void ApplyToLevel(TR2Level level)

public override void ApplyToLevel(TR3Level level)
{
EMLevelData data = new() { NumRooms = level.NumRooms };
EMLevelData data = GetData(level);

foreach (EMVisibilityPortal emPortal in Portals)
{
Expand Down
15 changes: 3 additions & 12 deletions TREnvironmentEditor/Model/Types/Rooms/EMCopyRoomFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,7 @@ public override void ApplyToLevel(TR1Level level)
BoxGenerator.Generate(newRoom, level, linkedSector);
}

List<TRRoom> rooms = level.Rooms.ToList();
rooms.Add(newRoom);
level.Rooms = rooms.ToArray();
level.NumRooms++;
level.Rooms.Add(newRoom);
}

public override void ApplyToLevel(TR2Level level)
Expand Down Expand Up @@ -311,10 +308,7 @@ public override void ApplyToLevel(TR2Level level)
BoxGenerator.Generate(newRoom, level, linkedSector);
}

List<TR2Room> rooms = level.Rooms.ToList();
rooms.Add(newRoom);
level.Rooms = rooms.ToArray();
level.NumRooms++;
level.Rooms.Add(newRoom);
}

public override void ApplyToLevel(TR3Level level)
Expand Down Expand Up @@ -466,10 +460,7 @@ public override void ApplyToLevel(TR3Level level)
BoxGenerator.Generate(newRoom, level, linkedSector);
}

List<TR3Room> rooms = level.Rooms.ToList();
rooms.Add(newRoom);
level.Rooms = rooms.ToArray();
level.NumRooms++;
level.Rooms.Add(newRoom);
}

private TRRoomSector RebuildSector(TRRoomSector originalSector, int sectorIndex, FDControl floorData, int ydiff, TRRoomInfo oldRoomInfo)
Expand Down
15 changes: 3 additions & 12 deletions TREnvironmentEditor/Model/Types/Rooms/EMCreateRoomFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ public override void ApplyToLevel(TR1Level level)

room.NumDataWords = (uint)(room.RoomData.Serialize().Length / 2);

List<TRRoom> rooms = level.Rooms.ToList();
rooms.Add(room);
level.Rooms = rooms.ToArray();
level.NumRooms++;
level.Rooms.Add(room);
}

public override void ApplyToLevel(TR2Level level)
Expand Down Expand Up @@ -185,10 +182,7 @@ public override void ApplyToLevel(TR2Level level)

room.NumDataWords = (uint)(room.RoomData.Serialize().Length / 2);

List<TR2Room> rooms = level.Rooms.ToList();
rooms.Add(room);
level.Rooms = rooms.ToArray();
level.NumRooms++;
level.Rooms.Add(room);
}

public override void ApplyToLevel(TR3Level level)
Expand Down Expand Up @@ -271,10 +265,7 @@ public override void ApplyToLevel(TR3Level level)

room.NumDataWords = (uint)(room.RoomData.Serialize().Length / 2);

List<TR3Room> rooms = level.Rooms.ToList();
rooms.Add(room);
level.Rooms = rooms.ToArray();
level.NumRooms++;
level.Rooms.Add(room);
}

private List<TRRoomSector> GenerateSectors(sbyte ceiling, sbyte floor)
Expand Down
26 changes: 7 additions & 19 deletions TREnvironmentEditor/Model/Types/Rooms/EMImportRoomFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public override void ApplyToLevel(TR2Level level)
if (!PreserveBoxes)
{
TRRoomSector linkedSector = FDUtilities.GetRoomSector(LinkedLocation.X, LinkedLocation.Y, LinkedLocation.Z, data.ConvertRoom(LinkedLocation.Room), level, floorData);
newBoxIndex = (ushort)level.NumBoxes;
newBoxIndex = (ushort)level.Boxes.Count;
int linkedBoxIndex = linkedSector.BoxIndex;

TR2BoxUtilities.DuplicateZone(level, linkedBoxIndex);
Expand All @@ -209,11 +209,8 @@ public override void ApplyToLevel(TR2Level level)
XMax = xmax,
ZMax = zmax,
TrueFloor = (short)newRoom.Info.YBottom
};
List<TR2Box> boxes = level.Boxes.ToList();
boxes.Add(box);
level.Boxes = boxes.ToArray();
level.NumBoxes++;
};
level.Boxes.Add(box);

// Link the box to the room we're joining to
TR2BoxUtilities.UpdateOverlaps(level, box, new List<ushort> { (ushort)linkedBoxIndex });
Expand Down Expand Up @@ -302,10 +299,7 @@ public override void ApplyToLevel(TR2Level level)

floorData.WriteToLevel(level);

List<TR2Room> rooms = level.Rooms.ToList();
rooms.Add(newRoom);
level.Rooms = rooms.ToArray();
level.NumRooms++;
level.Rooms.Add(newRoom);
}

public override void ApplyToLevel(TR3Level level)
Expand Down Expand Up @@ -456,7 +450,7 @@ public override void ApplyToLevel(TR3Level level)

if (!PreserveBoxes)
{
newBoxIndex = (ushort)level.NumBoxes;
newBoxIndex = (ushort)level.Boxes.Count;

// Duplicate the zone for the new box and link the current box to the new room
TR2BoxUtilities.DuplicateZone(level, linkedBoxIndex);
Expand All @@ -478,10 +472,7 @@ public override void ApplyToLevel(TR3Level level)
ZMax = zmax,
TrueFloor = (short)newRoom.Info.YBottom
};
List<TR2Box> boxes = level.Boxes.ToList();
boxes.Add(box);
level.Boxes = boxes.ToArray();
level.NumBoxes++;
level.Boxes.Add(box);

// Link the box to the room we're joining to
TR2BoxUtilities.UpdateOverlaps(level, box, new List<ushort> { (ushort)linkedBoxIndex });
Expand Down Expand Up @@ -612,10 +603,7 @@ public override void ApplyToLevel(TR3Level level)

floorData.WriteToLevel(level);

List<TR3Room> rooms = level.Rooms.ToList();
rooms.Add(newRoom);
level.Rooms = rooms.ToArray();
level.NumRooms++;
level.Rooms.Add(newRoom);
}

public void RemapTextures(Dictionary<ushort, ushort> indexMap)
Expand Down
15 changes: 3 additions & 12 deletions TREnvironmentEditor/Model/Types/Sounds/EMAddSoundSourceFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,16 @@ public class EMAddSoundSourceFunction : BaseEMFunction

public override void ApplyToLevel(TR1Level level)
{
List<TRSoundSource> soundSources = level.SoundSources.ToList();
soundSources.Add(Source);
level.SoundSources = soundSources.ToArray();
level.NumSoundSources++;
level.SoundSources.Add(Source);
}

public override void ApplyToLevel(TR2Level level)
{
List<TRSoundSource> soundSources = level.SoundSources.ToList();
soundSources.Add(Source);
level.SoundSources = soundSources.ToArray();
level.NumSoundSources++;
level.SoundSources.Add(Source);
}

public override void ApplyToLevel(TR3Level level)
{
List<TRSoundSource> soundSources = level.SoundSources.ToList();
soundSources.Add(Source);
level.SoundSources = soundSources.ToArray();
level.NumSoundSources++;
level.SoundSources.Add(Source);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,22 @@ public class EMRemoveSoundSourceFunction : BaseEMFunction

public override void ApplyToLevel(TR1Level level)
{
// Can't remove by index in case several are called in succession
List<TRSoundSource> sources = level.SoundSources.ToList();
if (RemoveSource(sources))
{
level.SoundSources = sources.ToArray();
level.NumSoundSources--;
}
RemoveSource(level.SoundSources);
}

public override void ApplyToLevel(TR2Level level)
{
// Can't remove by index in case several are called in succession
List<TRSoundSource> sources = level.SoundSources.ToList();
if (RemoveSource(sources))
{
level.SoundSources = sources.ToArray();
level.NumSoundSources--;
}
RemoveSource(level.SoundSources);
}

public override void ApplyToLevel(TR3Level level)
{
List<TRSoundSource> sources = level.SoundSources.ToList();
if (RemoveSource(sources))
{
level.SoundSources = sources.ToArray();
level.NumSoundSources--;
}
RemoveSource(level.SoundSources);
}

private bool RemoveSource(List<TRSoundSource> sources)
{
TRSoundSource source = sources.Find(s => s.X == Source.X && s.Y == Source.Y && s.Z == Source.Z);
if (source != null)
{
return sources.Remove(source);
}
return false;
return source != null && sources.Remove(source);
}
}
Loading

0 comments on commit 4e109d6

Please sign in to comment.