diff --git a/include/matcher.h b/include/matcher.h index 71dde0f..3d2cda0 100644 --- a/include/matcher.h +++ b/include/matcher.h @@ -16,6 +16,9 @@ public: int x = 0, y = 0; int width = 0; int height = 0; + + bool operator==(const Match&) const; + bool operator!=(const Match&) const; }; Match match(const cv::Mat& img); diff --git a/src/main.cpp b/src/main.cpp index cba1dea..0d5eaf6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,18 @@ #include +static void debugImage(cv::Mat img) { + if(img.channels() > 3) { + std::vector channels(4); + cv::split(img, channels); + channels.resize(3); // drop alpha channel + cv::merge(channels, img); + } + + time_t t = time(0); + cv::imwrite("debugimages/" + std::to_string(t) + ".png", img); +} + int main(int argc, const char** argv) { Log::init(); Log::setConsoleLogLevel(Log::Level::TRACE); @@ -25,31 +37,45 @@ int main(int argc, const char** argv) { XInputSimulator& sim = XInputSimulator::getInstance(); + Matcher::Match lastmatch = {false}; + while(true) { cv::Mat img; screen(img); Matcher::Match mat = matcher.match(img); - Log::warn << "matched: " << mat.doesMatch; - if(mat.doesMatch) { + Log::info << "matched"; cv::rectangle(img, cv::Point(mat.x, mat.y), cv::Point( mat.x + mat.width , mat.y + mat.height ), cv::Scalar(0, 0, 255, 0), 2); + debugImage(img); - time_t t = time(0); - cv::imwrite("debugimages/" + std::to_string(t) + ".jpg", img); + if(lastmatch.doesMatch) { + Log::info << "second match"; - int x = mat.x + (mat.width/2); - int y = mat.y + (mat.height/2); - sim.mouseMoveTo(x, y); + if(lastmatch != mat) { + Log::warn << "not same match!"; + lastmatch.doesMatch = false; + continue; + } - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - sim.mouseClick(XIS::LEFT_MOUSE_BUTTON); + int x = mat.x + (mat.width/2); + int y = mat.y + (mat.height/2); + Log::info << "click"; + sim.mouseMoveTo(x, y); - std::this_thread::sleep_for(std::chrono::seconds(10)); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + sim.mouseClick(XIS::LEFT_MOUSE_BUTTON); + + // security sleep + std::this_thread::sleep_for(std::chrono::seconds(10)); + } else { + Log::info << "first match"; + } } + lastmatch = mat; - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } Log::stop(); diff --git a/src/matcher.cpp b/src/matcher.cpp index 9b84dce..bf1d54a 100644 --- a/src/matcher.cpp +++ b/src/matcher.cpp @@ -10,6 +10,15 @@ Matcher::Matcher(const cv::Mat& templ) { } Matcher::~Matcher() {} +bool Matcher::Match::operator==(const Match& other) const { + return doesMatch == other.doesMatch && x == other.x && y == other.y && width == other.width && height == other.height; +} + +bool Matcher::Match::operator!=(const Match& other) const { + return !(*this == other); +} + + Matcher::Match Matcher::match(const cv::Mat& img) { // create out mat int result_cols = img.cols - templ.cols + 1; @@ -31,7 +40,7 @@ Matcher::Match Matcher::match(const cv::Mat& img) { cv::Point minLoc; cv::Point maxLoc; cv::minMaxLoc( out, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat() ); - Log::info << "minValbn: " << minValbn << " maxValbn: " << maxValbn << " minVal: " << minVal << " maxVal: " << maxVal << " minLoc: " << minLoc.x << " " << minLoc.y << " maxLoc: " << maxLoc.x << " " << maxLoc.y; + Log::debug << "minValbn: " << minValbn << " maxValbn: " << maxValbn << " minVal: " << minVal << " maxVal: " << maxVal << " minLoc: " << minLoc.x << " " << minLoc.y << " maxLoc: " << maxLoc.x << " " << maxLoc.y; bool matched = minValbn < 2e7;