Files
fishbot/main.go
2025-11-13 14:43:59 +01:00

133 lines
3.0 KiB
Go

package main
import (
"embed"
"image"
_ "image/png"
"time"
"github.com/go-vgo/robotgo"
"github.com/vova616/screenshot"
"gocv.io/x/gocv"
)
const start_x = 2560
const fish_key = "8"
//go:embed fishing_target.png
var s embed.FS
func check(e error) {
if e != nil {
panic(e)
}
}
func imageToRGBA(img image.Image) []uint8 {
sz := img.Bounds()
raw := make([]uint8, (sz.Max.X-sz.Min.X)*(sz.Max.Y-sz.Min.Y)*4)
idx := 0
for y := sz.Min.Y; y < sz.Max.Y; y++ {
for x := sz.Min.X; x < sz.Max.X; x++ {
r, g, b, a := img.At(x, y).RGBA()
raw[idx], raw[idx+1], raw[idx+2], raw[idx+3] = uint8(r), uint8(g), uint8(b), uint8(a)
idx += 4
}
}
return raw
}
func main() {
mask := gocv.NewMat()
mask.SetTo(gocv.NewScalar(255, 255, 255, 255))
fishing_target_src, err := s.Open("fishing_target.png")
check(err)
defer fishing_target_src.Close()
fishing_target, _, err := image.Decode(fishing_target_src)
check(err)
fishing_target_mat, err := gocv.NewMatFromBytes(fishing_target.Bounds().Max.Y, fishing_target.Bounds().Max.X, gocv.MatTypeCV8UC4, imageToRGBA(fishing_target))
check(err)
ticker := time.Now()
start := time.Now()
fishing := false
mouse_set := false
block_everything := false
// window := gocv.NewWindow("Hello")
for {
result, loc, bobber := findBobber(fishing_target_mat)
// window.IMShow(result)
// window.WaitKey(1)
elapsed := time.Since(start) / time.Second
if time.Since(ticker)/time.Second >= 1 {
ticker = time.Now()
println(elapsed)
}
if !block_everything && (!fishing && elapsed >= 5) {
println("Start fishing")
// robotgo.KeyDown("CTRL")
robotgo.KeyPress(fish_key)
// robotgo.KeyUp("CTRL")
time.Sleep(time.Duration(100 * time.Millisecond))
mouse_set = false
fishing = true
start = time.Now()
}
if !block_everything && (!mouse_set && fishing && bobber && elapsed >= 1) {
println("Moving to bobber")
robotgo.Move(loc.X+start_x, loc.Y)
time.Sleep(time.Duration(100 * time.Millisecond))
mouse_set = true
start = time.Now()
}
if mouse_set && fishing && !bobber && elapsed >= 1 {
block_everything = true
println("Stop fishing")
robotgo.MouseDown("right")
time.Sleep(time.Duration(100 * time.Millisecond))
robotgo.MouseUp("right")
time.Sleep(time.Duration(100 * time.Millisecond))
block_everything = false
fishing = false
start = time.Now()
// os.Exit(0)
}
result.Close()
}
}
func findBobber(target gocv.Mat) (gocv.Mat, image.Point, bool) {
img, err := screenshot.CaptureRect(image.Rect(start_x, 0, 2560+start_x, 1440)) // *image.RGBA
if err != nil {
panic(err)
}
mat, _ := gocv.NewMatFromBytes(img.Bounds().Max.Y, img.Bounds().Max.X, gocv.MatTypeCV8UC4, img.Pix)
defer mat.Close()
result := gocv.NewMat()
defer result.Close()
err = gocv.MatchTemplate(mat, target, &result, gocv.TmCcoeffNormed, gocv.NewMat())
check(err)
result2 := gocv.NewMat()
// defer result2.Close()
gocv.Threshold(result, &result2, 0.8, 1, gocv.ThresholdBinary)
_, _, _, max_loc := gocv.MinMaxLoc(result2)
return result2, max_loc, (max_loc.X > 0 && max_loc.Y > 0)
}