Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.46 KB | None | 0 0
  1. package scripts.api.world;
  2.  
  3. import org.tribot.api.General;
  4. import org.tribot.api.Timing;
  5. import org.tribot.api.input.Mouse;
  6. import org.tribot.api.types.generic.Condition;
  7. import org.tribot.api2007.*;
  8. import org.tribot.api2007.Login.STATE;
  9.  
  10. import java.awt.*;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13.  
  14.  
  15. public class WorldHop {
  16.  
  17. public static List<World> allWorlds = null;
  18. private static int[] ftpWorlds = null;
  19. private static int[] p2pWorlds = null;
  20. public static int[] worldsNotSupported = null;
  21. public static int[] worldsThatDontExist = null;
  22.  
  23. public final static Color WORLD_SWITCH_COLOUR = new Color(189, 152, 57);
  24. public final Color WORLD_GREEN_ARROW_COLOUR = new Color(47, 130, 43);
  25. public final static Color WORLD_RED_ARROW_COLOUR = new Color(172, 12, 4);
  26.  
  27. public final static int WORLD_PIXEL_SIZE_X = 78;
  28. public final static int WORLD_PIXEL_SIZE_Y = 17;
  29. public static int worldPixelStartX = 208;
  30. public static int worldPixelStartY = 61;
  31. public static int worldsPerColumn = -1;
  32. public static boolean useF2P = false;
  33.  
  34. private static int[] toIntArray(ArrayList<Integer> list) {
  35. int[] ret = new int[list.size()];
  36. int i = 0;
  37. for (Integer e : list)
  38. ret[i++] = e.intValue();
  39. return ret;
  40. }
  41.  
  42. static {
  43. /**/
  44. }
  45.  
  46. public static void load() {
  47. while(allWorlds == null) {
  48. System.out.println("Loading runescape world list");
  49.  
  50. if((allWorlds = World.loadWorlds()) == null)
  51. General.sleep(50, 100);
  52. }
  53.  
  54. ftpWorlds = getF2pWorlds();
  55. p2pWorlds = getP2pWorlds();
  56. worldsThatDontExist = getWorldsThatDontExist(false);
  57. worldsNotSupported = getWorldsNotSupported();
  58. setWorldColumnData();
  59. }
  60.  
  61.  
  62. public static void setWorldColumnData() {
  63.  
  64. if(allWorlds.size() > 85) { // pattern has shown if there are more than 68 worlds then they fit 18 worlds per column
  65. worldPixelStartX = 155;
  66. worldPixelStartY = 51;
  67. worldsPerColumn = 18;
  68. return;
  69. }
  70. worldPixelStartX = 155;
  71. worldPixelStartY = 61;
  72. worldsPerColumn = 17;
  73. }
  74.  
  75.  
  76.  
  77. private static int worldhopAttempts = 0;
  78.  
  79. private static int failedworldhopAttempts = 0;
  80.  
  81. public static int getRandomF2pWorld() {
  82. return ftpWorlds[General.random(0, ftpWorlds.length - 1)];
  83. }
  84.  
  85. public static int getRandomMembersWorld() {
  86. return p2pWorlds[General.random(0, p2pWorlds.length -1)];
  87. }
  88.  
  89. public static boolean isAtWorldHopScreen() {
  90. return Screen.getColorAt(11,0).equals(WORLD_SWITCH_COLOUR);
  91.  
  92. }
  93.  
  94. public static boolean hasMisconfiguredWorldSettings() {
  95. return Screen.getColorAt(301, 9).equals(WORLD_RED_ARROW_COLOUR);
  96. }
  97.  
  98. public final static int[] getWorldsNotSupported() {
  99. if (allWorlds == null) return new int[0];
  100. ArrayList<Integer> worldsNotSupported = new ArrayList<>();
  101. for(World world : allWorlds) {
  102. if(!useF2P && !world.isMember() || world.isDeadman() || world.isPvp() || world.getActivity().contains("skill total") || world.getPlayerCount() < 5 || world.isBlueWorld()) { // 33554433 is blue world?
  103. worldsNotSupported.add(world.getId() - 300);
  104. }
  105. }
  106. for(int i : worldsThatDontExist) { // combine both arrays; worlds that are not supported & worlds that dont exist
  107. worldsNotSupported.add(i);
  108. }
  109. int[] worlds = toIntArray(worldsNotSupported);
  110. return worlds;
  111. }
  112.  
  113. public final static int[] getWorldsThatDontExist(boolean use300Format) {
  114. ArrayList<Integer> worldsThatDontExist = new ArrayList<Integer>();
  115. for(int i = 0; i < allWorlds.size(); i++) {
  116. if(i == allWorlds.size() - 1)
  117. break;
  118.  
  119. int worldOne = allWorlds.get(i).getId();
  120. int worldTwo = allWorlds.get(i+1).getId();
  121. int difference = worldTwo - worldOne;
  122.  
  123. if(difference <= 1)
  124. continue;
  125.  
  126. for(int s = worldOne + 1; s < worldTwo; s++) {
  127. worldsThatDontExist.add(s - (!use300Format ? 300 : 0));
  128. }
  129. }
  130. return toIntArray(worldsThatDontExist);
  131. }
  132.  
  133. public final static int[] getF2pWorlds() {
  134. ArrayList<Integer> f2pWorlds = new ArrayList<Integer>();
  135. for(World world : allWorlds) {
  136. if(!world.isMember() && !world.getActivity().contains("skill total"))
  137. f2pWorlds.add(world.getId() - 300);
  138. }
  139. int[] worlds = toIntArray(f2pWorlds);
  140.  
  141. return worlds;
  142. }
  143.  
  144. public final static int[] getP2pWorlds() {
  145. ArrayList<Integer> p2pWorlds = new ArrayList<Integer>();
  146. for(World world : allWorlds) {
  147. if(world.isMember() && !world.isPvp() && !world.isDeadman() && !world.isBlueWorld() && !world.getActivity().contains("skill total"))
  148. p2pWorlds.add(world.getId() - 300);
  149. }
  150.  
  151. return toIntArray(p2pWorlds);
  152. }
  153.  
  154. public static void setUseF2P(boolean useF2p) {
  155. useF2P = useF2p;
  156. worldsNotSupported = getWorldsNotSupported();
  157. }
  158.  
  159. public static boolean isInMemberWorld() {
  160. int world = getWorld();
  161. for(int i : ftpWorlds) {
  162. if(i == world)
  163. return false;
  164. }
  165. return true;
  166. }
  167.  
  168. public static int getWorld() {
  169. return Game.getCurrentWorld() - 300;
  170. }
  171.  
  172. public static void drawWorlds(Graphics2D g2) {
  173. if (allWorlds == null) return;
  174.  
  175. for (World world : allWorlds) {
  176. Rectangle rect = getWorldClickArea(world.getId());
  177. g2.draw(rect);
  178. }
  179. }
  180.  
  181.  
  182. public static void closeWorldHopScreen() {
  183. Mouse.click(734, 11, 1);
  184. }
  185.  
  186. public static boolean switchWorld(final int world) {
  187.  
  188. if (allWorlds == null)
  189. load();
  190.  
  191. General.println("hopping to world " + world);
  192. long timeout = Timing.currentTimeMillis() + 60000;
  193.  
  194. while (true && timeout > Timing.currentTimeMillis()) {
  195.  
  196. if(failedworldhopAttempts > 2) {
  197. if(isAtWorldHopScreen()) {
  198. General.println("detected worlds are most likely offline");
  199. closeWorldHopScreen();
  200. General.sleep(500,1000);
  201. failedworldhopAttempts = 0;
  202. }
  203. }
  204.  
  205. if(worldhopAttempts >= 4) {
  206. worldhopAttempts = 0;
  207. failedworldhopAttempts++;
  208. return false;
  209. }
  210.  
  211. if(Game.getGameState() != 10)
  212. Interfaces.closeAll();
  213.  
  214. if(Combat.isUnderAttack()) {
  215. General.println("under attack exiting world hop loop");
  216. worldhopAttempts = 0;
  217. return false;
  218. }
  219.  
  220. if(!isAtWorldHopScreen() && getWorld() == world % 300) {
  221. //General.println("it returned true, currenet world = designated world");
  222. worldhopAttempts = 0;
  223. return true;
  224. }
  225.  
  226. if (Login.getLoginState() == STATE.INGAME) {
  227. if(Login.logout()) {
  228. Timing.waitCondition(new Condition() {
  229. @Override
  230. public boolean active() {
  231. General.sleep(50,100);
  232. return Game.getGameState() != 30;
  233. }
  234. }, General.random(800,1000));
  235. }
  236. } else if(Login.getLoginState().equals(STATE.WELCOMESCREEN)) {
  237. Login.login();
  238. } else if (isAtWorldHopScreen()) {
  239. if (hasMisconfiguredWorldSettings()) {
  240. Mouse.click(301, 9, 1);
  241. Timing.waitCondition(new Condition() {
  242. @Override
  243. public boolean active() {
  244. return !hasMisconfiguredWorldSettings();
  245. }
  246. }, 2000);
  247.  
  248. } else {
  249.  
  250. if(worldsPerColumn == -1)
  251. setWorldColumnData();
  252.  
  253. Rectangle clickArea = getWorldClickArea(world);
  254. Point location = new Point((int)clickArea.getCenterX(), (int)clickArea.getCenterY());
  255. Mouse.hop(location);
  256. General.sleep(200);
  257. Mouse.sendPress(location, 1);
  258. Mouse.sendRelease(location, 1);
  259. Mouse.sendClickEvent(location, 1);
  260.  
  261. if(Timing.waitCondition(new Condition() {
  262. @Override
  263. public boolean active() {
  264. return !isAtWorldHopScreen() && getWorld() == world;
  265. }
  266. }, 1000)) {
  267. //General.println("it returned true, we not at world hop screen and currenet world = designated world");
  268. return true;
  269. } else {
  270. worldhopAttempts++;
  271. }
  272. }
  273. } else {
  274. if(getWorld() != world) {
  275. Rectangle rec = new Rectangle(10, 486, 100, 493);
  276. Point location = rec.getLocation();
  277. Mouse.hop(location);
  278. Mouse.sendPress(location, 1);
  279. Mouse.sendRelease(location, 1);
  280. Mouse.sendClickEvent(location, 1);
  281. Timing.waitCondition(new Condition() {
  282. @Override
  283. public boolean active() {
  284. return isAtWorldHopScreen();
  285. }
  286. }, 5000);
  287. }
  288. }
  289. }
  290. worldhopAttempts = 0;
  291. failedworldhopAttempts++;
  292. return !isAtWorldHopScreen() && getWorld() == world;
  293. }
  294.  
  295. public static boolean isInP2P() {
  296. for (int world : WorldHop.getP2pWorlds()) {
  297. if (world == WorldHop.getWorld() || world - 300 == WorldHop.getWorld() || WorldHop.getWorld() - 300 == world) {
  298. return true;
  299. }
  300. }
  301.  
  302. return false;
  303. }
  304.  
  305.  
  306. public static boolean isWorldSupported(int world) {
  307. for (int i=0; i < worldsNotSupported.length; i++) {
  308. if (worldsNotSupported[i] == world) {
  309. return false;
  310. }
  311. }
  312. return true;
  313. }
  314.  
  315. public static int getRandomWorld() {
  316. int randomWorld;
  317. while (!isWorldSupported(randomWorld = (allWorlds.get(General.random(0, allWorlds.size() - 1))).getId() - 300));
  318. return randomWorld;
  319. }
  320.  
  321. public static Rectangle getWorldClickArea(int world) {
  322. return getWorldClickArea(world, worldsPerColumn, true);
  323. }
  324.  
  325. public static Rectangle getWorldClickArea(int world, int worldPerColumn, boolean checkExist) {
  326. if(checkExist && !isWorldSupported(world)) {
  327. General.println("World " + world + " is not supported");
  328. return null;
  329. }
  330.  
  331.  
  332. int diff = 0;
  333. world -= 300;
  334. for(int i : worldsThatDontExist) {
  335. if(world > i) {
  336. diff++;
  337. }
  338. }
  339.  
  340. int x = (world - diff) / worldPerColumn;
  341. int y = (world - diff) % worldPerColumn;
  342. if (x == -1 || y == -1) {
  343. return null;
  344. } else {
  345. if(y == 0) {
  346. y = worldPerColumn;
  347. x -= 1;
  348. }
  349. int xR = worldPixelStartX + (x * 93);
  350. int yR = worldPixelStartY + ((y - 1) * 24);
  351. return new Rectangle(xR, yR, WORLD_PIXEL_SIZE_X, WORLD_PIXEL_SIZE_Y);
  352. }
  353. }
  354.  
  355. }
  356.  
  357.  
  358. package scripts.api.world;
  359.  
  360. import org.tribot.api.General;
  361.  
  362. import java.io.DataInputStream;
  363. import java.io.IOException;
  364. import java.net.URL;
  365. import java.net.URLConnection;
  366. import java.util.ArrayList;
  367. import java.util.Collections;
  368. import java.util.Comparator;
  369. import java.util.List;
  370.  
  371. public class World {
  372. private final int id;
  373. private final boolean member;
  374. private final boolean pvp;
  375. private final boolean highRisk;
  376. private final boolean deadman;
  377. private final boolean bountyHunter;
  378. private final String host;
  379. private final String activity;
  380. private final int serverLoc;
  381. private final int playerCount;
  382. private final int flag;
  383. private final boolean blueWorld;
  384.  
  385. public World(int id, int flag, boolean member, boolean pvp, boolean highRisk, String host, String activity, int serverLoc, int playerCount) {
  386. this.id = id;
  387. this.flag = flag;
  388. this.member = member;
  389. this.pvp = pvp;
  390. this.highRisk = highRisk;
  391. this.host = host;
  392. this.activity = activity;
  393. this.serverLoc = serverLoc;
  394. this.playerCount = playerCount;
  395. this.deadman = activity.toLowerCase().contains("deadman");
  396. this.bountyHunter = activity.toLowerCase().contains("bounty");
  397. this.blueWorld = (flag == 33554433);
  398. }
  399.  
  400.  
  401. public boolean isBlueWorld() {
  402. return blueWorld;
  403. }
  404.  
  405. public int getId() {
  406. return id;
  407. }
  408.  
  409. public boolean isBountyhunter() {
  410. return bountyHunter;
  411. }
  412.  
  413. public int getFlag() {
  414. return flag;
  415. }
  416.  
  417. public boolean isMember() {
  418. return member;
  419. }
  420.  
  421. public boolean isPvp() {
  422. return pvp;
  423. }
  424.  
  425. public boolean isHighRisk() {
  426. return highRisk;
  427. }
  428.  
  429. public boolean isDeadman() {
  430. return deadman;
  431. }
  432.  
  433. public String getHost() {
  434. return host;
  435. }
  436.  
  437. public String getActivity() {
  438. return activity;
  439. }
  440.  
  441. public int getServerLoc() {
  442. return serverLoc;
  443. }
  444.  
  445. public int getPlayerCount() {
  446. return playerCount;
  447. }
  448.  
  449. public static List<World> loadWorlds() {
  450. List<World> worldList = new ArrayList<>();
  451. try {
  452.  
  453. URLConnection conn = new URL("http://oldschool.runescape.com/slr").openConnection();
  454. try (DataInputStream dis = new DataInputStream(conn.getInputStream())) {
  455. int size = dis.readInt() & 0xFF;
  456. int worldCount = dis.readShort();
  457. for (int i = 0; i < worldCount; i++) {
  458. int world = dis.readShort() & 0xFFFF;
  459. int flag = dis.readInt();
  460. boolean member = (flag & 0x1) != 0;
  461. boolean pvp = (flag & 0x4) != 0;
  462. boolean highRisk = (flag & 0x400) != 0;
  463.  
  464. String host = null, activity = null;
  465. StringBuilder sb = new StringBuilder();
  466. byte b;
  467. while (true) {
  468. b = dis.readByte();
  469. if (b == 0) {
  470. if (host == null) {
  471. host = sb.toString();
  472. sb = new StringBuilder();
  473. } else {
  474. activity = sb.toString();
  475. break;
  476. }
  477. } else {
  478. sb.append((char) b);
  479. }
  480. }
  481.  
  482. int serverLoc = dis.readByte() & 0xFF;
  483. int playerCount = dis.readShort();
  484. worldList.add(new World(world, flag, member, pvp, highRisk, host, activity, serverLoc, playerCount));
  485. }
  486. } catch (IOException e) {
  487. // TODO Auto-generated catch block
  488. e.printStackTrace();
  489. General.println("Failed to load runescape worlds");
  490. return null;
  491. }
  492. } catch (Exception e) {
  493. // TODO Auto-generated catch block
  494. e.printStackTrace();
  495. General.println("Failed to load runescape worlds");
  496. return null;
  497. }
  498. Collections.sort(worldList, new Comparator<World>() {
  499.  
  500. @Override
  501. public int compare(World o1, World o2) {
  502. // TODO Auto-generated method stub
  503. return o1.getId() - o2.getId();
  504. }
  505. });
  506. return Collections.unmodifiableList(worldList);
  507. }
  508. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement