mortar.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. local repairable_hashtable
  2. local repairing_goal = minetest.get_content_id("degradable_walls:wall")
  3. local floor = math.floor
  4. local ceil = math.ceil
  5. local sound_play = minetest.sound_play
  6. local S = minetest.get_translator("degradable_walls")
  7. local radius = 3
  8. local voxel_buffer = {}
  9. minetest.register_craftitem("degradable_walls:mortar",
  10. {
  11. description = S("Mortar"),
  12. inventory_image = "degradable_walls_mortar.png",
  13. max_stack = 128,
  14. on_use = function(itemstack, user, pointed_thing)
  15. if pointed_thing.type == "node"
  16. then
  17. local pos = pointed_thing.under
  18. local vm = minetest.get_voxel_manip()
  19. local minpos = {
  20. x = floor(pos.x - radius),
  21. y = floor(pos.y - radius),
  22. z = floor(pos.z - radius)
  23. }
  24. local maxpos = {
  25. x = ceil(pos.x + radius),
  26. y = ceil(pos.y + radius),
  27. z = ceil(pos.z + radius)
  28. }
  29. local emin, emax = vm:read_from_map(minpos, maxpos)
  30. local area = VoxelArea:new{MinEdge = emin, MaxEdge = emax}
  31. local data = vm:get_data(voxel_buffer)
  32. for iz = minpos.z, maxpos.z
  33. do
  34. for iy = minpos.y, maxpos.y
  35. do
  36. for ix = minpos.x, maxpos.x
  37. do
  38. local position_vector = {x = ix, y = iy, z = iz}
  39. local dist = vector.distance(pos, position_vector)
  40. if dist < radius and
  41. repairable_hashtable[data[area:index(ix, iy, iz)]]
  42. then
  43. data[area:index(ix, iy, iz)] = repairing_goal
  44. end
  45. end
  46. end
  47. end
  48. vm:set_data(data)
  49. vm:write_to_map(true)
  50. sound_play("degradable_walls_mortar_application",
  51. {
  52. gain = 0.5,
  53. pitch = 1 + (math.random() - 0.5) / 8
  54. })
  55. itemstack:take_item()
  56. return itemstack
  57. end
  58. end
  59. })
  60. minetest.register_alias("mortar", "degradable_walls:mortar")
  61. local cobble_name = minetest.registered_aliases["cobble"]
  62. assert(type(cobble_name) == "string", "There is no node with alias " ..
  63. "'cobble' registered.")
  64. minetest.register_node("degradable_walls:mortar_and_pestle",
  65. {
  66. description = S("Mortar And Pestle\n@1",
  67. minetest.colorize("#90FFFF", S("Right click with cobble to make mortar for repairing walls"))),
  68. inventory_image = "degradable_walls_mortar_and_pestle_inv.png",
  69. drawtype = "mesh",
  70. mesh = "degradable_walls_mortar_and_pestle.obj",
  71. tiles =
  72. {
  73. "degradable_walls_mortar_and_pestle_m.png",
  74. "degradable_walls_mortar_and_pestle_p.png"
  75. },
  76. collision_box =
  77. {
  78. type = "fixed",
  79. fixed = {
  80. {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  81. },
  82. },
  83. selection_box =
  84. {
  85. type = "fixed",
  86. fixed = {
  87. {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
  88. },
  89. },
  90. is_ground_content = false,
  91. groups = {cracky = 2, stone = 1},
  92. sounds = generic_media.node_sound_stone_defaults(),
  93. stack_max = 1,
  94. on_rightclick = function(pos, node, clicker, itemstack)
  95. local itemname = itemstack:get_name()
  96. if itemname == cobble_name and clicker
  97. then
  98. --TODO: play sound
  99. local inv = clicker:get_inventory()
  100. local leftover = inv:add_item("main", "degradable_walls:mortar")
  101. if leftover:is_empty()
  102. then
  103. itemstack:take_item()
  104. sound_play("degradable_walls_mortar_use",
  105. {
  106. gain = 0.2,
  107. pitch = 1 + (math.random() - 0.5) / 16
  108. })
  109. return itemstack
  110. end
  111. end
  112. end,
  113. })
  114. minetest.register_alias("mortar_and_pestle", "degradable_walls:mortar_and_pestle")
  115. crafting.register_recipe(
  116. {
  117. output = "mortar_and_pestle",
  118. type = "inv",
  119. items = {"cobble 8", "stick 1"},
  120. always_known = true,
  121. })
  122. return function(h)
  123. repairable_hashtable = h
  124. end