• 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 Snake System TFS 1.X [Mocks]

imkingran

Learning everyday.
Premium User
Joined
Jan 15, 2014
Messages
1,318
Solutions
35
Reaction score
435
Original thread: http://otland.net/threads/snake-system.88387/
Original Author: @Mock
Credits to: @Mock


Installation:
Edit these lines in the library to match your map:
Code:
itemid=1860,
freeglobalstorage=28103,
itemFood=6394,
controlpos= {x = 28, y = 198, z = 6},
exitpos = {x = 36, y = 190, z = 7},
centerpos= {x = 27, y = 197, z = 7},
wallID = 1486, -- itemId of the wall around the arena
interval = 300, -- speed at which the nake should move

Four tiles around the Control position:
Code:
    local generated = {
    {x = 28, y = 197, z = 6},
    {x = 29, y = 198, z = 6},
    {x = 28, y = 199, z = 6},
    {x = 27, y = 198, z = 6}
    }

First Floor:
iSechvT.png


Second Floor:
JvqgSLR.png


actions.xml
Code:
<action actionid="200" script="snake.lua"/>

data/actions/snake.lua

Code:
function onUse(player, item, fromPosition, itemEx, toPosition)
    if (Game.getStorageValue(SNAKE.freeglobalstorage)) ~= 1 then
        player:teleportTo(SNAKE.controlpos)
        SNAKE.timer(player.uid,1,nil,1000)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'Hold key CTRL and use the arrows to control snake.')
        SNAKE.generateFood()
    else
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'Please wait.')
    end
end

