z_mirror/Krastorio2

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

Caleb HeuerFix being able to spawn free roboports44f3c689

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