Skip to content
This repository has been archived by the owner on Jan 16, 2018. It is now read-only.

Commit

Permalink
autosave similar file
Browse files Browse the repository at this point in the history
  • Loading branch information
faceair committed Jan 1, 2018
1 parent 1846a18 commit 2178dc1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ iOS 设备

## 跳跃系数

目前一般设备设为 2.04,现在的程序处理逻辑是会先将图片 resize 成 720p 的图片然后再找点和跳跃。但如果设备的分辨率比较特殊,用默认的系数不能跳到中心点,可以微调一下系数争取每次都跳到中心点。但本程序的系数跟其他版本比如 python 的系数不一样,算法不一样,不要混淆。
现在新增了基于统计的动态跳跃系数调节,跳得越多调教越多自动判断的系数越准,记录也会自动保存,欢迎多多尝试。

程序开始输入的系数仅作为初始默认值使用,目前一般设备初始值可以设为 2.04。程序处理逻辑是会先将图片 resize 成 720p 的图片然后再找点和跳跃,但如果设备的分辨率比较特殊,用默认的系数不能每次跳到中心点,可以微调一下系数争取每次都跳到中心点。但本程序的系数跟其他版本比如 python 的系数不一样,算法不一样,不要混淆。

## FAQ

Expand Down
3 changes: 1 addition & 2 deletions android/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func screenshot(filename string) image.Image {

func main() {
defer func() {
similar.Save()
jump.Debugger()
if e := recover(); e != nil {
log.Printf("%s: %s", e, debug.Stack())
Expand Down Expand Up @@ -90,7 +89,7 @@ func main() {

go func() {
time.Sleep(time.Millisecond * 170)
src := screenshot("jump-test.png")
src := screenshot("jump.test.png")

finally, _ := jump.Find(src)
if finally != nil {
Expand Down
7 changes: 5 additions & 2 deletions ios/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func screenshot(ip string) (*ScreenshotRes, image.Image) {

func main() {
defer func() {
similar.Save()
jump.Debugger()
if e := recover(); e != nil {
log.Printf("%s: %s", e, debug.Stack())
Expand Down Expand Up @@ -110,10 +109,14 @@ func main() {
}

go func() {
time.Sleep(time.Millisecond * 170)
time.Sleep(time.Duration(nowDistance*nowRatio/1000+50) * time.Millisecond)
_, src := screenshot(ip)
finally, _ := jump.Find(src)

f, _ = os.OpenFile("jump.test.png", os.O_WRONLY|os.O_CREATE, 0600)
png.Encode(f, src)
f.Close()

if finally != nil {
finallyDistance := jump.Distance(start, finally)
finallyRatio := (nowDistance * nowRatio) / finallyDistance
Expand Down
45 changes: 28 additions & 17 deletions similar.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
package jump

import (
"encoding/gob"
"bufio"
"fmt"
"math"
"os"
"strconv"
"strings"
)

var similarFile *os.File

func init() {
similarFile, _ = os.OpenFile(basePath+"/similar.ai", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
}

func NewSimilar(ratio float64) *Similar {
similar := new(Similar)
f, err := os.Open("similar.ai")
if err == nil {
decoder := gob.NewDecoder(f)
err = decoder.Decode(similar)
similar := &Similar{
distances: []float64{},
ratios: map[float64]float64{},
defaultRatio: ratio,
}
scanner := bufio.NewScanner(similarFile)
for scanner.Scan() {
line := strings.Split(scanner.Text(), ",")
if len(line) == 2 {
distance, err1 := strconv.ParseFloat(line[0], 64)
ratio, err2 := strconv.ParseFloat(line[1], 64)
if err1 == nil && err2 == nil {
similar.Add(distance, ratio)
}
}
}
f.Close()
similar.defaultRatio = ratio

return similar
}

Expand All @@ -25,6 +43,8 @@ type Similar struct {
}

func (s *Similar) Add(distance, ratio float64) {
similarFile.Write([]byte(fmt.Sprintf("%v,%v\n", distance, ratio)))

s.distances = append(s.distances, distance)
s.ratios[distance] = ratio
}
Expand All @@ -47,12 +67,3 @@ func (s *Similar) Find(nowDistance float64) (similarDistance, simlarRatio float6

return sumD / count, sumR / count
}

func (s *Similar) Save() {
f, err := os.Create("similar.ai")
if err == nil {
encoder := gob.NewEncoder(f)
encoder.Encode(s)
}
f.Close()
}

0 comments on commit 2178dc1

Please sign in to comment.