Skip to content

Instantly share code, notes, and snippets.

@c99koder
Created August 25, 2012 05:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c99koder/3461305 to your computer and use it in GitHub Desktop.
Save c99koder/3461305 to your computer and use it in GitHub Desktop.
ComputerCraft lua script to control an IndustrialCraft refinery in Minecraft
-- ComputerCraft oil refinery controller
-- Alternates between two banks of combustion engines so the engines don't overheat and explode
function enableWire(color) -- Turn on a colored wire in the bundle without affecting the other colors
local currentSet = rs.getBundledOutput("back")
if not colors.test(currentSet, color) then
currentSet = colors.combine(currentSet, color)
rs.setBundledOutput("back", currentSet)
end
end
function disableWire(color) -- Turn off a colored wire in the bundle without affecting the other colors
local currentSet = rs.getBundledOutput("back")
if colors.test(currentSet, color) then
currentSet = colors.subtract(currentSet, color)
rs.setBundledOutput("back", currentSet)
end
end
function toggleWire(color) -- Toggle a colored wire in the bundle without affecting the other colors
local currentSet = rs.getBundledOutput("back")
if colors.test(currentSet, color) then
disableWire(color)
else
enableWire(color)
end
end
function outputOnOff(color) -- write "ON " or "OFF" depending on the state of a wire in the bundle
local currentSet = rs.getBundledOutput("back")
if colors.test(currentSet, color) then
write("ON \n")
else
write("OFF\n")
end
end
local onDuration = 240 -- Keep the engines on for 4 minutes
local offDuration = 180 -- Turn the engines off for 3 minutes to cool down
local bank1_start = os.startTimer(5) -- Start the first bank of engines after 5 seconds to allow the water and fuel to start circulating
local bank1_stop
local bank2_start = os.startTimer(195) -- Start the 2nd bank of engines after around 3 minutes so they will overlap for a minute
local bank2_stop
local update_display = os.startTimer(1) -- Draw the status display
enableWire(colors.green) -- Turn on the fuel pump
while true do
local evt, arg = os.pullEvent()
if evt == "key" then
if arg == 45 then -- X key is pressed
write("Shutting down!\n")
rs.setBundledOutput("back", 0) -- Turn everything off
break
elseif arg == 17 then -- W key is pressed, toggle the water pump
toggleWire(colors.blue)
elseif arg == 24 then -- O key is pressed, toggle the oil pump
toggleWire(colors.black)
end
elseif evt == "timer" then -- Timers to start and top the engine banks
if arg == bank1_start then
enableWire(colors.red)
bank1_stop = os.startTimer(onDuration)
elseif arg == bank1_stop then
disableWire(colors.red)
bank1_start = os.startTimer(offDuration)
elseif arg == bank2_start then
enableWire(colors.yellow)
bank2_stop = os.startTimer(onDuration)
elseif arg == bank2_stop then
disableWire(colors.yellow)
bank2_start = os.startTimer(offDuration)
elseif arg == update_display then -- Draw the status display
term.setCursorPos(1,1)
write("=== OIL REFINERY STATUS ===\n")
local currentSet = rs.getBundledOutput("back")
write("Engine bank 1: ")
outputOnOff(colors.red)
write("Engine bank 2: ")
outputOnOff(colors.yellow)
write("Fuel pump: ")
outputOnOff(colors.green)
write("Water tank pump: ")
outputOnOff(colors.blue)
write("Oil tank pump: ")
outputOnOff(colors.black)
write("===========================\n")
write("X: Shutdown\n")
write("W: Toggle water tank pump\n")
write("O: Toggle oil tank pump\n")
update_display = os.startTimer(1)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment