Train coupling and decoupling

Place to get help with not working mods / modding interface.
Post Reply
katyal
Fast Inserter
Fast Inserter
Posts: 208
Joined: Wed Nov 12, 2014 8:42 pm
Contact:

Train coupling and decoupling

Post by katyal »

I'm fully expecting to be disappointed but I still have to ask: Are the necessary functions to couple and decouple wagons on a train exposed via the modding api? I've got an idea for a mod but no sense going any further if it can't be done

MF-
Smart Inserter
Smart Inserter
Posts: 1235
Joined: Sun Feb 24, 2013 12:07 am
Contact:

Re: Train coupling and decoupling

Post by MF- »

As far as I know, there isn't even an API for manual train driving.
Without that, good luck with coupling and decoupling.

I am aware of:
API to tell you a train has changed state (Hooray, no more polling)
API to manipulate train mode (auto<->manual)
API to manipulate train schedule

JamesOFarrell
Filter Inserter
Filter Inserter
Posts: 402
Joined: Fri May 23, 2014 8:54 am
Contact:

Re: Train coupling and decoupling

Post by JamesOFarrell »

While there is no way to decouple a train there is nothing stopping you from deleting a train and replacing it with 2 trains made up of the wagons. You could do the reverse as well.

katyal
Fast Inserter
Fast Inserter
Posts: 208
Joined: Wed Nov 12, 2014 8:42 pm
Contact:

Re: Train coupling and decoupling

Post by katyal »

Too bad, would have been fun to try and implement an automated rail yard

JLBShecky
Long Handed Inserter
Long Handed Inserter
Posts: 60
Joined: Mon Jul 21, 2014 5:27 pm
Contact:

Re: Train coupling and decoupling

Post by JLBShecky »

Yeah trying to do an automated railyard would be a fun experiment/learning experience. I keep on wanting to lay the ground work for something along that line, but I keep on getting caught up in playing this game called factorio instead. :D

That said, it would probably be a good idea to request the ability to connect/disconnect trains in the Modding Interface Requests forum as we have the best chance of getting that ability if we ask for it.

katyal
Fast Inserter
Fast Inserter
Posts: 208
Joined: Wed Nov 12, 2014 8:42 pm
Contact:

Re: Train coupling and decoupling

Post by katyal »

AS MF- stated though we would also need access to manual train controls in the api for the coupling and decoupling to be of any use.

JLBShecky
Long Handed Inserter
Long Handed Inserter
Posts: 60
Joined: Mon Jul 21, 2014 5:27 pm
Contact:

Re: Train coupling and decoupling

Post by JLBShecky »

Although there is no access to the trains path finder it still is possible to manually drive a train using a few hacks per say. That said aside from not being able to separate cars, and having to create your own pathfinder in lua and potentially have issues with 3 way turnouts driving trains manually via scripts is doable as it stands, it just takes some finagling with the game.

A mod can manually control a train's movement using a fake player, in all way accempt breaking at the moment including breaking and turning.

Basically you make a fake player:

Code: Select all

driver = game.createentity({name="player", poistion={x0,y0}});
Get the loco that you want them to drive, and tell them to ride in it:

Code: Select all

loco = game.player.selected;
loco.passenger = driver;
Then you can control the movement of the train using the driver's riding state:

Code: Select all

driver.ridingstate = {direction=1, acceleration=0};
Acceleration has four possible values:

Code: Select all

0 - No affect on the locomotive's acceleration.
1 - Accelerate in the direction that the locomotive is facing.
2 - Decelerate the locomotive till it comes to a stop.
3 - Accelerate in the opposite direction that the locomotive is facing.
Direction has three possible values: (This applies to going both backwards or forwards.)

Code: Select all

0 - At a turnout take the left most route
1 - At a turnout take the straight path if possible. (If you come to a Y it is direction dependent.)
  Up/Left/Right - The train will take the route to the right.
  Down - The train will take the path to the left.  
