This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
control.lua
89 lines (69 loc) · 2.48 KB
/
control.lua
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
require "evoGUI"
if not evogui then evogui = {} end
function evogui.log(message)
if game then
for i, p in pairs(game.players) do
p.print(message)
end
else
error(serpent.dump(message, {compact = false, nocode = true, indent = ' '}))
end
end
function evogui.format_number(n) -- credit http://richard.warburton.it
local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end
function string.starts_with(haystack, needle)
return string.sub(haystack, 1, string.len(needle)) == needle
end
local octant_names = {
[0] = {"direction.east"},
[1] = {"direction.southeast"},
[2] = {"direction.south"},
[3] = {"direction.southwest"},
[4] = {"direction.west"},
[5] = {"direction.northwest"},
[6] = {"direction.north"},
[7] = {"direction.northeast"},
}
function evogui.get_octant_name(vector)
local radians = math.atan2(vector.y, vector.x)
local octant = math.floor( 8 * radians / (2*math.pi) + 8.5 ) % 8
return octant_names[octant]
end
script.on_init(evogui.mod_init)
script.on_configuration_changed(evogui.mod_update)
script.on_event(defines.events.on_player_created, function(event)
local status, err = pcall(evogui.new_player, event)
if err then evogui.log({"err_generic", "on_player_created", err}) end
end)
script.on_event(defines.events.on_tick, function(event)
local status, err = pcall(RemoteSensor.initialize)
if err then evogui.log({"err_generic", "on_tick:remote_initialize", err}) end
local status, err = pcall(evogui.update_gui, event)
if err then evogui.log({"err_generic", "on_tick:update_gui", err}) end
end)
local last_clicked = nil
local last_checked = nil
local function raise_on_click(event)
local status, err = pcall(evogui.on_gui_click, event)
if err then
if event.element.valid then
evogui.log({"err_specific", "on_gui_click", event.element.name, err})
else
evogui.log({"err_generic", "on_gui_click", err})
end
end
end
script.on_event(defines.events.on_gui_checked_state_changed, function(event)
last_checked = event.element.name
raise_on_click(event)
end)
script.on_event(defines.events.on_gui_click, function(event)
-- prevent raising on_click twice for the same element
if last_checked ~= nil and last_checked == event.element.name then
return
end
last_clicked = event.element.name
raise_on_click(event)
end)