crafting.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. dofile(core.get_modpath(core.get_current_modname()) .. "/crafting_prepare.lua")
  2. -- Test core.clear_craft function
  3. local function test_clear_craft()
  4. -- Clearing by output
  5. core.register_craft({
  6. output = "foo",
  7. recipe = {{"bar"}}
  8. })
  9. core.register_craft({
  10. output = "foo 4",
  11. recipe = {{"foo", "bar"}}
  12. })
  13. assert(#core.get_all_craft_recipes("foo") == 2)
  14. core.clear_craft({output="foo"})
  15. assert(core.get_all_craft_recipes("foo") == nil)
  16. -- Clearing by input
  17. core.register_craft({
  18. output = "foo 4",
  19. recipe = {{"foo", "bar"}}
  20. })
  21. assert(#core.get_all_craft_recipes("foo") == 1)
  22. core.clear_craft({recipe={{"foo", "bar"}}})
  23. assert(core.get_all_craft_recipes("foo") == nil)
  24. end
  25. unittests.register("test_clear_craft", test_clear_craft)
  26. -- Test core.get_craft_result function
  27. local function test_get_craft_result()
  28. -- normal
  29. local input = {
  30. method = "normal",
  31. width = 2,
  32. items = {"", "unittests:coal_lump", "", "unittests:stick"}
  33. }
  34. core.log("info", "[unittests] torch crafting input: "..dump(input))
  35. local output, decremented_input = core.get_craft_result(input)
  36. core.log("info", "[unittests] torch crafting output: "..dump(output))
  37. core.log("info", "[unittests] torch crafting decremented input: "..dump(decremented_input))
  38. assert(output.item)
  39. core.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. core.log("info", "[unittests] coal fuel input: "..dump(input))
  49. output, decremented_input = core.get_craft_result(input)
  50. core.log("info", "[unittests] coal fuel output: "..dump(output))
  51. core.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. core.log("info", "[unittests] iron lump cooking input: "..dump(output))
  61. output, decremented_input = core.get_craft_result(input)
  62. core.log("info", "[unittests] iron lump cooking output: "..dump(output))
  63. core.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. core.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. core.log("info", "[unittests] repairable tool crafting input: "..dump(input))
  78. output, decremented_input = core.get_craft_result(input)
  79. core.log("info", "[unittests] repairable tool crafting output: "..dump(output))
  80. core.log("info", "[unittests] repairable tool crafting decremented input: "..dump(decremented_input))
  81. assert(output.item)
  82. core.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 Luanti 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. core.log("info", "[unittests] unrepairable tool crafting input: "..dump(input))
  98. output, decremented_input = core.get_craft_result(input)
  99. core.log("info", "[unittests] unrepairable tool crafting output: "..dump(output))
  100. core.log("info", "[unittests] unrepairable tool crafting decremented input: "..dump(decremented_input))
  101. assert(output.item)
  102. core.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:is_empty())
  105. end
  106. unittests.register("test_get_craft_result", test_get_craft_result)