Skip to content

Commit d6c1dc4

Browse files
committed
Enhance train routing logic and memory management
- Added search radius - Updated several properties in TrainStation class to be readonly for better immutability and clarity. - Introduced heat decay interval and factor for more flexible heat management. - pre-computed decay factors avoiding Math.pow in critical paths. - Enhance logging - Refined routing logic - removed journeyPreviousStation property - removed RecentArrivals - unbounded StationTraffic.heat -> score can now be negative
1 parent fda1d39 commit d6c1dc4

File tree

4 files changed

+155
-98
lines changed

4 files changed

+155
-98
lines changed

src/core/execution/TrainExecution.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,20 @@ export class TrainExecution implements Execution {
2525
private speed: number = 2;
2626
// Journey tracking for organic route discovery - simplified to immediate neighbors only
2727
private hasProcessedArrival: boolean = false;
28-
private journeyPreviousStation: TrainStation | null = null; // Immediate previous station
2928
private journeyHopCount: number = 0;
3029

3130
// Local greedy routing properties
3231
private recentStations: TrainStation[] = []; // Recently visited stations (for loop prevention)
3332
private maxHops: number = 50; // Maximum hops before giving up
34-
private recentMemorySize: number = 30; // How many recent stations to remember
33+
private recentMemorySize: number = 50; // How many recent stations to remember
3534

3635
constructor(
3736
private railNetwork: RailNetwork,
3837
private player: Player,
3938
private source: TrainStation,
4039
private destination: TrainStation,
4140
private numCars: number,
42-
) {
43-
this.journeyPreviousStation = null; // Starting station has no previous
44-
}
41+
) {}
4542

4643
public owner(): Player {
4744
return this.player;
@@ -283,6 +280,9 @@ export class TrainExecution implements Execution {
283280
// Check if we've exceeded max hops
284281
if (this.journeyHopCount >= this.maxHops) {
285282
// Give up - we've wandered too long
283+
if (this.mg) {
284+
this.mg.recordTrainRemovedDueToHopLimit(this.journeyHopCount);
285+
}
286286
this.active = false;
287287
return null;
288288
}
@@ -320,7 +320,6 @@ export class TrainExecution implements Execution {
320320
// Update journey tracking - remember where we came from BEFORE changing currentStation
321321
// This should happen after arrival processing but before departure
322322
this.journeyHopCount++;
323-
this.journeyPreviousStation = this.currentStation;
324323

325324
this.currentStation = nextHop;
326325
this.currentRailroad = railroad;

src/core/game/Game.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@ export interface Game extends GameMap {
712712

713713
addExecution(...exec: Execution[]): void;
714714
recordTrainArrival(steps: number): void;
715+
recordTrainRemovedDueToHopLimit(steps: number): void;
715716
displayMessage(
716717
message: string,
717718
type: MessageType,

src/core/game/GameImpl.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ export class GameImpl implements Game {
7979
private unitGrid: UnitGrid;
8080

8181
// Train statistics tracking
82-
private trainArrivalTimes: number[] = []; // timestamps of recent train arrivals
83-
private completedTrainSteps: number[] = []; // steps of recently completed trains
82+
private arrivalsSinceLastPrint = 0;
83+
private completedStepsSinceLastPrint = 0;
8484
private activeTrainSteps = 0; // total steps taken by currently active trains (updated each tick)
8585
private lastStatsPrint = 0; // last time we printed stats
86+
private hopLimitRemovalsSinceLastPrint = 0;
8687

8788
private playerTeams: Team[];
8889
private botTeam: Team = ColoredTeams.Bot;
@@ -464,20 +465,12 @@ export class GameImpl implements Game {
464465

465466
// Train statistics tracking methods
466467
recordTrainArrival(steps: number) {
467-
this.trainArrivalTimes.push(this._ticks);
468-
this.completedTrainSteps.push(steps);
468+
this.arrivalsSinceLastPrint++;
469+
this.completedStepsSinceLastPrint += steps;
470+
}
469471

470-
// Clean up old data (keep only last 60 seconds)
471-
const cutoffTime = this._ticks - 60;
472-
this.trainArrivalTimes = this.trainArrivalTimes.filter(
473-
(time) => time > cutoffTime,
474-
);
475-
// Keep same number of completed train steps as arrival times
476-
if (this.completedTrainSteps.length > this.trainArrivalTimes.length) {
477-
this.completedTrainSteps = this.completedTrainSteps.slice(
478-
-this.trainArrivalTimes.length,
479-
);
480-
}
472+
recordTrainRemovedDueToHopLimit(_steps: number) {
473+
this.hopLimitRemovalsSinceLastPrint++;
481474
}
482475

483476
getActiveTrainCount(): number {
@@ -487,10 +480,8 @@ export class GameImpl implements Game {
487480
}
488481

489482
getAverageCompletedTrainSteps(): number {
490-
if (this.completedTrainSteps.length === 0) return 0;
491-
492-
const sum = this.completedTrainSteps.reduce((a, b) => a + b, 0);
493-
return sum / this.completedTrainSteps.length;
483+
if (this.arrivalsSinceLastPrint === 0) return 0;
484+
return this.completedStepsSinceLastPrint / this.arrivalsSinceLastPrint;
494485
}
495486

496487
getAverageActiveTrainSteps(): number {
@@ -502,16 +493,21 @@ export class GameImpl implements Game {
502493
}
503494

504495
printTrainStats() {
505-
const arrivalsLast60s = this.trainArrivalTimes.length;
496+
const arrivalsLastInterval = this.arrivalsSinceLastPrint;
506497
const activeTrains = this.getActiveTrainCount();
507498
const avgCompletedSteps =
508499
Math.round(this.getAverageCompletedTrainSteps() * 100) / 100;
509500
const avgActiveSteps =
510501
Math.round(this.getAverageActiveTrainSteps() * 100) / 100;
502+
const hopLimitRemovals = this.hopLimitRemovalsSinceLastPrint;
511503

512504
console.log(
513-
`🚂 Trains: ${arrivalsLast60s} arrived (${avgCompletedSteps} avg steps), ${activeTrains} active (${avgActiveSteps} avg steps)`,
505+
`🚂 Trains: ${arrivalsLastInterval} arrived (${avgCompletedSteps} avg steps), ${activeTrains} active (${avgActiveSteps} avg steps), ${hopLimitRemovals} removed (hop limit)`,
514506
);
507+
508+
this.arrivalsSinceLastPrint = 0;
509+
this.completedStepsSinceLastPrint = 0;
510+
this.hopLimitRemovalsSinceLastPrint = 0;
515511
}
516512

517513
playerView(id: PlayerID): Player {

0 commit comments

Comments
 (0)