Vc sempre kis npcs igual ao rl tibia ?
aki esta sua chanche
isso msm
com sell e buy
olha eu sei explicar a função + vou passar ela como me ensinaram
No final do code do npc adicione a sua função de npc andar
item naum agrupaveis por enquanto só poderão ser vendidos de 1 em 1
vai na pasta lib da pasta npc e adicione
-- get the distance to a creaturefunction getDistanceToCreature(id)
if id == 0 or id == nil then
selfGotoIdle()
end
local cx, cy, cz = creatureGetPosition(id)
if cx == nil then
return nil
end
local sx, sy, sz = selfGetPosition()
return math.max(math.abs(sx-cx), math.abs(sy-cy))
end
-- Add a creature to the costumer queue
function addToQueue(cid, queue)
local i = 1
while true do
local temp = queue
if temp == nil then
queue = cid
return
end
i = i+1
end
end
-- General function for removing the given index from a table
function removeFromTable(thisTable, position)
thisTable[position] = nil
while true do
position = position+1
local value = thisTable[position]
if value == nil then
return
else
thisTable[position-1] = value
thisTable[position] = nil
end
end
end
-- General function for adding the given value to a table
function addToTable(thisTable, value)
local position = 1
while true do
if(thisTable[position] == nil) then
thisTable[position] = value
return
end
position = position+1
end
end
-- General function for getting the amount of entries in the table
function getTableSize(thisTable)
local n = 0
local i = 1
while true do
if(thisTable == nil) then
return n
end
n = n+1
i = i+1
end
end
-- Remove a creature from the message queue
function removeFromQueue(cid, queue)
local pos = 1
while true do
local value = queue[pos]
if value == nil then
return
else
if value == cid then
removeFromTable(queue, pos)
return
end
end
pos = pos+1
end
end
-- Get the next costumer from the costumer queue
function getNextCostumer(queue)
local value = queue[1]
if value == nil then
return nil
else
return value
end
end
-- Checks if the given creature is in the queue
function isInQueue(cid, queue)
local pos = 1
while true do
local value = queue[pos]
if value == nil then
return false
else
if value == cid then
return true
end
end
pos = pos+1
end
return false
end
function greetNextCostumer(queue)
local nextCID = getNextCostumer(queue)
if nextCID == nil then
return
else
local value = creatureGetName(nextCID)
if value == nil then
removeFromQueue(nextCID, queue)
greetNextCostumer(queue)
return
else
local result = checkDistance(nextCID)
if result == 1 then
selfSay('Hello, ' .. creatureGetName(nextCID) .. '! What a pleasent surprise.')
focus = nextCID
talk_start = os.clock()
removeFromQueue(nextCID, queue)
return
else
removeFromQueue(nextCID, queue)
greetNextCostumer(queue)
return
end
end
end
end
function checkDistance(cid)
if cid > 0 then
local x, y, z = selfGetPosition()
local x2, y2, z2 = creatureGetPosition(cid)
if not(x2 == nil) and not(y2 == nil) and not(z2 == nil) then
if not(z == z2) or (x - x2) > 4 or (y - y2) > 4 or (x - x2) < (-4) or (y - y2) < (-4) then
return 0
else
return 1
end
end
end
return 1
end
function getCount(msg)
local pattern = "%d+"
local ret = 1
local b, e = string.find(msg, pattern)
if b ~= nil and e ~= nil then
ret = tonumber(string.sub(msg, b, e))
end
return ret
end
-- Test functions
function addKeyword(npcmap, keys, f, params)
addToTable(npcmap, {keywords = keys, func = f, parameters = params})
end
function msgContains(message, keyword)
return string.find(message, '(%a*)' .. keyword .. '(%a*)')
end
function processMessage(npcmap, message)
for i = 1,getTableSize(npcmap) do
local key = npcmap
local haskeywords = true
for i2 = 1,getTableSize(key.keywords) do
local tmp = key.keywords[i2]
if(not msgContains(message, tmp)) then
haskeywords = false
break
end
end
if(haskeywords) then
key.func(message, key.keywords, key.parameters)
return true
end
end
return false
end
(algums npcs podem bugar depois disso)
agr vamos ao um exemplo de npc
esse vende e compra joias
focus = 0itemid = 0
count = 0
charges = 0
cost = 0
talkcount = 0 -- Keep track of what we said last time --
talk_start = 0
tempcount = 0
queue = {}
npcmap = {}
function sellItem(message, keywords, parameters)
tempcount = getCount(message)
selfSay('Do you want to sell ' .. tempcount .. ' ' .. keywords[2] .. ' for ' .. parameters.cost*tempcount .. ' gold coins?')
talkcount = 1
itemid = parameters.itemid
count = tempcount
cost = count*parameters.cost
end
function buyItem(message, keywords, parameters)
tempcount = getCount(message)
selfSay('Do you want to buy ' .. tempcount .. ' ' .. keywords[1] .. ' for ' .. parameters.cost*tempcount .. ' gold coins?')
talkcount = 2
itemid = parameters.itemid
count = tempcount
cost = count*parameters.cost
end
function confirmAction(message, keywords, parameters)
if(keywords[1] == 'yes') then
if(talkcount == 1) then
talkcount = 0
sell(focus,itemid,count,cost) -- Add your function for selling items to an npc here
elseif(talkcount == 2) then
talkcount = 0
buy(focus,itemid,count,cost) -- Add your function for buying items from an npc here!
end
elseif(keywords[1] == 'no') then
if(talkcount == 1) then
selfSay('I wouldnt sell that either.')
talkcount = 0
elseif(talkcount == 2) then
selfSay('Too expensive you think?')
talkcount = 0
end
end
end
function sayMessage(message, keywords, parameters)
selfSay(parameters.text)
talkcount = 0
end
-- Sell item keywords
addKeyword(npcmap, {'sell', 'small amethyst'}, sellItem, {itemid = 2150, cost = 200})
addKeyword(npcmap, {'sell', 'small diamond'}, sellItem, {itemid = 2145, cost = 300})
addKeyword(npcmap, {'sell', 'small emerald'}, sellItem, {itemid = 2145, cost = 250})
addKeyword(npcmap, {'sell', 'small ruby'}, sellItem, {itemid = 2147, cost = 250})
addKeyword(npcmap, {'sell', 'small rubie'}, sellItem, {itemid = 2147, cost = 250})
addKeyword(npcmap, {'sell', 'small sapphire'}, sellItem, {itemid = 2146, cost = 250})
addKeyword(npcmap, {'sell', 'black pearl'}, sellItem, {itemid = 2143, cost = 160})
addKeyword(npcmap, {'sell', 'white pearl'}, sellItem, {itemid = 2144, cost = 280})
-- Buy item keywords
addKeyword(npcmap, {'small amethyst'}, buyItem, {itemid = 2150, cost = 400})
addKeyword(npcmap, {'small diamond'}, buyItem, {itemid = 2145, cost = 600})
addKeyword(npcmap, {'small emerald'}, buyItem, {itemid = 2145, cost = 500})
addKeyword(npcmap, {'small ruby'}, buyItem, {itemid = 2147, cost = 500})
addKeyword(npcmap, {'small rubie'}, buyItem, {itemid = 2147, cost = 500})
addKeyword(npcmap, {'small sapphire'}, buyItem, {itemid = 2146, cost = 500})
addKeyword(npcmap, {'black pearl'}, buyItem, {itemid = 2143, cost = 320})
addKeyword(npcmap, {'white pearl'}, buyItem, {itemid = 2144, cost = 560})
-- Confirm sell/buy keywords
addKeyword(npcmap, {'yes'}, confirmAction)
addKeyword(npcmap, {'no'}, confirmAction)
-- General message keywords
addKeyword(npcmap, {'offer'}, sayMessage, {text = 'I only buy items right now.'})
addKeyword(npcmap, {'sell'}, sayMessage, {text = 'Why would I need that rubbish?'})
addKeyword(npcmap, {'job'}, sayMessage, {text = 'I buy all kinds of pearls, diamonds, rubies, sapphires and amethysts!'})
addKeyword(npcmap, {'quest'}, sayMessage, {text = 'A quest is nothing I want to be involved in.'})
addKeyword(npcmap, {'mission'},sayMessage, {text = 'I cannot help you in that area, son.'})
addKeyword(npcmap, {'buy'}, sayMessage, {text = 'Sorry, but I do not sell any items right now.'})
function onThingMove(creature, thing, oldpos, oldstackpos)
end
function onCreatureAppear(creature)
end
function onCreatureDisappear(id)
if id == focus then
focus = 0
itemid = 0
count = 0
charges = 0
cost = 0
talk_start = 0
talkcount = 0
selfSay('Next please')
if not(queue[1] == nil) then
greetNextCostumer(queue)
end
end
end
function onCreatureTurn(creature)
end
function onCreatureSay(cid, type, msg)
local dist = checkDistance(cid)
if dist == 0 then
return
end
msg = string.lower(msg)
if cid == focus then
talk_start = os.clock()
local ret = processMessage(npcmap, msg)
if(ret == true) then
return
end
end
if (string.find(msg, '(%a*)hi(%a*)') and string.len(msg) == 2) or (string.find(msg, '(%a*)hello(%a*)') and string.len(msg) == 5) or (string.find(msg, '(%a*)hey(%a*)') and string.len(msg) == 3) then
if focus == cid then
selfSay('I am already talking to you.')
talk_start = os.clock()
elseif focus > 0 or not(queue[1] == nil) then
selfSay('Please, ' .. creatureGetName(cid) .. '. Wait for your turn!.')
if not isInQueue(cid, queue) then
addToQueue(cid, queue)
end
elseif(focus == 0) and (queue[1] == nil) then
selfSay('Hello, ' .. creatureGetName(cid) .. ', wanna trade some gems?')
focus = cid
talk_start = os.clock()
end
return
end
if (string.find(msg, '(%a*)bye(%a*)') or string.find(msg, '(%a*)farewell(%a*)')) and cid == focus then
selfSay('Farewell, ' .. creatureGetName(cid) .. '!')
focus = 0
itemid = 0
talk_start = 0
talkcount = 0
count = 0
charges = 0
cost = 0
if not(queue[1] == nil) then
greetNextCostumer(queue)
end
return
end
if(cid == focus and talkcount ~= 0) then
selfSay('I guess not then.')
talkcount = 0
return
end
end
function onCreatureChangeOutfit(creature)
end
function onThink()
if (os.clock() - talk_start) > 25 then
if focus > 0 then
selfSay('Next please!')
talkcount = 0
end
focus = 0
itemid = 0
talk_start = 0
talkcount = 0
count = 0
charges = 0
cost = 0
if not(queue[1] == nil) then
greetNextCostumer(queue)
end
end
local dist = checkDistance(focus)
if dist == 0 then
selfSay('Next please!')
focus = 0
itemid = 0
talk_start = 0
talkcount = 0
count = 0
charges = 0
cost = 0
talkcount = 0
if not(queue[1] == nil) then
greetNextCostumer(queue)
end
end
end
explicando:
-- General message keywordsaddKeyword(npcmap, {'offer'}, sayMessage, {text = 'I only buy items right now.'})
addKeyword(npcmap, {'sell'}, sayMessage, {text = 'Why would I need that rubbish?'})
addKeyword(npcmap, {'job'}, sayMessage, {text = 'I buy all kinds of pearls, diamonds, rubies, sapphires and amethysts!'})
addKeyword(npcmap, {'quest'}, sayMessage, {text = 'A quest is nothing I want to be involved in.'})
addKeyword(npcmap, {'mission'},sayMessage, {text = 'I cannot help you in that area, son.'})
addKeyword(npcmap, {'buy'}, sayMessage, {text = 'Sorry, but I do not sell any items right now.'})
ai são as palavras chaves toda vez q o player falar ele executara uma ação correspondente
-- Sell item keywordsaddKeyword(npcmap, {'sell', 'small amethyst'}, sellItem, {itemid = 2150, cost = 200})
addKeyword(npcmap, {'sell', 'small diamond'}, sellItem, {itemid = 2145, cost = 300})
addKeyword(npcmap, {'sell', 'small emerald'}, sellItem, {itemid = 2145, cost = 250})
addKeyword(npcmap, {'sell', 'small ruby'}, sellItem, {itemid = 2147, cost = 250})
addKeyword(npcmap, {'sell', 'small rubie'}, sellItem, {itemid = 2147, cost = 250})
addKeyword(npcmap, {'sell', 'small sapphire'}, sellItem, {itemid = 2146, cost = 250})
addKeyword(npcmap, {'sell', 'black pearl'}, sellItem, {itemid = 2143, cost = 160})
addKeyword(npcmap, {'sell', 'white pearl'}, sellItem, {itemid = 2144, cost = 280})
ai os itens q ele vende
vc só prescisa mudar isso
muito simples
onde esta sell é a 1º keyword white pearl seria a segunda qdo um player falar as 2
ele comparara o itemid 2144 (white pearl) custando 280 gp cada
-- Buy item keywordsaddKeyword(npcmap, {'small amethyst'}, buyItem, {itemid = 2150, cost = 400})
addKeyword(npcmap, {'small diamond'}, buyItem, {itemid = 2145, cost = 600})
addKeyword(npcmap, {'small emerald'}, buyItem, {itemid = 2145, cost = 500})
addKeyword(npcmap, {'small ruby'}, buyItem, {itemid = 2147, cost = 500})
addKeyword(npcmap, {'small rubie'}, buyItem, {itemid = 2147, cost = 500})
addKeyword(npcmap, {'small sapphire'}, buyItem, {itemid = 2146, cost = 500})
addKeyword(npcmap, {'black pearl'}, buyItem, {itemid = 2143, cost = 320})
addKeyword(npcmap, {'white pearl'}, buyItem, {itemid = 2144, cost = 560})
ai são os itens q ele vende
seguindo o msm eskema do sell
se vc é uma pessoa q sabe o basico de ot server
podera manejar esse sistem facilmente
+ cuidado
ao mexer
se houver bug do npc naum andar
é só vc add a função
é isso
creditos: jiddo do otfans por fazer a função
e eu por adaptar ela para uso
flws
duvidas criticas ou sugestões postem aki: