functionalize the filter code

so that both types of mapgen routines can use the same subroutines.
less code duplication that way.