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: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").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.
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.