-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaproll.ahk
More file actions
476 lines (434 loc) · 14.3 KB
/
maproll.ahk
File metadata and controls
476 lines (434 loc) · 14.3 KB
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
/**
* @description Small app to reset livesplit timer while rerolling factorio map
* @author janzert
* @version 0.2
*/
#Requires AutoHotkey v2.0
#Include "Socket.ahk"
class MyError extends Error {
}
class LocalLivesplit {
static debounce_delay := 305
__New(settings) {
this.reset_key := this.PrepKeys(settings.livesplit_local_reset)
this.start_key := this.PrepKeys(settings.livesplit_local_start)
this.last_send := A_TickCount
}
PrepKeys(hotkey) {
return RegExReplace(hotkey, "([#!^+<>]*)([\w]{2,})", "$1{$2}")
}
Delay() {
delay := LocalLivesplit.debounce_delay
next_send := this.last_send + delay
now := A_TickCount
if (now < next_send) {
wait := next_send - now
wait := wait < delay ? wait : delay
wait := wait > 0 ? wait : 0
Sleep(wait)
}
}
Reset() {
this.Delay()
Send this.reset_key
this.last_send := A_TickCount
}
Start() {
this.Delay()
Send this.start_key
this.last_send := A_TickCount
}
}
class NetworkLivesplit {
__New(settings) {
this.host := settings.livesplit_network_host
this.port := settings.livesplit_network_port
this.socket := false
}
Connect() {
if not this.socket {
this.socket := Socket.Client(this.host, this.port,
Socket.TYPE.STREAM, Socket.IPPROTO.TCP)
}
}
Send(line) {
try {
this.Connect()
this.socket.SendText(line . "`n")
} catch OSError as err {
this.socket := false
msg := "Error connecting to network livesplit:`n`n" . err.Message
throw MyError(msg)
}
}
Reset() {
this.Send("reset")
}
Start() {
this.Send("startorsplit")
}
}
class SettingStore {
static registry_key := "HKCU\Software\FactorioRoller"
reroll_key := "F12"
livesplit_method := "Keyboard"
livesplit_local_reset := "^!Numpad3"
livesplit_local_start := "Numpad1"
livesplit_network_host := ""
livesplit_network_port := 16834
Load() {
this.reroll_key := RegRead(SettingStore.registry_key, "reroll_key", "F12")
stored_method := RegRead(SettingStore.registry_key,
"livesplit_method", "Keyboard")
for check in ["Keyboard", "TCP"] {
if (stored_method == check) {
this.livesplit_method := stored_method
break
}
}
this.livesplit_local_reset := RegRead(SettingStore.registry_key,
"livesplit_local_reset", "!^{Numpad3}")
this.livesplit_local_start := RegRead(SettingStore.registry_key,
"livesplit_local_start", "{Numpad1}")
this.livesplit_network_host := RegRead(SettingStore.registry_key,
"livesplit_network_host", "")
this.livesplit_network_port := RegRead(SettingStore.registry_key,
"livesplit_network_port", 16834)
}
Save() {
RegWrite(this.reroll_key, "REG_SZ", SettingStore.registry_key, "reroll_key")
RegWrite(this.livesplit_method, "REG_SZ", SettingStore.registry_key,
"livesplit_method")
RegWrite(this.livesplit_local_reset, "REG_SZ", SettingStore.registry_key,
"livesplit_local_reset")
RegWrite(this.livesplit_local_start, "REG_SZ", SettingStore.registry_key,
"livesplit_local_start")
RegWrite(this.livesplit_network_host, "REG_SZ", SettingStore.registry_key,
"livesplit_network_host")
RegWrite(this.livesplit_network_port, "REG_DWORD", SettingStore.registry_key,
"livesplit_network_port")
}
}
class SettingGUI {
__New(settings, reroll_control) {
this.settings := settings
this.reroll_control := reroll_control
this.window := Gui("", "Factorio Map Roller", this)
close_func := ObjBindMethod(this, "Close")
this.window.OnEvent("Close", close_func)
this.save_func := ObjBindMethod(this, "TimedSave")
this.window.AddText("YM+6", "Reroll hotkey:")
hk_ctrl := this.window.AddHotkey("vreroll_key X+10 YM+0", settings.reroll_key)
hk_ctrl.OnEvent("Change", "OnChange")
this.window.AddText("Section XM+0 Y+10", "Livesplit control method:")
method_ix := settings.livesplit_method == "TCP" ? 2 : 1
loc_ctrl := this.window.AddDDL(
"vlivesplit_method X+10 YS-4 Choose" . method_ix,
["Keyboard", "TCP"]
)
loc_ctrl.OnEvent("Change", "OnChange")
lsr_text := this.window.AddText("Section XM+0 Y+10", "Livesplit reset key:")
lsr_ctrl := this.window.AddHotkey("vlivesplit_local_reset X+10 YS-4",
settings.livesplit_local_reset)
lsr_ctrl.OnEvent("Change", "OnChange")
lss_text := this.window.AddText("Section XM+0 Y+10", "Livesplit start key:")
lss_ctrl := this.window.AddHotkey("vlivesplit_local_start X+10 YS-4",
settings.livesplit_local_start)
lss_ctrl.OnEvent("Change", "OnChange")
loc_ctrl.GetPos(, &loc_y, , &loc_height)
lsip_y := loc_y + loc_height + 10
lsip_text := this.window.AddText("Section XM+0 Y" lsip_y, "Livesplit hostname/IP:")
lsip_ctrl := this.window.AddEdit("vlivesplit_network_host X+10 YS-4",
settings.livesplit_network_host)
lsip_ctrl.OnEvent("Change", "OnChange")
lsp_text := this.window.AddText("Section XM+0 Y+10", "Livesplit port:")
lsp_ctrl := this.window.AddEdit("vlivesplit_network_port Number X+10 YS-4",
settings.livesplit_network_port)
lsp_ctrl.OnEvent("Change", "OnChange")
this.local_options := [lsr_text, lsr_ctrl, lss_text, lss_ctrl]
this.network_options := [lsip_text, lsip_ctrl, lsp_text, lsp_ctrl]
this.test_button := this.window.AddButton("vlivesplit_test Center", "Test livesplit")
this.test_button.OnEvent("Click", "OnClick")
this.window.AddText("XM+0 Center", "Test will start livesplit timer and then reset it after 5 seconds.")
this.SetCtrlAvailability()
}
SetCtrlAvailability() {
local_enabled := True
if this.settings.livesplit_method == "TCP" {
local_enabled := False
}
for ctrl in this.local_options {
ctrl.Visible := local_enabled
ctrl.Enabled := local_enabled
}
for ctrl in this.network_options {
ctrl.Visible := !local_enabled
ctrl.Enabled := !local_enabled
}
}
Show() {
this.window.Show()
this.test_button.Focus()
}
Close(guiobj) {
this.window.Hide()
this.window.Destroy()
this.reroll_control.DisableHotkey()
this.settings.Save()
ExitApp
}
TimedSave() {
this.settings.Save()
SetTimer( , 0)
}
OnChange(ctrl, info) {
valid_change := False
Switch(ctrl.Name) {
Case "reroll_key":
if ctrl.Value == "" {
ctrl.Value := this.settings.reroll_key
} else if (RegExMatch(ctrl.Value, "[^+!^]") != 0
and this.settings.reroll_key != ctrl.Value) {
this.settings.reroll_key := ctrl.Value
this.reroll_control.UpdateHotkey()
valid_change := True
}
Case "livesplit_method":
if (this.settings.livesplit_method != ctrl.Text) {
this.settings.livesplit_method := ctrl.Text
this.SetCtrlAvailability()
this.reroll_control.UpdateLivesplit()
valid_change := True
}
Case "livesplit_local_reset":
if ctrl.Value == "" {
ctrl.Value := this.settings.livesplit_local_reset
} else if (RegExMatch(ctrl.Value, "[^+!^]") != 0
and this.settings.livesplit_local_reset != ctrl.Value) {
this.settings.livesplit_local_reset := ctrl.Value
this.reroll_control.UpdateLivesplit()
valid_change := True
}
Case "livesplit_local_start":
if ctrl.Value == "" {
ctrl.Value := this.settings.livesplit_local_start
} else if (RegExMatch(ctrl.Value, "[^+!^]") != 0
and this.settings.livesplit_local_start != ctrl.Value) {
this.settings.livesplit_local_start := ctrl.Value
this.reroll_control.UpdateLivesplit()
valid_change := True
}
Case "livesplit_network_host":
if (this.settings.livesplit_network_host != ctrl.Text) {
this.settings.livesplit_network_host := ctrl.Text
this.reroll_control.UpdateLivesplit()
valid_change := True
}
Case "livesplit_network_port":
if (ctrl.Text > 0 and ctrl.Text < 65536
and this.settings.livesplit_network_port != ctrl.Text) {
this.settings.livesplit_network_port := ctrl.Text
this.reroll_control.UpdateLivesplit()
valid_change := True
}
Default:
throw ValueError("Unknown control change event received" . ctrl.Name)
}
if (valid_change) {
SetTimer(this.save_func, 750)
}
}
OnClick(ctrl, info) {
SetTimer(ObjBindMethod(this.reroll_control, "RunTest"), -1)
}
}
class RerollControl {
__New(settings) {
this.settings := settings
this.current_hotkey := ""
this.UpdateLivesplit()
this.UpdateHotkey()
}
UpdateLivesplit() {
Switch(this.settings.livesplit_method) {
Case "Keyboard":
this.livesplit := LocalLivesplit(this.settings)
Case "TCP":
this.livesplit := NetworkLivesplit(this.settings)
Default:
throw ValueError("Unknown livesplit control method: " . settings.livesplit_method)
}
}
UpdateHotkey() {
cb_func := ObjBindMethod(this, "HotkeyCallback")
if this.current_hotkey != "" {
Hotkey(this.current_hotkey, "Off")
}
Hotkey(this.settings.reroll_key, cb_func)
this.current_hotkey := this.settings.reroll_key
}
DisableHotkey() {
if this.current_hotkey != "" {
Hotkey(this.current_hotkey, "Off")
this.current_hotkey := ""
}
}
FindButton() {
default_color := "0x8E8E8E"
inter_color := "0x242324"
active_color := "0xE39827"
border_color := "0x313031"
MouseGetPos(&mstart_x, &mstart_y)
WinGetClientPos(,, &win_width, &win_height, "A")
mcur_x := mstart_x
mcur_y := mstart_y
start_width := win_width * 0.2
start_height := win_height * 0.2
if (mcur_x < win_width / 2 and mcur_y < win_height / 2
and PixelSearch(&_, &_, mcur_x - 5, mcur_y - 5, mcur_x + 5, mcur_y + 5,
active_color, 25)) {
MouseMove(0, 0)
mcur_x := 0
mcur_y := 0
Sleep 100
}
maptype_found := false
search_y := 0
while (search_y < start_height and !maptype_found) {
search_x := 0
while (search_x < start_width) {
if (search_x == 0) {
search_bottom := start_height
} else {
search_bottom := search_y
}
if (PixelSearch(&maptype_left, &maptype_top,
search_x, search_y,
start_width, search_bottom,
default_color)) {
if (PixelGetColor(maptype_left + 1, maptype_top) == default_color
and PixelGetColor(maptype_left, maptype_top + 1) == default_color) {
maptype_found := true
break
}
search_x := maptype_left + 1
search_y := maptype_top
} else {
search_y := search_bottom
break
}
}
search_y += 1
}
if (!maptype_found) {
throw MyError("Could not find reroll button location`n`n{type_topleft}")
}
right_limit := maptype_left + (win_width * 0.5)
if (!PixelSearch(&inter_x, &inter_y,
maptype_left, maptype_top,
right_limit, maptype_top,
inter_color)) {
throw MyError("Could not find reroll button location`n`n{inter}")
}
right_limit := inter_x + (win_width * 0.3)
bottom_limit := maptype_top + ((inter_x - maptype_left) / 5)
if (mcur_y > maptype_top - 5 and mcur_y < bottom_limit
and mcur_x > inter_x and mcur_x < right_limit
and PixelSearch(&_, &_, mcur_x - 5, mcur_y - 5, mcur_x + 5, mcur_y + 5,
active_color, 25)) {
MouseMove(0, 0)
mcur_x := 0
mcur_y := 0
Sleep 100
}
if (!PixelSearch(&reroll_left, &reroll_top,
inter_x, inter_y - 2,
right_limit, inter_y + 2,
default_color)) {
throw MyError("Could not find reroll button location`n`n{reroll_left}")
}
right_limit := reroll_left + ((reroll_left - maptype_left) / 10)
bottom_limit := reroll_top + ((reroll_left - maptype_left) / 10)
if (mcur_y > reroll_top - 5 and mcur_y < bottom_limit + 5
and mcur_x > reroll_left - 5 and mcur_x < right_limit + 5
and PixelSearch(&_, &_, mcur_x - 5, mcur_y - 5, mcur_x + 5, mcur_y + 5,
active_color, 25)) {
MouseMove(0, 0)
mcur_x := 0
mcur_y := 0
Sleep 100
}
; Right to Left search for right edge of reroll button
if (!PixelSearch(&reroll_right, &_,
right_limit, reroll_top,
reroll_left, reroll_top,
default_color)) {
throw MyError("Could not find reroll button location`n`n{reroll_right}")
}
half := (reroll_right - reroll_left) / 2
center_x := reroll_left + half
center_y := reroll_top + half
MouseMove(center_x, center_y)
Loop 10 {
Sleep 10
found_color := PixelGetColor(center_x, reroll_top + (half / 2))
if (found_color == active_color) {
break
}
}
if (found_color != active_color) {
throw MyError("Could not find reroll button location`n`n{active_color}")
}
MouseMove(mstart_x, mstart_y)
return [reroll_left + half, reroll_top + half]
}
RunTest() {
try {
livesplit := this.livesplit
livesplit.Reset()
livesplit.Start()
Sleep(5000)
livesplit.Reset()
} catch MyError as err {
Msgbox(err.Message, "Error", "OK")
}
}
TriggerReroll() {
win_class := WinGetClass("A")
if win_class != "Factorio" {
MsgBox("Factorio doesn't appear to be the active window.")
return
}
try {
button_pos := this.FindButton()
this.livesplit.Reset()
this.livesplit.Start()
MouseGetPos(&mstart_x, &mstart_y)
Click(button_pos[1], button_pos[2])
MouseMove(mstart_x, mstart_y)
} catch MyError as err {
MsgBox(err.Message, "Error", "OK")
}
}
HotkeyCallback(hotkey_name) {
this.TriggerReroll()
}
}
#SingleInstance Force
A_ScriptName := "Factorio Map Roller"
A_IconTip := "Factorio Map Roller"
try {
TraySetIcon("maproll.ico")
} catch Error as err {
if (err.Message != "Can't load icon.") {
throw err
}
}
if (A_IsCompiled) {
A_IconHidden := 1
}
settings := SettingStore()
settings.Load()
reroller := RerollControl(settings)
setting_gui := SettingGUI(settings, reroller)
setting_gui.Show()