Olá,
venho postar uma função elaborada por mim que serve para vender items de quantidade mais facilmente.
a função é a seguinte:
buytype(uid,txt,id,min,max,price)
uid - variavel do player, normalmente é cid
txt - variavel das falas do player, normalmente é msg
id - id do item que quer vender (função feita para items de quantidade)
min - a quantidade minima que se pode comprar de tal item
max - a quantidade maxima que se pode comprar de tal item
price - preço por cada quantidade
Exemplo: (vendendo hmm por 40gps cada tiro sendo o maximo que se pode comprar 255 e o minimo 5)
buytype(cid,msg,2311,5,255,50)
Exemplo de uma conversa entre o player e o NPC:
player: Hi
NPC: Hi, I sell runes and arrows!
player: 15775 hmm (se o maximo for hmm de 100x, ele só recebe de 100x)
NPC: Here you are.
player: abc50 uh (o player irá comprar uma uh de 50x)
NPC: Here you are.
player: arrow (não colocou nenhum numero, então irá receber o minimo, neste caso poderia ser uma arrow)
NPC: Here you are
Agora vamos ao que interessa,
1º na pasta data\npc\scripts\lib abra o arquivo npc.lua e adicione o seguinte code no final:
-------function buytype by Colex-------function buytype(uid,txt,id,min,max,price)x = string.gsub(txt,"%a","")x = tonumber(x)if x ~= nill and x > min then if x <= max then price = x * price buy(uid,id,x,price) else price = max * price buy(uid,id,max,price) endelse buy(uid,id,min,price)endend
Agora vou dar um exemplo de um NPC que vende runes, o maximo de cargas que ele vende em cada rune é de 150 e o minimo variado por cada rune:
focus = 0talk_start = 0target = 0following = falseattacking = falsefunction onThingMove(creature, thing, oldpos, oldstackpos)endfunction onCreatureAppear(creature)endfunction onCreatureDisappear(cid, pos) if focus == cid then selfSay('Good bye then.') focus = 0 talk_start = 0 endendfunction onCreatureTurn(creature)endfunction msgcontains(txt, str) return (string.find(txt, str) and not string.find(txt, '(%w+)' .. str) and not string.find(txt, str .. '(%w+)'))endfunction onCreatureSay(cid, type, msg) msg = string.lower(msg) -- greeting phrase if string.find(msg, '(%a*)hi(%a*)') and focus == 0 and string.len(msg) == 2 and getDistanceToCreature(cid) < 3 then selfSay('Hello ' .. creatureGetName(cid) .. '! I sell runes.') focus = cid talk_start = os.clock() elseif focus == cid then talk_start = os.clock() if msgcontains(msg, 'runes') then selfSay('I sell hmm(40gps), uh(90gps), gfb(60gps), explosion(80gps), sd(100gps) and blank runes(5gps).) elseif msgcontains(msg, 'hmm') then buytype(cid,msg,2311,5,150,40) elseif msgcontains(msg, 'uh') then buytype(cid,msg,2273,1,150,90) elseif msgcontains(msg, 'gfb') then buytype(cid,msg,2304,3,150,60) elseif msgcontains(msg, 'explosion') then buytype(cid,msg,2313,3,150,80) elseif msgcontains(msg, 'sd') then buytype(cid,msg,2311,1,150,100) elseif msgcontains(msg, 'blank') then buy(cid,2260,1,5) ---não usa buytype porque não é item de quantidade elseif string.find(msg, '(%a*)bye(%a*)') and getDistanceToCreature(cid) < 4 then selfSay('Good bye, ' .. creatureGetName(cid) .. '!') focus = 0 talk_start = 0 end endendfunction onCreatureChangeOutfit(creature)endfunction onThink() end
Isto é só um exemplo para mostrar como usar a função!
Alguma dúvida? encontrou algum bug?
deixe aqui sua mensagem!
Atenciosamente,
Colex