z_mirror/Krastorio2

git clone https://git.raiguard.me/z_mirror/Krastorio2

Caleb HeuerRemove references to inserter hotkey, which is now a standalone mod21723abc

discrete-steel-pipes
7.1 KiB266 linesraw
1local flib_gui = require("__flib__.gui")
2
3local util = require("scripts.util")
4
5--- @type table<string, {color: Color?, suffix: string, text: string}
6local mode_data = {
7  normal = { suffix = "", text = "normal" },
8  construction = { color = { r = 0.5, g = 1, b = 0.4 }, suffix = "-construction-mode", text = "construction" },
9  logistic = { color = { r = 1, g = 0.5, b = 0.25 }, suffix = "-logistic-mode", text = "logistic" },
10}
11
12local next_roboport_mode = {
13  [""] = "construction",
14  ["-construction-mode"] = "logistic",
15  ["-logistic-mode"] = "normal",
16}
17
18--- @param player LuaPlayer
19--- @param entity LuaEntity
20local function update_gui(player, entity)
21  local player_gui = storage.roboport_guis[player.index]
22  if not player_gui or not player_gui.valid then
23    return
24  end
25  local mode = string.match(entity.name, "^.*%-(%w*)%-mode") or "normal"
26
27  for _, button in pairs(player_gui.inner_frame.children) do
28    if button.name == mode then
29      button.state = true
30    else
31      button.state = false
32    end
33  end
34end
35
36--- @param entity LuaEntity
37--- @param player LuaPlayer
38--- @param to_mode string?
39local function change_mode(entity, player, to_mode)
40  local matched, _, base_name, suffix = string.find(entity.name, "^(.-)(%-%w-%-mode)$")
41  if not matched then
42    base_name = entity.name
43    suffix = ""
44  end
45
46  local new_mode_data
47  if to_mode then
48    new_mode_data = mode_data[to_mode]
49  else
50    new_mode_data = mode_data[next_roboport_mode[suffix]]
51  end
52
53  local new_name = base_name .. new_mode_data.suffix
54
55  if not prototypes.entity[new_name] then
56    util.flying_text_with_sound(player, { "message.kr-roboport-modes-not-supported" }, { position = entity.position })
57    return
58  end
59
60  local energy = entity.energy
61  local health = entity.health
62
63  -- Find all players that have this roboport open
64  -- This must be done before the roboport is swapped
65  local guis_to_update = {}
66  for player_index in pairs(storage.roboport_guis) do
67    local gui_player = game.get_player(player_index)
68    if gui_player and gui_player.opened_gui_type == defines.gui_type.entity then
69      local opened = gui_player.opened
70      if opened and opened == entity then
71        table.insert(guis_to_update, gui_player)
72      end
73    end
74  end
75
76  local new_entity = entity.surface.create_entity({
77    name = new_name,
78    position = entity.position,
79    force = entity.force,
80    direction = entity.direction,
81    fast_replace = true,
82    raise_built = true,
83    spill = false,
84    create_build_effect_smoke = false,
85  })
86
87  if not new_entity then
88    return
89  end
90  new_entity.energy = energy
91  new_entity.health = health
92  new_entity.last_user = player
93
94  util.entity_flying_text(new_entity, { "message.kr-" .. new_mode_data.text .. "-mode" }, new_mode_data.color)
95  game.play_sound({
96    path = "kr-welding",
97    position = new_entity.position,
98    volume_modifier = 1.0,
99  })
100  new_entity.surface.create_particle({
101    name = "kr-welding-particle",
102    position = { new_entity.position.x, new_entity.position.y + 1 },
103    movement = { 0.0, -0.05 },
104    height = 1.0,
105    vertical_speed = 0.015,
106    frame_speed = 1,
107  })
108
109  -- Swapping the entity will close any open GUIs, so we must re-open them.
110  for _, gui_player in pairs(guis_to_update) do
111    gui_player.opened = new_entity
112    update_gui(gui_player, new_entity)
113  end
114end
115
116--- @param e EventData.on_gui_checked_state_changed
117local function on_mode_radio_button_checked(e)
118  local player = game.get_player(e.player_index)
119  if not player or player.opened_gui_type ~= defines.gui_type.entity then
120    return
121  end
122  local entity = player.opened --[[@as LuaEntity]]
123  if not entity.valid or entity.type ~= "roboport" then
124    return
125  end
126  change_mode(entity, player, e.element.name)
127end
128
129--- @param mode string
130local function radio_button(mode)
131  return {
132    type = "radiobutton",
133    name = mode,
134    caption = { "message.kr-" .. mode .. "-mode" },
135    state = false,
136    handler = { [defines.events.on_gui_checked_state_changed] = on_mode_radio_button_checked },
137  }
138end
139
140--- @param player_index uint
141local function destroy_gui(player_index)
142  local player_gui = storage.roboport_guis[player_index]
143  if not player_gui then
144    return
145  end
146  if player_gui.valid then
147    player_gui.destroy()
148  end
149  storage.roboport_guis[player_index] = nil
150end
151
152--- @param player LuaPlayer
153local function refresh_gui(player)
154  destroy_gui(player.index)
155
156  _, storage.roboport_guis[player.index] = flib_gui.add(player.gui.relative, {
157    type = "frame",
158    name = "kr_roboport_mode_window",
159    caption = { "gui.kr-change-mode" },
160    anchor = {
161      gui = defines.relative_gui_type.roboport_gui,
162      position = defines.relative_gui_position.right,
163      names = storage.roboport_has_variants_gui,
164    },
165    {
166      type = "frame",
167      name = "inner_frame",
168      style = "inside_shallow_frame_with_padding",
169      direction = "vertical",
170      radio_button("normal"),
171      radio_button("construction"),
172      radio_button("logistic"),
173    },
174  })
175end
176
177local function find_variants()
178  -- Find all compatible roboports
179  local has_variants_gui = {}
180  for _, entity in pairs(prototypes.get_entity_filtered({ { filter = "type", type = "roboport" } })) do
181    local matched, _, base_name = string.find(entity.name, "^(.-)%-%w*%-mode")
182    if matched then
183      has_variants_gui[base_name] = true
184      has_variants_gui[entity.name] = true
185    end
186  end
187
188  -- Convert to a format usable by GuiAnchor
189  local output = {}
190  local i = 0
191  for entity in pairs(has_variants_gui) do
192    i = i + 1
193    output[i] = entity
194  end
195
196  storage.roboport_has_variants_gui = output
197end
198
199--- @param e EventData.on_gui_opened
200local function on_gui_opened(e)
201  local entity = e.entity
202  if not entity or not entity.valid then
203    return
204  end
205  local player = game.get_player(e.player_index)
206  if not player then
207    return
208  end
209  if entity.type == "roboport" then
210    update_gui(player, entity)
211  end
212end
213
214--- @param e EventData.on_player_created
215local function on_player_created(e)
216  local player = game.get_player(e.player_index)
217  if player then
218    refresh_gui(player)
219  end
220end
221
222--- @param e EventData.on_player_removed
223local function on_player_removed(e)
224  destroy_gui(e.player_index)
225end
226
227--- @param e EventData.CustomInputEvent
228local function on_change_roboport_mode_hotkey(e)
229  local player = game.get_player(e.player_index)
230  if not player then
231    return
232  end
233  local selected = player.selected
234  if selected and selected.valid and selected.type == "roboport" then
235    change_mode(selected, player)
236  end
237end
238
239local function on_configuration_changed()
240  find_variants()
241  for _, player in pairs(game.players) do
242    refresh_gui(player)
243  end
244end
245
246local function on_init()
247  --- @type table<uint, LuaGuiElement>
248  storage.roboport_guis = {}
249  on_configuration_changed()
250end
251
252local roboport = {}
253
254roboport.on_init = on_init
255roboport.on_configuration_changed = on_configuration_changed
256
257roboport.events = {
258  [defines.events.on_gui_opened] = on_gui_opened,
259  [defines.events.on_player_created] = on_player_created,
260  [defines.events.on_player_removed] = on_player_removed,
261  ["kr-change-roboport-state"] = on_change_roboport_mode_hotkey,
262}
263
264flib_gui.add_handlers({ roboport_on_mode_radio_button_checked = on_mode_radio_button_checked })
265
266return roboport