-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
549 lines (440 loc) · 12.6 KB
/
main.go
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
// go语言运行shell命令
package main
import (
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"log"
"math"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strconv"
"time"
)
func out(m string, a ...interface{}) {
fmt.Fprintf(os.Stderr, m, a...)
}
func checkErr(doing string, err error, args ...interface{}) {
if err != nil {
if len(args) > 0 {
doing = fmt.Sprintf(doing, args...)
}
out("error %s: %s\n", doing, err)
os.Exit(1)
}
}
const (
imgPathInPhone = "/sdcard/screenshot.png"
imgDirName = "imgs"
)
const (
SleepLess = 1500 // sleep at least, wait for skip done
SleepRandom = 1000 // random sleep, prevent cheat
PressRandomFactor = 2 // random press, prevent cheat
MillSecond = 1000 * 1000
)
const (
JumpRatio = 1.363
)
func main() {
path, _ := filepath.Abs(os.Args[0])
imgDirPath := filepath.Join(filepath.Dir(path), imgDirName)
if _, err := os.Stat(imgDirPath); !os.IsNotExist(err) {
// checkErr("Init path", errors.New(""), fmt.Sprintf("Old img Path exist:%v", imgDirPath))
}
os.MkdirAll(imgDirPath, 0755)
configImgPathChan := make(chan string)
configResChan := make(chan config)
go getConfig(configImgPathChan, configResChan)
posImgPathChan := make(chan string)
posResChan := make(chan pos)
go findPos(posImgPathChan, posResChan)
nextCenterImgPathChan := make(chan string)
nextCenterResChan := make(chan pos)
go findNextCenter(nextCenterImgPathChan, nextCenterResChan)
var lastpos pos
var lastCenter pos
var jumpRatio float64
jumpRatio = JumpRatio
i := 0
for {
err := exec.Command("adb", "shell", "/system/bin/screencap", "-p", imgPathInPhone).Run()
checkErr("ScreenshotStart", err)
imgPath := filepath.Join(imgDirPath, strconv.Itoa(i)+".png")
err = exec.Command("adb", "pull", imgPathInPhone, imgPath).Run()
checkErr("PullPhoto", err, imgPath)
posImgPathChan <- imgPath
nextCenterImgPathChan <- imgPath
configImgPathChan <- imgPath
config := <-configResChan
log.Printf("Photo Size:%v", config)
pos := <-posResChan
log.Printf("Find Chess Position:X: %v Y: %v", pos.X, pos.Y)
nextCenter := <-nextCenterResChan
log.Printf("Find Next Center:X: %v Y: %v", nextCenter.X, nextCenter.Y)
jumpRatio = adjust(jumpRatio, lastpos, lastCenter, pos)
fmt.Println(jumpRatio)
pixelDistance := math.Sqrt(math.Abs(math.Pow(float64(nextCenter.X-pos.X), 2)) + math.Abs(math.Pow(float64(nextCenter.Y-pos.Y), 2)))
pressTime := jumpRatio * pixelDistance
// random swipe, prevent cheat
pressX := rand.Intn(config.weight / PressRandomFactor)
pressY := rand.Intn(config.height / PressRandomFactor)
err = exec.Command("adb", "shell", fmt.Sprintf("input swipe %d %d %d %d %d", pressX, pressY, pressX, pressY, int(pressTime))).Run()
checkErr("Skip", err, imgPath)
time.Sleep(time.Duration(MillSecond * (SleepLess + rand.Intn(SleepRandom))))
log.Printf("swipe %d %d: %d", pressX, pressY, int(pressTime))
lastpos = pos
lastCenter = nextCenter
i++
if i > 5000 {
break
}
}
}
func adjust(jumpRatio float64, lastpos, lastCenter, nextpos pos) float64 {
if lastpos.X == 0 {
return jumpRatio
}
return jumpRatio
expect := math.Sqrt(math.Abs(math.Pow(float64(lastCenter.X-lastpos.X), 2)) + math.Abs(math.Pow(float64(lastCenter.Y-lastpos.Y), 2)))
really := math.Sqrt(math.Abs(math.Pow(float64(nextpos.X-lastpos.X), 2)) + math.Abs(math.Pow(float64(nextpos.Y-lastpos.Y), 2)))
ratio := really / (expect * jumpRatio)
expectJumpRatio := 1 / ratio
fmt.Println(expect, really, expectJumpRatio, jumpRatio)
// slowly
jumpRatio += (expectJumpRatio - jumpRatio) / 3
return jumpRatio
}
type rgb struct {
r uint32
g uint32
b uint32
}
const (
sampleTopOffset = 200 // sample point offset
sampleBottomOffSet = 500
)
type topbottom struct {
top rgb
bottom rgb
}
type colorType map[rgb]topbottom
var colorModel = colorType{}
func getsample(p image.Image) (rgb, rgb) {
colorToOccurence := make(map[rgb]int)
var topSample rgb
var bottomSample rgb
// gradient background
// Upper corner target color
topSample.r, topSample.g, topSample.b = At(p, 0, sampleTopOffset)
if v, isfound := colorModel[topSample]; isfound {
return v.top, v.bottom
}
log.Println("Wrong:unknown color")
var color rgb
for i := 0; i < p.Bounds().Max.X; i++ {
color.r, color.g, color.b = At(p, i, p.Bounds().Max.Y-sampleBottomOffSet)
colorToOccurence[color]++
}
log.Printf("NewColor: %v", topSample)
maxOccurence := 0
for k, v := range colorToOccurence {
if v > maxOccurence {
maxOccurence = v
bottomSample = k
}
}
colorModel[topSample] = topbottom{
top: topSample,
bottom: bottomSample,
}
return topSample, bottomSample
}
const (
chessLength = 210
chessWidth = 100
tooCloseOffset = 50 // we use to judge if they are too close or just out of windows
BlockOutOfRangeX = 40 // we think either left or right point is out of window
BlockOutOfRangeY = 20 // we think either left or right point is out of window
outChessBoardYOffset = 20
)
const (
OffsetOfYToFindTop = 500 // avoid touching scores
OffsetOfXToFindTop = 10
OffsetOfXToFindCenter = 400 // find center in range[topX - OffsetOfXToFindCenter, topX + OffsetOfXToFindCenter]
OffsetOfYToFindCenter = 300 // find center in range[topY, topY + OffsetOfYToFindCenter]
)
func findNextCenter(imgPathChan chan string, resChan chan pos) {
defer func() {
resChan <- pos{}
}()
for imgPath := range imgPathChan {
f, err := os.OpenFile(imgPath, os.O_RDONLY, 0644)
if err != nil {
return
}
p, err := png.Decode(f)
if err != nil {
return
}
// get sample
topSample, bottomSample := getsample(p)
utr, utg, utb := topSample.r, topSample.g, topSample.b
ltr, ltg, ltb := bottomSample.r, bottomSample.g, bottomSample.b
minr := uint32(math.Min(float64(utr), float64(ltr)))
maxr := uint32(math.Max(float64(utr), float64(ltr)))
ming := uint32(math.Min(float64(utg), float64(ltg)))
maxg := uint32(math.Max(float64(utg), float64(ltg)))
minb := uint32(math.Min(float64(utb), float64(ltb)))
maxb := uint32(math.Max(float64(utb), float64(ltb)))
topX, topY := 0, 0
// wait for find position done
chessCoordinate := <-syncChan
loop:
for y := p.Bounds().Min.Y + OffsetOfYToFindTop; y <= p.Bounds().Max.Y; y++ {
for x := p.Bounds().Min.X + OffsetOfXToFindTop; x < p.Bounds().Max.X-OffsetOfXToFindTop; x++ {
r, g, b := At(p, x, y)
if !matchShading(r, g, b, minr, ming, minb, maxr, maxg, maxb) &&
!(math.Abs(float64(chessCoordinate.X-x)) < (chessWidth/2) &&
math.Abs(float64(chessCoordinate.Y-y)) < chessLength) {
topX = x
topY = y
break loop
}
}
}
leftX, rightX, leftY, rightY := p.Bounds().Max.X, 0, 0, 0
ntr, ntg, ntb := At(p, topX, topY)
// search right
loop2:
for y := topY; y <= topY+OffsetOfYToFindCenter; y++ {
// in the background
if r, g, b := At(p, topX, y); matchShading(r, g, b, minr, ming, minb, maxr, maxg, maxb) {
if r, g, b := At(p, topX, y+outChessBoardYOffset); matchShading(r, g, b, minr, ming, minb, maxr, maxg, maxb) {
break loop2
}
}
for x := topX; x <= topX+OffsetOfXToFindCenter; x++ {
r, g, b := At(p, x, y)
if matchShading(r, g, b, minr, ming, minb, maxr, maxg, maxb) {
break
}
if match(r, g, b, ntr, ntg, ntb, deviation) {
if x > rightX {
rightX = x
rightY = y
}
}
}
}
// search left
loop3:
for y := topY; y <= topY+OffsetOfYToFindCenter; y++ {
if r, g, b := At(p, topX, y); matchShading(r, g, b, minr, ming, minb, maxr, maxg, maxb) {
if r, g, b := At(p, topX, y+outChessBoardYOffset); matchShading(r, g, b, minr, ming, minb, maxr, maxg, maxb) {
break loop3
}
}
for x := topX; x >= topX-OffsetOfXToFindCenter; x-- {
r, g, b := At(p, x, y)
if matchShading(r, g, b, minr, ming, minb, maxr, maxg, maxb) {
break
}
if match(r, g, b, ntr, ntg, ntb, deviation) {
if x < leftX {
leftX = x
leftY = y
}
}
}
}
centerX := (leftX + rightX) / 2
centerY := (leftY + rightY) / 2
// case: block out of range or block too close
if math.Abs(float64(topX-leftX)-float64(rightX-topX)) > BlockOutOfRangeX ||
math.Abs(float64(leftY-rightY)) > BlockOutOfRangeY {
if leftX-p.Bounds().Min.X < tooCloseOffset || p.Bounds().Max.X-rightX < tooCloseOffset {
// out of range
if topX-leftX > rightX-topX {
// right point out of range
centerX = topX
centerY = leftY
} else {
// left point out of range
centerX = topX
centerY = rightY
}
} else {
// too close
if topX-leftX > rightX-topX {
// chess in left
centerX = topX
centerY = rightY
} else {
// chess in right
centerX = topX
centerY = leftY
}
}
}
if x, y := SpecialBlockWithRes(p, topX, topY); !(x == 0 && y == 0) {
centerX = x
centerY = y
}
// open again
f, err = os.OpenFile(imgPath, os.O_RDONLY, 0644)
if err != nil {
return
}
p, err = png.Decode(f)
if err != nil {
return
}
drawRectangle(imgPath, p, topX, topY, leftX, leftY, rightX, rightY, centerX, centerY)
f.Close()
resChan <- pos{
X: centerX,
Y: centerY,
}
}
}
const (
SpecialBlockSampleOne = 135
SpecialBlockSampleTwo = 180
SpecialBlockYOffset = 30
SpecialBlockXOffset = 5
)
// return 0, 0 if not found special block
func SpecialBlockWithRes(p image.Image, x, y int) (int, int) {
if r, g, b := At(p, x, y+SpecialBlockSampleOne); 94 <= r && r <= 107 && 126 <= g && g <= 143 && 154 <= b && b <= 175 {
if r, g, b := At(p, x, y+SpecialBlockSampleTwo); 94 <= r && r <= 107 && 126 <= g && g <= 143 && 154 <= b && b <= 175 {
log.Print("Wrong: Special Block")
return x + SpecialBlockXOffset, y + SpecialBlockYOffset
}
}
return 0, 0
}
type config struct {
weight int
height int
}
func getConfig(imgPathChan chan string, resChan chan config) {
defer func() {
resChan <- config{}
}()
for imgPath := range imgPathChan {
f, err := os.OpenFile(imgPath, os.O_RDONLY, 0644)
if err != nil {
return
}
c, err := png.DecodeConfig(f)
if err != nil {
return
}
f.Close()
resChan <- config{
weight: c.Width,
height: c.Height,
}
}
}
const (
RChess = 40
GChess = 43
BChess = 86
RectangleSize = 10
)
type pos struct {
X int
Y int
}
var syncChan = make(chan pos, 0)
func findPos(imgPathChan chan string, resChan chan pos) {
defer func() {
resChan <- pos{}
}()
for imgPath := range imgPathChan {
f, err := os.OpenFile(imgPath, os.O_RDONLY, 0644)
if err != nil {
return
}
p, err := png.Decode(f)
if err != nil {
return
}
minX, maxX, maxY := p.Bounds().Max.X, 0, 0
beginFindX, endFindX, beginFindY, endFindY := p.Bounds().Min.X, p.Bounds().Max.X, p.Bounds().Min.Y, p.Bounds().Max.Y
hadTouchChess := false
for y := beginFindY; y <= endFindY; y++ {
for x := beginFindX; x <= endFindX; x++ {
r, g, b := At(p, x, y)
if match(r, g, b, RChess, GChess, BChess, deviation) {
if !hadTouchChess {
beginFindX = x - (chessWidth / 2)
endFindX = x + (chessWidth / 2)
beginFindY = y
endFindY = y + chessLength
hadTouchChess = true
}
minX = int(math.Min(float64(x), float64(minX)))
maxX = int(math.Max(float64(x), float64(maxX)))
maxY = int(math.Max(float64(y), float64(maxY)))
}
}
}
chessCoordinate := pos{
X: (minX + maxX) / 2,
Y: maxY,
}
drawRectangle(imgPath, p, chessCoordinate.X, chessCoordinate.Y)
// notify find center can begin to find center
syncChan <- chessCoordinate
f.Close()
resChan <- chessCoordinate
}
}
func At(p image.Image, x int, y int) (uint32, uint32, uint32) {
r, g, b, _ := p.At(x, y).RGBA()
r = r >> 8
g = g >> 8
b = b >> 8
return r, g, b
}
const (
deviation = 16
)
func match(r, g, b, tr, tg, tb, t uint32) bool {
if r > tr-t && r < tr+t && g > tg-t && g < tg+t && b > tb-t && b < tb+t {
return true
}
return false
}
func matchShading(r, g, b, minr, ming, minb, maxr, maxg, maxb uint32) bool {
if r >= minr && r <= maxr && g >= ming && g <= maxg && b >= minb && b <= maxb {
return true
}
return false
}
func drawRectangle(imgPath string, p image.Image, pos ...int) {
myimage := image.NewRGBA(p.Bounds())
myred := color.RGBA{200, 0, 0, 255}
// copy first
draw.Draw(myimage, myimage.Bounds(), p, p.Bounds().Min, draw.Over)
for i := 0; i < len(pos); i += 2 {
x, y := pos[i], pos[i+1]
red_rect := image.Rect(x-RectangleSize, y-RectangleSize, x+RectangleSize, y+RectangleSize)
// draw rectangler
draw.Draw(myimage, red_rect, image.NewUniform(myred), p.Bounds().Min, draw.Over)
}
myfile, err := os.OpenFile(imgPath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
panic(err)
}
png.Encode(myfile, myimage)
myfile.Sync()
myfile.Close()
}