Skip to content

Commit fda1d39

Browse files
committed
removed journey source and replaced it with recent stations during journey transmission, journeySource wasnt loop proof
1 parent 51c603a commit fda1d39

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

src/core/execution/TrainExecution.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/core/game/TrainStation.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -745,9 +745,16 @@ export class TrainStation {
745745
private processJourneyInformation(trainExecution: TrainExecution): void {
746746
const journeyInfo = trainExecution.shareJourneyInfo();
747747

748-
// Only process journey information if the train has established a journey source (visited a city/port)
749-
if (!journeyInfo.journeySource) {
750-
// Train hasn't visited a city/port yet, skip journey processing
748+
// Only process journey information if the train has visited cities/ports in its recent journey
749+
const hasVisitedMeaningfulStations = journeyInfo.routeInformation.some(
750+
(routeInfo) => {
751+
const stationType = routeInfo.destination.unit.type();
752+
return stationType === UnitType.City || stationType === UnitType.Port;
753+
},
754+
);
755+
756+
if (!hasVisitedMeaningfulStations) {
757+
// Train hasn't visited any cities/ports in its recent journey segment, skip journey processing
751758
return;
752759
}
753760

0 commit comments

Comments
 (0)