z_mirror/RecipeBook

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

Caleb HeuerRemove usages of big data string library317aee5

trunk
4.3 KiB165 linesraw
1local flib_bounding_box = require("__flib__.bounding-box")
2local flib_format = require("__flib__.format")
3local flib_math = require("__flib__.math")
4
5--- @class Util
6local util = {}
7
8util.crafting_machine = {
9  ["assembling-machine"] = true,
10  ["character"] = true,
11  ["furnace"] = true,
12  ["rocket-silo"] = true,
13}
14
15--- @param player LuaPlayer
16--- @param text LocalisedString
17function util.flying_text(player, text)
18  player.create_local_flying_text({
19    text = text,
20    create_at_cursor = true,
21  })
22  player.play_sound({ path = "utility/cannot_build" })
23end
24
25--- @param num number
26--- @return string
27function util.format_number(num)
28  return flib_format.number(flib_math.round(num, 0.01))
29end
30
31function util.get_natural_entities()
32  return prototypes.get_entity_filtered({
33    --- @diagnostic disable-next-line unused-fields
34    { filter = "type", type = "resource" },
35    --- @diagnostic disable-next-line unused-fields
36    { filter = "type", type = "fish" },
37    --- @diagnostic disable-next-line unused-fields
38    { filter = "type", type = "tree" },
39    --- @diagnostic disable-next-line unused-fields
40    { filter = "type", type = "simple-entity" },
41  })
42end
43
44--- @param prototype GenericPrototype
45--- @return string path
46--- @return string type
47function util.get_path(prototype)
48  local type = util.object_name_to_type[prototype.object_name]
49  return type .. "/" .. prototype.name, type
50end
51
52--- @param obj DatabaseID
53--- @return GenericPrototype
54function util.get_prototype(obj)
55  return prototypes[obj.type][obj.name]
56end
57
58--- @param prototype GenericPrototype
59--- @return DatabaseID
60function util.get_id(prototype)
61  local type = util.object_name_to_type[prototype.object_name]
62  return { type = type, name = prototype.name }
63end
64
65util.object_name_to_type = {
66  LuaEntity = "entity",
67  LuaEntityPrototype = "entity",
68  LuaEquipmentPrototype = "equipment",
69  LuaFluidPrototype = "fluid",
70  LuaItemPrototype = "item",
71  LuaRecipePrototype = "recipe",
72  LuaRecipe = "recipe",
73  LuaTechnologyPrototype = "technology",
74  LuaTechnology = "technology",
75  LuaTilePrototype = "tile",
76}
77
78util.show_hidden_enabled = settings.startup["rb-enable-show-hidden"].value --[[@as boolean]]
79
80--- @param prototype GenericPrototype
81function util.should_include(prototype)
82  return util.show_hidden_enabled or (not prototype.hidden and not prototype.hidden_in_factoriopedia)
83end
84
85--- @return DatabaseID[]
86function util.unique_id_array()
87  local hash = {}
88
89  return setmetatable({}, {
90    --- @param self DatabaseID[]
91    --- @param index integer
92    --- @param value DatabaseID
93    __newindex = function(self, index, value)
94      if not value then
95        return
96      end
97      local key = value.type .. "/" .. value.name
98      if hash[key] then
99        return
100      end
101      hash[key] = true
102      rawset(self, index, value)
103    end,
104  })
105end
106
107--- @param prototype GenericPrototype
108--- @return LuaGroup
109function util.get_group(prototype)
110  if prototype.object_name == "LuaEquipmentPrototype" then
111    return prototypes.item_group["combat"]
112  end
113  return prototype.group
114end
115
116--- @param prototype GenericPrototype
117--- @return LuaGroup
118function util.get_subgroup(prototype)
119  if prototype.object_name == "LuaEquipmentPrototype" then
120    return prototypes.item_subgroup["rb-uncategorized-equipment"]
121  end
122  return prototype.subgroup
123end
124
125--- @param player LuaPlayer
126--- @param entity LuaEntityPrototype
127--- @param recipe LuaRecipePrototype
128function util.create_blueprint(player, entity, recipe)
129  local cursor_stack = player.cursor_stack
130  if not cursor_stack then
131    return
132  end
133
134  if not player.clear_cursor() then
135    return
136  end
137
138  -- Other mods could have done wacky things to the cursor stack
139  if not cursor_stack or not cursor_stack.valid then
140    return
141  end
142
143  local collision_box = entity.collision_box
144  local height = flib_bounding_box.height(collision_box)
145  local width = flib_bounding_box.width(collision_box)
146
147  cursor_stack.set_stack({ name = "blueprint", count = 1 })
148  cursor_stack.set_blueprint_entities({
149    {
150      entity_number = 1,
151      name = entity.name,
152      position = {
153        -- Entities with an even number of tiles to a side need to be set at -0.5 instead of 0
154        math.ceil(width) % 2 == 0 and -0.5 or 0,
155        math.ceil(height) % 2 == 0 and -0.5 or 0,
156      },
157      recipe = recipe.name,
158    },
159  })
160
161  player.add_to_clipboard(cursor_stack)
162  player.activate_paste()
163end
164
165return util