Advertisement
Guest User

Untitled

a guest
May 30th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.96 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics2D;
  3. import java.awt.Image;
  4. import java.awt.Point;
  5. import java.awt.Rectangle;
  6. import java.io.IOException;
  7. import java.net.URL;
  8. import java.util.Arrays;
  9.  
  10. import javax.imageio.ImageIO;
  11.  
  12. import org.osbot.rs07.api.filter.Filter;
  13. import org.osbot.rs07.api.map.Position;
  14. import org.osbot.rs07.api.model.Entity;
  15. import org.osbot.rs07.api.model.NPC;
  16. import org.osbot.rs07.api.model.RS2Object;
  17. import org.osbot.rs07.api.ui.Option;
  18. import org.osbot.rs07.api.ui.RS2Widget;
  19. import org.osbot.rs07.canvas.paint.Painter;
  20. import org.osbot.rs07.input.mouse.EntityDestination;
  21. import org.osbot.rs07.input.mouse.MiniMapTileDestination;
  22. import org.osbot.rs07.input.mouse.RectangleDestination;
  23. import org.osbot.rs07.script.MethodProvider;
  24. import org.osbot.rs07.script.Script;
  25. import org.osbot.rs07.script.ScriptManifest;
  26. import org.osbot.rs07.utility.ConditionalSleep;
  27.  
  28. import com.scripts.mrsdefnerd.api.TimeHandler;
  29.  
  30. @ScriptManifest(author = "Mrsdefnerd", info = "Planks logs", logo = "?", name = "Auto planker pro", version = 0.1D)
  31. public class AutoPlanker extends Script implements Painter {
  32.  
  33. public static final Position[] PATH = {new Position(3253, 3421, 0), new Position(3259, 3428, 0), new Position(3270, 3428, 0), new Position(3280, 3429, 0),
  34. new Position(3280, 3429, 0), new Position(3286, 3438, 0), new Position(3287, 3449, 0), new Position(3291, 3459, 0), new Position(3296, 3469, 0), new Position(3300, 3483, 0),
  35. new Position(3302, 3491, 0)};
  36.  
  37. private Image cursor;
  38.  
  39. public static final Plank PLANK = Plank.OAK_PLANK;
  40. public static final String SAW_NPC = "Sawmill operator";
  41. public static final int INTERACE_ID = 403;
  42.  
  43. private long timeRan;
  44.  
  45. private int nextRun = MethodProvider.random(25, 40);
  46.  
  47. private int planksMade = 0;
  48.  
  49. private String state = "N/A";
  50.  
  51. public void onStart() {
  52. this.antiBan.unregisterAllBehaviors();
  53. warn("Loading essentials, please wait..");
  54. timeRan = System.currentTimeMillis();
  55. this.cursor = getImage("http://i.imgur.com/ACfxYs3.png");
  56. }
  57.  
  58. @Override
  59. public int onLoop() throws InterruptedException {
  60. if (!settings.isRunning() && settings.getRunEnergy() >= nextRun) {
  61. if (settings.setRunning(true))
  62. new ConditionalSleep(1000, 2000) {
  63.  
  64. @Override
  65. public boolean condition()
  66. throws InterruptedException {
  67. return settings.isRunning();
  68. }
  69.  
  70. }.sleep();
  71. nextRun = MethodProvider.random(25, 40);
  72. }
  73. if (inventory.contains(PLANK.getLogName()) && inventory.getAmount(995) >= PLANK.getCost() * 27 && inventory.isFull()) {
  74. if (widgets.isVisible(INTERACE_ID)) {
  75. state = "Converting logs..";
  76. RS2Widget w = widgets.get(INTERACE_ID, PLANK.getChildId());
  77. if (w != null && w.isVisible()) {
  78. if (interact(w, "All")) {
  79. int amount = (int) inventory.getAmount(PLANK.getName());
  80. if (new ConditionalSleep(1000, 2000) {
  81.  
  82. @Override
  83. public boolean condition()
  84. throws InterruptedException {
  85. return !inventory.contains(PLANK.getLogName());
  86. }
  87.  
  88. }.sleep()) {
  89. planksMade += (int) inventory.getAmount(PLANK.getName()) - amount;
  90. }
  91. }
  92. }
  93. } else {
  94. NPC sawman = getSawmillNpc();
  95. if (sawman != null && sawman.isVisible()) {
  96. state = "Interacting with sawman..";
  97. if (sawman.interact("Buy-plank"))
  98. new ConditionalSleep(2500, 4000) {
  99.  
  100. @Override
  101. public boolean condition()
  102. throws InterruptedException {
  103. return widgets.isVisible(INTERACE_ID);
  104. }
  105.  
  106. }.sleep();
  107. } else {
  108. state = "Walking to sawmill..";
  109. traverse(PATH, true);
  110. }
  111. }
  112. } else {
  113. if (PATH[0].distance(myPosition()) <= 7) {
  114. final RS2Object bank = getBankBooth();
  115. if (bank != null) {
  116. if (getBank().isOpen()) {
  117. state = "Banking..";
  118. if (!getBank().contains(PLANK.getLogName()) || getBank().getAmount(995) < PLANK.getCost() * 27) {
  119. warn("Out of required items, stopping..");
  120. stop();
  121. return 0;
  122. }
  123. if (inventory.contains(PLANK.getName())) {
  124. if (getBank().depositAll(PLANK.getName()))
  125. new ConditionalSleep(1000, 2000) {
  126.  
  127. @Override
  128. public boolean condition()
  129. throws InterruptedException {
  130. return !inventory.contains(PLANK.getName());
  131. }
  132.  
  133. }.sleep();
  134. } else {
  135. if (inventory.getAmount(995) < 27 * PLANK.getCost()) {
  136. if (inventory.isFull() && !inventory.contains(995))
  137. if (getBank().deposit(PLANK.getLogName(), 1))
  138. new ConditionalSleep(1000, 2000) {
  139.  
  140. @Override
  141. public boolean condition()
  142. throws InterruptedException {
  143. return !inventory.isFull();
  144. }
  145.  
  146. }.sleep();
  147. if (getBank().withdraw(995, 27 * PLANK.getCost()))
  148. new ConditionalSleep(1000, 2000) {
  149.  
  150. @Override
  151. public boolean condition()
  152. throws InterruptedException {
  153. return inventory.contains(PLANK.getLogName());
  154. }
  155.  
  156. }.sleep();
  157. }
  158. if (inventory.getAmount(995) >= 27 * PLANK.getCost())
  159. if (getBank().withdrawAll(PLANK.getLogName()))
  160. new ConditionalSleep(1000, 2000) {
  161.  
  162. @Override
  163. public boolean condition()
  164. throws InterruptedException {
  165. return inventory.contains(PLANK.getLogName());
  166. }
  167.  
  168. }.sleep();
  169. }
  170. } else {
  171. state = "Opening bank..";
  172. if (bank.isVisible()) {
  173. if (interact(bank, "Bank")) {
  174. new ConditionalSleep(3500, 4000) {
  175.  
  176. @Override
  177. public boolean condition()
  178. throws InterruptedException {
  179. return getBank().isOpen();
  180. }
  181.  
  182. }.sleep();
  183. }
  184. } else {
  185. if (walk(bank.getPosition()))
  186. new ConditionalSleep(3500, 4000) {
  187.  
  188. @Override
  189. public boolean condition()
  190. throws InterruptedException {
  191. return bank.isVisible();
  192. }
  193.  
  194. }.sleep();
  195. }
  196. }
  197. }
  198. } else {
  199. state = "Walking to bank..";
  200. traverse(PATH, false);
  201. }
  202. }
  203. return 250;
  204. }
  205.  
  206. private long lastClick = 0;
  207.  
  208. @Override
  209. public void onPaint(Graphics2D g) {
  210. int x = getMouse().getPosition().x, y = getMouse().getPosition().y;
  211.  
  212. if (System.currentTimeMillis() - lastClick < 500)
  213. g.setColor(Color.red);
  214. else
  215. g.setColor(Color.WHITE);
  216.  
  217. g.drawImage(cursor, x, y, null);
  218.  
  219. g.setColor(Color.black);
  220. g.drawRect(325, 196, 190, 142);
  221. g.setColor(new Color(118, 118, 118, 100));
  222. g.fillRect(325, 196, 190, 142);
  223.  
  224. g.setColor(Color.WHITE);
  225. g.drawString("Planks (hr): "+TimeHandler.formatNumber(planksMade)+" ("+TimeHandler.perHour(timeRan, planksMade)+")", 330, 210);
  226. g.drawString("State: "+state, 330, 225);
  227. g.drawString("Time ran: "+TimeHandler.runTime(timeRan), 330, 240);
  228.  
  229. g.drawString("X: "+x+" Y: "+y, 200, 300);
  230. }
  231.  
  232. private boolean traverse(Position[] path, boolean reverse) {
  233. for (int i = reverse ? path.length - 1 : 0; reverse ? i >= 0 : i < path.length;) {
  234. Position p = path[i];
  235. if (p != null && p.distance(myPosition()) <= 17) {
  236. if (getMap().getDestination() == null || getMap().getDestination().distance(p) > 4) {
  237. p = new Position(p.getX() + MethodProvider.random(-2, 2), p.getY() + MethodProvider.random(-2, 2), p.getZ());
  238. if (walk(p))
  239. return true;
  240. }
  241. return false;
  242. }
  243. if (reverse)
  244. i--;
  245. else
  246. i++;
  247. }
  248. return false;
  249. }
  250.  
  251. private boolean walk(Position pos) {
  252. MiniMapTileDestination dest = new MiniMapTileDestination(bot, pos);
  253. return getMouse().click(dest);
  254. }
  255.  
  256. private RS2Object getBankBooth() {
  257. RS2Object ob = null;
  258. int distance = -1;
  259. for (RS2Object o : objects.getAll())
  260. if (ob == null || distance(o.getPosition(), myPosition()) < distance) {
  261. if (o != null && o.getName().equals("Bank booth") && o.getPosition().distance(myPosition()) <= 10 && Arrays.deepToString(o.getDefinition().getActions()).contains("Bank")) {
  262. ob = o;
  263. distance = distance(o.getPosition(), myPosition());
  264. }
  265. }
  266. return ob;
  267. }
  268.  
  269. @SuppressWarnings("unchecked")
  270. private NPC getSawmillNpc() {
  271. return npcs.closest(new Filter<NPC>() {
  272.  
  273. @Override
  274. public boolean match(NPC n) {
  275. if (n != null && n.getName().equals(SAW_NPC) && n.getPosition().distance(myPosition()) <= 10)
  276. return true;
  277. return false;
  278. }
  279.  
  280. });
  281. }
  282.  
  283. private boolean interact(Entity e, String action) throws InterruptedException {
  284. if (e != null) {
  285. if (e.isVisible()) {
  286. EntityDestination dest = new EntityDestination(bot, e);
  287. Option o = getAction(action);
  288. if (o == null)
  289. getMouse().move(dest);
  290. return selectAction(action);
  291. }
  292. }
  293. return false;
  294. }
  295.  
  296. private boolean interact(RS2Widget w, String action) throws InterruptedException {
  297. if (w != null) {
  298. if (w.isVisible()) {
  299. Rectangle r = shrink(w.getRectangle());
  300. RectangleDestination dest = new RectangleDestination(bot, r);
  301. if (!r.contains(getMouse().getPosition())) {
  302. getMouse().move(dest);
  303. MethodProvider.sleep(MethodProvider.random(50, 100));
  304. }
  305. Option o = getAction(action);
  306. if (o != null)
  307. return selectAction(action);
  308. }
  309. }
  310. return false;
  311. }
  312.  
  313. private boolean selectAction(String action) throws InterruptedException {
  314. int index = getIndex(action);
  315. if (index > -1) {
  316. if (getMouse().click(true)) {
  317. MethodProvider.sleep(MethodProvider.random(100, 250));
  318. if (getMenuAPI().isOpen()) {
  319. Point p = getPointForIndex(index);
  320. if (getMenuAPI().getMenu().get(index).action.toLowerCase().contains(action.toLowerCase()))
  321. return getMouse().click(p.x, p.y, false);
  322. }
  323. }
  324. }
  325. return false;
  326. }
  327.  
  328. private int distance(Position pos, Position pos1) {
  329. if (pos1.getX() == pos.getX())
  330. return Math.abs(pos.getX() - pos.getX());
  331. else if (pos.getY() == pos1.getY())
  332. return Math.abs(pos.getY() - pos.getY());
  333. return (int) Math.ceil(Math.sqrt(Math.pow(pos1.getX() - pos.getX(), 2) + Math.pow(pos1.getY() - pos.getY(), 2)));
  334. }
  335.  
  336. private int getIndex(String action) {
  337. int i = 0;
  338. for (Option o : getMenuAPI().getMenu()) {
  339. if (o.action.toLowerCase().contains(action.toLowerCase()))
  340. return i;
  341. i++;
  342. }
  343. return -1;
  344. }
  345.  
  346. private Point getPointForIndex(int index) {
  347. int x = getMenuAPI().getX() + 15 + MethodProvider.random(0, getMenuAPI().getWidth() - 40);
  348. int y = getMenuAPI().getY() + 18 + index * 15 + MethodProvider.random(4, 12);
  349. return new Point(x, y);
  350. }
  351.  
  352. private Option getAction(String action) {
  353. for (Option o : getMenuAPI().getMenu()) {
  354. if (o.action.toLowerCase().contains(action.toLowerCase())) {
  355. return o;
  356. }
  357. }
  358. return null;
  359. }
  360.  
  361. private Rectangle shrink(Rectangle rect) {
  362. return new Rectangle((int) (rect.x + rect.width * 0.25), (int) (rect.y + rect.height * 0.25), (int) (rect.width - rect.width * 0.35), (int) (rect.height - rect.height * 0.35));
  363. }
  364.  
  365. private Image getImage(String url) {
  366. try { return ImageIO.read(new URL(url)); }
  367. catch(IOException e) { return null; }
  368. }
  369.  
  370. public enum Plank {
  371.  
  372. PLANK("Logs", 100, 104),
  373. OAK_PLANK("Oak logs", 250, 110),
  374. TEAK_PLANK("Teak logs", 500, 115),
  375. MAHOGANY_PLANK("Mahogany logs", 1500, 120);
  376.  
  377. private String logName;
  378. private int childId, cost;
  379.  
  380. Plank(String logName, int cost, int childId) {
  381. this.logName = logName;
  382. this.childId = childId;
  383. this.cost = cost;
  384. }
  385.  
  386. @Override
  387. public String toString() {
  388. final String name = super.name();
  389. return name.charAt(0) + name.substring(1).toLowerCase().replace("_", " ");
  390. }
  391.  
  392. public final String getName() {
  393. return toString();
  394. }
  395.  
  396. public final String getLogName() {
  397. return this.logName;
  398. }
  399.  
  400. public final int getCost() {
  401. return this.cost;
  402. }
  403.  
  404. public final int getChildId() {
  405. return this.childId;
  406. }
  407.  
  408. }
  409.  
  410.  
  411.  
  412. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement