Jump to content

getNearestBank();


N O Special

Recommended Posts

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

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

}

Link to comment
Share on other sites

  • 2 weeks later...
  • 3 weeks later...

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

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

  • 2 years later...
  • 1 year later...
On 2/6/2016 at 10:45 PM, wallnuts5 said:

Please excuse my ignorance (only written 2 scripts ever) but to run this I understand I need to call WebBank.getNearest(x); but what do I put in for x? Do I put in (Script name_of_my_script)?

 

Thanks in advance!

i really wish this question was answered and not overlooked.

Link to comment
Share on other sites

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.

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...