Ir para conteúdo

Npc Que Que Vende Se Tiver Tal Storage


BravHart

Posts Recomendados

Como viu no título, preciso de um npc que ira vender tais itens somente se o cara tiver tal storage.

 

Tipo, o cara fala hi, o npc responde, ai o cara fala "sell", ai só ira abrir o trade se o cara tiver a storage.

 

 

Editado por BravHart
Link para o comentário
Compartilhar em outros sites

if getPlayerStorageValue(cid,STORAGE) then
                               selfSay('Mensagem se ele tiver storage?', cid)
                               talkState[talkUser] = 1
                       else
                               selfSay('Se ele nao tiver a storage', cid)
                       end

 

Certo, mais quero que que quando ele fale "sell", faça a verificação e se tiver com a storage certa, vai abrir o trade, assim ele podendo vender os titens.

 

desde já obrigado!

 

 

@Oneshot, ai este meu NPC.lua.

 

BravHart,

Poste o conteúdo de npc.lua localizado em data/npc/lib. Isso me ajudará a desenvolver seu pedido.

 


function selfIdle()
following = false
attacking = false

selfAttackCreature(0)
target = 0
end

function selfSayChannel(cid, message)
return selfSay(message, cid, false)
end

function selfMoveToCreature(id)
if(not id or id == 0) then
	return
end

local t = getCreaturePosition(id)
if(not t.x or t.x == nil) then
	return
end

selfMoveTo(t.x, t.y, t.z)
return
end

function getNpcDistanceToCreature(id)
if(not id or id == 0) then
	selfIdle()
	return nil
end

local c = getCreaturePosition(id)
if(not c.x or c.x == 0) then
	return nil
end

local s = getCreaturePosition(getNpcId())
if(not s.x or s.x == 0 or s.z ~= c.z) then
	return nil
end

return math.max(math.abs(s.x - c.x), math.abs(s.y - c.y))
end

function doMessageCheck(message, keyword)
if(type(keyword) == "table") then
	return table.isStrIn(keyword, message)
end

local a, b = message:lower():find(keyword:lower())
if(a ~= nil and b ~= nil) then
	return true
end

return false
end

function doNpcSellItem(cid, itemid, amount, subType, ignoreCap, inBackpacks, backpack)
local amount = amount or 1
local subType = subType or 1
local ignoreCap = ignoreCap and true or false

local item = 0
if(isItemStackable(itemid)) then
	if(isItemRune(itemid)) then
		amount = amount * subType
	end

	local count = amount
	repeat
		item = doCreateItemEx(itemid, math.min(100, count))
		if(doPlayerAddItemEx(cid, item, ignoreCap) ~= RETURNVALUE_NOERROR) then
			return 0, 0
		end

		count = count - math.min(100, count)
	until count == 0
	return amount, 0
end

local a = 0
if(inBackpacks) then
	local container = doCreateItemEx(backpack, 1)
	local b = 1
	for i = 1, amount do
		item = doAddContainerItem(container, itemid, subType)
		if(itemid == ITEM_PARCEL) then
			doAddContainerItem(item, ITEM_LABEL)
		end

		if(isInArray({(getContainerCapById(backpack) * b), amount}, i)) then
			if(doPlayerAddItemEx(cid, container, ignoreCap) ~= RETURNVALUE_NOERROR) then
				b = b - 1
				break
			end

			a = i
			if(amount > i) then
				container = doCreateItemEx(backpack, 1)
				b = b + 1
			end
		end
	end

	return a, b
end

for i = 1, amount do
	item = doCreateItemEx(itemid, subType)
	if(itemid == ITEM_PARCEL) then
		doAddContainerItem(item, ITEM_LABEL)
	end

	if(doPlayerAddItemEx(cid, item, ignoreCap) ~= RETURNVALUE_NOERROR) then
		break
	end

	a = i
end

return a, 0
end

