Skip to content

Commit 90a3e01

Browse files
committed
Automatic merge of T1.5.1-997-gb5992851e1 and 20 pull requests
- Pull request #799 at dfc715e: Consolidated wind simulation - Pull request #839 at d00beb9: First phase of https://blueprints.launchpad.net/or/+spec/additional-cruise-control-parameters - Pull request #876 at f92de76: docs: add source for documents previously on website to source Documentation folder - Pull request #882 at bc27ad2: Blueprint/train car operations UI window - Pull request #885 at 56c17fb: feat: Add notifications to Menu - Pull request #891 at 9a1d6b2: Auto save - Pull request #892 at 1f5ba4c: Signal Function OPP_SIG_ID_TRAINPATH - Pull request #896 at 5866028: First implementation of https://blueprints.launchpad.net/or/+spec/specific-sounds-for-ai-trains - Pull request #900 at c27f32d: DMI updates - Pull request #903 at 3e390b8: Downloading route content (Github, zip) - Pull request #912 at 359cfee: New Triple Valve Features Vol. 2 - Pull request #922 at abe2e52: Autopilot for timetable mode - Pull request #946 at 66f836c: Advanced track sounds - Pull request #949 at 33a3e1c: Oil Burning Locomotive - Pull request #950 at a98ff62: Ctrl-F5 showing yellow rectangles where mouse left button is active - Pull request #951 at 486081b: fix: Fix watchdog process state name - Pull request #952 at b2af1f5: Investigation - Pulsing graphics part 1 - Pull request #953 at 9b0ec01: Fix Lights Crash on Corrupt Shapes - Pull request #954 at 84c2f4b: Add Support for Multiple Track Profiles - Pull request #955 at 0c8e659: Copy dynamic brake speeds from other vehicles
22 parents 64ffd34 + b599285 + dfc715e + d00beb9 + f92de76 + bc27ad2 + 56c17fb + 9a1d6b2 + 1f5ba4c + 5866028 + c27f32d + 3e390b8 + 359cfee + abe2e52 + 66f836c + 33a3e1c + a98ff62 + 486081b + b2af1f5 + 9b0ec01 + 84c2f4b + 0c8e659 commit 90a3e01

File tree

3 files changed

+74
-65
lines changed

3 files changed

+74
-65
lines changed

Source/RunActivity/Viewer3D/DynamicTrack.cs

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
using System.Collections.Generic;
3030
using System.Diagnostics;
3131
using System.IO;
32+
using System.Linq;
3233
using System.Xml;
3334
using System.Xml.Schema;
3435

@@ -216,24 +217,75 @@ public void Mark()
216217
}
217218

