Forwardmost Entity of a Train (Train direction)
Forwardmost Entity of a Train (Train direction)
How can I know which is the forwardmost entity of a train?
Better explained: There is a front- and a back_stock. Which returns the first and last entity of a train. But how do I know, which one is now driving into front?
Differently explained: When a train is driving, the front gets blue lights, the back gets red lights. I want to know, which carriage is currently the front and which the back.
[Perhaps some function like train_direction(locomotive)
and it returns either 1 or -1 for currently driving into front or back-direction of train, so that I know if -1 then I need to pickup the back_stock for the forwardmost entity....]
PS: Also interesting: Which is the forwardmost locomotive?
Better explained: There is a front- and a back_stock. Which returns the first and last entity of a train. But how do I know, which one is now driving into front?
Differently explained: When a train is driving, the front gets blue lights, the back gets red lights. I want to know, which carriage is currently the front and which the back.
[Perhaps some function like train_direction(locomotive)
and it returns either 1 or -1 for currently driving into front or back-direction of train, so that I know if -1 then I need to pickup the back_stock for the forwardmost entity....]
PS: Also interesting: Which is the forwardmost locomotive?
Cool suggestion: Eatable MOUSE-pointers.
Have you used the Advanced Search today?
Need help, question? FAQ - Wiki - Forum help
I still like small signatures...
Have you used the Advanced Search today?
Need help, question? FAQ - Wiki - Forum help
I still like small signatures...
- aubergine18
- Smart Inserter
- Posts: 1264
- Joined: Fri Jul 22, 2016 8:51 pm
- Contact:
Re: Front-most Entity of a Train (Train direction)
proximity to front_rail or front_stock? http://lua-api.factorio.com/latest/LuaT ... l#LuaTrain
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
Re: Forwardmost Entity of a Train (Train direction)
No, depended to current driving direction.
The train doesn't change it's front and back, I tested that. it keeps the same, no matter in which direction I drive or in which cart I enter or if in automatic or manual mode.
I made some edits to the post, perhaps you might read it again.
The train doesn't change it's front and back, I tested that. it keeps the same, no matter in which direction I drive or in which cart I enter or if in automatic or manual mode.
Code: Select all
/c game.player.print("FRONT STOCK" .. game.player.selected.train.front_stock.unit_number);
game.player.print("BACK STOCK" ..game.player.selected.train.back_stock.unit_number);
for i, loco in pairs(game.player.selected.train.locomotives.front_movers) do
game.player.print("Front Mover " .. i .. " . " .. loco.unit_number)
end
for i, loco in pairs(game.player.selected.train.locomotives.back_movers) do
game.player.print("Back Mover " .. i .. " . " .. loco.unit_number)
end
for i, carr in pairs(game.player.selected.train.carriages) do
game.player.print("Carr " .. i .. " . " .. carr.unit_number)
end
Cool suggestion: Eatable MOUSE-pointers.
Have you used the Advanced Search today?
Need help, question? FAQ - Wiki - Forum help
I still like small signatures...
Have you used the Advanced Search today?
Need help, question? FAQ - Wiki - Forum help
I still like small signatures...
Re: Forwardmost Entity of a Train (Train direction)
LuaTrain.speed is negative when the train moves in it's back direction, so something like
should give you the unit_number of the correct stock.
Forwardmost locomotive is either LuaTrain.locomotives.front_movers[1] or back_movers[1]
Code: Select all
/c game.player.print(game.player.selected.train.speed < 0 and game.player.selected.train.back_stock.unit_number or game.player.selected.train.front_stock.unit_number)
Forwardmost locomotive is either LuaTrain.locomotives.front_movers[1] or back_movers[1]
Re: Forwardmost Entity of a Train (Train direction)
Ahhh.... the speed! Of course....
Cool suggestion: Eatable MOUSE-pointers.
Have you used the Advanced Search today?
Need help, question? FAQ - Wiki - Forum help
I still like small signatures...
Have you used the Advanced Search today?
Need help, question? FAQ - Wiki - Forum help
I still like small signatures...
- aubergine18
- Smart Inserter
- Posts: 1264
- Joined: Fri Jul 22, 2016 8:51 pm
- Contact:
Re: Forwardmost Entity of a Train (Train direction)
But then what happens when it's stopped at station or signal?
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
Re: Forwardmost Entity of a Train (Train direction)
Well, as I understand it yet:
- A train has a train-direction, no matter, if it is running or not. Or differently explained: we need to begin with counting at the front of train.
- This direction doesn't change over time. Never. Front keeps front.
- Now you can drive the train in one of two directions: Same direction as train direction: Speed is positive. Opposite direction: Speed is negative.
- When the speed is 0, the direction is the direction of the train. Zero is in this case positive.
- A train has a train-direction, no matter, if it is running or not. Or differently explained: we need to begin with counting at the front of train.
- This direction doesn't change over time. Never. Front keeps front.
- Now you can drive the train in one of two directions: Same direction as train direction: Speed is positive. Opposite direction: Speed is negative.
- When the speed is 0, the direction is the direction of the train. Zero is in this case positive.
Cool suggestion: Eatable MOUSE-pointers.
Have you used the Advanced Search today?
Need help, question? FAQ - Wiki - Forum help
I still like small signatures...
Have you used the Advanced Search today?
Need help, question? FAQ - Wiki - Forum help
I still like small signatures...
- aubergine18
- Smart Inserter
- Posts: 1264
- Joined: Fri Jul 22, 2016 8:51 pm
- Contact:
Re: Forwardmost Entity of a Train (Train direction)
I imagine also that in arrays of locos or cargo, the numeric index will be in correct order also - so 1 = first (original front) loco or cargo array.
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
Re: Forwardmost Entity of a Train (Train direction)
Yes, AFAIK the order never changes. What changes is how you need to iterate them.
IMHO what is really missing: a flag what is was the last/is the current direction, in which the train has been/is driving? That would enable to find out the "right" direction, even if the train currently is standing still.
IMHO what is really missing: a flag what is was the last/is the current direction, in which the train has been/is driving? That would enable to find out the "right" direction, even if the train currently is standing still.
Cool suggestion: Eatable MOUSE-pointers.
Have you used the Advanced Search today?
Need help, question? FAQ - Wiki - Forum help
I still like small signatures...
Have you used the Advanced Search today?
Need help, question? FAQ - Wiki - Forum help
I still like small signatures...
Re: Forwardmost Entity of a Train (Train direction)
What about if you have two locomotives?
The automatic trains never go backward so to go back and forth without turning you add 2 locomotives facing the opposite way so one always goes forward. Now does it actually go forward or does it go backward (speed negative) when it switches to the other locomotive?
The automatic trains never go backward so to go back and forth without turning you add 2 locomotives facing the opposite way so one always goes forward. Now does it actually go forward or does it go backward (speed negative) when it switches to the other locomotive?
Re: Forwardmost Entity of a Train (Train direction)
Tested and no: the train keeps the direction and order.
Cool suggestion: Eatable MOUSE-pointers.
Have you used the Advanced Search today?
Need help, question? FAQ - Wiki - Forum help
I still like small signatures...
Have you used the Advanced Search today?
Need help, question? FAQ - Wiki - Forum help
I still like small signatures...
Re: Forwardmost Entity of a Train (Train direction)
I can't believe this is not yet fixed.....
There's no sane way (measuring negative train speed is quite insane when you need orientation of a stationary train) through the API to determine which way a train faces.
There's no sane way (measuring negative train speed is quite insane when you need orientation of a stationary train) through the API to determine which way a train faces.
My Mods: mods.factorio.com
Re: Forwardmost Entity of a Train (Train direction)
Part of the problem is that it's completely ambiguous which is the front and which is the back if it's a double headed train stopped on a track that permits two way traffic.
That said, a couple of heuristics that can help:
1. If the train is on a one way track, then the front is possible to determine. I'm just not sure how we can do this from a mod, if at all.
2. A train can't be stopped at two stations at the same time, even if they're placed correctly to enclose a double headed train. Therefore when stopped at a station it's always possible to figure which is the front.
For point #2, we can do that ourselves now. If the train is stopped at a station, get the position of the front_stock, the back_stock and the station. Whichever of front_stock and back_stock is closest to to the station is the real front.
That said, a couple of heuristics that can help:
1. If the train is on a one way track, then the front is possible to determine. I'm just not sure how we can do this from a mod, if at all.
2. A train can't be stopped at two stations at the same time, even if they're placed correctly to enclose a double headed train. Therefore when stopped at a station it's always possible to figure which is the front.
For point #2, we can do that ourselves now. If the train is stopped at a station, get the position of the front_stock, the back_stock and the station. Whichever of front_stock and back_stock is closest to to the station is the real front.
Re: Forwardmost Entity of a Train (Train direction)
It's only ambiguous when a train is stopped in manual mode or can't find a path.dgnuff wrote:Part of the problem is that it's completely ambiguous which is the front and which is the back if it's a double headed train stopped on a track that permits two way traffic.
That said, a couple of heuristics that can help:
1. If the train is on a one way track, then the front is possible to determine. I'm just not sure how we can do this from a mod, if at all.
2. A train can't be stopped at two stations at the same time, even if they're placed correctly to enclose a double headed train. Therefore when stopped at a station it's always possible to figure which is the front.
For point #2, we can do that ourselves now. If the train is stopped at a station, get the position of the front_stock, the back_stock and the station. Whichever of front_stock and back_stock is closest to to the station is the real front.
Driving:
Front should be pretty obvious, it's just not available through the api and we have to do weird things like check for negative speed.
Stopped at signal:
Internally the game keeps track of the driving direction. Again it's just not available through the api.
Stopped in station:
As you said, the side closest to the stop is front.
I made a request for the api having a flag for these situations: viewtopic.php?f=28&t=43418
My Mods: mods.factorio.com