@@ -24,7 +24,6 @@ export class TrainExecution implements Execution {
2424 private currentStation : TrainStation | null = null ;
2525 private speed : number = 2 ;
2626 // Journey tracking for organic route discovery - simplified to immediate neighbors only
27- private journeySource : TrainStation | null ;
2827 private hasProcessedArrival : boolean = false ;
2928 private journeyPreviousStation : TrainStation | null = null ; // Immediate previous station
3029 private journeyHopCount : number = 0 ;
@@ -41,12 +40,6 @@ export class TrainExecution implements Execution {
4140 private destination : TrainStation ,
4241 private numCars : number ,
4342 ) {
44- // Initialize journey tracking - journeySource is the first city/port visited
45- const sourceType = source . unit . type ( ) ;
46- this . journeySource =
47- sourceType === UnitType . City || sourceType === UnitType . Port
48- ? source
49- : null ;
5043 this . journeyPreviousStation = null ; // Starting station has no previous
5144 }
5245
@@ -58,7 +51,6 @@ export class TrainExecution implements Execution {
5851 * Share journey information with a station for organic route discovery
5952 */
6053 public shareJourneyInfo ( ) : {
61- journeySource : TrainStation | null ;
6254 routeInformation : Array < {
6355 destination : TrainStation ;
6456 nextHop : TrainStation | null ;
@@ -78,8 +70,22 @@ export class TrainExecution implements Execution {
7870 ? this . recentStations [ this . recentStations . length - 2 ]
7971 : null ;
8072
81- // Only share routes to stations we visited (not the current station we're at)
82- for ( let i = 0 ; i < this . recentStations . length - 1 ; i ++ ) {
73+ // Find the start index for sharing journey information
74+ // Only share information about stations visited since the last time we passed through the current station
75+ let startIndex = 0 ;
76+ const currentStation = this . recentStations [ this . recentStations . length - 1 ] ;
77+
78+ // Look for the last occurrence of current station before the current visit
79+ for ( let i = this . recentStations . length - 2 ; i >= 0 ; i -- ) {
80+ if ( this . recentStations [ i ] === currentStation ) {
81+ // Found the last previous visit to this station, start sharing from after that visit
82+ startIndex = i + 1 ;
83+ break ;
84+ }
85+ }
86+
87+ // Only share routes to stations we visited since our last visit to this station (not including current)
88+ for ( let i = startIndex ; i < this . recentStations . length - 1 ; i ++ ) {
8389 const destination = this . recentStations [ i ] ;
8490 // For reverse routing: to reach any destination, go through the station we came from
8591 const nextHop = immediatePrevious ;
@@ -94,7 +100,6 @@ export class TrainExecution implements Execution {
94100 }
95101
96102 return {
97- journeySource : this . journeySource ,
98103 routeInformation,
99104 } ;
100105 }
@@ -346,14 +351,6 @@ export class TrainExecution implements Execution {
346351 throw new Error ( "Not initialized" ) ;
347352 }
348353
349- // Set journeySource to first city/port visited (if not already set)
350- if ( this . journeySource === null ) {
351- const stationType = this . currentStation . unit . type ( ) ;
352- if ( stationType === UnitType . City || stationType === UnitType . Port ) {
353- this . journeySource = this . currentStation ;
354- }
355- }
356-
357354 this . currentStation . onTrainStop ( this ) ;
358355 }
359356
0 commit comments