severak wrote: ↑Thu Dec 16, 2021 10:04
Hi. Many thanks for Advtrains. It really hooked me into Minetest again.
I started toying with train and track remote control and figured out how to create train remote control with Lua ATC rain, microcontroller, digiterms and some lua code (see here -
https://gist.github.com/severak/235c28c ... bf532be23f). It works surprisingly well until it hits something (including other trains - thus unusable for shunting). If it hits something trains get stuck and it's unresponsible with no way to fix it with remote control.
Then I decided to convert it into proper "train remote control" mod.
Thus these questions apparead:
- is there any internal API for controlling the trains?
- is this somewhat stable?
- is this documented or do I need to reverse engineer it from advtrains code? (which file then?)
- how to detect stuck train?
- it's possible to unstuck it in some scenarios? (for example when stuck by another train in front of it)
Any answer or hint would be aprecciated.
Hi Sevarak,
sorry, it took me a while to look into the forums (the usual christmas-time stuff, presents here, visits there).
advtrains doesn't really have an API for the internal workings. I tried to add code comments where applicable to document what the code does, but there is no real set of functions you can call. Perhaps the best method is to stick to one of the existing control methods:
- like the user control when sitting in the train: have a look at
trainhud.lua, the key variable here is 'train.ctrl_user' where you can set certain "lever values" (
see here what they mean)
- sending ATC commands, there you can use the advtrains.atc.train_set_command() function
For checking whether a train is stuck, there are actually 4 ways a train can get stuck:
-
Off-Track: when one end of the train is not on a track, speed is limited to 0 resp. 1 m/s
-
Train-to-Environment (counterpart
here: a cuboid around the wagon position is checked for each loaded wagon whether there is a walkable node (or a full-cube node with "forgiving collision") at that position. This happens only in loaded areas (that are emerged by the minetest server). To unstuck this the blocking node needs to be removed
-
Train-To-Train not on same track: This is probably the problematic collision you run into. Fact is, this system is only in effect when the train is moving, and it only checks a cube right at the front tip of the train, not on the whole length. This means you should always be able to "unstuck" such a train by coupling a shunter engine to one of the stuck trains and driving it out. I admit that this system is not ideal though, maybe we should remove it altogether (although that means that trains cannot collide on crossings anymore :/)
-
Train-To-Train on same track (NEW!): The new system for collisions of trains on the same track, and for coupling. I made this recently, before this the other train-to-train collision mechanic was the only one used and it was also used for coupling. With this system trains do not really get "stuck", they just stop at the train end and you can drive either train away. Once trains overlap this system has no effect anymore and the old train-to-train collision kicks in.
If you want to detect whether a train is stuck, that's not really doable right now unless you extend the "train_step_b()" function to export that information. But I happily accept a patch to make that possible.
I hope this helps.
Regards, orwell