-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinecraftOpenCVAI.cpp
More file actions
599 lines (534 loc) · 23.2 KB
/
MinecraftOpenCVAI.cpp
File metadata and controls
599 lines (534 loc) · 23.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include "opencv2/imgproc.hpp"
#include <opencv2/dnn.hpp>
#include <opencv2/cudaimgproc.hpp>
#include <opencv2/dnn/all_layers.hpp>
#include <Windows.h>
#include <ModelDetections.h>
#include <string>
#include <time.h>
#include <chrono>
#include "wtypes.h"
#include <KeySimulation.h>
#include <vector>
using namespace cv::dnn; //OpenCV ML
using namespace std::chrono; //Timing
using namespace std;
using namespace cv;
enum MODES
{
NONE = 0,
PATHING = 1,
FIGHT = 2,
MINE = 3,
HARVEST = 4,
SENTRY = 5,
};
enum KEYS
{
ONE = 0x31,
TWO = 0x32,
THREE = 0x33,
FOUR = 0x34,
};
vector<string>MODENAMES = { "NONE", "PATHING", "FIGHT", "MINE", "HARVEST", "SENTRY" };
const float THRESHOLD = 0.85; //For match templage
int CURRENTMODE = NONE, CURRENTKEY = ONE;
const int FRAMEHEIGHT = 540, FRAMEWIDTH = 960, DIMENSIONBEINGS = 10, DIMENSIONMINING = 12, DIMENSIONTREE = 7;
Mat returnImage(bool& val);
void returnMatchTemplate(Mat img, Mat templ, int& food, vector<Rect>& foodOutline);
void detectBeingsAttack(Mat& frame, Net &net, std::vector<cv::Scalar> colors, std::vector<std::string> class_list, steady_clock::time_point &attack_timer);
void detectBeingsAvoidance(Mat& frame, Net& net, std::vector<cv::Scalar> colors, std::vector<std::string> class_list);
void detectBeingsSentry(Mat& frame, Net& net, std::vector<cv::Scalar> colors, std::vector<std::string> class_list);
void detectTree(Mat& frame, Net& netBeings, std::vector<cv::Scalar> colors, std::vector<std::string> class_list, steady_clock::time_point& tree_timer);
void detectLight(Mat& frame);
void detectOre(Mat& frame, Net& netBeings, std::vector<cv::Scalar> colors, std::vector<std::string> class_list, steady_clock::time_point& ore_timer);
DWORD WINAPI eatThread(__in LPVOID lpParameter) //performs eating action while doing other tasks
{
KeyActionDown(THREE);//Press three key for food
Sleep(1);
KeyActionUp(THREE);
MouseRightClickAndHold();
KeyActionDown(CURRENTKEY);
Sleep(1);
KeyActionUp(CURRENTKEY);//Put key back to whichever place it was in
return 0;
}
DWORD WINAPI changeCurrentKey(__in LPVOID lpParameter) //changes key and sleeps while performing other tasks
{
KeyActionDown(CURRENTKEY);
Sleep(10);
KeyActionUp(CURRENTKEY);//Put key back to whichever place it was in
return 0;
}
int main()
{
bool gameWindowFocus = false, is_cuda = true;
int counter = 0, fpsCounter = 0, timePassed = 0, currentFood = 0, foodTimeCounter = 0, lastFPS = 0;
auto start_time = high_resolution_clock::now(); //Main loops fps timer
auto FoodTimer = high_resolution_clock::now(); //Timer to feed character
auto stuckTimer = high_resolution_clock::now();//Determine when to unstuck
auto AttackTimer = high_resolution_clock::now();//Determine when to stock attacking
auto treeTimer = high_resolution_clock::now();//Determine when to unstuck
auto oreTimer = high_resolution_clock::now();//Determine when to stock attacking
unsigned int diffsum, maxdiff;
double percent_diff;
Mat matGray, matDiff, matGrayPrev, frame;
string line;
//For storing food outlines
vector<Rect> foodOutline;
//Load the model(s) & information
cv::dnn::Net netBeings;
cv::dnn::Net netTrees;
cv::dnn::Net netOre;
load_net(netBeings, is_cuda, "Models/DetectEnemies.onnx");
load_net(netTrees, is_cuda, "Models/DetectTrees.onnx");
load_net(netOre, is_cuda, "Models/DetectOre.onnx");
std::vector<std::string> class_list_mobs = load_class_list("Models/classesMobs.txt");
std::vector<std::string> class_list_trees = load_class_list("Models/classesTrees.txt");
std::vector<std::string> class_list_ores = load_class_list("Models/classesOres.txt");
const std::vector<cv::Scalar> colors = { cv::Scalar(255, 255, 0), cv::Scalar(0, 255, 0), cv::Scalar(0, 255, 255), cv::Scalar(255, 0, 0) };
//INITAL SIZEING CODE
frame = returnImage(gameWindowFocus); //initial frame
cvtColor(frame, frame, COLOR_BGRA2BGR); //Go from 8UC4 to 8UC3
//Resize the frame for easier processing
resize(frame, frame, Size(960, 540), INTER_LINEAR);
cvtColor(frame, matGray, COLOR_BGR2GRAY);
matDiff = matGray.clone();
matGrayPrev = matGray.clone();
maxdiff = (matDiff.cols) * (matDiff.rows) * 255;
Mat templ = imread("MatchTemplate/food.png"); //For telling how much food exists
bool KeyFlag = true;
DWORD dwThreadId;
DWORD dwThreadTWO;
static HANDLE hThread = NULL;
static HANDLE keyThread = NULL;
while (1)
{
//Press X key to change working state, works once a second
if (GetKeyState('X') & 0x8000 && KeyFlag == true)
{
KeyFlag = false;
if (CURRENTMODE == 5)
CURRENTMODE = 0;
else
CURRENTMODE++;
cout << "CURRENT MODDE: " << MODENAMES[CURRENTMODE] << endl;
//Verify none of existing keys are still pressed
KeyActionUp(0x57);
//KeyActionUp(VK_SPACE);
MouseLeftClickUp();
switch (CURRENTMODE)
{
case 2:
CURRENTKEY = TWO;
break;
case 3:
CURRENTKEY = ONE;
break;
case 4:
CURRENTKEY = FOUR;
break;
default: //Defaults to pickaxe or 1 key
CURRENTKEY = ONE;
break;
}
keyThread = CreateThread(NULL, 0, changeCurrentKey, NULL, 0, &dwThreadTWO); //Changes to the current key
}
frame = returnImage(gameWindowFocus);
// Wait indefinitely for a key press
int key = cv::waitKey(1);
if (key == 27) // break if escape key is pressed
break;
//gameWindowFocus = true;
//DOWN SCALE AND USE FRAME
cvtColor(frame, frame, COLOR_BGRA2BGR); //Go from 8UC4 to 8UC3
resize(frame, frame, Size(960, 540), INTER_LINEAR);
//For getting unstuck
cvtColor(frame, matGray, COLOR_BGR2GRAY);
absdiff(matGrayPrev, matGray, matDiff);
diffsum = (unsigned int)sum(matDiff)[0];
percent_diff = ((double)diffsum / (double)maxdiff) * 100;
if (gameWindowFocus) { //Main window foucs
if (CURRENTMODE == NONE)
{
putText(frame, "NO MODE SELECTED", Point(50, 50), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(255, 255, 255));
}
if (CURRENTMODE == PATHING)
{
putText(frame, "PATHING MODE SELECTED", Point(50, 50), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(255, 255, 255));
KeyActionDown(0x57);
detectBeingsAvoidance(frame, netBeings, colors, class_list_mobs);
}
else if (CURRENTMODE == FIGHT)
{
putText(frame, "FIGHT MODE SELECTED", Point(50, 50), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(255, 255, 255));
detectBeingsAttack(frame, netBeings, colors, class_list_mobs, AttackTimer);
}
else if (CURRENTMODE == MINE)
{
putText(frame, "MINE MODE SELECTED", Point(50, 50), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(255, 255, 255));
detectOre(frame, netOre, colors, class_list_ores, oreTimer);
}
else if (CURRENTMODE == HARVEST)
{
putText(frame, "HARVEST MODE SELECTED", Point(50, 50), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(255, 255, 255));
detectTree(frame, netTrees, colors, class_list_trees, treeTimer);
}
else if(CURRENTMODE == SENTRY)
{
putText(frame, "SENTRY MODE SELECTED", Point(50, 50), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(255, 255, 255));
detectBeingsSentry(frame, netBeings, colors, class_list_mobs);
}
//Only feed if window is in focus
auto currentTimeFood = high_resolution_clock::now();
auto foodElapseTime = duration_cast<seconds>(currentTimeFood - FoodTimer).count();
//Feed every 3 seconds or more
if (foodElapseTime >= 3)
{
returnMatchTemplate(frame.clone(), templ, currentFood, foodOutline); //Find hunger and get shape
FoodTimer = currentTimeFood;
if (currentFood > 0) //Dont want this happening if the food level is gone or character is dead
{
if (currentFood <= 8 && hThread == NULL)
{
hThread = CreateThread(NULL, 0, eatThread, NULL, 0, &dwThreadId);
}
if (currentFood >= 9 && hThread != NULL) //If thread is occupied and food is good cancel thread
{
CloseHandle(hThread);
hThread = NULL;
}
}
else
{
foodOutline.clear();
}
}
if (foodOutline.size() > 0)
{
for (int k = 0; k < foodOutline.size(); k++)
{
rectangle(frame, Rect(foodOutline[k].x, foodOutline[k].y, foodOutline[k].height, foodOutline[k].width), Scalar(0, 255, 0), 2);
}
}
//For light detection dot NONFUNCTIONAL
detectLight(frame);
//Gets character unstuck
auto currentStuckTime = high_resolution_clock::now();
auto stuckElapsedTime = duration_cast<seconds>(currentStuckTime - stuckTimer).count();
if (percent_diff < 1)
{
//If no moevment for 5 or more seconds move the mouse to try get unstuck
if (stuckElapsedTime >= 5)
{
MouseMove(90, 0);
}
}
else //Keep resetting the timer for getting suck
stuckTimer = currentStuckTime;
}
else //If window is out of focus make sure no keys are still going.
{
//KeyActionUp(0x57);
// KeyActionUp(VK_SPACE);
//MouseLeftClickUp();
CloseHandle(hThread);
}
//imshow("diff", matDiff);
matGrayPrev = matGray.clone();
counter++;
auto current_time = high_resolution_clock::now();
auto elapsed_time = duration_cast<seconds>(current_time - start_time).count();
if (elapsed_time >= 1) {
cout << "Loop completed " << counter << " times in " << elapsed_time << " seconds." << endl;
KeyFlag = true; //Resets keypress timer so it dosent get spammed
lastFPS = counter; //For keeping frame fps active
counter = 0; //For fps counter
start_time = current_time;
}
string fpsString = to_string(lastFPS);
cv::putText(frame, fpsString, Point(FRAMEWIDTH - 60, 50), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 0, 255));
imshow("Game Overlay", frame);
}
return 0;
}
//returnImage:
Mat returnImage(bool &val)
{
HWND hwnd = FindWindow(NULL, L"Minecraft* 1.19.2 - Singleplayer");
HWND temp = GetForegroundWindow(); //Gets current window for comparing to needed window
if (hwnd == temp)//Window Focus
val = true; //Sets true if window is in focus so controls dont interact with the system.
else
val = false;
HDC hdc = GetDC(hwnd); // Get the device context of the window
RECT rect;
GetClientRect(hwnd, &rect); // Get the dimensions of the client area of the window
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
HDC hdcMem = CreateCompatibleDC(hdc); // Create a compatible device context
HBITMAP hBitmap = CreateCompatibleBitmap(hdc, width, height); // Create a compatible bitmap
// Select the bitmap into the device context
SelectObject(hdcMem, hBitmap);
// Copy the contents of the window to the bitmap
BitBlt(hdcMem, 0, 0, width, height, hdc, 0, 0, SRCCOPY);
// Create a new frame object to store the captured image, unsinged int 8bit four channels
Mat frame(height, width, CV_8UC4);
// Copy the bitmap data to the frame object
GetBitmapBits(hBitmap, width * height * 4, frame.data);
//cleanup
DeleteObject(hBitmap);
DeleteDC(hdcMem);
ReleaseDC(hwnd, hdc);
return frame;
}
//Import frame and a template image to match to
void returnMatchTemplate(Mat img, Mat templ, int &food, vector<Rect> &foodOutline)
{
food = 0;
Mat3b img2 = img.clone();
Mat3b templ2 = templ.clone();
Mat1b img_gray;
Mat1b templ_gray;
cvtColor(img2, img_gray, COLOR_BGR2GRAY);
cvtColor(templ2, templ_gray, COLOR_BGR2GRAY);
Mat1f result;
cvtColor(img, img, COLOR_BGRA2BGR);
int result_cols = img.cols - templ.cols + 1;
int result_rows = img.rows - templ.rows + 1;
//result.create(result_rows, result_cols, CV_32FC1);
matchTemplate(img2, templ2, result, TM_CCOEFF_NORMED);
//normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat());
threshold(result, result, 0.7, 1., THRESH_BINARY);
Mat1b resb;
result.convertTo(resb, CV_8U, 255);
vector<vector<Point>> contours;
findContours(resb, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); i++)
{
Mat1b mask(result.rows, result.cols, uchar(0));
drawContours(mask, contours, i, Scalar(255), FILLED);
double minVal; double maxVal; Point minLoc; Point maxLoc;
Point matchLoc;
minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, mask);
//rectangle(img2, Rect(maxLoc.x, maxLoc.y, templ2.cols, templ2.rows), Scalar(0,255,0), 2);
food++;
foodOutline.push_back(Rect(maxLoc.x, maxLoc.y, templ2.cols, templ2.rows)); //get the boxes around the food showing all the time
}
}
void detectBeingsAttack(Mat& frame, Net& netBeings, std::vector<cv::Scalar> colors, std::vector<std::string> class_list, steady_clock::time_point& attack_timer)
{
std::vector<Detection> output;
detect(frame, netBeings, output, class_list, DIMENSIONBEINGS);
int detections = output.size();
auto currentAttackTime = high_resolution_clock::now();
auto AttackElapsedTime = duration_cast<seconds>(currentAttackTime - attack_timer).count();
for (int i = 0; i < detections; ++i)
{
auto box = output[i].box;
auto classId = output[i].class_id;
const auto color = colors[classId % colors.size()];
cv::rectangle(frame, box, color, 3);
cv::rectangle(frame, cv::Point(box.x, box.y - 20), cv::Point(box.x + box.width, box.y), color, cv::FILLED);
circle(frame, Point2i(box.x + box.width / 2, box.y + box.height / 2), 5, Scalar(0, 125, 230), 4, 3); //Center enemy
circle(frame, Point2i(FRAMEWIDTH / 2, FRAMEHEIGHT / 2), 5, Scalar(0, 125, 230), 4, 3); //Center screen
cv::putText(frame, class_list[classId].c_str(), cv::Point(box.x, box.y - 5), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
float upperBound = FRAMEHEIGHT - (FRAMEHEIGHT * .70);
line(frame, Point(10, upperBound), Point(500, upperBound), Scalar(255, 0, 0), 2, LINE_4);
line(frame, Point(10, box.height), Point(500, box.height), Scalar(0, 0, 255), 2, LINE_4);
if (classId != 1) {
//When in creatures in face stop and attack
if (upperBound < box.height)
{
MouseLeftClick(); //attack
KeyActionUp(0x57); //Stop running
//KeyActionUp(VK_SPACE); //stop jumping
attack_timer = currentAttackTime;
}
if (box.x + 2 > FRAMEWIDTH / 2)
{
MouseMove(4, 0);
}
else if (box.x + box.width - 2< FRAMEWIDTH / 2)
{
MouseMove(-4, 0);
}
if (box.y > FRAMEHEIGHT / 2)
{
MouseMove(0, 6);
}
else if (box.y + box.height < FRAMEHEIGHT / 2)
{
MouseMove(0, -6);
}
}
}
if (detections == 0 && AttackElapsedTime >= 2) // if nothing is seen and no attacks have happened start moving
{
MouseLeftClickUp(); //cancel input
KeyActionDown(0x57); //walk towards
attack_timer = currentAttackTime;
}
}
void detectBeingsAvoidance(Mat& frame, Net& netBeings, std::vector<cv::Scalar> colors, std::vector<std::string> class_list)
{
std::vector<Detection> output;
detect(frame, netBeings, output, class_list, DIMENSIONBEINGS);
int detections = output.size();
for (int i = 0; i < detections; ++i)
{
auto box = output[i].box;
auto classId = output[i].class_id;
const auto color = colors[classId % colors.size()];
rectangle(frame, box, color, 3);
rectangle(frame, Point(box.x, box.y - 20), Point(box.x + box.width, box.y), color, FILLED);
circle(frame, Point2i(box.x + box.width / 2, box.y + box.height / 2), 5, Scalar(0, 125, 230), 4, 3); //Center enemy
circle(frame, Point2i(FRAMEWIDTH / 2, FRAMEHEIGHT / 2), 5, Scalar(0, 125, 230), 4, 3); //Center screen
putText(frame, class_list[classId].c_str(), Point(box.x, box.y - 5), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));
int centerX = box.x + box.width / 2;
// move left or right depening if the enemy is on the left or right side
if (centerX > FRAMEWIDTH / 2)
{
MouseMove(-5, 0);
}
if (centerX < FRAMEWIDTH / 2)
{
MouseMove(5, 0);
}
}
}
void detectBeingsSentry(Mat& frame, Net& netBeings, std::vector<cv::Scalar> colors, std::vector<std::string> class_list)
{
std::vector<Detection> output;
detect(frame, netBeings, output, class_list, DIMENSIONBEINGS);
int detections = output.size();
if (detections >= 1)
{
CURRENTMODE = PATHING;
}
MouseMove(3, 0);
}
//Detects brightest light in scene
void detectLight(Mat& frame)
{
Mat binary;
Mat tempFrame = frame.clone();
cvtColor(tempFrame, tempFrame, COLOR_RGB2GRAY);
//Find brightest and darkest areas
cv::threshold(tempFrame, binary, 0, 255, cv::THRESH_BINARY | cv::THRESH_OTSU);
// Find the contours in the binary image
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(binary, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
//Iterate through the contours to find the contour with the largest area
double max_area = 0.0;
int max_area_contour_idx = -1;
for (int i = 0; i < contours.size(); i++) {
double area = cv::contourArea(contours[i]);
if (area > max_area) {
max_area = area;
max_area_contour_idx = i;
}
}
//Use the moments of the largest contour to calculate the centroid of the brightest spot
if (max_area_contour_idx != -1) {
cv::Moments moments = cv::moments(contours[max_area_contour_idx], false);
double centroid_x = moments.m10 / moments.m00;
double centroid_y = moments.m01 / moments.m00;
cv::Point brightest_spot(centroid_x, centroid_y);
// Draw a circle around the brightest spot on the original frame
cv::circle(frame, brightest_spot, 10, cv::Scalar(0, 255, 0), -1);
}
}
void detectTree(Mat& frame, Net& net, std::vector<cv::Scalar> colors, std::vector<std::string> class_list, steady_clock::time_point& tree_timer)
{
std::vector<Detection> output;
detect(frame, net, output, class_list, DIMENSIONTREE);
int detections = output.size();
float upperBound = FRAMEHEIGHT - (FRAMEHEIGHT * .50);
auto currentHarvestTime = high_resolution_clock::now();
auto HarvestElapsedTime = duration_cast<seconds>(currentHarvestTime - tree_timer).count();
if(detections != 0){ //Only need one detection at a time doesnt really matter where
auto box = output[0].box;
auto classId = output[0].class_id;
const auto color = colors[classId % colors.size()];
cv::rectangle(frame, box, color, 3);
cv::rectangle(frame, cv::Point(box.x, box.y - 20), cv::Point(box.x + box.width, box.y), color, cv::FILLED);
circle(frame, Point2i(box.x + box.width / 2, box.y + box.height / 2), 5, Scalar(0, 125, 230), 4, 3); //Center enemy
circle(frame, Point2i(FRAMEWIDTH / 2, FRAMEHEIGHT / 2), 5, Scalar(0, 125, 230), 4, 3); //Center screen
cv::putText(frame, class_list[classId].c_str(), cv::Point(box.x, box.y - 5), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
line(frame, Point(10, upperBound), Point(500, upperBound), Scalar(255, 0, 0), 2, LINE_4);
line(frame, Point(10, box.height), Point(500, box.height), Scalar(0, 0, 255), 2, LINE_4);
if (upperBound < box.height)
{
MouseLeftClick(); //attack
tree_timer = currentHarvestTime;
KeyActionUp(0x57);
}
if (box.x > FRAMEWIDTH/2)
MouseMove(1, 0);
else if (box.x + box.width < FRAMEWIDTH / 2)
MouseMove(-1, 0);
if (box.y > FRAMEHEIGHT / 2)
MouseMove(0, 1);
else if (box.y + box.height < FRAMEHEIGHT / 2)
MouseMove(0, -1);
}
if (HarvestElapsedTime >= 4)
{
MouseLeftClickUp(); //cancel input
KeyActionDown(0x57); //walk towards
}
}
void detectOre(Mat& frame, Net& netOre, std::vector<cv::Scalar> colors, std::vector<std::string> class_list, steady_clock::time_point& ore_timer)
{
std::vector<Detection> output;
detect(frame, netOre, output, class_list, DIMENSIONMINING);
int detections = output.size();
float upperBound = FRAMEHEIGHT - (FRAMEHEIGHT * .65);
auto currentMineTime = high_resolution_clock::now();
auto MineElapsedTime = duration_cast<seconds>(currentMineTime - ore_timer).count();
if (detections != 0) {
auto box = output[0].box;
auto classId = output[0].class_id;
const auto color = colors[classId % colors.size()];
cv::rectangle(frame, box, color, 3);
cv::rectangle(frame, cv::Point(box.x, box.y - 20), cv::Point(box.x + box.width, box.y), color, cv::FILLED);
circle(frame, Point2i(box.x + box.width / 2, box.y + box.height / 2), 5, Scalar(0, 125, 230), 4, 3); //Center enemy
circle(frame, Point2i(FRAMEWIDTH / 2, FRAMEHEIGHT / 2), 5, Scalar(0, 125, 230), 4, 3); //Center screen
cv::putText(frame, class_list[classId].c_str(), cv::Point(box.x, box.y - 5), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
line(frame, Point(10, upperBound), Point(500, upperBound), Scalar(255, 0, 0), 2, LINE_4);
line(frame, Point(10, box.height), Point(500, box.height), Scalar(0, 0, 255), 2, LINE_4);
if (upperBound < box.height)
{
MouseLeftClick(); //attack+
ore_timer = currentMineTime;
KeyActionUp(0x57);
}
if (box.x > FRAMEWIDTH / 2)
{
MouseMove(1, 0);
}
else if (box.x + box.width < FRAMEWIDTH / 2)
{
MouseMove(-1, 0);
}
if (box.y > FRAMEHEIGHT / 2)
{
MouseMove(0, 1);
}
else if (box.y + box.height < FRAMEHEIGHT / 2)
{
MouseMove(0, -1);
}
}
if (MineElapsedTime >= 4)
{
MouseLeftClickUp(); //cancel input
KeyActionDown(0x57); //walk towards
}
}