This code comes from a mailing list request. It takes a signal, and runs through each route looking for one which can be set and gives up if unable to. It requires that routes are arranged in ascending numeric order, routes named like this:
There is no concept of ARS matching and this simply sets the first available route.
One usecase is with depots designed to store trains. By setting the route to a free track, this allows for trains to be stored where it is possible. Another usecase is terminal stations which branch out. This allows trains to go to an available track instead of waiting for a train to depart or segregating tracks by line.
The code takes the name of the signal and the last route as constants. When a train runs over the rail, the code attempts to find a setabble route. If unable to, the code gives up.
signalPos = "" -- Add the name of the signal lastRoute = "" -- The number name of the final route if event.type == "train" then for i = lastRoute,1,-1 do l = tostring(i) if can_set_route(signalPos, l) then set_route(signalPos, l) end end end