diff --git a/.luacheckrc b/.luacheckrc index cc73fb0..5ed2386 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -1,15 +1,25 @@ unused_args = false allow_defined_top = true +globals = { + "nether" +} + read_globals = { "core", "default", "DIR_DELIM", "dump", + "dungeon_loot", "intllib", "ItemStack", + "loot", + "doc", + "math.hypot", + "mesecon", "minetest", "nodeupdate", + "PcgRandom", "PseudoRandom", "stairs", "stairsplus", diff --git a/README.md b/README.md index 2e1d7d8..7404d14 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,10 @@ -Nether Mod for Minetest +Nether Mod for Minetest, with Portals API. + +See portal_api.txt for how to create custom portals to your own realms. + +See settingtypes.txt or go to "Settings"->"All Settings"->"Mods"->"nether" +in the game to view the options provided by this mod. + ## License of source code: @@ -17,15 +23,25 @@ WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -## License of media (textures and sounds) +## License and attribution of media (textures and sounds) -Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) -http://creativecommons.org/licenses/by-sa/3.0/ +### [Public Domain Dedication (CC0 1.0)](https://creativecommons.org/publicdomain/zero/1.0/) -## Authors of media files + * `nether_portal_teleport.ogg` is a timing adjusted version of "teleport" by outroelison (https://freesound.org/people/outroelison), used under CC0 1.0 -Everything not listed in here: -Copyright (C) 2013 PilzAdam +### [Attribution 3.0 Unported (CC BY 3.0)](https://creativecommons.org/licenses/by/3.0/) + + * `nether_portal_ambient.ogg` & `nether_portal_ambient.0.ogg` are extractions from "Deep Cinematic Rumble Stereo" by [Patrick Lieberkind](http://www.lieberkindvisuals.dk), used under CC BY 3.0 + * `nether_portal_extinguish.ogg` is an extraction from "Tight Laser Weapon Hit Scifi" by damjancd (https://freesound.org/people/damjancd), used under CC BY 3.0 + * `nether_portal_ignite.ogg` is a derivative of "Flame Ignition" by hykenfreak (https://freesound.org/people/hykenfreak), used under CC BY 3.0. "Nether Portal ignite" is licensed under CC BY 3.0 by Treer. + +### [Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)](https://creativecommons.org/licenses/by-sa/4.0/) + * `nether_book_`* (files starting with "nether_book"): Treer, 2019-2020 + * `nether_portal_ignition_failure.ogg`: Treer, 2019 + * `nether_particle_anim`* (files starting with "nether_particle_anim"): Treer, 2019 + +### [Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)](http://creativecommons.org/licenses/by-sa/3.0/) + * `nether_rack.png`: Zeg9 + * `nether_glowstone.png`: BlockMen - - nether_rack.png: Zeg9 - - nether_glowstone.png: BlockMen +All other media: Copyright © 2013 PilzAdam, licensed under CC BY-SA 3.0 by PilzAdam. \ No newline at end of file diff --git a/depends.txt b/depends.txt index 66acf1b..61303f2 100644 --- a/depends.txt +++ b/depends.txt @@ -1,3 +1,7 @@ stairs default moreblocks? +mesecons? +loot? +dungeon_loot? +doc_basics? diff --git a/init.lua b/init.lua index 90e5c83..aaec842 100644 --- a/init.lua +++ b/init.lua @@ -19,686 +19,146 @@ ]]-- - --- Parameters - -local NETHER_DEPTH = -5000 -local TCAVE = 0.6 -local BLEND = 128 - - --- 3D noise - -local np_cave = { - offset = 0, - scale = 1, - spread = {x = 384, y = 128, z = 384}, -- squashed 3:1 - seed = 59033, - octaves = 5, - persist = 0.7, - lacunarity = 2.0, - --flags = "" -} - - --- Stuff - -local yblmax = NETHER_DEPTH - BLEND * 2 - - --- Functions - -local function build_portal(pos, target) - local p1 = {x = pos.x - 1, y = pos.y - 1, z = pos.z} - local p2 = {x = p1.x + 3, y = p1.y + 4, z = p1.z} - - local path = minetest.get_modpath("nether") .. "/schematics/nether_portal.mts" - minetest.place_schematic({x = p1.x, y = p1.y, z = p1.z - 2}, path, 0, nil, true) - - for y = p1.y, p2.y do - for x = p1.x, p2.x do - local meta = minetest.get_meta({x = x, y = y, z = p1.z}) - meta:set_string("p1", minetest.pos_to_string(p1)) - meta:set_string("p2", minetest.pos_to_string(p2)) - meta:set_string("target", minetest.pos_to_string(target)) - end - end -end - - -local function volume_is_natural(minp, maxp) - local c_air = minetest.get_content_id("air") - local c_ignore = minetest.get_content_id("ignore") - - local vm = minetest.get_voxel_manip() - local pos1 = {x = minp.x, y = minp.y, z = minp.z} - local pos2 = {x = maxp.x, y = maxp.y, z = maxp.z} - local emin, emax = vm:read_from_map(pos1, pos2) - local area = VoxelArea:new({MinEdge = emin, MaxEdge = emax}) - local data = vm:get_data() - - for z = pos1.z, pos2.z do - for y = pos1.y, pos2.y do - local vi = area:index(pos1.x, y, z) - for x = pos1.x, pos2.x do - local id = data[vi] -- Existing node - if id ~= c_air and id ~= c_ignore then -- These are natural - local name = minetest.get_name_from_content_id(id) - if not minetest.registered_nodes[name].is_ground_content then - return false - end - end - vi = vi + 1 - end - end - end - - return true -end - - -local function find_nether_target_y(target_x, target_z, start_y) - local nobj_cave_point = minetest.get_perlin(np_cave) - local air = 0 -- Consecutive air nodes found - - for y = start_y, start_y - 4096, -1 do - local nval_cave = nobj_cave_point:get3d({x = target_x, y = y, z = target_z}) - - if nval_cave > TCAVE then -- Cavern - air = air + 1 - else -- Not cavern, check if 4 nodes of space above - if air >= 4 then - -- Check volume for non-natural nodes - local minp = {x = target_x - 1, y = y - 1, z = target_z - 2} - local maxp = {x = target_x + 2, y = y + 3, z = target_z + 2} - if volume_is_natural(minp, maxp) then - return y + 2 - else -- Restart search a little lower - find_nether_target_y(target_x, target_z, y - 16) - end - else -- Not enough space, reset air to zero - air = 0 - end - end - end - - return start_y -- Fallback -end - - -local function find_surface_target_y(target_x, target_z, start_y) - for y = start_y, start_y - 256, -16 do - -- Check volume for non-natural nodes - local minp = {x = target_x - 1, y = y - 1, z = target_z - 2} - local maxp = {x = target_x + 2, y = y + 3, z = target_z + 2} - if volume_is_natural(minp, maxp) then - return y - end - end - - return start_y - 256 -- Fallback -end - - -local function move_check(p1, max, dir) - local p = {x = p1.x, y = p1.y, z = p1.z} - local d = math.abs(max - p1[dir]) / (max - p1[dir]) - - while p[dir] ~= max do - p[dir] = p[dir] + d - if minetest.get_node(p).name ~= "default:obsidian" then - return false - end - end - - return true -end - - -local function check_portal(p1, p2) - if p1.x ~= p2.x then - if not move_check(p1, p2.x, "x") then - return false - end - if not move_check(p2, p1.x, "x") then - return false - end - elseif p1.z ~= p2.z then - if not move_check(p1, p2.z, "z") then - return false - end - if not move_check(p2, p1.z, "z") then - return false - end - else - return false - end - - if not move_check(p1, p2.y, "y") then - return false - end - if not move_check(p2, p1.y, "y") then - return false - end - - return true -end - - -local function is_portal(pos) - for d = -3, 3 do - for y = -4, 4 do - local px = {x = pos.x + d, y = pos.y + y, z = pos.z} - local pz = {x = pos.x, y = pos.y + y, z = pos.z + d} - - if check_portal(px, {x = px.x + 3, y = px.y + 4, z = px.z}) then - return px, {x = px.x + 3, y = px.y + 4, z = px.z} - end - if check_portal(pz, {x = pz.x, y = pz.y + 4, z = pz.z + 3}) then - return pz, {x = pz.x, y = pz.y + 4, z = pz.z + 3} - end - end +local S +if minetest.get_translator ~= nil then + S = minetest.get_translator("nether") +else + -- mock the translator function for MT 0.4 + S = function(str, ...) + local args={...} + return str:gsub( + "@%d+", + function(match) return args[tonumber(match:sub(2))] end + ) end end +-- Global Nether namespace +nether = {} +nether.modname = minetest.get_current_modname() +nether.path = minetest.get_modpath(nether.modname) +nether.get_translator = S -local function make_portal(pos) - local p1, p2 = is_portal(pos) - if not p1 or not p2 then - return false - end +-- Settings +nether.DEPTH = -5000 -- The y location of the Nether +nether.FASTTRAVEL_FACTOR = 8 -- 10 could be better value for Minetest, since there's no sprint, but ex-Minecraft players will be mathing for 8 +nether.PORTAL_BOOK_LOOT_WEIGHTING = 0.9 -- Likelyhood of finding the Book of Portals (guide) in dungeon chests. Set to 0 to disable. +nether.NETHER_REALM_ENABLED = true -- Setting to false disables the Nether and Nether portal - for d = 1, 2 do - for y = p1.y + 1, p2.y - 1 do - local p - if p1.z == p2.z then - p = {x = p1.x + d, y = y, z = p1.z} - else - p = {x = p1.x, y = y, z = p1.z + d} - end - if minetest.get_node(p).name ~= "air" then - return false - end - end - end - local param2 - if p1.z == p2.z then - param2 = 0 - else - param2 = 1 - end - - local target = {x = p1.x, y = p1.y, z = p1.z} - target.x = target.x + 1 - if target.y < NETHER_DEPTH then - target.y = find_surface_target_y(target.x, target.z, -16) - else - local start_y = NETHER_DEPTH - math.random(500, 1500) -- Search start - target.y = find_nether_target_y(target.x, target.z, start_y) - end +-- Override default settings with values from the .conf file, if any are present. +nether.FASTTRAVEL_FACTOR = tonumber(minetest.settings:get("nether_fasttravel_factor") or nether.FASTTRAVEL_FACTOR) +nether.PORTAL_BOOK_LOOT_WEIGHTING = tonumber(minetest.settings:get("nether_portalBook_loot_weighting") or nether.PORTAL_BOOK_LOOT_WEIGHTING) +nether.NETHER_REALM_ENABLED = minetest.settings:get_bool("nether_realm_enabled", nether.NETHER_REALM_ENABLED) or nether.NETHER_REALM_ENABLED - for d = 0, 3 do - for y = p1.y, p2.y do - local p - if param2 == 0 then - p = {x = p1.x + d, y = y, z = p1.z} - else - p = {x = p1.x, y = y, z = p1.z + d} - end - if minetest.get_node(p).name == "air" then - minetest.set_node(p, {name = "nether:portal", param2 = param2}) - end - local meta = minetest.get_meta(p) - meta:set_string("p1", minetest.pos_to_string(p1)) - meta:set_string("p2", minetest.pos_to_string(p2)) - meta:set_string("target", minetest.pos_to_string(target)) - end - end - return true +-- Load files +dofile(nether.path .. "/portal_api.lua") +dofile(nether.path .. "/nodes.lua") +if nether.NETHER_REALM_ENABLED then + dofile(nether.path .. "/mapgen.lua") end +dofile(nether.path .. "/portal_examples.lua") --- ABMs - -minetest.register_abm({ - nodenames = {"nether:portal"}, - interval = 1, - chance = 2, - action = function(pos, node) - minetest.add_particlespawner({ - amount = 32, - time = 4, - minpos = {x = pos.x - 0.25, y = pos.y - 0.25, z = pos.z - 0.25}, - maxpos = {x = pos.x + 0.25, y = pos.y + 0.25, z = pos.z + 0.25}, - minvel = {x = -0.8, y = -0.8, z = -0.8}, - maxvel = {x = 0.8, y = 0.8, z = 0.8}, - minacc = {x = 0, y = 0, z = 0}, - maxacc = {x = 0, y = 0, z = 0}, - minexptime = 0.5, - maxexptime = 1, - minsize = 1, - maxsize = 2, - collisiondetection = false, - texture = "nether_particle.png" - }) - for _, obj in ipairs(minetest.get_objects_inside_radius(pos, 1)) do - if obj:is_player() then - local meta = minetest.get_meta(pos) - local target = minetest.string_to_pos(meta:get_string("target")) - if target then - -- force emerge of target area - minetest.get_voxel_manip():read_from_map(target, target) - if not minetest.get_node_or_nil(target) then - minetest.emerge_area( - vector.subtract(target, 4), vector.add(target, 4)) - end - -- teleport the player - minetest.after(3, function(o, p, t) - local objpos = o:getpos() - if not objpos then -- player quit the game while teleporting - return - end - objpos.y = objpos.y + 0.1 -- Fix some glitches at -8000 - if minetest.get_node(objpos).name ~= "nether:portal" then - return - end - - o:setpos(t) - - local function check_and_build_portal(pp, tt) - local n = minetest.get_node_or_nil(tt) - if n and n.name ~= "nether:portal" then - build_portal(tt, pp) - minetest.after(2, check_and_build_portal, pp, tt) - minetest.after(4, check_and_build_portal, pp, tt) - elseif not n then - minetest.after(1, check_and_build_portal, pp, tt) - end - end - - minetest.after(1, check_and_build_portal, p, t) - - end, obj, pos, target) - end - end - end - end, -}) - - --- Nodes - -minetest.register_node("nether:portal", { - description = "Nether Portal", - tiles = { - "nether_transparent.png", - "nether_transparent.png", - "nether_transparent.png", - "nether_transparent.png", - { - name = "nether_portal.png", - animation = { - type = "vertical_frames", - aspect_w = 16, - aspect_h = 16, - length = 0.5, - }, - }, - { - name = "nether_portal.png", - animation = { - type = "vertical_frames", - aspect_w = 16, - aspect_h = 16, - length = 0.5, - }, - }, - }, - drawtype = "nodebox", - paramtype = "light", - paramtype2 = "facedir", - sunlight_propagates = true, - use_texture_alpha = true, - walkable = false, - diggable = false, - pointable = false, - buildable_to = false, - is_ground_content = false, - drop = "", - light_source = 5, - post_effect_color = {a = 180, r = 128, g = 0, b = 128}, - alpha = 192, - node_box = { - type = "fixed", - fixed = { - {-0.5, -0.5, -0.1, 0.5, 0.5, 0.1}, - }, - }, - groups = {not_in_creative_inventory = 1} -}) - -minetest.register_node(":default:obsidian", { - description = "Obsidian", - tiles = {"default_obsidian.png"}, - is_ground_content = false, - sounds = default.node_sound_stone_defaults(), - groups = {cracky = 1, level = 2}, - - on_destruct = function(pos) - local meta = minetest.get_meta(pos) - local p1 = minetest.string_to_pos(meta:get_string("p1")) - local p2 = minetest.string_to_pos(meta:get_string("p2")) - local target = minetest.string_to_pos(meta:get_string("target")) - if not p1 or not p2 then - return - end - - for x = p1.x, p2.x do - for y = p1.y, p2.y do - for z = p1.z, p2.z do - local nn = minetest.get_node({x = x, y = y, z = z}).name - if nn == "default:obsidian" or nn == "nether:portal" then - if nn == "nether:portal" then - minetest.remove_node({x = x, y = y, z = z}) - end - local m = minetest.get_meta({x = x, y = y, z = z}) - m:set_string("p1", "") - m:set_string("p2", "") - m:set_string("target", "") - end - end - end - end - - meta = minetest.get_meta(target) - if not meta then - return - end - p1 = minetest.string_to_pos(meta:get_string("p1")) - p2 = minetest.string_to_pos(meta:get_string("p2")) - if not p1 or not p2 then - return - end - - for x = p1.x, p2.x do - for y = p1.y, p2.y do - for z = p1.z, p2.z do - local nn = minetest.get_node({x = x, y = y, z = z}).name - if nn == "default:obsidian" or nn == "nether:portal" then - if nn == "nether:portal" then - minetest.remove_node({x = x, y = y, z = z}) - end - local m = minetest.get_meta({x = x, y = y, z = z}) - m:set_string("p1", "") - m:set_string("p2", "") - m:set_string("target", "") - end - end - end - end - end, -}) - -minetest.register_node("nether:rack", { - description = "Netherrack", - tiles = {"nether_rack.png"}, - is_ground_content = true, - groups = {cracky = 3, level = 2}, - sounds = default.node_sound_stone_defaults(), -}) - -minetest.register_node("nether:sand", { - description = "Nethersand", - tiles = {"nether_sand.png"}, - is_ground_content = true, - groups = {crumbly = 3, level = 2, falling_node = 1}, - sounds = default.node_sound_gravel_defaults({ - footstep = {name = "default_gravel_footstep", gain = 0.45}, - }), -}) - -minetest.register_node("nether:glowstone", { - description = "Glowstone", - tiles = {"nether_glowstone.png"}, - is_ground_content = true, - light_source = 14, - paramtype = "light", - groups = {cracky = 3, oddly_breakable_by_hand = 3}, - sounds = default.node_sound_glass_defaults(), -}) - -minetest.register_node("nether:brick", { - description = "Nether Brick", - tiles = {"nether_brick.png"}, - is_ground_content = false, - groups = {cracky = 2, level = 2}, - sounds = default.node_sound_stone_defaults(), -}) - -local fence_texture = - "default_fence_overlay.png^nether_brick.png^default_fence_overlay.png^[makealpha:255,126,126" - -minetest.register_node("nether:fence_nether_brick", { - description = "Nether Brick Fence", - drawtype = "fencelike", - tiles = {"nether_brick.png"}, - inventory_image = fence_texture, - wield_image = fence_texture, - paramtype = "light", - sunlight_propagates = true, - is_ground_content = false, - selection_box = { - type = "fixed", - fixed = {-1/7, -1/2, -1/7, 1/7, 1/2, 1/7}, - }, - groups = {cracky = 2, level = 2}, - sounds = default.node_sound_stone_defaults(), -}) - - --- Register stair and slab - -stairs.register_stair_and_slab( - "nether_brick", - "nether:brick", - {cracky = 2, level = 2}, - {"nether_brick.png"}, - "nether stair", - "nether slab", - default.node_sound_stone_defaults() +-- Portals are ignited by right-clicking with a mese crystal fragment +nether.register_portal_ignition_item( + "default:mese_crystal_fragment", + {name = "nether_portal_ignition_failure", gain = 0.3} ) --- StairsPlus - -if minetest.get_modpath("moreblocks") then - stairsplus:register_all( - "nether", "brick", "nether:brick", { - description = "Nether Brick", - groups = {cracky = 2, level = 2}, - tiles = {"nether_brick.png"}, - sounds = default.node_sound_stone_defaults(), - }) -end - - --- Craftitems - -minetest.override_item("default:mese_crystal_fragment", { - on_place = function(stack, _, pt) - if pt.under and minetest.get_node(pt.under).name == "default:obsidian" then - local done = make_portal(pt.under) - if done and not minetest.settings:get_bool("creative_mode") then - stack:take_item() - end - end - - return stack - end, -}) - --- Crafting - -minetest.register_craft({ - output = "nether:brick 4", - recipe = { - {"nether:rack", "nether:rack"}, - {"nether:rack", "nether:rack"}, - } -}) - -minetest.register_craft({ - output = "nether:fence_nether_brick 6", - recipe = { - {"nether:brick", "nether:brick", "nether:brick"}, - {"nether:brick", "nether:brick", "nether:brick"}, - }, -}) - - --- Mapgen --- Initialize noise object, localise noise and data buffers +if nether.NETHER_REALM_ENABLED then + -- Use the Portal API to add a portal type which goes to the Nether + -- See portal_api.txt for documentation + nether.register_portal("nether_portal", { + shape = nether.PortalShape_Traditional, + frame_node_name = "default:obsidian", + wormhole_node_color = 0, -- 0 is magenta + title = S("Nether Portal"), + book_of_portals_pagetext = S([[Construction requires 14 blocks of obsidian, which we found deep underground where water had solidified molten rock. The finished frame is four blocks wide, five blocks high, and stands vertically, like a doorway. -local nobj_cave = nil -local nbuf_cave = nil -local dbuf = nil +This opens to a truly hellish place, though for small mercies the air there is still breathable. There is an intriguing dimensional mismatch happening between this realm and ours, as after opening the second portal into it we observed that 10 strides taken in the Nether appear to be an equivalent of @1 in the natural world. +The expedition parties have found no diamonds or gold, and after an experienced search party failed to return from the trail of a missing expedition party, I must conclude this is a dangerous place.]], 10 * nether.FASTTRAVEL_FACTOR), --- Content ids + is_within_realm = function(pos) -- return true if pos is inside the Nether + return pos.y < nether.DEPTH + end, -local c_air = minetest.get_content_id("air") + find_realm_anchorPos = function(surface_anchorPos) + -- divide x and z by a factor of 8 to implement Nether fast-travel + local destination_pos = vector.divide(surface_anchorPos, nether.FASTTRAVEL_FACTOR) + destination_pos.x = math.floor(0.5 + destination_pos.x) -- round to int + destination_pos.z = math.floor(0.5 + destination_pos.z) -- round to int + destination_pos.y = nether.DEPTH - 1000 -- temp value so find_nearest_working_portal() returns nether portals ---local c_stone_with_coal = minetest.get_content_id("default:stone_with_coal") ---local c_stone_with_iron = minetest.get_content_id("default:stone_with_iron") -local c_stone_with_mese = minetest.get_content_id("default:stone_with_mese") -local c_stone_with_diamond = minetest.get_content_id("default:stone_with_diamond") -local c_stone_with_gold = minetest.get_content_id("default:stone_with_gold") ---local c_stone_with_copper = minetest.get_content_id("default:stone_with_copper") -local c_mese = minetest.get_content_id("default:mese") + -- a y_factor of 0 makes the search ignore the altitude of the portals (as long as they are in the Nether) + local existing_portal_location, existing_portal_orientation = + nether.find_nearest_working_portal("nether_portal", destination_pos, 8, 0) -local c_gravel = minetest.get_content_id("default:gravel") -local c_dirt = minetest.get_content_id("default:dirt") -local c_sand = minetest.get_content_id("default:sand") - -local c_cobble = minetest.get_content_id("default:cobble") -local c_mossycobble = minetest.get_content_id("default:mossycobble") -local c_stair_cobble = minetest.get_content_id("stairs:stair_cobble") - -local c_lava_source = minetest.get_content_id("default:lava_source") -local c_lava_flowing = minetest.get_content_id("default:lava_flowing") -local c_water_source = minetest.get_content_id("default:water_source") -local c_water_flowing = minetest.get_content_id("default:water_flowing") - -local c_glowstone = minetest.get_content_id("nether:glowstone") -local c_nethersand = minetest.get_content_id("nether:sand") -local c_netherbrick = minetest.get_content_id("nether:brick") -local c_netherrack = minetest.get_content_id("nether:rack") - - --- On-generated function - -minetest.register_on_generated(function(minp, maxp, seed) - if minp.y > NETHER_DEPTH then - return - end - - local x1 = maxp.x - local y1 = maxp.y - local z1 = maxp.z - local x0 = minp.x - local y0 = minp.y - local z0 = minp.z - - local vm, emin, emax = minetest.get_mapgen_object("voxelmanip") - local area = VoxelArea:new{MinEdge = emin, MaxEdge = emax} - local data = vm:get_data(dbuf) - - local x11 = emax.x -- Limits of mapchunk plus mapblock shell - local y11 = emax.y - local z11 = emax.z - local x00 = emin.x - local y00 = emin.y - local z00 = emin.z - - local ystride = x1 - x0 + 1 - local zstride = ystride * ystride - local chulens = {x = ystride, y = ystride, z = ystride} - local minposxyz = {x = x0, y = y0, z = z0} - - nobj_cave = nobj_cave or minetest.get_perlin_map(np_cave, chulens) - local nvals_cave = nobj_cave:get3dMap_flat(minposxyz, nbuf_cave) - - for y = y00, y11 do -- Y loop first to minimise tcave calculations - local tcave - local in_chunk_y = false - if y >= y0 and y <= y1 then - if y > yblmax then - tcave = TCAVE + ((y - yblmax) / BLEND) ^ 2 + if existing_portal_location ~= nil then + return existing_portal_location, existing_portal_orientation else - tcave = TCAVE + local start_y = nether.DEPTH - math.random(500, 1500) -- Search starting altitude + destination_pos.y = nether.find_nether_ground_y(destination_pos.x, destination_pos.z, start_y) + return destination_pos end - in_chunk_y = true - end - - for z = z00, z11 do - local vi = area:index(x00, y, z) -- Initial voxelmanip index - local ni - local in_chunk_yz = in_chunk_y and z >= z0 and z <= z1 - - for x = x00, x11 do - if in_chunk_yz and x == x0 then - -- Initial noisemap index - ni = (z - z0) * zstride + (y - y0) * ystride + 1 - end - local in_chunk_yzx = in_chunk_yz and x >= x0 and x <= x1 -- In mapchunk - - local id = data[vi] -- Existing node - -- Cave air, cave liquids and dungeons are overgenerated, - -- convert these throughout mapchunk plus shell - if id == c_air or -- Air and liquids to air - id == c_lava_source or - id == c_lava_flowing or - id == c_water_source or - id == c_water_flowing then - data[vi] = c_air - -- Dungeons are preserved so we don't need - -- to check for cavern in the shell - elseif id == c_cobble or -- Dungeons (preserved) to netherbrick - id == c_mossycobble or - id == c_stair_cobble then - data[vi] = c_netherbrick - end - - if in_chunk_yzx then -- In mapchunk - if nvals_cave[ni] > tcave then -- Only excavate cavern in mapchunk - data[vi] = c_air - elseif id == c_mese then -- Mese block to lava - data[vi] = c_lava_source - elseif id == c_stone_with_gold or -- Precious ores to glowstone - id == c_stone_with_mese or - id == c_stone_with_diamond then - data[vi] = c_glowstone - elseif id == c_gravel or -- Blob ore to nethersand - id == c_dirt or - id == c_sand then - data[vi] = c_nethersand - else -- All else to netherstone - data[vi] = c_netherrack - end - - ni = ni + 1 -- Only increment noise index in mapchunk - end - - vi = vi + 1 + end, + + find_surface_anchorPos = function(realm_anchorPos) + -- A portal definition doesn't normally need to provide a find_surface_anchorPos() function, + -- since find_surface_target_y() will be used by default, but Nether portals also scale position + -- to create fast-travel. + -- Defining a custom function also means we can look for existing nearby portals. + + -- Multiply x and z by a factor of 8 to implement Nether fast-travel + local destination_pos = vector.multiply(realm_anchorPos, nether.FASTTRAVEL_FACTOR) + destination_pos.x = math.min(30900, math.max(-30900, destination_pos.x)) -- clip to world boundary + destination_pos.z = math.min(30900, math.max(-30900, destination_pos.z)) -- clip to world boundary + destination_pos.y = 0 -- temp value so find_nearest_working_portal() doesn't return nether portals + + -- a y_factor of 0 makes the search ignore the altitude of the portals (as long as they are outside the Nether) + local existing_portal_location, existing_portal_orientation = + nether.find_nearest_working_portal("nether_portal", destination_pos, 8 * nether.FASTTRAVEL_FACTOR, 0) + + if existing_portal_location ~= nil then + return existing_portal_location, existing_portal_orientation + else + destination_pos.y = nether.find_surface_target_y(destination_pos.x, destination_pos.z, "nether_portal") + return destination_pos end + end, + + on_ignite = function(portalDef, anchorPos, orientation) + + -- make some sparks fly + local p1, p2 = portalDef.shape:get_p1_and_p2_from_anchorPos(anchorPos, orientation) + local pos = vector.divide(vector.add(p1, p2), 2) + + local textureName = portalDef.particle_texture + if type(textureName) == "table" then textureName = textureName.name end + + minetest.add_particlespawner({ + amount = 110, + time = 0.1, + minpos = {x = pos.x - 0.5, y = pos.y - 1.2, z = pos.z - 0.5}, + maxpos = {x = pos.x + 0.5, y = pos.y + 1.2, z = pos.z + 0.5}, + minvel = {x = -5, y = -1, z = -5}, + maxvel = {x = 5, y = 1, z = 5}, + minacc = {x = 0, y = 0, z = 0}, + maxacc = {x = 0, y = 0, z = 0}, + minexptime = 0.1, + maxexptime = 0.5, + minsize = 0.2 * portalDef.particle_texture_scale, + maxsize = 0.8 * portalDef.particle_texture_scale, + collisiondetection = false, + texture = textureName .. "^[colorize:#F4F:alpha", + animation = portalDef.particle_texture_animation, + glow = 8 + }) + end - end - vm:set_data(data) - vm:set_lighting({day = 0, night = 0}) - vm:calc_lighting() - vm:update_liquids() - vm:write_to_map() -end) + }) +end \ No newline at end of file diff --git a/mapgen.lua b/mapgen.lua new file mode 100644 index 0000000..653d8f0 --- /dev/null +++ b/mapgen.lua @@ -0,0 +1,225 @@ +--[[ + + Nether mod for minetest + + Copyright (C) 2013 PilzAdam + + Permission to use, copy, modify, and/or distribute this software for + any purpose with or without fee is hereby granted, provided that the + above copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL + WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR + BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES + OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + SOFTWARE. + +]]-- + + +-- Parameters + +local NETHER_DEPTH = nether.DEPTH +local TCAVE = 0.6 +local BLEND = 128 + + +-- 3D noise + +local np_cave = { + offset = 0, + scale = 1, + spread = {x = 384, y = 128, z = 384}, -- squashed 3:1 + seed = 59033, + octaves = 5, + persist = 0.7, + lacunarity = 2.0, + --flags = "" +} + + +-- Stuff + +local yblmax = NETHER_DEPTH - BLEND * 2 + + + + +-- Mapgen + +-- Initialize noise object, localise noise and data buffers + +local nobj_cave = nil +local nbuf_cave = nil +local dbuf = nil + + +-- Content ids + +local c_air = minetest.get_content_id("air") + +--local c_stone_with_coal = minetest.get_content_id("default:stone_with_coal") +--local c_stone_with_iron = minetest.get_content_id("default:stone_with_iron") +local c_stone_with_mese = minetest.get_content_id("default:stone_with_mese") +local c_stone_with_diamond = minetest.get_content_id("default:stone_with_diamond") +local c_stone_with_gold = minetest.get_content_id("default:stone_with_gold") +--local c_stone_with_copper = minetest.get_content_id("default:stone_with_copper") +local c_mese = minetest.get_content_id("default:mese") + +local c_gravel = minetest.get_content_id("default:gravel") +local c_dirt = minetest.get_content_id("default:dirt") +local c_sand = minetest.get_content_id("default:sand") + +local c_cobble = minetest.get_content_id("default:cobble") +local c_mossycobble = minetest.get_content_id("default:mossycobble") +local c_stair_cobble = minetest.get_content_id("stairs:stair_cobble") + +local c_lava_source = minetest.get_content_id("default:lava_source") +local c_lava_flowing = minetest.get_content_id("default:lava_flowing") +local c_water_source = minetest.get_content_id("default:water_source") +local c_water_flowing = minetest.get_content_id("default:water_flowing") + +local c_glowstone = minetest.get_content_id("nether:glowstone") +local c_nethersand = minetest.get_content_id("nether:sand") +local c_netherbrick = minetest.get_content_id("nether:brick") +local c_netherrack = minetest.get_content_id("nether:rack") + + +-- On-generated function + +minetest.register_on_generated(function(minp, maxp, seed) + if minp.y > NETHER_DEPTH then + return + end + + local x1 = maxp.x + local y1 = maxp.y + local z1 = maxp.z + local x0 = minp.x + local y0 = minp.y + local z0 = minp.z + + local vm, emin, emax = minetest.get_mapgen_object("voxelmanip") + local area = VoxelArea:new{MinEdge = emin, MaxEdge = emax} + local data = vm:get_data(dbuf) + + local x11 = emax.x -- Limits of mapchunk plus mapblock shell + local y11 = emax.y + local z11 = emax.z + local x00 = emin.x + local y00 = emin.y + local z00 = emin.z + + local ystride = x1 - x0 + 1 + local zstride = ystride * ystride + local chulens = {x = ystride, y = ystride, z = ystride} + local minposxyz = {x = x0, y = y0, z = z0} + + nobj_cave = nobj_cave or minetest.get_perlin_map(np_cave, chulens) + local nvals_cave = nobj_cave:get3dMap_flat(minposxyz, nbuf_cave) + + for y = y00, y11 do -- Y loop first to minimise tcave calculations + local tcave + local in_chunk_y = false + if y >= y0 and y <= y1 then + if y > yblmax then + tcave = TCAVE + ((y - yblmax) / BLEND) ^ 2 + else + tcave = TCAVE + end + in_chunk_y = true + end + + for z = z00, z11 do + local vi = area:index(x00, y, z) -- Initial voxelmanip index + local ni + local in_chunk_yz = in_chunk_y and z >= z0 and z <= z1 + + for x = x00, x11 do + if in_chunk_yz and x == x0 then + -- Initial noisemap index + ni = (z - z0) * zstride + (y - y0) * ystride + 1 + end + local in_chunk_yzx = in_chunk_yz and x >= x0 and x <= x1 -- In mapchunk + + local id = data[vi] -- Existing node + -- Cave air, cave liquids and dungeons are overgenerated, + -- convert these throughout mapchunk plus shell + if id == c_air or -- Air and liquids to air + id == c_lava_source or + id == c_lava_flowing or + id == c_water_source or + id == c_water_flowing then + data[vi] = c_air + -- Dungeons are preserved so we don't need + -- to check for cavern in the shell + elseif id == c_cobble or -- Dungeons (preserved) to netherbrick + id == c_mossycobble or + id == c_stair_cobble then + data[vi] = c_netherbrick + end + + if in_chunk_yzx then -- In mapchunk + if nvals_cave[ni] > tcave then -- Only excavate cavern in mapchunk + data[vi] = c_air + elseif id == c_mese then -- Mese block to lava + data[vi] = c_lava_source + elseif id == c_stone_with_gold or -- Precious ores to glowstone + id == c_stone_with_mese or + id == c_stone_with_diamond then + data[vi] = c_glowstone + elseif id == c_gravel or -- Blob ore to nethersand + id == c_dirt or + id == c_sand then + data[vi] = c_nethersand + else -- All else to netherstone + data[vi] = c_netherrack + end + + ni = ni + 1 -- Only increment noise index in mapchunk + end + + vi = vi + 1 + end + end + end + + vm:set_data(data) + vm:set_lighting({day = 0, night = 0}) + vm:calc_lighting() + vm:update_liquids() + vm:write_to_map() +end) + + +-- use knowledge of the nether mapgen algorithm to return a suitable ground level for placing a portal. +function nether.find_nether_ground_y(target_x, target_z, start_y) + local nobj_cave_point = minetest.get_perlin(np_cave) + local air = 0 -- Consecutive air nodes found + + for y = start_y, start_y - 4096, -1 do + local nval_cave = nobj_cave_point:get3d({x = target_x, y = y, z = target_z}) + + if nval_cave > TCAVE then -- Cavern + air = air + 1 + else -- Not cavern, check if 4 nodes of space above + if air >= 4 then + -- Check volume for non-natural nodes + local minp = {x = target_x - 1, y = y , z = target_z - 2} + local maxp = {x = target_x + 2, y = y + 4, z = target_z + 2} + if nether.volume_is_natural(minp, maxp) then + return y + 1 + else -- Restart search a little lower + nether.find_nether_ground_y(target_x, target_z, y - 16) + end + else -- Not enough space, reset air to zero + air = 0 + end + end + end + + return start_y -- Fallback +end \ No newline at end of file diff --git a/nodes.lua b/nodes.lua new file mode 100644 index 0000000..14e3bd2 --- /dev/null +++ b/nodes.lua @@ -0,0 +1,143 @@ +--[[ + + Nether mod for minetest + + Copyright (C) 2013 PilzAdam + + Permission to use, copy, modify, and/or distribute this software for + any purpose with or without fee is hereby granted, provided that the + above copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL + WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR + BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES + OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + SOFTWARE. + +]]-- + +local S = nether.get_translator + +-- Portal/wormhole node + +nether.register_wormhole_node("nether:portal", { + description = S("Nether Portal"), + post_effect_color = { + -- post_effect_color can't be changed dynamically in Minetest like the portal colour is. + -- If you need a different post_effect_color then use register_wormhole_node to create + -- another wormhole node and set it as the wormhole_node_name in your portaldef. + -- Hopefully this colour is close enough to magenta to work with the traditional magenta + -- portals, close enough to red to work for a red portal, and also close enough to red to + -- work with blue & cyan portals - since blue portals are sometimes portrayed as being red + -- from the opposite side / from the inside. + a = 160, r = 128, g = 0, b = 80 + } +}) + + +-- Nether nodes + +minetest.register_node("nether:rack", { + description = S("Netherrack"), + tiles = {"nether_rack.png"}, + is_ground_content = true, + groups = {cracky = 3, level = 2}, + sounds = default.node_sound_stone_defaults(), +}) + +minetest.register_node("nether:sand", { + description = S("Nethersand"), + tiles = {"nether_sand.png"}, + is_ground_content = true, + groups = {crumbly = 3, level = 2, falling_node = 1}, + sounds = default.node_sound_gravel_defaults({ + footstep = {name = "default_gravel_footstep", gain = 0.45}, + }), +}) + +minetest.register_node("nether:glowstone", { + description = S("Glowstone"), + tiles = {"nether_glowstone.png"}, + is_ground_content = true, + light_source = 14, + paramtype = "light", + groups = {cracky = 3, oddly_breakable_by_hand = 3}, + sounds = default.node_sound_glass_defaults(), +}) + +minetest.register_node("nether:brick", { + description = S("Nether Brick"), + tiles = {"nether_brick.png"}, + is_ground_content = false, + groups = {cracky = 2, level = 2}, + sounds = default.node_sound_stone_defaults(), +}) + +local fence_texture = + "default_fence_overlay.png^nether_brick.png^default_fence_overlay.png^[makealpha:255,126,126" + +minetest.register_node("nether:fence_nether_brick", { + description = S("Nether Brick Fence"), + drawtype = "fencelike", + tiles = {"nether_brick.png"}, + inventory_image = fence_texture, + wield_image = fence_texture, + paramtype = "light", + sunlight_propagates = true, + is_ground_content = false, + selection_box = { + type = "fixed", + fixed = {-1/7, -1/2, -1/7, 1/7, 1/2, 1/7}, + }, + groups = {cracky = 2, level = 2}, + sounds = default.node_sound_stone_defaults(), +}) + + +-- Register stair and slab + +stairs.register_stair_and_slab( + "nether_brick", + "nether:brick", + {cracky = 2, level = 2}, + {"nether_brick.png"}, + S("Nether Stair"), + S("Nether Slab"), + default.node_sound_stone_defaults() +) + +-- StairsPlus + +if minetest.get_modpath("moreblocks") then + stairsplus:register_all( + "nether", "brick", "nether:brick", { + description = S("Nether Brick"), + groups = {cracky = 2, level = 2}, + tiles = {"nether_brick.png"}, + sounds = default.node_sound_stone_defaults(), + }) +end + + +-- Crafting + +minetest.register_craft({ + output = "nether:brick 4", + recipe = { + {"nether:rack", "nether:rack"}, + {"nether:rack", "nether:rack"}, + } +}) + +minetest.register_craft({ + output = "nether:fence_nether_brick 6", + recipe = { + {"nether:brick", "nether:brick", "nether:brick"}, + {"nether:brick", "nether:brick", "nether:brick"}, + }, +}) + + diff --git a/portal_api.lua b/portal_api.lua new file mode 100644 index 0000000..13ab3ca --- /dev/null +++ b/portal_api.lua @@ -0,0 +1,2257 @@ +--[[ + + Portal API for Minetest + + See portal_api.txt for documentation + + -- + + Copyright (C) 2020 Treer + + Permission to use, copy, modify, and/or distribute this software for + any purpose with or without fee is hereby granted, provided that the + above copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL + WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR + BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES + OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + SOFTWARE. + +]]-- + +local DEBUG = false +local DEBUG_IGNORE_MODSTORAGE = false -- setting true prevents portals from knowing where other portals are, forcing find_realm_anchorpos() etc. to be executed every time + +nether.registered_portals = {} +nether.registered_portals_count = 0 + +-- Exposes a list of node names that are used as frame nodes by registered portals +nether.is_frame_node = {} + + +-- gives the colour values in nether_portals_palette.png that are used by the wormhole colorfacedir +-- hardware colouring. +nether.portals_palette = { + [0] = {r = 128, g = 0, b = 128, asString = "#800080"}, -- traditional/magenta + [1] = {r = 0, g = 0, b = 0, asString = "#000000"}, -- black + [2] = {r = 19, g = 19, b = 255, asString = "#1313FF"}, -- blue + [3] = {r = 55, g = 168, b = 0, asString = "#37A800"}, -- green + [4] = {r = 141, g = 237, b = 255, asString = "#8DEDFF"}, -- cyan + [5] = {r = 221, g = 0, b = 0, asString = "#DD0000"}, -- red + [6] = {r = 255, g = 240, b = 0, asString = "#FFF000"}, -- yellow + [7] = {r = 255, g = 255, b = 255, asString = "#FFFFFF"} -- white +} + + +if minetest.get_mod_storage == nil then + error(nether.modname .. " does not support Minetest versions earlier than 0.4.16", 0) +end + +--[[ + +Positions +========= + +p1 & p2 p1 and p2 is the system used by earlier versions of the nether mod, which the portal_api + is forwards and backwards compatible with. + p1 is the bottom/west/south corner of the portal, and p2 is the opposite corner, together + they define the bounding volume for the portal. + The value of p1 and p2 is kept in the metadata of every node in the portal + +WormholePos The location of the node that a portal's target is set to, and a player is teleported + to. It can also be used to test whether a portal is active. + +AnchorPos Introduced by the portal_api. Coordinates for portals are normally given in terms of + the AnchorPos. The AnchorPos does not change with portal orientation - portals rotate + around the AnchorPos. Ideally an AnchorPos would be near the bottom center of a portal + shape, but this is not the case with PortalShape_Traditional to keep comptaibility with + earlier versions of the nether mod. + Usually an orientation is required with an AnchorPos. + + Orientation is yaw, either 0 or 90, 0 meaning a portal that faces north/south - i.e. obsidian + running east/west. + +TimerPos The portal_api replaces ABMs with a single node timer per portal, and the TimerPos is the + node in which that timer is located. Extra metadata is also kept in the TimerPos node. + + +Portal shapes +============= + + + For the PortalShape_Traditional implementation, p1, p2, anchorPos, wormholdPos and TimerPos are defined + as follows: + . + +--------+--------+--------+--------+ + | | Frame | | + | | | | p2 | + +--------+--------+--------+--------+ + | | | | + | | | | + +--------+ + +--------+ + | | Wormhole | | + | | | | + +--------+ + +--------+ + | |Wormhole | | + | | Pos | | + +--------+--------+--------+--------+ + AnchorPos|TimerPos| | | + | p1 | | | | + +--------+--------+--------+--------+ + + +X/East or +Z/North -----> + +A better location for AnchorPos would be directly under WormholePos, as it's more centered +and you don't need to know the portal's orientation to find AnchorPos from the WormholePos +or vice-versa, however AnchorPos is in the bottom/south/west-corner to keep compatibility +with earlier versions of nether mod (which only records portal corners p1 & p2 in the node +metadata). + +]] + +local __ = {name = "air", prob = 0} +local AA = {name = "air", prob = 255, force_place = true} +local OO = {name = "default:obsidian", prob = 255, force_place = true} + +-- This object defines a portal's shape, segregating the shape logic code from portal behaviour code. +-- You can create a new "PortalShape" definition object which implements the same +-- functions if you wish to register a custom shaped portal in register_portal(). Examples of other +-- shapes follow after PortalShape_Traditional. +-- Since it's symmetric, this PortalShape definition has only implemented orientations of 0 and 90 +nether.PortalShape_Traditional = { + name = "Traditional", + size = vector.new(4, 5, 1), -- size of the portal, and not necessarily the size of the schematic, + -- which may clear area around the portal. + is_horizontal = false, -- whether the wormhole is a vertical or horizontal surface + diagram_image = { + image = "nether_book_diagram_traditional.png", -- The diagram to be shown in the Book of Portals + width = 142, + height = 305 + }, + + -- returns the coords for minetest.place_schematic() that will place the schematic on the anchorPos + get_schematicPos_from_anchorPos = function(anchorPos, orientation) + assert(orientation, "no orientation passed") + if orientation == 0 then + return {x = anchorPos.x, y = anchorPos.y, z = anchorPos.z - 2} + else + return {x = anchorPos.x - 2, y = anchorPos.y, z = anchorPos.z } + end + end, + + get_wormholePos_from_anchorPos = function(anchorPos, orientation) + assert(orientation, "no orientation passed") + if orientation == 0 then + return {x = anchorPos.x + 1, y = anchorPos.y + 1, z = anchorPos.z } + else + return {x = anchorPos.x, y = anchorPos.y + 1, z = anchorPos.z + 1} + end + end, + + get_anchorPos_from_wormholePos = function(wormholePos, orientation) + assert(orientation, "no orientation passed") + if orientation == 0 then + return {x = wormholePos.x - 1, y = wormholePos.y - 1, z = wormholePos.z } + else + return {x = wormholePos.x, y = wormholePos.y - 1, z = wormholePos.z - 1} + end + end, + + -- p1 and p2 are used to keep maps compatible with earlier versions of this mod. + -- p1 is the bottom/west/south corner of the portal, and p2 is the opposite corner, together + -- they define the bounding volume for the portal. + get_p1_and_p2_from_anchorPos = function(self, anchorPos, orientation) + assert(orientation, "no orientation passed") + assert(self ~= nil and self.name == nether.PortalShape_Traditional.name, "Must pass self as first argument, or use shape:func() instead of shape.func()") + local p1 = anchorPos -- PortalShape_Traditional puts the anchorPos at p1 for backwards&forwards compatibility + local p2 + + if orientation == 0 then + p2 = {x = p1.x + self.size.x - 1, y = p1.y + self.size.y - 1, z = p1.z } + else + p2 = {x = p1.x, y = p1.y + self.size.y - 1, z = p1.z + self.size.x - 1} + end + return p1, p2 + end, + + get_anchorPos_and_orientation_from_p1_and_p2 = function(p1, p2) + if p1.z == p2.z then + return p1, 0 + elseif p1.x == p2.x then + return p1, 90 + else + -- this KISS implementation will break you've made a 3D PortalShape definition + minetest.log("error", "get_anchorPos_and_orientation_from_p1_and_p2 failed on p1=" .. minetest.pos_to_string(p1) .. " p2=" .. minetest.pos_to_string(p2)) + end + end, + + -- returns true if function was applied to all frame nodes + apply_func_to_frame_nodes = function(anchorPos, orientation, func) + -- a 4x5 portal is small enough that hardcoded positions is simpler that procedural code + local shortCircuited + if orientation == 0 then + -- use short-circuiting of boolean evaluation to allow func() to cause an abort by returning true + shortCircuited = + func({x = anchorPos.x + 0, y = anchorPos.y, z = anchorPos.z}) or + func({x = anchorPos.x + 1, y = anchorPos.y, z = anchorPos.z}) or + func({x = anchorPos.x + 2, y = anchorPos.y, z = anchorPos.z}) or + func({x = anchorPos.x + 3, y = anchorPos.y, z = anchorPos.z}) or + func({x = anchorPos.x + 0, y = anchorPos.y + 4, z = anchorPos.z}) or + func({x = anchorPos.x + 1, y = anchorPos.y + 4, z = anchorPos.z}) or + func({x = anchorPos.x + 2, y = anchorPos.y + 4, z = anchorPos.z}) or + func({x = anchorPos.x + 3, y = anchorPos.y + 4, z = anchorPos.z}) or + + func({x = anchorPos.x, y = anchorPos.y + 1, z = anchorPos.z}) or + func({x = anchorPos.x, y = anchorPos.y + 2, z = anchorPos.z}) or + func({x = anchorPos.x, y = anchorPos.y + 3, z = anchorPos.z}) or + func({x = anchorPos.x + 3, y = anchorPos.y + 1, z = anchorPos.z}) or + func({x = anchorPos.x + 3, y = anchorPos.y + 2, z = anchorPos.z}) or + func({x = anchorPos.x + 3, y = anchorPos.y + 3, z = anchorPos.z}) + else + shortCircuited = + func({x = anchorPos.x, y = anchorPos.y, z = anchorPos.z + 0}) or + func({x = anchorPos.x, y = anchorPos.y, z = anchorPos.z + 1}) or + func({x = anchorPos.x, y = anchorPos.y, z = anchorPos.z + 2}) or + func({x = anchorPos.x, y = anchorPos.y, z = anchorPos.z + 3}) or + func({x = anchorPos.x, y = anchorPos.y + 4, z = anchorPos.z + 0}) or + func({x = anchorPos.x, y = anchorPos.y + 4, z = anchorPos.z + 1}) or + func({x = anchorPos.x, y = anchorPos.y + 4, z = anchorPos.z + 2}) or + func({x = anchorPos.x, y = anchorPos.y + 4, z = anchorPos.z + 3}) or + + func({x = anchorPos.x, y = anchorPos.y + 1, z = anchorPos.z }) or + func({x = anchorPos.x, y = anchorPos.y + 2, z = anchorPos.z }) or + func({x = anchorPos.x, y = anchorPos.y + 3, z = anchorPos.z }) or + func({x = anchorPos.x, y = anchorPos.y + 1, z = anchorPos.z + 3}) or + func({x = anchorPos.x, y = anchorPos.y + 2, z = anchorPos.z + 3}) or + func({x = anchorPos.x, y = anchorPos.y + 3, z = anchorPos.z + 3}) + end + return not shortCircuited + end, + + -- returns true if function was applied to all wormhole nodes + apply_func_to_wormhole_nodes = function(anchorPos, orientation, func) + local shortCircuited + if orientation == 0 then + local wormholePos = {x = anchorPos.x + 1, y = anchorPos.y + 1, z = anchorPos.z} + -- use short-circuiting of boolean evaluation to allow func() to cause an abort by returning true + shortCircuited = + func({x = wormholePos.x + 0, y = wormholePos.y + 0, z = wormholePos.z}) or + func({x = wormholePos.x + 1, y = wormholePos.y + 0, z = wormholePos.z}) or + func({x = wormholePos.x + 0, y = wormholePos.y + 1, z = wormholePos.z}) or + func({x = wormholePos.x + 1, y = wormholePos.y + 1, z = wormholePos.z}) or + func({x = wormholePos.x + 0, y = wormholePos.y + 2, z = wormholePos.z}) or + func({x = wormholePos.x + 1, y = wormholePos.y + 2, z = wormholePos.z}) + else + local wormholePos = {x = anchorPos.x, y = anchorPos.y + 1, z = anchorPos.z + 1} + shortCircuited = + func({x = wormholePos.x, y = wormholePos.y + 0, z = wormholePos.z + 0}) or + func({x = wormholePos.x, y = wormholePos.y + 0, z = wormholePos.z + 1}) or + func({x = wormholePos.x, y = wormholePos.y + 1, z = wormholePos.z + 0}) or + func({x = wormholePos.x, y = wormholePos.y + 1, z = wormholePos.z + 1}) or + func({x = wormholePos.x, y = wormholePos.y + 2, z = wormholePos.z + 0}) or + func({x = wormholePos.x, y = wormholePos.y + 2, z = wormholePos.z + 1}) + end + return not shortCircuited + end, + + -- Check for whether the portal is blocked in, and if so then provide a safe way + -- on one side for the player to step out of the portal. Suggest including a roof + -- incase the portal was blocked with lava flowing from above. + -- If portal can appear in mid-air then can also check for that and add a platform. + disable_portal_trap = function(anchorPos, orientation) + assert(orientation, "no orientation passed") + + -- Not implemented yet. It may not need to be implemented because if you + -- wait in a portal long enough you teleport again. So a trap portal would have to link + -- to one of two blocked-in portals which link to each other - which is possible, but + -- quite extreme. + end, + + schematic = { + size = {x = 4, y = 5, z = 5}, + data = { -- note that data is upside down + __,__,__,__, + AA,AA,AA,AA, + AA,AA,AA,AA, + AA,AA,AA,AA, + AA,AA,AA,AA, + + __,__,__,__, + AA,AA,AA,AA, + AA,AA,AA,AA, + AA,AA,AA,AA, + AA,AA,AA,AA, + + OO,OO,OO,OO, + OO,AA,AA,OO, + OO,AA,AA,OO, + OO,AA,AA,OO, + OO,OO,OO,OO, + + __,__,__,__, + AA,AA,AA,AA, + AA,AA,AA,AA, + AA,AA,AA,AA, + AA,AA,AA,AA, + + __,__,__,__, + AA,AA,AA,AA, + AA,AA,AA,AA, + AA,AA,AA,AA, + AA,AA,AA,AA, + } + } +} -- End of PortalShape_Traditional class + + +-- Example alternative PortalShape +nether.PortalShape_Circular = { + name = "Circular", + size = vector.new(7, 7, 1), -- size of the portal, and not necessarily the size of the schematic, + -- which may clear area around the portal. + is_horizontal = false, -- whether the wormhole is a vertical or horizontal surface + diagram_image = { + image = "nether_book_diagram_circular.png", -- The diagram to be shown in the Book of Portals + width = 149, + height = 243 + }, + + -- returns the coords for minetest.place_schematic() that will place the schematic on the anchorPos + get_schematicPos_from_anchorPos = function(anchorPos, orientation) + assert(orientation, "no orientation passed") + if orientation == 0 then + return {x = anchorPos.x - 3, y = anchorPos.y, z = anchorPos.z - 3} + else + return {x = anchorPos.x - 3, y = anchorPos.y, z = anchorPos.z - 3 } + end + end, + + get_wormholePos_from_anchorPos = function(anchorPos, orientation) + -- wormholePos is the node above anchorPos + return {x = anchorPos.x, y = anchorPos.y + 1, z = anchorPos.z} + end, + + get_anchorPos_from_wormholePos = function(wormholePos, orientation) + -- wormholePos is the node above anchorPos + return {x = wormholePos.x, y = wormholePos.y - 1, z = wormholePos.z} + end, + + -- p1 and p2 are used to keep maps compatible with earlier versions of this mod. + -- p1 is the bottom/west/south corner of the portal, and p2 is the opposite corner, together + -- they define the bounding volume for the portal. + get_p1_and_p2_from_anchorPos = function(self, anchorPos, orientation) + assert(orientation, "no orientation passed") + assert(self ~= nil and self.name == nether.PortalShape_Circular.name, "Must pass self as first argument, or use shape:func() instead of shape.func()") + local p1 + local p2 + + if orientation == 0 then + p1 = {x = anchorPos.x - 3, y = anchorPos.y, z = anchorPos.z } + p2 = {x = p1.x + self.size.x - 1, y = p1.y + self.size.y - 1, z = p1.z } + else + p1 = {x = anchorPos.x, y = anchorPos.y, z = anchorPos.z - 3 } + p2 = {x = p1.x, y = p1.y + self.size.y - 1, z = p1.z + self.size.x - 1} + end + return p1, p2 + end, + + get_anchorPos_and_orientation_from_p1_and_p2 = function(p1, p2) + if p1.z == p2.z then + return {x= p1.x + 3, y = p1.y, z = p1.z }, 0 + elseif p1.x == p2.x then + return {x= p1.x, y = p1.y, z = p1.z + 3}, 90 + end + end, + + apply_func_to_frame_nodes = function(anchorPos, orientation, func) + local shortCircuited + if orientation == 0 then + -- use short-circuiting of boolean evaluation to allow func() to cause an abort by returning true + shortCircuited = + func({x = anchorPos.x + 0, y = anchorPos.y + 0, z = anchorPos.z}) or + func({x = anchorPos.x + 1, y = anchorPos.y + 0, z = anchorPos.z}) or func({x = anchorPos.x - 1, y = anchorPos.y + 0, z = anchorPos.z}) or + func({x = anchorPos.x + 2, y = anchorPos.y + 1, z = anchorPos.z}) or func({x = anchorPos.x - 2, y = anchorPos.y + 1, z = anchorPos.z}) or + func({x = anchorPos.x + 3, y = anchorPos.y + 2, z = anchorPos.z}) or func({x = anchorPos.x - 3, y = anchorPos.y + 2, z = anchorPos.z}) or + func({x = anchorPos.x + 3, y = anchorPos.y + 3, z = anchorPos.z}) or func({x = anchorPos.x - 3, y = anchorPos.y + 3, z = anchorPos.z}) or + func({x = anchorPos.x + 3, y = anchorPos.y + 4, z = anchorPos.z}) or func({x = anchorPos.x - 3, y = anchorPos.y + 4, z = anchorPos.z}) or + func({x = anchorPos.x + 2, y = anchorPos.y + 5, z = anchorPos.z}) or func({x = anchorPos.x - 2, y = anchorPos.y + 5, z = anchorPos.z}) or + func({x = anchorPos.x + 1, y = anchorPos.y + 6, z = anchorPos.z}) or func({x = anchorPos.x - 1, y = anchorPos.y + 6, z = anchorPos.z}) or + func({x = anchorPos.x + 0, y = anchorPos.y + 6, z = anchorPos.z}) + else + shortCircuited = + func({x = anchorPos.x, y = anchorPos.y + 0, z = anchorPos.z + 0}) or + func({x = anchorPos.x, y = anchorPos.y + 0, z = anchorPos.z + 1}) or func({x = anchorPos.x, y = anchorPos.y + 0, z = anchorPos.z - 1}) or + func({x = anchorPos.x, y = anchorPos.y + 1, z = anchorPos.z + 2}) or func({x = anchorPos.x, y = anchorPos.y + 1, z = anchorPos.z - 2}) or + func({x = anchorPos.x, y = anchorPos.y + 2, z = anchorPos.z + 3}) or func({x = anchorPos.x, y = anchorPos.y + 2, z = anchorPos.z - 3}) or + func({x = anchorPos.x, y = anchorPos.y + 3, z = anchorPos.z + 3}) or func({x = anchorPos.x, y = anchorPos.y + 3, z = anchorPos.z - 3}) or + func({x = anchorPos.x, y = anchorPos.y + 4, z = anchorPos.z + 3}) or func({x = anchorPos.x, y = anchorPos.y + 4, z = anchorPos.z - 3}) or + func({x = anchorPos.x, y = anchorPos.y + 5, z = anchorPos.z + 2}) or func({x = anchorPos.x, y = anchorPos.y + 5, z = anchorPos.z - 2}) or + func({x = anchorPos.x, y = anchorPos.y + 6, z = anchorPos.z + 1}) or func({x = anchorPos.x, y = anchorPos.y + 6, z = anchorPos.z - 1}) or + func({x = anchorPos.x, y = anchorPos.y + 6, z = anchorPos.z + 0}) + end + return not shortCircuited + end, + + -- returns true if function was applied to all wormhole nodes + apply_func_to_wormhole_nodes = function(anchorPos, orientation, func) + local xRange = 2 + local zRange = 0 + if orientation ~= 0 then + xRange = 0 + zRange = 2 + end + + local xEdge, yEdge, zEdge + local pos = {} + for x = -xRange, xRange do + pos.x = anchorPos.x + x + xEdge = x == -xRange or x == xRange + for z = -zRange, zRange do + zEdge = z == -zRange or z == zRange + pos.z = anchorPos.z + z + for y = 1, 5 do + yEdge = y == 1 or y == 5 + if not (yEdge and xEdge and zEdge) then + pos.y = anchorPos.y + y + if func(pos) then + -- func() caused an abort by returning true + return false + end + end + end + end + end + + return true + end, + + -- Check for whether the portal is blocked in, and if so then provide a safe way + -- on one side for the player to step out of the portal. Suggest including a roof + -- incase the portal was blocked with lava flowing from above. + -- If portal can appear in mid-air then can also check for that and add a platform. + disable_portal_trap = function(anchorPos, orientation) + assert(orientation, "no orientation passed") + + -- Not implemented. + end, + + schematic = { + size = {x = 7, y = 7, z = 7}, + data = { -- note that data is upside down + __,__,__,__,__,__,__, + __,__,__,__,__,__,__, + __,__,AA,AA,AA,__,__, + __,__,AA,AA,AA,__,__, + __,__,AA,AA,AA,__,__, + __,__,__,__,__,__,__, + __,__,__,__,__,__,__, + + __,__,__,__,__,__,__, + __,AA,AA,AA,AA,AA,__, + __,AA,AA,AA,AA,AA,__, + __,AA,AA,AA,AA,AA,__, + __,AA,AA,AA,AA,AA,__, + __,AA,AA,AA,AA,AA,__, + __,__,__,__,__,__,__, + + __,__,__,__,__,__,__, + __,AA,AA,AA,AA,AA,__, + AA,AA,AA,AA,AA,AA,AA, + AA,AA,AA,AA,AA,AA,AA, + AA,AA,AA,AA,AA,AA,AA, + __,AA,AA,AA,AA,AA,__, + __,__,AA,AA,AA,__,__, + + __,__,OO,OO,OO,__,__, + __,OO,AA,AA,AA,OO,__, + OO,AA,AA,AA,AA,AA,OO, + OO,AA,AA,AA,AA,AA,OO, + OO,AA,AA,AA,AA,AA,OO, + __,OO,AA,AA,AA,OO,__, + __,__,OO,OO,OO,__,__, + + __,__,__,__,__,__,__, + __,AA,AA,AA,AA,AA,__, + AA,AA,AA,AA,AA,AA,AA, + AA,AA,AA,AA,AA,AA,AA, + AA,AA,AA,AA,AA,AA,AA, + __,AA,AA,AA,AA,AA,__, + __,__,AA,AA,AA,__,__, + + __,__,__,__,__,__,__, + __,AA,AA,AA,AA,AA,__, + __,AA,AA,AA,AA,AA,__, + __,AA,AA,AA,AA,AA,__, + __,AA,AA,AA,AA,AA,__, + __,AA,AA,AA,AA,AA,__, + __,__,__,__,__,__,__, + + __,__,__,__,__,__,__, + __,__,__,__,__,__,__, + __,__,AA,AA,AA,__,__, + __,__,AA,AA,AA,__,__, + __,__,AA,AA,AA,__,__, + __,__,__,__,__,__,__, + __,__,__,__,__,__,__, + } + } +} -- End of PortalShape_Circular class + + +-- Example alternative PortalShape +-- This platform shape is symmetrical around the y-axis, so the orientation value never matters. +nether.PortalShape_Platform = { + name = "Platform", + size = vector.new(5, 2, 5), -- size of the portal, and not necessarily the size of the schematic, + -- which may clear area around the portal. + is_horizontal = true, -- whether the wormhole is a vertical or horizontal surface + diagram_image = { + image = "nether_book_diagram_platform.png", -- The diagram to be shown in the Book of Portals + width = 200, + height = 130 + }, + + -- returns the coords for minetest.place_schematic() that will place the schematic on the anchorPos + get_schematicPos_from_anchorPos = function(anchorPos, orientation) + return {x = anchorPos.x - 2, y = anchorPos.y, z = anchorPos.z - 2} + end, + + get_wormholePos_from_anchorPos = function(anchorPos, orientation) + -- wormholePos is the node above anchorPos + return {x = anchorPos.x, y = anchorPos.y + 1, z = anchorPos.z} + end, + + get_anchorPos_from_wormholePos = function(wormholePos, orientation) + -- wormholePos is the node above anchorPos + return {x = wormholePos.x, y = wormholePos.y - 1, z = wormholePos.z} + end, + + -- p1 and p2 are used to keep maps compatible with earlier versions of this mod. + -- p1 is the bottom/west/south corner of the portal, and p2 is the opposite corner, together + -- they define the bounding volume for the portal. + get_p1_and_p2_from_anchorPos = function(self, anchorPos, orientation) + assert(self ~= nil and self.name == nether.PortalShape_Platform.name, "Must pass self as first argument, or use shape:func() instead of shape.func()") + local p1 = {x = anchorPos.x - 2, y = anchorPos.y, z = anchorPos.z - 2} + local p2 = {x = anchorPos.x + 2, y = anchorPos.y + 1, z = anchorPos.z + 2} + return p1, p2 + end, + + get_anchorPos_and_orientation_from_p1_and_p2 = function(p1, p2) + return {x= p1.x + 2, y = p1.y, z = p1.z + 2}, 0 + end, + + apply_func_to_frame_nodes = function(anchorPos, orientation, func) + local shortCircuited + local yPlus1 = anchorPos.y + 1 + -- use short-circuiting of boolean evaluation to allow func() to cause an abort by returning true + shortCircuited = + func({x = anchorPos.x - 2, y = yPlus1, z = anchorPos.z - 1}) or func({x = anchorPos.x + 2, y = yPlus1, z = anchorPos.z - 1}) or + func({x = anchorPos.x - 2, y = yPlus1, z = anchorPos.z }) or func({x = anchorPos.x + 2, y = yPlus1, z = anchorPos.z }) or + func({x = anchorPos.x - 2, y = yPlus1, z = anchorPos.z + 1}) or func({x = anchorPos.x + 2, y = yPlus1, z = anchorPos.z + 1}) or + + func({x = anchorPos.x - 1, y = yPlus1, z = anchorPos.z - 2}) or func({x = anchorPos.x - 1, y = yPlus1, z = anchorPos.z + 2}) or + func({x = anchorPos.x , y = yPlus1, z = anchorPos.z - 2}) or func({x = anchorPos.x , y = yPlus1, z = anchorPos.z + 2}) or + func({x = anchorPos.x + 1, y = yPlus1, z = anchorPos.z - 2}) or func({x = anchorPos.x + 1, y = yPlus1, z = anchorPos.z + 2}) or + + func({x = anchorPos.x - 1, y = anchorPos.y, z = anchorPos.z - 1}) or + func({x = anchorPos.x - 1, y = anchorPos.y, z = anchorPos.z }) or + func({x = anchorPos.x - 1, y = anchorPos.y, z = anchorPos.z + 1}) or + func({x = anchorPos.x , y = anchorPos.y, z = anchorPos.z - 1}) or + func({x = anchorPos.x , y = anchorPos.y, z = anchorPos.z }) or + func({x = anchorPos.x , y = anchorPos.y, z = anchorPos.z + 1}) or + func({x = anchorPos.x + 1, y = anchorPos.y, z = anchorPos.z - 1}) or + func({x = anchorPos.x + 1, y = anchorPos.y, z = anchorPos.z }) or + func({x = anchorPos.x + 1, y = anchorPos.y, z = anchorPos.z + 1}) + return not shortCircuited + end, + + -- returns true if function was applied to all wormhole nodes + apply_func_to_wormhole_nodes = function(anchorPos, orientation, func) + local shortCircuited + local yPlus1 = anchorPos.y + 1 + -- use short-circuiting of boolean evaluation to allow func() to cause an abort by returning true + shortCircuited = + func({x = anchorPos.x - 1, y = yPlus1, z = anchorPos.z - 1}) or + func({x = anchorPos.x - 1, y = yPlus1, z = anchorPos.z }) or + func({x = anchorPos.x - 1, y = yPlus1, z = anchorPos.z + 1}) or + func({x = anchorPos.x , y = yPlus1, z = anchorPos.z - 1}) or + func({x = anchorPos.x , y = yPlus1, z = anchorPos.z }) or + func({x = anchorPos.x , y = yPlus1, z = anchorPos.z + 1}) or + func({x = anchorPos.x + 1, y = yPlus1, z = anchorPos.z - 1}) or + func({x = anchorPos.x + 1, y = yPlus1, z = anchorPos.z }) or + func({x = anchorPos.x + 1, y = yPlus1, z = anchorPos.z + 1}) + return not shortCircuited + end, + + -- Check for suffocation + disable_portal_trap = function(anchorPos, orientation) + + -- Not implemented. + end, + + schematic = { + size = {x = 5, y = 5, z = 5}, + data = { -- note that data is upside down + __,__,__,__,__, + OO,OO,OO,OO,OO, + __,AA,AA,AA,__, + __,AA,AA,AA,__, + __,__,__,__,__, + + __,OO,OO,OO,__, + OO,AA,AA,AA,OO, + AA,AA,AA,AA,AA, + AA,AA,AA,AA,AA, + __,AA,AA,AA,__, + + __,OO,OO,OO,__, + OO,AA,AA,AA,OO, + AA,AA,AA,AA,AA, + AA,AA,AA,AA,AA, + __,AA,AA,AA,__, + + __,OO,OO,OO,__, + OO,AA,AA,AA,OO, + AA,AA,AA,AA,AA, + AA,AA,AA,AA,AA, + __,AA,AA,AA,__, + + __,__,__,__,__, + OO,OO,OO,OO,OO, + __,AA,AA,AA,__, + __,AA,AA,AA,__, + __,__,__,__,__, + } + } +} -- End of PortalShape_Platform class + + +--====================================================-- +--======== End of PortalShape implementations ========-- +--====================================================-- + + + + +-- Portal implementation functions -- +-- =============================== -- + +local ignition_item_name +local S = nether.get_translator +local mod_storage = minetest.get_mod_storage() +local meseconsAvailable = minetest.get_modpath("mesecon") ~= nil and minetest.global_exists("mesecon") +local book_added_as_treasure = false + + +local function get_timerPos_from_p1_and_p2(p1, p2) + -- Pick a frame node for the portal's timer. + -- + -- The timer event will need to know the portal definition, which can be determined by + -- what the portal frame is made from, so the timer node should be on the frame. + -- The timer event will also need to know its portal orientation, but unless someone + -- makes a cubic portal shape, orientation can be determined from p1 and p2 in the node's + -- metadata (frame nodes don't have orientation set in param2 like wormhole nodes do). + -- + -- We shouldn't pick p1 or p2 as it's possible for two orthogonal portals to share + -- the same p1, etc. - or at least it was - there's code to try to stop that now. + -- + -- I'll pick the bottom center node of the portal, since that works for rectangular portals + -- and if someone want to make a circular portal then that positon will still likely be part + -- of the frame. + return { + x = math.floor((p1.x + p2.x) / 2), + y = p1.y, + z = math.floor((p1.z + p2.z) / 2), + } +end + +-- orientation is the yaw rotation degrees passed to place_schematic: 0, 90, 180, or 270 +-- color is a value from 0 to 7 corresponding to the color of pixels in nether_portals_palette.png +-- portal_is_horizontal is a bool indicating whether the portal lies flat or stands vertically +local function get_colorfacedir_from_color_and_orientation(color, orientation, portal_is_horizontal) + assert(orientation, "no orientation passed") + + local axis_direction, rotation + local dir = math.floor((orientation % 360) / 90 + 0.5) + + -- if the portal is vertical then node axis direction will be +Y (up) and portal orientation + -- will set the node's rotation. + -- if the portal is horizontal then the node axis direction reflects the yaw orientation and + -- the node's rotation will be whatever's needed to keep the texture horizontal (either 0 or 1) + if portal_is_horizontal then + if dir == 0 then axis_direction = 1 end -- North + if dir == 1 then axis_direction = 3 end -- East + if dir == 2 then axis_direction = 2 end -- South + if dir == 3 then axis_direction = 4 end -- West + rotation = math.floor(axis_direction / 2); -- a rotation is only needed if axis_direction is east or west + else + axis_direction = 0 -- 0 is up, or +Y + rotation = dir + end + + -- wormhole nodes have a paramtype2 of colorfacedir, which means the + -- high 3 bits are palette, followed by 3 direction bits and 2 rotation bits. + -- We set the palette bits and rotation + return rotation + axis_direction * 4 + color * 32 +end + +local function get_orientation_from_colorfacedir(param2) + + local axis_direction = 0 + -- Strip off the top 6 bits to leave the 2 rotation bits, unfortunately MT lua has no bitwise '&' + -- (high 3 bits are palette, followed by 3 direction bits then 2 rotation bits) + if param2 >= 128 then param2 = param2 - 128 end + if param2 >= 64 then param2 = param2 - 64 end + if param2 >= 32 then param2 = param2 - 32 end + if param2 >= 16 then param2 = param2 - 16; axis_direction = axis_direction + 4 end + if param2 >= 8 then param2 = param2 - 8; axis_direction = axis_direction + 2 end + if param2 >= 4 then param2 = param2 - 4; axis_direction = axis_direction + 1 end + + -- if the portal is vertical then node axis direction will be +Y (up) and portal orientation + -- will set the node's rotation. + -- if the portal is horizontal then the node axis direction reflects the yaw orientation and + -- the node's rotation will be whatever's needed to keep the texture horizontal (either 0 or 1) + if axis_direction == 0 or axis_direction == 5 then + -- portal is vertical + return param2 * 90 + else + if axis_direction == 1 then return 0 end + if axis_direction == 3 then return 90 end + if axis_direction == 2 then return 180 end + if axis_direction == 4 then return 270 end + end +end + +-- We want wormhole nodes to only emit mesecon energy orthogonally to the +-- wormhole surface so that the wormhole will not send power to the frame, +-- this allows the portal frame to listen for mesecon energy from external switches/wires etc. +function get_mesecon_emission_rules_from_colorfacedir(param2) + + local axis_direction = 0 + -- Strip off the top 6 bits to leave the 2 rotation bits, unfortunately MT lua has no bitwise '&' + -- (high 3 bits are palette, followed by 3 direction bits then 2 rotation bits) + if param2 >= 128 then param2 = param2 - 128 end + if param2 >= 64 then param2 = param2 - 64 end + if param2 >= 32 then param2 = param2 - 32 end + if param2 >= 16 then param2 = param2 - 16; axis_direction = axis_direction + 4 end + if param2 >= 8 then param2 = param2 - 8; axis_direction = axis_direction + 2 end + if param2 >= 4 then param2 = param2 - 4; axis_direction = axis_direction + 1 end + + -- if the portal is vertical then node axis_direction will be +Y (up) and node rotation + -- will reflect the portal's yaw orientation. + -- If the portal is horizontal then the node axis direction reflects the yaw orientation and + -- the node's rotation will be whatever's needed to keep the texture horizontal (either 0 or 1) + local rules + if axis_direction == 0 or axis_direction == 5 then + -- portal is vertical + rules = {{x = 0, y = 0, z = 1}, {x = 0, y = 0, z = -1}} + if param2 % 2 ~= 0 then + rules = mesecon.rotate_rules_right(rules) + end + else + -- portal is horizontal, only emit up + rules = {{x = 0, y = 1, z = 0}} + end + return rules +end +nether.get_mesecon_emission_rules_from_colorfacedir = get_mesecon_emission_rules_from_colorfacedir -- make the function available to nodes.lua + +-- Combining frame_node_name, p1, and p2 will always be enough to uniquely identify a portal_definition +-- WITHOUT needing to inspect the world. register_portal() will enforce this. +-- This function does not require the portal to be in a loaded chunk. +-- Returns nil if no portal_definition matches the arguments +local function get_portal_definition(frame_node_name, p1, p2) + + local size = vector.add(vector.subtract(p2, p1), 1) + local rotated_size = {x = size.z, y = size.y, z = size.x} + + for _, portal_def in pairs(nether.registered_portals) do + if portal_def.frame_node_name == frame_node_name then + if vector.equals(size, portal_def.shape.size) or vector.equals(rotated_size, portal_def.shape.size) then + return portal_def + end + end + end + return nil +end + +-- Returns a list of all portal_definitions with a frame made of frame_node_name. +-- Ideally no two portal types will be built from the same frame material so this call might be enough +-- to uniquely identify a portal_definition without needing to inspect the world, HOWEVER we shouldn't +-- cramp anyone's style and prohibit non-nether use of obsidian to make portals, so it returns a list. +-- If the list contains more than one item then routines like ignite_portal() will have to search twice +-- for a portal and take twice the CPU. +local function list_portal_definitions_for_frame_node(frame_node_name) + local result = {} + for _, portal_def in pairs(nether.registered_portals) do + if portal_def.frame_node_name == frame_node_name then table.insert(result, portal_def) end + end + return result +end + + +-- Add portal information to mod storage, so new portals may find existing portals near the target location. +-- Do this whenever a portal is created or changes its ignition state +local function store_portal_location_info(portal_name, anchorPos, orientation, ignited) + if not DEBUG_IGNORE_MODSTORAGE then + local key = minetest.pos_to_string(anchorPos) .. " is " .. portal_name + if DEBUG then minetest.chat_send_all("Adding/updating portal in mod_storage: " .. key) end + mod_storage:set_string( + key, + minetest.serialize({orientation = orientation, active = ignited}) + ) + end +end + +-- Remove portal information from mod storage. +-- Do this if a portal frame is destroyed such that it cannot be ignited anymore. +local function remove_portal_location_info(portal_name, anchorPos) + if not DEBUG_IGNORE_MODSTORAGE then + local key = minetest.pos_to_string(anchorPos) .. " is " .. portal_name + if DEBUG then minetest.chat_send_all("Removing portal from mod_storage: " .. key) end + mod_storage:set_string(key, "") + end +end + +-- Returns a table of the nearest portals to anchorPos indexed by distance, based on mod_storage +-- data. +-- Only portals in the same realm as the anchorPos will be returned, even if y_factor is 0. +-- WARNING: Portals are not checked, and inactive portals especially may have been damaged without +-- being removed from the mod_storage data. Check these portals still exist before using them, and +-- invoke remove_portal_location_info() on any found to no longer exist. +-- +-- A y_factor of 0 means y does not affect the distance_limit, a y_factor of 1 means y is included, +-- and a y_factor of 2 would squash the search-sphere by a factor of 2 on the y-axis, etc. +-- Pass a nil or negative distance_limit to indicate no distance limit +local function list_closest_portals(portal_definition, anchorPos, distance_limit, y_factor) + + local result = {} + + if not DEBUG_IGNORE_MODSTORAGE then + + local isRealm = portal_definition.is_within_realm(anchorPos) + if distance_limit == nil then distance_limit = -1 end + if y_factor == nil then y_factor = 1 end + + for key, value in pairs(mod_storage:to_table().fields) do + local closingBrace = key:find(")", 6, true) + if closingBrace ~= nil then + local found_anchorPos = minetest.string_to_pos(key:sub(0, closingBrace)) + if found_anchorPos ~= nil and portal_definition.is_within_realm(found_anchorPos) == isRealm then + local found_name = key:sub(closingBrace + 5) + if found_name == portal_definition.name then + local x = anchorPos.x - found_anchorPos.x + local y = anchorPos.y - found_anchorPos.y + local z = anchorPos.z - found_anchorPos.z + local distance = math.hypot(y * y_factor, math.hypot(x, z)) + if distance <= distance_limit or distance_limit < 0 then + local info = minetest.deserialize(value) or {} + if DEBUG then minetest.chat_send_all("found " .. found_name .. " listed at distance " .. distance .. " (within " .. distance_limit .. ") from dest " .. minetest.pos_to_string(anchorPos) .. ", found: " .. minetest.pos_to_string(found_anchorPos) .. " orientation " .. info.orientation) end + info.anchorPos = found_anchorPos + info.distance = distance + result[distance] = info + end + end + end + end + end + end + return result +end + + +-- the timerNode is used to keep the metadata as that node already needs to be known any time a portal is stopped or run +-- see also ambient_sound_stop() +function ambient_sound_play(portal_definition, soundPos, timerNodeMeta) + if portal_definition.sounds.ambient ~= nil then + local soundLength = portal_definition.sounds.ambient.length + if soundLength == nil then soundLength = 3 end + local lastPlayed = timerNodeMeta:get_int("ambient_sound_last_played") + + -- Using "os.time() % soundLength == 0" is lightweight but means delayed starts, so trying a stored lastPlayed + if os.time() >= lastPlayed + soundLength then + local soundHandle = minetest.sound_play(portal_definition.sounds.ambient, {pos = soundPos, max_hear_distance = 8}) + if timerNodeMeta ~= nil then + timerNodeMeta:set_int("ambient_sound_handle", soundHandle) + timerNodeMeta:set_int("ambient_sound_last_played", os.time()) + end + end + end +end + +-- the timerNode is used to keep the metadata as that node already needs to be known any time a portal is stopped or run +-- see also ambient_sound_play() +function ambient_sound_stop(timerNodeMeta) + if timerNodeMeta ~= nil then + local soundHandle = timerNodeMeta:get_int("ambient_sound_handle") + minetest.sound_fade(soundHandle, -3, 0) + + -- clear the metadata + timerNodeMeta:set_string("ambient_sound_handle", "") + timerNodeMeta:set_string("ambient_sound_last_played", "") + end +end + + +-- WARNING - this is invoked by on_destruct, so you can't assume there's an accesible node at pos +-- Returns true if a portal was found to extinguish +function extinguish_portal(pos, node_name, frame_was_destroyed) + + -- mesecons seems to invoke action_off() 6 times every time you place a block? + if DEBUG then minetest.chat_send_all("extinguish_portal" .. minetest.pos_to_string(pos) .. " " .. node_name) end + + local meta = minetest.get_meta(pos) + local p1 = minetest.string_to_pos(meta:get_string("p1")) + local p2 = minetest.string_to_pos(meta:get_string("p2")) + local target = minetest.string_to_pos(meta:get_string("target")) + if p1 == nil or p2 == nil then + if DEBUG then minetest.chat_send_all(" no active portal found to extinguish") end + return false + end + + local portal_definition = get_portal_definition(node_name, p1, p2) + if portal_definition == nil then + minetest.log("error", "extinguish_portal() invoked on " .. node_name .. " but no registered portal is constructed from " .. node_name) + return false -- no portal frames are made from this type of node + end + + if portal_definition.sounds.extinguish ~= nil then + minetest.sound_play(portal_definition.sounds.extinguish, {pos = p1}) + end + + -- stop timer and ambient sound + local timerPos = get_timerPos_from_p1_and_p2(p1, p2) + minetest.get_node_timer(timerPos):stop() + ambient_sound_stop(minetest.get_meta(timerPos)) + + -- update the ignition state in the portal location info + local anchorPos, orientation = portal_definition.shape.get_anchorPos_and_orientation_from_p1_and_p2(p1, p2) + if frame_was_destroyed then + remove_portal_location_info(portal_definition.name, anchorPos) + else + store_portal_location_info(portal_definition.name, anchorPos, orientation, false) + end + + local frame_node_name = portal_definition.frame_node_name + local wormhole_node_name = portal_definition.wormhole_node_name + + + for x = p1.x, p2.x do + for y = p1.y, p2.y do + for z = p1.z, p2.z do + local clearPos = {x = x, y = y, z = z} + local nn = minetest.get_node(clearPos).name + if nn == frame_node_name or nn == wormhole_node_name then + if nn == wormhole_node_name then + minetest.remove_node(clearPos) + if meseconsAvailable then mesecon.receptor_off(clearPos) end + end + local m = minetest.get_meta(clearPos) + m:set_string("p1", "") + m:set_string("p2", "") + m:set_string("target", "") + m:set_string("portal_type", "") + end + end + end + end + + if target ~= nil then + if DEBUG then minetest.chat_send_all(" attempting to also extinguish target with wormholePos " .. minetest.pos_to_string(target)) end + extinguish_portal(target, node_name) + end + + if portal_definition.on_extinguish ~= nil then + portal_definition.on_extinguish(portal_definition, anchorPos, orientation) + end + + return true +end + + + + +-- Note: will extinguish any portal using the same nodes that are being set +local function set_portal_metadata(portal_definition, anchorPos, orientation, destination_wormholePos, ignite) + + if DEBUG then minetest.chat_send_all("set_portal_metadata(ignite=" .. tostring(ignite) .. ") at " .. minetest.pos_to_string(anchorPos) .. " orient " .. orientation .. ", setting to target " .. minetest.pos_to_string(destination_wormholePos)) end + + -- Portal position is stored in metadata as p1 and p2 to keep maps compatible with earlier versions of this mod. + -- p1 is the bottom/west/south corner of the portal, and p2 is the opposite corner, together + -- they define the bounding volume for the portal. + local p1, p2 = portal_definition.shape:get_p1_and_p2_from_anchorPos(anchorPos, orientation) + local p1_string, p2_string = minetest.pos_to_string(p1), minetest.pos_to_string(p2) + local param2 = get_colorfacedir_from_color_and_orientation(portal_definition.wormhole_node_color, orientation, portal_definition.shape.is_horizontal) + local mesecon_rules + if ignite and meseconsAvailable then mesecon_rules = get_mesecon_emission_rules_from_colorfacedir(param2) end + + local update_aborted-- using closures to allow the updateFunc to return extra information - by setting this variable + + local updateFunc = function(pos) + + local meta = minetest.get_meta(pos) + + if ignite then + local node_name = minetest.get_node(pos).name + if node_name == "air" then + minetest.set_node(pos, {name = portal_definition.wormhole_node_name, param2 = param2}) + if meseconsAvailable then mesecon.receptor_on(pos, mesecon_rules) end + end + + local existing_p1 = meta:get_string("p1") + if existing_p1 ~= "" then + local existing_p2 = meta:get_string("p2") + if existing_p1 ~= p1_string or existing_p2 ~= p2_string then + if DEBUG then minetest.chat_send_all("set_portal_metadata() found existing metadata from another portal: existing_p1 " .. existing_p1 .. ", existing_p2" .. existing_p2 .. ", p1 " .. p1_string .. ", p2 " .. p2_string .. ", will existinguish existing portal...") end + -- this node is already part of another portal, so extinguish that, because nodes only + -- contain a link in the metadata to one portal, and being part of two allows a slew of bugs + extinguish_portal(pos, node_name, false) + + -- clear the metadata to avoid causing a loop if extinguish_portal() fails on this node (e.g. it only works on frame nodes) + meta:set_string("p1", nil) + meta:set_string("p2", nil) + meta:set_string("target", nil) + meta:set_string("portal_type", nil) + + update_aborted = true + return true -- short-circuit the update + end + end + end + + meta:set_string("p1", minetest.pos_to_string(p1)) + meta:set_string("p2", minetest.pos_to_string(p2)) + meta:set_string("target", minetest.pos_to_string(destination_wormholePos)) + + if portal_definition.name ~= "nether_portal" then + -- Legacy portals won't have this extra metadata, so don't rely on it. + -- It's not strictly necessary for PortalShape_Traditional as we know that p1 is part of + -- the frame and we can look up the portal type from p1, p2, and frame node name. + -- Being able to read this from the metadata means other portal shapes needn't have their + -- frame at the timerPos, it may handle unloaded nodes better, and it saves an extra call + -- to minetest.getnode(). + meta:set_string("portal_type", portal_definition.name) + end + end + + repeat + update_aborted = false + portal_definition.shape.apply_func_to_frame_nodes(anchorPos, orientation, updateFunc) + portal_definition.shape.apply_func_to_wormhole_nodes(anchorPos, orientation, updateFunc) + until not update_aborted + + local timerPos = get_timerPos_from_p1_and_p2(p1, p2) + minetest.get_node_timer(timerPos):start(1) + + store_portal_location_info(portal_definition.name, anchorPos, orientation, true) +end + +local function set_portal_metadata_and_ignite(portal_definition, anchorPos, orientation, destination_wormholePos) + set_portal_metadata(portal_definition, anchorPos, orientation, destination_wormholePos, true) +end + + +-- this function returns two bools: portal found, portal is lit +local function is_portal_at_anchorPos(portal_definition, anchorPos, orientation, force_chunk_load) + + local nodes_are_valid -- using closures to allow the check functions to return extra information - by setting this variable + local portal_is_ignited -- using closures to allow the check functions to return extra information - by setting this variable + + local frame_node_name = portal_definition.frame_node_name + local check_frame_Func = function(check_pos) + local foundName = minetest.get_node(check_pos).name + if foundName ~= frame_node_name then + + if force_chunk_load and foundName == "ignore" then + -- area isn't loaded, force loading/emerge of check area + minetest.get_voxel_manip():read_from_map(check_pos, check_pos) + foundName = minetest.get_node(check_pos).name + if DEBUG then minetest.chat_send_all("Forced loading of 'ignore' node at " .. minetest.pos_to_string(check_pos) .. ", got " .. foundName) end + + if foundName ~= frame_node_name then + nodes_are_valid = false + return true -- short-circuit the search + end + else + nodes_are_valid = false + return true -- short-circuit the search + end + end + end + + local wormhole_node_name = portal_definition.wormhole_node_name + local check_wormhole_Func = function(check_pos) + local node_name = minetest.get_node(check_pos).name + if node_name ~= wormhole_node_name then + portal_is_ignited = false; + if node_name ~= "air" then + nodes_are_valid = false + return true -- short-circuit the search + end + end + end + + nodes_are_valid = true + portal_is_ignited = true + portal_definition.shape.apply_func_to_frame_nodes(anchorPos, orientation, check_frame_Func) -- check_frame_Func affects nodes_are_valid, portal_is_ignited + + if nodes_are_valid then + -- a valid frame exists at anchorPos, check the wormhole is either ignited or unobstructed + portal_definition.shape.apply_func_to_wormhole_nodes(anchorPos, orientation, check_wormhole_Func) -- check_wormhole_Func affects nodes_are_valid, portal_is_ignited + end + + return nodes_are_valid, portal_is_ignited and nodes_are_valid -- returns two bools: portal was found, portal is lit +end + +-- Checks pos, and if it's part of a portal or portal frame then three values are returned: anchorPos, orientation, is_ignited +-- where orientation is 0 or 90 (0 meaning a portal that faces north/south - i.e. obsidian running east/west) +local function is_within_portal_frame(portal_definition, pos) + + local width_minus_1 = portal_definition.shape.size.x - 1 + local height_minus_1 = portal_definition.shape.size.y - 1 + local depth_minus_1 = portal_definition.shape.size.z - 1 + + for d = -depth_minus_1, depth_minus_1 do + for w = -width_minus_1, width_minus_1 do + for y = -height_minus_1, height_minus_1 do + + local testAnchorPos_x = {x = pos.x + w, y = pos.y + y, z = pos.z + d} + local portal_found, portal_lit = is_portal_at_anchorPos(portal_definition, testAnchorPos_x, 0, true) + + if portal_found then + return testAnchorPos_x, 0, portal_lit + else + -- try orthogonal orientation + local testForAnchorPos_z = {x = pos.x + d, y = pos.y + y, z = pos.z + w} + portal_found, portal_lit = is_portal_at_anchorPos(portal_definition, testForAnchorPos_z, 90, true) + + if portal_found then return testForAnchorPos_z, 90, portal_lit end + end + end + end + end +end + + +local function build_portal(portal_definition, anchorPos, orientation, destination_wormholePos) + + minetest.place_schematic( + portal_definition.shape.get_schematicPos_from_anchorPos(anchorPos, orientation), + portal_definition.shape.schematic, + orientation, + { -- node replacements + ["default:obsidian"] = portal_definition.frame_node_name, + }, + true + ) + -- set the param2 on wormhole nodes to ensure they are the right color + local wormholeNode = { + name = portal_definition.wormhole_node_name, + param2 = get_colorfacedir_from_color_and_orientation(portal_definition.wormhole_node_color, orientation, portal_definition.shape.is_horizontal) + } + portal_definition.shape.apply_func_to_wormhole_nodes( + anchorPos, + orientation, + function(pos) minetest.swap_node(pos, wormholeNode) end + ) + + if DEBUG then minetest.chat_send_all("Placed " .. portal_definition.name .. " portal schematic at " .. minetest.pos_to_string(portal_definition.shape.get_schematicPos_from_anchorPos(anchorPos, orientation)) .. ", orientation " .. orientation) end + + set_portal_metadata(portal_definition, anchorPos, orientation, destination_wormholePos) + + if portal_definition.on_created ~= nil then + portal_definition.on_created(portal_definition, anchorPos, orientation) + end +end + + +-- Sometimes after a portal is placed, concurrent mapgen routines overwrite it. +-- Make portals immortal for ~20 seconds after creation +local function remote_portal_checkup(elapsed, portal_definition, anchorPos, orientation, destination_wormholePos) + + if DEBUG then minetest.chat_send_all("portal checkup at " .. elapsed .. " seconds") end + + local wormholePos = portal_definition.shape.get_wormholePos_from_anchorPos(anchorPos, orientation) + local wormhole_node = minetest.get_node_or_nil(wormholePos) + + local portalFound, portalLit = false, false + if wormhole_node ~= nil and wormhole_node.name == portal_definition.wormhole_node_name then + -- a wormhole node was there, but check the whole frame is intact + portalFound, portalLit = is_portal_at_anchorPos(portal_definition, anchorPos, orientation, false) + end + + if not portalFound or not portalLit then + -- ruh roh + local message = "Newly created portal at " .. minetest.pos_to_string(anchorPos) .. " was overwritten. Attempting to recreate. Issue spotted after " .. elapsed .. " seconds" + minetest.log("warning", message) + if DEBUG then minetest.chat_send_all("!!! " .. message) end + + -- A pre-existing portal frame wouldn't have been immediately overwritten, so no need to check for one, just place the portal. + build_portal(portal_definition, anchorPos, orientation, destination_wormholePos) + end + + if elapsed < 10 then -- stop checking after ~20 seconds + local delay = elapsed * 2 + minetest.after(delay, remote_portal_checkup, elapsed + delay, portal_definition, anchorPos, orientation, destination_wormholePos) + end +end + + +-- Used to find or build the remote twin after a portal is opened. +-- If a portal is found that is already lit then it will be extinguished first and its destination_wormholePos updated, +-- this is to enforce that portals only link together in mutual pairs. It would be better for gameplay if I didn't apply +-- that restriction, but it would require maintaining an accurate list of every portal that links to a portal so they +-- could be updated if the portal is destroyed. To keep the code simple I'm going to limit portals to only being the +-- destination of one lit portal at a time. +-- * suggested_wormholePos indicates where the portal should be built - note this not an anchorPos! +-- * suggested_orientation is the suggested schematic rotation to use if no useable portal is found at suggested_wormholePos: +-- 0, 90, 180, 270 (0 meaning a portal that faces north/south - i.e. obsidian running east/west) +-- * destination_wormholePos is the wormholePos of the destination portal this one will be linked to. +-- +-- Returns the final (anchorPos, orientation), as they may differ from the anchorPos and orientation that was +-- specified if an existing portal was already found there. +local function locate_or_build_portal(portal_definition, suggested_wormholePos, suggested_orientation, destination_wormholePos) + + if DEBUG then minetest.chat_send_all("locate_or_build_portal() called at wormholePos" .. minetest.pos_to_string(suggested_wormholePos) .. " with suggested orient " .. suggested_orientation .. ", targetted to " .. minetest.pos_to_string(destination_wormholePos)) end + + local result_anchorPos; + local result_orientation; + + -- Searching for an existing portal at wormholePos seems better than at anchorPos, though isn't important + local found_anchorPos, found_orientation, is_ignited = is_within_portal_frame(portal_definition, suggested_wormholePos) -- can be optimized - check for portal at exactly suggested_wormholePos first + + if found_anchorPos ~= nil then + -- A portal is already here, we don't have to build one, though we may need to ignite it + result_anchorPos = found_anchorPos + result_orientation = found_orientation + + if is_ignited then + -- We're about to link to this portal, so if it's already linked to a different portal then + -- extinguish it, to update the state of the about-to-be-orphaned portal. + local result_target_str = minetest.get_meta(result_anchorPos):get_string("target") + local result_target = minetest.string_to_pos(result_target_str) + if result_target ~= nil and vector.equals(result_target, destination_wormholePos) then + -- It already links back to the portal the player is teleporting from, so don't + -- extinguish it or the player's portal will also extinguish. + if DEBUG then minetest.chat_send_all(" Build unnecessary: already a lit portal that links back here at " .. minetest.pos_to_string(found_anchorPos) .. ", orientation " .. result_orientation) end + else + if DEBUG then minetest.chat_send_all(" Build unnecessary: already a lit portal at " .. minetest.pos_to_string(found_anchorPos) .. ", orientation " .. result_orientation .. ", linking to " .. result_target_str .. ". Extinguishing...") end + extinguish_portal(found_anchorPos, portal_definition.frame_node_name, false) + end + else + if DEBUG then minetest.chat_send_all(" Build unnecessary: already an unlit portal at " .. minetest.pos_to_string(found_anchorPos) .. ", orientation " .. result_orientation) end + end + -- ignite the portal + set_portal_metadata_and_ignite(portal_definition, result_anchorPos, result_orientation, destination_wormholePos) + + else + result_orientation = suggested_orientation + result_anchorPos = portal_definition.shape.get_anchorPos_from_wormholePos(suggested_wormholePos, result_orientation) + build_portal(portal_definition, result_anchorPos, result_orientation, destination_wormholePos) + -- make sure portal isn't overwritten by ongoing generation/emerge + minetest.after(2, remote_portal_checkup, 2, portal_definition, result_anchorPos, result_orientation, destination_wormholePos) + end + return result_anchorPos, result_orientation +end + + +-- invoked when a player attempts to turn obsidian nodes into an open portal +-- ignition_node_name is optional +local function ignite_portal(ignition_pos, ignition_node_name) + + if ignition_node_name == nil then ignition_node_name = minetest.get_node(ignition_pos).name end + if DEBUG then minetest.chat_send_all("IGNITE the " .. ignition_node_name .. " at " .. minetest.pos_to_string(ignition_pos)) end + + -- find which sort of portals are made from the node that was clicked on + local portal_definition_list = list_portal_definitions_for_frame_node(ignition_node_name) + + for _, portal_definition in ipairs(portal_definition_list) do + local continue = false -- WRT the for loop, since lua has no continue keyword + + -- check it was a portal frame that the player is trying to ignite + local anchorPos, orientation, is_ignited = is_within_portal_frame(portal_definition, ignition_pos) + if anchorPos == nil then + if DEBUG then minetest.chat_send_all("No " .. portal_definition.name .. " portal frame found at " .. minetest.pos_to_string(ignition_pos)) end + continue = true -- no portal is here, but perhaps there's more than one portal type we need to search for + elseif is_ignited then + -- Found a portal, check its metadata and timer is healthy. + local repair = false + local meta = minetest.get_meta(ignition_pos) + if meta ~= nil then + local p1, p2, target = meta:get_string("p1"), meta:get_string("p2"), meta:get_string("target") + if p1 == "" or p2 == "" or target == "" then + -- metadata is missing, the portal frame node must have been removed without calling + -- on_destruct - perhaps by an ABM, then replaced - presumably by a player. + -- allowing reigniting will repair the portal + if DEBUG then minetest.chat_send_all("Broken portal detected, allowing reignition/repair") end + repair = true + else + if DEBUG then minetest.chat_send_all("This portal links to " .. meta:get_string("target") .. ". p1=" .. meta:get_string("p1") .. " p2=" .. meta:get_string("p2")) end + + -- Check the portal's timer is running, and fix if it's not. + -- A portal's timer can stop running if the game is played without that portal type being + -- registered, e.g. enabling one of the example portals then later disabling it, then enabling it again. + -- (if this is a frequent problem, then change the value of "run_at_every_load" in the lbm) + local timer = minetest.get_node_timer(get_timerPos_from_p1_and_p2(minetest.string_to_pos(p1), minetest.string_to_pos(p2))) + if timer ~= nil and timer:get_timeout() == 0 then + if DEBUG then minetest.chat_send_all("Portal timer was not running: restarting the timer.") end + timer:start(1) + end + end + end + if not repair then return false end -- portal is already ignited (or timer has been fixed) + end + + if continue == false then + if DEBUG then minetest.chat_send_all("Found portal frame. Looked at " .. minetest.pos_to_string(ignition_pos) .. ", found at " .. minetest.pos_to_string(anchorPos) .. " orientation " .. orientation) end + + local destination_anchorPos, destination_orientation + if portal_definition.is_within_realm(ignition_pos) then + destination_anchorPos, destination_orientation = portal_definition.find_surface_anchorPos(anchorPos) + else + destination_anchorPos, destination_orientation = portal_definition.find_realm_anchorPos(anchorPos) + end + if DEBUG and destination_orientation == nil then minetest.chat_send_all("No destination_orientation given") end + if destination_orientation == nil then destination_orientation = orientation end + + if destination_anchorPos == nil then + if DEBUG then minetest.chat_send_all("No portal destination available here!") end + return false + else + local destination_wormholePos = portal_definition.shape.get_wormholePos_from_anchorPos(destination_anchorPos, destination_orientation) + if DEBUG then minetest.chat_send_all("Destination set to " .. minetest.pos_to_string(destination_anchorPos)) end + + -- ignition/BURN_BABY_BURN + set_portal_metadata_and_ignite(portal_definition, anchorPos, orientation, destination_wormholePos) + + if portal_definition.sounds.ignite ~= nil then + local local_wormholePos = portal_definition.shape.get_wormholePos_from_anchorPos(anchorPos, orientation) + minetest.sound_play(portal_definition.sounds.ignite, {pos = local_wormholePos, max_hear_distance = 20}) + end + + if portal_definition.on_ignite ~= nil then + portal_definition.on_ignite(portal_definition, anchorPos, orientation) + end + + return true + end + end + end +end + +-- invoked when a player is standing in a portal +local function ensure_remote_portal_then_teleport(playerName, portal_definition, local_anchorPos, local_orientation, destination_wormholePos) + + local player = minetest.get_player_by_name(playerName) + if player == nil then return end -- player quit the game while teleporting + local playerPos = player:get_pos() + if playerPos == nil then return end -- player quit the game while teleporting + + -- check player is still standing in a portal + playerPos.y = playerPos.y + 0.1 -- Fix some glitches at -8000 + if minetest.get_node(playerPos).name ~= portal_definition.wormhole_node_name then + return -- the player has moved out of the portal + end + + -- debounce - check player is still standing in the *same* portal that called this function + local meta = minetest.get_meta(playerPos) + local local_p1, local_p2 = portal_definition.shape:get_p1_and_p2_from_anchorPos(local_anchorPos, local_orientation) + local p1_at_playerPos = minetest.string_to_pos(meta:get_string("p1")) + if p1_at_playerPos == nil or not vector.equals(local_p1, p1_at_playerPos) then + if DEBUG then minetest.chat_send_all("the player already teleported from " .. minetest.pos_to_string(local_anchorPos) .. ", and is now standing in a different portal - " .. meta:get_string("p1")) end + return -- the player already teleported, and is now standing in a different portal + end + + local dest_wormhole_node = minetest.get_node_or_nil(destination_wormholePos) + + if dest_wormhole_node == nil then + -- area not emerged yet, delay and retry + if DEBUG then minetest.chat_send_all("ensure_remote_portal_then_teleport() could not find anything yet at " .. minetest.pos_to_string(destination_wormholePos)) end + minetest.after(1, ensure_remote_portal_then_teleport, playerName, portal_definition, local_anchorPos, local_orientation, destination_wormholePos) + else + local local_wormholePos = portal_definition.shape.get_wormholePos_from_anchorPos(local_anchorPos, local_orientation) + + if dest_wormhole_node.name == portal_definition.wormhole_node_name then + -- portal exists + + local destination_orientation = get_orientation_from_colorfacedir(dest_wormhole_node.param2) + local destination_anchorPos = portal_definition.shape.get_anchorPos_from_wormholePos(destination_wormholePos, destination_orientation) + portal_definition.shape.disable_portal_trap(destination_anchorPos, destination_orientation) + + -- if the portal is already linked to a different portal then extinguish the other portal and + -- update the target portal to point back at this one. + local remoteMeta = minetest.get_meta(destination_wormholePos) + local remoteTarget = minetest.string_to_pos(remoteMeta:get_string("target")) + if remoteTarget == nil then + if DEBUG then minetest.chat_send_all("Failed to test whether target portal links back to this one") end + elseif not vector.equals(remoteTarget, local_wormholePos) then + if DEBUG then minetest.chat_send_all("Target portal is already linked, extinguishing then relighting to point back at this one") end + extinguish_portal(remoteTarget, portal_definition.frame_node_name, false) + set_portal_metadata_and_ignite( + portal_definition, + destination_anchorPos, + destination_orientation, + local_wormholePos + ) + end + + if DEBUG then minetest.chat_send_all("Teleporting player from wormholePos" .. minetest.pos_to_string(local_wormholePos) .. " to wormholePos" .. minetest.pos_to_string(destination_wormholePos)) end + + -- play the teleport sound + if portal_definition.sounds.teleport ~= nil then + minetest.sound_play(portal_definition.sounds.teleport, {to_player = playerName}) + end + + -- rotate the player if the destination portal is a different orientation + local rotation_angle = math.rad(destination_orientation - local_orientation) + local offset = vector.subtract(playerPos, local_wormholePos) -- preserve player's position in the portal + local rotated_offset = {x = math.cos(rotation_angle) * offset.x - math.sin(rotation_angle) * offset.z, y = offset.y, z = math.sin(rotation_angle) * offset.x + math.cos(rotation_angle) * offset.z} + local new_playerPos = vector.add(destination_wormholePos, rotated_offset) + player:set_pos(new_playerPos) + player:set_look_horizontal(player:get_look_horizontal() + rotation_angle) + + if portal_definition.on_player_teleported ~= nil then + portal_definition.on_player_teleported(portal_definition, player, playerPos, new_playerPos) + end + else + -- no wormhole node at destination - destination portal either needs to be built or ignited. + -- Note: A very rare edge-case that is difficult to set up: + -- If the destination portal is unlit and its frame shares a node with a lit portal that is linked to this + -- portal (but has not been travelled through, thus not linking this portal back to it), then igniting + -- the destination portal will extinguish the portal it's touching, which will extinguish this portal + -- which will leave a confused player. + -- I don't think this is worth preventing, but I document it incase someone describes entering a portal + -- and then the portal turning off. + if DEBUG then minetest.chat_send_all("ensure_remote_portal_then_teleport() saw " .. dest_wormhole_node.name .. " at " .. minetest.pos_to_string(destination_wormholePos) .. " rather than a wormhole. Calling locate_or_build_portal()") end + + local new_dest_anchorPos, new_dest_orientation = locate_or_build_portal(portal_definition, destination_wormholePos, local_orientation, local_wormholePos) + local new_dest_wormholePos = portal_definition.shape.get_wormholePos_from_anchorPos(new_dest_anchorPos, new_dest_orientation) + + if not vector.equals(destination_wormholePos, new_dest_wormholePos) then + -- Update the local portal's target to match where the existing remote portal was found + + if minetest.get_meta(local_anchorPos):get_string("target") == "" then + -- The local portal has been extinguished! + -- Abort setting its metadata as that assumes it is active. + -- This shouldn't happen and may indicate a bug, I trap it incase when the destination + -- portal was found and extinguished, it somehow linked back to the local portal in a + -- misaligned fashion that wasn't recognized as being the local portal and caused the + -- local portal to also be extinguished. + local message = "Local portal at " .. minetest.pos_to_string(local_anchorPos) .. " was extinguished while linking to existing portal at " .. minetest.pos_to_string(new_dest_anchorPos) + minetest.log("error", message) + if DEBUG then minetest.chat_send_all("!ERROR! - " .. message) end + else + destination_wormholePos = new_dest_wormholePos + if DEBUG then minetest.chat_send_all(" updating target to where remote portal was found - " .. minetest.pos_to_string(destination_wormholePos)) end + + set_portal_metadata( + portal_definition, + local_anchorPos, + local_orientation, + destination_wormholePos + ) + end + end + minetest.after(0.1, ensure_remote_portal_then_teleport, playerName, portal_definition, local_anchorPos, local_orientation, destination_wormholePos) + end + end +end + + +-- run_wormhole() is invoked once per second per portal, handling teleportation and particle effects. +-- See get_timerPos_from_p1_and_p2() for an explanation of the timerPos location +function run_wormhole(timerPos, time_elapsed) + + local portal_definition -- will be used inside run_wormhole_node_func() + + local run_wormhole_node_func = function(pos) + + if math.random(2) == 1 then -- lets run only 3 particlespawners instead of 6 per portal + minetest.add_particlespawner({ + amount = 16, + time = 2, + minpos = {x = pos.x - 0.25, y = pos.y - 0.25, z = pos.z - 0.25}, + maxpos = {x = pos.x + 0.25, y = pos.y + 0.25, z = pos.z + 0.25}, + minvel = {x = -0.8, y = -0.8, z = -0.8}, + maxvel = {x = 0.8, y = 0.8, z = 0.8}, + minacc = {x = 0, y = 0, z = 0}, + maxacc = {x = 0, y = 0, z = 0}, + minexptime = 0.5, + maxexptime = 1.7, + minsize = 0.5 * portal_definition.particle_texture_scale, + maxsize = 1.5 * portal_definition.particle_texture_scale, + collisiondetection = false, + texture = portal_definition.particle_texture_colored, + animation = portal_definition.particle_texture_animation, + glow = 5 + }) + end + + for _, obj in ipairs(minetest.get_objects_inside_radius(pos, 1)) do + if obj:is_player() then + local meta = minetest.get_meta(pos) + local destination_wormholePos = minetest.string_to_pos(meta:get_string("target")) + local local_p1 = minetest.string_to_pos(meta:get_string("p1")) + local local_p2 = minetest.string_to_pos(meta:get_string("p2")) + if destination_wormholePos ~= nil and local_p1 ~= nil and local_p2 ~= nil then + + -- force emerge of target area + minetest.get_voxel_manip():read_from_map(destination_wormholePos, destination_wormholePos) -- force load + if minetest.get_node_or_nil(destination_wormholePos) == nil then + minetest.emerge_area(vector.subtract(destination_wormholePos, 4), vector.add(destination_wormholePos, 4)) + end + + local local_anchorPos, local_orientation = portal_definition.shape.get_anchorPos_and_orientation_from_p1_and_p2(local_p1, local_p2) + local playerName = obj:get_player_name() + minetest.after( + 3, -- hopefully target area is emerged in 3 seconds + function() + ensure_remote_portal_then_teleport( + playerName, + portal_definition, + local_anchorPos, + local_orientation, + destination_wormholePos + ) + end + ) + end + end + end + end + + local p1, p2, portal_name + local meta = minetest.get_meta(timerPos) + if meta ~= nil then + p1 = minetest.string_to_pos(meta:get_string("p1")) + p2 = minetest.string_to_pos(meta:get_string("p2")) + portal_name = minetest.string_to_pos(meta:get_string("portal_type")) -- don't rely on this yet until you're sure everything works with old portals that don't have this set + end + if p1 ~= nil and p2 ~= nil then + -- figure out the portal shape so we know where the wormhole nodes will be located + local frame_node_name + if portal_name ~= nil and nether.registered_portals[portal_name] ~= nil then + portal_definition = nether.registered_portals[portal_name] + else + frame_node_name = minetest.get_node(timerPos).name -- timerPos should be a frame node if the shape is traditionalPortalShape + portal_definition = get_portal_definition(frame_node_name, p1, p2) + end + + if portal_definition == nil then + minetest.log("error", "No portal with a \"" .. frame_node_name .. "\" frame is registered. run_wormhole" .. minetest.pos_to_string(timerPos) .. " was invoked but that location contains \"" .. frame_node_name .. "\"") + else + local anchorPos, orientation = portal_definition.shape.get_anchorPos_and_orientation_from_p1_and_p2(p1, p2) + portal_definition.shape.apply_func_to_wormhole_nodes(anchorPos, orientation, run_wormhole_node_func) + + if portal_definition.on_run_wormhole ~= nil then + portal_definition.on_run_wormhole(portal_definition, anchorPos, orientation) + end + + local wormholePos = portal_definition.shape.get_wormholePos_from_anchorPos(anchorPos, orientation) + ambient_sound_play(portal_definition, wormholePos, meta) + end + end +end + + +local function create_book(item_name, inventory_description, inventory_image, title, author, chapters) + + local display_book = function(itemstack, user, pointed_thing) + local player_name = user:get_player_name() + + minetest.sound_play("nether_book_open", {to_player = player_name, gain = 0.25}) + + local formspec = + "size[18,12.122]" .. + "background[0,0;18,11;nether_book_background.png;true]".. + "image_button_exit[17.3,0;0.8,0.8;nether_book_close.png;;]".. + + "label[3.1,0.5;" .. minetest.formspec_escape(title) .. "]" .. + "label[3.6,0.9;" .. author .. "]" + + local image_x_adj = -0.4 + local image_width = 1.6 + local image_padding = 0.06 + + for i, chapter in ipairs(chapters) do + local left_margin = 0.9 + local top_margin = 1.7 + local width = 7.9 + local height = 12.0 + local item_number = i + local items_on_page = math.floor(#chapters / 2) + if i > items_on_page then + -- page 2 + left_margin = 10.1 + top_margin = 0.8 + height = 12.9 + item_number = i - items_on_page + items_on_page = #chapters - items_on_page + end + + local available_height = (height - top_margin) / items_on_page + local y = top_margin + (item_number - 1) * available_height + + -- add chapter title + local title_height = 0 + if chapter.title ~= nil then + title_height = 0.6 + formspec = formspec .. "label[".. left_margin + 1.5 .. "," + .. y .. "; ──══♦♦♦◊ " .. minetest.formspec_escape(chapter.title) .. " ◊♦♦♦══──]" + end + + -- add chapter image + local x_offset = 0 + if chapter.image ~= nil then + x_offset = image_width + image_x_adj + image_padding + + local image_height = image_width / chapter.image.width * chapter.image.height + formspec = formspec .. "image[" .. left_margin + image_x_adj .. "," .. y + title_height .. ";" .. image_width .. "," + .. image_height .. ";" .. chapter.image.image .. "]" + end + + -- add chapter text + formspec = formspec .. "textarea[" .. left_margin + x_offset .. "," .. y + title_height .. ";" .. width - x_offset .. "," + .. available_height - title_height .. ";;" .. minetest.formspec_escape(chapter.text) .. ";]" + end + + minetest.show_formspec(player_name, item_name, formspec) + end + + minetest.register_craftitem(item_name, { + description = inventory_description, + inventory_image = inventory_image, + groups = {book = 1}, + on_use = display_book, + _doc_items_hidden = true, + _doc_items_longdesc = + S("A guidebook for how to build portals to other realms. It can sometimes be found in dungeon chests, however a copy of this book is not needed as its contents are included in this Encyclopedia.") .. "\n" .. + S("Refer: \"Help\" > \"Basics\" > \"Building Portals\""), + }) +end + + +local function add_book_as_treasure() + + book_added_as_treasure = true + + if minetest.get_modpath("loot") then + loot.register_loot({ + weights = { generic = nether.PORTAL_BOOK_LOOT_WEIGHTING * 1000, + books = 100 }, + payload = { stack = "nether:book_of_portals" } + }) + end + + if minetest.get_modpath("dungeon_loot") then + dungeon_loot.register({name = "nether:book_of_portals", chance = nether.PORTAL_BOOK_LOOT_WEIGHTING}) + end + + -- todo: add to Treasurer mod TRMP https://github.com/poikilos/trmp_minetest_game +end + + +-- Returns true if the Help-modpack was installed and Portal instructions were added to it +-- Help-modpack details can be found at https://forum.minetest.net/viewtopic.php?t=15912 +local function add_book_to_help_modpack(chapters) + + local result = false + + if minetest.get_modpath("doc") ~= nil and minetest.global_exists("doc") then + + if minetest.get_modpath("doc_basics") ~= nil then + + local text = S("Portals to other realms can be opened by building a frame in the right shape with the right blocks, then using an item to activate it. A local copy of the guidebook to portals is published below.\n---\n\n") + local images = {} + + for i, chapter in ipairs(chapters) do + if chapter.image ~= nil then + -- Portal chapters have images (from their portalDef.shape) + text = text .. "\n\n\n" .. (i - 1) .. ") " .. chapter.title .. "\n\n" + + local aspect_3_to_2_width = chapter.image.width + local aspect_3_to_2_height = aspect_3_to_2_width / 3 * 2 + if chapter.image.height > aspect_3_to_2_height then + aspect_3_to_2_height = chapter.image.height + aspect_3_to_2_width = aspect_3_to_2_height / 2 * 3 + end + local image_conveted_to_3_2_ratio = + "[combine:"..aspect_3_to_2_width.."x"..aspect_3_to_2_height..":0,0="..chapter.image.image + + images[#images + 1] = {image=image_conveted_to_3_2_ratio} + end + + text = text .. chapter.text + end + + result = doc.add_entry("basics", "portals_api", { + name = S("Building Portals"), + data = { + text = text, + images = images, + aspect_ratio=.5 + } + }) + end + end + return result +end + + +-- Updates nether:book_of_portals +-- A book the player can read to lean how to build the different portals +local function create_book_of_portals() + + local chapters = {} + + local intro_text + -- tell the player how many portal types there are + if nether.registered_portals_count == 1 then + intro_text = S("In all my travels, and time spent in the Great Libraries, I have encountered no shortage of legends surrounding preternatural doorways said to open into other worlds, yet only one can I confirm as being more than merely a story.") + else + intro_text = S("In all my travels, and time spent in the Great Libraries, I have encountered no shortage of legends surrounding preternatural doorways said to open into other worlds, yet only @1 can I confirm as being more than merely stories.", nether.registered_portals_count) + end + + -- tell the player how to ignite portals + local ignition_item_description = "" + if ignition_item_name ~= nil and minetest.registered_items[ignition_item_name] ~= nil then + ignition_item_description = minetest.registered_items[ignition_item_name].description + end + intro_text = intro_text .. + S("\n\nThe key to opening such a doorway is to strike the frame with a @1, at which point the very air inside begins to crackle and glow.", string.lower(ignition_item_description)) + + chapters[#chapters + 1] = {text = intro_text} + + -- Describe how to create each type of portal, or perhaps just give clues or flavor text, + -- but ensure the Nether is always listed first on the first page so other definitions can + -- refer to it (pairs() returns order based on a random hash). + local portalDefs_in_order = {} + if nether.registered_portals["nether_portal"] then + portalDefs_in_order[#portalDefs_in_order + 1] = nether.registered_portals["nether_portal"] + end + for portalName, portalDef in pairs(nether.registered_portals) do + if portalName ~= "nether_portal" then + portalDefs_in_order[#portalDefs_in_order + 1] = portalDef + end + end + for _, portalDef in ipairs(portalDefs_in_order) do + chapters[#chapters + 1] = { + text = portalDef.book_of_portals_pagetext, + image = portalDef.shape.diagram_image, + title = portalDef.title + } + end + + + create_book( + ":nether:book_of_portals", + S("Book of Portals"), + "nether_book_of_portals.png", + S("A definitive guide to Rifts and Portals"), + "Riccard F. Burton", -- perhaps a Richard F. Burton of an alternate universe + chapters + ) + + local using_helpModpack = add_book_to_help_modpack(chapters) + + if not using_helpModpack and not book_added_as_treasure and nether.PORTAL_BOOK_LOOT_WEIGHTING > 0 then + -- Only place the Book of Portals in chests if there are non-Nether (i.e. novel) portals + -- which players need a way to find out about. + if nether.registered_portals_count > 1 or (nether.registered_portals_count == 1 and nether.registered_portals["nether_portal"] == nil) then + add_book_as_treasure() + end + end +end + + +function register_frame_node(frame_node_name) + + -- copy the existing node definition + local node_def = minetest.registered_nodes[frame_node_name] + local extended_node_def = {} + for key, value in pairs(node_def) do extended_node_def[key] = value end + + extended_node_def.replaced_by_portalapi = {} -- allows chaining or restoration of original functions, if necessary + + -- add portal portal functionality + extended_node_def.replaced_by_portalapi.mesecons = extended_node_def.mesecons + extended_node_def.mesecons = {effector = { + action_on = function (pos, node) + if DEBUG then minetest.chat_send_all("portal frame material: mesecons action ON") end + ignite_portal(pos, node.name) + end, + action_off = function (pos, node) + if DEBUG then minetest.chat_send_all("portal frame material: mesecons action OFF") end + extinguish_portal(pos, node.name, false) + end + }} + extended_node_def.replaced_by_portalapi.on_destruct = extended_node_def.on_destruct + extended_node_def.on_destruct = function(pos) + if DEBUG then minetest.chat_send_all("portal frame material: destruct") end + extinguish_portal(pos, frame_node_name, true) + end + extended_node_def.replaced_by_portalapi.on_blast = extended_node_def.on_blast + extended_node_def.on_blast = function(pos, intensity) + if DEBUG then minetest.chat_send_all("portal frame material: blast") end + extinguish_portal(pos, frame_node_name, extended_node_def.replaced_by_portalapi.on_blast == nil) + if extended_node_def.replaced_by_portalapi.on_blast ~= nil then + extended_node_def.replaced_by_portalapi.on_blast(pos, intensity) + else + minetest.remove_node(pos) + end + end + extended_node_def.replaced_by_portalapi.on_timer = extended_node_def.on_timer + extended_node_def.on_timer = function(pos, elapsed) + run_wormhole(pos, elapsed) + return true + end + + -- replace the node with the new extended definition + minetest.register_node(":" .. frame_node_name, extended_node_def) +end + +function unregister_frame_node(frame_node_name) + + -- copy the existing node definition + local node = minetest.registered_nodes[frame_node_name] + local restored_node_def = {} + for key, value in pairs(node) do restored_node_def[key] = value end + + -- remove portal portal functionality + restored_node_def.mesecons = nil + restored_node_def.on_destruct = nil + restored_node_def.on_timer = nil + restored_node_def.replaced_by_portalapi = nil + + if node.replaced_by_portalapi ~= nil then + for key, value in pairs(node.replaced_by_portalapi) do restored_node_def[key] = value end + end + + -- replace the node with the restored definition + minetest.register_node(":" .. frame_node_name, restored_node_def) +end + + +-- check for mistakes people might make in custom shape definitions +function test_shapedef_is_valid(shape_defintion) + assert(shape_defintion ~= nil, "shape definition cannot be nil") + assert(shape_defintion.name ~= nil, "shape definition must have a name") + + local result = true + + local origin = vector.new() + local p1, p2 = shape_defintion:get_p1_and_p2_from_anchorPos(origin, 0) + assert(vector.equals(shape_defintion.size, vector.add(vector.subtract(p2, p1), 1)), "p1 and p2 of shape definition '" .. shape_defintion.name .. "' don't match shapeDef.size") + + assert(shape_defintion.diagram_image ~= nil and shape_defintion.diagram_image.image ~= nil, "Shape definition '" .. shape_defintion.name .. "' does not provide an image for Help/Book of Portals") + assert(shape_defintion.diagram_image.width > 0 and shape_defintion.diagram_image.height > 0, "Shape definition '" .. shape_defintion.name .. "' does not provide the size of the image for Help/Book of Portals") + + -- todo + + return result +end + + +-- check for mistakes people might make in portal definitions +function test_portaldef_is_valid(portal_definition) + + local result = test_shapedef_is_valid(portal_definition.shape) + + assert(portal_definition.wormhole_node_color >= 0 and portal_definition.wormhole_node_color < 8, "portaldef.wormhole_node_color must be between 0 and 7 (inclusive)") + assert(portal_definition.is_within_realm ~= nil, "portaldef.is_within_realm() must be implemented") + assert(portal_definition.find_realm_anchorPos ~= nil, "portaldef.find_realm_anchorPos() must be implemented") + -- todo + + return result +end + + +-- convert portals made with old ABM version of nether mod to use the timer instead +minetest.register_lbm({ + label = "Start portal timer", + name = "nether:start_portal_timer", + nodenames = {"nether:portal"}, + run_at_every_load = false, + action = function(pos, node) + local p1, p2 + local meta = minetest.get_meta(pos) + if meta ~= nil then + p1 = minetest.string_to_pos(meta:get_string("p1")) + p2 = minetest.string_to_pos(meta:get_string("p2")) + end + if p1 ~= nil and p2 ~= nil then + local timerPos = get_timerPos_from_p1_and_p2(p1, p2) + local timer = minetest.get_node_timer(timerPos) + if timer ~= nil then + timer:start(1) + if DEBUG then minetest.chat_send_all("LBM started portal timer " .. minetest.pos_to_string(timerPos)) end + elseif DEBUG then + minetest.chat_send_all("get_node_timer" .. minetest.pos_to_string(timerPos) .. " returned null") + end + end + end +}) + + + +-- Portal API functions -- +-- ==================== -- + + +-- the fallback defaults for wormhole nodedefs +local wormhole_nodedef_default = { + description = S("Portal wormhole"), + tiles = { + "nether_transparent.png", + "nether_transparent.png", + "nether_transparent.png", + "nether_transparent.png", + { + name = "nether_portal.png", + animation = { + type = "vertical_frames", + aspect_w = 16, + aspect_h = 16, + length = 0.5, + }, + }, + { + name = "nether_portal.png", + animation = { + type = "vertical_frames", + aspect_w = 16, + aspect_h = 16, + length = 0.5, + }, + }, + }, + drawtype = "nodebox", + paramtype = "light", + paramtype2 = "colorfacedir", + palette = "nether_portals_palette.png", + post_effect_color = { + -- post_effect_color can't be changed dynamically in Minetest like the portal colour is. + -- If you need a different post_effect_color then use register_wormhole_node() to create + -- another wormhole node with the right post_effect_color and set it as the wormhole_node_name + -- in your portaldef. + -- Hopefully this colour is close enough to magenta to work with the traditional magenta + -- portals, close enough to red to work for a red portal, and also close enough to red to + -- work with blue & cyan portals - since blue portals are sometimes portrayed as being red + -- from the opposite side / from the inside. + a = 160, r = 128, g = 0, b = 80 + }, + sunlight_propagates = true, + use_texture_alpha = true, + walkable = false, + diggable = false, + pointable = false, + buildable_to = false, + is_ground_content = false, + drop = "", + light_source = 5, + alpha = 192, + node_box = { + type = "fixed", + fixed = { + {-0.5, -0.5, -0.1, 0.5, 0.5, 0.1}, + }, + }, + groups = {not_in_creative_inventory = 1}, + mesecons = {receptor = { + state = "on", + rules = function(node) + return nether.get_mesecon_emission_rules_from_colorfacedir(node.param2) + end + }} +} + +-- Call only at load time +function nether.register_wormhole_node(name, nodedef) + assert(name ~= nil, "Unable to register wormhole node: Name is nil") + assert(nodedef ~= nil, "Unable to register wormhole node ''" .. name .. "'': nodedef is nil") + + for key, value in pairs(wormhole_nodedef_default) do + if nodedef[key] == nil then nodedef[key] = value end + end + minetest.register_node(name, nodedef) +end + + +-- The fallback defaults for registered portaldef tables +local portaldef_default = { + title = S("Untitled portal"), + book_of_portals_pagetext = S("We know almost nothing about this portal"), + shape = nether.PortalShape_Traditional, + wormhole_node_name = "nether:portal", + wormhole_node_color = 0, + frame_node_name = "default:obsidian", + particle_texture = "nether_particle.png", + particle_texture_animation = nil, + particle_texture_scale = 1, + sounds = { + ambient = {name = "nether_portal_ambient", gain = 0.6, length = 3}, + ignite = {name = "nether_portal_ignite", gain = 0.7}, + extinguish = {name = "nether_portal_extinguish", gain = 0.6}, + teleport = {name = "nether_portal_teleport", gain = 0.3} + } +} + + +function nether.register_portal(name, portaldef) + + assert(name ~= nil, "Unable to register portal: Name is nil") + assert(portaldef ~= nil, "Unable to register portal ''" .. name .. "'': portaldef is nil") + if nether.registered_portals[name] ~= nil then + minetest.log("error", "Unable to register portal: '" .. name .. "' is already in use") + return false; + end + + portaldef.name = name + portaldef.mod_name = minetest.get_current_modname() + + -- use portaldef_default for any values missing from portaldef or portaldef.sounds + if portaldef.sounds ~= nil then setmetatable(portaldef.sounds, {__index = portaldef_default.sounds}) end + setmetatable(portaldef, {__index = portaldef_default}) + + if portaldef.particle_color == nil then + -- default the particle colours to be the same as the wormhole colour + assert(portaldef.wormhole_node_color >= 0 and portaldef.wormhole_node_color < 8, "portaldef.wormhole_node_color must be between 0 and 7 (inclusive)") + portaldef.particle_color = nether.portals_palette[portaldef.wormhole_node_color].asString + end + if portaldef.particle_texture_colored == nil then + -- Combine the particle texture with the particle color unless a particle_texture_colored was specified. + if type(portaldef.particle_texture) == "table" and portaldef.particle_texture.animation ~= nil then + portaldef.particle_texture_colored = portaldef.particle_texture.name .. "^[colorize:" .. portaldef.particle_color .. ":alpha" + portaldef.particle_texture_animation = portaldef.particle_texture.animation + portaldef.particle_texture_scale = portaldef.particle_texture.scale or 1 + else + portaldef.particle_texture_colored = portaldef.particle_texture .. "^[colorize:" .. portaldef.particle_color .. ":alpha" + end + end + + if portaldef.find_surface_anchorPos == nil then -- default to using find_surface_target_y() + portaldef.find_surface_anchorPos = function(pos) + + local destination_pos = {x = pos.x, y = 0, z = pos.z} + local existing_portal_location, existing_portal_orientation = + nether.find_nearest_working_portal(name, destination_pos, 10, 0) -- a y_factor of 0 makes the search ignore the altitude of the portals (as long as they are outside the realm) + if existing_portal_location ~= nil then + return existing_portal_location, existing_portal_orientation + else + destination_pos.y = nether.find_surface_target_y(destination_pos.x, destination_pos.z, name) + return destination_pos + end + end + end + + if test_portaldef_is_valid(portaldef) then + + -- check whether the portal definition clashes with anyone else's portal + local p1, p2 = portaldef.shape:get_p1_and_p2_from_anchorPos(vector.new(), 0) + local existing_portaldef = get_portal_definition(portaldef.frame_node_name, p1, p2) + if existing_portaldef ~= nil then + minetest.log("error", + portaldef.mod_name .." tried to register a portal '" .. portaldef.name .. "' made of " .. portaldef.frame_node_name .. + ", but it is the same material and shape as the portal '" .. existing_portaldef.name .. "' already registered by " .. existing_portaldef.mod_name .. + ". Edit the values one of those mods uses in its call to nether.register_portal() if you wish to resolve this clash.") + else + -- the new portaldef is good + nether.registered_portals[portaldef.name] = portaldef + + -- Update registered_portals_count + local portalCount = 0 + for _ in pairs(nether.registered_portals) do portalCount = portalCount + 1 end + nether.registered_portals_count = portalCount + + create_book_of_portals() + + if not nether.is_frame_node[portaldef.frame_node_name] then + -- add portal functions to the nodedef being used for the portal frame + register_frame_node(portaldef.frame_node_name) + nether.is_frame_node[portaldef.frame_node_name] = true + end + + return true + end + end + + return false +end + +function nether.unregister_portal(name) + + assert(name ~= nil, "Cannot unregister portal: Name is nil") + + local portaldef = nether.registered_portals[name] + local result = portaldef ~= nil + + if portaldef ~= nil then + nether.registered_portals[name] = nil + + local portals_still_using_frame_node = list_portal_definitions_for_frame_node(portaldef.frame_node_name) + if next(portals_still_using_frame_node) == nil then + -- no portals are using this frame node any more + unregister_frame_node(portaldef.frame_node_name) + nether.is_frame_node[portaldef.frame_node_name] = nil + end + end + + return result +end + +function nether.register_portal_ignition_item(item_name, ignition_failure_sound) + + minetest.override_item(item_name, { + on_place = function(stack, _, pt) + local done = false + if pt.under and nether.is_frame_node[minetest.get_node(pt.under).name] then + done = ignite_portal(pt.under) + if done and not minetest.settings:get_bool("creative_mode") then + stack:take_item() + end + end + if not done and ignition_failure_sound ~= nil then + minetest.sound_play(ignition_failure_sound, {pos = pt.under, max_hear_distance = 10}) + end + + + return stack + end, + }) + + ignition_item_name = item_name +end + +-- use this when determining where to spawn a portal, to avoid overwriting player builds +-- It checks the area for any nodes that aren't ground or trees. +-- (Water also fails this test, unless it is unemerged) +function nether.volume_is_natural(minp, maxp) + local c_air = minetest.get_content_id("air") + local c_ignore = minetest.get_content_id("ignore") + + local vm = minetest.get_voxel_manip() + local pos1 = {x = minp.x, y = minp.y, z = minp.z} + local pos2 = {x = maxp.x, y = maxp.y, z = maxp.z} + local emin, emax = vm:read_from_map(pos1, pos2) + local area = VoxelArea:new({MinEdge = emin, MaxEdge = emax}) + local data = vm:get_data() + + for z = pos1.z, pos2.z do + for y = pos1.y, pos2.y do + local vi = area:index(pos1.x, y, z) + for x = pos1.x, pos2.x do + local id = data[vi] -- Existing node + if DEBUG and id == nil then minetest.chat_send_all("nil block at index " .. vi) end + if id ~= c_air and id ~= c_ignore and id ~= nil then -- checked for common natural or not emerged + local name = minetest.get_name_from_content_id(id) + local nodedef = minetest.registered_nodes[name] + if not nodedef.is_ground_content then + -- trees are natural but not "ground content" + local node_groups = nodedef.groups + if node_groups == nil or (node_groups.tree == nil and node_groups.leaves == nil and node_groups.leafdecay == nil) then + if DEBUG then minetest.chat_send_all("volume_is_natural() found unnatural node " .. name) end + return false + end + end + end + vi = vi + 1 + end + end + end + + if DEBUG then minetest.chat_send_all("Volume is natural") end + return true +end + +-- Can be used when implementing custom find_surface_anchorPos() functions +-- portal_name is optional, providing it allows existing portals on the surface to be reused. +function nether.find_surface_target_y(target_x, target_z, portal_name) + + assert(target_x ~= nil and target_z ~= nil, "Arguments `target_x` and `target_z` cannot be nil when calling find_surface_target_y()") + + -- default to starting the search at -16 (probably underground) if we don't know the + -- surface, like paramat's original code from before get_spawn_level() was available: + -- https://github.com/minetest-mods/nether/issues/5#issuecomment-506983676 + local start_y = -16 + + -- try to spawn on surface first + if minetest.get_spawn_level ~= nil then -- older versions of Minetest don't have this + local surface_level = minetest.get_spawn_level(target_x, target_z) + if surface_level ~= nil then -- test this since get_spawn_level() can return nil over water or steep/high terrain + + -- get_spawn_level() tends to err on the side of caution and spawns the player a + -- block higher than the ground level. The implementation is mapgen specific + -- and -2 seems to be the right correction for v6, v5, carpathian, valleys, and flat, + -- but v7 only needs -1. + -- Perhaps this was not always the case, and -2 may be too much in older versions + -- of minetest, but half-buried portals are perferable to floating ones, and they + -- will clear a suitable hole around themselves. + if minetest.get_mapgen_setting("mg_name") == "v7" then + surface_level = surface_level - 1 + else + surface_level = surface_level - 2 + end + start_y = surface_level + end + end + + for y = start_y, start_y - 256, -16 do + -- Check volume for non-natural nodes + local minp = {x = target_x - 1, y = y - 1, z = target_z - 2} + local maxp = {x = target_x + 2, y = y + 3, z = target_z + 2} + if nether.volume_is_natural(minp, maxp) then + return y + elseif portal_name ~= nil and nether.registered_portals[portal_name] ~= nil then + -- players have built here - don't grief. + -- but reigniting existing portals in portal rooms is fine - desirable even. + local anchorPos, orientation, is_ignited = is_within_portal_frame(nether.registered_portals[portal_name], {x = target_x, y = y, z = target_z}) + if anchorPos ~= nil then + if DEBUG then minetest.chat_send_all("Volume_is_natural check failed, but a portal frame is here " .. minetest.pos_to_string(anchorPos) .. ", so this is still a good target y level") end + return y + end + end + end + + return start_y - 256 -- Fallback +end + + +-- Returns the anchorPos, orientation of the nearest portal, or nil. +-- A y_factor of 0 means y does not affect the distance_limit, a y_factor of 1 means y is included, +-- and a y_factor of 2 would squash the search-sphere by a factor of 2 on the y-axis, etc. +-- Pass a negative distance_limit to indicate no distance limit +function nether.find_nearest_working_portal(portal_name, anchorPos, distance_limit, y_factor) + + local portal_definition = nether.registered_portals[portal_name] + assert(portal_definition ~= nil, "find_nearest_working_portal() called with portal_name '" .. portal_name .. "', but no portal is registered with that name.") + assert(anchorPos ~= nil, "Argument `anchorPos` cannot be nil when calling find_nearest_working_portal()") + + local contenders = list_closest_portals(portal_definition, anchorPos, distance_limit, y_factor) + + -- sort by distance + local dist_list = {} + for dist, _ in pairs(contenders) do table.insert(dist_list, dist) end + table.sort(dist_list) + + for _, dist in ipairs(dist_list) do + local portal_info = contenders[dist] + if DEBUG then minetest.chat_send_all("checking portal from mod_storage at " .. minetest.pos_to_string(portal_info.anchorPos) .. " orientation " .. portal_info.orientation) end + + -- the mod_storage list of portals is unreliable - e.g. it won't know if inactive portals have been + -- destroyed, so check the portal is still there + local portalFound, portalIsActive = is_portal_at_anchorPos(portal_definition, portal_info.anchorPos, portal_info.orientation, true) + + if portalFound then + return portal_info.anchorPos, portal_info.orientation + else + if DEBUG then minetest.chat_send_all("Portal wasn't found, removing portal from mod_storage at " .. minetest.pos_to_string(portal_info.anchorPos) .. " orientation " .. portal_info.orientation) end + -- The portal at that location must have been destroyed + remove_portal_location_info(portal_name, portal_info.anchorPos) + end + end + return nil +end \ No newline at end of file diff --git a/portal_api.txt b/portal_api.txt new file mode 100644 index 0000000..e6cf533 --- /dev/null +++ b/portal_api.txt @@ -0,0 +1,238 @@ +Portal API Reference +==================== + +The portal system used to get to the Nether can be used to create portals +to other realms. + +Pick a node type to have your portals built from, a shape in which the +portals must be built, and provide 3 functions for portals to find their +destination with: + * `find_realm_anchorPos(surface_anchorPos)` + * `find_surface_anchorPos(realm_anchorPos)` + * `is_within_realm(pos)` + +Optionally decorate by choosing portal colors, particles, media etc. + +See `init.lua` and `portal_examples.lua` for examples of 3 different portals. + +Portal code is more efficient when each type of portal uses a different type +of node to build its frame out of - consider creating your own node for +players to build portals from. However it is possible to register more than +one kind of portal with the same frame material — such as obsidian — provided +the size of the PortalShape is distinct from any other type of portal that is +using the same node for its frame, and portal sizes remain small. + + +Realms +------ + +This API uses the concept of a realm for each type of portal. If a portal is +outside its realm then it links to a portal inside the realm, if a portal is +inside its realm then it links to the outside. + +You get to decide what constitutes your realm by implementing the function +`is_within_realm(position)`. + +For example, the Nether realm is defined as existing at a certain depth and +anything above that depth is considered outside the Realm. + +In contrast, the "Surface portal" - an example in portal_examples.lua, only +uses one realm. Its `is_within_realm()` function always returns true so that +any time a portal is opened it will use `find_surface_anchorPos()`. + +Note that the name "find_surface_anchorPos" is a Nether-centric misnomer, as +different types of portals are free to use different definitions of a realm +such that leaving the realm might not be synonymous with travelling to the +surface. + + +Helper functions +---------------- + +* `nether.volume_is_natural(minp, maxp)`: returns a boolean + * use this when determining where to spawn a portal, to avoid overwriting + player builds. It checks the area for any nodes that aren't ground or + trees. + Water will fail this test, unless it is unemerged. + +* `nether.find_surface_target_y(target_x, target_z, portal_name)`: returns a + suitable anchorPos + * Can be used when implementing custom find_surface_anchorPos() functions + * portal_name is optional, providing it allows existing portals on the + surface to be reused. + +* `nether.find_nearest_working_portal(portal_name, anchorPos, distance_limit, y_factor)`: returns + (anchorPos, orientation), or nil if no portal was found within the + distance_limit. + * A y_factor of 0 means y does not affect the distance_limit, a y_factor + of 1 means y is included (the default if y_factor is nil), and a y_factor + of 2 would squash the search-sphere by a factor of 2 on the y-axis, etc. + * Only portals in the same realm as the anchorPos will be returned, even if + y_factor is 0. + * Pass a nil (or negative) distance_limit to indicate no distance limit + + +API functions +------------- + +Call these functions only at load time: + +* `nether.register_portal(name, portal_definition)` + * Returns true on success. Can return false if the portal definition + clashes with a portal already registered by another mod, e.g. if the size + and frame node is not unique. + A false return value should be handled, you could: + * Fall back to using a secondary material for portals to be built with. + * Use error() to exit lua with a message explaining how two mods are + clashing and how it can be resolved. + * Continue without a portal (the reason will be logged for the user). +* `nether.register_portal_ignition_item(name, ignition_failure_sound)` + * ignition_failure_sound is optional, it plays any time an attempt to use + the item occurs if a portal is not ignited. +* `nether.register_wormhole_node(name, nodedef_overrides)` + * Can be used to register wormhole nodes with a different post_effect_color + from the "nether:portal" node. "Post effect color" is the tint the world + takes on when you are standing inside a portal. `post_effect_color` is the + only key/value that is needed in the nodedef_overrides table to achieve that, + but the function allows any nodedef key/value to be specified/overridden. + * After `register_wormhole_node()`, invoke `register_portal()` and include + `wormhole_node_name` in the portal_definition, assigning it the name of the + new wormhole node. +* `nether.unregister_portal(name)` + * Unregisters the portal from the engine, and deletes the entry with key + `name` from `nether.registered_portals` and associated internal tables. + * Returns true on success + * You will probably never need to call this, it exists only for completeness. + + +Portal definition +----------------- + +Used by `nether.register_portal`. + + { + frame_node_name = "default:obsidian", + -- Required. For best results, have your portal constructed of a + -- material nobody else is using. + + shape = nether.PortalShape_Traditional, + -- Optional. + -- Shapes available are: + -- nether.PortalShape_Traditional (default) + -- nether.PortalShape_Circular + -- nether.PortalShape_Platform + -- New shapes can be created, but are beyond the scope of this guide. + + wormhole_node_name = "nether:portal", + -- Optional. Allows a custom wormhole node to be specified. + -- Useful if you want the portals to have a different post_effect_color + -- or texture. + + wormhole_node_color = 0, + -- Optional. Defaults to 0/magenta. + -- A value from 0 to 7 corresponding to the color of pixels in + -- nether_portals_palette.png: + -- 0 traditional/magenta + -- 1 black + -- 2 blue + -- 3 green + -- 4 cyan + -- 5 red + -- 6 yellow + -- 7 white + + particle_color = "#808", + -- Optional. Will default to a colour matching the wormhole_node_color + -- if not specified. + + particle_texture = "image.png", + -- Optional. Hardware colouring (i.e. particle_color) is applied to + -- this texture, use particle_texture_colored instead if you want to + -- use the colors of the image. + -- Animation and particle scale may also be specified, e.g: + -- particle_texture = { + -- name = "nether_particle_anim1.png", + -- animation = { + -- type = "vertical_frames", + -- aspect_w = 7, + -- aspect_h = 7, + -- length = 1, + -- }, + -- scale = 1.5 + -- }, + -- See lua_api.txt for Tile Animation definition + -- Some animated and non-animated textures are provided by this mod: + -- nether_particle.png (original) + -- nether_particle_anim1.png (stars) + -- nether_particle_anim2.png (bubbles) + -- nether_particle_anim3.png (sparks) + -- nether_particle_anim4.png (particles) + + title = "Gateway to Moria", + -- Optional. Provides a title for the portal. + -- Used in the Book of Portals or Help modpack. + + book_of_portals_pagetext = "Everything I need the player to know", + -- Optional. Provides the text for the portal in the Book of Portals + -- and Help modpack. + -- The Book of Portals is a book that can be found in chests, and + -- provides players with instructions on how to build and use the + -- portal, so be sure to mention the node type the frame must be built + -- from. + -- This can also provide flavortext or details about where the portal + -- will take the player. + + sounds = { + ambient = , + -- if the ambient SimpleSoundSpec is a table it can also contain a + -- "length" int, which is the number of seconds to wait before + -- repeating the ambient sound. Default is 3. + ignite = , + extinguish = , + teleport = , + } + -- sounds is optional + + within_realm = function(pos), + -- Required. Return true if a portal at pos is in the realm, rather + -- than the surface world. + -- Ideally implementations are fast, as this function can be used to + -- sift through a list of portals. + + find_realm_anchorPos = function(surface_anchorPos), + -- Required. Return a position in the realm that a portal created at + -- surface_anchorPos will link to. + -- Return an anchorPos or (anchorPos, orientation) + -- If orientation is not specified then the orientation of the surface + -- portal will be used. + -- If the location of an existing portal is returned then include the + -- orientation, otherwise the existing portal could be overwritten by + -- a new one with the orientation of the surface portal. + + find_surface_anchorPos = function(realm_anchorPos), + -- Optional. If you don't implement this then a position near the + -- surface will be picked. + -- Return an anchorPos or (anchorPos, orientation) + -- The name of this function is a Nether-centric misnomer. It should + -- return a position outside the realm, and different types of portals + -- are free to use different definitions of a realm such that leaving + -- the realm might not be synonymous with travelling to the surface. + -- If orientation is not specified then the orientation of the realm + -- portal will be used. + -- If the location of an existing portal is returned then include the + -- orientation, otherwise the existing portal could be overwritten by + -- a new one with the orientation of the realm portal. + + on_run_wormhole = function(portalDef, anochorPos, orientation), + -- invoked once per second per portal + on_extinguish = function(portalDef, anochorPos, orientation), + -- invoked when a portal is extinguished, including when the portal + -- it connected to was extinguished. + on_player_teleported = function(portalDef, player, oldPos, newPos), + -- invoked immediately after a player is teleported + on_ignite = function(portalDef, anochorPos, orientation) + -- invoked when a player or mesecon ignites a portal + on_created = function(portalDef, anochorPos, orientation) + -- invoked when a portal creates a remote twin, this is usually when + -- a player travels through a portal for the first time. + } diff --git a/portal_examples.lua b/portal_examples.lua new file mode 100644 index 0000000..0b402ad --- /dev/null +++ b/portal_examples.lua @@ -0,0 +1,319 @@ +--[[ + + Nether mod portal examples for Minetest + + These portal API examples work independently of the Nether realm + and Nether portal. To try these examples, enable them in: + Minetest -> Settings -> All settings -> Mods -> nether + Once enabled, details on how to build them can be found in dungeon + chests in the book of portals. + + -- + + Copyright (C) 2020 Treer + + Permission to use, copy, modify, and/or distribute this software for + any purpose with or without fee is hereby granted, provided that the + above copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL + WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR + BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES + OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + SOFTWARE. + +]]-- +local S = nether.get_translator + + +local ENABLE_PORTAL_EXAMPLE_FLOATLANDS = false +local ENABLE_PORTAL_EXAMPLE_SURFACETRAVEL = false + +-- Sets how far a Surface Portal will travel, measured in cells along the Moore curve, +-- which are about 117 nodes square each. Larger numbers will generally mean further distance +-- as-the-crow-flies, but this will not always be true due to the how the Moore curve +-- frequently doubles back upon itself. +-- This doubling-back prevents the surface portal from taking players easily accross the +-- map - the curve is 262144 cells long! +local SURFACE_TRAVEL_DISTANCE = 26 + + +--=================================================-- +-- Portal to the Floatlands, playable code example -- +--==================================================-- +local FLOATLANDS_ENABLED +local FLOATLAND_LEVEL = 1280 + +if minetest.settings:get_bool("nether_enable_portal_example_floatlands", ENABLE_PORTAL_EXAMPLE_FLOATLANDS) or ENABLE_PORTAL_EXAMPLE_FLOATLANDS then + + local floatlands_flavortext = "" + if minetest.get_mapgen_setting("mg_name") == "v7" then + local mgv7_spflags = minetest.get_mapgen_setting("mgv7_spflags") + FLOATLANDS_ENABLED = mgv7_spflags ~= nil and mgv7_spflags:find("floatlands") ~= nil and mgv7_spflags:find("nofloatlands") == nil + FLOATLAND_LEVEL = minetest.get_mapgen_setting("mgv7_floatland_level") or 1280 + + if FLOATLANDS_ENABLED then + floatlands_flavortext = "\n\n " .. S("There is a floating land of hills and forests up there, over the edges of which is a perilous drop all the way back down to sea level. We have not found how far these pristine lands extend. I have half a mind to retire there one day.") + end + end + + + nether.register_portal("floatlands_portal", { + shape = nether.PortalShape_Platform, + frame_node_name = "default:ice", + wormhole_node_color = 7, -- 7 is white + particle_texture = { + name = "nether_particle_anim1.png", + animation = { + type = "vertical_frames", + aspect_w = 7, + aspect_h = 7, + length = 1, + }, + scale = 1.5 + }, + title = S("Floatlands Portal"), + book_of_portals_pagetext = S([[Requiring 21 blocks of ice, and constructed in the shape of a 3 × 3 platform with walls, or like a bowl. A finished platform is 2 blocks high, and 5 blocks wide at the widest in both directions. + +This portal is different to the others, rather than acting akin to a doorway it appears to the eye more like a small pool of water which can be stepped into. Upon setting foot in the portal we found ourselves at a tremendous altitude.@1]], + floatlands_flavortext), + + is_within_realm = function(pos) -- return true if pos is inside the Nether + return pos.y > FLOATLAND_LEVEL - 200 + end, + + find_realm_anchorPos = function(surface_anchorPos) + -- TODO: Once paramat finishes adjusting the floatlands, implement a surface algorithm that finds land + local destination_pos = {x = surface_anchorPos.x ,y = FLOATLAND_LEVEL + 2, z = surface_anchorPos.z} + + -- a y_factor of 0 makes the search ignore the altitude of the portals (as long as they are in the Floatlands) + local existing_portal_location, existing_portal_orientation = nether.find_nearest_working_portal("floatlands_portal", destination_pos, 10, 0) + if existing_portal_location ~= nil then + return existing_portal_location, existing_portal_orientation + else + return destination_pos + end + end + }) + +end + + +--==============================================-- +-- Surface-travel portal, playable code example -- +--==============================================-- + +-- These Moore Curve functions requred by surface_portal's find_surface_anchorPos() will +-- be assigned later in this file. +local get_moore_distance -- will be function get_moore_distance(cell_count, x, y): integer +local get_moore_coords -- will be function get_moore_coords(cell_count, distance): pos2d + +if minetest.settings:get_bool("nether_enable_portal_example_surfacetravel", ENABLE_PORTAL_EXAMPLE_SURFACETRAVEL) or ENABLE_PORTAL_EXAMPLE_SURFACETRAVEL then + + nether.register_portal("surface_portal", { + shape = nether.PortalShape_Circular, + frame_node_name = "default:tinblock", + wormhole_node_color = 4, -- 4 is cyan + title = S("Surface Portal"), + book_of_portals_pagetext = S([[Requiring 16 blocks of tin and constructed in a circular fashion, a finished frame is seven blocks wide, seven blocks high, and stands vertically like a doorway. + +These travel a distance along the ground, and even when constructed deep underground will link back up to the surface. They appear to favor a strange direction, with the exit portal linking back only for as long as the portal stays open — attempting to reopen a portal from the exit doorway leads to a new destination along this favored direction. It has stymied our ability to study the behavior of these portals because without constructing dual portals and keeping both open it's hard to step through more than one and still be able to return home. + +Due to such difficulties, we never learned what determines the direction and distance where the matching twin portal will appear, and I have lost my friend and protégé. In cavalier youth and with little more than a rucksack, Coudreau has decided to follow the chain as far as it goes, and has not been seen since. Coudreau believes it works in epicycles, but I am not convinced. Still, I cling to the hope that one day the portal will open and Coudreau will step out from whichever place leads to this one, perhaps with an epic tale to tell.]]), + + is_within_realm = function(pos) + -- Always return true, because these portals always just take you around the surface + -- rather than taking you to a different realm + return true + end, + + find_realm_anchorPos = function(surface_anchorPos) + -- This function isn't needed, since this type of portal always goes to the surface + minetest.log("error" , "find_realm_anchorPos called for surface portal") + return {x=0, y=0, z=0} + end, + + find_surface_anchorPos = function(realm_anchorPos) + -- A portal definition doesn't normally need to provide a find_surface_anchorPos() function, + -- since find_surface_target_y() will be used by default, but these portals travel around the + -- surface (following a Moore curve) so will be calculating a different x and z to realm_anchorPos. + + local cellCount = 512 + local maxDistFromOrigin = 30000 -- the world edges are at X=30927, X=−30912, Z=30927 and Z=−30912 + + -- clip realm_anchorPos to maxDistFromOrigin, and move the origin so that all values are positive + local x = math.min(maxDistFromOrigin, math.max(-maxDistFromOrigin, realm_anchorPos.x)) + maxDistFromOrigin + local z = math.min(maxDistFromOrigin, math.max(-maxDistFromOrigin, realm_anchorPos.z)) + maxDistFromOrigin + + local divisor = math.ceil(maxDistFromOrigin * 2 / cellCount) + local distance = get_moore_distance(cellCount, math.floor(x / divisor + 0.5), math.floor(z / divisor + 0.5)) + local destination_distance = (distance + SURFACE_TRAVEL_DISTANCE) % (cellCount * cellCount) + local moore_pos = get_moore_coords(cellCount, destination_distance) + local target_x = moore_pos.x * divisor - maxDistFromOrigin + local target_z = moore_pos.y * divisor - maxDistFromOrigin + + local search_radius = divisor / 2 - 5 -- any portal within this area will do + + -- a y_factor of 0 makes the search ignore the altitude of the portals + local existing_portal_location, existing_portal_orientation = + nether.find_nearest_working_portal("surface_portal", {x = target_x, y = 0, z = target_z}, search_radius, 0) + + if existing_portal_location ~= nil then + -- use the existing portal that was found near target_x, target_z + return existing_portal_location, existing_portal_orientation + else + -- find a good location for the new portal, or if that isn't possible then at + -- least adjust the coords a little so portals don't line up in a grid + local adj_x, adj_z = 0, 0 + + -- Deterministically look for a location in the cell where get_spawn_level() can give + -- us a surface height, since nether.find_surface_target_y() works *much* better when + -- it can use get_spawn_level() + local prng = PcgRandom( -- seed the prng so that all portals for these Moore Curve coords will use the same random location + moore_pos.x * 65732 + + moore_pos.y * 729 + + minetest.get_mapgen_setting("seed") * 3 + ) + + local attemptLimit = 15 -- how many attempts we'll make at finding a good location + for attempt = 1, attemptLimit do + adj_x = math.floor(prng:rand_normal_dist(-search_radius, search_radius, 2) + 0.5) + adj_z = math.floor(prng:rand_normal_dist(-search_radius, search_radius, 2) + 0.5) + if minetest.get_spawn_level == nil or minetest.get_spawn_level(target_x + adj_x, target_z + adj_z) ~= nil then + -- Found a location which will be at ground level - unless a player has built there. + -- Or this is MT 0.4 which does not have get_spawn_level(), so there's no point looking + -- at any further further random locations. + break + end + end + + local destination_pos = {x = target_x + adj_x, y = 0, z = target_z + adj_z} + destination_pos.y = nether.find_surface_target_y(destination_pos.x, destination_pos.z, "surface_portal") + + return destination_pos + end + end + }) + +end + +--=========================================-- +-- Hilbert curve and Moore curve functions -- +--=========================================-- + +-- These are space-filling curves, used by the surface_portal example as a way to determine where +-- to place portals. https://en.wikipedia.org/wiki/Moore_curve + + +-- Flip a quadrant on a diagonal axis +-- cell_count is the number of cells across the square is split into, and must be a power of 2 +-- if flip_twice is true then pos does not change (even numbers of flips cancel out) +-- if flip_direction is true then the position is flipped along the \ diagonal +-- if flip_direction is false then the position is flipped along the / diagonal +local function hilbert_flip(cell_count, pos, flip_direction, flip_twice) + if not flip_twice then + if flip_direction then + pos.x = (cell_count - 1) - pos.x; + pos.y = (cell_count - 1) - pos.y; + end + + local temp_x = pos.x; + pos.x = pos.y; + pos.y = temp_x; + end +end + +local function test_bit(cell_count, value, flag) + local bit_value = cell_count / 2 + while bit_value > flag and bit_value >= 1 do + if value >= bit_value then value = value - bit_value end + bit_value = bit_value / 2 + end + return value >= bit_value +end + +-- Converts (x,y) to distance +-- starts at bottom left corner, i.e. (0, 0) +-- ends at bottom right corner, i.e. (cell_count - 1, 0) +local function get_hilbert_distance (cell_count, x, y) + local distance = 0 + local pos = {x=x, y=y} + local rx, ry + + local s = cell_count / 2 + while s > 0 do + + if test_bit(cell_count, pos.x, s) then rx = 1 else rx = 0 end + if test_bit(cell_count, pos.y, s) then ry = 1 else ry = 0 end + + local rx_XOR_ry = rx + if ry == 1 then rx_XOR_ry = 1 - rx_XOR_ry end -- XOR'd ry against rx + + distance = distance + s * s * (2 * rx + rx_XOR_ry) + hilbert_flip(cell_count, pos, rx > 0, ry > 0); + + s = math.floor(s / 2) + end + return distance; +end + +-- Converts distance to (x,y) +local function get_hilbert_coords(cell_count, distance) + local pos = {x=0, y=0} + local rx, ry + + local s = 1 + while s < cell_count do + rx = math.floor(distance / 2) % 2 + ry = distance % 2 + if rx == 1 then ry = 1 - ry end -- XOR ry with rx + + hilbert_flip(s, pos, rx > 0, ry > 0); + pos.x = pos.x + s * rx + pos.y = pos.y + s * ry + distance = math.floor(distance / 4) + + s = s * 2 + end + return pos +end + + +-- Converts (x,y) to distance +-- A Moore curve is a variation of the Hilbert curve that has the start and +-- end next to each other. +-- Top middle point is the start/end location +get_moore_distance = function(cell_count, x, y) + + local quadLength = cell_count / 2 + local quadrant = 1 - math.floor(y / quadLength) + if math.floor(x / quadLength) == 1 then quadrant = 3 - quadrant end + local flipDirection = x < quadLength + + local pos = {x = x % quadLength, y = y % quadLength} + hilbert_flip(quadLength, pos, flipDirection, false) + + return (quadrant * quadLength * quadLength) + get_hilbert_distance(quadLength, pos.x, pos.y) +end + + +-- Converts distance to (x,y) +-- A Moore curve is a variation of the Hilbert curve that has the start and +-- end next to each other. +-- Top middle point is the start/end location +get_moore_coords = function(cell_count, distance) + local quadLength = cell_count / 2 + local quadDistance = quadLength * quadLength + local quadrant = math.floor(distance / quadDistance) + local flipDirection = distance * 2 < cell_count * cell_count + local pos = get_hilbert_coords(quadLength, distance % quadDistance) + hilbert_flip(quadLength, pos, flipDirection, false) + + if quadrant >= 2 then pos.x = pos.x + quadLength end + if quadrant % 3 == 0 then pos.y = pos.y + quadLength end + + return pos +end diff --git a/schematics/nether_portal.mts b/schematics/nether_portal.mts deleted file mode 100644 index 4c38fd5..0000000 Binary files a/schematics/nether_portal.mts and /dev/null differ diff --git a/settingtypes.txt b/settingtypes.txt new file mode 100644 index 0000000..dbea779 --- /dev/null +++ b/settingtypes.txt @@ -0,0 +1,19 @@ +# Travelling a short distance in the Nether can correspond to a much further distance on the surface. +# +# A factor of 10 might be a better value for Minetest, since there's no sprint, but ex-Minecraft players will be mathing for 8. +nether_fasttravel_factor (Nether fast-travel factor) int 8 + +# The likelyhood of finding a Book containing all the portal plans in a dungeon chest. +# Set to 0 to disable, or 10 to have it extremely common. +# +# (This value will be treated as 0 when the Nether portal is the only type of portal available, or when the help modpack is installed) +nether_portalBook_loot_weighting (Likelyhood of finding Book of Portals in dungeon chests) int 9 + +# Turn off to disable the Nether and Nether portal +nether_realm_enabled (Enable Nether realm & portal) bool true + +# Enables the Floatlands portal api code example +nether_enable_portal_example_floatlands (Enable example portal: Floatlands) bool false + +# Enables the Surface-travel portal api code example +nether_enable_portal_example_surfacetravel (Enable example portal: Surface-travel) bool false \ No newline at end of file diff --git a/sounds/nether_book_open.ogg b/sounds/nether_book_open.ogg new file mode 100644 index 0000000..9af993e Binary files /dev/null and b/sounds/nether_book_open.ogg differ diff --git a/sounds/nether_portal_ambient.0.ogg b/sounds/nether_portal_ambient.0.ogg new file mode 100644 index 0000000..e6d339f Binary files /dev/null and b/sounds/nether_portal_ambient.0.ogg differ diff --git a/sounds/nether_portal_ambient.ogg b/sounds/nether_portal_ambient.ogg new file mode 100644 index 0000000..ea08651 Binary files /dev/null and b/sounds/nether_portal_ambient.ogg differ diff --git a/sounds/nether_portal_extinguish.ogg b/sounds/nether_portal_extinguish.ogg new file mode 100644 index 0000000..60efef2 Binary files /dev/null and b/sounds/nether_portal_extinguish.ogg differ diff --git a/sounds/nether_portal_ignite.ogg b/sounds/nether_portal_ignite.ogg new file mode 100644 index 0000000..40f7094 Binary files /dev/null and b/sounds/nether_portal_ignite.ogg differ diff --git a/sounds/nether_portal_ignition_failure.ogg b/sounds/nether_portal_ignition_failure.ogg new file mode 100644 index 0000000..e1fd0e5 Binary files /dev/null and b/sounds/nether_portal_ignition_failure.ogg differ diff --git a/sounds/nether_portal_teleport.ogg b/sounds/nether_portal_teleport.ogg new file mode 100644 index 0000000..9b62b62 Binary files /dev/null and b/sounds/nether_portal_teleport.ogg differ diff --git a/textures/nether_book_background.png b/textures/nether_book_background.png new file mode 100644 index 0000000..f767f2a Binary files /dev/null and b/textures/nether_book_background.png differ diff --git a/textures/nether_book_close.png b/textures/nether_book_close.png new file mode 100644 index 0000000..7455e68 Binary files /dev/null and b/textures/nether_book_close.png differ diff --git a/textures/nether_book_diagram_circular.png b/textures/nether_book_diagram_circular.png new file mode 100644 index 0000000..c6a4df1 Binary files /dev/null and b/textures/nether_book_diagram_circular.png differ diff --git a/textures/nether_book_diagram_platform.png b/textures/nether_book_diagram_platform.png new file mode 100644 index 0000000..4f81312 Binary files /dev/null and b/textures/nether_book_diagram_platform.png differ diff --git a/textures/nether_book_diagram_traditional.png b/textures/nether_book_diagram_traditional.png new file mode 100644 index 0000000..f159024 Binary files /dev/null and b/textures/nether_book_diagram_traditional.png differ diff --git a/textures/nether_book_of_portals.png b/textures/nether_book_of_portals.png new file mode 100644 index 0000000..4ad3a20 Binary files /dev/null and b/textures/nether_book_of_portals.png differ diff --git a/textures/nether_particle.png b/textures/nether_particle.png index 56a5b78..6171305 100644 Binary files a/textures/nether_particle.png and b/textures/nether_particle.png differ diff --git a/textures/nether_particle_anim1.png b/textures/nether_particle_anim1.png new file mode 100644 index 0000000..7518037 Binary files /dev/null and b/textures/nether_particle_anim1.png differ diff --git a/textures/nether_particle_anim2.png b/textures/nether_particle_anim2.png new file mode 100644 index 0000000..244cd83 Binary files /dev/null and b/textures/nether_particle_anim2.png differ diff --git a/textures/nether_particle_anim3.png b/textures/nether_particle_anim3.png new file mode 100644 index 0000000..da33cd6 Binary files /dev/null and b/textures/nether_particle_anim3.png differ diff --git a/textures/nether_portal.png b/textures/nether_portal.png index 824d652..583a099 100644 Binary files a/textures/nether_portal.png and b/textures/nether_portal.png differ diff --git a/textures/nether_portals_palette.png b/textures/nether_portals_palette.png new file mode 100644 index 0000000..8cc6f30 Binary files /dev/null and b/textures/nether_portals_palette.png differ