Skip to content

Commit

Permalink
LostArtefacts#314 Enemy Meshes
Browse files Browse the repository at this point in the history
Ensures that any clones of Lara remain visible if Lara herself is invisible. This is done by duplicating the meshes before we clear them.
  • Loading branch information
lahm86 committed Mar 24, 2022
1 parent d5ecaaa commit de65551
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
61 changes: 61 additions & 0 deletions TRRandomizerCore/Helpers/MeshEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,67 @@ public void RemoveColouredTriangles(IEnumerable<int> indices)
Mesh.NumColouredTriangles = (short)triangles.Count;
}

public TRMesh CloneMesh(TRMesh mesh)
{
TRMesh clone = new TRMesh
{
Centre = mesh.Centre,
CollRadius = mesh.CollRadius,
ColouredRectangles = new TRFace4[mesh.NumColouredRectangles],
ColouredTriangles = new TRFace3[mesh.NumColouredTriangles],
Lights = mesh.Lights,
Normals = mesh.Normals,
NumColouredRectangles = mesh.NumColouredRectangles,
NumColouredTriangles = mesh.NumColouredTriangles,
NumNormals = mesh.NumNormals,
NumTexturedRectangles = mesh.NumTexturedRectangles,
NumTexturedTriangles = mesh.NumTexturedTriangles,
NumVertices = mesh.NumVertices,
Pointer = mesh.Pointer,
TexturedRectangles = new TRFace4[mesh.NumTexturedRectangles],
TexturedTriangles = new TRFace3[mesh.NumTexturedTriangles],
Vertices = mesh.Vertices
};

for (int i = 0; i < mesh.NumColouredRectangles; i++)
{
clone.ColouredRectangles[i] = new TRFace4
{
Texture = mesh.ColouredRectangles[i].Texture,
Vertices = mesh.ColouredRectangles[i].Vertices
};
}

for (int i = 0; i < mesh.NumColouredTriangles; i++)
{
clone.ColouredTriangles[i] = new TRFace3
{
Texture = mesh.ColouredTriangles[i].Texture,
Vertices = mesh.ColouredTriangles[i].Vertices
};
}

for (int i = 0; i < mesh.NumTexturedRectangles; i++)
{
clone.TexturedRectangles[i] = new TRFace4
{
Texture = mesh.TexturedRectangles[i].Texture,
Vertices = mesh.TexturedRectangles[i].Vertices
};
}

for (int i = 0; i < mesh.NumTexturedTriangles; i++)
{
clone.TexturedTriangles[i] = new TRFace3
{
Texture = mesh.TexturedTriangles[i].Texture,
Vertices = mesh.TexturedTriangles[i].Vertices
};
}

return clone;
}

public void WriteToLevel(TR2Level level)
{
TRMeshUtilities.UpdateMeshPointers(level, Mesh, _oldLength);
Expand Down
33 changes: 29 additions & 4 deletions TRRandomizerCore/Randomizers/TR2/TR2OutfitRandomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,14 @@ protected override void ProcessImpl()

private bool Import(TR2CombinedLevel level, TR2Entities lara)
{
List<TRModel> models = level.Data.Models.ToList();
TRModel laraModel = models.Find(m => m.ID == (uint)TR2Entities.Lara);
List<TRModel> laraClones = models.FindAll(m => m.MeshTree == laraModel.MeshTree && m != laraModel);

if (lara == TR2Entities.LaraInvisible)
{
// #314 Ensure cloned Laras remain visible
CloneLaraMeshes(level, laraClones, laraModel);
// No import needed, just clear each of Lara's meshes. A haircut is implied
// with this and we don't need to alter the outfit.
HideEntities(level, _invisibleLaraEntities);
Expand Down Expand Up @@ -272,10 +278,6 @@ private bool Import(TR2CombinedLevel level, TR2Entities lara)

try
{
List<TRModel> models = level.Data.Models.ToList();
TRModel laraModel = models.Find(m => m.ID == (uint)TR2Entities.Lara);
List<TRModel> laraClones = models.FindAll(m => m.MeshTree == laraModel.MeshTree);

// Try to import the selected models into the level.
importer.Import();

Expand Down Expand Up @@ -313,6 +315,29 @@ private bool Import(TR2CombinedLevel level, TR2Entities lara)
}
}

private void CloneLaraMeshes(TR2CombinedLevel level, List<TRModel> clones, TRModel laraModel)
{
if (clones.Count > 0)
{
MeshEditor editor = new MeshEditor();
TRMesh[] meshes = TRMeshUtilities.GetModelMeshes(level.Data, laraModel);
int firstMeshIndex = -1;
for (int i = 0; i < meshes.Length; i++)
{
int insertedIndex = TRMeshUtilities.InsertMesh(level.Data, editor.CloneMesh(meshes[i]));
if (firstMeshIndex == -1)
{
firstMeshIndex = insertedIndex;
}
}

foreach (TRModel model in clones)
{
model.StartingMesh = (ushort)firstMeshIndex;
}
}
}

private void HideEntities(TR2CombinedLevel level, IEnumerable<TR2Entities> entities)
{
MeshEditor editor = new MeshEditor();
Expand Down

0 comments on commit de65551

Please sign in to comment.