Flaghacker

Debug computed magic

Jun 19th, 2014
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.51 KB | None | 0 0
  1. --Debug fix ~ flaghacker, all credit goes to GamerNebulae
  2.  
  3. --Computed Magic v2.1.3: TT[kami] update, unified version
  4. --Special thanks to flaghacker for helping me with updating the code
  5.  
  6. --Declaring variables: All local variables cannot be used in other programs!
  7. local tArgs = {...}
  8. local reqpipe
  9. local chest
  10. local jarData = {}
  11. local jarDataIndex
  12. local NBT = {}
  13. local amountManaBean = {}
  14. local beansToRequest = {}
  15.  
  16. if tArgs[1] == "troubleshoot" then troubleshoot = true end
  17.  
  18. --Functions:
  19.  
  20. --Peripheral detection:
  21. local function detectPeripherals()
  22.     local p = peripheral.getNames()
  23.     for k,v in pairs(p) do
  24.         if (string.find(v, "LogisticsPipes:Request")) then
  25.             reqpipe = peripheral.wrap(v)
  26.             if troubleshoot then print("Request Pipe found!") end
  27.         elseif (string.find(v, "reinforced_chest") or string.find(v, "thaumiumchest")) then
  28.             chest = peripheral.wrap(v)
  29.             if troubleshoot then print("Buffer found! (BetterStorage mod)") end
  30.         elseif (string.find(v, "iron") or string.find(v, "gold") or string.find(v, "diamond") or string.find(v, "copper") or string.find(v, "silver") or string.find(v, "crystal") or string.find(v, "obsidian")) then
  31.             chest = peripheral.wrap(v)
  32.             if troubleshoot then print("Buffer found! (Iron Chests mod)") end
  33.         elseif (string.find(v, "hungry_chest")) then
  34.             chest = peripheral.wrap(v)
  35.             if troubleshoot then print("Buffer found! (ThaumCraft mod)") end
  36.         elseif (string.find(v, "container_chest")) then
  37.             chest = peripheral.wrap(v)
  38.             if troubleshoot then print("Buffer found! (Vanilla chest)") end
  39.         end
  40.     end
  41. end
  42.  
  43. --Retrieve data from the jars
  44. local function retrieveData()
  45.     --Retrieving which aspects you have and how much is in those jars and getting information from storage system
  46.     local p = peripheral.getNames()
  47.     local items = reqpipe.getAvailableItems()
  48.     jarData = {}   
  49.  
  50.     for k,v in pairs(p) do
  51.         if (string.find(v, "tt_aspectContainer") or string.find(v, "tilejar")) then
  52.             local aspectJar
  53.             local c = peripheral.call(v, "getAspects")
  54.             if (type(c) == "table") then
  55.                 aspectJar = string.lower(c[1]["name"])
  56.             else
  57.                 aspectJar = string.lower(c)
  58.             end
  59.             local aspectCount = peripheral.call(v, "getAspectCount", aspectJar)
  60.             if (not jarData[aspectJar]) then
  61.                 jarData[aspectJar] = {}
  62.                 jarDataIndex = #jarData[aspectJar] + 1
  63.                 jarData[aspectJar][jarDataIndex] = aspectCount
  64.             else
  65.                 jarDataIndex = #jarData[aspectJar] + 1
  66.                 jarData[aspectJar][jarDataIndex] = aspectCount
  67.             end
  68.         end
  69.     end
  70.  
  71.     for q = 1, #items, 1 do
  72.         local NBTCompound = reqpipe.getNBTTagCompound(items[q][1])
  73.         if type(NBTCompound) == "nil" then error("Report to the thread as 'NBTCompound is nil'") end
  74.         local handler = fs.open("logFile", "a")
  75.         handler.write(textutils.serialize(NBTCompound))
  76.         handler.close()
  77.         error("Done logging, post the 'logFile' file to the thread")
  78.         local aspectBean = NBTCompound["value"]["Aspects"]["value"][1]["value"]["key"]["value"]
  79.         NBT[aspectBean] = items[q][1]
  80.         amountManaBean[aspectBean] = items[q][2]
  81.     end
  82. end
  83.  
  84. local function request(beanType, amountType)
  85.     reqpipe.makeRequest(NBT[beanType], amountType)
  86. end
  87.  
  88. --Calculate the amount of mana beans you can request
  89. local function calculate()
  90.     local chestSpaceNeeded = 0
  91.     for k,v in pairs(jarData) do
  92.         for q = 1, #jarData[k] do
  93.             if (not beansToRequest[k]) then
  94.                 beansToRequest[k] = 64 - jarData[k][q]
  95.             else
  96.                 beansToRequest[k] = beansToRequest[k] + (64 - jarData[k][q])
  97.             end
  98.         end
  99.         chestSpaceNeeded = chestSpaceNeeded + math.ceil(beansToRequest[k] / 64)
  100.     end
  101.  
  102.     if (chestSpaceNeeded <= chest.getInventorySize()) then
  103.         for k,v in pairs(beansToRequest) do
  104.             if (not NBT[k]) then
  105.                 if troubleshoot then print("You do not have any "..k.." mana beans!") end
  106.             elseif (amountManaBean[k] < v) then
  107.                 request(k, amountManaBean[k] - 1)
  108.             else
  109.                 request(k,v)
  110.             end
  111.         end
  112.     else
  113.         for k,v in pairs(beansToRequest) do
  114.             local invSpaceFound = false
  115.             while (not invSpaceFound) do
  116.                 for q = 1, chest.getInventorySize() do
  117.                     if (not chest.getStackInSlot(q)) then
  118.                         if (not NBT[k]) then
  119.                             if troubleshoot then print("You do not have any "..k.." mana beans!") end
  120.                         elseif (amountManaBean[k] < v) then
  121.                             request(k, amountManaBean[k] - 1)
  122.                         else
  123.                             request(k,v)
  124.                         end
  125.                     elseif (q == chest.getInventorySize() and not invSpaceFound) then
  126.                         sleep(10)
  127.                     end
  128.                 end
  129.             end
  130.         end
  131.     end
  132. end
  133.  
  134. local function run()
  135.     while (true) do
  136.         detectPeripherals()
  137.         retrieveData()
  138.         calculate()
  139.         sleep(600)
  140.     end
  141. end
  142.  
  143. run()
Advertisement
Add Comment
Please, Sign In to add comment