data/lib/snakesys.lua
Code:
--[[
Script by Mock the bear
Converted to TFS 1.1 by imkingran
]]
SNAKE = {
---Snake config
itemid=1860,
freeglobalstorage=28103,
itemFood=6394,
controlpos= {x = 28, y = 198, z = 6},
exitpos = {x = 36, y = 190, z = 7},
centerpos= {x = 27, y = 197, z = 7},
wallID = 1486,
interval = 300,
timer =
    function(player,n,pos_)
    local player = Player(player)
    local pos_ = pos_ or {{SNAKE.centerpos}}
    Game.setStorageValue(SNAKE.freeglobalstorage, 1)
        if not player:isPlayer() then
            SNAKE.clean()
            return
        end
        for i,pos in pairs(pos_) do
        SNAKE.find_and_delete(pos[1])
            if i == 1 then
                pos[2] = SNAKE.copypos(pos[1])
                if player:getDirection() == 0 then
                    pos[1] = {x=pos[1].x,y=pos[1].y - 1,z=pos[1].z,stackpos=255}
                elseif player:getDirection() == 1 then
                    pos[1] = {x=pos[1].x + 1,y=pos[1].y,z=pos[1].z,stackpos=255}
                elseif player:getDirection() == 2 then
                    pos[1] = {x=pos[1].x,y=pos[1].y + 1,z=pos[1].z,stackpos=255}
                elseif player:getDirection() == 3 then
                    pos[1] = {x=pos[1].x - 1,y=pos[1].y,z=pos[1].z,stackpos=255}
                end
            else
                pos[2] = SNAKE.copypos(pos[1])
                pos[1] = pos_[i-1][2]
            end
        local ret,p,walk = SNAKE.check(pos[1])
            if ret == 1 or ret == 3 then
                player:teleportTo(SNAKE.exitpos, false)
                player:say('Points '..(#pos_-1)..'.', TALKTYPE_MONSTER_SAY)
                SNAKE.clean()
                Game.setStorageValue(SNAKE.freeglobalstorage,0)
                return
            end
            if ret == 2 then
                p:remove()
                    if p then
                        pos_[#pos_+1] = {pos[2],pos[2]}
                                local tile = Tile(pos[1])
                                tile:getPosition():sendMagicEffect(CONST_ME_FIREWORK_RED)
                        player:say(''..(#pos_-1)..'', TALKTYPE_MONSTER_SAY, false, player, pos[1])
                        SNAKE.generateFood()
                    end
            end
            Game.createItem(SNAKE.itemid,1,pos[1])

        end
 
    local plpos = player:getPosition()

    local generated = {
    {x = 28, y = 197, z = 6},
    {x = 29, y = 198, z = 6},
    {x = 28, y = 199, z = 6},
    {x = 27, y = 198, z = 6}
    }
        for i,pos in pairs(generated) do
                if SNAKE.samepos(plpos,pos) then

                    player:teleportTo(SNAKE.controlpos,false)
                end
            local tile = Tile(pos)
            tile:getPosition():sendMagicEffect(CONST_ME_TUTORIALSQUARE)
        end
    addEvent(SNAKE.timer,SNAKE.interval,player.uid,n,pos_)
    end,

copypos =
    function(p)
    return {x=p.x,y=p.y,z=p.z,stackpos=p.stackpos}
    end,

samepos =
    function(p1,p2)
        if p1.x == p2.x and p1.y == p2.y then
            return true
        end
    return false
    end,

generateFood =
    function()
        local pp = {x=SNAKE.centerpos.x+math.random(-6,6),y=SNAKE.centerpos.y+math.random(-4,4),z=SNAKE.centerpos.z}
        local tile = Tile(pp)
        tile:getPosition():sendMagicEffect(CONST_ME_FIREWORK_BLUE)
        Game.createItem(SNAKE.itemFood,1,pp)
    end,

clean = function()
     for y=-4,4 do
       for x=-6,6 do
       local pp = {x=SNAKE.centerpos.x+x,y=SNAKE.centerpos.y+y,z=SNAKE.centerpos.z}
         for i=250,255 do
         pp.stackpos = i
         local tile = Tile(pp)
           if tile then
             local items = tile:getItems()
             if #items > 0 then
               for i =1,#items do
                 items[i]:remove()
               end
               tile:getPosition():sendMagicEffect(CONST_ME_HITBYFIRE)
             end
           end
         end
       end
     end
   end,
 
check =
    function(pos)
        for i=1,10 do
        pos.stackpos = i
        local tile = Tile(pos)
        local cake = tile:getItemById(SNAKE.itemid)
        local p = tile:getItemById(SNAKE.itemFood)
        local wall = tile:getItemById(SNAKE.wallID)
            if cake then
                return 1,cake,true
            elseif wall then
                return 1,wall,true
            elseif p then
                return 2,p
            end
        end
    return false
    end,

find_and_delete =
    function(pos)
        for i=0,255 do
        pos.stackpos = 255-i
            local tile1 = Tile(pos)
            local p = tile1 and tile1:getItemById(SNAKE.itemid) or false
            if p then
                return p:remove()
            end
        end
    end,

isWalkable =
    function(pos, creature, proj, pz)-- by Nord
    local position = {x = pos.x, y = pos.y, z = pos.z, stackpos = 0}
    local tile2 = Tile(position)
    if tile2:getThing().itemid == 0 then return false end
    if tile2:getCreatureCount() > 0 then return false end
    if tile2:hasFlag(TILESTATE_PROTECTIONZONE) then return false, true end
    local n = not proj and 3 or 2
        for i = 0, 255 do
        pos.stackpos = i
        local tile = Tile(pos)
        local tile3 = tile:getItemById()
            if tile3.itemid ~= 0 and not tile3:isCreature() then
                if hasItemProperty(tile3.uid, n) or hasItemProperty(tile3.uid, 7) then
                    return false
                end
            end
        end
    return true
    end
}
 
Last edited:
Our idea is to rewrite all well-known scripts to 1.1. We're not going to downgrade anything.
Original thread link was posted so 0.4 users can use old script version too.
 
3ICZRr8.png



Help ? TFS 1.2 Protocol 10.79

It looks like the library: snakesys.lua, wasn't loaded.
Where did you put the library file?

Just add in global.lua: dofile('data/libs/snakesys.lua') and excecute the command /reload global in-game, make sure the path to the file is correct.
 
Ouxi, sorry guys..

now i got other thing, the item (snake) dont appear and I'm kicked from event, maybe wrong floors?

Ixad7GY.png
 
3d25f3a2e650ec11313faabdb3dd60d5.jpg
I give up.. lol any ideas? TFS 1.2
 
3d25f3a2e650ec11313faabdb3dd60d5.jpg
I give up.. lol any ideas? TFS 1.2

Sorry for double...

SOLVED: I had center pos same as control pos lawl.. Control pos is the one on level 2 and center pos is the one below. incase anyone else runs into this.
 
It looks like the library: snakesys.lua, wasn't loaded.
Where did you put the library file?

Just add in global.lua: dofile('data/libs/snakesys.lua') and excecute the command /reload global in-game, make sure the path to the file is correct.

Is it possible to clear the whole room when you lose? Instead of just the food and snake? Because if someone is playing and throwing something down after he loses it still stays there
 
Last edited:
Is it possible to clear the whole room when you lose? Instead of just the food and snake? Because if someone is playing and throwing something down after he loses it still stays there

You can use this new library, the SNAKE.clean() function was improved to remove all the items upon a loss.
 
Last edited:
You can use this new library, the SNAKE.clean() function was improved to remove all the items upon a loss.
Code:
--[[
Script by Mock the bear
Converted to TFS 1.1 by imkingran
]]
SNAKE = {
---Snake config
itemid=1860,
freeglobalstorage=28103,
itemFood=6394,
controlpos= {x = 28, y = 198, z = 6},
exitpos = {x = 36, y = 190, z = 7},
centerpos= {x = 27, y = 197, z = 7},
wallID = 1486,
interval = 300,
timer =
    function(player,n,pos_)
    local player = Player(player)
    local pos_ = pos_ or {{SNAKE.centerpos}}
    Game.setStorageValue(SNAKE.freeglobalstorage, 1)
        if not player:isPlayer() then
            SNAKE.clean()
            return
        end
        for i,pos in pairs(pos_) do
        SNAKE.find_and_delete(pos[1])
            if i == 1 then
                pos[2] = SNAKE.copypos(pos[1])
                if player:getDirection() == 0 then
                    pos[1] = {x=pos[1].x,y=pos[1].y - 1,z=pos[1].z,stackpos=255}
                elseif player:getDirection() == 1 then
                    pos[1] = {x=pos[1].x + 1,y=pos[1].y,z=pos[1].z,stackpos=255}
                elseif player:getDirection() == 2 then
                    pos[1] = {x=pos[1].x,y=pos[1].y + 1,z=pos[1].z,stackpos=255}
                elseif player:getDirection() == 3 then
                    pos[1] = {x=pos[1].x - 1,y=pos[1].y,z=pos[1].z,stackpos=255}
                end
            else
                pos[2] = SNAKE.copypos(pos[1])
                pos[1] = pos_[i-1][2]
            end
        local ret,p,walk = SNAKE.check(pos[1])
            if ret == 1 or ret == 3 then
                player:teleportTo(SNAKE.exitpos, false)
                player:say('Points '..(#pos_-1)..'.', TALKTYPE_MONSTER_SAY)
                SNAKE.clean()
                Game.setStorageValue(SNAKE.freeglobalstorage,0)
                return
            end
            if ret == 2 then
                p:remove()
                    if p then
                        pos_[#pos_+1] = {pos[2],pos[2]}
                                local tile = Tile(pos[1])
                                tile:getPosition():sendMagicEffect(CONST_ME_FIREWORK_RED)
                        player:say(''..(#pos_-1)..'', TALKTYPE_MONSTER_SAY, false, player, pos[1])
                        SNAKE.generateFood()
                    end
            end
            Game.createItem(SNAKE.itemid,1,pos[1])

        end
 
    local plpos = player:getPosition()

    local generated = {
    {x = 28, y = 197, z = 6},
    {x = 29, y = 198, z = 6},
    {x = 28, y = 199, z = 6},
    {x = 27, y = 198, z = 6}
    }
        for i,pos in pairs(generated) do
                if SNAKE.samepos(plpos,pos) then

                    player:teleportTo(SNAKE.controlpos,false)
                end
            local tile = Tile(pos)
            tile:getPosition():sendMagicEffect(CONST_ME_TUTORIALSQUARE)
        end
    addEvent(SNAKE.timer,SNAKE.interval,player.uid,n,pos_)
    end,

copypos =
    function(p)
    return {x=p.x,y=p.y,z=p.z,stackpos=p.stackpos}
    end,

samepos =
    function(p1,p2)
        if p1.x == p2.x and p1.y == p2.y then
            return true
        end
    return false
    end,

generateFood =
    function()
        local pp = {x=SNAKE.centerpos.x+math.random(-6,6),y=SNAKE.centerpos.y+math.random(-4,4),z=SNAKE.centerpos.z}
        local tile = Tile(pp)
        tile:getPosition():sendMagicEffect(CONST_ME_FIREWORK_BLUE)
        Game.createItem(SNAKE.itemFood,1,pp)
    end,

clean = function()
     for y=-4,4 do
       for x=-6,6 do
       local pp = {x=SNAKE.centerpos.x+x,y=SNAKE.centerpos.y+y,z=SNAKE.centerpos.z}
         for i=250,255 do
         pp.stackpos = i
         local tile = Tile(pp)
           if tile then
             local items = tile:getItems()
             if #items > 0 then
               for i =1,#items do
                 items[i]:remove()
               end
               tile:getPosition():sendMagicEffect(CONST_ME_HITBYFIRE)
             end
           end
         end
       end
     end
   end,
 
check =
    function(pos)
        for i=1,10 do
        pos.stackpos = i
        local tile = Tile(pos)
        local cake = tile:getItemById(SNAKE.itemid)
        local p = tile:getItemById(SNAKE.itemFood)
        local wall = tile:getItemById(SNAKE.wallID)
            if cake then
                return 1,cake,true
            elseif wall then
                return 1,wall,true
            elseif p then
                return 2,p
            end
        end
    return false
    end,

find_and_delete =
    function(pos)
        for i=0,255 do
        pos.stackpos = 255-i
     
            local tile1 = Tile(pos)
            local p = tile1:getItemById(SNAKE.itemid)
            if p then
                return p:remove()
            end
        end
    end,

isWalkable =
    function(pos, creature, proj, pz)-- by Nord
    local position = {x = pos.x, y = pos.y, z = pos.z, stackpos = 0}
    local tile2 = Tile(position)
    if tile2:getThing().itemid == 0 then return false end
    if tile2:getCreatureCount() > 0 then return false end
    if tile2:hasFlag(TILESTATE_PROTECTIONZONE) then return false, true end
    local n = not proj and 3 or 2
        for i = 0, 255 do
        pos.stackpos = i
        local tile = Tile(pos)
        local tile3 = tile:getItemById()
            if tile3.itemid ~= 0 and not tile3:isCreature() then
                if hasItemProperty(tile3.uid, n) or hasItemProperty(tile3.uid, 7) then
                    return false
                end
            end
        end
    return true
    end
}

It's giving me this error
Code:
Lua Script Error: [Action Interface]
data/actions/scripts/snake.lua:onUse
data/lib/snakesys.lua:149: attempt to index local 'tile1' <a nil value>
stack traceback:
    [C]: in fuction '___index'
    data/lib/snakesys.lua:149: in function 'find_and_delete'
    data/lib/snakesys.lua:25: in function 'timer'
    data/actions/scripts/snake.lua:4: in function <data/actions/scripts.snake.lua:1>
 
It's giving me this error
Code:
Lua Script Error: [Action Interface]
data/actions/scripts/snake.lua:onUse
data/lib/snakesys.lua:149: attempt to index local 'tile1' <a nil value>
stack traceback:
    [C]: in fuction '___index'
    data/lib/snakesys.lua:149: in function 'find_and_delete'
    data/lib/snakesys.lua:25: in function 'timer'
    data/actions/scripts/snake.lua:4: in function <data/actions/scripts.snake.lua:1>

Copy the library again from the main post and see if it fixes your error.
 
Back
Top