Guest User

Untitled

a guest
Jun 19th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.79 KB | None | 0 0
  1. -- This file is part of Jiddo's advanced NpcSystem v3.0x. This npcsystem is free to use by anyone, for any purpuse.
  2. -- Initial release date: 2007-02-21
  3. -- Credits: Jiddo, honux(I'm using a modified version of his Find function).
  4. -- Please include full credits whereever you use this system, or parts of it.
  5. -- For support, questions and updates, please consult the following thread:
  6. -- http://otfans.net/showthread.php?t=67810
  7.  
  8. if(Modules == nil) then
  9.  
  10. -- default words for greeting and ungreeting the npc. Should be a talbe containing all such words.
  11. FOCUS_GREETWORDS = {'hi', 'hello'}
  12. FOCUS_FAREWELLWORDS = {'bye', 'farewell', 'cya'}
  13.  
  14. -- The word for accepting/declining an offer. CAN ONLY CONTAIN ONE FIELD! Should be a teble with a single string value.
  15. SHOP_YESWORD = {'yes'}
  16. SHOP_NOWORD = {'no'}
  17.  
  18. -- Pattern used to get the amount of an item a player wants to buy/sell.
  19. PATTERN_COUNT = '%d+'
  20.  
  21.  
  22. -- Constants used to separate buying from selling.
  23. SHOPMODULE_SELL_ITEM = 1
  24. SHOPMODULE_BUY_ITEM = 2
  25.  
  26.  
  27. Modules = {
  28. parseableModules = {}
  29. }
  30.  
  31.  
  32. StdModule = {
  33.  
  34. }
  35.  
  36. -- These callback function must be called with parameters.npcHandler = npcHandler in the parameters table or they will not work correctly.
  37. -- Notice: The members of StdModule have not yet been tested. If you find any bugs, please report them to me.
  38.  
  39. -- Usage:
  40. -- keywordHandler:addKeyword({'offer'}, StdModule.say, {npcHandler = npcHandler, text = 'I sell many powerful melee weapons.'})
  41. function StdModule.say(cid, message, keywords, parameters, node)
  42. local npcHandler = parameters.npcHandler
  43. if(npcHandler == nil) then
  44. error('StdModule.say called without any npcHandler instance.')
  45. end
  46. if(cid ~= npcHandler.focus and (parameters.onlyFocus == nil or parameters.onlyFocus == true)) then
  47. return false
  48. end
  49. local parseInfo = {
  50. [TAG_PLAYERNAME] = getPlayerName(cid),
  51. }
  52. msgout = npcHandler:parseMessage(parameters.text or parameters.message, parseInfo)
  53. npcHandler:say(msgout)
  54. if(parameters.reset == true) then
  55. npcHandler:resetNpc()
  56. elseif(parameters.moveup ~= nil and type(parameters.moveup) == 'number') then
  57. npcHandler.keywordHandler:moveUp(parameters.moveup)
  58. end
  59. return true
  60. end
  61.  
  62.  
  63. --Usage:
  64. -- local node1 = keywordHandler:addKeyword({'promot'}, StdModule.say, {npcHandler = npcHandler, text = 'I can promote you for 20000 gold coins. Do you want me to promote you?'})
  65. -- node1:addChildKeyword({'yes'}, StdModule.promotePlayer, {npcHandler = npcHandler, cost = 20000, level = 20}, text = 'Congratulations! You are now promoted.')
  66. -- node1:addChildKeyword({'no'}, StdModule.say, {npcHandler = npcHandler, text = 'Allright then. Come back when you are ready.'}, reset = true)
  67. function StdModule.promotePlayer(cid, message, keywords, parameters, node)
  68. local npcHandler = parameters.npcHandler
  69. if(npcHandler == nil) then
  70. error('StdModule.promotePlayer called without any npcHandler instance.')
  71. end
  72. if(cid ~= npcHandler.focus) then
  73. return false
  74. end
  75.  
  76. if(isPlayerPremiumCallback == nil or isPlayerPremiumCallback(cid) == true or parameters.premium == false) then
  77. local promotedVoc = getPromotedVocation(getPlayerVocation(cid))
  78. if(getPlayerStorageValue(cid, 30018) == TRUE) then
  79. npcHandler:say('You are already promoted!', cid)
  80. elseif(getPlayerLevel(cid) < parameters.level) then
  81. npcHandler:say('I am sorry, but I can only promote you once you have reached level ' .. parameters.level .. '.', cid)
  82. elseif(doPlayerRemoveMoney(cid, parameters.cost) ~= TRUE) then
  83. npcHandler:say('You do not have enough money!', cid)
  84. else
  85. doPlayerSetVocation(cid, promotedVoc)
  86. npcHandler:say(parameters.text, cid)
  87. end
  88. else
  89. npcHandler:say("You need a premium account in order to get promoted", cid)
  90. end
  91. npcHandler:resetNpc()
  92. return true
  93. end
  94.  
  95. function StdModule.learnSpell(cid, message, keywords, parameters, node)
  96. local npcHandler = parameters.npcHandler
  97. if(npcHandler == nil) then
  98. error('StdModule.buySpell called without any npcHandler instance.')
  99. end
  100. if(cid ~= npcHandler.focus) then
  101. return false
  102. end
  103.  
  104. if(isPlayerPremiumCallback == nil or isPlayerPremiumCallback(cid) == true or parameters.premium == false) then
  105. if getPlayerLearnedInstantSpell(cid, parameters.spellName) == TRUE then
  106. npcHandler:say('You already know this spell.', cid)
  107. elseif getPlayerLevel(cid) < parameters.level then
  108. npcHandler:say('You need to obtain a level of ' .. parameters.level .. ' or higher to be able to learn ' .. parameters.spellName .. '.', cid)
  109. elseif getPlayerVocation(cid) ~= parameters.vocation and getPlayerVocation(cid) ~= parameters.vocation + 4 and vocation ~= 9 then
  110. npcHandler:say('This spell is not for your vocation', cid)
  111. elseif doPlayerRemoveMoney(cid, parameters.price) == FALSE then
  112. npcHandler:say('You do not have enough money, this spell costs ' .. parameters.price .. ' gold.', cid)
  113. else
  114. npcHandler:say('You have learned ' .. parameters.spellName .. '.', cid)
  115. playerLearnInstantSpell(cid, parameters.spellName)
  116. end
  117. else
  118. npcHandler:say('You need a premium account in order to buy ' .. parameters.spellName .. '.', cid)
  119. end
  120. npcHandler:resetNpc()
  121. return true
  122. end
  123.  
  124. function StdModule.bless(cid, message, keywords, parameters, node)
  125. local npcHandler = parameters.npcHandler
  126. if(npcHandler == nil) then
  127. error('StdModule.bless called without any npcHandler instance.')
  128. end
  129.  
  130. if(cid ~= npcHandler.focus) then
  131. return false
  132. end
  133.  
  134. if(isPremium(cid) or isPlayerPremiumCallback == nil or isPlayerPremiumCallback(cid) == true or parameters.premium == false) then
  135. if getPlayerBlessing(cid, parameters.bless) then
  136. npcHandler:say("Gods have already blessed you with this blessing!", cid)
  137. elseif doPlayerRemoveMoney(cid, parameters.cost) == FALSE then
  138. npcHandler:say("You don't have enough money for blessing.", cid)
  139. else
  140. npcHandler:say("You have been blessed by one of the five gods!", cid)
  141. doPlayerAddBlessing(cid, parameters.bless)
  142. end
  143. else
  144. npcHandler:say('You need a premium account in order to be blessed.', cid)
  145. end
  146.  
  147. npcHandler:resetNpc()
  148. return true
  149. end
  150.  
  151.  
  152. function StdModule.travel(cid, message, keywords, parameters, node)
  153. local npcHandler = parameters.npcHandler
  154. if(npcHandler == nil) then
  155. error('StdModule.travel called without any npcHandler instance.')
  156. end
  157. if(cid ~= npcHandler.focus) then
  158. return false
  159. end
  160.  
  161. if(isPremium(cid) or isPlayerPremiumCallback == nil or isPlayerPremiumCallback(cid) == true or parameters.premium == false) then
  162. if(parameters.level ~= nil and getPlayerLevel(cid) < parameters.level) then
  163. npcHandler:say('You must reach level ' .. parameters.level .. ' before I can let you go there.')
  164. elseif(doPlayerRemoveMoney(cid, parameters.cost) ~= TRUE) then
  165. npcHandler:say('You do not have enough money!')
  166. else
  167. doTeleportThing(cid, parameters.destination)
  168. doSendMagicEffect(parameters.destination, 10)
  169. end
  170. else
  171. npcHandler:say('I can only allow premium players to travel with me.')
  172. end
  173.  
  174. npcHandler:resetNpc()
  175. return true
  176. end
  177.  
  178.  
  179. function StdModule.bless(cid, message, keywords, parameters, node)
  180. local npcHandler = parameters.npcHandler
  181. if(npcHandler == nil) then
  182. error('StdModule.bless called without any npcHandler instance.')
  183. end
  184.  
  185. if(cid ~= npcHandler.focus) then
  186. return false
  187. end
  188.  
  189. if(isPremium(cid) or isPlayerPremiumCallback == nil or isPlayerPremiumCallback(cid) == true or parameters.premium == false) then
  190. if getPlayerBlessing(cid, parameters.bless) then
  191. npcHandler:say("Gods have already blessed you with this blessing!", cid)
  192. elseif doPlayerRemoveMoney(cid, parameters.cost) == FALSE then
  193. npcHandler:say("You don't have enough money for blessing.", cid)
  194. else
  195. npcHandler:say("You have been blessed by one of the five gods!", cid)
  196. doPlayerAddBlessing(cid, parameters.bless)
  197. end
  198. else
  199. npcHandler:say('You need a premium account in order to be blessed.', cid)
  200. end
  201.  
  202. npcHandler:resetNpc()
  203. return true
  204. end
  205.  
  206.  
  207. FocusModule = {
  208. npcHandler = nil
  209. }
  210.  
  211. -- Creates a new instance of FocusModule without an associated NpcHandler.
  212. function FocusModule:new()
  213. local obj = {}
  214. setmetatable(obj, self)
  215. self.__index = self
  216. return obj
  217. end
  218.  
  219. -- Inits the module and associates handler to it.
  220. function FocusModule:init(handler)
  221. self.npcHandler = handler
  222. for i, word in pairs(FOCUS_GREETWORDS) do
  223. local obj = {}
  224. table.insert(obj, word)
  225. obj.callback = FOCUS_GREETWORDS.callback or FocusModule.messageMatcher
  226. handler.keywordHandler:addKeyword(obj, FocusModule.onGreet, {module = self})
  227. end
  228.  
  229. for i, word in pairs(FOCUS_FAREWELLWORDS) do
  230. local obj = {}
  231. table.insert(obj, word)
  232. obj.callback = FOCUS_FAREWELLWORDS.callback or FocusModule.messageMatcher
  233. handler.keywordHandler:addKeyword(obj, FocusModule.onFarewell, {module = self})
  234. end
  235.  
  236. return true
  237. end
  238.  
  239.  
  240. -- Greeting callback function.
  241. function FocusModule.onGreet(cid, message, keywords, parameters)
  242. parameters.module.npcHandler:onGreet(cid)
  243. return true
  244. end
  245.  
  246. -- UnGreeting callback function.
  247. function FocusModule.onFarewell(cid, message, keywords, parameters)
  248. if(parameters.module.npcHandler.focus == cid) then
  249. parameters.module.npcHandler:onFarewell()
  250. return true
  251. else
  252. return false
  253. end
  254. end
  255.  
  256. -- Custom message matching callback function for greeting messages.
  257. function FocusModule.messageMatcher(keywords, message)
  258. for i, word in pairs(keywords) do
  259. if(type(word) == 'string') then
  260. if string.find(message, word) and not string.find(message, '[%w+]' .. word) and not string.find(message, word .. '[%w+]') then
  261. return true
  262. end
  263. end
  264. end
  265. return false
  266. end
  267.  
  268.  
  269.  
  270. KeywordModule = {
  271. npcHandler = nil
  272. }
  273. -- Add it to the parseable module list.
  274. Modules.parseableModules['module_keywords'] = KeywordModule
  275.  
  276. function KeywordModule:new()
  277. local obj = {}
  278. setmetatable(obj, self)
  279. self.__index = self
  280. return obj
  281. end
  282.  
  283. function KeywordModule:init(handler)
  284. self.npcHandler = handler
  285. return true
  286. end
  287.  
  288. -- Parses all known parameters.
  289. function KeywordModule:parseParameters()
  290. local ret = NpcSystem.getParameter('keywords')
  291. if(ret ~= nil) then
  292. self:parseKeywords(ret)
  293. end
  294. end
  295.  
  296. function KeywordModule:parseKeywords(data)
  297. local n = 1
  298. for keys in string.gmatch(data, '[^;]+') do
  299. local i = 1
  300.  
  301. local keywords = {}
  302.  
  303. for temp in string.gmatch(keys, '[^,]+') do
  304. table.insert(keywords, temp)
  305. i = i+1
  306. end
  307.  
  308. if(i ~= 1) then
  309. local reply = NpcSystem.getParameter('keyword_reply' .. n)
  310. if(reply ~= nil) then
  311. self:addKeyword(keywords, reply)
  312. else
  313. print('[Warning] NpcSystem:', 'Parameter \'' .. 'keyword_reply' .. n .. '\' missing. Skipping...')
  314. end
  315. else
  316. print('[Warning] NpcSystem:', 'No keywords found for keyword set #' .. n .. '. Skipping...')
  317. end
  318. n = n+1
  319. end
  320. end
  321.  
  322. function KeywordModule:addKeyword(keywords, reply)
  323. self.npcHandler.keywordHandler:addKeyword(keywords, StdModule.say, {npcHandler = self.npcHandler, onlyFocus = true, text = reply, reset = true})
  324. end
  325.  
  326.  
  327.  
  328. TravelModule = {
  329. npcHandler = nil,
  330. destinations = nil,
  331. yesNode = nil,
  332. noNode = nil,
  333. }
  334. -- Add it to the parseable module list.
  335. Modules.parseableModules['module_travel'] = TravelModule
  336.  
  337. function TravelModule:new()
  338. local obj = {}
  339. setmetatable(obj, self)
  340. self.__index = self
  341. return obj
  342. end
  343.  
  344. function TravelModule:init(handler)
  345. self.npcHandler = handler
  346. self.yesNode = KeywordNode:new(SHOP_YESWORD, TravelModule.onConfirm, {module = self})
  347. self.noNode = KeywordNode:new(SHOP_NOWORD, TravelModule.onDecline, {module = self})
  348. self.destinations = {}
  349. return true
  350. end
  351.  
  352. -- Parses all known parameters.
  353. function TravelModule:parseParameters()
  354. local ret = NpcSystem.getParameter('travel_destinations')
  355. if(ret ~= nil) then
  356. self:parseDestinations(ret)
  357.  
  358. self.npcHandler.keywordHandler:addKeyword({'destination'}, TravelModule.listDestinations, {module = self})
  359. self.npcHandler.keywordHandler:addKeyword({'where'}, TravelModule.listDestinations, {module = self})
  360. self.npcHandler.keywordHandler:addKeyword({'travel'}, TravelModule.listDestinations, {module = self})
  361.  
  362. end
  363. end
  364.  
  365. function TravelModule:parseDestinations(data)
  366. for destination in string.gmatch(data, '[^;]+') do
  367. local i = 1
  368.  
  369. local name = nil
  370. local x = nil
  371. local y = nil
  372. local z = nil
  373. local cost = nil
  374. local premium = false
  375.  
  376.  
  377. for temp in string.gmatch(destination, '[^,]+') do
  378. if(i == 1) then
  379. name = temp
  380. elseif(i == 2) then
  381. x = tonumber(temp)
  382. elseif(i == 3) then
  383. y = tonumber(temp)
  384. elseif(i == 4) then
  385. z = tonumber(temp)
  386. elseif(i == 5) then
  387. cost = tonumber(temp)
  388. elseif(i == 6) then
  389. premium = temp == 'true'
  390. else
  391. print('[Warning] NpcSystem:', 'Unknown parameter found in travel destination parameter.', temp, destination)
  392. end
  393. i = i+1
  394. end
  395.  
  396. if(name ~= nil and x ~= nil and y ~= nil and z ~= nil and cost ~= nil) then
  397. self:addDestination(name, {x=x, y=y, z=z}, cost, premium)
  398. else
  399. print('[Warning] NpcSystem:', 'Parameter(s) missing for travel destination:', name, x, y, z, cost, premium)
  400. end
  401. end
  402. end
  403.  
  404. function TravelModule:addDestination(name, position, price, premium)
  405. table.insert(self.destinations, name)
  406.  
  407. local parameters = {
  408. cost = price,
  409. destination = position,
  410. premium = premium,
  411. module = self
  412. }
  413. local keywords = {}
  414. table.insert(keywords, name)
  415.  
  416. local keywords2 = {}
  417. table.insert(keywords2, 'bring me to ' .. name)
  418. local node = self.npcHandler.keywordHandler:addKeyword(keywords, TravelModule.travel, parameters)
  419. self.npcHandler.keywordHandler:addKeyword(keywords2, TravelModule.bringMeTo, parameters)
  420. node:addChildKeywordNode(self.yesNode)
  421. node:addChildKeywordNode(self.noNode)
  422. end
  423.  
  424. function TravelModule.travel(cid, message, keywords, parameters, node)
  425. local module = parameters.module
  426. if(cid ~= module.npcHandler.focus) then
  427. return false
  428. end
  429.  
  430. local npcHandler = module.npcHandler
  431.  
  432.  
  433. local cost = parameters.cost
  434. local destination = parameters.destination
  435. local premium = parameters.premium
  436.  
  437. module.npcHandler:say('Do you want to travel to ' .. keywords[1] .. ' for ' .. cost .. ' gold coins?')
  438. return true
  439.  
  440. end
  441.  
  442. function TravelModule.onConfirm(cid, message, keywords, parameters, node)
  443. local module = parameters.module
  444. if(cid ~= module.npcHandler.focus) then
  445. return false
  446. end
  447.  
  448. local npcHandler = module.npcHandler
  449.  
  450.  
  451. local parentParameters = node:getParent():getParameters()
  452. local cost = parentParameters.cost
  453. local destination = parentParameters.destination
  454. local premium = parentParameters.premium
  455.  
  456. if(isPlayerPremiumCallback == nil or isPlayerPremiumCallback(cid) == true or parameters.premium ~= true) then
  457. if(doPlayerRemoveMoney(cid, cost) ~= TRUE) then
  458. npcHandler:say('You do not have enough money!')
  459. else
  460. npcHandler:say('It was a pleasure doing business with you.', false)
  461. npcHandler:releaseFocus()
  462. doTeleportThing(cid, destination)
  463. doSendMagicEffect(destination, 10)
  464. end
  465. else
  466. npcHandler:say('I can only allow premium players to travel there.')
  467. end
  468.  
  469. npcHandler:resetNpc()
  470. return true
  471. end
  472.  
  473. -- onDecliune keyword callback function. Generally called when the player sais 'no' after wanting to buy an item.
  474. function TravelModule.onDecline(cid, message, keywords, parameters, node)
  475. local module = parameters.module
  476. if(cid ~= module.npcHandler.focus) then
  477. return false
  478. end
  479. local parentParameters = node:getParent():getParameters()
  480. local parseInfo = {
  481. [TAG_PLAYERNAME] = getPlayerName(cid),
  482. }
  483. local msg = module.npcHandler:parseMessage(module.npcHandler:getMessage(MESSAGE_DECLINE), parseInfo)
  484. module.npcHandler:say(msg)
  485. module.npcHandler:resetNpc()
  486. return true
  487. end
  488.  
  489. function TravelModule.bringMeTo(cid, message, keywords, parameters, node)
  490. local module = parameters.module
  491. if(cid == module.npcHandler.focus) then
  492. return false
  493. end
  494.  
  495. local cost = parameters.cost
  496. local destination = parameters.destination
  497. local premium = parameters.premium
  498.  
  499. if(isPlayerPremiumCallback == nil or isPlayerPremiumCallback(cid) == true or parameters.premium ~= true) then
  500. if(doPlayerRemoveMoney(cid, cost) == TRUE) then
  501. doTeleportThing(cid, destination)
  502. doSendMagicEffect(destination, 10)
  503. end
  504. end
  505.  
  506. return true
  507. end
  508.  
  509. function TravelModule.listDestinations(cid, message, keywords, parameters, node)
  510. local module = parameters.module
  511. if(cid ~= module.npcHandler.focus) then
  512. return false
  513. end
  514.  
  515. local msg = 'I can bring you to '
  516. --local i = 1
  517. local maxn = table.maxn(module.destinations)
  518. for i,destination in pairs(module.destinations) do
  519. msg = msg .. destination
  520. if(i == maxn-1) then
  521. msg = msg .. ' and '
  522. elseif(i == maxn) then
  523. msg = msg .. '.'
  524. else
  525. msg = msg .. ', '
  526. end
  527. i = i+1
  528. end
  529.  
  530. module.npcHandler:say(msg)
  531. module.npcHandler:resetNpc()
  532. return true
  533. end
  534.  
  535.  
  536.  
  537.  
  538. ShopModule = {
  539. yesNode = nil,
  540. noNode = nil,
  541. npcHandler = nil,
  542. noText = '',
  543. maxCount = 500,
  544. amount = 0
  545. }
  546. -- Add it to the parseable module list.
  547. Modules.parseableModules['module_shop'] = ShopModule
  548.  
  549. -- Creates a new instance of ShopModule
  550. function ShopModule:new()
  551. local obj = {}
  552. setmetatable(obj, self)
  553. self.__index = self
  554. return obj
  555. end
  556.  
  557. -- Parses all known parameters.
  558. function ShopModule:parseParameters()
  559.  
  560. local ret = NpcSystem.getParameter('shop_sellable')
  561. if(ret ~= nil) then
  562. self:parseSellable(ret)
  563. end
  564.  
  565. local ret = NpcSystem.getParameter('shop_buyable')
  566. if(ret ~= nil) then
  567. self:parseBuyable(ret)
  568. end
  569.  
  570. end
  571.  
  572. -- Parse a string contaning a set of buyable items.
  573. function ShopModule:parseBuyable(data)
  574. for item in string.gmatch(data, '[^;]+') do
  575. local i = 1
  576.  
  577. local name = nil
  578. local itemid = nil
  579. local cost = nil
  580. local charges = nil
  581.  
  582. for temp in string.gmatch(item, '[^,]+') do
  583. if(i == 1) then
  584. name = temp
  585. elseif(i == 2) then
  586. itemid = tonumber(temp)
  587. elseif(i == 3) then
  588. cost = tonumber(temp)
  589. elseif(i == 4) then
  590. charges = tonumber(temp)
  591. else
  592. print('[Warning] NpcSystem:', 'Unknown parameter found in buyable items parameter.', temp, item)
  593. end
  594. i = i+1
  595. end
  596.  
  597. if(name ~= nil and itemid ~= nil and cost ~= nil) then
  598. if((isItemRune(itemid) == TRUE or isItemFluidContainer(itemid) == TRUE) and charges == nil) then
  599. print('[Warning] NpcSystem:', 'Charges missing for parameter item:' , item)
  600. else
  601. local names = {}
  602. table.insert(names, name)
  603. self:addBuyableItem(names, itemid, cost, charges)
  604. end
  605. else
  606. print('[Warning] NpcSystem:', 'Parameter(s) missing for item:', name, itemid, cost)
  607. end
  608. end
  609. end
  610.  
  611. -- Parse a string contaning a set of sellable items.
  612. function ShopModule:parseSellable(data)
  613. for item in string.gmatch(data, '[^;]+') do
  614. local i = 1
  615.  
  616. local name = nil
  617. local itemid = nil
  618. local cost = nil
  619.  
  620. for temp in string.gmatch(item, '[^,]+') do
  621. if(i == 1) then
  622. name = temp
  623. elseif(i == 2) then
  624. itemid = tonumber(temp)
  625. elseif(i == 3) then
  626. cost = tonumber(temp)
  627. else
  628. print('[Warning] NpcSystem:', 'Unknown parameter found in sellable items parameter.', temp, item)
  629. end
  630. i = i+1
  631. end
  632.  
  633. if(name ~= nil and itemid ~= nil and cost ~= nil) then
  634. local names = {}
  635. table.insert(names, name)
  636. self:addSellableItem(names, itemid, cost)
  637. else
  638. print('[Warning] NpcSystem:', 'Parameter(s) missing for item:', name, itemid, cost)
  639. end
  640. end
  641. end
  642.  
  643. -- Initializes the module and associates handler to it.
  644. function ShopModule:init(handler)
  645. self.npcHandler = handler
  646. self.yesNode = KeywordNode:new(SHOP_YESWORD, ShopModule.onConfirm, {module = self})
  647. self.noNode = KeywordNode:new(SHOP_NOWORD, ShopModule.onDecline, {module = self})
  648. self.noText = handler:getMessage(MESSAGE_DECLINE)
  649.  
  650. return true
  651. end
  652.  
  653. -- Resets the module-specific variables.
  654. function ShopModule:reset()
  655. self.amount = 0
  656. end
  657.  
  658. -- Function used to match a number value from a string.
  659. function ShopModule:getCount(message)
  660. local ret = 1
  661. local b, e = string.find(message, PATTERN_COUNT)
  662. if b ~= nil and e ~= nil then
  663. ret = tonumber(string.sub(message, b, e))
  664. end
  665. if(ret <= 0) then
  666. ret = 1
  667. elseif(ret > self.maxCount) then
  668. ret = self.maxCount
  669. end
  670.  
  671. return ret
  672. end
  673.  
  674. -- Adds a new buyable item.
  675. -- names = A table containing one or more strings of alternative names to this item.
  676. -- itemid = the itemid of the buyable item
  677. -- cost = the price of one single item with item id itemid ^^
  678. -- charges - The charges of each rune or fluidcontainer item. Can be left out if it is not a rune/fluidcontainer and no realname is needed. Default value is nil.
  679. -- 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 (keywords[1]/names will be used)
  680. function ShopModule:addBuyableItem(names, itemid, cost, charges, realname)
  681. for i, name in pairs(names) do
  682. local parameters = {
  683. itemid = itemid,
  684. cost = cost,
  685. eventType = SHOPMODULE_BUY_ITEM,
  686. module = self
  687. }
  688. if(realname ~= nil) then
  689. parameters.realname = realname
  690. end
  691. if(isItemRune(itemid) == TRUE or isItemFluidContainer(itemid) == TRUE) then
  692. parameters.charges = charges
  693. end
  694. keywords = {}
  695. table.insert(keywords, name)
  696. local node = self.npcHandler.keywordHandler:addKeyword(keywords, ShopModule.tradeItem, parameters)
  697. node:addChildKeywordNode(self.yesNode)
  698. node:addChildKeywordNode(self.noNode)
  699. end
  700. end
  701.  
  702. -- Adds a new sellable item.
  703. -- names = A table containing one or more strings of alternative names to this item.
  704. -- itemid = the itemid of the buyable item
  705. -- cost = the price of one single item with item id itemid ^^
  706. -- 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 (keywords[2]/names will be used)
  707. function ShopModule:addSellableItem(names, itemid, cost, realname)
  708. for i, name in pairs(names) do
  709. local parameters = {
  710. itemid = itemid,
  711. cost = cost,
  712. eventType = SHOPMODULE_SELL_ITEM,
  713. module = self
  714. }
  715. if(realname ~= nil) then
  716. parameters.realname = realname
  717. end
  718. keywords = {}
  719. table.insert(keywords, 'sell')
  720. table.insert(keywords, name)
  721. local node = self.npcHandler.keywordHandler:addKeyword(keywords, ShopModule.tradeItem, parameters)
  722. node:addChildKeywordNode(self.yesNode)
  723. node:addChildKeywordNode(self.noNode)
  724. end
  725. end
  726.  
  727.  
  728. -- onModuleReset callback function. Calls ShopModule:reset()
  729. function ShopModule:callbackOnModuleReset()
  730. self:reset()
  731.  
  732. return true
  733. end
  734.  
  735.  
  736. -- tradeItem callback function. Makes the npc say the message defined by MESSAGE_BUY or MESSAGE_SELL
  737. function ShopModule.tradeItem(cid, message, keywords, parameters, node)
  738. local module = parameters.module
  739. if(cid ~= module.npcHandler.focus) then
  740. return false
  741. end
  742. local count = module:getCount(message)
  743. module.amount = count
  744. local tmpName = nil
  745. if(parameters.eventType == SHOPMODULE_SELL_ITEM) then
  746. tmpName = node:getKeywords()[2]
  747. elseif(parameters.eventType == SHOPMODULE_BUY_ITEM) then
  748. tmpName = node:getKeywords()[1]
  749. end
  750. local parseInfo = {
  751. [TAG_PLAYERNAME] = getPlayerName(cid),
  752. [TAG_ITEMCOUNT] = module.amount,
  753. [TAG_TOTALCOST] = parameters.cost*module.amount,
  754. [TAG_ITEMNAME] = parameters.realname or tmpName
  755. }
  756.  
  757. if(parameters.eventType == SHOPMODULE_SELL_ITEM) then
  758. local msg = module.npcHandler:getMessage(MESSAGE_SELL)
  759. msg = module.npcHandler:parseMessage(msg, parseInfo)
  760. module.npcHandler:say(msg)
  761. elseif(parameters.eventType == SHOPMODULE_BUY_ITEM) then
  762. local msg = module.npcHandler:getMessage(MESSAGE_BUY)
  763. msg = module.npcHandler:parseMessage(msg, parseInfo)
  764. module.npcHandler:say(msg)
  765. end
  766.  
  767. return true
  768.  
  769. end
  770.  
  771.  
  772. -- onConfirm keyword callback function. Sells/buys the actual item.
  773. function ShopModule.onConfirm(cid, message, keywords, parameters, node)
  774. local module = parameters.module
  775. if(cid ~= module.npcHandler.focus) then
  776. return false
  777. end
  778. local parentParameters = node:getParent():getParameters()
  779. local parseInfo = {
  780. [TAG_PLAYERNAME] = getPlayerName(cid),
  781. [TAG_ITEMCOUNT] = module.amount,
  782. [TAG_TOTALCOST] = parentParameters.cost*module.amount,
  783. [TAG_ITEMNAME] = parentParameters.realname or node:getParent():getKeywords()[1]
  784. }
  785.  
  786. if(parentParameters.eventType == SHOPMODULE_SELL_ITEM) then
  787. local ret = doPlayerSellItem(cid, parentParameters.itemid, module.amount, parentParameters.cost*module.amount)
  788. if(ret == LUA_NO_ERROR) then
  789. local msg = module.npcHandler:getMessage(MESSAGE_ONSELL)
  790. msg = module.npcHandler:parseMessage(msg, parseInfo)
  791. module.npcHandler:say(msg)
  792. else
  793. local msg = module.npcHandler:getMessage(MESSAGE_NOTHAVEITEM)
  794. msg = module.npcHandler:parseMessage(msg, parseInfo)
  795. module.npcHandler:say(msg)
  796. end
  797. elseif(parentParameters.eventType == SHOPMODULE_BUY_ITEM) then
  798. local ret = doPlayerBuyItem(cid, parentParameters.itemid, module.amount, parentParameters.cost*module.amount, parentParameters.charges)
  799. if(ret == LUA_NO_ERROR) then
  800. local msg = module.npcHandler:getMessage(MESSAGE_ONBUY)
  801. msg = module.npcHandler:parseMessage(msg, parseInfo)
  802. module.npcHandler:say(msg)
  803. else
  804. local msg = module.npcHandler:getMessage(MESSAGE_NEEDMOREMONEY)
  805. msg = module.npcHandler:parseMessage(msg, parseInfo)
  806. module.npcHandler:say(msg)
  807. end
  808. end
  809.  
  810. module.npcHandler:resetNpc()
  811. return true
  812. end
  813.  
  814. -- onDecliune keyword callback function. Generally called when the player sais 'no' after wanting to buy an item.
  815. function ShopModule.onDecline(cid, message, keywords, parameters, node)
  816. local module = parameters.module
  817. if(cid ~= module.npcHandler.focus) then
  818. return false
  819. end
  820. local parentParameters = node:getParent():getParameters()
  821. local parseInfo = {
  822. [TAG_PLAYERNAME] = getPlayerName(cid),
  823. [TAG_ITEMCOUNT] = module.amount,
  824. [TAG_TOTALCOST] = parentParameters.cost*module.amount,
  825. [TAG_ITEMNAME] = parentParameters.realname or node:getParent():getKeywords()[1]
  826. }
  827. local msg = module.npcHandler:parseMessage(module.noText, parseInfo)
  828. module.npcHandler:say(msg)
  829. module.npcHandler:resetNpc()
  830. return true
  831. end
  832. end
Add Comment
Please, Sign In to add comment