Page 1 of 1

Faster trains and corridors

Posted: Thu Jan 30, 2020 3:36 pm
by vorner
Hello

I've got to the phase where all my outposts and parts of the factory span across large part of the map and it takes a long time to ride a train from one end to the other. And I've noticed that you get the locomotive very early (green science) and use the very same locomotive to the very end, only with somewhat better fuel. So, I was thinking if there was something that could be done with that.

And indeed, there are mods that add faster trains. However, they add just that, in a very boring way, no challenge to them ‒ they are faster and more expensive and that's it and the price of few locomotives doesn't really matter at this phase of the game, as there are very few anyway.

So my idea is to have a locomotive with a faster max speed (but maybe worse acceleration, it would be suitable for long journeys but not for these local stop-to-stop jumps inside a small factory). I'd also add next tier of rails (maybe one that would use concrete instead of stones). The locomotive could go at its max speed only on these new better rails and only if there were no curves. That would present the challenge of building the corridor that doesn't include turns, using the better rails (more expensive ones). On normal rails or curves, it would be able to go the normal ~300km/h or so. That would also make it look more realistic (these extra-fast trains need higher quality rails to go fast).

What do you think about the idea?

I was actually thinking of trying to make the mod. I know lua, and I program, but I haven't done any factorio mods yet, so a little push in the right direction could probably save me a lot of time.

What I've researched so far:
  • I'd probably just create a copy of the ordinary locomotive and tweak some parameters to it, to create the new prototype (I hope I'm not confusing the terminology).
  • Similar with the rails.
  • Then, in the on_tick hook, I'd go through all the trains and check if they are not going too fast according to where they are and brake them a little if needed. The speed property of a train is read/write.
  • I probably want to slow down *before* the turns or ordinary rails. So I'd have to read the path property, find where the train is on that and look a bit ahead. That should be possible, maybe a bit tedious.
What I don't know yet:
  • What are downsides of this approach? Will I break something? Is there a better hook than on_tick, one that wouldn't have to run as often?
  • Will my new rails integrate with the rail planner?
  • Will they integrate with upgrade planner? How do I make them „the next tier“?
  • How do I know how much a train can decelerate right now? There are these „braking force“ researches and I assume I can read if these were researched. But there are other mods that add more levels of these or tweak by how much the braking force is improved, so I'd really prefer to have an authoritative number, the same one the engine uses for slowing down before eg. signals.
  • Would it be possible to make the path finding algorithm prefer fastest paths instead of shortest ones?
As I said, I'm familiar with coding so answers like „Hey, my mod does something similar and it's the proper way to do X, look at it“ are very welcome, as pointers to the relevant parts of API, which I've skimmed but it's sometimes hard to be sure where something would be.

Re: Faster trains and corridors

Posted: Mon Feb 10, 2020 10:47 pm
by Qon
on_tick is something you want to avoid if possible because of performance reasons. But you are also fairly limited in what you can do with trains. Unlike vehicles you can't just set a speed on them to accelerate and brake with them.

There's mods that makes faster trains with lower acceleration. Like https://mods.factorio.com/mod/TrainOverhaul
Go through https://mods.factorio.com/tag/trains https://mods.factorio.com/tag/trains?version=0.18 all the train mods and see if you find something that is similar to some of the things you want to do. But the pathfinder requests rail blocks for your trains, which depends on the speed of your train. So for it to be able to do it's job you can't be allowed to touch whatever you want. You can spawn trains at 0 speed and remove them. And you can fiddle with their settings. But if you want to increase or decrease speed then you need to make faster locomotives or faster fuels.

For dynamic speed something very ugly but possible would be to have some low energy fuel that burns up each unit quickly and then modyfy the fuel type in the locomotive in real time (You can probably not empty the currently burning fuel so make something that burns quickly). You would have to convert fuels of other types to your types of fuel and back all the time if you want it to be "invisible" (kind of) and there's a lot of issues to get rid of then still. And maybe there's some API call I missed or that was added since I checked so you don't have to take this stupid route.


Edit: apparently train speed can be written to directly now! https://lua-api.factorio.com/latest/Lua ... rain.speed

Edit2:
vorner wrote: Thu Jan 30, 2020 3:36 pm
  • Would it be possible to make the path finding algorithm prefer fastest paths instead of shortest ones?
It kind of does this already. The fastest path is the "shortest" path, and train stop rails are virtually 1km long for the pathfinder. There might be some kind of way to make bent rails longer from the perspective of the pathfinder, but it might not be possible. An alternative would be to automatically spawn invisible train stops (with maybe a shorter than 1km virtual distance if possible) at each bend or something similar.