Advertisement
Guest User

Untitled

a guest
Aug 1st, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.32 KB | None | 0 0
  1. CONFIG =
  2. {
  3.     WAITTIME = 500, -- miliseconds to wait between each iteration
  4.     BACKPACKS = -- list of backpacks to search for
  5.     {
  6.         ["mainBp"] = "brocade backpack",
  7.         ["goldBP"] = "blue backpack",
  8.         ["lootBP"] = "fur backpack",
  9.         ["wormsBP"] = "golden backpack",
  10.         ["purseBP"] = "beach backpack"
  11.     },
  12.  
  13.     CHECKBP = "mainBp", -- backpack name to check for moving items
  14.     USEGOLDBP = "goldBP", -- backpack name to use gold from
  15.  
  16.     WORMSID = 3492,
  17.     TOTALWORMS = 500,
  18.  
  19.  
  20.     ITEMS = -- items to be moved
  21.     {
  22.         [12731] = "lootBP", [3580] = "lootBP", [12736] = "lootBP", [12732] = "lootBP", [12735] = "lootBP", [12741] = "lootBP",
  23.         [12733] = "lootBP", [12557] = "lootBP", [12807] = "lootBP", [13992] = "lootBP", [3492] = "wormsBP", [3043] = "goldBP",
  24.         [3035] = "goldBP", [16087] = "purseBP", [5895] = "goldBP",
  25.     }
  26. }
  27.  
  28. function showError(text)
  29.     alert()
  30.     error(text)
  31. end
  32.  
  33. STATES =
  34. {
  35.     {
  36.         name = "Check Backpacks",
  37.         isReached = function(state)
  38.             -- this state is reached as long as all backpacks are open
  39.             if (not state.backpacks) then
  40.                 return false
  41.             end
  42.  
  43.             for title, _ in pairs(CONFIG.BACKPACKS) do
  44.                 if (not state.backpacks[title]:isOpen()) then
  45.                     return false
  46.                 end
  47.             end
  48.  
  49.             return true
  50.         end,
  51.         tryToReach = function(state)
  52.             -- to reach this state, we must look to indentify all required backpacks
  53.             state.backpacks = {}
  54.             for title, bpName in pairs(CONFIG.BACKPACKS) do
  55.                 state.backpacks[title] = Container.GetByName(bpName)
  56.                 if (not state.backpacks[title]:isOpen()) then
  57.                     showError("Can't locate '" .. title .. "', " .. bpName .. " is not open!")
  58.                 end
  59.             end
  60.         end
  61.  
  62.     },
  63.     {
  64.         name = "Move Items",
  65.         isReached = function(state)
  66.             -- this state is reached as long as our main backpack is not almost full
  67.             return (state.backpacks[CONFIG.CHECKBP]:Capacity() - state.backpacks[CONFIG.CHECKBP]:ItemCount() > 2)
  68.         end,
  69.         tryToReach = function(state)
  70.             -- to reach this state, we must move items to the correct backpacks
  71.             function moveItemToBackpack(fromBp, fromSpot, toBp, toBpTitle)
  72.                 if (toBp:Capacity() == toBp:ItemCount()) then -- bp is full, try to cascade
  73.                     if (not Item.isContainer(self:GetItemData(spot).id)) then
  74.                         showError("Backpack '" .. toBpTitle .. "' is full and no cascaded backpacks remain!")
  75.                     end
  76.                     toBp:UseItem(toBp:ItemCount() - 1, true)
  77.                 else -- no need to cascade, lets move
  78.                     fromBp:MoveItemToContainer(fromSpot, toBp:Index(), toBp:ItemCount())
  79.                 end
  80.             end
  81.  
  82.             local mainBp = state.backpacks[CONFIG.CHECKBP]
  83.             for spot = mainBp:ItemCount() - 1, 0, -1 do
  84.                 local checkItem = mainBp:GetItemData(spot)
  85.                 local toBpTitle = CONFIG.ITEMS[checkItem.id]
  86.                 if (toBpTitle) then
  87.                     moveItemToBackpack(mainBp, spot, state.backpacks[toBpTitle], toBpTitle)
  88.                     return
  89.                 end
  90.             end
  91.  
  92.             showError("Main backpack is almost full, but no items can be moved!")
  93.         end
  94.     },
  95.     {
  96.         name = "Use Plats",
  97.         isReached = function(state)
  98.             -- return true if no stacks of 100 exist
  99.             return true
  100.         end,
  101.         tryToReach = function(state)
  102.             -- use a stack of 100
  103.         end
  104.     },
  105.     {
  106.         name = "Buy Worms",
  107.         isReached = function(state)
  108.             -- this state is reached as long as we have more than 0 worms
  109.             local complete = (Self.ItemCount(WORMSID) > 0)
  110.             state.wormsState = complete and 0 or state.wormsState
  111.             return complete
  112.         end,
  113.         tryToReach = function(state)
  114.             -- to reach this state, we buy some worms
  115.             if (state.wormsState == 0) then
  116.                 Self.SayToNpc({'hi', 'trade'}, 70)
  117.             else if (state.wormsState >= 3) then
  118.                 Self.ShopBuyItemsUpTo(WORMSID, CONFIG.TOTALWORMS)
  119.             else if (state.wormsState >= 6) then
  120.                 showError("Failed to buy worms!")
  121.             end
  122.             state.wormsState = state.wormsState + 1
  123.         end
  124.     },
  125.     {
  126.         name = "Fish",
  127.         isReached = function(state)
  128.             -- always say this state isnt reached, so we will always do it when others are reached
  129.             return false
  130.         end,
  131.         tryToReach = function(state)
  132.             -- fish here
  133.         end
  134.     },
  135. }
  136.  
  137.  
  138.  
  139. local stateData = {} -- this holds variables for the state machine
  140. while (true) do
  141.     local stateNum = 1
  142.     while (STATES[stateNum] and STATES[stateNum]:isReached(stateData)) do -- this loop locates the first state that is not "reached"
  143.         stateNum = stateNum + 1
  144.     end
  145.  
  146.     STATES[stateNum] and STATES[stateNum]:tryToReach(stateData) -- this tries to reach the unreached state
  147.  
  148.     wait(CONFIG.WAITTIME)
  149. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement