redefinitions.lua 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. --[[
  2. More Blocks: redefinitions of default stuff
  3. Copyright © 2011-2020 Hugo Locurcio and contributors.
  4. Licensed under the zlib license. See LICENSE.md for more information.
  5. --]]
  6. local modname = minetest.get_current_modname()
  7. -- Redefine some of the default crafting recipes to be more productive
  8. -- Auxiliary function: take a recipe as returned by get_all_craft_recipes
  9. -- and turn it into a table that can be used to clear a craft or declare a new one
  10. local reconstruct_internal_craft = function(recipe)
  11. local recp = {
  12. { "", "", "" },
  13. { "", "", "" },
  14. { "", "", "" },
  15. }
  16. local width = recipe.width
  17. for idx, item in pairs(recipe.items) do
  18. local row = math.ceil(idx / width)
  19. local col = idx - (row-1)*width
  20. recp[row][col] = item
  21. end
  22. return recp
  23. end
  24. -- Change the amount produced by recipe by apply func to the old amount
  25. local change_recipe_amount = function(product, recipe, func)
  26. -- if width == 0, this is a shapeless recipe, for which the
  27. -- internal and Lua API recipe table is the same.
  28. -- Otherwise we need to reconstruct the table for the shaped recipe.
  29. local shapeless = (recipe.width == 0)
  30. local recp = shapeless and recipe.items or reconstruct_internal_craft(recipe)
  31. local oldamount = tonumber(recipe.output:match(" [0-9]+$") or "1")
  32. local newamount = func(oldamount)
  33. -- remove old crafting recipe
  34. local redo = { recipe = recp }
  35. -- preserve shapelessness
  36. if shapeless then
  37. redo.type = "shapeless"
  38. end
  39. minetest.clear_craft(redo)
  40. -- new output
  41. redo.output = ("%s %d"):format(product, newamount)
  42. minetest.register_craft(redo)
  43. minetest.log("action", ("[MOD]%s: recipe for %s production: %d => %d"):format(modname, product, oldamount, newamount))
  44. end
  45. local increase_craft_production = function(product, func)
  46. local recipes = minetest.get_all_craft_recipes(product)
  47. for _, r in pairs(recipes) do
  48. if r.type == "normal" or r.method == "normal" then
  49. change_recipe_amount(product, r, func)
  50. end
  51. end
  52. end
  53. -- Increase the crafting production according to the rules from the table, which is in the form:
  54. -- {
  55. -- { detector, amount changing function }
  56. -- { detector, amount changing function }
  57. -- }
  58. -- TODO: consider exporting this function to other mods
  59. local increase_craft_production_table = function(map_table)
  60. for product, _ in pairs(minetest.registered_items) do
  61. for _, tab in pairs(map_table) do
  62. local detector = tab[1]
  63. local func = tab[2]
  64. if detector(product) then
  65. increase_craft_production(product, func)
  66. -- only apply one boost
  67. break
  68. end
  69. end
  70. end
  71. end
  72. increase_craft_production_table({
  73. { function(n) return n:match('^default:sign_wall') end, function(old) return old + 1 end },
  74. { function(n) return n == 'default:paper' end, function(old) return old*4 end },
  75. { function(n) return n:match('^carts:.*rail$') or n:match('^default:.*rail$') end, function(old) return old + old/2 end },
  76. })
  77. minetest.register_craft({
  78. type = "toolrepair",
  79. additional_wear = -0.10, -- Tool repair buff (10% bonus instead of 2%).
  80. })