• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

TFS 1.X+ Teleport spell

venius

Active Member
Joined
Dec 27, 2010
Messages
150
Reaction score
36
Hi guys, I am trying to make a spell that teleports the user next to the target. I have this script:
Lua:
local config = {
    effect = 65, --effect
}

function onCastSpell(cid, var)
    local target = variantToNumber(var)
    local playerPos = getThingPos(target)
    local position = Position(playerPos.x,playerPos.y,playerPos.z)

    if isCreature(cid) and isCreature(target) then
        --local lookingDirection = getCreatureLookDirection(target)
        --print(lookingDirection)


        doSendMagicEffect(getThingPos(cid), config.effect)
        doTeleportThing(cid, position)
        doSendMagicEffect(getThingPos(cid), config.effect)
    end
    return true
end

However, this script teleports me on the spot of my target, which gets problematic/OP in PVP..
How can I add so that the user teleports next to the target IF possible and if there is no free square around them then the teleport doesn't work?
 
You need to register 'needtarget' attribute on spell itself
Lua:
needtarget="1"

Lua:
local isWalkable = function(position)
    local tile = Tile(position)
    if not tile or tile:hasFlag(TILESTATE_FLOORCHANGE) or tile:hasFlag(TILESTATE_PROTECTIONZONE) then
        return false
    end

    local ground = tile:getGround()
    if not ground or ground:hasProperty(CONST_PROP_BLOCKSOLID) then
        return false
    end

    local items = tile:getItems()
    for i = 1, tile:getItemCount() do
        local item = items[i]
        local itemType = item:getType()
        if itemType:getType() ~= ITEM_TYPE_MAGICFIELD and not itemType:isMovable() and item:hasProperty(CONST_PROP_BLOCKSOLID) then
            return false
        end
    end
    return tile
end

function onCastSpell(player, variant)
    local target = Player(variant:getNumber())
    if target then
        local position, random
        local directions = {0, 1, 2, 3, 4, 5, 6, 7}
        repeat
            position = target:getPosition()

            random = math.random(1, #directions)
            position:getNextPosition(directions[random])

            local tile = Tile(position)
            if tile then
                if isWalkable(tile:getPosition()) then
                    for i = 1, 2 do
                        if (i % 2) == 0 then
                            player:teleportTo(position)
                        end
                        player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
                    end
                    return true
                end
            end
            table.remove(directions, random)

        until #directions == 0
    end
    player:getPosition():sendMagicEffect(CONST_ME_POFF)
    return true
end
EDIT: Added check for all tiles around, instead only 1 randomly
 
Last edited:
You need to register 'needtarget' attribute on spell itself
Lua:
needtarget="1"

Lua:
local isWalkable = function(position)
    local tile = Tile(position)
    if not tile or tile:hasFlag(TILESTATE_FLOORCHANGE) then
        return false
    end

    local ground = tile:getGround()
    if not ground or ground:hasProperty(CONST_PROP_BLOCKSOLID) then
        return false
    end

    local items = tile:getItems()
    for i = 1, tile:getItemCount() do
        local item = items[i]
        local itemType = item:getType()
        if itemType:getType() ~= ITEM_TYPE_MAGICFIELD and not itemType:isMovable() and item:hasProperty(CONST_PROP_BLOCKSOLID) then
            return false
        end
    end
    return tile
end

function onCastSpell(player, variant)
    local target = Creature(variant:getNumber())
    if target then
        local position, random
        local directions = {0, 1, 2, 3, 4, 5, 6, 7}
        repeat
            position = target:getPosition()

            random = math.random(1, #directions)
            position:getNextPosition(directions[random])

            local tile = Tile(position)
            if tile then
                if isWalkable(tile:getPosition()) then
                    for i = 1, 2 do
                        if (i % 2) == 0 then
                            player:teleportTo(position)
                        end
                        player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
                    end
                    return true
                end
            end
            table.remove(directions, random)

        until #directions == 0
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
    end
    return true
end
EDIT: Added check for all tiles around, instead only 1 randomly
Yes! essentially this is what I want. However, I tested it some and it seems to sometimes tp me into PZ if it's within 1 sqm of my target and it also teleports on monsters
 
Yes! essentially this is what I want. However, I tested it some and it seems to sometimes tp me into PZ if it's within 1 sqm of my target and it also teleports on monsters
I updated the script on my first post
 
The spell works on players(only), it doesn't enter pz anymore, but it still sometimes teleports onto a monster
I just tested and worked, try by changing variant:getNumber() to player:getTarget()

EDIT: Ah, i just realized what you meant

Change
Lua:
if not tile or tile:hasFlag(TILESTATE_FLOORCHANGE) or tile:hasFlag(TILESTATE_PROTECTIONZONE) then

To this
Lua:
if not tile or tile:hasFlag(TILESTATE_FLOORCHANGE) or tile:hasFlag(TILESTATE_PROTECTIONZONE) or tile:getTopCreature() then
 
Last edited:
I just tested and worked, try by changing variant:getNumber() to player:getTarget()

EDIT: Ah, i just realized what you meant

Change
Lua:
if not tile or tile:hasFlag(TILESTATE_FLOORCHANGE) or tile:hasFlag(TILESTATE_PROTECTIONZONE) then

To this
Lua:
if not tile or tile:hasFlag(TILESTATE_FLOORCHANGE) or tile:hasFlag(TILESTATE_PROTECTIONZONE) or tile:getTopCreature() then
Thank you, dude! It works perfectly! I will test it more extensively tomorrow
 
Back
Top