z_mirror/EditorExtensions

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

Caleb HeuerRe-add debug world overridinge4c5e75

1.14
3.5 KiB125 linesraw
1--- @class Util
2local util = {}
3
4local coreutil = require("__core__/lualib/util")
5util.parse_energy = coreutil.parse_energy
6
7--- @param handler GuiElemHandler
8function util.close_button(handler)
9  return {
10    type = "sprite-button",
11    style = "frame_action_button",
12    sprite = "utility/close_white",
13    hovered_sprite = "utility/close_black",
14    clicked_sprite = "utility/close_black",
15    tooltip = { "gui.close-instruction" },
16    mouse_button_filter = { "left" },
17    handler = { [defines.events.on_gui_click] = handler },
18  }
19end
20
21--- @param mode InfinityPipeMode
22--- @param handler GuiElemHandler
23--- @return GuiElemDef
24function util.mode_radio_button(mode, handler)
25  return {
26    type = "radiobutton",
27    name = "mode_radio_button_" .. string.gsub(mode, "%-", "_"),
28    caption = { "gui-infinity-container." .. mode },
29    tooltip = { "gui-infinity-pipe." .. mode .. "-tooltip" },
30    state = false,
31    tags = { mode = mode },
32    handler = { [defines.events.on_gui_checked_state_changed] = handler },
33  }
34end
35
36function util.pusher()
37  return { type = "empty-widget", style = "flib_horizontal_pusher", ignored_by_interaction = true }
38end
39
40--- @return boolean
41function util.in_debug_world()
42  if not settings.global["ee-override-debug-world"].value then
43    return false
44  end
45  if script.level.mod_name ~= "base" or script.level.level_name ~= "freeplay" then
46    return false
47  end
48  local nauvis = game.get_surface("nauvis")
49  if not nauvis then
50    return false
51  end
52  local mps = nauvis.map_gen_settings
53  return mps.height == 50 and mps.width == 50
54end
55
56--- @return boolean
57function util.in_testing_scenario()
58  return script.level.mod_name == "EditorExtensions" and script.level.level_name == "testing"
59end
60
61--- @param player LuaPlayer
62--- @param message LocalisedString
63--- @param play_sound boolean?
64--- @param position MapPosition?
65function util.flying_text(player, message, play_sound, position)
66  player.create_local_flying_text({
67    text = message,
68    create_at_cursor = not position,
69    position = position,
70  })
71  if play_sound then
72    player.play_sound({ path = "utility/cannot_build" })
73  end
74end
75
76--- @param player LuaPlayer
77--- @return boolean
78function util.player_can_use_editor(player)
79  local can_use_editor = player.admin
80  local permission_group = player.permission_group
81  if permission_group then
82    can_use_editor = permission_group.allows_action(defines.input_action.toggle_map_editor)
83    if not can_use_editor and player.controller_type == defines.controllers.editor then
84      player.print({ "message.ee-cannot-use-map-editor" })
85      -- XXX: We need to re-enable the capability in order to get them out of the editor
86      permission_group.set_allows_action(defines.input_action.toggle_map_editor, true)
87      player.toggle_map_editor()
88      permission_group.set_allows_action(defines.input_action.toggle_map_editor, false)
89    end
90  end
91  player.set_shortcut_available("ee-toggle-map-editor", can_use_editor)
92  return can_use_editor
93end
94
95--- @param e EventData.on_player_setup_blueprint
96--- @return LuaItemStack?
97function util.get_blueprint(e)
98  local player = game.get_player(e.player_index)
99  if not player then
100    return
101  end
102
103  local bp = player.blueprint_to_setup
104  if bp and bp.valid_for_read then
105    return bp
106  end
107
108  bp = player.cursor_stack
109  if not bp or not bp.valid_for_read then
110    return
111  end
112
113  if bp.type == "blueprint-book" then
114    local item_inventory = bp.get_inventory(defines.inventory.item_main)
115    if item_inventory then
116      bp = item_inventory[bp.active_index]
117    else
118      return
119    end
120  end
121
122  return bp
123end
124
125return util