Logging train entering/leaving the station for analytical purposes

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Post Reply
Posts: 1
Joined: 11 Apr 2021 08:26

Logging train entering/leaving the station for analytical purposes

Post by knotekjaroslav »

Hello developers,


I want to log VehicleId, StationId and time when a train enters/leaves a station for some analytical purposes.

Is the Vehicle_EnterStation in station_cmd.cpp file a good place to track the entering?

I struggle to find the place for logging the leaving event though. So far, I've found only CheckNextTrainTile in train_cmd.cpp.

The reason for that is below.

Thank you.

Long Version:
I have been playing the game for a while and created an infrastructure for trains that has it's quirks. The infrastructure runs fine 90% of the time but the rest 10% it takes the train, under yet unknown circumstances, much more time to deliver the cargo. It creates delays in cargo resulting in loss of "Gung Ho" moment (playing FIRS). I started thinking about the possible improvements and applied some but I wasn't able to fix the issue completely.

I decided to start investigating the source cause thinking about that even though I am playing a game I am as frustrated as when I debug some code. This gave me an idea about a possible solution but more importantly about something else.

I am a software engineer working in a domain of internet-of-things. I started thinking about trains arriving to/departing from some station as "events". With collecting such events and implementing some basic statistics I would be able to say, how long it should take for a train to run it's orders and detect quickly when somethings is a bit off (hopefully before it leads to a chain reaction) by comparing the usual time spent on this segment (path between two stations) with the current time. With the knowledge, of when the train leaves, I can also detect that train is waiting way too long for its cargo. Consider this to be more of a pet project than a serious OTTD feature.

For this feature, I would like to collect StatsionId, VehicleId, DateTime of the event (ideally also the number of the respective timetable order to identify first and the last station). I forked the repo, started looking around and identified a place that would be great to emit the arrival event. The place is in file station_cmd.cpp Vehicle_EnterStation. That is the place where I have the vehicle (which in turn has the it's timetable orders), station but I don't know where do I get game time.

I haven't seen any specific part in the code dedicated to the vehicle leaving the station. I have seen come across this piece of code (CheckNextTrainTile in train_cmd.cpp, there seems to be a case when a train left) but I don't think it's a good place. It's specific to the Trains, so should I want to make this into a general feature encompassing the road vehicles as well, this would be a problem.

Just for clarity, the events will be somehow passed to a standalone application which will perform the analysis. For starters I would just start the game and write data to CSV. Then after a while I would just pass the data to it for an offline analysis. If this works then I will start thinking about something real-time.

What is your opinion about the project?
- Does it make sense to you?
- Is there anybody doing the same thing I could just reuse (I made research and found nothing particularly helpful)?

How about the implementation?
- Is the place `Vehicle_EnterStation` place good for publishing the event or you would suggest something better?
- Where should I log that the vehicle left event? The proposed location does not seems to be the best one
- Do you have any suggestion about storing the events or passing them downstream?
- I've seen that you can configure the OTTD logging so it writes data to the standard output, which I can also use. Could it be also used?

Any other suggestion is welcomed.

Thank you very much.
Traffic Manager
Traffic Manager
Posts: 186
Joined: 14 Apr 2017 15:51
Location: Czech Republic

Re: Logging train entering/leaving the station for analytical purposes

Post by agentw4b »

I don't have a solution to your problem, but I've noticed that you're trying to fix it by editing the source code. Maybe it would go a different way, using the GS or AI API.

GS API : https://docs.openttd.org/gs-api/annotated.html

AI API: https://docs.openttd.org/ai-api/annotated.html

But I don't know if there is a solution :-)
Owner and admin of servers:Experimental games 01 (, Experimental games 02 (, Experimental games 03 (, Experimental games 04 (, Experimental games 05 (
My heightmaps: Flat Earth Map and United nations logo
My scenarios: Game Fallout 1,2,3 Map scenario
My gamescripts: City Founder GS
User avatar
Posts: 1334
Joined: 08 Jan 2003 23:09
Location: Denmark

Re: Logging train entering/leaving the station for analytical purposes

Post by jfs »

The GS and AI APIs don't have a great way to monitor those events. You would need to continually poll all vehicles if you were to use that. Making a modified client is the right approach if you want to add detailed analytics/logging like that.

If you want to log a vehicle that has stopped at a station and is about to begin unloading and loading cargo, then my best suggestion is Vehicle::BeginLoading(), also in vehicle.cpp.
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 12 guests