screwdriver.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. if not minetest.global_exists("electric_screwdriver") then electric_screwdriver = {} end
  2. electric_screwdriver.modpath = minetest.get_modpath("silicon")
  3. electric_screwdriver.image = "technic_sonic_screwdriver.png"
  4. electric_screwdriver.sound = "technic_sonic_screwdriver"
  5. electric_screwdriver.name = "electric_screwdriver:electric_screwdriver"
  6. electric_screwdriver.description = "Electric Screwdriver\n\nAn electrical, reusable screwdriver.\nMust be charged to use.\nLeft-click + 'E' copies rotation, right-click + 'E' applies rotation."
  7. electric_screwdriver.sound_gain = 0.1
  8. electric_screwdriver.sound_dist = 25
  9. -- Localize for performance.
  10. local math_floor = math.floor
  11. -- This is how many nodes the electric screwdriver can spin.
  12. electric_screwdriver.uses = math_floor(65535/2500)
  13. function electric_screwdriver.on_use(itemstack, user, pt)
  14. if not user or not user:is_player() then
  15. return
  16. end
  17. if pt.type ~= "node" then
  18. return
  19. end
  20. local wear = itemstack:get_wear()
  21. if wear == 0 then
  22. -- Tool isn't charged!
  23. -- Once it is charged the first time, wear should never be 0 again.
  24. return
  25. end
  26. if wear > math_floor(65535-electric_screwdriver.uses) then
  27. -- Tool has no charge left.
  28. return
  29. end
  30. local control = user:get_player_control()
  31. if control.aux1 then
  32. local meta = itemstack:get_meta()
  33. local node = minetest.get_node(pt.under)
  34. local ndef = minetest.registered_nodes[node.name]
  35. if ndef and ndef.paramtype2 == "facedir" then
  36. if node.param2 >= 0 and node.param2 <= 23 then
  37. meta:set_int("screwdriver_rotation", node.param2)
  38. minetest.chat_send_player(user:get_player_name(),
  39. "# Server: copied facedir (" .. node.param2 .. ")!")
  40. end
  41. end
  42. else
  43. -- We handle stack ourselves.
  44. local fakestack = ItemStack(itemstack:get_name())
  45. screwdriver.handler(fakestack, user, pt, screwdriver.ROTATE_FACE, 200)
  46. end
  47. ambiance.sound_play(electric_screwdriver.sound, pt.under, electric_screwdriver.sound_gain, electric_screwdriver.sound_dist)
  48. wear = wear + electric_screwdriver.uses
  49. -- Don't let wear reach max or tool will be destroyed.
  50. if wear >= 65535 then
  51. wear = 65534
  52. end
  53. itemstack:set_wear(wear)
  54. return itemstack
  55. end
  56. function electric_screwdriver.on_place(itemstack, user, pt)
  57. if not user or not user:is_player() then
  58. return
  59. end
  60. if pt.type ~= "node" then
  61. return
  62. end
  63. local wear = itemstack:get_wear()
  64. if wear == 0 then
  65. -- Tool isn't charged!
  66. -- Once it is charged the first time, wear should never be 0 again.
  67. return
  68. end
  69. if wear > math_floor(65535-electric_screwdriver.uses) then
  70. -- Tool has no charge left.
  71. return
  72. end
  73. local control = user:get_player_control()
  74. if control.aux1 then
  75. if not minetest.is_protected(pt.under, user:get_player_name()) then
  76. local meta = itemstack:get_meta()
  77. local param2 = meta:get_int("screwdriver_rotation")
  78. local node = minetest.get_node(pt.under)
  79. local ndef = minetest.registered_nodes[node.name]
  80. if ndef and ndef.paramtype2 == "facedir" then
  81. if param2 >= 0 and param2 <= 23 then
  82. node.param2 = param2
  83. minetest.swap_node(pt.under, node)
  84. ambiance.sound_play("default_dug_metal", pt.under, 1, 30)
  85. end
  86. end
  87. end
  88. else
  89. -- We handle stack ourselves.
  90. local fakestack = ItemStack(itemstack:get_name())
  91. screwdriver.handler(fakestack, user, pt, screwdriver.ROTATE_AXIS, 200)
  92. end
  93. ambiance.sound_play(electric_screwdriver.sound, pt.under, electric_screwdriver.sound_gain, electric_screwdriver.sound_dist)
  94. wear = wear + electric_screwdriver.uses
  95. -- Don't let wear reach max or tool will be destroyed.
  96. if wear >= 65535 then
  97. wear = 65534
  98. end
  99. itemstack:set_wear(wear)
  100. return itemstack
  101. end
  102. if not electric_screwdriver.run_once then
  103. minetest.register_tool(":" .. electric_screwdriver.name, {
  104. description = electric_screwdriver.description,
  105. inventory_image = electric_screwdriver.image,
  106. wear_represents = "eu_charge",
  107. groups = {not_repaired_by_anvil = 1, disable_repair = 1},
  108. on_use = function(...)
  109. return electric_screwdriver.on_use(...)
  110. end,
  111. on_place = function(...)
  112. return electric_screwdriver.on_place(...)
  113. end,
  114. })
  115. ---[[
  116. minetest.register_craft({
  117. output = electric_screwdriver.name,
  118. recipe = {
  119. {"", "default:diamond", ""},
  120. {"plastic:plastic_sheeting", "battery:battery", "plastic:plastic_sheeting"},
  121. {"plastic:plastic_sheeting", "moreores:mithril_ingot", "plastic:plastic_sheeting"},
  122. }
  123. })
  124. --]]
  125. local c = "electric_screwdriver:core"
  126. local f = electric_screwdriver.modpath .. "/screwdriver.lua"
  127. reload.register_file(c, f, false)
  128. electric_screwdriver.run_once = true
  129. end