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 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() {
|
2018-02-22 11:28:01 -05:00
|
|
|
inventoryRectangleToCapture = new Rectangle(Constants.INVENTORY_WINDOW_OFFSET_X, Constants.INVENTORY_WINDOW_OFFSET_Y, Constants.INVENTORY_WINDOW_WIDTH, Constants.INVENTORY_WINDOW_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-02-22 11:28:01 -05:00
|
|
|
for (int row = 0; row < Constants.INVENTORY_NUM_ROWS; row++) {
|
|
|
|
for (int column = 0; column < Constants.INVENTORY_NUM_COLUMNS; column++) {
|
2018-01-31 09:05:34 -05:00
|
|
|
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-22 13:46:32 -05:00
|
|
|
items = new InventoryItems(Paths.INVENTORY_ITEMS_DIRECTORY_PATH);
|
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-01-31 09:05:34 -05:00
|
|
|
updateAllInventorySlots(image);
|
|
|
|
}
|
2018-02-02 00:49:19 -05:00
|
|
|
|
2018-01-31 09:05:34 -05:00
|
|
|
private void updateAllInventorySlots(BufferedImage image) throws IOException {
|
2018-02-22 11:28:01 -05:00
|
|
|
for (int row = 0; row < Constants.INVENTORY_NUM_ROWS; row++) {
|
|
|
|
for (int column = 0; column < Constants.INVENTORY_NUM_COLUMNS; column++) {
|
2018-02-01 22:39:57 -05:00
|
|
|
inventorySlots[row][column].updateInventorySlot(image);
|
2018-02-22 11:28:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 11:58:54 -05:00
|
|
|
public void updateAndWriteAllInventoryImages() throws IOException {
|
2018-02-22 11:28:01 -05:00
|
|
|
BufferedImage image = robot.createScreenCapture(this.inventoryRectangleToCapture);
|
2018-02-22 11:58:54 -05:00
|
|
|
ImageIO.write(image, "png", new File("/home/dpapp/Desktop/RunescapeAI/Tests/Inventory/inventory_TO_RENAME.png"));
|
|
|
|
writeAllInventorySlotImages(image);
|
2018-02-22 11:28:01 -05:00
|
|
|
}
|
|
|
|
|
2018-02-22 11:58:54 -05:00
|
|
|
private void writeAllInventorySlotImages(BufferedImage image) throws IOException {
|
2018-02-22 11:28:01 -05:00
|
|
|
for (int row = 0; row < Constants.INVENTORY_NUM_ROWS; row++) {
|
|
|
|
for (int column = 0; column < Constants.INVENTORY_NUM_COLUMNS; column++) {
|
|
|
|
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-22 11:28:01 -05:00
|
|
|
for (int row = 0; row < Constants.INVENTORY_NUM_ROWS; row++) {
|
|
|
|
for (int column = 0; column < Constants.INVENTORY_NUM_COLUMNS; 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();
|
2018-02-22 11:28:01 -05:00
|
|
|
int x = Constants.INVENTORY_WINDOW_OFFSET_X + row * Constants.INVENTORY_SLOT_WIDTH + centerOfInventorySlot.x;
|
|
|
|
int y = Constants.INVENTORY_WINDOW_OFFSET_Y + column * Constants.INVENTORY_SLOT_HEIGHT + centerOfInventorySlot.y;
|
2018-02-02 06:27:29 -05:00
|
|
|
return new Point(x, y);
|
|
|
|
}
|
2018-02-22 11:28:01 -05:00
|
|
|
|
|
|
|
// For testing only
|
|
|
|
public void updateWithFakeImageForTests(BufferedImage testImage) throws IOException {
|
|
|
|
updateAllInventorySlots(testImage);
|
|
|
|
}
|
2018-01-31 08:12:02 -05:00
|
|
|
}
|