init.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. local wallmounted_tbl = {
  54. [screwdriver.ROTATE_FACE] = {[2] = 5, [3] = 4, [4] = 2, [5] = 3, [1] = 0, [0] = 1},
  55. [screwdriver.ROTATE_AXIS] = {[2] = 5, [3] = 4, [4] = 2, [5] = 1, [1] = 0, [0] = 3}
  56. }
  57. screwdriver.rotate.wallmounted = function(pos, node, mode)
  58. local rotation = node.param2 % 8 -- get first 3 bits
  59. local other = node.param2 - rotation
  60. rotation = wallmounted_tbl[mode][rotation] or 0
  61. if minetest.get_item_group(node.name, "attached_node") ~= 0 then
  62. -- find an acceptable orientation
  63. for i = 1, 5 do
  64. if not check_attached_node(pos, rotation) then
  65. rotation = wallmounted_tbl[mode][rotation] or 0
  66. else
  67. break
  68. end
  69. end
  70. end
  71. return rotation + other
  72. end
  73. screwdriver.rotate.colorwallmounted = screwdriver.rotate.wallmounted
  74. -- Handles rotation
  75. screwdriver.handler = function(itemstack, user, pointed_thing, mode, uses)
  76. if pointed_thing.type ~= "node" then
  77. return
  78. end
  79. local pos = pointed_thing.under
  80. local player_name = user and user:get_player_name() or ""
  81. if minetest.is_protected(pos, player_name) then
  82. minetest.record_protection_violation(pos, player_name)
  83. return
  84. end
  85. local node = minetest.get_node(pos)
  86. local ndef = minetest.registered_nodes[node.name]
  87. if not ndef then
  88. return itemstack
  89. end
  90. -- can we rotate this paramtype2?
  91. local fn = screwdriver.rotate[ndef.paramtype2]
  92. if not fn and not ndef.on_rotate then
  93. return itemstack
  94. end
  95. local should_rotate = true
  96. local new_param2
  97. if fn then
  98. new_param2 = fn(pos, node, mode)
  99. else
  100. new_param2 = node.param2
  101. end
  102. -- Node provides a handler, so let the handler decide instead if the node can be rotated
  103. if ndef.on_rotate then
  104. -- Copy pos and node because callback can modify it
  105. local result = ndef.on_rotate(vector.new(pos),
  106. {name = node.name, param1 = node.param1, param2 = node.param2},
  107. user, mode, new_param2)
  108. if result == false then -- Disallow rotation
  109. return itemstack
  110. elseif result == true then
  111. should_rotate = false
  112. end
  113. elseif ndef.on_rotate == false then
  114. return itemstack
  115. elseif ndef.can_dig and not ndef.can_dig(pos, user) then
  116. return itemstack
  117. end
  118. if should_rotate and new_param2 ~= node.param2 then
  119. node.param2 = new_param2
  120. minetest.swap_node(pos, node)
  121. minetest.check_for_falling(pos)
  122. end
  123. if not (creative and creative.is_enabled_for and
  124. creative.is_enabled_for(player_name)) then
  125. itemstack:add_wear(65535 / ((uses or 200) - 1))
  126. end
  127. return itemstack
  128. end
  129. -- Screwdriver
  130. minetest.register_tool("screwdriver:screwdriver", {
  131. description = S("Screwdriver") .. "\n" .. S("(left-click rotates face, right-click rotates axis)"),
  132. inventory_image = "screwdriver.png",
  133. groups = {tool = 1},
  134. on_use = function(itemstack, user, pointed_thing)
  135. screwdriver.handler(itemstack, user, pointed_thing, screwdriver.ROTATE_FACE, 200)
  136. return itemstack
  137. end,
  138. on_place = function(itemstack, user, pointed_thing)
  139. screwdriver.handler(itemstack, user, pointed_thing, screwdriver.ROTATE_AXIS, 200)
  140. return itemstack
  141. end,
  142. })
  143. minetest.register_craft({
  144. output = "screwdriver:screwdriver",
  145. recipe = {
  146. {"default:steel_ingot"},
  147. {"group:stick"}
  148. }
  149. })
  150. minetest.register_alias("screwdriver:screwdriver1", "screwdriver:screwdriver")
  151. minetest.register_alias("screwdriver:screwdriver2", "screwdriver:screwdriver")
  152. minetest.register_alias("screwdriver:screwdriver3", "screwdriver:screwdriver")
  153. minetest.register_alias("screwdriver:screwdriver4", "screwdriver:screwdriver")