- Joined
- Jan 15, 2014
- Messages
- 1,317
- Solutions
- 35
- Reaction score
- 434
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:
Four tiles around the Control position:
First Floor:
Second Floor:
actions.xml
data/actions/snake.lua
data/lib/snakesys.lua
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:
Second Floor:
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: