z_mirror/BlueprintTools

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

raiguardUse flib area module6df20db

clipboards
2.0 KiB72 linesraw
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)
7  local blueprint = util.get_blueprint(player.cursor_stack)
8  if not blueprint then return end
9
10  local entities = blueprint.get_blueprint_entities()
11  local entity_prototypes = game.entity_prototypes
12
13  local tiles = {}
14  local tile_index = 0
15
16  if fill_gaps then
17    local TileArea = area.load(area.from_position(entities[1].position))
18
19    -- iterate entities to calculate needed grid size
20    for _, entity in pairs(entities) do
21      local prototype = entity_prototypes[entity.name]
22      if prototype then
23        TileArea:expand_to_contain_area(area.center_on(prototype.collision_box, entity.position))
24      end
25    end
26
27    -- ceil to outside edges and add a margin
28    TileArea: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
31    for position in TileArea:iterate() do
32      tile_index = tile_index + 1
33      tiles[tile_index] = {
34        position = position,
35        name = tile_name
36      }
37    end
38  else
39    local mapping = {}
40
41    for _, entity in pairs(entities) do
42      local prototype = entity_prototypes[entity.name]
43      if prototype then
44        local EntityArea = area.load(area.center_on(prototype.collision_box, entity.position)):ceil():expand(margin)
45
46        for position in EntityArea:iterate() do
47          local add = false
48
49          if mapping[position.x] then
50            if not mapping[position.x][position.y] then
51              add = true
52              mapping[position.x][position.y] = true
53            end
54          else
55            add = true
56            mapping[position.x] = {[position.y] = true}
57          end
58
59          if add then
60            tile_index = tile_index + 1
61            tiles[tile_index] = {
62              position = position,
63              name = tile_name
64            }
65          end
66        end
67      end
68    end
69  end
70
71  blueprint.set_blueprint_tiles(tiles)
72end