init.lua 5.4 KB

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