Advertisement
MajorVictory

GraviChestplate AutoCharger

Apr 28th, 2013
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.90 KB | None | 0 0
  1.  
  2. -- number of seconds before rebooting the machine, use -1 to disable
  3. local timeToReboot = 300
  4.  
  5. --time in seconds between updates
  6. local sleepTime = 0.5
  7.  
  8. -- how long to wait whe nthe item is nearly fully charged before removing it
  9. local waitForFullChargeTime = 20
  10.  
  11. -- the itemid of item to charge
  12. local chargeItemID = 30473
  13.  
  14. sorter = peripheral.wrap("right")
  15.  
  16.  
  17. local seconds = 0
  18. function SecondsToClock(sSeconds)
  19.     local nSeconds = tonumber(sSeconds)
  20.     if nSeconds == 0 then
  21.         return "00:00:00";
  22.     else
  23.         nHours = string.format("%02d", math.floor(nSeconds/3600));
  24.         nMins = string.format("%02d", math.floor(nSeconds/60 - (nHours*60)));
  25.         nSecs = string.format("%02d", math.floor(nSeconds - nHours*3600 - nMins *60));
  26.         return nHours..":"..nMins..":"..nSecs
  27.     end
  28. end
  29.  
  30.  
  31. --[[ Original code by NeverCast, revised for v3.0 by lost_RD
  32.      This should be loaded like a typical api
  33.      Feel free to name it what you like
  34.      This paste created for http://ftbwiki.org/Interactive_Sorter
  35.      Fixed by MajorVictory (April 28, 2013)
  36. --]]
  37. local directions = { [0]=0,[1]=1,[2]=2,[3]=3,[4]=4,[5]=5,["down"] = 0, ["up"] = 1, ["-Z"] = 2, ["+Z"] = 3, ["-X"] = 4, ["+X"] = 5, ["+Y"] = 1, ["-Y"] = 0}
  38. directions.south = directions["+Z"]
  39. directions.east = directions["+X"]
  40. directions.north = directions["-Z"]
  41. directions.west = directions["-X"]
  42.  
  43. -- Gets the Unique ID based on the ID and Meta
  44. function getUUID(id, meta)
  45.     uuid = id + meta * 32768
  46.     return uuid
  47. end
  48.  
  49. -- Get a stack table from a single uuid and amount
  50. -- Valid for version 3.0
  51. function getID(uuid)
  52.     local durability, id = 0, uuid
  53.  
  54.     if uuid > 32768 then
  55.         id = uuid % 32768
  56.         durability = (uuid - id) / 32768
  57.     end
  58.  
  59.     return id, durability
  60. end
  61.  
  62. function getStack(uuid, c)
  63.     local stack = {}
  64.  
  65.     stack.id, stack.meta = getID(uuid)
  66.  
  67.     stack.count = c
  68.     stack.uuid = uuid
  69.  
  70.     return stack
  71. end
  72.  
  73.  
  74. -- Get stacks from an Interactive Sorter
  75. -- direction   : the direction of the Interactive Sorter Peripheral
  76. -- invDirection: the direction of the inventory from the peripheral
  77. -- valid options for invDirection are 0,1,2,3,4,5 ( original values),
  78. -- north, south, east, west, up, down, and the +/-X,Y,Z strings.
  79. -- (see directions variable)
  80. function getStacks(direction, invDirection)
  81.     if not peripheral.isPresent(direction) then
  82.         return false, {}
  83.     end
  84.  
  85.     if peripheral.getType(direction) ~= "interactiveSorter" then
  86.         return false, {}
  87.     end
  88.  
  89.     local stacks = {}
  90.     local lowerinv = peripheral.call(direction, "list", directions[invDirection])
  91.  
  92.     if lowerinv ~= nil then
  93.         for uuid,count in pairs(peripheral.call(direction, "list", directions[invDirection])) do
  94.             table.insert(stacks, getStack(uuid, count))
  95.         end
  96.         return true, stacks
  97.     else
  98.         return false, {}
  99.     end
  100. end
  101.  
  102.  
  103. function table.contains(table, element)
  104.     for _, value in pairs(table) do
  105.         if value == element then
  106.             return true
  107.         end
  108.     end
  109.     return false
  110. end
  111.  
  112. local unchargedIDs = {}
  113. for n=0,25 do table.insert(unchargedIDs, getUUID(chargeItemID, n)) end
  114.  
  115. local isRunning = true
  116. local reboot = false
  117. local charging = false
  118. local status1,status2,status3 = "","",""
  119. local w,h = term.getSize()
  120.  
  121. function printStatus()
  122.     term.clear()
  123.  
  124.     local clockTime = "Time: "..SecondsToClock(seconds)
  125.     term.setCursorPos(w - #clockTime+1, 1)
  126.     term.setBackgroundColor(colors.white)
  127.     term.setTextColor(colors.black)
  128.     term.write(clockTime)
  129.  
  130.     if timeToReboot > 0 then
  131.         local rebootDisplay = "Reboot in: "..string.format("%d",(timeToReboot - seconds))
  132.         term.setCursorPos(w - #rebootDisplay+1, 2)
  133.         term.write(rebootDisplay)
  134.     end
  135.  
  136.  
  137.     term.setBackgroundColor(colors.black)
  138.     term.setTextColor(colors.white)
  139.  
  140.     term.setCursorPos(1,1)
  141.     term.write(status1)
  142.     term.setCursorPos(1,2)
  143.     term.write(status2)
  144.     -- uncomment for debug infos
  145.     --term.setCursorPos(1,3)
  146.     --term.write(status3)
  147.     --term.setCursorPos(1,4)
  148.     --term.write(status4)
  149.     --term.setCursorPos(1,5)
  150.     --term.write(status5)
  151. end
  152.  
  153. while isRunning do
  154.     printStatus()
  155.  
  156.     local _, lowerInv = getStacks("right","down")
  157.     local lowerInvSize = #lowerInv
  158.  
  159.     status5 = lowerInvSize
  160.  
  161.     if not charging and lowerInvSize > 0 then
  162.         charging = true
  163.         status1 = "Charging item..."
  164.         status2 = "Waiting for item to finish..."
  165.     end
  166.  
  167.     printStatus()
  168.  
  169.     if not charging then
  170.  
  171.         for uuid,count in pairs(sorter.list(directions.up)) do
  172.  
  173.             local id, meta = getID(uuid)
  174.             meta = (27 - meta)
  175.  
  176.             status1 = "Looking for item to charge..."
  177.             status2 = ""
  178.  
  179.             status3 = "Item: "..id.." ("..meta.."/27)"
  180.             status4 = "uuid: "..uuid
  181.             printStatus()
  182.             seconds = seconds + 1
  183.             sleep(1)
  184.  
  185.             if not charging and id == chargeItemID and meta < 26 then
  186.  
  187.                 if lowerInvSize < 1 then
  188.  
  189.                     status1 = "Charging item..."
  190.                     printStatus()
  191.  
  192.                     sorter.extract(directions.up, uuid, directions.down, 1)
  193.                     charging = true
  194.                 end
  195.  
  196.             end
  197.         end
  198.  
  199.     elseif lowerInvSize > 0 then
  200.  
  201.         for uuid,count in pairs(sorter.list(directions.down)) do
  202.  
  203.             local id, meta = getID(uuid)
  204.             meta = (27.0 - meta)
  205.  
  206.             status1 = "Charging item..."
  207.  
  208.             status3 = "Charging Item: "..id.." ("..meta.."/27)"
  209.             status4 = "uuid: "..uuid
  210.  
  211.             if id == chargeItemID and meta == 26 then
  212.  
  213.                 local waitleft = waitForFullChargeTime
  214.  
  215.                 while waitleft > 0 do
  216.                     status1 = "Charging finishing in "..waitleft.." seconds..."
  217.                     printStatus()
  218.                     waitleft = waitleft - 1
  219.                     seconds = seconds + 1
  220.                     sleep(1)
  221.                 end
  222.  
  223.                 status1 = "Finished charging, moving item back up."
  224.                 printStatus()
  225.  
  226.                 sorter.extract(directions.down, uuid, directions.up, 1)
  227.                 charging = false
  228.             else
  229.                 status2 = "Waiting for item to finish...("..meta.."/27)"
  230.                 printStatus()
  231.             end
  232.         end
  233.  
  234.     end
  235.  
  236.     if not charging and timeToReboot > 0 and seconds >= timeToReboot then
  237.         term.clear()
  238.         term.setCursorPos(1,1)
  239.         term.write("Rebooting...")
  240.         reboot = true
  241.         isRunning = false
  242.         sleep(2)
  243.     else
  244.         seconds = seconds + sleepTime
  245.         sleep(sleepTime)
  246.     end
  247. end
  248.  
  249. if reboot then os.reboot() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement