Coloque seu npchandler.lua e modules.lua em spoilers.
info
Grupo: Cavaleiro
Registrado: 01/04/13
Posts: 156
Reputação: +26
Gênero: Masculino
Char no Tibia: Yugami

Npchandler
modules
-- Advanced NPC System (Created by Jiddo),
Editado Outubro 15 por Tiagone
Tenta assim:
Modules:
-- Advanced NPC System (Created by Jiddo),
Editado Outubro 15 por XScupion
info
Grupo: Cavaleiro
Registrado: 01/04/13
Posts: 156
Reputação: +26
Gênero: Masculino
Char no Tibia: Yugami

Tenta assim:
Modules:
-- Advanced NPC System (Created by Jiddo),
-- Modified by Talaturen.-- Modified by Elf.if(Modules == nil) then-- Constants used to separate buying from selling.SHOPMODULE_SELL_ITEM = 1SHOPMODULE_BUY_ITEM = 2SHOPMODULE_BUY_ITEM_CONTAINER = 3-- Constants used for shop mode. Notice: addBuyableItemContainer is working on all modesSHOPMODULE_MODE_TALK = 1 -- Old system used before Tibia 8.2: sell/buy item nameSHOPMODULE_MODE_TRADE = 2 -- Trade window system introduced in Tibia 8.2SHOPMODULE_MODE_BOTH = 3 -- Both working at one time-- Used in shop modeSHOPMODULE_MODE = SHOPMODULE_MODE_BOTH-- Constants used for outfit giving modeOUTFITMODULE_FUNCTION_OLD = { doPlayerAddOutfit, canPlayerWearOutfit } -- lookType usageOUTFITMODULE_FUNCTION_NEW = { doPlayerAddOutfitId, canPlayerWearOutfitId } -- OutfitId usage-- Used in outfit moduleOUTFITMODULE_FUNCTION = OUTFITMODULE_FUNCTION_NEWif(OUTFITMODULE_FUNCTION[1] == nil or OUTFITMODULE_FUNCTION[2] == nil) thenOUTFITMODULE_FUNCTION = OUTFITMODULE_FUNCTION_OLDendModules = {parseableModules = {}}StdModule = {}-- These callback function must be called with parameters.npcHandler = npcHandler in the parameters table or they will not work correctly.-- Notice: The members of StdModule have not yet been tested. If you find any bugs, please report them to me.-- Usage:-- keywordHandler:addKeyword({'offer'}, StdModule.say, {npcHandler = npcHandler, text = 'I sell many powerful melee weapons.'})function StdModule.say(cid, message, keywords, parameters, node)local npcHandler = parameters.npcHandlerif(npcHandler == nil) thenerror('StdModule.say called without any npcHandler instance.')endlocal onlyFocus = (parameters.onlyFocus == nil or parameters.onlyFocus == true)if(not npcHandler:isFocused(cid) and onlyFocus) thenreturn falseendlocal parseInfo = {[TAG_PLAYERNAME] = getCreatureName(cid)}npcHandler:say(npcHandler:parseMessage(parameters.text or parameters.message, parseInfo), cid, parameters.publicize and true)if(parameters.reset == true) thennpcHandler:resetNpc()elseif(parameters.moveup ~= nil and type(parameters.moveup) == 'number') thennpcHandler.keywordHandler:moveUp(parameters.moveup)endreturn trueend--Usage:-- local node1 = keywordHandler:addKeyword({'promot'}, StdModule.say, {npcHandler = npcHandler, text = 'I can promote you for 20000 brozne coins. Do you want me to promote you?'})-- node1:addChildKeyword({'yes'}, StdModule.promotePlayer, {npcHandler = npcHandler, cost = 20000, promotion = 1, level = 20}, text = 'Congratulations! You are now promoted.')-- node1:addChildKeyword({'no'}, StdModule.say, {npcHandler = npcHandler, text = 'Alright then, come back when you are ready.'}, reset = true)function StdModule.promotePlayer(cid, message, keywords, parameters, node)local npcHandler = parameters.npcHandlerif(npcHandler == nil) thenerror('StdModule.promotePlayer called without any npcHandler instance.')endif(not npcHandler:isFocused(cid)) thenreturn falseendif(isPlayerPremiumCallback(cid) or not getBooleanFromString(getConfigValue('premiumForPromotion')) or not(parameters.premium)) thenif(getPlayerPromotionLevel(cid) >= parameters.promotion) thennpcHandler:say('You are already promoted!', cid)elseif(getPlayerLevel(cid) < parameters.level) thennpcHandler:say('I am sorry, but I can only promote you once you have reached level ' .. parameters.level .. '.', cid)elseif(not doPlayerRemoveMoney(cid, parameters.cost)) thennpcHandler:say('You do not have enough money!', cid)elsesetPlayerPromotionLevel(cid, parameters.promotion)npcHandler:say(parameters.text, cid)endelsenpcHandler:say("You need a premium account in order to get promoted.", cid)endnpcHandler:resetNpc()return trueendfunction StdModule.learnSpell(cid, message, keywords, parameters, node)local npcHandler = parameters.npcHandlerif(npcHandler == nil) thenerror('StdModule.learnSpell called without any npcHandler instance.')endif(not npcHandler:isFocused(cid)) thenreturn falseendif(isPlayerPremiumCallback(cid) or not(parameters.premium)) thenif(getPlayerLearnedInstantSpell(cid, parameters.spellName)) thennpcHandler:say('You already know this spell.', cid)elseif(getPlayerLevel(cid) < parameters.level) thennpcHandler:say('You need to obtain a level of ' .. parameters.level .. ' or higher to be able to learn ' .. parameters.spellName .. '.', cid)elseif(not parameters.vocation(cid)) thennpcHandler:say('This spell is not for your vocation', cid)elseif(not doPlayerRemoveMoney(cid, parameters.price)) thennpcHandler:say('You do not have enough money, this spell costs ' .. parameters.price .. ' gold coins.', cid)elsenpcHandler:say('You have learned ' .. parameters.spellName .. '.', cid)playerLearnInstantSpell(cid, parameters.spellName)endelsenpcHandler:say('You need a premium account in order to buy ' .. parameters.spellName .. '.', cid)endnpcHandler:resetNpc()return trueendfunction StdModule.bless(cid, message, keywords, parameters, node)local npcHandler = parameters.npcHandlerif(npcHandler == nil) thenerror('StdModule.bless called without any npcHandler instance.')endif(not npcHandler:isFocused(cid)) thenreturn falseendif(isPlayerPremiumCallback(cid) or not getBooleanFromString(getConfigValue('blessingsOnlyPremium')) or not parameters.premium) thenlocal price = parameters.baseCostif(getPlayerLevel(cid) > parameters.startLevel) thenprice = (price + ((math.min(parameters.endLevel, getPlayerLevel(cid)) - parameters.startLevel) * parameters.levelCost))endif(getPlayerBlessing(cid, parameters.number)) thennpcHandler:say("Gods have already blessed you with this blessing!", cid)elseif(not doPlayerRemoveMoney(cid, price)) thennpcHandler:say("You don't have enough money for blessing.", cid)elsenpcHandler:say("You have been blessed by one of the five gods!", cid)doPlayerAddBlessing(cid, parameters.number)endelsenpcHandler:say('You need a premium account in order to be blessed.', cid)endnpcHandler:resetNpc()return trueendfunction StdModule.travel(cid, message, keywords, parameters, node)local npcHandler = parameters.npcHandlerif(npcHandler == nil) thenerror('StdModule.travel called without any npcHandler instance.')endif(not npcHandler:isFocused(cid)) thenreturn falseendlocal storage, pzLocked = parameters.storageValue or (EMPTY_STORAGE + 1), parameters.allowLocked or falseif(parameters.premium and not isPlayerPremiumCallback(cid)) thennpcHandler:say('I can only allow premium players to travel with me.', cid)elseif(parameters.level ~= nil and getPlayerLevel(cid) < parameters.level) thennpcHandler:say('You must reach level ' .. parameters.level .. ' before I can let you go there.', cid)elseif(parameters.storageId ~= nil and getPlayerStorageValue(cid, parameters.storageId) < storage) thennpcHandler:say(parameters.storageInfo or 'You may not travel there!', cid)elseif(not pzLocked and isPlayerPzLocked(cid)) thennpcHandler:say('Get out of there with this blood!', cid)elseif(not doPlayerRemoveMoney(cid, parameters.cost)) thennpcHandler:say('You do not have enough money.', cid)elsenpcHandler:say('It was a pleasure doing business with you.', cid)npcHandler:releaseFocus(cid)doTeleportThing(cid, parameters.destination, false)doSendMagicEffect(parameters.destination, CONST_ME_TELEPORT)endnpcHandler:resetNpc()return trueendFocusModule = {npcHandler = nil}-- Creates a new instance of FocusModule without an associated NpcHandler.function FocusModule:new()local obj = {}setmetatable(obj, self)self.__index = selfreturn objend-- Inits the module and associates handler to it.function FocusModule:init(handler)self.npcHandler = handlerfor i, word in pairs(FOCUS_GREETWORDS) dolocal obj = {}table.insert(obj, word)obj.callback = FOCUS_GREETWORDS.callback or FocusModule.messageMatcherhandler.keywordHandler:addKeyword(obj, FocusModule.onGreet, {module = self})endfor i, word in pairs(FOCUS_FAREWELLWORDS) dolocal obj = {}table.insert(obj, word)obj.callback = FOCUS_FAREWELLWORDS.callback or FocusModule.messageMatcherhandler.keywordHandler:addKeyword(obj, FocusModule.onFarewell, {module = self})endend-- Greeting callback function.function FocusModule.onGreet(cid, message, keywords, parameters)parameters.module.npcHandler:onGreet(cid)return trueend-- UnGreeting callback function.function FocusModule.onFarewell(cid, message, keywords, parameters)if(not parameters.module.npcHandler:isFocused(cid)) thenreturn falseendparameters.module.npcHandler:onFarewell(cid)return trueend-- Custom message matching callback function for greeting messages.function FocusModule.messageMatcher(keywords, message)local spectators = getSpectators(getCreaturePosition(getNpcId()), 7, 7)for i, word in pairs(keywords) doif(type(word) == 'string') thenif(string.find(message, word) and not string.find(message, '[%w+]' .. word) and not string.find(message, word .. '[%w+]')) thenif(string.find(message, getCreatureName(getNpcId()))) thenreturn trueendfor i, uid in ipairs(spectators) doif(string.find(message, getCreatureName(uid))) thenreturn falseendendreturn trueendendendreturn falseendKeywordModule = {npcHandler = nil}-- Add it to the parseable module list.Modules.parseableModules['module_keywords'] = KeywordModulefunction KeywordModule:new()local obj = {}setmetatable(obj, self)self.__index = selfreturn objendfunction KeywordModule:init(handler)self.npcHandler = handlerend-- Parses all known parameters.function KeywordModule:parseParameters()local ret = NpcSystem.getParameter('keywords')if(ret ~= nil) thenself:parseKeywords(ret)endendfunction KeywordModule:parseKeywords(data)local n = 1for keys in string.gmatch(data, '[^;]+') dolocal i = 1local keywords = {}for temp in string.gmatch(keys, '[^,]+') dotable.insert(keywords, temp)i = i + 1endif(i ~= 1) thenlocal reply = NpcSystem.getParameter('keyword_reply' .. n)if(reply ~= nil) thenself:addKeyword(keywords, reply)elseprint('[Warning] NpcSystem:', 'Parameter \'' .. 'keyword_reply' .. n .. '\' missing. Skipping...')endelseprint('[Warning] NpcSystem:', 'No keywords found for keyword set #' .. n .. '. Skipping...')endn = n + 1endendfunction KeywordModule:addKeyword(keywords, reply)self.npcHandler.keywordHandler:addKeyword(keywords, StdModule.say, {npcHandler = self.npcHandler, onlyFocus = true, text = reply, reset = true})endTravelModule = {npcHandler = nil,destinations = nil,yesNode = nil,noNode = nil,}-- Add it to the parseable module list.Modules.parseableModules['module_travel'] = TravelModulefunction TravelModule:new()local obj = {}setmetatable(obj, self)self.__index = selfreturn objendfunction TravelModule:init(handler)self.npcHandler = handlerself.yesNode = KeywordNode:new(SHOP_YESWORD, TravelModule.onConfirm, {module = self})self.noNode = KeywordNode:new(SHOP_NOWORD, TravelModule.onDecline, {module = self})self.destinations = {}end-- Parses all known parameters.function TravelModule:parseParameters()local ret = NpcSystem.getParameter('travel_destinations')if(ret ~= nil) thenself:parseDestinations(ret)for _, word in ipairs({'destination', 'list', 'where', 'travel'}) doself.npcHandler.keywordHandler:addKeyword({word}, TravelModule.listDestinations, {module = self})endendendfunction TravelModule:parseDestinations(data)for destination in string.gmatch(data, '[^;]+') dolocal i, name, pos, cost, premium, level, storage = 1, nil, {x = nil, y = nil, z = nil}, nil, falsefor tmp in string.gmatch(destination, '[^,]+') doif(i == 1) thenname = tmpelseif(i == 2) thenpos.x = tonumber(tmp)elseif(i == 3) thenpos.y = tonumber(tmp)elseif(i == 4) thenpos.z = tonumber(tmp)elseif(i == 5) thencost = tonumber(tmp)elseif(i == 6) thenpremium = getBooleanFromString(tmp)elseprint('[Warning] NpcSystem:', 'Unknown parameter found in travel destination parameter.', tmp, destination)endi = i + 1endif(name ~= nil and pos.x ~= nil and pos.y ~= nil and pos.z ~= nil and cost ~= nil) thenself:addDestination(name, pos, cost, premium)elseprint('[Warning] NpcSystem:', 'Parameter(s) missing for travel destination:', name, pos, cost, premium)endendendfunction TravelModule:addDestination(name, position, price, premium)table.insert(self.destinations, name)local parameters = {cost = price,destination = position,premium = premium,module = self}local keywords, bringwords = {}, {}table.insert(keywords, name)table.insert(bringwords, 'bring me to ' .. name)self.npcHandler.keywordHandler:addKeyword(bringwords, TravelModule.bring, parameters)local node = self.npcHandler.keywordHandler:addKeyword(keywords, TravelModule.travel, parameters)node:addChildKeywordNode(self.yesNode)node:addChildKeywordNode(self.noNode)endfunction TravelModule.travel(cid, message, keywords, parameters, node)local module = parameters.moduleif(not module.npcHandler:isFocused(cid)) thenreturn falseendmodule.npcHandler:say('Do you want to travel to ' .. keywords[1] .. ' for ' .. parameters.cost .. ' gold coins?', cid)return trueendfunction TravelModule.onConfirm(cid, message, keywords, parameters, node)local module = parameters.moduleif(not module.npcHandler:isFocused(cid)) thenreturn falseendlocal parent = node:getParent():getParameters()if(isPlayerPremiumCallback(cid) or not parent.premium) thenif(not isPlayerPzLocked(cid)) thenif(doPlayerRemoveMoney(cid, parent.cost)) thenmodule.npcHandler:say('It was a pleasure doing business with you.', cid)module.npcHandler:releaseFocus(cid)doTeleportThing(cid, parent.destination, true)doSendMagicEffect(parent.destination, CONST_ME_TELEPORT)elsemodule.npcHandler:say('You do not have enough money.', cid)endelsemodule.npcHandler:say('Get out of there with this blood!', cid)endelsemodulenpcHandler:say('I can only allow premium players to travel there.', cid)endmodule.npcHandler:resetNpc()return trueend-- onDecline keyword callback function. Generally called when the player sais 'no' after wanting to buy an item.function TravelModule.onDecline(cid, message, keywords, parameters, node)local module = parameters.moduleif(not module.npcHandler:isFocused(cid)) thenreturn falseendmodule.npcHandler:say(module.npcHandler:parseMessage(module.npcHandler:getMessage(MESSAGE_DECLINE), {[TAG_PLAYERNAME] = getCreatureName(cid)}), cid)module.npcHandler:resetNpc()return trueendfunction TravelModule.bring(cid, message, keywords, parameters, node)local module = parameters.moduleif(not module.npcHandler:isFocused(cid)) thenreturn falseendif((isPlayerPremiumCallback(cid) or not parameters.premium) and not isPlayerPzLocked(cid) and doPlayerRemoveMoney(cid, parameters.cost)) thenmodule.npcHandler:say('Sure!', cid)module.npcHandler:releaseFocus(cid)doTeleportThing(cid, parameters.destination, false)doSendMagicEffect(parameters.destination, CONST_ME_TELEPORT)endmodule.npcHandler:releaseFocus(cid)return trueendfunction TravelModule.listDestinations(cid, message, keywords, parameters, node)local module = parameters.moduleif(not module.npcHandler:isFocused(cid)) thenreturn falseendlocal msg = nilfor _, destination in ipairs(module.destinations) doif(msg ~= nil) thenmsg = msg .. ", "elsemsg = ""endmsg = msg .. "{" .. destination .. "}"endmodule.npcHandler:say(msg .. ".", cid)module.npcHandler:resetNpc()return trueendOutfitModule = {npcHandler = nil,outfits = nil,yesNode = nil,noNode = nil,}-- Add it to the parseable module list.Modules.parseableModules['module_outfit'] = OutfitModulefunction OutfitModule:new()if(OUTFITMODULE_FUNCTION[1] == nil or OUTFITMODULE_FUNCTION[2] == nil) thenreturn nilendlocal obj = {}setmetatable(obj, self)self.__index = selfreturn objendfunction OutfitModule:init(handler)self.npcHandler = handlerself.yesNode = KeywordNode:new(SHOP_YESWORD, OutfitModule.onConfirm, {module = self})self.noNode = KeywordNode:new(SHOP_NOWORD, OutfitModule.onDecline, {module = self})self.outfits = {}end-- Parses all known parameters.function OutfitModule:parseParameters()local ret = NpcSystem.getParameter('outfits')if(ret ~= nil) thenself:parseKeywords(ret)for _, word in ipairs({'outfits', 'addons'}) doself.npcHandler.keywordHandler:addKeyword({word}, OutfitModule.listOutfits, {module = self})endendendfunction OutfitModule:parseKeywords(data)local n = 1for outfit in string.gmatch(data, '[^;]+') dolocal i, keywords = 1, {}for tmp in string.gmatch(outfit, '[^,]+') dotable.insert(keywords, tmp)i = i + 1endif(i > 0) thenlocal ret = NpcSystem.getParameter('outfit' .. n)if(ret ~= nil) thenself:parseList(keywords, ret)elseprint('[Warning] NpcSystem:', 'Missing \'outfit' .. n .. '\' parameter, skipping...')endelseprint('[Warning] NpcSystem:', 'No keywords found for outfit set #' .. n .. ', skipping...')endn = n + 1endendfunction OutfitModule:parseList(keywords, data)local outfit, items = nil, {}for list in string.gmatch(data, '[^;]+') dolocal a, b, c, d, e = nil, nil, nil, nil, 1for tmp in string.gmatch(list, '[^,]+') doif(e == 1) thena = tmpelseif(e == 2) thenb = tmpelseif(e == 3) thenc = tmpelseif(e == 4) thend = tmpelseprint('[Warning] NpcSystem:', 'Unknown parameter found in outfit list while parsing ' .. (outfit == nil and 'outfit' or 'item') .. '.', tmp, list)ende = e + 1endif(outfit == nil) thenoutfit = {tonumber(a), tonumber(b), getBooleanFromString©, d}elseif(a ~= nil) thenlocal tmp = tonumber(a)if((tmp ~= nil or tostring(a) == "money") and b ~= nil and c ~= nil) thena = tmp or 20000tmp = tonumber(d)if(tmp == nil) thentmp = -1enditems[a] = {b, tmp, c}elseprint('[Warning] NpcSystem:', 'Missing parameter(s) for outfit items.', b, c, d)endelseprint('[Warning] NpcSystem:', 'Missing base parameter for outfit items.', a)endendif(type(outfit) == 'table') thenlocal tmp = truefor i = 1, 2 doif(outfit == nil) thentmp = falsebreakendendif(tmp and table.maxn(items) > 0) thenself:addOutfit(keywords, outfit, items)elseprint('[Warning] NpcSystem:', 'Invalid outfit, addon or empty items pool.', data)endendendfunction OutfitModule:addOutfit(keywords, outfit, items)table.insert(self.outfits, keywords[1])local parameters = {outfit = outfit[1],addon = outfit[2],premium = outfit[3],gender = nil,items = items,module = self}if(outfit[4] ~= nil) thenlocal tmp = string.lower(tostring(outfit[5]))if(tmp == 'male' or tmp == '1') thenparameters.gender = 1elseif(tmp == 'female' or tmp == '0') thenparameters.gender = 0endendfor i, name in pairs(keywords) dolocal words = {}table.insert(words, name)local node = self.npcHandler.keywordHandler:addKeyword(words, OutfitModule.obtain, parameters)node:addChildKeywordNode(self.yesNode)node:addChildKeywordNode(self.noNode)endendfunction OutfitModule.obtain(cid, message, keywords, parameters, node)local module = parameters.moduleif(not module.npcHandler:isFocused(cid)) thenreturn falseendlocal i, items, size = 0, nil, table.maxn(parameters.items)for k, v in pairs(parameters.items) doif(v[1] ~= "storageset") theni = i + 1if(items ~= nil) thenif(i == size) thenitems = items .. " and "elseitems = items .. ", "endelseitems = ""endif(tonumber(v[1]) ~= nil and tonumber(v[1]) > 1) thenitems = items .. v[1] .. " "enditems = items .. v[3]endendmodule.npcHandler:say('Do you want ' .. keywords[1] .. ' ' .. (addon == 0 and "outfit" or "addon") .. ' for ' .. items .. '?', cid)return trueendfunction OutfitModule.onConfirm(cid, message, keywords, parameters, node)local module = parameters.moduleif(not module.npcHandler:isFocused(cid)) thenreturn falseendlocal parent = node:getParent():getParameters()if(isPlayerPremiumCallback(cid) or not parent.premium) thenif(not OUTFITMODULE_FUNCTION[2](cid, parent.outfit, parent.addon)) thenif(parent.addon == 0 or OUTFITMODULE_FUNCTION[2](cid, parent.outfit)) thenif(parent.gender == nil or parent.gender == getPlayerSex(cid)) thenlocal found = truefor k, v in pairs(parent.items) dolocal tmp = tonumber(v[1])if(tmp == nil) thenif(v[1] == "storagecheck") thenif(getCreatureStorage(cid, k) < v[2]) thenfound = falseendelseif(v[1] == "outfitid") thenif(not canPlayerWearOutfitId(cid, k, v[2])) thenfound = falseendelseif(v[1] == "outfit") thenif(not canPlayerWearOutfit(cid, k, v[2])) thenfound = falseendelsefound = falseendelseif(k == 20000) thenif(getPlayerMoney(cid) < tmp) thenfound = falseendelseif(getPlayerItemCount(cid, k, v[2]) < tmp) thenfound = falseendif(not found) thenbreakendendif(found) thenfor k, v in pairs(parent.items) doif(tonumber(v[1]) ~= nil) thenif(k == 20000) thendoPlayerRemoveMoney(cid, v[1])elsedoPlayerRemoveItem(cid, k, v[1], v[2])endelseif(v[1] == "storageset") thendoCreatureSetStorage(cid, k, v[2])endendmodule.npcHandler:say('It was a pleasure to dress you.', cid)OUTFITMODULE_FUNCTION[1](cid, parent.outfit, parent.addon)doPlayerSetStorageValue(cid, parent.storageId, storage)elsemodule.npcHandler:say('You don\'t have these items!', cid)endelsemodule.npcHandler:say('Sorry, this ' .. (parent.addon == 0 and 'outfit' or 'addon') .. ' is not for your gender.', cid)endelsemodule.npcHandler:say('I will not dress you with addon of outfit you cannot wear!', cid)endelsemodule.npcHandler:say('You alrady have this ' .. (parent.addon == 0 and 'outfit' or 'addon') .. '!', cid)endelsemodule.npcHandler:say('Sorry, I dress only premium players.', cid)endmodule.npcHandler:resetNpc()return trueend-- onDecline keyword callback function. Generally called when the player sais 'no' after wanting to buy an item.function OutfitModule.onDecline(cid, message, keywords, parameters, node)local module = parameters.moduleif(not module.npcHandler:isFocused(cid)) thenreturn falseendmodule.npcHandler:say(module.npcHandler:parseMessage(module.npcHandler:getMessage(MESSAGE_DECLINE), {[TAG_PLAYERNAME] = getCreatureName(cid)}), cid)module.npcHandler:resetNpc()return trueendfunction OutfitModule.listOutfits(cid, message, keywords, parameters, node)local module = parameters.moduleif(not module.npcHandler:isFocused(cid)) thenreturn falseendlocal msg, size = nil, table.maxn(module.outfits)if(size > 0) thenfor i, outfit in ipairs(module.outfits) doif(msg ~= nil) thenif(i == size) thenmsg = msg .. " and "elsemsg = msg .. ", "endelsemsg = "I can dress you into "endmsg = msg .. "{" .. outfit .. "}"endelsemsg = "Sorry, I have nothing to offer right now."endmodule.npcHandler:say(msg .. ".", cid)module.npcHandler:resetNpc()return trueendShopModule = {npcHandler = nil,yesNode = nil,noNode = nil,noText = '',maxCount = 100,amount = 0}-- Add it to the parseable module list.Modules.parseableModules['module_shop'] = ShopModule-- Creates a new instance of ShopModulefunction ShopModule:new()local obj = {}setmetatable(obj, self)self.__index = selfreturn objend-- Parses all known parameters.function ShopModule:parseParameters()local ret = NpcSystem.getParameter('shop_buyable')if(ret ~= nil) thenself:parseBuyable(ret)endlocal ret = NpcSystem.getParameter('shop_sellable')if(ret ~= nil) thenself:parseSellable(ret)endlocal ret = NpcSystem.getParameter('shop_buyable_containers')if(ret ~= nil) thenself:parseBuyableContainers(ret)endend-- Parse a string contaning a set of buyable items.function ShopModule:parseBuyable(data)for item in string.gmatch(data, '[^;]+') dolocal i, name, itemid, cost, subType, realName = 1, nil, nil, nil, nil, nilfor temp in string.gmatch(item, '[^,]+') doif(i == 1) thenname = tempelseif(i == 2) thenitemid = tonumber(temp)elseif(i == 3) thencost = tonumber(temp)elseif(i == 4) thensubType = tonumber(temp)elseif(i == 5) thenrealName = tempelseprint('[Warning] NpcSystem:', 'Unknown parameter found in buyable items parameter.', temp, item)endi = i + 1endif(SHOPMODULE_MODE == SHOPMODULE_MODE_TRADE) thenif(itemid ~= nil and cost ~= nil) thenif((isItemRune(itemid) or isItemFluidContainer(itemid)) and subType == nil) thenprint('[Warning] NpcSystem:', 'SubType missing for parameter item:', item)elseself:addBuyableItem(nil, itemid, cost, subType, realName)endelseprint('[Warning] NpcSystem:', 'Parameter(s) missing for item:', itemid, cost)endelseif(name ~= nil and itemid ~= nil and cost ~= nil) thenif((isItemRune(itemid) or isItemFluidContainer(itemid)) and subType == nil) thenprint('[Warning] NpcSystem:', 'SubType missing for parameter item:', item)elselocal names = {}table.insert(names, name)self:addBuyableItem(names, itemid, cost, subType, realName)endelseprint('[Warning] NpcSystem:', 'Parameter(s) missing for item:', name, itemid, cost)endendend-- Parse a string contaning a set of sellable items.function ShopModule:parseSellable(data)for item in string.gmatch(data, '[^;]+') dolocal i, name, itemid, cost, realName = 1, nil, nil, nil, nilfor temp in string.gmatch(item, '[^,]+') doif(i == 1) thenname = tempelseif(i == 2) thenitemid = tonumber(temp)elseif(i == 3) thencost = tonumber(temp)elseif(i == 4) thenrealName = tempelseprint('[Warning] NpcSystem:', 'Unknown parameter found in sellable items parameter.', temp, item)endi = i + 1endif(SHOPMODULE_MODE == SHOPMODULE_MODE_TRADE) thenif(itemid ~= nil and cost ~= nil) thenself:addSellableItem(nil, itemid, cost, realName)elseprint('[Warning] NpcSystem:', 'Parameter(s) missing for item:', itemid, cost)endelseif(name ~= nil and itemid ~= nil and cost ~= nil) thenlocal names = {}table.insert(names, name)self:addSellableItem(names, itemid, cost, realName)elseprint('[Warning] NpcSystem:', 'Parameter(s) missing for item:', name, itemid, cost)endendend-- Parse a string contaning a set of buyable items.function ShopModule:parseBuyableContainers(data)for item in string.gmatch(data, '[^;]+') dolocal i, name, container, itemid, cost, subType, realName = 1, nil, nil, nil, nil, nil, nilfor temp in string.gmatch(item, '[^,]+') doif(i == 1) thenname = tempelseif(i == 2) thenitemid = tonumber(temp)elseif(i == 3) thenitemid = tonumber(temp)elseif(i == 4) thencost = tonumber(temp)elseif(i == 5) thensubType = tonumber(temp)elseif(i == 6) thenrealName = tempelseprint('[Warning] NpcSystem:', 'Unknown parameter found in buyable items parameter.', temp, item)endi = i + 1endif(name ~= nil and container ~= nil and itemid ~= nil and cost ~= nil) thenif((isItemRune(itemid) or isItemFluidContainer(itemid)) and subType == nil) thenprint('[Warning] NpcSystem:', 'SubType missing for parameter item:', item)elselocal names = {}table.insert(names, name)self:addBuyableItemContainer(names, container, itemid, cost, subType, realName)endelseprint('[Warning] NpcSystem:', 'Parameter(s) missing for item:', name, container, itemid, cost)endendend-- Initializes the module and associates handler to it.function ShopModule:init(handler)self.npcHandler = handlerself.yesNode = KeywordNode:new(SHOP_YESWORD, ShopModule.onConfirm, {module = self})self.noNode = KeywordNode:new(SHOP_NOWORD, ShopModule.onDecline, {module = self})self.noText = handler:getMessage(MESSAGE_DECLINE)if(SHOPMODULE_MODE ~= SHOPMODULE_MODE_TALK) thenfor i, word in pairs(SHOP_TRADEREQUEST) dolocal obj = {}table.insert(obj, word)obj.callback = SHOP_TRADEREQUEST.callback or ShopModule.messageMatcherhandler.keywordHandler:addKeyword(obj, ShopModule.requestTrade, {module = self})endendend-- Custom message matching callback function for requesting trade messages.function ShopModule.messageMatcher(keywords, message)for i, word in pairs(keywords) doif(type(word) == 'string' and string.find(message, word) and not string.find(message, '[%w+]' .. word) and not string.find(message, word .. '[%w+]')) thenreturn trueendendreturn falseend-- Resets the module-specific variables.function ShopModule:reset()self.amount = 0end-- Function used to match a number value from a string.function ShopModule:getCount(message)local ret, b, e = 1, string.find(message, PATTERN_COUNT)if(b ~= nil and e ~= nil) thenret = tonumber(string.sub(message, b, e))endreturn math.max(1, math.min(self.maxCount, ret))end-- 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) thenlocal item = {id = itemid,buy = cost,sell = -1,subType = subType or 1,name = realName or getItemNameById(itemid)}for i, shopItem in ipairs(self.npcHandler.shopItems) doif(shopItem.id == item.id and shopItem.subType == item.subType) thenif(item.sell ~= shopItem.sell) thenitem.sell = shopItem.sellendself.npcHandler.shopItems = itemitem = nilbreakendendif(item ~= nil) thentable.insert(self.npcHandler.shopItems, item)endendif(names ~= nil and SHOPMODULE_MODE ~= SHOPMODULE_MODE_TRADE) thenlocal 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) dolocal 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)endendend-- Adds a new buyable container of items.-- names = A table containing one or more strings of alternative names to this item.-- container = Backpack, bag or any other itemid of container where bought items will be stored-- 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:addBuyableItemContainer(names, container, itemid, cost, subType, realName)if(names ~= nil) thenlocal parameters = {container = container,itemid = itemid,cost = cost,eventType = SHOPMODULE_BUY_ITEM_CONTAINER,module = self,realName = realName or getItemNameById(itemid),subType = subType or 1}for i, name in pairs(names) dolocal 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)endendend-- Adds a new sellable item.-- names = A table containing one or more strings of alternative names to this item. Used only by old buy/sell system.-- itemid = The itemid of the sellable item-- cost = The price of one single item-- 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:addSellableItem(names, itemid, cost, realName)if(SHOPMODULE_MODE ~= SHOPMODULE_MODE_TALK) thenlocal item = {id = itemid,buy = -1,sell = cost,subType = 1,name = realName or getItemNameById(itemid)}for i, shopItem in ipairs(self.npcHandler.shopItems) doif(shopItem.id == item.id and shopItem.subType == item.subType) thenif(item.buy ~= shopItem.buy) thenitem.buy = shopItem.buyendself.npcHandler.shopItems = itemitem = nilbreakendendif(item ~= nil) thentable.insert(self.npcHandler.shopItems, item)endendif(names ~= nil and SHOPMODULE_MODE ~= SHOPMODULE_MODE_TRADE) thenlocal parameters = {itemid = itemid,cost = cost,eventType = SHOPMODULE_SELL_ITEM,module = self,realName = realName or getItemNameById(itemid)}for i, name in pairs(names) dolocal keywords = {}table.insert(keywords, 'sell')table.insert(keywords, name)local node = self.npcHandler.keywordHandler:addKeyword(keywords, ShopModule.tradeItem, parameters)node:addChildKeywordNode(self.yesNode)node:addChildKeywordNode(self.noNode)endendend-- onModuleReset callback function. Calls ShopModule:reset()function ShopModule:callbackOnModuleReset()self:reset()return trueend-- Callback onBuy() function. If you wish, you can change certain Npc to use your onBuy().function ShopModule:callbackOnBuy(cid, itemid, subType, amount, ignoreCap, inBackpacks)local shopItem = nilfor _, item in ipairs(self.npcHandler.shopItems) doif(item.id == itemid and item.subType == subType) thenshopItem = itembreakendendif(shopItem == nil) thenprint('[Warning - ' .. getCreatureName(getNpcId()) .. '] NpcSystem:', 'ShopModule.onBuy - Item not found on shopItems list')return falseendif(shopItem.buy == -1) thenerror("[shopModule.onSell]", "Attempt to purchase an item which only sellable")return falseendlocal backpack, totalCost = 1988, amount * shopItem.buyif(inBackpacks) thentotalCost = totalCost + (math.max(1, math.floor(amount / getContainerCapById(backpack))) * 20)endlocal parseInfo = {[TAG_PLAYERNAME] = getPlayerName(cid),[TAG_ITEMCOUNT] = amount,[TAG_TOTALCOST] = totalCost,[TAG_ITEMNAME] = shopItem.name}if(getPlayerMoney(cid) < totalCost) thenlocal msg = self.npcHandler:getMessage(MESSAGE_NEEDMONEY)doPlayerSendCancel(cid, self.npcHandler:parseMessage(msg, parseInfo))return falseendlocal subType = shopItem.subType or 1local a, b = doNpcSellItem(cid, itemid, amount, subType, ignoreCap, inBackpacks, backpack)if(a < amount) thenlocal msgId = MESSAGE_NEEDMORESPACEif(a == 0) thenmsgId = MESSAGE_NEEDSPACEendlocal msg = self.npcHandler:getMessage(msgId)parseInfo[TAG_ITEMCOUNT] = adoPlayerSendCancel(cid, self.npcHandler:parseMessage(msg, parseInfo))if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) thenself.npcHandler.talkStart[cid] = os.time()elseself.npcHandler.talkStart = os.time()endif(a > 0) thendoPlayerRemoveMoney(cid, ((a * shopItem.buy) + (b * 20)))return trueendreturn falseendlocal msg = self.npcHandler:getMessage(MESSAGE_BOUGHT)doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, self.npcHandler:parseMessage(msg, parseInfo))doPlayerRemoveMoney(cid, totalCost)if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) thenself.npcHandler.talkStart[cid] = os.time()elseself.npcHandler.talkStart = os.time()endreturn trueend-- Callback onSell() function. If you wish, you can change certain Npc to use your onSell().function ShopModule:callbackOnSell(cid, itemid, subType, amount, ignoreCap, inBackpacks)local shopItem = nilfor _, item in ipairs(self.npcHandler.shopItems) doif(item.id == itemid and item.subType == subType) thenshopItem = itembreakendendif(shopItem == nil) thenerror("[shopModule.onBuy]", "Item not found on shopItems list")return falseendif(shopItem.sell == -1) thenerror("[shopModule.onSell]", "Attempt to sell an item which is only buyable")return falseendlocal parseInfo = {[TAG_PLAYERNAME] = getPlayerName(cid),[TAG_ITEMCOUNT] = amount,[TAG_TOTALCOST] = amount * shopItem.sell,[TAG_ITEMNAME] = shopItem.name}if(subType < 1 or getItemInfo(itemid).stackable) thensubType = -1endif(doPlayerRemoveItem(cid, itemid, amount, subType)) thenlocal msg = self.npcHandler:getMessage(MESSAGE_SOLD)doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, self.npcHandler:parseMessage(msg, parseInfo))doPlayerAddMoney(cid, amount * shopItem.sell)if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) thenself.npcHandler.talkStart[cid] = os.time()elseself.npcHandler.talkStart = os.time()endreturn trueendlocal msg = self.npcHandler:getMessage(MESSAGE_NEEDITEM)doPlayerSendCancel(cid, self.npcHandler:parseMessage(msg, parseInfo))if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) thenself.npcHandler.talkStart[cid] = os.time()elseself.npcHandler.talkStart = os.time()endreturn falseend-- Callback for requesting a trade window with the NPC.function ShopModule.requestTrade(cid, message, keywords, parameters, node)local module = parameters.moduleif(not module.npcHandler:isFocused(cid)) thenreturn falseendif(table.maxn(module.npcHandler.shopItems) == 0) thenlocal parseInfo = { [TAG_PLAYERNAME] = getPlayerName(cid) }local msg = module.npcHandler:parseMessage(module.npcHandler:getMessage(MESSAGE_NOSHOP), parseInfo)module.npcHandler:say(msg, cid)return trueendlocal parseInfo = { [TAG_PLAYERNAME] = getPlayerName(cid) }local msg = module.npcHandler:parseMessage(module.npcHandler:getMessage(MESSAGE_SENDTRADE), parseInfo)openShopWindow(cid, module.npcHandler.shopItems,function(cid, itemid, subType, amount, ignoreCap, inBackpacks)module.npcHandler:onBuy(cid, itemid, subType, amount, ignoreCap, inBackpacks)end,function(cid, itemid, subType, amount, ignoreCap, inBackpacks)module.npcHandler:onSell(cid, itemid, subType, amount, ignoreCap, inBackpacks)end)module.npcHandler:say(msg, cid)return trueend-- onConfirm keyword callback function. Sells/buys the actual item.function ShopModule.onConfirm(cid, message, keywords, parameters, node)local module = parameters.moduleif(not module.npcHandler:isFocused(cid)) thenreturn falseendlocal parentParameters = node:getParent():getParameters()local parseInfo = {[TAG_PLAYERNAME] = getPlayerName(cid),[TAG_ITEMCOUNT] = module.amount,[TAG_TOTALCOST] = parentParameters.cost * module.amount,[TAG_ITEMNAME] = parentParameters.realName}if(parentParameters.eventType == SHOPMODULE_SELL_ITEM) thenlocal ret = doPlayerSellItem(cid, parentParameters.itemid, module.amount, parentParameters.cost * module.amount)if(ret) thenlocal msg = module.npcHandler:getMessage(MESSAGE_ONSELL)msg = module.npcHandler:parseMessage(msg, parseInfo)module.npcHandler:say(msg, cid)elselocal msg = module.npcHandler:getMessage(MESSAGE_MISSINGITEM)msg = module.npcHandler:parseMessage(msg, parseInfo)module.npcHandler:say(msg, cid)endelseif(parentParameters.eventType == SHOPMODULE_BUY_ITEM) thenlocal ret = doPlayerBuyItem(cid, parentParameters.itemid, module.amount, parentParameters.cost * module.amount, parentParameters.subType)if(ret) thenif parentParameters.itemid == ITEM_PARCEL thendoPlayerBuyItem(cid, ITEM_LABEL, module.amount, 0, parentParameters.subType)endlocal msg = module.npcHandler:getMessage(MESSAGE_ONBUY)msg = module.npcHandler:parseMessage(msg, parseInfo)module.npcHandler:say(msg, cid)elselocal msg = module.npcHandler:getMessage(MESSAGE_MISSINGMONEY)msg = module.npcHandler:parseMessage(msg, parseInfo)module.npcHandler:say(msg, cid)endelseif(parentParameters.eventType == SHOPMODULE_BUY_ITEM_CONTAINER) thenlocal ret = doPlayerBuyItemContainer(cid, parentParameters.container, parentParameters.itemid, module.amount, parentParameters.cost * module.amount, parentParameters.subType)if(ret) thenlocal msg = module.npcHandler:getMessage(MESSAGE_ONBUY)msg = module.npcHandler:parseMessage(msg, parseInfo)module.npcHandler:say(msg, cid)elselocal msg = module.npcHandler:getMessage(MESSAGE_MISSINGMONEY)msg = module.npcHandler:parseMessage(msg, parseInfo)module.npcHandler:say(msg, cid)endendmodule.npcHandler:resetNpc()return trueend-- onDecliune keyword callback function. Generally called when the player sais 'no' after wanting to buy an item.function ShopModule.onDecline(cid, message, keywords, parameters, node)local module = parameters.moduleif(not module.npcHandler:isFocused(cid)) thenreturn falseendlocal parentParameters = node:getParent():getParameters()local parseInfo = {[TAG_PLAYERNAME] = getPlayerName(cid),[TAG_ITEMCOUNT] = module.amount,[TAG_TOTALCOST] = parentParameters.cost * module.amount,[TAG_ITEMNAME] = parentParameters.realName}local msg = module.npcHandler:parseMessage(module.noText, parseInfo)module.npcHandler:say(msg, cid)module.npcHandler:resetNpc()return trueend-- tradeItem callback function. Makes the npc say the message defined by MESSAGE_BUY or MESSAGE_SELLfunction ShopModule.tradeItem(cid, message, keywords, parameters, node)local module = parameters.moduleif(not module.npcHandler:isFocused(cid)) thenreturn falseendlocal count = module:getCount(message)module.amount = countlocal parseInfo = {[TAG_PLAYERNAME] = getPlayerName(cid),[TAG_ITEMCOUNT] = module.amount,[TAG_TOTALCOST] = parameters.cost * module.amount,[TAG_ITEMNAME] = parameters.realName}if(parameters.eventType == SHOPMODULE_SELL_ITEM) thenlocal msg = module.npcHandler:getMessage(MESSAGE_SELL)msg = module.npcHandler:parseMessage(msg, parseInfo)module.npcHandler:say(msg, cid)elseif(parameters.eventType == SHOPMODULE_BUY_ITEM) thenlocal msg = module.npcHandler:getMessage(MESSAGE_BUY)msg = module.npcHandler:parseMessage(msg, parseInfo)module.npcHandler:say(msg, cid)elseif(parameters.eventType == SHOPMODULE_BUY_ITEM_CONTAINER) thenlocal msg = module.npcHandler:getMessage(MESSAGE_BUY)msg = module.npcHandler:parseMessage(msg, parseInfo)module.npcHandler:say(msg, cid)endreturn trueendendNpchandler:
-- Advanced NPC System (Created by Jiddo),-- Modified by Talaturen.if(NpcHandler == nil) then-- Constant talkdelay behaviors.TALKDELAY_NONE = 0 -- No talkdelay. Npc will reply immedeatly.TALKDELAY_ONTHINK = 1 -- Talkdelay handled through the onThink callback function. (Default)TALKDELAY_EVENT = 2 -- Not yet implemented-- Currently applied talkdelay behavior. TALKDELAY_ONTHINK is default.NPCHANDLER_TALKDELAY = TALKDELAY_ONTHINK-- Constant conversation behaviors.CONVERSATION_DEFAULT = 0 -- Conversation through default window, like it was before 8.2 update.CONVERSATION_PRIVATE = 1 -- Conversation through NPCs chat window, as of 8.2 update. (Default)--Small Note: Private conversations also means the NPC will use multi-focus system.-- Currently applied conversation behavior. CONVERSATION_PRIVATE is default.NPCHANDLER_CONVBEHAVIOR = CONVERSATION_PRIVATE-- Constant indexes for defining default messages.MESSAGE_GREET = 1 -- When the player greets the npc.MESSAGE_FAREWELL = 2 -- When the player unGreets the npc.MESSAGE_BUY = 3 -- When the npc asks the player if he wants to buy something.MESSAGE_ONBUY = 4 -- When the player successfully buys something via talk.MESSAGE_BOUGHT = 5 -- When the player bought something through the shop window.MESSAGE_SELL = 6 -- When the npc asks the player if he wants to sell something.MESSAGE_ONSELL = 7 -- When the player successfully sells something via talk.MESSAGE_SOLD = 8 -- When the player sold something through the shop window.MESSAGE_MISSINGMONEY = 9 -- When the player does not have enough money.MESSAGE_NEEDMONEY = 10 -- Same as above, used for shop window.MESSAGE_MISSINGITEM = 11 -- When the player is trying to sell an item he does not have.MESSAGE_NEEDITEM = 12 -- Same as above, used for shop window.MESSAGE_NEEDSPACE = 13 -- When the player don't have any space to buy an itemMESSAGE_NEEDMORESPACE = 14 -- When the player has some space to buy an item, but not enough spaceMESSAGE_IDLETIMEOUT = 15 -- When the player has been idle for longer then idleTime allows.MESSAGE_WALKAWAY = 16 -- When the player walks out of the talkRadius of the npc.MESSAGE_DECLINE = 17 -- When the player says no to something.MESSAGE_SENDTRADE = 18 -- When the npc sends the trade window to the playerMESSAGE_NOSHOP = 19 -- When the npc's shop is requested but he doesn't have anyMESSAGE_ONCLOSESHOP = 20 -- When the player closes the npc's shop windowMESSAGE_ALREADYFOCUSED = 21 -- When the player already has the focus of this npc.MESSAGE_PLACEDINQUEUE = 22 -- When the player has been placed in the costumer queue.-- Constant indexes for callback functions. These are also used for module callback ids.CALLBACK_CREATURE_APPEAR = 1CALLBACK_CREATURE_DISAPPEAR = 2CALLBACK_CREATURE_SAY = 3CALLBACK_ONTHINK = 4CALLBACK_GREET = 5CALLBACK_FAREWELL = 6CALLBACK_MESSAGE_DEFAULT = 7CALLBACK_PLAYER_ENDTRADE = 8CALLBACK_PLAYER_CLOSECHANNEL = 9CALLBACK_ONBUY = 10CALLBACK_ONSELL = 11-- Addidional module callback idsCALLBACK_MODULE_INIT = 12CALLBACK_MODULE_RESET = 13-- Constant strings defining the keywords to replace in the default messages.TAG_PLAYERNAME = '|PLAYERNAME|'TAG_ITEMCOUNT = '|ITEMCOUNT|'TAG_TOTALCOST = '|TOTALCOST|'TAG_ITEMNAME = '|ITEMNAME|'TAG_QUEUESIZE = '|QUEUESIZE|'NpcHandler = {keywordHandler = nil,focuses = nil,talkStart = nil,idleTime = 90,talkRadius = 4,talkDelayTime = 1, -- Seconds to delay outgoing messages.queue = nil,talkDelay = nil,callbackFunctions = nil,modules = nil,shopItems = nil, -- They must be here since ShopModule uses "static" functionsmessages = {-- These are the default replies of all npcs. They can/should be changed individually for each npc.[MESSAGE_GREET] = 'Welcome, |PLAYERNAME|! I have been expecting you.',[MESSAGE_FAREWELL] = 'Good bye, |PLAYERNAME|!',[MESSAGE_BUY] = 'Do you want to buy |ITEMCOUNT| |ITEMNAME| for |TOTALCOST| gold coins?',[MESSAGE_ONBUY] = 'It was a pleasure doing business with you.',[MESSAGE_BOUGHT] = 'Bought |ITEMCOUNT|x |ITEMNAME| for |TOTALCOST| gold.',[MESSAGE_SELL] = 'Do you want to sell |ITEMCOUNT| |ITEMNAME| for |TOTALCOST| gold coins?',[MESSAGE_ONSELL] = 'Thank you for this |ITEMNAME|, |PLAYERNAME| gold.',[MESSAGE_SOLD] = 'Sold |ITEMCOUNT|x |ITEMNAME| for |TOTALCOST| gold.',[MESSAGE_MISSINGMONEY] = 'Sorry, you don\'t have enough money.',[MESSAGE_NEEDMONEY] = 'You do not have enough money.',[MESSAGE_MISSINGITEM] = 'You don\'t even have that item, |PLAYERNAME|!',[MESSAGE_NEEDITEM] = 'You do not have this object.',[MESSAGE_NEEDSPACE] = 'You do not have enough capacity.',[MESSAGE_NEEDMORESPACE] = 'You do not have enough capacity for all items.',[MESSAGE_IDLETIMEOUT] = 'Next, please!',[MESSAGE_WALKAWAY] = 'How rude!',[MESSAGE_DECLINE] = 'Not good enough, is it... ?',[MESSAGE_SENDTRADE] = 'Here\'s my offer, |PLAYERNAME|. Don\'t you like it?',[MESSAGE_NOSHOP] = 'Sorry, I\'m not offering anything.',[MESSAGE_ONCLOSESHOP] = 'Thank you, come back when you want something more.',[MESSAGE_ALREADYFOCUSED]= '|PLAYERNAME|! I am already talking to you...',[MESSAGE_PLACEDINQUEUE] = '|PLAYERNAME|, please wait for your turn. There are |QUEUESIZE| customers before you.'}}-- Creates a new NpcHandler with an empty callbackFunction stack.function NpcHandler:new(keywordHandler)local obj = {}obj.callbackFunctions = {}obj.modules = {}if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) thenobj.focuses = {}obj.talkStart = {}elseobj.queue = Queue:new(obj)obj.focuses = 0obj.talkStart = 0endobj.talkDelay = {}obj.keywordHandler = keywordHandlerobj.messages = {}obj.shopItems = {}setmetatable(obj.messages, self.messages)self.messages.__index = self.messagessetmetatable(obj, self)self.__index = selfreturn objend-- Re-defines the maximum idle time allowed for a player when talking to this npc.function NpcHandler:setMaxIdleTime(newTime)self.idleTime = newTimeend-- Attackes a new keyword handler to this npchandlerfunction NpcHandler:setKeywordHandler(newHandler)self.keywordHandler = newHandlerend-- Function used to change the focus of this npc.function NpcHandler:addFocus(newFocus)if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) thenif(self:isFocused(newFocus)) thenreturnendtable.insert(self.focuses, newFocus)elseself.focuses = newFocusendself:updateFocus()endNpcHandler.changeFocus = NpcHandler.addFocus --"changeFocus" looks better for CONVERSATION_DEFAULT-- Function used to verify if npc is focused to certain playerfunction NpcHandler:isFocused(focus)if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) thenfor k,v in pairs(self.focuses) doif v == focus thenreturn trueendendreturn falseendreturn (self.focuses == focus)end-- This function should be called on each onThink and makes sure the npc faces the player it is talking to.-- Should also be called whenever a new player is focused.function NpcHandler:updateFocus()if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) thenfor pos, focus in pairs(self.focuses) doif(focus ~= nil) thendoNpcSetCreatureFocus(focus)returnendenddoNpcSetCreatureFocus(0)elsedoNpcSetCreatureFocus(self.focuses)endend-- Used when the npc should un-focus the player.function NpcHandler:releaseFocus(focus)if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) thenif(not self:isFocused(focus)) thenreturnendlocal pos = nilfor k,v in pairs(self.focuses) doif v == focus thenpos = kendendtable.remove(self.focuses, pos)self.talkStart[focus] = nilcloseShopWindow(focus) --Even if it can not exist, we need to prevent it.self:updateFocus()elsecloseShopWindow(focus)self:changeFocus(0)endend-- Returns the callback function with the specified id or nil if no such callback function exists.function NpcHandler:getCallback(id)local ret = nilif(self.callbackFunctions ~= nil) thenret = self.callbackFunctions[id]endreturn retend-- Changes the callback function for the given id to callback.function NpcHandler:setCallback(id, callback)if(self.callbackFunctions ~= nil) thenself.callbackFunctions[id] = callbackendend-- Adds a module to this npchandler and inits it.function NpcHandler:addModule(module)if(self.modules == nil or module == nil) thenreturn falseendmodule:init(self)if(module.parseParameters ~= nil) thenmodule:parseParameters()endtable.insert(self.modules, module)return trueend-- Calls the callback function represented by id for all modules added to this npchandler with the given arguments.function NpcHandler:processModuleCallback(id, ...)local ret = truefor i, module in pairs(self.modules) dolocal tmpRet = trueif(id == CALLBACK_CREATURE_APPEAR and module.callbackOnCreatureAppear ~= nil) thentmpRet = module:callbackOnCreatureAppear(unpack(arg))elseif(id == CALLBACK_CREATURE_DISAPPEAR and module.callbackOnCreatureDisappear ~= nil) thentmpRet = module:callbackOnCreatureDisappear(unpack(arg))elseif(id == CALLBACK_CREATURE_SAY and module.callbackOnCreatureSay ~= nil) thentmpRet = module:callbackOnCreatureSay(unpack(arg))elseif(id == CALLBACK_PLAYER_ENDTRADE and module.callbackOnPlayerEndTrade ~= nil) thentmpRet = module:callbackOnPlayerEndTrade(unpack(arg))elseif(id == CALLBACK_PLAYER_CLOSECHANNEL and module.callbackOnPlayerCloseChannel ~= nil) thentmpRet = module:callbackOnPlayerCloseChannel(unpack(arg))elseif(id == CALLBACK_ONBUY and module.callbackOnBuy ~= nil) thentmpRet = module:callbackOnBuy(...)elseif(id == CALLBACK_ONSELL and module.callbackOnSell ~= nil) thentmpRet = module:callbackOnSell(unpack(arg))elseif(id == CALLBACK_ONTHINK and module.callbackOnThink ~= nil) thentmpRet = module:callbackOnThink(unpack(arg))elseif(id == CALLBACK_GREET and module.callbackOnGreet ~= nil) thentmpRet = module:callbackOnGreet(unpack(arg))elseif(id == CALLBACK_FAREWELL and module.callbackOnFarewell ~= nil) thentmpRet = module:callbackOnFarewell(unpack(arg))elseif(id == CALLBACK_MESSAGE_DEFAULT and module.callbackOnMessageDefault ~= nil) thentmpRet = module:callbackOnMessageDefault(unpack(arg))elseif(id == CALLBACK_MODULE_RESET and module.callbackOnModuleReset ~= nil) thentmpRet = module:callbackOnModuleReset(unpack(arg))endif(not tmpRet) thenret = falsebreakendendreturn retend-- Returns the message represented by id.function NpcHandler:getMessage(id)local ret = nilif(self.messages ~= nil) thenret = self.messages[id]endreturn retend-- Changes the default response message with the specified id to newMessage.function NpcHandler:setMessage(id, newMessage)if(self.messages ~= nil) thenself.messages[id] = newMessageendend-- Translates all message tags found in msg using parseInfofunction NpcHandler:parseMessage(msg, parseInfo)local ret = msgfor search, replace in pairs(parseInfo) doret = string.gsub(ret, search, replace)endreturn retend-- Makes sure the npc un-focuses the currently focused playerfunction NpcHandler:unGreet(cid)if(not self:isFocused(cid)) thenreturnendlocal callback = self:getCallback(CALLBACK_FAREWELL)if(callback == nil or callback(cid)) thenif(self:processModuleCallback(CALLBACK_FAREWELL)) thenif(self.queue == nil or not self.queue:greetNext()) thenlocal msg = self:getMessage(MESSAGE_FAREWELL)local parseInfo = { [TAG_PLAYERNAME] = getPlayerName(cid) }msg = self:parseMessage(msg, parseInfo)self:say(msg, cid)self:releaseFocus(cid)self:say(msg)endendendend-- Greets a new player.function NpcHandler:greet(cid)if(cid ~= 0) thenlocal callback = self:getCallback(CALLBACK_GREET)if(callback == nil or callback(cid)) thenif(self:processModuleCallback(CALLBACK_GREET, cid)) thenlocal msg = self:getMessage(MESSAGE_GREET)local parseInfo = { [TAG_PLAYERNAME] = getCreatureName(cid) }msg = self:parseMessage(msg, parseInfo)self:say(msg)self:addFocus(cid)self:say(msg, cid)endendendend-- Handles onCreatureAppear events. If you with to handle this yourself, please use the CALLBACK_CREATURE_APPEAR callback.function NpcHandler:onCreatureAppear(cid)local callback = self:getCallback(CALLBACK_CREATURE_APPEAR)if(callback == nil or callback(cid)) thenif(self:processModuleCallback(CALLBACK_CREATURE_APPEAR, cid)) then--endendend-- Handles onCreatureDisappear events. If you with to handle this yourself, please use the CALLBACK_CREATURE_DISAPPEAR callback.function NpcHandler:onCreatureDisappear(cid)local callback = self:getCallback(CALLBACK_CREATURE_DISAPPEAR)if(callback == nil or callback(cid)) thenif(self:processModuleCallback(CALLBACK_CREATURE_DISAPPEAR, cid)) thenif(self:isFocused(cid)) thenself:unGreet(cid)endendendend-- Handles onCreatureSay events. If you with to handle this yourself, please use the CALLBACK_CREATURE_SAY callback.function NpcHandler:onCreatureSay(cid, class, msg)local callback = self:getCallback(CALLBACK_CREATURE_SAY)if(callback == nil or callback(cid, class, msg)) thenif(self:processModuleCallback(CALLBACK_CREATURE_SAY, cid, class, msg)) thenif(not self:isInRange(cid)) thenreturnendif(self.keywordHandler ~= nil) thenif((self:isFocused(cid) and (class == TALKTYPE_PRIVATE_PN or NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT)) or not self:isFocused(cid)) thenlocal ret = self.keywordHandler:processMessage(cid, msg)if(not ret) thenlocal callback = self:getCallback(CALLBACK_MESSAGE_DEFAULT)if(callback ~= nil and callback(cid, class, msg)) thenif(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) thenself.talkStart[cid] = os.time()elseself.talkStart = os.time()endendelseif(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) thenself.talkStart[cid] = os.time()elseself.talkStart = os.time()endendendendendendend-- Handles onPlayerEndTrade events. If you wish to handle this yourself, use the CALLBACK_PLAYER_ENDTRADE callback.function NpcHandler:onPlayerEndTrade(cid)local callback = self:getCallback(CALLBACK_PLAYER_ENDTRADE)if(callback == nil or callback(cid)) thenif(self:processModuleCallback(CALLBACK_PLAYER_ENDTRADE, cid, class, msg)) thenif(self:isFocused(cid)) thenlocal parseInfo = { [TAG_PLAYERNAME] = getPlayerName(cid) }local msg = self:parseMessage(self:getMessage(MESSAGE_ONCLOSESHOP), parseInfo)self:say(msg, cid)endendendend-- Handles onPlayerCloseChannel events. If you wish to handle this yourself, use the CALLBACK_PLAYER_CLOSECHANNEL callback.function NpcHandler:onPlayerCloseChannel(cid)local callback = self:getCallback(CALLBACK_PLAYER_CLOSECHANNEL)if(callback == nil or callback(cid)) thenif(self:processModuleCallback(CALLBACK_PLAYER_CLOSECHANNEL, cid, class, msg)) thenif(self:isFocused(cid)) thenself:unGreet(cid)endendendend-- Handles onBuy events. If you wish to handle this yourself, use the CALLBACK_ONBUY callback.function NpcHandler:onBuy(cid, itemid, subType, amount, ignoreCap, inBackpacks)local callback = self:getCallback(CALLBACK_ONBUY)if(callback == nil or callback(cid, itemid, subType, amount, ignoreCap, inBackpacks)) thenif(self:processModuleCallback(CALLBACK_ONBUY, cid, itemid, subType, amount, ignoreCap, inBackpacks)) then--endendend-- Handles onSell events. If you wish to handle this yourself, use the CALLBACK_ONSELL callback.function NpcHandler:onSell(cid, itemid, subType, amount, ignoreCap, inBackpacks)local callback = self:getCallback(CALLBACK_ONSELL)if(callback == nil or callback(cid, itemid, subType, amount, ignoreCap, inBackpacks)) thenif(self:processModuleCallback(CALLBACK_ONSELL, cid, itemid, subType, amount, ignoreCap, inBackpacks)) then--endendend-- Handles onThink events. If you wish to handle this yourself, please use the CALLBACK_ONTHINK callback.function NpcHandler:onThink()local callback = self:getCallback(CALLBACK_ONTHINK)if(callback == nil or callback()) thenif(NPCHANDLER_TALKDELAY == TALKDELAY_ONTHINK) thenif(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) thenfor cid, talkDelay in pairs(self.talkDelay) doif(talkDelay.time ~= nil and talkDelay.message ~= nil and os.time() >= talkDelay.time) thenselfSay(talkDelay.message, cid)self.talkDelay[cid] = nilendendelseif(self.talkDelay.time ~= nil and self.talkDelay.message ~= nil and os.time() >= self.talkDelay.time) thenselfSay(self.talkDelay.message)self.talkDelay.time = nilself.talkDelay.message = nilendendif(self:processModuleCallback(CALLBACK_ONTHINK)) thenif(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) thenfor pos, focus in pairs(self.focuses) doif(focus ~= nil) thenif(not self:isInRange(focus)) thenself:onWalkAway(focus)elseif((os.time() - self.talkStart[focus]) > self.idleTime) thenself:unGreet(focus)elseself:updateFocus()endendendelseif(self.focuses ~= 0) thenif(not self:isInRange(self.focuses)) thenself:onWalkAway(self.focuses)elseif(os.time()-self.talkStart > self.idleTime) thenself:unGreet(self.focuses)elseself:updateFocus()endendendendend-- Tries to greet the player with the given cid.function NpcHandler:onGreet(cid)if(self:isInRange(cid)) thenif(NPCHANDLER_CONVBEHAVIOR == CONVERSATION_PRIVATE) thenif(not self:isFocused(cid)) thenself:greet(cid)returnendelseif(NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT) thenif(self.focuses == 0) thenself:greet(cid)elseif(self.focuses == cid) thenlocal msg = self:getMessage(MESSAGE_ALREADYFOCUSED)local parseInfo = { [TAG_PLAYERNAME] = getCreatureName(cid) }msg = self:parseMessage(msg, parseInfo)self:say(msg)elseif(not self.queue:isInQueue(cid)) thenself.queue:push(cid)endlocal msg = self:getMessage(MESSAGE_PLACEDINQUEUE)local parseInfo = { [TAG_PLAYERNAME] = getCreatureName(cid), [TAG_QUEUESIZE] = self.queue:getSize() }msg = self:parseMessage(msg, parseInfo)self:say(msg)endendendend-- Simply calls the underlying unGreet function.function NpcHandler:onFarewell(cid)self:unGreet(cid)end-- Should be called on this npc's focus if the distance to focus is greater then talkRadius.function NpcHandler:onWalkAway(cid)if(self:isFocused(cid)) thenlocal callback = self:getCallback(CALLBACK_CREATURE_DISAPPEAR)if(callback == nil or callback(cid)) thenif(self:processModuleCallback(CALLBACK_CREATURE_DISAPPEAR, cid)) thenif(self.queue == nil or not self.queue:greetNext()) thenlocal msg = self:getMessage(MESSAGE_WALKAWAY)local parseInfo = { [TAG_PLAYERNAME] = getPlayerName(cid) }msg = self:parseMessage(msg, parseInfo)self:say(msg, cid)self:releaseFocus(cid)self:say(msg)endendendendend-- Returns true if cid is within the talkRadius of this npc.function NpcHandler:isInRange(cid)local distance = getDistanceTo(cid) or -1if(distance == -1) thenreturn falseendreturn (distance <= self.talkRadius)end-- Resets the npc into it's initial state (in regard of the keyrodhandler).-- All modules are also receiving a reset call through their callbackOnModuleReset function.function NpcHandler:resetNpc()if(self:processModuleCallback(CALLBACK_MODULE_RESET)) thenself.keywordHandler:reset()endend-- Makes the npc represented by this instance of NpcHandler say something.-- This implements the currently set type of talkdelay.-- shallDelay is a boolean value. If it is false, the message is not delayed. Default value is false.function NpcHandler:say(message, focus, shallDelay)local shallDelay = shallDelay or falseif(NPCHANDLER_TALKDELAY == TALKDELAY_NONE or not shallDelay) thenif(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) thenselfSay(message, focus)returnelseselfSay(message)returnendend-- TODO: Add an event handling method for delayed messagesif(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) thenself.talkDelay[focus] = {message = message,time = os.time() + self.talkDelayTime,}elseself.talkDelay = {message = message,time = os.time() + self.talkDelayTime}endendend
fis isso e agora deu erro em todos Npc(travel,blesser...)
[Error - Npc interface] data/npc/scripts/travel/boat.lua Description: data/npc/scripts/travel/boat.lua:3: attempt to index global 'NpcSystem' (a nil v alue) [Warning - NpcScript::NpcScript] Cannot load script: data/npc/scripts/travel/boa t.lua
Coloca esse npchandler
-- Advanced NPC System (Created by Jiddo),
-- Modified by TheForgottenServer Team,
-- Modified by OTX Server Team.
if(NpcHandler == nil) then
-- Constant talkdelay behaviors.
TALKDELAY_NONE = 0 -- No talkdelay. Npc will reply immedeatly.
TALKDELAY_ONTHINK = 1 -- Talkdelay handled through the onThink callback function. (Default)
TALKDELAY_EVENT = 2 -- Not yet implemented
-- Currently applied talkdelay behavior. TALKDELAY_ONTHINK is default.
NPCHANDLER_TALKDELAY = TALKDELAY_ONTHINK
-- Constant conversation behaviors.
CONVERSATION_DEFAULT = 0 -- Conversation through default window, like it was before 8.2 update.
CONVERSATION_PRIVATE = 1 -- Conversation through NPCs chat window, as of 8.2 update. (Default)
--Small Note: Private conversations also means the NPC will use multi-focus system.
-- Currently applied conversation behavior. CONVERSATION_PRIVATE is default.
NPCHANDLER_CONVBEHAVIOR = CONVERSATION_PRIVATE
-- Constant indexes for defining default messages.
MESSAGE_GREET = 1 -- When the player greets the npc.
MESSAGE_FAREWELL = 2 -- When the player unGreets the npc.
MESSAGE_BUY = 3 -- When the npc asks the player if he wants to buy something.
MESSAGE_ONBUY = 4 -- When the player successfully buys something via talk.
MESSAGE_BOUGHT = 5 -- When the player bought something through the shop window.
MESSAGE_SELL = 6 -- When the npc asks the player if he wants to sell something.
MESSAGE_ONSELL = 7 -- When the player successfully sells something via talk.
MESSAGE_SOLD = 8 -- When the player sold something through the shop window.
MESSAGE_MISSINGMONEY = 9 -- When the player does not have enough money.
MESSAGE_NEEDMONEY = 10 -- Same as above, used for shop window.
MESSAGE_MISSINGITEM = 11 -- When the player is trying to sell an item he does not have.
MESSAGE_NEEDITEM = 12 -- Same as above, used for shop window.
MESSAGE_NEEDSPACE = 13 -- When the player don't have any space to buy an item
MESSAGE_NEEDMORESPACE = 14 -- When the player has some space to buy an item, but not enough space
MESSAGE_IDLETIMEOUT = 15 -- When the player has been idle for longer then idleTime allows.
MESSAGE_WALKAWAY = 16 -- When the player walks out of the talkRadius of the npc.
MESSAGE_DECLINE = 17 -- When the player says no to something.
MESSAGE_SENDTRADE = 18 -- When the npc sends the trade window to the player
MESSAGE_NOSHOP = 19 -- When the npc's shop is requested but he doesn't have any
MESSAGE_ONCLOSESHOP = 20 -- When the player closes the npc's shop window
MESSAGE_ALREADYFOCUSED = 21 -- When the player already has the focus of this npc.
MESSAGE_PLACEDINQUEUE = 22 -- When the player has been placed in the costumer queue.
-- Constant indexes for callback functions. These are also used for module callback ids.
CALLBACK_CREATURE_APPEAR = 1
CALLBACK_CREATURE_DISAPPEAR = 2
CALLBACK_CREATURE_SAY = 3
CALLBACK_ONTHINK = 4
CALLBACK_GREET = 5
CALLBACK_FAREWELL = 6
CALLBACK_MESSAGE_DEFAULT = 7
CALLBACK_PLAYER_ENDTRADE = 8
CALLBACK_PLAYER_CLOSECHANNEL = 9
CALLBACK_ONBUY = 10
CALLBACK_ONSELL = 11
-- Addidional module callback ids
CALLBACK_MODULE_INIT = 12
CALLBACK_MODULE_RESET = 13
-- Constant strings defining the keywords to replace in the default messages.
TAG_PLAYERNAME = '|PLAYERNAME|'
TAG_ITEMCOUNT = '|ITEMCOUNT|'
TAG_TOTALCOST = '|TOTALCOST|'
TAG_ITEMNAME = '|ITEMNAME|'
TAG_QUEUESIZE = '|QUEUESIZE|'
NpcHandler = {
keywordHandler = nil,
focuses = nil,
talkStart = nil,
idleTime = 300,
talkRadius = 3,
talkDelayTime = 400, -- Seconds to delay outgoing messages.
queue = nil,
talkDelay = nil,
callbackFunctions = nil,
modules = nil,
shopItems = nil, -- They must be here since ShopModule uses "static" functions
messages = {
-- These are the default replies of all npcs. They can/should be changed individually for each npc.
[MESSAGE_GREET] = 'Bem-vindo, |PLAYERNAME|! Estava esperando você.',
[MESSAGE_FAREWELL] = 'Até logo, |PLAYERNAME|!',
[MESSAGE_BUY] = 'Quer comprar |ITEMCOUNT| |ITEMNAME| para |TOTALCOST| gold coins?',
[MESSAGE_ONBUY] = 'Foi um prazer fazer negócios com você.',
[MESSAGE_BOUGHT] = 'Comprei |ITEMCOUNT|x |ITEMNAME| para |TOTALCOST| gold.',
[MESSAGE_SELL] = 'Você quer vender |ITEMCOUNT| |ITEMNAME| para |TOTALCOST| gold coins?',
[MESSAGE_ONSELL] = 'Obrigado por isso |ITEMNAME|, |PLAYERNAME| gold.',
[MESSAGE_SOLD] = 'Vendida |ITEMCOUNT|x |ITEMNAME| para |TOTALCOST| gold.',
[MESSAGE_MISSINGMONEY] = 'Desculpe, que você não tem dinheiro suficiente.',
[MESSAGE_NEEDMONEY] = 'Você não tem dinheiro suficiente.',
[MESSAGE_MISSINGITEM] = 'Você não tem esse item, |PLAYERNAME|!',
[MESSAGE_NEEDITEM] = 'Você não tem este objeto.',
[MESSAGE_NEEDSPACE] = 'Você não tem capacidade suficiente.',
[MESSAGE_NEEDMORESPACE] = 'Você não tem capacidade suficiente para todos os itens.',
[MESSAGE_IDLETIMEOUT] = 'Próximo, por favor!',
[MESSAGE_WALKAWAY] = 'Que falta de educação!',
[MESSAGE_DECLINE] = 'Não é bom o suficiente, é... ?',
[MESSAGE_SENDTRADE] = 'Eis a minha oferta, |PLAYERNAME|. Você não gosta?',
[MESSAGE_NOSHOP] = 'Desculpa, eu não estou oferecendo qualquer coisa.',
[MESSAGE_ONCLOSESHOP] = 'Obrigado, volte quando você quer algo mais.',
[MESSAGE_ALREADYFOCUSED]= '|PLAYERNAME|! Já estou falando com você...',
[MESSAGE_PLACEDINQUEUE] = '|PLAYERNAME|, por favor, espere a sua vez. Existem |QUEUESIZE| clientes antes de você.'
}
}
-- Creates a new NpcHandler with an empty callbackFunction stack.
function NpcHandler:new(keywordHandler)
local obj = {}
obj.callbackFunctions = {}
obj.modules = {}
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
obj.focuses = {}
obj.talkStart = {}
else
obj.queue = Queue:new(obj)
obj.focuses = 0
obj.talkStart = 0
end
obj.talkDelay = {}
obj.keywordHandler = keywordHandler
obj.messages = {}
obj.shopItems = {}
setmetatable(obj.messages, self.messages)
self.messages.__index = self.messages
setmetatable(obj, self)
self.__index = self
return obj
end
-- Re-defines the maximum idle time allowed for a player when talking to this npc.
function NpcHandler:setMaxIdleTime(newTime)
self.idleTime = newTime
end
-- Attackes a new keyword handler to this npchandler
function NpcHandler:setKeywordHandler(newHandler)
self.keywordHandler = newHandler
end
-- Function used to change the focus of this npc.
function NpcHandler:addFocus(newFocus)
if(not isCreature(newFocus)) then
return
end
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
if(self:isFocused(newFocus, true)) then
return
end
table.insert(self.focuses, newFocus)
else
self.focuses = newFocus
end
self:updateFocus(true)
end
NpcHandler.changeFocus = NpcHandler.addFocus -- "changeFocus" looks better for CONVERSATION_DEFAULT
-- Function used to verify if npc is focused to certain player
function NpcHandler:isFocused(focus, creatureCheck)
local creatureCheck = creatureCheck or false
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
for k, v in pairs(self.focuses) do
if(v == focus) then
if(creatureCheck or isCreature(v)) then
return true
end
self:unsetFocus(focus, k)
return false
end
end
return false
end
if(creatureCheck or isCreature(self.focuses)) then
return self.focuses == focus
end
self:changeFocus(0)
return false
end
-- This function should be called on each onThink and makes sure the npc faces the player it is talking to.
-- Should also be called whenever a new player is focused.
function NpcHandler:updateFocus(creatureCheck)
local creatureCheck = creatureCheck or false
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
for _, focus in pairs(self.focuses) do
if(creatureCheck or isCreature(focus)) then
doNpcSetCreatureFocus(focus)
return
end
end
elseif(creatureCheck or isCreature(self.focuses)) then
doNpcSetCreatureFocus(self.focuses)
return
end
doNpcSetCreatureFocus(0)
end
-- Used when the npc should un-focus the player.
function NpcHandler:releaseFocus(focus)
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
if(not self:isFocused(focus)) then
return
end
local pos = nil
for k, v in pairs(self.focuses) do
if(v == focus) then
pos = k
end
end
if(pos ~= nil) then
closeShopWindow(focus)
self:unsetFocus(focus, pos)
end
elseif(self.focuses == focus) then
if(isCreature(focus)) then
closeShopWindow(focus)
end
self:changeFocus(0)
end
end
-- Internal un-focusing function, beware using!
function NpcHandler:unsetFocus(focus, pos)
if(type(self.focuses) ~= "table" or pos == nil or self.focuses[pos] == nil) then
return
end
table.remove(self.focuses, pos)
self.talkStart[focus] = nil
self:updateFocus()
end
-- Returns the callback function with the specified id or nil if no such callback function exists.
function NpcHandler:getCallback(id)
local ret = nil
if(self.callbackFunctions ~= nil) then
ret = self.callbackFunctions[id]
end
return ret
end
-- Changes the callback function for the given id to callback.
function NpcHandler:setCallback(id, callback)
if(self.callbackFunctions ~= nil) then
self.callbackFunctions[id] = callback
end
end
-- Adds a module to this npchandler and inits it.
function NpcHandler:addModule(module)
if(self.modules == nil or module == nil) then
return false
end
module:init(self)
if(module.parseParameters ~= nil) then
module:parseParameters()
end
table.insert(self.modules, module)
return true
end
-- Calls the callback function represented by id for all modules added to this npchandler with the given arguments.
function NpcHandler:processModuleCallback(id, ...)
local ret = true
for _, module in pairs(self.modules) do
local tmpRet = true
if(id == CALLBACK_CREATURE_APPEAR and module.callbackOnCreatureAppear ~= nil) then
tmpRet = module:callbackOnCreatureAppear(...)
elseif(id == CALLBACK_CREATURE_DISAPPEAR and module.callbackOnCreatureDisappear ~= nil) then
tmpRet = module:callbackOnCreatureDisappear(...)
elseif(id == CALLBACK_CREATURE_SAY and module.callbackOnCreatureSay ~= nil) then
tmpRet = module:callbackOnCreatureSay(...)
elseif(id == CALLBACK_PLAYER_ENDTRADE and module.callbackOnPlayerEndTrade ~= nil) then
tmpRet = module:callbackOnPlayerEndTrade(...)
elseif(id == CALLBACK_PLAYER_CLOSECHANNEL and module.callbackOnPlayerCloseChannel ~= nil) then
tmpRet = module:callbackOnPlayerCloseChannel(...)
elseif(id == CALLBACK_ONBUY and module.callbackOnBuy ~= nil) then
tmpRet = module:callbackOnBuy(...)
elseif(id == CALLBACK_ONSELL and module.callbackOnSell ~= nil) then
tmpRet = module:callbackOnSell(...)
elseif(id == CALLBACK_ONTHINK and module.callbackOnThink ~= nil) then
tmpRet = module:callbackOnThink(...)
elseif(id == CALLBACK_GREET and module.callbackOnGreet ~= nil) then
tmpRet = module:callbackOnGreet(...)
elseif(id == CALLBACK_FAREWELL and module.callbackOnFarewell ~= nil) then
tmpRet = module:callbackOnFarewell(...)
elseif(id == CALLBACK_MESSAGE_DEFAULT and module.callbackOnMessageDefault ~= nil) then
tmpRet = module:callbackOnMessageDefault(...)
elseif(id == CALLBACK_MODULE_RESET and module.callbackOnModuleReset ~= nil) then
tmpRet = module:callbackOnModuleReset(...)
end
if(not tmpRet) then
ret = false
break
end
end
return ret
end
-- Returns the message represented by id.
function NpcHandler:getMessage(id)
local ret = nil
if(self.messages ~= nil) then
ret = self.messages[id]
end
return ret
end
-- Changes the default response message with the specified id to newMessage.
function NpcHandler:setMessage(id, newMessage)
if(self.messages ~= nil) then
self.messages[id] = newMessage
end
end
-- Translates all message tags found in msg using parseInfo
function NpcHandler:parseMessage(msg, parseInfo)
for search, replace in pairs(parseInfo) do
if(replace ~= nil) then
msg = msg:gsub(search, replace)
end
end
return msg
end
-- Makes sure the npc un-focuses the currently focused player
function NpcHandler:unGreet(cid)
if(not self:isFocused(cid)) then
return
end
local callback = self:getCallback(CALLBACK_FAREWELL)
if(callback == nil or callback(cid)) then
if(self:processModuleCallback(CALLBACK_FAREWELL)) then
if(self.queue == nil or not self.queue:greetNext()) then
local msg = self:getMessage(MESSAGE_FAREWELL)
local parseInfo = { [TAG_PLAYERNAME] = getPlayerName(cid) }
msg = self:parseMessage(msg, parseInfo)
self:say(msg, cid, 0, true)
self:releaseFocus(cid)
if(not isPlayerGhost(cid)) then
self:say(msg)
end
end
end
end
end
-- Greets a new player.
function NpcHandler:greet(cid)
local callback = self:getCallback(CALLBACK_GREET)
if(callback == nil or callback(cid)) then
if(self:processModuleCallback(CALLBACK_GREET, cid)) then
local msg = self:getMessage(MESSAGE_GREET)
local parseInfo = { [TAG_PLAYERNAME] = getCreatureName(cid) }
msg = self:parseMessage(msg, parseInfo)
if(not isPlayerGhost(cid)) then
self:say(msg)
end
self:addFocus(cid)
self:say(msg, cid)
end
end
end
-- Handles onCreatureAppear events. If you with to handle this yourself, please use the CALLBACK_CREATURE_APPEAR callback.
function NpcHandler:onCreatureAppear(cid)
local callback = self:getCallback(CALLBACK_CREATURE_APPEAR)
if(callback == nil or callback(cid)) then
if(self:processModuleCallback(CALLBACK_CREATURE_APPEAR, cid)) then
--
end
end
end
-- Handles onCreatureDisappear events. If you with to handle this yourself, please use the CALLBACK_CREATURE_DISAPPEAR callback.
function NpcHandler:onCreatureDisappear(cid)
local callback = self:getCallback(CALLBACK_CREATURE_DISAPPEAR)
if(callback == nil or callback(cid)) then
if(self:processModuleCallback(CALLBACK_CREATURE_DISAPPEAR, cid)) then
if(self:isFocused(cid)) then
self:unGreet(cid)
end
end
end
end
-- Handles onCreatureSay events. If you with to handle this yourself, please use the CALLBACK_CREATURE_SAY callback.
function NpcHandler:onCreatureSay(cid, class, msg)
local callback = self:getCallback(CALLBACK_CREATURE_SAY)
if(callback == nil or callback(cid, class, msg)) then
if(self:processModuleCallback(CALLBACK_CREATURE_SAY, cid, class, msg)) then
if(not self:isInRange(cid)) then
return
end
if(self.keywordHandler ~= nil) then
if((self:isFocused(cid) and (class == TALKTYPE_PRIVATE_PN or NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT)) or not self:isFocused(cid)) then
local ret = self.keywordHandler:processMessage(cid, msg)
if(not ret) then
local callback = self:getCallback(CALLBACK_MESSAGE_DEFAULT)
if(callback ~= nil and callback(cid, class, msg)) then
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
self.talkStart[cid] = os.time()
else
self.talkStart = os.time()
end
end
elseif(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
self.talkStart[cid] = os.time()
else
self.talkStart = os.time()
end
end
end
end
end
end
-- Handles onPlayerEndTrade events. If you wish to handle this yourself, use the CALLBACK_PLAYER_ENDTRADE callback.
function NpcHandler:onPlayerEndTrade(cid)
local callback = self:getCallback(CALLBACK_PLAYER_ENDTRADE)
if(callback == nil or callback(cid)) then
if(self:processModuleCallback(CALLBACK_PLAYER_ENDTRADE, cid)) then
if(self:isFocused(cid)) then
local parseInfo = { [TAG_PLAYERNAME] = getPlayerName(cid) }
local msg = self:parseMessage(self:getMessage(MESSAGE_ONCLOSESHOP), parseInfo)
self:say(msg, cid)
end
end
end
end
-- Handles onPlayerCloseChannel events. If you wish to handle this yourself, use the CALLBACK_PLAYER_CLOSECHANNEL callback.
function NpcHandler:onPlayerCloseChannel(cid)
local callback = self:getCallback(CALLBACK_PLAYER_CLOSECHANNEL)
if(callback == nil or callback(cid)) then
if(self:processModuleCallback(CALLBACK_PLAYER_CLOSECHANNEL, cid)) then
if(self:isFocused(cid)) then
self:unGreet(cid)
end
end
end
end
-- Handles onBuy events. If you wish to handle this yourself, use the CALLBACK_ONBUY callback.
function NpcHandler:onBuy(cid, itemid, subType, amount, ignoreCap, inBackpacks)
local callback = self:getCallback(CALLBACK_ONBUY)
if(callback == nil or callback(cid, itemid, subType, amount, ignoreCap, inBackpacks)) then
if(self:processModuleCallback(CALLBACK_ONBUY, cid, itemid, subType, amount, ignoreCap, inBackpacks)) then
--
end
end
end
-- Handles onSell events. If you wish to handle this yourself, use the CALLBACK_ONSELL callback.
function NpcHandler:onSell(cid, itemid, subType, amount, ignoreCap, inBackpacks)
local callback = self:getCallback(CALLBACK_ONSELL)
if(callback == nil or callback(cid, itemid, subType, amount, ignoreCap, inBackpacks)) then
if(self:processModuleCallback(CALLBACK_ONSELL, cid, itemid, subType, amount, ignoreCap, inBackpacks)) then
--
end
end
end
-- Handles onThink events. If you wish to handle this yourself, please use the CALLBACK_ONTHINK callback.
function NpcHandler:onThink()
local callback = self:getCallback(CALLBACK_ONTHINK)
if(callback == nil or callback()) then
for i, speech in pairs(self.talkDelay) do
if((speech.cid == nil or speech.cid == 0) and speech.time ~= nil and speech.message ~= nil) then
if(os.mtime() >= speech.time) then
selfSay(speech.message)
self.talkDelay = nil
end
elseif(isCreature(speech.cid) and speech.start ~= nil and speech.time ~= nil and speech.message ~= nil) then
if(os.mtime() >= speech.time) then
local talkStart = (NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT and self.talkStart[speech.cid] or self.talkStart)
if(speech.force or (self:isFocused(speech.cid) and talkStart == speech.start)) then
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
selfSay(speech.message, speech.cid)
else
selfSay(speech.message)
end
end
self.talkDelay = nil
end
else
self.talkDelay = nil
end
end
if(self:processModuleCallback(CALLBACK_ONTHINK)) then
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
for _, focus in pairs(self.focuses) do
if(focus ~= nil) then
if(not self:isInRange(focus)) then
self:onWalkAway(focus)
elseif((os.time() - self.talkStart[focus]) > self.idleTime) then
self:unGreet(focus)
else
self:updateFocus()
end
end
end
elseif(self.focuses ~= 0) then
if(not self:isInRange(self.focuses)) then
self:onWalkAway(self.focuses)
elseif((os.time() - self.talkStart) > self.idleTime) then
self:unGreet(self.focuses)
else
self:updateFocus()
end
end
end
end
end
-- Tries to greet the player with the given cid.
function NpcHandler:onGreet(cid)
if(self:isInRange(cid)) then
if(NPCHANDLER_CONVBEHAVIOR == CONVERSATION_PRIVATE) then
if(not self:isFocused(cid)) then
self:greet(cid)
return
end
elseif(NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT) then
if(self.focuses == 0) then
self:greet(cid)
elseif(self.focuses == cid) then
local msg = self:getMessage(MESSAGE_ALREADYFOCUSED)
local parseInfo = { [TAG_PLAYERNAME] = getCreatureName(cid) }
msg = self:parseMessage(msg, parseInfo)
self:say(msg)
else
if(not self.queue:isInQueue(cid)) then
self.queue:push(cid)
end
local msg = self:getMessage(MESSAGE_PLACEDINQUEUE)
local parseInfo = { [TAG_PLAYERNAME] = getCreatureName(cid), [TAG_QUEUESIZE] = self.queue:getSize() }
msg = self:parseMessage(msg, parseInfo)
self:say(msg)
end
end
end
end
-- Simply calls the underlying unGreet function.
function NpcHandler:onFarewell(cid)
self:unGreet(cid)
end
-- Should be called on this npc's focus if the distance to focus is greater then talkRadius.
function NpcHandler:onWalkAway(cid)
if(self:isFocused(cid)) then
local callback = self:getCallback(CALLBACK_CREATURE_DISAPPEAR)
if(callback == nil or callback(cid)) then
if(self:processModuleCallback(CALLBACK_CREATURE_DISAPPEAR, cid)) then
if(self.queue == nil or not self.queue:greetNext()) then
local msg = self:getMessage(MESSAGE_WALKAWAY)
local parseInfo = { [TAG_PLAYERNAME] = getPlayerName(cid) or -1 }
msg = self:parseMessage(msg, parseInfo)
self:say(msg, cid, 0, true)
self:releaseFocus(cid)
self:say(msg)
end
end
end
end
end
-- Returns true if cid is within the talkRadius of this npc.
function NpcHandler:isInRange(cid)
if not isPlayer(cid) then
return false
end
local distance = getNpcDistanceTo(cid) or -1
return distance ~= -1 and distance <= self.talkRadius
end
-- Resets the npc into it's initial state (in regard of the keyrodhandler).
-- All modules are also receiving a reset call through their callbackOnModuleReset function.
function NpcHandler:resetNpc()
if(self:processModuleCallback(CALLBACK_MODULE_RESET)) then
self.keywordHandler:reset()
end
end
-- Makes the npc represented by this instance of NpcHandler say something.
-- This implements the currently set type of talkdelay.
function NpcHandler:say(message, focus, delay, force)
local delay = delay or 0
if(NPCHANDLER_TALKDELAY == TALKDELAY_NONE or delay <= 0) then
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
selfSay(message, focus)
else
selfSay(message)
end
return
end
-- TODO: Add an event handling method for delayed messages
table.insert(self.talkDelay, {
id = getNpcId(),
cid = focus,
message = message,
time = os.mtime() + (delay and delay or self.talkDelayTime),
start = os.time(),
force = force or false
})
end
end
info
Grupo: Cavaleiro
Registrado: 01/04/13
Posts: 156
Reputação: +26
Gênero: Masculino
Char no Tibia: Yugami

Coloca esse npchandler
-- Advanced NPC System (Created by Jiddo),
-- Modified by TheForgottenServer Team,
-- Modified by OTX Server Team.
if(NpcHandler == nil) then
-- Constant talkdelay behaviors.
TALKDELAY_NONE = 0 -- No talkdelay. Npc will reply immedeatly.
TALKDELAY_ONTHINK = 1 -- Talkdelay handled through the onThink callback function. (Default)
TALKDELAY_EVENT = 2 -- Not yet implemented
-- Currently applied talkdelay behavior. TALKDELAY_ONTHINK is default.
NPCHANDLER_TALKDELAY = TALKDELAY_ONTHINK
-- Constant conversation behaviors.
CONVERSATION_DEFAULT = 0 -- Conversation through default window, like it was before 8.2 update.
CONVERSATION_PRIVATE = 1 -- Conversation through NPCs chat window, as of 8.2 update. (Default)
--Small Note: Private conversations also means the NPC will use multi-focus system.
-- Currently applied conversation behavior. CONVERSATION_PRIVATE is default.
NPCHANDLER_CONVBEHAVIOR = CONVERSATION_PRIVATE
-- Constant indexes for defining default messages.
MESSAGE_GREET = 1 -- When the player greets the npc.
MESSAGE_FAREWELL = 2 -- When the player unGreets the npc.
MESSAGE_BUY = 3 -- When the npc asks the player if he wants to buy something.
MESSAGE_ONBUY = 4 -- When the player successfully buys something via talk.
MESSAGE_BOUGHT = 5 -- When the player bought something through the shop window.
MESSAGE_SELL = 6 -- When the npc asks the player if he wants to sell something.
MESSAGE_ONSELL = 7 -- When the player successfully sells something via talk.
MESSAGE_SOLD = 8 -- When the player sold something through the shop window.
MESSAGE_MISSINGMONEY = 9 -- When the player does not have enough money.
MESSAGE_NEEDMONEY = 10 -- Same as above, used for shop window.
MESSAGE_MISSINGITEM = 11 -- When the player is trying to sell an item he does not have.
MESSAGE_NEEDITEM = 12 -- Same as above, used for shop window.
MESSAGE_NEEDSPACE = 13 -- When the player don't have any space to buy an item
MESSAGE_NEEDMORESPACE = 14 -- When the player has some space to buy an item, but not enough space
MESSAGE_IDLETIMEOUT = 15 -- When the player has been idle for longer then idleTime allows.
MESSAGE_WALKAWAY = 16 -- When the player walks out of the talkRadius of the npc.
MESSAGE_DECLINE = 17 -- When the player says no to something.
MESSAGE_SENDTRADE = 18 -- When the npc sends the trade window to the player
MESSAGE_NOSHOP = 19 -- When the npc's shop is requested but he doesn't have any
MESSAGE_ONCLOSESHOP = 20 -- When the player closes the npc's shop window
MESSAGE_ALREADYFOCUSED = 21 -- When the player already has the focus of this npc.
MESSAGE_PLACEDINQUEUE = 22 -- When the player has been placed in the costumer queue.
-- Constant indexes for callback functions. These are also used for module callback ids.
CALLBACK_CREATURE_APPEAR = 1
CALLBACK_CREATURE_DISAPPEAR = 2
CALLBACK_CREATURE_SAY = 3
CALLBACK_ONTHINK = 4
CALLBACK_GREET = 5
CALLBACK_FAREWELL = 6
CALLBACK_MESSAGE_DEFAULT = 7
CALLBACK_PLAYER_ENDTRADE = 8
CALLBACK_PLAYER_CLOSECHANNEL = 9
CALLBACK_ONBUY = 10
CALLBACK_ONSELL = 11
-- Addidional module callback ids
CALLBACK_MODULE_INIT = 12
CALLBACK_MODULE_RESET = 13
-- Constant strings defining the keywords to replace in the default messages.
TAG_PLAYERNAME = '|PLAYERNAME|'
TAG_ITEMCOUNT = '|ITEMCOUNT|'
TAG_TOTALCOST = '|TOTALCOST|'
TAG_ITEMNAME = '|ITEMNAME|'
TAG_QUEUESIZE = '|QUEUESIZE|'
NpcHandler = {
keywordHandler = nil,
focuses = nil,
talkStart = nil,
idleTime = 300,
talkRadius = 3,
talkDelayTime = 400, -- Seconds to delay outgoing messages.
queue = nil,
talkDelay = nil,
callbackFunctions = nil,
modules = nil,
shopItems = nil, -- They must be here since ShopModule uses "static" functions
messages = {
-- These are the default replies of all npcs. They can/should be changed individually for each npc.
[MESSAGE_GREET] = 'Bem-vindo, |PLAYERNAME|! Estava esperando você.',
[MESSAGE_FAREWELL] = 'Até logo, |PLAYERNAME|!',
[MESSAGE_BUY] = 'Quer comprar |ITEMCOUNT| |ITEMNAME| para |TOTALCOST| gold coins?',
[MESSAGE_ONBUY] = 'Foi um prazer fazer negócios com você.',
[MESSAGE_BOUGHT] = 'Comprei |ITEMCOUNT|x |ITEMNAME| para |TOTALCOST| gold.',
[MESSAGE_SELL] = 'Você quer vender |ITEMCOUNT| |ITEMNAME| para |TOTALCOST| gold coins?',
[MESSAGE_ONSELL] = 'Obrigado por isso |ITEMNAME|, |PLAYERNAME| gold.',
[MESSAGE_SOLD] = 'Vendida |ITEMCOUNT|x |ITEMNAME| para |TOTALCOST| gold.',
[MESSAGE_MISSINGMONEY] = 'Desculpe, que você não tem dinheiro suficiente.',
[MESSAGE_NEEDMONEY] = 'Você não tem dinheiro suficiente.',
[MESSAGE_MISSINGITEM] = 'Você não tem esse item, |PLAYERNAME|!',
[MESSAGE_NEEDITEM] = 'Você não tem este objeto.',
[MESSAGE_NEEDSPACE] = 'Você não tem capacidade suficiente.',
[MESSAGE_NEEDMORESPACE] = 'Você não tem capacidade suficiente para todos os itens.',
[MESSAGE_IDLETIMEOUT] = 'Próximo, por favor!',
[MESSAGE_WALKAWAY] = 'Que falta de educação!',
[MESSAGE_DECLINE] = 'Não é bom o suficiente, é... ?',
[MESSAGE_SENDTRADE] = 'Eis a minha oferta, |PLAYERNAME|. Você não gosta?',
[MESSAGE_NOSHOP] = 'Desculpa, eu não estou oferecendo qualquer coisa.',
[MESSAGE_ONCLOSESHOP] = 'Obrigado, volte quando você quer algo mais.',
[MESSAGE_ALREADYFOCUSED]= '|PLAYERNAME|! Já estou falando com você...',
[MESSAGE_PLACEDINQUEUE] = '|PLAYERNAME|, por favor, espere a sua vez. Existem |QUEUESIZE| clientes antes de você.'
}
}
-- Creates a new NpcHandler with an empty callbackFunction stack.
function NpcHandler:new(keywordHandler)
local obj = {}
obj.callbackFunctions = {}
obj.modules = {}
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
obj.focuses = {}
obj.talkStart = {}
else
obj.queue = Queue:new(obj)
obj.focuses = 0
obj.talkStart = 0
end
obj.talkDelay = {}
obj.keywordHandler = keywordHandler
obj.messages = {}
obj.shopItems = {}
setmetatable(obj.messages, self.messages)
self.messages.__index = self.messages
setmetatable(obj, self)
self.__index = self
return obj
end
-- Re-defines the maximum idle time allowed for a player when talking to this npc.
function NpcHandler:setMaxIdleTime(newTime)
self.idleTime = newTime
end
-- Attackes a new keyword handler to this npchandler
function NpcHandler:setKeywordHandler(newHandler)
self.keywordHandler = newHandler
end
-- Function used to change the focus of this npc.
function NpcHandler:addFocus(newFocus)
if(not isCreature(newFocus)) then
return
end
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
if(self:isFocused(newFocus, true)) then
return
end
table.insert(self.focuses, newFocus)
else
self.focuses = newFocus
end
self:updateFocus(true)
end
NpcHandler.changeFocus = NpcHandler.addFocus -- "changeFocus" looks better for CONVERSATION_DEFAULT
-- Function used to verify if npc is focused to certain player
function NpcHandler:isFocused(focus, creatureCheck)
local creatureCheck = creatureCheck or false
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
for k, v in pairs(self.focuses) do
if(v == focus) then
if(creatureCheck or isCreature(v)) then
return true
end
self:unsetFocus(focus, k)
return false
end
end
return false
end
if(creatureCheck or isCreature(self.focuses)) then
return self.focuses == focus
end
self:changeFocus(0)
return false
end
-- This function should be called on each onThink and makes sure the npc faces the player it is talking to.
-- Should also be called whenever a new player is focused.
function NpcHandler:updateFocus(creatureCheck)
local creatureCheck = creatureCheck or false
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
for _, focus in pairs(self.focuses) do
if(creatureCheck or isCreature(focus)) then
doNpcSetCreatureFocus(focus)
return
end
end
elseif(creatureCheck or isCreature(self.focuses)) then
doNpcSetCreatureFocus(self.focuses)
return
end
doNpcSetCreatureFocus(0)
end
-- Used when the npc should un-focus the player.
function NpcHandler:releaseFocus(focus)
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
if(not self:isFocused(focus)) then
return
end
local pos = nil
for k, v in pairs(self.focuses) do
if(v == focus) then
pos = k
end
end
if(pos ~= nil) then
closeShopWindow(focus)
self:unsetFocus(focus, pos)
end
elseif(self.focuses == focus) then
if(isCreature(focus)) then
closeShopWindow(focus)
end
self:changeFocus(0)
end
end
-- Internal un-focusing function, beware using!
function NpcHandler:unsetFocus(focus, pos)
if(type(self.focuses) ~= "table" or pos == nil or self.focuses[pos] == nil) then
return
end
table.remove(self.focuses, pos)
self.talkStart[focus] = nil
self:updateFocus()
end
-- Returns the callback function with the specified id or nil if no such callback function exists.
function NpcHandler:getCallback(id)
local ret = nil
if(self.callbackFunctions ~= nil) then
ret = self.callbackFunctions[id]
end
return ret
end
-- Changes the callback function for the given id to callback.
function NpcHandler:setCallback(id, callback)
if(self.callbackFunctions ~= nil) then
self.callbackFunctions[id] = callback
end
end
-- Adds a module to this npchandler and inits it.
function NpcHandler:addModule(module)
if(self.modules == nil or module == nil) then
return false
end
module:init(self)
if(module.parseParameters ~= nil) then
module:parseParameters()
end
table.insert(self.modules, module)
return true
end
-- Calls the callback function represented by id for all modules added to this npchandler with the given arguments.
function NpcHandler:processModuleCallback(id, ...)
local ret = true
for _, module in pairs(self.modules) do
local tmpRet = true
if(id == CALLBACK_CREATURE_APPEAR and module.callbackOnCreatureAppear ~= nil) then
tmpRet = module:callbackOnCreatureAppear(...)
elseif(id == CALLBACK_CREATURE_DISAPPEAR and module.callbackOnCreatureDisappear ~= nil) then
tmpRet = module:callbackOnCreatureDisappear(...)
elseif(id == CALLBACK_CREATURE_SAY and module.callbackOnCreatureSay ~= nil) then
tmpRet = module:callbackOnCreatureSay(...)
elseif(id == CALLBACK_PLAYER_ENDTRADE and module.callbackOnPlayerEndTrade ~= nil) then
tmpRet = module:callbackOnPlayerEndTrade(...)
elseif(id == CALLBACK_PLAYER_CLOSECHANNEL and module.callbackOnPlayerCloseChannel ~= nil) then
tmpRet = module:callbackOnPlayerCloseChannel(...)
elseif(id == CALLBACK_ONBUY and module.callbackOnBuy ~= nil) then
tmpRet = module:callbackOnBuy(...)
elseif(id == CALLBACK_ONSELL and module.callbackOnSell ~= nil) then
tmpRet = module:callbackOnSell(...)
elseif(id == CALLBACK_ONTHINK and module.callbackOnThink ~= nil) then
tmpRet = module:callbackOnThink(...)
elseif(id == CALLBACK_GREET and module.callbackOnGreet ~= nil) then
tmpRet = module:callbackOnGreet(...)
elseif(id == CALLBACK_FAREWELL and module.callbackOnFarewell ~= nil) then
tmpRet = module:callbackOnFarewell(...)
elseif(id == CALLBACK_MESSAGE_DEFAULT and module.callbackOnMessageDefault ~= nil) then
tmpRet = module:callbackOnMessageDefault(...)
elseif(id == CALLBACK_MODULE_RESET and module.callbackOnModuleReset ~= nil) then
tmpRet = module:callbackOnModuleReset(...)
end
if(not tmpRet) then
ret = false
break
end
end
return ret
end
-- Returns the message represented by id.
function NpcHandler:getMessage(id)
local ret = nil
if(self.messages ~= nil) then
ret = self.messages[id]
end
return ret
end
-- Changes the default response message with the specified id to newMessage.
function NpcHandler:setMessage(id, newMessage)
if(self.messages ~= nil) then
self.messages[id] = newMessage
end
end
-- Translates all message tags found in msg using parseInfo
function NpcHandler:parseMessage(msg, parseInfo)
for search, replace in pairs(parseInfo) do
if(replace ~= nil) then
msg = msg:gsub(search, replace)
end
end
return msg
end
-- Makes sure the npc un-focuses the currently focused player
function NpcHandler:unGreet(cid)
if(not self:isFocused(cid)) then
return
end
local callback = self:getCallback(CALLBACK_FAREWELL)
if(callback == nil or callback(cid)) then
if(self:processModuleCallback(CALLBACK_FAREWELL)) then
if(self.queue == nil or not self.queue:greetNext()) then
local msg = self:getMessage(MESSAGE_FAREWELL)
local parseInfo = { [TAG_PLAYERNAME] = getPlayerName(cid) }
msg = self:parseMessage(msg, parseInfo)
self:say(msg, cid, 0, true)
self:releaseFocus(cid)
if(not isPlayerGhost(cid)) then
self:say(msg)
end
end
end
end
end
-- Greets a new player.
function NpcHandler:greet(cid)
local callback = self:getCallback(CALLBACK_GREET)
if(callback == nil or callback(cid)) then
if(self:processModuleCallback(CALLBACK_GREET, cid)) then
local msg = self:getMessage(MESSAGE_GREET)
local parseInfo = { [TAG_PLAYERNAME] = getCreatureName(cid) }
msg = self:parseMessage(msg, parseInfo)
if(not isPlayerGhost(cid)) then
self:say(msg)
end
self:addFocus(cid)
self:say(msg, cid)
end
end
end
-- Handles onCreatureAppear events. If you with to handle this yourself, please use the CALLBACK_CREATURE_APPEAR callback.
function NpcHandler:onCreatureAppear(cid)
local callback = self:getCallback(CALLBACK_CREATURE_APPEAR)
if(callback == nil or callback(cid)) then
if(self:processModuleCallback(CALLBACK_CREATURE_APPEAR, cid)) then
--
end
end
end
-- Handles onCreatureDisappear events. If you with to handle this yourself, please use the CALLBACK_CREATURE_DISAPPEAR callback.
function NpcHandler:onCreatureDisappear(cid)
local callback = self:getCallback(CALLBACK_CREATURE_DISAPPEAR)
if(callback == nil or callback(cid)) then
if(self:processModuleCallback(CALLBACK_CREATURE_DISAPPEAR, cid)) then
if(self:isFocused(cid)) then
self:unGreet(cid)
end
end
end
end
-- Handles onCreatureSay events. If you with to handle this yourself, please use the CALLBACK_CREATURE_SAY callback.
function NpcHandler:onCreatureSay(cid, class, msg)
local callback = self:getCallback(CALLBACK_CREATURE_SAY)
if(callback == nil or callback(cid, class, msg)) then
if(self:processModuleCallback(CALLBACK_CREATURE_SAY, cid, class, msg)) then
if(not self:isInRange(cid)) then
return
end
if(self.keywordHandler ~= nil) then
if((self:isFocused(cid) and (class == TALKTYPE_PRIVATE_PN or NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT)) or not self:isFocused(cid)) then
local ret = self.keywordHandler:processMessage(cid, msg)
if(not ret) then
local callback = self:getCallback(CALLBACK_MESSAGE_DEFAULT)
if(callback ~= nil and callback(cid, class, msg)) then
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
self.talkStart[cid] = os.time()
else
self.talkStart = os.time()
end
end
elseif(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
self.talkStart[cid] = os.time()
else
self.talkStart = os.time()
end
end
end
end
end
end
-- Handles onPlayerEndTrade events. If you wish to handle this yourself, use the CALLBACK_PLAYER_ENDTRADE callback.
function NpcHandler:onPlayerEndTrade(cid)
local callback = self:getCallback(CALLBACK_PLAYER_ENDTRADE)
if(callback == nil or callback(cid)) then
if(self:processModuleCallback(CALLBACK_PLAYER_ENDTRADE, cid)) then
if(self:isFocused(cid)) then
local parseInfo = { [TAG_PLAYERNAME] = getPlayerName(cid) }
local msg = self:parseMessage(self:getMessage(MESSAGE_ONCLOSESHOP), parseInfo)
self:say(msg, cid)
end
end
end
end
-- Handles onPlayerCloseChannel events. If you wish to handle this yourself, use the CALLBACK_PLAYER_CLOSECHANNEL callback.
function NpcHandler:onPlayerCloseChannel(cid)
local callback = self:getCallback(CALLBACK_PLAYER_CLOSECHANNEL)
if(callback == nil or callback(cid)) then
if(self:processModuleCallback(CALLBACK_PLAYER_CLOSECHANNEL, cid)) then
if(self:isFocused(cid)) then
self:unGreet(cid)
end
end
end
end
-- Handles onBuy events. If you wish to handle this yourself, use the CALLBACK_ONBUY callback.
function NpcHandler:onBuy(cid, itemid, subType, amount, ignoreCap, inBackpacks)
local callback = self:getCallback(CALLBACK_ONBUY)
if(callback == nil or callback(cid, itemid, subType, amount, ignoreCap, inBackpacks)) then
if(self:processModuleCallback(CALLBACK_ONBUY, cid, itemid, subType, amount, ignoreCap, inBackpacks)) then
--
end
end
end
-- Handles onSell events. If you wish to handle this yourself, use the CALLBACK_ONSELL callback.
function NpcHandler:onSell(cid, itemid, subType, amount, ignoreCap, inBackpacks)
local callback = self:getCallback(CALLBACK_ONSELL)
if(callback == nil or callback(cid, itemid, subType, amount, ignoreCap, inBackpacks)) then
if(self:processModuleCallback(CALLBACK_ONSELL, cid, itemid, subType, amount, ignoreCap, inBackpacks)) then
--
end
end
end
-- Handles onThink events. If you wish to handle this yourself, please use the CALLBACK_ONTHINK callback.
function NpcHandler:onThink()
local callback = self:getCallback(CALLBACK_ONTHINK)
if(callback == nil or callback()) then
for i, speech in pairs(self.talkDelay) do
if((speech.cid == nil or speech.cid == 0) and speech.time ~= nil and speech.message ~= nil) then
if(os.mtime() >= speech.time) then
selfSay(speech.message)
self.talkDelay = nil
end
elseif(isCreature(speech.cid) and speech.start ~= nil and speech.time ~= nil and speech.message ~= nil) then
if(os.mtime() >= speech.time) then
local talkStart = (NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT and self.talkStart[speech.cid] or self.talkStart)
if(speech.force or (self:isFocused(speech.cid) and talkStart == speech.start)) then
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
selfSay(speech.message, speech.cid)
else
selfSay(speech.message)
end
end
self.talkDelay = nil
end
else
self.talkDelay = nil
end
end
if(self:processModuleCallback(CALLBACK_ONTHINK)) then
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
for _, focus in pairs(self.focuses) do
if(focus ~= nil) then
if(not self:isInRange(focus)) then
self:onWalkAway(focus)
elseif((os.time() - self.talkStart[focus]) > self.idleTime) then
self:unGreet(focus)
else
self:updateFocus()
end
end
end
elseif(self.focuses ~= 0) then
if(not self:isInRange(self.focuses)) then
self:onWalkAway(self.focuses)
elseif((os.time() - self.talkStart) > self.idleTime) then
self:unGreet(self.focuses)
else
self:updateFocus()
end
end
end
end
end
-- Tries to greet the player with the given cid.
function NpcHandler:onGreet(cid)
if(self:isInRange(cid)) then
if(NPCHANDLER_CONVBEHAVIOR == CONVERSATION_PRIVATE) then
if(not self:isFocused(cid)) then
self:greet(cid)
return
end
elseif(NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT) then
if(self.focuses == 0) then
self:greet(cid)
elseif(self.focuses == cid) then
local msg = self:getMessage(MESSAGE_ALREADYFOCUSED)
local parseInfo = { [TAG_PLAYERNAME] = getCreatureName(cid) }
msg = self:parseMessage(msg, parseInfo)
self:say(msg)
else
if(not self.queue:isInQueue(cid)) then
self.queue:push(cid)
end
local msg = self:getMessage(MESSAGE_PLACEDINQUEUE)
local parseInfo = { [TAG_PLAYERNAME] = getCreatureName(cid), [TAG_QUEUESIZE] = self.queue:getSize() }
msg = self:parseMessage(msg, parseInfo)
self:say(msg)
end
end
end
end
-- Simply calls the underlying unGreet function.
function NpcHandler:onFarewell(cid)
self:unGreet(cid)
end
-- Should be called on this npc's focus if the distance to focus is greater then talkRadius.
function NpcHandler:onWalkAway(cid)
if(self:isFocused(cid)) then
local callback = self:getCallback(CALLBACK_CREATURE_DISAPPEAR)
if(callback == nil or callback(cid)) then
if(self:processModuleCallback(CALLBACK_CREATURE_DISAPPEAR, cid)) then
if(self.queue == nil or not self.queue:greetNext()) then
local msg = self:getMessage(MESSAGE_WALKAWAY)
local parseInfo = { [TAG_PLAYERNAME] = getPlayerName(cid) or -1 }
msg = self:parseMessage(msg, parseInfo)
self:say(msg, cid, 0, true)
self:releaseFocus(cid)
self:say(msg)
end
end
end
end
end
-- Returns true if cid is within the talkRadius of this npc.
function NpcHandler:isInRange(cid)
if not isPlayer(cid) then
return false
end
local distance = getNpcDistanceTo(cid) or -1
return distance ~= -1 and distance <= self.talkRadius
end
-- Resets the npc into it's initial state (in regard of the keyrodhandler).
-- All modules are also receiving a reset call through their callbackOnModuleReset function.
function NpcHandler:resetNpc()
if(self:processModuleCallback(CALLBACK_MODULE_RESET)) then
self.keywordHandler:reset()
end
end
-- Makes the npc represented by this instance of NpcHandler say something.
-- This implements the currently set type of talkdelay.
function NpcHandler:say(message, focus, delay, force)
local delay = delay or 0
if(NPCHANDLER_TALKDELAY == TALKDELAY_NONE or delay <= 0) then
if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
selfSay(message, focus)
else
selfSay(message)
end
return
end
-- TODO: Add an event handling method for delayed messages
table.insert(self.talkDelay, {
id = getNpcId(),
cid = focus,
message = message,
time = os.mtime() + (delay and delay or self.talkDelayTime),
start = os.time(),
force = force or false
})
end
end
desculpe a demora,mais esse ai deu o msm erro



info
Grupo: Cavaleiro
Registrado: 01/04/13
Posts: 156
Reputação: +26
Gênero: Masculino
Char no Tibia: Yugami
pessoal eu to com outro problema aqui que não consigo resolve e não acho nem um Topico ou tutorial ajudando a resolver isso,queria sabe o seguinte no NPC eu consigo cria tudo normal... mais na hr q eu vou comprar ou vender Itens novos ele não vende e nem compra é como se eles não responde-se,e se eu coloco algo q ja tenha tipo bag 1999,1998 as bags,itens de tibia,rings... ele vende e compra normal,e quando eu fasso por .lua a venda e troca da esse erro
[Error - Npc interface]
(Unknown script file)
Description:
data/npc/lib/npcsystem/modules.lua:1151: bad argument #2 to 'error' (number expe
cted, got string)
stack traceback:
[C]: in function 'error'
data/npc/lib/npcsystem/modules.lua:1151: in function 'callbackOnBuy'
data/npc/lib/npcsystem/npchandler.lua:263: in function 'processModuleCal
lback'
data/npc/lib/npcsystem/npchandler.lua:440: in function 'onBuy'
data/npc/lib/npcsystem/modules.lua:1292: in function <data/npc/lib/npcsy
stem/modules.lua:1291>
eu ja trokei a pasta do OT(lib da "data",a lib do "npc",e não para de dar esse erro