z_mirror/BlueprintTools
git clone https://git.raiguard.me/z_mirror/BlueprintTools
7da9f64
clipboards
1local area =require ( "__flib__.area" ) 2 3local util =require ( "scripts.util" ) 4 5local function quick_grid ( player ) 6local blueprint =util . get_blueprint ( player . cursor_stack ) 7if not blueprint then return end 8 9local entities =blueprint . get_blueprint_entities () 10local tiles =blueprint . get_blueprint_tiles () 11 12local GridArea =area . load ( area . from_position ( entities [ 1 ]. position )) 13local entity_prototypes =game . entity_prototypes 14 15-- iterate entities and tiles to calculate needed grid size 16for _ , entity in pairs ( entities ) do 17local prototype =entity_prototypes [ entity . name ] 18if prototype then 19GridArea : expand_to_contain_area ( area . center_on ( prototype . collision_box , entity . position )) 20end 21end 22for _ , tile in pairs ( tiles ) do 23-- add 0.5 to tile position to avoid off-by-one error on the right and bottom edges 24GridArea : expand_to_contain_position ( { x =tile . position . x + 0.5 , y =tile . position . y + 0.5 } ) 25end 26 27-- ceil to outside edges 28GridArea : ceil () 29 30-- offset is simply how far away from 0,0 the top-left of the area is 31local offset ={ x =GridArea . left_top . x , y =GridArea . left_top . y } 32 33-- move all entities and tiles by the offset 34for _ , entity in pairs ( entities ) do 35entity . position . x =entity . position . x - offset . x 36entity . position . y =entity . position . y - offset . y 37end 38for _ , tile in pairs ( tiles ) do 39tile . position . x =tile . position . x - offset . x 40tile . position . y =tile . position . y - offset . y 41end 42 43-- set grid dimensions and snapping mode 44blueprint . blueprint_snap_to_grid ={ x =GridArea : width (), y =GridArea : height () } 45blueprint . blueprint_absolute_snapping =false 46 47-- set updated entities 48blueprint . set_blueprint_entities ( entities ) 49blueprint . set_blueprint_tiles ( tiles ) 50end 51 52return quick_grid