Refactoring. Starting work with OpenCV

This commit is contained in:
davpapp 2018-02-15 06:27:12 -05:00
parent 90c8cbb33b
commit 1a03890095
18 changed files with 172 additions and 115 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
bin/OpenCVTest.class Normal file

Binary file not shown.

Binary file not shown.

View File

@ -18,14 +18,10 @@ import java.util.regex.Pattern;
public class Cursor { public class Cursor {
public static final int NUMBER_OF_DISTANCES = 1000; public static final int NUMBER_OF_DISTANCES = 2203; // For 1080p screen
public static final int MINIMUM_CLICK_LENGTH = 120; public static final int MINIMUM_CLICK_LENGTH = 120;
public static final int MAXIMUM_CLICK_LENGTH = 240; public static final int MAXIMUM_CLICK_LENGTH = 240;
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;
private Robot robot; private Robot robot;
private Randomizer randomizer; private Randomizer randomizer;
private Random random; private Random random;
@ -96,38 +92,39 @@ public class Cursor {
leftClickCursor(); leftClickCursor();
} }
public void moveAndRightClickAtCoordinates(Point goalPoint) throws InterruptedException {
moveCursorToCoordinates(goalPoint);
rightClickCursor();
}
public Point moveAndLeftClickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws InterruptedException { public Point moveAndLeftClickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws InterruptedException {
Point randomizedGoalPoint = randomizePoint(goalPoint, xTolerance, yTolerance); Point randomizedGoalPoint = randomizePoint(goalPoint, xTolerance, yTolerance);
moveCursorToCoordinates(randomizedGoalPoint); moveCursorToCoordinates(randomizedGoalPoint);
leftClickCursor(); leftClickCursor();
return randomizedGoalPoint; // Return the point in case we need precise movement afterwards return randomizedGoalPoint; // Return the point we moved to in case we need precise movement afterwards
}
public void moveAndRightClickAtCoordinates(Point goalPoint) throws InterruptedException {
moveCursorToCoordinates(goalPoint);
rightClickCursor();
} }
public Point moveAndRightlickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws InterruptedException { public Point moveAndRightlickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws InterruptedException {
Point randomizedGoalPoint = randomizePoint(goalPoint, xTolerance, yTolerance); Point randomizedGoalPoint = randomizePoint(goalPoint, xTolerance, yTolerance);
moveCursorToCoordinates(randomizedGoalPoint); moveCursorToCoordinates(randomizedGoalPoint);
rightClickCursor(); rightClickCursor();
return randomizedGoalPoint; // Return the point in case we need precise movement afterwards return randomizedGoalPoint; // Return the point we moved to in case we need precise movement afterwards
} }
public void moveCursorToCoordinates(Point goalPoint) throws InterruptedException { public void moveCursorToCoordinates(Point goalPoint) throws InterruptedException {
Point startingPoint = getCurrentCursorPoint(); Point startingPoint = getCurrentCursorPoint();
int distanceToMoveCursor = getDistanceBetweenPoints(startingPoint, goalPoint); int distanceToMoveCursor = getDistanceBetweenPoints(startingPoint, goalPoint);
double angleToRotateCursorPathTo = getThetaBetweenPoints(startingPoint, goalPoint);
System.out.println("R:" + distanceToMoveCursor + ", theta:" + angleToRotateCursorPathTo);
if (distanceToMoveCursor == 0) { if (distanceToMoveCursor == 0) {
return; return;
} }
double angleToMoveCursor = getThetaBetweenPoints(startingPoint, goalPoint);
CursorPath cursorPathToFollow = chooseCursorPathToFollowBasedOnDistance(distanceToMoveCursor); CursorPath cursorPathWithDistanceSet = chooseCursorPathToFollowBasedOnDistance(distanceToMoveCursor);
CursorPath cursorPathWithDistanceAndAngleSet = cursorPathWithDistanceSet.getRotatedCopyOfCursorPath(angleToRotateCursorPathTo);
double angleToTranslatePathBy = angleToMoveCursor - cursorPathToFollow.getCursorPathTheta(); System.out.println("Rotated the points: ");
//cursorPathWithDistanceAndAngleSet.displayCursorPoints();
followCursorPath(startingCursorPoint, angleToTranslatePathBy, cursorPathToFollow); followCursorPath(cursorPathWithDistanceAndAngleSet, startingPoint);
} }
public int getDistanceBetweenPoints(Point startingPoint, Point goalPoint) { public int getDistanceBetweenPoints(Point startingPoint, Point goalPoint) {
@ -135,14 +132,14 @@ public class Cursor {
} }
public double getThetaBetweenPoints(Point startingPoint, Point goalPoint) { public double getThetaBetweenPoints(Point startingPoint, Point goalPoint) {
return Math.atan2(goalPoint.x - startingPoint.x, goalPoint.y - startingPoint.y); return Math.atan2((goalPoint.x - startingPoint.x), (goalPoint.y - startingPoint.y));
} }
private void followCursorPath(Point startingCursorPoint, double angleToTranslatePathBy, CursorPath cursorPathToFollow) throws InterruptedException { private void followCursorPath(CursorPath cursorPathToFollow, Point startingPoint) throws InterruptedException {
for (CursorPoint untranslatedCursorPoint : cursorPathToFollow.getCursorPathPoints()) { for (CursorPoint cursorPoint : cursorPathToFollow.getCursorPathPoints()) {
Point translatedPointToClick = translatePoint(startingCursorPoint, angleToTranslatePathBy, untranslatedCursorPoint); Point translatedPointToClick = new Point(cursorPoint.x + startingPoint.x, cursorPoint.y + startingPoint.y);
robotMouseMove(translatedPointToClick); robotMouseMove(translatedPointToClick);
Thread.sleep(untranslatedCursorPoint.delay); Thread.sleep(cursorPoint.delay);
} }
} }
@ -153,11 +150,13 @@ public class Cursor {
private CursorPath chooseCursorPathToFollowBasedOnDistance(int distanceToMoveCursor) { private CursorPath chooseCursorPathToFollowBasedOnDistance(int distanceToMoveCursor) {
int newDistanceToMoveCursor = findNearestPathLengthThatExists(distanceToMoveCursor); int newDistanceToMoveCursor = findNearestPathLengthThatExists(distanceToMoveCursor);
double scaleToFactorBy = getScaleToFactorBy(newDistanceToMoveCursor, distanceToMoveCursor); double scaleToFactorBy = getScaleToFactorBy(newDistanceToMoveCursor, distanceToMoveCursor);
System.out.println("new distance to follow cursor is: " + newDistanceToMoveCursor);
ArrayList<CursorPath> cursorPathsWithSameDistance = cursorPathsByDistance.get(newDistanceToMoveCursor); ArrayList<CursorPath> cursorPathsWithSameDistance = cursorPathsByDistance.get(newDistanceToMoveCursor);
int indexOfRandomPathToFollow = random.nextInt(cursorPathsWithSameDistance.size()); int indexOfRandomPathToFollow = random.nextInt(cursorPathsWithSameDistance.size());
ArrayList<CursorPoint> scaledCursorPath = cursorPathsWithSameDistance.get(indexOfRandomPathToFollow).getScaledCopyOfCursorPath(scaleToFactorBy); //CursorPath scaledCursorPath = cursorPathsWithSameDistance.get(indexOfRandomPathToFollow).getScaledCopyOfCursorPath(scaleToFactorBy);
return scaledCursorPath; //System.out.println("Chose the following path: ");
//scaledCursorPath.displayCursorPoints();
return cursorPathsByDistance.get(newDistanceToMoveCursor).get(indexOfRandomPathToFollow);//scaledCursorPath;
} }
private int findNearestPathLengthThatExists(int distanceToMoveCursor) { private int findNearestPathLengthThatExists(int distanceToMoveCursor) {
@ -188,7 +187,7 @@ public class Cursor {
} }
public void displayCursorPaths() { public void displayCursorPaths() {
for (int i = 0; i < 200; i++) { for (int i = 0; i < 1000; i++) {
System.out.println("There are " + cursorPathsByDistance.get(i).size() + " paths of size " + i); System.out.println("There are " + cursorPathsByDistance.get(i).size() + " paths of size " + i);
} }
System.out.println("--------------"); System.out.println("--------------");

View File

@ -26,8 +26,8 @@ public class CursorDataFileParser {
String line; String line;
CursorPoint lastCursorPoint = new CursorPoint(0, 0, 0); CursorPoint lastCursorPoint = new CursorPoint(0, 0, 0);
int numberOfRepeats = 0; int numberOfRepeats = 0;
ArrayList<CursorPoint> currentCursorPath = new ArrayList<CursorPoint>(); ArrayList<CursorPoint> currentCursorPoints = new ArrayList<CursorPoint>();
currentCursorPath.add(lastCursorPoint); currentCursorPoints.add(lastCursorPoint);
while ((line = bufferedReader.readLine()) != null) { while ((line = bufferedReader.readLine()) != null) {
if (lineMatchesPattern(line)) { if (lineMatchesPattern(line)) {
@ -35,14 +35,14 @@ public class CursorDataFileParser {
if (cursorPointsHaveEqualCoordinates(newCursorPoint, lastCursorPoint)) { if (cursorPointsHaveEqualCoordinates(newCursorPoint, lastCursorPoint)) {
numberOfRepeats++; numberOfRepeats++;
if (numberOfRepeats == 20) { if (numberOfRepeats == 20) {
CursorPath newCursorPath = new CursorPath(currentCursorPath); CursorPath newCursorPath = new CursorPath(currentCursorPoints);
cursorPaths.add(newCursorPath); cursorPaths.add(newCursorPath);
currentCursorPath.clear(); currentCursorPoints.clear();
} }
} }
else { else {
numberOfRepeats = 0; numberOfRepeats = 0;
currentCursorPath.add(newCursorPoint); currentCursorPoints.add(newCursorPoint);
lastCursorPoint = newCursorPoint; lastCursorPoint = newCursorPoint;
} }
} }

View File

@ -19,22 +19,32 @@ public class CursorPath {
this.timespan = calculateCursorPathTimespan(); this.timespan = calculateCursorPathTimespan();
} }
public CursorPath(ArrayList<CursorPoint> cursorPoints, boolean setInitializiationOff)
{
this.cursorPoints = cursorPoints;
this.distance = calculateCursorPathDistance();
this.theta = calculateCursorPathTheta();
this.timespan = calculateCursorPathTimespan();
}
private ArrayList<CursorPoint> initializePathOfCursorPoints(ArrayList<CursorPoint> cursorPoints) { private ArrayList<CursorPoint> initializePathOfCursorPoints(ArrayList<CursorPoint> cursorPoints) {
CursorPoint startingCursorPoint = cursorPoints.get(0); CursorPoint startingCursorPoint = cursorPoints.get(0);
ArrayList<CursorPoint> translatedCursorPoints = getTranslatedCopyOfCursorPath(cursorPoints, startingCursorPoint); ArrayList<CursorPoint> translatedCursorPoints = getTranslatedListOfCursorPoints(cursorPoints, startingCursorPoint);
ArrayList<CursorPoint> normalizedDelayCursorPoints = getNormalizedDelayCopyOfCursorPath(translatedCursorPoints); ArrayList<CursorPoint> normalizedDelayCursorPoints = getNormalizedDelayListOfCursorPoints(translatedCursorPoints);
return normalizedDelayCursorPoints; return normalizedDelayCursorPoints;
} }
private CursorPath getTranslatedCopyOfCursorPath(ArrayList<CursorPoint> cursorPoints, CursorPoint cursorPointToTranslateBy) { // util
ArrayList<CursorPoint> offsetCursorPath = new ArrayList<CursorPoint>(); private ArrayList<CursorPoint> getTranslatedListOfCursorPoints(ArrayList<CursorPoint> cursorPoints, CursorPoint cursorPointToTranslateBy) {
ArrayList<CursorPoint> translatedCursorPath = new ArrayList<CursorPoint>();
for (CursorPoint cursorPoint : cursorPoints) { for (CursorPoint cursorPoint : cursorPoints) {
offsetCursorPath.add(cursorPoint.getCursorPointTranslatedBy(cursorPointToTranslateBy)); translatedCursorPath.add(cursorPoint.getCursorPointTranslatedBy(cursorPointToTranslateBy));
} }
return new CursorPath(offsetCursorPath); return translatedCursorPath;
} }
private CursorPath getNormalizedDelayCopyOfCursorPath(ArrayList<CursorPoint> cursorPoints) { //util
private ArrayList<CursorPoint> getNormalizedDelayListOfCursorPoints(ArrayList<CursorPoint> cursorPoints) {
ArrayList<CursorPoint> normalizedDelayCursorPoints = new ArrayList<CursorPoint>(); ArrayList<CursorPoint> normalizedDelayCursorPoints = new ArrayList<CursorPoint>();
for (int i = 0; i < cursorPoints.size() - 1; i++) { for (int i = 0; i < cursorPoints.size() - 1; i++) {
CursorPoint cursorPoint = cursorPoints.get(i); CursorPoint cursorPoint = cursorPoints.get(i);
@ -42,24 +52,27 @@ public class CursorPath {
normalizedDelayCursorPoints.add(cursorPoint.getCursorPointWithNewDelay(nextCursorPoint.delay - cursorPoint.delay)); normalizedDelayCursorPoints.add(cursorPoint.getCursorPointWithNewDelay(nextCursorPoint.delay - cursorPoint.delay));
} }
normalizedDelayCursorPoints.add(cursorPoints.get(cursorPoints.size() - 1).getCursorPointWithNewDelay(0)); normalizedDelayCursorPoints.add(cursorPoints.get(cursorPoints.size() - 1).getCursorPointWithNewDelay(0));
return new CursorPath(normalizedDelayCursorPoints); return normalizedDelayCursorPoints;
} }
//util
public CursorPath getScaledCopyOfCursorPath(ArrayList<CursorPoint> cursorPoints, double factorToScaleBy) { public CursorPath getScaledCopyOfCursorPath(double factorToScaleBy) {
ArrayList<CursorPoint> scaledCursorPath = new ArrayList<CursorPoint>(); ArrayList<CursorPoint> scaledCursorPath = new ArrayList<CursorPoint>();
for (CursorPoint cursorPoint : cursorPoints) { for (CursorPoint cursorPoint : this.cursorPoints) {
scaledCursorPath.add(cursorPoint.getCursorPointScaledBy(factorToScaleBy)); scaledCursorPath.add(cursorPoint.getCursorPointScaledBy(factorToScaleBy));
} }
return new CursorPath(scaledCursorPath); return new CursorPath(scaledCursorPath, true);
} }
public CursorPath getRotatedCopyOfCursorPath(ArrayList<CursorPoint> cursorPoints, double angleToRotateBy) { //util
public CursorPath getRotatedCopyOfCursorPath(double angleToRotateTo) {
ArrayList<CursorPoint> rotatedCursorPath = new ArrayList<CursorPoint>(); ArrayList<CursorPoint> rotatedCursorPath = new ArrayList<CursorPoint>();
for (CursorPoint cursorPoint : cursorPoints) { double angleToRotateBy = this.theta - angleToRotateTo;
System.out.println("Rotating points by: " + angleToRotateBy);
for (CursorPoint cursorPoint : this.cursorPoints) {
rotatedCursorPath.add(cursorPoint.getCursorPointRotatedBy(angleToRotateBy)); rotatedCursorPath.add(cursorPoint.getCursorPointRotatedBy(angleToRotateBy));
} }
return new CursorPath(rotatedCursorPath); return new CursorPath(rotatedCursorPath, true);
} }
private int calculateCursorPathTimespan() { private int calculateCursorPathTimespan() {
@ -71,12 +84,11 @@ public class CursorPath {
} }
private int calculateCursorPathDistance() { private int calculateCursorPathDistance() {
return (int) (getStartingCursorPoint().getDistanceFrom(getEndingCursorPoint())); return (int) (getEndingCursorPoint().getDistanceFromOrigin());
} }
private double calculateCursorPathTheta() { private double calculateCursorPathTheta() {
CursorPoint endingCursorPoint = getEndingCursorPoint(); return getEndingCursorPoint().getThetaFromOrigin();
return Math.atan2(endingCursorPoint.y, endingCursorPoint.x);
} }
public CursorPoint getStartingCursorPoint() { public CursorPoint getStartingCursorPoint() {
@ -102,7 +114,7 @@ public class CursorPath {
} }
private boolean isCursorPathNumPointsReasonable() { private boolean isCursorPathNumPointsReasonable() {
return (this.cursorPoints.size() > 0 && this.cursorPoints.size() < 50); return (this.cursorPoints.size() > 0 && this.cursorPoints.size() < 80);
} }
public ArrayList<CursorPoint> getCursorPathPoints() { public ArrayList<CursorPoint> getCursorPathPoints() {
@ -122,7 +134,7 @@ public class CursorPath {
for (CursorPoint p : this.cursorPoints) { for (CursorPoint p : this.cursorPoints) {
p.display(); p.display();
} }
System.out.println("Number of points:" + this.cursorPoints.size() + ", Timespan:" + this.timespan); System.out.println("Number of points:" + this.cursorPoints.size() + ", Timespan:" + this.timespan + ", Distance: " + this.distance + ", Theta: " + this.theta);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~ End of Path ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~ End of Path ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
} }
} }

View File

@ -10,21 +10,30 @@ class CursorPathTest {
@Test @Test
void testCursorPathOffset() { void testCursorPaths() {
initializeCursorPath(); initializeCursorPath();
for (CursorPath cursorPath : cursorPaths) { for (CursorPath cursorPath : cursorPaths) {
cursorPath.displayCursorPoints();
testCursorPathStartsAtOrigin(cursorPath); testCursorPathStartsAtOrigin(cursorPath);
testEndingCursorPointInCursorPathHasZeroDelay(cursorPath); testEndingCursorPointInCursorPathHasZeroDelay(cursorPath);
testCursorPathDistance(cursorPath); testCursorPathDistance(cursorPath);
testCursorPathTheta(cursorPath); testCursorPathTheta(cursorPath);
//testCursorPathLocations(cursorPath);
testCursorPathRotation(cursorPath, Math.PI); testCursorPathRotation(cursorPath, Math.PI);
testCursorPathRotation(cursorPath, 7 / 4 * Math.PI);
testCursorPathScaling(cursorPath, 1.15); testCursorPathScaling(cursorPath, 1.15);
cursorPath.displayCursorPoints();
testCopyTime(cursorPath);
} }
} }
private void initializeCursorPath() {
CursorDataFileParser cursorDataFileParser = new CursorDataFileParser("/home/dpapp/eclipse-workspace/RunescapeAI/testfiles/cursorPathTest.txt");
this.cursorPaths = cursorDataFileParser.getArrayListOfCursorPathsFromFile();
}
private void testCursorPathStartsAtOrigin(CursorPath cursorPath) { private void testCursorPathStartsAtOrigin(CursorPath cursorPath) {
CursorPoint startingCursorPoint = cursorPath.getStartingCursorPoint(); CursorPoint startingCursorPoint = cursorPath.getStartingCursorPoint();
assertTrue(startingCursorPoint.x == 0 && startingCursorPoint.y == 0); assertTrue(startingCursorPoint.x == 0 && startingCursorPoint.y == 0);
@ -32,63 +41,58 @@ class CursorPathTest {
private void testEndingCursorPointInCursorPathHasZeroDelay(CursorPath cursorPath) { private void testEndingCursorPointInCursorPathHasZeroDelay(CursorPath cursorPath) {
CursorPoint endingCursorPoint = cursorPath.getEndingCursorPoint(); CursorPoint endingCursorPoint = cursorPath.getEndingCursorPoint();
assertEquals(endingCursorPoint.delay, 0); assertEquals(0, endingCursorPoint.delay);
} }
private void testCursorPathDistance(CursorPath cursorPath) { private void testCursorPathDistance(CursorPath cursorPath) {
int distance = cursorPath.getCursorPathDistance(); int cursorPathDistance = cursorPath.getCursorPathDistance();
CursorPoint startingCursorPoint = cursorPath.getStartingCursorPoint(); CursorPoint startingCursorPoint = cursorPath.getStartingCursorPoint();
CursorPoint endingCursorPoint = cursorPath.getEndingCursorPoint(); CursorPoint endingCursorPoint = cursorPath.getEndingCursorPoint();
int actualDistance = (int) Math.hypot(startingCursorPoint.x - endingCursorPoint.x, startingCursorPoint.y - endingCursorPoint.y); startingCursorPoint.display();
assertEquals(distance, actualDistance); endingCursorPoint.display();
int expectedDistance = (int) Math.hypot(startingCursorPoint.x - endingCursorPoint.x, startingCursorPoint.y - endingCursorPoint.y);
assertEquals(expectedDistance, cursorPathDistance);
} }
private void testCursorPathTheta(CursorPath cursorPath) { private void testCursorPathTheta(CursorPath cursorPath) {
double theta = cursorPath.getCursorPathTheta(); double theta = cursorPath.getCursorPathTheta();
CursorPoint startingCursorPoint = cursorPath.getStartingCursorPoint();
CursorPoint endingCursorPoint = cursorPath.getEndingCursorPoint(); CursorPoint endingCursorPoint = cursorPath.getEndingCursorPoint();
double actualTheta = Math.atan2(endingCursorPoint.y, endingCursorPoint.x); double actualTheta = Math.atan2(endingCursorPoint.x - startingCursorPoint.x, endingCursorPoint.y - startingCursorPoint.y);
assertEquals(theta, actualTheta); assertEquals(theta, actualTheta);
} }
/*private void testCursorPathLocations(CursorPath cursorPath) {
}*/
void testCursorPathRotation(CursorPath cursorPath, double angleToRotateBy) { void testCursorPathRotation(CursorPath cursorPath, double angleToRotateTo) {
CursorPath rotatedCursorPath = cursorPath.getRotatedCopyOfCursorPath(angleToRotateTo);
assertEquals(angleToRotateTo, rotatedCursorPath.getCursorPathTheta());
ArrayList<CursorPoint> cursorPoints = cursorPath.getCursorPathPoints(); ArrayList<CursorPoint> cursorPoints = cursorPath.getCursorPathPoints();
ArrayList<CursorPoint> rotatedCursorPoints = cursorPath.getRotatedCopyOfCursorPath(angleToRotateBy); ArrayList<CursorPoint> rotatedCursorPoints = rotatedCursorPath.getCursorPathPoints();
assertEquals(cursorPoints.size(), rotatedCursorPoints.size()); assertEquals(cursorPoints.size(), rotatedCursorPoints.size());
for (int i = 1; i < cursorPoints.size(); i++) {
double originalThetaOfCursorPoint = cursorPoints.get(i).getTheta();
double rotatedThetaOfCursorPoint = rotatedCursorPoints.get(i).getTheta();
System.out.println((originalThetaOfCursorPoint + angleToRotateBy) % (Math.PI) + "," + rotatedThetaOfCursorPoint);
//assertInRangeByAbsoluteValue(originalThetaOfCursorPoint + angleToRotateBy, rotatedThetaOfCursorPoint, 3);
}
}
void testCursorPathScaling(CursorPath cursorPath, double factorToScaleBy) {
ArrayList<CursorPoint> cursorPoints = cursorPath.getCursorPathPoints();
ArrayList<CursorPoint> scaledCursorPoints = cursorPath.getScaledCopyOfCursorPath(factorToScaleBy);
assertEquals(cursorPoints.size(), scaledCursorPoints.size());
CursorPoint startingPoint = cursorPoints.get(0);
for (int i = 0; i < cursorPoints.size(); i++) {
double originalDistanceOfCursorPoint = cursorPoints.get(i).getDistanceFrom(startingPoint);
double scaledDistanceOfCursorPoint = scaledCursorPoints.get(i).getDistanceFrom(startingPoint);
assertInRangeByAbsoluteValue(originalDistanceOfCursorPoint * factorToScaleBy, scaledDistanceOfCursorPoint, 3);
}
}
void initializeCursorPath() {
CursorDataFileParser cursorDataFileParser = new CursorDataFileParser("/home/dpapp/eclipse-workspace/RunescapeAI/testfiles/cursorPathTest.txt");
this.cursorPaths = cursorDataFileParser.getArrayListOfCursorPathsFromFile();
} }
void assertInRangeByRatio(double expected, double actual, double toleranceRatio) { void testCopyTime(CursorPath cursorPath) {
assertTrue((actual <= (expected * (1.0 + toleranceRatio))) && (actual >= (expected * (1.0 - toleranceRatio)))); CursorPath rotatedCursorPath = cursorPath.getRotatedCopyOfCursorPath(2.3 / 9.0 * Math.PI);
ArrayList<CursorPoint> cursorPoints = cursorPath.getCursorPathPoints();
ArrayList<CursorPoint> rotatedCursorPoints = rotatedCursorPath.getCursorPathPoints();
assertEquals(cursorPoints.size(), rotatedCursorPoints.size());
for (int i = 0; i < cursorPoints.size(); i++) {
assertEquals(cursorPoints.get(i).delay, rotatedCursorPoints.get(i).delay);
}
}
void testCursorPathScaling(CursorPath cursorPath, double factorToScaleBy) {
CursorPath scaledCursorPath = cursorPath.getScaledCopyOfCursorPath(factorToScaleBy);
ArrayList<CursorPoint> cursorPoints = cursorPath.getCursorPathPoints();
ArrayList<CursorPoint> scaledCursorPoints = scaledCursorPath.getCursorPathPoints();
assertEquals(cursorPoints.size(), scaledCursorPoints.size());
} }
void assertInRangeByAbsoluteValue(double valueToTest, double expectation, double toleranceAbsolute) { void assertInRangeByTolerance(double expected, double actual, double tolerance) {
assertTrue((valueToTest <= (expectation + toleranceAbsolute)) && (valueToTest >= (expectation * (1 - toleranceAbsolute)))); assertTrue((actual <= (expected + tolerance)) && (actual >= (expected - tolerance)));
} }
} }

View File

@ -12,26 +12,26 @@ public class CursorPoint {
this.delay = delay; this.delay = delay;
} }
public double getDistanceFrom(CursorPoint b) { public double getDistanceFromOrigin() {
return Math.hypot(this.x - b.x, this.y - b.y); return Math.hypot(this.x, this.y);
} }
public double getTheta() { public double getThetaFromOrigin() {
return Math.atan2(this.y, this.x); return Math.atan2(this.x, this.y);
} }
public CursorPoint getCursorPointTranslatedBy(CursorPoint startingCursorPoint) { public CursorPoint getCursorPointTranslatedBy(CursorPoint startingCursorPoint) {
return new CursorPoint(this.x - startingCursorPoint.x, this.y - startingCursorPoint.y, this.delay - startingCursorPoint.delay); return new CursorPoint(x - startingCursorPoint.x, y - startingCursorPoint.y, delay - startingCursorPoint.delay);
} }
public CursorPoint getCursorPointScaledBy(double scaleFactor) { public CursorPoint getCursorPointScaledBy(double scaleFactor) {
return (new CursorPoint((int) (this.x * scaleFactor), (int) (this.y * scaleFactor), (int) (this.delay * scaleFactor))); return (new CursorPoint((int) (this.x * scaleFactor), (int) (this.y * scaleFactor), (int) (delay * scaleFactor)));
} }
public CursorPoint getCursorPointRotatedBy(double angleOfRotation) { public CursorPoint getCursorPointRotatedBy(double angleOfRotation) {
int rotatedX = (int) (Math.cos(angleOfRotation) * this.x - Math.sin(angleOfRotation) * this.y); int rotatedX = (int) (Math.cos(angleOfRotation) * this.x - Math.sin(angleOfRotation) * this.y);
int rotatedY = (int) (Math.sin(angleOfRotation) * this.x + Math.cos(angleOfRotation) * this.y); int rotatedY = (int) (Math.sin(angleOfRotation) * this.x + Math.cos(angleOfRotation) * this.y);
return (new CursorPoint(rotatedX, rotatedY, this.delay)); return (new CursorPoint(rotatedX, rotatedY, delay));
} }
public CursorPoint getCursorPointWithNewDelay(int delay) { public CursorPoint getCursorPointWithNewDelay(int delay) {

View File

@ -5,17 +5,34 @@ import org.junit.jupiter.api.Test;
class CursorPointTest { class CursorPointTest {
@Test @Test
void testDistanceFrom() { void testDistanceFromOrigin() {
CursorPoint a = new CursorPoint(0, 0, 0); CursorPoint a = new CursorPoint(0, 0, 0);
CursorPoint b = new CursorPoint(3, 4, 0); CursorPoint b = new CursorPoint(3, 4, 0);
CursorPoint c = new CursorPoint(500, 750, 0); CursorPoint c = new CursorPoint(500, 750, 0);
CursorPoint d = new CursorPoint(284, 848, 0); CursorPoint d = new CursorPoint(284, 848, 0);
assertTrue(withinRangeByRatio(a.getDistanceFrom(b), 5, 0.0001)); assertEquals(0, a.getDistanceFromOrigin());
assertTrue(withinRangeByRatio(a.getDistanceFrom(c), 901.387818866, 0.0001)); assertTrue(withinRangeByRatio(a.getDistanceFromOrigin(), 901.387818866, 0.0001));
assertTrue(withinRangeByRatio(a.getDistanceFrom(d), 894.293016857, 0.0001)); assertTrue(withinRangeByRatio(a.getDistanceFromOrigin(), 894.293016857, 0.0001));
assertTrue(withinRangeByRatio(b.getDistanceFrom(c), 896.395560007, 0.0001)); assertTrue(withinRangeByRatio(b.getDistanceFromOrigin(), 896.395560007, 0.0001));
assertTrue(withinRangeByRatio(c.getDistanceFrom(d), 237.191905427, 0.0001)); assertTrue(withinRangeByRatio(c.getDistanceFromOrigin(), 237.191905427, 0.0001));
}
@Test
void testThetaFromOrigin() {
CursorPoint a = new CursorPoint(0, 0, 0);
CursorPoint b = new CursorPoint(3, 4, 0);
CursorPoint c = new CursorPoint(500, 750, 0);
CursorPoint d = new CursorPoint(284, 848, 0);
CursorPoint e = new CursorPoint(10, 0, 0);
CursorPoint f = new CursorPoint(0, 10, 0);
assertEquals(0, a.getThetaFromOrigin());
assertEquals(0.6435011087932844, b.getThetaFromOrigin());
assertEquals(0.5880026035475675, c.getThetaFromOrigin());
assertEquals(0.32316498061040844, d.getThetaFromOrigin());
assertEquals(1.5707963267948966, e.getThetaFromOrigin());
assertEquals(0.0, f.getThetaFromOrigin());
} }
@Test @Test
@ -51,6 +68,8 @@ class CursorPointTest {
assertTrue(h.x == -428 && h.y == -321); assertTrue(h.x == -428 && h.y == -321);
} }
boolean withinRangeByRatio(double actual, double expectation, double toleranceRatio) { boolean withinRangeByRatio(double actual, double expectation, double toleranceRatio) {
return ((actual <= (expectation * (1 + toleranceRatio))) && (actual >= (expectation * (1 - toleranceRatio)))); return ((actual <= (expectation * (1 + toleranceRatio))) && (actual >= (expectation * (1 - toleranceRatio))));
} }

View File

@ -12,23 +12,28 @@ class CursorTest {
void initialize() throws AWTException { void initialize() throws AWTException {
cursor = new Cursor(); cursor = new Cursor();
cursorTolerance = 3; cursorTolerance = 5;
} }
@Test @Test
void testMoveCursorToCoordinatesHelper() throws InterruptedException, AWTException { void testMoveCursorToCoordinatesHelper() throws InterruptedException, AWTException {
initialize(); initialize();
cursor.displayCursorPaths();
testThetaBetweenPoints();
testMoveCursorToCoordinates(); testMoveCursorToCoordinates();
testRightClickCursor(); //testRightClickCursor();
} }
@Test
void testThetaBetweenPoints() { void testThetaBetweenPoints() {
Point a = new Point(0, 0); Point a = new Point(0, 0);
Point b = new Point(10, 0); Point b = new Point(10, 0);
Point c = new Point(10, 10); Point c = new Point(10, 10);
Point d = new Point(20, 10); Point d = new Point(20, 10);
assertEquals(cursor.getThetaBetweenPoints(a, b), 0); assertEquals(degreesToRadians(90), cursor.getThetaBetweenPoints(a, b));
assertEquals(degreesToRadians(45), cursor.getThetaBetweenPoints(a, c));
assertEquals(1.1071487177940904, cursor.getThetaBetweenPoints(a, d));
assertEquals(degreesToRadians(45), cursor.getThetaBetweenPoints(b, d));
} }
void testMoveCursorToCoordinates() throws InterruptedException { void testMoveCursorToCoordinates() throws InterruptedException {
@ -52,7 +57,7 @@ class CursorTest {
testMoveCursorToCoordinates(a, g); testMoveCursorToCoordinates(a, g);
} }
void testRightClickCursor() throws InterruptedException { /*void testRightClickCursor() throws InterruptedException {
Point a = new Point(375, 600); Point a = new Point(375, 600);
Point b = new Point(952, 603); Point b = new Point(952, 603);
Point c = new Point(1025, 133); Point c = new Point(1025, 133);
@ -61,21 +66,24 @@ class CursorTest {
testMoveAndRightClickCursor(b, c); testMoveAndRightClickCursor(b, c);
testMoveAndRightClickCursor(c, d); testMoveAndRightClickCursor(c, d);
testMoveAndRightClickCursor(d, a); testMoveAndRightClickCursor(d, a);
} }*/
void testMoveAndRightClickCursor(Point a, Point b) throws InterruptedException { /*void testMoveAndRightClickCursor(Point a, Point b) throws InterruptedException {
cursor.robotMouseMove(a); cursor.robotMouseMove(a);
cursor.moveAndRightClickAtCoordinates(b); cursor.moveAndRightClickAtCoordinates(b);
Point point = cursor.getCurrentCursorPoint(); Point point = cursor.getCurrentCursorPoint();
System.out.println("Cursor ended up on " + point.x + "," + point.y);
verifyCursorIsInCorrectPlace(point, b); verifyCursorIsInCorrectPlace(point, b);
// Way to verify that context menu is open? // Way to verify that context menu is open?
} }*/
void testMoveCursorToCoordinates(Point a, Point b) throws InterruptedException { void testMoveCursorToCoordinates(Point a, Point b) throws InterruptedException {
cursor.robotMouseMove(a); cursor.robotMouseMove(a);
cursor.moveCursorToCoordinates(b); cursor.moveCursorToCoordinates(b);
Point point = cursor.getCurrentCursorPoint(); Point point = cursor.getCurrentCursorPoint();
System.out.println("Cursor ended up on " + point.x + "," + point.y);
verifyCursorIsInCorrectPlace(point, b); verifyCursorIsInCorrectPlace(point, b);
Thread.sleep(500);
} }
void verifyCursorIsInCorrectPlace(Point actualPoint, Point expectedPoint) { void verifyCursorIsInCorrectPlace(Point actualPoint, Point expectedPoint) {
@ -90,6 +98,10 @@ class CursorTest {
void assertInRangeByAbsoluteValue(double valueToTest, double expectation, double toleranceAbsolute) { void assertInRangeByAbsoluteValue(double valueToTest, double expectation, double toleranceAbsolute) {
assertTrue((valueToTest <= (expectation + toleranceAbsolute)) && (valueToTest >= (expectation * (1 - toleranceAbsolute)))); assertTrue((valueToTest <= (expectation + toleranceAbsolute)) && (valueToTest >= (expectation * (1 - toleranceAbsolute))));
} }
private double degreesToRadians(double degrees) {
return degrees * Math.PI / 180.0;
}
} }

11
src/OpenCVTest.java Normal file
View File

@ -0,0 +1,11 @@
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
public class OpenCVTest {
public static void main( String[] args ) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
System.out.println("mat = " + mat.dump());
}
}

View File

@ -8,7 +8,7 @@ public class main {
public static void main(String[] args) throws AWTException, InterruptedException, IOException { public static void main(String[] args) throws AWTException, InterruptedException, IOException {
Cursor cursor = new Cursor(); /*Cursor cursor = new Cursor();
CursorTask cursorTask = new CursorTask(); CursorTask cursorTask = new CursorTask();
Inventory inventory = new Inventory(); Inventory inventory = new Inventory();
@ -19,7 +19,7 @@ public class main {
cursorTask.optimizedDropAllItemsInInventory(cursor, inventory); cursorTask.optimizedDropAllItemsInInventory(cursor, inventory);
} }
Thread.sleep(100); Thread.sleep(100);
} } */
//cursor.moveCursorToCoordinates(new Point(620, 420)); //cursor.moveCursorToCoordinates(new Point(620, 420));
} }