diff --git a/bin/Cursor.class b/bin/Cursor.class index 71bac50..6ed146e 100644 Binary files a/bin/Cursor.class and b/bin/Cursor.class differ diff --git a/bin/CursorTask.class b/bin/CursorTask.class index 4314405..b5eb0d0 100644 Binary files a/bin/CursorTask.class and b/bin/CursorTask.class differ diff --git a/bin/Inventory.class b/bin/Inventory.class index baf249e..c55c372 100644 Binary files a/bin/Inventory.class and b/bin/Inventory.class differ diff --git a/bin/InventorySlot.class b/bin/InventorySlot.class index 0ec5fff..a3a694a 100644 Binary files a/bin/InventorySlot.class and b/bin/InventorySlot.class differ diff --git a/bin/InventoryTest.class b/bin/InventoryTest.class index 5ac6678..9702b2b 100644 Binary files a/bin/InventoryTest.class and b/bin/InventoryTest.class differ diff --git a/bin/main.class b/bin/main.class index 2a1128a..3bc40d4 100644 Binary files a/bin/main.class and b/bin/main.class differ diff --git a/src/Cursor.java b/src/Cursor.java index f47c595..8a0b036 100644 --- a/src/Cursor.java +++ b/src/Cursor.java @@ -25,12 +25,6 @@ public class Cursor { public static final int GAME_WINDOW_OFFSET_WIDTH = 100; // top left corner of main game screen, from top left corner of screen public static final int GAME_WINDOW_OFFSET_HEIGHT = 81; - public static final int INVENTORY_OFFSET_WIDTH = 649; //top left corner of inventory, fromm top left corner of screen - public static final int INVENTORY_OFFSET_HEIGHT = 286; - public static final int INVENTORY_WIDTH = 820 - 649;// 820 - public static final int INVENTORY_HEIGHT = 530 - 286; // 530 - - private Robot robot; private Random random = new Random(); @@ -38,6 +32,7 @@ public class Cursor { private ArrayList> cursorPathsByDistance; public Cursor() throws AWTException { + System.out.println("Initializing cursor..."); initializeCursorPathsByDistanceFromFile("/home/dpapp/GhostMouse/coordinates.txt"); robot = new Robot(); @@ -84,12 +79,14 @@ public class Cursor { robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); Thread.sleep(getRandomClickLength()); robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); + Thread.sleep(getRandomClickLength()); } public void rightClickCursor() throws InterruptedException { robot.mousePress(InputEvent.BUTTON3_DOWN_MASK); - Thread.sleep(getRandomClickLength()); + Thread.sleep(200 + getRandomClickLength() * 2); robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK); + Thread.sleep(getRandomClickLength()); } public void moveAndLeftClickAtCoordinates(Point goalPoint) throws InterruptedException { @@ -97,10 +94,22 @@ public class Cursor { leftClickCursor(); } + public void moveAndLeftClickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws InterruptedException { + Point randomizedGoalPoint = randomizePoint(goalPoint, xTolerance, yTolerance); + moveCursorToCoordinates(randomizedGoalPoint); + leftClickCursor(); + } + public void moveAndRightClickAtCoordinates(Point goalPoint) throws InterruptedException { moveCursorToCoordinates(goalPoint); rightClickCursor(); } + + public void moveAndRightlickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws InterruptedException { + Point randomizedGoalPoint = randomizePoint(goalPoint, xTolerance, yTolerance); + moveCursorToCoordinates(randomizedGoalPoint); + rightClickCursor(); + } public void moveCursorToCoordinates(Point goalPoint) throws InterruptedException { Point startingCursorPoint = getCurrentCursorPoint(); @@ -147,4 +156,12 @@ public class Cursor { public Point getCurrentCursorPoint() { return MouseInfo.getPointerInfo().getLocation(); } + + private int getRandomIntSigned(int tolerance) { + return random.nextInt(tolerance) - tolerance / 2; + } + + private Point randomizePoint(Point goalPoint, int xTolerance, int yTolerance) { + return new Point(goalPoint.x + getRandomIntSigned(xTolerance), goalPoint.y + getRandomIntSigned(yTolerance)); + } } \ No newline at end of file diff --git a/src/CursorTask.java b/src/CursorTask.java index c602711..bf100db 100644 --- a/src/CursorTask.java +++ b/src/CursorTask.java @@ -1,22 +1,38 @@ import java.awt.AWTException; +import java.awt.Point; -public class CursorTask extends Cursor { - - public CursorTask() throws AWTException { - super(); - } +public class CursorTask { - public void dropAllItemsInInventory() { - for (int inventoryColumn = 0; inventoryColumn < 7; inventoryColumn++) { - for (int inventoryRow = 0; inventoryRow < 4; inventoryRow++) { - //dropItem + public static final int DROP_OFFSET = 40; + public static final int DROP_OFFSET_BOTTOM_ROW = 10; + + public void dropAllItemsInInventory(Cursor cursor, Inventory inventory) throws InterruptedException { + for (int row = 0; row < 4; row++) { + for (int column = 0; column < 7; column++) { + dropItem(cursor, inventory, row, column); } } } - public void dropItem(InventorySlot inventorySlot) { - /*Point inventorySlotCoordinates = inventorySlot.getCoordinates(); - moveAndRightClickAtCoordinates(inventorySlotCoordinates); - moveAndLeftClickAtCoordinates();*/ + public void dropItem(Cursor cursor, Inventory inventory, int row, int column) throws InterruptedException { + System.out.println("Dropping item..."); + Point coordinatesToRightClick = inventory.getClickCoordinatesCoordinatesForInventorySlot(row, column); + rightClickItemSlot(cursor, coordinatesToRightClick); + leftClickDropOption(cursor, coordinatesToRightClick, row); + } + + public void rightClickItemSlot(Cursor cursor, Point coordinatesToRightClick) throws InterruptedException { + cursor.moveAndRightlickAtCoordinatesWithRandomness(coordinatesToRightClick, 10, 10); + } + + private void leftClickDropOption(Cursor cursor, Point coordinatesToLeftClick, int row) throws InterruptedException { + Point offsetCoordinatesToLeftClick = coordinatesToLeftClick; + if (row < 6) { + offsetCoordinatesToLeftClick.y += DROP_OFFSET; + } + else { + offsetCoordinatesToLeftClick.y += DROP_OFFSET_BOTTOM_ROW; + } + cursor.moveAndLeftClickAtCoordinatesWithRandomness(coordinatesToLeftClick, 10, 10); } } diff --git a/src/Inventory.java b/src/Inventory.java index 3f480bc..eff91b0 100644 --- a/src/Inventory.java +++ b/src/Inventory.java @@ -1,4 +1,5 @@ import java.awt.AWTException; +import java.awt.Point; import java.awt.Rectangle; import java.awt.Robot; import java.awt.image.BufferedImage; @@ -14,6 +15,9 @@ public class Inventory { public static final int INVENTORY_WIDTH = 820 - 649;// 820 public static final int INVENTORY_HEIGHT = 350; // 530 + private static final int INVENTORY_SLOT_WIDTH = 171 / 4; + private static final int INVENTORY_SLOT_HEIGHT = 254 / 7; + public static final int NUM_ROWS = 4; public static final int NUM_COLUMNS = 7; @@ -48,7 +52,10 @@ public class Inventory { public void update() throws IOException { BufferedImage image = robot.createScreenCapture(this.inventoryRectangleToCapture); - ImageIO.write(image, "png", new File(getImageName())); + + // For test image generation only + //ImageIO.write(image, "png", new File(getImageName())); + updateAllInventorySlots(image); } @@ -56,6 +63,7 @@ public class Inventory { return ("/home/dpapp/Desktop/RunescapeAIPics/Tests/Inventory/inventory.png"); } + // For testing only public void updateWithFakeImageForTests(BufferedImage testImage) throws IOException { updateAllInventorySlots(testImage); } @@ -73,14 +81,22 @@ public class Inventory { return inventorySlots[row][column].getItemNameInInventorySlot(items); } - /*public boolean isInventoryFull() { + public boolean isInventoryFull() { + // TODO: this will fail if some unexpected item shows up for (int row = 0; row < 4; row++) { for (int column = 0; column < 7; column++) { - if (!inventorySlots[row][column].isInventorySlotEmpty(items)) { + if (inventorySlots[row][column].isInventorySlotEmpty(items)) { return false; } } } return true; - }*/ + } + + public Point getClickCoordinatesCoordinatesForInventorySlot(int row, int column) { + Point centerOfInventorySlot = inventorySlots[row][column].getClickablePointWithinItemSlot(); + int x = INVENTORY_OFFSET_WIDTH + row * INVENTORY_SLOT_WIDTH + centerOfInventorySlot.x; + int y = INVENTORY_OFFSET_HEIGHT + column * INVENTORY_SLOT_HEIGHT + centerOfInventorySlot.y; + return new Point(x, y); + } } diff --git a/src/InventorySlot.java b/src/InventorySlot.java index 5dc3001..84d9887 100644 --- a/src/InventorySlot.java +++ b/src/InventorySlot.java @@ -31,15 +31,21 @@ public class InventorySlot { } public boolean isInventorySlotEmpty(InventoryItems items) { - return ("empty" == items.getNameOfItemFromImage(this.inventorySlotImage)); + return (items.getNameOfItemFromImage(this.inventorySlotImage).equals("empty")); } - public void writeInventorySlotImage(BufferedImage image, int row, int column) throws IOException { + public Point getClickablePointWithinItemSlot() { + return new Point(INVENTORY_SLOT_WIDTH / 2, INVENTORY_SLOT_HEIGHT / 2); + } + + // For test image generation only + /*public void writeInventorySlotImage(BufferedImage image, int row, int column) throws IOException { updateInventorySlot(image); ImageIO.write(this.inventorySlotImage, "png", new File(getImageName(row, column))); } + // For test image generation only private String getImageName(int row, int column) { return ("/home/dpapp/Desktop/RunescapeAIPics/InventorySlots/inventorySlot_" + row + "_" + column + ".png"); - } + }*/ } diff --git a/src/InventoryTest.java b/src/InventoryTest.java index a27c8f3..c979efa 100644 --- a/src/InventoryTest.java +++ b/src/InventoryTest.java @@ -35,20 +35,32 @@ class InventoryTest { {"empty", "oakLogs", "empty", "logs", "willowLogs", "empty", "willowLogs"}, {"logs", "empty", "oakLogs", "oakLogs", "empty", "oakLogs", "empty"}, {"willowLogs", "empty", "logs", "willowLogs", "empty", "logs", "logs"}}; - testInventory("inventory_0.png", expectedItemNames0); - testInventory("inventory_1.png", expectedItemNames1); - testInventory("inventory_2.png", expectedItemNames2); + testGetNameInItemInventorySlotHelper("inventory_0.png", expectedItemNames0); + testGetNameInItemInventorySlotHelper("inventory_1.png", expectedItemNames1); + testGetNameInItemInventorySlotHelper("inventory_2.png", expectedItemNames2); } - public BufferedImage loadBufferedImage(String fileName) throws IOException { - File itemFile = new File(this.testingInventoryDirectoryPath + fileName); - BufferedImage itemImage = ImageIO.read(itemFile); - return itemImage; + @Test + public void testInventoryFull() throws AWTException, IOException { + initialize(); + + testInventoryFullHelper("inventory_full_0.png", true); + testInventoryFullHelper("inventory_full_1.png", true); + testInventoryFullHelper("inventory_full_2.png", true); + testInventoryFullHelper("inventory_full_3.png", true); + testInventoryFullHelper("inventory_full_4.png", true); + testInventoryFullHelper("inventory_not_full_0.png", false); + testInventoryFullHelper("inventory_not_full_1.png", false); + testInventoryFullHelper("inventory_not_full_2.png", false); } - void testInventory(String inventoryFileName, String[][] expectedItemNames) throws IOException { - BufferedImage testImage = loadBufferedImage(inventoryFileName); - inventory.updateWithFakeImageForTests(testImage); + public void testInventoryFullHelper(String inventoryFileName, boolean expectedResult) throws IOException { + loadTestingImageToInventory(inventoryFileName); + assertEquals(inventory.isInventoryFull(), expectedResult); + } + + public void testGetNameInItemInventorySlotHelper(String inventoryFileName, String[][] expectedItemNames) throws IOException { + loadTestingImageToInventory(inventoryFileName); for (int row = 0; row < 4; row++) { for (int column = 0; column < 7; column++) { @@ -57,4 +69,17 @@ class InventoryTest { } } + public BufferedImage loadBufferedImage(String fileName) throws IOException { + File itemFile = new File(this.testingInventoryDirectoryPath + fileName); + BufferedImage itemImage = ImageIO.read(itemFile); + return itemImage; + } + + public void loadTestingImageToInventory(String inventoryFileName) throws IOException { + BufferedImage testImage = loadBufferedImage(inventoryFileName); + inventory.updateWithFakeImageForTests(testImage); + } + + + } diff --git a/src/main.java b/src/main.java index a4ed668..ade6622 100644 --- a/src/main.java +++ b/src/main.java @@ -8,11 +8,18 @@ public class main { public static void main(String[] args) throws AWTException, InterruptedException, IOException { - //Cursor cursor = new Cursor(); + Cursor cursor = new Cursor(); + CursorTask cursorTask = new CursorTask(); //Inventory inventory = new Inventory(); //inventory.update(); Inventory inventory = new Inventory(); - inventory.update(); + //inventory.update(); + cursorTask.dropAllItemsInInventory(cursor, inventory); + /*cursorTask.dropItem(cursor, inventory, 0, 0); + Thread.sleep(3000); + cursorTask.dropItem(cursor, inventory, 3, 5); + Thread.sleep(3000); + cursorTask.dropItem(cursor, inventory, 0, 6);*/ //Items items = new Items("/home/dpapp/Desktop/RunescapeAIPics/Items/"); //items.displayItems();