z_mirror/BlueprintTools
git clone https://git.raiguard.me/z_mirror/BlueprintTools
00b37f1
trunk
1local flib_bounding_box =require ( "__flib__.bounding-box" ) 2local table =require ( "__flib__.table" ) 3 4local constants =require ( "constants" ) 5local util =require ( "scripts.util" ) 6 7return function ( player , tile_name , fill_gaps , margin ) 8local blueprint =util . get_blueprint ( player . cursor_stack ) 9if not blueprint then 10return 11end 12 13local entities =blueprint . get_blueprint_entities () or {} 14local tiles =blueprint . get_blueprint_tiles () or {} 15local entity_prototypes =prototypes . entity 16 17local output_tiles ={} 18local tile_index =0 19 20local first_obj =entities [ 1 ] or tiles [ 1 ] 21 22if fill_gaps then 23local box =flib_bounding_box . from_position ( first_obj . position ) 24 25-- iterate entities to calculate needed grid size 26for _ , entity in pairs ( entities ) do 27local prototype =entity_prototypes [ entity . name ] 28if prototype then 29local entity_box =( 30prototype . type == "curved-rail" 31and table . deep_copy ( constants . curved_rail_grid_sizes [ math . floor (( entity . direction or 0 ) / 2 ) % 2 + 1 ]) 32or prototype . collision_box 33) 34box =flib_bounding_box . expand_to_contain_box ( box , flib_bounding_box . move ( entity_box , entity . position )) 35end 36end 37-- and tiles 38for _ , tile in pairs ( tiles ) do 39box =flib_bounding_box . expand_to_contain_box ( box , { 40left_top ={ x =tile . position . x , y =tile . position . y } , 41right_bottom ={ x =tile . position . x + 1 , y =tile . position . y + 1 } , 42} ) 43end 44 45-- ceil to outside edges and add a margin 46box =flib_bounding_box . resize ( flib_bounding_box . ceil ( box ), margin ) 47 48-- for the purpose of this function, we don't care about any pre-existing tiles - we will replace all of them 49util . for_each_position ( box , function ( position ) 50tile_index =tile_index + 1 51output_tiles [ tile_index ] ={ 52position =position , 53name =tile_name , 54} 55end ) 56else 57local mapping ={} 58local function add_tile ( position ) 59local add =false 60 61if mapping [ position . x ] then 62if not mapping [ position . x ][ position . y ] then 63add =true 64mapping [ position . x ][ position . y ] =true 65end 66else 67add =true 68mapping [ position . x ] ={ [ position . y ] =true } 69end 70 71if add then 72tile_index =tile_index + 1 73output_tiles [ tile_index ] ={ 74position =position , 75name =tile_name , 76} 77end 78end 79 80for _ , entity in pairs ( entities ) do 81local rail_tiles =util . get_rail_tiles ( entity ) 82if rail_tiles then 83local entity_pos =entity . position 84for _ , src_position in pairs ( rail_tiles ) do 85util . for_each_position ( 86flib_bounding_box . resize ( flib_bounding_box . from_position ( src_position , true ), margin ), 87function ( position ) 88add_tile ( { x =position . x + entity_pos . x , y =position . y + entity_pos . y } ) 89end 90) 91end 92else 93local prototype =entity_prototypes [ entity . name ] 94if prototype then 95util . for_each_position ( 96flib_bounding_box . resize ( 97flib_bounding_box . ceil ( flib_bounding_box . move ( prototype . collision_box , entity . position )), 98margin 99), 100function ( position ) 101add_tile ( position ) 102end 103) 104end 105end 106end 107for _ , tile in pairs ( tiles ) do 108add_tile ( tile . position ) 109end 110end 111 112blueprint . set_blueprint_tiles ( output_tiles ) 113end