Jump to content

getNearestBank();


Recommended Posts

Posted

Couldn't seem to find a method to get nearest bank, and all the Banks were constants...

 

So i threw them into an enum class and made a method to get nearest bank:

public enum WebBank {

    DRAYNOR(Banks.DRAYNOR),
    AL_KHARID(Banks.AL_KHARID),
    LUMBRIDGE(Banks.LUMBRIDGE_UPPER),
    FALADOR_EAST(Banks.FALADOR_EAST),
    FALADOR_WEST(Banks.FALADOR_WEST),
    VARROCK_EAST(Banks.FALADOR_EAST),
    VARROCK_WEST(Banks.VARROCK_WEST),
    SEERS(Banks.CAMELOT),
    CATHERBY(Banks.CATHERBY),
    EDGEVILLE(Banks.EDGEVILLE),
    YANILLE(Banks.YANILLE),
    GNOME_STRONGHOLD(Banks.GNOME_STRONGHOLD),
    ARDOUNGE_NORTH(Banks.ARDOUGNE_NORTH),
    ARDOUNE_SOUTH(Banks.ARDOUGNE_SOUTH),
    CASTLE_WARS(Banks.CASTLE_WARS),
    DUEL_ARENA(Banks.DUEL_ARENA),
    PEST_CONTROL(Banks.PEST_CONTROL),
    CANIFIS(Banks.CANIFIS),
    TZHAAR(Banks.TZHAAR);

    private final Area area;

    WebBank(Area area) {
        this.area = area;
    }

    public static WebBank getNearest(Script script) {
        WebBank bank = null;
        int distance = Integer.MAX_VALUE;
        for (WebBank b : WebBank.values()) {
            final int bDistance = b.area.getRandomPosition().distance(script.myPosition());
            if (bDistance < distance) {
                distance = bDistance;
                bank = b;
            }
        }
        return bank;
    }

    public Position getRandomPosition() {
        return area.getRandomPosition();
    }

    public Area getArea() {
        return area;
    }
}

have fun :)

  • Like 3
Posted

I made the same 2 days ago ohmy.png

package rqai.utilities;

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.map.constants.Banks;
import org.osbot.rs07.script.Script;

/**
 * 
 * @author randqm
 * 
 * Represents a bank.
 *
 */

public enum Bank {
	
	LUMBRIDGE(new Area(new Position(3207, 3220, 0), new Position(3210, 3218, 0))),
	VARROCK_WEST(Banks.VARROCK_WEST),
	VARROCK_EAST(Banks.VARROCK_EAST),
	GRAND_EXCHANGE(new Area(new Position(3162, 3486, 0), new Position(3168, 3493, 0))),
	DRAYNOR(Banks.DRAYNOR),
	EDGEVILLE(Banks.EDGEVILLE),
	SEERS(Banks.CAMELOT);
	
	
	/* The area of the bank. */
	private final Area area;
	
	
	/**
	 * Creates a new bank.
	 * 
	 * @param area The area of the bank.
	 */
	private Bank(Area area) {
		this.area = area;
	}
	
	/**
	 * Retrieves the area of the bank.
	 * 
	 * @return The bank's area.
	 */
	public Area getArea() {
		return area;
	}
	
	
	/**
	 * Retrieves the closest bank.
	 * 
	 * @param script The script.
	 * 
	 * @return The closest bank found.
	 */
	public static Bank getClosestBank(Script script) {
		Bank closest = null;
		
		for (Bank bank : values()) {
			if (closest == null
					|| bank.getArea().getRandomPosition().distance(script.myPosition())
					< closest.getArea().getRandomPosition().distance(script.myPosition())) {
				closest = bank;
			}
		}
		return closest;
	}

}

  • 2 weeks later...
  • 3 weeks later...
Posted (edited)

Couldn't seem to find a method to get nearest bank, and all the Banks were constants...

 

So i threw them into an enum class and made a method to get nearest bank:

    public static WebBank getNearest(Script script) {
        WebBank bank = null;
        int distance = Integer.MAX_VALUE;
        for (WebBank b : WebBank.values()) {
            final int bDistance = b.area.getRandomPosition().distance(script.myPosition());
            if (bDistance < distance) {
                distance = bDistance;
                bank = b;
            }
        }
        return bank;
    }

have fun smile.png

 

This is not correct though, as the distance between two positions is not the same as walking distance.

Therefore it can easily return a bank that is further away, as it is closer, but the route is longer.

 

Also if you are in a dungeon it will return an incorrect bank due to the way the map is laid out.

Edited by Explv
  • Like 1
Posted

This is not correct though, as the distance between two positions is not the same as walking distance.

Therefore it can easily return a bank that is further away, as it is closer, but the route is longer.

 

Also if you are in a dungeon it will return an incorrect bank due to the way the map is laid out.

 

Correct. This is the use of a heuristic.

To know the actual lengths, you'd need to pathfind every node. However, a heuristic uses a lot less resources and is typically accurate. (In the case of OSRS, can be very very bad due to dungeon layout like you said)

  • Like 1
  • 2 years later...
  • 1 year later...
Posted
22 hours ago, Jonathan Katz said:

i really wish this question was answered and not overlooked.

If you're calling the method in onLoop(), it would be 'this' (Main extends Script)

Although passing the script instance is bad practice... and I would suggest against using this due to the comments from above.

You can pass multiple areas to the webWalker and it will automatically choose the closest.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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