diff --git a/src/Cursor.java b/src/Cursor.java index ee4b30b..f1ff2d6 100644 --- a/src/Cursor.java +++ b/src/Cursor.java @@ -68,7 +68,6 @@ public class Cursor { this.cursorPathsByDistance.get(cursorPath.getCursorPathDistance()).add(cursorPath); } - // TODO: make sure these are reasonable private int getRandomClickLength() { return Randomizer.nextGaussianWithinRange(MINIMUM_CLICK_LENGTH, MAXIMUM_CLICK_LENGTH); } @@ -76,7 +75,6 @@ public class Cursor { private int getRandomClickReleaseLength() { return Randomizer.nextGaussianWithinRange(MINIMUM_CLICK_LENGTH + 5, MAXIMUM_CLICK_LENGTH + 10); } - // END public void leftClickCursor() throws InterruptedException { Thread.sleep(30, 55); @@ -240,12 +238,10 @@ public class Cursor { } private Point randomizePoint(Point goalPoint, int xTolerance, int yTolerance) { - Randomizer randomizer = new Randomizer(); return new Point(goalPoint.x + Randomizer.nextGaussianWithinRange(-xTolerance, xTolerance), goalPoint.y + Randomizer.nextGaussianWithinRange(-yTolerance, yTolerance)); } private Point randomizePoint(Point goalPoint, int xToleranceLeft, int xToleranceRight, int yTolerance) { - Randomizer randomizer = new Randomizer(); return new Point(goalPoint.x + Randomizer.nextGaussianWithinRange(-xToleranceLeft, xToleranceRight), goalPoint.y + Randomizer.nextGaussianWithinRange(-yTolerance, yTolerance)); } diff --git a/src/CursorTask.java b/src/CursorTask.java index f5e1b99..6bc68f9 100644 --- a/src/CursorTask.java +++ b/src/CursorTask.java @@ -38,7 +38,6 @@ public class CursorTask { } } - public Point dropItem(Cursor cursor, Inventory inventory, int row, int column) throws Exception { Point coordinatesToRightClick = inventory.getClickCoordinatesForInventorySlot(row, column); Point clickedCoordinates = rightClickItemSlotWithRandomness(cursor, coordinatesToRightClick); diff --git a/src/DetectedObject.java b/src/DetectedObject.java index 6797d0f..c22b822 100644 --- a/src/DetectedObject.java +++ b/src/DetectedObject.java @@ -17,7 +17,6 @@ public class DetectedObject { this.detectionClass = initializeLabel(detectionClass); } - // TODO: migrate this all to a Rect2d data type private Rect2d initializeBoundingBox(float[] detectionBox) { int offset_x = (int) (detectionBox[1] * Constants.GAME_WINDOW_WIDTH); int offset_y = (int) (detectionBox[0] * Constants.GAME_WINDOW_HEIGHT); diff --git a/src/ImageCapturer.java b/src/ImageCapturer.java new file mode 100644 index 0000000..b6206dd --- /dev/null +++ b/src/ImageCapturer.java @@ -0,0 +1,17 @@ +import java.awt.AWTException; +import java.awt.Rectangle; +import java.awt.Robot; +import java.awt.image.BufferedImage; + +public class ImageCapturer { + + public static BufferedImage captureScreenshotGameWindow() throws AWTException { + Rectangle rectangle = new Rectangle(Constants.GAME_WINDOW_OFFSET_X, Constants.GAME_WINDOW_OFFSET_Y, Constants.GAME_WINDOW_WIDTH, Constants.GAME_WINDOW_HEIGHT); + return new Robot().createScreenCapture(rectangle); + } + + public static BufferedImage captureScreenRectangle(Rectangle rectangle) throws AWTException { + return new Robot().createScreenCapture(rectangle); + } + +} diff --git a/src/ImageCollector.java b/src/ImageCollector.java index 36dc467..921da01 100644 --- a/src/ImageCollector.java +++ b/src/ImageCollector.java @@ -99,11 +99,12 @@ public class ImageCollector { inventory.updateAndWriteAllInventoryImages(); } - public static void main(String[] args) throws Exception + /*public static void main(String[] args) throws Exception { ImageCollector imageCollector = new ImageCollector("/home/dpapp/Desktop/RunescapeAI/Images/"); imageCollector.collectImages("chatDialogue"); //imageCollector.generateInventoryImages(); //imageCollector.captureAndSaveFullWindow(); - } + }*/ + } diff --git a/src/Inventory.java b/src/Inventory.java index e29596e..cc6a05f 100644 --- a/src/Inventory.java +++ b/src/Inventory.java @@ -39,16 +39,17 @@ public class Inventory { items = new InventoryItems(Paths.INVENTORY_ITEMS_DIRECTORY_PATH); } - public void update() throws IOException { + /*public void update() throws IOException { BufferedImage image = robot.createScreenCapture(this.inventoryRectangleToCapture); 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++; @@ -58,6 +59,19 @@ public class Inventory { return numberOfItemsOfType++; } + public void dropAllItemsOfType(String itemType, CursorTask cursorTask, Cursor cursor) throws Exception { + 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)) { + cursorTask.dropItem(cursor, this, row, column); + } + } + } + } + public int getFirstIronOreInInventory() throws IOException { BufferedImage image = robot.createScreenCapture(this.inventoryRectangleToCapture); for (int row = 0; row < Constants.INVENTORY_NUM_ROWS; row++) { @@ -126,6 +140,8 @@ public class Inventory { return true; } + + public Point getClickCoordinatesForInventorySlot(int row, int column) { Point centerOfInventorySlot = inventorySlots[row][column].getClickablePointWithinItemSlot(); int x = Constants.INVENTORY_WINDOW_OFFSET_X + row * Constants.INVENTORY_SLOT_WIDTH + centerOfInventorySlot.x; diff --git a/src/IronMiner.java b/src/IronMiner.java index 863a0ff..0356772 100644 --- a/src/IronMiner.java +++ b/src/IronMiner.java @@ -33,7 +33,6 @@ public class IronMiner { Robot robot; HumanBehavior humanBehavior; CameraCalibrator cameraCalibrator; - RandomDetector randomDetector; MiningSuccessHistory miningSuccessHistory; ObjectDetectionHistory objectDetectionHistory; @@ -46,7 +45,6 @@ public class IronMiner { objectDetector = new ObjectDetector(); robot = new Robot(); humanBehavior = new HumanBehavior(); - randomDetector = new RandomDetector(); cameraCalibrator = new CameraCalibrator(targetNumberOfDetectedOres); miningSuccessHistory = new MiningSuccessHistory(); objectDetectionHistory = new ObjectDetectionHistory(targetNumberOfDetectedOres); @@ -54,16 +52,18 @@ public class IronMiner { public void run() throws Exception { long startTime = System.currentTimeMillis(); - int worldHopCounter = 0; int count = 0; while (((System.currentTimeMillis() - startTime) / 1000.0 / 60) < 93) { - BufferedImage screenCapture = objectDetector.captureScreenshotGameWindow(); + BufferedImage screenCapture = ImageCapturer.captureScreenshotGameWindow(); ArrayList detectedObjects = objectDetector.getObjectsInImage(screenCapture, 0.30); ArrayList ironOres = objectDetector.getIronOres(detectedObjects); readjustCameraIfObjectsAreNotBeingDetected(detectedObjects.size()); - + humanBehavior.randomlyCheckMiningXP(cursor); + RandomDetector.dealWithRandoms(screenCapture, cursor); + dropInventoryIfCloseToFull(); + DetectedObject closestIronOre = getClosestObjectToCharacter(ironOres); if (closestIronOre != null) { @@ -109,15 +109,10 @@ public class IronMiner { count++; System.out.println("Ores in inventory: " + numberOfOresInInventoryBefore + ". Mining success? " + miningSuccess); - boolean worldHopped = hopWorldsIfMiningSuccessRateIsLow(miningSuccess); - if (worldHopped) { - worldHopCounter++; - } - System.out.println(count + ", time: " + ((System.currentTimeMillis() - startTime) / 1000 / 60) + ". Hops: " + worldHopCounter); + printMiningStats(count, startTime); + + hopWorldsIfMiningSuccessRateIsLow(miningSuccess); } - humanBehavior.randomlyCheckMiningXP(cursor); - randomDetector.dealWithRandoms(screenCapture, cursor); - dropInventoryIfFull(); } } @@ -140,14 +135,24 @@ public class IronMiner { } } - private void dropInventoryIfFull() throws Exception { + /*private void dropInventoryIfFull() throws Exception { inventory.updateLastSlot(); if (inventory.isLastSlotInInventoryFull()) { cursorTask.optimizedDropAllItemsInInventory(cursor, inventory); Thread.sleep(Randomizer.nextGaussianWithinRange(1104, 1651)); } + }*/ + + private void dropInventoryIfCloseToFull() throws Exception { + if (inventory.getNumberOfItemsOfTypeInInventory("ironOre") > 15) { + inventory.dropAllItemsOfType("ironOre", cursorTask, cursor); + Thread.sleep(Randomizer.nextGaussianWithinRange(1104, 1651)); + } } + private void printMiningStats(int numberOfIronOresMined, long startTime) { + System.out.println(numberOfIronOresMined + " ores mined in " + ((System.currentTimeMillis() - startTime) / 1000 / 60) + " minutes"); + } private DetectedObject getClosestObjectToCharacter(ArrayList detectedObjects) { int closestDistanceToCharacter = Integer.MAX_VALUE; diff --git a/src/ObjectDetector.java b/src/ObjectDetector.java index 1ac0531..d22cd90 100644 --- a/src/ObjectDetector.java +++ b/src/ObjectDetector.java @@ -146,9 +146,4 @@ public class ObjectDetector { data[i + 2] = tmp; } } - - public BufferedImage captureScreenshotGameWindow() throws IOException, AWTException { - Rectangle area = new Rectangle(Constants.GAME_WINDOW_OFFSET_X, Constants.GAME_WINDOW_OFFSET_Y, Constants.GAME_WINDOW_WIDTH, Constants.GAME_WINDOW_HEIGHT); - return robot.createScreenCapture(area); - } } \ No newline at end of file diff --git a/src/PathTransformer.java b/src/PathTransformer.java index a61a18e..9150b7f 100644 --- a/src/PathTransformer.java +++ b/src/PathTransformer.java @@ -29,7 +29,7 @@ public class PathTransformer { Matrix lhs = new Matrix(lhsArray); Matrix rhs = new Matrix(rhsArray, 3); - System.out.println("(" + p1.getX() + "," + p1.getY() + "), (" + p2.getX() + "," + p2.getY() + "), (" + p3.getX() + "," + p3.getY() + ")"); + //System.out.println("(" + p1.getX() + "," + p1.getY() + "), (" + p2.getX() + "," + p2.getY() + "), (" + p3.getX() + "," + p3.getY() + ")"); Matrix ans = lhs.solve(rhs); /*System.out.println("x = " + ans.get(0, 0)); diff --git a/src/RandomDetector.java b/src/RandomDetector.java index 3dbe999..76887ca 100644 --- a/src/RandomDetector.java +++ b/src/RandomDetector.java @@ -11,13 +11,8 @@ import java.util.ArrayList; import javax.imageio.ImageIO; public class RandomDetector { - Robot robot; - public RandomDetector() throws AWTException { - robot = new Robot(); - } - - public void dealWithRandoms(BufferedImage screenCapture, Cursor cursor) throws Exception { + public static void dealWithRandoms(BufferedImage screenCapture, Cursor cursor) throws Exception { Point chatDialogueCornerPoint = findChatDialogueCornerPoint(screenCapture); Point speakerPoint = findSpeakerPointFromCornerPoint(screenCapture, chatDialogueCornerPoint); @@ -35,9 +30,9 @@ public class RandomDetector { } } - private boolean dialogueHasDismissOption(Point speakerPoint) throws IOException, AWTException { + private static boolean dialogueHasDismissOption(Point speakerPoint) throws IOException, AWTException { Rectangle dialogueRectangle = new Rectangle(Constants.GAME_WINDOW_OFFSET_X + speakerPoint.x - 25, Constants.GAME_WINDOW_OFFSET_Y + speakerPoint.y, 95, 55); - BufferedImage dialogueCapture = robot.createScreenCapture(dialogueRectangle); + BufferedImage dialogueCapture = ImageCapturer.captureScreenRectangle(dialogueRectangle); for (int x = 0; x < 95; x++) { for (int y = 0; y < 55; y++) { int pixelColor = dialogueCapture.getRGB(x, y); @@ -49,7 +44,7 @@ public class RandomDetector { return false; } - public Point findChatDialogueCornerPoint(BufferedImage screenCapture) throws AWTException, InterruptedException { + public static Point findChatDialogueCornerPoint(BufferedImage screenCapture) throws AWTException, InterruptedException { for (int x = 30; x < Constants.GAME_WINDOW_WIDTH - 30; x++) { for (int y = 20; y < Constants.GAME_WINDOW_HEIGHT - 20; y++) { int pixelColor = screenCapture.getRGB(x, y); @@ -61,14 +56,14 @@ public class RandomDetector { return null; } - private boolean isPixelChatColor(int color) { + private static boolean isPixelChatColor(int color) { int[] colorChannels = getRGBChannelsFromPixel(color); return (isColorWithinTolerance(colorChannels[0], 0, 3) && isColorWithinTolerance(colorChannels[1], 255, 3) && isColorWithinTolerance(colorChannels[2], 255, 3)); } - public Point findSpeakerPointFromCornerPoint(BufferedImage screenCapture, Point chatDialogueStart) { + public static Point findSpeakerPointFromCornerPoint(BufferedImage screenCapture, Point chatDialogueStart) { if (chatDialogueStart == null) { return null; } @@ -93,25 +88,20 @@ public class RandomDetector { return null; } - private boolean isSpeakerPointCloseToCharacter(Point speakerPoint) { + private static boolean isSpeakerPointCloseToCharacter(Point speakerPoint) { return (Math.abs(speakerPoint.x + Constants.GAME_WINDOW_OFFSET_X - Constants.CHARACTER_CENTER_X) < 90 && Math.abs(speakerPoint.y + Constants.GAME_WINDOW_OFFSET_Y - Constants.CHARACTER_CENTER_Y) < 80); } - private Point getDismissOptionClickLocation(Point speakerLocation) { + private static Point getDismissOptionClickLocation(Point speakerLocation) { return new Point(speakerLocation.x, speakerLocation.y + 46); } - private boolean isColorWithinTolerance(int color1, int color2, int tolerance) { + private static boolean isColorWithinTolerance(int color1, int color2, int tolerance) { return (Math.abs(color1 - color2) < tolerance); } - private int[] getRGBChannelsFromPixel(int pixel) { + private static int[] getRGBChannelsFromPixel(int pixel) { int[] colors = {(pixel)&0xFF, (pixel>>8)&0xFF, (pixel>>16)&0xFF, (pixel>>24)&0xFF}; return colors; } - - public BufferedImage captureScreenshotGameWindow() throws IOException, AWTException { - Rectangle area = new Rectangle(Constants.GAME_WINDOW_OFFSET_X, Constants.GAME_WINDOW_OFFSET_Y, Constants.GAME_WINDOW_WIDTH, Constants.GAME_WINDOW_HEIGHT); - return robot.createScreenCapture(area); - } } diff --git a/src/TrackerThread.java b/src/TrackerThread.java index bf4c399..a60ab87 100644 --- a/src/TrackerThread.java +++ b/src/TrackerThread.java @@ -32,7 +32,7 @@ public class TrackerThread implements Runnable { int oreLostCount = 0; while (!objectTrackingFailure && oreLostCount < 3 && !isTimeElapsedOverLimit(miningStartTime, maxTimeToMine)) { long frameStartTime = System.currentTimeMillis(); - screenCapture = objectDetector.captureScreenshotGameWindow(); + screenCapture = ImageCapturer.captureScreenshotGameWindow(); ArrayList detectedObjects = objectDetector.getObjectsInImage(screenCapture, 0.15); ArrayList ironOres = objectDetector.getObjectsOfClassInList(detectedObjects, "ironOre"); objectTrackingFailure = ironOreTracker.update(screenCapture, boundingBox); diff --git a/target/classes/CameraCalibrator.class b/target/classes/CameraCalibrator.class index 6067219..4477ff9 100644 Binary files a/target/classes/CameraCalibrator.class and b/target/classes/CameraCalibrator.class differ diff --git a/target/classes/Cursor.class b/target/classes/Cursor.class index aad5c08..2936923 100644 Binary files a/target/classes/Cursor.class and b/target/classes/Cursor.class differ diff --git a/target/classes/CursorTask.class b/target/classes/CursorTask.class index d7ce81b..253ddc0 100644 Binary files a/target/classes/CursorTask.class and b/target/classes/CursorTask.class differ diff --git a/target/classes/DetectedObject.class b/target/classes/DetectedObject.class index 27f2259..fdeb79a 100644 Binary files a/target/classes/DetectedObject.class and b/target/classes/DetectedObject.class differ diff --git a/target/classes/ImageCapturer.class b/target/classes/ImageCapturer.class new file mode 100644 index 0000000..871a4d9 Binary files /dev/null and b/target/classes/ImageCapturer.class differ diff --git a/target/classes/ImageCollector.class b/target/classes/ImageCollector.class index e8f7c4e..0f14ee8 100644 Binary files a/target/classes/ImageCollector.class and b/target/classes/ImageCollector.class differ diff --git a/target/classes/Inventory.class b/target/classes/Inventory.class index 8db850a..797b254 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 9832e16..81a6e41 100644 Binary files a/target/classes/IronMiner.class and b/target/classes/IronMiner.class differ diff --git a/target/classes/ObjectDetector.class b/target/classes/ObjectDetector.class index 4f38b99..0385881 100644 Binary files a/target/classes/ObjectDetector.class and b/target/classes/ObjectDetector.class differ diff --git a/target/classes/PathTransformer.class b/target/classes/PathTransformer.class index d62fa82..843b6a8 100644 Binary files a/target/classes/PathTransformer.class and b/target/classes/PathTransformer.class differ diff --git a/target/classes/RandomDetector.class b/target/classes/RandomDetector.class index 5fdd851..4bc5792 100644 Binary files a/target/classes/RandomDetector.class and b/target/classes/RandomDetector.class differ diff --git a/target/classes/RandomDetectorTest.class b/target/classes/RandomDetectorTest.class index de44f65..4a78244 100644 Binary files a/target/classes/RandomDetectorTest.class and b/target/classes/RandomDetectorTest.class differ diff --git a/target/classes/TrackerThread.class b/target/classes/TrackerThread.class index 3a6e9c6..d1b75f2 100644 Binary files a/target/classes/TrackerThread.class and b/target/classes/TrackerThread.class differ