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

Lua Snake system

Akkz

New Member
Joined
Apr 22, 2012
Messages
172
Reaction score
0
Getting theese errors when I'm trying to enter the snake game

Code:
[Error - MoveEvents Interface]
data/movements/scripts/snake.lua:onStepIn
Description:
data/movements/scripts/snake.lua:22: attempt to call global 'isCreatureInArea' (
a nil value)
stack traceback:
        data/movements/scripts/snake.lua:22: in function <data/movements/scripts
/snake.lua:18>

And here is my snake.lua
Lua:
-- [TGY]
local snake = {}
local speed = 500
local itemPos = {}
local itemId = 1629
local playing = false
local addTrail = false

local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_AGGRESSIVE, 0)

local condition = createConditionObject(CONDITION_HASTE)
setConditionParam(condition, CONDITION_PARAM_TICKS, 2000)
setConditionFormula(condition, 200, -200, 200, -200)
setCombatCondition(combat, condition)
-- Combat param for boost!

function onStepIn(cid, item, position, fromPosition)
	if not (isPlayer(cid)) then
		return true
	end
	if #isCreatureInArea({x = 1204, y = 1017, z = 7}, {x = 1203, y = 1016, z = 7}) > 0 then
		doTeleportThing(cid, fromPosition)
		return true
	end
	areaClean()
	doCombat(cid, combat, numberToVariant(cid))
	doTeleportThing(cid, {x = 1203, y = 1016, z = 7})
	doCreatureSetNoMove(cid, true)

	placeDisc(true)
	addTrail = false
	speed = 500
	snake = {}
	
	doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "[SNAKE] ------------------------------------------------")
	doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "[SNAKE] Hold CTRL and use the ARROW KEYS to change direction.")
	local score = getPlayerStorageValue(cid, 45720)
	if score < 0 then score = 0 end
	doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "[SNAKE] Your highscore: ".. score ..".")
	doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "[SNAKE] ------------------------------------------------")
	
	addEvent(playerMove, speed, cid)
	playing = true
	return true
end

function playerMove(cid)
	local dir = getCreatureLookDirection(cid)
	local pos = getCreaturePosition(cid) -- Old
	if (dir == 0 and pos.y == 1006) or (dir == 2 and pos.y == 1016) or (dir == 1 and pos.x == 1198) or (dir == 3 and pos.x == 1208) then
		gameOver(cid) -- Bail out if walking against a wall
		return
	end
	doMoveCreature(cid, dir)
	local pos2 = getCreaturePosition(cid) -- New
	if isInArea(pos2, {x = 1198, y = 1006, z = 7}, {x = 1208, y = 1016, z = 7}) == false then
		gameOver(cid) -- Glitched out somehow (teleport?)
		return
	end
	--[[if #snake == 1 then
		if pos2.x == snake[1].x and pos2.y == snake[1].y then
			gameOver(cid) -- Bail out knocking in a snake piece
			return
		end
	end]]
	for _,v in pairs(snake) do
		if pos2.x == v.x and pos2.y == v.y then
			gameOver(cid) -- Bail out knocking in a snake piece
			return
		end
	end
	if addTrail then
		doCreateItem(1486, 1, pos)
		table.insert(snake,pos)
		addTrail = false
	else
		-- Move latest trail item
		if #snake > 0 then
			local part = getTileItemById({x=snake[1].x,y=snake[1].y,z=7}, 1486).uid
			if part ~= 0 then
				doRemoveItem(part)
				doCreateItem(1486, 1, pos)
				table.insert(snake,pos)
				table.remove(snake,1)
			end
		end
	end
	if pos2.x == itemPos.x and pos2.y == itemPos.y then
		-- Item caught
		speed = math.floor(speed * 0.97)
		placeDisc(false)
		doSendAnimatedText(pos2, (#snake + 1), TEXTCOLOR_ORANGE)
		-- Add item to trail
		addTrail = true
	end
	doCombat(cid, combat, numberToVariant(cid))
	addEvent(playerMove, speed, cid)
end

function gameOver(cid)
	doCreatureSetNoMove(cid, false)
	doTeleportThing(cid, {x = 1000, y = 1000, z = 7})
	doRemoveItem(itemId)
	for _,v in pairs(snake) do
		local part = getThingFromPos({x=snake[1].x,y=snake[1].y,z=7,stackpos=1})
		if part.itemid > 0 then
			doRemoveItem(part.uid)
		end
	end
	local hscore = getPlayerStorageValue(cid, 45720)
	local score = #snake
	local profit = math.floor((score - 10)/4)
	if profit < 0 then profit = 0 end
	if score > hscore then
		setPlayerStorageValue(cid, 45720, score)
		doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "[SNAKE] Game ended! Score: ".. score .." (NEW HIGHSCORE). Earnings: ".. profit .." Crystal Coins.")
	else
		doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "[SNAKE] Game ended! Score: ".. score .." (Highscore: ".. hscore .."). Earnings: ".. profit .." Crystal Coins.")
	end
	if profit > 0.2 then
		doPlayerAddItem(cid,2160,profit)
	end
	playing = false
end

function placeDisc(isNew)
	if not isNew then
		--itemPos = {x=math.random(866,876),y=math.random(923,933),z=8}
		doRemoveItem(itemId)
	end
	local try = 0
	while true do
		local done = true
		try = try + 1
		itemPos = {x=math.random(1198,1208),y=math.random(1006,1016),z=7}
		for _,v in pairs(snake) do
			if itemPos.x == v.x and itemPos.y == v.y then
				done = false
				--print("exhaust")
				break
			end
		end
		if done or try > 25 then break end
	end
	doItemSetAttribute(doCreateItem(9566, 1, itemPos), "uid", itemId)
	--end
end

function areaClean()
	for x=1198,1208 do
		for y=1006,1016 do
			local t = getThingFromPos({x=x,y=y,z=7,stackpos=1})
			if t.itemid > 0 then
				doRemoveItem(t.uid)
			end
		end
	end
end
 
Make a new file in your data/lib/yourfilename.lua

Add this

Lua:
function isCreatureInArea(fromPos, toPos)
	local t = {}
	for z=fromPos.z, toPos.z do
		for y=fromPos.y, toPos.y do
			for x=fromPos.x, toPos.x do
				local v = getTopCreature({x=x,y=y,z=z})
				if v.itemid == 1 and table.find({1,2,3}, v.type) then
					table.insert(t, v.uid)
				end
			end
		end
	end
	return t
end
 
Back
Top