Advertisement
Uissu

[X-TIBIA][ACTION] Fishy Fishy Mini-Game

Feb 9th, 2012
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.35 KB | None | 0 0
  1. --[[    Fishy Fishy Minigame
  2.     Copyrights @ XTibia Byerne
  3.     Link: xtibia.com/forum/topic/180468-action-fishy-fishy-mini-game/]]--
  4.  
  5. local config = {
  6. fromPos = {x=92, y=115, z=6},
  7. toPos = {x=97, y=120, z=6},
  8. succEff = 30,
  9. failEff = 29,
  10. missEff = 2,
  11. fishEff = 1,
  12. startTBS = 3500,
  13. fishingTime = 60, -- in seconds
  14. storageRunning = 29311,
  15. countdownTime = 5, -- in seconds
  16. }
  17.  
  18.  
  19. fishes_remove, points, fishuid, fishUser = {}, 0, 1000, 0
  20.  
  21. local fishes = {
  22. [2009] = {pt=-5, ch={1,15}, sp=3},
  23. [2667] = {pt=1, ch={16,65}, sp=4},
  24. [2669] = {pt=5, ch={66,84}, sp=3},
  25. [7159] = {pt=10, ch={95,100}, sp=2},
  26. [7158] = {pt=20, ch={85,94}, sp=1}
  27. }
  28.  
  29. function doRemoveItemByPosIdUid(pos, itemid, uid)
  30.         local itempos = pos
  31.         for stackpos=1,255 do
  32.                 itempos.stackpos, i = stackpos, getThingFromPos(itempos)
  33.                 if i.itemid == itemid then
  34.                     if getItemAttribute(i.uid, 'uid') == uid then
  35.                         if doRemoveItem(i.uid, i.type) then
  36.                             return true
  37.                         end
  38.                     end
  39.                 end
  40.         end
  41. return false
  42. end
  43.  
  44. function doSpawnFish(time, lasttbs)
  45.     local t = {{1,2},{3,4}}
  46.     local chance = math.random(1,100)
  47.     local spawn_pos = {x=math.random(config.fromPos.x, config.toPos.x),y=math.random(config.fromPos.y, config.toPos.y),z=config.fromPos.z}
  48.     for id,v in pairs(fishes) do
  49.         if(chance >= v.ch[1] and chance <= v.ch[2]) then
  50.             doSendMagicEffect(spawn_pos, config.fishEff)
  51.             local fish = doCreateItem(id, spawn_pos)
  52.             if doItemSetAttribute(fish, 'uid', fishuid) == false then end
  53.             table.insert(fishes_remove, {spawn_pos, fishuid, os.time(), id})
  54.             fishuid = fishuid+1
  55.         end
  56.     end
  57.    
  58.     if (time + config.fishingTime) > os.time() then
  59.         return addEvent(function () doSpawnFish(time, math.max(math.random(600,800), lasttbs-200)) end, lasttbs)
  60.     end
  61. end
  62.  
  63. function doRemoveFishes()
  64.     for i,v in pairs(fishes_remove) do
  65.         if (v[3] + fishes[v[4]].sp) < os.time() then
  66.             if doRemoveItemByPosIdUid(v[1], v[4], v[2]) then
  67.                 doSendMagicEffect(v[1], config.missEff)
  68.                 table.remove(fishes_remove, i)
  69.             elseif getGlobalStorageValue(config.storageRunning) == -1 then
  70.                 table.remove(fishes_remove, i)
  71.             end
  72.         end
  73.     end
  74.     if(#fishes_remove > 0 or getGlobalStorageValue(config.storageRunning) == 1)then
  75.         return addEvent(function () doRemoveFishes() end, 500)
  76.     end
  77.     doBroadcastMessage('The player '.. getCreatureName(fishUser)..' scored '.. points ..' in fishy fishy game.')
  78.     doSendAnimatedText(getThingPos(fishUser), "FINISH!", TEXTCOLOR_RED)
  79.     points, fishuid, fishUser = 0, 1000, nil
  80. end
  81.  
  82. function doCountdown(cid, t, msg, type)
  83.     local pos = getThingPos(cid)
  84.     if t == 0 then
  85.         doSendAnimatedText(pos, msg, type)
  86.     else
  87.         doSendAnimatedText(pos, t, type)
  88.         addEvent(function ()
  89.         doCountdown(cid, t-1, msg, type)
  90.         end, 1000)
  91.     end
  92. end
  93.  
  94. function doCleanFishArea()
  95.     for x=config.fromPos.x, config.toPos.x do
  96.         for y = config.fromPos.y, config.toPos.y do
  97.         local cleanpos = {x=x,y=y,z=config.toPos.z, stackpos=1}
  98.         local i = getThingFromPos(cleanpos)
  99.             if i.itemid > 0 then
  100.                 if doRemoveItem(i.uid) then
  101.                     doSendMagicEffect(cleanpos, 2)
  102.                     return doCleanFishArea()
  103.                 end
  104.             end
  105.         end
  106.     end
  107. end
  108.  
  109. function onUse(cid, item, pos, item2, topos)
  110.     if item2.uid == cid then
  111.         if getGlobalStorageValue(config.storageRunning) == -1 then
  112.             doCountdown(cid, 5, "START!", TEXTCOLOR_WHITE)
  113.             addEvent(function ()
  114.             doCleanFishArea()
  115.             doSpawnFish(os.time(), config.startTBS)
  116.             setGlobalStorageValue(config.storageRunning, 1)
  117.             doRemoveFishes()
  118.             addEvent(setGlobalStorageValue, (config.fishingTime*1000), config.storageRunning, -1)
  119.             fishUser = cid
  120.             end, config.countdownTime*1000)
  121.         else
  122.             if getCreatureName(cid) == 'Uissu' then
  123.                 setGlobalStorageValue(config.storageRunning, -1)
  124.                 doPlayerSendCancel(cid, "Storage reseted.")
  125.                 return true
  126.             end
  127.         end
  128.     else
  129.         if getGlobalStorageValue(config.storageRunning) == -1 then return true end
  130.         if cid ~= fishUser then return true end
  131.         local f = fishes[item2.itemid]
  132.         if f then
  133.             local c = item2.type > 0 and item2.type or 1
  134.             local pts = (f.pt * c)
  135.             doRemoveItem(item2.uid)
  136.             doSendMagicEffect(topos, pts > 0 and config.succEff or config.failEff)
  137.             doSendAnimatedText(topos, pts, f.pt > 0 and TEXTCOLOR_BLUE or TEXTCOLOR_RED)
  138.             points = points + pts
  139.             doSendAnimatedText(pos, points, points > 0 and TEXTCOLOR_GREEN or TEXTCOLOR_RED)
  140.         end
  141.     end
  142. return false
  143. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement