forked from ac-minetest/basic_machines
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathautocrafter.lua
305 lines (262 loc) · 9.48 KB
/
autocrafter.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
-- Modified and adapted from pipeworks mod by VanessaE
-- https://github.com/mt-mods/pipeworks
-- by rnd
-- Copyright (C) 2022-2025 мтест
-- See README.md for license details
-- Disabled timers and on/off button, now autocrafter is only activated by signal
local S = basic_machines.S
-- caches some recipe data to avoid to call the slow function minetest.get_craft_result() every second
local autocrafterCache = {}
local function autocrafter_update_form(meta)
meta:set_string("formspec", table.concat({
"formspec_version[4]size[10.45,13.35]",
"style_type[list;spacing=0.25,0.15]",
"list[context;recipe;0.35,0.35;3,3]",
"image[4.1,1.5;1,1;[combine:1x1^[noalpha^[colorize:#141318]",
"list[context;output;4.1,1.5;1,1]",
"list[context;dst;5.35,0.35;4,3]",
"list[context;src;0.35,4.25;8,3]",
basic_machines.get_form_player_inventory(0.35, 8.3, 8, 4, 0.25),
"listring[context;dst]",
"listring[current_player;main]",
"listring[context;src]",
"listring[current_player;main]",
"listring[context;recipe]",
"listring[current_player;main]"}))
end
local function count_index(invlist)
local index = {}
for _, stack in pairs(invlist) do
if not stack:is_empty() then
local stack_name = stack:get_name()
index[stack_name] = (index[stack_name] or 0) + stack:get_count()
end
end
return index
end
local function get_craft(pos, inventory, hash)
hash = hash or minetest.hash_node_position(pos)
local craft = autocrafterCache[hash]
if craft then return craft end
local recipe = inventory:get_list("recipe")
local output, decremented_input = minetest.get_craft_result({method = "normal", width = 3, items = recipe})
craft = {recipe = recipe, consumption = count_index(recipe), output = output, decremented_input = decremented_input}
autocrafterCache[hash] = craft
return craft
end
local function after_recipe_change(pos, inventory)
-- if we emptied the grid, there's no point in keeping it running or cached
if inventory:is_empty("recipe") then
autocrafterCache[minetest.hash_node_position(pos)] = nil
minetest.get_meta(pos):set_string("infotext", S("Unconfigured autocrafter"))
inventory:set_stack("output", 1, ItemStack(""))
else
local hash = minetest.hash_node_position(pos)
autocrafterCache[hash] = nil
local output_item = get_craft(pos, inventory, hash).output.item
local name = output_item:get_name()
local description = basic_machines.get_item_description(name ~= "" and name or nil)
minetest.get_meta(pos):set_string("infotext", S("Autocrafter: '@1' (@2)", description, name))
inventory:set_stack("output", 1, output_item)
end
end
-- clean out unknown items and groups
local function normalize(items_list)
for i, item in pairs(items_list) do
if not minetest.registered_items[item] then -- ItemStack(item):get_name(), for items with count > 1
items_list[i] = ItemStack("")
end
end
return items_list
end
local function on_output_change(pos, inventory, stack, index)
if stack then
local recipe
if index then
recipe = minetest.get_all_craft_recipes(stack:get_name())
recipe = (recipe or {})[index] or {}
else
recipe = minetest.get_craft_recipe(stack:get_name())
end
if not recipe.items or recipe.type ~= "normal" then return end
local items, width = normalize(recipe.items), recipe.width
if width == 0 then -- shapeless recipe
inventory:set_list("recipe", items)
else
local item_idx, width_idx = 1, 1
for i = 1, 9 do
if width_idx <= width then
inventory:set_stack("recipe", i, items[item_idx])
item_idx = item_idx + 1
else
inventory:set_stack("recipe", i, ItemStack(""))
end
width_idx = (width_idx < 3) and (width_idx + 1) or 1
end
end
else
inventory:set_list("recipe", {})
end
after_recipe_change(pos, inventory)
end
basic_machines.change_autocrafter_recipe = on_output_change
local function autocraft(inventory, craft)
if not craft then return end
local consumption = craft.consumption
-- check if we have enough material available
local inv_items = count_index(inventory:get_list("src"))
for itemname, number in pairs(consumption) do
local itemcount = inv_items[itemname]
if not itemcount or itemcount < number then return end
end
local output = craft.output.item
local decremented_input_items = craft.decremented_input.items -- replacements
-- check if output and all replacements fit in dst
local empty_count = 0
local out_items = count_index(decremented_input_items)
local output_name = output:get_name()
out_items[output_name] = (out_items[output_name] or 0) + output:get_count()
for _, item in pairs(inventory:get_list("dst")) do
if item:is_empty() then
empty_count = empty_count + 1
else
local name = item:get_name()
if out_items[name] then
out_items[name] = out_items[name] - item:get_free_space()
end
end
end
for _, count in pairs(out_items) do
if count > 0 then
empty_count = empty_count - 1
end
end
if empty_count < 0 then return end
-- consume material
for itemname, number in pairs(consumption) do
for _ = 1, number do -- we have to do that since remove_item does not work if count > stack_max
inventory:remove_item("src", ItemStack(itemname))
end
end
-- craft the result into the dst inventory and add any "replacements" as well
inventory:add_item("dst", output)
for i = 1, 9 do
inventory:add_item("dst", decremented_input_items[i])
end
end
local machine_name = "basic_machines:autocrafter"
minetest.register_node(machine_name, {
description = S("Autocrafter"),
groups = {cracky = 3},
drawtype = "normal",
tiles = {"basic_machines_autocrafter.png"},
is_ground_content = false,
sounds = basic_machines.sound_node_machine(),
on_destruct = function(pos)
autocrafterCache[minetest.hash_node_position(pos)] = nil
end,
after_place_node = function(pos, placer)
if not placer then return end
local meta = minetest.get_meta(pos)
meta:set_string("infotext", S("Unconfigured autocrafter: Place items for recipe top left." ..
" To operate place required items in bottom space (src inventory) and activate with signal." ..
" Obtain crafted item from top right (dst inventory)."))
meta:set_string("owner", placer:get_player_name())
local inv = meta:get_inventory()
inv:set_size("src", 3 * 8)
inv:set_size("recipe", 3 * 3)
inv:set_size("dst", 4 * 3)
inv:set_size("output", 1)
autocrafter_update_form(meta)
end,
can_dig = function(pos, player)
return basic_machines.can_dig(pos, player, {"src", "dst"})
end,
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
if minetest.is_protected(pos, player:get_player_name()) then return 0 end
if to_list == "recipe" or from_list == "recipe" then
local inv = minetest.get_meta(pos):get_inventory()
if to_list == "recipe" then
local stack = inv:get_stack(from_list, from_index)
stack:set_count(1)
inv:set_stack(to_list, to_index, stack)
elseif from_list == "recipe" then
inv:set_stack(from_list, from_index, ItemStack(""))
end
after_recipe_change(pos, inv)
return 0
elseif to_list == "output" or from_list == "output" then
local inv = minetest.get_meta(pos):get_inventory()
if to_list == "output" then
local stack = inv:get_stack(from_list, from_index)
on_output_change(pos, inv, stack)
return 0
elseif from_list == "output" then
on_output_change(pos, inv, nil)
if to_list ~= "recipe" then return 0 end
end
end
return count
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then return 0 end
if listname == "recipe" or listname == "output" then
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if listname == "recipe" then
stack:set_count(1)
inv:set_stack(listname, index, stack)
after_recipe_change(pos, inv)
autocrafter_update_form(meta)
elseif listname == "output" then
on_output_change(pos, inv, stack)
autocrafter_update_form(meta)
end
return 0
end
return stack:get_count()
end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then return 0 end
if listname == "recipe" or listname == "output" then
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if listname == "recipe" then
inv:set_stack(listname, index, ItemStack(""))
after_recipe_change(pos, inv)
autocrafter_update_form(meta)
elseif listname == "output" then
on_output_change(pos, inv, nil)
autocrafter_update_form(meta)
end
return 0
end
return stack:get_count()
end,
on_blast = function(pos, intensity)
return basic_machines.on_blast(pos, intensity, machine_name, {"src", "dst"})
end,
effector = { -- rnd: run machine when activated by signal
action_on = function(pos, _)
local meta = minetest.get_meta(pos)
local inventory = meta:get_inventory()
local craft = get_craft(pos, inventory, nil)
local output_item = craft.output.item
-- only use crafts that have an actual result
if output_item:is_empty() then
meta:set_string("infotext", S("Unconfigured autocrafter: Unknown recipe")); return
end
autocraft(inventory, craft)
end
}
})
if basic_machines.settings.register_crafts and basic_machines.use_default then
minetest.register_craft({
output = "basic_machines:autocrafter",
recipe = {
{"default:steel_ingot", "default:mese_crystal", "default:steel_ingot"},
{"default:diamondblock", "default:steel_ingot", "default:diamondblock"},
{"default:steel_ingot", "default:mese_crystal", "default:steel_ingot"}
}
})
end