Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.54 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.FlowLayout;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Point;
  7. import java.awt.Polygon;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10.  
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JPanel;
  15. import javax.swing.JTextField;
  16.  
  17. import org.rsbot.event.listeners.PaintListener;
  18. import org.rsbot.script.Script;
  19. import org.rsbot.script.ScriptManifest;
  20. import org.rsbot.script.util.Filter;
  21. import org.rsbot.script.wrappers.RSArea;
  22. import org.rsbot.script.wrappers.RSItem;
  23. import org.rsbot.script.wrappers.RSModel;
  24. import org.rsbot.script.wrappers.RSNPC;
  25. import org.rsbot.script.wrappers.RSPlayer;
  26. import org.rsbot.script.wrappers.RSTile;
  27.  
  28. @ScriptManifest(authors = "Jomik", name = "Air Runner", version = 0.71, description = "This will run essence to the air altar.")
  29. public class AirRunner extends Script implements PaintListener {
  30. private final System system = new System();
  31. private final InfoDatabase info = new InfoDatabase();
  32. private final Trade trade = new Trade();
  33. private final GUI gui = new GUI();
  34. private final Paint paint = new Paint();
  35.  
  36. private enum State {
  37. GO_BANK, GO_ALTAR, TRADE, BANK, UNK_0
  38. }
  39.  
  40. @Override
  41. public boolean onStart() {
  42. return gui.show();
  43. }
  44.  
  45. @Override
  46. public int loop() {
  47. mouse.setSpeed(random(4, 10));
  48. if (!game.isLoggedIn())
  49. return 1000;
  50.  
  51. State curState = system.getState();
  52. switch (curState) {
  53. case TRADE:
  54. RSPlayer crafter = info.getCrafter();
  55. if (crafter == null) {
  56. system.setStatus("Searching for crafter");
  57. break;
  58. }
  59. if (trade.isConfirming()) {
  60. system.setStatus("Accepting");
  61. trade.accept();
  62. } else if (trade.isTrading()) {
  63. if (trade.getNumberOfItemsOffered() > 0) {
  64. system.setStatus("Accepting");
  65. trade.accept();
  66. } else if (inventory.contains(info.getEssenceID())) {
  67. system.setStatus("Offering items");
  68. trade.offer(info.getEssenceID(), trade.getFreeSlots());
  69. return random(1000, 1200);
  70. }
  71. } else if (crafter.isOnScreen()) {
  72. if (trade.requestTrade(crafter)) {
  73. system.setStatus("Requesting trade");
  74. return random(3000, 6000);
  75. }
  76.  
  77. } else if (calc.distanceTo(crafter) < 5) {
  78. camera.turnToCharacter(crafter);
  79. } else {
  80. system.walk(crafter.getLocation());
  81. system.setStatus("Walking to crafter");
  82. }
  83. break;
  84. case BANK:
  85. system.setStatus("Banking");
  86. if (bank.isOpen()) {
  87. if (inventory.getCount() > 0)
  88. bank.depositAll();
  89. bank.withdraw(info.getEssenceID(), 0);
  90. return random(600, 800);
  91. } else {
  92. bank.open();
  93. }
  94. break;
  95. case GO_ALTAR:
  96. if (system.checkEnergy()) {
  97. system.setStatus("Walking to altar");
  98. // system.walkPath(info.getPath());
  99. system.walk(system.getTileInAreaTowardsTile(info.getPathArea(),
  100. info.ALTAR_TILE));
  101. }
  102. break;
  103. case GO_BANK:
  104. if (system.checkEnergy()) {
  105. system.setStatus("Walking to bank");
  106. // system.walkPath(walking.reversePath(info.getPath()));
  107. system.walk(system.getTileInAreaTowardsTile(info.getPathArea(),
  108. info.BANK_TILE));
  109. }
  110. break;
  111. }
  112. return random(400, 700);
  113. }
  114.  
  115. private class System {
  116. private String status = "Starting up";
  117.  
  118. private State getState() {
  119. if (trade.isTrading() || trade.isConfirming())
  120. return State.TRADE;
  121. if ((inventory.containsOneOf(info.getPaymentID()) || !inventory
  122. .isFull())) {
  123. if (!info.atBank()) {
  124. return State.GO_BANK;
  125. }
  126. return State.BANK;
  127. }
  128. if (inventory.contains(info.getEssenceID())) {
  129. if (info.getCrafter() != null) {
  130. if (calc.distanceTo(info.getCrafter()) < 10) {
  131. return State.TRADE;
  132. }
  133. }
  134. if (!info.atAltar()) {
  135. return State.GO_ALTAR;
  136. }
  137. return State.TRADE;
  138. }
  139. log("ERROR: UNKNOWN STATE!");
  140. return State.UNK_0;
  141. }
  142.  
  143. private RSTile getTileInAreaTowardsTile(RSArea area, RSTile dest) {
  144. RSTile closest = walking.getClosestTileOnMap(dest);
  145. RSTile inArea = area.getNearestTile(closest);
  146. inArea = inArea.randomizeTile(3, 3);
  147. return area.getNearestTile(inArea);
  148. }
  149.  
  150. private void walk(RSTile location) {
  151. if (!walking.isRunEnabled() && walking.getEnergy() > 20)
  152. walking.setRun(true);
  153.  
  154. if (!getMyPlayer().isMoving()
  155. || calc.distanceTo(walking.getDestination()) < 6) {
  156. location = location.randomizeTile(2, 2);
  157. RSTile newTile = walking.getClosestTileOnMap(location);
  158. if (calc.tileOnScreen(newTile)) {
  159. mouse.move(calc.tileToScreen(newTile));
  160. menu.doAction("Walk here");
  161. } else {
  162. walking.walkTileMM(newTile);
  163. }
  164. }
  165. }
  166.  
  167. private boolean checkEnergy() {
  168. if (getMyPlayer().getAnimation() == info.getRestingAnim()) {
  169. RSNPC musician = npcs.getNearest(info.getMusicianID());
  170. if (musician != null) {
  171. if (calc.distanceTo(musician) < 4) {
  172. if (walking.getEnergy() > 90)
  173. return true;
  174. } else {
  175. if (walking.getEnergy() > 50)
  176. return true;
  177. }
  178. }
  179. } else if (walking.getEnergy() < 20) {
  180. RSNPC musician = npcs.getNearest(info.getMusicianID());
  181. if (musician != null) {
  182. if (calc.distanceTo(musician) > 3
  183. && calc.distanceTo(musician) < 15) {
  184. system.walk(musician.getLocation());
  185. } else {
  186. walking.rest();
  187. }
  188. } else {
  189. walking.rest();
  190. }
  191. system.setStatus("Resting");
  192. return false;
  193. }
  194. return true;
  195. }
  196.  
  197. private String getStatus() {
  198. return this.status;
  199. }
  200.  
  201. private void setStatus(String status) {
  202. this.status = status;
  203. }
  204. }
  205.  
  206. private class InfoDatabase {
  207. private final RSTile[] PATH_AREA_TILES = { new RSTile(3189, 3440),
  208. new RSTile(3182, 3440), new RSTile(3181, 3431),
  209. new RSTile(3171, 3431), new RSTile(3162, 3424),
  210. new RSTile(3148, 3422), new RSTile(3144, 3414),
  211. new RSTile(3124, 3406), new RSTile(3130, 3400),
  212. new RSTile(3143, 3410), new RSTile(3159, 3419),
  213. new RSTile(3173, 3425), new RSTile(3184, 3428) };
  214. private final RSArea PATH_AREA = new RSArea(PATH_AREA_TILES);
  215. private final RSTile BANK_TILE = new RSTile(3182, 3436);
  216. private final RSTile ALTAR_TILE = new RSTile(3129, 3407);
  217. private final RSArea ALTAR_AREA = new RSArea(new RSTile(3124, 3400),
  218. new RSTile(3134, 3408));
  219. private final RSArea BANK_AREA = new RSArea(new RSTile(3182, 3433),
  220. new RSTile(3189, 3446));
  221. private final int RESTING_ANIM = 11786;
  222. private final int ESSENCE_ID = 1436;
  223. private final int PURE_ESSENCE_ID = 7936;
  224. private final int PAYMENT_ID[] = { 1437, 7937, 556, 995 };
  225. private final int MUSICIAN_ID = 8699;
  226. private Filter<RSPlayer> CRAFTER_FILTER = new Filter<RSPlayer>() {
  227. public boolean accept(RSPlayer t) {
  228. if (t != null) {
  229. if (t.getName() != null) {
  230. return t.getName().toLowerCase()
  231. .equals(gui.getName().toLowerCase());
  232. }
  233. }
  234. return false;
  235. }
  236. };
  237. private boolean pure = false;
  238.  
  239. private RSArea getPathArea() {
  240. return this.PATH_AREA;
  241. }
  242.  
  243. private boolean atAltar() {
  244. return this.ALTAR_AREA.contains(getMyPlayer().getLocation());
  245. }
  246.  
  247. private boolean atBank() {
  248. return BANK_AREA.contains(getMyPlayer().getLocation());
  249. }
  250.  
  251. private int getRestingAnim() {
  252. return this.RESTING_ANIM;
  253. }
  254.  
  255. private int getMusicianID() {
  256. return this.MUSICIAN_ID;
  257. }
  258.  
  259. private int[] getPaymentID() {
  260. return this.PAYMENT_ID;
  261. }
  262.  
  263. private int getEssenceID() {
  264. if (this.pure)
  265. return this.PURE_ESSENCE_ID;
  266. return this.ESSENCE_ID;
  267. }
  268.  
  269. private RSPlayer getCrafter() {
  270.  
  271. RSPlayer player = players.getNearest(CRAFTER_FILTER);
  272. return player;
  273. }
  274. }
  275.  
  276. private class GUI {
  277. private String name = "";
  278. private boolean shouldStart = false;
  279.  
  280. private String getName() {
  281. return this.name.replace(' ', '\u00A0');
  282. }
  283.  
  284. private boolean show() {
  285. JFrame frame;
  286.  
  287. JPanel namePanel = new JPanel();
  288. JTextField crafterName = new JTextField(12);
  289. namePanel.setLayout(new FlowLayout());
  290. namePanel.add(new JLabel("Crafter name: "));
  291. namePanel.add(crafterName);
  292.  
  293. frame = new JFrame("GUI for Jomik's Air Runner!");
  294. JPanel panel = new JPanel();
  295. JButton submit = new JButton("Start!");
  296.  
  297. panel.setLayout(new BorderLayout());
  298. panel.add(namePanel, BorderLayout.CENTER);
  299.  
  300. frame.setLayout(new BorderLayout());
  301.  
  302. frame.add(panel, BorderLayout.NORTH);
  303. frame.add(submit, BorderLayout.SOUTH);
  304. frame.pack();
  305. frame.setVisible(true);
  306. frame.setAlwaysOnTop(true);
  307.  
  308. submit.addActionListener(new ActionListener(){
  309. public void actionPerformed(ActionEvent e){
  310. shouldStart = true;
  311. }
  312. });
  313.  
  314. while(!shouldStart)
  315. try{
  316. Thread.sleep(500);
  317. }catch(Exception e){
  318. return false;
  319. };
  320.  
  321. try{
  322. String newName = crafterName.getText();
  323. if (newName != null && !newName.equals("")) {
  324. this.name = newName;
  325. }
  326. else {
  327. log("ERROR: INVALID NAME");
  328. frame.dispose();
  329. return false;
  330. }
  331. }catch(Exception e){
  332. frame.dispose();
  333. shouldStart = false;
  334. return false;
  335. }
  336. shouldStart = false;
  337. frame.dispose();
  338. return true;
  339. }
  340. }
  341.  
  342. public class Trade {
  343. private final static int INTERFACE_MAIN = 335;
  344. private final static int INTERFACE_MAIN_ACCEPT = 16;
  345. private final static int INTERFACE_MAIN_DECLINE = 18;
  346. private final static int INTERFACE_MAIN_INV_SLOTS = 21;
  347. private final static int INTERFACE_MAIN_ITEMS = 31;
  348. private final static int INTERFACE_MAIN_INFO = 38;
  349.  
  350. private final static int INTERFACE_CONFIRM = 334;
  351. private final static int INTERFACE_CONFIRM_ACCEPT = 35;
  352. private final static int INTERFACE_CONFIRM_DECLINE = 36;
  353. private final static int INTERFACE_CONFIRM_NAME = 46;
  354. private final static int INTERFACE_CONFIRM_INFO = 33;
  355.  
  356. private boolean isTrading() {
  357. return interfaces.get(INTERFACE_MAIN)
  358. .getComponent(INTERFACE_MAIN_ACCEPT).isValid();
  359. }
  360.  
  361. private boolean isConfirming() {
  362. return interfaces.get(INTERFACE_CONFIRM)
  363. .getComponent(INTERFACE_CONFIRM_ACCEPT).isValid();
  364. }
  365.  
  366. private int getNumberOfItemsOffered() {
  367. int number = 0;
  368. for (int i = 0; i < 28; i++) {
  369. if (interfaces.get(INTERFACE_MAIN)
  370. .getComponent(INTERFACE_MAIN_ITEMS).getComponent(i)
  371. .getComponentStackSize() != 0)
  372. ++number;
  373. }
  374. return number;
  375. }
  376.  
  377. private int getFreeSlots() {
  378. if (isTrading()) {
  379. String text = interfaces.get(INTERFACE_MAIN)
  380. .getComponent(INTERFACE_MAIN_INV_SLOTS).getText()
  381. .substring(4, 6);
  382. text = text.trim();
  383. try {
  384. return Integer.parseInt(text);
  385. } catch (Exception e) {
  386. }
  387. }
  388. return 0;
  389. }
  390.  
  391. private boolean requestTrade(RSPlayer player) {
  392. if (player.isOnScreen()) {
  393. String action = "Trade with " + player.getName() + " (level: "
  394. + player.getCombatLevel() + ")";
  395.  
  396. RSModel model = player.getModel();
  397. if (model == null)
  398. return false;
  399. Point p = model.getPoint();
  400. if (p.x == -1 || p.y == -1) {
  401. return false;
  402. }
  403. mouse.click(p, false);
  404. return menu.doAction(action);
  405. }
  406. return false;
  407. }
  408.  
  409. private boolean accept() {
  410. if (isConfirming()) {
  411. if (interfaces.get(INTERFACE_CONFIRM)
  412. .getComponent(INTERFACE_CONFIRM_INFO).getText()
  413. .toLowerCase().contains("waiting"))
  414. return true;
  415. else
  416. return interfaces.get(INTERFACE_CONFIRM)
  417. .getComponent(INTERFACE_CONFIRM_ACCEPT)
  418. .doAction("Accept");
  419. }
  420.  
  421. else if (isTrading()) {
  422. if (interfaces.get(INTERFACE_MAIN)
  423. .getComponent(INTERFACE_MAIN_INFO).getText()
  424. .toLowerCase().contains("waiting"))
  425. return true;
  426. else
  427. return interfaces.get(INTERFACE_MAIN)
  428. .getComponent(INTERFACE_MAIN_ACCEPT)
  429. .doAction("Accept");
  430. }
  431. return false;
  432. }
  433.  
  434. private boolean decline() {
  435. if (isTrading())
  436. return interfaces.get(INTERFACE_MAIN)
  437. .getComponent(INTERFACE_MAIN_DECLINE)
  438. .doAction("Decline");
  439. else if (isConfirming()) {
  440. return interfaces.get(INTERFACE_CONFIRM)
  441. .getComponent(INTERFACE_CONFIRM_DECLINE)
  442. .doAction("Decline");
  443.  
  444. }
  445. return false;
  446. }
  447.  
  448. private boolean offer(final int itemID, final int numberToOffer) {
  449. if (isTrading()) {
  450. final int inventoryCount = inventory.getCount(itemID);
  451. RSItem item = inventory.getItem(itemID);
  452. if (numberToOffer < 0)
  453. throw new IllegalArgumentException("numberToOffer < 0 ("
  454. + numberToOffer + ")");
  455. switch (numberToOffer) {
  456. case 0:
  457. item.doAction("Offer-All");
  458. break;
  459. case 1:
  460. item.doAction("Offer-1");
  461. break;
  462. case 5:
  463. item.doAction("Offer-5");
  464. break;
  465. case 10:
  466. item.doAction("Offer-10");
  467. break;
  468. case 28:
  469. item.doAction("Offer-All");
  470. break;
  471. default:
  472. if (item.doAction("Offer-X")) {
  473. sleep(random(1000, 1300));
  474. keyboard.sendText("" + numberToOffer, true);
  475. }
  476. break;
  477. }
  478. return (inventory.getCount(itemID) < inventoryCount)
  479. || (inventory.getCount() == 0);
  480. }
  481. return false;
  482. }
  483. }
  484.  
  485. public class Paint {
  486. /**
  487. * Gets the point the tile would be on if the minimap was bigger.
  488. *
  489. * @param t
  490. * The tile to convert to a point.
  491. * @author Enfilade
  492. */
  493. private Point tileToMinimap(RSTile t) {
  494. RSTile player = getMyPlayer().getLocation();
  495. Point a = calc.tileToMinimap(player);
  496. Point b = calc.tileToMinimap(new RSTile(player.getX(), player
  497. .getY() + 10));
  498. Point c = calc.tileToMinimap(new RSTile(player.getX() + 10, player
  499. .getY()));
  500.  
  501. double bDistX = (b.x - a.x) / 10.0;
  502. double bDistY = (b.y - a.y) / 10.0;
  503.  
  504. double cDistX = (c.x - a.x) / 10.0;
  505. double cDistY = (c.y - a.y) / 10.0;
  506.  
  507. int xDist = t.getX() - player.getX();
  508. int yDist = t.getY() - player.getY();
  509.  
  510. return new Point(a.x + (int) (cDistX * xDist + bDistX * yDist), a.y
  511. + (int) (bDistY * yDist + cDistY * xDist));
  512. }
  513.  
  514. private void paint(Graphics render) {
  515. if (!game.isLoggedIn())
  516. return;
  517. Graphics2D g = (Graphics2D) render;
  518. g.drawString("Status: " + system.getStatus(), 200, 200);
  519. g.drawString("Crafter: " + gui.getName(), 200, 220);
  520. Polygon p = new Polygon();
  521. for (RSTile t : info.PATH_AREA_TILES) {
  522. Point po = tileToMinimap(t);
  523. p.addPoint(po.x, po.y);
  524. }
  525. g.setColor(Color.BLACK);
  526. g.draw(p);
  527. g.setColor(new Color(0, 255, 0, 100));
  528. g.fill(p);
  529. }
  530. }
  531.  
  532. @Override
  533. public void onRepaint(Graphics render) {
  534. paint.paint(render);
  535. }
  536. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement