diff --git a/bin/Cursor.class b/bin/Cursor.class index 69bf4de..372473b 100644 Binary files a/bin/Cursor.class and b/bin/Cursor.class differ diff --git a/bin/CursorPath.class b/bin/CursorPath.class index 9396e75..17b5f80 100644 Binary files a/bin/CursorPath.class and b/bin/CursorPath.class differ diff --git a/bin/CursorTest.class b/bin/CursorTest.class new file mode 100644 index 0000000..9336681 Binary files /dev/null and b/bin/CursorTest.class differ diff --git a/bin/ScreenshotAutomator.class b/bin/ScreenshotAutomator.class new file mode 100644 index 0000000..788778d Binary files /dev/null and b/bin/ScreenshotAutomator.class differ diff --git a/bin/main.class b/bin/main.class index 65bca78..8af913e 100644 Binary files a/bin/main.class and b/bin/main.class differ diff --git a/src/Cursor.java b/src/Cursor.java index 2e3eac3..70e4b40 100644 --- a/src/Cursor.java +++ b/src/Cursor.java @@ -1,28 +1,33 @@ /* Reads a file of coordinates */ +import java.awt.AWTException; import java.awt.MouseInfo; import java.awt.Point; import java.awt.PointerInfo; +import java.awt.Robot; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; +import java.util.Random; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Cursor { - public static final int NUMBER_OF_DISTANCES = 600; + public static final int NUMBER_OF_DISTANCES = 1000; + private Robot robot; private ArrayList> cursorPathsByDistance; - public Cursor() { + public Cursor() throws AWTException { ArrayList cursorPaths = getArrayListOfCursorPathsFromFile("/home/dpapp/GhostMouse/coordinates.txt");// read from file or something; - initializeCursorPathsByDistance(); assignCursorPathsByDistance(cursorPaths); + + robot = new Robot(); } private void initializeCursorPathsByDistance() { @@ -49,10 +54,56 @@ public class Cursor { this.cursorPathsByDistance.get(cursorPath.getCursorPathDistance()).add(cursorPath); } + public void moveCursorToCoordinates(Point goalPoint) throws InterruptedException { + Point startingCursorPoint = getCurrentCursorPoint(); + int distanceToMoveCursor = calculateDistanceBetweenPoints(startingCursorPoint, goalPoint); + double thetaDirectionToMoveCursor = calculateThetaBetweenPoints(startingCursorPoint, goalPoint); + + CursorPath cursorPathToFollow = chooseCursorPathToFollowBasedOnDistance(distanceToMoveCursor); + System.out.println("Starting from " + startingCursorPoint.x + ", " + startingCursorPoint.y); + System.out.println("Moving to " + goalPoint.x + ", " + goalPoint.y); + System.out.println("Moving in " + thetaDirectionToMoveCursor / Math.PI * 180 + " degree direction."); + followCursorPath(startingCursorPoint, thetaDirectionToMoveCursor, cursorPathToFollow); + } + + private void followCursorPath(Point startingCursorPoint, double thetaDirectionToMoveCursor, CursorPath cursorPathToFollow) throws InterruptedException { + for (CursorPoint translationPoint : cursorPathToFollow.getCursorPathPoints()) { + robotMouseMove(calculatePoint(startingCursorPoint, thetaDirectionToMoveCursor, translationPoint)); + Thread.sleep(50); + } + } + + private Point calculatePoint(Point startingCursorPoint, double thetaDirectionToMoveCursor, CursorPoint translationPoint) { + int x = (int) (startingCursorPoint.x + Math.cos(thetaDirectionToMoveCursor) * translationPoint.x); + int y = (int) (startingCursorPoint.y + Math.sin(thetaDirectionToMoveCursor) * translationPoint.y); + return new Point(x, y); + } + + private void robotMouseMove(Point pointToMoveCursorTo) { + robot.mouseMove(pointToMoveCursorTo.x, pointToMoveCursorTo.y); + } + + private CursorPath chooseCursorPathToFollowBasedOnDistance(int distanceToMoveCursor) { + ArrayList cursorPathsWithSameDistance = cursorPathsByDistance.get(distanceToMoveCursor); + return cursorPathsWithSameDistance.get(new Random().nextInt(cursorPathsWithSameDistance.size())); + } + + private int calculateDistanceBetweenPoints(Point a, Point b) { + return (int) (Math.hypot(a.x - b.x, a.y - b.y)); + } + + public double calculateThetaBetweenPoints(Point a, Point b) { + return Math.atan2((b.y - a.y), (b.x - a.x)); + } + + private Point getCurrentCursorPoint() { + return MouseInfo.getPointerInfo().getLocation(); + } + + public void displaycursorPathsByDistance() { for (int i = 0; i < cursorPathsByDistance.size(); i++) { System.out.println("There are " + cursorPathsByDistance.get(i).size() + " CursorPaths of length " + i); - } } } \ No newline at end of file diff --git a/src/CursorPath.java b/src/CursorPath.java index 4cb2c3a..81a88c1 100644 --- a/src/CursorPath.java +++ b/src/CursorPath.java @@ -13,17 +13,19 @@ public class CursorPath { public CursorPath(ArrayList cursorPoints) { - this.pathCursorPoints = deepCopyCursorPoints(cursorPoints); + this.pathCursorPoints = copyCursorPointsWithOffset(cursorPoints); this.pathNumPoints = cursorPoints.size(); this.pathDistance = calculateCursorPathDistance(); this.pathTimespanMilliseconds = calculateCursorPathTimespan(); } - private ArrayList deepCopyCursorPoints(ArrayList cursorPoints) { + private ArrayList copyCursorPointsWithOffset(ArrayList cursorPoints) { ArrayList cursorPointsCopy = new ArrayList(cursorPoints.size()); + CursorPoint startingCursorPoint = cursorPoints.get(0); for (CursorPoint cursorPoint : cursorPoints) { - CursorPoint cursorPointCopy = new CursorPoint(cursorPoint.x, cursorPoint.y, cursorPoint.time); - cursorPointsCopy.add(cursorPointCopy); + CursorPoint offsetCursorPoint = new CursorPoint(cursorPoint.x - startingCursorPoint.x, + cursorPoint.y - startingCursorPoint.y,cursorPoint.time - startingCursorPoint.time); + cursorPointsCopy.add(offsetCursorPoint); } return cursorPointsCopy; } @@ -58,7 +60,7 @@ public class CursorPath { } private boolean isCursorPathDistanceReasonable() { - return (this.pathDistance > 5 && this.pathDistance < 600); + return (this.pathDistance > 5 && this.pathDistance < 1000); } private boolean isCursorPathNumPointsReasonable() { diff --git a/src/CursorTest.java b/src/CursorTest.java new file mode 100644 index 0000000..47b4454 --- /dev/null +++ b/src/CursorTest.java @@ -0,0 +1,41 @@ +import static org.junit.jupiter.api.Assertions.*; + +import java.awt.AWTException; +import java.awt.Point; + +import org.junit.jupiter.api.Test; + +class CursorTest { + + Cursor cursor; + double toleranceMin; + double toleranceMax; + + void initialize() throws AWTException { + cursor = new Cursor(); + toleranceMin = 0.9999; + toleranceMax = 1.0001; + } + + @Test + void testCalculateThetaBetweenPoints() throws AWTException { + initialize(); + Point a = new Point(0, 0); + Point b = new Point(10, 0); + Point c = new Point(0, 10); + Point d = new Point(-10, 0); + Point e = new Point(0, -10); + + assertInRange(cursor.calculateThetaBetweenPoints(a, b), 0.0); + assertInRange(cursor.calculateThetaBetweenPoints(a, c), Math.PI / 2); + assertInRange(cursor.calculateThetaBetweenPoints(a, d), Math.PI); + assertInRange(cursor.calculateThetaBetweenPoints(a, e), - Math.PI / 2); + } + + void assertInRange(double valueToTest, double expectation) { + System.out.println(valueToTest + " expected: " + expectation); + assertTrue((valueToTest <= (expectation * toleranceMax)) && (valueToTest >= (expectation * toleranceMin))); + } + +} + \ No newline at end of file diff --git a/src/ScreenshotAutomator.java b/src/ScreenshotAutomator.java new file mode 100644 index 0000000..0b57af4 --- /dev/null +++ b/src/ScreenshotAutomator.java @@ -0,0 +1,56 @@ +import java.awt.AWTException; +import java.awt.Rectangle; +import java.awt.Robot; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.Scanner; + +import javax.imageio.ImageIO; + +public class ScreenshotAutomator { + + public String screenshotOutputDirectory; + public Robot robot; + public Rectangle screenAreaToCapture; + + public ScreenshotAutomator(String screenshotOutputDirectory) throws AWTException { + this.screenshotOutputDirectory = screenshotOutputDirectory; + this.screenAreaToCapture = new Rectangle(0, 0, 1920 / 2, 1080); + this.robot = new Robot(); + } + + public void captureEveryNSeconds(int n) throws IOException, InterruptedException { + for (int i = 0; i < 20; i++) { + captureScreenshot(i); + System.out.println("Created image: " + getImageName(i)); + Thread.sleep(n * 1000); + } + } + + public void captureOnKeyboardInput() throws IOException, InterruptedException { + int counter = 0; + Scanner scanner = new Scanner(System.in); + while (true) { + captureScreenshot(counter); + scanner.nextLine(); + System.out.println("Created image: " + getImageName(counter)); + counter++; + } + } + + private void captureScreenshot(int counter) throws IOException { + BufferedImage image = robot.createScreenCapture(this.screenAreaToCapture); + ImageIO.write(image, "png", new File(getImageName(counter))); + } + + private String getImageName(int counter) { + return screenshotOutputDirectory + "screenshot" + counter + ".png"; + } + + public static void main(String[] args) throws Exception + { + ScreenshotAutomator screenshotAutomator = new ScreenshotAutomator("/home/dpapp/Desktop/RunescapeAIPics/"); + screenshotAutomator.captureOnKeyboardInput(); + } +} diff --git a/src/main.java b/src/main.java index f3749e9..a664f88 100644 --- a/src/main.java +++ b/src/main.java @@ -1,13 +1,16 @@ +import java.awt.AWTException; +import java.awt.Point; import java.net.URL; public class main { - public static void main(String[] args) { + public static void main(String[] args) throws AWTException, InterruptedException { // TODO Auto-generated method stub Cursor cursor = new Cursor(); - cursor.displaycursorPathsByDistance(); + cursor.moveCursorToCoordinates(new Point(620, 420)); + //cursor.displaycursorPathsByDistance(); - System.out.println("Finished..."); + //System.out.println("Finished..."); } }