init.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. --[[
  2. Copyright (C) 2016 - Auke Kok <sofar@foo-projects.org>
  3. "lightning" is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1
  6. of the license, or (at your option) any later version.
  7. --]]
  8. lightning = {}
  9. lightning.interval_low = 17
  10. lightning.interval_high = 503
  11. lightning.range_h = 100
  12. lightning.range_v = 50
  13. lightning.size = 100
  14. -- disable this to stop lightning mod from striking
  15. lightning.auto = true
  16. local rng = PcgRandom(32321123312123)
  17. local ps = {}
  18. local ttl = 1
  19. lightning.revertsky = function()
  20. if ttl == 0 then
  21. return
  22. end
  23. ttl = ttl - 1
  24. if ttl > 0 then
  25. return
  26. end
  27. for key, entry in pairs(ps) do
  28. local sky = entry.sky
  29. entry.p:set_sky(sky.bgcolor, sky.type, sky.textures)
  30. end
  31. ps = {}
  32. end
  33. minetest.register_globalstep(lightning.revertsky)
  34. -- select a random strike point, midpoint
  35. local function choose_pos(pos)
  36. if not pos then
  37. local playerlist = minetest.get_connected_players()
  38. local playercount = table.getn(playerlist)
  39. -- nobody on
  40. if playercount == 0 then
  41. return nil, nil
  42. end
  43. local r = rng:next(1, playercount)
  44. local randomplayer = playerlist[r]
  45. pos = randomplayer:getpos()
  46. -- avoid striking underground
  47. if pos.y < -20 then
  48. return nil, nil
  49. end
  50. pos.x = math.floor(pos.x - (lightning.range_h / 2) + rng:next(1, lightning.range_h))
  51. pos.y = pos.y + (lightning.range_v / 2)
  52. pos.z = math.floor(pos.z - (lightning.range_h / 2) + rng:next(1, lightning.range_h))
  53. end
  54. local b, pos2 = minetest.line_of_sight(pos, {x = pos.x, y = pos.y - lightning.range_v, z = pos.z}, 1)
  55. -- nothing but air found
  56. if b then
  57. return nil, nil
  58. end
  59. local n = minetest.get_node({x = pos2.x, y = pos2.y - 1/2, z = pos2.z})
  60. if n.name == "air" or n.name == "ignore" then
  61. return nil, nil
  62. end
  63. return pos, pos2
  64. end
  65. -- lightning strike API
  66. -- * pos: optional, if not given a random pos will be chosen
  67. -- * returns: bool - success if a strike happened
  68. lightning.strike = function(pos)
  69. if lightning.auto then
  70. minetest.after(rng:next(lightning.interval_low, lightning.interval_high), lightning.strike)
  71. end
  72. local pos2
  73. pos, pos2 = choose_pos(pos)
  74. if not pos then
  75. return false
  76. end
  77. minetest.add_particlespawner({
  78. amount = 1,
  79. time = 0.2,
  80. -- make it hit the top of a block exactly with the bottom
  81. minpos = {x = pos2.x, y = pos2.y + (lightning.size / 2) + 1/2, z = pos2.z },
  82. maxpos = {x = pos2.x, y = pos2.y + (lightning.size / 2) + 1/2, z = pos2.z },
  83. minvel = {x = 0, y = 0, z = 0},
  84. maxvel = {x = 0, y = 0, z = 0},
  85. minacc = {x = 0, y = 0, z = 0},
  86. maxacc = {x = 0, y = 0, z = 0},
  87. minexptime = 0.2,
  88. maxexptime = 0.2,
  89. minsize = lightning.size * 10,
  90. maxsize = lightning.size * 10,
  91. collisiondetection = true,
  92. vertical = true,
  93. -- to make it appear hitting the node that will get set on fire, make sure
  94. -- to make the texture lightning bolt hit exactly in the middle of the
  95. -- texture (e.g. 127/128 on a 256x wide texture)
  96. texture = "lightning_lightning_" .. rng:next(1,3) .. ".png",
  97. -- 0.4.15+
  98. glow = 14,
  99. })
  100. minetest.sound_play({ pos = pos, name = "lightning_thunder", gain = 10, max_hear_distance = 500 })
  101. -- damage nearby objects, player or not
  102. for _, obj in ipairs(minetest.get_objects_inside_radius(pos, 5)) do
  103. -- nil as param#1 is supposed to work, but core can't handle it.
  104. obj:punch(obj, 1.0, {full_punch_interval = 1.0, damage_groups = {fleshy=8}}, nil)
  105. end
  106. local playerlist = minetest.get_connected_players()
  107. for i = 1, #playerlist do
  108. local player = playerlist[i]
  109. local sky = {}
  110. sky.bgcolor, sky.type, sky.textures = player:get_sky()
  111. local name = player:get_player_name()
  112. if ps[name] == nil then
  113. ps[name] = {p = player, sky = sky}
  114. player:set_sky(0xffffff, "plain", {})
  115. end
  116. end
  117. -- trigger revert of skybox
  118. ttl = 5
  119. -- set the air node above it on fire
  120. pos2.y = pos2.y + 1/2
  121. if minetest.get_item_group(minetest.get_node({x = pos2.x, y = pos2.y - 1, z = pos2.z}).name, "liquid") < 1 then
  122. if minetest.get_node(pos2).name == "air" then
  123. -- only 1/4 of the time, something is changed
  124. if rng:next(1,4) > 1 then
  125. return
  126. end
  127. -- very rarely, potentially cause a fire
  128. if fire and rng:next(1,1000) == 1 then
  129. minetest.set_node(pos2, {name = "fire:basic_flame"})
  130. else
  131. minetest.set_node(pos2, {name = "lightning:dying_flame"})
  132. end
  133. end
  134. end
  135. -- perform block modifications
  136. if not default or rng:next(1,10) > 1 then
  137. return
  138. end
  139. pos2.y = pos2.y - 1
  140. local n = minetest.get_node(pos2)
  141. if minetest.get_item_group(n.name, "tree") > 0 then
  142. minetest.set_node(pos2, { name = "default:coalblock"})
  143. elseif minetest.get_item_group(n.name, "sand") > 0 then
  144. minetest.set_node(pos2, { name = "default:glass"})
  145. elseif minetest.get_item_group(n.name, "soil") > 0 then
  146. minetest.set_node(pos2, { name = "default:gravel"})
  147. end
  148. end
  149. -- a special fire node that doesn't burn anything, and automatically disappears
  150. minetest.register_node("lightning:dying_flame", {
  151. description = "Dying Flame",
  152. drawtype = "firelike",
  153. tiles = {
  154. {
  155. name = "fire_basic_flame_animated.png",
  156. animation = {
  157. type = "vertical_frames",
  158. aspect_w = 16,
  159. aspect_h = 16,
  160. length = 1
  161. },
  162. },
  163. },
  164. inventory_image = "fire_basic_flame.png",
  165. paramtype = "light",
  166. light_source = 14,
  167. walkable = false,
  168. buildable_to = true,
  169. sunlight_propagates = true,
  170. damage_per_second = 4,
  171. groups = {dig_immediate = 3, not_in_creative_inventory=1},
  172. on_timer = function(pos)
  173. minetest.remove_node(pos)
  174. end,
  175. drop = "",
  176. on_construct = function(pos)
  177. minetest.get_node_timer(pos):start(rng:next(20, 40))
  178. if fire and fire.on_flame_add_at then
  179. minetest.after(0.5, fire.on_flame_add_at, pos)
  180. end
  181. end,
  182. })
  183. -- if other mods disable auto lightning during initialization, don't trigger the first lightning.
  184. minetest.after(5, function(dtime)
  185. if lightning.auto then
  186. minetest.after(rng:next(lightning.interval_low,
  187. lightning.interval_high), lightning.strike)
  188. end
  189. end)