z_mirror/EditorExtensions
git clone https://git.raiguard.me/z_mirror/EditorExtensions
5caf159
trunk
1--- @class InfinityFluidWagonData 2--- @field flip integer 3--- @field proxy LuaEntity 4--- @field wagon LuaEntity 5--- @field wagon_last_position MapPosition 6--- @field wagon_name string 7 8--- @param player LuaPlayer 9--- @param entity LuaEntity 10local function open_gui ( player , entity ) 11player . opened =storage . wagons [ entity . unit_number ]. proxy 12end 13 14--- @param e BuiltEvent 15local function on_entity_built ( e ) 16local entity =e . entity or e . destination 17if not entity or not entity . valid or entity . name ~= "ee-infinity-fluid-wagon" then 18return 19end 20 21local proxy =entity . surface . create_entity ( { 22name ="ee-infinity-wagon-pipe" , 23position =entity . position , 24force =entity . force , 25} ) 26if not proxy then 27return 28end 29 30-- Create all api lookups here to save time in on_tick() 31local data ={ 32flip =0 , 33proxy =proxy , 34wagon =entity , 35wagon_last_position =entity . position , 36wagon_name =entity . name , 37} 38storage . wagons [ entity . unit_number ] =data 39 40local tags =e . tags 41if not tags or not tags . EditorExtensions then 42return 43end 44proxy . set_infinity_pipe_filter ( tags . EditorExtensions --[[@as InfinityPipeFilter]] ) 45end 46 47--- @param e DestroyedEvent 48local function on_entity_destroyed ( e ) 49local entity =e . entity 50if not entity or not entity . valid or entity . name ~= "ee-infinity-fluid-wagon" then 51return 52end 53 54local proxy =storage . wagons [ entity . unit_number ]. proxy 55if proxy and proxy . valid then 56proxy . destroy ( {} ) 57end 58storage . wagons [ entity . unit_number ] =nil 59end 60 61--- @param e EventData.on_marked_for_deconstruction 62local function on_marked_for_deconstruction ( e ) 63local entity =e . entity 64if not entity . valid or entity . name ~= "ee-infinity-fluid-wagon" then 65return 66end 67storage . wagons [ entity . unit_number ]. flip =3 68end 69 70--- @param e EventData.on_cancelled_deconstruction 71local function on_cancelled_deconstruction ( e ) 72local entity =e . entity 73if not entity . valid or entity . name ~= "ee-infinity-fluid-wagon" then 74return 75end 76-- Resume syncing 77storage . wagons [ entity . unit_number ]. flip =0 78end 79 80--- @param data InfinityFluidWagonData 81local function sync_fluid ( data ) 82if data . flip == 0 then 83local fluid =data . proxy . get_fluid ( 1 ) 84if fluid then 85fluid . amount =math . max ( fluid . amount * 250 , 0 ) 86data . wagon . set_fluid ( 1 , fluid ) 87else 88data . wagon . clear_fluid ( 1 ) 89end 90data . flip =1 91elseif data . flip == 1 then 92local fluid =data . wagon . get_fluid ( 1 ) 93if fluid then 94fluid . amount =math . max ( fluid . amount / 250 , 0 ) 95data . proxy . set_fluid ( 1 , fluid ) 96else 97data . proxy . clear_fluid ( 1 ) 98end 99data . flip =0 100end 101end 102 103local function on_tick () 104for unit_number , data in pairs ( storage . wagons ) do 105if not data . wagon . valid or not data . proxy . valid then 106if data . wagon . valid then 107data . wagon . destroy ( { raise_destroy =true } ) 108end 109if data . proxy . valid then 110data . proxy . destroy ( { raise_destroy =true } ) 111end 112storage . wagons [ unit_number ] =nil 113goto continue 114end 115sync_fluid ( data ) 116local position =data . wagon . position 117local last_position =data . wagon_last_position 118if not last_position or last_position . x ~= position . x or last_position . y ~= position . y then 119data . proxy . teleport ( data . wagon . position ) 120data . wagon_last_position =data . wagon . position 121end 122:: continue :: 123end 124end 125 126--- @param e EventData.CustomInputEvent 127local function on_linked_open_gui ( e ) 128local player =game . get_player ( e . player_index ) 129if not player then 130return 131end 132local entity =player . selected 133if not entity or entity . name ~= "ee-infinity-fluid-wagon" then 134return 135end 136if not player . can_reach_entity ( entity ) then 137return 138end 139open_gui ( player , entity ) 140end 141 142--- @param e EventData.on_entity_settings_pasted 143local function on_entity_settings_pasted ( e ) 144local source , destination =e . source , e . destination 145if not source . valid or not destination . valid then 146return 147end 148if source . name ~= "ee-infinity-fluid-wagon" or destination . name ~= "ee-infinity-fluid-wagon" then 149return 150end 151if source . name ~= destination . name then 152return 153end 154 155storage . wagons [ destination . unit_number ]. proxy . copy_settings ( storage . wagons [ source . unit_number ]. proxy ) 156end 157 158--- @param e EventData.on_player_setup_blueprint 159local function on_player_setup_blueprint ( e ) 160local blueprint =e . stack or e . record 161if not blueprint then 162return 163end 164 165local entities =blueprint . get_blueprint_entities () 166if not entities then 167return 168end 169for i , entity in pairs ( entities ) do 170--- @cast i uint 171if entity . name ~= "ee-infinity-fluid-wagon" then 172goto continue 173end 174local real_entity =e . surface . find_entity ( entity . name , entity . position ) 175if not real_entity then 176goto continue 177end 178local proxy =storage . wagons [ real_entity . unit_number ]. proxy 179local tags ={} 180if entity . name == "ee-infinity-cargo-wagon" then 181tags . filters =proxy . infinity_container_filters 182tags . remove_unfiltered_items =proxy . remove_unfiltered_items 183else 184tags =proxy . get_infinity_pipe_filter () 185end 186blueprint . set_blueprint_entity_tag ( i , "EditorExtensions" , tags --[[@as AnyBasic]] ) 187:: continue :: 188end 189end 190 191local infinity_wagon ={} 192 193infinity_wagon . on_init =function () 194--- @type table<uint, InfinityFluidWagonData> 195storage . wagons ={} 196end 197 198infinity_wagon . events ={ 199[ defines . events . on_built_entity ] =on_entity_built , 200[ defines . events . on_cancelled_deconstruction ] =on_cancelled_deconstruction , 201[ defines . events . on_entity_cloned ] =on_entity_built , 202[ defines . events . on_entity_died ] =on_entity_destroyed , 203[ defines . events . on_entity_settings_pasted ] =on_entity_settings_pasted , 204[ defines . events . on_marked_for_deconstruction ] =on_marked_for_deconstruction , 205[ defines . events . on_player_mined_entity ] =on_entity_destroyed , 206[ defines . events . on_player_setup_blueprint ] =on_player_setup_blueprint , 207[ defines . events . on_robot_built_entity ] =on_entity_built , 208[ defines . events . on_robot_mined_entity ] =on_entity_destroyed , 209[ defines . events . on_tick ] =on_tick , 210[ defines . events . script_raised_built ] =on_entity_built , 211[ defines . events . script_raised_destroy ] =on_entity_destroyed , 212[ defines . events . script_raised_revive ] =on_entity_built , 213[ defines . events . on_space_platform_built_entity ] =on_entity_built , 214[ defines . events . on_space_platform_mined_entity ] =on_entity_destroyed , 215[ "ee-linked-open-gui" ] =on_linked_open_gui , 216} 217 218return infinity_wagon