z_mirror/Krastorio2
git clone https://git.raiguard.me/z_mirror/Krastorio2
dd4cdd3e
discrete-steel-pipes
1local flib_bounding_box =require ( "__flib__.bounding-box" ) 2local flib_position =require ( "__flib__.position" ) 3 4local util =require ( "scripts.util" ) 5 6local max_reach =15 7 8--- @param e EventData.on_player_selected_area 9local function on_player_selected_area ( e ) 10if e . item ~= "kr-jackhammer" then 11return 12end 13 14local player =game . get_player ( e . player_index ) 15if not player then 16return 17end 18 19local player_pos =player . position 20 21local items_to_give ={} 22local tiles_to_set ={} 23local i =0 24for _ , tile in pairs ( e . tiles ) do 25if flib_position . distance ( tile . position , player_pos ) > max_reach then 26goto continue 27end 28 29local mineable =tile . prototype . mineable_properties 30local products =mineable . products 31if not mineable or not products or not mineable . minable then 32goto continue 33end 34 35i =i + 1 36tiles_to_set [ i ] ={ name =tile . hidden_tile or "landfill" , position =tile . position } 37for _ , product_ident in pairs ( products ) do 38local probability =product_ident . probability 39if product_ident . type == "item" and not probability or math . random () <= probability then 40local amount =product_ident . amount 41if not amount then 42amount =math . random ( product_ident . amount_min , product_ident . amount_max ) 43end 44local name =product_ident . name 45items_to_give [ name ] =( items_to_give [ name ] or 0 ) + amount 46end 47end 48 49:: continue :: 50end 51 52if i > 0 then 53-- Check if we can insert all of the items 54local inventory =player . get_main_inventory () 55if not inventory then 56return 57end 58for name , count in pairs ( items_to_give ) do 59-- If we can't fit all of the items, print a message and don't take any tiles 60if not inventory . can_insert ( { name =name , count =count } ) then 61util . flying_text_with_sound ( 62player , 63{ "message.kr-inventory-is-full" } , 64{ position =flib_bounding_box . center ( e . area ) } 65) 66return 67end 68end 69 70-- Insert all of the items 71local inserted_count =0 72for name , count in pairs ( items_to_give ) do 73inventory . insert ( { name =name , count =count } ) 74inserted_count =inserted_count + count 75end 76 77-- Set the tiles 78e . surface . set_tiles ( tiles_to_set ) 79 80-- FX 81player . play_sound ( { path ="kr-jackhammer" , volume_modifier =1 } ) 82else 83util . flying_text_with_sound ( 84player , 85{ "message.kr-no-tiles-in-selection" } , 86{ position =flib_bounding_box . center ( e . area ) } 87) 88end 89end 90 91local jackhammer ={} 92 93jackhammer . events ={ 94[ defines . events . on_player_selected_area ] =on_player_selected_area , 95[ defines . events . on_player_alt_selected_area ] =on_player_selected_area , 96} 97 98return jackhammer