218219
/// <summary>
219-
/// Returns the index of the track profile that best suits the given TrVectorSection
220-
/// The result is cached in the section object for future use
220+
/// Returns the index of the track profile that best suits the given Viewer and TrVectorSection object,
221+
/// with an optional shape file name to use as reference.
222+
/// The result is cached in the vector section object for future use
221223
/// </summary>
222224
/// <returns>Index of viewer.TRPs</returns>
223-
public static int GetBestTrackProfile(Viewer viewer, TrVectorSection trSection)
225+
public static int GetBestTrackProfile(Viewer viewer, TrVectorSection trSection, string shapeName = "")
224226
{
225-
string shapeName = "";
226-
if (viewer.Simulator.TSectionDat.TrackShapes.ContainsKey(trSection.ShapeIndex))
227+
int trpIndex;
228+
if (shapeName == "" && viewer.Simulator.TSectionDat.TrackShapes.ContainsKey(trSection.ShapeIndex))
227229
shapeName = viewer.Simulator.TSectionDat.TrackShapes.Get(trSection.ShapeIndex).FileName;
228230

229-
viewer.TrackProfileIndicies.TryGetValue(shapeName, out int trpIndex);
231+
if (viewer.TrackProfileIndicies.ContainsKey(shapeName))
232+
viewer.TrackProfileIndicies.TryGetValue(shapeName, out trpIndex);
233+
else if (shapeName != "") // Haven't checked this track shape yet
234+
{
235+
// Need to load the shape file if not already loaded
236+
var trackShape = viewer.ShapeManager.Get(viewer.Simulator.BasePath + @"\Global\Shapes\" + shapeName);
237+
trpIndex = GetBestTrackProfile(viewer, trackShape);
238+
viewer.TrackProfileIndicies.Add(shapeName, trpIndex);
239+
}
240+
else // Not enough info-use default track profile
241+
trpIndex = 0;
230242

231243
trSection.TRPIndex = trpIndex;
232244
return trpIndex;
233245
}
234246
// Note: Dynamic track objects will ALWAYS use TRPIndex = 0
235247
// This is because dynamic tracks don't have shapes, and as such lack the information needed
236248
// to select a specific track profile.
249+
250+
/// <summary>
251+
/// Determines the index of the track profile that would be the most suitable replacement
252+
/// for the given shared shape object.
253+
/// </summary>
254+
public static int GetBestTrackProfile(Viewer viewer, SharedShape shape)
255+
{
256+
float score = float.NegativeInfinity;
257+
int bestIndex = -1;
258+
for (int i = 0; i < viewer.TRPs.Count; i++)
259+
{
260+
float bestScore = score;
261+
score = 0;
262+
// Default behavior: Attempt to match track shape to track profile using texture names alone
263+
foreach (string image in viewer.TRPs[i].TrackProfile.Images)
264+
{
265+
if (shape.ImageNames != null && shape.ImageNames.Contains(image, StringComparer.InvariantCultureIgnoreCase))
266+
score++;
267+
else // Slight bias against track profiles with extra textures defined
268+
score -= 0.05f;
269+
}
270+
if (score > bestScore && shape.ImageNames != null) // Only continue checking if current profile might be the best one
271+
{
272+
foreach (string image in shape.ImageNames)
273+
{
274+
// Strong bias against track profiles that are missing textures
275+
if (!viewer.TRPs[i].TrackProfile.Images.Contains(image, StringComparer.InvariantCultureIgnoreCase))
276+
score -= 0.25f;
277+
}
278+
}
279+
if (score > bestScore)
280+
bestIndex = i;
281+
else
282+
score = bestScore;
283+
}
284+
285+
if (bestIndex < 0)
286+
bestIndex = 0;
287+
return bestIndex;
288+
}
237289
}
238290

239291
// A track profile consists of a number of groups used for LOD considerations. There are LODs,

Source/RunActivity/Viewer3D/Scenery.cs

Lines changed: 10 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ public WorldFile(Viewer viewer, int tileX, int tileZ, bool visible)
300300
}
301301
}
302302

