crafting.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. -- Test minetest.clear_craft function
  2. local function test_clear_craft()
  3. minetest.log("info", "[unittests] Testing minetest.clear_craft")
  4. -- Clearing by output
  5. minetest.register_craft({
  6. output = "foo",
  7. recipe = {{"bar"}}
  8. })
  9. minetest.register_craft({
  10. output = "foo 4",
  11. recipe = {{"foo", "bar"}}
  12. })
  13. assert(#minetest.get_all_craft_recipes("foo") == 2)
  14. minetest.clear_craft({output="foo"})
  15. assert(minetest.get_all_craft_recipes("foo") == nil)
  16. -- Clearing by input
  17. minetest.register_craft({
  18. output = "foo 4",
  19. recipe = {{"foo", "bar"}}
  20. })
  21. assert(#minetest.get_all_craft_recipes("foo") == 1)
  22. minetest.clear_craft({recipe={{"foo", "bar"}}})
  23. assert(minetest.get_all_craft_recipes("foo") == nil)
  24. end
  25. -- Test minetest.get_craft_result function
  26. local function test_get_craft_result()
  27. minetest.log("info", "[unittests] Testing minetest.get_craft_result")
  28. -- normal
  29. local input = {
  30. method = "normal",
  31. width = 2,
  32. items = {"", "unittests:coal_lump", "", "unittests:stick"}
  33. }
  34. minetest.log("info", "[unittests] torch crafting input: "..dump(input))
  35. local output, decremented_input = minetest.get_craft_result(input)
  36. minetest.log("info", "[unittests] torch crafting output: "..dump(output))
  37. minetest.log("info", "[unittests] torch crafting decremented input: "..dump(decremented_input))
  38. assert(output.item)
  39. minetest.log("info", "[unittests] torch crafting output.item:to_table(): "..dump(output.item:to_table()))
  40. assert(output.item:get_name() == "unittests:torch")
  41. assert(output.item:get_count() == 4)
  42. -- fuel
  43. input = {
  44. method = "fuel",
  45. width = 1,
  46. items = {"unittests:coal_lump"}
  47. }
  48. minetest.log("info", "[unittests] coal fuel input: "..dump(input))
  49. output, decremented_input = minetest.get_craft_result(input)
  50. minetest.log("info", "[unittests] coal fuel output: "..dump(output))
  51. minetest.log("info", "[unittests] coal fuel decremented input: "..dump(decremented_input))
  52. assert(output.time)
  53. assert(output.time > 0)
  54. -- cooking
  55. input = {
  56. method = "cooking",
  57. width = 1,
  58. items = {"unittests:iron_lump"}
  59. }
  60. minetest.log("info", "[unittests] iron lump cooking input: "..dump(output))
  61. output, decremented_input = minetest.get_craft_result(input)
  62. minetest.log("info", "[unittests] iron lump cooking output: "..dump(output))
  63. minetest.log("info", "[unittests] iron lump cooking decremented input: "..dump(decremented_input))
  64. assert(output.time)
  65. assert(output.time > 0)
  66. assert(output.item)
  67. minetest.log("info", "[unittests] iron lump cooking output.item:to_table(): "..dump(output.item:to_table()))
  68. assert(output.item:get_name() == "unittests:steel_ingot")
  69. assert(output.item:get_count() == 1)
  70. -- tool repair (repairable)
  71. input = {
  72. method = "normal",
  73. width = 2,
  74. -- Using a wear of 60000
  75. items = {"unittests:repairable_tool 1 60000", "unittests:repairable_tool 1 60000"}
  76. }
  77. minetest.log("info", "[unittests] repairable tool crafting input: "..dump(input))
  78. output, decremented_input = minetest.get_craft_result(input)
  79. minetest.log("info", "[unittests] repairable tool crafting output: "..dump(output))
  80. minetest.log("info", "[unittests] repairable tool crafting decremented input: "..dump(decremented_input))
  81. assert(output.item)
  82. minetest.log("info", "[unittests] repairable tool crafting output.item:to_table(): "..dump(output.item:to_table()))
  83. assert(output.item:get_name() == "unittests:repairable_tool")
  84. -- Test the wear value.
  85. -- See src/craftdef.cpp in Minetest source code for the formula. The formula to calculate
  86. -- the value 51187 is:
  87. -- 65536 - ((65536-60000)+(65536-60000)) + floor(additonal_wear * 65536 + 0.5) = 51187
  88. -- where additional_wear = 0.05
  89. assert(output.item:get_wear() == 51187)
  90. assert(output.item:get_count() == 1)
  91. -- failing tool repair (unrepairable)
  92. input = {
  93. method = "normal",
  94. width = 2,
  95. items = {"unittests:unrepairable_tool 1 60000", "unittests:unrepairable_tool 1 60000"}
  96. }
  97. minetest.log("info", "[unittests] unrepairable tool crafting input: "..dump(input))
  98. output, decremented_input = minetest.get_craft_result(input)
  99. minetest.log("info", "[unittests] unrepairable tool crafting output: "..dump(output))
  100. minetest.log("info", "[unittests] unrepairable tool crafting decremented input: "..dump(decremented_input))
  101. assert(output.item)
  102. minetest.log("info", "[unittests] unrepairable tool crafting output.item:to_table(): "..dump(output.item:to_table()))
  103. -- unrepairable tool must not yield any output
  104. assert(output.item:get_name() == "")
  105. end
  106. function unittests.test_crafting()
  107. test_clear_craft()
  108. test_get_craft_result()
  109. minetest.log("action", "[unittests] Crafting tests passed!")
  110. return true
  111. end