z_mirror/BlueprintTools

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

Caleb HeuerRemove blueprint nudging as it is now built into vanillad6a0c1c

trunk
3.3 KiB106 linesraw
1local constants = require("constants")
2
3local util = {}
4
5--- Get the current blueprint from the item stack, if it exists and is set up.
6--- @param item_stack LuaItemStack
7--- @return LuaItemStack?
8function util.get_blueprint(item_stack)
9  if not item_stack or not item_stack.valid_for_read then
10    return
11  end
12  local blueprint
13  if item_stack.is_blueprint then
14    blueprint = item_stack
15  elseif item_stack.is_blueprint_book and item_stack.active_index then
16    local inventory = item_stack.get_inventory(defines.inventory.item_main) --[[@as LuaInventory]]
17    if inventory.is_empty() or item_stack.active_index > #inventory then
18      return
19    end
20    blueprint = util.get_blueprint(inventory[item_stack.active_index])
21  end
22
23  if blueprint and blueprint.is_blueprint_setup() then
24    return blueprint
25  end
26end
27
28--- @param entity BlueprintEntity
29function util.get_rail_tiles(entity)
30  local category = constants.rail_tiles[prototypes.entity[entity.name].type]
31  if not category then
32    return
33  end
34
35  return category[entity.direction or 0]
36end
37
38--- Displays notification for player at cursor position.
39--- @param player LuaPlayer Player to notify.
40--- @param message LocalisedString Message to show to player.
41--- @param sound SoundPath|nil Optional sound to play for the player.
42function util.cursor_notification(player, message, sound)
43  player.create_local_flying_text({
44    text = message,
45    create_at_cursor = true,
46  })
47
48  if sound then
49    player.play_sound({ path = sound })
50  end
51end
52
53--- Calculates largest build grid size for all entities in the passed-in list of blueprints.
54--- Useful when dealing with things like rails and train stops that can only be repositioned in increments of two.
55--- @param blueprints LuaItemStack[] List of blueprints for which to calculate the value. Must be valid for read.
56function util.get_blueprint_largest_build_grid_size(blueprints)
57  local build_grid_size = 1
58
59  for _, blueprint in pairs(blueprints) do
60    for _, entity in pairs(blueprint.get_blueprint_entities() or {}) do
61      if prototypes.entity[entity.name].building_grid_bit_shift > build_grid_size then
62        build_grid_size = prototypes.entity[entity.name].building_grid_bit_shift
63      end
64    end
65  end
66
67  return build_grid_size
68end
69
70--- Returns list of all blueprints in a blueprint book.
71--- @param book LuaItemStack
72--- @return LuaItemStack[]
73function util.get_book_blueprints(book)
74  --- Helper function that recursively collects blueprints from the passed-in blueprint book and stores them in the passed-in result table.
75  --- @param book LuaItemStack
76  --- @param result_table LuaItemStack[]
77  local function collect_blueprints(book, result_table)
78    local items = book.get_inventory(defines.inventory.item_main) --[[@as LuaInventory]]
79    for i = 1, #items do
80      local item = items[i]
81      if not item then
82        return
83      elseif item.is_blueprint_book then
84        collect_blueprints(item, result_table)
85      elseif item.is_blueprint then
86        table.insert(result_table, item)
87      end
88    end
89  end
90
91  local blueprints = {}
92  collect_blueprints(book, blueprints)
93  return blueprints
94end
95
96--- @param box BoundingBox
97--- @param callback fun(pos: MapPosition)
98function util.for_each_position(box, callback)
99  for y = box.left_top.y, box.right_bottom.y - 1 do
100    for x = box.left_top.x, box.right_bottom.x - 1 do
101      callback({ x = x, y = y })
102    end
103  end
104end
105
106return util