LuaTrain.driving_direction

User avatar
Wiwiweb
Long Handed Inserter
Long Handed Inserter
Posts: 76
Joined: Sat May 08, 2021 2:36 am
Contact:

LuaTrain.driving_direction

Post by Wiwiweb »

What?
Add an attribute on LuaTrain that would tell us if the train is currently going forwards or backwards. It would return a new define that has 2 possible values: forwards/backwards.
Why?
The rules for train directionality are esoteric. As mentioned in docs:
The front of the train is in the direction that a majority of locomotives are pointing in. If it's a tie, the North and West directions take precedence.
When setting the speed of an automatic train, the penalty for getting the sign of the speed wrong is a crash. ("Trying to change direction of automatic train moving on path").

Most commonly we can read train.speed and judge from the sign whether the train is going backwards or forwards. But when a train is stopped, its speed is 0 but it still secretly has a direction. That's when we run into danger.

Every so often we get a crash related to the Space Elevator when a player comes up with a whole new weird train setup that no one has tested, and it crashes because our hacks to detect directionality fail for their particular case.

I would love to be able to just do this:

Code: Select all

local speed_direction = new_carriage.train.driving_direction == defines.train_driving_direction.forwards and 1 or -1
new_carriage.train.speed = speed_direction * math.abs(old_train_speed)

... or letting us move a train that has 0 speed in any direction we want would also work. But I'm guessing there's some internal reason why this is currently the case.

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2764
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: LuaTrain.driving_direction

Post by boskid »

Implemented for 1.1.75 as LuaRailPath::is_front, because in the end this is the important bit which prevents setting speed of wrong sign (the train would be going back on the path, extending remaining path instead of consuming it). When is_front = true, speed must be >= 0, and when is_front = false, speed must be <= 0

User avatar
Wiwiweb
Long Handed Inserter
Long Handed Inserter
Posts: 76
Joined: Sat May 08, 2021 2:36 am
Contact:

Re: LuaTrain.driving_direction

Post by Wiwiweb »

That works, thanks!

Post Reply

Return to “Implemented mod requests”