Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package scripts.tasks;
- import java.util.ArrayList;
- import java.util.Arrays;
- import org.tribot.api.DynamicClicking;
- import org.tribot.api.General;
- import org.tribot.api.Timing;
- import org.tribot.api2007.Equipment;
- import org.tribot.api2007.Inventory;
- import org.tribot.api2007.Objects;
- import org.tribot.api2007.Player;
- import org.tribot.api2007.Players;
- import org.tribot.api2007.types.RSArea;
- import org.tribot.api2007.types.RSItem;
- import org.tribot.api2007.types.RSObject;
- import org.tribot.api2007.types.RSPlayer;
- import org.tribot.api2007.types.RSTile;
- import org.tribot.api2007.types.RSNPC;
- import scripts.data.Vars;
- import scripts.tools.*;
- import scripts.data.*;
- import org.tribot.api2007.Walking;
- import org.tribot.api2007.WebWalking;
- public class Mining {
- RSNPC thingo;
- // If it takes the bot more than TIME_TO_WAIT milliseconds to mine ORES_TO_MINE clay, then it hops.
- private static final int TIME_TO_WAIT = 60000;
- private static final int ORES_TO_MINE = 6; // This is also the length of the miningTimes arrayList
- private static RSObject targetRock;
- private static RSObject updatedTargetRock; // Used to check if an ore has been mined or not
- // Array of the duration of time between each ore. This arrayList is used to decide when to hop worlds.
- private static ArrayList<Long> miningTimes = new ArrayList<Long>();
- private static long clayTime = 0; // Used in timing miningTimes array
- private static int amountOfClay = Inventory.getCount(Vars.ORES[0]);; // Amount of clay in your inventory
- private static boolean north;
- // returns true if we are in the mining area
- public static boolean IsInMineArea() {
- return Vars.MINING_VARROCK_AREA.contains(Player.getPosition());
- }
- public static void WalkToMine() {
- WebWalking.setUseRun(true);
- WebWalking.walkTo(Vars.VARROCK_MINE);
- Timing.waitCondition(Conditions.inMine, General.random(40000, 60000));
- }
- public static void Mine() {
- ResetMiningTimes();
- north = ChooseSpot();
- long timer = System.currentTimeMillis(); // to keep track of how long it takes to mine an inventory.
- while (!Inventory.isFull()) { // keeps on mining until inventory is full
- if (IsInMineArea()) {
- System.out.println(Vars.accountName + " is now mining clay...");
- UpdateAmountOfClay();
- MineClay();
- // this is just for purposes of finding time taken to mine an inventory at different mining levels.
- //int timerr = (int) (System.currentTimeMillis() - timer);
- //FileWriting.WriteFile(SKILLS.MINING.getActualLevel() + " " + timerr);
- // This doesn't work well because mining level is too low, it always takes over 5 mins to mine.
- //if (TotalMiningTimes() > TIME_TO_WAIT)
- // ChangeServ(false);
- } else {
- System.out.println(Vars.accountName + " walking to mine...");
- WalkToMine();
- // Walking.blindWalkTo(Vars.MINING_VARROCK_AREA);
- }
- }
- System.out.println(Vars.accountName + " has a full inv. Walking to fountain...");
- }
- // Mines the rocks
- public static void MineClay() {
- targetRock = FindRock(ChooseSpot());
- if (targetRock != null) {
- if (targetRock.isOnScreen()) {
- DynamicClicking.clickRSTile(targetRock, "Mine");
- General.sleep(600);
- // does nothing while it waits for it to mine, either until rock has been mined or for 9 seconds.
- long t = Timer.Tic();
- while (IsMining() && Timer.Toc(t) < 9000) {
- General.sleep(140);
- }
- } else {
- // if the object is not on screen lets walk to it
- Walking.walkTo(targetRock);
- }
- }
- }
- // Returns true if you're mining a rock that's still there. Returns false either if you're idle or if the rock
- // has been mined by someone else. Basically tells you whether or not you should be waiting to finish mining.
- private static boolean IsMining() {
- updatedTargetRock = GetUpdatedTargetRock();
- boolean isOreMined = (updatedTargetRock != null) && !(Logic.Contains(Vars.CLAY_ROCKS, updatedTargetRock.getID()));
- boolean isIdle = Player.getAnimation() == -1;
- return !isOreMined;// && !isIdle;
- }
- // Returns the rock. This means that we'll be able to see if it's changed ID, if its been mined
- private static RSObject GetUpdatedTargetRock() {
- if (targetRock != null) {
- RSObject[] rocks = Objects.getAt(targetRock);
- if (rocks.length > 0) {
- return rocks[0];
- }
- }
- return null;
- }
- // Boolean is whether we're looking for north, single ore (true), or south, double ore (false)
- // Returns the rock we're going to try and mine
- public static RSObject FindRock(boolean singleSpot) {
- RSObject[] Rocks = Objects.findNearest(20, Vars.CLAY_ROCKS);
- if (Rocks.length > 0 && Rocks[0] != null) {
- for (int i = 0; i < Rocks.length; i++) {
- if ((!singleSpot && Vars.DOUBLE_ORE_AREA.contains(Rocks[i].getPosition()))
- || singleSpot && (Vars.SINGLE_ROCK_TILE.getX() == Rocks[i].getPosition().getX()
- && Vars.SINGLE_ROCK_TILE.getY() == Rocks[i].getPosition().getY())) {
- // If an ore is either in the double ore area if singleSpot = false,
- // or is in the single rock tile if singleSpot = true, then return that item.
- return Rocks[i];
- }
- }
- }
- return null;
- }
- // Unused function. Don't think we'll need.
- /*public static RSObject[] FindMinedRocks() {
- return Objects.findNearest(20, Vars.MINED_ROCKS);
- }*/
- // Works the same as LowestPopulation(), just for a specific area though.
- // Returns true if we've chosen to mine at the north (single) spot, false is the south (double) spot
- public static boolean ChooseSpot() {
- // Gets the density of the 2 rocks
- RSPlayer players[] = Players.getAll();
- int southDensity = 0;
- int northDensity = 0;
- RSTile southWest = new RSTile((Vars.WEST_ROCK_TILE.getX() - 1), (Vars.WEST_ROCK_TILE.getY() - 1));
- RSTile northEast = new RSTile((Vars.NORTH_ROCK_TILE.getX() + 1), (Vars.NORTH_ROCK_TILE.getY() + 1));
- RSArea area = new RSArea(southWest, northEast);
- for (int p = 0; p < players.length; p++) {
- if (area.contains(players[p].getPosition())) {
- southDensity++;
- }
- }
- southDensity = (int) Math.floor(southDensity / 2);
- northDensity = GetDensity(Vars.SINGLE_ROCK_TILE);
- if (northDensity < southDensity) {
- return true;
- } else return false;
- }
- public static int GetDensity(RSTile tile) {
- RSPlayer players[] = Players.getAll();
- int density = 0;
- RSTile southWest = new RSTile((tile.getX() - 1), (tile.getY() - 1));
- RSTile northEast = new RSTile((tile.getX() + 1), (tile.getY() + 1));
- RSArea area = new RSArea(southWest, northEast);
- for (int p = 0; p < players.length; p++) {
- if (area.contains(players[p].getPosition())) {
- density++;
- }
- }
- return density;
- }
- // A function that takes any number of tiles/rock locations and finds the
- // tile with the least number of players near it, in a 3x3 area
- // This isn't used in this script, we use ChooseSpot() instead
- public static RSTile LowestPopulation(RSTile... rsTiles) {
- // This array holds all the different numbers of people
- // on each tile
- int[] populationArray = new int[rsTiles.length];
- // This is the main loop which will make sure the
- // population is checked for each tile
- for (int i = 0; i < rsTiles.length; i++) {
- // The first 2 variables act like the top left and right of
- // a rectangle and make up an area. Code code be shortened to
- // 1 variable, but it would look messy.
- RSTile northWest = new RSTile((rsTiles[i].getX() - 1), (rsTiles[i].getY() + 1), 0);
- RSTile southEast = new RSTile((rsTiles[i].getX() + 1), (rsTiles[i].getY() - 1), 0);
- RSArea area = new RSArea(northWest, southEast);
- // Finds all the players that are in your nearby location
- RSPlayer[] players = Players.getAll();
- int population = 0;
- // This checks to see how many players are actually within
- // the area/near the specific tile
- for (int p = 0; p < players.length; p++) {
- if (area.contains(players[p].getPosition())) {
- population++;
- }
- }
- populationArray[i] = population;
- }
- /* This part of the function is sort of elaborate and feels
- * unneeded. It sorts out the population from lowest to highest.
- * Although this can be done with the "Arrays.sort" function,
- * I needed to do it manually so that I could sort the
- * rsTiles[] array alongside it to make sure that they still
- * match up.
- *
- * This is because the function relies on
- * populationArray[0] being the assigned population to
- * rsTiles[0].
- */
- int lowest = 0;
- int highest = 0;
- RSTile[] buffer = rsTiles.clone();
- for (int s = 0; s < rsTiles.length; s++) {
- if (populationArray[s] > highest) {
- highest = populationArray[s];
- rsTiles[s] = buffer[buffer.length - 1];
- } else if (populationArray[s] < lowest) {
- lowest = populationArray[s];
- rsTiles[s] = buffer[0];
- }
- }
- // This line is for future use. I might add implementation
- // to find the nth most populated tile
- Arrays.sort(populationArray);
- // Since we have sorted the array from lowest to highest,
- // rsTiles[0] must have the lowest population
- return rsTiles[0];
- }
- public static RSItem CurrentPickaxe() {
- RSItem[] inventoryTool = Inventory.find(Vars.PICKAXES);
- if (inventoryTool != null && inventoryTool.length > 0) {
- return inventoryTool[0];
- }
- if (Equipment.SLOTS.WEAPON.getItem() != null) {
- return Equipment.SLOTS.WEAPON.getItem();
- }
- else {
- return null;
- }
- }
- // Updates the variable that keeps track of total clay mined, and updates the arrayList that keeps track of time taken
- // Between ores mined.
- private static void UpdateAmountOfClay() {
- int newAmountOfClay = Inventory.getCount(Vars.ORES[0]);
- // Checking whether we've mined more clay.
- if (newAmountOfClay > amountOfClay) {
- System.out.println("New clay found in inventory.");
- Vars.clayMined += (newAmountOfClay - amountOfClay); // Incrementing the variable that keeps track of total clay mined
- miningTimes.add(0, Timer.Toc(clayTime)); // Adds the time since last mine to the arrayList's first slot
- while (miningTimes.size() > ORES_TO_MINE) // makes sure that the arrayList is the right length
- miningTimes.remove(ORES_TO_MINE - 1);
- clayTime = Timer.Tic(); // Resets the timer so that the next time this function is called, it will add the correct time.
- amountOfClay = newAmountOfClay;
- }
- }
- // Resets the miningTimes arrayList
- private static void ResetMiningTimes() {
- miningTimes.clear();
- }
- // Returns the sum of the times stored in the miningTimes arrayList
- private static int TotalMiningTimes() {
- long sum = 0;
- for (int i = 0; i < miningTimes.size(); i++) {
- sum += miningTimes.get(i);
- }
- return (int) (sum);
- }
- // Resets a few things and hops servers.
- private static void ChangeServ(boolean members) {
- WorldHandler.Hop(members);
- ResetMiningTimes();
- north = ChooseSpot();
- }
- }
Add Comment
Please, Sign In to add comment