init.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. screwdriver = {}
  2. screwdriver.ROTATE_FACE = 1
  3. screwdriver.ROTATE_AXIS = 2
  4. screwdriver.disallow = function(pos, node, user, mode, new_param2)
  5. return false
  6. end
  7. screwdriver.rotate_simple = function(pos, node, user, mode, new_param2)
  8. if mode ~= screwdriver.ROTATE_FACE then
  9. return false
  10. end
  11. end
  12. -- For attached wallmounted nodes: returns true if rotation is valid
  13. -- simplified version of minetest:builtin/game/falling.lua#L148.
  14. local function check_attached_node(pos, rotation)
  15. local d = minetest.wallmounted_to_dir(rotation)
  16. local p2 = vector.add(pos, d)
  17. local n = minetest.get_node(p2).name
  18. local def2 = minetest.registered_nodes[n]
  19. if def2 and not def2.walkable then
  20. return false
  21. end
  22. return true
  23. end
  24. screwdriver.rotate = {}
  25. local facedir_tbl = {
  26. [screwdriver.ROTATE_FACE] = {
  27. [0] = 1, [1] = 2, [2] = 3, [3] = 0,
  28. [4] = 5, [5] = 6, [6] = 7, [7] = 4,
  29. [8] = 9, [9] = 10, [10] = 11, [11] = 8,
  30. [12] = 13, [13] = 14, [14] = 15, [15] = 12,
  31. [16] = 17, [17] = 18, [18] = 19, [19] = 16,
  32. [20] = 21, [21] = 22, [22] = 23, [23] = 20,
  33. },
  34. [screwdriver.ROTATE_AXIS] = {
  35. [0] = 4, [1] = 4, [2] = 4, [3] = 4,
  36. [4] = 8, [5] = 8, [6] = 8, [7] = 8,
  37. [8] = 12, [9] = 12, [10] = 12, [11] = 12,
  38. [12] = 16, [13] = 16, [14] = 16, [15] = 16,
  39. [16] = 20, [17] = 20, [18] = 20, [19] = 20,
  40. [20] = 0, [21] = 0, [22] = 0, [23] = 0,
  41. },
  42. }
  43. screwdriver.rotate.facedir = function(pos, node, mode)
  44. local rotation = node.param2 % 32 -- get first 5 bits
  45. local other = node.param2 - rotation
  46. rotation = facedir_tbl[mode][rotation] or 0
  47. return rotation + other
  48. end
  49. screwdriver.rotate.colorfacedir = screwdriver.rotate.facedir
  50. local wallmounted_tbl = {
  51. [screwdriver.ROTATE_FACE] = {[2] = 5, [3] = 4, [4] = 2, [5] = 3, [1] = 0, [0] = 1},
  52. [screwdriver.ROTATE_AXIS] = {[2] = 5, [3] = 4, [4] = 2, [5] = 1, [1] = 0, [0] = 3}
  53. }
  54. screwdriver.rotate.wallmounted = function(pos, node, mode)
  55. local rotation = node.param2 % 8 -- get first 3 bits
  56. local other = node.param2 - rotation
  57. rotation = wallmounted_tbl[mode][rotation] or 0
  58. if minetest.get_item_group(node.name, "attached_node") ~= 0 then
  59. -- find an acceptable orientation
  60. for i = 1, 5 do
  61. if not check_attached_node(pos, rotation) then
  62. rotation = wallmounted_tbl[mode][rotation] or 0
  63. else
  64. break
  65. end
  66. end
  67. end
  68. return rotation + other
  69. end
  70. screwdriver.rotate.colorwallmounted = screwdriver.rotate.wallmounted
  71. -- Handles rotation
  72. screwdriver.handler = function(itemstack, user, pointed_thing, mode, uses)
  73. if pointed_thing.type ~= "node" then
  74. return
  75. end
  76. local pos = pointed_thing.under
  77. if minetest.is_protected(pos, user:get_player_name()) then
  78. minetest.record_protection_violation(pos, user:get_player_name())
  79. return
  80. end
  81. local node = minetest.get_node(pos)
  82. local ndef = minetest.registered_nodes[node.name]
  83. if not ndef then
  84. return itemstack
  85. end
  86. -- can we rotate this paramtype2?
  87. local fn = screwdriver.rotate[ndef.paramtype2]
  88. if not fn then
  89. return itemstack
  90. end
  91. local should_rotate = true
  92. local new_param2 = fn(pos, node, mode)
  93. -- Node provides a handler, so let the handler decide instead if the node can be rotated
  94. if ndef.on_rotate then
  95. -- Copy pos and node because callback can modify it
  96. local result = ndef.on_rotate(vector.new(pos),
  97. {name = node.name, param1 = node.param1, param2 = node.param2},
  98. user, mode, new_param2)
  99. if result == false then -- Disallow rotation
  100. return itemstack
  101. elseif result == true then
  102. should_rotate = false
  103. end
  104. elseif ndef.on_rotate == false then
  105. return itemstack
  106. elseif ndef.can_dig and not ndef.can_dig(pos, user) then
  107. return itemstack
  108. end
  109. if should_rotate then
  110. node.param2 = new_param2
  111. minetest.swap_node(pos, node)
  112. minetest.check_for_falling(pos)
  113. end
  114. local sound = "default_place_node_hard"
  115. if ndef.sounds then
  116. if ndef.sounds.rotate then
  117. sound = ndef.sounds.rotate
  118. elseif ndef.sounds.footstep then
  119. sound = ndef.sounds.footstep
  120. elseif ndef.sounds.place then
  121. sound = ndef.sounds.place
  122. end
  123. else
  124. if node.name:find("wood") then
  125. sound = "default_wood_footstep"
  126. elseif node.name:find("stone") then
  127. sound = "default_hard_footstep"
  128. elseif node.name:find("glass") then
  129. sound = "default_glass_footstep"
  130. end
  131. end
  132. ambiance.sound_play(sound, pos, 1, 30)
  133. itemstack:add_wear(65535 / ((uses or 200) - 1))
  134. return itemstack
  135. end
  136. -- Screwdriver
  137. minetest.register_tool("screwdriver:screwdriver", {
  138. description = "Screwdriver\n\nLeft-click rotates face around current axis of rotation.\nRight-click changes axis of rotation.",
  139. inventory_image = "screwdriver.png",
  140. sound = {breaks = "default_tool_breaks"},
  141. on_use = function(itemstack, user, pointed_thing)
  142. screwdriver.handler(itemstack, user, pointed_thing, screwdriver.ROTATE_FACE, 200)
  143. return itemstack
  144. end,
  145. on_place = function(itemstack, user, pointed_thing)
  146. screwdriver.handler(itemstack, user, pointed_thing, screwdriver.ROTATE_AXIS, 200)
  147. return itemstack
  148. end,
  149. })
  150. minetest.register_craft({
  151. output = "screwdriver:screwdriver",
  152. recipe = {
  153. {"default:steel_ingot"},
  154. {"group:stick"}
  155. }
  156. })
  157. minetest.register_craft({
  158. output = "screwdriver:screwdriver",
  159. recipe = {
  160. {"moreores:tin_ingot"},
  161. {"group:stick"}
  162. }
  163. })
  164. minetest.register_alias("screwdriver:screwdriver1", "screwdriver:screwdriver")
  165. minetest.register_alias("screwdriver:screwdriver2", "screwdriver:screwdriver")
  166. minetest.register_alias("screwdriver:screwdriver3", "screwdriver:screwdriver")
  167. minetest.register_alias("screwdriver:screwdriver4", "screwdriver:screwdriver")