Advertisement
Guest User

npchandles

a guest
Feb 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.54 KB | None | 0 0
  1. -- Advanced NPC System (Created by Jiddo),
  2. -- Modified by Talaturen.
  3.  
  4. if(NpcHandler == nil) then
  5. -- Constant talkdelay behaviors.
  6. TALKDELAY_NONE = 0 -- No talkdelay. Npc will reply immedeatly.
  7. TALKDELAY_ONTHINK = 1 -- Talkdelay handled through the onThink callback function. (Default)
  8. TALKDELAY_EVENT = 2 -- Not yet implemented
  9.  
  10. -- Currently applied talkdelay behavior. TALKDELAY_ONTHINK is default.
  11. NPCHANDLER_TALKDELAY = TALKDELAY_ONTHINK
  12.  
  13. -- Constant conversation behaviors.
  14. CONVERSATION_DEFAULT = 0 -- Conversation through default window, like it was before 8.2 update.
  15. CONVERSATION_PRIVATE = 1 -- Conversation through NPCs chat window, as of 8.2 update. (Default)
  16. --Small Note: Private conversations also means the NPC will use multi-focus system.
  17.  
  18. -- Currently applied conversation behavior. CONVERSATION_PRIVATE is default.
  19. NPCHANDLER_CONVBEHAVIOR = CONVERSATION_PRIVATE
  20.  
  21. -- Constant indexes for defining default messages.
  22. MESSAGE_GREET = 1 -- When the player greets the npc.
  23. MESSAGE_FAREWELL = 2 -- When the player unGreets the npc.
  24. MESSAGE_BUY = 3 -- When the npc asks the player if he wants to buy something.
  25. MESSAGE_ONBUY = 4 -- When the player successfully buys something via talk.
  26. MESSAGE_BOUGHT = 5 -- When the player bought something through the shop window.
  27. MESSAGE_SELL = 6 -- When the npc asks the player if he wants to sell something.
  28. MESSAGE_ONSELL = 7 -- When the player successfully sells something via talk.
  29. MESSAGE_SOLD = 8 -- When the player sold something through the shop window.
  30. MESSAGE_MISSINGMONEY = 9 -- When the player does not have enough money.
  31. MESSAGE_NEEDMONEY = 10 -- Same as above, used for shop window.
  32. MESSAGE_MISSINGITEM = 11 -- When the player is trying to sell an item he does not have.
  33. MESSAGE_NEEDITEM = 12 -- Same as above, used for shop window.
  34. MESSAGE_NEEDSPACE = 13 -- When the player don't have any space to buy an item
  35. MESSAGE_NEEDMORESPACE = 14 -- When the player has some space to buy an item, but not enough space
  36. MESSAGE_IDLETIMEOUT = 15 -- When the player has been idle for longer then idleTime allows.
  37. MESSAGE_WALKAWAY = 16 -- When the player walks out of the talkRadius of the npc.
  38. MESSAGE_DECLINE = 17 -- When the player says no to something.
  39. MESSAGE_SENDTRADE = 18 -- When the npc sends the trade window to the player
  40. MESSAGE_NOSHOP = 19 -- When the npc's shop is requested but he doesn't have any
  41. MESSAGE_ONCLOSESHOP = 20 -- When the player closes the npc's shop window
  42. MESSAGE_ALREADYFOCUSED = 21 -- When the player already has the focus of this npc.
  43. MESSAGE_PLACEDINQUEUE = 22 -- When the player has been placed in the costumer queue.
  44.  
  45. -- Constant indexes for callback functions. These are also used for module callback ids.
  46. CALLBACK_CREATURE_APPEAR = 1
  47. CALLBACK_CREATURE_DISAPPEAR = 2
  48. CALLBACK_CREATURE_SAY = 3
  49. CALLBACK_ONTHINK = 4
  50. CALLBACK_GREET = 5
  51. CALLBACK_FAREWELL = 6
  52. CALLBACK_MESSAGE_DEFAULT = 7
  53. CALLBACK_PLAYER_ENDTRADE = 8
  54. CALLBACK_PLAYER_CLOSECHANNEL = 9
  55. CALLBACK_ONBUY = 10
  56. CALLBACK_ONSELL = 11
  57.  
  58. -- Addidional module callback ids
  59. CALLBACK_MODULE_INIT = 12
  60. CALLBACK_MODULE_RESET = 13
  61.  
  62. -- Constant strings defining the keywords to replace in the default messages.
  63. TAG_PLAYERNAME = '|PLAYERNAME|'
  64. TAG_ITEMCOUNT = '|ITEMCOUNT|'
  65. TAG_TOTALCOST = '|TOTALCOST|'
  66. TAG_ITEMNAME = '|ITEMNAME|'
  67. TAG_QUEUESIZE = '|QUEUESIZE|'
  68.  
  69. NpcHandler = {
  70. keywordHandler = nil,
  71. focuses = nil,
  72. talkStart = nil,
  73. idleTime = 90,
  74. talkRadius = 3,
  75. talkDelayTime = 5000, -- Seconds to delay outgoing messages.
  76. queue = nil,
  77. talkDelay = 5000,
  78. callbackFunctions = nil,
  79. modules = nil,
  80. shopItems = nil, -- They must be here since ShopModule uses "static" functions
  81. messages = {
  82. -- These are the default replies of all npcs. They can/should be changed individually for each npc.
  83. [MESSAGE_GREET] = 'Welcome, |PLAYERNAME|! I have been expecting you.',
  84. [MESSAGE_FAREWELL] = 'Good bye, |PLAYERNAME|!',
  85. [MESSAGE_BUY] = 'Do you want to buy |ITEMCOUNT| |ITEMNAME| for |TOTALCOST| gold coins?',
  86. [MESSAGE_ONBUY] = 'It was a pleasure doing business with you.',
  87. [MESSAGE_BOUGHT] = 'Bought |ITEMCOUNT|x |ITEMNAME| for |TOTALCOST| gold.',
  88. [MESSAGE_SELL] = 'Do you want to sell |ITEMCOUNT| |ITEMNAME| for |TOTALCOST| gold coins?',
  89. [MESSAGE_ONSELL] = 'Thank you for this |ITEMNAME|, |PLAYERNAME| gold.',
  90. [MESSAGE_SOLD] = 'Sold |ITEMCOUNT|x |ITEMNAME| for |TOTALCOST| gold.',
  91. [MESSAGE_MISSINGMONEY] = 'Sorry, you don\'t have enough money.',
  92. [MESSAGE_NEEDMONEY] = 'You do not have enough money.',
  93. [MESSAGE_MISSINGITEM] = 'You don\'t even have that item, |PLAYERNAME|!',
  94. [MESSAGE_NEEDITEM] = 'You do not have this object.',
  95. [MESSAGE_NEEDSPACE] = 'You do not have enough capacity.',
  96. [MESSAGE_NEEDMORESPACE] = 'You do not have enough capacity for all items.',
  97. [MESSAGE_IDLETIMEOUT] = 'Next, please!',
  98. [MESSAGE_WALKAWAY] = 'How rude!',
  99. [MESSAGE_DECLINE] = 'Not good enough, is it... ?',
  100. [MESSAGE_SENDTRADE] = 'Here\'s my offer, |PLAYERNAME|. Don\'t you like it?',
  101. [MESSAGE_NOSHOP] = 'Sorry, I\'m not offering anything.',
  102. [MESSAGE_ONCLOSESHOP] = 'Thank you, come back when you want something more.',
  103. [MESSAGE_ALREADYFOCUSED]= '',
  104. [MESSAGE_PLACEDINQUEUE] = '|PLAYERNAME|, please wait for your turn. There are |QUEUESIZE| customers before you.'
  105. }
  106. }
  107.  
  108. -- Creates a new NpcHandler with an empty callbackFunction stack.
  109. function NpcHandler:new(keywordHandler)
  110. local obj = {}
  111. obj.callbackFunctions = {}
  112. obj.modules = {}
  113. if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
  114. obj.focuses = {}
  115. obj.talkStart = {}
  116. else
  117. obj.queue = Queue:new(obj)
  118. obj.focuses = 0
  119. obj.talkStart = 0
  120. end
  121. obj.talkDelay = {}
  122. obj.keywordHandler = keywordHandler
  123. obj.messages = {}
  124. obj.shopItems = {}
  125.  
  126. setmetatable(obj.messages, self.messages)
  127. self.messages.__index = self.messages
  128.  
  129. setmetatable(obj, self)
  130. self.__index = self
  131. return obj
  132. end
  133.  
  134. -- Re-defines the maximum idle time allowed for a player when talking to this npc.
  135. function NpcHandler:setMaxIdleTime(newTime)
  136. self.idleTime = newTime
  137. end
  138.  
  139. -- Attackes a new keyword handler to this npchandler
  140. function NpcHandler:setKeywordHandler(newHandler)
  141. self.keywordHandler = newHandler
  142. end
  143.  
  144. -- Function used to change the focus of this npc.
  145. function NpcHandler:addFocus(newFocus)
  146. if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
  147. if(self:isFocused(newFocus)) then
  148. return
  149. end
  150.  
  151. table.insert(self.focuses, newFocus)
  152. else
  153. self.focuses = newFocus
  154. end
  155.  
  156. self:updateFocus()
  157. end
  158. NpcHandler.changeFocus = NpcHandler.addFocus --"changeFocus" looks better for CONVERSATION_DEFAULT
  159.  
  160. -- Function used to verify if npc is focused to certain player
  161. function NpcHandler:isFocused(focus)
  162. if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
  163. for k,v in pairs(self.focuses) do
  164. if v == focus then
  165. return true
  166. end
  167. end
  168.  
  169. return false
  170. end
  171.  
  172. return (self.focuses == focus)
  173. end
  174.  
  175. -- This function should be called on each onThink and makes sure the npc faces the player it is talking to.
  176. -- Should also be called whenever a new player is focused.
  177. function NpcHandler:updateFocus()
  178. if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
  179. for pos, focus in pairs(self.focuses) do
  180. if(focus ~= nil) then
  181. doNpcSetCreatureFocus(focus)
  182. return
  183. end
  184. end
  185.  
  186. doNpcSetCreatureFocus(0)
  187. else
  188. doNpcSetCreatureFocus(self.focuses)
  189. end
  190. end
  191.  
  192. -- Used when the npc should un-focus the player.
  193. function NpcHandler:releaseFocus(focus)
  194. if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
  195. if(not self:isFocused(focus)) then
  196. return
  197. end
  198.  
  199. local pos = nil
  200. for k,v in pairs(self.focuses) do
  201. if v == focus then
  202. pos = k
  203. end
  204. end
  205. table.remove(self.focuses, pos)
  206. self.talkStart[focus] = nil
  207. closeShopWindow(focus) --Even if it can not exist, we need to prevent it.
  208. self:updateFocus()
  209. else
  210. closeShopWindow(focus)
  211. self:changeFocus(0)
  212. end
  213. end
  214.  
  215. -- Returns the callback function with the specified id or nil if no such callback function exists.
  216. function NpcHandler:getCallback(id)
  217. local ret = nil
  218. if(self.callbackFunctions ~= nil) then
  219. ret = self.callbackFunctions[id]
  220. end
  221.  
  222. return ret
  223. end
  224.  
  225. -- Changes the callback function for the given id to callback.
  226. function NpcHandler:setCallback(id, callback)
  227. if(self.callbackFunctions ~= nil) then
  228. self.callbackFunctions[id] = callback
  229. end
  230. end
  231.  
  232. -- Adds a module to this npchandler and inits it.
  233. function NpcHandler:addModule(module)
  234. if(self.modules == nil or module == nil) then
  235. return false
  236. end
  237.  
  238. module:init(self)
  239. if(module.parseParameters ~= nil) then
  240. module:parseParameters()
  241. end
  242.  
  243. table.insert(self.modules, module)
  244. return true
  245. end
  246.  
  247. -- Calls the callback function represented by id for all modules added to this npchandler with the given arguments.
  248. function NpcHandler:processModuleCallback(id, ...)
  249. local ret = true
  250. for i, module in pairs(self.modules) do
  251. local tmpRet = true
  252. if(id == CALLBACK_CREATURE_APPEAR and module.callbackOnCreatureAppear ~= nil) then
  253. tmpRet = module:callbackOnCreatureAppear(unpack(arg))
  254. elseif(id == CALLBACK_CREATURE_DISAPPEAR and module.callbackOnCreatureDisappear ~= nil) then
  255. tmpRet = module:callbackOnCreatureDisappear(unpack(arg))
  256. elseif(id == CALLBACK_CREATURE_SAY and module.callbackOnCreatureSay ~= nil) then
  257. tmpRet = module:callbackOnCreatureSay(unpack(arg))
  258. elseif(id == CALLBACK_PLAYER_ENDTRADE and module.callbackOnPlayerEndTrade ~= nil) then
  259. tmpRet = module:callbackOnPlayerEndTrade(unpack(arg))
  260. elseif(id == CALLBACK_PLAYER_CLOSECHANNEL and module.callbackOnPlayerCloseChannel ~= nil) then
  261. tmpRet = module:callbackOnPlayerCloseChannel(unpack(arg))
  262. elseif(id == CALLBACK_ONBUY and module.callbackOnBuy ~= nil) then
  263. tmpRet = module:callbackOnBuy(unpack(arg))
  264. elseif(id == CALLBACK_ONSELL and module.callbackOnSell ~= nil) then
  265. tmpRet = module:callbackOnSell(unpack(arg))
  266. elseif(id == CALLBACK_ONTHINK and module.callbackOnThink ~= nil) then
  267. tmpRet = module:callbackOnThink(unpack(arg))
  268. elseif(id == CALLBACK_GREET and module.callbackOnGreet ~= nil) then
  269. tmpRet = module:callbackOnGreet(unpack(arg))
  270. elseif(id == CALLBACK_FAREWELL and module.callbackOnFarewell ~= nil) then
  271. tmpRet = module:callbackOnFarewell(unpack(arg))
  272. elseif(id == CALLBACK_MESSAGE_DEFAULT and module.callbackOnMessageDefault ~= nil) then
  273. tmpRet = module:callbackOnMessageDefault(unpack(arg))
  274. elseif(id == CALLBACK_MODULE_RESET and module.callbackOnModuleReset ~= nil) then
  275. tmpRet = module:callbackOnModuleReset(unpack(arg))
  276. end
  277.  
  278. if(not tmpRet) then
  279. ret = false
  280. break
  281. end
  282. end
  283.  
  284. return ret
  285. end
  286.  
  287. -- Returns the message represented by id.
  288. function NpcHandler:getMessage(id)
  289. local ret = nil
  290. if(self.messages ~= nil) then
  291. ret = self.messages[id]
  292. end
  293.  
  294. return ret
  295. end
  296.  
  297. -- Changes the default response message with the specified id to newMessage.
  298. function NpcHandler:setMessage(id, newMessage)
  299. if(self.messages ~= nil) then
  300. self.messages[id] = newMessage
  301. end
  302. end
  303.  
  304. -- Translates all message tags found in msg using parseInfo
  305. function NpcHandler:parseMessage(msg, parseInfo)
  306. local ret = msg
  307. for search, replace in pairs(parseInfo) do
  308. ret = string.gsub(ret, search, replace)
  309. end
  310.  
  311. return ret
  312. end
  313.  
  314. -- Makes sure the npc un-focuses the currently focused player
  315. function NpcHandler:unGreet(cid)
  316. if(not self:isFocused(cid)) then
  317. return
  318. end
  319.  
  320. local callback = self:getCallback(CALLBACK_FAREWELL)
  321. if(callback == nil or callback(cid)) then
  322. if(self:processModuleCallback(CALLBACK_FAREWELL)) then
  323. if(self.queue == nil or not self.queue:greetNext()) then
  324. local msg = self:getMessage(MESSAGE_FAREWELL)
  325. local parseInfo = { [TAG_PLAYERNAME] = getPlayerName(cid) }
  326. msg = self:parseMessage(msg, parseInfo)
  327.  
  328. self:releaseFocus(cid)
  329. self:say(msg)
  330. end
  331. end
  332. end
  333. end
  334.  
  335. -- Greets a new player.
  336. function NpcHandler:greet(cid)
  337. if(cid ~= 0) then
  338. local callback = self:getCallback(CALLBACK_GREET)
  339. if(callback == nil or callback(cid)) then
  340. if(self:processModuleCallback(CALLBACK_GREET, cid)) then
  341. local msg = self:getMessage(MESSAGE_GREET)
  342. local parseInfo = { [TAG_PLAYERNAME] = getCreatureName(cid) }
  343. msg = self:parseMessage(msg, parseInfo)
  344. if #msg >= 1 then
  345. selfSay(msg, cid)--alterado v1.4 agora a msg de HI sai no channel de npcs ja ^^
  346. --self:say(msg)
  347. else
  348. end
  349. self:addFocus(cid)
  350. end
  351. end
  352. end
  353. end
  354.  
  355. -- Handles onCreatureAppear events. If you with to handle this yourself, please use the CALLBACK_CREATURE_APPEAR callback.
  356. function NpcHandler:onCreatureAppear(cid)
  357. local callback = self:getCallback(CALLBACK_CREATURE_APPEAR)
  358. if(callback == nil or callback(cid)) then
  359. if(self:processModuleCallback(CALLBACK_CREATURE_APPEAR, cid)) then
  360. --
  361. end
  362. end
  363. end
  364.  
  365. -- Handles onCreatureDisappear events. If you with to handle this yourself, please use the CALLBACK_CREATURE_DISAPPEAR callback.
  366. function NpcHandler:onCreatureDisappear(cid)
  367. local callback = self:getCallback(CALLBACK_CREATURE_DISAPPEAR)
  368. if(callback == nil or callback(cid)) then
  369. if(self:processModuleCallback(CALLBACK_CREATURE_DISAPPEAR, cid)) then
  370. if(self:isFocused(cid)) then
  371. self:unGreet(cid)
  372. end
  373. end
  374. end
  375. end
  376.  
  377. -- Handles onCreatureSay events. If you with to handle this yourself, please use the CALLBACK_CREATURE_SAY callback.
  378. function NpcHandler:onCreatureSay(cid, class, msg)
  379. local callback = self:getCallback(CALLBACK_CREATURE_SAY)
  380. if(callback == nil or callback(cid, class, msg)) then
  381. if(self:processModuleCallback(CALLBACK_CREATURE_SAY, cid, class, msg)) then
  382. if(not self:isInRange(cid)) then
  383. return
  384. end
  385.  
  386. if(self.keywordHandler ~= nil) then
  387. if((self:isFocused(cid) and (class == TALKTYPE_PRIVATE_PN or NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT)) or not self:isFocused(cid)) then
  388. local ret = self.keywordHandler:processMessage(cid, msg)
  389. if(not ret) then
  390. local callback = self:getCallback(CALLBACK_MESSAGE_DEFAULT)
  391. if(callback ~= nil and callback(cid, class, msg)) then
  392. if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
  393. self.talkStart[cid] = os.time()
  394. else
  395. self.talkStart = os.time()
  396. end
  397. end
  398. else
  399. if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
  400. self.talkStart[cid] = os.time()
  401. else
  402. self.talkStart = os.time()
  403. end
  404. end
  405. end
  406. end
  407. end
  408. end
  409. end
  410.  
  411. -- Handles onPlayerEndTrade events. If you wish to handle this yourself, use the CALLBACK_PLAYER_ENDTRADE callback.
  412. function NpcHandler:onPlayerEndTrade(cid)
  413. local callback = self:getCallback(CALLBACK_PLAYER_ENDTRADE)
  414. if(callback == nil or callback(cid)) then
  415. if(self:processModuleCallback(CALLBACK_PLAYER_ENDTRADE, cid, class, msg)) then
  416. if(self:isFocused(cid)) then
  417. local parseInfo = { [TAG_PLAYERNAME] = getPlayerName(cid) }
  418. local msg = self:parseMessage(self:getMessage(MESSAGE_ONCLOSESHOP), parseInfo)
  419. self:say(msg, cid)
  420. end
  421. end
  422. end
  423. end
  424.  
  425. -- Handles onPlayerCloseChannel events. If you wish to handle this yourself, use the CALLBACK_PLAYER_CLOSECHANNEL callback.
  426. function NpcHandler:onPlayerCloseChannel(cid)
  427. local callback = self:getCallback(CALLBACK_PLAYER_CLOSECHANNEL)
  428. if(callback == nil or callback(cid)) then
  429. if(self:processModuleCallback(CALLBACK_PLAYER_CLOSECHANNEL, cid, class, msg)) then
  430. if(self:isFocused(cid)) then
  431. self:unGreet(cid)
  432. end
  433. end
  434. end
  435. end
  436.  
  437. -- Handles onBuy events. If you wish to handle this yourself, use the CALLBACK_ONBUY callback.
  438. function NpcHandler:onBuy(cid, itemid, subType, amount, ignoreCap, inBackpacks)
  439. local callback = self:getCallback(CALLBACK_ONBUY)
  440. if(callback == nil or callback(cid, itemid, subType, amount, ignoreCap, inBackpacks)) then
  441. if(self:processModuleCallback(CALLBACK_ONBUY, cid, itemid, subType, amount, ignoreCap, inBackpacks)) then
  442. --
  443. end
  444. end
  445. end
  446.  
  447. -- Handles onSell events. If you wish to handle this yourself, use the CALLBACK_ONSELL callback.
  448. function NpcHandler:onSell(cid, itemid, subType, amount, ignoreCap, inBackpacks)
  449. local callback = self:getCallback(CALLBACK_ONSELL)
  450. if(callback == nil or callback(cid, itemid, subType, amount, ignoreCap, inBackpacks)) then
  451. if(self:processModuleCallback(CALLBACK_ONSELL, cid, itemid, subType, amount, ignoreCap, inBackpacks)) then
  452. --
  453. end
  454. end
  455. end
  456.  
  457. -- Handles onThink events. If you wish to handle this yourself, please use the CALLBACK_ONTHINK callback.
  458. function NpcHandler:onThink()
  459. local callback = self:getCallback(CALLBACK_ONTHINK)
  460. if(callback == nil or callback()) then
  461. if(NPCHANDLER_TALKDELAY == TALKDELAY_ONTHINK) then
  462. if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
  463. for cid, talkDelay in pairs(self.talkDelay) do
  464. if(talkDelay.time ~= nil and talkDelay.message ~= nil and os.time() >= talkDelay.time) then
  465. selfSay(talkDelay.message, cid)
  466. self.talkDelay[cid] = nil
  467. end
  468. end
  469. elseif(self.talkDelay.time ~= nil and self.talkDelay.message ~= nil and os.time() >= self.talkDelay.time) then
  470. selfSay(self.talkDelay.message)
  471. self.talkDelay.time = nil
  472. self.talkDelay.message = nil
  473. end
  474. end
  475.  
  476. if(self:processModuleCallback(CALLBACK_ONTHINK)) then
  477. if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
  478. for pos, focus in pairs(self.focuses) do
  479. if(focus ~= nil) then
  480. if(not self:isInRange(focus)) then
  481. self:onWalkAway(focus)
  482. elseif((os.time() - self.talkStart[focus]) > self.idleTime) then
  483. self:unGreet(focus)
  484. else
  485. self:updateFocus()
  486. end
  487. end
  488. end
  489. elseif(self.focuses ~= 0) then
  490. if(not self:isInRange(self.focuses)) then
  491. self:onWalkAway(self.focuses)
  492. elseif(os.time()-self.talkStart > self.idleTime) then
  493. self:unGreet(self.focuses)
  494. else
  495. self:updateFocus()
  496. end
  497. end
  498. end
  499. end
  500. end
  501.  
  502. -- Tries to greet the player with the given cid.
  503. function NpcHandler:onGreet(cid)
  504. if(self:isInRange(cid)) then
  505. if(NPCHANDLER_CONVBEHAVIOR == CONVERSATION_PRIVATE) then
  506. if(not self:isFocused(cid)) then
  507. self:greet(cid)
  508. return
  509. end
  510. elseif(NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT) then
  511. if(self.focuses == 0) then
  512. self:greet(cid)
  513. elseif(self.focuses == cid) then
  514. local msg = self:getMessage(MESSAGE_ALREADYFOCUSED)
  515. local parseInfo = { [TAG_PLAYERNAME] = getCreatureName(cid) }
  516. msg = self:parseMessage(msg, parseInfo)
  517. if #msg >= 1 then
  518. self:say(msg)
  519. else
  520. end
  521. else
  522. if(not self.queue:isInQueue(cid)) then
  523. self.queue:push(cid)
  524. end
  525.  
  526. local msg = self:getMessage(MESSAGE_PLACEDINQUEUE)
  527. local parseInfo = { [TAG_PLAYERNAME] = getCreatureName(cid), [TAG_QUEUESIZE] = self.queue:getSize() }
  528. msg = self:parseMessage(msg, parseInfo)
  529. self:say(msg)
  530. end
  531. end
  532. end
  533. end
  534.  
  535. -- Simply calls the underlying unGreet function.
  536. function NpcHandler:onFarewell(cid)
  537. self:unGreet(cid)
  538. end
  539.  
  540. -- Should be called on this npc's focus if the distance to focus is greater then talkRadius.
  541. function NpcHandler:onWalkAway(cid)
  542. if(self:isFocused(cid)) then
  543. local callback = self:getCallback(CALLBACK_CREATURE_DISAPPEAR)
  544. if(callback == nil or callback(cid)) then
  545. if(self:processModuleCallback(CALLBACK_CREATURE_DISAPPEAR, cid)) then
  546. if(self.queue == nil or not self.queue:greetNext()) then
  547. local msg = self:getMessage(MESSAGE_WALKAWAY)
  548. local parseInfo = { [TAG_PLAYERNAME] = getPlayerName(cid) }
  549. msg = self:parseMessage(msg, parseInfo)
  550.  
  551. self:releaseFocus(cid)
  552. self:say(msg)
  553. end
  554. end
  555. end
  556. end
  557. end
  558.  
  559. -- Returns true if cid is within the talkRadius of this npc.
  560. function NpcHandler:isInRange(cid)
  561. local distance = getDistanceTo(cid) or -1
  562. if(distance == -1) then
  563. return false
  564. end
  565.  
  566. return (distance <= self.talkRadius)
  567. end
  568.  
  569. -- Resets the npc into it's initial state (in regard of the keyrodhandler).
  570. -- All modules are also receiving a reset call through their callbackOnModuleReset function.
  571. function NpcHandler:resetNpc()
  572. if(self:processModuleCallback(CALLBACK_MODULE_RESET)) then
  573. self.keywordHandler:reset()
  574. end
  575. end
  576.  
  577. -- Makes the npc represented by this instance of NpcHandler say something.
  578. -- This implements the currently set type of talkdelay.
  579. -- shallDelay is a boolean value. If it is false, the message is not delayed. Default value is false.
  580. function NpcHandler:say(message, focus, shallDelay)
  581. local shallDelay = shallDelay or false
  582. if(NPCHANDLER_TALKDELAY == TALKDELAY_NONE or not shallDelay) then
  583. if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
  584. selfSay(message, focus)
  585. return
  586. else
  587. selfSay(message)
  588. return
  589. end
  590. end
  591.  
  592. -- TODO: Add an event handling method for delayed messages
  593. if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
  594. self.talkDelay[focus] = {
  595. message = message,
  596. time = os.time() + self.talkDelayTime,
  597. }
  598. else
  599. self.talkDelay = {
  600. message = message,
  601. time = os.time() + self.talkDelayTime
  602. }
  603. end
  604. end
  605. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement