Advertisement
Guest User

Time Chests (with chance)

a guest
Mar 19th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.98 KB | None | 0 0
  1. -- Time Chests by Limos (with chance)
  2. function onUse(cid, item, fromPosition, itemEx, toPosition)
  3.  
  4.     local chests = {
  5.         [9001] = {
  6.             waittime = 14400, -- time in seconds
  7.             storage = 3301,
  8.             level = 50,
  9.             rewarditems = {
  10.                 {id = 2647, count = 1, chance = 5}, -- start with the lowest chances
  11.                 {id = 2462, count = 1, chance = 10},
  12.                 {id = 2124, count = 1, chance = 15},
  13.                 {id = 2152, count = math.random(1, 50), chance = 70}
  14.             }
  15.         },
  16.         [9002] = {
  17.             waittime = 14400,
  18.             storage = 3302,
  19.             level = 50,
  20.             rewarditems = {
  21.                 {id = 2488, count = 1, chance = 5},
  22.                 {id = 2498, count = 1, chance = 10},
  23.                 {id = 2492, count = 1, chance = 15},
  24.                 {id = 2160, count = math.random(1, 5), chance = 70}
  25.             }
  26.         },
  27.         [9003] = {
  28.             waittime = 14400,
  29.             storage = 3303,
  30.             level = 50,
  31.             rewarditems = {
  32.                 {id = 2470, count = 1, chance = 5},
  33.                 {id = 7385, count = 1, chance = 10},
  34.                 {id = 2514, count = 1, chance = 15},
  35.                 {id = 2160, count = math.random(20, 50), chance = 70}
  36.             }
  37.         }          
  38.     }
  39.  
  40.     local x = chests[item.uid]
  41.     if getPlayerLevel(cid) < x.level then
  42.         doSendMagicEffect(getThingPos(cid), CONST_ME_POFF)
  43.         doPlayerSendCancel(cid, "You need to be level "..x.level.." to open the chest.")
  44.         return true
  45.     end
  46.  
  47.     if exhaustion.check(cid, x.storage) then
  48.         local time = exhaustion.get(cid, x.storage)
  49.         local hours, minutes, seconds = math.floor (time / 3600), math.floor ((time - ((math.floor (time / 3600)) * 3600))/ 60), time - ((math.floor (time/60)) * 60)
  50.         if time >= 3600 then
  51.             text = hours.." "..(hours > 1 and "hours" or "hour")..", "..minutes.." "..(minutes > 1 and "minutes" or "minute").." and "..seconds.." "..(seconds > 1 and "seconds" or "second")
  52.         elseif time >= 120 then
  53.             text = minutes.." "..(minutes > 1 and "minutes" or "minute").." and "..seconds.." "..(seconds > 1 and "seconds" or "second")
  54.         else
  55.             text = seconds.." "..(seconds > 1 and "seconds" or "second")
  56.         end
  57.         doSendMagicEffect(getThingPos(cid), CONST_ME_POFF)
  58.         doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "It is empty. You need to wait "..text.." before you can get a reward again.")
  59.         return true
  60.     end
  61.  
  62.     local chance = math.random(1,100)
  63.     for i = 1, #x.rewarditems, 1 do
  64.         if chance < x.rewarditems[i].chance then
  65.             local info = getItemInfo(x.rewarditems[i].id)
  66.             if x.rewarditems[i].count > 1 then 
  67.                 text = x.rewarditems[i].count .. " " .. info.plural
  68.             else
  69.                 text = info.article .. " " .. info.name
  70.             end
  71.  
  72.             local item = doCreateItemEx(x.rewarditems[i].id, x.rewarditems[i].count)
  73.             if doPlayerAddItemEx(cid, item, false) ~= RETURNVALUE_NOERROR then
  74.                 doSendMagicEffect(getThingPos(cid), CONST_ME_POFF)
  75.                 text = "You have found a reward. It is to heavy or you have not enough space."
  76.             else
  77.                 text = "You have found " .. text .. "."
  78.                 exhaustion.set(cid, x.storage, x.waittime)
  79.             end
  80.             doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, text)
  81.             return true
  82.         else
  83.             chance = chance - x.rewarditems[i].chance
  84.         end
  85.     end
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement