refactored some code

This commit is contained in:
davpapp 2018-03-15 23:14:51 -04:00
parent 0f65ea8507
commit 8839f97456
24 changed files with 69 additions and 51 deletions

View File

@ -68,7 +68,6 @@ public class Cursor {
this.cursorPathsByDistance.get(cursorPath.getCursorPathDistance()).add(cursorPath);
}
// TODO: make sure these are reasonable
private int getRandomClickLength() {
return Randomizer.nextGaussianWithinRange(MINIMUM_CLICK_LENGTH, MAXIMUM_CLICK_LENGTH);
}
@ -76,7 +75,6 @@ public class Cursor {
private int getRandomClickReleaseLength() {
return Randomizer.nextGaussianWithinRange(MINIMUM_CLICK_LENGTH + 5, MAXIMUM_CLICK_LENGTH + 10);
}
// END
public void leftClickCursor() throws InterruptedException {
Thread.sleep(30, 55);
@ -240,12 +238,10 @@ public class Cursor {
}
private Point randomizePoint(Point goalPoint, int xTolerance, int yTolerance) {
Randomizer randomizer = new Randomizer();
return new Point(goalPoint.x + Randomizer.nextGaussianWithinRange(-xTolerance, xTolerance), goalPoint.y + Randomizer.nextGaussianWithinRange(-yTolerance, yTolerance));
}
private Point randomizePoint(Point goalPoint, int xToleranceLeft, int xToleranceRight, int yTolerance) {
Randomizer randomizer = new Randomizer();
return new Point(goalPoint.x + Randomizer.nextGaussianWithinRange(-xToleranceLeft, xToleranceRight), goalPoint.y + Randomizer.nextGaussianWithinRange(-yTolerance, yTolerance));
}

View File

@ -38,7 +38,6 @@ public class CursorTask {
}
}
public Point dropItem(Cursor cursor, Inventory inventory, int row, int column) throws Exception {
Point coordinatesToRightClick = inventory.getClickCoordinatesForInventorySlot(row, column);
Point clickedCoordinates = rightClickItemSlotWithRandomness(cursor, coordinatesToRightClick);

View File

@ -17,7 +17,6 @@ public class DetectedObject {
this.detectionClass = initializeLabel(detectionClass);
}
// TODO: migrate this all to a Rect2d data type
private Rect2d initializeBoundingBox(float[] detectionBox) {
int offset_x = (int) (detectionBox[1] * Constants.GAME_WINDOW_WIDTH);
int offset_y = (int) (detectionBox[0] * Constants.GAME_WINDOW_HEIGHT);

17
src/ImageCapturer.java Normal file
View File

@ -0,0 +1,17 @@
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
public class ImageCapturer {
public static BufferedImage captureScreenshotGameWindow() throws AWTException {
Rectangle rectangle = new Rectangle(Constants.GAME_WINDOW_OFFSET_X, Constants.GAME_WINDOW_OFFSET_Y, Constants.GAME_WINDOW_WIDTH, Constants.GAME_WINDOW_HEIGHT);
return new Robot().createScreenCapture(rectangle);
}
public static BufferedImage captureScreenRectangle(Rectangle rectangle) throws AWTException {
return new Robot().createScreenCapture(rectangle);
}
}

View File

@ -99,11 +99,12 @@ public class ImageCollector {
inventory.updateAndWriteAllInventoryImages();
}
public static void main(String[] args) throws Exception
/*public static void main(String[] args) throws Exception
{
ImageCollector imageCollector = new ImageCollector("/home/dpapp/Desktop/RunescapeAI/Images/");
imageCollector.collectImages("chatDialogue");
//imageCollector.generateInventoryImages();
//imageCollector.captureAndSaveFullWindow();
}
}*/
}

View File

@ -39,16 +39,17 @@ public class Inventory {
items = new InventoryItems(Paths.INVENTORY_ITEMS_DIRECTORY_PATH);
}
public void update() throws IOException {
/*public void update() throws IOException {
BufferedImage image = robot.createScreenCapture(this.inventoryRectangleToCapture);
updateAllInventorySlots(image);
}
}*/
public int getNumberOfItemsOfTypeInInventory(String itemType) throws IOException {
int numberOfItemsOfType = 0;
BufferedImage image = robot.createScreenCapture(this.inventoryRectangleToCapture);
for (int row = 0; row < Constants.INVENTORY_NUM_ROWS; row++) {
for (int column = 0; column < Constants.INVENTORY_NUM_COLUMNS; column++) {
inventorySlots[row][column].updateInventorySlot(image);
if (inventorySlots[row][column].getItemNameInInventorySlot(items).equals(itemType)) {
numberOfItemsOfType++;
@ -58,6 +59,19 @@ public class Inventory {
return numberOfItemsOfType++;
}
public void dropAllItemsOfType(String itemType, CursorTask cursorTask, Cursor cursor) throws Exception {
BufferedImage image = robot.createScreenCapture(this.inventoryRectangleToCapture);
for (int row = 0; row < Constants.INVENTORY_NUM_ROWS; row++) {
for (int column = 0; column < Constants.INVENTORY_NUM_COLUMNS; column++) {
inventorySlots[row][column].updateInventorySlot(image);
if (inventorySlots[row][column].getItemNameInInventorySlot(items).equals(itemType)) {
cursorTask.dropItem(cursor, this, row, column);
}
}
}
}
public int getFirstIronOreInInventory() throws IOException {
BufferedImage image = robot.createScreenCapture(this.inventoryRectangleToCapture);
for (int row = 0; row < Constants.INVENTORY_NUM_ROWS; row++) {
@ -126,6 +140,8 @@ public class Inventory {
return true;
}
public Point getClickCoordinatesForInventorySlot(int row, int column) {
Point centerOfInventorySlot = inventorySlots[row][column].getClickablePointWithinItemSlot();
int x = Constants.INVENTORY_WINDOW_OFFSET_X + row * Constants.INVENTORY_SLOT_WIDTH + centerOfInventorySlot.x;

View File

@ -33,7 +33,6 @@ public class IronMiner {
Robot robot;
HumanBehavior humanBehavior;
CameraCalibrator cameraCalibrator;
RandomDetector randomDetector;
MiningSuccessHistory miningSuccessHistory;
ObjectDetectionHistory objectDetectionHistory;
@ -46,7 +45,6 @@ public class IronMiner {
objectDetector = new ObjectDetector();
robot = new Robot();
humanBehavior = new HumanBehavior();
randomDetector = new RandomDetector();
cameraCalibrator = new CameraCalibrator(targetNumberOfDetectedOres);
miningSuccessHistory = new MiningSuccessHistory();
objectDetectionHistory = new ObjectDetectionHistory(targetNumberOfDetectedOres);
@ -54,16 +52,18 @@ public class IronMiner {
public void run() throws Exception {
long startTime = System.currentTimeMillis();
int worldHopCounter = 0;
int count = 0;
while (((System.currentTimeMillis() - startTime) / 1000.0 / 60) < 93) {
BufferedImage screenCapture = objectDetector.captureScreenshotGameWindow();
BufferedImage screenCapture = ImageCapturer.captureScreenshotGameWindow();
ArrayList<DetectedObject> detectedObjects = objectDetector.getObjectsInImage(screenCapture, 0.30);
ArrayList<DetectedObject> ironOres = objectDetector.getIronOres(detectedObjects);
readjustCameraIfObjectsAreNotBeingDetected(detectedObjects.size());
humanBehavior.randomlyCheckMiningXP(cursor);
RandomDetector.dealWithRandoms(screenCapture, cursor);
dropInventoryIfCloseToFull();
DetectedObject closestIronOre = getClosestObjectToCharacter(ironOres);
if (closestIronOre != null) {
@ -109,15 +109,10 @@ public class IronMiner {
count++;
System.out.println("Ores in inventory: " + numberOfOresInInventoryBefore + ". Mining success? " + miningSuccess);
boolean worldHopped = hopWorldsIfMiningSuccessRateIsLow(miningSuccess);
if (worldHopped) {
worldHopCounter++;
}
System.out.println(count + ", time: " + ((System.currentTimeMillis() - startTime) / 1000 / 60) + ". Hops: " + worldHopCounter);
printMiningStats(count, startTime);
hopWorldsIfMiningSuccessRateIsLow(miningSuccess);
}
humanBehavior.randomlyCheckMiningXP(cursor);
randomDetector.dealWithRandoms(screenCapture, cursor);
dropInventoryIfFull();
}
}
@ -140,14 +135,24 @@ public class IronMiner {
}
}
private void dropInventoryIfFull() throws Exception {
/*private void dropInventoryIfFull() throws Exception {
inventory.updateLastSlot();
if (inventory.isLastSlotInInventoryFull()) {
cursorTask.optimizedDropAllItemsInInventory(cursor, inventory);
Thread.sleep(Randomizer.nextGaussianWithinRange(1104, 1651));
}
}*/
private void dropInventoryIfCloseToFull() throws Exception {
if (inventory.getNumberOfItemsOfTypeInInventory("ironOre") > 15) {
inventory.dropAllItemsOfType("ironOre", cursorTask, cursor);
Thread.sleep(Randomizer.nextGaussianWithinRange(1104, 1651));
}
}
private void printMiningStats(int numberOfIronOresMined, long startTime) {
System.out.println(numberOfIronOresMined + " ores mined in " + ((System.currentTimeMillis() - startTime) / 1000 / 60) + " minutes");
}
private DetectedObject getClosestObjectToCharacter(ArrayList<DetectedObject> detectedObjects) {
int closestDistanceToCharacter = Integer.MAX_VALUE;

View File

@ -146,9 +146,4 @@ public class ObjectDetector {
data[i + 2] = tmp;
}
}
public BufferedImage captureScreenshotGameWindow() throws IOException, AWTException {
Rectangle area = new Rectangle(Constants.GAME_WINDOW_OFFSET_X, Constants.GAME_WINDOW_OFFSET_Y, Constants.GAME_WINDOW_WIDTH, Constants.GAME_WINDOW_HEIGHT);
return robot.createScreenCapture(area);
}
}

View File

@ -29,7 +29,7 @@ public class PathTransformer {
Matrix lhs = new Matrix(lhsArray);
Matrix rhs = new Matrix(rhsArray, 3);
System.out.println("(" + p1.getX() + "," + p1.getY() + "), (" + p2.getX() + "," + p2.getY() + "), (" + p3.getX() + "," + p3.getY() + ")");
//System.out.println("(" + p1.getX() + "," + p1.getY() + "), (" + p2.getX() + "," + p2.getY() + "), (" + p3.getX() + "," + p3.getY() + ")");
Matrix ans = lhs.solve(rhs);
/*System.out.println("x = " + ans.get(0, 0));

View File

@ -11,13 +11,8 @@ import java.util.ArrayList;
import javax.imageio.ImageIO;
public class RandomDetector {
Robot robot;
public RandomDetector() throws AWTException {
robot = new Robot();
}
public void dealWithRandoms(BufferedImage screenCapture, Cursor cursor) throws Exception {
public static void dealWithRandoms(BufferedImage screenCapture, Cursor cursor) throws Exception {
Point chatDialogueCornerPoint = findChatDialogueCornerPoint(screenCapture);
Point speakerPoint = findSpeakerPointFromCornerPoint(screenCapture, chatDialogueCornerPoint);
@ -35,9 +30,9 @@ public class RandomDetector {
}
}
private boolean dialogueHasDismissOption(Point speakerPoint) throws IOException, AWTException {
private static boolean dialogueHasDismissOption(Point speakerPoint) throws IOException, AWTException {
Rectangle dialogueRectangle = new Rectangle(Constants.GAME_WINDOW_OFFSET_X + speakerPoint.x - 25, Constants.GAME_WINDOW_OFFSET_Y + speakerPoint.y, 95, 55);
BufferedImage dialogueCapture = robot.createScreenCapture(dialogueRectangle);
BufferedImage dialogueCapture = ImageCapturer.captureScreenRectangle(dialogueRectangle);
for (int x = 0; x < 95; x++) {
for (int y = 0; y < 55; y++) {
int pixelColor = dialogueCapture.getRGB(x, y);
@ -49,7 +44,7 @@ public class RandomDetector {
return false;
}
public Point findChatDialogueCornerPoint(BufferedImage screenCapture) throws AWTException, InterruptedException {
public static Point findChatDialogueCornerPoint(BufferedImage screenCapture) throws AWTException, InterruptedException {
for (int x = 30; x < Constants.GAME_WINDOW_WIDTH - 30; x++) {
for (int y = 20; y < Constants.GAME_WINDOW_HEIGHT - 20; y++) {
int pixelColor = screenCapture.getRGB(x, y);
@ -61,14 +56,14 @@ public class RandomDetector {
return null;
}
private boolean isPixelChatColor(int color) {
private static boolean isPixelChatColor(int color) {
int[] colorChannels = getRGBChannelsFromPixel(color);
return (isColorWithinTolerance(colorChannels[0], 0, 3) &&
isColorWithinTolerance(colorChannels[1], 255, 3) &&
isColorWithinTolerance(colorChannels[2], 255, 3));
}
public Point findSpeakerPointFromCornerPoint(BufferedImage screenCapture, Point chatDialogueStart) {
public static Point findSpeakerPointFromCornerPoint(BufferedImage screenCapture, Point chatDialogueStart) {
if (chatDialogueStart == null) {
return null;
}
@ -93,25 +88,20 @@ public class RandomDetector {
return null;
}
private boolean isSpeakerPointCloseToCharacter(Point speakerPoint) {
private static boolean isSpeakerPointCloseToCharacter(Point speakerPoint) {
return (Math.abs(speakerPoint.x + Constants.GAME_WINDOW_OFFSET_X - Constants.CHARACTER_CENTER_X) < 90 && Math.abs(speakerPoint.y + Constants.GAME_WINDOW_OFFSET_Y - Constants.CHARACTER_CENTER_Y) < 80);
}
private Point getDismissOptionClickLocation(Point speakerLocation) {
private static Point getDismissOptionClickLocation(Point speakerLocation) {
return new Point(speakerLocation.x, speakerLocation.y + 46);
}
private boolean isColorWithinTolerance(int color1, int color2, int tolerance) {
private static boolean isColorWithinTolerance(int color1, int color2, int tolerance) {
return (Math.abs(color1 - color2) < tolerance);
}
private int[] getRGBChannelsFromPixel(int pixel) {
private static int[] getRGBChannelsFromPixel(int pixel) {
int[] colors = {(pixel)&0xFF, (pixel>>8)&0xFF, (pixel>>16)&0xFF, (pixel>>24)&0xFF};
return colors;
}
public BufferedImage captureScreenshotGameWindow() throws IOException, AWTException {
Rectangle area = new Rectangle(Constants.GAME_WINDOW_OFFSET_X, Constants.GAME_WINDOW_OFFSET_Y, Constants.GAME_WINDOW_WIDTH, Constants.GAME_WINDOW_HEIGHT);
return robot.createScreenCapture(area);
}
}

View File

@ -32,7 +32,7 @@ public class TrackerThread implements Runnable {
int oreLostCount = 0;
while (!objectTrackingFailure && oreLostCount < 3 && !isTimeElapsedOverLimit(miningStartTime, maxTimeToMine)) {
long frameStartTime = System.currentTimeMillis();
screenCapture = objectDetector.captureScreenshotGameWindow();
screenCapture = ImageCapturer.captureScreenshotGameWindow();
ArrayList<DetectedObject> detectedObjects = objectDetector.getObjectsInImage(screenCapture, 0.15);
ArrayList<DetectedObject> ironOres = objectDetector.getObjectsOfClassInList(detectedObjects, "ironOre");
objectTrackingFailure = ironOreTracker.update(screenCapture, boundingBox);

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.