-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbot.go
318 lines (253 loc) · 7.04 KB
/
bot.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
package main
import "C"
import (
"flag"
"github.com/chistiykot/gobot/window"
"github.com/go-vgo/robotgo"
"log"
"math/rand"
"os"
"time"
)
const (
applicationWindowName = "New World"
applicationProcessName = "NewWorld.exe"
applicationWindowWidth = 1920
applicationWindowHeight = 1080
)
type Direction int
const (
forward Direction = iota
left
right
any
)
const (
runMinTime = 1500
runMaxTime = 4500
runMaxTurnGap = 30
turnMaxGap = 300
)
const (
alwaysRunTickerMinTime = 4000
alwaysRunTickerMaxTime = 8000
alwaysRunActionTickerMinTime = 800
alwaysRunActionTickerMaxTime = 1500
alwaysRunDirectionChangeTickerMinTime = 1500
alwaysRunDirectionChangeTickerMaxTime = 6000
)
const (
//actionBitmapSource = "action_1920x1080.png" // "E" letter image (used for action recognition)
actionBitmapSource = "action_side_1920x1080.png" // part of action square (used for action recognition)
actionBitmapTolerance = 0.4
actionScanScreenCaptureLag = 400
actionMinTime = 4000
actionMaxTime = 9000
)
type GameWindow struct {
pid int32
hwnd window.HWND
left, top, width, height int
}
type BotConfiguration struct {
GameWindow
actionBitmapFile string
actionBitmapTolerance float64
}
type Bot struct {
config BotConfiguration
}
func (bot Bot) recognizeActionCoordinates() (int, int) {
log.Printf("action scan start")
actionBitmap := robotgo.OpenBitmap(bot.config.actionBitmapFile)
screenBitmap := robotgo.CaptureScreen(bot.config.left, bot.config.top, bot.config.width, bot.config.height)
defer robotgo.FreeBitmap(actionBitmap)
defer robotgo.FreeBitmap(screenBitmap)
bot.sleep(actionScanScreenCaptureLag)
fx, fy := robotgo.FindBitmap(actionBitmap, screenBitmap, bot.config.actionBitmapTolerance)
if fx == -1 {
log.Printf("action scan fail: not found")
} else {
log.Printf("action scan succes: x, y: %d, %d", fx, fy)
}
return fx, fy
}
func getConfiguration() BotConfiguration {
applicationInfo := window.GetApplicationInfo(applicationWindowName, applicationProcessName)
left := int(applicationInfo.Left)
top := int(applicationInfo.Top)
return BotConfiguration{
GameWindow{applicationInfo.Pid, applicationInfo.Hwnd, left, top, applicationWindowWidth, applicationWindowHeight},
actionBitmapSource,
actionBitmapTolerance,
}
}
func (bot Bot) getRandomDirection() Direction {
return Direction(rand.Intn(int(any)))
}
func (bot Bot) getRandomTurnDirection() Direction {
return Direction(1 + rand.Intn(int(any-1)))
}
func (bot Bot) randomTurn(direction Direction) {
if direction == any {
direction = bot.getRandomTurnDirection()
}
turnGap := rand.Intn(turnMaxGap)
bot.turnAround(direction, turnGap)
}
func (bot Bot) randomRun(direction Direction, time int) {
if direction == any {
direction = bot.getRandomDirection()
}
turnGap := rand.Intn(runMaxTurnGap)
bot.run(direction, turnGap, time)
}
func (bot Bot) sleep(ms int) {
log.Printf("sleep for %d ms", ms)
robotgo.MilliSleep(ms)
}
func (bot Bot) getRandomRange(min, max int) int {
return min + rand.Intn(max-min)
}
func (bot Bot) randomSleep(min, max int) {
bot.sleep(bot.getRandomRange(min, max))
}
func (bot Bot) run(direction Direction, turnGap int, time int) {
log.Printf("run start, direction: `%d`, run time: `%d` ms", direction, time)
done := make(chan bool)
go func() {
for {
select {
case <-done:
robotgo.KeyToggle("w", "up")
return
default:
if direction == left {
bot.turnLeft(turnGap)
} else if direction == right {
bot.turnRight(turnGap)
}
robotgo.KeyToggle("w", "down")
robotgo.MilliSleep(10)
}
}
}()
robotgo.MilliSleep(time)
done <- true
log.Printf("run finish")
}
func (bot Bot) alwaysRun() {
log.Printf("always run")
robotgo.KeyTap("=")
}
func (bot Bot) turnAround(direction Direction, gap int) {
log.Printf("turn start, direction: `%d`, gap: `%d`", direction, gap)
if direction == left {
bot.turnLeft(gap)
} else if direction == right {
bot.turnRight(gap)
}
log.Printf("turn finish")
}
func (bot Bot) turnLeft(gap int) {
cx, cy := robotgo.GetMousePos()
robotgo.MoveMouseSmooth(cx-gap, cy)
}
func (bot Bot) turnRight(gap int) {
cx, cy := robotgo.GetMousePos()
robotgo.MoveMouseSmooth(cx+gap, cy)
}
func (bot Bot) invokeAction() {
log.Printf("action invoke")
robotgo.KeyTap("e")
}
func runTestCommandIfFlagSet(bot Bot, flag string) {
if flag == "action" {
if bot.recognizeAndInvokeAction() {
bot.randomSleep(actionMinTime, actionMaxTime)
}
}
if flag == "turn" {
bot.randomTurn(any)
}
if flag != "" {
os.Exit(0)
}
}
func (bot Bot) recognizeAndInvokeAction() bool {
actionX, _ := bot.recognizeActionCoordinates()
if actionX == -1 {
return false
}
bot.invokeAction()
return true
}
func (bot Bot) createRandomTicker(min, max int) *time.Ticker {
runTime := time.Duration(int32(bot.getRandomRange(min, max)))
return time.NewTicker(runTime * time.Millisecond)
}
func (bot Bot) alwaysRunAndInvokeAction() {
runTicker := bot.createRandomTicker(alwaysRunTickerMinTime, alwaysRunTickerMaxTime)
actionTicker := bot.createRandomTicker(alwaysRunActionTickerMinTime, alwaysRunActionTickerMaxTime)
randomDirectionTicker := bot.createRandomTicker(alwaysRunDirectionChangeTickerMinTime, alwaysRunDirectionChangeTickerMaxTime)
quit := make(chan struct{})
go func() {
for {
select {
case <-runTicker.C:
bot.alwaysRun()
runTicker.Stop()
runTicker = bot.createRandomTicker(alwaysRunTickerMinTime, alwaysRunTickerMaxTime)
case <-actionTicker.C:
bot.invokeAction()
actionTicker.Stop()
actionTicker = bot.createRandomTicker(alwaysRunActionTickerMinTime, alwaysRunActionTickerMaxTime)
case <-randomDirectionTicker.C:
bot.centerView()
bot.randomTurn(any)
randomDirectionTicker.Stop()
randomDirectionTicker = bot.createRandomTicker(alwaysRunDirectionChangeTickerMinTime, alwaysRunDirectionChangeTickerMaxTime)
case <-quit:
runTicker.Stop()
return
}
}
}()
}
func (bot Bot) randomRunAndInvokeAction() {
runTime := rand.Intn(runMaxTime-runMinTime) + runMinTime
bot.centerView()
bot.randomRun(any, runTime)
bot.sleep(800)
for bot.recognizeAndInvokeAction() {
bot.randomSleep(actionMinTime, actionMaxTime)
}
}
func (bot Bot) randomTurnAndInvokeAction() {
bot.centerView()
bot.randomTurn(any)
bot.sleep(800)
for bot.recognizeAndInvokeAction() {
bot.randomSleep(actionMinTime, actionMaxTime)
}
}
func (bot Bot) centerView() {
robotgo.MoveMouse(500, 600) // todo
}
func main() {
//todo find window geometry more accurate
//todo implement harvest area fn
rand.Seed(time.Now().UnixNano())
testActionFlag := flag.String("t", "", "test action")
flag.Parse()
bot := Bot{getConfiguration()}
robotgo.ActivePID(bot.config.pid)
runTestCommandIfFlagSet(bot, *testActionFlag)
// 1st method of resource gathering (preferred)
bot.alwaysRunAndInvokeAction()
for {
// 2nd and 3d method of resource gathering (based on image recognition)
//bot.randomTurnAndInvokeAction()
//bot.randomRunAndInvokeAction()
}
}