-
Notifications
You must be signed in to change notification settings - Fork 706
Local train routing instead of global pathfinding #2473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…lection Replace fixed pathfinding with dynamic routing system featuring: - Local greedy routing: Trains evaluate neighbors based on profit potential, traffic congestion, distance, and recent history - Exploration capability: 10% randomness prevents suboptimal but discovers new routes - Congestion avoidance: Trains naturally spread to less busy stations - Loop prevention: Memory of recent visits prevents getting stuck - Adaptive behavior: System responds to changing network conditions - Enhanced journey tracking: Share complete route information instead of just start position Includes BATMAN-style routing protocol (currently disabled) for future network-wide knowledge distribution.
…urney transmission, journeySource wasnt loop proof
- 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
WalkthroughRefactors train traversal to local greedy routing with journey tracking and hop limits; adds station-originated routing broadcasts and traffic metrics; injects station tick calls; extends Game API and GameImpl with train arrival/hop-limit recording and runtime stats; invokes station cleanup on removal. Changes
Sequence Diagram(s)sequenceDiagram
participant GameLoop as Game.executeNextTick
participant GameImpl as GameImpl
participant StationExec as TrainStationExecution
participant Station as TrainStation
participant TrainExec as TrainExecution
GameLoop->>+GameImpl: executeNextTick()
GameImpl->>+StationExec: tick all stations
StationExec->>+Station: tick()
Note right of Station `#E8F0FF`: Broadcast/age routes\nupdate edge metrics & traffic
Station-->>-StationExec: done
StationExec->>GameImpl: spawn trains (if any)
GameImpl->>+TrainExec: tick trains
alt train moving
TrainExec->>Station: shareJourneyInfo() (optional)
TrainExec->>Station: chooseNextStation(destination,recent)
Station-->>TrainExec: nextHop / null
TrainExec->>TrainExec: processArrival/update journey
TrainExec->>GameImpl: recordTrainArrival(steps)
else hop limit exceeded
TrainExec->>GameImpl: recordTrainRemovedDueToHopLimit(steps)
end
GameImpl->>GameImpl: accumulate per-tick stats
alt every 60 ticks
GameImpl->>GameImpl: printTrainStats()
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Pre-merge checks✅ Passed checks (3 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…counting The new station-aware routing introduced a bug where train arrivals were being double-counted and termination conditions were incorrectly classified as arrivals. Problems fixed: - targetReached() was called twice for successful arrivals: once in getNextTile() when destination was reached, and again in tick() when no tile was returned - Trains removed due to hop limit were counted as successful arrivals - Trains stuck with no routing options were counted as successful arrivals Solution implemented: - Introduced MoveResult union type with explicit cases: "move", "arrived", "hopLimit", "stuck" to clearly distinguish termination conditions - Renamed getNextTile() to getNextStep() and changed return type to MoveResult - Removed targetReached() call from navigation logic to prevent double counting - Updated tick() method to use switch statement on MoveResult for proper handling - Ensured recordTrainArrival() only called for actual destination arrivals - Ensured recordTrainRemovedDueToHopLimit() only called for hop limit terminations - Stuck trains are deleted without recording any arrival statistics This ensures accurate train statistics tracking with the new routing system.
da8ae46 to
1d352e1
Compare
|
Personally, I'd say close this and work off the new branch you made, once it's ready, make a new PR using that branch |
evanpelle
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of local pathfinding. this approach seems a bit overly complex to me. is there a way to simplify this down to something like 100 lines?
|
Maybe discuss the changes here https://discord.com/channels/1359946986937258015/1441027562967928862 ? |
This is not yet a pr but a idea to tackle multiple train related issues. Feedback very welcome! Code needs cleanup!
Description:
Before: Trains followed fixed, pre-calculated paths between stations using a pathfinding algorithm
After: After: Trains now use a local cost based routing system.
Local Greedy Routing (Smart decision-making at each station)
At each station, trains evaluate neighboring stations based on multiple factors:
Profit potential: How much gold they can earn based on diplomatic relationships
Traffic congestion: Avoids overcrowded stations
Travel distance: Considers physical distance and travel time
Recent history: Avoids recently visited stations to prevent loops
Some randomness: chance of making a non-optimal choice for exploration
Key Benefits
Organic route discovery: Routes improve as trains explore the network
Congestion avoidance: Trains naturally spread out to less busy areas
Adaptive behavior: System responds to changing network conditions
Loop prevention: Memory of recent visits discourages trains from getting stuck
Currenlty disabled:
BATMAN like broadcast.
Stations broadcast routing information to neighbors like the BATMAN wireless mesh protocol Builds a distributed routing table that learns about the entire network over time Handles network topology changes automatically
Uses lazy cleanup to manage memory efficiently
Settings need to be tweaked
Performance impact is not yet measured, but could be positive.
Describe the PR.
Please complete the following:
Please put your Discord username so you can be contacted if a bug or regression is found:
DISCORD_USERNAME