init.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. local player_name = user and user:get_player_name() or ""
  78. if minetest.is_protected(pos, player_name) then
  79. minetest.record_protection_violation(pos, player_name)
  80. return
  81. end
  82. local node = minetest.get_node(pos)
  83. local ndef = minetest.registered_nodes[node.name]
  84. if not ndef then
  85. return itemstack
  86. end
  87. -- can we rotate this paramtype2?
  88. local fn = screwdriver.rotate[ndef.paramtype2]
  89. if not fn and not ndef.on_rotate then
  90. return itemstack
  91. end
  92. local should_rotate = true
  93. local new_param2
  94. if fn then
  95. new_param2 = fn(pos, node, mode)
  96. else
  97. new_param2 = node.param2
  98. end
  99. -- Node provides a handler, so let the handler decide instead if the node can be rotated
  100. if ndef.on_rotate then
  101. -- Copy pos and node because callback can modify it
  102. local result = ndef.on_rotate(vector.new(pos),
  103. {name = node.name, param1 = node.param1, param2 = node.param2},
  104. user, mode, new_param2)
  105. if result == false then -- Disallow rotation
  106. return itemstack
  107. elseif result == true then
  108. should_rotate = false
  109. end
  110. elseif ndef.on_rotate == false then
  111. return itemstack
  112. elseif ndef.can_dig and not ndef.can_dig(pos, user) then
  113. return itemstack
  114. end
  115. if should_rotate and new_param2 ~= node.param2 then
  116. node.param2 = new_param2
  117. minetest.swap_node(pos, node)
  118. minetest.check_for_falling(pos)
  119. end
  120. if not (creative and creative.is_enabled_for and
  121. creative.is_enabled_for(player_name)) then
  122. itemstack:add_wear(65535 / ((uses or 200) - 1))
  123. end
  124. return itemstack
  125. end
  126. -- Screwdriver
  127. minetest.register_tool("screwdriver:screwdriver", {
  128. description = "Screwdriver (left-click rotates face, right-click rotates axis)",
  129. inventory_image = "screwdriver.png",
  130. stack_max = 1,
  131. on_use = function(itemstack, user, pointed_thing)
  132. screwdriver.handler(itemstack, user, pointed_thing, screwdriver.ROTATE_FACE, 200)
  133. return itemstack
  134. end,
  135. on_place = function(itemstack, user, pointed_thing)
  136. screwdriver.handler(itemstack, user, pointed_thing, screwdriver.ROTATE_AXIS, 200)
  137. return itemstack
  138. end,
  139. })
  140. minetest.register_craft({
  141. output = "screwdriver:screwdriver",
  142. recipe = {
  143. {"default:steel_ingot"},
  144. {"group:stick"}
  145. }
  146. })
  147. minetest.register_alias("screwdriver:screwdriver1", "screwdriver:screwdriver")
  148. minetest.register_alias("screwdriver:screwdriver2", "screwdriver:screwdriver")
  149. minetest.register_alias("screwdriver:screwdriver3", "screwdriver:screwdriver")
  150. minetest.register_alias("screwdriver:screwdriver4", "screwdriver:screwdriver")