Savage_Me55iah

CC Miner Compressor

Jun 24th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.65 KB | None | 0 0
  1. --Script by Savage_Me55iah of CraftAU.com.au and Nuqube.co--
  2. local scriptName, version, pastebinCode = "StockKeeper", "v1.0", "iWHbXKyf"
  3.  
  4. --Other Savage_Me55iah scripts @ http://pastebin.com/u/Savage_Me55iah
  5.  
  6. --INSTRUCTIONS ON USE--
  7. --Instruction video on http://youtube.com/savagemessiah5
  8.  
  9. --Contact Savage_Me55iah @ CraftAU.com.au or Nuqube.co or http://www.computercraft.info/forums2/ with suggestions/fixes/bugs
  10.  
  11. -- CHANGELOG --
  12. --V1.0
  13.  
  14. -- SETUP --
  15.  
  16. local tArgs = {...}
  17.  
  18. do
  19.     update = function(script_name, pastebincode)
  20.         print("UPDATING") shell.run("pastebin get "..pastebincode.." update")
  21.         if fs.exists("update") then
  22.             fs.delete(script_name) fs.copy("update", script_name) fs.delete("update") print("UPDATE SUCCESS")
  23.         else
  24.             print("UPDATE FAILED")
  25.         end
  26.         sleep(2) os.reboot()
  27.     end
  28. end
  29.  
  30. if tArgs[1] == "update" then update(scriptName, pastebinCode) end
  31.  
  32. input = {"bottom", "top", "front", "back"}
  33. sideReverse = {["bottom"] = "up", ["top"] = "down", ["left"] = "right", ["right"] = "left", ["front"] = "back", ["back"] = "front"}
  34. output = "bottom"
  35. craftingLoc = {1, 2, 3, 5, 6, 7, 9, 10, 11}
  36. redstone.setOutput("back", false)
  37.  
  38. if fs.exists("dataStore") then
  39.     local fh = fs.open("dataStore", 'r')
  40.     data = textutils.unserialize(fh:readAll())
  41.     fh.close()
  42. end
  43. data = data or {}
  44.  
  45. function wrapper(param)
  46.     local result = false
  47.     result = peripheral.wrap(param) or false
  48.     if result == false then print("missing chest @ "..param) end
  49.     return result
  50. end
  51.  
  52. chest = wrapper(output)
  53.  
  54. function crafting(param)
  55.     local result, tempPeripheral, inputData, materialsData, requiredMaterials, gatheringMaterials, matReturn = false, false, {}, {}, true, true, {}
  56.     for a,b in pairs(input) do
  57.         tempPeripheral = wrapper(b)
  58.         if tempPeripheral ~= false then
  59.             tempPeripheral.condenseItems()
  60.             inputData[b] = tempPeripheral.getAllStacks()
  61.         end
  62.     end
  63.     for a,b in pairs(inputData) do
  64.         for c,d in pairs(inputData[a]) do
  65.             materialsData[inputData[a][c]["id"]..":"..inputData[a][c]["rawName"]] = (materialsData[inputData[a][c]["id"]..":"..inputData[a][c]["rawName"]] or 0) + inputData[a][c]["qty"]
  66.         end
  67.     end
  68.     for a,b in pairs(param["materials"]) do
  69.         if tostring(materialsData[a]) ~= "nil" then
  70.             if tonumber(materialsData[a]) < b then
  71.                 requiredMaterials = false
  72.             end
  73.         end
  74.     end
  75.     if requiredMaterials then
  76.         for y,z in pairs(param["recipe"]) do
  77.             for a,b in pairs(input) do
  78.                 tempPeripheral = wrapper(b)
  79.                 if tempPeripheral ~= false then
  80.                     tempPeripheral.condenseItems()
  81.                     inputData[b] = tempPeripheral.getAllStacks()
  82.                 end
  83.             end
  84.             for a,b in pairs(inputData) do
  85.                 for c,d in pairs(inputData[a]) do
  86.                     if inputData[a][c]["id"]..":"..inputData[a][c]["rawName"] == param["recipe"][y]["id"] then
  87.                         if inputData[a][c]["qty"] >= param["recipe"][y]["qty"] then
  88.                             tempPeripheral, matReturn[y] = wrapper(a), a
  89.                             tempPeripheral.pushItemIntoSlot(sideReverse[a], c, param["recipe"][y]["qty"], craftingLoc[y])
  90.                         end
  91.                     end
  92.                 end
  93.             end
  94.         end
  95.         turtle.select(4)
  96.         result = turtle.craft()
  97.         for a,b in pairs(matReturn) do
  98.             tempPeripheral = wrapper(matReturn[a])
  99.             tempPeripheral.pullItemIntoSlot(sideReverse[matReturn[a]], craftingLoc[a], 64)
  100.         end
  101.         tempPeripheral = wrapper(output)
  102.         tempPeripheral.pullItemIntoSlot(sideReverse[output], 4, 64)
  103.     end
  104.     return result
  105. end
  106.  
  107. function scan()
  108.     local craft, tempListA, tempListB, result = true, nil, {}, false
  109.     for a,b in pairs(data) do
  110.         chest.condenseItems()
  111.         tempListA = chest.getAllStacks()
  112.         tempListB = {}
  113.         for a,b in pairs(tempListA) do tempListB[tempListA[a]["id"]..":"..tempListA[a]["rawName"]] = (tempListB[tempListA[a]["id"]..":"..tempListA[a]["rawName"]] or 0) + tempListA[a]["qty"] end
  114.         if data[a]["min"] ~= false then
  115.             if tostring(tempListB[a]) == "nil" or tempListB[a] < data[a]["min"] then
  116.                 craft = crafting(data[a]["craftMin"])
  117.                 result = true
  118.             else
  119.                 craft = false
  120.             end
  121.         end
  122.         if tostring(tempListB[a]) ~= "nil" and data[a]["max"] ~= false then
  123.             if tempListB[a] > data[a]["max"] then
  124.                 craft = crafting(data[a]["craftMax"])
  125.                 result = true
  126.             else
  127.                 craft = false
  128.             end
  129.         end
  130.     end
  131.     return result
  132. end
  133.    
  134.    
  135. if tArgs[1] == "mem" then
  136.     local tempPeripheral, listA, listB, paramMin, paramMax, tempData, num, hold = wrapper(output), nil, {}, nil, nil, data, 0, nil
  137.     local lock = true
  138.     while lock do
  139.         term.clear() term.setCursorPos(1,1)
  140.         print("needs empty chest @ output to work")
  141.         print("place result item in first slot of turtle and hit enter")
  142.         read()
  143.         if turtle.getItemCount(1) >= 1 then lock = false end
  144.     end
  145.     tempPeripheral.pullItemIntoSlot(sideReverse[output], 1, 1, 1)
  146.     listA = tempPeripheral.getAllStacks()
  147.     hold = listA[1]["id"]..":"..listA[1]["rawName"]
  148.     tempData[hold] = {["min"] = false, ["max"] = false, ["craftMin"] = {["recipe"] = false, ["materials"] = false}, ["craftMax"] = {["recipe"] = false, ["materials"] = false}}
  149.     tempPeripheral.pushItemIntoSlot(sideReverse[output], 1, 1, 1)
  150.     term.clear() term.setCursorPos(1,1)
  151.     print("type minium amount to keep stocked or false and hit enter")
  152.     tempData[hold]["min"] = tonumber(read()) or false
  153.     term.clear() term.setCursorPos(1,1)
  154.     print("type maxium amount to keep stocked or false and hit enter")
  155.     tempData[hold]["max"] = tonumber(read()) or false
  156.     if tempData[hold]["min"] ~= false then
  157.         lock = true
  158.         while lock do
  159.             term.clear() term.setCursorPos(1,1)
  160.             print("remove previous setup items")
  161.             print("place minium craft recipe in top-left 3x3 format and hit enter")
  162.             read()
  163.             num = 0
  164.             for a,b in pairs(craftingLoc) do num = num + turtle.getItemCount(b) end
  165.             if num >= 1 then lock = false end
  166.         end
  167.         for a,b in pairs(craftingLoc) do tempPeripheral.pullItemIntoSlot(sideReverse[output], b, 64, a) end
  168.         listA = tempPeripheral.getAllStacks()
  169.         tempData[hold]["craftMin"]["materials"], tempData[hold]["craftMin"]["recipe"] = {}, {}
  170.         for a,b in pairs(listA) do
  171.             tempData[hold]["craftMin"]["materials"][listA[a]["id"]..":"..listA[a]["rawName"]] = (tempData[hold]["craftMin"]["materials"][listA[a]["id"]..":"..listA[a]["rawName"]] or 0) + listA[a]["qty"]
  172.             tempData[hold]["craftMin"]["recipe"][a] = {["id"] = listA[a]["id"]..":"..listA[a]["rawName"], ["qty"] = listA[a]["qty"]}
  173.         end
  174.         for a,b in pairs(craftingLoc) do tempPeripheral.pushItemIntoSlot(sideReverse[output], a, 64, b) end
  175.     end
  176.     if tempData[hold]["max"] ~= false then
  177.         lock = true
  178.         while lock do
  179.             term.clear() term.setCursorPos(1,1)
  180.             print("remove previous setup items")
  181.             print("place maxium craft recipe in top-left 3x3 format and hit enter")
  182.             read()
  183.             num = 0
  184.             for a,b in pairs(craftingLoc) do num = num + turtle.getItemCount(b) end
  185.             if num >= 1 then lock = false end
  186.         end
  187.         for a,b in pairs(craftingLoc) do tempPeripheral.pullItemIntoSlot(sideReverse[output], b, 64, a) end
  188.         listA = tempPeripheral.getAllStacks()
  189.         tempData[hold]["craftMax"]["materials"], tempData[hold]["craftMax"]["recipe"] = {}, {}
  190.         for a,b in pairs(listA) do
  191.             tempData[hold]["craftMax"]["materials"][listA[a]["id"]..":"..listA[a]["rawName"]] = (tempData[hold]["craftMax"]["materials"][listA[a]["id"]..":"..listA[a]["rawName"]] or 0) + listA[a]["qty"]
  192.             tempData[hold]["craftMax"]["recipe"][a] = {["id"] = listA[a]["id"]..":"..listA[a]["rawName"], ["qty"] = listA[a]["qty"]}
  193.         end
  194.         for a,b in pairs(craftingLoc) do tempPeripheral.pushItemIntoSlot(sideReverse[output], a, 64, b) end
  195.     end
  196.     data = tempData
  197.     local fh = fs.open("dataStore", 'w')
  198.     fh.write(textutils.serialize(data))
  199.     fh.close()
  200. else
  201.     while true do
  202.         local result = false
  203.         result = scan()
  204.         if not result then
  205.             os.startTimer(1)
  206.             os.pullEvent()
  207.         end
  208.     end
  209. end
Advertisement
Add Comment
Please, Sign In to add comment