init.lua 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. -- Realistic Torch mod by TenPlus1
  2. real_torch = {}
  3. -- Localize for performance.
  4. local math_random = math.random
  5. -- check for timer settings or use defaults
  6. -- torches stay lit for 6 - 7 hours of real time
  7. real_torch.min_duration = tonumber(minetest.settings:get("torch_min_duration")) or (60*60*6) --10--1200
  8. real_torch.max_duration = tonumber(minetest.settings:get("torch_min_duration")) or (60*60*7) --20--1800
  9. -- check which torch(es) are available in minetest version
  10. if minetest.registered_nodes["torches:torch_ceiling"] then
  11. dofile(minetest.get_modpath("real_torch") .. "/3d.lua")
  12. dofile(minetest.get_modpath("real_torch") .. "/3dk.lua")
  13. else
  14. dofile(minetest.get_modpath("real_torch") .. "/2d.lua")
  15. end
  16. -- start timer on any already placed torches
  17. --[[
  18. minetest.register_lbm({
  19. name = "real_torch:convert_torch_to_node_timer",
  20. nodenames = {"torches:torch_floor", "torches:torch_wall", "torches:torch_ceiling"},
  21. action = function(pos)
  22. if not minetest.get_node_timer(pos):is_started() then
  23. minetest.get_node_timer(pos):start(
  24. math_random(real_torch.min_duration, real_torch.max_duration))
  25. end
  26. end
  27. })
  28. minetest.register_lbm({
  29. name = "real_torch:convert_kalite_torch_to_node_timer",
  30. nodenames = {"torches:kalite_torch_floor", "torches:kalite_torch_wall", "torches:kalite_torch_ceiling"},
  31. action = function(pos)
  32. if not minetest.get_node_timer(pos):is_started() then
  33. minetest.get_node_timer(pos):start(
  34. math_random(real_torch.min_duration*5, real_torch.max_duration*6))
  35. end
  36. end
  37. })
  38. --]]
  39. function real_torch.start_timer(pos)
  40. minetest.get_node_timer(pos):start(math_random(real_torch.min_duration, real_torch.max_duration))
  41. end
  42. function real_torch.start_kalite_timer(pos)
  43. minetest.get_node_timer(pos):start(math_random(real_torch.min_duration*5, real_torch.max_duration*6))
  44. --minetest.get_node_timer(pos):start(math_random(5, 10))
  45. end
  46. -- creative check
  47. local creative_mode_cache = minetest.settings:get_bool("creative_mode")
  48. function is_creative(name)
  49. return creative_mode_cache or minetest.check_player_privs(name, {creative = true})
  50. end
  51. -- Note: this is also called by the tinderbox mod.
  52. function real_torch.relight(itemstack, user, pointed_thing)
  53. if not pointed_thing or pointed_thing.type ~= "node" then
  54. return
  55. end
  56. local pos = pointed_thing.under
  57. local nod = minetest.get_node(pos)
  58. local rep = false
  59. if nod.name == "real_torch:torch" then
  60. nod.name = "torches:torch_floor"
  61. rep = true
  62. elseif nod.name == "real_torch:torch_wall" then
  63. nod.name = "torches:torch_wall"
  64. rep = true
  65. elseif nod.name == "real_torch:torch_ceiling" then
  66. nod.name = "torches:torch_ceiling"
  67. rep = true
  68. elseif nod.name == "real_torch:kalite_torch" then
  69. nod.name = "torches:kalite_torch_floor"
  70. rep = true
  71. elseif nod.name == "real_torch:kalite_torch_wall" then
  72. nod.name = "torches:kalite_torch_wall"
  73. rep = true
  74. elseif nod.name == "real_torch:kalite_torch_ceiling" then
  75. nod.name = "torches:kalite_torch_ceiling"
  76. rep = true
  77. end
  78. if rep then
  79. minetest.add_node(pos, {name = nod.name, param2 = nod.param2})
  80. if not is_creative(user:get_player_name()) then
  81. itemstack:take_item()
  82. end
  83. end
  84. return itemstack
  85. end
  86. -- coal powder
  87. minetest.override_item("dusts:coal", {
  88. -- punching unlit torch with coal powder relights
  89. on_use = function(...)
  90. return real_torch.relight(...)
  91. end,
  92. })
  93. -- use coal powder as furnace fuel
  94. minetest.register_craft({
  95. type = "fuel",
  96. recipe = "dusts:coal",
  97. burntime = 1,
  98. })
  99. minetest.register_craft({
  100. type = "fuel",
  101. recipe = "real_torch:torch",
  102. burntime = 1,
  103. })
  104. minetest.register_craft({
  105. type = "fuel",
  106. recipe = "real_torch:kalite_torch",
  107. burntime = 1,
  108. })
  109. -- add coal powder to burnt out torch to relight
  110. minetest.register_craft({
  111. type = "shapeless",
  112. output = "torches:torch_floor",
  113. recipe = {"real_torch:torch", "dusts:coal"},
  114. })
  115. minetest.register_craft({
  116. type = "shapeless",
  117. output = "torches:kalite_torch_floor",
  118. recipe = {"real_torch:kalite_torch", "kalite:dust"},
  119. })
  120. -- 4x burnt out torches = 1x stick
  121. minetest.register_craft({
  122. type = "shapeless",
  123. output = "default:stick",
  124. recipe = {"real_torch:torch", "real_torch:torch", "real_torch:torch", "real_torch:torch"},
  125. })
  126. minetest.register_craft({
  127. type = "shapeless",
  128. output = "default:stick",
  129. recipe = {"real_torch:kalite_torch", "real_torch:kalite_torch", "real_torch:kalite_torch", "real_torch:kalite_torch"},
  130. })
  131. -- particle effects
  132. local function add_effects(pos, radius)
  133. minetest.add_particle({
  134. pos = pos,
  135. velocity = vector.new(),
  136. acceleration = vector.new(),
  137. expirationtime = 0.4,
  138. size = radius * 10,
  139. collisiondetection = false,
  140. vertical = false,
  141. texture = "tnt_boom.png",
  142. glow = 15,
  143. })
  144. minetest.add_particlespawner({
  145. amount = 16,
  146. time = 0.5,
  147. minpos = vector.subtract(pos, radius / 2),
  148. maxpos = vector.add(pos, radius / 2),
  149. minvel = {x = -2, y = -2, z = -2},
  150. maxvel = {x = 2, y = 2, z = 2},
  151. minacc = vector.new(),
  152. maxacc = vector.new(),
  153. minexptime = 1,
  154. maxexptime = 2.5,
  155. minsize = radius * 3,
  156. maxsize = radius * 5,
  157. texture = "tnt_smoke.png",
  158. })
  159. minetest.sound_play("tnt_explode", {pos = pos, gain = 0.1, max_hear_distance = 5})
  160. end
  161. -- override tnt:gunpowder to explode when used on torch,
  162. -- will also re-light torches and cause player damage when used on lit torch.
  163. if minetest.get_modpath("tnt") then
  164. minetest.override_item("tnt:gunpowder", {
  165. on_use = function(itemstack, user, pointed_thing)
  166. if not user or not user:is_player() then return end
  167. local pname = user:get_player_name() or ""
  168. if not pointed_thing or pointed_thing.type ~= "node" then
  169. return
  170. end
  171. local pos = pointed_thing.under
  172. local nod = minetest.get_node(pos)
  173. local rep = false
  174. if nod.name == "real_torch:torch" then
  175. nod.name = "torches:torch_floor"
  176. rep = true
  177. elseif nod.name == "real_torch:torch_wall" then
  178. nod.name = "torches:torch_wall"
  179. rep = true
  180. elseif nod.name == "real_torch:torch_ceiling" then
  181. nod.name = "torches:torch_ceiling"
  182. rep = true
  183. elseif nod.name == "real_torch:kalite_torch" then
  184. nod.name = "torches:kalite_torch_floor"
  185. rep = true
  186. elseif nod.name == "real_torch:kalite_torch_wall" then
  187. nod.name = "torches:kalite_torch_wall"
  188. rep = true
  189. elseif nod.name == "real_torch:kalite_torch_ceiling" then
  190. nod.name = "torches:kalite_torch_ceiling"
  191. rep = true
  192. end
  193. if rep then
  194. minetest.add_node(pos, {name = nod.name, param2 = nod.param2})
  195. add_effects(pos, 1)
  196. if not is_creative(user:get_player_name()) then
  197. itemstack:take_item()
  198. end
  199. return itemstack
  200. end
  201. if nod.name == "torches:torch_floor"
  202. or nod.name == "torches:torch_wall"
  203. or nod.name == "torches:torch_ceiling"
  204. or nod.name == "torches:kalite_torch_floor"
  205. or nod.name == "torches:kalite_torch_wall"
  206. or nod.name == "torches:kalite_torch_ceiling" then
  207. -- must execute outside callback!
  208. minetest.after(0, function()
  209. local user = minetest.get_player_by_name(pname)
  210. if not user or not user:is_player() then return end
  211. user:set_hp(user:get_hp() - 2)
  212. end)
  213. add_effects(pos, 1)
  214. if not is_creative(user:get_player_name()) then
  215. itemstack:take_item()
  216. end
  217. end
  218. return itemstack
  219. end,
  220. })
  221. end