function doRemoveItemIdFromPos(id, n, position)
local thing = getThingFromPos({x = position.x, y = position.y, z = position.z, stackpos = 1})
if(thing.itemid ~= id) then
	return false
end

doRemoveItem(thing.uid, n)
return true
end

function getNpcName()
return getCreatureName(getNpcId())
end

function getNpcPos()
return getCreaturePosition(getNpcId())
end

function selfGetPosition()
local t = getNpcPos()
return t.x, t.y, t.z
end

msgcontains = doMessageCheck
moveToPosition = selfMoveTo
moveToCreature = selfMoveToCreature
selfMoveToPosition = selfMoveTo
selfGotoIdle = selfIdle
isPlayerPremiumCallback = isPremium
doPosRemoveItem = doRemoveItemIdFromPos
doNpcBuyItem = doPlayerRemoveItem
doNpcSetCreatureFocus = selfFocus
getNpcCid = getNpcId
getDistanceTo = getNpcDistanceTo
getDistanceToCreature = getNpcDistanceToCreature


Editado por BravHart
Link para o comentário
Compartilhar em outros sites

Desculpe a demora.

 

 

 

local focuses = {}
local talk_start = 0

local topic = {}

local itemTable = {
{id = 5880, subType = 0, buy = 100, sell = 100, name = "Iron Ore"}
}

local storage = 9999

local items = {}
for _, item in ipairs(itemTable) do
items[item.id] = {buyPrice = item.buy, sellPrice = item.sell, subType = item.subType, realName = item.name}
end

function onBuy(cid, item, subType, amount, ignoreCap, inBackpacks)
if items[item] == nil then
	selfSay("Ehm... sorry... This shoudn't be there, I'm not selling it.", cid)
	return
end

if getPlayerMoney(cid) >= amount * items[item].buyPrice then
	local i = doNpcSellItem(cid, item, amount, subType, ignoreCap, inBackpacks)
	if i < amount then
		if i == 0 then
			selfSay("Sorry, but you don't have space to take it.", cid)
		else
			selfSay("I've sold some for you, but it seems you can't carry more than this. I won't take more money than necessary.", cid)
			doPlayerRemoveMoney(cid, i * items[item].buyPrice)
		end
	else
		selfSay("Thank you for the money.", cid)
		doPlayerRemoveMoney(cid, amount * items[item].buyPrice)
	end
else
	selfSay("Sorry, but you don't have money.", cid)
end
return true
end

function onSell(cid, item, subType, amount, ignoreCap, inBackpacks)
if(items[item] == nil) then
	selfSay("Ehm.. sorry... this shouldn't be there, I'm not buying it.", cid)
end

if(subType < 1) then
	subType = -1
end
if(doPlayerRemoveItem(cid, item, amount, subType)) then
	doPlayerAddMoney(cid, items[item].sellPrice * amount)
	selfSay("Here your money.", cid)
else
	selfSay("No item, no deal.", cid)
end
return true
end

function onCreatureSay(cid, type, msg)
msg = msg:lower() or ""
if getNpcDistanceTo(cid) > 3 then
	return false
end

if doMessageCheck(msg, {"hi", "hello"}) and not(isFocused(cid, focuses)) then
	selfSay("Hello, ".. getCreatureName(cid) ..". Just say {trade} if you want to see my rare items.", cid)
	addFocus(cid, focuses)
	selfFocus(cid)
	talk_start = os.clock()
	topic[cid] = 1
elseif doMessageCheck(msg, {"bye", "farewell"}) and isFocused(cid, focuses) then
	selfSay("Good bye, ".. getCreatureName(cid) ..".", cid)
	removeFocus(cid, focuses)
	closeShopWindow(cid)
	setFocus(focuses)
end

if topic[cid] == 1 then
	if doMessageCheck(msg, {"trade", "buy", "sell"}) then
		if getPlayerStorageValue(cid, storage) >= 1 then
			selfSay("Pretty nice, isn't it?", cid)
			openShopWindow(cid, itemTable, onBuy, onSell)
		else
			selfSay("Heh... sorry... but I can't show my items to you.", cid)
		end
	end
