You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

436 lines
14KB

  1. local S = minetest.get_translator("pipeworks")
  2. local autocrafterCache = {} -- caches some recipe data to avoid to call the slow function minetest.get_craft_result() every second
  3. local craft_time = 1
  4. local function count_index(invlist)
  5. local index = {}
  6. for _, stack in pairs(invlist) do
  7. if not stack:is_empty() then
  8. local stack_name = stack:get_name()
  9. index[stack_name] = (index[stack_name] or 0) + stack:get_count()
  10. end
  11. end
  12. return index
  13. end
  14. local function get_item_info(stack)
  15. local name = stack:get_name()
  16. local def = minetest.registered_items[name]
  17. local description = def and def.description or S("Unknown item")
  18. return description, name
  19. end
  20. local function get_craft(pos, inventory, hash)
  21. local hash = hash or minetest.hash_node_position(pos)
  22. local craft = autocrafterCache[hash]
  23. if not craft then
  24. local recipe = inventory:get_list("recipe")
  25. local output, decremented_input = minetest.get_craft_result({method = "normal", width = 3, items = recipe})
  26. craft = {recipe = recipe, consumption=count_index(recipe), output = output, decremented_input = decremented_input}
  27. autocrafterCache[hash] = craft
  28. end
  29. return craft
  30. end
  31. local function autocraft(inventory, craft)
  32. if not craft then return false end
  33. local output_item = craft.output.item
  34. -- check if we have enough room in dst
  35. if not inventory:room_for_item("dst", output_item) then return false end
  36. local consumption = craft.consumption
  37. local inv_index = count_index(inventory:get_list("src"))
  38. -- check if we have enough material available
  39. for itemname, number in pairs(consumption) do
  40. if (not inv_index[itemname]) or inv_index[itemname] < number then return false end
  41. end
  42. -- consume material
  43. for itemname, number in pairs(consumption) do
  44. for i = 1, number do -- We have to do that since remove_item does not work if count > stack_max
  45. inventory:remove_item("src", ItemStack(itemname))
  46. end
  47. end
  48. -- craft the result into the dst inventory and add any "replacements" as well
  49. inventory:add_item("dst", output_item)
  50. for i = 1, 9 do
  51. inventory:add_item("dst", craft.decremented_input.items[i])
  52. end
  53. return true
  54. end
  55. -- returns false to stop the timer, true to continue running
  56. -- is started only from start_autocrafter(pos) after sanity checks and cached recipe
  57. local function run_autocrafter(pos, elapsed)
  58. local meta = minetest.get_meta(pos)
  59. local inventory = meta:get_inventory()
  60. local craft = get_craft(pos, inventory)
  61. local output_item = craft.output.item
  62. -- only use crafts that have an actual result
  63. if output_item:is_empty() then
  64. meta:set_string("infotext", S("unconfigured Autocrafter: unknown recipe"))
  65. return false
  66. end
  67. for step = 1, math.floor(elapsed/craft_time) do
  68. local continue = autocraft(inventory, craft)
  69. if not continue then return false end
  70. end
  71. return true
  72. end
  73. local function start_crafter(pos)
  74. local meta = minetest.get_meta(pos)
  75. if meta:get_int("enabled") == 1 then
  76. local timer = minetest.get_node_timer(pos)
  77. if not timer:is_started() then
  78. timer:start(craft_time)
  79. end
  80. end
  81. end
  82. local function after_inventory_change(pos)
  83. start_crafter(pos)
  84. end
  85. -- note, that this function assumes allready being updated to virtual items
  86. -- and doesn't handle recipes with stacksizes > 1
  87. local function after_recipe_change(pos, inventory)
  88. local meta = minetest.get_meta(pos)
  89. -- if we emptied the grid, there's no point in keeping it running or cached
  90. if inventory:is_empty("recipe") then
  91. minetest.get_node_timer(pos):stop()
  92. autocrafterCache[minetest.hash_node_position(pos)] = nil
  93. meta:set_string("infotext", S("unconfigured Autocrafter"))
  94. inventory:set_stack("output", 1, "")
  95. return
  96. end
  97. local recipe_changed = false
  98. local recipe = inventory:get_list("recipe")
  99. local hash = minetest.hash_node_position(pos)
  100. local craft = autocrafterCache[hash]
  101. if craft then
  102. -- check if it changed
  103. local cached_recipe = craft.recipe
  104. for i = 1, 9 do
  105. if recipe[i]:get_name() ~= cached_recipe[i]:get_name() then
  106. autocrafterCache[hash] = nil -- invalidate recipe
  107. craft = nil
  108. break
  109. end
  110. end
  111. end
  112. craft = craft or get_craft(pos, inventory, hash)
  113. local output_item = craft.output.item
  114. local description, name = get_item_info(output_item)
  115. meta:set_string("infotext", S("'@1' Autocrafter (@2)", description, name))
  116. inventory:set_stack("output", 1, output_item)
  117. after_inventory_change(pos)
  118. end
  119. -- clean out unknown items and groups, which would be handled like unknown items in the crafting grid
  120. -- if minetest supports query by group one day, this might replace them
  121. -- with a canonical version instead
  122. local function normalize(item_list)
  123. for i = 1, #item_list do
  124. local name = item_list[i]
  125. if not minetest.registered_items[name] then
  126. item_list[i] = ""
  127. end
  128. end
  129. return item_list
  130. end
  131. local function on_output_change(pos, inventory, stack)
  132. if not stack then
  133. inventory:set_list("output", {})
  134. inventory:set_list("recipe", {})
  135. else
  136. local input = minetest.get_craft_recipe(stack:get_name())
  137. if not input.items or input.type ~= "normal" then return end
  138. local items, width = normalize(input.items), input.width
  139. local item_idx, width_idx = 1, 1
  140. for i = 1, 9 do
  141. if width_idx <= width then
  142. inventory:set_stack("recipe", i, items[item_idx])
  143. item_idx = item_idx + 1
  144. else
  145. inventory:set_stack("recipe", i, ItemStack(""))
  146. end
  147. width_idx = (width_idx < 3) and (width_idx + 1) or 1
  148. end
  149. -- we'll set the output slot in after_recipe_change to the actual result of the new recipe
  150. end
  151. after_recipe_change(pos, inventory)
  152. end
  153. -- returns false if we shouldn't bother attempting to start the timer again after this
  154. local function update_meta(meta, enabled)
  155. local state = enabled and "on" or "off"
  156. meta:set_int("enabled", enabled and 1 or 0)
  157. local fs = "size[8,12]"..
  158. "list[context;recipe;0,0;3,3;]"..
  159. "image[3,1;1,1;gui_hb_bg.png^[colorize:#141318:255]"..
  160. "list[context;output;3,1;1,1;]"..
  161. "image_button[3,2;1,0.6;pipeworks_button_" .. state .. ".png;" .. state .. ";;;false;pipeworks_button_interm.png]" ..
  162. "list[context;src;0,4.5;8,3;]"..
  163. "list[context;dst;4,0;4,3;]"..
  164. default.gui_bg..
  165. default.gui_bg_img..
  166. default.gui_slots..
  167. default.get_hotbar_bg(0,8) ..
  168. "list[current_player;main;0,8;8,4;]" ..
  169. "listring[current_player;main]"..
  170. "listring[context;src]" ..
  171. "listring[current_player;main]"..
  172. "listring[context;dst]" ..
  173. "listring[current_player;main]"
  174. if minetest.get_modpath("digilines") then
  175. fs = fs.."field[1,3.5;4,1;channel;"..S("Channel")..";${channel}]"
  176. fs = fs.."button_exit[5,3.2;2,1;save;"..S("Save").."]"
  177. end
  178. meta:set_string("formspec",fs)
  179. -- toggling the button doesn't quite call for running a recipe change check
  180. -- so instead we run a minimal version for infotext setting only
  181. -- this might be more written code, but actually executes less
  182. local output = meta:get_inventory():get_stack("output", 1)
  183. if output:is_empty() then -- doesn't matter if paused or not
  184. meta:set_string("infotext", S("unconfigured Autocrafter"))
  185. return false
  186. end
  187. local description, name = get_item_info(output)
  188. local infotext = enabled and S("'@1' Autocrafter (@2)", description, name)
  189. or S("paused '@1' Autocrafter", description)
  190. meta:set_string("infotext", infotext)
  191. return enabled
  192. end
  193. -- 1st version of the autocrafter had actual items in the crafting grid
  194. -- the 2nd replaced these with virtual items, dropped the content on update and set "virtual_items" to string "1"
  195. -- the third added an output inventory, changed the formspec and added a button for enabling/disabling
  196. -- so we work out way backwards on this history and update each single case to the newest version
  197. local function upgrade_autocrafter(pos, meta)
  198. local meta = meta or minetest.get_meta(pos)
  199. local inv = meta:get_inventory()
  200. if inv:get_size("output") == 0 then -- we are version 2 or 1
  201. inv:set_size("output", 1)
  202. -- migrate the old autocrafters into an "enabled" state
  203. update_meta(meta, true)
  204. if meta:get_string("virtual_items") == "1" then -- we are version 2
  205. -- we already dropped stuff, so lets remove the metadatasetting (we are not being called again for this node)
  206. meta:set_string("virtual_items", "")
  207. else -- we are version 1
  208. local recipe = inv:get_list("recipe")
  209. if not recipe then return end
  210. for idx, stack in ipairs(recipe) do
  211. if not stack:is_empty() then
  212. minetest.add_item(pos, stack)
  213. stack:set_count(1)
  214. stack:set_wear(0)
  215. inv:set_stack("recipe", idx, stack)
  216. end
  217. end
  218. end
  219. -- update the recipe, cache, and start the crafter
  220. autocrafterCache[minetest.hash_node_position(pos)] = nil
  221. after_recipe_change(pos, inv)
  222. end
  223. end
  224. minetest.register_node("pipeworks:autocrafter", {
  225. description = S("Autocrafter"),
  226. drawtype = "normal",
  227. tiles = {"pipeworks_autocrafter.png"},
  228. groups = {snappy = 3, tubedevice = 1, tubedevice_receiver = 1},
  229. tube = {insert_object = function(pos, node, stack, direction)
  230. local meta = minetest.get_meta(pos)
  231. local inv = meta:get_inventory()
  232. local added = inv:add_item("src", stack)
  233. after_inventory_change(pos)
  234. return added
  235. end,
  236. can_insert = function(pos, node, stack, direction)
  237. local meta = minetest.get_meta(pos)
  238. local inv = meta:get_inventory()
  239. return inv:room_for_item("src", stack)
  240. end,
  241. input_inventory = "dst",
  242. connect_sides = {left = 1, right = 1, front = 1, back = 1, top = 1, bottom = 1}},
  243. on_construct = function(pos)
  244. local meta = minetest.get_meta(pos)
  245. local inv = meta:get_inventory()
  246. inv:set_size("src", 3*8)
  247. inv:set_size("recipe", 3*3)
  248. inv:set_size("dst", 4*3)
  249. inv:set_size("output", 1)
  250. update_meta(meta, false)
  251. end,
  252. on_receive_fields = function(pos, formname, fields, sender)
  253. if not pipeworks.may_configure(pos, sender) then return end
  254. local meta = minetest.get_meta(pos)
  255. if fields.on then
  256. update_meta(meta, false)
  257. minetest.get_node_timer(pos):stop()
  258. elseif fields.off then
  259. if update_meta(meta, true) then
  260. start_crafter(pos)
  261. end
  262. elseif fields.save then
  263. meta:set_string("channel",fields.channel)
  264. end
  265. end,
  266. can_dig = function(pos, player)
  267. upgrade_autocrafter(pos)
  268. local meta = minetest.get_meta(pos)
  269. local inv = meta:get_inventory()
  270. return (inv:is_empty("src") and inv:is_empty("dst"))
  271. end,
  272. after_place_node = pipeworks.scan_for_tube_objects,
  273. after_dig_node = function(pos)
  274. pipeworks.scan_for_tube_objects(pos)
  275. end,
  276. on_destruct = function(pos)
  277. autocrafterCache[minetest.hash_node_position(pos)] = nil
  278. end,
  279. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  280. if not pipeworks.may_configure(pos, player) then return 0 end
  281. upgrade_autocrafter(pos)
  282. local inv = minetest.get_meta(pos):get_inventory()
  283. if listname == "recipe" then
  284. stack:set_count(1)
  285. inv:set_stack(listname, index, stack)
  286. after_recipe_change(pos, inv)
  287. return 0
  288. elseif listname == "output" then
  289. on_output_change(pos, inv, stack)
  290. return 0
  291. end
  292. after_inventory_change(pos)
  293. return stack:get_count()
  294. end,
  295. allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  296. if not pipeworks.may_configure(pos, player) then
  297. minetest.log("action", string.format("%s attempted to take from autocrafter at %s", player:get_player_name(), minetest.pos_to_string(pos)))
  298. return 0
  299. end
  300. upgrade_autocrafter(pos)
  301. local inv = minetest.get_meta(pos):get_inventory()
  302. if listname == "recipe" then
  303. inv:set_stack(listname, index, ItemStack(""))
  304. after_recipe_change(pos, inv)
  305. return 0
  306. elseif listname == "output" then
  307. on_output_change(pos, inv, nil)
  308. return 0
  309. end
  310. after_inventory_change(pos)
  311. return stack:get_count()
  312. end,
  313. allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  314. if not pipeworks.may_configure(pos, player) then return 0 end
  315. upgrade_autocrafter(pos)
  316. local inv = minetest.get_meta(pos):get_inventory()
  317. local stack = inv:get_stack(from_list, from_index)
  318. if to_list == "output" then
  319. on_output_change(pos, inv, stack)
  320. return 0
  321. elseif from_list == "output" then
  322. on_output_change(pos, inv, nil)
  323. if to_list ~= "recipe" then
  324. return 0
  325. end -- else fall through to recipe list handling
  326. end
  327. if from_list == "recipe" or to_list == "recipe" then
  328. if from_list == "recipe" then
  329. inv:set_stack(from_list, from_index, ItemStack(""))
  330. end
  331. if to_list == "recipe" then
  332. stack:set_count(1)
  333. inv:set_stack(to_list, to_index, stack)
  334. end
  335. after_recipe_change(pos, inv)
  336. return 0
  337. end
  338. after_inventory_change(pos)
  339. return count
  340. end,
  341. on_timer = run_autocrafter,
  342. digiline = {
  343. receptor = {},
  344. effector = {
  345. action = function(pos,node,channel,msg)
  346. local meta = minetest.get_meta(pos)
  347. if channel ~= meta:get_string("channel") then return end
  348. if type(msg) == "table" then
  349. if #msg < 3 then return end
  350. local inv = meta:get_inventory()
  351. for y=0,2,1 do
  352. for x=1,3,1 do
  353. local slot = y*3+x
  354. if minetest.registered_items[msg[y+1][x]] then
  355. inv:set_stack("recipe",slot,ItemStack(msg[y+1][x]))
  356. else
  357. inv:set_stack("recipe",slot,ItemStack(""))
  358. end
  359. end
  360. end
  361. after_recipe_change(pos,inv)
  362. elseif msg == "get_recipe" then
  363. local meta = minetest.get_meta(pos)
  364. local inv = meta:get_inventory()
  365. local recipe = {}
  366. for y=0,2,1 do
  367. local row = {}
  368. for x=1,3,1 do
  369. local slot = y*3+x
  370. table.insert(row, inv:get_stack("recipe",slot):get_name())
  371. end
  372. table.insert(recipe, row)
  373. end
  374. local setchan = meta:get_string("channel")
  375. local output = inv:get_stack("output", 1)
  376. digiline:receptor_send(pos, digiline.rules.default, setchan, {
  377. recipe = recipe,
  378. result = {
  379. name = output:get_name(),
  380. count = output:get_count(),
  381. }
  382. })
  383. elseif msg == "off" then
  384. update_meta(meta, false)
  385. minetest.get_node_timer(pos):stop()
  386. elseif msg == "on" then
  387. if update_meta(meta, true) then
  388. start_crafter(pos)
  389. end
  390. elseif msg == "single" then
  391. run_autocrafter(pos,1)
  392. end
  393. end,
  394. },
  395. },
  396. })
  397. minetest.register_craft( {
  398. output = "pipeworks:autocrafter 2",
  399. recipe = {
  400. { "default:steel_ingot", "default:mese_crystal", "default:steel_ingot" },
  401. { "basic_materials:plastic_sheet", "default:steel_ingot", "basic_materials:plastic_sheet" },
  402. { "default:steel_ingot", "default:mese_crystal", "default:steel_ingot" }
  403. },
  404. })