Guest User

BreederControl.lua

a guest
Mar 20th, 2013
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.47 KB | None | 0 0
  1. -- Specific to reactor configuration and placement
  2. local reactorSide = "left"        -- Used to output redstone signal to run the reactor
  3. local reactorRelPos = "0,-1,-1"   -- Relative position from sensor, check "sensorview"
  4. local maxCookDuration = 716       -- How long does it take (seconds) in THIS reactor to enrich a cell
  5. local cellSlots = {2, 10, 12, 20} -- Where are the slots for depleted cells
  6. local uraniumSlots = {11}         -- What slots do you want to monitor for uranium, factors into getCookDuration()
  7.  
  8. -- Predefining vars, no need to change these
  9. local cooking = false
  10. local cookTimer = 0
  11. local startedTime = 0
  12. local cookDuration = 0
  13.  
  14. -- Find a monitor
  15. local monitor = nil
  16. local monitorWidth, monitorHeight = 0
  17. for sideId, side in ipairs(redstone.getSides()) do
  18.    if peripheral.getType(side) == "monitor" then
  19.       monitor = peripheral.wrap(side)
  20.       monitor.setTextScale(0.5)
  21.       monitorWidth, monitorHeight = monitor.getSize()
  22.    end
  23. end
  24.  
  25. -- Find inventory sensor
  26. os.loadAPI("ocs/apis/sensor")
  27. local invSensor = nil
  28. for sideId, side in ipairs(redstone.getSides()) do
  29.    if peripheral.getType(side) == "sensor" then
  30.       local tempSensor = sensor.wrap(side)
  31.       if tempSensor.getTargetDetails("0,0,0").Slots[1].RawName == "openccsensors.item.inventorysensor" then
  32.          invSensor = sensor.wrap(side)
  33.       end
  34.    end
  35. end
  36.  
  37. local terminalWidth, terminalHeight = term.getSize()
  38. term.clear()
  39. local myLine = "Breeder Reactor Control"
  40. term.setCursorPos(terminalWidth/2-string.len(myLine)/2, 2)
  41. term.write(myLine)
  42. myLine = "Press 'Q' to terminate Breeder Control"
  43. term.setCursorPos(terminalWidth/2-string.len(myLine)/2, 5)
  44. term.write(myLine)
  45.  
  46. local function displayStatus()
  47.    local statusColor = (cooking and colors.red or colors.lime)
  48.    local statusMessage = (cooking and "Enriching" or "Idle")
  49.    local remainingSeconds = math.floor(startedTime + cookDuration - os.clock())
  50.    if monitor ~= nil then
  51.       monitor.setTextColor(statusColor)
  52.       monitor.setCursorPos(monitorWidth/2-string.len(statusMessage)/2, 3)
  53.       monitor.clearLine()
  54.       monitor.write(statusMessage)
  55.       monitor.setCursorPos(monitorWidth/2-6, 6)
  56.    end
  57.    term.setCursorPos(terminalWidth/2-6, 11)
  58.    if cooking then
  59.       term.write("Remaining: "..remainingSeconds.."   ")
  60.       if monitor ~= nil then
  61.          monitor.setCursorPos(monitorWidth/2-6, 6)
  62.          monitor.write("Remaining: "..remainingSeconds.."   ")
  63.       end
  64.    else
  65.       term.clearLine()
  66.       if monitor ~= nil then monitor.clearLine() end
  67.    end
  68. end
  69.  
  70. local function getCookDuration()
  71.    local duration = 0
  72.    local cellDamage = 0
  73.    local uraniumDamage = 10000
  74.    if invSensor == nil then return maxCookDuration end -- if we don't have a sensor then just cook for the max time
  75.    local reactor = invSensor.getTargetDetails(reactorRelPos)
  76.    local i
  77.    for i = 1, #cellSlots do
  78.       if reactor.Slots[cellSlots[i]].RawName == "item.reactorisotopecell" then
  79.          if reactor.Slots[cellSlots[i]].DamageValue > cellDamage then
  80.             cellDamage = reactor.Slots[cellSlots[i]].DamageValue
  81.          end
  82.       end
  83.    end
  84.    duration = math.ceil(cellDamage/(10000/maxCookDuration)) + 2 -- 2 seconds added to cover the margins
  85.    for i = 1, #uraniumSlots do
  86.       local name = string.find(reactor.Slots[uraniumSlots[i]].RawName, "item.reactoruranium")
  87.       if name ~= nil and not depleted then
  88.          if reactor.Slots[uraniumSlots[i]].DamageValue < uraniumDamage then
  89.             uraniumDamage = reactor.Slots[uraniumSlots[i]].DamageValue
  90.          end
  91.       end
  92.    end
  93.    local maxDuration = 10000 - uraniumDamage
  94.    if (duration) <= maxDuration then
  95.       return duration
  96.    else
  97.       return maxDuration
  98.    end
  99. end
  100.  
  101. os.queueEvent("stopcooking")
  102. local displayTimer = 0
  103. while true do
  104.    local event, p1 = os.pullEvent()
  105.    if event == "key" and p1 == 16 then -- 'q' was pressed
  106.       redstone.setOutput(reactorSide, false)
  107.       term.setCursorPos(1, terminalHeight-3)
  108.       if monitor ~= nil then monitor.clear() end
  109.       return
  110.    elseif event == "key" and p1 == 18 and not cooking then -- 'e' pressed and not cooking already
  111.       os.queueEvent("startcooking")
  112.    elseif event == "redstone" and redstone.getInput("front") and not cooking then -- The button was pushed and we aren't cooking
  113.       os.queueEvent("startcooking")
  114.    elseif event == "startcooking" then
  115.       cooking = true
  116.       cookDuration = getCookDuration()
  117.       redstone.setOutput(reactorSide, true)
  118.       cookTimer = os.startTimer(cookDuration)
  119.       startedTime = os.clock()
  120.       myLine = "Press 'T' to terminate cell enrichment"
  121.       term.setCursorPos(terminalWidth/2-string.len(myLine)/2, 7)
  122.       term.clearLine()
  123.       term.write(myLine)
  124.       displayStatus()
  125.       displayTimer = os.startTimer(1)
  126.    elseif event == "key" and p1 == 20 and cooking then -- 't' pressed and cooking so terminate current cook run
  127.       os.queueEvent("stopcooking")
  128.    elseif event == "timer" and p1 == cookTimer and cooking then -- enrichment complete, shut down breeder
  129.       os.queueEvent("stopcooking")
  130.    elseif event == "stopcooking" then
  131.       cooking = false
  132.       redstone.setOutput(reactorSide, false)
  133.       myLine = "Press 'E' to begin cell enrichment"
  134.       term.setCursorPos(terminalWidth/2-string.len(myLine)/2, 7)
  135.       term.clearLine()
  136.       term.write(myLine)
  137.       displayStatus()
  138.    elseif event == "timer" and p1 == displayTimer then
  139.       displayStatus()
  140.       displayTimer = os.startTimer(1)
  141.    end
  142. end
Advertisement
Add Comment
Please, Sign In to add comment