Checks whether the specified node name is a track and the train can drive on this track type.
Note: this page has been written for Advtrains 2.0.1 TSS and Minetest 5.1.0. Examples and practices described in this page don't take advantage of improvements introduced in later releases.
advtrains.is_track_and_drives_on(nodename, drives_on_p)
This function is used to check whether a certain node is a valid track. In case a train can only drive on certain Tracktypes, can also restrict the valid tracktypes to the ones given.
nodename
(string): the track node name.drives_on_p
(table): a table containing tracktype names the train can drive on. Defaults to advtrains.all_tracktypes.True if the specified node name is a track, and the train can drive on this track.
Firstly, this function checks the parameter drives_on_p
. If it is nil
or empty, the parameter is replaced with advtrains.all_tracktypes
. Then, it checks the existence of the specified nodename
- if it does not exist, the function returns immediately.
The third step is to check that the track group of the node definition is present in the drives_on_p
table.
If one of the keys in the table matches the node name, the function returns true. Otherwise it returns false.
advtrains.is_track_and_drives_on("advtrains:dtrack", nil) -- true advtrains.is_track_and_drives_on("advtrains:dtrack", {}) -- true advtrains.is_track_and_drives_on("advtrains:dtrack", { nonstandard = true }) -- false advtrains.is_track_and_drives_on("tracklib:custom", { nonstandard = true }) -- false unless tracklib:custom has been registered with track type "nonstandard"
advtrains/tracks.lua, lines 365 to 384:
function advtrains.is_track_and_drives_on(nodename, drives_on_p) local drives_on = drives_on_p if not drives_on then drives_on = advtrains.all_tracktypes end local hasentry = false for _,_ in pairs(drives_on) do hasentry=true end if not hasentry then drives_on = advtrains.all_tracktypes end if not minetest.registered_nodes[nodename] then return false end local nodedef=minetest.registered_nodes[nodename] for k,v in pairs(drives_on) do if nodedef.groups["advtrains_track_"..k] then return true end end return false end