For you to understand that expression, let's start out with the "var[...]" statement.
It's not documented anywhere, so it's understandable that you don't know it. But if one checks the source for NMLC one can see how it's supposed to work.
var[num, <shift>, <mask>, <param>]
num - The variable to access. This is feature specific so accessing for example variable 0xB4 on a train would return the current speed.
shift - An optional parameter that performs a bitwise shift on the result. Example: 123 << 4 = 1,968 or in binary: 0111 1011 << 4 = 0111 1011 0000, in other words. It shifts bits left or right. I would assume positive numbers are left shift (<<) and negative numbers are right shift (>>)
mask - An optional parameter that masks the result. For example: 123 & 0xF = 11 or in binary: 0111 1011 & 0000 1111 = 1011. That is, if both bits are 1 in the result and the mask then they are kept.
param - An optional parameter that is sent to the "60+x" variable in question. In this case, since we are working on var 0x61 which is "query variable of n'th vehicle in chain" we are trying to get the value of variable C6 (Vehicle type ID) on the n'th vehicle in the chain.
Or in other words, get variable from n'th vehicle in chain that is "Vehicle type ID" applying no bit shifting and applying logical AND 0x0000FFFF to the result. Or in other words, clamping the result between 0 and 65535.
So, we are trying to base our decision in the switch on the vehicle ID of the wagon that exists before this wagon is attached to the chain.
We only allow adding "unit_rail" wagons to DMU, EMU, unit_rail and SMU's before it.
Now you wonder, how does 0x61 (query var of n'th vehicle in chain) know what the n'th vehicle in the chain is? I mean, there's only a parameter for what variable (0xC6, vehicle id) to get from that n'th vehicle.
Well, when using var 0x61, you have to make use of a temporary variable that refers to the actual offset in the chain. That's what the previous STORE_TEMP(x, 0x10F) does. It stores a value in a SPECIAL register used for this very purpose.
In this case, the value is the "position_in_consist_from_end" variable.
But i fail to see the logic behind this one though... Wouldn't it simply suffice to store 0 (zero) in that temporary register? Or is it actually counting from the engine down because the vehicle being evaluated is a wagon? So many question marks.
Either way, surely there's a reason for that. And if you search for it, you end up on page 43 of the NML language thread: viewtopic.php?t=48891&start=840
And there you have it. Even the comments on the code you pasted tells you the same story. Albeit a bit briefer...
//Check vehicle ID of the vehicle in front of the unit wagon being attached
//The offset for var 61 is determined by position_in_consist_from_end, which results in the last wagon before the vehicle being attached
If you don't understand some code, but it still works beautifully, then what's the harm in re-using it?