pruby

breeder

Oct 24th, 2012
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local tArgs = { ... }
  2.  
  3. reactor = {
  4.   controlFace = 'bottom',
  5.   senseFace = 'front',
  6.   turnedOn = false,
  7.   on = function()
  8.     rs.setOutput(reactor.controlFace, true)
  9.     turnedOn = true
  10.   end,
  11.   off = function()
  12.     rs.setOutput(reactor.controlFace, false)
  13.     turnedOn = false
  14.   end,
  15.   -- Detects whether the reactor has been disabled
  16.   -- False unless both we and the thermal monitor allow it to run
  17.   isRunning = function()
  18.     return not rs.getInput(reactor.senseFace)
  19.   end,
  20.   -- Run the reactor until the thermal monitor stops it
  21.   heat = function()
  22.     reactor.on()
  23.     sleep(0.1)
  24.     while reactor.isRunning() do
  25.       sleep(1)
  26.     end
  27.     reactor.off()
  28.   end,
  29.   -- Run the reactor for a fixed duration
  30.   run = function(duration)
  31.     reactor.on()
  32.     sleep(duration)
  33.     reactor.off()
  34.   end
  35. }
  36.  
  37. breeder = {
  38.   iceInventorySlot = 3,
  39.   cellInventorySlot = 4,
  40.   -- Run the reactor, chilling if necessary to start
  41.   run = function(duration)
  42.     reactor.on()
  43.     sleep(0.1)
  44.     if not reactor.isRunning() then
  45.       reactor.off()
  46.       breeder.chill()
  47.       reactor.on()
  48.       sleep(0.1)
  49.     end
  50.     reactor.run(duration)
  51.   end,
  52.   replaceCoolant = function()
  53.     breeder.getCoolant()
  54.     breeder.putCoolant()
  55.   end,
  56.   -- Get a pair of coolant cells
  57.   getCoolant = function()
  58.     turtle.turnLeft()
  59.     for slot = 1,2 do
  60.       turtle.select(slot)
  61.       while not turtle.suck() do
  62.         print("No coolant left")
  63.         io.read("*l")
  64.       end
  65.     end
  66.     turtle.turnRight()
  67.   end,
  68.   -- Put in the reactor, running until we can
  69.   putCoolant = function()
  70.     for slot = 1,2 do
  71.       turtle.select(slot)
  72.       local success = turtle.drop()
  73.       while not success do
  74.         breeder.run(5.0)
  75.         turtle.select(slot)
  76.         success = turtle.drop()
  77.       end
  78.     end
  79.   end,
  80.   -- Use a single piece of ice to chill the reactor
  81.   -- Brings below the thermal monitor's cutoff, letting the reactor run
  82.   chill = function()
  83.     if not reactor.isRunning() then
  84.       breeder.getIce()
  85.       breeder.swapInIce()
  86.     end
  87.   end,
  88.   getIce = function()
  89.     turtle.turnRight()
  90.     print("Getting ice")
  91.     turtle.select(breeder.iceInventorySlot)
  92.     turtle.suck()
  93.     -- Put back all but one
  94.     if turtle.getItemCount(breeder.iceInventorySlot) > 1 then
  95.       turtle.drop(turtle.getItemCount(breeder.iceInventorySlot) - 1)
  96.     end
  97.     turtle.turnLeft()
  98.   end,
  99.   -- Melt ice in reactor
  100.   swapInIce = function()
  101.     -- Pull out top left cell
  102.     turtle.select(breeder.cellInventorySlot)
  103.     turtle.suck()
  104.     -- Put ice in reactor
  105.     turtle.select(breeder.iceInventorySlot)
  106.     local success = turtle.drop()
  107.     if success then
  108.       print("Chilled")
  109.     end
  110.     -- Wait for it to melt
  111.     sleep(2.0)
  112.     -- Put cell back in
  113.     turtle.select(breeder.cellInventorySlot)
  114.     turtle.drop()
  115.   end,
  116.   -- Run a series of breeder cycles
  117.   doCycles = function(nCycles)
  118.     local i
  119.     for i = 1,nCycles do
  120.       print("Cycle " .. i .. "/" .. nCycles)
  121.       reactor.heat()
  122.       breeder.replaceCoolant()
  123.       breeder.writeRemainingFile(nCycles - i)
  124.       breeder.run(65.0)
  125.     end
  126.   end,
  127.   -- Writes a script to "remaining" which will finish the current run if executed
  128.   writeRemainingFile = function(cycles)
  129.     local h = io.open('remaining', 'w')
  130.     h:write("shell.run('breeder', ")
  131.     h:write(cycles)
  132.     h:write(")")
  133.     h:close()
  134.   end,
  135.   orientToFaceReactor = function()
  136.     reactor.off()
  137.     sleep(0.2)
  138.     while not rs.getInput('front') do
  139.       turtle.turnRight()
  140.     end
  141.   end
  142. }
  143.  
  144. local nCycles = tArgs[1] or "6"
  145. breeder.orientToFaceReactor()
  146. print("Initial heat")
  147. reactor.heat()
  148. breeder.doCycles(nCycles)
Advertisement
Add Comment
Please, Sign In to add comment