Started manual testing on dropping items

This commit is contained in:
davpapp 2018-02-02 06:27:29 -05:00
parent 9a436b2324
commit f8e4a55283
12 changed files with 126 additions and 39 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.

View File

@ -25,12 +25,6 @@ public class Cursor {
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_WIDTH = 100; // top left corner of main game screen, from top left corner of screen
public static final int GAME_WINDOW_OFFSET_HEIGHT = 81; public static final int GAME_WINDOW_OFFSET_HEIGHT = 81;
public static final int INVENTORY_OFFSET_WIDTH = 649; //top left corner of inventory, fromm top left corner of screen
public static final int INVENTORY_OFFSET_HEIGHT = 286;
public static final int INVENTORY_WIDTH = 820 - 649;// 820
public static final int INVENTORY_HEIGHT = 530 - 286; // 530
private Robot robot; private Robot robot;
private Random random = new Random(); private Random random = new Random();
@ -38,6 +32,7 @@ public class Cursor {
private ArrayList<ArrayList<CursorPath>> cursorPathsByDistance; private ArrayList<ArrayList<CursorPath>> cursorPathsByDistance;
public Cursor() throws AWTException { public Cursor() throws AWTException {
System.out.println("Initializing cursor...");
initializeCursorPathsByDistanceFromFile("/home/dpapp/GhostMouse/coordinates.txt"); initializeCursorPathsByDistanceFromFile("/home/dpapp/GhostMouse/coordinates.txt");
robot = new Robot(); robot = new Robot();
@ -84,12 +79,14 @@ public class Cursor {
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(getRandomClickLength()); Thread.sleep(getRandomClickLength());
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(getRandomClickLength());
} }
public void rightClickCursor() throws InterruptedException { public void rightClickCursor() throws InterruptedException {
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK); robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
Thread.sleep(getRandomClickLength()); Thread.sleep(200 + getRandomClickLength() * 2);
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK); robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
Thread.sleep(getRandomClickLength());
} }
public void moveAndLeftClickAtCoordinates(Point goalPoint) throws InterruptedException { public void moveAndLeftClickAtCoordinates(Point goalPoint) throws InterruptedException {
@ -97,10 +94,22 @@ public class Cursor {
leftClickCursor(); leftClickCursor();
} }
public void moveAndLeftClickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws InterruptedException {
Point randomizedGoalPoint = randomizePoint(goalPoint, xTolerance, yTolerance);
moveCursorToCoordinates(randomizedGoalPoint);
leftClickCursor();
}
public void moveAndRightClickAtCoordinates(Point goalPoint) throws InterruptedException { public void moveAndRightClickAtCoordinates(Point goalPoint) throws InterruptedException {
moveCursorToCoordinates(goalPoint); moveCursorToCoordinates(goalPoint);
rightClickCursor(); rightClickCursor();
} }
public void moveAndRightlickAtCoordinatesWithRandomness(Point goalPoint, int xTolerance, int yTolerance) throws InterruptedException {
Point randomizedGoalPoint = randomizePoint(goalPoint, xTolerance, yTolerance);
moveCursorToCoordinates(randomizedGoalPoint);
rightClickCursor();
}
public void moveCursorToCoordinates(Point goalPoint) throws InterruptedException { public void moveCursorToCoordinates(Point goalPoint) throws InterruptedException {
Point startingCursorPoint = getCurrentCursorPoint(); Point startingCursorPoint = getCurrentCursorPoint();
@ -147,4 +156,12 @@ public class Cursor {
public Point getCurrentCursorPoint() { public Point getCurrentCursorPoint() {
return MouseInfo.getPointerInfo().getLocation(); return MouseInfo.getPointerInfo().getLocation();
} }
private int getRandomIntSigned(int tolerance) {
return random.nextInt(tolerance) - tolerance / 2;
}
private Point randomizePoint(Point goalPoint, int xTolerance, int yTolerance) {
return new Point(goalPoint.x + getRandomIntSigned(xTolerance), goalPoint.y + getRandomIntSigned(yTolerance));
}
} }

View File

@ -1,22 +1,38 @@
import java.awt.AWTException; import java.awt.AWTException;
import java.awt.Point;
public class CursorTask extends Cursor { public class CursorTask {
public CursorTask() throws AWTException {
super();
}
public void dropAllItemsInInventory() { public static final int DROP_OFFSET = 40;
for (int inventoryColumn = 0; inventoryColumn < 7; inventoryColumn++) { public static final int DROP_OFFSET_BOTTOM_ROW = 10;
for (int inventoryRow = 0; inventoryRow < 4; inventoryRow++) {
//dropItem public void dropAllItemsInInventory(Cursor cursor, Inventory inventory) throws InterruptedException {
for (int row = 0; row < 4; row++) {
for (int column = 0; column < 7; column++) {
dropItem(cursor, inventory, row, column);
} }
} }
} }
public void dropItem(InventorySlot inventorySlot) { public void dropItem(Cursor cursor, Inventory inventory, int row, int column) throws InterruptedException {
/*Point inventorySlotCoordinates = inventorySlot.getCoordinates(); System.out.println("Dropping item...");
moveAndRightClickAtCoordinates(inventorySlotCoordinates); Point coordinatesToRightClick = inventory.getClickCoordinatesCoordinatesForInventorySlot(row, column);
moveAndLeftClickAtCoordinates();*/ rightClickItemSlot(cursor, coordinatesToRightClick);
leftClickDropOption(cursor, coordinatesToRightClick, row);
}
public void rightClickItemSlot(Cursor cursor, Point coordinatesToRightClick) throws InterruptedException {
cursor.moveAndRightlickAtCoordinatesWithRandomness(coordinatesToRightClick, 10, 10);
}
private void leftClickDropOption(Cursor cursor, Point coordinatesToLeftClick, int row) throws InterruptedException {
Point offsetCoordinatesToLeftClick = coordinatesToLeftClick;
if (row < 6) {
offsetCoordinatesToLeftClick.y += DROP_OFFSET;
}
else {
offsetCoordinatesToLeftClick.y += DROP_OFFSET_BOTTOM_ROW;
}
cursor.moveAndLeftClickAtCoordinatesWithRandomness(coordinatesToLeftClick, 10, 10);
} }
} }

View File

@ -1,4 +1,5 @@
import java.awt.AWTException; import java.awt.AWTException;
import java.awt.Point;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.Robot; import java.awt.Robot;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
@ -14,6 +15,9 @@ public class Inventory {
public static final int INVENTORY_WIDTH = 820 - 649;// 820 public static final int INVENTORY_WIDTH = 820 - 649;// 820
public static final int INVENTORY_HEIGHT = 350; // 530 public static final int INVENTORY_HEIGHT = 350; // 530
private static final int INVENTORY_SLOT_WIDTH = 171 / 4;
private static final int INVENTORY_SLOT_HEIGHT = 254 / 7;
public static final int NUM_ROWS = 4; public static final int NUM_ROWS = 4;
public static final int NUM_COLUMNS = 7; public static final int NUM_COLUMNS = 7;
@ -48,7 +52,10 @@ public class Inventory {
public void update() throws IOException { public void update() throws IOException {
BufferedImage image = robot.createScreenCapture(this.inventoryRectangleToCapture); BufferedImage image = robot.createScreenCapture(this.inventoryRectangleToCapture);
ImageIO.write(image, "png", new File(getImageName()));
// For test image generation only
//ImageIO.write(image, "png", new File(getImageName()));
updateAllInventorySlots(image); updateAllInventorySlots(image);
} }
@ -56,6 +63,7 @@ public class Inventory {
return ("/home/dpapp/Desktop/RunescapeAIPics/Tests/Inventory/inventory.png"); return ("/home/dpapp/Desktop/RunescapeAIPics/Tests/Inventory/inventory.png");
} }
// For testing only
public void updateWithFakeImageForTests(BufferedImage testImage) throws IOException { public void updateWithFakeImageForTests(BufferedImage testImage) throws IOException {
updateAllInventorySlots(testImage); updateAllInventorySlots(testImage);
} }
@ -73,14 +81,22 @@ public class Inventory {
return inventorySlots[row][column].getItemNameInInventorySlot(items); return inventorySlots[row][column].getItemNameInInventorySlot(items);
} }
/*public boolean isInventoryFull() { public boolean isInventoryFull() {
// TODO: this will fail if some unexpected item shows up
for (int row = 0; row < 4; row++) { for (int row = 0; row < 4; row++) {
for (int column = 0; column < 7; column++) { for (int column = 0; column < 7; column++) {
if (!inventorySlots[row][column].isInventorySlotEmpty(items)) { if (inventorySlots[row][column].isInventorySlotEmpty(items)) {
return false; return false;
} }
} }
} }
return true; return true;
}*/ }
public Point getClickCoordinatesCoordinatesForInventorySlot(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;
return new Point(x, y);
}
} }

View File

@ -31,15 +31,21 @@ public class InventorySlot {
} }
public boolean isInventorySlotEmpty(InventoryItems items) { public boolean isInventorySlotEmpty(InventoryItems items) {
return ("empty" == items.getNameOfItemFromImage(this.inventorySlotImage)); return (items.getNameOfItemFromImage(this.inventorySlotImage).equals("empty"));
} }
public void writeInventorySlotImage(BufferedImage image, int row, int column) throws IOException { public Point getClickablePointWithinItemSlot() {
return new Point(INVENTORY_SLOT_WIDTH / 2, INVENTORY_SLOT_HEIGHT / 2);
}
// For test image generation only
/*public void writeInventorySlotImage(BufferedImage image, int row, int column) throws IOException {
updateInventorySlot(image); updateInventorySlot(image);
ImageIO.write(this.inventorySlotImage, "png", new File(getImageName(row, column))); ImageIO.write(this.inventorySlotImage, "png", new File(getImageName(row, column)));
} }
// For test image generation only
private String getImageName(int row, int column) { private String getImageName(int row, int column) {
return ("/home/dpapp/Desktop/RunescapeAIPics/InventorySlots/inventorySlot_" + row + "_" + column + ".png"); return ("/home/dpapp/Desktop/RunescapeAIPics/InventorySlots/inventorySlot_" + row + "_" + column + ".png");
} }*/
} }

