screwdriver.lua 4.4 KB

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