init.lua 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. tnt = {}
  2. local enable_tnt = true
  3. local tnt_radius = 3
  4. -- Fill a list with data for content IDs, after all nodes are registered
  5. function tnt.burn(pos)
  6. local name = minetest.get_node(pos).name
  7. local group = minetest.get_item_group(name, "tnt")
  8. if group > 0 then
  9. minetest.sound_play("tnt_ignite", {pos = pos})
  10. minetest.add_node(pos, {name = name .. "_burning"})
  11. minetest.get_node_timer(pos):start(1)
  12. elseif name == "tnt:gunpowder" then
  13. minetest.add_node(pos, {name = "tnt:gunpowder_burning"})
  14. end
  15. end
  16. reload.register_file("tnt:boom", minetest.get_modpath("tnt") .. "/tnt_boom.lua")
  17. reload.register_file("tnt:cdp", minetest.get_modpath("tnt") .. "/cdp.lua")
  18. minetest.register_node("tnt:boom", {
  19. drawtype = "airlike",
  20. light_source = default.LIGHT_MAX - 1,
  21. walkable = false,
  22. drop = "",
  23. groups = utility.dig_groups("item"),
  24. pointable = false,
  25. buildable_to = true,
  26. on_construct = function(pos)
  27. minetest.get_node_timer(pos):start(0.5)
  28. end,
  29. on_timer = function(pos, elapsed)
  30. minetest.remove_node(pos)
  31. end,
  32. -- TNT chain-reactions cause slowness problems for the server.
  33. on_blast = function(pos)
  34. minetest.remove_node(pos)
  35. end,
  36. })
  37. -- overriden to add additional functionality in real_torch
  38. minetest.register_node("tnt:gunpowder", {
  39. description = "Gun Powder",
  40. drawtype = "raillike",
  41. paramtype = "light",
  42. is_ground_content = false,
  43. sunlight_propagates = true,
  44. walkable = false,
  45. tiles = {
  46. "tnt_gunpowder_straight.png",
  47. "tnt_gunpowder_curved.png",
  48. "tnt_gunpowder_t_junction.png",
  49. "tnt_gunpowder_crossing.png",
  50. },
  51. inventory_image = "tnt_gunpowder_inventory.png",
  52. wield_image = "tnt_gunpowder_inventory.png",
  53. selection_box = {
  54. type = "fixed",
  55. fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
  56. },
  57. groups = utility.dig_groups("bigitem", {
  58. attached_node = 1,
  59. connect_to_raillike = minetest.raillike_group("gunpowder"),
  60. }),
  61. sounds = default.node_sound_leaves_defaults(),
  62. on_punch = function(pos, node, puncher)
  63. local wielded = puncher:get_wielded_item():get_name()
  64. if wielded == "torches:torch_floor" or
  65. wielded == "torches:iron_torch" or
  66. wielded == "torches:kalite_torch_floor" then
  67. tnt.burn(pos)
  68. minetest.log("action", puncher:get_player_name() ..
  69. " ignites tnt:gunpowder at " ..
  70. minetest.pos_to_string(pos))
  71. end
  72. end,
  73. -- Chain-reactions cause slowness problems for the server.
  74. on_blast = function(pos)
  75. minetest.remove_node(pos)
  76. end,
  77. })
  78. minetest.register_node("tnt:gunpowder_burning", {
  79. drawtype = "raillike",
  80. paramtype = "light",
  81. sunlight_propagates = true,
  82. walkable = false,
  83. light_source = 5,
  84. tiles = {{
  85. name = "tnt_gunpowder_burning_straight_animated.png",
  86. animation = {
  87. type = "vertical_frames",
  88. aspect_w = 16,
  89. aspect_h = 16,
  90. length = 1,
  91. }
  92. },
  93. {
  94. name = "tnt_gunpowder_burning_curved_animated.png",
  95. animation = {
  96. type = "vertical_frames",
  97. aspect_w = 16,
  98. aspect_h = 16,
  99. length = 1,
  100. }
  101. },
  102. {
  103. name = "tnt_gunpowder_burning_t_junction_animated.png",
  104. animation = {
  105. type = "vertical_frames",
  106. aspect_w = 16,
  107. aspect_h = 16,
  108. length = 1,
  109. }
  110. },
  111. {
  112. name = "tnt_gunpowder_burning_crossing_animated.png",
  113. animation = {
  114. type = "vertical_frames",
  115. aspect_w = 16,
  116. aspect_h = 16,
  117. length = 1,
  118. }
  119. }},
  120. selection_box = {
  121. type = "fixed",
  122. fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
  123. },
  124. drop = "",
  125. groups = utility.dig_groups("bigitem", {attached_node = 1, connect_to_raillike = minetest.raillike_group("gunpowder")}),
  126. sounds = default.node_sound_leaves_defaults(),
  127. on_timer = function(pos, elapsed)
  128. for dx = -1, 1 do
  129. for dz = -1, 1 do
  130. for dy = -1, 1 do
  131. if not (dx == 0 and dz == 0) then
  132. tnt.burn({
  133. x = pos.x + dx,
  134. y = pos.y + dy,
  135. z = pos.z + dz,
  136. })
  137. end
  138. end
  139. end
  140. end
  141. minetest.remove_node(pos)
  142. end,
  143. -- TNT chain-reactions cause slowness problems for the server.
  144. on_blast = function(pos)
  145. minetest.remove_node(pos)
  146. end,
  147. on_construct = function(pos)
  148. minetest.sound_play("tnt_gunpowder_burning", {pos = pos, gain = 2})
  149. minetest.get_node_timer(pos):start(1)
  150. end,
  151. })
  152. minetest.register_craft({
  153. output = "tnt:gunpowder 6",
  154. type = "shapeless",
  155. recipe = {"default:coal_lump", "default:gravel", "sulfur:dust"}
  156. })
  157. if enable_tnt then
  158. minetest.register_craft({
  159. output = "tnt:tnt",
  160. recipe = {
  161. {"group:wood", "tnt:gunpowder", "group:wood"},
  162. {"tnt:gunpowder", "tnt:gunpowder", "tnt:gunpowder"},
  163. {"group:wood", "tnt:gunpowder", "group:wood"},
  164. }
  165. })
  166. minetest.register_abm({
  167. label = "TNT ignition",
  168. nodenames = {"group:tnt", "tnt:gunpowder"},
  169. neighbors = {"group:igniter"},
  170. interval = 4 * default.ABM_TIMER_MULTIPLIER,
  171. chance = 1 * default.ABM_CHANCE_MULTIPLIER,
  172. action = tnt.burn,
  173. })
  174. end
  175. minetest.register_privilege("tnt",
  176. {description="Player can place TNT anywhere.", give_to_singleplayer=false})
  177. function tnt.register_tnt(def)
  178. local name
  179. if not def.name:find(':') then
  180. name = "tnt:" .. def.name
  181. else
  182. name = def.name
  183. def.name = def.name:match(":([%w_]+)")
  184. end
  185. if not def.tiles then def.tiles = {} end
  186. local tnt_top = def.tiles.top or def.name .. "_top.png"
  187. local tnt_bottom = def.tiles.bottom or def.name .. "_bottom.png"
  188. local tnt_side = def.tiles.side or def.name .. "_side.png"
  189. local tnt_burning = def.tiles.burning or def.name .. "_top_burning_animated.png"
  190. if not def.damage_radius then def.damage_radius = def.radius * 2 end
  191. minetest.register_node(":" .. name, {
  192. description = def.description,
  193. tiles = {tnt_top, tnt_bottom, tnt_side},
  194. is_ground_content = false,
  195. groups = utility.dig_groups("bigitem", {tnt = 1}),
  196. sounds = default.node_sound_wood_defaults(),
  197. on_punch = function(pos, node, puncher)
  198. local wielded = puncher:get_wielded_item():get_name()
  199. if wielded == "torches:torch_floor" or
  200. wielded == "torches:iron_torch" or
  201. wielded == "torches:kalite_torch_floor" then
  202. minetest.add_node(pos, {name = name .. "_burning"})
  203. minetest.log("action", puncher:get_player_name() ..
  204. " ignites " .. node.name .. " at " ..
  205. minetest.pos_to_string(pos))
  206. end
  207. end,
  208. -- TNT chain-reactions cause slowness problems for the server.
  209. on_blast = function(pos)
  210. minetest.remove_node(pos)
  211. end,
  212. on_place = function(itemstack, placer, pointed_thing)
  213. local pos = pointed_thing.under
  214. if not placer then return end
  215. if not placer:is_player() then return end
  216. local pname = placer:get_player_name()
  217. -- Players without the TNT priv go through checks.
  218. if not minetest.check_player_privs(placer, {tnt=true}) then
  219. if (pos.y > -100 and pos.y < 1000) then
  220. minetest.chat_send_player(pname, "# Server: Use of TNT near the Overworld's ground level is forbidden.")
  221. return itemstack
  222. end
  223. if city_block:in_no_tnt_zone(pos) then
  224. minetest.chat_send_player(pname, "# Server: Too close to a residential zone for blasting!")
  225. return itemstack
  226. end
  227. end
  228. -- All checks passed.
  229. return minetest.item_place(itemstack, placer, pointed_thing)
  230. end,
  231. })
  232. minetest.register_node(":" .. name .. "_burning", {
  233. tiles = {
  234. {
  235. name = tnt_burning,
  236. animation = {
  237. type = "vertical_frames",
  238. aspect_w = 16,
  239. aspect_h = 16,
  240. length = 1,
  241. }
  242. },
  243. tnt_bottom, tnt_side
  244. },
  245. light_source = 5,
  246. drop = "",
  247. sounds = default.node_sound_wood_defaults(),
  248. groups = {falling_node = 1},
  249. on_timer = function(pos, elapsed)
  250. tnt.boom(pos, def)
  251. end,
  252. -- No chain-reactions.
  253. on_blast = function(pos)
  254. minetest.remove_node(pos)
  255. end,
  256. on_construct = function(pos)
  257. minetest.sound_play("tnt_ignite", {pos = pos})
  258. minetest.get_node_timer(pos):start(5)
  259. minetest.check_for_falling(pos)
  260. end,
  261. })
  262. end
  263. tnt.register_tnt({
  264. name = "tnt:tnt",
  265. description = "Explosive TNT",
  266. radius = tnt_radius,
  267. })