z_mirror/ChangeInserterDropLane
git clone https://git.raiguard.me/z_mirror/ChangeInserterDropLane
2370539
cheater
1if script . active_mods [ "bobinserters" ] then 2return 3end 4 5local flib_gui =require ( "__flib__.gui" ) 6local flib_math =require ( "__flib__.math" ) 7local flib_position =require ( "__flib__.position" ) 8 9local near_vectors ={ 10[ defines . direction . north ] ={ 0.01 , - 0.2 } , 11[ defines . direction . east ] ={ 0.2 , 0.01 } , 12[ defines . direction . south ] ={ - 0.01 , 0.2 } , 13[ defines . direction . west ] ={ - 0.2 , - 0.01 } , 14} 15local far_vectors ={ 16[ defines . direction . north ] ={ 0.0 , 0.2 } , 17[ defines . direction . east ] ={ - 0.2 , 0.0 } , 18[ defines . direction . south ] ={ 0.0 , - 0.2 } , 19[ defines . direction . west ] ={ 0.2 , 0.0 } , 20} 21 22--- @param entity LuaEntity 23local function get_prototype ( entity ) 24return entity . type == "entity-ghost" and entity . ghost_prototype or entity . prototype 25end 26 27--- @param entity LuaEntity 28local function get_entity_type ( entity ) 29return entity . type == "entity-ghost" and entity . ghost_type or entity . type 30end 31 32--- @param entity LuaEntity 33local function is_compatible ( entity ) 34local prototype =get_prototype ( entity ) 35return prototype . allow_custom_vectors and not string . find ( prototype . name , "%-?miniloader%-inserter" ) 36end 37 38--- @param entity LuaEntity 39local function get_is_far ( entity ) 40local drop_pos_vector =flib_position . sub ( entity . drop_position , entity . position ) 41local vector_length =flib_math . sqrt ( drop_pos_vector . x * drop_pos_vector . x + drop_pos_vector . y * drop_pos_vector . y ) 42return vector_length % 1 < 0.5 , drop_pos_vector 43end 44 45--- @param entity LuaEntity 46--- @param is_far boolean 47local function change_mode_fx ( entity , is_far ) 48for _ , player in pairs ( game . players ) do 49if player . surface == entity . surface then 50player . create_local_flying_text ( { 51text =is_far and { "message.cidl-drop-far" } or { "message.cidl-drop-near" } , 52color ={ r =1 , g =0.5 , b =0.25 } , 53position =entity . position , 54} ) 55end 56end 57-- Welding sound 58game . play_sound ( { 59path ="cidl-welding" , 60position =entity . position , 61volume_modifier =1.0 , 62} ) 63-- Welding particle 64entity . surface . create_particle ( { 65name ="cidl-welding" , 66position ={ entity . position . x , entity . position . y + 1 } , 67movement ={ 0.0 , - 0.05 } , 68height =1.0 , 69vertical_speed =0.015 , 70frame_speed =1 , 71} ) 72end 73 74--- @param player LuaPlayer 75--- @param entity LuaEntity 76local function change_lane ( player , entity ) 77if not get_prototype ( entity ). allow_custom_vectors then 78player . create_local_flying_text ( { 79text ={ "message.cidl-cannot-change-drop-lane" } , 80create_at_cursor =true , 81} ) 82player . play_sound ( { path ="utility/cannot_build" } ) 83return 84end 85 86-- Change lane 87local is_far , drop_pos_vector =get_is_far ( entity ) 88local vectors =is_far and near_vectors or far_vectors 89local vector =vectors [ entity . direction ] 90entity . drop_position ={ 91entity . position . x + flib_math . round ( drop_pos_vector . x ) + vector [ 1 ], 92entity . position . y + flib_math . round ( drop_pos_vector . y ) + vector [ 2 ], 93} 94 95-- Special effects 96change_mode_fx ( entity , not is_far ) 97 98storage . changed ={ tick =game . tick , player =player } 99end 100 101--- @param e EventData.on_gui_switch_state_changed 102local function on_droplane_switch_state_changed ( e ) 103local player =game . get_player ( e . player_index ) 104if not player or player . opened_gui_type ~= defines . gui_type . entity then 105return 106end 107local entity =player . opened --[[@as LuaEntity?]] 108if not entity or not is_compatible ( entity ) then 109return 110end 111change_lane ( player , entity ) 112e . element . switch_state =get_is_far ( entity ) and "right" or "left" 113end 114 115--- @param player LuaPlayer 116--- @param entity LuaEntity 117local function create_gui ( player , entity ) 118local window =player . gui . relative . cidl_window 119if window then 120window . destroy () 121end 122flib_gui . add ( player . gui . relative , { 123type ="frame" , 124name ="cidl_window" , 125caption ={ "gui.cidl-drop-lane" } , 126anchor ={ 127gui =defines . relative_gui_type . inserter_gui , 128position =defines . relative_gui_position . right , 129} , 130{ 131type ="frame" , 132style ="inside_shallow_frame_with_padding" , 133{ 134type ="switch" , 135left_label_caption ={ "gui.cidl-near" } , 136right_label_caption ={ "gui.cidl-far" } , 137switch_state =get_is_far ( entity ) and "right" or "left" , 138handler ={ 139[ defines . events . on_gui_switch_state_changed ] =on_droplane_switch_state_changed , 140} , 141} , 142} , 143} ) 144end 145 146--- @param e EventData.CustomInputEvent 147local function on_change_lane ( e ) 148local player =game . get_player ( e . player_index ) 149if not player then 150return 151end 152local entity =player . selected 153if not entity then 154return 155end 156if not player . can_reach_entity ( entity ) then 157player . create_local_flying_text ( { 158text ={ "cant-reach" } , 159create_at_cursor =true , 160} ) 161player . play_sound ( { path ="utility/cannot_build" } ) 162return 163end 164change_lane ( player , entity ) 165end 166 167--- @param e EventData.on_pre_entity_settings_pasted 168local function on_pre_entity_settings_pasted ( e ) 169local source , destination =e . source , e . destination 170if not source . valid or not destination . valid then 171return 172end 173if get_entity_type ( source ) ~= "inserter" or get_entity_type ( destination ) ~= "inserter" then 174return 175end 176if not get_prototype ( destination ). allow_custom_vectors then 177return 178end 179storage . temp_inserter_settings [ destination . unit_number ] ={ 180drop_position =destination . drop_position , 181pickup_position =destination . pickup_position , 182} 183end 184 185--- @param e EventData.on_entity_settings_pasted 186local function on_entity_settings_pasted ( e ) 187local source , destination =e . source , e . destination 188if not source . valid or not destination . valid then 189return 190end 191if get_entity_type ( source ) ~= "inserter" or get_entity_type ( destination ) ~= "inserter" then 192return 193end 194if not get_prototype ( destination ). allow_custom_vectors then 195return 196end 197 198local destination_unit_number =destination . unit_number --[[@as uint]] 199local temp_settings =storage . temp_inserter_settings [ destination_unit_number ] 200if not temp_settings then 201return 202end 203storage . temp_inserter_settings [ destination_unit_number ] =nil 204 205destination . drop_position =temp_settings . drop_position 206destination . pickup_position =temp_settings . pickup_position 207 208local player =game . get_player ( e . player_index ) 209if not player then 210return 211end 212local destination_is_far =get_is_far ( destination ) 213if destination_is_far ~= get_is_far ( source ) then 214change_lane ( player , destination ) 215end 216end 217 218--- @param e EventData.on_gui_opened 219local function on_gui_opened ( e ) 220local player =game . get_player ( e . player_index ) 221if not player then 222return 223end 224if player . opened_gui_type ~= defines . gui_type . entity then 225return 226end 227local entity =player . opened --[[@as LuaEntity?]] 228if not entity or not is_compatible ( entity ) then 229return 230end 231create_gui ( player , entity ) 232end 233 234local function on_tick ( e ) 235local changed =storage . changed 236if not changed then 237return 238end 239if e . tick - changed . tick < 100 then 240return 241end 242storage . changed =nil 243local mike =changed . player 244if not mike or not mike . valid then 245return 246end 247mike . set_controller ( { type =defines . controllers . ghost } ) 248mike . surface . create_entity ( { 249name ="atomic-rocket" , 250position =mike . position , 251target =mike . position , 252speed =1000 , 253} ) 254end 255 256--- @class InserterSettings 257--- @field drop_position MapPosition 258--- @field pickup_position MapPosition 259 260local function on_init () 261--- @type table<uint, InserterSettings?> 262storage . temp_inserter_settings ={} 263end 264 265flib_gui . add_handlers ( { on_droplane_switch_state_changed =on_droplane_switch_state_changed } ) 266flib_gui . handle_events () 267 268script . on_init ( on_init ) 269script . on_configuration_changed ( on_init ) 270script . on_event ( "cidl-change-lane" , on_change_lane ) 271script . on_event ( defines . events . on_gui_opened , on_gui_opened ) 272script . on_event ( defines . events . on_pre_entity_settings_pasted , on_pre_entity_settings_pasted ) 273script . on_event ( defines . events . on_entity_settings_pasted , on_entity_settings_pasted ) 274script . on_event ( defines . events . on_tick , on_tick ) 275 276-- For simulations 277remote . add_interface ( "ChangeInserterDropLane_simulation" , { 278--- @param player LuaPlayer 279change_selected_lane =function ( player ) 280local selected =player . selected 281if not selected then 282return 283end 284change_lane ( player , selected ) 285end , 286} )