1
0
mirror of https://github.com/davpapp/PowerMiner synced 2024-11-15 14:05:04 -05:00

Tested Dropping, improved randomization and SMART drop

This commit is contained in:
davpapp 2018-02-19 16:12:25 -05:00
parent ee0efadb37
commit f2d9d52711
20 changed files with 95 additions and 75 deletions

View File

@ -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,51 +163,46 @@ 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<CursorPath> cursorPathsWithSameDistance = cursorPathsByDistance.get(newDistanceToMoveCursor);
CursorPath scaledCursorPath = cursorPathsWithSameDistance.get(random.nextInt(cursorPathsWithSameDistance.size())).getScaledCopyOfCursorPath(scaleToFactorBy);
CursorPath randomlyChosenCursorPath = cursorPathsWithSameDistance.get(random.nextInt(cursorPathsWithSameDistance.size()));
if (newDistanceToMoveCursor == distanceToMoveCursor) {
return randomlyChosenCursorPath;
}
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;
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;
}
if (distanceToMoveCursor - Math.abs(offset) - 1 < 0) {
reachedMinimumLimit = true;
}
if (reachedMaximumLimit && reachedMinimumLimit) {
throw new Exception("No paths exist.");
for (int i = distanceToMoveCursor; i < 2203; i++) {
if (cursorPathsByDistance.get(i).size() > 0) {
closestLongerPathLength = i;
break;
}
}
if (offset < 0) {
if (!reachedMaximumLimit) {
offset = -offset + 1;
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 {
offset--;
return (Math.abs(distanceToMoveCursor - closestShorterPathLength) <= Math.abs(distanceToMoveCursor - closestLongerPathLength)) ? closestShorterPathLength : closestLongerPathLength;
}
}
else {
if (!reachedMinimumLimit) {
offset = -offset - 1;
}
else {
offset++;
}
}
}
return distanceToMoveCursor + offset;
}
private double getScaleToFactorBy(int newDistanceToMoveCursor, int distanceToMoveCursor) {
return (1.0 * distanceToMoveCursor / newDistanceToMoveCursor);
@ -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();
}
}

View File

@ -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<CursorPoint> getCursorPathPoints() {

View File

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

View File

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

View File

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

View File

@ -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

View File

@ -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

View File

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

View File

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

View File

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

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.

Binary file not shown.

Binary file not shown.