Creating Item and Items classes
This commit is contained in:
parent
46248edc8e
commit
7610163b13
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
bin/Item.class
BIN
bin/Item.class
Binary file not shown.
Binary file not shown.
BIN
bin/main.class
BIN
bin/main.class
Binary file not shown.
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String, Item> items;
|
||||
|
||||
public Items() throws IOException {
|
||||
initializeItemsFromDirectory("/home/dpapp/Desktop/RunescapeAIPics/Items/");
|
||||
}
|
||||
|
||||
private void initializeItemsFromDirectory(String itemDirectoryPath) throws IOException {
|
||||
this.items = new HashMap<String, Item>();
|
||||
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<String, Item> entry : items.entrySet()) {
|
||||
String itemName = entry.getKey();
|
||||
Item item = entry.getValue();
|
||||
System.out.println("Item name: " + itemName);
|
||||
}
|
||||
}*/
|
||||
}
|
|
@ -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));
|
||||
|
|
Loading…
Reference in New Issue