22
22
* Could this be used for player trains also?
23
23
*
24
24
*/
25
- using Orts . Formats . Msts ;
26
- using Orts . Formats . OR ;
27
- using ORTS . Common ;
28
25
using System ;
29
26
using System . Collections . Generic ;
30
27
using System . Diagnostics ;
31
28
using System . IO ;
32
29
using System . Linq ;
30
+ using Orts . Formats . Msts ;
31
+ using Orts . Formats . OR ;
32
+ using ORTS . Common ;
33
33
34
34
namespace Orts . Simulation . AIs
35
35
{
@@ -42,10 +42,10 @@ public class AIPath
42
42
#if ACTIVITY_EDITOR
43
43
public ORRouteConfig orRouteConfig { get ; protected set ; }
44
44
#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?
47
47
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.
49
49
50
50
/// <summary>
51
51
/// Creates an AIPath from PAT file information.
@@ -84,11 +84,11 @@ public AIPath(TDBFile TDB, TSectionDatFile tsectiondat, string filePath)
84
84
node . Index = i ;
85
85
TrPathNode tpn = patFile . TrPathNodes [ i ] ;
86
86
87
- // find TVNindex to next main node.
87
+ // Find TVNindex to next main node.
88
88
if ( tpn . HasNextMainNode )
89
89
{
90
90
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 ) ;
92
92
if ( node . JunctionIndex >= 0 )
93
93
node . IsFacingPoint = TestFacingPoint ( node . JunctionIndex , node . NextMainTVNIndex ) ;
94
94
if ( node . NextMainTVNIndex < 0 )
@@ -99,7 +99,7 @@ public AIPath(TDBFile TDB, TSectionDatFile tsectiondat, string filePath)
99
99
}
100
100
}
101
101
102
- // find TVNindex to next siding node
102
+ // Find TVNindex to next siding node
103
103
if ( tpn . HasNextSidingNode )
104
104
{
105
105
node . NextSidingNode = Nodes [ ( int ) tpn . nextSidingNode ] ;
@@ -120,14 +120,13 @@ public AIPath(TDBFile TDB, TSectionDatFile tsectiondat, string filePath)
120
120
121
121
FindSidingEnds ( ) ;
122
122
123
- if ( fatalerror ) Nodes = null ; // invalid path - do not return any nodes
123
+ if ( fatalerror ) Nodes = null ; // Invalid path - do not return any nodes
124
124
}
125
125
126
126
/// <summary>
127
127
/// constructor out of other path
128
128
/// </summary>
129
129
/// <param name="otherPath"></param>
130
-
131
130
public AIPath ( AIPath otherPath )
132
131
{
133
132
TrackDB = otherPath . TrackDB ; ;
@@ -138,8 +137,7 @@ public AIPath(AIPath otherPath)
138
137
Nodes . Add ( new AIPathNode ( otherNode ) ) ;
139
138
}
140
139
141
- // set correct node references
142
-
140
+ // Set correct node references
143
141
for ( int iNode = 0 ; iNode <= otherPath . Nodes . Count - 1 ; iNode ++ )
144
142
{
145
143
AIPathNode otherNode = otherPath . Nodes [ iNode ] ;
@@ -190,7 +188,7 @@ private void FindSidingEnds()
190
188
// kvp.Value.IsLastSwitchUse = true;
191
189
}
192
190
193
- // restore game state
191
+ // Restore game state
194
192
public AIPath ( TrackDatabaseFile TDB , TrackSectionsFile tsectiondat , BinaryReader inf )
195
193
{
196
194
pathName = inf . ReadString ( ) ;
@@ -211,13 +209,10 @@ public AIPath(TrackDatabaseFile TDB, TrackSectionsFile tsectiondat, BinaryReader
211
209
public AIPathNode ReadNode ( BinaryReader inf )
212
210
{
213
211
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 ] ;
218
213
}
219
214
220
- // save game state
215
+ // Save game state
221
216
public void Save ( BinaryWriter outf )
222
217
{
223
218
outf . Write ( pathName ) ;
@@ -234,7 +229,7 @@ public void Save(BinaryWriter outf)
234
229
public static void WriteNode ( BinaryWriter outf , AIPathNode node )
235
230
{
236
231
if ( node == null )
237
- outf . Write ( ( int ) - 1 ) ;
232
+ outf . Write ( - 1 ) ;
238
233
else
239
234
outf . Write ( node . Index ) ;
240
235
}
@@ -248,9 +243,7 @@ private bool TestFacingPoint(int junctionIndex, int vectorIndex)
248
243
if ( junctionIndex < 0 || vectorIndex < 0 )
249
244
return false ;
250
245
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 ;
254
247
}
255
248
}
256
249
@@ -292,7 +285,6 @@ public AIPathNode(TrPathNode tpn, TrackPDP pdp, TrackDB trackDB, bool isTimetabl
292
285
/// Constructor from other AIPathNode
293
286
/// </summary>
294
287
/// <param name="otherNode"></param>
295
-
296
288
public AIPathNode ( AIPathNode otherNode )
297
289
{
298
290
ID = otherNode . ID ;
@@ -301,8 +293,8 @@ public AIPathNode(AIPathNode otherNode)
301
293
WaitTimeS = otherNode . WaitTimeS ;
302
294
WaitUntil = otherNode . WaitUntil ;
303
295
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
306
298
NextMainTVNIndex = otherNode . NextMainTVNIndex ;
307
299
NextSidingTVNIndex = otherNode . NextSidingTVNIndex ;
308
300
Location = otherNode . Location ;
@@ -327,60 +319,61 @@ public AIPathNode(AIPathNode otherNode)
327
319
private void InterpretPathNodeFlags ( TrPathNode tpn , TrackPDP pdp , bool isTimetableMode )
328
320
{
329
321
if ( ( tpn . pathFlags & 03 ) == 0 ) return ;
330
- // bit 0 and/or bit 1 is set.
322
+ // Bit 0 and/or bit 1 is set.
331
323
332
324
if ( ( tpn . pathFlags & 01 ) != 0 )
333
325
{
334
- // if bit 0 is set: reversal
326
+ // If bit 0 is set: reversal
335
327
Type = AIPathNodeType . Reverse ;
336
328
}
337
329
else
338
330
{
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
340
332
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;
343
335
// 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
345
337
{
346
338
Type = AIPathNodeType . Invalid ;
347
339
}
348
340
}
349
341
350
342
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
+ }*/
361
354
// 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
+ } */
379
372
380
373
}
381
374
382
375
383
- // restore game state
376
+ // Restore game state
384
377
public AIPathNode ( BinaryReader inf )
385
378
{
386
379
ID = inf . ReadInt32 ( ) ;
@@ -401,7 +394,7 @@ public AIPathNode(BinaryReader inf)
401
394
Location . Location . Z = inf . ReadSingle ( ) ;
402
395
}
403
396
404
- // save game state
397
+ // Save game state
405
398
public void Save ( BinaryWriter outf )
406
399
{
407
400
outf . Write ( ID ) ;
@@ -429,7 +422,7 @@ public int FindTVNIndex(AIPathNode nextNode, TrackDatabaseFile TDB, TrackSection
429
422
int junctionIndexThis = JunctionIndex ;
430
423
int junctionIndexNext = nextNode . JunctionIndex ;
431
424
432
- // if this is no junction, try to find the TVN index
425
+ // If this is no junction, try to find the TVN index
433
426
if ( junctionIndexThis < 0 )
434
427
{
435
428
try
@@ -455,7 +448,7 @@ public int FindTVNIndex(AIPathNode nextNode, TrackDatabaseFile TDB, TrackSection
455
448
}
456
449
}
457
450
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.
459
452
var iCand = - 1 ;
460
453
for ( int i = 0 ; i < TDB . TrackDB . TrackNodes . Count ( ) ; i ++ )
461
454
{
@@ -516,7 +509,7 @@ public static int FindJunctionOrEndIndex(WorldLocation location, TrackDB trackDB
516
509
float dz = tn . UiD . Z - location . Location . Z ;
517
510
dz += ( tn . UiD . TileZ - location . TileZ ) * 2048 ;
518
511
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 ) ;
520
513
if ( bestDistance2 > d )
521
514
{
522
515
bestIndex = j ;
0 commit comments