-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpHelper.cpp
More file actions
300 lines (245 loc) · 6.42 KB
/
JumpHelper.cpp
File metadata and controls
300 lines (245 loc) · 6.42 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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <random>
#include <regex>
#include <opencv2/opencv.hpp>
#include "Config.h"
#include "common.h"
#define VERSION "1.0"
using namespace std;
using namespace cv;
static double press_coefficient;
static int piece_base_height_1_2;
static int piece_body_width;
Mat screenshot() {
#ifdef _WIN32
system("adb shell screencap -p /sdcard/screenshot.png");
system("adb pull /sdcard/screenshot.png .");
#else
FILE *p, *f;
char buf[1024];
p = popen("adb shell screencap -p", "r");
f = fopen("screenshot.png", "w");
if (p != nullptr && f != nullptr) {
while (!feof(p)) {
size_t size;
size = fread(buf, 1, 1024, p);
fwrite(buf, 1, size, f);
}
pclose(p);
fclose(f);
}
#endif
Mat ret = imread("screenshot.png");
return ret;
}
void split(char *str, const char *sep, string array[]) {
int i = 0;
const char *p;
p = strtok(str, sep);
while (p) {
array[i++] = string(p);
p = strtok(nullptr, sep);
}
}
void loadConfig() {
FILE *p;
char buf[64] = { 0 };
p = popen("adb shell wm size", "r");
fread(buf, 1, 128, p);
cout << buf << endl;
pclose(p);
buf[strlen(buf) - 1] = 0;
string args[2];
split(buf + 15, "x", args);
int height = stoi(args[1]);
int width = stoi(args[0]);
Config cfg(height, width);
piece_base_height_1_2 = cfg["piece_base_height_1_2"].asInt();
piece_body_width = cfg["piece_body_width"].asInt();
press_coefficient = cfg["press_coefficient"].asDouble();
}
int randInt(const int &a, const int &b) {
static random_device rd;
return (rd() % (b - a) + a);
}
void buttonPosition(const Mat &img, int *const x, int *const y) {
int width = img.cols;
int height = img.rows;
int left, top;
left = width / 2;
top = 1584 * (height / 1920);
*x = randInt(left - 50, left + 50);
*y = randInt(top - 10, top + 10);
}
void jump(const double &distance, const int &x, const int &y) {
double press_time = distance * press_coefficient;
press_time = MAX(press_time, 200);
char cmd[128];
sprintf(cmd, "adb shell input swipe %d %d %d %d %d", x, y, x, y, static_cast<int>(press_time));
cout << cmd << endl;
system(cmd);
}
void findPieceAndBoard(const Mat &img,
int *const px, int *const py, int *const bx, int *const by) {
int width = img.cols;
int height = img.rows;
int piece_x_sum = 0;
int piece_x_c = 0;
int piece_y_max = 0;
int scan_x_border = width / 8;
int scan_start_y = 0;
*px = *py = *bx = *by = 0;
for (int i = height / 3; i < height * 2 / 3; i += 50) {
const auto &last_pixel = img.at<Vec3b>(i, 0);
for (int j = 1; j < width; j++) {
const auto &pixel = img.at<Vec3b>(i, j);
if (pixel[0] != last_pixel[0]
|| pixel[1] != last_pixel[1]
|| pixel[2] != last_pixel[2]) {
scan_start_y = i - 50;
break;
}
}
if (scan_start_y) break;
}
for (int i = scan_start_y; i < height * 2 / 3; i++) {
for (int j = scan_x_border; j < width - scan_x_border; j++) {
const auto &pixel = img.at<Vec3b>(i, j);
if (50 < pixel[2] && pixel[2] < 60
&& 53 < pixel[1] && pixel[1] < 63
&& 95 < pixel[0] && pixel[0] < 110) {
piece_x_sum += j;
piece_x_c += 1;
piece_y_max = MAX(i, piece_y_max);
}
}
}
if (!(piece_x_c && piece_x_sum)) {
*px = *py = *bx = *by = 0;
return;
}
*px = piece_x_sum / piece_x_c;
*py = piece_y_max - piece_base_height_1_2;
int board_x_start, board_x_end;
if (*px < width / 2) {
board_x_start = *px;
board_x_end = width;
}
else {
board_x_start = 0;
board_x_end = *px;
}
int index = 0;
for (int i = height / 3; i < height * 2 / 3; i++) {
index = i;
const auto &last_pixel = img.at<Vec3b>(i, 0);
if (*bx || *by) {
break;
}
int board_x_sum = 0;
int board_x_c = 0;
for (int j = board_x_start; j < board_x_end; j++) {
const auto &pixel = img.at<Vec3b>(i, j);
if (abs(j - *px) < piece_body_width) {
continue;
}
if ((abs(pixel[0] - last_pixel[0])
+ abs(pixel[1] - last_pixel[1])
+ abs(pixel[2] - last_pixel[2])) > 10) {
board_x_sum += j;
board_x_c += 1;
}
}
if (board_x_sum) {
*bx = board_x_sum / board_x_c;
}
}
const auto &last_pixel = img.at<Vec3b>(index, *bx);
int k = 0;
for (k = index + 274; k > index; k--) {
const auto &pixel = img.at<Vec3b>(k, *bx);
if (abs(pixel[0] - last_pixel[0])
+ abs(pixel[1] - last_pixel[1])
+ abs(pixel[2] - last_pixel[2]) < 10) {
break;
}
}
*by = (index + k) / 2;
for (int l = index; l < index; l++) {
const auto &pixel = img.at<Vec3b>(l, *bx);
if (abs(pixel[0] - 245)
+ abs(pixel[1] - 245)
+ abs(pixel[2] - 245) == 0) {
*by = l + 10;
}
}
if (!(bx, by)) {
*px = *py = *bx = *by = 0;
return;
}
}
bool ask(const string &prompt,
const string &_true_value = "y",
const string &_false_value = "n",
bool _default = true) {
string s;
string _default_value = _default ? _true_value : _false_value;
printf("%s %s/%s [%s]: ",
prompt.c_str(),
_true_value.c_str(),
_false_value.c_str(),
_default_value.c_str());
cin >> noskipws >> s;
getchar();
if (s.empty()) {
return _default;
}
for (;;) {
if (s == _true_value) {
return true;
}
else if (s == _false_value) {
return false;
}
else {
printf("%s %s/%s [%s]: ",
prompt.c_str(),
_true_value.c_str(),
_false_value.c_str(),
_default_value.c_str());
cin >> noskipws >> s;
getchar();
if (s.empty()) {
return _default;
}
}
}
}
void showInfo() {
cout << "Version: " << VERSION << endl;
cout << "开源地址: https://github.com/neoql/WechatJumpHelper" << endl;
cout << "原项目地址: https://github.com/wangshub/wechat_jump_game" << endl;
cout << "算法作者: wangshub" << endl;
cout << "本程序作者: @author月梦书" << endl;
}
int main(int argc, char *argv[]) {
showInfo();
bool flag = ask("请确保手机打开了 ADB 并连接了电脑,然后打开跳一跳并【开始游戏】后再用本程序,确定开始?");
if (!flag) {
cout << "bye." << endl;
return 0;
}
cout << "Ctrl-c退出程序." << endl;
loadConfig();
for (;;) {
Mat img = screenshot();
int px, py, bx, by;
int x, y;
findPieceAndBoard(img, &px, &py, &bx, &by);
buttonPosition(img, &x, &y);
jump(sqrt((bx - px) * (bx - px) + (by - py) * (by - py)), x, y);
sleep(1);
}
}