z_mirror/BlueprintTools

git clone https://git.raiguard.me/z_mirror/BlueprintTools

raiguardInclude tiles in quick-grid7da9f64

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