daily pastebin goal
26%
SHARE
TWEET

Untitled

a guest Dec 10th, 2018 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ComputerCraft oil refinery controller
  2. -- Alternates between two banks of combustion engines so the engines don't overheat and explode
  3.  
  4. function enableWire(color) -- Turn on a colored wire in the bundle without affecting the other colors
  5.   local currentSet = rs.getBundledOutput("back")
  6.   if not colors.test(currentSet, color) then
  7.     currentSet = colors.combine(currentSet, color)
  8.     rs.setBundledOutput("back", currentSet)
  9.   end
  10. end
  11.  
  12. function disableWire(color) -- Turn off a colored wire in the bundle without affecting the other colors
  13.   local currentSet = rs.getBundledOutput("back")
  14.   if colors.test(currentSet, color) then
  15.     currentSet = colors.subtract(currentSet, color)
  16.     rs.setBundledOutput("back", currentSet)
  17.   end
  18. end
  19.  
  20. function toggleWire(color) -- Toggle a colored wire in the bundle without affecting the other colors
  21.   local currentSet = rs.getBundledOutput("back")
  22.   if colors.test(currentSet, color) then
  23.     disableWire(color)
  24.   else
  25.     enableWire(color)
  26.   end
  27. end
  28.  
  29. function outputOnOff(color) -- write "ON " or "OFF" depending on the state of a wire in the bundle
  30.   local currentSet = rs.getBundledOutput("back")
  31.   if colors.test(currentSet, color) then
  32.     write("ON \n")
  33.   else
  34.     write("OFF\n")
  35.   end
  36. end
  37.  
  38. local onDuration = 240 -- Keep the engines on for 4 minutes
  39. local offDuration = 180 -- Turn the engines off for 3 minutes to cool down
  40. local bank1_start = os.startTimer(5) -- Start the first bank of engines after 5 seconds to allow the water and fuel to start circulating
  41. local bank1_stop
  42. local bank2_start = os.startTimer(195) -- Start the 2nd bank of engines after around 3 minutes so they will overlap for a minute
  43. local bank2_stop
  44. local update_display = os.startTimer(1) -- Draw the status display
  45.  
  46. enableWire(colors.green) -- Turn on the fuel pump
  47.  
  48. while true do
  49.   local evt, arg = os.pullEvent()
  50.   if evt == "key" then
  51.     if arg == 45 then -- X key is pressed
  52.       write("Shutting down!\n")
  53.       rs.setBundledOutput("back", 0) -- Turn everything off
  54.       break
  55.     elseif arg == 17 then -- W key is pressed, toggle the water pump
  56.       toggleWire(colors.blue)
  57.     elseif arg == 24 then -- O key is pressed, toggle the oil pump
  58.       toggleWire(colors.black)
  59.     end
  60.   elseif evt == "timer" then -- Timers to start and top the engine banks
  61.     if arg == bank1_start then
  62.       enableWire(colors.red)
  63.       bank1_stop = os.startTimer(onDuration)
  64.     elseif arg == bank1_stop then
  65.       disableWire(colors.red)
  66.       bank1_start = os.startTimer(offDuration)
  67.     elseif arg == bank2_start then
  68.       enableWire(colors.yellow)
  69.       bank2_stop = os.startTimer(onDuration)
  70.     elseif arg == bank2_stop then
  71.       disableWire(colors.yellow)
  72.       bank2_start = os.startTimer(offDuration)
  73.     elseif arg == update_display then -- Draw the status display
  74.       term.setCursorPos(1,1)
  75.       write("=== OIL REFINERY STATUS ===\n")
  76.       local currentSet = rs.getBundledOutput("back")
  77.       write("Engine bank 1: ")
  78.       outputOnOff(colors.red)
  79.       write("Engine bank 2: ")
  80.       outputOnOff(colors.yellow)
  81.       write("Fuel pump: ")
  82.       outputOnOff(colors.green)
  83.       write("Water tank pump: ")
  84.       outputOnOff(colors.blue)
  85.       write("Oil tank pump: ")
  86.       outputOnOff(colors.black)
  87.       write("===========================\n")
  88.       write("X: Shutdown\n")
  89.       write("W: Toggle water tank pump\n")
  90.       write("O: Toggle oil tank pump\n")
  91.       update_display = os.startTimer(1)
  92.     end
  93.   end
  94. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Pastebin PRO 'WINTER SPECIAL'!
Get 40% OFF Pastebin PRO accounts!
 
Top