gamebot

package module
v0.0.0-...-cdf80e7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2022 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bot

type Bot struct {
	// contains filtered or unexported fields
}

func NewBot

func NewBot(processName string) (*Bot, error)

NewBot create a new bot instance.

Example
package main

import (
	"fmt"

	"os"
	"path/filepath"

	"github.com/KalebHawkins/gamebot"
)

func main() {
	procName := filepath.Base(os.Args[0])
	b, err := gamebot.NewBot(procName)

	if err != nil {
		panic(err)
	}

	// return the process name the bot is attached to.
	fmt.Println(b.ProcessName())
}
Output:

gamebot.test.exe

func (*Bot) CVMatchMode

func (b *Bot) CVMatchMode() string

(b *Bot) SetCVMatchMode returns the opencv template matching mode. Reference: [OpenCV Documentation](https://docs.opencv.org/4.6.0/df/dfb/group__imgproc__object.html) for more information.

func (*Bot) CaptureWindow

func (b *Bot) CaptureWindow() *image.Image

(b *Bot) CaptureWindow can be used to capture an image of the bot's set window.

func (*Bot) Click

func (b *Bot) Click(btn MouseButton, doubleClick bool)

(b *Bot) Click click the specified mouse button at the current location of the cursor. Reference https://github.com/go-vgo/robotgo/blob/master/docs/keys.md for keycodes.

Example
package main

import (
	"os"
	"path/filepath"

	"github.com/KalebHawkins/gamebot"
)

func main() {
	procName := filepath.Base(os.Args[0])
	b, err := gamebot.NewBot(procName)

	if err != nil {
		panic(err)
	}

	// Single click the right mouse button
	b.Click(gamebot.Right, false)
}
Output:

func (*Bot) DetectImage

func (b *Bot) DetectImage(in *image.Image, tmpl *image.Image) (float32, float32, *image.Point, *image.Point, error)

(b *Bot) DetectImage scan the bot's set window to detect images within the window. This function returns the minValue, maxValue, minLocation and maxLocation of the matched image. If an error occurs and error is returned.

The `in` parameter represents the larger image where `tmpl` is the template image to search for.

This function utilized opencv's TmCcoeffNormed algorithm by default. To change the algorithm use the `(b *Bot) SetCVMatchMode()`.

func (*Bot) GetPixelColor

func (b *Bot) GetPixelColor(x, y int) string

(b *Bot) GetPixelColor return the color of the pixel at the x, y coordinates of the screen.

func (*Bot) IsKeyDown

func (b *Bot) IsKeyDown(key string) bool

(b *Bot) IsKeyDown returns true is the specified key is in a down state.

Note that mouse keys are prefixed with the string mouse e.g. mouseleft, mouseright, mousecenter

Example
package main

import (
	"fmt"

	"os"
	"path/filepath"

	"github.com/KalebHawkins/gamebot"
)

func main() {
	procName := filepath.Base(os.Args[0])
	b, err := gamebot.NewBot(procName)

	if err != nil {
		panic(err)
	}

	// Press the 'w' key to a down state
	b.PressKey("w")

	// Check if a key is pushed down.
	if b.IsKeyDown("w") {
		fmt.Println("w key is down")
	}

	b.ReleaseKey("w")
}
Output:

w key is down

func (*Bot) KeyTap

func (b *Bot) KeyTap(key string)

(b *Bot) KeyTap will press and release a key. The `args` parameter represents special characters that may need to be pressed alongside the primary key, e.g shift. Reference https://github.com/go-vgo/robotgo/blob/master/docs/keys.md for full list of keycodes.

func (*Bot) KeysDown

func (b *Bot) KeysDown() []string

(b *Bot) KeysDown returns a slice of keys currently in the down position. If there are no keys in a down state this function returns an empty slice.

Example
package main

import (
	"fmt"

	"os"
	"path/filepath"

	"github.com/KalebHawkins/gamebot"
)

func main() {
	procName := filepath.Base(os.Args[0])
	b, err := gamebot.NewBot(procName)

	if err != nil {
		panic(err)
	}

	b.PressKey("w")
	b.PressKey("a")
	b.PressKey("s")
	b.PressKey("d")

	// Get a slice of keys in down position
	depressedKeys := b.KeysDown()

	for _, v := range depressedKeys {
		fmt.Println(v)
	}

	b.ReleaseKey("w")
}
Output:

w
a
s
d

func (*Bot) MilliSleep

func (b *Bot) MilliSleep(ms int)

(b *Bot) MilliSleep will pause program operation for the specified number of milliseconds.

Example
package main

import (
	"os"
	"path/filepath"

	"github.com/KalebHawkins/gamebot"
)

func main() {
	procName := filepath.Base(os.Args[0])
	b, err := gamebot.NewBot(procName)

	if err != nil {
		panic(err)
	}

	// Bot will pause operation for 100 milliseconds
	b.MilliSleep(100)
}
Output:

func (*Bot) MousePosition

func (b *Bot) MousePosition() (int, int)

(b *Bot) MousePosition returns the mouse's current x, y coordinates.

Example
package main

import (
	"fmt"

	"os"
	"path/filepath"

	"github.com/KalebHawkins/gamebot"
)

func main() {
	procName := filepath.Base(os.Args[0])
	b, err := gamebot.NewBot(procName)

	if err != nil {
		panic(err)
	}

	mpx, mpy := b.MousePosition()
	fmt.Println(mpx, mpy)
	// ouptut: 0 0
}
Output:

func (*Bot) MousePress

func (b *Bot) MousePress(btn MouseButton)

(b *Bot) MousePress puts the specified mouse button in a down state. To release the button use `(b *Bot) MousePress`.

Example
package main

import (
	"fmt"

	"os"
	"path/filepath"

	"github.com/KalebHawkins/gamebot"
)

func main() {
	procName := filepath.Base(os.Args[0])
	b, err := gamebot.NewBot(procName)

	if err != nil {
		panic(err)
	}

	b.MousePress(gamebot.Left)

	if b.IsKeyDown("mouseleft") {
		fmt.Println("left mouse key is down")
	}

	b.MouseRelease(gamebot.Left)
}
Output:

left mouse key is down

func (*Bot) MouseRelease

func (b *Bot) MouseRelease(btn MouseButton)

(b *Bot) MouseRelease puts the specified mouse button in an up state.

Example
package main

import (
	"fmt"

	"os"
	"path/filepath"

	"github.com/KalebHawkins/gamebot"
)

func main() {
	procName := filepath.Base(os.Args[0])
	b, err := gamebot.NewBot(procName)

	if err != nil {
		panic(err)
	}

	b.MousePress(gamebot.Left)

	if b.IsKeyDown("mouseleft") {
		fmt.Println("left mouse key is down")
	}

	b.MouseRelease(gamebot.Left)
}
Output:

left mouse key is down

func (*Bot) MoveCursor

func (b *Bot) MoveCursor(x, y int)

(b *Bot) MoveCursor simulates moving the cursor from it's current position to the x, y location on the screen. This simulates human-like movement. If you want to move x, y number of pixels from the current mouses position see `(b *Bot) MoveCursorRelative`. (x: 0, y: 0) represents the top left-hand corner of the screen.

Example
package main

import (
	"os"
	"path/filepath"

	"github.com/KalebHawkins/gamebot"
)

func main() {
	procName := filepath.Base(os.Args[0])
	b, err := gamebot.NewBot(procName)

	if err != nil {
		panic(err)
	}

	// Drag the cursor position to the top-left hand corner
	b.MoveCursor(0, 0)
}
Output:

func (*Bot) MoveCursorClick

func (b *Bot) MoveCursorClick(x, y int, btn MouseButton, doubleClick bool)

(b *Bot) MoveClick puts the cursor at the specified x, y position then clicks the specified mouse button. This movement is nearly instant and does not simulate human-like movement. Reference https://github.com/go-vgo/robotgo/blob/master/docs/keys.md for keycodes.

Example
package main

import (
	"os"
	"path/filepath"

	"github.com/KalebHawkins/gamebot"
)

func main() {
	procName := filepath.Base(os.Args[0])
	b, err := gamebot.NewBot(procName)

	if err != nil {
		panic(err)
	}

	// Instantly set the location of the cursor to 100, 100
	// and click the left mouse button once.
	b.MoveCursorClick(100, 100, gamebot.Left, false)

	// Instantly set the location of the cursor to 100, 100
	// and double click the left mouse
	b.MoveCursorClick(100, 100, gamebot.Left, true)
}
Output:

func (*Bot) MoveCursorRelative

func (b *Bot) MoveCursorRelative(x, y int)

(b *Bot) MoveCursorRelative simulates moving the cursor from it's current position by x and y number of pixels. This simulates human-like movement. x represents left and right movement while y represents up and down on the screen.

Example
package main

import (
	"os"
	"path/filepath"

	"github.com/KalebHawkins/gamebot"
)

func main() {
	procName := filepath.Base(os.Args[0])
	b, err := gamebot.NewBot(procName)

	if err != nil {
		panic(err)
	}

	// Drag the cursor position from the current position
	// 10 pixels left and 10 pixels down.
	b.MoveCursor(-10, 10)
}
Output:

func (*Bot) MoveCursorSmoothClick

func (b *Bot) MoveCursorSmoothClick(x, y int, btn MouseButton, doubleClick bool)

(b *Bot) MoveCursorSmoothClick puts the cursor at the specified x, y position then clicks the specified mouse button. This movement simulates human-like movement. Reference https://github.com/go-vgo/robotgo/blob/master/docs/keys.md for keycodes.

Example
package main

import (
	"os"
	"path/filepath"

	"github.com/KalebHawkins/gamebot"
)

func main() {
	procName := filepath.Base(os.Args[0])
	b, err := gamebot.NewBot(procName)

	if err != nil {
		panic(err)
	}

	// Drag the location of the cursor to 100, 100
	// and click the left mouse button once.
	b.MoveCursorClick(100, 100, gamebot.Left, false)

	// Drag the location of the cursor to 100, 100
	// and double click the left mouse
	b.MoveCursorClick(100, 100, gamebot.Left, true)
}
Output:

func (*Bot) OpenImage

func (b *Bot) OpenImage(path string) (*image.Image, error)

(b *Bot) OpenImage will provide an image given a path. An error is returned if there is a problem reading the file.

func (*Bot) PressKey

func (b *Bot) PressKey(key string)

(b *Bot) PressKey toggles a key on the keyboard. This will put the key in a down state until ReleaseKey is called. Reference https://github.com/go-vgo/robotgo/blob/master/docs/keys.md for full list of keycodes.

Example
package main

import (
	"os"
	"path/filepath"

	"github.com/KalebHawkins/gamebot"
)

func main() {
	procName := filepath.Base(os.Args[0])
	b, err := gamebot.NewBot(procName)

	if err != nil {
		panic(err)
	}

	// Press the 'w' key to a down state
	b.PressKey("w")
	// Release the 'w' key
	b.ReleaseKey("w")
}
Output:

func (*Bot) ProcessName

func (b *Bot) ProcessName() string

(b *Bot) ProcessName() return the bot's currently configured processName.

func (*Bot) RandomInt

func (b *Bot) RandomInt(min, max int) int

(b *Bot) RandomInt generate random integers within a range of min and max values (inclusive).

Example
package main

import (
	"fmt"
	"math/rand"
	"os"
	"path/filepath"

	"github.com/KalebHawkins/gamebot"
)

func main() {
	procName := filepath.Base(os.Args[0])
	b, err := gamebot.NewBot(procName)

	if err != nil {
		panic(err)
	}

	rand.Seed(0)

	// get a random integer between 5 and 10 (he min and max values are inclusive.
	rn := b.RandomInt(5, 10)
	fmt.Println(rn)
}
Output:

5

func (*Bot) ReleaseKey

func (b *Bot) ReleaseKey(key string)

(b *Bot) ReleaseKey toggles a key on the keyboard. This will put the key in a down state until ReleaseKey is called. Reference https://github.com/go-vgo/robotgo/blob/master/docs/keys.md for full list of keycodes.

Example
package main

import (
	"os"
	"path/filepath"

	"github.com/KalebHawkins/gamebot"
)

func main() {
	procName := filepath.Base(os.Args[0])
	b, err := gamebot.NewBot(procName)

	if err != nil {
		panic(err)
	}

	// Press the 'w' key to a down state
	b.PressKey("w")
	// Release the 'w' key
	b.ReleaseKey("w")
}
Output:

func (*Bot) SetCVMatchMode

func (b *Bot) SetCVMatchMode(matchMode gocv.TemplateMatchMode)

(b *Bot) SetCVMatchMode set the opencv template matching mode. Reference: [OpenCV Documentation](https://docs.opencv.org/4.6.0/df/dfb/group__imgproc__object.html) for more information.

func (*Bot) SetCursor

func (b *Bot) SetCursor(x, y int)

(b *Bot) SetCursor puts the cursor at the specified x, y position. This movement is nearly instant and does not simulate human-like movement.

Example
package main

import (
	"os"
	"path/filepath"

	"github.com/KalebHawkins/gamebot"
)

func main() {
	procName := filepath.Base(os.Args[0])
	b, err := gamebot.NewBot(procName)

	if err != nil {
		panic(err)
	}

	// Instantly set the location of the cursor to 100, 100
	b.SetCursor(100, 100)
}
Output:

func (*Bot) ShowDetectedImage

func (b *Bot) ShowDetectedImage(windowTitle string, tmpl *image.Image) error

(b *Bot) ShowDetectedImage this function opens a window creating a rectangle around the min and max areas of the detected image. Pressing the 'q' key will close this window. This function is mostly used for debugging your bot.

The `tmpl` is the template image to search for within the game window. This function will print the minValue, maxValue, MinLocation, and MaxLocation.

func (*Bot) Sleep

func (b *Bot) Sleep(s int)

(b *Bot) Sleep will pause program operation for the specified number of seconds.

Example
package main

import (
	"os"
	"path/filepath"

	"github.com/KalebHawkins/gamebot"
)

func main() {
	procName := filepath.Base(os.Args[0])
	b, err := gamebot.NewBot(procName)

	if err != nil {
		panic(err)
	}

	// Bot will pause operation for 1 seconds
	b.Sleep(1)
}
Output:

func (*Bot) UpdateWindow

func (b *Bot) UpdateWindow() error

(b *Bot) UpdateWindow sets the bot's window configuration in the event that the window changed size or position.

Example
package main

import (
	"os"
	"path/filepath"

	"github.com/KalebHawkins/gamebot"
)

func main() {
	procName := filepath.Base(os.Args[0])
	b, err := gamebot.NewBot(procName)

	if err != nil {
		panic(err)
	}

	didChange, err := b.Window().Changed()
	if err != nil {
		panic(err)
	}

	if didChange {
		b.UpdateWindow()
	}
}
Output:

func (*Bot) Window

func (b *Bot) Window() *window

(b *Bot) Window() returns the bot's current window information.

type KeyState

type KeyState string
const (
	Down KeyState = "down"
	Up   KeyState = "up"
)

type MouseButton

type MouseButton string
const (
	Left       MouseButton = "left"
	Right      MouseButton = "center"
	Center     MouseButton = "right"
	WheelDown  MouseButton = "wheelDown"
	WheelUp    MouseButton = "wheelUp"
	WheelLeft  MouseButton = "wheelLeft"
	WheelRight MouseButton = "wheelRight"
)

type WindowPidGreaterThenOneError

type WindowPidGreaterThenOneError struct {
	Applciation string
}

WindowPidGreaterThenOneError is returned when nPids > 1.

func NewWindowPidGreaterThenOneError

func NewWindowPidGreaterThenOneError(processName string) *WindowPidGreaterThenOneError

NewWindowPidGreaterThenOneError is returned when nPids > 1.

func (*WindowPidGreaterThenOneError) Error

func (*WindowPidGreaterThenOneError) Is

type WindowPidNotFoundError

type WindowPidNotFoundError struct {
	Applciation string
}

WindowPidNotFoundError is returned when the window PID cannot be found.

func NewWindowPidNotFound

func NewWindowPidNotFound(processName string) *WindowPidNotFoundError

NewWindowPidNotFound is returned if a window PID cannot be found.

func (*WindowPidNotFoundError) Error

func (e *WindowPidNotFoundError) Error() string

func (*WindowPidNotFoundError) Is

func (e *WindowPidNotFoundError) Is(tgt error) bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL