init.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. flameportal = flameportal or {}
  2. flameportal.modpath = minetest.get_modpath("flameportal")
  3. -- Localize for performance.
  4. local vector_round = vector.round
  5. local math_random = math.random
  6. -- Check if the position `pos` is the center of a portal ring.
  7. flameportal.check_is_gateway = function(pos)
  8. local p = vector_round(pos)
  9. local positions = {
  10. -- North side.
  11. {x=p.x-1, y=p.y, z=p.z+2},
  12. {x=p.x, y=p.y, z=p.z+2},
  13. {x=p.x+1, y=p.y, z=p.z+2},
  14. -- South side.
  15. {x=p.x-1, y=p.y, z=p.z-2},
  16. {x=p.x, y=p.y, z=p.z-2},
  17. {x=p.x+1, y=p.y, z=p.z-2},
  18. -- West side.
  19. {x=p.x-2, y=p.y, z=p.z+1},
  20. {x=p.x-2, y=p.y, z=p.z },
  21. {x=p.x-2, y=p.y, z=p.z-1},
  22. -- East side.
  23. {x=p.x+2, y=p.y, z=p.z+1},
  24. {x=p.x+2, y=p.y, z=p.z },
  25. {x=p.x+2, y=p.y, z=p.z-1},
  26. }
  27. for k, v in ipairs(positions) do
  28. local n1 = minetest.get_node(v).name
  29. if n1 ~= "default:obsidian" then return false end
  30. end
  31. local pos_air = {
  32. {x=p.x, y=p.y, z=p.z },
  33. {x=p.x, y=p.y, z=p.z+1},
  34. {x=p.x, y=p.y, z=p.z-1},
  35. {x=p.x+1, y=p.y, z=p.z },
  36. {x=p.x-1, y=p.y, z=p.z },
  37. {x=p.x+1, y=p.y, z=p.z+1},
  38. {x=p.x+1, y=p.y, z=p.z-1},
  39. {x=p.x-1, y=p.y, z=p.z+1},
  40. {x=p.x-1, y=p.y, z=p.z-1},
  41. }
  42. for k, v in ipairs(pos_air) do
  43. local n1 = minetest.get_node(v).name
  44. if n1 ~= "air" then return false end
  45. end
  46. return true
  47. end
  48. -- Attempt to find a gateway using a brute-force method, starting at position `pos`.
  49. -- Note: the gateway can only be found if `pos` is the position of one of the obsidian blocks at the top.
  50. flameportal.find_gateway = function(pos)
  51. local p = {x=pos.x, y=pos.y, z=pos.z}
  52. local positions = {
  53. -- North side.
  54. {x=p.x-2, y=p.y, z=p.z+2},
  55. {x=p.x-1, y=p.y, z=p.z+2},
  56. {x=p.x, y=p.y, z=p.z+2},
  57. {x=p.x+1, y=p.y, z=p.z+2},
  58. {x=p.x+2, y=p.y, z=p.z+2},
  59. -- South side.
  60. {x=p.x-2, y=p.y, z=p.z-2},
  61. {x=p.x-1, y=p.y, z=p.z-2},
  62. {x=p.x, y=p.y, z=p.z-2},
  63. {x=p.x+1, y=p.y, z=p.z-2},
  64. {x=p.x+2, y=p.y, z=p.z-2},
  65. -- West side.
  66. {x=p.x-2, y=p.y, z=p.z+2},
  67. {x=p.x-2, y=p.y, z=p.z+1},
  68. {x=p.x-2, y=p.y, z=p.z },
  69. {x=p.x-2, y=p.y, z=p.z-1},
  70. {x=p.x-2, y=p.y, z=p.z-2},
  71. -- East side.
  72. {x=p.x+2, y=p.y, z=p.z+2},
  73. {x=p.x+2, y=p.y, z=p.z+1},
  74. {x=p.x+2, y=p.y, z=p.z },
  75. {x=p.x+2, y=p.y, z=p.z-1},
  76. {x=p.x+2, y=p.y, z=p.z-2},
  77. }
  78. for k, v in ipairs(positions) do
  79. if flameportal.check_is_gateway(v) then return true, v end
  80. end
  81. return false, nil
  82. end
  83. -- Attempt to activate a gateway at the given position.
  84. flameportal.activate_gateway = function(pos)
  85. if flameportal.check_is_gateway(pos) == false then return end
  86. local p = vector_round(pos)
  87. minetest.log("action", "Nether portal activated at (" .. minetest.pos_to_string(p) .. ")")
  88. local flames = {
  89. -- North side.
  90. {x=p.x-1, y=p.y+1, z=p.z+2},
  91. {x=p.x, y=p.y+1, z=p.z+2},
  92. {x=p.x+1, y=p.y+1, z=p.z+2},
  93. -- South side.
  94. {x=p.x-1, y=p.y+1, z=p.z-2},
  95. {x=p.x, y=p.y+1, z=p.z-2},
  96. {x=p.x+1, y=p.y+1, z=p.z-2},
  97. -- West side.
  98. {x=p.x-2, y=p.y+1, z=p.z+1},
  99. {x=p.x-2, y=p.y+1, z=p.z },
  100. {x=p.x-2, y=p.y+1, z=p.z-1},
  101. -- East side.
  102. {x=p.x+2, y=p.y+1, z=p.z+1},
  103. {x=p.x+2, y=p.y+1, z=p.z },
  104. {x=p.x+2, y=p.y+1, z=p.z-1},
  105. }
  106. for k, v in ipairs(flames) do
  107. if math_random(1, 3) == 1 then
  108. if minetest.get_node(v).name == "air" then
  109. minetest.add_node(v, {name="fire:nether_flame"})
  110. end
  111. end
  112. end
  113. local void = {
  114. {x=p.x, y=p.y, z=p.z },
  115. {x=p.x, y=p.y, z=p.z+1},
  116. {x=p.x, y=p.y, z=p.z-1},
  117. {x=p.x+1, y=p.y, z=p.z },
  118. {x=p.x-1, y=p.y, z=p.z },
  119. {x=p.x-1, y=p.y, z=p.z-1},
  120. {x=p.x+1, y=p.y, z=p.z-1},
  121. {x=p.x-1, y=p.y, z=p.z+1},
  122. {x=p.x+1, y=p.y, z=p.z+1},
  123. }
  124. for k, v in ipairs(void) do
  125. minetest.add_node(v, {name="voidstone:void"})
  126. end
  127. end
  128. flameportal.make_platform = function(param)
  129. for x = param.minp.x, param.maxp.x, 1 do
  130. for y = param.minp.y, param.maxp.y, 1 do
  131. for z = param.minp.z, param.maxp.z, 1 do
  132. local pos = {x=x, y=y, z=z}
  133. local node = minetest.get_node(pos)
  134. if node.name == "air" or node.name == "rackstone:redrack" then
  135. if not minetest.test_protection(pos, "") then
  136. minetest.add_node(pos, {name="rackstone:redrack"})
  137. if vector.equals(pos, param.top) then
  138. minetest.add_node(pos, {name="flameportal:redrack"})
  139. end
  140. end
  141. end
  142. end
  143. end
  144. end
  145. end
  146. flameportal.make_flame_pillar = function(param)
  147. for x = param.minp.x, param.maxp.x, 1 do
  148. for y = param.minp.y, param.maxp.y, 1 do
  149. for z = param.minp.z, param.maxp.z, 1 do
  150. if math_random(1, 7) == 1 then
  151. -- Leave a vertical tunnel clear for the player to drop down.
  152. if not (x == param.avoid_x and z == param.avoid_z) then
  153. local pos = {x=x, y=y, z=z}
  154. local node = minetest.get_node(pos)
  155. if node.name == "air" then
  156. minetest.set_node(pos, {name="fire:basic_flame"})
  157. end
  158. end
  159. end
  160. end
  161. end
  162. end
  163. end
  164. flameportal.teleport_player = function(name, voidpos)
  165. local player = minetest.get_player_by_name(name)
  166. if player and player:is_player() then
  167. -- Don't teleport dead players.
  168. if player:get_hp() > 0 then
  169. if minetest.get_node(voidpos).name ~= "voidstone:void" then return end
  170. if sheriff.is_cheater(name) then
  171. if sheriff.punish_probability(name) then
  172. sheriff.punish_player(name)
  173. return
  174. end
  175. end
  176. local pp = player:get_pos()
  177. if pp.y > -25000 then
  178. -- Player is not in nether. Teleport them to the nether.
  179. flameportal.teleport_player_to_nether(player, voidpos)
  180. else
  181. local pname = player:get_player_name()
  182. local storage = flameportal.modstorage
  183. local return_pos = minetest.string_to_pos(storage:get_string(pname))
  184. if return_pos then
  185. minetest.log("action", "Player " .. pname .. " returns from the nether at (" .. minetest.pos_to_string(voidpos) .. ")")
  186. preload_tp.execute({
  187. player_name = pname,
  188. target_position = return_pos,
  189. emerge_radius = 32,
  190. particle_effects = true,
  191. post_teleport_callback = function()
  192. -- Damage player on return journey only sometimes.
  193. if math_random(1, 30) == 1 then
  194. minetest.after(0.5, function()
  195. local pref = minetest.get_player_by_name(pname)
  196. if pref and pref:is_player() then
  197. pref:set_hp(pref:get_hp() - math_random(2, 15))
  198. end
  199. end)
  200. end
  201. portal_sickness.on_use_portal(pname)
  202. end,
  203. force_teleport = false,
  204. teleport_sound = "nether_portal_usual",
  205. send_blocks = true,
  206. })
  207. end
  208. end
  209. end
  210. end
  211. end
  212. -- 18200,-30794,-22386
  213. -- Teleport a player into or out of the nether.
  214. flameportal.teleport_player_to_nether = function(player, voidpos)
  215. local pname = player:get_player_name()
  216. local meta = minetest.get_meta(voidpos)
  217. local spos = meta:get_string("target") or ""
  218. local target
  219. if spos == "" then
  220. -- If metadata target hasn't been initialized yet.
  221. local pos = vector_round(voidpos)
  222. --local pr = PcgRandom(pos.x+pos.y+pos.z)
  223. target = {x=0, y=-30790, z=0}
  224. --target.x = pr:next(-30000, 30000)
  225. --target.z = pr:next(-30000, 30000)
  226. -- Chose a wholy random location.
  227. target.x = math_random(-30000, 30000)
  228. target.z = math_random(-30000, 30000)
  229. meta:set_string("target", minetest.pos_to_string(target))
  230. else
  231. -- Target location already set, use it.
  232. target = minetest.string_to_pos(spos)
  233. if target == nil then return end
  234. end
  235. -- Create platform beneath player.
  236. local minp = {x=target.x-1, y=target.y-12, z=target.z-1}
  237. local maxp = {x=target.x+1, y=target.y-6, z=target.z+1}
  238. local tb = {minp=minp, maxp=maxp, top={x=target.x, y=target.y-6, z=target.z}}
  239. -- Create flame pillar.
  240. local minp2 = {x=target.x-1, y=target.y-3, z=target.z-1}
  241. local maxp2 = {x=target.x+1, y=target.y+10, z=target.z+1}
  242. local tb2 = {
  243. minp = minp2,
  244. maxp = maxp2,
  245. avoid_x = target.x,
  246. avoid_z = target.z,
  247. }
  248. target.y = target.y+10
  249. preload_tp.execute({
  250. player_name = pname,
  251. target_position = target,
  252. emerge_radius = 64,
  253. particle_effects = true,
  254. pre_teleport_callback = function()
  255. flameportal.make_platform(tb)
  256. flameportal.make_flame_pillar(tb2)
  257. end,
  258. post_teleport_callback = function()
  259. local storage = flameportal.modstorage
  260. local return_pos = {x=voidpos.x, y=voidpos.y+1, z=voidpos.z}
  261. storage:set_string(pname, minetest.pos_to_string(return_pos))
  262. minetest.log("action", "Player " .. pname .. " teleports into the nether @ (" .. minetest.pos_to_string(target) .. ")")
  263. end,
  264. teleport_sound = "nether_portal_usual",
  265. send_blocks = true,
  266. })
  267. end
  268. -- API function.
  269. function flameportal.clear_return_location(pname)
  270. flameportal.modstorage:set_string(pname, nil)
  271. end
  272. -- Flint & steel should call this when used on obsidian.
  273. -- This function should check if the player is standing on void, and teleport them.
  274. flameportal.try_teleport_on_flint_use =
  275. function(pref)
  276. if not pref or not pref:is_player() then return end
  277. local p1 = utility.node_under_pos(pref:get_pos())
  278. if minetest.get_node(p1).name == "voidstone:void" then
  279. flameportal.teleport_player(pref:get_player_name(), p1)
  280. end
  281. end
  282. -- This function should be called whenever a nodetype that takes part in portal construction is removed.
  283. flameportal.after_portal_destruct = function(pos, oldnode)
  284. if oldnode.name == "fire:nether_flame" then
  285. local p = minetest.find_node_near(pos, 2, "voidstone:void")
  286. if p then
  287. minetest.add_node(p, {name="fire:basic_flame"})
  288. end
  289. elseif oldnode.name == "voidstone:void" then
  290. local void = {
  291. {x=pos.x+1, y=pos.y, z=pos.z },
  292. {x=pos.x-1, y=pos.y, z=pos.z },
  293. {x=pos.x, y=pos.y, z=pos.z+1},
  294. {x=pos.x, y=pos.y, z=pos.z-1},
  295. {x=pos.x, y=pos.y+1, z=pos.z },
  296. {x=pos.x, y=pos.y-1, z=pos.z },
  297. }
  298. for k, v in pairs(void) do
  299. local n = minetest.get_node(v).name
  300. if n == "voidstone:void" then
  301. minetest.add_node(v, {name="fire:basic_flame"})
  302. end
  303. end
  304. elseif oldnode.name == "default:obsidian" then
  305. local fp = {x=pos.x, y=pos.y+1, z=pos.z}
  306. if minetest.get_node(fp).name == "fire:nether_flame" then
  307. minetest.remove_node(fp)
  308. end
  309. local void = {
  310. {x=pos.x+1, y=pos.y, z=pos.z },
  311. {x=pos.x-1, y=pos.y, z=pos.z },
  312. {x=pos.x, y=pos.y, z=pos.z+1},
  313. {x=pos.x, y=pos.y, z=pos.z-1},
  314. {x=pos.x, y=pos.y+1, z=pos.z },
  315. {x=pos.x, y=pos.y-1, z=pos.z },
  316. }
  317. for k, v in pairs(void) do
  318. local n = minetest.get_node(v).name
  319. if n == "voidstone:void" then
  320. minetest.add_node(v, {name="fire:basic_flame"})
  321. end
  322. end
  323. end
  324. end
  325. if not flameportal.run_once then
  326. flameportal.modstorage = minetest.get_mod_storage()
  327. -- Special node which the player can land on safely when dropped into the nether.
  328. minetest.register_node("flameportal:redrack", {
  329. description = "Netherack",
  330. tiles = {"rackstone_redrack.png"},
  331. groups = utility.dig_groups("netherack", {fall_damage_add_percent=-100}),
  332. sounds = rackstone.rackstone_sounds(),
  333. drop = "rackstone:redrack",
  334. })
  335. minetest.register_alias("flameportal:no_fall_damage", "flameportal:redrack")
  336. minetest.override_item("default:obsidian", {
  337. after_destruct = function(pos, oldnode)
  338. minetest.after(0, ambiance.recheck_nearby_sound_beacons, {x=pos.x, y=pos.y, z=pos.z}, 16)
  339. return flameportal.after_portal_destruct(pos, oldnode)
  340. end,
  341. -- Obsidian is blast resitant now.
  342. --on_blast = function(pos) minetest.remove_node(pos) end,
  343. })
  344. minetest.override_item("voidstone:void", {
  345. after_destruct = function(...) return flameportal.after_portal_destruct(...) end,
  346. on_blast = function(pos) minetest.remove_node(pos) end,
  347. })
  348. minetest.override_item("fire:nether_flame", {
  349. after_destruct = function(pos, oldnode)
  350. flameportal.after_portal_destruct(pos, oldnode)
  351. fireambiance.on_flame_addremove(pos)
  352. particles.del_flame_spawner(pos)
  353. end,
  354. on_blast = function(pos) minetest.remove_node(pos) end,
  355. })
  356. -- Reloadable.
  357. local file = flameportal.modpath .. "/init.lua"
  358. local name = "flameportal:core"
  359. reload.register_file(name, file, false)
  360. flameportal.run_once = true
  361. end