Ex0rcism Posted October 7, 2017 Share Posted October 7, 2017 (edited) I was having troubles for a little bit trying to get the closest bank, then stumbled upon Chris's post on how getting the closest bank works. So all credits go to him for this one. All I did was modified the coding a little bit to make it compatible with task usage. Code: import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.stream.Stream; import org.osbot.rs07.api.map.Area; import org.osbot.rs07.api.map.Position; import org.osbot.rs07.script.MethodProvider; public enum WebBank { GRAND_EXCHANGE(new Area(new Position(3161, 3493, 0), new Position(3168, 3486, 0))), VARROCK_EAST(new Area(new Position(3250, 3423, 0), new Position(3257, 3416, 0))), VARROCK_WEST(new Area(new Position(3180, 3447, 0), new Position(3185, 3433, 0))), EDGEVILLE(new Area(new Position(3091, 3499, 0), new Position(3098, 3487, 0))); private final Area area; WebBank(Area area) { this.area = area; } public static Area closestTo(MethodProvider e) { HashMap<WebBank, Integer> distMap = new HashMap<WebBank, Integer>(); for (WebBank b : WebBank.values()) { distMap.put(b, e.myPosition().distance(b.area.getRandomPosition())); } HashMap<Integer, WebBank> distMapSorted = sortByDistance(distMap); Area cBank = distMapSorted.values().toArray(new WebBank[WebBank.values().length])[0].area; return cBank; } private static <K, V extends Comparable<? super V>> HashMap<V, K> sortByDistance(Map<K, V> map) { HashMap<V, K> result = new LinkedHashMap<>(); Stream<Map.Entry<K, V>> st = map.entrySet().stream(); st.sorted(Map.Entry.comparingByValue()).forEachOrdered(e -> result.put(e.getValue(), e.getKey())); return result; } } Usage: import org.osbot.rs07.event.WebWalkEvent; import org.osbot.rs07.event.webwalk.PathPreferenceProfile; import org.osbot.rs07.script.MethodProvider; public class DropTask extends Task { public DropTask(MethodProvider api) { super(api); } @Override public void startBankWalk() { WebWalkEvent webEvent = new WebWalkEvent(WebBank.closestTo(api)); webEvent.useSimplePath(); PathPreferenceProfile ppp = new PathPreferenceProfile(); ppp.setAllowObstacles(true); webEvent.setPathPreferenceProfile(ppp); api.execute(webEvent); return; } } Edited October 7, 2017 by Ex0rcism Quote Link to comment Share on other sites More sharing options...
Chris Posted October 7, 2017 Share Posted October 7, 2017 No 1 1 Quote Link to comment Share on other sites More sharing options...
Runnwith Posted October 7, 2017 Share Posted October 7, 2017 1 hour ago, Chris said: No Tfw dad rejects you. 1 1 Quote Link to comment Share on other sites More sharing options...
Antonio Kala Posted October 7, 2017 Share Posted October 7, 2017 2 hours ago, Chris said: No That's pretty mean. 1 Quote Link to comment Share on other sites More sharing options...
Chris Posted October 7, 2017 Share Posted October 7, 2017 37 minutes ago, Antonio Kala said: That's pretty mean. this is not the right way! 1 Quote Link to comment Share on other sites More sharing options...
progamerz Posted October 7, 2017 Share Posted October 7, 2017 This calculates by the distance between u and the tile/area u want to go to, not the actual path, for it to walk to the closest bank u will have to have an Array of banks: Then, u will set a webwalk event: WebWalkEvent webEvent = new WebWalkEvent(banking); PathPreferenceProfile ppp = new PathPreferenceProfile(); ppp.setAllowObstacles(true); webEvent.setPathPreferenceProfile(ppp); api.execute(webEvent); return; 4 hours ago, Ex0rcism said: I was having troubles for a little bit trying to get the closest bank, then stumbled upon Chris's post on how getting the closest bank works. So all credits go to him for this one. All I did was modified the coding a little bit to make it compatible with task usage. Code: import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.stream.Stream; import org.osbot.rs07.api.map.Area; import org.osbot.rs07.api.map.Position; import org.osbot.rs07.script.MethodProvider; public enum WebBank { GRAND_EXCHANGE(new Area(new Position(3161, 3493, 0), new Position(3168, 3486, 0))), VARROCK_EAST(new Area(new Position(3250, 3423, 0), new Position(3257, 3416, 0))), VARROCK_WEST(new Area(new Position(3180, 3447, 0), new Position(3185, 3433, 0))), EDGEVILLE(new Area(new Position(3091, 3499, 0), new Position(3098, 3487, 0))); private final Area area; WebBank(Area area) { this.area = area; } public static Area closestTo(MethodProvider e) { HashMap<WebBank, Integer> distMap = new HashMap<WebBank, Integer>(); for (WebBank b : WebBank.values()) { distMap.put(b, e.myPosition().distance(b.area.getRandomPosition())); } HashMap<Integer, WebBank> distMapSorted = sortByDistance(distMap); Area cBank = distMapSorted.values().toArray(new WebBank[WebBank.values().length])[0].area; return cBank; } private static <K, V extends Comparable<? super V>> HashMap<V, K> sortByDistance(Map<K, V> map) { HashMap<V, K> result = new LinkedHashMap<>(); Stream<Map.Entry<K, V>> st = map.entrySet().stream(); st.sorted(Map.Entry.comparingByValue()).forEachOrdered(e -> result.put(e.getValue(), e.getKey())); return result; } } Usage: import org.osbot.rs07.event.WebWalkEvent; import org.osbot.rs07.event.webwalk.PathPreferenceProfile; import org.osbot.rs07.script.MethodProvider; public class DropTask extends Task { public DropTask(MethodProvider api) { super(api); } @Override public void startBankWalk() { WebWalkEvent webEvent = new WebWalkEvent(WebBank.closestTo(api)); webEvent.useSimplePath(); PathPreferenceProfile ppp = new PathPreferenceProfile(); ppp.setAllowObstacles(true); webEvent.setPathPreferenceProfile(ppp); api.execute(webEvent); return; } } 2 1 Quote Link to comment Share on other sites More sharing options...
Explv Posted October 7, 2017 Share Posted October 7, 2017 12 hours ago, Ex0rcism said: I was having troubles for a little bit trying to get the closest bank, then stumbled upon Chris's post on how getting the closest bank works. So all credits go to him for this one. All I did was modified the coding a little bit to make it compatible with task usage. Code: import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.stream.Stream; import org.osbot.rs07.api.map.Area; import org.osbot.rs07.api.map.Position; import org.osbot.rs07.script.MethodProvider; public enum WebBank { GRAND_EXCHANGE(new Area(new Position(3161, 3493, 0), new Position(3168, 3486, 0))), VARROCK_EAST(new Area(new Position(3250, 3423, 0), new Position(3257, 3416, 0))), VARROCK_WEST(new Area(new Position(3180, 3447, 0), new Position(3185, 3433, 0))), EDGEVILLE(new Area(new Position(3091, 3499, 0), new Position(3098, 3487, 0))); private final Area area; WebBank(Area area) { this.area = area; } public static Area closestTo(MethodProvider e) { HashMap<WebBank, Integer> distMap = new HashMap<WebBank, Integer>(); for (WebBank b : WebBank.values()) { distMap.put(b, e.myPosition().distance(b.area.getRandomPosition())); } HashMap<Integer, WebBank> distMapSorted = sortByDistance(distMap); Area cBank = distMapSorted.values().toArray(new WebBank[WebBank.values().length])[0].area; return cBank; } private static <K, V extends Comparable<? super V>> HashMap<V, K> sortByDistance(Map<K, V> map) { HashMap<V, K> result = new LinkedHashMap<>(); Stream<Map.Entry<K, V>> st = map.entrySet().stream(); st.sorted(Map.Entry.comparingByValue()).forEachOrdered(e -> result.put(e.getValue(), e.getKey())); return result; } } Usage: import org.osbot.rs07.event.WebWalkEvent; import org.osbot.rs07.event.webwalk.PathPreferenceProfile; import org.osbot.rs07.script.MethodProvider; public class DropTask extends Task { public DropTask(MethodProvider api) { super(api); } @Override public void startBankWalk() { WebWalkEvent webEvent = new WebWalkEvent(WebBank.closestTo(api)); webEvent.useSimplePath(); PathPreferenceProfile ppp = new PathPreferenceProfile(); ppp.setAllowObstacles(true); webEvent.setPathPreferenceProfile(ppp); api.execute(webEvent); return; } } This is extremely overcomplicated. The only time you ever really need the closest bank is if you want to walk there. If you pass multiple areas to the web walking method it will automatically walk to the closest one, and will take into account actual path distance, rather than straight line distance which your method uses: getWalking().webWalk(bankAreas); 1 Quote Link to comment Share on other sites More sharing options...
liverare Posted October 7, 2017 Share Posted October 7, 2017 (edited) If you're looking to find the closest bank and keep a reference to it, then you could definitely trim down your code bigly: /** * * @return Closest WebBank */ public WebBank getClosest() { return Stream.of(WebBank.values()) .sorted(this::sortByDistance) .findFirst() .orElse(null); } /** * Calculate distance to WebBank * * @param webBank * - WebWalk object * @return Distance to WebBank */ private int distance(WebBank webBank) { return webBank.area.getPositions().get(0).distance(myPlayer()); } /** * Compare distances to two WebWalks * * @param a * - first WebWalk * @param b * - second WebWalk * @return Array sort */ private int sortByDistance(WebBank a, WebBank b) { return Integer.compare(distance(a), distance(b)); } You don't need to map out distances or any of that non-sense. Edited October 7, 2017 by liverare 1 Quote Link to comment Share on other sites More sharing options...
HeyImJamie Posted October 7, 2017 Share Posted October 7, 2017 15 hours ago, progamerz said: This calculates by the distance between u and the tile/area u want to go to, not the actual path, for it to walk to the closest bank u will have to have an Array of banks: Then, u will set a webwalk event: WebWalkEvent webEvent = new WebWalkEvent(banking); PathPreferenceProfile ppp = new PathPreferenceProfile(); ppp.setAllowObstacles(true); webEvent.setPathPreferenceProfile(ppp); api.execute(webEvent); return; This, Just a heads up though you don't need to setAllowObstacles as the boolean is already set to true on execution of the event. Teleports you do need to set though 1 2 Quote Link to comment Share on other sites More sharing options...
Ex0rcism Posted October 8, 2017 Author Share Posted October 8, 2017 14 hours ago, Explv said: This is extremely overcomplicated. The only time you ever really need the closest bank is if you want to walk there. If you pass multiple areas to the web walking method it will automatically walk to the closest one, and will take into account actual path distance, rather than straight line distance which your method uses: getWalking().webWalk(bankAreas); Alright, will take that duely noted. I thought there was an easier way, but I'm just starting out with OSRS scripting using a different api, and using this website for beginner resources. 8 hours ago, liverare said: If you're looking to find the closest bank and keep a reference to it, then you could definitely trim down your code bigly: /** * * @return Closest WebBank */ public WebBank getClosest() { return Stream.of(WebBank.values()) .sorted(this::sortByDistance) .findFirst() .orElse(null); } /** * Calculate distance to WebBank * * @param webBank * - WebWalk object * @return Distance to WebBank */ private int distance(WebBank webBank) { return webBank.area.getPositions().get(0).distance(myPlayer()); } /** * Compare distances to two WebWalks * * @param a * - first WebWalk * @param b * - second WebWalk * @return Array sort */ private int sortByDistance(WebBank a, WebBank b) { return Integer.compare(distance(a), distance(b)); } You don't need to map out distances or any of that non-sense. As I stated this is not my code, I just modified Chris's that was quite old to make it work. Still learning though thanks for the tip! 7 hours ago, HeyImJamie said: This, Just a heads up though you don't need to setAllowObstacles as the boolean is already set to true on execution of the event. Teleports you do need to set though Alright thank you for the heads up I appreciate it , Like I said before I'm gonna have to dig deeper into the API and learn much more. Quote Link to comment Share on other sites More sharing options...
sn0wman Posted November 5, 2017 Share Posted November 5, 2017 (edited) Spoiler https://gist.github.com/git-sm/8f87cd8d8ea51dfaa3a133a6802dce42 i find works atleast...i think! meh. Edited November 5, 2017 by sn0wman notify Quote Link to comment Share on other sites More sharing options...
Chris Posted November 6, 2017 Share Posted November 6, 2017 5 hours ago, sn0wman said: Hide contents https://gist.github.com/git-sm/8f87cd8d8ea51dfaa3a133a6802dce42 i find works atleast...i think! meh. wat tha fak Quote Link to comment Share on other sites More sharing options...
Viston Posted November 6, 2017 Share Posted November 6, 2017 (edited) 6 hours ago, sn0wman said: Hide contents https://gist.github.com/git-sm/8f87cd8d8ea51dfaa3a133a6802dce42 i find works atleast...i think! meh. 49 minutes ago, Chris said: wat tha fak if (!getBank().isOpen()); { getBank().open(); new ConditionalSleep(2000, 100) { @Override public boolean condition() throws InterruptedException { return getInventory().isEmpty(); } }.sleep(); getBank().open() > getInventory().isEmpty() Edited November 6, 2017 by Viston Quote Link to comment Share on other sites More sharing options...
progamerz Posted November 6, 2017 Share Posted November 6, 2017 (edited) 7 hours ago, sn0wman said: Hide contents https://gist.github.com/git-sm/8f87cd8d8ea51dfaa3a133a6802dce42 i find works atleast...i think! meh. for (int i = 0; i < 53; i++) { if (!bankArea[i].contains(myPlayer())) { walking.webWalk(bankArea); if (bankArea[i].contains(myPosition())) { } else if (!getBank().isOpen()); { getBank().open(); new ConditionalSleep(2000, 100) { @Override public boolean condition() throws InterruptedException { return getInventory().isEmpty(); } }.sleep(); } } } return random(2000, 500); why return random(2000,500)? that would simply not work i think, the first param should always be less than the second return(500,2000) instead of doing for(int i = 0; i < 53, i++) you could simply do for(int i = 0; i < bankArea.size, i++) And you have done many non logical things here, your logic currently is if bankArea[0] does not contain your player walk to it? so that would probably walk you to everybank, cause to walk to the closest you need to use WebWalkEvent. Instead of all this just do an object booth and assign to it the closest object with name "Bank Booth" if object is null(as it will be null if you aren't in the bank) u will create a webwalkevent and walk to nearest bank, or else open bank and etc. As for the ConditionalSleep please check out the docs, and why would you wait for the inventory being empty? instead wait for the bank to be open. for (int i = 0; i < bankArea.length; i++) { if (!bankArea[i].contains(myPlayer())) { WebWalkEvent event = new WebWalkEvent(bankArea); execute(event); } else { if(getBank().isOpen()) { if(getBank().depositAll()) { new ConditionalSleep(2000) { @Override public boolean condition() throws InterruptedException { return getInventory().isEmpty(); } }; } } else { if(getBank().open()) { new ConditionalSleep(2000) { @Override public boolean condition() throws InterruptedException { return getBank().isOpen(); } }; } } } } OR RS2Object bankBooth = getObjects().closest("Bank booth"); if(bankBooth != null) { if(getBank().isOpen()) { if(getBank().depositAll()) { new ConditionalSleep(2000) { @Override public boolean condition() throws InterruptedException { return getInventory().isEmpty(); } }; } } else { if(getBank().open()) { new ConditionalSleep(2000) { @Override public boolean condition() throws InterruptedException { return getBank().isOpen(); } }; } } } else { WebWalkEvent event = new WebWalkEvent(bankArea); execute(event); } Edited November 6, 2017 by progamerz 1 Quote Link to comment Share on other sites More sharing options...
sn0wman Posted November 6, 2017 Share Posted November 6, 2017 (edited) https://gist.github.com/git-sm/a07058582302b7c8af76a83cd26cdc81 so heres a rewrite with @progamerz edit. Has one issue any suggestions or help more than welcome. To come: gui with a combobox to select which bank with a nearest option as default. Edited November 6, 2017 by sn0wman new gui Quote Link to comment Share on other sites More sharing options...