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-04-14 08:41:07 -04:00
|
|
|
public static final int MAXIMUM_DISTANCE_TO_WALK_TO_IRON_ORE = 400;
|
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;
|
2018-03-07 00:51:47 -05:00
|
|
|
HumanBehavior humanBehavior;
|
|
|
|
CameraCalibrator cameraCalibrator;
|
2018-03-14 04:18:07 -04:00
|
|
|
MiningSuccessHistory miningSuccessHistory;
|
2018-03-14 03:17:51 -04:00
|
|
|
ObjectDetectionHistory objectDetectionHistory;
|
2018-02-19 20:55:50 -05:00
|
|
|
|
|
|
|
public IronMiner() throws AWTException, IOException
|
|
|
|
{
|
2018-03-27 08:08:19 -04:00
|
|
|
int targetNumberOfDetectedOres = 3;
|
2018-03-04 18:08:04 -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();
|
2018-03-07 00:51:47 -05:00
|
|
|
humanBehavior = new HumanBehavior();
|
2018-03-14 03:17:51 -04:00
|
|
|
cameraCalibrator = new CameraCalibrator(targetNumberOfDetectedOres);
|
2018-03-14 04:18:07 -04:00
|
|
|
miningSuccessHistory = new MiningSuccessHistory();
|
2018-03-14 03:17:51 -04:00
|
|
|
objectDetectionHistory = new ObjectDetectionHistory(targetNumberOfDetectedOres);
|
2018-02-19 20:55:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void run() throws Exception {
|
2018-03-05 05:20:29 -05:00
|
|
|
long startTime = System.currentTimeMillis();
|
|
|
|
|
2018-03-12 05:54:47 -04:00
|
|
|
int count = 0;
|
2018-03-21 04:56:48 -04:00
|
|
|
int worldHops = 0;
|
2018-03-27 08:08:19 -04:00
|
|
|
int lastIronOreInInventory = -1;
|
2018-04-14 08:41:07 -04:00
|
|
|
int noIronOresCount = 0;
|
2018-03-21 04:56:48 -04:00
|
|
|
|
2018-04-14 08:41:07 -04:00
|
|
|
while (((System.currentTimeMillis() - startTime) / 1000.0 / 60) < 163) {
|
2018-03-15 23:14:51 -04:00
|
|
|
BufferedImage screenCapture = ImageCapturer.captureScreenshotGameWindow();
|
2018-03-13 06:43:04 -04:00
|
|
|
ArrayList<DetectedObject> detectedObjects = objectDetector.getObjectsInImage(screenCapture, 0.30);
|
2018-03-12 05:54:47 -04:00
|
|
|
ArrayList<DetectedObject> ironOres = objectDetector.getIronOres(detectedObjects);
|
2018-02-24 10:49:49 -05:00
|
|
|
|
2018-03-14 03:17:51 -04:00
|
|
|
readjustCameraIfObjectsAreNotBeingDetected(detectedObjects.size());
|
2018-03-15 23:14:51 -04:00
|
|
|
humanBehavior.randomlyCheckMiningXP(cursor);
|
2018-03-27 08:08:19 -04:00
|
|
|
humanBehavior.randomlyRotateCamera(cameraCalibrator);
|
2018-03-15 23:14:51 -04:00
|
|
|
RandomDetector.dealWithRandoms(screenCapture, cursor);
|
|
|
|
dropInventoryIfCloseToFull();
|
2018-04-14 08:41:07 -04:00
|
|
|
|
|
|
|
/*if (noIronOresCount > 10000) {
|
|
|
|
return;
|
|
|
|
}*/
|
2018-03-15 23:14:51 -04:00
|
|
|
|
2018-03-05 05:20:29 -05:00
|
|
|
DetectedObject closestIronOre = getClosestObjectToCharacter(ironOres);
|
2018-03-13 11:15:08 -04:00
|
|
|
|
2018-02-23 16:43:57 -05:00
|
|
|
if (closestIronOre != null) {
|
2018-04-14 08:41:07 -04:00
|
|
|
noIronOresCount = 0;
|
2018-02-28 00:16:57 -05:00
|
|
|
cursor.moveAndLeftClickAtCoordinatesWithRandomness(closestIronOre.getCenterForClicking(), 10, 10);
|
2018-02-23 16:43:57 -05:00
|
|
|
|
2018-03-27 08:08:19 -04:00
|
|
|
int ironOreInInventory = inventory.getFirstIronOreInInventoryDifferentFromLast(lastIronOreInInventory);
|
|
|
|
lastIronOreInInventory = ironOreInInventory;
|
2018-03-15 20:52:57 -04:00
|
|
|
|
2018-03-14 04:18:07 -04:00
|
|
|
int numberOfOresInInventoryBefore = inventory.getNumberOfItemsOfTypeInInventory("ironOre");
|
2018-03-15 20:52:57 -04:00
|
|
|
boolean miningSuccess = false;
|
2018-03-13 06:43:04 -04:00
|
|
|
|
2018-03-14 03:17:51 -04:00
|
|
|
if (ironOreInInventory > -1) {
|
|
|
|
int ironOreInInventoryColumn = ironOreInInventory % 7;
|
|
|
|
int ironOreInInventoryRow = ironOreInInventory / 7;
|
|
|
|
|
2018-03-21 04:56:48 -04:00
|
|
|
Point rightClickItemClickLocation = inventory.getClickCoordinatesForInventorySlot(ironOreInInventoryRow, ironOreInInventoryColumn);
|
|
|
|
|
2018-03-14 03:17:51 -04:00
|
|
|
TrackerThread trackerThread = new TrackerThread(screenCapture, closestIronOre, objectDetector);
|
|
|
|
trackerThread.start();
|
|
|
|
|
2018-03-21 04:56:48 -04:00
|
|
|
DropperThread dropperThread = new DropperThread(rightClickItemClickLocation, cursor, cursorTask);
|
2018-03-14 03:17:51 -04:00
|
|
|
dropperThread.start();
|
|
|
|
|
|
|
|
trackerThread.waitTillDone();
|
|
|
|
dropperThread.waitTillDone();
|
|
|
|
|
2018-03-21 04:56:48 -04:00
|
|
|
cursor.leftClickCursor();
|
2018-03-14 04:18:07 -04:00
|
|
|
|
2018-03-15 20:52:57 -04:00
|
|
|
if (inventory.getNumberOfItemsOfTypeInInventory("ironOre") >= numberOfOresInInventoryBefore) {
|
|
|
|
miningSuccess = true;
|
|
|
|
}
|
2018-03-14 03:17:51 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
TrackerThread trackerThread = new TrackerThread(screenCapture, closestIronOre, objectDetector);
|
|
|
|
trackerThread.start();
|
|
|
|
trackerThread.waitTillDone();
|
2018-03-14 04:18:07 -04:00
|
|
|
|
2018-03-15 20:52:57 -04:00
|
|
|
if (inventory.getNumberOfItemsOfTypeInInventory("ironOre") > numberOfOresInInventoryBefore) {
|
|
|
|
miningSuccess = true;
|
|
|
|
}
|
2018-03-14 03:17:51 -04:00
|
|
|
}
|
|
|
|
count++;
|
2018-03-14 04:18:07 -04:00
|
|
|
|
2018-03-27 08:08:19 -04:00
|
|
|
//System.out.println("Ores in inventory: " + numberOfOresInInventoryBefore + ". Mining success? " + miningSuccess);
|
2018-03-15 23:14:51 -04:00
|
|
|
printMiningStats(count, startTime);
|
|
|
|
|
2018-03-21 04:56:48 -04:00
|
|
|
boolean worldHopped = hopWorldsIfMiningSuccessRateIsLow(miningSuccess);
|
|
|
|
if (worldHopped) {
|
|
|
|
worldHops++;
|
|
|
|
}
|
|
|
|
System.out.println("worldHops: " + worldHops);
|
2018-03-12 05:54:47 -04:00
|
|
|
}
|
2018-04-14 08:41:07 -04:00
|
|
|
else {
|
|
|
|
noIronOresCount++;
|
|
|
|
}
|
2018-02-22 09:28:21 -05:00
|
|
|
}
|
|
|
|
}
|
2018-03-14 03:17:51 -04:00
|
|
|
|
2018-03-15 20:52:57 -04:00
|
|
|
private boolean hopWorldsIfMiningSuccessRateIsLow(boolean miningSuccess) throws Exception {
|
2018-03-14 04:18:07 -04:00
|
|
|
boolean hopWorld = miningSuccessHistory.updateHistory(miningSuccess);
|
|
|
|
if (hopWorld) {
|
2018-03-15 20:52:57 -04:00
|
|
|
System.out.println("Hopping worlds");
|
|
|
|
WorldHopper.hopWorld(cursor);
|
2018-03-14 04:18:07 -04:00
|
|
|
miningSuccessHistory.resetQueue();
|
2018-03-15 20:52:57 -04:00
|
|
|
return true;
|
2018-03-14 04:18:07 -04:00
|
|
|
}
|
2018-03-15 20:52:57 -04:00
|
|
|
return false;
|
2018-03-14 04:18:07 -04:00
|
|
|
}
|
2018-02-22 09:28:21 -05:00
|
|
|
|
2018-03-14 03:17:51 -04:00
|
|
|
private void readjustCameraIfObjectsAreNotBeingDetected(int detectedObjectsSize) throws Exception {
|
|
|
|
boolean readjustCamera = objectDetectionHistory.updateHistory(detectedObjectsSize);
|
|
|
|
if (readjustCamera) {
|
|
|
|
cameraCalibrator.rotateUntilObjectFound(objectDetector, "ironOre");
|
|
|
|
objectDetectionHistory.resetQueue();
|
2018-03-13 10:33:32 -04:00
|
|
|
}
|
2018-03-14 03:17:51 -04:00
|
|
|
}
|
|
|
|
|
2018-03-15 23:14:51 -04:00
|
|
|
/*private void dropInventoryIfFull() throws Exception {
|
2018-03-05 05:20:29 -05:00
|
|
|
inventory.updateLastSlot();
|
|
|
|
if (inventory.isLastSlotInInventoryFull()) {
|
2018-02-22 09:28:21 -05:00
|
|
|
cursorTask.optimizedDropAllItemsInInventory(cursor, inventory);
|
2018-03-07 00:51:47 -05:00
|
|
|
Thread.sleep(Randomizer.nextGaussianWithinRange(1104, 1651));
|
2018-02-19 20:55:50 -05:00
|
|
|
}
|
2018-03-15 23:14:51 -04:00
|
|
|
}*/
|
|
|
|
|
|
|
|
private void dropInventoryIfCloseToFull() throws Exception {
|
2018-03-21 04:56:48 -04:00
|
|
|
if (inventory.getNumberOfItemsOfTypeInInventory("ironOre") > 12) {
|
2018-03-15 23:14:51 -04:00
|
|
|
inventory.dropAllItemsOfType("ironOre", cursorTask, cursor);
|
|
|
|
Thread.sleep(Randomizer.nextGaussianWithinRange(1104, 1651));
|
|
|
|
}
|
2018-02-19 20:55:50 -05:00
|
|
|
}
|
2018-03-14 03:17:51 -04:00
|
|
|
|
2018-03-15 23:14:51 -04:00
|
|
|
private void printMiningStats(int numberOfIronOresMined, long startTime) {
|
|
|
|
System.out.println(numberOfIronOresMined + " ores mined in " + ((System.currentTimeMillis() - startTime) / 1000 / 60) + " minutes");
|
|
|
|
}
|
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) {
|
2018-03-07 00:51:47 -05:00
|
|
|
int objectDistanceToCharacter = getDistanceBetweenPoints(Constants.getCharacterLocation(), detectedObject.getCenterForClicking());
|
2018-02-23 09:26:15 -05:00
|
|
|
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-03-27 08:08:19 -04:00
|
|
|
/*public isDetectedObjectInRange(int maxDistance) {
|
|
|
|
if (getDistanceBetweenPoints()
|
|
|
|
}*/
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|