Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ (comment block)
- by meigrafd @ 09.07.2015
- Version History:
- - Computer
- http://pastebin.com/ewb0tM52
- http://pastebin.com/EJxbbxyN
- - Turtle
- http://pastebin.com/YUfiniV9
- http://pastebin.com/6BSDQzEm
- Requires:
- ComputerCraft >=1.5
- OpenCCsensors >=1.7.5
- OpenPeripheral
- Setup Reactor like:
- Bundled Cable ends to Computer.
- First Thermal Sensor turns Reactor off. Set this to max 1000.
- Second Thermal Sensor tells Turtle to drop Uran Cells to prevent explosion. Set this to max 2500.
- (when Reactor was on it will still head up also after shutdown)
- For a little bit better Cooling and Blastprotection you also should add Water above the complete Reactor.
- Setup Reactor Inventory as:
- http://s2.postimage.org/5r41iiq15/Reactor_Setup.jpg
- (biggest Setup with 1120 EU/t)
- Use a Wireless Turtle (modem always on the left side) and add a Wireless Modem to your Computer.
- Place a OpenCCsensor to any side of your Computer and add an 'Inventory Sensor Card'.
- Install the Scripts as 'startup'.
- First start Script on Computer and only then on Turtle.
- Optional:
- Execute once in Terminal of Cumputer near Reactor: label set reacomp
- and on the Turtle: label set reatur
- --]]
- ---[[ CONFIG - START ]]
- local UseReactorSetup = 2
- --[[ Set to 1 or 2.
- 1 = Biggest with five Quad-Fuel-Rods in a row, twice.
- 2 = three Quad-Fuel-Rods in a row, twice.
- --]]
- local CheckEvery = 1 -- sec. Dont set too high! (but also not lower 0.5)
- -- Modem channel. Must be same as Turtle uses
- local ModemChannel = 32768 -- Broadcast channel.
- -- Wire Colors. Available Colors with Bundled Cable: http://computercraft.info/wiki/Colors_(API)#Colors
- wireReactor = colors.green -- To turn Reactor on/off
- wireThermal1 = colors.blue -- If Reactor Temperature gets >1000
- wireThermal2 = colors.red -- If Reactor Temperature gets >2000
- -- Bundled Cable Side on Computer. Only valid sides: bottom, top, back, front, right, left
- wireSide = "bottom"
- --sometimes "right" doesnt work..
- ---[[ CONFIG - END ]]
- --[[
- Required Reactor Inventory setup Table.
- first row from left to right, then the second row from left to right, etc.
- one row = 9 columns.
- NOTE: Not the exact Item-Name is used, just a match of e.g. *Coolant* so 10k or 60k Coolant Cells can be used,
- but better use 60k Coolant Cells instead of 10k.
- --]]
- if UseReactorSetup == 1 then
- -- 5 Quad-Fuel-Rods in a row, twice.
- RequiredReactorInventory = {
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Coolant Cell",
- "Uran",
- "Uran",
- "Uran",
- "Uran",
- "Uran",
- "Coolant Cell",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Coolant Cell",
- "Uran",
- "Uran",
- "Uran",
- "Uran",
- "Uran",
- "Coolant Cell",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent"
- }
- else
- -- only 3 Quad-Fuel-Rods in a row, twice
- RequiredReactorInventory = {
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Coolant Cell",
- "Coolant Cell",
- "Uran",
- "Uran",
- "Uran",
- "Coolant Cell",
- "Coolant Cell",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Coolant Cell",
- "Coolant Cell",
- "Uran",
- "Uran",
- "Uran",
- "Coolant Cell",
- "Coolant Cell",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Coolant Cell",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent",
- "Reactor Heat Vent"
- }
- end
- ---[[ functions ]]
- -- source: http://pastebin.com/RCPEHmxs
- function clear()
- myMonitor.setBackgroundColor(colors.black)
- myMonitor.clear()
- myMonitor.setCursorPos(1,1)
- end
- --display text on computer's terminal screen
- function draw_text_term(x, y, text, text_color, bg_color)
- term.setTextColor(text_color)
- term.setBackgroundColor(bg_color)
- term.setCursorPos(x,y)
- write(text)
- end
- --display text on monitor, "myMonitor" peripheral
- function draw_text(x, y, text, text_color, bg_color)
- myMonitor.setBackgroundColor(bg_color)
- myMonitor.setTextColor(text_color)
- myMonitor.setCursorPos(x,y)
- myMonitor.write(text)
- end
- --draw line on computer nonitor
- function draw_line(x, y, length, color)
- myMonitor.setBackgroundColor(color)
- myMonitor.setCursorPos(x,y)
- myMonitor.write(string.rep(" ", length))
- end
- --draw line on computer terminal
- function draw_line_term(x, y, length, color)
- term.setBackgroundColor(color)
- term.setCursorPos(x,y)
- term.write(string.rep(" ", length))
- end
- --create progress bar
- --draws two overlapping lines
- --background line of bg_color
- --main line of bar_color as a percentage of minVal/maxVal
- function progress_bar(x, y, length, minVal, maxVal, bar_color, bg_color)
- draw_line(x, y, length, bg_color) --backgoround bar
- local barSize = math.floor((minVal/maxVal) * length)
- draw_line(x, y, barSize, bar_color) --progress so far
- end
- --same as above but on the computer terminal
- function progress_bar_term(x, y, length, minVal, maxVal, bar_color, bg_color)
- draw_line_term(x, y, length, bg_color) --backgoround bar
- local barSize = math.floor((minVal/maxVal) * length)
- draw_line_term(x, y, barSize, bar_color) --progress so far
- end
- --create button on monitor
- function button(x, y, length, text, txt_color, bg_color)
- draw_line(x, y, length, bg_color)
- draw_text((x+2), y, text, txt_color, bg_color)
- end
- -- http://stackoverflow.com/questions/1426954/split-string-in-lua
- function split(pString, pPattern)
- local Table = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pPattern
- local last_end = 1
- local s, e, cap = pString:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(Table, cap)
- end
- last_end = e+1
- s, e, cap = pString:find(fpat, last_end)
- end
- if last_end <= #pString then
- cap = pString:sub(last_end)
- table.insert(Table, cap)
- end
- return Table
- end
- function stringMatch(text, find)
- if string.find(string.lower(text), string.lower(find)) then
- return true
- else
- return false
- end
- end
- function getDeviceSide(deviceType)
- -- loop through all the sides
- for i,side in pairs(rs.getSides()) do
- if (peripheral.isPresent(side)) then
- -- Yup, there is something on this side
- if (peripheral.getType(side) == string.lower(deviceType)) then
- -- Yes, this is the device type we need, so return the side
- return side;
- end
- end
- end
- --nothing found, return nil
- return nil;
- end
- function findReactorInventory()
- local targets = mySensor.getTargets()
- for pos,info in pairs(targets) do
- if stringMatch(info.Name, "reactor") then
- myReactor = pos
- foundReactor = true
- break -- stop after first found coz all are one..
- end
- end
- end
- function checkInventory()
- draw_text_term(1, 1, "Checking Reactor Inventory..", colors.white, colors.black)
- details = mySensor.getTargetDetails(myReactor)
- allFine = true
- sleep(0.1)
- linecount = 1
- for i=1, #RequiredReactorInventory do
- found = false
- depleted_uran = false
- for slot,info in pairs(details.Slots) do
- if slot == i and (stringMatch(info.Name, RequiredReactorInventory[i]) == true) then
- if stringMatch(info.Name, "Depleted") == true then
- depleted_uran = true
- break
- else
- found = true
- break
- end
- end
- end
- if depleted_uran == true then
- allFine = false
- linecount = linecount + 1
- local Slot = i
- local Item = "Depleted Uran"
- -- does not meet the requirements. replace it!
- draw_text_term(1, linecount, 'Slot#'..Slot..': "'..Item..'" replacing it!', colors.red, colors.black)
- -- tell Turtle to replace Item. Format -> Slot:Item
- myModem.transmit(ModemChannel, ModemChannel, Slot..':depleted')
- elseif found == false then
- allFine = false
- linecount = linecount + 1
- local Slot = i
- local Item = RequiredReactorInventory[Slot]
- -- does not meet the requirements. replace it!
- draw_text_term(1, linecount, 'Slot#'..Slot..': "'..Item..'" not found!', colors.red, colors.black)
- -- tell Turtle to replace Item. Format -> Slot:Item
- myModem.transmit(ModemChannel, ModemChannel, Slot..':'..Item)
- end
- end
- if allFine == true then
- draw_text_term(1, 2, "Nothing to do.", colors.lime, colors.black)
- end
- end
- -- http://computercraft.info/wiki/Usage_of_the_bundled_cable_using_the_Redstone_API
- -- http://computercraft.info/wiki/Redstone_(API)
- function changeReactorState(state, reason)
- if reason ~= '' then
- msg = "Changing Reactor State to: "..state.." coz: "..reason
- else
- msg = "Changing Reactor State to: "..state
- end
- draw_text_term(1, h-1, msg, colors.red, colors.black)
- if state == "on" then
- if uranDropped == true then
- uranDropped = false
- end
- rs.setBundledOutput(wireSide, wireReactor)
- else
- rs.setBundledOutput(wireSide, 0)
- end
- reactorState = state
- end
- function printInv(from, list)
- print(from, " Inventory:")
- -- list format received: slot#=name|slot#=name|slot#=name
- local t1 = split(list, "|")
- for _,n in pairs(t1) do
- local t2 = split(n, "=")
- print("slot#"..t2[1]..": "..t2[2])
- end
- end
- --[[ End functions ]]
- --[[ Internal values ]]
- local version = 0.7
- foundModem = false
- foundSensor = false
- foundReactor = false
- foundMonitor = false
- w,h = term.getSize()
- reactorState = 'off'
- uranDropped = false
- thermalTriggered = false
- --[[ Main Script ]]
- term.clear()
- if fs.exists("ocs/apis/sensor") then
- os.loadAPI("ocs/apis/sensor")
- else
- error('Missing OCS mod api')
- end
- draw_text_term(1, 3, "Searching for peripherals...", colors.white, colors.black)
- sleep(0.5)
- -- detect Modem
- draw_text_term(2, 5, "Connecting to modem...", colors.white, colors.black)
- sleep(0.5)
- side = getDeviceSide("modem")
- if side == nil then
- draw_text_term(1, 8, "Error:", colors.red, colors.black)
- draw_text_term(1, 9, "Could not connect to Modem", colors.red, colors.black)
- os.exit()
- else
- myModem = peripheral.wrap(side)
- myModem.open(ModemChannel)
- foundModem = true
- if myModem.isWireless() then
- draw_text_term(27, 5, "success", colors.lime, colors.black)
- draw_text_term(35, 5, "(wireless)", colors.white, colors.black)
- else
- draw_text_term(27, 5, "success", colors.lime, colors.black)
- draw_text_term(35, 5, "(wired)", colors.white, colors.black)
- end
- sleep(0.5)
- end
- -- detect Sensor
- draw_text_term(2, 6, "Connecting to sensor...", colors.white, colors.black)
- sleep(0.5)
- side = getDeviceSide("sensor")
- if side == nil then
- draw_text_term(1, 7, "Error:", colors.red, colors.black)
- draw_text_term(1, 8, "Could not connect to Sensor", colors.red, colors.black)
- os.exit()
- else
- mySensor = peripheral.wrap(side)
- foundSensor = true
- draw_text_term(27, 6, "success", colors.lime, colors.black)
- sleep(0.5)
- end
- -- detect Reactor
- draw_text_term(2, 7, "Connecting to Reactor...", colors.white, colors.black)
- sleep(0.5)
- findReactorInventory()
- if foundReactor == false then
- draw_text_term(1, 8, "Error:", colors.red, colors.black)
- draw_text_term(1, 9, "no Reactor found! Check adjacency of sensors and type of card", colors.red, colors.black)
- error()
- else
- draw_text_term(27, 7, "success", colors.lime, colors.black)
- sleep(0.5)
- end
- -- detect (optional) Monitor
- side = getDeviceSide("monitor")
- if side == nil then
- draw_text_term(2, 8, "Could not connect to optional Monitor", colors.white, colors.black)
- else
- myMonitor = peripheral.wrap(side)
- foundMonitor = true
- mw,mh = myMonitor.getSize()
- draw_text_term(2, 8, "Detected optional Monitor", colors.white, colors.black)
- -- check if monitor can colors (advanced monitor)
- if not myMonitor.isColor() then
- draw_text_term(2, 9, "Please use an Advanced Monitor for Colors!", colors.white, colors.black)
- end
- end
- draw_text_term(1, 11, "Setup Complete!", colors.lime, colors.black)
- sleep(3)
- draw_text_term(1, 12, "Starting", colors.white, colors.black)
- -- all seems fine, start main loop
- while true do
- term.clear()
- -- only check inventory when we didnt told turtle to remove uran to prevent explosion..
- if uranDropped == false then
- checkInventory()
- end
- -- check State of Thermal1
- if (colors.test(rs.getBundledInput(wireSide), wireThermal1)) == true then
- -- turn Reactor off
- changeReactorState('off', 'Thermal1 triggered')
- thermalTriggered = true
- end
- -- check State of Thermal2
- if (colors.test(rs.getBundledInput(wireSide), wireThermal2)) == true then
- -- turn Reactor off
- changeReactorState('off', 'Thermal2 triggered')
- thermalTriggered = true
- -- tell Turtle to drop all Uran Cells to prevent explosion
- if uranDropped == false then
- draw_text_term(1, linecount+1, 'Telling Turtle to drop all Uran Cells', colors.red, colors.black)
- dropList = ""
- for i=1, #RequiredReactorInventory do
- if stringMatch(RequiredReactorInventory[i], "Uran") == true then
- if dropList == '' then
- dropList = i
- else
- dropList = dropList..','..i
- end
- end
- end
- myModem.transmit(ModemChannel, ModemChannel, 'DropUranSlots:'..dropList)
- -- after drop cmd was send make sure to not send it again
- uranDropped = true
- end
- end
- if (thermalTriggered == true
- and colors.test(rs.getBundledInput(wireSide), wireThermal1) == false
- and colors.test(rs.getBundledInput(wireSide), wireThermal2) == false) then
- changeReactorState('on', '')
- thermalTriggered = false
- end
- -- Receive messages Event loop
- timerID = os.startTimer(0.5) -- Wait for ... secounds before rise a timer event.
- repeat
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent()
- if event == "modem_message" then
- -- Determine if message contains ':'
- if string.find(message, "\:") then
- -- split message e.g. slot:item into a table
- msgTable = split(message,"\:")
- if msgTable[1] == "CoolantInv" then
- printInv("Coolant", msgTable[2])
- elseif msgTable[1] == "UranInv" then
- printInv("Uran", msgTable[2])
- elseif msgTable[1] == "TrashInv" then
- printInv("Trash", msgTable[2])
- elseif msgTable[1] == "ReactorInv" then
- printInv("Reactor", msgTable[2])
- elseif msgTable[1] == "STOP" then
- -- Turtle registered a Problem so turn Reactor off. Reason = msgTable[2]
- changeReactorState('off', msgTable[2])
- end
- -- message doesnt contain ':'
- else
- if message == "READY" then
- -- Turtle is ready so turn Reactor on
- changeReactorState('on', '')
- else
- draw_text_term(1, h-1, "ERROR: Received unknown message: "..message, colors.red, colors.black)
- end
- end
- end
- -- exit repeat loop on timer event
- until (event == "timer" and modemSide == timerID)
- sleep(CheckEvery)
- end
- --
- -- EOF
- --
Add Comment
Please, Sign In to add comment