Forgee_XenoBot

Backpack test

Feb 12th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.03 KB | None | 0 0
  1. function closeBackpacks(...)
  2.     local backpacks = {...}
  3.     local indexes = Container.GetIndexes()
  4.     if #backpacks > 0 then
  5.         for i = #backpacks, 1, -1 do
  6.             if type(backpacks[i]) == "string" then
  7.                 tmpBP = Container.GetByName(backpacks[i])
  8.             elseif backpacks[i] < 18 then
  9.                 tmpBP = Container.GetFromIndex(backpacks[i])
  10.             else
  11.                 tmpBP = Container.GetByName(Item.GetName(backpacks[i]))
  12.             end
  13.             tmpBP:Close()
  14.             wait(200, 500)
  15.         end
  16.     else
  17.         Self.CloseContainers()
  18.     end
  19. end
  20.  
  21. function openBackpacks(...)
  22.     local backpacks = {...} -- List of backpacks to open.
  23.     local open = Container.GetIndexes() -- List of already open backpacks.
  24.     local main = Container.GetFirst() -- First open container is assumed to be main backpack (will be 0 if no container is open).
  25.     local toOpen = #backpacks -- Number of backpacks we need to open (used to check success).
  26.     local bps = {} -- A place to store the backpacks to open by container:UseItem(), if any. Why not always use :OpenChildren()? This way I can open bps by their relative position instead of id, so you don't have to worry about colours anymore.
  27.     if main:ID() == 0 then -- If no backpack is open, we need to open main.
  28.         if backpacks[1] ~= Self.Backpack().id then
  29.             toOpen = toOpen + 1
  30.         end
  31.         repeat -- Open main backpack.
  32.             wait(200, 600)
  33.         until Self.OpenMainBackpack() > 0
  34.         wait(500)
  35.         main = Container.GetFirst()
  36.     end
  37.    
  38.     if #backpacks > 0 then
  39.         if type(backpacks[1]) == "table" then
  40.             local bp, minimize = backpacks[1][1], backpacks[1][2]
  41.         else
  42.             local bp, minimize = backpacks[1], false
  43.         end
  44.         if type(bp) == "number" then
  45.             if bp <= 20 then
  46.                 openByIndex(main, unpack(backpacks))
  47.             else
  48.                 main:OpenChildren(unpack(backpacks))
  49.             end
  50.         else
  51.             main:OpenChildren(unpack(backpacks))
  52.         end
  53.     end
  54.     wait(400)
  55.     if #open + toOpen == #Container.GetIndexes() then
  56.         return true
  57.     end
  58.     return false   
  59. end
  60.  
  61. function openByIndex(parent, ...)
  62.     local bps = {}
  63.     -- Look for containers in main backpack.
  64.     for i = 0, parent:ItemCount() do
  65.         local thing = parent:GetItemData(i)
  66.         if Item.isContainer(thing.id) then
  67.             table.insert(bps, i)
  68.         end
  69.     end
  70.    
  71.     for index, bp in pairs({...}) do
  72.         if type(bp) == "table" then
  73.             local spot, minimize = bps[bp[1]], bp[2]
  74.         else
  75.             local spot, minimize = bps[bp], false
  76.         end
  77.         parent:UseItem(spot)
  78.         if minimize then
  79.             wait(200)
  80.             Container.GetLast():Minimize()
  81.         end
  82.     end
  83. end
  84.  
  85. function getOpenBackpacks()
  86.     local indexes = Container.GetIndexes() -- Find index for all open backpacks
  87.     local open = {} -- Create a place to store our open backpacks
  88.     for i = 1, #indexes do -- Search all open backpacks
  89.         tmpBP = Container.GetFromIndex(indexes[i])
  90.         table.insert(open, tmpBP:ID()) -- Store this backpack
  91.     end
  92.     return open -- Return found backpacks
  93. end
  94.  
  95. --[[
  96. resetBackpacks(reset)
  97. If reset is specified it will reset the number of backpacks specified. resetBackpacks(1) will reset the last opened backpack, resetBackpacks(2) the last 2 etc.
  98. Empty argument resets all open backpacks.
  99. Checks if all backpacks were reopened correctly. If not it will start over up to 3 times. If it failed after 3 tries it returns false (true if it succeeds).
  100. ]]
  101. function resetBackpacks(reset, minimize)
  102.     Walker.Stop()
  103.     local open = {} -- Create a table to hold backpack list
  104.     local indexes = Container.GetIndexes() -- Get indexes of open backpacks.
  105.     if reset then
  106.         if reset > 0 and reset < #indexes then
  107.             for i = (#indexes-reset)+1, #indexes do
  108.                 table.insert(open, indexes[i])
  109.             end
  110.         else
  111.             for i = 2, #indexes do -- If 'reset' is above the number of open backpacks we should reset all.
  112.                 table.insert(open, indexes[i])
  113.             end
  114.         end
  115.     else
  116.         for i = 2, #indexes do -- If 'reset' is nil (empty argument) we should reset all backpacks.
  117.             table.insert(open, indexes[i])
  118.         end
  119.     end
  120.     local tries = 3 -- Give the script 3 tries to achieve a successful reset.
  121.     local close = open
  122.     if minimize then
  123.         for i = 1, #open do
  124.             open[i] = {open[i], true}
  125.         end
  126.     end
  127.        
  128.     repeat
  129.         if reset then
  130.             if type(reset) == "number" then
  131.                 if reset > 0 and reset < #indexes then
  132.                     closeBackpacks(unpack(close)) -- Close selected backpacks
  133.                 else
  134.                     Self.CloseContainers() -- Close all backpacks.
  135.                 end
  136.             elseif type(reset) == "boolean" then
  137.                 minimize = reset
  138.             end
  139.         else
  140.             Self.CloseContainers() -- Close all backpacks
  141.         end
  142.         wait(600, 800) -- Wait a little while      
  143.         local reopen = openBackpacks(unpack(open)) -- I store the return from openBackpacks, true if successfull, false otherwise
  144.         if tries == 0 then -- Check how many tries the function has left
  145.             print("BackpackReset failed!") -- Tell the user that reset has failed
  146.             return false
  147.         end
  148.         tries = tries - 1 -- One try has been spent
  149.         wait(100,200)
  150.     until reopen -- If reopen is true it means all backpacks were opened successfully and function is done
  151.     Walker.Start()
  152.     return true
  153. end
  154.  
  155. resetBackpacks(1, true)
Advertisement
Add Comment
Please, Sign In to add comment