2018-02-19 20:55:50 -05:00
|
|
|
import java.awt.AWTException;
|
2018-02-24 10:49:49 -05:00
|
|
|
import java.awt.Graphics2D;
|
2018-02-19 20:55:50 -05:00
|
|
|
import java.awt.Point;
|
2018-02-22 09:28:21 -05:00
|
|
|
import java.awt.Rectangle;
|
|
|
|
import java.awt.Robot;
|
|
|
|
import java.awt.image.BufferedImage;
|
2018-02-23 17:04:28 -05:00
|
|
|
import java.awt.image.DataBufferByte;
|
2018-02-22 09:28:21 -05:00
|
|
|
import java.io.File;
|
2018-02-19 20:55:50 -05:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2018-02-22 09:28:21 -05:00
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
2018-02-24 10:49:49 -05:00
|
|
|
import org.opencv.core.Core;
|
|
|
|
import org.opencv.core.CvType;
|
2018-02-23 17:04:28 -05:00
|
|
|
import org.opencv.core.Mat;
|
2018-02-23 09:26:15 -05:00
|
|
|
import org.opencv.core.Rect2d;
|
2018-02-23 16:43:57 -05:00
|
|
|
import org.opencv.tracking.Tracker;
|
2018-02-24 14:42:05 -05:00
|
|
|
import org.opencv.tracking.TrackerBoosting;
|
|
|
|
import org.opencv.tracking.TrackerGOTURN;
|
2018-02-23 16:43:57 -05:00
|
|
|
import org.opencv.tracking.TrackerKCF;
|
2018-02-24 14:42:05 -05:00
|
|
|
import org.opencv.tracking.TrackerMOSSE;
|
2018-02-23 09:26:15 -05:00
|
|
|
|
2018-02-19 20:55:50 -05:00
|
|
|
public class IronMiner {
|
|
|
|
|
2018-02-23 16:43:57 -05:00
|
|
|
public static final int IRON_ORE_MINING_TIME_MILLISECONDS = 1320;
|
2018-02-22 09:28:21 -05:00
|
|
|
public static final int MAXIMUM_DISTANCE_TO_WALK_TO_IRON_ORE = 400;
|
2018-02-23 09:26:15 -05:00
|
|
|
public static final Point GAME_WINDOW_CENTER = new Point(Constants.GAME_WINDOW_WIDTH / 2, Constants.GAME_WINDOW_HEIGHT / 2);
|
2018-02-19 20:55:50 -05:00
|
|
|
|
|
|
|
Cursor cursor;
|
|
|
|
CursorTask cursorTask;
|
|
|
|
Inventory inventory;
|
2018-02-22 09:28:21 -05:00
|
|
|
ObjectDetector objectDetector;
|
|
|
|
Robot robot;
|
|
|
|
Randomizer randomizer;
|
2018-02-19 20:55:50 -05:00
|
|
|
|
|
|
|
public IronMiner() throws AWTException, IOException
|
|
|
|
{
|
2018-02-24 14:42:05 -05:00
|
|
|
//cursor = new Cursor();
|
|
|
|
//cursorTask = new CursorTask();
|
|
|
|
//inventory = new Inventory();
|
2018-02-22 09:28:21 -05:00
|
|
|
objectDetector = new ObjectDetector();
|
|
|
|
robot = new Robot();
|
|
|
|
randomizer = new Randomizer();
|
2018-02-19 20:55:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void run() throws Exception {
|
2018-02-24 10:49:49 -05:00
|
|
|
while (true) {
|
2018-02-23 16:43:57 -05:00
|
|
|
BufferedImage screenCapture = objectDetector.captureScreenshotGameWindow();
|
2018-02-24 14:42:05 -05:00
|
|
|
ArrayList<DetectedObject> detectedObjects = objectDetector.getObjectsInImage(screenCapture, 0.60);
|
2018-02-24 10:49:49 -05:00
|
|
|
ArrayList<DetectedObject> ironOres = objectDetector.getObjectsOfClassInList(detectedObjects, "ironOre");
|
|
|
|
|
|
|
|
DetectedObject closestIronOre = getClosestObjectToCharacter(ironOres);
|
2018-02-23 16:43:57 -05:00
|
|
|
if (closestIronOre != null) {
|
2018-02-24 10:49:49 -05:00
|
|
|
System.out.println("Found iron ore! Starting tracking!");
|
2018-02-24 14:42:05 -05:00
|
|
|
Tracker objectTracker = TrackerBoosting.create();
|
2018-02-24 10:49:49 -05:00
|
|
|
Rect2d boundingBox = closestIronOre.getBoundingRect2d();
|
2018-02-23 17:04:28 -05:00
|
|
|
objectTracker.init(getMatFromBufferedImage(screenCapture), boundingBox);
|
|
|
|
|
2018-02-24 14:42:05 -05:00
|
|
|
//cursor.moveAndLeftClickAtCoordinatesWithRandomness(closestIronOre.getCenterForClicking(), 10, 10);
|
2018-02-23 16:43:57 -05:00
|
|
|
|
|
|
|
long mineStartTime = System.currentTimeMillis();
|
|
|
|
int maxTimeToMine = randomizer.nextGaussianWithinRange(3500, 5000);
|
|
|
|
|
2018-02-23 17:04:28 -05:00
|
|
|
// track until either we lose the object or too much time passes
|
2018-02-24 10:49:49 -05:00
|
|
|
int lostTrackCounter = 0;
|
2018-02-24 14:42:05 -05:00
|
|
|
//while (((System.currentTimeMillis() - mineStartTime) < maxTimeToMine) && lostTrackCounter < 3) {
|
|
|
|
while (lostTrackCounter < 600) {
|
2018-02-24 10:49:49 -05:00
|
|
|
|
2018-02-24 14:42:05 -05:00
|
|
|
//screenCapture = objectDetector.captureScreenshotGameWindow();
|
|
|
|
detectedObjects = objectDetector.getObjectsInImage(screenCapture, 0.3); // Call with lower threshold value here
|
|
|
|
/*
|
|
|
|
* 600 iterations: 30 seconds
|
2018-02-23 17:04:28 -05:00
|
|
|
boolean ok = objectTracker.update(getMatFromBufferedImage(screenCapture), boundingBox);
|
2018-02-24 14:42:05 -05:00
|
|
|
if (!ok) {
|
2018-02-24 10:49:49 -05:00
|
|
|
System.out.println("Lost track for + " + lostTrackCounter + "! Finding new ore soon.");
|
|
|
|
lostTrackCounter++;
|
|
|
|
}
|
2018-02-24 14:42:05 -05:00
|
|
|
else if (!objectDetector.isObjectPresentInBoundingBoxInImage(detectedObjects, boundingBox, "ironOre")) {
|
|
|
|
System.out.println("Can't find object in bounding box. Error " + lostTrackCounter + "! Finding new ore soon.");
|
|
|
|
lostTrackCounter++;
|
|
|
|
}
|
2018-02-24 10:49:49 -05:00
|
|
|
else if (ok) {
|
2018-02-24 14:42:05 -05:00
|
|
|
//lostTrackCounter = 0;
|
2018-02-24 10:49:49 -05:00
|
|
|
System.out.println("Tracking at " + boundingBox.x + ", " + boundingBox.y + ", " + boundingBox.width + ", " + boundingBox.height);
|
2018-02-24 14:42:05 -05:00
|
|
|
}*/
|
|
|
|
System.out.println(lostTrackCounter);
|
|
|
|
lostTrackCounter++;
|
2018-02-23 16:43:57 -05:00
|
|
|
}
|
2018-02-24 14:42:05 -05:00
|
|
|
System.out.println("600 iterations took " + (System.currentTimeMillis() - mineStartTime));
|
|
|
|
break;
|
2018-02-23 16:43:57 -05:00
|
|
|
}
|
2018-02-19 20:55:50 -05:00
|
|
|
|
2018-02-24 14:42:05 -05:00
|
|
|
//dropInventoryIfFull();
|
2018-02-22 09:28:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void dropInventoryIfFull() throws Exception {
|
2018-02-24 14:42:05 -05:00
|
|
|
inventory.update(); // TODO: add iron ore to indonventory items
|
2018-02-22 09:28:21 -05:00
|
|
|
if (inventory.isInventoryFull()) {
|
|
|
|
cursorTask.optimizedDropAllItemsInInventory(cursor, inventory);
|
2018-02-19 20:55:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 09:26:15 -05:00
|
|
|
|
2018-02-23 16:43:57 -05:00
|
|
|
private void mineClosestIronOre(ArrayList<DetectedObject> ironOres) throws Exception {
|
2018-02-23 09:26:15 -05:00
|
|
|
DetectedObject closestIronOre = getClosestObjectToCharacter(ironOres);
|
2018-02-23 16:43:57 -05:00
|
|
|
if (closestIronOre != null) {
|
2018-02-23 09:26:15 -05:00
|
|
|
cursor.moveAndLeftClickAtCoordinatesWithRandomness(closestIronOre.getCenterForClicking(), 10, 10);
|
2018-02-23 16:43:57 -05:00
|
|
|
Thread.sleep(randomizer.nextGaussianWithinRange(IRON_ORE_MINING_TIME_MILLISECONDS - 350, IRON_ORE_MINING_TIME_MILLISECONDS + 1850));
|
2018-02-19 20:55:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 09:26:15 -05:00
|
|
|
private DetectedObject getClosestObjectToCharacter(ArrayList<DetectedObject> detectedObjects) {
|
|
|
|
int closestDistanceToCharacter = Integer.MAX_VALUE;
|
|
|
|
DetectedObject closestObjectToCharacter = null;
|
|
|
|
|
|
|
|
for (DetectedObject detectedObject : detectedObjects) {
|
|
|
|
int objectDistanceToCharacter = getDistanceBetweenPoints(GAME_WINDOW_CENTER, detectedObject.getCenterForClicking());
|
|
|
|
if (objectDistanceToCharacter < closestDistanceToCharacter) {
|
|
|
|
closestDistanceToCharacter = objectDistanceToCharacter;
|
|
|
|
closestObjectToCharacter = detectedObject;
|
2018-02-19 20:55:50 -05:00
|
|
|
}
|
|
|
|
}
|
2018-02-23 09:26:15 -05:00
|
|
|
if (closestObjectToCharacter != null && closestDistanceToCharacter < MAXIMUM_DISTANCE_TO_WALK_TO_IRON_ORE) {
|
|
|
|
return closestObjectToCharacter;
|
2018-02-19 20:55:50 -05:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-24 10:49:49 -05:00
|
|
|
private Mat getMatFromBufferedImage(BufferedImage image) {
|
|
|
|
BufferedImage formattedImage = convertBufferedImage(image, BufferedImage.TYPE_3BYTE_BGR);
|
|
|
|
byte[] data = ((DataBufferByte) formattedImage.getData().getDataBuffer()).getData();
|
|
|
|
bgr2rgb(data);
|
|
|
|
Mat matImage = new Mat(formattedImage.getWidth(), formattedImage.getHeight(), CvType.CV_8UC3);
|
|
|
|
byte[] pixels = ((DataBufferByte) formattedImage.getRaster().getDataBuffer()).getData();
|
2018-02-23 17:04:28 -05:00
|
|
|
matImage.put(0, 0, pixels);
|
|
|
|
return matImage;
|
|
|
|
}
|
|
|
|
|
2018-02-24 10:49:49 -05:00
|
|
|
private static BufferedImage convertBufferedImage(BufferedImage sourceImage, int bufferedImageType) {
|
|
|
|
BufferedImage image = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), bufferedImageType);
|
|
|
|
Graphics2D g2d = image.createGraphics();
|
|
|
|
g2d.drawImage(sourceImage, 0, 0, null);
|
|
|
|
g2d.dispose();
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void bgr2rgb(byte[] data) {
|
|
|
|
for (int i = 0; i < data.length; i += 3) {
|
|
|
|
byte tmp = data[i];
|
|
|
|
data[i] = data[i + 2];
|
|
|
|
data[i + 2] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 20:55:50 -05:00
|
|
|
public int getDistanceBetweenPoints(Point startingPoint, Point goalPoint) {
|
|
|
|
return (int) (Math.hypot(goalPoint.x - startingPoint.x, goalPoint.y - startingPoint.y));
|
|
|
|
}
|
|
|
|
}
|