Pandemic Posted January 13, 2014 Author Share Posted January 13, 2014 Awesome in-depth guide, thank you. No problem I'm trying to make my script walk from the mine back to the bank and it's acting weird. I have this for the positions: private Position[] path = { new Position(3289, 3371, 0), new Position(3290, 3374, 0), new Position(3294, 3386, 0), new Position(3290, 3391, 0), new Position(3291, 3401, 0), new Position(3289, 3406, 0), new Position(3286, 3418, 0), new Position(3276, 3426, 0), new Position(3264, 3429, 0), new Position(3254, 3421, 0) }; I manually put them in so I don't know why it's acting odd. Once it gets to the 3rd position it clicks back towards the mine then starts walking South. Any idea what's causing this? Here is my whole script for reference. import java.awt.Graphics; import org.osbot.script.Script; import org.osbot.script.ScriptManifest; import org.osbot.script.mouse.MinimapTileDestination; import org.osbot.script.rs2.map.Position; import org.osbot.script.rs2.model.RS2Object; import org.osbot.script.rs2.utility.Area; @ScriptManifest(author ="Lingoling", info="Mines Tin in Varrock east. Start in bank or at mine.", name = "BasicMiner", version = 0) public class BasicMiner extends Script { private static final int[] TIN_ID = {11636,11634, 11635}; private enum State { MINE, WALK_TO_BANK, BANK, WALK_TO_MINE}; private State getState() { if (client.getInventory().isFull() && MINE_AREA.contains(myPlayer())) return State.WALK_TO_BANK; if (!client.getInventory().isFull() && BANK_AREA.contains(myPlayer())) return State.WALK_TO_MINE; if (client.getInventory().isFull() && BANK_AREA.contains(myPlayer())) return State.BANK; return State.MINE; } private Position[] path = { new Position(3289, 3371, 0), new Position(3290, 3374, 0), new Position(3294, 3386, 0), new Position(3290, 3391, 0), new Position(3291, 3401, 0), new Position(3289, 3406, 0), new Position(3286, 3418, 0), new Position(3276, 3426, 0), new Position(3264, 3429, 0), new Position(3254, 3421, 0) }; private static final Area MINE_AREA = new Area(3277, 3358, 3293, 3371); private static final Area BANK_AREA = new Area(3250, 3419, 3257, 3423); private void traversePath(Position[] path, boolean reversed) throws InterruptedException { if (!reversed) { for (int i = 1; i < path.length; i++) if (!walkTile(path[i])) i--; } else { for (int i = path.length-2; i > 0; i--) if (!walkTile(path[i])) i++; } } private boolean walkTile(Position p) throws InterruptedException { client.moveMouse(new MinimapTileDestination(bot, p), false); sleep(random(150, 250)); client.pressMouse(); int failsafe = 0; while (failsafe < 10 && myPlayer().getPosition().distance(p) > 2) { sleep(200); failsafe++; if (myPlayer().isMoving()) failsafe = 0; } if (failsafe == 10) return false; return true; } @Override public void onStart() { log("I like women but they don't like me, and it's hard, yes siree"); } @Override public int onLoop() throws InterruptedException { switch (getState()) { case MINE: if (!myPlayer().isAnimating()) { RS2Object vein = closestObject(TIN_ID); if (vein != null) { if (vein.interact("Mine")) sleep(random(1000, 1500)); } } break; case WALK_TO_BANK: traversePath(path, false); sleep(random(1500, 2500)); break; case WALK_TO_MINE: traversePath(path, true); sleep(random(1500, 2500)); break; case BANK: RS2Object bank = closestObjectForName("Bank booth"); if (bank != null) { if (bank.interact("Bank")) { while (!client.getBank().isOpen()) sleep(250); client.getBank().depositAll(); } } break; } return random(200, 300); } @Override public void onExit() { log("Thanks ********a."); } @Override public void onPaint(Graphics g) { } } I'm not too sure, did you change the areas at all, and double check your path. Quote Link to comment Share on other sites More sharing options...
Dreamliner Posted January 13, 2014 Share Posted January 13, 2014 (edited) while (failsafe < 10 && myPlayer().getPosition().distance(p) > 2) { sleep(200); if (!myPlayer().isMoving()) failsafe++; }No need to set it to 0 every time you move. Once you start moving, you won't stop unless you get interupted or something (causing the while loop to fail anyway).Considering how many times this portion of the code is run O(n^2), this should be taught in the tutorial to at least have code efficiency running through their minds while coding Edited January 13, 2014 by dreamliner Quote Link to comment Share on other sites More sharing options...
Pandemic Posted January 13, 2014 Author Share Posted January 13, 2014 (edited) while (failsafe < 10 && myPlayer().getPosition().distance(p) > 2) { sleep(200); if (!myPlayer().isMoving()) failsafe++; }What's this for?Mine resets your failsafe if your character is actually moving, so a couple hundred ms of lag over the span of a few seconds won't kick you out of the loop, it'll basically restart it. Edited January 13, 2014 by Pandemic Quote Link to comment Share on other sites More sharing options...
Dreamliner Posted January 13, 2014 Share Posted January 13, 2014 What's this for? Mine resets your failsafe if your character is actually moving, so a couple hundred ms of lag over the span of a few seconds won't kick you out of the loop, it'll basically restart it. No need to set it to 0 every time you move. Once you start moving, you won't stop unless you get interrupted or something (causing the while loop to fail anyway). Considering how many times this portion of the code is run O(n^2), this should be taught in the tutorial to at least have code efficiency running through their minds while coding Also, if if kicks you out of the loop, a normal player would click again anyway. I've never once in my RS play time not try to click again after 2 seconds of lag Quote Link to comment Share on other sites More sharing options...
Pandemic Posted January 13, 2014 Author Share Posted January 13, 2014 No need to set it to 0 every time you move. Once you start moving, you won't stop unless you get interrupted or something (causing the while loop to fail anyway). Considering how many times this portion of the code is run O(n^2), this should be taught in the tutorial to at least have code efficiency running through their minds while coding Also, if if kicks you out of the loop, a normal player would click again anyway. I've never once in my RS play time not try to click again after 2 seconds of lag Mine will click again after 2 seconds of lag, it's yours that won't. Your version will return false if there is a TOTAL of 2 seconds of lag, not 2 seconds of straight lag. Quote Link to comment Share on other sites More sharing options...
Dreamliner Posted January 13, 2014 Share Posted January 13, 2014 Mine will click again after 2 seconds of lag, it's yours that won't. Your version will return false if there is a TOTAL of 2 seconds of lag, not 2 seconds of straight lag. Correct. Why is this a problem? Quote Link to comment Share on other sites More sharing options...
lingoling Posted January 13, 2014 Share Posted January 13, 2014 No problem I'm not too sure, did you change the areas at all, and double check your path. I didn't change any of the areas, and I've experienced this problem using the positions you provided and custom ones. I've been told that it's a problem with the API though. It runs for hours on end and randomly starts going off the path. I've also experienced this in other scripts too. Quote Link to comment Share on other sites More sharing options...
Pandemic Posted January 13, 2014 Author Share Posted January 13, 2014 Correct. Why is this a problem? It's not a problem at all, it's just not what I wanted it to do. Quote Link to comment Share on other sites More sharing options...
Sigma Posted January 23, 2014 Share Posted January 23, 2014 No point in creating static variables that are not going to be used at the class level in any other classes, especially if they are inside a class that's parent is Script. Also, it's probably a good idea to be consistent with formatting, especially when making a tutorial (I am referring to your braces). It would be different if this was black box code, or something of the like. Your walking method looks to be written poorly with static sleeps not accounting for movements or lag. I don't know how the walk method inside the api works, but you're explanation of it "tending to click until it's at the destination" can easily be remedied by not calling the method over and over if the current game destination is the tile you wanted to walk to. Another thing, you may want to check if the object you're trying to click is on the screen before clicking it as that could cause problems. Lastly, you could be stuck in an infinite loop with that banking method you've written. Instead, why not use a conditional sleep that times out after x seconds if the clients open method returns true? Quote Link to comment Share on other sites More sharing options...
Noterickho123 Posted January 23, 2014 Share Posted January 23, 2014 ty tat me alot 1 Quote Link to comment Share on other sites More sharing options...
Pandemic Posted January 23, 2014 Author Share Posted January 23, 2014 No point in creating static variables that are not going to be used at the class level in any other classes, especially if they are inside a class that's parent is Script. Also, it's probably a good idea to be consistent with formatting, especially when making a tutorial (I am referring to your braces). It would be different if this was black box code, or something of the like. Your walking method looks to be written poorly with static sleeps not accounting for movements or lag. I don't know how the walk method inside the api works, but you're explanation of it "tending to click until it's at the destination" can easily be remedied by not calling the method over and over if the current game destination is the tile you wanted to walk to. Another thing, you may want to check if the object you're trying to click is on the screen before clicking it as that could cause problems. Lastly, you could be stuck in an infinite loop with that banking method you've written. Instead, why not use a conditional sleep that times out after x seconds if the clients open method returns true? By static variables I'll assume you're talking about my constants, and they're not strictly needed, but they make it easier to look at. Where are my braces not consistent? I'm not seeing it My walking method is far from perfect, this is a tutorial for learning purposes, so I'm not going to write some sophisticated walking method that the new scripters here won't understand. The API's walking will keep clicking the same spot (not because I'm calling it 100 times, you only have to call it once and it just spam clicks the position). There's no need to check if the object is on screen, because the interact function handles that. I agree it could get in an infinite loop, but once again, this is just a beginners tutorial so it'll work for learning purposes. Thanks for reading. Quote Link to comment Share on other sites More sharing options...
Nezz Posted January 23, 2014 Share Posted January 23, 2014 Hey Pandemic, any chance you could do a tutorial on creating a config? Like how in your AIO fighter, you can set what you attack, what you pick up, etc. Thanks Quote Link to comment Share on other sites More sharing options...
Pandemic Posted January 23, 2014 Author Share Posted January 23, 2014 Hey Pandemic, any chance you could do a tutorial on creating a config? Like how in your AIO fighter, you can set what you attack, what you pick up, etc. Thanks That'll be my next tutorial Quote Link to comment Share on other sites More sharing options...
Nezz Posted January 24, 2014 Share Posted January 24, 2014 That'll be my next tutorial I love you. Do you know when you'll be able to get to it? Config is the next big step for me. Then I need to add a little anti-ban, some more error checking, and I can upload my first script. :') Quote Link to comment Share on other sites More sharing options...
Sigma Posted January 24, 2014 Share Posted January 24, 2014 (edited) By static variables I'll assume you're talking about my constants, and they're not strictly needed, but they make it easier to look at. Where are my braces not consistent? I'm not seeing it My walking method is far from perfect, this is a tutorial for learning purposes, so I'm not going to write some sophisticated walking method that the new scripters here won't understand. The API's walking will keep clicking the same spot (not because I'm calling it 100 times, you only have to call it once and it just spam clicks the position). There's no need to check if the object is on screen, because the interact function handles that. I agree it could get in an infinite loop, but once again, this is just a beginners tutorial so it'll work for learning purposes. Thanks for reading. Omitting braces with only one line inside the statement is ok... but explain why you do that, otherwise people looking at the tutorial will be confused. Sometimes you did it with more than one line, though. I promise you that the walking method in the api does not recurse itself until the destination is reached, therefore you are able to easily fix it by doing what I stated above. Lastly, if this is a tutorial, make sure the code is presentable and functional with no logic errors or syntax errors. The entire point of a tutorial is to teach people how to code correctly. Edited January 24, 2014 by Sigma 1 Quote Link to comment Share on other sites More sharing options...