• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua Mock's snake system error

MohamedRefaat

Marketing and Coding
Premium User
Joined
Jan 18, 2014
Messages
991
Solutions
3
Reaction score
232
Location
Egypt
Hello,

I am using Mock's Snake system and i made everything like it say, but something wrong.

character teleported to control position for like 1 second and kicked out and only (snake food) appear on the ground.

there is no console error.

i am using tfs 0.3.7 for 8.6

lib file
Code:
--[[
Script by Mock the bear
]]
SNAKE = {
_VERSION="1.0 by mock",
---Snake config
itemid=1739,
freeglobalstorage=28103,
itemFood=6394,
controlpos={x=1465,y=2629,z=6},
exitpos = {x=1470,y=2620,z=7},
centerpos={x=1464,y=2628,z=7},
timer = function(cid,n,pos_,time)
local pos_ = pos_ or {{SNAKE.centerpos}}
setGlobalStorageValue(SNAKE.freeglobalstorage,cid)
if not isPlayer(cid) 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])
pos[1] = getPosByDir({x=pos[1].x,y=pos[1].y,z=pos[1].z,stackpos=255},getCreatureLookDir(cid))
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
addEvent(doTeleportThing,1000,cid,SNAKE.exitpos)
addEvent(doCreatureSay,1100,cid,'Points '..(#pos_-1)..'.')
SNAKE.clean()
setGlobalStorageValue(SNAKE.freeglobalstorage,0)
return
end
if ret == 2 then
doRemoveItem(p.uid,-1)
if p.itemid == SNAKE.itemFood then
pos_[#pos_+1] = {pos[2],pos[2]}
for i=1,5 do
addEvent(doSendMagicEffect,100*i,pos[1],29)
end
SNAKE.generateFood()
end
end
doCreateItem(SNAKE.itemid,1,pos[1])

end
local plpos = getCreaturePosition(cid)
local generated = {}
for i=0,3 do
generated = getPosByDir({x=SNAKE.controlpos.x,y=SNAKE.controlpos.y,z=SNAKE.controlpos.z},i)
end
for i,pos in pairs(generated) do
if SNAKE.samepos(plpos,pos) then

doTeleportThing(cid,SNAKE.controlpos,false)
end
addEvent(doSendMagicEffect,100,pos,56,cid)
end
addEvent(SNAKE.timer,time,cid,n,pos_,time)
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 p2.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}
for i=1,5 do
addEvent(doSendMagicEffect,100*i,pp,30)
end
doCreateItem(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 p = getThingFromPos(pp)
if p.itemid ~= 0 then
doRemoveItem(p.uid,-1)
doSendMagicEffect(pp,15)
end
end
end
end
end,
check = function(pos)
for i=1,10 do
pos.stackpos = i
local p = getThingFromPos(pos)
if p.itemid == SNAKE.itemid then
return 1,p,true
elseif not SNAKE.isWalkable(pos) then
return 3,p,false
elseif p.itemid ~= 0 then
return 2,p
end
end
return false
end,
find_and_delete = function(pos)
for i=0,255 do
pos.stackpos = 255-i
local p = getThingFromPos(pos)
if p.itemid == SNAKE.itemid then
return doRemoveItem(p.uid,1)
end
end
end,
isWalkable = function(pos, creature, proj, pz)-- by Nord
if getTileThingByPos({x = pos.x, y = pos.y, z = pos.z, stackpos = 0}).itemid == 0 then return false end
if getTopCreature(pos).uid > 0 and creature then return false end
if getTileInfo(pos).protection and pz then return false, true end
local n = not proj and 3 or 2
for i = 0, 255 do
pos.stackpos = i
local tile = getTileThingByPos(pos)
if tile.itemid ~= 0 and not isCreature(tile.uid) then
if hasProperty(tile.uid, n) or hasProperty(tile.uid, 7) then
return false
end
end
end
return true
end,
}

and action file
Code:
function onUse(cid, item, frompos, item2, topos)
if not isPlayer(getGlobalStorageValue(SNAKE.freeglobalstorage)) then
doTeleportThing(cid,SNAKE.controlpos)
SNAKE.timer(cid,1,nil,item.actionid == 0 and 500 or item.actionid)
doPlayerSendTextMessage(cid,25,'Hold key CTRL and use the arrows to control snake.')
SNAKE.generateFood()
else
doPlayerSendTextMessage(cid,25,'Please wait.')
end
end

this is map photo
snake-setting-png.27722


<action itemid="9564" event="script" value="snakesys.lua"/>
 
Last edited:
There are no crashes you can have uptime of 100+ hours with no errors.

I only have one problem (could be my own lua scripts/linux setup) where occasionally it would get laggy (too frequent for my preference).
Still looking to find an answer for that.
ok , thanks :)
 
I got it working on 0.3.7,
What I did was changed this part:
Code:
local generated = {}
for i=0,3 do
generated = getPosByDir({x=SNAKE.controlpos.x,y=SNAKE.controlpos.y,z=SNAKE.controlpos.z},i)
end
to: (The 4 control positions around the center position (floor 6)
Code:
    local generated = {
    {x = 20, y = 511, z = 6},
    {x = 21, y = 512, z = 6},
    {x = 20, y = 513, z = 6},
    {x = 19, y = 512, z = 6}
    }

Ofc change the positions to match your map :p
 
I got it working on 0.3.7,
What I did was changed this part:
Code:
local generated = {}
for i=0,3 do
generated = getPosByDir({x=SNAKE.controlpos.x,y=SNAKE.controlpos.y,z=SNAKE.controlpos.z},i)
end
to: (The 4 control positions around the center position (floor 6)
Code:
    local generated = {
    {x = 20, y = 511, z = 6},
    {x = 21, y = 512, z = 6},
    {x = 20, y = 513, z = 6},
    {x = 19, y = 512, z = 6}
    }

Ofc change the positions to match your map :p
should be like this ?
Code:
local plpos = getCreaturePosition(cid)
    local generated = {
    {x = 1465, y = 2628, z = 6},
    {x = 1466, y = 2629, z = 6},
    {x = 1465, y = 2630, z = 6},
    {x = 1464, y = 2629, z = 6}
    }   
for i,pos in pairs(generated) do
if SNAKE.samepos(plpos,pos) then

doTeleportThing(cid,SNAKE.controlpos,false)
end
 
should be like this ?
Code:
local plpos = getCreaturePosition(cid)
    local generated = {
    {x = 1465, y = 2628, z = 6},
    {x = 1466, y = 2629, z = 6},
    {x = 1465, y = 2630, z = 6},
    {x = 1464, y = 2629, z = 6}
    } 
for i,pos in pairs(generated) do
if SNAKE.samepos(plpos,pos) then

doTeleportThing(cid,SNAKE.controlpos,false)
end

Should be fine. But as I've been playing it since last night with my brother, we tested and once the player reaches 20+ Snake chains it begins to lag the whole server. I'm looking for a way to optimize the performance, let me know if you how your server performs after adding it in.
 
Should be fine. But as I've been playing it since last night with my brother, we tested and once the player reaches 20+ Snake chains it begins to lag the whole server. I'm looking for a way to optimize the performance, let me know if you how your server performs after adding it in.
aha :( anyways it still no work after i changed that :(
 
The way you are describing it I can tell the problem is here:
Code:
check = function(pos)
for i=1,10 do
pos.stackpos = i
local p = getThingFromPos(pos)
if p.itemid == SNAKE.itemid then
return 1,p,true
elseif not SNAKE.isWalkable(pos) then
return 3,p,false
elseif p.itemid ~= 0 then
return 2,p
end
end
return false
end
If it returns 1 or 3 it will send you to the exit position with 0 points.
That is why I asked to make sure that the 5 squares above the game board are protection zone.
You can try to change to this:
Code:
check = function(pos)
for i=1,10 do
pos.stackpos = i
local p = getThingFromPos(pos)
if p.itemid == SNAKE.itemid then
return 1,p,true
elseif p.itemid ~= 0 then
return 2,p
end
end
return false
end
The second code doesn't check the tiles anymore for protection zone. However, just double check on your map first that the tiles are indeed a PZ zone and if they are and it still doens't work try the second code.

But I also advise against using this game on your server. When the players reach 35-40 chains everyone begins to lag. If someone is playing all day then you'll have a lag all day. I was sad to remove it because it's so fun but when it comes down to it people don't log on to tibia to play Snake.
 
The way you are describing it I can tell the problem is here:
Code:
check = function(pos)
for i=1,10 do
pos.stackpos = i
local p = getThingFromPos(pos)
if p.itemid == SNAKE.itemid then
return 1,p,true
elseif not SNAKE.isWalkable(pos) then
return 3,p,false
elseif p.itemid ~= 0 then
return 2,p
end
end
return false
end
If it returns 1 or 3 it will send you to the exit position with 0 points.
That is why I asked to make sure that the 5 squares above the game board are protection zone.
You can try to change to this:
Code:
check = function(pos)
for i=1,10 do
pos.stackpos = i
local p = getThingFromPos(pos)
if p.itemid == SNAKE.itemid then
return 1,p,true
elseif p.itemid ~= 0 then
return 2,p
end
end
return false
end
The second code doesn't check the tiles anymore for protection zone. However, just double check on your map first that the tiles are indeed a PZ zone and if they are and it still doens't work try the second code.

But I also advise against using this game on your server. When the players reach 35-40 chains everyone begins to lag. If someone is playing all day then you'll have a lag all day. I was sad to remove it because it's so fun but when it comes down to it people don't log on to tibia to play Snake.
i think i wont use it too :( but i will try the second code
 
Back
Top