end
return true
end

function onThink()
for _, cid in ipairs(focuses) do
	if isPlayer(cid) and isFocused(cid, focuses) then
		if os.clock() > (talk_start + 180) then
			talk_start = 0
			selfSay("Hmph!")
			closeShopWindow(cid)
			removeFocus(cid, focuses)
		elseif getNpcDistanceTo(cid) > 3 then
			talk_start = 0
			selfSay("How Rude!")
			closeShopWindow(cid)
			removeFocus(cid, focuses)
		end
	end
end
lookAtFocus(focuses)
end

 

 

 

 

Para adicionar os itens que o NPC venderá ou comprará:

 


local itemTable = {
{id = 5880, subType = 0, buy = 100, sell = 0, name = "Iron Ore"}
}

 

Coloque uma vírgula no final da última linha e adicione outra:

 


local itemTable = {
{id = 5880, subType = 0, buy = 100, sell = 0, name = "Iron Ore"},
       {id = 3000, subType = 0, buy = 500, sell = 10, name = "EXEMPLO"} 
}

 

E adicione essas funções no seu arquivo npc.lua, caso contrário o código apresentará erros.

 


function isFocused(cid, t)
   for i, v in pairs(t) do
       if(v == cid) then
           return true
       end
   end
   return false
end

function addFocus(focus, t)
   if(not isFocused(focus, t)) then
       table.insert(t, focus)
   end
end

function setFocus(t)
   for i, v in pairs(t) do
       if(isPlayer(v)) then
           doNpcSetCreatureFocus(v)
           return
       end
   end
   doNpcSetCreatureFocus(0)
end

function removeFocus(cid, t)
   for i, v in pairs(t) do
       if(v == cid) then
           table.remove(t, i)
		selfFocus(0)
           break
       end
   end
end

 

E configure o storage que o código verificará aqui:

 

local storage = 9999

 

Sinta-se livre para efetuar quaisquer modificações nas falas do NPC.

Editado por Oneshot
Link para o comentário
Compartilhar em outros sites

 

Desculpe a demora.

 

 

 

local focuses = {}
local talk_start = 0

local topic = {}

local itemTable = {
{id = 5880, subType = 0, buy = 100, sell = 100, name = "Iron Ore"}
}

local storage = 9999

local items = {}
for _, item in ipairs(itemTable) do
items[item.id] = {buyPrice = item.buy, sellPrice = item.sell, subType = item.subType, realName = item.name}
end

function onBuy(cid, item, subType, amount, ignoreCap, inBackpacks)
if items[item] == nil then
	selfSay("Ehm... sorry... This shoudn't be there, I'm not selling it.", cid)
	return
end

if getPlayerMoney(cid) >= amount * items[item].buyPrice then
	local i = doNpcSellItem(cid, item, amount, subType, ignoreCap, inBackpacks)
	if i < amount then
		if i == 0 then
			selfSay("Sorry, but you don't have space to take it.", cid)
		else
			selfSay("I've sold some for you, but it seems you can't carry more than this. I won't take more money than necessary.", cid)
			doPlayerRemoveMoney(cid, i * items[item].buyPrice)
		end
	else
		selfSay("Thank you for the money.", cid)
		doPlayerRemoveMoney(cid, amount * items[item].buyPrice)
	end
else
	selfSay("Sorry, but you don't have money.", cid)
end
return true
end

function onSell(cid, item, subType, amount, ignoreCap, inBackpacks)
if(items[item] == nil) then
	selfSay("Ehm.. sorry... this shouldn't be there, I'm not buying it.", cid)
end

if(subType < 1) then
	subType = -1
end
if(doPlayerRemoveItem(cid, item, amount, subType)) then
	doPlayerAddMoney(cid, items[item].sellPrice * amount)
	selfSay("Here your money.", cid)