View File

@ -35,20 +35,32 @@ class InventoryTest {
{"empty", "oakLogs", "empty", "logs", "willowLogs", "empty", "willowLogs"}, {"empty", "oakLogs", "empty", "logs", "willowLogs", "empty", "willowLogs"},
{"logs", "empty", "oakLogs", "oakLogs", "empty", "oakLogs", "empty"}, {"logs", "empty", "oakLogs", "oakLogs", "empty", "oakLogs", "empty"},
{"willowLogs", "empty", "logs", "willowLogs", "empty", "logs", "logs"}}; {"willowLogs", "empty", "logs", "willowLogs", "empty", "logs", "logs"}};
testInventory("inventory_0.png", expectedItemNames0); testGetNameInItemInventorySlotHelper("inventory_0.png", expectedItemNames0);
testInventory("inventory_1.png", expectedItemNames1); testGetNameInItemInventorySlotHelper("inventory_1.png", expectedItemNames1);
testInventory("inventory_2.png", expectedItemNames2); testGetNameInItemInventorySlotHelper("inventory_2.png", expectedItemNames2);
} }
public BufferedImage loadBufferedImage(String fileName) throws IOException { @Test
File itemFile = new File(this.testingInventoryDirectoryPath + fileName); public void testInventoryFull() throws AWTException, IOException {
BufferedImage itemImage = ImageIO.read(itemFile); initialize();
return itemImage;
testInventoryFullHelper("inventory_full_0.png", true);
testInventoryFullHelper("inventory_full_1.png", true);
testInventoryFullHelper("inventory_full_2.png", true);
testInventoryFullHelper("inventory_full_3.png", true);
testInventoryFullHelper("inventory_full_4.png", true);
testInventoryFullHelper("inventory_not_full_0.png", false);
testInventoryFullHelper("inventory_not_full_1.png", false);
testInventoryFullHelper("inventory_not_full_2.png", false);
} }
void testInventory(String inventoryFileName, String[][] expectedItemNames) throws IOException { public void testInventoryFullHelper(String inventoryFileName, boolean expectedResult) throws IOException {
BufferedImage testImage = loadBufferedImage(inventoryFileName); loadTestingImageToInventory(inventoryFileName);
inventory.updateWithFakeImageForTests(testImage); assertEquals(inventory.isInventoryFull(), expectedResult);
}
public void testGetNameInItemInventorySlotHelper(String inventoryFileName, String[][] expectedItemNames) throws IOException {
loadTestingImageToInventory(inventoryFileName);
for (int row = 0; row < 4; row++) { for (int row = 0; row < 4; row++) {
for (int column = 0; column < 7; column++) { for (int column = 0; column < 7; column++) {
@ -57,4 +69,17 @@ class InventoryTest {
} }
} }
public BufferedImage loadBufferedImage(String fileName) throws IOException {
File itemFile = new File(this.testingInventoryDirectoryPath + fileName);
BufferedImage itemImage = ImageIO.read(itemFile);
return itemImage;
}
public void loadTestingImageToInventory(String inventoryFileName) throws IOException {
BufferedImage testImage = loadBufferedImage(inventoryFileName);
inventory.updateWithFakeImageForTests(testImage);
}
} }

View File

@ -8,11 +8,18 @@ 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();
//Inventory inventory = new Inventory(); //Inventory inventory = new Inventory();
//inventory.update(); //inventory.update();
Inventory inventory = new Inventory(); Inventory inventory = new Inventory();
inventory.update(); //inventory.update();
cursorTask.dropAllItemsInInventory(cursor, inventory);
/*cursorTask.dropItem(cursor, inventory, 0, 0);
Thread.sleep(3000);
cursorTask.dropItem(cursor, inventory, 3, 5);
Thread.sleep(3000);
cursorTask.dropItem(cursor, inventory, 0, 6);*/
//Items items = new Items("/home/dpapp/Desktop/RunescapeAIPics/Items/"); //Items items = new Items("/home/dpapp/Desktop/RunescapeAIPics/Items/");
//items.displayItems(); //items.displayItems();