• 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.3] How to get all tiles with aid between 2 distances?

Lava Titan

Developer
Joined
Jul 25, 2009
Messages
1,529
Solutions
1
Reaction score
85
Location
Portugal
Hey, any idea on how can I check for all tiles with certain aid between 2 distances?
 
Solution
Why do you need to check the actionID? are you creating new tiles dynamically?
Because if you are checking an area of the map that is already built and is only used for this purpose, then it is not necessary to check the actionID
Or are you trying to find the mosaic with the given action ID in the given region?
Post automatically merged:

I think what you were really looking for was this:
Lua:
function getEmptyTileBetween(fromPos, toPos, actionId, exitPos)
    for x = fromPos.x, toPos.x do for y = fromPos.y, toPos.y do
        local tempPos = Position(x, y, fromPos.z)
        local tile = Tile(tempPos)
        if tile then
            local ground = tile:getGround()
            if ground and ground:getActionId() == actionId then
                if...
Hey, any idea on how can I check for all tiles with certain aid between 2 distances?
🤔
Lua:
local config = {
    actionid = 12345
}

----[ HORIZONTAL LINE ]----
local posA = Position(1000, 1000, 7)
local posB = Position(1005, 1000, 7)
for x = posA.x, posB.x do
    local tile = Tile(x, posA.y, posA.z)
    if not tile then return end

    local ground = tile:getGround()
    if ground:getActionId() == config.actionid then
        -- do something
    end
end

----[ VERTICAL LINE ]----
local posA = Position(1000, 1000, 7)
local posB = Position(1000, 1005, 7)
for y = posA.y, posB.y do
    local tile = Tile(posA.x, y, posA.z)
    if not tile then return end

    local ground = tile:getGround()
    if ground:getActionId() == config.actionid then
        -- do something
    end
end

----[ AREA ]----
local z = posA.z
for x = posA.x, posB.x do
    for y = posA.y, posB.y do
        local tile = Tile(x, y, z)
        if not tile then return end

        local ground = tile:getGround()
        if ground:getActionId() == config.actionid then
            -- do something
        end
    end
end
 
🤔
Lua:
local config = {
    actionid = 12345
}

----[ AREA ]----
local z = posA.z
for x = posA.x, posB.x do
    for y = posA.y, posB.y do
        local tile = Tile(x, y, z)
        if not tile then return end

        local ground = tile:getGround()
        if ground:getActionId() == config.actionid then
            -- do something
        end
    end
end


yeah that's it I think, but still can't figure out how to print how many tiles are there with that aid in that area or how to teleport someone there like:


