Advertisement
Guest User

Untitled

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