Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------
- -- Code by PlowmanPlow --
- -------------------------
- -- http://forums.technicpack.net/topic/39951-mass-fabricator-control-cc
- -- http://www.computercraft.info/forums2/index.php?/topic/11501-mass-fabricator-control
- -------------------------
- local needModem = false -- Do we require a modem to run?
- local remoteDisplayChannel = nil -- Modem channel to transmit data for status display. Set to nil to disable remote display
- local outputSide = "bottom" -- The side of the computer to send out redstone signals for mass fab control
- local inputSide = "front" -- The side where we will watch for a redstone pulse to starting burning a partial stack
- -- Turn off the mass fab immediately to protect from errors in the code
- redstone.setOutput(outputSide, true)
- term.clear()
- term.setCursorPos(15,2)
- print("Mass Fabricator Control")
- term.setCursorPos(1,8)
- print("Press 'Q' to terminate program")
- print(" This will halt fabrication")
- print()
- print("Press 'T' to run fabricator for set time")
- print(" User will be prompted for # of seconds")
- print()
- print("Press button to burn partial stacks of scrap")
- -- Open the modem if one exists
- local modem = nil
- for sideId, side in ipairs(redstone.getSides()) do
- if peripheral.getType(side) == "modem" then
- modem = peripheral.wrap(side)
- if nameservice then -- register this computer with name service if we have one
- nameservice.registerComputer(modem)
- end
- end
- end
- if (modem == nil) and needModem then
- print("Error: Could not bind to modem. Is one attached?")
- return
- end
- -- Find inventory sensor
- os.loadAPI("ocs/apis/sensor")
- local tempSensor, invSensor = nil
- for sideId, side in ipairs(redstone.getSides()) do
- if peripheral.getType(side) == "sensor" then
- tempSensor = sensor.wrap(side)
- if tempSensor.getTargetDetails("0,0,0").Slots[1].RawName == "openccsensors.item.inventorysensor" then
- invSensor = sensor.wrap(side)
- end
- end
- end
- if invSensor == nil then
- error("Could not bind to inventory sensor. Is one attached (with card)?")
- return
- end
- -- Find mass fabricator
- local massFabRelPos = ""
- local targets = invSensor.getTargets()
- local targetPos, targetData
- for targetPos, targetData in pairs(targets) do
- if targetData.RawName == "ic2.blockmatter" then
- massFabRelPos = targetPos
- end
- end
- if massFabRelPos == "" then
- error("Could not find a mass fabricator. Is there one in range of the sensor?")
- return
- end
- -- Find a monitor
- local monitor = nil
- local monitorWidth, monitorHeight = 0
- for sideId, side in ipairs(redstone.getSides()) do
- if peripheral.getType(side) == "monitor" then
- monitor = peripheral.wrap(side)
- monitor.setTextScale(0.5)
- monitorWidth, monitorHeight = monitor.getSize()
- end
- end
- local stackSize, stackSizeTarget
- local haltFabrication = true -- State to set when program starts (true = fab is shut down)
- redstone.setOutput(outputSide, haltFabrication)
- local forcedRun = false
- local secsToRun, forcedStartTime = 0, 0
- stackSizeTarget = 64
- local termWidth, termHeight = term.getSize()
- local enum = 0
- local pollTimerId = os.startTimer(1.0)
- -------------------------
- -- Code by PlowmanPlow --
- -------------------------
- while true do
- local event, p1, p2, p3, p4, p5, p6 = os.pullEvent()
- enum = enum + 1
- if event == "modem_message" then
- elseif event == "key" then
- if p1 == 16 then -- 'q'
- os.sleep(0)
- if monitor ~= nil then
- monitor.clear()
- end
- term.setCursorPos(1,17)
- redstone.setOutput(outputSide, true)
- return
- end
- if p1 == 45 then -- 'x'
- forcedStartTime = 0
- secsToRun = 0
- end
- if p1 == 20 then -- 't'
- term.setCursorPos(1,16)
- term.write("How many seconds to run fabrication (1-9900): ")
- local input = read()
- term.setCursorPos(1,16)
- term.clearLine()
- if input == nil then input = 0 end
- input = tonumber(input)
- if input == nil then input = 0 end
- req = math.floor(input)
- if (req > 0) or (req <= 9900) then
- secsToRun = req
- forcedStartTime = math.floor(os.clock())
- forcedRun = true
- redstone.setOutput(outputSide, false)
- term.write("Press 'X' to stop the forced run")
- pollTimerId = os.startTimer(1)
- end
- end
- elseif (event == "redstone") then
- if redstone.getInput(inputSide) then
- if (stackSize < stackSizeTarget) and (not forcedRun) then
- stackSizeTarget = 0
- haltFabrication = not haltFabrication
- redstone.setOutput(outputSide, haltFabrication)
- end
- end
- elseif (event == "timer") and (p1 == pollTimerId) then
- stackSize = invSensor.getTargetDetails(massFabRelPos).Slots[1].Size
- nowTime = math.floor(os.clock())
- if forcedRun and (nowTime < (forcedStartTime+secsToRun)) then
- term.setCursorPos(termWidth-15,1)
- term.write("Run Time: "..(forcedStartTime + secsToRun - nowTime).." ")
- elseif forcedRun and (nowTime >= (forcedStartTime+secsToRun)) then
- term.setCursorPos(1,1)
- term.clearLine()
- forcedRun = false
- secsToRun = 0
- forcedStartTime = 0
- if stackSize < stackSizeTarget then
- redstone.setOutput(outputSide, true)
- end
- term.setCursorPos(1,16)
- term.clearLine()
- if monitor ~= nil then
- monitor.setCursorPos(1,8)
- monitor.clearLine()
- end
- end
- if not forcedRun and (stackSize == stackSizeTarget) then
- stackSizeTarget = math.abs(stackSizeTarget-64)
- haltFabrication = not haltFabrication
- redstone.setOutput(outputSide, haltFabrication)
- end
- if monitor ~= nil then
- monitor.setCursorPos((monitorWidth/2),1)
- monitor.write("MFC")
- monitor.setCursorPos(1,4)
- monitor.write("Scrap: "..stackSize.." ")
- monitor.setCursorPos(1,6)
- monitor.write("State: "..(redstone.getOutput(outputSide) and "Off" or "On "))
- if forcedRun then
- monitor.setCursorPos(1,8)
- monitor.write("Force Run: "..(forcedStartTime + secsToRun - nowTime).." ")
- end
- end
- if remoteDisplayChannel ~= nil then
- local remoteData = {}
- remoteData.stackSize = stackSize
- remoteData.powerStatus = redstone.getOutput(outputSide)
- remoteData.forcedRun = forcedRun
- remoteData.runTime = forcedRun and (forcedStartTime + secsToRun - nowTime) or 0
- remoteData.MFC_STATUS = true
- modem.transmit(remoteDisplayChannel, 0, textutils.serialize(remoteData))
- end
- pollTimerId = os.startTimer(1)
- end
- end
Add Comment
Please, Sign In to add comment