Arkangel Nyx
Member
Error: [28/08/2012 16:49:08] Lua Script Error: [Npc interface]
[28/08/2012 16:49:08] data/npc/scripts/default.lua
[28/08/2012 16:49:08] data/npc/scripts/default.lua:1: attempt to index global 'KeywordHandler' (a nil value)
[28/08/2012 16:49:08] stack traceback:
[28/08/2012 16:49:08] [C]: in function '__index'
[28/08/2012 16:49:08] data/npc/scripts/default.lua:1: in main chunk
[28/08/2012 16:49:09] [Warning - NpcScript::NpcScript] Can not load script: data/npc/scripts/default.lua
[28/08/2012 16:49:09] cannot open default.lua: No such file or directory
It says this error over and over again, i am assuming for every npc i have. I am using Jiddos npc system which im sure you are all familiar with considering that it seems to be the npc system that is ran on all distributions. Now....if you think you can fix this error, by all means let me know what your idea is...i have placed all the scripts involved in the error below. However, after searching for a fix for almost 3 days now i am concluding that the error is being caused because i am trying to run Jiddos npc system with a server(or npc pack) that doesnt support it. Obviously going through and changing all 600 of them is a last resort option, but i was hoping otland could help me out...
1. Is there another npc system out there? If so, where can i download it?
2. Does anyone know of a full rl npc pack that can run using Jiddos system?
Right now these are the only two options i see as realistic. Thanks guys, here are the scripts...Rep++
Default.lua:
KeywordHandler:
npcsystem.lua:
npchandler.lua:
I don't think the last two scripts listed are relevant..but i figured it couldn't hurt to add them. If you made it this far in my thread and are still reading..i think another THANKS is in order . Well, do your thangg otland.
[28/08/2012 16:49:08] data/npc/scripts/default.lua
[28/08/2012 16:49:08] data/npc/scripts/default.lua:1: attempt to index global 'KeywordHandler' (a nil value)
[28/08/2012 16:49:08] stack traceback:
[28/08/2012 16:49:08] [C]: in function '__index'
[28/08/2012 16:49:08] data/npc/scripts/default.lua:1: in main chunk
[28/08/2012 16:49:09] [Warning - NpcScript::NpcScript] Can not load script: data/npc/scripts/default.lua
[28/08/2012 16:49:09] cannot open default.lua: No such file or directory
It says this error over and over again, i am assuming for every npc i have. I am using Jiddos npc system which im sure you are all familiar with considering that it seems to be the npc system that is ran on all distributions. Now....if you think you can fix this error, by all means let me know what your idea is...i have placed all the scripts involved in the error below. However, after searching for a fix for almost 3 days now i am concluding that the error is being caused because i am trying to run Jiddos npc system with a server(or npc pack) that doesnt support it. Obviously going through and changing all 600 of them is a last resort option, but i was hoping otland could help me out...
1. Is there another npc system out there? If so, where can i download it?
2. Does anyone know of a full rl npc pack that can run using Jiddos system?
Right now these are the only two options i see as realistic. Thanks guys, here are the scripts...Rep++
Default.lua:
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
function onCreatureAppear(cid) npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
function onThink() npcHandler:onThink() end
npcHandler:addModule(FocusModule:new())
KeywordHandler:
Lua:
-- Advanced NPC System (Created by Jiddo),
-- Modified by Talaturen.
if(KeywordHandler == nil) then
BEHAVIOR_SIMPLE = 1 -- Does not support nested keywords. If you choose this setting you must use a variable such as 'talkState' to keep track of how to handle keywords.
BEHAVIOR_NORMAL = 2 -- Default behvaior. If a sub-keyword is not found, then the root is searched, not the parent hierarchy,
BEHAVIOR_NORMAL_EXTENDED = 3 -- Same as BEHAVIOR_NORMAL but it also searches through the last node's parent.
BEHAVIOR_COMPLEX = 4 -- Extended behavior. It a sub-keyword is not found, then the entire keyword hierarchy is searched upwards until root is reached.
-- BEHAVIOR_NORMAL_EXTENDED is recommended as it (probably) mimics the behavior of real Tibia's NPCs the most.
-- However, you are strongly recommended to test some (or all) other settings as well as it might suit you better.
-- Also note that not much difference can be seen with the different settings unless you have a npc with a quite heavy
-- nestled keyword hierarchy.
-- Note: BEHAVIOR_SIMPLE should not be used unless you have any special reason to do so as it forces you to keep track of talkStates etc.
-- This was pretty much the method used in the 2.0x versions of this system. It is here mainly for compability issues.
KEYWORD_BEHAVIOR = BEHAVIOR_NORMAL_EXTENDED
KeywordNode = {
keywords = nil,
callback = nil,
parameters = nil,
children = nil,
parent = nil
}
-- Created a new keywordnode with the given keywords, callback function and parameters and without any childNodes.
function KeywordNode:new(keys, func, param)
local obj = {}
obj.keywords = keys
obj.callback = func
obj.parameters = param
obj.children = {}
setmetatable(obj, self)
self.__index = self
return obj
end
-- Calls the underlying callback function if it is not nil.
function KeywordNode:processMessage(cid, message)
return (self.callback == nil or self.callback(cid, message, self.keywords, self.parameters, self))
end
-- Returns true if message contains all patterns/strings found in keywords.
function KeywordNode:checkMessage(message)
local ret = true
if(self.keywords.callback ~= nil) then
return self.keywords.callback(self.keywords, message)
end
for i,v in ipairs(self.keywords) do
if(type(v) == 'string') then
local a, b = string.find(message, v)
if(a == nil or b == nil) then
ret = false
break
end
end
end
return ret
end
-- Returns the parent of this node or nil if no such node exists.
function KeywordNode:getParent()
return self.parent
end
-- Returns an array of the callback function parameters assosiated with this node.
function KeywordNode:getParameters()
return self.parameters
end
-- Returns an array of the triggering keywords assosiated with this node.
function KeywordNode:getKeywords()
return self.keywords
end
-- Adds a childNode to this node. Creates the childNode based on the parameters (k = keywords, c = callback, p = parameters)
function KeywordNode:addChildKeyword(keywords, callback, parameters)
local new = KeywordNode:new(keywords, callback, parameters)
return self:addChildKeywordNode(new)
end
-- Adds a pre-created childNode to this node. Should be used for example if several nodes should have a common child.
function KeywordNode:addChildKeywordNode(childNode)
table.insert(self.children, childNode)
childNode.parent = self
return childNode
end
KeywordHandler = {
root = nil,
lastNode = nil
}
-- Creates a new keywordhandler with an empty rootnode.
function KeywordHandler:new()
local obj = {}
obj.root = KeywordNode:new(nil, nil, nil)
setmetatable(obj, self)
self.__index = self
return obj
end
-- Resets the lastNode field, and this resetting the current position in the node hierarchy to root.
function KeywordHandler:reset()
self.lastNode = nil
end
-- Makes sure the correct childNode of lastNode gets a chance to process the message.
-- The behavior of this function depends much on the KEYWORD_BEHAVIOR.
function KeywordHandler:processMessage(cid, message)
local node = self:getLastNode()
if(node == nil) then
error('No root node found.')
return false
end
if(KEYWORD_BEHAVIOR == BEHAVIOR_SIMPLE) then
local ret = self:processNodeMessage(node, cid, message)
if(ret) then
return true
end
elseif(KEYWORD_BEHAVIOR == BEHAVIOR_NORMAL or KEYWORD_BEHAVIOR == BEHAVIOR_NORMAL_EXTENDED) then
local ret = self:processNodeMessage(node, cid, message)
if(ret) then
return true
end
if(KEYWORD_BEHAVIOR == BEHAVIOR_NORMAL_EXTENDED and node:getParent()) then
node = node:getParent() -- Search through the parent.
local ret = self:processNodeMessage(node, cid, message)
if(ret) then
return true
end
end
if(node ~= self:getRoot()) then
node = self:getRoot() -- Search through the root.
local ret = self:processNodeMessage(node, cid, message)
if(ret) then
return true
end
end
elseif(KEYWORD_BEHAVIOR == BEHAVIOR_COMPLEX) then
while true do
local ret = self:processNodeMessage(node, cid, message)
if(ret) then
return true
end
if(node:getParent() ~= nil) then
node = node:getParent() -- Move one step upwards in the hierarchy.
else
break
end
end
else
error('Unknown keyword behavior.')
end
return false
end
-- Tries to process the given message using the node parameter's children and calls the node's callback function if found.
-- Returns the childNode which processed the message or nil if no such node was found.
function KeywordHandler:processNodeMessage(node, cid, message)
local messageLower = string.lower(message)
for i, childNode in pairs(node.children) do
if(childNode:checkMessage(messageLower)) then
local oldLast = self.lastNode
self.lastNode = childNode
childNode.parent = node -- Make sure node is the parent of childNode (as one node can be parent to several nodes).
if(childNode:processMessage(cid, message)) then
return true
else
self.lastNode = oldLast
end
end
end
return false
end
-- Returns the root keywordnode
function KeywordHandler:getRoot()
return self.root
end
-- Returns the last processed keywordnode or root if no last node is found.
function KeywordHandler:getLastNode()
if(KEYWORD_BEHAVIOR == BEHAVIOR_SIMPLE) then
return self:getRoot()
else
return self.lastNode or self:getRoot()
end
end
-- Adds a new keyword to the root keywordnode. Returns the new node.
function KeywordHandler:addKeyword(keys, callback, parameters)
return self:getRoot():addChildKeyword(keys, callback, parameters)
end
-- Moves the current position in the keyword hierarchy count steps upwards. Count defalut value = 1.
-- This function MIGHT not work properly yet. Use at your own risk.
function KeywordHandler:moveUp(count)
local steps = count
if(steps == nil) then
steps = 1
end
for i = 1, steps,1 do
if(self.lastNode == nil) then
break
else
self.lastNode = self.lastNode:getParent() or self:getRoot()
end
end
return self.lastNode
end
end
npcsystem.lua:
Lua:
-- This file is part of Jiddo's advanced NpcSystem v3.0x. This npcsystem is free to use by anyone, for any purpuse.
-- Initial release date: 2007-02-21
-- Credits: Jiddo, honux(I'm using a modified version of his Find function).
-- Please include full credits whereever you use this system, or parts of it.
-- For support, questions and updates, please consult the following thread:
-- [url=http://otfans.net/showthread.php?t=67810][Release] Advanced NPC System v3.0a![/url]
if(NpcSystem == nil) then
-- Loads the underlying classes of the npcsystem.
dofile('data/npc/scripts/lib/npcsystem/keywordhandler.lua')
dofile('data/npc/scripts/lib/npcsystem/queue.lua')
dofile('data/npc/scripts/lib/npcsystem/npchandler.lua')
dofile('data/npc/scripts/lib/npcsystem/modules.lua')
-- Global npc constants:
-- Keyword nestling behavior. For more information look at the top of keywordhandler.lua
KEYWORD_BEHAVIOR = BEHAVIOR_NORMAL_EXTENDED
-- Gerrting and unGreeting keywords. For mor information look at the top of modules.lua
FOCUS_GREETWORDS = {'hi', 'hello'}
FOCUS_FAREWELLWORDS = {'bye', 'farewell', 'cya'}
-- The word for accepting/declining an offer. CAN ONLY CONTAIN ONE FIELD! For more information look at the top of modules.lua
SHOP_YESWORD = {'yes'}
SHOP_NOWORD = {'no'}
-- Pattern used to get the amount of an item a player wants to buy/sell.
PATTERN_COUNT = '%d+'
-- Talkdelay behavior. For mor information, look at the top of npchandler.lua.
NPCHANDLER_TALKDELAY = TALKDELAY_ONTHINK
-- Constant strings defining the keywords to replace in the default messages.
-- For more information, look at the top of npchandler.lua...
TAG_PLAYERNAME = '|PLAYERNAME|'
TAG_ITEMCOUNT = '|ITEMCOUNT|'
TAG_TOTALCOST = '|TOTALCOST|'
TAG_ITEMNAME = '|ITEMNAME|'
TAG_QUEUESIZE = '|QUEUESIZE|'
NpcSystem = {
--
}
-- Gets an npcparameter with the specified key. Returns nil if no such parameter is found.
function NpcSystem.getParameter(key)
local ret = getNpcParameter(tostring(key))
if((type(ret) == 'number' and ret == 0) or ret == nil) then
return nil
else
return ret
end
end
-- Parses all known parameters for the npc. Also parses parseable modules.
function NpcSystem.parseParameters(npcHandler)
local ret = NpcSystem.getParameter('idletime')
if(ret ~= nil) then
npcHandler.idleTime = tonumber(ret)
end
local ret = NpcSystem.getParameter('talkradius')
if(ret ~= nil) then
npcHandler.talkRadius = tonumber(ret)
end
local ret = NpcSystem.getParameter('talkdelaytime')
if(ret ~= nil) then
npcHandler.talkDelayTime = tonumber(ret)
end
local ret = NpcSystem.getParameter('message_greet')
if(ret ~= nil) then
npcHandler:setMessage(MESSAGE_GREET, ret)
end
local ret = NpcSystem.getParameter('message_farewell')
if(ret ~= nil) then
npcHandler:setMessage(MESSAGE_FAREWELL, ret)
end
local ret = NpcSystem.getParameter('message_buy')
if(ret ~= nil) then
npcHandler:setMessage(MESSAGE_BUY, ret)
end
local ret = NpcSystem.getParameter('message_sell')
if(ret ~= nil) then
npcHandler:setMessage(MESSAGE_SELL, ret)
end
local ret = NpcSystem.getParameter('message_onbuy')
if(ret ~= nil) then
npcHandler:setMessage(MESSAGE_ONBUY, ret)
end
local ret = NpcSystem.getParameter('message_onsell')
if(ret ~= nil) then
npcHandler:setMessage(MESSAGE_ONSELL, ret)
end
local ret = NpcSystem.getParameter('message_needmoremoney')
if(ret ~= nil) then
npcHandler:setMessage(MESSAGE_NEEDMOREMONEY, ret)
end
local ret = NpcSystem.getParameter('message_nothaveitem')
if(ret ~= nil) then
npcHandler:setMessage(MESSAGE_NOTHAVEITEM, ret)
end
local ret = NpcSystem.getParameter('message_idletimeout')
if(ret ~= nil) then
npcHandler:setMessage(MESSAGE_IDLETIMEOUT, ret)
end
local ret = NpcSystem.getParameter('message_walkaway')
if(ret ~= nil) then
npcHandler:setMessage(MESSAGE_WALKAWAY, ret)
end
local ret = NpcSystem.getParameter('message_alreadyfocused')
if(ret ~= nil) then
npcHandler:setMessage(MESSAGE_ALREADYFOCUSED, ret)
end
local ret = NpcSystem.getParameter('message_placedinqueue')
if(ret ~= nil) then
npcHandler:setMessage(MESSAGE_PLACEDINQUEUE, ret)
end
local ret = NpcSystem.getParameter('message_decline')
if(ret ~= nil) then
npcHandler:setMessage(MESSAGE_DECLINE, ret)
end
-- Parse modules.
for parameter, module in pairs(Modules.parseableModules) do
local ret = NpcSystem.getParameter(parameter)
if(ret ~= nil) then
local number = tonumber(ret)
if(number ~= 0 and module.parseParameters ~= nil) then
local instance = module:new()
npcHandler:addModule(instance)
instance:parseParameters()
end
end
end
end
end
npchandler.lua:
Lua:
-- This file is part of Jiddo's advanced NpcSystem v3.0x. This npcsystem is free to use by anyone, for any purpuse.
-- Initial release date: 2007-02-21
-- Credits: Jiddo, honux(I'm using a modified version of his Find function).
-- Please include full credits whereever you use this system, or parts of it.
-- For support, questions and updates, please consult the following thread:
-- [url=http://otfans.net/showthread.php?t=67810][Release] Advanced NPC System v3.0a![/url]
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 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_SELL = 4 -- When the npc asks the player if he wants to sell something.
MESSAGE_ONBUY = 5 -- When the player successfully buys something
MESSAGE_ONSELL = 6 -- When the player successfully sells something
MESSAGE_NEEDMOREMONEY = 7 -- When the player does not have enough money
MESSAGE_NOTHAVEITEM = 8 -- When the player is trying to sell an item he does not have.
MESSAGE_IDLETIMEOUT = 9 -- When the player has been idle for longer then idleTime allows.
MESSAGE_WALKAWAY = 10 -- When the player walks out of the talkRadius of the npc.
MESSAGE_ALREADYFOCUSED = 11 -- When the player already has the focus of this nopc.
MESSAGE_PLACEDINQUEUE = 12 -- When the player has been placed in the costumer queue.
MESSAGE_DECLINE = 13 -- When the player sais no to something.
-- 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
-- Addidional module callback ids
CALLBACK_MODULE_INIT = 10
CALLBACK_MODULE_RESET = 11
-- 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,
queue = nil,
focus = 0,
talkStart = 0,
idleTime = 30,
talkRadius = 5,
talkDelayTime = 1, -- Seconds to delay outgoing messages.
talkDelay = nil,
callbackFunctions = nil,
modules = nil,
messages = {
-- 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_SELL] = 'Do you want to sell |ITEMCOUNT| |ITEMNAME| for |TOTALCOST| gold coins?',
[MESSAGE_ONBUY] = 'It was a pleasure doing business with you.',
[MESSAGE_ONSELL] = 'Thank you for this item, |PLAYERNAME|.',
[MESSAGE_NEEDMOREMONEY] = 'You do not have enough money.',
[MESSAGE_NOTHAVEITEM] = 'You don\'t even have that item!',
[MESSAGE_IDLETIMEOUT] = 'Next please!',
[MESSAGE_WALKAWAY] = 'How rude!',
[MESSAGE_ALREADYFOCUSED]= '|PLAYERNAME|, I am already talking to you.',
[MESSAGE_PLACEDINQUEUE] = '|PLAYERNAME|, please wait for your turn. There are |QUEUESIZE| customers before you.',
[MESSAGE_DECLINE] = 'Not good enough, is it?'
}
}
-- Creates a new NpcHandler with an empty callbackFunction stack.
function NpcHandler:new(keywordHandler)
local obj = {}
obj.callbackFunctions = {}
obj.modules = {}
obj.talkDelay = {
message = nil,
time = nil
}
obj.queue = Queue:new(obj)
obj.keywordHandler = keywordHandler
obj.messages = {}
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
-- Attaches a new costumer queue to this npchandler.
function NpcHandler:setQueue(newQueue)
self.queue = newQueue
self.queue:setHandler(self)
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:changeFocus(newFocus)
self.focus = newFocus
self:updateFocus()
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()
doNpcSetCreatureFocus(self.focus)
end
-- Used when the npc should un-focus the player.
function NpcHandler:releaseFocus()
self:changeFocus(0)
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) then
table.insert(self.modules, module)
module:init(self)
end
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 i, module in pairs(self.modules) do
local tmpRet = true
if(id == CALLBACK_CREATURE_APPEAR and module.callbackOnCreatureAppear ~= nil) then
tmpRet = module:callbackCreatureAppear(unpack(arg))
elseif(id == CALLBACK_CREATURE_DISAPPEAR and module.callbackOnCreatureDisappear ~= nil) then
tmpRet = module:callbackCreatureDisappear(unpack(arg))
elseif(id == CALLBACK_CREATURE_SAY and module.callbackOnCreatureSay ~= nil) then
tmpRet = module:callbackCreatureSay(unpack(arg))
elseif(id == CALLBACK_ONTHINK and module.callbackOnThink ~= nil) then
tmpRet = module:callbackOnThink(unpack(arg))
elseif(id == CALLBACK_GREET and module.callbackOnGreet ~= nil) then
tmpRet = module:callbackOnGreet(unpack(arg))
elseif(id == CALLBACK_FAREWELL and module.callbackOnFarewell ~= nil) then
tmpRet = module:callbackOnFarewell(unpack(arg))
elseif(id == CALLBACK_MESSAGE_DEFAULT and module.callbackOnMessageDefault ~= nil) then
tmpRet = module:callbackOnMessageDefault(unpack(arg))
elseif(id == CALLBACK_MODULE_RESET and module.callbackOnModuleReset ~= nil) then
tmpRet = module:callbackOnModuleReset(unpack(arg))
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)
local ret = msg
for search, replace in pairs(parseInfo) do
ret = string.gsub(ret, search, replace)
end
return ret
end
-- Makes sure the npc un-focuses the furrently focused player, and greets the next player in the queue is it is not empty.
function NpcHandler:unGreet()
if(self.focus == 0) then
return
end
local callback = self:getCallback(CALLBACK_FAREWELL)
if(callback == nil or callback()) 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(self.focus) }
msg = self:parseMessage(msg, parseInfo)
self:say(msg)
self:releaseFocus()
end
end
end
end
-- Greets a new player.
function NpcHandler:greet(cid)
if(cid ~= 0) then
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] = getPlayerName(cid) }
msg = self:parseMessage(msg, parseInfo)
self:say(msg)
else
return
end
else
return
end
end
self:changeFocus(cid)
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.focus == cid) then
self:unGreet()
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, msgtype, msg)
local callback = self:getCallback(CALLBACK_CREATURE_SAY)
if(callback == nil or callback(cid, msgtype, msg)) then
if(self:processModuleCallback(CALLBACK_CREATURE_SAY, cid, msgtype, msg)) then
if(not self:isInRange(cid)) then
return
end
if(self.keywordHandler ~= nil) 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, msgtype, msg)) then
self.talkStart = os.time()
end
else
self.talkStart = os.time()
end
end
end
end
end
-- Handles onThink events. If you with 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
if(NPCHANDLER_TALKDELAY == TALKDELAY_ONTHINK and self.talkDelay.time ~= nil and self.talkDelay.message ~= nil and os.time() >= self.talkDelay.time) then
selfSay(self.talkDelay.message)
self.talkDelay.time = nil
self.talkDelay.message = nil
end
if(self:processModuleCallback(CALLBACK_ONTHINK)) then
if(self.focus ~= 0) then
if(not self:isInRange(self.focus)) then
self:onWalkAway(self.focus)
elseif(os.time()-self.talkStart > self.idleTime) then
self:unGreet()
else
self:updateFocus()
end
end
end
end
end
-- Tries to greet the player iwth the given cid. This function does not override queue order, current focus etc.
function NpcHandler:onGreet(cid)
if(self:isInRange(cid)) then
if(self.focus == 0) then
self:greet(cid)
elseif(cid == self.focus) then
local msg = self:getMessage(MESSAGE_ALREADYFOCUSED)
local parseInfo = { [TAG_PLAYERNAME] = getPlayerName(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] = getPlayerName(cid), [TAG_QUEUESIZE] = self.queue:getSize() }
msg = self:parseMessage(msg, parseInfo)
self:say(msg)
end
end
end
-- Simply calls the underlying unGreet function.
function NpcHandler:onFarewell()
self:unGreet()
end
-- Should be called on this npc's focus if the distance to focus is greater then talkRadius.
function NpcHandler:onWalkAway(cid)
if(cid == self.focus) then
local callback = self:getCallback(CALLBACK_CREATURE_DISAPPEAR)
if(callback == nil or callback()) 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(self.focus) }
msg = self:parseMessage(msg, parseInfo)
self:say(msg)
self:releaseFocus()
end
end
end
end
end
-- Returns true if cid is within the talkRadius of this npc.
function NpcHandler:isInRange(cid)
local playerPos = getPlayerPosition(cid)
if playerPos == LUA_ERROR or playerPos == LUA_NO_ERROR then
return false
end
local sx, sy, sz = selfGetPosition()
local dx = math.abs(sx-playerPos.x)
local dy = math.abs(sy-playerPos.y)
local dz = math.abs(sz-playerPos.z)
local dist = (dx^2 + dy^2)^0.5
return (dist <= self.talkRadius and dz == 0)
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.
-- shallDelay is a boolean value. If it is false, the message is not delayed. Default value is true.
function NpcHandler:say(message, shallDelay)
if(shallDelay == nil) then
shallDelay = true
end
if(NPCHANDLER_TALKDELAY == TALKDELAY_NONE or shallDelay == false) then
selfSay(message)
return
end
self.talkDelay.message = message
self.talkDelay.time = os.time()+self.talkDelayTime
end
end
I don't think the last two scripts listed are relevant..but i figured it couldn't hurt to add them. If you made it this far in my thread and are still reading..i think another THANKS is in order . Well, do your thangg otland.