====== advtrains.is_track_and_drives_on ======
//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.//
===== Syntax =====
advtrains.is_track_and_drives_on(nodename, drives_on_p)
===== Extended description =====
This function is used to check whether a certain node is a valid track. In case a train can only drive on certain [[dev:core:tracktype]]s, can also restrict the valid tracktypes to the ones given.
===== Parameters =====
* ''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.
===== Returns =====
True if the specified node name is a track, and the train can drive on this track.
===== How it works internally =====
Firstly, this function checks the parameter ''drives_on_p''. If it is ''nil'' or empty, the parameter is replaced with ''[[dev:core:tracktype|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**.
===== Examples =====
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"
===== In source code =====
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