init.lua 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. -- Note: this is also called by the tinderbox mod.
  47. function real_torch.relight(itemstack, user, pointed_thing)
  48. if not pointed_thing or pointed_thing.type ~= "node" then
  49. return
  50. end
  51. --minetest.chat_send_all('test2.1')
  52. local pos = pointed_thing.under
  53. local nod = minetest.get_node(pos)
  54. local rep = false
  55. if nod.name == "real_torch:torch" then
  56. nod.name = "torches:torch_floor"
  57. rep = true
  58. elseif nod.name == "real_torch:torch_wall" then
  59. nod.name = "torches:torch_wall"
  60. rep = true
  61. elseif nod.name == "real_torch:torch_ceiling" then
  62. nod.name = "torches:torch_ceiling"
  63. rep = true
  64. elseif nod.name == "real_torch:kalite_torch" then
  65. nod.name = "torches:kalite_torch_floor"
  66. rep = true
  67. elseif nod.name == "real_torch:kalite_torch_wall" then
  68. nod.name = "torches:kalite_torch_wall"
  69. rep = true
  70. elseif nod.name == "real_torch:kalite_torch_ceiling" then
  71. nod.name = "torches:kalite_torch_ceiling"
  72. rep = true
  73. end
  74. --minetest.chat_send_all('test2.2')
  75. if rep then
  76. minetest.add_node(pos, {name = nod.name, param2 = nod.param2})
  77. minetest.sound_play("real_torch_light", {pos = pos, gain = 0.1, max_hear_distance = 16}, true)
  78. itemstack:take_item()
  79. end
  80. --minetest.chat_send_all('test2.3')
  81. return itemstack
  82. end
  83. -- coal powder
  84. minetest.override_item("dusts:coal", {
  85. -- punching unlit torch with coal powder relights
  86. on_use = function(...)
  87. return real_torch.relight(...)
  88. end,
  89. })
  90. -- use coal powder as furnace fuel
  91. minetest.register_craft({
  92. type = "fuel",
  93. recipe = "dusts:coal",
  94. burntime = 1,
  95. })
  96. minetest.register_craft({
  97. type = "fuel",
  98. recipe = "real_torch:torch",
  99. burntime = 1,
  100. })
  101. minetest.register_craft({
  102. type = "fuel",
  103. recipe = "real_torch:kalite_torch",
  104. burntime = 1,
  105. })
  106. -- add coal powder to burnt out torch to relight
  107. minetest.register_craft({
  108. type = "shapeless",
  109. output = "torches:torch_floor",
  110. recipe = {"real_torch:torch", "dusts:coal"},
  111. })
  112. minetest.register_craft({
  113. type = "shapeless",
  114. output = "torches:kalite_torch_floor",
  115. recipe = {"real_torch:kalite_torch", "kalite:dust"},
  116. })
  117. -- 4x burnt out torches = 1x stick
  118. minetest.register_craft({
  119. type = "shapeless",
  120. output = "default:stick",
  121. recipe = {"real_torch:torch", "real_torch:torch", "real_torch:torch", "real_torch:torch"},
  122. })
  123. minetest.register_craft({
  124. type = "shapeless",
  125. output = "default:stick",
  126. recipe = {"real_torch:kalite_torch", "real_torch:kalite_torch", "real_torch:kalite_torch", "real_torch:kalite_torch"},
  127. })
  128. -- particle effects
  129. local function add_effects(pos, radius)
  130. minetest.add_particle({
  131. pos = pos,
  132. velocity = vector.new(),
  133. acceleration = vector.new(),
  134. expirationtime = 0.4,
  135. size = radius * 10,
  136. collisiondetection = false,
  137. vertical = false,
  138. texture = "tnt_boom.png",
  139. glow = 15,
  140. })
  141. minetest.add_particlespawner({
  142. amount = 16,
  143. time = 0.5,
  144. minpos = vector.subtract(pos, radius / 2),
  145. maxpos = vector.add(pos, radius / 2),
  146. minvel = {x = -2, y = -2, z = -2},
  147. maxvel = {x = 2, y = 2, z = 2},
  148. minacc = vector.new(),
  149. maxacc = vector.new(),
  150. minexptime = 1,
  151. maxexptime = 2.5,
  152. minsize = radius * 3,
  153. maxsize = radius * 5,
  154. texture = "tnt_smoke.png",
  155. })
  156. minetest.sound_play("tnt_explode", {pos = pos, gain = 0.1, max_hear_distance = 5}, true)
  157. end
  158. -- override tnt:gunpowder to explode when used on torch,
  159. -- will also re-light torches and cause player damage when used on lit torch.
  160. if minetest.get_modpath("tnt") then
  161. minetest.override_item("tnt:gunpowder", {
  162. on_use = function(itemstack, user, pointed_thing)
  163. if not user or not user:is_player() then return end
  164. local pname = user:get_player_name() or ""
  165. if not pointed_thing or pointed_thing.type ~= "node" then
  166. return
  167. end
  168. local pos = pointed_thing.under
  169. local nod = minetest.get_node(pos)
  170. local rep = false
  171. if nod.name == "real_torch:torch" then
  172. nod.name = "torches:torch_floor"
  173. rep = true
  174. elseif nod.name == "real_torch:torch_wall" then
  175. nod.name = "torches:torch_wall"
  176. rep = true
  177. elseif nod.name == "real_torch:torch_ceiling" then
  178. nod.name = "torches:torch_ceiling"
  179. rep = true
  180. elseif nod.name == "real_torch:kalite_torch" then
  181. nod.name = "torches:kalite_torch_floor"
  182. rep = true
  183. elseif nod.name == "real_torch:kalite_torch_wall" then
  184. nod.name = "torches:kalite_torch_wall"
  185. rep = true
  186. elseif nod.name == "real_torch:kalite_torch_ceiling" then
  187. nod.name = "torches:kalite_torch_ceiling"
  188. rep = true
  189. end
  190. if rep then
  191. minetest.add_node(pos, {name = nod.name, param2 = nod.param2})
  192. add_effects(pos, 1)
  193. itemstack:take_item()
  194. return itemstack
  195. end
  196. if nod.name == "torches:torch_floor"
  197. or nod.name == "torches:torch_wall"
  198. or nod.name == "torches:torch_ceiling"
  199. or nod.name == "torches:kalite_torch_floor"
  200. or nod.name == "torches:kalite_torch_wall"
  201. or nod.name == "torches:kalite_torch_ceiling" then
  202. -- must execute outside callback!
  203. minetest.after(0, function()
  204. local user = minetest.get_player_by_name(pname)
  205. if not user or not user:is_player() then return end
  206. utility.damage_player(user, "heat", 2*500)
  207. end)
  208. add_effects(pos, 1)
  209. itemstack:take_item()
  210. end
  211. return itemstack
  212. end,
  213. })
  214. end