You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This may or may not be useful to this project, but I thought I'd donate it here in case you thought it may be. Attempting to work with blueprints (specifically of the ctrl-c variety being stamped on existing entities) is tough. We don't get many events with a lot of info. Just on_pre_build. So you pretty much have to do your own BP position calculations. These are a couple of functions that I put together to get a bounding box of all the BP entities, and how to find the center of where the BP will be placed. If helpful, feel free to use them freely in flib.
---@paramentitiesBlueprintEntity[] # E.g. from LuaPlayer.get_blueprint_entities()---@returnBoundingBox # Box that contains all blueprint entities---@returnuint # The building grid size needed to build this blueprintfunctionM.get_blueprint_bounding_box(entities)
localbox=flib_box.from_position(entities[1].position, true)
localnames= {}
for_, einipairs(entities) donames[e.name] =trueendlocalname_filter= {}
fork, _inpairs(names) dotable.insert(name_filter, k)
end-- Define a bounding box the size of the blueprint to be placedlocalgrid_size=1---@diagnosticdisable-next-line:missing-fieldslocalprotos=game.get_filtered_entity_prototypes{{filter="name", name=name_filter}}
for_, entityinpairs(entities) dolocalcollision_box=protos[entity.name].collision_boxgrid_size=math.max(grid_size, protos[entity.name].building_grid_bit_shift)
box=flib_box.expand_to_contain_box(
box,
flib_box.from_dimensions(
entity.position,
flib_box.width(collision_box),
flib_box.height(collision_box)
)
)
end-- Expand bounding box to be full tiles based on the entity with the largest building grid sizebox.left_top.x=grid_size*math.floor(box.left_top.x/grid_size)
box.left_top.y=grid_size*math.floor(box.left_top.y/grid_size)
box.right_bottom.x=grid_size*math.ceil(box.right_bottom.x/grid_size)
box.right_bottom.y=grid_size*math.ceil(box.right_bottom.y/grid_size)
returnbox, grid_sizeend
---@paramboxBoundingBox # Bounding box of blueprint to place---@paramposMapPosition # Position to center new bounding box around---@paramgrid_sizeuint? # Building grid size to base centering. Default: 1---@returnMapPosition # Center position of where blueprint will be placedfunctionM.get_placed_blueprint_center(box, pos, grid_size)
localgrid_size=grid_sizeor1localpos_x=pos.xorpos[1]
localpos_y=pos.yorpos[2]
pos_x= (flib_box.width(box) /grid_size) %2==0andmath.floor(pos_x/grid_size+.5 ) *grid_sizeormath.floor(pos_x/grid_size) *grid_size+grid_size/2pos_y= (flib_box.height(box) /grid_size) %2==0andmath.floor(pos_y/grid_size+.5 ) *grid_sizeormath.floor(pos_y/grid_size) *grid_size+grid_size/2ifpos.xthenreturn { x=pos_x, y=pos_y }
elsereturn { pos_x, pos_y }
endend
This may or may not be useful to this project, but I thought I'd donate it here in case you thought it may be. Attempting to work with blueprints (specifically of the ctrl-c variety being stamped on existing entities) is tough. We don't get many events with a lot of info. Just on_pre_build. So you pretty much have to do your own BP position calculations. These are a couple of functions that I put together to get a bounding box of all the BP entities, and how to find the center of where the BP will be placed. If helpful, feel free to use them freely in flib.