diff --git a/bin/Inventory.class b/bin/Inventory.class index c5560f1..9c8d680 100644 Binary files a/bin/Inventory.class and b/bin/Inventory.class differ diff --git a/bin/Item.class b/bin/InventoryItem.class similarity index 86% rename from bin/Item.class rename to bin/InventoryItem.class index 92f0382..e950b6d 100644 Binary files a/bin/Item.class and b/bin/InventoryItem.class differ diff --git a/bin/Items.class b/bin/InventoryItems.class similarity index 80% rename from bin/Items.class rename to bin/InventoryItems.class index 99a8636..32d37ff 100644 Binary files a/bin/Items.class and b/bin/InventoryItems.class differ diff --git a/bin/ItemsTest.class b/bin/InventoryItemsTest.class similarity index 86% rename from bin/ItemsTest.class rename to bin/InventoryItemsTest.class index f85b003..745c35d 100644 Binary files a/bin/ItemsTest.class and b/bin/InventoryItemsTest.class differ diff --git a/bin/InventorySlot.class b/bin/InventorySlot.class index 06d7b91..136e4f4 100644 Binary files a/bin/InventorySlot.class and b/bin/InventorySlot.class differ diff --git a/bin/InventorySlotTest.class b/bin/InventorySlotTest.class deleted file mode 100644 index a355f15..0000000 Binary files a/bin/InventorySlotTest.class and /dev/null differ diff --git a/bin/InventoryTest.class b/bin/InventoryTest.class new file mode 100644 index 0000000..effbe69 Binary files /dev/null and b/bin/InventoryTest.class differ diff --git a/bin/main.class b/bin/main.class index b021dea..88f95e1 100644 Binary files a/bin/main.class and b/bin/main.class differ diff --git a/src/Inventory.java b/src/Inventory.java index 5a92901..f508248 100644 --- a/src/Inventory.java +++ b/src/Inventory.java @@ -11,14 +11,23 @@ public class Inventory { public static final int INVENTORY_WIDTH = 820 - 649;// 820 public static final int INVENTORY_HEIGHT = 350; // 530 - Robot robot; - Rectangle inventoryAreaToCapture; - InventorySlot[][] inventorySlots; + public static final int NUM_ROWS = 4; + public static final int NUM_COLUMNS = 7; - public Inventory() throws AWTException { + Robot robot; + Rectangle inventoryRectangleToCapture; + InventorySlot[][] inventorySlots; + InventoryItems items; + + public Inventory() throws AWTException, IOException { + initializeInventoryRectangle(); initializeInventorySlots(); + initializeItems(); robot = new Robot(); - this.inventoryAreaToCapture = new Rectangle(INVENTORY_OFFSET_WIDTH, INVENTORY_OFFSET_HEIGHT, INVENTORY_WIDTH, INVENTORY_HEIGHT); + } + + private void initializeInventoryRectangle() { + inventoryRectangleToCapture = new Rectangle(INVENTORY_OFFSET_WIDTH, INVENTORY_OFFSET_HEIGHT, INVENTORY_WIDTH, INVENTORY_HEIGHT); } private void initializeInventorySlots() { @@ -30,11 +39,19 @@ public class Inventory { } } + private void initializeItems() throws IOException { + items = new InventoryItems("/home/dpapp/Desktop/RunescapeAIPics/Items/"); + } + public void update() throws IOException { - BufferedImage image = robot.createScreenCapture(this.inventoryAreaToCapture); + BufferedImage image = robot.createScreenCapture(this.inventoryRectangleToCapture); updateAllInventorySlots(image); } - + + public void updateWithFakeImageForTests(BufferedImage testImage) throws IOException { + updateAllInventorySlots(testImage); + } + private void updateAllInventorySlots(BufferedImage image) throws IOException { for (int row = 0; row < 4; row++) { for (int column = 0; column < 7; column++) { @@ -42,5 +59,19 @@ public class Inventory { } } } - + + public String getItemNameInInventorySlot(int row, int column) { + return inventorySlots[row][column].getItemNameInInventorySlot(items); + } + + public boolean isInventoryFull() { + for (int row = 0; row < 4; row++) { + for (int column = 0; column < 7; column++) { + if (!inventorySlots[row][column].isInventorySlotEmpty(items)) { + return false; + } + } + } + return true; + } } diff --git a/src/Item.java b/src/InventoryItem.java similarity index 94% rename from src/Item.java rename to src/InventoryItem.java index 2fa529c..af0f61b 100644 --- a/src/Item.java +++ b/src/InventoryItem.java @@ -4,12 +4,12 @@ import java.io.IOException; import javax.imageio.ImageIO; -public class Item { +public class InventoryItem { private BufferedImage itemImage; private int minimumNumberOfMatchingPixels; - public Item(String itemDirectoryPath, String itemName) throws IOException { + public InventoryItem(String itemDirectoryPath, String itemName) throws IOException { initializeImage(itemDirectoryPath, itemName); this.minimumNumberOfMatchingPixels = 100; } diff --git a/src/Items.java b/src/InventoryItems.java similarity index 83% rename from src/Items.java rename to src/InventoryItems.java index 0810c75..da8483a 100644 --- a/src/Items.java +++ b/src/InventoryItems.java @@ -3,16 +3,16 @@ import java.io.File; import java.io.IOException; import java.util.HashMap; -public class Items { +public class InventoryItems { // TODO: write tests - HashMap items; + HashMap items; - public Items(String itemDirectoryPath) throws IOException { + public InventoryItems(String itemDirectoryPath) throws IOException { initializeItemsFromDirectory(itemDirectoryPath); } private void initializeItemsFromDirectory(String itemDirectoryPath) throws IOException { - this.items = new HashMap(); + this.items = new HashMap(); for (File itemFile : getListOfFilesFromItemDirectory(itemDirectoryPath)) { if (itemFile.isFile()) { putItemInMap(itemDirectoryPath, itemFile.getName()); @@ -26,7 +26,7 @@ public class Items { } private void putItemInMap(String itemDirectoryPath, String itemFileName) throws IOException { - Item item = new Item(itemDirectoryPath, itemFileName); + InventoryItem item = new InventoryItem(itemDirectoryPath, itemFileName); String itemName = getItemNameFromFile(itemFileName); this.items.put(itemName, item); } @@ -51,7 +51,7 @@ public class Items { return "empty"; } - private Item getItemByName(String itemName) { + private InventoryItem getItemByName(String itemName) { return items.get(itemName); } diff --git a/src/ItemsTest.java b/src/InventoryItemsTest.java similarity index 92% rename from src/ItemsTest.java rename to src/InventoryItemsTest.java index 5458a7c..f182752 100644 --- a/src/ItemsTest.java +++ b/src/InventoryItemsTest.java @@ -11,14 +11,14 @@ import org.junit.Before; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -class ItemsTest { +class InventoryItemsTest { - Items items; + InventoryItems items; String testingItemDirectoryPath; public void initialize() throws IOException { System.out.println("running initialize..."); - items = new Items("/home/dpapp/Desktop/RunescapeAIPics/Tests/Items/"); + items = new InventoryItems("/home/dpapp/Desktop/RunescapeAIPics/Tests/Items/"); this.testingItemDirectoryPath = "/home/dpapp/Desktop/RunescapeAIPics/Tests/ItemNameRecognition/"; } diff --git a/src/InventorySlot.java b/src/InventorySlot.java index 8e56fa9..834eb3b 100644 --- a/src/InventorySlot.java +++ b/src/InventorySlot.java @@ -11,9 +11,8 @@ public class InventorySlot { private static final int INVENTORY_SLOT_WIDTH = 171 / 4; private static final int INVENTORY_SLOT_HEIGHT = 254 / 7; - String screenshotOutputDirectory; - Item itemInInventorySlot; Rectangle rectangleToCapture; + BufferedImage inventorySlotImage; public InventorySlot(int row, int column) { initializeRectangleToCapture(row, column); @@ -24,62 +23,14 @@ public class InventorySlot { } public void updateInventorySlot(BufferedImage image) throws IOException { - BufferedImage croppedInventorySlotArea = image.getSubimage(rectangleToCapture.x, rectangleToCapture.y, rectangleToCapture.width, rectangleToCapture.height); - setItemInInventorySlotFromImage(croppedInventorySlotArea); + this.inventorySlotImage = image.getSubimage(rectangleToCapture.x, rectangleToCapture.y, rectangleToCapture.width, rectangleToCapture.height); } - private void setItemInInventorySlotFromImage(BufferedImage croppedInventorySlotArea) throws IOException { - if (itemIsLog(croppedInventorySlotArea)) System.out.println("LOG!"); + public String getItemNameInInventorySlot(InventoryItems items) { + return items.getNameOfItemFromImage(this.inventorySlotImage); } - 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; + public boolean isInventorySlotEmpty(InventoryItems items) { + return ("empty" == items.getNameOfItemFromImage(this.inventorySlotImage)); } - - 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 deleted file mode 100644 index f749035..0000000 --- a/src/InventorySlotTest.java +++ /dev/null @@ -1,29 +0,0 @@ -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/InventoryTest.java b/src/InventoryTest.java new file mode 100644 index 0000000..c3fe3db --- /dev/null +++ b/src/InventoryTest.java @@ -0,0 +1,39 @@ +import static org.junit.jupiter.api.Assertions.*; + +import java.awt.AWTException; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + +import org.junit.jupiter.api.Test; + +class InventoryTest { + + Inventory inventory; + String testingInventoryDirectoryPath; + + public void initialize() throws AWTException, IOException { + inventory = new Inventory(); + this.testingInventoryDirectoryPath = "/home/dpapp/Desktop/RunescapeAIPics/Tests/Inventory/"; + } + + @Test + public void testGetNameInItemInventorySlot() throws IOException, AWTException { + initialize(); + + // TODO: add image for uploading custom image to Inventory + BufferedImage testImage = loadBufferedImage("inventory_0.png"); + inventory.updateWithFakeImageForTests(testImage); + assertEquals(inventory.getItemNameInInventorySlot(0, 0), "willowLogs"); + assertEquals(inventory.getItemNameInInventorySlot(3, 6), "empty"); + } + + public BufferedImage loadBufferedImage(String fileName) throws IOException { + File itemFile = new File(this.testingInventoryDirectoryPath + fileName); + BufferedImage itemImage = ImageIO.read(itemFile); + return itemImage; + } + +} diff --git a/src/main.java b/src/main.java index 31beb20..6ca5b39 100644 --- a/src/main.java +++ b/src/main.java @@ -11,7 +11,8 @@ public class main { //Cursor cursor = new Cursor(); //Inventory inventory = new Inventory(); //inventory.update(); - Items items = new Items("/home/dpapp/Desktop/RunescapeAIPics/Items/"); + Inventory inventory = new Inventory(); + //Items items = new Items("/home/dpapp/Desktop/RunescapeAIPics/Items/"); //items.displayItems(); System.out.println("Success!");