init.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. meta:mark_as_private({"target"})
  231. else
  232. -- Target location already set, use it.
  233. target = minetest.string_to_pos(spos)
  234. if target == nil then return end
  235. end
  236. -- Create platform beneath player.
  237. local minp = {x=target.x-1, y=target.y-12, z=target.z-1}
  238. local maxp = {x=target.x+1, y=target.y-6, z=target.z+1}
  239. local tb = {minp=minp, maxp=maxp, top={x=target.x, y=target.y-6, z=target.z}}
  240. -- Create flame pillar.
  241. local minp2 = {x=target.x-1, y=target.y-3, z=target.z-1}
  242. local maxp2 = {x=target.x+1, y=target.y+10, z=target.z+1}
  243. local tb2 = {
  244. minp = minp2,
  245. maxp = maxp2,
  246. avoid_x = target.x,
  247. avoid_z = target.z,
  248. }
  249. target.y = target.y+10
  250. preload_tp.execute({
  251. player_name = pname,
  252. target_position = target,
  253. emerge_radius = 64,
  254. particle_effects = true,
  255. pre_teleport_callback = function()
  256. flameportal.make_platform(tb)
  257. flameportal.make_flame_pillar(tb2)
  258. end,
  259. post_teleport_callback = function()
  260. local storage = flameportal.modstorage
  261. local return_pos = {x=voidpos.x, y=voidpos.y+1, z=voidpos.z}
  262. storage:set_string(pname, minetest.pos_to_string(return_pos))
  263. minetest.log("action", "Player " .. pname .. " teleports into the nether @ (" .. minetest.pos_to_string(target) .. ")")
  264. end,
  265. teleport_sound = "nether_portal_usual",
  266. send_blocks = true,
  267. })
  268. end
  269. -- API function.
  270. function flameportal.clear_return_location(pname)
  271. flameportal.modstorage:set_string(pname, nil)
  272. end
  273. -- Flint & steel should call this when used on obsidian.
  274. -- This function should check if the player is standing on void, and teleport them.
  275. flameportal.try_teleport_on_flint_use =
  276. function(pref)
  277. if not pref or not pref:is_player() then return end
  278. local p1 = utility.node_under_pos(pref:get_pos())
  279. if minetest.get_node(p1).name == "voidstone:void" then
  280. flameportal.teleport_player(pref:get_player_name(), p1)
  281. end
  282. end
  283. -- This function should be called whenever a nodetype that takes part in portal construction is removed.
  284. flameportal.after_portal_destruct = function(pos, oldnode)
  285. if oldnode.name == "fire:nether_flame" then
  286. local p = minetest.find_node_near(pos, 2, "voidstone:void")
  287. if p then
  288. minetest.add_node(p, {name="fire:basic_flame"})
  289. end
  290. elseif oldnode.name == "voidstone:void" then
  291. local void = {
  292. {x=pos.x+1, y=pos.y, z=pos.z },
  293. {x=pos.x-1, y=pos.y, z=pos.z },
  294. {x=pos.x, y=pos.y, z=pos.z+1},
  295. {x=pos.x, y=pos.y, z=pos.z-1},
  296. {x=pos.x, y=pos.y+1, z=pos.z },
  297. {x=pos.x, y=pos.y-1, z=pos.z },
  298. }
  299. for k, v in pairs(void) do
  300. local n = minetest.get_node(v).name
  301. if n == "voidstone:void" then
  302. minetest.add_node(v, {name="fire:basic_flame"})
  303. end
  304. end
  305. elseif oldnode.name == "default:obsidian" then
  306. local fp = {x=pos.x, y=pos.y+1, z=pos.z}
  307. if minetest.get_node(fp).name == "fire:nether_flame" then
  308. minetest.remove_node(fp)
  309. end
  310. local void = {
  311. {x=pos.x+1, y=pos.y, z=pos.z },
  312. {x=pos.x-1, y=pos.y, z=pos.z },
  313. {x=pos.x, y=pos.y, z=pos.z+1},
  314. {x=pos.x, y=pos.y, z=pos.z-1},
  315. }
  316. for k, v in pairs(void) do
  317. local n = minetest.get_node(v).name
  318. if n == "voidstone:void" then
  319. minetest.add_node(v, {name="fire:basic_flame"})
  320. end
  321. end
  322. end
  323. end
  324. if not flameportal.run_once then
  325. flameportal.modstorage = minetest.get_mod_storage()
  326. -- Special node which the player can land on safely when dropped into the nether.
  327. minetest.register_node("flameportal:redrack", {
  328. description = "Netherack",
  329. tiles = {"rackstone_redrack.png"},
  330. groups = utility.dig_groups("netherack", {fall_damage_add_percent=-100}),
  331. sounds = rackstone.rackstone_sounds(),
  332. drop = "rackstone:redrack",
  333. })
  334. minetest.register_alias("flameportal:no_fall_damage", "flameportal:redrack")
  335. minetest.override_item("default:obsidian", {
  336. after_destruct = function(pos, oldnode)
  337. minetest.after(0, ambiance.recheck_nearby_sound_beacons, {x=pos.x, y=pos.y, z=pos.z}, 16)
  338. return flameportal.after_portal_destruct(pos, oldnode)
  339. end,
  340. -- Obsidian is blast resitant now.
  341. --on_blast = function(pos) minetest.remove_node(pos) end,
  342. })
  343. minetest.override_item("voidstone:void", {
  344. after_destruct = function(...) return flameportal.after_portal_destruct(...) end,
  345. on_blast = function(pos) minetest.remove_node(pos) end,
  346. })
  347. minetest.override_item("fire:nether_flame", {
  348. after_destruct = function(pos, oldnode)
  349. flameportal.after_portal_destruct(pos, oldnode)
  350. fireambiance.on_flame_addremove(pos)
  351. particles.del_flame_spawner(pos)
  352. end,
  353. on_blast = function(pos) minetest.remove_node(pos) end,
  354. })
  355. -- Reloadable.
  356. local file = flameportal.modpath .. "/init.lua"
  357. local name = "flameportal:core"
  358. reload.register_file(name, file, false)
  359. flameportal.run_once = true
  360. end