• 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 8.60 , system carpet

Lbtg

Intermediate OT User
Joined
Nov 22, 2008
Messages
2,298
Reaction score
127
Hello, we making this carpet system and little bit failing, could someone look up at this and can help solve it :)

carpetTPplayer.lua
Lua:
function onThink(cid, interval, lastExecution)
      
    for i = 0, CARPET_QUEST.carpet.sqms - 1 do
    local _teleported = false
      
        local position = CARPET_QUEST.carpet.startPosition
        local pos = Position(position.x + i, position.y, position.z) 
              local tile = Tile(pos)
              if tile then
               local ground = tile:getItemById(CARPET_QUEST.carpet.ItemID)
            if ground then
                 local topCreature = tile:getTopCreature()
                 if topCreature and topCreature:isPlayer() then
                    if not _teleported then
                           if ground:getAttribute(ITEM_ATTRIBUTE_ACTIONID) == CARPET_QUEST.carpet.ActionID then
                            
                              if i == CARPET_QUEST.carpet.sqms - 1 then
                            
                                local position = Position(topCreature:getPosition().x + 2, topCreature:getPosition().y, topCreature:getPosition().z)
                                _teleported = true
                    
                                addEvent(function()
                                   topCreature:teleportTo(position)
                                   topCreature:getPosition():sendMagicEffect(15)
                                end, 650)
                          
                                else
                            
                                local position = Position(topCreature:getPosition().x + 1, topCreature:getPosition().y, topCreature:getPosition().z)
                                _teleported = true
                    
                                addEvent(function()
                                topCreature:teleportTo(position)
                                end, 650)

                              end
                            
                            
                            
                           end
                    end
                 end
              end
            end
      
    end
  
    for i = 0, CARPET_QUEST.carpet.sqms - 1 do
    local _teleported = false
            
        local position = Position(CARPET_QUEST.carpet.startPosition.x + CARPET_QUEST.carpet.sqms, CARPET_QUEST.carpet.startPosition.y, CARPET_QUEST.carpet.startPosition.z)
      
        local pos = Position(position.x - i, position.y, position.z) 
              local tile = Tile(pos)
              if tile then
               local ground = tile:getItemById(CARPET_QUEST.carpet.ItemID)
            if ground then
                 local topCreature = tile:getTopCreature()
                 if topCreature and topCreature:isPlayer() then
                    if not _teleported then
                           if ground:getAttribute(ITEM_ATTRIBUTE_ACTIONID) == CARPET_QUEST.carpet.ActionID + 1 then
                                
                                if i == CARPET_QUEST.carpet.sqms - 1 then
                            
                                local position = Position(topCreature:getPosition().x - 2, topCreature:getPosition().y, topCreature:getPosition().z)
                                _teleported = true
                    
                                addEvent(function()
                                   topCreature:teleportTo(position)
                                   topCreature:getPosition():sendMagicEffect(15)
                                end, 650)
                          
                                else
                            
                                local position = Position(topCreature:getPosition().x - 1, topCreature:getPosition().y, topCreature:getPosition().z)
                                _teleported = true
                    
                                addEvent(function()
                                topCreature:teleportTo(position)
                                end, 650)

                              end
                            
                            
                            
                            
                           end
                    end
                 end
              end
            end
      
    end         
  
return true
end

carpetLIB.lua
Lua:
CARPET_QUEST = {
                 carpet = { startPosition = Position(33177, 31548, 15),
                            sqms = 23,
                            delay = 800,
                            ItemID = 13661,
                            ActionID = 8575,
                          }
               }
            
            


CARPET_QUEST_FIRST_RUN = true
CARPET_QUEST_BEGIN = true
CARPET_QUEST_START = true
CARPET_QUEST_CURRENT_POSITION = nil
CARPET_QUEST_TELEPORT = nil
CARPET_QUEST_DELAY = 0

carpetCreate.lua

Lua:
function onThink(cid, interval, lastExecution)

if CARPET_QUEST_BEGIN and CARPET_QUEST_START then

  
    if not CARPET_QUEST_TELEPORT then
    CARPET_QUEST_TELEPORT = Game.createItem(CARPET_QUEST.carpet.ItemID, 1, CARPET_QUEST.carpet.startPosition)
    CARPET_QUEST_TELEPORT:setAttribute(ITEM_ATTRIBUTE_ACTIONID, CARPET_QUEST.carpet.ActionID)
    end

    CARPET_QUEST_START = false
    for i = 1, CARPET_QUEST.carpet.sqms do
  
        local pos = Position(CARPET_QUEST.carpet.startPosition.x + i, CARPET_QUEST.carpet.startPosition.y, CARPET_QUEST.carpet.startPosition.z)
  
        if i == 1 then
            CARPET_QUEST_DELAY = 0
        else
            CARPET_QUEST_DELAY = CARPET_QUEST_DELAY + CARPET_QUEST.carpet.delay
        end
      
        addEvent(function()
          CARPET_QUEST_TELEPORT:remove()
          CARPET_QUEST_TELEPORT = Game.createItem(CARPET_QUEST.carpet.ItemID, 1, pos)
          CARPET_QUEST_TELEPORT:setAttribute(ITEM_ATTRIBUTE_ACTIONID, CARPET_QUEST.carpet.ActionID)
        
        if i == CARPET_QUEST.carpet.sqms then
           CARPET_QUEST_BEGIN = false
           CARPET_QUEST_START = true
           currentPosition = pos
           CARPET_QUEST_TELEPORT:setAttribute(ITEM_ATTRIBUTE_ACTIONID, CARPET_QUEST.carpet.ActionID + 1)
        end
      
        end, CARPET_QUEST_DELAY)
      


    end 
end

if not CARPET_QUEST_BEGIN and CARPET_QUEST_START then
        CARPET_QUEST_START = false
        for i = 1, CARPET_QUEST.carpet.sqms do
        local pos = Position(currentPosition.x - i, currentPosition.y, currentPosition.z)
  
        if i == 1 then
            CARPET_QUEST_DELAY = 0
        else
            CARPET_QUEST_DELAY = CARPET_QUEST_DELAY + CARPET_QUEST.carpet.delay
        end
      
        addEvent(function()
          CARPET_QUEST_TELEPORT:remove()
          CARPET_QUEST_TELEPORT = Game.createItem(CARPET_QUEST.carpet.ItemID, 1, pos)
          CARPET_QUEST_TELEPORT:setAttribute(ITEM_ATTRIBUTE_ACTIONID, CARPET_QUEST.carpet.ActionID + 1)
        if i == CARPET_QUEST.carpet.sqms then
           CARPET_QUEST_BEGIN = true
           CARPET_QUEST_START = true
           _isFirstRun = false
           CARPET_QUEST_TELEPORT:setAttribute(ITEM_ATTRIBUTE_ACTIONID, CARPET_QUEST.carpet.ActionID)
        end
        end, CARPET_QUEST_DELAY)     
    end
end

return true
end
Post automatically merged:

trying to add videos, but can't..

The problem is the carpet is glitching with player. Its not flying together nicely...


 
Last edited:
I made 2 scripts. First with item as carpet, second with creature as carpet.
Tested on TFS 1.4.1, but should work on all 1.2+.
In globalevents.xml add:
Code:
<globalevent name="Carpet" interval="1000" script="carpet.lua" />

Carpet with item as carpet. Item teleports, there is no smooth animation of carpet movement:
Lua:
-- config
local carpetPositions = {
    { x = 219, y = 458, z = 12 },
    { x = 220, y = 458, z = 12 },
    { x = 221, y = 458, z = 12 },
    { x = 222, y = 458, z = 12 },
}
local carpetItemId = 2400

-- script variables
local CARPET_DIRECTION_FORWARD = 1
local CARPET_DIRECTION_BACKWARD = 2
local currentDirection = CARPET_DIRECTION_FORWARD

local function comparePositions(position1, position2)
    return position1.x == position2.x and position1.y == position2.y and position1.z == position2.z
end

local function isCarpetPosition(position)
    local tile = Tile(position)
    if tile then
        local carpet = tile:getItemById(carpetItemId)
        if carpet then
            return true
        end
    end

    return false
end

local function getCarpetPosition()
    for _, position in pairs(carpetPositions) do
        if isCarpetPosition(position) then
            return position
        end
    end
    return nil
end

local function moveCarpet(fromPosition, toPosition)
    -- remove old item
    local fromTile = Tile(fromPosition)
    fromTile:getItemById(carpetItemId):remove()

    -- create new item
    Game.createItem(carpetItemId, 1, toPosition)

    -- move creatures
    local creatures = fromTile:getCreatures()
    for _, creature in pairs(creatures) do
        creature:teleportTo(toPosition, false)
    end
end

local function getNextCarpetPosition(carpetPosition)
    -- find carpet position key in table
    local carpetPositionKey = 1
    for i, position in pairs(carpetPositions) do
        if comparePositions(position, carpetPosition) then
            carpetPositionKey = i
            break
        end
    end

    if carpetPositionKey == 1 then
        -- carpet at first positions, move forward, get next position
        currentDirection = CARPET_DIRECTION_FORWARD
        return carpetPositions[carpetPositionKey + 1]
    elseif carpetPositionKey == #carpetPositions then
        -- carpet at last position, move backward, get previous position
        currentDirection = CARPET_DIRECTION_BACKWARD
        return carpetPositions[carpetPositionKey - 1]
    else
        -- carpet between first and last position, check move direction
        if currentDirection == CARPET_DIRECTION_FORWARD then
            return carpetPositions[carpetPositionKey + 1]
        else
            return carpetPositions[carpetPositionKey - 1]
        end
    end
end

local function createCarpet(position)
    Game.createItem(carpetItemId, 1, position)
end

function onThink()
    -- get or create carpet
    local currentCarpetPosition = getCarpetPosition()
    if not currentCarpetPosition then
        -- carpet does not exist, create one
        currentCarpetPosition = carpetPositions[1]
        createCarpet(currentCarpetPosition)
    end

    -- get next carpet position
    local nextCarpetPosition = getNextCarpetPosition(currentCarpetPosition)
    -- move carpet to next position
    moveCarpet(currentCarpetPosition, nextCarpetPosition)

    return true
end

Carpet with monster as carpet - smooth movement animation of carpet:
Lua:
-- config
local carpetPositions = {
    { x = 219, y = 458, z = 12 },
    { x = 220, y = 458, z = 12 },
    { x = 221, y = 458, z = 12 },
    { x = 222, y = 458, z = 12 },
}
local carpetMonsterName = "Lavahole"

-- script variables
local CARPET_DIRECTION_FORWARD = 1
local CARPET_DIRECTION_BACKWARD = 2
local currentDirection = CARPET_DIRECTION_FORWARD

local function comparePositions(position1, position2)
    return position1.x == position2.x and position1.y == position2.y and position1.z == position2.z
end

local function isCarpetPosition(position)
    local tile = Tile(position)
    if tile then
        local creatures = tile:getCreatures()
        for _, creature in pairs(creatures) do
            if Monster(creature) and creature:getName() == carpetMonsterName then
                return true
            end
        end
    end

    return false
end

local function getCarpetPosition()
    for _, position in pairs(carpetPositions) do
        if isCarpetPosition(position) then
            return position
        end
    end
    return nil
end

local function moveCarpet(fromPosition, toPosition)
    local fromTile = Tile(fromPosition)
    -- get speed of creature on tile
    local creatures = fromTile:getCreatures()
    local speed = 200
    for _, creature in pairs(creatures) do
        if Player(creature) then
            speed = creature:getSpeed()
        end
    end
    -- move carpet first, so it should be 'under player' (walk first on new tile)
    for _, creature in pairs(creatures) do
        if Monster(creature) and creature:getName() == carpetMonsterName then
            creature:changeSpeed(speed - creature:getSpeed())
            creature:teleportTo(toPosition, true)
        end
    end
    -- move players
    for _, creature in pairs(creatures) do
        if Player(creature) then
            creature:teleportTo(toPosition, true)
        end
    end
end

local function getNextCarpetPosition(carpetPosition)
    -- find carpet position key in table
    local carpetPositionKey = 1
    for i, position in pairs(carpetPositions) do
        if comparePositions(position, carpetPosition) then
            carpetPositionKey = i
            break
        end
    end

    if carpetPositionKey == 1 then
        -- carpet at first positions, move forward, get next position
        currentDirection = CARPET_DIRECTION_FORWARD
        return carpetPositions[carpetPositionKey + 1]
    elseif carpetPositionKey == #carpetPositions then
        -- carpet at last position, move backward, get previous position
        currentDirection = CARPET_DIRECTION_BACKWARD
        return carpetPositions[carpetPositionKey - 1]
    else
        -- carpet between first and last position, check move direction
        if currentDirection == CARPET_DIRECTION_FORWARD then
            return carpetPositions[carpetPositionKey + 1]
        else
            return carpetPositions[carpetPositionKey - 1]
        end
    end
end

local function createCarpet(position)
    Game.createMonster(carpetMonsterName, position, false, true)
end

function onThink()
    -- get or create carpet
    local currentCarpetPosition = getCarpetPosition()
    if not currentCarpetPosition then
        -- carpet does not exist, create one
        currentCarpetPosition = carpetPositions[1]
        createCarpet(currentCarpetPosition)
    end

    -- get next carpet position
    local nextCarpetPosition = getNextCarpetPosition(currentCarpetPosition)
    -- move carpet to next position
    moveCarpet(currentCarpetPosition, nextCarpetPosition)

    return true
end
 
Back
Top