-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontrol.lua
250 lines (228 loc) · 7.23 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
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
if script.active_mods["bobinserters"] then
return
end
local flib_gui = require("__flib__.gui")
local flib_math = require("__flib__.math")
local flib_position = require("__flib__.position")
local near_vectors = {
[defines.direction.north] = { 0.01, -0.2 },
[defines.direction.east] = { 0.2, 0.01 },
[defines.direction.south] = { -0.01, 0.2 },
[defines.direction.west] = { -0.2, -0.01 },
}
local far_vectors = {
[defines.direction.north] = { 0.0, 0.2 },
[defines.direction.east] = { -0.2, 0.0 },
[defines.direction.south] = { 0.0, -0.2 },
[defines.direction.west] = { 0.2, 0.0 },
}
--- @param entity LuaEntity
local function is_compatible(entity)
return entity.prototype.allow_custom_vectors and not string.find(entity.name, "%-?miniloader%-inserter")
end
--- @param entity LuaEntity
local function get_is_far(entity)
local drop_pos_vector = flib_position.sub(entity.drop_position, entity.position)
local vector_length = flib_math.sqrt(drop_pos_vector.x * drop_pos_vector.x + drop_pos_vector.y * drop_pos_vector.y)
return vector_length % 1 < 0.5, drop_pos_vector
end
--- @param entity LuaEntity
--- @param is_far boolean
local function change_mode_fx(entity, is_far)
for _, player in pairs(game.players) do
if player.surface == entity.surface then
player.create_local_flying_text({
text = is_far and { "message.cidl-drop-far" } or { "message.cidl-drop-near" },
color = { r = 1, g = 0.5, b = 0.25 },
position = entity.position,
})
end
end
-- Welding sound
game.play_sound({
path = "cidl-welding",
position = entity.position,
volume_modifier = 1.0,
})
-- Welding particle
entity.surface.create_particle({
name = "cidl-welding",
position = { entity.position.x, entity.position.y + 1 },
movement = { 0.0, -0.05 },
height = 1.0,
vertical_speed = 0.015,
frame_speed = 1,
})
end
--- @param player LuaPlayer
--- @param entity LuaEntity
local function change_lane(player, entity)
if not entity.prototype.allow_custom_vectors then
player.create_local_flying_text({
text = { "message.cidl-cannot-change-drop-lane" },
create_at_cursor = true,
})
player.play_sound({ path = "utility/cannot_build" })
return
end
-- Change lane
local is_far, drop_pos_vector = get_is_far(entity)
local vectors = is_far and near_vectors or far_vectors
local vector = vectors[entity.direction]
entity.drop_position = {
entity.position.x + flib_math.round(drop_pos_vector.x) + vector[1],
entity.position.y + flib_math.round(drop_pos_vector.y) + vector[2],
}
-- Special effects
change_mode_fx(entity, not is_far)
end
--- @param e EventData.on_gui_switch_state_changed
local function on_droplane_switch_state_changed(e)
local player = game.get_player(e.player_index)
if not player or player.opened_gui_type ~= defines.gui_type.entity then
return
end
local entity = player.opened --[[@as LuaEntity?]]
if not entity or not is_compatible(entity) then
return
end
change_lane(player, entity)
e.element.switch_state = get_is_far(entity) and "right" or "left"
end
--- @param player LuaPlayer
--- @param entity LuaEntity
local function create_gui(player, entity)
local window = player.gui.relative.cidl_window
if window then
window.destroy()
end
flib_gui.add(player.gui.relative, {
type = "frame",
name = "cidl_window",
caption = { "gui.cidl-drop-lane" },
anchor = {
gui = defines.relative_gui_type.inserter_gui,
position = defines.relative_gui_position.right,
},
{
type = "frame",
style = "inside_shallow_frame_with_padding",
{
type = "switch",
left_label_caption = { "gui.cidl-near" },
right_label_caption = { "gui.cidl-far" },
switch_state = get_is_far(entity) and "right" or "left",
handler = {
[defines.events.on_gui_switch_state_changed] = on_droplane_switch_state_changed,
},
},
},
})
end
--- @param e EventData.CustomInputEvent
local function on_change_lane(e)
local player = game.get_player(e.player_index)
if not player then
return
end
local entity = player.selected
if not entity then
return
end
if not player.can_reach_entity(entity) then
player.create_local_flying_text({
text = { "cant-reach" },
create_at_cursor = true,
})
player.play_sound({ path = "utility/cannot_build" })
return
end
change_lane(player, entity)
end
--- @param e EventData.on_pre_entity_settings_pasted
local function on_pre_entity_settings_pasted(e)
local source, destination = e.source, e.destination
if not source.valid or not destination.valid then
return
end
if source.type ~= "inserter" or destination.type ~= "inserter" then
return
end
if not destination.prototype.allow_custom_vectors then
return
end
storage.temp_inserter_settings[destination.unit_number] = {
drop_position = destination.drop_position,
pickup_position = destination.pickup_position,
}
end
--- @param e EventData.on_entity_settings_pasted
local function on_entity_settings_pasted(e)
local source, destination = e.source, e.destination
if not source.valid or not destination.valid then
return
end
if source.type ~= "inserter" or destination.type ~= "inserter" then
return
end
if not destination.prototype.allow_custom_vectors then
return
end
local destination_unit_number = destination.unit_number --[[@as uint]]
local temp_settings = storage.temp_inserter_settings[destination_unit_number]
if not temp_settings then
return
end
storage.temp_inserter_settings[destination_unit_number] = nil
destination.drop_position = temp_settings.drop_position
destination.pickup_position = temp_settings.pickup_position
local player = game.get_player(e.player_index)
if not player then
return
end
local destination_is_far = get_is_far(destination)
if destination_is_far ~= get_is_far(source) then
change_lane(player, destination)
end
end
--- @param e EventData.on_gui_opened
local function on_gui_opened(e)
local player = game.get_player(e.player_index)
if not player then
return
end
if player.opened_gui_type ~= defines.gui_type.entity then
return
end
local entity = player.opened --[[@as LuaEntity?]]
if not entity or not is_compatible(entity) then
return
end
create_gui(player, entity)
end
--- @class InserterSettings
--- @field drop_position MapPosition
--- @field pickup_position MapPosition
local function on_init()
--- @type table<uint, InserterSettings?>
storage.temp_inserter_settings = {}
end
flib_gui.add_handlers({ on_droplane_switch_state_changed = on_droplane_switch_state_changed })
flib_gui.handle_events()
script.on_init(on_init)
script.on_configuration_changed(on_init)
script.on_event("cidl-change-lane", on_change_lane)
script.on_event(defines.events.on_gui_opened, on_gui_opened)
script.on_event(defines.events.on_pre_entity_settings_pasted, on_pre_entity_settings_pasted)
script.on_event(defines.events.on_entity_settings_pasted, on_entity_settings_pasted)
-- For simulations
remote.add_interface("ChangeInserterDropLane_simulation", {
--- @param player LuaPlayer
change_selected_lane = function(player)
local selected = player.selected
if not selected then
return
end
change_lane(player, selected)
end,
})