z_mirror/pipe-visualization-overlay

git clone https://git.raiguard.me/z_mirror/pipe-visualization-overlay

Caleb HeuerUpdate to Factorio 2.1b4514a9

trunk
2.4 KiB90 linesraw
1-- In the Factorio source code, 200 is defined as the maximum viewable distance.
2-- The map editor is 3x that, hence, 600.
3local overlay_size = 600
4
5--- @param player LuaPlayer
6local function create_overlay(player)
7  local opacity = player.mod_settings["pvo-overlay-opacity"].value --[[@as double]]
8  storage.overlays[player.index] = rendering.draw_sprite({
9    sprite = "pvo-black",
10    tint = { a = opacity },
11    render_layer = "148", -- One below "Pipe visualization" (149)
12    target = player.position,
13    surface = player.surface,
14    players = { player },
15    x_scale = overlay_size,
16    y_scale = overlay_size,
17    only_in_alt_mode = true,
18  })
19end
20
21--- @param player LuaPlayer
22--- @return boolean
23local function should_have_overlay(player)
24  local cursor_stack = player.cursor_stack
25  local cursor_ghost = player.cursor_ghost
26  --- @type LuaEntityPrototype?
27  local entity
28  if cursor_stack and cursor_stack.valid_for_read then
29    entity = cursor_stack.prototype.place_result
30  elseif cursor_ghost and cursor_ghost.name then
31    entity = cursor_ghost.name.place_result
32  end
33  return entity and entity.show_fluid_visualization_when_in_cursor or false
34end
35
36script.on_init(function()
37  --- @type table<uint, LuaRenderObject>
38  storage.overlays = {}
39end)
40
41script.on_event(defines.events.on_player_changed_position, function(e)
42  if not storage.overlays then
43    return
44  end
45  local overlay = storage.overlays[e.player_index]
46  if overlay then
47    overlay.target = game.get_player(e.player_index).position
48  end
49end)
50
51script.on_event(defines.events.on_player_changed_surface, function(e)
52  if not storage.overlays then
53    return
54  end
55  local overlay = storage.overlays[e.player_index]
56  if not overlay or not overlay.valid then
57    return
58  end
59  overlay.destroy()
60  storage.overlays[e.player_index] = nil
61  local player = game.get_player(e.player_index)
62  --- @cast player -?
63  if should_have_overlay(player) then
64    create_overlay(player)
65  end
66end)
67
68script.on_event(defines.events.on_player_cursor_stack_changed, function(e)
69  if not storage.overlays then
70    return
71  end
72
73  local player = game.get_player(e.player_index)
74  --- @cast player -?
75
76  local should_have_overlay = should_have_overlay(player)
77  local overlay = storage.overlays[e.player_index]
78
79  if overlay and overlay.valid then
80    if not should_have_overlay then
81      overlay.destroy()
82      storage.overlays[e.player_index] = nil
83    end
84    return
85  end
86
87  if should_have_overlay then
88    create_overlay(player)
89  end
90end)