238 lines
5.2 KiB
C++
238 lines
5.2 KiB
C++
#include "lolautoaccept.h"
|
|
|
|
#include <thread>
|
|
#include <Log.h>
|
|
|
|
#include "util.h"
|
|
#include "fakescreen.h"
|
|
|
|
void debugImage(cv::Mat img, const std::string& name) {
|
|
if(img.channels() > 3) {
|
|
std::vector<cv::Mat> channels(4);
|
|
cv::split(img, channels);
|
|
channels.resize(3); // drop alpha channel
|
|
cv::merge(channels, img);
|
|
}
|
|
|
|
time_t t = time(0);
|
|
cv::imwrite("debugimages/" + name + std::to_string(t) + ".png", img);
|
|
}
|
|
|
|
void LolAutoAccept::checkForGame() {
|
|
cv::Mat img;
|
|
screen->take(img);
|
|
|
|
cv::resize(img, img, cv::Size(ScreenShot::DEFAULTWIDTH, ScreenShot::DEFAULTHEIGHT));
|
|
|
|
// debugImage(img, "resized");
|
|
|
|
Matcher::Match mat = acceptmatcher.match(img);
|
|
|
|
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);
|
|
|
|
if(lastacceptmatch.doesMatch) {
|
|
Log::info << "second match";
|
|
|
|
if(lastacceptmatch != mat) {
|
|
Log::warn << "not same match!";
|
|
lastacceptmatch.doesMatch = false;
|
|
return;
|
|
}
|
|
|
|
performClick(1); // accept Game
|
|
|
|
// security sleep
|
|
std::this_thread::sleep_for(std::chrono::seconds(3));
|
|
} else {
|
|
Log::info << "first match";
|
|
}
|
|
} else {
|
|
// check if next stage is here
|
|
Matcher::Match mat = arrowmatcher.match(img);
|
|
if(mat.doesMatch) {
|
|
Log::info << "Pre pick state";
|
|
|
|
if(!prepick.empty())
|
|
pickFirst(prepick);
|
|
|
|
state = State::PREPICK;
|
|
}
|
|
}
|
|
lastacceptmatch = mat;
|
|
}
|
|
|
|
void LolAutoAccept::checkForBan() {
|
|
cv::Mat img;
|
|
screen->take(img);
|
|
|
|
cv::resize(img, img, cv::Size(ScreenShot::DEFAULTWIDTH, ScreenShot::DEFAULTHEIGHT));
|
|
|
|
Matcher::Match mat = banmatcher.match(img);
|
|
if(mat.doesMatch) {
|
|
Log::info << "Ban state";
|
|
|
|
if(!ban.empty())
|
|
pickFirst(ban);
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(800));
|
|
|
|
// click "ban"
|
|
performClick(4);
|
|
|
|
state = State::BAN;
|
|
}
|
|
}
|
|
|
|
void LolAutoAccept::performClick(uint32_t nr) {
|
|
inputs.setOffset(screen->getXOffset(), screen->getYOffset());
|
|
inputs.setScale(screen->getXScale(), screen->getYScale());
|
|
|
|
auto p = inputs.get(nr);
|
|
Log::info << "click " << nr << " @ " << p.x << " " << p.y;
|
|
|
|
sim.mouseMoveTo(p.x, p.y);
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(170));
|
|
sim.mouseClick(XIS::LEFT_MOUSE_BUTTON);
|
|
|
|
// move mouse away
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(170));
|
|
p = inputs.get(0);
|
|
sim.mouseMoveTo(p.x, p.y);
|
|
}
|
|
|
|
void LolAutoAccept::enterSearch(const std::string& text) {
|
|
performClick(2); // click searchbox
|
|
|
|
Log::debug << "enter text: " << text;
|
|
|
|
sim.keySequence(text);
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(750));
|
|
}
|
|
|
|
void LolAutoAccept::pickFirst(const std::string& search) {
|
|
enterSearch(search);
|
|
|
|
performClick(3); // first champion
|
|
}
|
|
|
|
LolAutoAccept::LolAutoAccept() : acceptmatcher("imgs/Accept.png"), arrowmatcher("imgs/arrowdown.png"), banmatcher("imgs/ban.png"), sim(XInputSimulator::getInstance()) {
|
|
// click positions in 1280x720 scale
|
|
inputs.addPoint({0, 0}); // zero zero
|
|
inputs.addPoint({645, 560}); // accept game
|
|
inputs.addPoint({775, 105}); // search box
|
|
inputs.addPoint({380, 160}); // first champ
|
|
inputs.addPoint({638, 608}); // pick champ
|
|
|
|
acceptmatcher.setOffset(539, 529);
|
|
arrowmatcher.setOffset(615, 617);
|
|
banmatcher.setOffset(1232, 90);
|
|
}
|
|
|
|
LolAutoAccept::~LolAutoAccept() {
|
|
stopJoinThread();
|
|
|
|
delete screen;
|
|
screen = nullptr;
|
|
}
|
|
|
|
void LolAutoAccept::setPrePick(const std::string& prePick) {
|
|
prepick = prePick;
|
|
}
|
|
|
|
void LolAutoAccept::setBan(const std::string& ban) {
|
|
this->ban = ban;
|
|
}
|
|
|
|
void LolAutoAccept::setPick(const std::string& pick) {
|
|
this->pick = pick;
|
|
}
|
|
|
|
void LolAutoAccept::setAutoAcceptEnabled(bool b) {
|
|
autoacceptenabled = b;
|
|
}
|
|
|
|
void LolAutoAccept::setPrePickEnabled(bool b) {
|
|
prepickenabled = b;
|
|
}
|
|
|
|
void LolAutoAccept::setBanEnabled(bool b) {
|
|
banenabled = b;
|
|
}
|
|
|
|
void LolAutoAccept::setPickEnabled(bool b) {
|
|
pickenabled = b;
|
|
}
|
|
|
|
|
|
bool LolAutoAccept::init() {
|
|
if(screen)
|
|
return true;
|
|
|
|
// get window
|
|
auto wins = ScreenShot::getWindows("League of Legends");
|
|
if(wins.size() != 1) {
|
|
Log::fatal << "invalid count of windows found: " << wins.size();
|
|
for(auto w : wins) {
|
|
delete w;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
screen = (wins.at(0)); // just take the first
|
|
|
|
// testing whole screen, but take a part of it, so it behaves like a focused screenshot
|
|
// screen = new FakeScreen(1, 683, 1280, 720);
|
|
|
|
// testing: whole screen:
|
|
// screen = new ScreenShot();
|
|
return true;
|
|
}
|
|
|
|
void LolAutoAccept::run() {
|
|
// make sure its not running
|
|
stopJoinThread();
|
|
|
|
if(!screen) return;
|
|
|
|
lolaathread = std::thread(&LolAutoAccept::innerRun, this);
|
|
}
|
|
|
|
void LolAutoAccept::stop() {
|
|
shouldrun = false;
|
|
}
|
|
|
|
void LolAutoAccept::stopJoinThread() {
|
|
stop();
|
|
|
|
if(lolaathread.joinable()) {
|
|
lolaathread.join();
|
|
}
|
|
}
|
|
|
|
void LolAutoAccept::innerRun() {
|
|
lastacceptmatch = {false};
|
|
shouldrun = true;
|
|
|
|
while(shouldrun) {
|
|
auto start = std::chrono::high_resolution_clock::now();
|
|
switch(state) {
|
|
case State::LOBBY:
|
|
checkForGame();
|
|
break;
|
|
case State::PREPICK:
|
|
checkForBan();
|
|
default: break;
|
|
}
|
|
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
std::chrono::duration<double> dur = (end-start);
|
|
if(dur.count() > 1e-5)
|
|
Log::info << "iteration took: " << (dur.count() * 1000 ) << " ms";
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(800));
|
|
}
|
|
}
|