craftingutil.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. -- This below is the Crafter mod by the legend MasterGollum
  2. function factory.register_craft(craft)
  3. assert(craft.type ~= nil and craft.recipe ~= nil and craft.output ~= nil,
  4. "Invalid craft definition, it must have type, recipe and output")
  5. assert(type(craft.recipe)=="table" and type(craft.recipe[1])=="table","'recipe' must be a bidimensional table")
  6. minetest.log("verbose","registerCraft ("..craft.type..", output="..craft.output.." recipe="..dump(craft.recipe))
  7. craft._h=#craft.recipe
  8. craft._w=#craft.recipe[1]
  9. -- TODO check that all the arrays have the same length...
  10. factory.crafts[#factory.crafts+1]=craft
  11. end
  12. function factory.get_craft_result(data)
  13. assert(data.method ~= nil and data.items ~= nil, "Invalid call, method and items must be provided")
  14. local w = 1
  15. if data.width ~= nil and data.width>0 then
  16. w=data.width
  17. end
  18. local r=nil
  19. for zz,craft in ipairs(factory.crafts) do
  20. r=factory._check_craft(data,w,craft)
  21. if r ~= nil then
  22. if factory.debug then
  23. print("Craft found, returning "..dump(r.item))
  24. end
  25. return r
  26. end
  27. end
  28. return factory.empty
  29. end
  30. function factory._check_craft(data,w,c)
  31. if c.type == data.method then
  32. -- Here we go..
  33. for i=1,w-c._h+1 do
  34. for j=1,w-c._w+1 do
  35. local p=(i-1)*w+j
  36. if factory.debug then
  37. print("Checking data.items["..dump(i).."]["..dump(j).."]("..dump(p)..")="..dump(data.items[p]:get_name()).." vs craft.recipe[1][1]="..dump(c.recipe[1][1]))
  38. end
  39. if data.items[p]:get_name() == c.recipe[1][1] then
  40. for m=1,c._h do
  41. for n=1,c._w do
  42. local q=(i+m-1-1)*w+j+n-1
  43. if factory.debug then
  44. print(" Checking data.items["..dump(i+m-1).."]["..dump(j+n-1).."]("..dump(q)..")="..dump(data.items[q]:get_name())..
  45. " vs craft.recipe["..dump(m).."]["..dump(n).."]="..dump(c.recipe[m][n]))
  46. end
  47. if c.recipe[m][n] ~= data.items[q]:get_name() then
  48. return nil
  49. end
  50. end
  51. end
  52. -- found! we still must check that is not any other stuff outside the limits of the recipe sizes...
  53. -- Checking at right of the matching square
  54. for m=i-c._h+1+1,w do
  55. for n=j+c._w,w do
  56. local q=(m-1)*w+n
  57. if factory.debug then
  58. print(" Checking right data.items["..dump(m).."]["..dump(n).."]("..dump(q)..")="..dump(data.items[q]:get_name()))
  59. end
  60. if data.items[q]:get_name() ~= "" then
  61. return nil
  62. end
  63. end
  64. end
  65. -- Checking at left of the matching square (the first row has been already scanned)
  66. for m=i-c._h+1+1+1,w do
  67. for n=1,j-1 do
  68. local q=(m-1)*w+n
  69. if factory.debug then
  70. print(" Checking left data.items["..dump(m).."]["..dump(n).."]("..dump(q)..")="..dump(data.items[q]:get_name()))
  71. end
  72. if data.items[q]:get_name() ~= "" then
  73. return nil
  74. end
  75. end
  76. end
  77. -- Checking at bottom of the matching square
  78. for m=i+c._h,w do
  79. for n=j,j+c._w do
  80. local q=(m-1)*w+n
  81. if factory.debug then
  82. print(" Checking bottom data.items["..dump(m).."]["..dump(n).."]("..dump(q)..")="..dump(data.items[q]:get_name()))
  83. end
  84. if data.items[q]:get_name() ~= "" then
  85. return nil
  86. end
  87. end
  88. end
  89. if factory.debug then
  90. print("Craft found! "..c.output)
  91. end
  92. return {item=ItemStack(c.output),time=1}
  93. elseif data.items[p] ~= nil and data.items[p]:get_name() ~= "" then
  94. if factory.debug then
  95. print("Invalid data item "..dump(data.items[p]:get_name()))
  96. end
  97. return nil
  98. end
  99. end
  100. end
  101. end
  102. end