z_mirror/BlueprintTools
git clone https://git.raiguard.me/z_mirror/BlueprintTools
6df20db
clipboards
1local area =require ( "__flib__.area" ) 2 3local util =require ( "scripts.util" ) 4 5-- TODO: fix edge case with curved rails 6return function ( player , tile_name , fill_gaps , margin ) 7local blueprint =util . get_blueprint ( player . cursor_stack ) 8if not blueprint then return end 9 10local entities =blueprint . get_blueprint_entities () 11local entity_prototypes =game . entity_prototypes 12 13local tiles ={} 14local tile_index =0 15 16if fill_gaps then 17local TileArea =area . load ( area . from_position ( entities [ 1 ]. position )) 18 19-- iterate entities to calculate needed grid size 20for _ , entity in pairs ( entities ) do 21local prototype =entity_prototypes [ entity . name ] 22if prototype then 23TileArea : expand_to_contain_area ( area . center_on ( prototype . collision_box , entity . position )) 24end 25end 26 27-- ceil to outside edges and add a margin 28TileArea : ceil (): expand ( margin ) 29 30-- for the purpose of this function, we don't care about any pre-existing tiles - we will replace all of them 31for position in TileArea : iterate () do 32tile_index =tile_index + 1 33tiles [ tile_index ] ={ 34position =position , 35name =tile_name 36} 37end 38else 39local mapping ={} 40 41for _ , entity in pairs ( entities ) do 42local prototype =entity_prototypes [ entity . name ] 43if prototype then 44local EntityArea =area . load ( area . center_on ( prototype . collision_box , entity . position )): ceil (): expand ( margin ) 45 46for position in EntityArea : iterate () do 47local add =false 48 49if mapping [ position . x ] then 50if not mapping [ position . x ][ position . y ] then 51add =true 52mapping [ position . x ][ position . y ] =true 53end 54else 55add =true 56mapping [ position . x ] ={ [ position . y ] =true } 57end 58 59if add then 60tile_index =tile_index + 1 61tiles [ tile_index ] ={ 62position =position , 63name =tile_name 64} 65end 66end 67end 68end 69end 70 71blueprint . set_blueprint_tiles ( tiles ) 72end