• 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 NPC sell vial of ...

arturhaddad

Member
Joined
Aug 14, 2010
Messages
217
Reaction score
8
My NPCs don't sell vial of something, like vial of water, blood, oil, etc

In scripts I've tried:

Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local Topic = {}

function onCreatureAppear(cid)            npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)        npcHandler:onCreatureDisappear(cid)            end
function onCreatureSay(cid, type, msg)    npcHandler:onCreatureSay(cid, type, msg)    end
function onThink()                        npcHandler:onThink()    end

local shopModule = ShopModule:new()
npcHandler:addModule(shopModule)
-------------------------------------------------SHOP---------------------------------------------------------
shopModule:addBuyableItem({'vial of water'}, 2006, 8, 1, 'vial of water')

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

It worked for the water, but:

Gives 2 vials of water (or one of the subtypes listed bellow)

Work until subtype 7
1 - water
2 - blood
3 - beer
4 - slime
5 - limonade
6 - milk
7 - manafluid

Any value above 7 gives

[ShopModule.onBuy]
Item not found on shopItems list


And if I try to add it trough XML:

<parameter key="shop_buyable" value="vial of oil,2006,20;11"/>

fz16br.jpg
 
Code:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="name npc" nameDescription="potion vendor" script="default.lua" walkinterval="2000" floorchange="0">
	<health now="100" max="100"/>
	<look type="130" head="39" body="122" legs="125" feet="57" addons="0"/>
	<parameters>
		<parameter key="message_greet" value="Hello |PLAYERNAME|. I sell potions,etccc........"/>
		<parameter key="message_decline" value="Is |TOTALCOST| gold coins too much for you? Get out of here!"/>
		<parameter key="shop_buyable" value="vial of oil,2006,20" />
	</parameters>
</npc>



:p easy
 
Code:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="name npc" nameDescription="potion vendor" script="default.lua" walkinterval="2000" floorchange="0">
	<health now="100" max="100"/>
	<look type="130" head="39" body="122" legs="125" feet="57" addons="0"/>
	<parameters>
		<parameter key="message_greet" value="Hello |PLAYERNAME|. I sell potions,etccc........"/>
		<parameter key="message_decline" value="Is |TOTALCOST| gold coins too much for you? Get out of here!"/>
		<parameter key="shop_buyable" value="vial of oil,2006,20" />
	</parameters>
</npc>



:p easy
That would give the player an empty vial.
Gives 2 vials of water (or one of the subtypes listed bellow)
That would happen if you defined shop module in both the Lua script and in the XML file (module_shop).

If you want the NPC to sell vials of oil, try:
XML:
<parameter key="shop_buyable" value="vial of oil,2006,20,11"/>
but because of limitations of reverseFluidMap, they can NOT sell both vials of oil and vials of liquids of same colour, like beer, tea, mead or mud, without hacks to the Lua npc system or some source edits.
 
Cyk, with this:

Lua:
<parameter key="shop_buyable" value="vial of oil,2006,20,11"/>

I receive

[ShopModule.onBuy]
Item not found on shopItems list

That would happen if you defined shop module in both the Lua script and in the XML file (module_shop).

I receive 2 vials if I set it trough LUA, XML don't even work (error mentioned above)

BUT, if I set an value between 1 and 7, like this:

Lua:
<parameter key="shop_buyable" value="vial of oil,2006,20,7;"/>

13:06 Bought 1x vial for 20 gold.

It works, both in XML and LUA

-- edit --

I've found this in npc/lib/npcsystem/modules.lua

