Skip to content

Commit ae3b5cf

Browse files
authored
Merge pull request #867 from Looky1173/code-quality-fixes
Code quality improvements
2 parents d394472 + c8d2485 commit ae3b5cf

File tree

10 files changed

+3197
-4084
lines changed

10 files changed

+3197
-4084
lines changed

Source/Orts.Simulation/Simulation/AIs/AI.cs

Lines changed: 141 additions & 178 deletions
Large diffs are not rendered by default.

Source/Orts.Simulation/Simulation/AIs/AIAuxAction.cs

Lines changed: 141 additions & 215 deletions
Large diffs are not rendered by default.

Source/Orts.Simulation/Simulation/AIs/AIPath.cs

Lines changed: 57 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
* Could this be used for player trains also?
2323
*
2424
*/
25-
using Orts.Formats.Msts;
26-
using Orts.Formats.OR;
27-
using ORTS.Common;
2825
using System;
2926
using System.Collections.Generic;
3027
using System.Diagnostics;
3128
using System.IO;
3229
using System.Linq;
30+
using Orts.Formats.Msts;
31+
using Orts.Formats.OR;
32+
using ORTS.Common;
3333

3434
namespace Orts.Simulation.AIs
3535
{
@@ -42,10 +42,10 @@ public class AIPath
4242
#if ACTIVITY_EDITOR
4343
public ORRouteConfig orRouteConfig { get; protected set; }
4444
#endif
45-
public AIPathNode FirstNode; // path starting node
46-
//public AIPathNode LastVisitedNode; not used anymore
45+
public AIPathNode FirstNode; // Path starting node
46+
//public AIPathNode LastVisitedNode; not used anymore --- TODO: Remove?
4747
public List<AIPathNode> Nodes = new List<AIPathNode>();
48-
public string pathName; //name of the path to be able to print it.
48+
public string pathName; // Name of the path to be able to print it.
4949

5050
/// <summary>
5151
/// Creates an AIPath from PAT file information.
@@ -84,11 +84,11 @@ public AIPath(TDBFile TDB, TSectionDatFile tsectiondat, string filePath)
8484
node.Index = i;
8585
TrPathNode tpn = patFile.TrPathNodes[i];
8686

87-
// find TVNindex to next main node.
87+
// Find TVNindex to next main node.
8888
if (tpn.HasNextMainNode)
8989
{
9090
node.NextMainNode = Nodes[(int)tpn.nextMainNode];
91-
node.NextMainTVNIndex = node.FindTVNIndex(node.NextMainNode, TDB, tsectiondat, i == 0 ? -1 : Nodes[i-1].NextMainTVNIndex );
91+
node.NextMainTVNIndex = node.FindTVNIndex(node.NextMainNode, TDB, tsectiondat, i == 0 ? -1 : Nodes[i - 1].NextMainTVNIndex);
9292
if (node.JunctionIndex >= 0)
9393
node.IsFacingPoint = TestFacingPoint(node.JunctionIndex, node.NextMainTVNIndex);
9494
if (node.NextMainTVNIndex < 0)
@@ -99,7 +99,7 @@ public AIPath(TDBFile TDB, TSectionDatFile tsectiondat, string filePath)
9999
}
100100
}
101101

102-
// find TVNindex to next siding node
102+
// Find TVNindex to next siding node
103103
if (tpn.HasNextSidingNode)
104104
{
105105
node.NextSidingNode = Nodes[(int)tpn.nextSidingNode];
@@ -120,14 +120,13 @@ public AIPath(TDBFile TDB, TSectionDatFile tsectiondat, string filePath)
120120

121121
FindSidingEnds();
122122

123-
if (fatalerror) Nodes = null; // invalid path - do not return any nodes
123+
if (fatalerror) Nodes = null; // Invalid path - do not return any nodes
124124
}
125125

126126
/// <summary>
127127
/// constructor out of other path
128128
/// </summary>
129129
/// <param name="otherPath"></param>
130-
131130
public AIPath(AIPath otherPath)
132131
{
133132
TrackDB = otherPath.TrackDB; ;
@@ -138,8 +137,7 @@ public AIPath(AIPath otherPath)
138137
Nodes.Add(new AIPathNode(otherNode));
139138
}
140139

141-
// set correct node references
142-
140+
// Set correct node references
143141
for (int iNode = 0; iNode <= otherPath.Nodes.Count - 1; iNode++)
144142
{
145143
AIPathNode otherNode = otherPath.Nodes[iNode];
@@ -190,7 +188,7 @@ private void FindSidingEnds()
190188
// kvp.Value.IsLastSwitchUse = true;
191189
}
192190

