Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.17 KB | None | 0 0
  1. function isFocused(cid, t)
  2.     for i, v in pairs(t) do
  3.         if(v == cid) then
  4.             return true
  5.         end
  6.     end
  7.     return false
  8. end
  9.  
  10. function addFocus(cid, t)
  11.     if(not isFocused(cid, t)) then
  12.         table.insert(t, cid)
  13.     end
  14. end
  15.  
  16. function removeFocus(cid, t)
  17.     for i, v in pairs(t) do
  18.         if(v == cid) then
  19.             table.remove(t, i)
  20.             break
  21.         end
  22.     end
  23. end
  24.  
  25. function lookAtFocus(t)
  26.     for i, v in pairs(t) do
  27.         if(isPlayer(v)) then
  28.             doNpcSetCreatureFocus(v)
  29.             return
  30.         end
  31.     end
  32.     doNpcSetCreatureFocus(0)
  33. end
  34.  
  35. function msgcontains(msg, ...)
  36.     local function checkstring(msg, key)
  37.       return (" " .. msg .. " "):find("%s+" .. key .. "[%s%p]+") and true or false
  38.     end
  39.     for _, key in ipairs(arg) do
  40.         if checkstring(tostring(msg):lower(), tostring(key):lower()) then
  41.           return true
  42.         end
  43.     end
  44.   return false
  45. end
  46. local focuses = {}
  47. function onCreatureDisappear(cid)
  48.     if isFocused(cid, focuses) then
  49.      removeFocus(cid, focuses)
  50.         if isPlayer(cid) then
  51.          closeShopWindow(cid)
  52.         end
  53.     end
  54. end
  55.  
  56. local onBuy = function(cid, item, subType, amount, ignoreCap, inBackpack,sellPrice,price,buyPrice)
  57.       local it = items[item]
  58.       for i,v in pairs(it) do
  59.       print(i,v)
  60.       end      
  61. end
  62. local onSell = function(cid, item, subType, amount, ignoreCap, inBackpacks)
  63.       return      
  64. end
  65.  
  66. function onCreatureSay(cid, type,msg)
  67. items = {}
  68.     if msgcontains(msg, "hi", "hello") and not isFocused(cid, focuses) and getDistanceToCreature(cid) < 4 then
  69.      selfSay("Hiho " .. getCreatureName(cid)..". Say {offers} to see my offers.", cid)
  70.      addFocus(cid, focuses)
  71.      selfFocus(cid)
  72.     elseif msgcontains(msg, "offers") and isFocused(cid, focuses) then
  73.      selfSay("Here are the places that I can take you.", cid)
  74.      text = "--==## id | Price | name | ##==-- \n"
  75.       for index, info in pairs(readyResellerItems()) do
  76.         local itemname = getItemDescriptionsById(info.itemid).name
  77.         text = ""..text.."\n".. info.item_uid.." | ".. info.price.." | ".. itemname .."."
  78.         items[info.item_uid] = {id = info.itemid, price = info.price, info.playerid}
  79.                
  80.       end
  81.       text = ""..text.."\n \n Para comprar diga (buy + id do o item)."    
  82.         doPlayerPopupFYI(cid, text)
  83.      elseif msgcontains(msg, "buy") and isFocused(cid, focuses) then
  84.      local item = tonumber(string.find (msg, "(.%d)"))
  85.      print(item)
  86.      item = items[item]
  87.      print(item)
  88.     elseif msgcontains(msg, "bye") and isFocused(cid, focuses) then
  89.      selfSay("Goodbye!", cid)
  90.      closeShopWindow(cid)
  91.      removeFocus(cid, focuses)
  92.      lookAtFocus(focuses)
  93.     end
  94. end
  95.  
  96. function onThink()
  97.     for _, focus in pairs(focuses) do
  98.         if not isCreature(focus) then
  99.          removeFocus(focus, focuses)
  100.         else
  101.          local distance = getDistanceTo(focus) or 5
  102.             if distance > 4 then
  103.                 selfSay("Hmpf!", focus)
  104.                 closeShopWindow(focus)
  105.                 removeFocus(focus, focuses)
  106.             end
  107.         end
  108.     end
  109.     lookAtFocus(focuses)
  110. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement