Imagine X/Y/Z battle coordinates.
Assume the previous town search ended at X-1/Y/Z.
Add 1 to (X - 1), check if Map[X][Y][Z] is a town, stop if so. Otherwise continue searching. In the former case we will end at X + DX/Y + DY/Z + DZ coordinate, which is not original X/Y/Z, obviously.