Forwardmost Entity of a Train (Train direction)

Place to get help with not working mods / modding interface.
User avatar
ssilk
Global Moderator
Global Moderator
Posts: 12889
Joined: Tue Apr 16, 2013 10:35 pm
Contact:

Forwardmost Entity of a Train (Train direction)

Post by ssilk »

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?
Cool suggestion: Eatable MOUSE-pointers.
Have you used the Advanced Search today?
Need help, question? FAQ - Wiki - Forum help
I still like small signatures...
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Front-most Entity of a Train (Train direction)

Post by aubergine18 »

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.
User avatar
ssilk
Global Moderator
Global Moderator
Posts: 12889
Joined: Tue Apr 16, 2013 10:35 pm
Contact:

Re: Forwardmost Entity of a Train (Train direction)

Post by ssilk »

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.

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
I made some edits to the post, perhaps you might read it again. :)
Cool suggestion: Eatable MOUSE-pointers.
Have you used the Advanced Search today?
Need help, question? FAQ - Wiki - Forum help
I still like small signatures...
Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

Re: Forwardmost Entity of a Train (Train direction)

Post by Choumiko »

LuaTrain.speed is negative when the train moves in it's back direction, so something like

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)
should give you the unit_number of the correct stock.

Forwardmost locomotive is either LuaTrain.locomotives.front_movers[1] or back_movers[1]
User avatar
ssilk
Global Moderator
Global Moderator
Posts: 12889
Joined: Tue Apr 16, 2013 10:35 pm
Contact:

Re: Forwardmost Entity of a Train (Train direction)

Post by ssilk »

Ahhh.... the speed! Of course.... :shock:
Cool suggestion: Eatable MOUSE-pointers.
Have you used the Advanced Search today?
Need help, question? FAQ - Wiki - Forum help
I still like small signatures...
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Forwardmost Entity of a Train (Train direction)

Post by aubergine18 »

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.
User avatar
ssilk
Global Moderator
Global Moderator
Posts: 12889
Joined: Tue Apr 16, 2013 10:35 pm
Contact:

Re: Forwardmost Entity of a Train (Train direction)

Post by ssilk »

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.
Cool suggestion: Eatable MOUSE-pointers.
Have you used the Advanced Search today?
Need help, question? FAQ - Wiki - Forum help
I still like small signatures...
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Forwardmost Entity of a Train (Train direction)

Post by aubergine18 »

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.
User avatar
ssilk
Global Moderator
Global Moderator
Posts: 12889
Joined: Tue Apr 16, 2013 10:35 pm
Contact:

Re: Forwardmost Entity of a Train (Train direction)

Post by ssilk »

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.
Cool suggestion: Eatable MOUSE-pointers.
Have you used the Advanced Search today?
Need help, question? FAQ - Wiki - Forum help
I still like small signatures...
mrvn
Smart Inserter
Smart Inserter
Posts: 5873
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Forwardmost Entity of a Train (Train direction)

Post by mrvn »

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?
User avatar
ssilk
Global Moderator
Global Moderator
Posts: 12889
Joined: Tue Apr 16, 2013 10:35 pm
Contact:

Re: Forwardmost Entity of a Train (Train direction)

Post by ssilk »

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...
User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2920
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: Forwardmost Entity of a Train (Train direction)

Post by Optera »

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.
dgnuff
Burner Inserter
Burner Inserter
Posts: 15
Joined: Fri Feb 24, 2017 8:06 pm
Contact:

Re: Forwardmost Entity of a Train (Train direction)

Post by dgnuff »

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.
User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2920
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: Forwardmost Entity of a Train (Train direction)

Post by Optera »

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.
It's only ambiguous when a train is stopped in manual mode or can't find a path.

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
Post Reply

Return to “Modding help”