Lua:
		-- Adds a new buyable item.
	--	names = A table containing one or more strings of alternative names to this item. Used only for old buy/sell system.
	--	itemid = The itemid of the buyable item
	--	cost = The price of one single item
	--	subType - The subType of each rune or fluidcontainer item. Can be left out if it is not a rune/fluidcontainer. Default value is 1.
	--	realName - The real, full name for the item. Will be used as ITEMNAME in MESSAGE_ONBUY and MESSAGE_ONSELL if defined. Default value is nil (getItemNameById will be used)
	function ShopModule:addBuyableItem(names, itemid, cost, subType, realName)
		if(SHOPMODULE_MODE ~= SHOPMODULE_MODE_TALK) then
			local item = {
				id = itemid,
				buy = cost,
				sell = -1,
				subType = subType or 1,
				name = realName or getItemNameById(itemid)
			}

			for i, shopItem in ipairs(self.npcHandler.shopItems) do
				if(shopItem.id == item.id and shopItem.subType == item.subType) then
					if(item.sell ~= shopItem.sell) then
						item.sell = shopItem.sell
					end

					self.npcHandler.shopItems[i] = item
					item = nil
					break
				end
			end

			if(item ~= nil) then
				table.insert(self.npcHandler.shopItems, item)
			end
		end

		if(names ~= nil and SHOPMODULE_MODE ~= SHOPMODULE_MODE_TRADE) then
			local parameters = {
				itemid = itemid,
				cost = cost,
				eventType = SHOPMODULE_BUY_ITEM,
				module = self,
				realName = realName or getItemNameById(itemid),
				subType = subType or 1
			}

			for i, name in pairs(names) do
				local keywords = {}
				table.insert(keywords, 'buy')
				table.insert(keywords, name)

				local node = self.npcHandler.keywordHandler:addKeyword(keywords, ShopModule.tradeItem, parameters)
				node:addChildKeywordNode(self.yesNode)
				node:addChildKeywordNode(self.noNode)
			end
		end
	end

Any shots?
 
Last edited:
Found something interesting in const.h:

Lua:
enum FluidTypes_t
{
	FLUID_NONE		= FLUID_EMPTY,
	FLUID_WATER		= FLUID_BLUE,
	FLUID_BLOOD		= FLUID_RED,
	FLUID_BEER		= FLUID_BROWN,
	FLUID_SLIME		= FLUID_GREEN,
	FLUID_LEMONADE		= FLUID_YELLOW,
	FLUID_MILK		= FLUID_WHITE,
	FLUID_MANA		= FLUID_PURPLE,

	FLUID_LIFE		= FLUID_RED + 8,
	FLUID_OIL		= FLUID_BROWN + 8,
	FLUID_URINE		= FLUID_YELLOW + 8,
	FLUID_COCONUTMILK	= FLUID_WHITE + 8,
	FLUID_WINE		= FLUID_PURPLE + 8,

	FLUID_MUD		= FLUID_BROWN + 16,
	FLUID_FRUITJUICE	= FLUID_YELLOW + 16,

	FLUID_LAVA		= FLUID_RED + 24,
	FLUID_RUM		= FLUID_BROWN + 24,
	FLUID_SWAMP		= FLUID_GREEN + 24,

	FLUID_TEA               = FLUID_BROWN + 32,
	FLUID_MEAD              = FLUID_BROWN + 40
};
const uint8_t reverseFluidMap[] =
{
	FLUID_EMPTY,
	FLUID_WATER,
	FLUID_MANA,
	FLUID_BEER,
	FLUID_EMPTY,
	FLUID_BLOOD,
	FLUID_SLIME,
	FLUID_EMPTY,
	FLUID_LEMONADE,
	FLUID_MILK
};

Subtypes higher than 7 have +8, +16, etc, that can't be coincidence, that must be causing the bug

ALSO, reversefluidmap don't have OIL, etc... Is there a way to change beer to oil or something like this? That's whats causing tah bug!
 
Hmm, try to 'look' at the vial of oil sprite in the trade window. Does it say "You see a vial of oil" or "You see a vial of beer"?
 
going to school now, when I get back I'll report everything I've done and stuff, so we can find an solution for this annoying bug that got even u Cyk... see
 
So...

I've tried this alternative script:

Lua:
local focuses = {}
local function isFocused(cid)
	for i, v in pairs(focuses) do
		if(v == cid) then
			return true
		end
	end
	return false
end

local function addFocus(cid)
	if(not isFocused(cid)) then
		table.insert(focuses, cid)
	end