193-
// restore game state
191+
// Restore game state
194192
public AIPath(TrackDatabaseFile TDB, TrackSectionsFile tsectiondat, BinaryReader inf)
195193
{
196194
pathName = inf.ReadString();
@@ -211,13 +209,10 @@ public AIPath(TrackDatabaseFile TDB, TrackSectionsFile tsectiondat, BinaryReader
211209
public AIPathNode ReadNode(BinaryReader inf)
212210
{
213211
int index = inf.ReadInt32();
214-
if (index < 0 || index > Nodes.Count)
215-
return null;
216-
else
217-
return Nodes[index];
212+
return index < 0 || index > Nodes.Count ? null : Nodes[index];
218213
}
219214

220-
// save game state
215+
// Save game state
221216
public void Save(BinaryWriter outf)
222217
{
223218
outf.Write(pathName);
@@ -234,7 +229,7 @@ public void Save(BinaryWriter outf)
234229
public static void WriteNode(BinaryWriter outf, AIPathNode node)
235230
{
236231
if (node == null)
237-
outf.Write((int)-1);
232+
outf.Write(-1);
238233
else
239234
outf.Write(node.Index);
240235
}
@@ -248,9 +243,7 @@ private bool TestFacingPoint(int junctionIndex, int vectorIndex)
248243
if (junctionIndex < 0 || vectorIndex < 0)
249244
return false;
250245
TrackNode tn = TrackDB.TrackNodes[junctionIndex];
251-
if (tn.TrJunctionNode == null || tn.TrPins[0].Link == vectorIndex)
252-
return false;
253-
return true;
246+
return tn.TrJunctionNode != null && tn.TrPins[0].Link != vectorIndex;
254247
}
255248
}
256249

@@ -292,7 +285,6 @@ public AIPathNode(TrPathNode tpn, TrackPDP pdp, TrackDB trackDB, bool isTimetabl
292285
/// Constructor from other AIPathNode
293286
/// </summary>
294287
/// <param name="otherNode"></param>
295-
296288
public AIPathNode(AIPathNode otherNode)
297289
{
298290
ID = otherNode.ID;
@@ -301,8 +293,8 @@ public AIPathNode(AIPathNode otherNode)
301293
WaitTimeS = otherNode.WaitTimeS;
302294
WaitUntil = otherNode.WaitUntil;
303295
NCars = otherNode.NCars;
304-
NextMainNode = null; // set after completion of copying to get correct reference
305-
NextSidingNode = null; // set after completion of copying to get correct reference
296+
NextMainNode = null; // Set after completion of copying to get correct reference
297+
NextSidingNode = null; // Set after completion of copying to get correct reference
306298
NextMainTVNIndex = otherNode.NextMainTVNIndex;
307299
NextSidingTVNIndex = otherNode.NextSidingTVNIndex;
308300
Location = otherNode.Location;
@@ -327,60 +319,61 @@ public AIPathNode(AIPathNode otherNode)
327319
private void InterpretPathNodeFlags(TrPathNode tpn, TrackPDP pdp, bool isTimetableMode)
328320
{
329321
if ((tpn.pathFlags & 03) == 0) return;
330-
// bit 0 and/or bit 1 is set.
322+
// Bit 0 and/or bit 1 is set.
331323

332324
if ((tpn.pathFlags & 01) != 0)
333325
{
334-
// if bit 0 is set: reversal
326+
// If bit 0 is set: reversal
335327
Type = AIPathNodeType.Reverse;
336328
}
337329
else
338330
{
339-
// bit 0 is not set, but bit 1 is set:waiting point
331+
// Bit 0 is not set, but bit 1 is set: waiting point
340332
Type = AIPathNodeType.Stop;
341-
//<CSComment> tests showed me that value 9 in pdp is generated when the waiting point (or also
342-
//a path start or end point) are dragged within the path editor of the MSTS activity editor; the points are still valid;
333+
// <CSComment> Tests showed me that value 9 in pdp is generated when the waiting point (or also
334+
// a path start or end point) are dragged within the path editor of the MSTS activity editor; the points are still valid;
343335
// however, as a contradictory case of the past has been reported, the check is skipped only when the enhanced compatibility flag is on;
344-
if (pdp.IsInvalid && isTimetableMode) // not a valid point
336+
if (pdp.IsInvalid && isTimetableMode) // Not a valid point
345337
{
346338
Type = AIPathNodeType.Invalid;
347339
}
348340
}
349341

350342
WaitTimeS = (int)((tpn.pathFlags >> 16) & 0xffff); // get the AAAA part.
351-
// computations for absolute wait times are made within AITrain.cs
352-
/* if (WaitTimeS >= 30000 && WaitTimeS < 40000)
353-
{
354-
// real wait time.
355-
// waitTimeS (in decimal notation) = 3HHMM (hours and minuts)
356-
int hour = (WaitTimeS / 100) % 100;
357-
int minute = WaitTimeS % 100;
358-
WaitUntil = 60 * (minute + 60 * hour);
359-
WaitTimeS = 0;
360-
}*/
343+
// computations for absolute wait times are made within AITrain.cs
344+
// TODO: Remove?
345+
/* if (WaitTimeS >= 30000 && WaitTimeS < 40000)
346+
{
347+
// real wait time.
348+
// waitTimeS (in decimal notation) = 3HHMM (hours and minuts)
349+
int hour = (WaitTimeS / 100) % 100;
350+
int minute = WaitTimeS % 100;
351+
WaitUntil = 60 * (minute + 60 * hour);
352+
WaitTimeS = 0;
353+
}*/
361354
// computations are made within AITrain.cs
362-
/* else if (WaitTimeS >= 40000 && WaitTimeS < 60000)
363-
{
364-
// Uncouple if a wait=stop point
365-
// waitTimeS (in decimal notation) = 4NNSS (uncouple NN cars, wait SS seconds)
366-
// or 5NNSS (uncouple NN cars, keep rear, wait SS seconds)
367-
NCars = (WaitTimeS / 100) % 100;
368-
if (WaitTimeS >= 50000)
369-
NCars = -NCars;
370-
WaitTimeS %= 100;
371-
if (Type == AIPathNodeType.Stop)
372-
Type = AIPathNodeType.Uncouple;
373-
}
374-
else if (WaitTimeS >= 60000) // this is old and should be removed/reused
375-
{
376-
// waitTimes = 6xSSS with waitTime SSS seconds.
377-
WaitTimeS %= 1000;
378-
} */
355+
/* else if (WaitTimeS >= 40000 && WaitTimeS < 60000)
356+
{
357+
// Uncouple if a wait=stop point
358+
// waitTimeS (in decimal notation) = 4NNSS (uncouple NN cars, wait SS seconds)
359+
// or 5NNSS (uncouple NN cars, keep rear, wait SS seconds)
360+
NCars = (WaitTimeS / 100) % 100;
361+
if (WaitTimeS >= 50000)
362+
NCars = -NCars;
363+
WaitTimeS %= 100;
364+
if (Type == AIPathNodeType.Stop)
365+
Type = AIPathNodeType.Uncouple;
366+
}
367+
else if (WaitTimeS >= 60000) // this is old and should be removed/reused
368+
{
369+
// waitTimes = 6xSSS with waitTime SSS seconds.
370+
WaitTimeS %= 1000;
371+
} */
379372

380373
}
381374

382375

383-
// restore game state
376+
// Restore game state
384377
public AIPathNode(BinaryReader inf)
385378
{
386379
ID = inf.ReadInt32();
@@ -401,7 +394,7 @@ public AIPathNode(BinaryReader inf)
401394
Location.Location.Z = inf.ReadSingle();
402395
}
403396

404-
// save game state
397+
// Save game state
405398
public void Save(BinaryWriter outf)
406399
{
407400
outf.Write(ID);
@@ -429,7 +422,7 @@ public int FindTVNIndex(AIPathNode nextNode, TrackDatabaseFile TDB, TrackSection
429422
int junctionIndexThis = JunctionIndex;
430423
int junctionIndexNext = nextNode.JunctionIndex;
431424

432-
// if this is no junction, try to find the TVN index
425+
// If this is no junction, try to find the TVN index
433426
if (junctionIndexThis < 0)
434427
{
435428
try
@@ -455,7 +448,7 @@ public int FindTVNIndex(AIPathNode nextNode, TrackDatabaseFile TDB, TrackSection
455448
}
456449
}
457450

458-
//both this node and the next node are junctions: find the vector node connecting them.
451+
// Both this node and the next node are junctions: find the vector node connecting them.
459452
var iCand = -1;
460453
for (int i = 0; i < TDB.TrackDB.TrackNodes.Count(); i++)
461454
{
@@ -516,7 +509,7 @@ public static int FindJunctionOrEndIndex(WorldLocation location, TrackDB trackDB
516509
float dz = tn.UiD.Z - location.Location.Z;
517510
dz += (tn.UiD.TileZ - location.TileZ) * 2048;
518511
float dy = tn.UiD.Y - location.Location.Y;
519-
float d = dx * dx + dy * dy + dz * dz;
512+
float d = (dx * dx) + (dy * dy) + (dz * dz);
520513
if (bestDistance2 > d)
521514
{
522515
bestIndex = j;

0 commit comments

Comments
 (0)