2018-01-31 09:05:34 -05:00
|
|
|
import java.awt.AWTException;
|
2018-02-02 06:27:29 -05:00
|
|
|
import java.awt.Point;
|
2018-01-31 09:05:34 -05:00
|
|
|
import java.awt.Rectangle;
|
|
|
|
import java.awt.Robot;
|
|
|
|
import java.awt.image.BufferedImage;
|
2018-02-02 05:25:00 -05:00
|
|
|
import java.io.File;
|
2018-01-31 09:05:34 -05:00
|
|
|
import java.io.IOException;
|
2018-01-31 08:12:02 -05:00
|
|
|
|
2018-02-02 05:25:00 -05:00
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
2018-01-31 08:12:02 -05:00
|
|
|
public class Inventory {
|
|
|
|
|
2018-01-31 23:38:32 -05:00
|
|
|
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;
|
2018-01-31 09:05:34 -05:00
|
|
|
public static final int INVENTORY_WIDTH = 820 - 649;// 820
|
2018-01-31 23:38:32 -05:00
|
|
|
public static final int INVENTORY_HEIGHT = 350; // 530
|
2018-01-31 09:05:34 -05:00
|
|
|
|
2018-02-02 06:27:29 -05:00
|
|
|
private static final int INVENTORY_SLOT_WIDTH = 171 / 4;
|
|
|
|
private static final int INVENTORY_SLOT_HEIGHT = 254 / 7;
|
|
|
|
|
2018-02-02 00:49:19 -05:00
|
|
|
public static final int NUM_ROWS = 4;
|
|
|
|
public static final int NUM_COLUMNS = 7;
|
|
|
|
|
2018-01-31 09:05:34 -05:00
|
|
|
Robot robot;
|
2018-02-02 00:49:19 -05:00
|
|
|
Rectangle inventoryRectangleToCapture;
|
2018-01-31 08:12:02 -05:00
|
|
|
InventorySlot[][] inventorySlots;
|
2018-02-02 00:49:19 -05:00
|
|
|
InventoryItems items;
|
2018-01-31 08:12:02 -05:00
|
|
|
|
2018-02-02 00:49:19 -05:00
|
|
|
public Inventory() throws AWTException, IOException {
|
|
|
|
initializeInventoryRectangle();
|
2018-01-31 09:05:34 -05:00
|
|
|
initializeInventorySlots();
|
2018-02-02 00:49:19 -05:00
|
|
|
initializeItems();
|
2018-01-31 09:05:34 -05:00
|
|
|
robot = new Robot();
|
2018-02-02 00:49:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeInventoryRectangle() {
|
|
|
|
inventoryRectangleToCapture = new Rectangle(INVENTORY_OFFSET_WIDTH, INVENTORY_OFFSET_HEIGHT, INVENTORY_WIDTH, INVENTORY_HEIGHT);
|
2018-01-31 09:05:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeInventorySlots() {
|
2018-01-31 08:12:02 -05:00
|
|
|
inventorySlots = new InventorySlot[4][7];
|
2018-01-31 09:05:34 -05:00
|
|
|
for (int row = 0; row < 4; row++) {
|
|
|
|
for (int column = 0; column < 7; column++) {
|
|
|
|
inventorySlots[row][column] = new InventorySlot(row, column);
|
2018-01-31 08:12:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 00:49:19 -05:00
|
|
|
private void initializeItems() throws IOException {
|
2018-02-19 16:12:25 -05:00
|
|
|
items = new InventoryItems("/home/dpapp/Desktop/RunescapeAI/Items/");
|
2018-02-02 00:49:19 -05:00
|
|
|
}
|
|
|
|
|
2018-01-31 09:05:34 -05:00
|
|
|
public void update() throws IOException {
|
2018-02-02 00:49:19 -05:00
|
|
|
BufferedImage image = robot.createScreenCapture(this.inventoryRectangleToCapture);
|
2018-02-02 06:27:29 -05:00
|
|
|
|
|
|
|
// For test image generation only
|
|
|
|
//ImageIO.write(image, "png", new File(getImageName()));
|
|
|
|
|
2018-01-31 09:05:34 -05:00
|
|
|
updateAllInventorySlots(image);
|
|
|
|
}
|
2018-02-02 05:25:00 -05:00
|
|
|
|
2018-02-02 06:27:29 -05:00
|
|
|
// For testing only
|
2018-02-02 00:49:19 -05:00
|
|
|
public void updateWithFakeImageForTests(BufferedImage testImage) throws IOException {
|
|
|
|
updateAllInventorySlots(testImage);
|
|
|
|
}
|
|
|
|
|
2018-01-31 09:05:34 -05:00
|
|
|
private void updateAllInventorySlots(BufferedImage image) throws IOException {
|
|
|
|
for (int row = 0; row < 4; row++) {
|
|
|
|
for (int column = 0; column < 7; column++) {
|
2018-02-01 22:39:57 -05:00
|
|
|
inventorySlots[row][column].updateInventorySlot(image);
|
2018-02-02 05:25:00 -05:00
|
|
|
//inventorySlots[row][column].writeInventorySlotImage(image, row, column);
|
2018-01-31 09:05:34 -05:00
|
|
|
}
|
|
|
|
}
|
2018-01-31 08:12:02 -05:00
|
|
|
}
|
2018-02-02 00:49:19 -05:00
|
|
|
|
|
|
|
public String getItemNameInInventorySlot(int row, int column) {
|
|
|
|
return inventorySlots[row][column].getItemNameInInventorySlot(items);
|
|
|
|
}
|
|
|
|
|
2018-02-02 06:27:29 -05:00
|
|
|
public boolean isInventoryFull() {
|
|
|
|
// TODO: this will fail if some unexpected item shows up
|
2018-02-02 00:49:19 -05:00
|
|
|
for (int row = 0; row < 4; row++) {
|
|
|
|
for (int column = 0; column < 7; column++) {
|
2018-02-02 06:27:29 -05:00
|
|
|
if (inventorySlots[row][column].isInventorySlotEmpty(items)) {
|
2018-02-02 00:49:19 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2018-02-02 06:27:29 -05:00
|
|
|
}
|
|
|
|
|
2018-02-19 16:12:25 -05:00
|
|
|
public Point getClickCoordinatesForInventorySlot(int row, int column) {
|
2018-02-02 06:27:29 -05:00
|
|
|
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);
|
|
|
|
}
|
2018-01-31 08:12:02 -05:00
|
|
|
}
|