1
0
mirror of https://github.com/davpapp/PowerMiner synced 2024-10-31 15:45:02 -04:00

Added moveMouse code, but not yet working

This commit is contained in:
davpapp 2018-01-30 08:15:20 -05:00
parent 9b0539b53c
commit b604c8a70d
10 changed files with 165 additions and 12 deletions

Binary file not shown.

Binary file not shown.

BIN
bin/CursorTest.class Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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<ArrayList<CursorPath>> cursorPathsByDistance;
public Cursor() {
public Cursor() throws AWTException {
ArrayList<CursorPath> 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<CursorPath> 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);
}
}
}

View File

@ -13,17 +13,19 @@ public class CursorPath {
public CursorPath(ArrayList<CursorPoint> cursorPoints)
{
this.pathCursorPoints = deepCopyCursorPoints(cursorPoints);
this.pathCursorPoints = copyCursorPointsWithOffset(cursorPoints);
this.pathNumPoints = cursorPoints.size();
this.pathDistance = calculateCursorPathDistance();
this.pathTimespanMilliseconds = calculateCursorPathTimespan();
}
private ArrayList<CursorPoint> deepCopyCursorPoints(ArrayList<CursorPoint> cursorPoints) {
private ArrayList<CursorPoint> copyCursorPointsWithOffset(ArrayList<CursorPoint> cursorPoints) {
ArrayList<CursorPoint> cursorPointsCopy = new ArrayList<CursorPoint>(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() {

41
src/CursorTest.java Normal file
View File

@ -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)));
}
}

View File

@ -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();
}
}

View File

@ -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...");
}
}