Lua:
local p = player
    if not p then
        return p:teleportTo(fromPosition)
    end

    ----[ AREA ]----
    local posA = Position(3375, 1983, 14)
    local posB = Position(3736, 2163, 14)
    local z = posA.z
    for x = posA.x, posB.x do
        for y = posA.y, posB.y do
            local tile = Tile(x, y, z)
            --print(#tile) -- trying to print how many tiles in this area have aid 3333
        end
    end

    if item:getActionId() == 1111 then -- temple teleport action id
        local ground = tile:getGround()
        if ground:getActionId() == 3333 then -- tile where player is supposed to be teleported to
            p:teleportTo(Position(ground:getPosition())) -- teleport to any free tile in this area with aid 3333
        end
    end
 
this is what I'm trying to do xD

trying to create a new function that checks for empty tiles in area and teleports creature there, if no empty tiles then returns fromPosition

global.lua (the function keeps teleporting me to temple even that there are alot of free tiles without players)

Lua:
function getEmptyTileBetween(pos1, pos2, actionId) -- getEmptyTileBetween(config.firstPos, config.lastPos)
    local posA = pos1
    local posB = pos2
    local returnPos = Position(1000, 1000, 6) -- temple pos
    local z = posA.z
    for x = posA.x, posB.x do
        for y = posA.y, posB.y do
            local tile = Tile(x, y, z)
            if not tile then return end
            local ground = tile:getGround()
            local var = ground:getPosition()
            local spectators = Game.getSpectators(var, false, true, 5, 5, 5, 5)
            if ground:getActionId() == actionId then
                if #spectators == 0 then
                    print(#spectators)
                    return Position(x, y, z)
                else
                    print(#spectators)
                    return returnPos
                end
            end
        end
    end
end

data/scripts/trainingRoom.lua


Lua:
----------------------------------------------------------------------------------------------------------------
-- This is some kind of advanced lazy training room script, but I find this easier for big amounts of training rooms
-- The script should check for an empty room based on an area where all rooms have one tile with XXXX aid,
-- instead of adding all rooms positions it should just find them by checking the spectators in the area
----------------------------------------------------------------------------------------------------------------

local config = {
    firstPos = Position(3143, 1984, 14), -- first north west pos
    lastPos = Position(3623, 2488, 14), -- last south east pos
    templePos = Position(1000, 1000, 6), -- temple pos
    stepInAid = 11111, -- this is a teleport in temple
    stepOutAid = 22222, -- this is the teleport in all training rooms that teleports them out
    checkForAid = 33333, -- this is the aid of the tile that should be empty in the training room
    trainingStorage = 12345
}

local stepInTrainingRoom = MoveEvent()
stepInTrainingRoom:type("stepin")

function stepInTrainingRoom.onStepIn(player, item, position, fromPosition)
    local p = player
    if not p then
        return p:teleportTo(fromPosition)
    end

    local teleportPos = getEmptyTileBetween(config.firstPos, config.lastPos, config.checkForAid)
    p:teleportTo(teleportPos)
    player:setStorageValue(12345, 1) -- training storage

return true
end

stepInTrainingRoom:aid(11111)
stepInTrainingRoom:register()

------------------------------------------------------------

local stepOutTraining = MoveEvent()
stepOutTraining:type("stepOut")

function stepOutTraining.onStepIn(player, item, position, fromPosition)
    local p = player
    if not p then
        return p:teleportTo(fromPosition)
    end

    if item:getActionId() == config.stepOutAid then
        player:setStorageValue(12345, 0) -- training storage
        p:teleportTo(config.templePos)
    end

return true
end

stepOutTraining:aid(22222)
stepOutTraining:register()
 
Last edited:
updated last post xd
This might be what you're looking for.


You just copy-paste the training rooms, and the script will find all the training rooms automatically, within the designated area you tell it to look.
 
This might be what you're looking for.


You just copy-paste the training rooms, and the script will find all the training rooms automatically, within the designated area you tell it to look.
kinda, yeah, this will work thank you very much ❤️

even tho I wanted to fix this function so I could use it in other scripts in the future 😇

if possible xD


Lua:
function getEmptyTileBetween(pos1, pos2, aid) -- getEmptyTileBetween(config.firstPos, config.lastPos)
    local posA = pos1
    local posB = pos2
    local returnPos = Position(1000, 1000, 6)
    local z = posA.z
    for x = posA.x, posB.x do
        for y = posA.y, posB.y do
            local tile = Tile(x, y, z)
            if not tile then return end
            local ground = tile:getGround()
            local var = ground:getPosition()
            local spectators = Game.getSpectators(var, false, true, 5, 5, 5, 5)
            if ground:getActionId() == actionId then
                if #spectators == 0 then
                    print(#spectators)
                    return Position(x, y, z)
                else
                    print(#spectators)
                    return returnPos
                end
            end
        end
    end
end
 
kinda, yeah, this will work thank you very much ❤️

even tho I wanted to fix this function so I could use it in other scripts in the future 😇

if possible xD


Lua:
function getEmptyTileBetween(pos1, pos2, aid) -- getEmptyTileBetween(config.firstPos, config.lastPos)
    local posA = pos1
    local posB = pos2
    local returnPos = Position(1000, 1000, 6)
    local z = posA.z
    for x = posA.x, posB.x do
        for y = posA.y, posB.y do
            local tile = Tile(x, y, z)
            if not tile then return end
            local ground = tile:getGround()
            local var = ground:getPosition()
            local spectators = Game.getSpectators(var, false, true, 5, 5, 5, 5)
            if ground:getActionId() == actionId then
                if #spectators == 0 then
                    print(#spectators)
                    return Position(x, y, z)
                else
                    print(#spectators)
                    return returnPos
                end
            end
        end
    end
end

Lua:
function getEmptyTileBetween(fromPos, toPos)
    local x, y = math.floor((toPos.x - fromPos.x)/2), math.floor((toPos.y - fromPos.y)/2)
    local tempPos = Position(x, y, fromPos.z)
    local spectators = Game.getSpectators(tempPos, false, true, x, x, y, y)
    return #spectators == 0 and tempPos or Position(3191, 1809, 7)
end
 
Lua:
function getEmptyTileBetween(fromPos, toPos)
    local x, y = math.floor((toPos.x - fromPos.x)/2), math.floor((toPos.y - fromPos.y)/2)
    local tempPos = Position(x, y, fromPos.z)
    local spectators = Game.getSpectators(tempPos, false, true, x, x, y, y)
    return #spectators == 0 and tempPos or Position(3191, 1809, 7)
end
damn Sarah, you shortned it so much xD

i tried to change it but tempPos is returning nil '-'

Lua:
local teleportPos  = getEmptyTileBetween(Position(3143, 1984, 14), Position(3623, 2488, 14), 33333, Position(1000, 1000, 6))
    p:teleportTo(teleportPos)


Lua:
function getEmptyTileBetween(fromPos, toPos, actionId, exitPos)
    local x, y = math.floor((toPos.x - fromPos.x)/2), math.floor((toPos.y - fromPos.y)/2)
    local tempPos = Position(x, y, fromPos.z)
    local spectators = Game.getSpectators(tempPos, false, true, x, x, y, y)
    local tile = Tile(tempPos):getGround()
    if tile:getActionId() == actionId and #spectators == 0 then
        return tempPos
    else
        return exitPos
    end
end
 
Why do you need to check the actionID? are you creating new tiles dynamically?
Because if you are checking an area of the map that is already built and is only used for this purpose, then it is not necessary to check the actionID
Or are you trying to find the mosaic with the given action ID in the given region?
Post automatically merged:

I think what you were really looking for was this:
Lua:
function getEmptyTileBetween(fromPos, toPos, actionId, exitPos)
    for x = fromPos.x, toPos.x do for y = fromPos.y, toPos.y do
        local tempPos = Position(x, y, fromPos.z)
        local tile = Tile(tempPos)
        if tile then
            local ground = tile:getGround()
            if ground and ground:getActionId() == actionId then
                if #Game.getSpectators(tempPos, false, true, 5, 5, 5, 5) == 0 then
                    return tempPos
                end
            end
        end
    end end
    return exitPos
end
 
Last edited:
Solution
Back
Top