end
local function removeFocus(cid)
	for i, v in pairs(focuses) do
		if(v == cid) then
			table.remove(focuses, i)
			break
		end
	end
end
local function lookAtFocus()
	for i, v in pairs(focuses) do
		if(isPlayer(v)) then
			doNpcSetCreatureFocus(v)
			return
		end
	end
	doNpcSetCreatureFocus(0)
end

local itemWindow = {
	{id=2006, subType=11, buy=20, name="Vial of Oil"},
	{id=2152, subType=0, buy=100, sell=100, name="Test2"},
	{id=2148, subType=0, buy=1, sell=1, name="Test3"},
	{id=2173, subType=0, buy=10000, sell=5000, name="Test4"}
}

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

local function getPlayerMoney(cid)
	return ((getPlayerItemCount(cid, 2160) * 10000) +
	(getPlayerItemCount(cid, 2152) * 100) +
	getPlayerItemCount(cid, 2148))
end

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

	if(getPlayerMoney(cid) >= amount * items[item].buyPrice) then
		local itemz, 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("Thanks for the money!", cid)
			doPlayerRemoveMoney(cid, amount * items[item].buyPrice)
		end
	else
		selfSay("Stfu noob, you don't have money.", cid)
	end
end

local onSell = function(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 you are.", cid)
	else
		selfSay("No item, no deal.", cid)
	end
end

function onCreatureAppear(cid)
end

function onCreatureDisappear(cid)
	if(isFocused(cid)) then
		selfSay("Hmph!")
		removeFocus(cid)
		if(isPlayer(cid)) then --Be sure he's online
			closeShopWindow(cid)
		end
	end
end

function onCreatureSay(cid, type, msg)
	if((msg == "hi") and not (isFocused(cid))) then
		selfSay("Welcome, ".. getCreatureName(cid) ..".", cid, true)
		selfSay("Do you want to see my {wares}?", cid)
		addFocus(cid)
	elseif((isFocused(cid)) and (msg == "wares" or msg == "trade")) then
		selfSay("Pretty nice, right?", cid)
		openShopWindow(cid, itemWindow, onBuy, onSell)
	elseif((isFocused(cid)) and (msg == "bye" or msg == "goodbye" or msg == "cya")) then
		selfSay("Goodbye!", cid, true)
		closeShopWindow(cid)
		removeFocus(cid)
	end
end

function onPlayerCloseChannel(cid)
	if(isFocused(cid)) then
		selfSay("Hmph!")
		closeShopWindow(cid)
		removeFocus(cid)
	end
end

function onPlayerEndTrade(cid)
	selfSay("It was a pleasure doing business with you.", cid)
end

function onThink()
	for i, focus in pairs(focuses) do
		if(not isCreature(focus)) then
			removeFocus(focus)
		else
			local distance = getDistanceTo(focus) or -1
			if((distance > 4) or (distance == -1)) then
				selfSay("Hmph!")
				closeShopWindow(focus)
				removeFocus(focus)
			end
		end
	end
	lookAtFocus()
end

Result: When looking item trough the shop:

00:40 You see a vial of oil.
It weighs 1.80 oz.
ItemID: [2006].

When buying item:

00:40 You see a vial of beer.
It weighs 1.80 oz.
ItemID: [2006].

I'll keep trying here...
 
Hah, I've done the following, create a chest near the NPCs that were suppose to sell the vial of oil, and put this script:

Lua:
function onUse(cid, item, frompos, item2, topos)

	if item.uid == 20044 then
		if exhaustion.check(cid, 20044) == false then
			doPlayerSendTextMessage(cid,25,"You have taken a vial of oil from the chest.")
			doPlayerAddItem(cid,2006,11)
			exhaustion.set(cid, 20044, 86400)
		else
			doPlayerSendTextMessage(cid,25,"You can only take one vial of oil per day from the chest.")
		end
	
	end
	return TRUE
end

brazilians and the art of ugly improvisation lol
 
Back
Top