diff --git a/src/DropperThread.java b/src/DropperThread.java index a327112..2c49661 100644 --- a/src/DropperThread.java +++ b/src/DropperThread.java @@ -22,11 +22,11 @@ public class DropperThread implements Runnable { // TODO Auto-generated catch block e.printStackTrace(); } - System.out.println("~~~~~~~~~~~~~ dropperThread finished!"); + //System.out.println("~~~~~~~~~~~~~ dropperThread finished!"); } public void start() { - System.out.println("dropperThread started"); + //System.out.println("dropperThread started"); if (dropperThread == null) { dropperThread = new Thread(this, "dropperThread"); dropperThread.start(); diff --git a/src/Inventory.java b/src/Inventory.java index 4f28872..e29596e 100644 --- a/src/Inventory.java +++ b/src/Inventory.java @@ -44,14 +44,26 @@ public class Inventory { updateAllInventorySlots(image); } + public int getNumberOfItemsOfTypeInInventory(String itemType) throws IOException { + int numberOfItemsOfType = 0; + BufferedImage image = robot.createScreenCapture(this.inventoryRectangleToCapture); + for (int row = 0; row < Constants.INVENTORY_NUM_ROWS; row++) { + for (int column = 0; column < Constants.INVENTORY_NUM_COLUMNS; column++) { + inventorySlots[row][column].updateInventorySlot(image); + if (inventorySlots[row][column].getItemNameInInventorySlot(items).equals(itemType)) { + numberOfItemsOfType++; + } + } + } + return numberOfItemsOfType++; + } + public int getFirstIronOreInInventory() throws IOException { BufferedImage image = robot.createScreenCapture(this.inventoryRectangleToCapture); for (int row = 0; row < Constants.INVENTORY_NUM_ROWS; row++) { for (int column = 0; column < Constants.INVENTORY_NUM_COLUMNS; column++) { inventorySlots[row][column].updateInventorySlot(image); if (!inventorySlots[row][column].isInventorySlotEmpty(items)) { - //System.out.println("Not empty!"); - System.out.println("Returning " + (row * 7 + column)); return (row * 7 + column); } } diff --git a/src/IronMiner.java b/src/IronMiner.java index 1ac69f7..a27fa20 100644 --- a/src/IronMiner.java +++ b/src/IronMiner.java @@ -35,6 +35,7 @@ public class IronMiner { CameraCalibrator cameraCalibrator; RandomDetector randomDetector; WorldHopper worldHopper; + MiningSuccessHistory miningSuccessHistory; ObjectDetectionHistory objectDetectionHistory; public IronMiner() throws AWTException, IOException @@ -49,6 +50,7 @@ public class IronMiner { randomDetector = new RandomDetector(); worldHopper = new WorldHopper(); cameraCalibrator = new CameraCalibrator(targetNumberOfDetectedOres); + miningSuccessHistory = new MiningSuccessHistory(); objectDetectionHistory = new ObjectDetectionHistory(targetNumberOfDetectedOres); } @@ -56,9 +58,8 @@ public class IronMiner { long startTime = System.currentTimeMillis(); int count = 0; - while (((System.currentTimeMillis() - startTime) / 1000.0 / 75) < 85) { + while (((System.currentTimeMillis() - startTime) / 1000.0 / 60) < 85) { BufferedImage screenCapture = objectDetector.captureScreenshotGameWindow(); - ArrayList detectedObjects = objectDetector.getObjectsInImage(screenCapture, 0.30); ArrayList ironOres = objectDetector.getIronOres(detectedObjects); @@ -70,6 +71,8 @@ public class IronMiner { cursor.moveAndLeftClickAtCoordinatesWithRandomness(closestIronOre.getCenterForClicking(), 10, 10); int ironOreInInventory = inventory.getFirstIronOreInInventory(); + int numberOfOresInInventoryBefore = inventory.getNumberOfItemsOfTypeInInventory("ironOre"); + int numberOfOresInInventoryAfter = 0; if (ironOreInInventory > -1) { int ironOreInInventoryColumn = ironOreInInventory % 7; @@ -86,16 +89,27 @@ public class IronMiner { trackerThread.waitTillDone(); dropperThread.waitTillDone(); + + Point rightClickLocation = cursor.getCurrentCursorPoint(); cursorTask.leftClickDropOption(cursor, rightClickLocation, 0); + + numberOfOresInInventoryAfter = inventory.getNumberOfItemsOfTypeInInventory("ironOre") + 1; } else { TrackerThread trackerThread = new TrackerThread(screenCapture, closestIronOre, objectDetector); trackerThread.start(); trackerThread.waitTillDone(); + + numberOfOresInInventoryAfter = inventory.getNumberOfItemsOfTypeInInventory("ironOre"); } count++; - //System.out.println(count + ", time: " + ((System.currentTimeMillis() - startTime) / 1000 / 60)); + + boolean miningSuccess = (numberOfOresInInventoryAfter > numberOfOresInInventoryBefore); + //System.out.println("Ores in inventory: " + numberOfOresInInventoryBefore + ". Mining success? " + miningSuccess); + hopWorldsIfMiningSuccessRateIsLow(miningSuccess); + + System.out.println(count + ", time: " + ((System.currentTimeMillis() - startTime) / 1000 / 60)); } humanBehavior.randomlyCheckMiningXP(cursor); randomDetector.dealWithRandoms(screenCapture, cursor); @@ -103,6 +117,13 @@ public class IronMiner { } } + private void hopWorldsIfMiningSuccessRateIsLow(boolean miningSuccess) { + boolean hopWorld = miningSuccessHistory.updateHistory(miningSuccess); + if (hopWorld) { + //System.out.println("Hopping worlds (Theoretically)"); + miningSuccessHistory.resetQueue(); + } + } private void readjustCameraIfObjectsAreNotBeingDetected(int detectedObjectsSize) throws Exception { boolean readjustCamera = objectDetectionHistory.updateHistory(detectedObjectsSize); @@ -110,7 +131,6 @@ public class IronMiner { cameraCalibrator.rotateUntilObjectFound(objectDetector, "ironOre"); objectDetectionHistory.resetQueue(); } - } private void dropInventoryIfFull() throws Exception { diff --git a/src/MiningSuccessHistory.java b/src/MiningSuccessHistory.java new file mode 100644 index 0000000..81aadf2 --- /dev/null +++ b/src/MiningSuccessHistory.java @@ -0,0 +1,46 @@ +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.Queue; + +public class MiningSuccessHistory { + + int queueSize; + int numberOfTruesInQueue; + Queue miningSuccessHistory; + + public MiningSuccessHistory() { + queueSize = 30; + this.numberOfTruesInQueue = queueSize; + miningSuccessHistory = new LinkedList(); + for (int i = 0; i < queueSize; i++) { + miningSuccessHistory.add(true); + } + } + + public boolean updateHistory(boolean success) { + miningSuccessHistory.add(success); + if (success) { + numberOfTruesInQueue++; + } + if (miningSuccessHistory.poll()) { + numberOfTruesInQueue--; + } + return isMiningRateSufficient(); + } + + private boolean isMiningRateSufficient() { + return numberOfTruesInQueue < (queueSize * 0.6); + } + + public void resetQueue() { + this.numberOfTruesInQueue = queueSize; + miningSuccessHistory.clear(); + for (int i = 0; i < queueSize; i++) { + miningSuccessHistory.add(true); + } + } + + public void displayStats() { + System.out.println("Mining success rate: " + (numberOfTruesInQueue * 1.0 / queueSize)); + } +} diff --git a/src/ObjectDetectionHistory.java b/src/ObjectDetectionHistory.java index 8e1bea9..949d04a 100644 --- a/src/ObjectDetectionHistory.java +++ b/src/ObjectDetectionHistory.java @@ -25,7 +25,6 @@ public class ObjectDetectionHistory { } public void updateHistory(boolean success) { - System.out.println("Increasing? " + success); numberOfDetectedObjectsHistory.add(success); if (success) { numberOfTruesInQueue++; diff --git a/src/TrackerThread.java b/src/TrackerThread.java index ad99670..bf4c399 100644 --- a/src/TrackerThread.java +++ b/src/TrackerThread.java @@ -65,7 +65,7 @@ public class TrackerThread implements Runnable { } public void start() { - System.out.println("TrackerThread started"); + //System.out.println("TrackerThread started"); if (trackerThread == null) { trackerThread = new Thread(this, "trackerThread"); trackerThread.start(); diff --git a/target/classes/DropperThread.class b/target/classes/DropperThread.class index 557b0bb..36be55c 100644 Binary files a/target/classes/DropperThread.class and b/target/classes/DropperThread.class differ diff --git a/target/classes/Inventory.class b/target/classes/Inventory.class index a324059..8db850a 100644 Binary files a/target/classes/Inventory.class and b/target/classes/Inventory.class differ diff --git a/target/classes/IronMiner.class b/target/classes/IronMiner.class index 876f105..8c43e8e 100644 Binary files a/target/classes/IronMiner.class and b/target/classes/IronMiner.class differ diff --git a/target/classes/MiningSuccessHistory.class b/target/classes/MiningSuccessHistory.class new file mode 100644 index 0000000..3199de9 Binary files /dev/null and b/target/classes/MiningSuccessHistory.class differ diff --git a/target/classes/ObjectDetectionHistory.class b/target/classes/ObjectDetectionHistory.class index 7ce62bb..7c75e34 100644 Binary files a/target/classes/ObjectDetectionHistory.class and b/target/classes/ObjectDetectionHistory.class differ diff --git a/target/classes/TrackerThread.class b/target/classes/TrackerThread.class index 70c1583..3a6e9c6 100644 Binary files a/target/classes/TrackerThread.class and b/target/classes/TrackerThread.class differ