Look into sentient.c . If I understand the code correctly, you can set wanderpath (as a second arg to SetWander or by SetWanderPath). It's an array which can contain strings ("go west", "open door", "go east", "press button") or (: function_pointers

which are called. The function can examine the environment or sth, and return a command string.
Elements of the array are called in sequence, looping from last to first.
If you want to restrict to one terrain type, make a one-element array linking to a function, which checks terrain type of adjacent rooms, and randomly chooses one of the correct type.