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