z_mirror/EditorExtensions
git clone https://git.raiguard.me/z_mirror/EditorExtensions
b32ba01
hidden-items
1local direction_util =require ( "__flib__/direction" ) 2local position =require ( "__flib__/position" ) 3 4--- @type table<defines.direction, Vector> 5local offsets ={ 6[ defines . direction . north ] ={ 0 , - 1 } , 7[ defines . direction . east ] ={ 1 , 0 } , 8[ defines . direction . south ] ={ 0 , 1 } , 9[ defines . direction . west ] ={ - 1 , 0 } , 10} 11 12local transport_belt_connectables ={ 13"transport-belt" , 14"underground-belt" , 15"splitter" , 16"loader" , 17"loader-1x1" , 18"linked-belt" , 19} 20 21--- @param entity LuaEntity 22local function snap ( entity ) 23local offset_direction =entity . direction 24if entity . loader_type == "input" then 25offset_direction =direction_util . opposite ( offset_direction ) 26end 27local belt_position =position . add ( entity . position , offsets [ offset_direction ]) 28local belt = 29entity . surface . find_entities_filtered ( { position =belt_position , type =transport_belt_connectables } )[ 1 ] 30if not belt then 31belt = 32entity . surface . find_entities_filtered ( { position =belt_position , ghost_type =transport_belt_connectables } )[ 1 ] 33end 34if not belt then 35return 36end 37if belt . direction == direction_util . opposite ( entity . direction ) then 38entity . loader_type =entity . loader_type == "output" and "input" or "output" 39end 40end 41 42--- @param entity LuaEntity 43--- @param chest LuaEntity? 44local function sync_chest_filter ( entity , chest ) 45if not chest then 46chest =entity . surface . find_entity ( "ee-infinity-loader-chest" , entity . position ) 47end 48if not chest then 49entity . destroy () 50return 51end 52local filter =entity . get_filter ( 1 ) 53if filter then 54chest . set_infinity_container_filter ( 1 , { 55index =1 , 56name =filter , 57count =game . item_prototypes [ filter ]. stack_size * 5 , 58mode ="exactly" , 59} ) 60else 61chest . set_infinity_container_filter ( 1 , nil ) 62end 63end 64 65--- @param loader LuaEntity 66--- @param combinator LuaEntity 67local function copy_from_loader_to_combinator ( loader , combinator ) 68local filter =loader . get_filter ( 1 ) 69if not filter then 70return 71end 72local cb =combinator . get_or_create_control_behavior () --[[@as LuaConstantCombinatorControlBehavior]] 73cb . set_signal ( 1 , { 74signal ={ type ="item" , name =filter } , 75count =1 , 76} ) 77end 78 79--- @param combinator LuaEntity 80--- @param loader LuaEntity 81local function copy_from_combinator_to_loader ( combinator , loader ) 82local cb =combinator . get_control_behavior () --[[@as LuaConstantCombinatorControlBehavior?]] 83if not cb then 84return 85end 86local signal =cb . get_signal ( 1 ) 87if signal . signal and signal . signal . type == "item" then 88loader . set_filter ( 1 , signal . signal . name ) 89else 90loader . set_filter ( 1 , nil ) 91end 92sync_chest_filter ( loader ) 93end 94 95--- @param e BuiltEvent 96local function on_entity_built ( e ) 97local entity =e . entity or e . created_entity or e . destination 98if not entity . valid then 99return 100end 101 102if entity . name ~= "ee-infinity-loader" then 103return 104end 105 106-- Create chest 107local chest =entity . surface . create_entity ( { 108name ="ee-infinity-loader-chest" , 109position =entity . position , 110force =entity . force , 111create_build_effect_smoke =false , 112raise_built =true , 113} ) 114 115if not chest then 116entity . destroy () 117return 118end 119 120chest . remove_unfiltered_items =true 121sync_chest_filter ( entity , chest ) 122snap ( entity ) 123end 124 125--- @param e DestroyedEvent 126local function on_entity_destroyed ( e ) 127local entity =e . entity 128if not entity . valid or entity . name ~= "ee-infinity-loader" then 129return 130end 131local chest =entity . surface . find_entity ( "ee-infinity-loader-chest" , entity . position ) 132if chest then 133chest . destroy ( { raise_destroy =true } ) 134end 135end 136 137--- @param e EventData.on_player_rotated_entity 138local function on_entity_rotated ( e ) 139local entity =e . entity 140if not entity . valid or entity . name ~= "ee-infinity-loader" then 141return 142end 143sync_chest_filter ( entity ) 144end 145 146--- @param e EventData.on_entity_settings_pasted 147local function on_entity_settings_pasted ( e ) 148local source , destination =e . source , e . destination 149local source_is_loader , destination_is_loader = 150source . name == "ee-infinity-loader" , destination . name == "ee-infinity-loader" 151if source_is_loader and destination . name == "constant-combinator" then 152copy_from_loader_to_combinator ( source , destination ) 153elseif source . name == "constant-combinator" and destination_is_loader then 154copy_from_combinator_to_loader ( source , destination ) 155elseif destination_is_loader then 156sync_chest_filter ( destination ) 157end 158end 159 160--- @param e EventData.on_gui_opened 161local function on_gui_opened ( e ) 162if e . gui_type ~= defines . gui_type . entity then 163return 164end 165local entity =e . entity 166if not entity or not entity . valid then 167return 168end 169if entity . name == "ee-infinity-loader" then 170global . infinity_loader_open [ e . player_index ] =entity 171end 172end 173 174--- @param e EventData.on_gui_closed 175local function on_gui_closed ( e ) 176if e . gui_type ~= defines . gui_type . entity then 177return 178end 179local loader =global . infinity_loader_open [ e . player_index ] 180if loader and loader . valid then 181sync_chest_filter ( loader ) 182global . infinity_loader_open [ e . player_index ] =nil 183end 184end 185 186local infinity_loader ={} 187 188function infinity_loader . on_init () 189--- @type table<uint, LuaEntity> 190global . infinity_loader_open ={} 191end 192 193infinity_loader . events ={ 194[ defines . events . on_built_entity ] =on_entity_built , 195[ defines . events . on_entity_cloned ] =on_entity_built , 196[ defines . events . on_entity_died ] =on_entity_destroyed , 197[ defines . events . on_entity_settings_pasted ] =on_entity_settings_pasted , 198[ defines . events . on_gui_closed ] =on_gui_closed , 199[ defines . events . on_gui_opened ] =on_gui_opened , 200[ defines . events . on_player_mined_entity ] =on_entity_destroyed , 201[ defines . events . on_player_rotated_entity ] =on_entity_rotated , 202[ defines . events . on_robot_built_entity ] =on_entity_built , 203[ defines . events . on_robot_mined_entity ] =on_entity_destroyed , 204[ defines . events . script_raised_built ] =on_entity_built , 205[ defines . events . script_raised_destroy ] =on_entity_destroyed , 206[ defines . events . script_raised_revive ] =on_entity_built , 207} 208 209infinity_loader . on_nth_tick ={ 210[ 15 ] =function () 211for unit_number , loader in pairs ( global . infinity_loader_open ) do 212if loader . valid then 213sync_chest_filter ( loader ) 214else 215global . infinity_loader_open [ unit_number ] =nil 216end 217end 218end , 219} 220 221return infinity_loader