Portals: Fix rapid back-and-forth teleportation

This commit is contained in:
Wuzzy 2017-08-17 18:14:49 +02:00
parent 6dbd120397
commit 14678af2ed
2 changed files with 39 additions and 3 deletions

@ -12,10 +12,14 @@ local np_cave = {
octaves = 5, octaves = 5,
persist = 0.7 persist = 0.7
} }
-- Portal frame material -- Portal frame material
local portal_frame = "mcl_nether:quartz_block" local portal_frame = "mcl_nether:quartz_block"
-- Table of objects (including players) which recently teleported by a
-- End portal. Those objects have a brief cooloff period before they
-- can teleport again. This prevents annoying back-and-forth teleportation.
local portal_cooloff = {}
-- Destroy portal if pos (portal frame or portal node) got destroyed -- Destroy portal if pos (portal frame or portal node) got destroyed
local destroy_portal = function(pos) local destroy_portal = function(pos)
-- Deactivate Nether portal -- Deactivate Nether portal
@ -326,6 +330,10 @@ minetest.register_abm({
for _,obj in ipairs(minetest.get_objects_inside_radius(pos,1)) do --maikerumine added for objects to travel for _,obj in ipairs(minetest.get_objects_inside_radius(pos,1)) do --maikerumine added for objects to travel
local lua_entity = obj:get_luaentity() --maikerumine added for objects to travel local lua_entity = obj:get_luaentity() --maikerumine added for objects to travel
if obj:is_player() or lua_entity then if obj:is_player() or lua_entity then
-- No rapid back-and-forth teleportatio
if portal_cooloff[obj] then
return
end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local target3 = minetest.string_to_pos(meta:get_string("portal_target")) local target3 = minetest.string_to_pos(meta:get_string("portal_target"))
if target3 then if target3 then
@ -336,8 +344,12 @@ minetest.register_abm({
vector.subtract(target3, 4), vector.add(target3, 4)) vector.subtract(target3, 4), vector.add(target3, 4))
end end
-- teleport the player -- teleport the object
minetest.after(3, function(obj, pos, target3) minetest.after(3, function(obj, pos, target3)
-- No rapid back-and-forth teleportatio
if portal_cooloff[obj] then
return
end
local objpos = obj:getpos() local objpos = obj:getpos()
if objpos == nil then if objpos == nil then
return return
@ -366,6 +378,12 @@ minetest.register_abm({
obj:setpos(target3) obj:setpos(target3)
minetest.sound_play("mcl_portals_teleport", {pos=target3, gain=0.5, max_hear_distance = 16}) minetest.sound_play("mcl_portals_teleport", {pos=target3, gain=0.5, max_hear_distance = 16})
-- Enable teleportation cooloff to prevent frequent back-and-forth teleportation
portal_cooloff[obj] = true
minetest.after(3, function(o)
portal_cooloff[o] = false
end, obj)
end, obj, pos, target3) end, obj, pos, target3)
end end
end end

@ -19,6 +19,11 @@ local np_cave = {
persist = 0.7 persist = 0.7
} }
-- Table of objects (including players) which recently teleported by a
-- Nether portal. Those objects have a brief cooloff period before they
-- can teleport again. This prevents annoying back-and-forth teleportation.
local portal_cooloff = {}
-- Destroy portal if pos (portal frame or portal node) got destroyed -- Destroy portal if pos (portal frame or portal node) got destroyed
local destroy_portal = function(pos) local destroy_portal = function(pos)
-- Deactivate Nether portal -- Deactivate Nether portal
@ -357,6 +362,10 @@ minetest.register_abm({
for _,obj in ipairs(minetest.get_objects_inside_radius(pos,1)) do --maikerumine added for objects to travel for _,obj in ipairs(minetest.get_objects_inside_radius(pos,1)) do --maikerumine added for objects to travel
local lua_entity = obj:get_luaentity() --maikerumine added for objects to travel local lua_entity = obj:get_luaentity() --maikerumine added for objects to travel
if obj:is_player() or lua_entity then if obj:is_player() or lua_entity then
-- Prevent quick back-and-forth teleportation
if portal_cooloff[obj] then
return
end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local target = minetest.string_to_pos(meta:get_string("portal_target")) local target = minetest.string_to_pos(meta:get_string("portal_target"))
if target then if target then
@ -366,8 +375,12 @@ minetest.register_abm({
minetest.emerge_area( minetest.emerge_area(
vector.subtract(target, 4), vector.add(target, 4)) vector.subtract(target, 4), vector.add(target, 4))
end end
-- teleport the player -- teleport the object
minetest.after(3, function(obj, pos, target) minetest.after(3, function(obj, pos, target)
-- Prevent quick back-and-forth teleportation
if portal_cooloff[obj] then
return
end
local objpos = obj:getpos() local objpos = obj:getpos()
if objpos == nil then if objpos == nil then
return return
@ -397,6 +410,11 @@ minetest.register_abm({
obj:setpos(target) obj:setpos(target)
minetest.sound_play("mcl_portals_teleport", {pos=target, gain=0.5, max_hear_distance = 16}) minetest.sound_play("mcl_portals_teleport", {pos=target, gain=0.5, max_hear_distance = 16})
-- Enable teleportation cooloff for 4 seconds, to prevent back-and-forth teleportation
portal_cooloff[obj] = true
minetest.after(4, function(o)
portal_cooloff[o] = false
end, obj)
end, obj, pos, target) end, obj, pos, target)
end end