diff --git a/src/Cursor.java b/src/Cursor.java index 7f735f7..45d0f33 100644 --- a/src/Cursor.java +++ b/src/Cursor.java @@ -73,18 +73,23 @@ public class Cursor { return randomizer.nextGaussianWithinRange(MINIMUM_CLICK_LENGTH, MAXIMUM_CLICK_LENGTH); } + private int getRandomClickReleaseLength() { + return randomizer.nextGaussianWithinRange(MINIMUM_CLICK_LENGTH + 5, MAXIMUM_CLICK_LENGTH + 10); + } + + public void leftClickCursor() throws InterruptedException { robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); Thread.sleep(getRandomClickLength()); robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); - Thread.sleep(getRandomClickLength()); + Thread.sleep(getRandomClickReleaseLength()); } public void rightClickCursor() throws InterruptedException { robot.mousePress(InputEvent.BUTTON3_DOWN_MASK); - Thread.sleep(20 + getRandomClickLength()); + Thread.sleep(getRandomClickLength() + 20); robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK); - Thread.sleep(getRandomClickLength()); + Thread.sleep(getRandomClickReleaseLength()); } public void moveAndLeftClickAtCoordinates(Point goalPoint) throws Exception { @@ -99,15 +104,25 @@ public class Cursor { public Point moveAndLeftClickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws Exception { Point randomizedGoalPoint = randomizePoint(goalPoint, xTolerance, yTolerance); - moveCursorToCoordinates(randomizedGoalPoint); - leftClickCursor(); + moveAndLeftClickAtCoordinates(randomizedGoalPoint); + return randomizedGoalPoint; // Return the point we moved to in case we need precise movement afterwards + } + + public Point moveAndLeftClickAtCoordinatesWithRandomness(Point goalPoint, int xToleranceLeft, int xToleranceRight, int yTolerance) throws Exception { + Point randomizedGoalPoint = randomizePoint(goalPoint, xToleranceLeft, xToleranceRight, yTolerance); + moveAndLeftClickAtCoordinates(randomizedGoalPoint); return randomizedGoalPoint; // Return the point we moved to in case we need precise movement afterwards } public Point moveAndRightlickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws Exception { Point randomizedGoalPoint = randomizePoint(goalPoint, xTolerance, yTolerance); - moveCursorToCoordinates(randomizedGoalPoint); - rightClickCursor(); + moveAndRightClickAtCoordinates(randomizedGoalPoint); + return randomizedGoalPoint; // Return the point we moved to in case we need precise movement afterwards + } + + public Point moveAndRightlickAtCoordinatesWithRandomness(Point goalPoint, int xToleranceLeft, int xToleranceRight, int yTolerance) throws Exception { + Point randomizedGoalPoint = randomizePoint(goalPoint, xToleranceLeft, xToleranceRight, yTolerance); + moveAndRightClickAtCoordinates(randomizedGoalPoint); return randomizedGoalPoint; // Return the point we moved to in case we need precise movement afterwards } @@ -115,15 +130,14 @@ public class Cursor { Point startingPoint = getCurrentCursorPoint(); int distanceToMoveCursor = getDistanceBetweenPoints(startingPoint, goalPoint); double angleToRotateCursorPathTo = getThetaBetweenPoints(startingPoint, goalPoint); - System.out.println("R:" + distanceToMoveCursor + ", theta:" + angleToRotateCursorPathTo); + if (distanceToMoveCursor == 0) { return; } CursorPath cursorPathWithDistanceSet = chooseCursorPathToFollowBasedOnDistance(distanceToMoveCursor); CursorPath cursorPathWithDistanceAndAngleSet = cursorPathWithDistanceSet.getRotatedCopyOfCursorPath(angleToRotateCursorPathTo); - System.out.println("Rotated the points: "); - //cursorPathWithDistanceAndAngleSet.displayCursorPoints(); + followCursorPath(cursorPathWithDistanceAndAngleSet, startingPoint); } @@ -149,50 +163,45 @@ public class Cursor { private CursorPath chooseCursorPathToFollowBasedOnDistance(int distanceToMoveCursor) throws Exception { int newDistanceToMoveCursor = findNearestPathLengthThatExists(distanceToMoveCursor); - double scaleToFactorBy = getScaleToFactorBy(newDistanceToMoveCursor, distanceToMoveCursor); - System.out.println("new distance to follow cursor is: " + newDistanceToMoveCursor + " from scaling by " + scaleToFactorBy); ArrayList cursorPathsWithSameDistance = cursorPathsByDistance.get(newDistanceToMoveCursor); + + CursorPath randomlyChosenCursorPath = cursorPathsWithSameDistance.get(random.nextInt(cursorPathsWithSameDistance.size())); + if (newDistanceToMoveCursor == distanceToMoveCursor) { + return randomlyChosenCursorPath; + } - CursorPath scaledCursorPath = cursorPathsWithSameDistance.get(random.nextInt(cursorPathsWithSameDistance.size())).getScaledCopyOfCursorPath(scaleToFactorBy); - - return scaledCursorPath; - //return cursorPathsByDistance.get(newDistanceToMoveCursor).get(indexOfRandomPathToFollow);//scaledCursorPath; + double scaleToFactorBy = getScaleToFactorBy(newDistanceToMoveCursor, distanceToMoveCursor); + return randomlyChosenCursorPath.getScaledCopyOfCursorPath(scaleToFactorBy); } public int findNearestPathLengthThatExists(int distanceToMoveCursor) throws Exception { - int offset = 0; - boolean reachedMinimumLimit = false; - boolean reachedMaximumLimit = false; - while (cursorPathsByDistance.get(distanceToMoveCursor + offset).size() == 0) { - // Go up - if (distanceToMoveCursor + Math.abs(offset) + 1 >= NUMBER_OF_DISTANCES) { - reachedMaximumLimit = true; - } - if (distanceToMoveCursor - Math.abs(offset) - 1 < 0) { - reachedMinimumLimit = true; - } - if (reachedMaximumLimit && reachedMinimumLimit) { - throw new Exception("No paths exist."); - } - - if (offset < 0) { - if (!reachedMaximumLimit) { - offset = -offset + 1; - } - else { - offset--; - } - } - else { - if (!reachedMinimumLimit) { - offset = -offset - 1; - } - else { - offset++; - } + int closestShorterPathLength = Integer.MIN_VALUE; + int closestLongerPathLength = Integer.MAX_VALUE; + for (int i = distanceToMoveCursor; i >= 0; i--) { + if (cursorPathsByDistance.get(i).size() > 0) { + closestShorterPathLength = i; + break; } } - return distanceToMoveCursor + offset; + for (int i = distanceToMoveCursor; i < 2203; i++) { + if (cursorPathsByDistance.get(i).size() > 0) { + closestLongerPathLength = i; + break; + } + } + + if (closestShorterPathLength == Integer.MIN_VALUE && closestLongerPathLength == Integer.MAX_VALUE) { + throw new Exception("No paths of any size exist."); + } + else if (closestShorterPathLength == Integer.MIN_VALUE) { + return closestLongerPathLength; + } + else if (closestLongerPathLength == Integer.MAX_VALUE) { + return closestShorterPathLength; + } + else { + return (Math.abs(distanceToMoveCursor - closestShorterPathLength) <= Math.abs(distanceToMoveCursor - closestLongerPathLength)) ? closestShorterPathLength : closestLongerPathLength; + } } private double getScaleToFactorBy(int newDistanceToMoveCursor, int distanceToMoveCursor) { @@ -209,14 +218,15 @@ public class Cursor { 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)); + } + public void displayCursorPaths() { for (int i = 0; i < NUMBER_OF_DISTANCES; i++) { System.out.println("There are " + cursorPathsByDistance.get(i).size() + " paths of size " + i); } System.out.println("--------------"); - /*for (int i = 0; i < cursorPathsByDistance.get(1).size(); i++) { - cursorPathsByDistance.get(1).get(i).displayCursorPoints(); - }*/ - //cursorPathsByDistance.get(0).get(0).displayCursorPoints(); } } \ No newline at end of file diff --git a/src/CursorPath.java b/src/CursorPath.java index 30bb08f..9242347 100644 --- a/src/CursorPath.java +++ b/src/CursorPath.java @@ -101,7 +101,7 @@ public class CursorPath { } private boolean isCursorPathTimespanReasonable() { - return (this.timespan > 50 && this.timespan < 400); + return (this.timespan > 50 && this.timespan < 300); } private boolean isCursorPathDistanceReasonable() { @@ -109,7 +109,7 @@ public class CursorPath { } private boolean isCursorPathNumPointsReasonable() { - return (this.cursorPoints.size() > 0 && this.cursorPoints.size() < 80); + return (this.cursorPoints.size() > 0 && this.cursorPoints.size() < 40); } public ArrayList getCursorPathPoints() { diff --git a/src/CursorTask.java b/src/CursorTask.java index efb586a..01dca78 100644 --- a/src/CursorTask.java +++ b/src/CursorTask.java @@ -8,13 +8,13 @@ public class CursorTask { // Human drop time: 29 seconds - // Measured: 30 seconds, 29 seconds - public void optimizedDropAllItemsInInventory(Cursor cursor, Inventory inventory) throws InterruptedException { + // Measured: 30 seconds, 29 seconds, 28 + public void optimizedDropAllItemsInInventory(Cursor cursor, Inventory inventory) throws Exception { for (int row = 0; row < 4; row++) { Point coordinatesToClick = dropItem(cursor, inventory, row, 0); for (int column = 1; column < 7; column++) { - if (distanceBetweenPoints(coordinatesToClick, inventory.getClickCoordinatesCoordinatesForInventorySlot(row, column)) > 12) { - coordinatesToClick = inventory.getClickCoordinatesCoordinatesForInventorySlot(row, column); + if (distanceBetweenPoints(coordinatesToClick, inventory.getClickCoordinatesForInventorySlot(row, column)) > 12) { + coordinatesToClick = inventory.getClickCoordinatesForInventorySlot(row, column); } rightClickItemSlot(cursor, coordinatesToClick); coordinatesToClick = leftClickDropOption(cursor, coordinatesToClick, column); @@ -26,7 +26,7 @@ public class CursorTask { return (int) (Math.hypot(a.x - b.x, a.y - b.y)); } - public void dropAllItemsInInventory(Cursor cursor, Inventory inventory) throws InterruptedException { + public void dropAllItemsInInventory(Cursor cursor, Inventory inventory) throws Exception { for (int row = 0; row < 4; row++) { for (int column = 0; column < 7; column++) { dropItem(cursor, inventory, row, column); @@ -35,22 +35,22 @@ public class CursorTask { } - public Point dropItem(Cursor cursor, Inventory inventory, int row, int column) throws InterruptedException { - Point coordinatesToRightClick = inventory.getClickCoordinatesCoordinatesForInventorySlot(row, column); + public Point dropItem(Cursor cursor, Inventory inventory, int row, int column) throws Exception { + Point coordinatesToRightClick = inventory.getClickCoordinatesForInventorySlot(row, column); Point clickedCoordinates = rightClickItemSlotWithRandomness(cursor, coordinatesToRightClick); return leftClickDropOption(cursor, clickedCoordinates, column); } - public void rightClickItemSlot(Cursor cursor, Point coordinatesToRightClick) throws InterruptedException { + public void rightClickItemSlot(Cursor cursor, Point coordinatesToRightClick) throws Exception { cursor.moveAndRightClickAtCoordinates(coordinatesToRightClick); } - public Point rightClickItemSlotWithRandomness(Cursor cursor, Point coordinatesToRightClick) throws InterruptedException { + public Point rightClickItemSlotWithRandomness(Cursor cursor, Point coordinatesToRightClick) throws Exception { Point clickedCoordinates = cursor.moveAndRightlickAtCoordinatesWithRandomness(coordinatesToRightClick, 6, 6); return clickedCoordinates; } - private Point leftClickDropOption(Cursor cursor, Point coordinatesToLeftClick, int column) throws InterruptedException { + private Point leftClickDropOption(Cursor cursor, Point coordinatesToLeftClick, int column) throws Exception { Point offsetCoordinatesToLeftClick = coordinatesToLeftClick; if (column < 6) { offsetCoordinatesToLeftClick.y += DROP_OFFSET; @@ -58,6 +58,7 @@ public class CursorTask { else { offsetCoordinatesToLeftClick.y = DROP_BOTTOM_ROW; } - return cursor.moveAndLeftClickAtCoordinatesWithRandomness(offsetCoordinatesToLeftClick, 10, 6); + System.out.println("foudn where to click..."); + return cursor.moveAndLeftClickAtCoordinatesWithRandomness(offsetCoordinatesToLeftClick, 13, 10, 6); } } diff --git a/src/CursorTest.java b/src/CursorTest.java index 36f293d..b40ac59 100644 --- a/src/CursorTest.java +++ b/src/CursorTest.java @@ -27,9 +27,12 @@ class CursorTest { } void testFindNearestPathLengthThatExists() throws Exception { + int closestLengthForPreviousValue = 0; for (int i = 0; i < 2203; i++) { int closestLength = cursor.findNearestPathLengthThatExists(i); - System.out.println("Closest path to length " + i + " is " + closestLength); + assertTrue(closestLength >= closestLengthForPreviousValue); + closestLengthForPreviousValue = closestLength; + //System.out.println("Closest path to length " + i + " is " + closestLength); } } diff --git a/src/Inventory.java b/src/Inventory.java index 1c79505..31a1256 100644 --- a/src/Inventory.java +++ b/src/Inventory.java @@ -47,7 +47,7 @@ public class Inventory { } private void initializeItems() throws IOException { - items = new InventoryItems("/home/dpapp/Desktop/RunescapeAIPics/Items/"); + items = new InventoryItems("/home/dpapp/Desktop/RunescapeAI/Items/"); } public void update() throws IOException { @@ -89,7 +89,7 @@ public class Inventory { return true; } - public Point getClickCoordinatesCoordinatesForInventorySlot(int row, int column) { + public Point getClickCoordinatesForInventorySlot(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; diff --git a/src/InventoryItemsTest.java b/src/InventoryItemsTest.java index d562816..b4ff817 100644 --- a/src/InventoryItemsTest.java +++ b/src/InventoryItemsTest.java @@ -18,8 +18,8 @@ class InventoryItemsTest { public void initialize() throws IOException { System.out.println("running initialize..."); - items = new InventoryItems("/home/dpapp/Desktop/RunescapeAIPics/Items/"); - this.testingItemDirectoryPath = "/home/dpapp/Desktop/RunescapeAIPics/Tests/ItemNameRecognition/"; + items = new InventoryItems("/home/dpapp/Desktop/RunescapeAI/Items/"); + this.testingItemDirectoryPath = "/home/dpapp/Desktop/RunescapeAI/Tests/ItemNameRecognition/"; } @Test diff --git a/src/InventoryTest.java b/src/InventoryTest.java index c979efa..a0a4068 100644 --- a/src/InventoryTest.java +++ b/src/InventoryTest.java @@ -16,7 +16,7 @@ class InventoryTest { public void initialize() throws AWTException, IOException { inventory = new Inventory(); - this.testingInventoryDirectoryPath = "/home/dpapp/Desktop/RunescapeAIPics/Tests/Inventory/"; + this.testingInventoryDirectoryPath = "/home/dpapp/Desktop/RunescapeAI/Tests/Inventory/"; } @Test diff --git a/src/Randomizer.java b/src/Randomizer.java index 4d71aa7..1b5c768 100644 --- a/src/Randomizer.java +++ b/src/Randomizer.java @@ -11,10 +11,12 @@ public class Randomizer { } public int nextGaussianWithinRange(double rangeBegin, double rangeEnd) { - double rangeMean = (rangeEnd - rangeBegin) / 2.0; + double rangeMean = (rangeEnd + rangeBegin) / 2.0; double rangeSTD = (rangeEnd - rangeMean) / 3.0; double result = random.nextGaussian() * rangeSTD + rangeMean; while (result > rangeEnd || result < rangeBegin) { + System.out.println("Gaussian result out of range..."); + System.out.println(rangeMean + ", std: " + rangeSTD); result = random.nextGaussian() * rangeSTD + rangeMean; } return (int) result; diff --git a/src/WillowChopper.java b/src/WillowChopper.java index d1c2902..e887ea7 100644 --- a/src/WillowChopper.java +++ b/src/WillowChopper.java @@ -14,9 +14,10 @@ public class WillowChopper { inventory = new Inventory(); } - public void run() throws IOException, InterruptedException { + public void run() throws Exception { while (true) { + Thread.sleep(250); /* if (character.isCharacterEngaged()) { // DO NOTHING @@ -29,7 +30,9 @@ public class WillowChopper { */ inventory.update(); if (inventory.isInventoryFull()) { - cursorTask.dropAllItemsInInventory(cursor, inventory); + System.out.println("Inventory is full! Dropping..."); + cursorTask.optimizedDropAllItemsInInventory(cursor, inventory); + //cursorTask.dropAllItemsInInventory(cursor, inventory); } } } diff --git a/src/main.java b/src/main.java index 4978077..a1d43fc 100644 --- a/src/main.java +++ b/src/main.java @@ -6,8 +6,9 @@ import java.net.URL; public class main { - public static void main(String[] args) throws AWTException, InterruptedException, IOException { - + public static void main(String[] args) throws Exception { + WillowChopper willowChopper = new WillowChopper(); + willowChopper.run(); /*Cursor cursor = new Cursor(); CursorTask cursorTask = new CursorTask(); Inventory inventory = new Inventory(); diff --git a/target/classes/Cursor.class b/target/classes/Cursor.class index 6b55742..8f2edb5 100644 Binary files a/target/classes/Cursor.class and b/target/classes/Cursor.class differ diff --git a/target/classes/CursorPath.class b/target/classes/CursorPath.class index 1d560c9..f352f91 100644 Binary files a/target/classes/CursorPath.class and b/target/classes/CursorPath.class differ diff --git a/target/classes/CursorTask.class b/target/classes/CursorTask.class index b6df67f..c601f0e 100644 Binary files a/target/classes/CursorTask.class and b/target/classes/CursorTask.class differ diff --git a/target/classes/CursorTest.class b/target/classes/CursorTest.class index 3943342..d2d7f56 100644 Binary files a/target/classes/CursorTest.class and b/target/classes/CursorTest.class differ diff --git a/target/classes/Inventory.class b/target/classes/Inventory.class index 6f5da5b..76dd518 100644 Binary files a/target/classes/Inventory.class and b/target/classes/Inventory.class differ diff --git a/target/classes/InventoryItemsTest.class b/target/classes/InventoryItemsTest.class index f28c40d..59dad8f 100644 Binary files a/target/classes/InventoryItemsTest.class and b/target/classes/InventoryItemsTest.class differ diff --git a/target/classes/InventoryTest.class b/target/classes/InventoryTest.class index 9702b2b..63e34c7 100644 Binary files a/target/classes/InventoryTest.class and b/target/classes/InventoryTest.class differ diff --git a/target/classes/Randomizer.class b/target/classes/Randomizer.class index 6485227..a4fbe73 100644 Binary files a/target/classes/Randomizer.class and b/target/classes/Randomizer.class differ diff --git a/target/classes/WillowChopper.class b/target/classes/WillowChopper.class index d06345f..a118a2e 100644 Binary files a/target/classes/WillowChopper.class and b/target/classes/WillowChopper.class differ diff --git a/target/classes/main.class b/target/classes/main.class index 8317e8f..cf345f1 100644 Binary files a/target/classes/main.class and b/target/classes/main.class differ