Jump to content

Ex0rcism

Recommended Posts

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 by Ex0rcism
Link to comment
Share on other sites

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;
	}
  
}

 

 

  • Like 2
  • Boge 1
Link to comment
Share on other sites

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);

  • Like 1
Link to comment
Share on other sites

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 by liverare
  • Like 1
Link to comment
Share on other sites

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 :boge: 

  • Like 1
  • Boge 2
Link to comment
Share on other sites

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! :D

 

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 :boge: 

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.

Link to comment
Share on other sites

  • 4 weeks later...
6 hours ago, sn0wman said:
  Hide contents

 

 

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 by Viston
Link to comment
Share on other sites

7 hours ago, sn0wman said:
  Hide contents

 

        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);
  1. 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)
  2. instead of doing for(int i = 0; i < 53, i++) you could simply do for(int i = 0; i < bankArea.size, i++)
  3. 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.
  4. 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.
  5. 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 by progamerz
  • Like 1
Link to comment
Share on other sites

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 by sn0wman
new gui
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...