local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
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
function greetCallback(cid)
talkState[talkUser] = 0
return true
end
function creatureSayCallback(cid, type, msg)
local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
if(not npcHandler:isFocused(cid)) then
return false
end
local trade = {
{id=7589, buy=80, sell=10, name="strong mana potion"},
{id=7591, buy=190, sell=10, name="great health potion"},
}
local items = {}
for _, item in ipairs(trade) do
items[item.id] = {storage = item.storage, item_id = item.id, buyPrice = item.buy, sellPrice = item.sell, subType = 0, realName = item.name}
end
local onBuy = function(cid, item, subType, amount, ignoreCap, inBackpacks)
if items[item].buyPrice ~= 0 then
doPlayerRemoveMoney(cid, amount * items[item].buyPrice)
for i = 1, amount do
doPlayerAddItem(cid, items[item].item_id, amount)
end
end
end
local onSell = function(cid, item, subType, amount, ignoreCap, inBackpacks)
if items[item].sellPrice ~= 0 then
doPlayerAddMoney(cid, items[item].sellPrice * amount)
doPlayerRemoveItem(cid, items[item].item_id, amount)
doNPCTalkALot(cid, 200, {"You sell "..amount.." "..items[item].realName.." for "..items[item].sellPrice * amount.." gold coins."})
end
end
if msgcontains(msg, 'trade') or msgcontains(msg, 'offer') then
if (getPlayerStorageValue(cid, 1002) ~= 1) then
selfSay("I won't trade with you", cid))
else
openShopWindow(cid, trade, onBuy, onSell)
selfSay("It's my offer.", cid)
end
end
return 1
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())