Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.rsbot.bot.Bot;
- import org.rsbot.bot.input.CanvasWrapper;
- import org.rsbot.bot.input.Mouse;
- import org.rsbot.event.listeners.PaintListener;
- import org.rsbot.script.Calculations;
- import org.rsbot.script.Methods;
- import org.rsbot.script.Script;
- import org.rsbot.script.ScriptManifest;
- import org.rsbot.script.wrappers.*;
- import java.applet.Applet;
- import java.awt.*;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseWheelEvent;
- import java.util.ArrayList;
- import java.util.Map;
- /**
- * Created by IntelliJ IDEA.
- * User: Raven
- * Date: 2-jan-2010
- * Time: 23:52:27
- * To change this template use File | Settings | File Templates.
- */
- @ScriptManifest(authors = "Bool", name = "BRuneMiner")
- public class BRuneMiner extends Script implements PaintListener {
- Pathfinder pf = new Pathfinder();
- int COMPONENTS = -1;
- public final int ROCKID = 14859, GATEID = 21505, bridge1 = 21314, bridge2 = 21315, RUNEID = 451;
- WorldSwitcher ws = new WorldSwitcher();
- RSTile bankLoc = new RSTile(2336, 3807), gateLoc = new RSTile(2326, 3805), rockLoc = new RSTile(2376, 3850);
- RSTile ropeloc = new RSTile(2378, 3839);
- RSTile bridge2loc = new RSTile(2343, 3829);
- int oldcount, currentcount, total;
- Timer running;
- public boolean painting = false;
- public int paintx = 20, painty = 70, xoff = -1, yoff = -1;
- public int paintwidth = -1, paintheight = -1;
- int price = 1;
- int startexp = 0;
- int FOODID = 379;
- public boolean onStart(Map<String, String> args) {
- price = grandExchange.loadItemInfo(RUNEID).getMarketPrice();
- oldcount = getInventoryCount(RUNEID);
- currentcount = getInventoryCount(RUNEID);
- running = new Timer();
- startexp = skills.getCurrentSkillExp(STAT_MINING);
- return true;
- }
- @Override
- public int loop() {
- if (!isLoggedIn())
- return random(100, 600);
- currentcount = getInventoryCount(RUNEID);
- if (oldcount < currentcount) {
- total++;
- oldcount = currentcount;
- }
- if (isInventoryFull()) {
- if (getMyPlayer().getLocation().getY() > 3847) {
- RSObject bridge = getNearestObjectByID(bridge2);
- if (bridge != null) {
- log("clicking bridge 1");
- while (getMyPlayer().getLocation().getY() > 3847) {
- if (!tileOnScreen(bridge.getLocation())) {
- walkTileMM(bridge.getLocation());
- waitToMove(2000);
- while (getMyPlayer().isMoving())
- sleep(200, 600);
- } else {
- if (atTile(bridge.getLocation(), "across")) {
- sleep(900, 1200);
- while (getMyPlayer().getLocation().getY() < 3848 && getMyPlayer().getLocation().getY() > 3839 && getLocation().getX() == 2378) {
- log("Walking on bridge");
- sleep(200, 500);
- }
- }
- }
- }
- return random(100, 300);
- }
- }
- if (getMyPlayer().getLocation().getY() > 3828) {
- log("walking to bridge");
- walkPath(bridge2loc);
- RSObject bridge = getNearestObjectByID(21309);
- if (bridge != null) {
- log("clicking bridge");
- while (getMyPlayer().getLocation().getY() > 3828) {
- if (!tileOnScreen(bridge.getLocation())) {
- walkTileMM(bridge.getLocation());
- waitToMove(2000);
- while (getMyPlayer().isMoving())
- sleep(200, 600);
- } else {
- if (atTile(bridge.getLocation(), "across")) {
- sleep(900, 1200);
- while (getMyPlayer().getLocation().getY() > 3820 && getMyPlayer().getLocation().getY() < 3829 && getLocation().getX() == 2343) {
- log("Walking on bridge");
- sleep(200, 500);
- }
- }
- }
- }
- }
- return random(100, 300);
- }
- if (!pf.validTile(bankLoc)) {
- log("walking to gate");
- walkPath(gateLoc);
- RSObject gate = getNearestObjectByID(GATEID);
- if (gate != null) {
- RSTile tile1 = new RSTile(2328,3804);
- RSTile tile2 = new RSTile(2329,3804);
- log("opening door");
- atDoorTiles(tile1,tile2);
- return random(100,700);
- }
- }
- if (distanceTo(bankLoc) > 5) {
- log("walking to bank");
- walkPath(bankLoc);
- return random(100, 400);
- } else {
- RSObject BANK = getNearestObjectByID(21301);
- if (BANK != null) {
- if (tileOnScreen(BANK.getLocation())) {
- if (bank.isOpen() || atTile(BANK.getLocation(), "Use")) {
- long start = System.currentTimeMillis();
- while (!bank.isOpen()) {
- if ((System.currentTimeMillis() - start) > 2000)
- break;
- if (getMyPlayer().isMoving())
- start = System.currentTimeMillis();
- }
- if (bank.isOpen()) {
- bank.depositAll();
- start = System.currentTimeMillis();
- while (getInventoryCount() > 0) {
- if ((System.currentTimeMillis() - start) > 2000)
- break;
- sleep(40, 500);
- }
- if (getInventoryCount() == 0) {
- int food = getNeededLobsters();
- log("we need " + food + " food");
- if (food > 0) {
- bank.withdraw(FOODID, food);
- start = System.currentTimeMillis();
- while (getInventoryCount() == 0) {
- if ((System.currentTimeMillis() - start) > 2000)
- break;
- sleep(100, 300);
- }
- }
- }
- }
- }
- } else if (tileOnMap(BANK.getLocation())) {
- walkTileMM(BANK.getLocation());
- waitToMove(1200);
- while (getMyPlayer().isMoving())
- sleep(100, 600);
- }
- }
- }
- return random(100, 400);
- }
- if (!pf.validTile(rockLoc) && !isInventoryFull()) {
- if(pf.validTile(bankLoc) && !pf.validTile(new RSTile(2342, 3819))){
- RSObject gate = getNearestObjectByID(GATEID);
- if (gate != null) {
- RSTile tile1 = new RSTile(2328,3804);
- RSTile tile2 = new RSTile(2329,3804);
- atDoorTiles(tile1,tile2);
- return random(100,700);
- }
- }
- if (getMyPlayer().getLocation().getY() < 3829) {
- log("walking to bridge");
- walkPath(new RSTile(2342, 3819));
- RSObject bridge = getNearestObjectByID(21308);
- if (bridge != null) {
- log("clicking bridge");
- while (getMyPlayer().getLocation().getY() < 3829) {
- if (!tileOnScreen(bridge.getLocation())) {
- walkTileMM(bridge.getLocation());
- waitToMove(2000);
- while (getMyPlayer().isMoving())
- sleep(200, 600);
- } else {
- if (atTile(bridge.getLocation(), "across")) {
- sleep(900, 1200);
- while (getMyPlayer().getLocation().getY() > 3820 && getMyPlayer().getLocation().getY() < 3829 && getLocation().getX() == 2343) {
- log("Walking on bridge");
- sleep(200, 500);
- }
- }
- }
- }
- }
- return random(100, 300);
- }
- if (getMyPlayer().getLocation().getY() < 3847) {
- walkPath(ropeloc);
- RSObject bridge = getNearestObjectByID(bridge1);
- if (bridge != null) {
- while (getMyPlayer().getLocation().getY() < 3847) {
- if (!tileOnScreen(bridge.getLocation())) {
- walkTileMM(bridge.getLocation());
- waitToMove(2000);
- while (getMyPlayer().isMoving())
- sleep(200, 600);
- } else {
- if (atTile(bridge.getLocation(), "Cross")) {
- sleep(900, 1200);
- while (getMyPlayer().getLocation().getY() < 3848 && getMyPlayer().getLocation().getY() > 3839 && getLocation().getX() == 2378) {
- log("Walking on bridge");
- sleep(200, 500);
- }
- }
- }
- }
- }
- return random(100, 300);
- }
- } else {
- RSObject rock = getNearestObjectByID(ROCKID);
- if (rock != null) {
- if (!isMining()) {
- atTile(rock.getLocation(), "Mine");
- waitForAnim(1200);
- }
- } else {
- ws.changeWorld();
- return random(300, 800);
- }
- }
- return random(300, 700);
- }
- public int getNeededLobsters() {
- return (skills.getRealSkillLevel(STAT_HITPOINTS) - skills.getCurrentSkillLevel(STAT_HITPOINTS)) / 12;
- }
- public void onRepaint(Graphics g) {
- if (running == null)
- return;
- Mouse m = Bot.getClient().getMouse();
- int x = m.x, y = m.y;
- if (running.getTimeElapsed() > 1) {
- int expgained = (skills.getCurrentSkillExp(STAT_MINING) - startexp);
- int expph = expgained > 0 ? (int) ((expgained * 60 * 60) / (running.getTimeElapsed() / 1000)) : 0;
- int logsph = total > 0 ? (int) ((total * 60 * 60) / (running.getTimeElapsed() / 1000)) : 0;
- int moneyph = logsph * price;
- java.awt.geom.Rectangle2D r = g.getFontMetrics().getStringBounds("Show paint", g);
- r.setRect(paintx, painty, r.getWidth() + 5, r.getHeight() + 3);
- if (m.pressed) {
- if (xoff == -1) {
- xoff = m.x - (int) r.getX();
- yoff = m.y - (int) r.getY();
- }
- //painting = false;
- }
- if (painting || (paintheight > r.getHeight() || paintwidth > r.getWidth())) {
- if (m.pressed) {
- if (new Rectangle(paintx, painty, paintwidth, paintheight).contains(m.x, m.y)) {
- paintx = m.x - xoff;
- painty = m.y - yoff;
- }
- } else {
- if (!pointValid(new Point(paintx, painty))) {
- paintx = 20;
- painty = 70;
- }
- xoff = -1;
- }
- String[] strings = {"BRuneMiner by bool", "Time running : " + running.toString(), "", "Rocks mined : " + total, "Rocks p/h : " + logsph, "", "Money p/h : " + moneyph, "Made : " + (total * price), "", "Exp gained : " + expgained, "Exp p/h : " + expph};
- int width = 0, height = 5;
- for (String s : strings) {
- if (s.length() > 2) {
- height += 15;
- } else {
- height += 1;
- }
- java.awt.geom.Rectangle2D rect = g.getFontMetrics().getStringBounds(s, g);
- if (width < rect.getWidth())
- width = (int) rect.getWidth();
- }
- width += 5;
- if (paintwidth == -1) {
- paintwidth = (int) r.getWidth();
- paintheight = (int) r.getHeight();
- }
- g.setColor(Color.black);
- if (painting) {
- if (paintwidth < width)
- paintwidth += 4;
- else
- paintwidth = width;
- if (paintheight < height)
- paintheight += 4;
- else
- paintheight = height;
- } else {
- if (paintheight > r.getHeight()) {
- paintheight -= 4;
- } else {
- paintheight = (int) r.getHeight();
- }
- if (paintwidth > r.getWidth()) {
- paintwidth -= 4;
- } else {
- paintwidth = (int) r.getWidth();
- }
- }
- g.drawRoundRect(paintx, painty, paintwidth, paintheight, 3, 3);
- g.setColor(new Color(51, 204, 102, 50));
- g.fillRoundRect(paintx, painty, paintwidth, paintheight, 3, 3);
- g.setColor(Color.black);
- int lines = 0;
- for (int i = 0; i < strings.length; i++) {
- String s = strings[i];
- boolean fit = s.length() < 2;
- while (!fit) {
- if (paintheight == height) {
- fit = true;
- break;
- }
- if ((painty + paintheight) < (painty + 2 + (i * 15) + 14))
- break;
- java.awt.geom.Rectangle2D temp = g.getFontMetrics().getStringBounds(s, g);
- if (new Rectangle(paintx, painty, paintwidth, paintheight).contains(paintx + 2, painty + 2 + (i * 15) + 14, temp.getWidth(), temp.getHeight())) {
- fit = true;
- break;
- }
- if (s.length() > 0) {
- s = s.substring(0, s.length() - 1);
- } else {
- break;
- }
- }
- if (fit) {
- if (s.length() < 2) {
- if (lines == 3)
- continue;
- if ((painty + paintheight) > painty + 2 + ((i - lines) * 15))
- g.drawLine(paintx, painty + 4 + ((i - lines) * 15), paintx + paintwidth, painty + 4 + ((i - lines) * 15));
- lines++;
- } else {
- drawShadow(s, paintx + 2, painty + 2 + ((i - lines) * 15) + 14, g);
- }
- }
- }
- if (!new Rectangle(paintx, painty, paintwidth, paintheight).contains(x, y))
- painting = false;
- } else {
- if (m.pressed) {
- if (r.contains(m.x, m.y)) {
- paintx = m.x - xoff;
- painty = m.y - yoff;
- }
- } else {
- if (!pointValid(new Point(paintx, painty))) {
- paintx = 20;
- painty = 70;
- }
- xoff = -1;
- }
- g.setColor(Color.black);
- g.drawRoundRect((int) r.getX(), (int) r.getY(), (int) r.getWidth(), (int) r.getHeight(), 3, 3);
- g.setColor(new Color(51, 204, 102, 50));
- g.fillRoundRect((int) r.getX(), (int) r.getY(), (int) r.getWidth(), (int) r.getHeight(), 3, 3);
- g.setColor(Color.black);
- drawShadow("Show paint", (int) (r.getX() + 2), (int) (r.getY() + r.getHeight()) - 3, g);
- if (r.contains(x, y)) {
- painting = true;
- }
- }
- }
- }
- public void drawShadow(String text, int x, int y, Graphics g) {
- Color c = g.getColor();
- g.setColor(c.brighter());
- g.drawString(text, x + 1, y);
- g.setColor(c);
- g.drawString(text, x, y);
- }
- public boolean pointValid(final Point check) {
- final int x = check.x, y = check.y;
- return x > 0 && x < CanvasWrapper.getGameWidth() && y > 0
- && y < CanvasWrapper.getGameHeight();
- }
- public void walkPath(RSTile tile) {
- while (distanceTo(tile) > 5) {
- RSTile valid = getValidTile(tile);
- RSTile[] path = pf.findPath(valid);
- while (!walkNext(path, 4)) {
- sleep(10, 30);
- }
- }
- }
- private boolean walkNext(final RSTile[] path, int dist) {
- final char left = 37;
- final char right = 39;
- RSTile tile = getNext(path);
- /*if (distanceTo(path[path.length - 1]) < 10)
- return true;*/
- if (getDestination() != null) {
- if (distanceBetween(tile, getDestination()) < 4) {
- return false;
- }
- }
- moveMouse(Calculations.worldToMinimap(tile.getX(), tile.getY()));
- try {
- if (getMenuItems().get(0).contains("ancel")) {
- char dir = random(0, 3) == 2 ? left : right;
- Bot.getInputManager().pressKey(dir);
- wait(random(800, 1200));
- Bot.getInputManager().releaseKey(dir);
- return false;
- }
- } catch (Exception e) {
- wait(random(600, 800));
- return walkNext(path, dist);
- }
- try {
- walkTileMM(getNext(path));
- wait(random(200, 500));
- while (getMyPlayer().isMoving() && distanceTo(getDestination()) > 7) {
- wait(random(600, 800));
- }
- } catch (final Exception ignored) {
- }
- return distanceTo(path[path.length - 1]) < 4;
- }
- private RSTile getNext(final RSTile[] path) {
- RSTile nearest = path[0];
- for (final RSTile element : path) {
- if (distanceBetween(element, path[path.length - 1]) < distanceBetween(nearest, path[path.length - 1])) {
- if (tileOnMap(element)) {
- nearest = element;
- }
- }
- }
- return nearest;
- }
- public boolean isMining() {
- return getMyPlayer().getAnimation() != -1;
- }
- public void sleep(int min, int max) {
- wait(random(min, max));
- }
- public RSTile getValidTile(RSTile tile) {
- while (!pf.validTile(tile))
- tile = new RSTile((getLocation().getX() + tile.getX()) / 2, (getLocation().getY() + tile.getY()) / 2);
- return tile;
- }
- class WorldSwitcher {
- public World[] worlds = null;
- private final int WORLD_INTERFACE = 744;
- public RSInterfaceComponent[] getComponents() {
- if (COMPONENTS == -1) {
- COMPONENTS = getComponentIndex();
- return getComponents();
- }
- return RSInterface.getChildInterface(WORLD_INTERFACE, COMPONENTS).getComponents();
- }
- public void changeWorld() {
- try {
- while (isLoggedIn()) {
- logout();
- sleep(200, 600);
- }
- int x9 = random(345, 415);
- int y9 = random(242, 250);
- clickMouse(x9, y9, true);
- sleep(1000, 2000);
- if (worlds == null)
- loadWorlds();
- Rectangle theRect = new Rectangle(79, 148, 608, 229);
- Rectangle a = null;
- World world = null;
- while (world == null || !world.canSwitch()) {
- world = worlds[random(0, worlds.length)];
- //log("getting world");
- }
- while (!theRect.contains((a = world.getArea()))) {
- if (!theRect.contains(getMouseLocation())) {
- moveMouse(theRect.x + Bot.getInputManager().random(0, world.getArea().width), theRect.y + Bot.getInputManager().random(0, world.getArea().height));
- }
- if (new Point(a.x, a.y).distance(new Point(100, 132)) < new Point(
- a.x, a.y).distance(new Point(104, 455))) {
- scroll(false);
- try {
- Thread.sleep(20);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- } else {
- scroll(true);
- try {
- Thread.sleep(20);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- clickMouse(world.getArea().x + Bot.getInputManager().random(0, world.getArea().width), world.getArea().y + Bot.getInputManager().random(0, world.getArea().height), true);
- world.reset();
- } catch (Exception e) {
- changeWorld();
- }
- }
- public int getComponentIndex() {
- RSInterface inter = RSInterface.getInterface(744);
- for (RSInterfaceChild child : inter.getChildren()) {
- for (RSInterfaceComponent com : child.getComponents()) {
- if (com.getText().contains("World")) {
- return child.getIndex();
- }
- }
- }
- return -1;
- }
- public void scroll(boolean up) {
- int x = getMouseLocation().x;
- int y = getMouseLocation().y;
- MouseWheelEvent event = new MouseWheelEvent(((Applet) Bot.getClient()),
- MouseEvent.MOUSE_WHEEL, System.currentTimeMillis(), 0, x,
- y, 0, false, MouseWheelEvent.WHEEL_UNIT_SCROLL, 3, (up ? 1 : -1));
- Bot.getClient().getMouseWheel()._mouseWheelMoved(event);
- }
- public void loadWorlds() {
- ArrayList<World> result = new ArrayList<World>();
- RSInterfaceComponent[] coms = getComponents();
- int index = 0;
- try {
- for (RSInterfaceComponent i : coms) {
- index++;
- if (i != null && i.isValid()) {
- if (i.getText() != null && !i.getText().contains("Bounty")
- && i.getText().contains("World") && !coms[index].getText().isEmpty()) {
- int pop = coms[index].getText().equals("FULL")
- || coms[index].getText().equals("OFFLINE") ? 2000
- : Integer.parseInt(coms[index].getText().trim());
- boolean mem = coms[index + 6].getText()
- .contains("bers");
- String worldNam = i.getText();
- int worldNum = Integer.parseInt(worldNam.replace(
- "World ", ""));
- result.add(new World(i.getText(), worldNum, pop, mem,
- index - 1));
- }
- }
- }
- } catch (Exception e) {
- loadWorlds();
- }
- worlds = result.toArray(new World[result.size()]);
- }
- }
- class World {
- private String worldName;
- private int worldNumber = -1;
- private int index = 0;
- private int worldPopulation = -1;
- private boolean members = false;
- private final int WORLD_INTERFACE = 744;
- Timer canswitch = null;
- public World(String worldName, int worldNumber,
- int worldPopulation, boolean members, int i) {
- this.index = i;
- this.worldName = worldName;
- this.worldNumber = worldNumber;
- this.worldPopulation = worldPopulation;
- this.members = members;
- }
- public boolean canSwitch() {
- if (!isMembers() || worldNumber == 26 || worldNumber == 72 || worldNumber == 86 || worldNumber == 18 || worldNumber == 65 || worldNumber == 137 || worldNumber == 124) {
- return false;
- }
- if (canswitch == null)
- return true;
- int pop = getWorldPopulation();
- if (pop > 0 && pop < 400) {
- return canswitch.getminutes() > 27;
- } else if (pop >= 400 && pop <= 800) {
- return canswitch.getminutes() > 24;
- } else if (pop >= 800 && pop <= 1200) {
- return canswitch.getminutes() > 21;
- } else if (pop >= 1200 && pop <= 1600) {
- return canswitch.getminutes() > 19;
- } else if (pop >= 1600 && pop <= 2000) {
- return canswitch.getminutes() > 16;
- } else if (pop == 2000) {
- return canswitch.getminutes() > 13;
- }
- return false;
- }
- public void reset() {
- canswitch = new Timer();
- }
- public RSInterfaceComponent toInterface() {
- try {
- return getComponents()[index];
- } catch (Exception e) {
- return toInterface();
- }
- }
- private RSInterfaceComponent[] getComponents() {
- return RSInterface.getChildInterface(WORLD_INTERFACE, COMPONENTS).getComponents();
- }
- public boolean isMembers() {
- return members;
- }
- public int getWorldNumber() {
- return worldNumber;
- }
- public String getWorldName() {
- return worldName;
- }
- public int getWorldPopulation() {
- return worldPopulation;
- }
- public Rectangle getArea() {
- RSInterfaceComponent com = toInterface();
- Rectangle r = com.getArea();
- r.y -= RSInterface.getChildInterface(WORLD_INTERFACE, COMPONENTS).getVerticalScrollBarPosition();
- r.width = 608;
- r.x = 79;
- return r;
- }
- }
- class Timer {
- private long start;
- private int time;
- public Timer(int time) {
- start = System.currentTimeMillis();
- this.time = time;
- }
- public long getTimeElapsed() {
- return (System.currentTimeMillis() - start);
- }
- public Timer() {
- this(0);
- }
- public boolean isDone() {
- return (System.currentTimeMillis() - start) > time;
- }
- public void reset() {
- start = System.currentTimeMillis();
- }
- public void resest(int newtime) {
- time = newtime;
- reset();
- }
- public long gethours() {
- return (System.currentTimeMillis() - start) / (1000 * 60 * 60);
- }
- public long getminutes() {
- return ((System.currentTimeMillis() - start) - gethours() * 1000 * 60 * 60) / (1000 * 60);
- }
- public long getseconds() {
- return ((System.currentTimeMillis() - start) - getminutes() * 1000 * 60 * 60) / (1000);
- }
- @Override
- public String toString() {
- return timeToString((System.currentTimeMillis() - start));
- }
- }
- public String timeToString(long time) {
- final long hours = time / (1000 * 60 * 60);
- time -= hours * 1000 * 60 * 60;
- final long minutes = time / (1000 * 60);
- time -= minutes * 1000 * 60;
- final long seconds = time / 1000;
- String str = "";
- if (hours < 10)
- str += "0";
- str += hours + ":";
- if (minutes < 10)
- str += "0";
- str += minutes + ":";
- if (seconds < 10)
- str += "0";
- str += seconds;
- return str;
- }
- class Pathfinder {
- public int basex, basey;
- public int[][] blocks;
- public Pathfinder() {
- reload();
- }
- public void reload() {
- basex = Bot.getClient().getBaseX();
- basey = Bot.getClient().getBaseY();
- blocks = Bot.getClient().getRSGroundDataArray()[Bot.getClient()
- .getPlane()].getBlocks();
- }
- public RSTile[] findPath(RSTile dest) {
- if (!isValid())
- reload();
- return findPath(getLocation(), dest);
- }
- public RSTile[] findPath(RSTile start, RSTile dest) {
- if (!isValid())
- reload();
- return findPath(
- new Node(start.getX() - basex, start.getY() - basey),
- new Node(dest.getX() - basex, dest.getY() - basey));
- }
- public RSTile[] findPath(Node start, Node dest) {
- if (!isValid())
- reload();
- if (!canReach(new RSTile(dest.x + basex, dest.y + basey), false)) {
- return null;
- }
- ArrayList<Node> closed = new ArrayList<Node>();
- ArrayList<Node> open = new ArrayList<Node>();
- Node current = start;
- open.add(current);
- while (open.size() != 0) {
- current = getBestNode(open);
- closed.add(current);
- open.remove(current);
- for (Node node : getNodesAround(current)) {
- if (!closed.contains(node)) {
- if (!open.contains(node)) {
- node.parent = current;
- node.cost = current.cost
- + getMovementCost(node, current);
- node.heuristic = node.cost
- + getHeuristicCost(node, dest);
- open.add(node);
- } else {
- if (current.cost + getMovementCost(node, current) < node.cost) {
- node.parent = current;
- node.cost = current.cost
- + getMovementCost(node, current);
- node.heuristic = node.cost
- + getHeuristicCost(node, dest);
- }
- }
- }
- }
- if (closed.contains(dest)) {
- final ArrayList<RSTile> path = new ArrayList<RSTile>();
- Node node = closed.get(closed.size() - 1);
- while (node.parent != null) {
- path.add(new RSTile(node.x + basex, node.y + basey));
- node = node.parent;
- }
- return reversePath(path.toArray(new RSTile[path.size()]));
- }
- }
- return null;
- }
- public Node getBestNode(ArrayList<Node> nodes) {
- Node winner = null;
- for (Node node : nodes) {
- if (winner == null || node.cost < winner.cost) {
- winner = node;
- }
- }
- return winner;
- }
- public double getHeuristicCost(Node current, Node dest) {
- float dx = dest.x - current.x;
- float dy = dest.y - current.y;
- return (double) (Math.sqrt((dx * dx) + (dy * dy)));
- }
- public double getMovementCost(Node current, Node dest) {
- return (double) Math.hypot(dest.x - current.x, dest.y - current.y);
- }
- /**
- * credits to jacmob
- */
- public ArrayList<Node> getNodesAround(Node node) {
- final ArrayList<Node> tiles = new ArrayList<Node>();
- final int curX = node.x, curY = node.y;
- if (curX > 0 && curY < 103
- && (blocks[curX - 1][curY + 1] & 0x1280138) == 0
- && (blocks[curX - 1][curY] & 0x1280108) == 0
- && (blocks[curX][curY + 1] & 0x1280120) == 0) {
- tiles.add(new Node(curX - 1, curY + 1));
- }
- if (curY < 103 && (blocks[curX][curY + 1] & 0x1280120) == 0) {
- tiles.add(new Node(curX, curY + 1));
- }
- if (curX > 0 && curY < 103
- && (blocks[curX - 1][curY + 1] & 0x1280138) == 0
- && (blocks[curX - 1][curY] & 0x1280108) == 0
- && (blocks[curX][curY + 1] & 0x1280120) == 0) {
- tiles.add(new Node(curX + 1, curY + 1));
- }
- if (curX > 0 && (blocks[curX - 1][curY] & 0x1280108) == 0) {
- tiles.add(new Node(curX - 1, curY));
- }
- if (curX < 103 && (blocks[curX + 1][curY] & 0x1280180) == 0) {
- tiles.add(new Node(curX + 1, curY));
- }
- if (curX > 0 && curY > 0
- && (blocks[curX - 1][curY - 1] & 0x128010e) == 0
- && (blocks[curX - 1][curY] & 0x1280108) == 0
- && (blocks[curX][curY - 1] & 0x1280102) == 0) {
- tiles.add(new Node(curX - 1, curY - 1));
- }
- if (curY > 0 && (blocks[curX][curY - 1] & 0x1280102) == 0) {
- tiles.add(new Node(curX, curY - 1));
- }
- if (curX < 103 && curY > 0
- && (blocks[curX + 1][curY - 1] & 0x1280183) == 0
- && (blocks[curX + 1][curY] & 0x1280180) == 0
- && (blocks[curX][curY - 1] & 0x1280102) == 0) {
- tiles.add(new Node(curX + 1, curY - 1));
- }
- return tiles;
- }
- public boolean validTile(RSTile tile) {
- return canReach(tile, false);
- }
- public boolean isValid() {
- return basex == Bot.getClient().getBaseX()
- && basey == Bot.getClient().getBaseY();
- }
- class Node {
- public final int x, y;
- public double cost = 0, heuristic = 0;
- public Node parent;
- public Node(int x, int y) {
- this.x = x;
- this.y = y;
- }
- public boolean equals(Object other) {
- if (other instanceof Node) {
- Node o = (Node) other;
- return x == o.x && y == o.y;
- }
- return false;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment