• 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!

Action Moving Wagon [TFS 1.x]

Printer

if Printer then print("LUA") end
Senator
Premium User
Joined
Dec 27, 2009
Messages
5,782
Solutions
31
Reaction score
2,284
Location
Sweden?
Hello,

i was bored, so enjoy!
wagon.gif

Insert this in action folder:
Code:
<action fromaid="5000" toaid="5003" script="wagon.lua" />

Code:
local config = {
    speed = {wagonSpeed = 200, playerSpeed = 500},
    wagonStorage = 3000,
    wagonActionId = 5000, -- North = 5000, EAST = 5001, SOUTH = 5002, WEST = 5003
    wagonDirection = {[DIRECTION_NORTH] = 7132, [DIRECTION_EAST] = 7131, [DIRECTION_SOUTH] = 7132, [DIRECTION_WEST] = 7131},
    railDirection = {
        [7123] = {DIRECTION_EAST, DIRECTION_SOUTH},
        [7124] = {DIRECTION_WEST, DIRECTION_SOUTH},
        [7125] = {DIRECTION_EAST, DIRECTION_NORTH},
        [7126] = {DIRECTION_WEST, DIRECTION_NORTH}
    }
}

local function getRail(position)
    local tile = Tile(position)
    if tile then
        -- Loop through items
        for _, item in ipairs(tile:getItems()) do
            -- We found rail, return id
            if isInArray({7121, 7122, 7123, 7124, 7125, 7126, 7127, 7128, 7129, 7130}, item:getId()) then
                return item:getId()
            end
        end
    end

    return 0
end

local outfitCondition = Condition(CONDITION_OUTFIT, CONDITIONID_COMBAT)
outfitCondition:setTicks(-1)

local function moveWagon(cid, direction)
    local player = Player(cid)
    if not player then
        return
    end

    -- If there is no rail, just stop the wagon
    local position = player:getPosition()
    local getRail = getRail(position)
    if getRail == 0 then
        -- Remove Outfit
        player:removeCondition(CONDITION_OUTFIT, CONDITIONID_COMBAT)

        -- Remove speed
        player:changeSpeed(-config.speed.playerSpeed)

        -- Remove Storage
        player:setStorageValue(config.wagonStorage, 0)

        -- Teleport 1 sqm forward and make sure it's not a blocking item
        position:getNextPosition(direction)
        position = player:getClosestFreePosition(position, false)
        player:teleportTo(position, true)
        return
    end

    -- Handle new rail directions
    local newRail = config.railDirection[getRail]
    if newRail and type(newRail) == 'table' then
        direction = newRail[newRail[1] == Game.getReverseDirection(direction) and 2 or 1]
        outfitCondition:setOutfit(config.wagonDirection[direction])
        player:addCondition(outfitCondition)
    end

    -- Handle movement
    doMoveCreature(cid, direction)
    addEvent(moveWagon, config.speed.wagonSpeed, cid, direction)
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    -- Teleport into the wagon
    player:teleportTo(toPosition, true)

    -- Get direction by action id
    local direction = item.actionid - config.wagonActionId

    -- Set Outfit, according the direction and set it
    outfitCondition:setOutfit(config.wagonDirection[direction])
    player:addCondition(outfitCondition)
    player:setDirection(direction)

    -- Change Speed
    player:changeSpeed(config.speed.playerSpeed)

    -- Set Storage
    player:setStorageValue(config.wagonStorage, 1)

    -- Move the wagon
    moveWagon(player:getId(), direction)
    return true
end
 
Last edited:
xD
30 minutes well spent.

However config could be a little better.
 
Instead of having these:
Code:
wagonDirection = {[DIRECTION_NORTH] = 7132, [DIRECTION_EAST] = 7131, [DIRECTION_SOUTH] = 7132, [DIRECTION_WEST] = 7131},
    railId = {7121, 7122, 7123, 7124, 7125, 7126, 7127, 7128, 7129, 7130},
    railDirection = {
        [7123] = {DIRECTION_EAST, DIRECTION_SOUTH},
        [7124] = {DIRECTION_WEST, DIRECTION_SOUTH},
        [7125] = {DIRECTION_EAST, DIRECTION_NORTH},
        [7126] = {DIRECTION_WEST, DIRECTION_NORTH}
    }
You could make 1 table:
Code:
    railDirection = {
        [7123] = {DIRECTION_EAST, DIRECTION_SOUTH},
        [7124] = {DIRECTION_WEST, DIRECTION_SOUTH},
        [7125] = {DIRECTION_EAST, DIRECTION_NORTH},
        [7126] = {DIRECTION_WEST, DIRECTION_NORTH},
        [7132] = {DIRECTION_SOUTH, DIRECTION_NORTH},
        [7131] = {DIRECTION_WEST, DIRECTION_WEST},
    }

And simply generate the railID's from that on script load.

there is simply no need to make config complicated xD
I Wouldn't even put these values inside a config and would just make a different table with name rails = {}
because why would anyone need to change a rail what shows turns left and make it turn right.

EDIT:
Skimmed code a little too.
Why do you use "redundant" commands if I may ask xD?
Example:
-- Change Speed
player:changeSpeed()

What is the purpose of this comment xD?
 
Instead of having these:
Code:
wagonDirection = {[DIRECTION_NORTH] = 7132, [DIRECTION_EAST] = 7131, [DIRECTION_SOUTH] = 7132, [DIRECTION_WEST] = 7131},
    railId = {7121, 7122, 7123, 7124, 7125, 7126, 7127, 7128, 7129, 7130},
    railDirection = {
        [7123] = {DIRECTION_EAST, DIRECTION_SOUTH},
        [7124] = {DIRECTION_WEST, DIRECTION_SOUTH},
        [7125] = {DIRECTION_EAST, DIRECTION_NORTH},
        [7126] = {DIRECTION_WEST, DIRECTION_NORTH}
    }
You could make 1 table:
Code:
    railDirection = {
        [7123] = {DIRECTION_EAST, DIRECTION_SOUTH},
        [7124] = {DIRECTION_WEST, DIRECTION_SOUTH},
        [7125] = {DIRECTION_EAST, DIRECTION_NORTH},
        [7126] = {DIRECTION_WEST, DIRECTION_NORTH},
        [7132] = {DIRECTION_SOUTH, DIRECTION_NORTH},
        [7131] = {DIRECTION_WEST, DIRECTION_WEST},
    }

And simply generate the railID's from that on script load.

there is simply no need to make config complicated xD
I Wouldn't even put these values inside a config and would just make a different table with name rails = {}
because why would anyone need to change a rail what shows turns left and make it turn right.

EDIT:
Skimmed code a little too.
Why do you use "redundant" commands if I may ask xD?
Example:
-- Change Speed
player:changeSpeed()

What is the purpose of this comment xD?
I made the code in one table, since there is no purpose to make two tables. Does not make any slight difference.

I comment, so people understand what those lines mean. Since i don't want people just copy and paste. Woah i changed speed value and take credit for code that don't understand. The meaning of my release, is that people take part and understand about it.
 
I comment, so people understand what those lines mean. Since i don't want people just copy and paste.
this function and comment has same name..

The reason to make 2 tables is simple:
1 for config. 1 for things you don't change or change very rarely (item/sprite changes)

And if you say there is no purpose of make more tables, why you made 3 tables to register itemID's?
 
I comment, so people understand what those lines mean. Since i don't want people just copy and paste.
this function and comment has same name..

The reason to make 2 tables is simple:
1 for config. 1 for things you don't change or change very rarely (item/sprite changes)

And if you say there is no purpose of make more tables, why you made 3 tables to register itemID's?
Wow, seems you just read the comment line of speed, there are other lines that is not self explanatory for beginners.
I splitted the table in one table, since i don't want to waste resources on using more loops. I rather have big table, than several loops to extract the information. There is not much information to exract anyway, but something to keep in mind. I'm looking for perfomence and simplicity.

If you don't like how i do it, feel free to make your own.
 
Wow, seems you just read the comment line of speed, there are other lines that is not self explanatory for beginners.
I splitted the table in one table, since i don't want to waste resources on using more loops. I rather have big table, than several loops to extract the information. There is not much information to exract anyway, but something to keep in mind. I'm looking for perfomence and simplicity.

If you don't like how i do it, feel free to make your own.
I don't want to make my own and just saying its not more simple xD
 
ghfh.PNG
im having a little issue here TFS 1.X. i never had this error before so i don't really know what is trying to tell me.
I would appreciate the help.
 
Post automatically merged:

1595868242887.png
 

Attachments

Last edited:
Back
Top