Guest User

Untitled

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