-- In the Factorio source code, 200 is defined as the maximum viewable distance. -- The map editor is 3x that, hence, 600. local overlay_size = 600 --- @param player LuaPlayer local function create_overlay(player) local opacity = player.mod_settings["pvo-overlay-opacity"].value --[[@as double]] storage.overlays[player.index] = rendering.draw_sprite({ sprite = "pvo-black", tint = { a = opacity }, render_layer = "148", -- One below "Pipe visualization" (149) target = player.position, surface = player.surface, players = { player }, x_scale = overlay_size, y_scale = overlay_size, only_in_alt_mode = true, }) end --- @param player LuaPlayer --- @return boolean local function should_have_overlay(player) local cursor_stack = player.cursor_stack local cursor_ghost = player.cursor_ghost --- @type LuaEntityPrototype? local entity if cursor_stack and cursor_stack.valid_for_read then entity = cursor_stack.prototype.place_result elseif cursor_ghost and cursor_ghost.name then entity = cursor_ghost.name.place_result end return entity and entity.show_fluid_visualization_when_in_cursor or false end script.on_init(function() --- @type table storage.overlays = {} end) script.on_event(defines.events.on_player_changed_position, function(e) if not storage.overlays then return end local overlay = storage.overlays[e.player_index] if overlay then overlay.target = game.get_player(e.player_index).position end end) script.on_event(defines.events.on_player_changed_surface, function(e) if not storage.overlays then return end local overlay = storage.overlays[e.player_index] if not overlay or not overlay.valid then return end overlay.destroy() storage.overlays[e.player_index] = nil local player = game.get_player(e.player_index) --- @cast player -? if should_have_overlay(player) then create_overlay(player) end end) script.on_event(defines.events.on_player_cursor_stack_changed, function(e) if not storage.overlays then return end local player = game.get_player(e.player_index) --- @cast player -? local should_have_overlay = should_have_overlay(player) local overlay = storage.overlays[e.player_index] if overlay and overlay.valid then if not should_have_overlay then overlay.destroy() storage.overlays[e.player_index] = nil end return end if should_have_overlay then create_overlay(player) end end)