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_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 Random random = new Random();
@ -38,6 +32,7 @@ public class Cursor {
private ArrayList<ArrayList<CursorPath>> cursorPathsByDistance;
public Cursor() throws AWTException {
System.out.println("Initializing cursor...");
initializeCursorPathsByDistanceFromFile("/home/dpapp/GhostMouse/coordinates.txt");
robot = new Robot();
@ -84,12 +79,14 @@ public class Cursor {
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(getRandomClickLength());
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(getRandomClickLength());
}
public void rightClickCursor() throws InterruptedException {
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
Thread.sleep(getRandomClickLength());
Thread.sleep(200 + getRandomClickLength() * 2);
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
Thread.sleep(getRandomClickLength());
}
public void moveAndLeftClickAtCoordinates(Point goalPoint) throws InterruptedException {
@ -97,10 +94,22 @@ public class Cursor {
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 {
moveCursorToCoordinates(goalPoint);
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 {
Point startingCursorPoint = getCurrentCursorPoint();
@ -147,4 +156,12 @@ public class Cursor {
public Point getCurrentCursorPoint() {
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.Point;
public class CursorTask extends Cursor {
public CursorTask() throws AWTException {
super();
}
public class CursorTask {
public void dropAllItemsInInventory() {
for (int inventoryColumn = 0; inventoryColumn < 7; inventoryColumn++) {
for (int inventoryRow = 0; inventoryRow < 4; inventoryRow++) {
//dropItem
public static final int DROP_OFFSET = 40;
public static final int DROP_OFFSET_BOTTOM_ROW = 10;
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) {
/*Point inventorySlotCoordinates = inventorySlot.getCoordinates();
moveAndRightClickAtCoordinates(inventorySlotCoordinates);
moveAndLeftClickAtCoordinates();*/
public void dropItem(Cursor cursor, Inventory inventory, int row, int column) throws InterruptedException {
System.out.println("Dropping item...");
Point coordinatesToRightClick = inventory.getClickCoordinatesCoordinatesForInventorySlot(row, column);
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.Point;
import java.awt.Rectangle;
import java.awt.Robot;
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_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_COLUMNS = 7;
@ -48,7 +52,10 @@ public class Inventory {
public void update() throws IOException {
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);
}
@ -56,6 +63,7 @@ public class Inventory {
return ("/home/dpapp/Desktop/RunescapeAIPics/Tests/Inventory/inventory.png");
}
// For testing only
public void updateWithFakeImageForTests(BufferedImage testImage) throws IOException {
updateAllInventorySlots(testImage);
}
@ -73,14 +81,22 @@ public class Inventory {
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 column = 0; column < 7; column++) {
if (!inventorySlots[row][column].isInventorySlotEmpty(items)) {
if (inventorySlots[row][column].isInventorySlotEmpty(items)) {
return false;
}
}
}
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) {
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);
ImageIO.write(this.inventorySlotImage, "png", new File(getImageName(row, column)));
}
// For test image generation only
private String getImageName(int row, int column) {
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"},
{"logs", "empty", "oakLogs", "oakLogs", "empty", "oakLogs", "empty"},
{"willowLogs", "empty", "logs", "willowLogs", "empty", "logs", "logs"}};
testInventory("inventory_0.png", expectedItemNames0);
testInventory("inventory_1.png", expectedItemNames1);
testInventory("inventory_2.png", expectedItemNames2);
testGetNameInItemInventorySlotHelper("inventory_0.png", expectedItemNames0);
testGetNameInItemInventorySlotHelper("inventory_1.png", expectedItemNames1);
testGetNameInItemInventorySlotHelper("inventory_2.png", expectedItemNames2);
}
public BufferedImage loadBufferedImage(String fileName) throws IOException {
File itemFile = new File(this.testingInventoryDirectoryPath + fileName);
BufferedImage itemImage = ImageIO.read(itemFile);
return itemImage;
@Test
public void testInventoryFull() throws AWTException, IOException {
initialize();
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 {
BufferedImage testImage = loadBufferedImage(inventoryFileName);
inventory.updateWithFakeImageForTests(testImage);
public void testInventoryFullHelper(String inventoryFileName, boolean expectedResult) throws IOException {
loadTestingImageToInventory(inventoryFileName);
assertEquals(inventory.isInventoryFull(), expectedResult);
}
public void testGetNameInItemInventorySlotHelper(String inventoryFileName, String[][] expectedItemNames) throws IOException {
loadTestingImageToInventory(inventoryFileName);
for (int row = 0; row < 4; row++) {
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 {
//Cursor cursor = new Cursor();
Cursor cursor = new Cursor();
CursorTask cursorTask = new CursorTask();
//Inventory inventory = new Inventory();
//inventory.update();
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.displayItems();