Train control / rail signals

roothorick
Long Handed Inserter
Long Handed Inserter
Posts: 92
Joined: Mon Aug 11, 2014 5:22 am
Contact:

Train control / rail signals

Post by roothorick »

Rail signals do... something... to trains, but I can't find any Lua-visible state change whatsoever between a train that's en route or waiting at a station, and a train that's waiting at a signal. Is this undocumented, or simply not exposed yet?

I was hoping to do a simple little tweak, perhaps a circuit-aware rail signal that goes to red on a user-set condition, but came up empty. If the rail signal <-> train interaction isn't Lua accessible, what else can I do? Override/wrap Train somehow, and force my own UI and scripts?
MF-
Smart Inserter
Smart Inserter
Posts: 1235
Joined: Sun Feb 24, 2013 12:07 am
Contact:

Re: Train control / rail signals

Post by MF- »

+1 for that mod idea..
Rail signals controllable by circuit network is one of my dreams :)

what do you think will be more likely?
a) dedicated CCnet signal, which wouldn't separate blocks.
It's sole purpose would be to indicate the CCnet condition to the train, which then would or wouldn't pass

b) CCnet connection to existing signals.
There it would be harder to see whether it blocks because there are trains in the segment OR because of the CCnet condition.

c) CCnet connections for stations
JLBShecky
Long Handed Inserter
Long Handed Inserter
Posts: 60
Joined: Mon Jul 21, 2014 5:27 pm
Contact:

Re: Train control / rail signals

Post by JLBShecky »

As far as I have seen from my own attempts at making self routing trains, there is one thing that is not exposed by the LUA API that in my opinion is needed for a mod to be capable of directing trains to where the are needed, and four other API interfaces that although they could be "hacked" around, would make the management of the automated scheduling system much more user "friendly". If by Rail signals, you meant just 'Rail signal's and not 'Train stop's then the majority of this post is invalid as I in my opinion 'Train stop's are much more intesting as all 'Rail signal's from my perspective is help prevent crashs while letting more than one train run on a line of track, while 'Train stop's are what the game actually uses in it's path finding. As far as I know from my own poking around at train automation, there is no way to get the state of a Rail signal and although there is no way to tell how long a train has been at a Train stop, you can use the existing API to tell if it is at a stop and then if you so desire force it to move on to the next station in it's route.

The biggest show stopper at this point is the fact that at the moment trains schedules use train station custom names, and there is no way to get this information at the moment, shot of placing down a locomotive and adding every stop to it's schedule manually. Once you are able to get a station's custom name even without the other interfaces a mod should in theory be able to fully direct trains, simply set a train's destination in it's schedule, and tell the train to run in automatic mode, when it get's to the destination load or unload it as needed.

If we got access to station custom names then we run into the first cosmetic thing, the fact that there is no pretty way to interact with custom entities. If you have access to custom station names, and you are able to rename them then you should have everything that is needed to be able to recreate the vanilla station GUI plus add in any GUI logic for automatic train routing. Although it is not presently possible to detect when a player tries interact with an entity, one could create a "crowbar", much like the one in the in the one railroad mod for another game that I will not mention, that when you hold the "crowbar" in hand and click on train stop would bring up the custom routing GUI. Although this is not as nice as an api hook that would let us know when a play tries to interact with an entity and then cancel the default GUI and display our own, or the ability to access the entity GUI from LUA it would still work.

We currently don't have a direct way to know if a train can't reach the next destination, although we can hack it by forcing a train to try to head to the next destination on it's schedule and if it does not start moving then we can assume that there is no path. A direct interface in the train schedule recors list that says if the path is valid or such would be useful.

At the moment the way to give a smart "logistics" train stop the ability know if has or needs something would be to give the train stop the ability to watch chests. Although I was initially thinking that it would be best if the stop was able to pull circuit states off the network, in most situations it would be better/faster to link chests to the train stop, most likely using the previously mentioned "crowbar".

Inventory slots on Cargo wagons are able to be set by the player but not read/set via the LUA API, if had the ability to modify these from LUA then we should be able to then pull a train up to a station that had multiple things in stock and only take the things that we needed. Again it is not something that is really needed, but is something that could improve usability.

