-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontrol.lua
164 lines (138 loc) · 4.52 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
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
local MINIMUM_SPEED = 0.125
local MAXIMUM_SPEED = 64
local DISPLAY_MODE_LOOKUP = {
none = nil,
always = true,
['when-warping'] = false
}
local DISPLAY_DEFAULT_STYLE = "bold_label" -- Style used at 1x warp
local DISPLAY_STYLES = {
[true] = "bold_green_label", -- Style used at speeds > 1
[false] = "bold_red_label", -- Style used at speeds < 1
}
local display_preferences = {}
local display_caption = { "timecontrol.gui-speed-normal" }
local display_style = DISPLAY_DEFAULT_STYLE
local function update_gui(player_index, visible)
if visible == nil then
visible = display_preferences[player_index] or false
end
local top = game.players[player_index].gui.top
local ctl = top.TimeControlSpeedometer
if not ctl then
top.add {
type = "label",
name = "TimeControlSpeedometer",
caption = display_caption,
style = display_style,
}
top.TimeControlSpeedometer.visible = visible
return
end
if visible then
ctl.caption = display_caption
ctl.style = display_style
ctl.visible = true
else
ctl.visible = false
end
end
local function update_display(all_players)
local warping = game.speed ~= 1
if warping then
display_caption = { "timecontrol.gui-speed-warping", game.speed }
display_style = DISPLAY_STYLES[game.speed > 1]
else
display_caption = { "timecontrol.gui-speed-normal" }
display_style = DISPLAY_DEFAULT_STYLE
end
if all_players then
for index, _ in pairs(game.players) do
update_gui(index)
end
else
for index, always in pairs(display_preferences) do
update_gui(index, warping or always)
end
end
end
local function update_preferences(index)
if not index then
display_preferences = {}
global.display_preferences = display_preferences
for index, _ in pairs(game.players) do update_preferences(index) end
return
end
local mode = DISPLAY_MODE_LOOKUP[settings.get_player_settings(game.players[index])["TimeControl-display"].value or 'when-warping']
display_preferences[index] = mode
return mode
end
local function on_init()
update_preferences()
return update_display(true)
end
local function on_load()
display_preferences = global.display_preferences
end
script.on_init(on_init)
script.on_load(on_load)
script.on_configuration_changed(on_init)
script.on_event(defines.events.on_player_created, function(event)
update_preferences(event.player_index)
update_gui(event.player_index)
end)
script.on_event(defines.events.on_player_removed, function(event)
display_preferences[event.player_index] = nil
end)
script.on_event(defines.events.on_runtime_mod_setting_changed, function(event)
if event.setting ~= "TimeControl-display" or event.setting_type ~= "runtime-per-user" then
return
end
update_gui(
event.player_index,
update_preferences(event.player_index) or false
)
end)
local function warp_time(event, multiplier)
local player = game.players[event.player_index]
-- Permissions check
if not (player.admin or settings.global['TimeControl-access'] == 'everyone') then
return
end
-- -- Experimental; FIXME
-- if not (
-- player and player.permission_group
---- and player.permission_group.allows_action(defines.input_action.admin_action)
---- and player.permission_group.allows_action(defines.input_action.write_to_console)
-- and true
-- ) then
-- return
-- end
local msg = "timecontrol.speed-changed"
local oldspeed = game.speed
local newspeed
if multiplier == 0 then
newspeed = 1
msg = "timecontrol.speed-reset"
else
newspeed = game.speed * multiplier
if newspeed < MINIMUM_SPEED then
newspeed = MINIMUM_SPEED
elseif newspeed > MAXIMUM_SPEED then
newspeed = MAXIMUM_SPEED
end
end
-- If the effective speed wouldn't change, avoid console spam.
if newspeed == oldspeed then
return
end
if #game.players > 1 then
msg = msg .. "-by"
end
game.print { msg, newspeed, player.name }
game.speed = newspeed
update_display()
end
script.on_event("timecontrol_normal", function(event) return warp_time(event, 0) end)
script.on_event("timecontrol_slower", function(event) return warp_time(event, 0.5) end)
script.on_event("timecontrol_faster", function(event) return warp_time(event, 2.0) end)