Taken from my Stealth Quester's API:
public static Position getClosestBank(API api) {
ArrayList<BankEntry> banks = new ArrayList<BankEntry>();
for (Bank bank : Bank.values()) {
if (!api.worlds.isMembersWorld() && !bank.isF2P)
continue;
List<IDirection> directions = api.finder.route(api, api.myPosition(), bank.walkablePosition);
if (directions != null) {
banks.add(new BankEntry(bank.walkablePosition, bank, directions.size()));
}
}
banks.sort(new Comparator<BankEntry>() {
public int compare(BankEntry entry1, BankEntry entry2) {
return entry1.directions - entry2.directions;
}
});
if (banks.size() > 0) {
Bank closestBank = banks.get(0).bank;
api.log("[DEBUG][BANKING] Closest bank is: " + closestBank + " " + banks.get(0).walkablePosition);
return banks.get(0).walkablePosition;
} else {
return null;
}
}
As you may notice this actually finds the closest bank based on webwalking paths and NOT PLAIN DISTANCE. The banks are associated "walkable" positions so it only polls those positions making the algorithm run a lot faster. This method may take up to ~15 seconds to execute if you poll all positions in every bank so I strongly advise you to do it like I did with the walkablePosition when defining the Bank enum.
public static class BankEntry {
public BankEntry(Position walkablePosition, Bank bank, int directions) {
this.walkablePosition = walkablePosition;
this.bank = bank;
this.directions = directions;
}
Position walkablePosition;
Bank bank;
int directions;
}
The BankEntry class was defined for the sake of being a data structure (as I'm used to lower level programming languages), you may find more "OOP" implementations for this or you can just remove the class altogether and do the data mapping inside that method.
EDIT: The purpose of this method is to actually find the closest bank without being forced to walk to it. You may walk to it if you want as the returned position is guaranteed to be accessible by the webwalker.