Tezlaz

Mining

Aug 6th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.21 KB | None | 0 0
  1. package scripts.tasks;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5.  
  6. import org.tribot.api.DynamicClicking;
  7. import org.tribot.api.General;
  8. import org.tribot.api.Timing;
  9. import org.tribot.api2007.Equipment;
  10. import org.tribot.api2007.Inventory;
  11. import org.tribot.api2007.Objects;
  12. import org.tribot.api2007.Player;
  13. import org.tribot.api2007.Players;
  14. import org.tribot.api2007.types.RSArea;
  15. import org.tribot.api2007.types.RSItem;
  16. import org.tribot.api2007.types.RSObject;
  17. import org.tribot.api2007.types.RSPlayer;
  18. import org.tribot.api2007.types.RSTile;
  19. import org.tribot.api2007.types.RSNPC;
  20.  
  21. import scripts.data.Vars;
  22. import scripts.tools.*;
  23. import scripts.data.*;
  24.  
  25. import org.tribot.api2007.Walking;
  26. import org.tribot.api2007.WebWalking;
  27.  
  28. public class Mining {
  29.  
  30. RSNPC thingo;
  31.  
  32. // If it takes the bot more than TIME_TO_WAIT milliseconds to mine ORES_TO_MINE clay, then it hops.
  33. private static final int TIME_TO_WAIT = 60000;
  34. private static final int ORES_TO_MINE = 6; // This is also the length of the miningTimes arrayList
  35.  
  36. private static RSObject targetRock;
  37. private static RSObject updatedTargetRock; // Used to check if an ore has been mined or not
  38.  
  39. // Array of the duration of time between each ore. This arrayList is used to decide when to hop worlds.
  40. private static ArrayList<Long> miningTimes = new ArrayList<Long>();
  41. private static long clayTime = 0; // Used in timing miningTimes array
  42. private static int amountOfClay = Inventory.getCount(Vars.ORES[0]);; // Amount of clay in your inventory
  43.  
  44. private static boolean north;
  45.  
  46. // returns true if we are in the mining area
  47. public static boolean IsInMineArea() {
  48. return Vars.MINING_VARROCK_AREA.contains(Player.getPosition());
  49. }
  50.  
  51. public static void WalkToMine() {
  52. WebWalking.setUseRun(true);
  53. WebWalking.walkTo(Vars.VARROCK_MINE);
  54. Timing.waitCondition(Conditions.inMine, General.random(40000, 60000));
  55. }
  56.  
  57.  
  58. public static void Mine() {
  59.  
  60. ResetMiningTimes();
  61. north = ChooseSpot();
  62.  
  63. long timer = System.currentTimeMillis(); // to keep track of how long it takes to mine an inventory.
  64.  
  65. while (!Inventory.isFull()) { // keeps on mining until inventory is full
  66.  
  67. if (IsInMineArea()) {
  68. System.out.println(Vars.accountName + " is now mining clay...");
  69. UpdateAmountOfClay();
  70. MineClay();
  71.  
  72. // this is just for purposes of finding time taken to mine an inventory at different mining levels.
  73. //int timerr = (int) (System.currentTimeMillis() - timer);
  74. //FileWriting.WriteFile(SKILLS.MINING.getActualLevel() + " " + timerr);
  75.  
  76. // This doesn't work well because mining level is too low, it always takes over 5 mins to mine.
  77. //if (TotalMiningTimes() > TIME_TO_WAIT)
  78. // ChangeServ(false);
  79.  
  80. } else {
  81.  
  82. System.out.println(Vars.accountName + " walking to mine...");
  83. WalkToMine();
  84.  
  85. // Walking.blindWalkTo(Vars.MINING_VARROCK_AREA);
  86.  
  87. }
  88. }
  89. System.out.println(Vars.accountName + " has a full inv. Walking to fountain...");
  90. }
  91.  
  92. // Mines the rocks
  93. public static void MineClay() {
  94.  
  95. targetRock = FindRock(ChooseSpot());
  96.  
  97. if (targetRock != null) {
  98. if (targetRock.isOnScreen()) {
  99.  
  100. DynamicClicking.clickRSTile(targetRock, "Mine");
  101. General.sleep(600);
  102.  
  103. // does nothing while it waits for it to mine, either until rock has been mined or for 9 seconds.
  104. long t = Timer.Tic();
  105. while (IsMining() && Timer.Toc(t) < 9000) {
  106. General.sleep(140);
  107. }
  108. } else {
  109. // if the object is not on screen lets walk to it
  110. Walking.walkTo(targetRock);
  111. }
  112. }
  113. }
  114.  
  115. // Returns true if you're mining a rock that's still there. Returns false either if you're idle or if the rock
  116. // has been mined by someone else. Basically tells you whether or not you should be waiting to finish mining.
  117. private static boolean IsMining() {
  118. updatedTargetRock = GetUpdatedTargetRock();
  119.  
  120. boolean isOreMined = (updatedTargetRock != null) && !(Logic.Contains(Vars.CLAY_ROCKS, updatedTargetRock.getID()));
  121. boolean isIdle = Player.getAnimation() == -1;
  122.  
  123. return !isOreMined;// && !isIdle;
  124. }
  125.  
  126. // Returns the rock. This means that we'll be able to see if it's changed ID, if its been mined
  127. private static RSObject GetUpdatedTargetRock() {
  128. if (targetRock != null) {
  129.  
  130. RSObject[] rocks = Objects.getAt(targetRock);
  131.  
  132. if (rocks.length > 0) {
  133. return rocks[0];
  134. }
  135. }
  136. return null;
  137. }
  138.  
  139. // Boolean is whether we're looking for north, single ore (true), or south, double ore (false)
  140. // Returns the rock we're going to try and mine
  141. public static RSObject FindRock(boolean singleSpot) {
  142.  
  143. RSObject[] Rocks = Objects.findNearest(20, Vars.CLAY_ROCKS);
  144.  
  145. if (Rocks.length > 0 && Rocks[0] != null) {
  146.  
  147. for (int i = 0; i < Rocks.length; i++) {
  148.  
  149. if ((!singleSpot && Vars.DOUBLE_ORE_AREA.contains(Rocks[i].getPosition()))
  150. || singleSpot && (Vars.SINGLE_ROCK_TILE.getX() == Rocks[i].getPosition().getX()
  151. && Vars.SINGLE_ROCK_TILE.getY() == Rocks[i].getPosition().getY())) {
  152. // If an ore is either in the double ore area if singleSpot = false,
  153. // or is in the single rock tile if singleSpot = true, then return that item.
  154.  
  155. return Rocks[i];
  156. }
  157. }
  158. }
  159. return null;
  160. }
  161.  
  162. // Unused function. Don't think we'll need.
  163. /*public static RSObject[] FindMinedRocks() {
  164. return Objects.findNearest(20, Vars.MINED_ROCKS);
  165. }*/
  166.  
  167. // Works the same as LowestPopulation(), just for a specific area though.
  168. // Returns true if we've chosen to mine at the north (single) spot, false is the south (double) spot
  169. public static boolean ChooseSpot() {
  170. // Gets the density of the 2 rocks
  171. RSPlayer players[] = Players.getAll();
  172. int southDensity = 0;
  173. int northDensity = 0;
  174. RSTile southWest = new RSTile((Vars.WEST_ROCK_TILE.getX() - 1), (Vars.WEST_ROCK_TILE.getY() - 1));
  175. RSTile northEast = new RSTile((Vars.NORTH_ROCK_TILE.getX() + 1), (Vars.NORTH_ROCK_TILE.getY() + 1));
  176. RSArea area = new RSArea(southWest, northEast);
  177.  
  178. for (int p = 0; p < players.length; p++) {
  179. if (area.contains(players[p].getPosition())) {
  180. southDensity++;
  181. }
  182. }
  183.  
  184. southDensity = (int) Math.floor(southDensity / 2);
  185. northDensity = GetDensity(Vars.SINGLE_ROCK_TILE);
  186.  
  187. if (northDensity < southDensity) {
  188. return true;
  189. } else return false;
  190. }
  191.  
  192.  
  193. public static int GetDensity(RSTile tile) {
  194. RSPlayer players[] = Players.getAll();
  195. int density = 0;
  196. RSTile southWest = new RSTile((tile.getX() - 1), (tile.getY() - 1));
  197. RSTile northEast = new RSTile((tile.getX() + 1), (tile.getY() + 1));
  198. RSArea area = new RSArea(southWest, northEast);
  199.  
  200. for (int p = 0; p < players.length; p++) {
  201. if (area.contains(players[p].getPosition())) {
  202. density++;
  203. }
  204. }
  205. return density;
  206. }
  207.  
  208. // A function that takes any number of tiles/rock locations and finds the
  209. // tile with the least number of players near it, in a 3x3 area
  210. // This isn't used in this script, we use ChooseSpot() instead
  211. public static RSTile LowestPopulation(RSTile... rsTiles) {
  212. // This array holds all the different numbers of people
  213. // on each tile
  214. int[] populationArray = new int[rsTiles.length];
  215.  
  216. // This is the main loop which will make sure the
  217. // population is checked for each tile
  218. for (int i = 0; i < rsTiles.length; i++) {
  219.  
  220. // The first 2 variables act like the top left and right of
  221. // a rectangle and make up an area. Code code be shortened to
  222. // 1 variable, but it would look messy.
  223. RSTile northWest = new RSTile((rsTiles[i].getX() - 1), (rsTiles[i].getY() + 1), 0);
  224. RSTile southEast = new RSTile((rsTiles[i].getX() + 1), (rsTiles[i].getY() - 1), 0);
  225. RSArea area = new RSArea(northWest, southEast);
  226.  
  227. // Finds all the players that are in your nearby location
  228. RSPlayer[] players = Players.getAll();
  229. int population = 0;
  230.  
  231. // This checks to see how many players are actually within
  232. // the area/near the specific tile
  233. for (int p = 0; p < players.length; p++) {
  234. if (area.contains(players[p].getPosition())) {
  235. population++;
  236. }
  237. }
  238. populationArray[i] = population;
  239. }
  240.  
  241. /* This part of the function is sort of elaborate and feels
  242. * unneeded. It sorts out the population from lowest to highest.
  243. * Although this can be done with the "Arrays.sort" function,
  244. * I needed to do it manually so that I could sort the
  245. * rsTiles[] array alongside it to make sure that they still
  246. * match up.
  247. *
  248. * This is because the function relies on
  249. * populationArray[0] being the assigned population to
  250. * rsTiles[0].
  251. */
  252. int lowest = 0;
  253. int highest = 0;
  254. RSTile[] buffer = rsTiles.clone();
  255.  
  256. for (int s = 0; s < rsTiles.length; s++) {
  257. if (populationArray[s] > highest) {
  258. highest = populationArray[s];
  259. rsTiles[s] = buffer[buffer.length - 1];
  260. } else if (populationArray[s] < lowest) {
  261. lowest = populationArray[s];
  262. rsTiles[s] = buffer[0];
  263. }
  264. }
  265.  
  266. // This line is for future use. I might add implementation
  267. // to find the nth most populated tile
  268. Arrays.sort(populationArray);
  269.  
  270. // Since we have sorted the array from lowest to highest,
  271. // rsTiles[0] must have the lowest population
  272. return rsTiles[0];
  273. }
  274.  
  275. public static RSItem CurrentPickaxe() {
  276. RSItem[] inventoryTool = Inventory.find(Vars.PICKAXES);
  277. if (inventoryTool != null && inventoryTool.length > 0) {
  278. return inventoryTool[0];
  279. }
  280. if (Equipment.SLOTS.WEAPON.getItem() != null) {
  281. return Equipment.SLOTS.WEAPON.getItem();
  282. }
  283. else {
  284. return null;
  285. }
  286. }
  287.  
  288. // Updates the variable that keeps track of total clay mined, and updates the arrayList that keeps track of time taken
  289. // Between ores mined.
  290. private static void UpdateAmountOfClay() {
  291. int newAmountOfClay = Inventory.getCount(Vars.ORES[0]);
  292.  
  293. // Checking whether we've mined more clay.
  294. if (newAmountOfClay > amountOfClay) {
  295.  
  296. System.out.println("New clay found in inventory.");
  297.  
  298. Vars.clayMined += (newAmountOfClay - amountOfClay); // Incrementing the variable that keeps track of total clay mined
  299.  
  300. miningTimes.add(0, Timer.Toc(clayTime)); // Adds the time since last mine to the arrayList's first slot
  301.  
  302. while (miningTimes.size() > ORES_TO_MINE) // makes sure that the arrayList is the right length
  303. miningTimes.remove(ORES_TO_MINE - 1);
  304.  
  305. clayTime = Timer.Tic(); // Resets the timer so that the next time this function is called, it will add the correct time.
  306. amountOfClay = newAmountOfClay;
  307. }
  308. }
  309.  
  310. // Resets the miningTimes arrayList
  311. private static void ResetMiningTimes() {
  312. miningTimes.clear();
  313. }
  314.  
  315. // Returns the sum of the times stored in the miningTimes arrayList
  316. private static int TotalMiningTimes() {
  317. long sum = 0;
  318. for (int i = 0; i < miningTimes.size(); i++) {
  319. sum += miningTimes.get(i);
  320. }
  321. return (int) (sum);
  322. }
  323.  
  324.  
  325. // Resets a few things and hops servers.
  326. private static void ChangeServ(boolean members) {
  327. WorldHandler.Hop(members);
  328.  
  329. ResetMiningTimes();
  330. north = ChooseSpot();
  331. }
  332. }
Add Comment
Please, Sign In to add comment