303+
// Generate track profiles if none have been set up yet
304+
if (viewer.TRPs == null)
305+
{
306+
Trace.Write(" TRP");
307+
// Creates profile and loads materials into SceneryMaterials
308+
TRPFile.CreateTrackProfile(viewer, viewer.Simulator.RoutePath, out viewer.TRPs);
309+
}
310+
303311
// create all the individual scenery objects specified in the WFile
304312
foreach (var worldObject in WFile.Tr_Worldfile)
305313
{
@@ -369,25 +377,15 @@ public WorldFile(Viewer viewer, int tileX, int tileZ, bool visible)
369377
{
370378
if (viewer.Simulator.UseSuperElevation > 0)
371379
SuperElevationManager.DecomposeStaticSuperElevation(viewer, dTrackList, trackObj, worldMatrix, TileX, TileZ, shapeFilePath);
372-
var newScenery = new SwitchTrackShape(viewer, shapeFilePath, worldMatrix, trJunctionNode);
373-
sceneryObjects.Add(newScenery);
374-
375-
string newShapeName = Path.GetFileName(newScenery.SharedShape.FilePath);
376-
if (!viewer.TrackProfileIndicies.ContainsKey(newShapeName))
377-
viewer.TrackProfileIndicies.Add(newShapeName, GetBestTrackProfile(viewer, newScenery.SharedShape));
380+
sceneryObjects.Add(new SwitchTrackShape(viewer, shapeFilePath, worldMatrix, trJunctionNode));
378381
}
379382
else
380383
{
381384
//if want to use super elevation, we will generate tracks using dynamic tracks
382385
if (viewer.Simulator.UseSuperElevation > 0
383386
&& SuperElevationManager.DecomposeStaticSuperElevation(viewer, dTrackList, trackObj, worldMatrix, TileX, TileZ, shapeFilePath))
384387
{
385-
// Need to load the static shape file to determine which track profile to use
386-
var superelevatedShape = viewer.ShapeManager.Get(shapeFilePath);
387-
388-
string newShapeName = Path.GetFileName(superelevatedShape.FilePath);
389-
if (!viewer.TrackProfileIndicies.ContainsKey(newShapeName))
390-
viewer.TrackProfileIndicies.Add(newShapeName, GetBestTrackProfile(viewer, superelevatedShape));
388+
// No need to add shapes for this segment of track
391389
}
392390
//otherwise, use shapes
393391
else if (!containsMovingTable) sceneryObjects.Add(new StaticTrackShape(viewer, shapeFilePath, worldMatrix));
@@ -664,52 +662,6 @@ public void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
664662
forest.PrepareFrame(frame, elapsedTime);
665663
}
666664

667-
/// <summary>
668-
/// Determines the index of the track profile that would be the most suitable replacement
669-
/// for the given shared shape object.
670-
/// </summary>
671-
public static int GetBestTrackProfile(Viewer viewer, SharedShape shape)
672-
{
673-
if (viewer.TRPs == null)
674-
{
675-
// First to need a track profile creates it
676-
Trace.Write(" TRP");
677-
// Creates profile and loads materials into SceneryMaterials
678-
TRPFile.CreateTrackProfile(viewer, viewer.Simulator.RoutePath, out viewer.TRPs);
679-
}
680-
681-
float score = float.NegativeInfinity;
682-
int bestIndex = -1;
683-
for (int i = 0; i < viewer.TRPs.Count; i++)
684-
{
685-
float prevScore = score;
686-
score = 0;
687-
// Default behavior: Attempt to match track shape to track profile using texture names alone
688-
foreach (string image in viewer.TRPs[i].TrackProfile.Images)
689-
{
690-
if (shape.ImageNames.Contains(image, StringComparer.InvariantCultureIgnoreCase))
691-
score++;
692-
else // Slight bias against track profiles with extra textures defined
693-
score -= 0.05f;
694-
}
695-
foreach (string image in shape.ImageNames)
696-
{
697-
// Strong bias against track profiles that are missing textures
698-
if (!viewer.TRPs[i].TrackProfile.Images.Contains(image, StringComparer.InvariantCultureIgnoreCase))
699-
score -= 0.25f;
700-
}
701-
if (score > prevScore)
702-
bestIndex = i;
703-
else
704-
score = prevScore;
705-
}
706-
707-
if (bestIndex < 0)
708-
return 0;
709-
else
710-
return bestIndex;
711-
}
712-
713665
/// <summary>
714666
/// MSTS WFiles represent some location with a position, quaternion and tile coordinates
715667
/// This converts it to the ORTS WorldPosition representation

Source/RunActivity/Viewer3D/SuperElevation.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
using ORTS.Common;
2525
using System;
2626
using System.Collections.Generic;
27-
using System.Diagnostics;
27+
using System.IO;
2828

2929
namespace Orts.Viewer3D
3030
{
@@ -85,6 +85,11 @@ public static bool DecomposeStaticSuperElevation(Viewer viewer, List<DynamicTrac
8585
}
8686
sectionsinShape.Add(tmp);
8787

88+
// Determine the track profile to use for this section
89+
// It's possible that the tsection ID used by the track section points to the wrong shape
90+
// Use the static shape name instead to get expected results
91+
DynamicTrackViewer.GetBestTrackProfile(viewer, tmp, Path.GetFileName(shapeFilePath));
92+
8893
drawn++;
8994
}
9095
}

0 commit comments

Comments
 (0)