diff --git a/bin/Inventory.class b/bin/Inventory.class index bbcd7c7..c5560f1 100644 Binary files a/bin/Inventory.class and b/bin/Inventory.class differ diff --git a/bin/InventorySlot.class b/bin/InventorySlot.class index f4f103f..06d7b91 100644 Binary files a/bin/InventorySlot.class and b/bin/InventorySlot.class differ diff --git a/bin/InventorySlotTest.class b/bin/InventorySlotTest.class new file mode 100644 index 0000000..a355f15 Binary files /dev/null and b/bin/InventorySlotTest.class differ diff --git a/bin/Item.class b/bin/Item.class index 45f6372..7432017 100644 Binary files a/bin/Item.class and b/bin/Item.class differ diff --git a/bin/Items.class b/bin/Items.class new file mode 100644 index 0000000..d87b8c7 Binary files /dev/null and b/bin/Items.class differ diff --git a/bin/main.class b/bin/main.class index 32d8380..2ecbb46 100644 Binary files a/bin/main.class and b/bin/main.class differ diff --git a/src/Inventory.java b/src/Inventory.java index 4e063df..5a92901 100644 --- a/src/Inventory.java +++ b/src/Inventory.java @@ -6,10 +6,6 @@ import java.io.IOException; public class Inventory { - /*x0 = 655; - x1 = 697; - x2 = 738; - x3 = 781;*/ public static final int INVENTORY_OFFSET_WIDTH = 655; //top left corner of inventory, fromm top left corner of screen public static final int INVENTORY_OFFSET_HEIGHT = 290; public static final int INVENTORY_WIDTH = 820 - 649;// 820 @@ -29,7 +25,6 @@ public class Inventory { inventorySlots = new InventorySlot[4][7]; for (int row = 0; row < 4; row++) { for (int column = 0; column < 7; column++) { - // might need to manually create these inventorySlots[row][column] = new InventorySlot(row, column); } } @@ -43,7 +38,7 @@ public class Inventory { private void updateAllInventorySlots(BufferedImage image) throws IOException { for (int row = 0; row < 4; row++) { for (int column = 0; column < 7; column++) { - inventorySlots[row][column].writeInventorySlotToImage(image, row, column); + inventorySlots[row][column].updateInventorySlot(image); } } } diff --git a/src/InventorySlot.java b/src/InventorySlot.java index f1789f5..8e56fa9 100644 --- a/src/InventorySlot.java +++ b/src/InventorySlot.java @@ -8,8 +8,8 @@ import javax.imageio.ImageIO; public class InventorySlot { - private static final int INVENTORY_WIDTH = 171; - private static final int INVENTORY_HEIGHT = 254; + private static final int INVENTORY_SLOT_WIDTH = 171 / 4; + private static final int INVENTORY_SLOT_HEIGHT = 254 / 7; String screenshotOutputDirectory; Item itemInInventorySlot; @@ -17,29 +17,69 @@ public class InventorySlot { public InventorySlot(int row, int column) { initializeRectangleToCapture(row, column); - this.screenshotOutputDirectory = "/home/dpapp/Desktop/RunescapeAIPics/InventorySlots/"; } private void initializeRectangleToCapture(int row, int column) { - int slotWidth = INVENTORY_WIDTH / 4; - int slotHeight = INVENTORY_HEIGHT / 7; - //System.out.println("SlotWidth: " + slotWidth + ", slotHeight: " + slotHeight); - rectangleToCapture = new Rectangle(row * slotWidth, column * slotHeight, slotWidth, slotHeight); + rectangleToCapture = new Rectangle(row * INVENTORY_SLOT_WIDTH, column * INVENTORY_SLOT_HEIGHT, INVENTORY_SLOT_WIDTH, INVENTORY_SLOT_HEIGHT); } - public void updateInventorySlot(BufferedImage image) { - //BufferedImage croppedInventorySlotArea = image.getSubimage(rectangleToCapture.x, rectangleToCapture.y, rectangleToCapture.width, rectangleToCapture.height); - } - - public void writeInventorySlotToImage(BufferedImage image, int row, int column) throws IOException { - System.out.println("Pre-cropped image is of size:" + image.getWidth() + ", " + image.getHeight()); - System.out.println(row + ", " + column); - System.out.println("Getting image from: " + rectangleToCapture.x + ", " + rectangleToCapture.y + ", " + rectangleToCapture.width + ", " + rectangleToCapture.height); + public void updateInventorySlot(BufferedImage image) throws IOException { BufferedImage croppedInventorySlotArea = image.getSubimage(rectangleToCapture.x, rectangleToCapture.y, rectangleToCapture.width, rectangleToCapture.height); - ImageIO.write(croppedInventorySlotArea, "png", new File(getImageName(row, column))); + setItemInInventorySlotFromImage(croppedInventorySlotArea); } - - private String getImageName(int row, int column) { - return this.screenshotOutputDirectory + "screenshot" + row + "_" + column + ".png"; + + private void setItemInInventorySlotFromImage(BufferedImage croppedInventorySlotArea) throws IOException { + if (itemIsLog(croppedInventorySlotArea)) System.out.println("LOG!"); } + + public boolean itemIsLog(BufferedImage croppedInventorySlotArea) throws IOException { + int matchingPixel = 0; + //int nonMatchingPixel = 0; + File image = new File("/home/dpapp/Desktop/RunescapeAIPics/Items/willowLogs.png"); + BufferedImage logImage = ImageIO.read(image); + /*System.out.println(logImage.getWidth() + ", " + logImage.getHeight()); + System.out.println(INVENTORY_SLOT_WIDTH + ", " + INVENTORY_SLOT_HEIGHT); + System.out.println("Dimension match?");*/ + for (int row = 0; row < INVENTORY_SLOT_WIDTH; row++) { + for (int col = 0; col < INVENTORY_SLOT_HEIGHT; col++) { + if (pixelsWithinRGBTolerance(croppedInventorySlotArea.getRGB(row, col), logImage.getRGB(row, col))) { + matchingPixel++; + } + /*else { + nonMatchingPixel++; + }*/ + } + } + + if (matchingPixel > 300) { + //System.out.println("Found log with " + matchingPixel + " matches!" + nonMatchingPixel); + return true; + } + //System.out.println("No match!" + matchingPixel + ", nonmatching: " + nonMatchingPixel); + return false; + } + + private boolean pixelsWithinRGBTolerance(int rgb1, int rgb2) { + int[] colors1 = getRGBValuesFromPixel(rgb1); + int[] colors2 = getRGBValuesFromPixel(rgb2); + for (int i = 0; i < 3; i++) { + if (Math.abs(colors1[i] - colors2[i]) > 5) { + return false; + } + } + /*displayColor(colors1); + System.out.println("vs"); + displayColor(colors2); + System.out.println();*/ + return true; + } + + private void displayColor(int[] colors) { + System.out.println(colors[0] + "," + colors[1] + "," + colors[2]); + } + + private int[] getRGBValuesFromPixel(int pixel) { + int[] colors = {(pixel)&0xFF, (pixel>>8)&0xFF, (pixel>>16)&0xFF, (pixel>>24)&0xFF}; + return colors; + } } diff --git a/src/InventorySlotTest.java b/src/InventorySlotTest.java new file mode 100644 index 0000000..f749035 --- /dev/null +++ b/src/InventorySlotTest.java @@ -0,0 +1,29 @@ +import static org.junit.jupiter.api.Assertions.*; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + +import org.junit.Before; +import org.junit.jupiter.api.Test; + +class InventorySlotTest { + + + InventorySlot inventorySlot; + + void initialize() { + inventorySlot = new InventorySlot(0, 0); + } + + @Test + void test() throws IOException { + initialize(); + File image = new File("/home/dpapp/Desktop/RunescapeAIPics/Tests/screenshot0_0.png"); + BufferedImage testImage = ImageIO.read(image); + assertTrue(inventorySlot.itemIsLog(testImage)); + } + +} diff --git a/src/Item.java b/src/Item.java index 22a7e83..b8b9533 100644 --- a/src/Item.java +++ b/src/Item.java @@ -1,4 +1,66 @@ +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; public class Item { + + private BufferedImage itemImage; + + public Item(String itemDirectoryPath, String itemName) throws IOException { + initializeImage(itemDirectoryPath, itemName); + } + + private void initializeImage(String itemDirectoryPath, String itemName) throws IOException { + File itemImageFile = new File(joinPathWithFileName(itemDirectoryPath, itemName)); + this.itemImage = ImageIO.read(itemImageFile); + } + + private String joinPathWithFileName(String itemDirectoryPath, String itemName) { + return (itemDirectoryPath + itemName); + } + public boolean itemMatchesImage(BufferedImage itemImageToCompare) { + if (imagesAreTheSameSize(itemImageToCompare)) { + return imagesMatch(itemImageToCompare); + } + return false; + } + + private boolean imagesAreTheSameSize(BufferedImage itemImageToCompare) { + return (itemImage.getWidth() == itemImageToCompare.getWidth()) && (itemImage.getHeight() == itemImageToCompare.getHeight()); + } + + private boolean imagesMatch(BufferedImage itemImageToCompare) { + return false; + } + + private boolean imagesMatch(BufferedImage itemImageToCompare, int pixelTolerance) { + int matchingPixel = 0; + for (int row = 0; row < itemImageToCompare.getWidth(); row++) { + for (int col = 0; col < itemImageToCompare.getHeight(); col++) { + if (pixelsAreWithinRGBTolerance(itemImage.getRGB(row, col), itemImageToCompare.getRGB(row, col))) { + matchingPixel++; + } + } + } + return true; + } + + private boolean pixelsAreWithinRGBTolerance(int rgb1, int rgb2) { + int[] colors1 = getRGBValuesFromPixel(rgb1); + int[] colors2 = getRGBValuesFromPixel(rgb2); + for (int i = 0; i < 3; i++) { + if (Math.abs(colors1[i] - colors2[i]) > 5) { + return false; + } + } + return true; + } + + private int[] getRGBValuesFromPixel(int pixel) { + int[] colors = {(pixel)&0xFF, (pixel>>8)&0xFF, (pixel>>16)&0xFF, (pixel>>24)&0xFF}; + return colors; + } } diff --git a/src/Items.java b/src/Items.java new file mode 100644 index 0000000..7ac0f99 --- /dev/null +++ b/src/Items.java @@ -0,0 +1,56 @@ +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.HashMap; + +public class Items { + // TODO: write tests + HashMap items; + + public Items() throws IOException { + initializeItemsFromDirectory("/home/dpapp/Desktop/RunescapeAIPics/Items/"); + } + + private void initializeItemsFromDirectory(String itemDirectoryPath) throws IOException { + this.items = new HashMap(); + for (File itemFile : getListOfFilesFromItemDirectory(itemDirectoryPath)) { + if (itemFile.isFile()) { + putItemInMap(itemDirectoryPath, itemFile.getName()); + } + } + } + + private File[] getListOfFilesFromItemDirectory(String itemDirectoryPath) { + File itemDirectory = new File(itemDirectoryPath); + return itemDirectory.listFiles(); + } + + private void putItemInMap(String itemDirectoryPath, String itemFileName) throws IOException { + Item item = new Item(itemDirectoryPath, itemFileName); + String itemName = getItemNameFromFile(itemFileName); + this.items.put(itemName, item); + } + + private String getItemNameFromFile(String fileName) { + return fileName.substring(0, fileName.indexOf('.')); + } + + public boolean isInstanceOf(BufferedImage itemImage, String itemName) { + if (items.containsKey(itemName)) { + return getItem(itemName).itemMatchesImage(itemImage); + } + return false; + } + + private Item getItem(String itemName) { + return items.get(itemName); + } + + /*public void displayItems() { + for (HashMap.Entry entry : items.entrySet()) { + String itemName = entry.getKey(); + Item item = entry.getValue(); + System.out.println("Item name: " + itemName); + } + }*/ +} diff --git a/src/main.java b/src/main.java index 68e1016..22eef7e 100644 --- a/src/main.java +++ b/src/main.java @@ -9,8 +9,10 @@ public class main { public static void main(String[] args) throws AWTException, InterruptedException, IOException { //Cursor cursor = new Cursor(); - Inventory inventory = new Inventory(); - inventory.update(); + //Inventory inventory = new Inventory(); + //inventory.update(); + Items items = new Items(); + //items.displayItems(); System.out.println("Success!"); //cursor.moveCursorToCoordinates(new Point(620, 420));