mirror of
https://github.com/davpapp/PowerMiner
synced 2025-01-08 12:28:06 -05:00
Beginnings of multithreading have begun
This commit is contained in:
parent
957ddd61e1
commit
ae260edfc0
40
src/DropperThread.java
Normal file
40
src/DropperThread.java
Normal file
@ -0,0 +1,40 @@
|
||||
import java.io.IOException;
|
||||
|
||||
public class DropperThread implements Runnable {
|
||||
Thread dropperThread;
|
||||
Inventory inventory;
|
||||
Cursor cursor;
|
||||
|
||||
public DropperThread(Inventory inventory, Cursor cursor) {
|
||||
this.inventory = inventory;
|
||||
this.cursor = cursor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
inventory.update();
|
||||
cursor.moveAndRightlickAtCoordinatesWithRandomness(inventory.getClickCoordinatesForInventorySlot(0, 0), 15, 15);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("~~~~~~~~~~~~~ dropperThread finished!");
|
||||
}
|
||||
|
||||
public void start() {
|
||||
System.out.println("dropperThread started");
|
||||
if (dropperThread == null) {
|
||||
dropperThread = new Thread(this, "dropperThread");
|
||||
dropperThread.start();
|
||||
}
|
||||
}
|
||||
|
||||
public void waitTillDone() throws InterruptedException {
|
||||
dropperThread.join();
|
||||
}
|
||||
|
||||
}
|
@ -46,7 +46,6 @@ public class IronMiner {
|
||||
humanBehavior = new HumanBehavior();
|
||||
cameraCalibrator = new CameraCalibrator();
|
||||
randomDetector = new RandomDetector();
|
||||
worldHopper = new WorldHopper();
|
||||
}
|
||||
|
||||
public void run() throws Exception {
|
||||
@ -73,37 +72,18 @@ public class IronMiner {
|
||||
|
||||
DetectedObject closestIronOre = getClosestObjectToCharacter(ironOres);
|
||||
if (closestIronOre != null) {
|
||||
Rect2d boundingBox = closestIronOre.getBoundingRect2d();
|
||||
ObjectTracker ironOreTracker = new ObjectTracker(screenCapture, boundingBox);
|
||||
|
||||
cursor.moveAndLeftClickAtCoordinatesWithRandomness(closestIronOre.getCenterForClicking(), 10, 10);
|
||||
|
||||
long miningStartTime = System.currentTimeMillis();
|
||||
int maxTimeToMine = Randomizer.nextGaussianWithinRange(3400, 4519);
|
||||
System.out.println("Starting threads!");
|
||||
TrackerThread trackerThread = new TrackerThread(screenCapture, closestIronOre, objectDetector);
|
||||
trackerThread.start();
|
||||
DropperThread dropperThread = new DropperThread(inventory, cursor);
|
||||
dropperThread.start();
|
||||
|
||||
boolean objectTrackingFailure = false;
|
||||
boolean oreAvailable = true;
|
||||
int oreLostCount = 0;
|
||||
while (!objectTrackingFailure && oreLostCount < 3 && !isTimeElapsedOverLimit(miningStartTime, maxTimeToMine)) {
|
||||
long trackingFrameStartTime = System.currentTimeMillis();
|
||||
|
||||
screenCapture = objectDetector.captureScreenshotGameWindow();
|
||||
detectedObjects = objectDetector.getObjectsInImage(screenCapture, 0.15);
|
||||
ironOres = objectDetector.getObjectsOfClassInList(detectedObjects, "ironOre");
|
||||
objectTrackingFailure = ironOreTracker.update(screenCapture, boundingBox);
|
||||
oreAvailable = objectDetector.isObjectPresentInBoundingBoxInImage(ironOres, boundingBox, "ironOre");
|
||||
if (!oreAvailable) {
|
||||
oreLostCount++;
|
||||
}
|
||||
else {
|
||||
oreLostCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
inventory.update();
|
||||
if (!inventory.inventorySlotIsEmpty(0, 0)) {
|
||||
cursorTask.dropOre(cursor, inventory);
|
||||
}
|
||||
trackerThread.waitTillDone();
|
||||
dropperThread.waitTillDone();
|
||||
|
||||
System.out.println("Both threads finished?");
|
||||
}
|
||||
|
||||
humanBehavior.randomlyCheckMiningXP(cursor);
|
||||
@ -116,6 +96,46 @@ public class IronMiner {
|
||||
System.out.println("Completed full mining session.");
|
||||
}
|
||||
|
||||
/*private void dropOre() throws Exception {
|
||||
inventory.update();
|
||||
System.out.println("Thread 1 [mouse hover] finished!");
|
||||
//cursorTask.dropOre(cursor, inventory);
|
||||
}*/
|
||||
|
||||
/*private void trackOre(DetectedObject closestIronOre, BufferedImage screenCapture) throws Exception {
|
||||
Rect2d boundingBox = closestIronOre.getBoundingRect2d();
|
||||
ObjectTracker ironOreTracker = new ObjectTracker(screenCapture, boundingBox);
|
||||
|
||||
long miningStartTime = System.currentTimeMillis();
|
||||
int maxTimeToMine = Randomizer.nextGaussianWithinRange(3400, 4519);
|
||||
|
||||
boolean objectTrackingFailure = false;
|
||||
boolean oreAvailable = true;
|
||||
int oreLostCount = 0;
|
||||
while (!objectTrackingFailure && oreLostCount < 3 && !isTimeElapsedOverLimit(miningStartTime, maxTimeToMine)) {
|
||||
BufferedImage screenCapture2 = objectDetector.captureScreenshotGameWindow();
|
||||
ArrayList<DetectedObject> detectedObjects = objectDetector.getObjectsInImage(screenCapture2, 0.15);
|
||||
ArrayList<DetectedObject> ironOres = objectDetector.getObjectsOfClassInList(detectedObjects, "ironOre");
|
||||
objectTrackingFailure = ironOreTracker.update(screenCapture, boundingBox);
|
||||
oreAvailable = objectDetector.isObjectPresentInBoundingBoxInImage(ironOres, boundingBox, "ironOre");
|
||||
if (!oreAvailable) {
|
||||
oreLostCount++;
|
||||
}
|
||||
else {
|
||||
oreLostCount = 0;
|
||||
}
|
||||
}
|
||||
System.out.println("Thread 2 [track ore] finished!");
|
||||
}*/
|
||||
|
||||
/*private void dropOre() throws IOException {
|
||||
inventory.update();
|
||||
if (inventory.containsItem("ironOre")) {
|
||||
cursor.moveAndRightlickAtCoordinatesWithRandomness(inventory.getClickCoordinatesForInventorySlot(0, 0), 15, 15);
|
||||
// move cursor down 40 pixels
|
||||
cursor.moveAndLeftClickAtCoordinatesWithRandomness(goalPoint, 20, 5);
|
||||
}
|
||||
}*/
|
||||
|
||||
private void dropInventoryIfFull() throws Exception {
|
||||
inventory.updateLastSlot();
|
||||
|
13
src/MultiThreadingExample.java
Normal file
13
src/MultiThreadingExample.java
Normal file
@ -0,0 +1,13 @@
|
||||
|
||||
public class MultiThreadingExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
System.out.println("Starting multithreading...");
|
||||
TrackerThread trackerThread = new TrackerThread();
|
||||
trackerThread.start();
|
||||
DropperThread dropperThread = new DropperThread();
|
||||
dropperThread.start();
|
||||
}
|
||||
|
||||
}
|
77
src/TrackerThread.java
Normal file
77
src/TrackerThread.java
Normal file
@ -0,0 +1,77 @@
|
||||
import java.awt.AWTException;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.opencv.core.Rect2d;
|
||||
|
||||
public class TrackerThread implements Runnable {
|
||||
Thread trackerThread;
|
||||
BufferedImage screenCapture;
|
||||
DetectedObject closestIronOre;
|
||||
ObjectDetector objectDetector;
|
||||
|
||||
public TrackerThread(BufferedImage screenCapture, DetectedObject closestIronOre, ObjectDetector objectDetector) {
|
||||
this.screenCapture = screenCapture;
|
||||
this.closestIronOre = closestIronOre;
|
||||
this.objectDetector = objectDetector;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Rect2d boundingBox = closestIronOre.getBoundingRect2d();
|
||||
ObjectTracker ironOreTracker;
|
||||
try {
|
||||
ironOreTracker = new ObjectTracker(screenCapture, boundingBox);
|
||||
|
||||
long miningStartTime = System.currentTimeMillis();
|
||||
int maxTimeToMine = Randomizer.nextGaussianWithinRange(3400, 4519);
|
||||
|
||||
boolean objectTrackingFailure = false;
|
||||
boolean oreAvailable = true;
|
||||
int oreLostCount = 0;
|
||||
while (!objectTrackingFailure && oreLostCount < 3 && !isTimeElapsedOverLimit(miningStartTime, maxTimeToMine)) {
|
||||
BufferedImage screenCapture2 = objectDetector.captureScreenshotGameWindow();
|
||||
ArrayList<DetectedObject> detectedObjects = objectDetector.getObjectsInImage(screenCapture2, 0.15);
|
||||
ArrayList<DetectedObject> ironOres = objectDetector.getObjectsOfClassInList(detectedObjects, "ironOre");
|
||||
objectTrackingFailure = ironOreTracker.update(screenCapture, boundingBox);
|
||||
oreAvailable = objectDetector.isObjectPresentInBoundingBoxInImage(ironOres, boundingBox, "ironOre");
|
||||
if (!oreAvailable) {
|
||||
oreLostCount++;
|
||||
}
|
||||
else {
|
||||
oreLostCount = 0;
|
||||
}
|
||||
System.out.println("trackerThread working...");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (AWTException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
System.out.println("~~~~~~~~~~~~~ trackerThread finished!");
|
||||
}
|
||||
|
||||
private boolean isTimeElapsedOverLimit(long startTime, int timeLimit) {
|
||||
return (System.currentTimeMillis() - startTime) > timeLimit;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
System.out.println("TrackerThread started");
|
||||
if (trackerThread == null) {
|
||||
trackerThread = new Thread(this, "trackerThread");
|
||||
trackerThread.start();
|
||||
}
|
||||
}
|
||||
|
||||
public void waitTillDone() throws InterruptedException {
|
||||
trackerThread.join();
|
||||
}
|
||||
|
||||
}
|
@ -10,7 +10,7 @@ public class main {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.out.println("Starting Iron Miner.");
|
||||
System.load("/usr/local/cuda/lib64/libcublas.so.9.0");
|
||||
System.load("/usr/local/cuda/lib64/libcublas.so.9.0");
|
||||
System.load("/usr/local/cuda/lib64/libcusolver.so.9.0");
|
||||
System.load("/usr/local/cuda/lib64/libcudart.so.9.0");
|
||||
System.load("/usr/local/cuda/lib64/libcufft.so.9.0");
|
||||
@ -20,19 +20,7 @@ public class main {
|
||||
|
||||
IronMiner ironMiner = new IronMiner();
|
||||
ironMiner.run();
|
||||
/*Cursor cursor = new Cursor();
|
||||
CursorTask cursorTask = new CursorTask();
|
||||
Inventory inventory = new Inventory();
|
||||
|
||||
while (true) {
|
||||
inventory.update();
|
||||
if (inventory.isInventoryFull()) {
|
||||
System.out.println("Inventory full. Emptying...");
|
||||
cursorTask.optimizedDropAllItemsInInventory(cursor, inventory);
|
||||
}
|
||||
Thread.sleep(100);
|
||||
} */
|
||||
//cursor.moveCursorToCoordinates(new Point(620, 420));
|
||||
|
||||
System.out.println("Finished everything!");
|
||||
}
|
||||
}
|
||||
|
BIN
target/classes/DropperThread.class
Normal file
BIN
target/classes/DropperThread.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
target/classes/MultiThreadingExample.class
Normal file
BIN
target/classes/MultiThreadingExample.class
Normal file
Binary file not shown.
BIN
target/classes/TrackerThread.class
Normal file
BIN
target/classes/TrackerThread.class
Normal file
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user