Factor out terrain height calculation