The TL;DR version:

The current Show stopper:
Add Setter/getter for Train stop custom names

Code: Select all

trainstop.getcustomname()
trainstop.setcustomname("New name")
API interfaces that would make Automated trains nicer for moders:
Either cancelable entity interaction or the ability to access the entity GUI.

Code: Select all

game.onevent(defines.events.ontick, function(event)
  if event.interactedentity.name == "circuit-train-stop" then
    -- TODO: Show custom GUI
    -- Prevent the default GUI from showing
    return false;
  end
 end);
A way to check if the current path for a train is viable

Code: Select all

train.schedule.records[x].valid

Enable checking if a circuit state is true on smart inserters.

Code: Select all

smartinserter.getcircuitstate(defines.circuitconnector.red)
Let Cargo Wagon filters be set/read via lua and not just the player.
MF-
Smart Inserter
Smart Inserter
Posts: 1235
Joined: Sun Feb 24, 2013 12:07 am
Contact:

Re: Train control / rail signals

Post by MF- »

I seek far simpler solution.
A way how to make a train depart / inhibit it's departure on a circuit network signal
JLBShecky
Long Handed Inserter
Long Handed Inserter
Posts: 60
Joined: Mon Jul 21, 2014 5:27 pm
Contact:

Re: Train control / rail signals

Post by JLBShecky »

MF- wrote:I seek far simpler solution.
A way how to make a train depart / inhibit it's departure on a circuit network signal
Well if smart inserters had the ability to check the status of a circuit condition to see if it was in the pass or fail condition, I guess that one could mod one so that it would put a train that is on an adjacent track into manual mode while the condition was false and then put the train back into automatic operation when the state was true. It wouldn't be optimum but it should work as a rough hack, but it would require getting the ability to check the circuit connection state.

At the moment I am not really able to mentally visualize how normal signals could really be used for much other than basic block handling due to how I have seen the train path finding seems to work. Without being able to tie direcltly into the train path finding code there really isn't much that I can't be done using the games native pathing using stations. I am probably missing something, so if you could give an example to show me what I am missing it would probably help me a tad.
MF-
Smart Inserter
Smart Inserter
Posts: 1235
Joined: Sun Feb 24, 2013 12:07 am
Contact:

Re: Train control / rail signals

Post by MF- »

I guess that would work fine as long as the script could be alerted when the train arrives,
so it can put it into manual mode immediately instead of requiring larger in-station wait time.

If the status of a smart inserter be read by a script.... then it's "running / paused" state could be abused for reading and configuring the CCnet condition.
As long as it would work, I wouldn't mind placing a smart inserter next to the station as-if there was something to unload from it.


@ signals
You might be right.
Overriding signals is likely way more likely to cause blockages than I initially thought.
Cargo cannot be unloaded from trains stopped on signals anyway...
For train depots I'll be definitely better off using stations.

The only usecase I can think about now is a pedestrian railway crossing,
where one would insert a piece of iron into a "paywall" chest, which would then guarantee him 5 train-free seconds :P
JLBShecky
Long Handed Inserter
Long Handed Inserter
Posts: 60
Joined: Mon Jul 21, 2014 5:27 pm
Contact:

Re: Train control / rail signals

Post by JLBShecky »