2 - At a turnout take the right most route.
With that knowledge and then writing some code to find the path to where you want to go, or maybe making some sort of system with chests and blueprints to give directions to the train you can give a train directions via a mod and the only thing that is prevent it from being able to make an automated yard is beign able join and break the trains.
`

n9103
Smart Inserter
Smart Inserter
Posts: 1067
Joined: Wed Feb 20, 2013 12:09 am
Contact:

Re: Train coupling and decoupling

Post by n9103 »

Colonel Failure wrote:You can lose your Ecologist Badge quite quickly once you get to the point of just being able to murder them willy-nilly without a second care in the world.

katyal
Fast Inserter
Fast Inserter
Posts: 208
Joined: Wed Nov 12, 2014 8:42 pm
Contact:

Re: Train coupling and decoupling

Post by katyal »

Interesting, thank you very much ... I'm gonna start playing around with all of this and if I can get to the point where all I'm missing is the coupling and decoupling I'll request those on the api request forum.

JLBShecky
Long Handed Inserter
Long Handed Inserter
Posts: 60
Joined: Mon Jul 21, 2014 5:27 pm
Contact:

Re: Train coupling and decoupling

Post by JLBShecky »

I'm not sure what the Train object's wiki page has to do with this subject. The info that I posted does not relate to the train object, as everything that I referenced was part of the vehicle/player entities.

Currently there isn't a page on the wiki for vehicle's lua API and I am not sure exactly where the passenger member originates from, whether it is the vehicle or the rolling stock thought I would assume that the car/tank has the same property.

As for the character's ridingstate, yeah it is not documented on the wiki, but when I discovered it originally I couldn't figure out how to get the train to stop/go backwards with it at the time, since I thought that it was not possible to drive a vehicle backwards I had chalked it up to a partial exposed feature that was not meant to be such and thus a "hack". It wasn't till I started to play around with it again to write the above post that I discovered that what values needed to be passed to go backwards and the exact mechanics for how the train chooses it's path based on the ridingstate's direction. Most of the details in the post were written up as I worked them out, and I haven't had the time to triple check them much less get them on the wiki, though anyone is free to do such if they desire.
katyal wrote:Interesting, thank you very much ... I'm gonna start playing around with all of this and if I can get to the point where all I'm missing is the coupling and decoupling I'll request those on the api request forum.
I'd deferentially be interested in seeing what you can come up with, and if I am able to help I'd be more than willing to do such, I've kept on meaning to try something along this line but I always find an excuse to put it off for one reason or another. If you do go on ahead and start on it I've got a couple other half backed/implemented things that relate to automated train control/routing that I'd be more than willing to share as if I am honest with myself I may never actually get around to actually fully implementing them into an actual mod, and it would be a shame to never make use of them if someone else is interested in them.

katyal
Fast Inserter
Fast Inserter
Posts: 208
Joined: Wed Nov 12, 2014 8:42 pm
Contact:

Re: Train coupling and decoupling

Post by katyal »

I'm really at the concept stage right now. Basically I saw a pic of a railyard somewhere and thought "That would be a cool mod project for Factorio". My vision is still foggy about what this would actually accomplish.

My ideas so far:

- Outpost would fill wagons
- A locomotive follows a route collecting full wagons at outposts
- Many routes feed into the rail yard
- Wagons are decoupled from trains and sorted in the yard
- Make it so yards can "build" trains according to what the player wants (for example imagine having an outpost that produces just electronic circuits, you tell the yard you want a train with one wagon of iron and three wagons of copper)

I think this would be something for the endgame ... Massive decentralized factory with outposts acting as assemblers and trains acting as belts.

I'd like to hear what others where picturing when they thought of railyards because all of a sudden this feels more like it would be trying to create the "end game" as a mod.

JLBShecky
Long Handed Inserter
Long Handed Inserter
Posts: 60
Joined: Mon Jul 21, 2014 5:27 pm
Contact:

Re: Train coupling and decoupling

Post by JLBShecky »

I haven't tried playing around with the blueprints as of yet, but one might be able to use them to store information, or baring that a requester chest with no inventory slots but enough logistics slots to set the required settings. Come to think of it the logistics chest sounds like the best idea for setting the inputs/outputs of a station for automated routing. Basically have two custom requester chest with no inventory slots, one that says what the station need and it's quantities and one that says what it provides/quantities.

The big problem that I see in doing a traditional yard is that as it stands breaking the trains and then rejoining them would not be all that economical in my opinion, every station would need two train stops, unless all the routing was done manually. Although you could in theory make it so that the train stop was on the mainline, then the locomotive would stop there and drop off the wagons, pull past the turnout and then back up to the loaded cars, back up and hook the unloaded cars on it's front, back on up then pull forwards and grab the empty cars with the front of the locomotive back up again and then push the empty cars on to the siding and then finally back up one last time after decoupling the empty cars, and then you can be on your way.

Image

Granted there are better orders to do it in, but the basic concept is the same, and you would have to make your switcher much more efficient than a vanilla locomotive or it would just be wasting fuel when you could have had it sitting there all the time. Oh and the locomotive recipe would need to be more expensive so as to give more incentive to run with fewer locos.

Another thing is that one would probably need to make different types of wagons that can only carry certain types of goods to encourage (force) train building that one would want to automate. The one way that it see that happening is to copy the vanilla wagon prototype and possibly just add a new tint to it and have a different name so that you don't have to have tons of extra vram being used. you would have to play with wagon filters and either have a hand help tool that is used to set the filters and/or a building/track that would set the filter as cars passed over it on their way out if the yard to limit extra api calls. So when the yard needed to build an ore train a switch would grab some ore cars and as they went past the staging track they would have their filters set to the type of ore that they would need to hold.

I am wondering how well it would work out in terms of playability at the moment. The model RR in me really wants to do it but it would be so much work.

katyal
Fast Inserter
Fast Inserter
Posts: 208
Joined: Wed Nov 12, 2014 8:42 pm
Contact:

Re: Train coupling and decoupling

Post by katyal »

JLBShecky wrote:I am wondering how well it would work out in terms of playability at the moment. The model RR in me really wants to do it but it would be so much work.
Thats how I'm feeling too, unfortunately this might be a case of having a solution in search of a problem

n9103
Smart Inserter
Smart Inserter
Posts: 1067
Joined: Wed Feb 20, 2013 12:09 am
Contact:

Re: Train coupling and decoupling

Post by n9103 »

Half the purpose was to point out the fact that the schedules are available for scripting, which is generally a preferable way of sending trains around, since the schedule can deal with walls and signals far better than manual control.
The other half is pointing out the lack of documentation for the control technique you're describing.
Colonel Failure wrote:You can lose your Ecologist Badge quite quickly once you get to the point of just being able to murder them willy-nilly without a second care in the world.

JLBShecky
Long Handed Inserter
Long Handed Inserter
Posts: 60
Joined: Mon Jul 21, 2014 5:27 pm
Contact:

Re: Train coupling and decoupling

Post by JLBShecky »

n9103 wrote:Half the purpose was to point out the fact that the schedules are available for scripting, which is generally a preferable way of sending trains around, since the schedule can deal with walls and signals far better than manual control.
The other half is pointing out the lack of documentation for the control technique you're describing.
Although schedules are available for scripting, and have been for long than I have been playing factorio, they don't cover all use casees, just the ones that modders are most likely to use. :D

In the context of this topic where you one wants to make and break trains it gets a little more difficult, as what if you want to have a train navigate so that the locomotive will stop on a curve, or the place that you want to train to stop at is two crowded with belts and other things to allow room for a train stop? You might be able to make a temp train stop that has no collision box and is able to exist in place with other things in the same tile but that could get a little confusing as you will have them showing up here and there and disappearing rapidly. What if you have three cars sitting on a track and they are not connected currently and you want to make one train out of them, would you calculate where you need the train stop to be located for the locomotive to be in range of the first car, have it go there, stop hook up to that car then have to figure out where to place the next stop based on the new length of the train, and then rinse and repeat? Curves only make it harder, while if the train is manually controlled all you would have to do once you have the train on the correct track is move it backwards and then when the rear car is within connection range make the connection.

As for the method of controlling the train that I put forth, am not sure if anyone else have ever tried or used it in any way shape or form prior to me, I have never tried the Red Alert harvester mod, but if anyone had tried doing this I would have pegged it as being the best bet. As for the documentation as I said above, most of my own documentation was written up as I wrote the post, prior to that it was only a partial non working version that existed in my head.

I first started looking for a way to direct trains without using the schedule system was back when I posted this thread, when I ran out of options on how to get the list of station names without them being listed as part of an existing trains schedule. Since there were no examples that I know of I started playing around with the help method of lua objects to see if I could find something that might have fitted my needs.

By the time that the Train Control/Rail Signals thread was started I had discovered that you could put a fake player into a train and make them move it forward and turn, but that was it. I hadn't figured out that the acceleration was an int value specifying state and not amount and much less figured out the rules for how trains interact with turnouts. I somewhat thought that ridingstate was something that was not intended to be exposed since I couldn't get it to do everything that I wanted and it seemed to be acting like magic so I pretty much gave up on it as a viable solution.

When this thread came around I wanted to validate my memory of what I had done in the past so I made a sandbox world in peaceful mode, plopped down a simple rail line and created a fake player via the console and started playing around with everything tring to remember what I had done in the past so that I could make sure that everything was as accurate as possible, and the results are basically the first bit of documentation on ridingstate that I know of in existence, though there is probably something in the game dev's internals docs/design specs.

JamesOFarrell
Filter Inserter
Filter Inserter
Posts: 402
Joined: Fri May 23, 2014 8:54 am
Contact:

Re: Train coupling and decoupling

Post by JamesOFarrell »

JLBShecky wrote:When this thread came around I wanted to validate my memory of what I had done in the past so I made a sandbox world in peaceful mode, plopped down a simple rail line and created a fake player via the console and started playing around with everything tring to remember what I had done in the past so that I could make sure that everything was as accurate as possible, and the results are basically the first bit of documentation on ridingstate that I know of in existence, though there is probably something in the game dev's internals docs/design specs.
I think I've read all of the API docs on here and the wiki and this is the first time I've heard about riding state . Looking at the factorio exe it looks like there is also WalkingState and PickingState as well. Interesting.

n9103
Smart Inserter
Smart Inserter
Posts: 1067
Joined: Wed Feb 20, 2013 12:09 am
Contact:

Re: Train coupling and decoupling

Post by n9103 »

I have never tried the Red Alert harvester mod, but if anyone had tried doing this I would have pegged it as being the best bet.
Never played it? You don't say? :P

The harvester is essentially just a car that mines the tile(s) underneath the vehicle. And only while being driven.
No vehicle automation there at all. Not even progress towards it. Not something the author was really aiming for.
I was a bit disappointed, both by the manual nature of it, and the highly underwhelming performance. (I ended up modifying the latter a good deal.)
Colonel Failure wrote:You can lose your Ecologist Badge quite quickly once you get to the point of just being able to murder them willy-nilly without a second care in the world.

Post Reply

Return to “Modding help”