hand_grenade.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. minetest.register_craftitem("rangedweapons:pin", {
  2. wield_scale = {x=2.5,y=2.5,z=1.0},
  3. inventory_image = "rangedweapons_pin.png",
  4. })
  5. local rangedweapons_grenade_pin = {
  6. physical = false,
  7. timer = 0,
  8. visual = "wielditem",
  9. visual_size = {x=0.15, y=0.15},
  10. textures = {"rangedweapons:pin"},
  11. lastpos= {},
  12. collisionbox = {0, 0, 0, 0, 0, 0},
  13. }
  14. rangedweapons_grenade_pin.on_step = function(self, dtime, pos)
  15. self.timer = self.timer + dtime
  16. local pos = self.object:get_pos()
  17. local node = minetest.get_node(pos)
  18. if self.lastpos.x ~= nil then
  19. if minetest.registered_nodes[node.name].walkable then
  20. self.object:remove()
  21. minetest.sound_play("rangedweapons_bulletdrop", {pos = self.lastpos, gain = 0.8})
  22. end
  23. end
  24. self.lastpos= {x = pos.x, y = pos.y, z = pos.z}
  25. end
  26. minetest.register_entity("rangedweapons:grenade_pin", rangedweapons_grenade_pin)
  27. local grenade_boom = {
  28. name = "rangedweapons:grenade_explosion",
  29. --description = "DuN mInD mEh...",
  30. radius = 3,
  31. tiles = {
  32. side = "rangedweapons_invisible.png",
  33. top = "rangedweapons_invisible.png",
  34. bottom = "rangedweapons_invisible.png",
  35. burning = "rangedweapons_invisible.png"
  36. },
  37. }
  38. local gtimer = 0
  39. minetest.register_craftitem("rangedweapons:hand_grenade", {
  40. stack_max= 1,
  41. wield_scale = {x=1.1,y=1.1,z=1.05},
  42. description = "" ..core.colorize("#35cdff","Hand grenade\n") ..core.colorize("#FFFFFF", "Explosion radius: 3\n")..core.colorize("#FFFFFF", "Throw force: 12\n") ..core.colorize("#FFFFFF", "Grenade gravitational pull: 6\n") ..core.colorize("#ffc000", "Right-click to unpin, Left click to throw after unpinning\n") ..core.colorize("#ffc000", "Thrown or not, it will explode after 3 secons from unpinning, be careful!"),
  43. range = 0,
  44. inventory_image = "rangedweapons_hand_grenade.png",
  45. on_secondary_use = function(itemstack, user, pointed_thing)
  46. if minetest.find_node_near(user:get_pos(), 10,"rangedweapons:antigun_block")
  47. then
  48. minetest.chat_send_player(user:get_player_name(), "" ..core.colorize("#ff0000","Grenades are prohibited in this area!"))
  49. return itemstack
  50. end
  51. gtimer = 0
  52. minetest.sound_play("rangedweapons_reload_a", {user})
  53. itemstack = "rangedweapons:hand_grenade_nopin"
  54. local pos = user:get_pos()
  55. pos.y = pos.y + 1.5
  56. local pinEnt = minetest.add_entity(pos, "rangedweapons:grenade_pin")
  57. if pinEnt then
  58. local dir = user:get_look_dir()
  59. local yaw = user:get_look_yaw()
  60. local svertical = user:get_look_vertical()
  61. pinEnt:setvelocity({x=dir.x * -10, y=dir.y * -10, z=dir.z * -10})
  62. pinEnt:setacceleration({x=dir.x * -5, y= -10, z=dir.z * -5})
  63. pinEnt:set_rotation({x=0,y=yaw + math.pi,z=-svertical})
  64. end
  65. return itemstack end,
  66. })
  67. minetest.register_craftitem("rangedweapons:hand_grenade_nopin", {
  68. stack_max= 1,
  69. wield_scale = {x=1.1,y=1.1,z=1.05},
  70. description = "***HURRY UP AND THROW IT!!!***",
  71. range = 0,
  72. inventory_image = "rangedweapons_hand_grenade_nopin.png",
  73. groups = {not_in_creative_inventory = 1},
  74. on_use = function(itemstack, user, pointed_thing)
  75. local pos = user:get_pos()
  76. local dir = user:get_look_dir()
  77. local yaw = user:get_look_yaw()
  78. if pos and dir and yaw then
  79. pos.y = pos.y + 1.6
  80. local obj = minetest.add_entity(pos, "rangedweapons:grenade")
  81. if obj then
  82. obj:set_velocity({x=dir.x * 12, y=dir.y * 12, z=dir.z * 12})
  83. obj:set_acceleration({x=0, y=-6, z=0})
  84. obj:set_yaw(yaw + math.pi)
  85. btimer = gtimer
  86. local ent = obj:get_luaentity()
  87. if ent then
  88. ent.player = ent.player or user
  89. itemstack = ""
  90. end
  91. end
  92. end
  93. return itemstack
  94. end,
  95. })
  96. tnt.register_tnt(grenade_boom)
  97. minetest.register_globalstep(function(dtime, player, pos)
  98. gtimer = gtimer + dtime;
  99. if gtimer >= 3.0 then
  100. for _, player in pairs(minetest.get_connected_players()) do
  101. local pos = player:get_pos()
  102. if player:get_wielded_item():get_name() == "rangedweapons:hand_grenade_nopin" then
  103. player:set_wielded_item("")
  104. gtimer = 0
  105. tnt.boom(pos, grenade_boom)
  106. end end end end)
  107. local rangedweapons_grenade = {
  108. physical = true,
  109. btimer = 0,
  110. timer = 0,
  111. hp_max = 420,
  112. visual = "sprite",
  113. visual_size = {x=0.5, y=0.5},
  114. textures = {"rangedweapons_hand_grenade_nopin.png"},
  115. lastpos= {},
  116. collisionbox = {-0.1, -0.1, -0.1, 0.1, 0.1, 0.1},
  117. }
  118. rangedweapons_grenade.on_step = function(self, dtime, pos)
  119. local pos = self.object:get_pos()
  120. local node = minetest.get_node(pos)
  121. local btimer = btimer or 0
  122. self.timer = self.timer + dtime
  123. if self.timer > (3.0 - btimer) then
  124. tnt.boom(pos, grenade_boom)
  125. self.object:remove()
  126. end
  127. self.lastpos= {x = pos.x, y = pos.y, z = pos.z}
  128. end
  129. minetest.register_entity("rangedweapons:grenade", rangedweapons_grenade)