init.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. tnt = {}
  2. -- Default to enabled when in singleplayer
  3. local enable_tnt = minetest.settings:get_bool("enable_tnt")
  4. if enable_tnt == nil then
  5. enable_tnt = minetest.is_singleplayer()
  6. end
  7. local tnt_radius = tonumber(minetest.settings:get("tnt_radius") or 3)
  8. dofile(minetest.get_modpath("tnt").."/functions.lua")
  9. minetest.register_node("tnt:gunpowder", {
  10. description = "Gun Powder",
  11. drawtype = "raillike",
  12. paramtype = "light",
  13. is_ground_content = false,
  14. sunlight_propagates = true,
  15. walkable = false,
  16. tiles = {
  17. "tnt_gunpowder_straight.png",
  18. "tnt_gunpowder_curved.png",
  19. "tnt_gunpowder_t_junction.png",
  20. "tnt_gunpowder_crossing.png"
  21. },
  22. inventory_image = "tnt_gunpowder_inventory.png",
  23. wield_image = "tnt_gunpowder_inventory.png",
  24. selection_box = {
  25. type = "fixed",
  26. fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
  27. },
  28. groups = {dig_immediate = 2, attached_node = 1, flammable = 5,
  29. connect_to_raillike = minetest.raillike_group("gunpowder")},
  30. sounds = default.node_sound_leaves_defaults(),
  31. on_punch = function(pos, node, puncher)
  32. if puncher:get_wielded_item():get_name() == "default:torch" then
  33. minetest.set_node(pos, {name = "tnt:gunpowder_burning"})
  34. minetest.log("action", puncher:get_player_name() ..
  35. " ignites tnt:gunpowder at " ..
  36. minetest.pos_to_string(pos))
  37. end
  38. end,
  39. on_blast = function(pos, intensity)
  40. minetest.set_node(pos, {name = "tnt:gunpowder_burning"})
  41. end,
  42. on_burn = function(pos)
  43. minetest.set_node(pos, {name = "tnt:gunpowder_burning"})
  44. end,
  45. on_ignite = function(pos, igniter)
  46. minetest.set_node(pos, {name = "tnt:gunpowder_burning"})
  47. end,
  48. })
  49. minetest.register_node("tnt:gunpowder_burning", {
  50. drawtype = "raillike",
  51. paramtype = "light",
  52. sunlight_propagates = true,
  53. walkable = false,
  54. light_source = 5,
  55. tiles = {{
  56. name = "tnt_gunpowder_burning_straight_animated.png",
  57. animation = {
  58. type = "vertical_frames",
  59. aspect_w = 16,
  60. aspect_h = 16,
  61. length = 1,
  62. }
  63. },
  64. {
  65. name = "tnt_gunpowder_burning_curved_animated.png",
  66. animation = {
  67. type = "vertical_frames",
  68. aspect_w = 16,
  69. aspect_h = 16,
  70. length = 1,
  71. }
  72. },
  73. {
  74. name = "tnt_gunpowder_burning_t_junction_animated.png",
  75. animation = {
  76. type = "vertical_frames",
  77. aspect_w = 16,
  78. aspect_h = 16,
  79. length = 1,
  80. }
  81. },
  82. {
  83. name = "tnt_gunpowder_burning_crossing_animated.png",
  84. animation = {
  85. type = "vertical_frames",
  86. aspect_w = 16,
  87. aspect_h = 16,
  88. length = 1,
  89. }
  90. }},
  91. selection_box = {
  92. type = "fixed",
  93. fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
  94. },
  95. drop = "",
  96. groups = {
  97. dig_immediate = 2,
  98. attached_node = 1,
  99. connect_to_raillike = minetest.raillike_group("gunpowder")
  100. },
  101. sounds = default.node_sound_leaves_defaults(),
  102. on_timer = function(pos, elapsed)
  103. for dx = -1, 1 do
  104. for dz = -1, 1 do
  105. if math.abs(dx) + math.abs(dz) == 1 then
  106. for dy = -1, 1 do
  107. tnt.burn({
  108. x = pos.x + dx,
  109. y = pos.y + dy,
  110. z = pos.z + dz,
  111. })
  112. end
  113. end
  114. end
  115. end
  116. minetest.remove_node(pos)
  117. end,
  118. -- unaffected by explosions
  119. on_blast = function() end,
  120. on_construct = function(pos)
  121. minetest.sound_play("tnt_gunpowder_burning", {pos = pos, gain = 2})
  122. minetest.get_node_timer(pos):start(1)
  123. end,
  124. })
  125. minetest.register_craft({
  126. output = "tnt:gunpowder 2",
  127. type = "shapeless",
  128. recipe = {"default:coal_lump", "default:gravel"}
  129. })
  130. minetest.register_craftitem("tnt:tnt_stick", {
  131. description = "TNT Stick",
  132. inventory_image = "tnt_tnt_stick.png",
  133. groups = {flammable = 5},
  134. })
  135. if enable_tnt then
  136. minetest.register_craft({
  137. output = "tnt:tnt_stick 3",
  138. recipe = {
  139. {"tnt:gunpowder", "", "tnt:gunpowder"},
  140. {"tnt:gunpowder", "default:paper", "tnt:gunpowder"},
  141. {"tnt:gunpowder", "", "tnt:gunpowder"},
  142. }
  143. })
  144. minetest.register_craft({
  145. output = "tnt:tnt",
  146. recipe = {
  147. {"tnt:tnt_stick", "tnt:tnt_stick", "tnt:tnt_stick"},
  148. {"tnt:tnt_stick", "tnt:tnt_stick", "tnt:tnt_stick"},
  149. {"tnt:tnt_stick", "tnt:tnt_stick", "tnt:tnt_stick"}
  150. }
  151. })
  152. minetest.register_abm({
  153. label = "TNT ignition",
  154. nodenames = {"group:tnt", "tnt:gunpowder"},
  155. neighbors = {"fire:basic_flame", "default:lava_source", "default:lava_flowing"},
  156. interval = 4,
  157. chance = 1,
  158. action = function(pos, node)
  159. tnt.burn(pos, node.name)
  160. end,
  161. })
  162. end
  163. function tnt.register_tnt(def)
  164. local name
  165. if not def.name:find(':') then
  166. name = "tnt:" .. def.name
  167. else
  168. name = def.name
  169. def.name = def.name:match(":([%w_]+)")
  170. end
  171. if not def.tiles then def.tiles = {} end
  172. local tnt_top = def.tiles.top or def.name .. "_top.png"
  173. local tnt_bottom = def.tiles.bottom or def.name .. "_bottom.png"
  174. local tnt_side = def.tiles.side or def.name .. "_side.png"
  175. local tnt_burning = def.tiles.burning or def.name .. "_top_burning_animated.png"
  176. if not def.damage_radius then def.damage_radius = def.radius * 2 end
  177. if enable_tnt then
  178. minetest.register_node(":" .. name, {
  179. description = def.description,
  180. tiles = {tnt_top, tnt_bottom, tnt_side},
  181. is_ground_content = false,
  182. groups = {dig_immediate = 2, mesecon = 2, tnt = 1, flammable = 5},
  183. sounds = default.node_sound_wood_defaults(),
  184. after_place_node = function(pos, placer)
  185. if placer:is_player() then
  186. local meta = minetest.get_meta(pos)
  187. meta:set_string("owner", placer:get_player_name())
  188. end
  189. end,
  190. on_punch = function(pos, node, puncher)
  191. if puncher:get_wielded_item():get_name() == "default:torch" then
  192. minetest.swap_node(pos, {name = name .. "_burning"})
  193. minetest.registered_nodes[name .. "_burning"].on_construct(pos)
  194. minetest.log("action", puncher:get_player_name() ..
  195. " ignites " .. node.name .. " at " ..
  196. minetest.pos_to_string(pos))
  197. end
  198. end,
  199. on_blast = function(pos, intensity)
  200. minetest.after(0.1, function()
  201. tnt.boom(pos, def)
  202. end)
  203. end,
  204. mesecons = {effector =
  205. {action_on =
  206. function(pos)
  207. tnt.boom(pos, def)
  208. end
  209. }
  210. },
  211. on_burn = function(pos)
  212. minetest.swap_node(pos, {name = name .. "_burning"})
  213. minetest.registered_nodes[name .. "_burning"].on_construct(pos)
  214. end,
  215. on_ignite = function(pos, igniter)
  216. minetest.swap_node(pos, {name = name .. "_burning"})
  217. minetest.registered_nodes[name .. "_burning"].on_construct(pos)
  218. end,
  219. })
  220. end
  221. minetest.register_node(":" .. name .. "_burning", {
  222. tiles = {
  223. {
  224. name = tnt_burning,
  225. animation = {
  226. type = "vertical_frames",
  227. aspect_w = 16,
  228. aspect_h = 16,
  229. length = 1,
  230. }
  231. },
  232. tnt_bottom, tnt_side
  233. },
  234. light_source = 5,
  235. drop = "",
  236. sounds = default.node_sound_wood_defaults(),
  237. groups = {falling_node = 1},
  238. on_timer = function(pos, elapsed)
  239. tnt.boom(pos, def)
  240. end,
  241. -- unaffected by explosions
  242. on_blast = function() end,
  243. on_construct = function(pos)
  244. minetest.sound_play("tnt_ignite", {pos = pos})
  245. minetest.get_node_timer(pos):start(4)
  246. minetest.check_for_falling(pos)
  247. end,
  248. })
  249. end
  250. tnt.register_tnt({
  251. name = "tnt:tnt",
  252. description = "TNT",
  253. radius = tnt_radius,
  254. disable_drops = true,
  255. })