init.lua 7.0 KB

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