Advertisement
Guest User

Modules

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