init.lua 4.9 KB

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