Savage_Me55iah

CC Stock Keeper

Jun 26th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.13 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. timer = 0.1
  33. facing = "north"
  34. input = {"bottom", "top", "front", "back"}
  35. side = {"front", "right", "back", "left"}
  36. sideReverse = {["bottom"] = "up", ["top"] = "down", ["left"] = "right", ["right"] = "left", ["front"] = "back", ["back"] = "front"}
  37. directions = {"north", "east", "south", "west"}
  38. directionsReverse = {["down"] = "up", ["up"] = "down", ["north"] = "south", ["south"] = "north", ["east"] = "west", ["west"] = "east"}
  39. output = "bottom"
  40. craftingLoc = {1, 2, 3, 5, 6, 7, 9, 10, 11}
  41. redstone.setOutput("back", false)
  42.  
  43. chestTranslate = {["up"] = "up", ["down"] = "down"}
  44. for a,b in pairs(directions) do
  45.     if b == facing then
  46.         e = a
  47.         for c,d in pairs(side) do
  48.             chestTranslate[d] = directions[e]
  49.             e = e + 1
  50.             if e > 4 then e = 1 end
  51.         end
  52.     end
  53. end
  54.  
  55. table.insert(side, "top") table.insert(side, "bottom") table.insert(directions, "up") table.insert(directions, "down")
  56.  
  57. if fs.exists("dataStore") then
  58.     local fh = fs.open("dataStore", 'r')
  59.     data = textutils.unserialize(fh:readAll())
  60.     fh.close()
  61. end
  62. data = data or {}
  63.  
  64. function wrapper(param)
  65.     local result = false
  66.     result = peripheral.wrap(param) or false
  67.     if result == false then print("missing chest @ "..param) end
  68.     return result
  69. end
  70.  
  71. chest = wrapper(output)
  72.  
  73. function crafting(paramA, paramB)
  74.     local result, tempPeripheral, inputData, materialsData, requiredMaterials, gatheringMaterials, matReturn = false, false, {}, {}, true, true, {}
  75.     print(paramA)
  76.     for a,b in pairs(input) do
  77.         tempPeripheral = wrapper(b)
  78.         if tempPeripheral ~= false then
  79.             tempPeripheral.condenseItems()
  80.             inputData[b] = tempPeripheral.getAllStacks()
  81.         end
  82.     end
  83.     for a,b in pairs(inputData) do
  84.         for c,d in pairs(inputData[a]) do
  85.             materialsData[inputData[a][c]["id"]..":"..inputData[a][c]["rawName"]] = (materialsData[inputData[a][c]["id"]..":"..inputData[a][c]["rawName"]] or 0) + inputData[a][c]["qty"]
  86.         end
  87.     end
  88.     for a,b in pairs(data[paramA][paramB]["materials"]) do
  89.         if tostring(materialsData[a]) ~= "nil" then
  90.             if tonumber(materialsData[a]) < b then
  91.                 requiredMaterials = false
  92.             end
  93.         else
  94.             requiredMaterials = false
  95.         end
  96.     end
  97.     if requiredMaterials then
  98.         for y,z in pairs(data[paramA][paramB]["recipe"]) do
  99.             done = false
  100.             inputData = {}
  101.             for a,b in pairs(input) do
  102.                 tempPeripheral = wrapper(b)
  103.                 if tempPeripheral ~= false then
  104.                     tempPeripheral.condenseItems()
  105.                     inputData[b] = tempPeripheral.getAllStacks()
  106.                 end
  107.             end
  108.             for a,b in pairs(inputData) do
  109.                 for c,d in pairs(inputData[a]) do
  110.                     if done == false then
  111.                         if inputData[a][c]["id"]..":"..inputData[a][c]["rawName"] == data[paramA][paramB]["recipe"][y]["id"] then
  112.                             if inputData[a][c]["qty"] >= data[paramA][paramB]["recipe"][y]["qty"] then
  113.                                 tempPeripheral, matReturn[y] = wrapper(a), a
  114. --                              print(y..":"..data[paramA][paramB]["recipe"][y]["id"]..":"..c..":"..data[paramA][paramB]["recipe"][y]["qty"]..":"..craftingLoc[y])
  115.                                 tempPeripheral.pushItemIntoSlot(chestTranslate[sideReverse[a]], c, data[paramA][paramB]["recipe"][y]["qty"], craftingLoc[y])
  116.                                 done = true
  117.                             end
  118.                         end
  119.                     end
  120.                 end
  121.             end
  122.         end
  123. --      sleep(timer)
  124. --      print("--")
  125.         turtle.select(4)
  126.         done = true
  127.         for y,z in pairs(data[paramA][paramB]["recipe"]) do
  128. --          print(y..":"..turtle.getItemCount(y)..":"..data[paramA][paramB]["recipe"][y]["qty"])
  129.             if turtle.getItemCount(craftingLoc[y]) == data[paramA][paramB]["recipe"][y]["qty"] then
  130.                 done = done
  131.             else
  132.                 done = false
  133.             end
  134.         end
  135.         if done then result = turtle.craft() end
  136.         for a,b in pairs(matReturn) do
  137.             tempPeripheral = wrapper(matReturn[a])
  138.             tempPeripheral.pullItemIntoSlot(chestTranslate[sideReverse[matReturn[a]]], craftingLoc[a], 64)
  139.         end
  140.         tempPeripheral = wrapper(output)
  141.         tempPeripheral.pullItemIntoSlot(chestTranslate[sideReverse[output]], 4, 64)
  142.     end
  143.     return result
  144. end
  145.  
  146. function scan()
  147.     local craft, tempListA, tempListB, result = true, nil, {}, false
  148.     for a,b in pairs(data) do
  149.         chest.condenseItems()
  150. --      sleep(0.5)
  151.         tempListA = chest.getAllStacks()
  152.         tempListB = {}
  153.         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
  154.         if data[a]["min"] ~= false then
  155.             if tostring(tempListB[a]) == "nil" or tempListB[a] < data[a]["min"] then
  156.                 craft = crafting(a, "craftMin")
  157.                 result = true
  158.             else
  159.                 craft = false
  160.             end
  161.         end
  162.         if tostring(tempListB[a]) ~= "nil" and data[a]["max"] ~= false then
  163.             if tempListB[a] > data[a]["max"] then
  164.                 craft = crafting(a, "craftMax")
  165.                 result = true
  166.             else
  167.                 craft = false
  168.             end
  169.         end
  170.     end
  171.     return result
  172. end
  173.    
  174.    
  175. if tArgs[1] == "mem" then
  176.     local tempPeripheral, listA, listB, paramMin, paramMax, tempData, num, hold = wrapper(output), nil, {}, nil, nil, data, 0, nil
  177.     local lock = true
  178.     while lock do
  179.         term.clear() term.setCursorPos(1,1)
  180.         print("needs empty chest @ output to work")
  181.         print("place result item in first slot of turtle and hit enter")
  182.         read()
  183.         if turtle.getItemCount(1) >= 1 then lock = false end
  184.     end
  185.     tempPeripheral.pullItemIntoSlot(chestTranslate[sideReverse[output]], 1, 1, 1)
  186.     listA = tempPeripheral.getAllStacks()
  187.     hold = listA[1]["id"]..":"..listA[1]["rawName"]
  188.     tempData[hold] = {["min"] = false, ["max"] = false, ["craftMin"] = {["recipe"] = false, ["materials"] = false}, ["craftMax"] = {["recipe"] = false, ["materials"] = false}}
  189.     tempPeripheral.pushItemIntoSlot(chestTranslate[sideReverse[output]], 1, 1, 1)
  190.     term.clear() term.setCursorPos(1,1)
  191.     print("type minium amount to keep stocked or false and hit enter")
  192.     tempData[hold]["min"] = tonumber(read()) or false
  193.     term.clear() term.setCursorPos(1,1)
  194.     print("type maxium amount to keep stocked or false and hit enter")
  195.     tempData[hold]["max"] = tonumber(read()) or false
  196.     if tempData[hold]["min"] ~= false then
  197.         lock = true
  198.         while lock do
  199.             term.clear() term.setCursorPos(1,1)
  200.             print("remove previous setup items")
  201.             print("place minium craft recipe in top-left 3x3 format and hit enter")
  202.             read()
  203.             num = 0
  204.             for a,b in pairs(craftingLoc) do num = num + turtle.getItemCount(b) end
  205.             if num >= 1 then lock = false end
  206.         end
  207.         for a,b in pairs(craftingLoc) do tempPeripheral.pullItemIntoSlot(chestTranslate[sideReverse[output]], b, 64, a) end
  208.         listA = tempPeripheral.getAllStacks()
  209.         tempData[hold]["craftMin"]["materials"], tempData[hold]["craftMin"]["recipe"] = {}, {}
  210.         for a,b in pairs(listA) do
  211.             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"]
  212.             tempData[hold]["craftMin"]["recipe"][a] = {["id"] = listA[a]["id"]..":"..listA[a]["rawName"], ["qty"] = listA[a]["qty"]}
  213.         end
  214.         for a,b in pairs(craftingLoc) do tempPeripheral.pushItemIntoSlot(chestTranslate[sideReverse[output]], a, 64, b) end
  215.     end
  216.     if tempData[hold]["max"] ~= false then
  217.         lock = true
  218.         while lock do
  219.             term.clear() term.setCursorPos(1,1)
  220.             print("remove previous setup items")
  221.             print("place maxium craft recipe in top-left 3x3 format and hit enter")
  222.             read()
  223.             num = 0
  224.             for a,b in pairs(craftingLoc) do num = num + turtle.getItemCount(b) end
  225.             if num >= 1 then lock = false end
  226.         end
  227.         for a,b in pairs(craftingLoc) do tempPeripheral.pullItemIntoSlot(chestTranslate[sideReverse[output]], b, 64, a) end
  228.         listA = tempPeripheral.getAllStacks()
  229.         tempData[hold]["craftMax"]["materials"], tempData[hold]["craftMax"]["recipe"] = {}, {}
  230.         for a,b in pairs(listA) do
  231.             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"]
  232.             tempData[hold]["craftMax"]["recipe"][a] = {["id"] = listA[a]["id"]..":"..listA[a]["rawName"], ["qty"] = listA[a]["qty"]}
  233.         end
  234.         for a,b in pairs(craftingLoc) do tempPeripheral.pushItemIntoSlot(chestTranslate[sideReverse[output]], a, 64, b) end
  235.     end
  236.     data = tempData
  237.     local fh = fs.open("dataStore", 'w')
  238.     fh.write(textutils.serialize(data))
  239.     fh.close()
  240. else
  241.     while true do
  242.         local result = false
  243.         result = scan()
  244.         if not result then
  245.             os.startTimer(1)
  246.             os.pullEvent()
  247.         end
  248.     end
  249. end
Add Comment
Please, Sign In to add comment