MF- wrote:I guess that would work fine as long as the script could be alerted when the train arrives,
so it can put it into manual mode immediately instead of requiring larger in-station wait time.
Being able to start and stop the trains is relatively simple, a mod just keeps track of any entities of type "train-stop" (to allow listening to other mods custom train stops) and then check to see if there is a train on the track next to it. If the check is only performed once every half second then it should catch any train that stops the station for at least a second. When whatever condition you have set for the train occurs, the mod could simply switch the train back from manual mode and the train would continue with it's route.
MF- wrote:If the status of a smart inserter be read by a script.... then it's "running / paused" state could be abused for reading and configuring the CCnet condition.
As long as it would work, I wouldn't mind placing a smart inserter next to the station as-if there was something to unload from it.
The only problem with try to abuse the active state of a smart inserter is that it has 2 "fail" conditions, when it has nothing to move and when there is no space for it to place it's load, which could result in a train heading out to pick up a load of iron when it was already backlogged. Although you could use a smart inserter for it's filter conditions, and then leave based on the conditions based on the contents of the train. For example you could put a smart inserter with a red circuit condition of equals 0 iron and a green circuit condition of greater than 1000 ammo, and the station would wait till those condition were met in the trains cargo to head out.
MF- wrote:The only usecase I can think about now is a pedestrian railway crossing,
where one would insert a piece of iron into a "paywall" chest, which would then guarantee him 5 train-free seconds :P
Technically one could fake a player crossing "block" using a modded piece of track, and if there is an entity of type player on/near the track it would scan for any locomotives in a fixed distance from the track, put them into manual mode, and then innate and abrupt breaking. At the moment one can't use a fake player to slow down a train, so the train would need to be manually stopped, but with a little work it should be doable. I'll have to add this to list of things to add to my proof of concept railroad controls mod idea come to think of it.

That being said a tool chest that stops train would also be very doable, as it would just have a single inventory slot and every so often it would check to see if something is in the chest, then it would consume one item from the chest. (multiple items could give you extend time.) When the stopper chest was active it would then search for locomotives near it, put them into manual mode till the timer ran out and then go inactive.
MF-
Smart Inserter
Smart Inserter
Posts: 1235
Joined: Sun Feb 24, 2013 12:07 am
Contact:

Re: Train control / rail signals

Post by MF- »

Sounds neat.

Too bad the game can't let you know the train arrived and you are forced to check on a timer.
I am not a fan of that approach.

@ smart inserter..
I would use a dedicated one, which would attempt (and always fail) to grab stuff from the station it "controls"
MF-
Smart Inserter
Smart Inserter
Posts: 1235
Joined: Sun Feb 24, 2013 12:07 am
Contact:

Re: Train control / rail signals

Post by MF- »

Yay, seems 0.11 has the right event for proper train-control:
ontrainchangedstate: passes the train

or is that not useful?
JLBShecky
Long Handed Inserter
Long Handed Inserter
Posts: 60
Joined: Mon Jul 21, 2014 5:27 pm
Contact:

Re: Train control / rail signals

Post by JLBShecky »

MF- wrote:Yay, seems 0.11 has the right event for proper train-control:
ontrainchangedstate: passes the train

or is that not useful?
0.11 Change Log wrote: Lua API for reading backer names of entities (and writing station-names).
I haven't had a chance to play around in 0.11 as of yet, but the ontrainchangedstate does show promise in a way that I had not thought of as a requirement. Combined with the ability to read the name of the station that the strain is located at means that one should be able to track stations as they are placed down (or do a search for them since if I am remember correctly one of the api changes for the mapping stuff lets you get a bounding box of the generated world) and then ask the stations to tell you what are their names.

I hadn't thought about a ontrainchangedstate event, but it really reduces the work when you are using a partially automatic control system that only tells the trains where to go and does not do the driving on it's own, as you don't have to do a check every tick and can let game let you know when a train has stopped so you can plan it's next route depending on network demand.

Now all that I really need to do is to see if I can figure out a way to figure out all valid destinations based on the current station/train and I should have a working model of how everything should work for an automated train network. I think I could probably do it with a fake locomotive, but having some way to call the same path finder that trains use to navigate to be able to ask it "Can I get from this track to this station?" would be useful. Although in general practice it might be better to just ask "Can this train get to station X?", and giving a train a destination and some fuel, then setting it's destination, telling it to go and then waiting to see if it changes its state would also work.

Anyways I'll have to dig into this as soon as I have the time once I am done working on the other mod that I help out with has updated so I don't have to split my attention to see if I can finally have "real" fun with factorio modding.
mazetar
Burner Inserter
Burner Inserter
Posts: 17
Joined: Fri Jan 02, 2015 7:45 am
Contact:

Re: Train control / rail signals

Post by mazetar »

Hoping we get more hooks into signals and trains and their pathing in 0.13 :)
Post Reply

Return to “Implemented mod requests”