• 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+ Tfs 1.2 teleport player to Area Random

roriscrave

Advanced OT User
Joined
Dec 7, 2011
Messages
1,188
Solutions
34
Reaction score
201
hello i am using tfs 1.2 to 8.6.
I need to do a function that will teleport the player into the red area (the position where he will be teleport need to be random), and cannot teleport into the green area (which is area pz).
How i can do it?

1565204378891.png
 
Solution
lowest x,y coordinates are top left.
highest x,y coordinates are bottom right.

You can use math.random to artifically throw the dice between lowest and highest x and y coordinate. If the randomed coordinate is in the center square, roll again until it isn't.

Alternatively mark the zones around pz into smaller zones, and do a math first to decide which section to go in, and then where in that section.
lowest x,y coordinates are top left.
highest x,y coordinates are bottom right.

You can use math.random to artifically throw the dice between lowest and highest x and y coordinate. If the randomed coordinate is in the center square, roll again until it isn't.

Alternatively mark the zones around pz into smaller zones, and do a math first to decide which section to go in, and then where in that section.
 
Solution
Better performance than rolling random positions until one is found, this guarantees you have a valid randomized position as soon as you need it.
Lua:
-- needs to be somewhere where it only gets loaded once (lib file, OUTSIDE of an interface function such as onUse/onSay/onKill/etc)
TP_validPositions = {}
local fromPos = Position(1000, 1000, 7)
local toPos = Position(1050, 1050, 7)
local tempPos = Position(0, 0, fromPos.z)
for x = fromPos.x, fromPos.x do
    for y = toPos.y, toPos.y do
        tempPos.x = x
        tempPos.y = y
        local tile = Tile(tempPos)
        if tile and not tile:hasFlag(TILESTATE_PROTECTIONZONE) then
            TP_validPositions[#TP_validPositions+1] = Position(tempPos.x, tempPos.y, tempPos.z) -- copying to avoid issues with table references
        end
    end
end

-- example
function onUse(player)
    local randomValidPos = TP_validPositions[math.random(#TP_validPositions)]
    player:teleportTo(randomValidPos)
    return true
end
 
Last edited:
lowest x,y coordinates are top left.
highest x,y coordinates are bottom right.

You can use math.random to artifically throw the dice between lowest and highest x and y coordinate. If the randomed coordinate is in the center square, roll again until it isn't.

Alternatively mark the zones around pz into smaller zones, and do a math first to decide which section to go in, and then where in that section.
Better performance than rolling random positions until one is found, this guarantees you have a valid randomized position as soon as you need it.

Thx you all!!!
 
Back
Top