ToLazyToThink

serum refiller

Aug 25th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.32 KB | None | 0 0
  1.  
  2. local args = {...}
  3.  
  4. local serumSlot = 2
  5.  
  6. function dumpSerum()
  7.   while turtle.getItemCount(1) > 0 do
  8.     turtle.select(1)
  9.     while not turtle.drop() do
  10.       print("failed to drop serum")
  11.       sleep(1)
  12.     end
  13.   end
  14. end
  15.  
  16. function synthesizerDone()
  17.   local mach = peripheral.wrap("bottom")
  18.   local itemInfo = mach.getStackInSlot(serumSlot)
  19.  
  20.   return itemInfo ~= nil and itemInfo["dmg"] == 0
  21. end
  22.  
  23. function purifierDone()
  24.   local mach = peripheral.wrap("bottom")
  25.   local itemInfo = mach.getStackInSlot(serumSlot)
  26.  
  27.   -- shouldn't happen
  28.   if itemInfo == nil then
  29.     return false
  30.   end
  31.  
  32.   -- Since there's nothing better to go by, we're
  33.   --  going to guess if it's done based on how
  34.   --  how it's energy storage changes over the course
  35.   --  of a few seconds.
  36.  
  37.   -- If we're low on BNA, assume we're waiting for more
  38.   local bnaTank = mach.getTanks("unknown")[1]
  39.   if (bnaTank.amount == nil or bnaTank.amount < 1000) then
  40.     return false
  41.   end
  42.  
  43.   local maxPow = 0 + mach.getMaxEnergyStored()
  44.   local startPow = 0 + mach.getEnergyStored()
  45.   sleep(2)
  46.   local endPow = 0 + mach.getEnergyStored()
  47.  
  48.   -- if the machine is almost full, assume we're done
  49.   if (maxPow - endPow) < 10 then
  50.     return true
  51.   end
  52.  
  53.   -- I'm afraid of odd ball behavior when we're almost
  54.   --   out of power, let's play it safe
  55.   if endPow < 1000 then
  56.     return false
  57.   end
  58.  
  59.   -- if we had a gain in energy, we're done
  60.   if endPow - startPow > 20 then
  61.     return true
  62.   end
  63.  
  64.   -- Either there is an energy shortage, or we're still
  65.   --  working
  66.   return false
  67. end
  68.  
  69. local machineDone = nil
  70. if args[1] == "purifier" then
  71.   machineDone = purifierDone
  72. elseif args[1] == "synthesizer" then
  73.   machineDone = synthesizerDone
  74. else
  75.   print("bad machine name, or arg")
  76.   print("expected one of: purifier, synthesizer")
  77.   error()
  78. end
  79.  
  80. function machineEmpty()
  81.   local mach = peripheral.wrap("bottom")
  82.   local itemInfo = mach.getStackInSlot(serumSlot)
  83.  
  84.   return itemInfo == nil
  85. end
  86.  
  87. function transferSerum()
  88.   turtle.select(1)
  89.   turtle.suckDown()
  90. end
  91.  
  92. function main()
  93.   dumpSerum()
  94.   while true do
  95.     if machineEmpty() then
  96.       sleep(15)
  97.     else
  98.       if machineDone() then
  99.         transferSerum()
  100.         dumpSerum()
  101.       else
  102.         sleep(1)
  103.       end
  104.     end
  105.   end
  106. end
  107.  
  108. main()
Advertisement
Add Comment
Please, Sign In to add comment