init.lua 7.0 KB

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