else
	selfSay("No item, no deal.", cid)
end
return true
end

function onCreatureSay(cid, type, msg)
msg = msg:lower() or ""
if getNpcDistanceTo(cid) > 3 then
	return false
end

if doMessageCheck(msg, {"hi", "hello"}) and not(isFocused(cid, focuses)) then
	selfSay("Hello, ".. getCreatureName(cid) ..". Just say {trade} if you want to see my rare items.", cid)
	addFocus(cid, focuses)
	selfFocus(cid)
	talk_start = os.clock()
	topic[cid] = 1
elseif doMessageCheck(msg, {"bye", "farewell"}) and isFocused(cid, focuses) then
	selfSay("Good bye, ".. getCreatureName(cid) ..".", cid)
	removeFocus(cid, focuses)
	closeShopWindow(cid)
	setFocus(focuses)
end

if topic[cid] == 1 then
	if doMessageCheck(msg, {"trade", "buy", "sell"}) then
		if getPlayerStorageValue(cid, storage) >= 1 then
			selfSay("Pretty nice, isn't it?", cid)
			openShopWindow(cid, itemTable, onBuy, onSell)
		else
			selfSay("Heh... sorry... but I can't show my items to you.", cid)
		end
	end
end
return true
end

function onThink()
for _, cid in ipairs(focuses) do
	if isPlayer(cid) and isFocused(cid, focuses) then
		if os.clock() > (talk_start + 180) then
			talk_start = 0
			selfSay("Hmph!")
			closeShopWindow(cid)
			removeFocus(cid, focuses)
		elseif getNpcDistanceTo(cid) > 3 then
			talk_start = 0
			selfSay("How Rude!")
			closeShopWindow(cid)
			removeFocus(cid, focuses)
		end
	end
end
lookAtFocus(focuses)
end

 

 

 

 

Para adicionar os itens que o NPC venderá ou comprará:

 


local itemTable = {
{id = 5880, subType = 0, buy = 100, sell = 0, name = "Iron Ore"}
}

 

Coloque uma vírgula no final da última linha e adicione outra:

 


local itemTable = {
{id = 5880, subType = 0, buy = 100, sell = 0, name = "Iron Ore"},
       {id = 3000, subType = 0, buy = 500, sell = 10, name = "EXEMPLO"} 
}

 

E adicione essas funções no seu arquivo npc.lua, caso contrário o código apresentará erros.

 


function isFocused(cid, t)
   for i, v in pairs(t) do
       if(v == cid) then
           return true
       end
   end
   return false
end

function addFocus(focus, t)
   if(not isFocused(focus, t)) then
       table.insert(t, focus)
   end
end

function setFocus(t)
   for i, v in pairs(t) do
       if(isPlayer(v)) then
           doNpcSetCreatureFocus(v)
           return
       end
   end
   doNpcSetCreatureFocus(0)
end

function removeFocus(cid, t)
   for i, v in pairs(t) do
       if(v == cid) then
           table.remove(t, i)
		selfFocus(0)
           break
       end
   end
end

 

E configure o storage que o código verificará aqui:

 

local storage = 9999

 

Sinta-se livre para efetuar quaisquer modificações nas falas do NPC.

 

 

Hey Oneshot!

 

 

Da dando esse erro no exe, sem parar:

 

1zvbdpc.png

 

 

 

 

Desde já, agradeço.

Editado por BravHart
Link para o comentário
Compartilhar em outros sites

Desculpe, BravHart, fiquei ausente por um tempo. O problema é que outra função também devia ser adicionada ao npc.lua. Adicione apenas essa linha que o sistema irá reconhecer.

lookAtFocus = setFocus

 

Um abraço.

Editado por Oneshot
Link para o comentário
Compartilhar em outros sites

  • Quem Está Navegando   0 membros estão online

    • Nenhum usuário registrado visualizando esta página.
×
×
  • Criar Novo...