• 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

freddzor11

Member
Joined
May 25, 2009
Messages
695
Reaction score
5
Hello! I get this when I try to enter the snake-area
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 script
Code:
-- [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 = 740, y = 920, z = 7}, {x = 750, y = 930, z = 7}) > 0 then
		doTeleportThing(cid, fromPosition)
		return true
	end
	areaClean()
	doCombat(cid, combat, numberToVariant(cid))
	doTeleportThing(cid, {x = 746, y = 931, 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 == 920) or (dir == 2 and pos.y == 930) or (dir == 1 and pos.x == 750) or (dir == 3 and pos.x == 740) then
		gameOver(cid) -- Bail out if walking against a wall
		return
	end
	doMoveCreature(cid, dir)
	local pos2 = getCreaturePosition(cid) -- New
	if isInArea(pos2, {x = 740, y = 920, z = 7}, {x = 750, y = 930, 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(1487, 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}, 1487).uid
			if part ~= 0 then
				doRemoveItem(part)
				doCreateItem(1487, 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 .." Game Tokens.")
	else
		doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "[SNAKE] Game ended! Score: ".. score .." (Highscore: ".. hscore .."). Earnings: ".. profit .." Game Tokens.")
	end
	if profit > 0 then
		doPlayerAddItem(cid,6527,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(740,750),y=math.random(920,930),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=740,750 do
		for y=920,930 do
			local t = getThingFromPos({x=x,y=y,z=7,stackpos=1})
			if t.itemid > 0 then
				doRemoveItem(t.uid)
			end
		end
	end
end
 
Add this to functions:

Code:
function isCreatureInArea(fromPos, toPos)
                local creatures = {}
                for z=fromPos.z, toPos.z do
                                for y=fromPos.y, toPos.y do
                                                for x=fromPos.x, toPos.x do
                                                                thing = getThingFromPos({x=x,y=y,z=z, stackpos=255})
                                                                if thing.itemid > 0 then
                                                                                if isCreature(thing.uid) then
                                                                                                table.insert(creatures, thing.uid)
                                                                                end
                                                                end
                                                end
                                end
                end
                return creatures
end
 
Thank you very much! Can you also take a look on this? When I play pacman the tiles don't press down when I went on them once
Code:
-- Pacman script: Controller [TGY] 
pressedTiles = {}
gameStarted = false
dif = 1

local function pacmanCounter(text, start, col, dif)
	if start then 
		gameStarted = true
		doCreateMonster("Pacman Ghost", {x = 716, y = 937, z = 7})
		if dif > 1 then
			doCreateMonster("Pacman Ghost", {x = 726, y = 937, z = 7}) 
		end
		if dif > 2 then
			doCreateMonster("Pacman Ghost", {x = 721, y = 937, z = 7}) 
		end
	end
	doSendAnimatedText({x = 721, y = 942, z = 7}, text, col)
end

function onStepIn(cid, item, position, lastPosition, fromPosition, toPosition, actor)
	if false == isPlayer(cid) then
		return
	end
	
	if item.actionid == 5301 and item.itemid == 426 then
		-- HANDLE TILE
		if not gameStarted then
			--doPlayerPopupFYI(cid, "x")
			doTeleportThing(cid, fromPosition, true)
			return true
		end
		doTransformItem(item.uid, 425)
		table.insert(pressedTiles, position)
		local cnt = #pressedTiles
		doSendAnimatedText(position, (cnt).."/84", TEXTCOLOR_GREEN)
		if cnt > 83 then
			doTeleportThing(cid, {x = 717, y = 934, z = 6}, true)
			doSendAnimatedText(position, ":D", TEXTCOLOR_GREEN)
			local prize = 5
			if dif == 2 then prize = 10 end
			if dif == 3 then prize = 20 end
			doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "[PACMAN] You won ".. prize .." Game Tokens!")
			doPlayerAddItem(cid,6527,prize)
		end
	elseif item.actionid == 7392 then
		-- HANDLE JOINING PERSON
		local c = isCreatureInArea({x = 716, y = 937, z = 7}, {x = 726, y = 947, z = 7}) -- Getting crss inside
		for _,v in pairs(c) do
			if isPlayer(v) then
				doTeleportThing(cid, fromPosition, true)
				return true
			end
		end
		-- Nobody is inside, check ticket/pick it - check exhaust first
		--doPlayerPopupFYI(cid, "You must wait " .. (math.floor((getPlayerStorageValue(cid, 7948)-os.time())/60)+1) .. " more minutes till you can play again.")
		if getPlayerStorageValue(cid, 7948) > 0 and (os.time() < getPlayerStorageValue(cid, 7948)) then
			doTeleportThing(cid, fromPosition, true)
			doPlayerPopupFYI(cid, "You must wait " .. (math.floor((getPlayerStorageValue(cid, 7948)-os.time())/60)+1) .. " more minute(s) till you can play again.")
			return true
		end
		if getPlayerItemCount(cid, 6527) < 1 then
			doTeleportThing(cid, fromPosition, true)
			doPlayerPopupFYI(cid, "You need 1 Gold nugget to play.")
			return true
		end
		doPlayerRemoveItem(cid, 6527, 1)
		setPlayerStorageValue(cid, 7948, os.time() + 900)
		-- Set difficulity variabele
		dif = 1
		if item.uid == 7396 then
			dif = 2
		elseif item.uid == 7397 then
			dif = 3
		end
		-- Clean the area and reset the tiles now
		for _,v in pairs(c) do
			doRemoveCreature(v)
		end
		for _,v in pairs(pressedTiles) do
			doItemSetAttribute(doCreateItem(426, v),"aid", 5301)
		end
		pressedTiles = {}
		gameStarted = false
		doTeleportThing(cid, {x = 721, y = 942, z = 7}, true)
		pacmanCounter("3", false, TEXTCOLOR_RED, dif)
		addEvent(pacmanCounter,1000,"2", false, TEXTCOLOR_ORANGE, dif)
		addEvent(pacmanCounter,2000,"1", false, TEXTCOLOR_YELLOW, dif)
		addEvent(pacmanCounter,3000,"GO!", true, TEXTCOLOR_GREEN, dif)
	end


	return true
end

I also get this in Console
Code:
(luaDoCreateMonster) Monster with name 'Pacman Ghost' not found
[Error - MoveEvent::executeStep] Call stack overflow.
[Error - MoveEvent::executeStep] Call stack overflow.

[Error - MoveEvents Interface]
In a timer event called from:
data/movements/scripts/pacman.lua:onStepIn
 
Last edited:
Add an entry for it, should look like the other entries in the file and just change the name and file path.
 
Add where? :O and what should I add etc?
Code:
[Error - TalkAction Interface]
data/talkactions/scripts/creature.lua:onSay
Description:
(luaDoCreateMonster) Monster with name 'pacman ghost' not found
 
Back
Top