1ng0

ComputerCraft - Massfabricator - Monitor Controled

Jun 3rd, 2015
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.80 KB | None | 0 0
  1. -------------------------
  2. -- Code by PlowmanPlow --
  3. -------------------------
  4. -- http://forums.technicpack.net/topic/39951-mass-fabricator-control-cc
  5. -- http://www.computercraft.info/forums2/index.php?/topic/11501-mass-fabricator-control
  6. -------------------------
  7. local needModem = false -- Do we require a modem to run?
  8. local remoteDisplayChannel = nil -- Modem channel to transmit data for status display. Set to nil to disable remote display
  9. local outputSide = "bottom" -- The side of the computer to send out redstone signals for mass fab control
  10. local inputSide = "front" -- The side where we will watch for a redstone pulse to starting burning a partial stack
  11.  
  12. -- Turn off the mass fab immediately to protect from errors in the code
  13. redstone.setOutput(outputSide, true)
  14.  
  15. term.clear()
  16. term.setCursorPos(15,2)
  17. print("Mass Fabricator Control")
  18. term.setCursorPos(1,8)
  19. print("Press 'Q' to terminate program")
  20. print("   This will halt fabrication")
  21. print()
  22. print("Press 'T' to run fabricator for set time")
  23. print("   User will be prompted for # of seconds")
  24. print()
  25. print("Press button to burn partial stacks of scrap")
  26.  
  27. -- Open the modem if one exists
  28. local modem = nil
  29. for sideId, side in ipairs(redstone.getSides()) do
  30.    if peripheral.getType(side) == "modem" then
  31.       modem = peripheral.wrap(side)
  32.       if nameservice then -- register this computer with name service if we have one
  33.          nameservice.registerComputer(modem)
  34.       end
  35.    end
  36. end
  37. if (modem == nil) and needModem then
  38.    print("Error: Could not bind to modem. Is one attached?")
  39.    return
  40. end
  41.  
  42. -- Find inventory sensor
  43. os.loadAPI("ocs/apis/sensor")
  44. local tempSensor, invSensor = nil
  45. for sideId, side in ipairs(redstone.getSides()) do
  46.    if peripheral.getType(side) == "sensor" then
  47.       tempSensor = sensor.wrap(side)
  48.       if tempSensor.getTargetDetails("0,0,0").Slots[1].RawName == "openccsensors.item.inventorysensor" then
  49.          invSensor = sensor.wrap(side)
  50.       end
  51.    end
  52. end
  53. if invSensor == nil then
  54.    error("Could not bind to inventory sensor. Is one attached (with card)?")
  55.    return
  56. end
  57.  
  58. -- Find mass fabricator
  59. local massFabRelPos = ""
  60. local targets = invSensor.getTargets()
  61. local targetPos, targetData
  62. for targetPos, targetData in pairs(targets) do
  63.   if targetData.RawName == "ic2.blockmatter" then
  64.      massFabRelPos = targetPos
  65.   end
  66. end
  67. if massFabRelPos == "" then
  68.    error("Could not find a mass fabricator. Is there one in range of the sensor?")
  69.    return
  70. end
  71.  
  72. -- Find a monitor
  73. local monitor = nil
  74. local monitorWidth, monitorHeight = 0
  75. for sideId, side in ipairs(redstone.getSides()) do
  76.    if peripheral.getType(side) == "monitor" then
  77.       monitor = peripheral.wrap(side)
  78.       monitor.setTextScale(0.5)
  79.       monitorWidth, monitorHeight = monitor.getSize()
  80.    end
  81. end
  82.  
  83. local stackSize, stackSizeTarget
  84. local haltFabrication = true -- State to set when program starts (true = fab is shut down)
  85. redstone.setOutput(outputSide, haltFabrication)
  86. local forcedRun = false
  87. local secsToRun, forcedStartTime = 0, 0
  88. stackSizeTarget = 64
  89. local termWidth, termHeight = term.getSize()
  90.  
  91. local enum = 0
  92. local pollTimerId = os.startTimer(1.0)
  93. -------------------------
  94. -- Code by PlowmanPlow --
  95. -------------------------
  96. while true do
  97.    local event, p1, p2, p3, p4, p5, p6 = os.pullEvent()
  98.    enum = enum + 1
  99.    if event == "modem_message" then
  100.    elseif event == "key" then
  101.       if p1 == 16 then -- 'q'
  102.          os.sleep(0)
  103.          if monitor ~= nil then
  104.             monitor.clear()
  105.          end
  106.          term.setCursorPos(1,17)
  107.          redstone.setOutput(outputSide, true)
  108.          return
  109.       end
  110.       if p1 == 45 then -- 'x'
  111.          forcedStartTime = 0
  112.          secsToRun = 0
  113.       end
  114.       if p1 == 20 then -- 't'
  115.          term.setCursorPos(1,16)
  116.          term.write("How many seconds to run fabrication (1-9900): ")
  117.          local input = read()
  118.          term.setCursorPos(1,16)
  119.          term.clearLine()
  120.          if input == nil then input = 0 end
  121.          input = tonumber(input)
  122.          if input == nil then input = 0 end
  123.          req = math.floor(input)
  124.          if (req > 0) or (req <= 9900) then
  125.             secsToRun = req
  126.             forcedStartTime = math.floor(os.clock())
  127.             forcedRun = true
  128.             redstone.setOutput(outputSide, false)
  129.             term.write("Press 'X' to stop the forced run")
  130.             pollTimerId = os.startTimer(1)
  131.          end
  132.       end
  133.    elseif (event == "redstone") then
  134.       if redstone.getInput(inputSide) then
  135.          if (stackSize < stackSizeTarget) and (not forcedRun) then
  136.             stackSizeTarget = 0
  137.             haltFabrication = not haltFabrication
  138.             redstone.setOutput(outputSide, haltFabrication)
  139.          end
  140.       end
  141.    elseif (event == "timer") and (p1 == pollTimerId) then
  142.       stackSize = invSensor.getTargetDetails(massFabRelPos).Slots[1].Size
  143.       nowTime = math.floor(os.clock())
  144.       if forcedRun and (nowTime < (forcedStartTime+secsToRun)) then
  145.          term.setCursorPos(termWidth-15,1)
  146.          term.write("Run Time: "..(forcedStartTime + secsToRun - nowTime).." ")
  147.       elseif forcedRun and (nowTime >= (forcedStartTime+secsToRun)) then
  148.          term.setCursorPos(1,1)
  149.          term.clearLine()
  150.          forcedRun = false
  151.          secsToRun = 0
  152.          forcedStartTime = 0
  153.          if stackSize < stackSizeTarget then
  154.             redstone.setOutput(outputSide, true)
  155.          end
  156.          term.setCursorPos(1,16)
  157.          term.clearLine()
  158.          if monitor ~= nil then
  159.             monitor.setCursorPos(1,8)
  160.             monitor.clearLine()
  161.          end
  162.       end
  163.       if not forcedRun and (stackSize == stackSizeTarget) then
  164.          stackSizeTarget = math.abs(stackSizeTarget-64)
  165.          haltFabrication = not haltFabrication
  166.          redstone.setOutput(outputSide, haltFabrication)
  167.       end
  168.       if monitor ~= nil then
  169.          monitor.setCursorPos((monitorWidth/2),1)
  170.          monitor.write("MFC")
  171.          monitor.setCursorPos(1,4)
  172.          monitor.write("Scrap: "..stackSize.." ")
  173.          monitor.setCursorPos(1,6)
  174.          monitor.write("State: "..(redstone.getOutput(outputSide) and "Off" or "On "))
  175.          if forcedRun then
  176.             monitor.setCursorPos(1,8)
  177.             monitor.write("Force Run: "..(forcedStartTime + secsToRun - nowTime).." ")
  178.          end
  179.       end
  180.       if remoteDisplayChannel ~= nil then
  181.          local remoteData = {}
  182.          remoteData.stackSize = stackSize
  183.          remoteData.powerStatus = redstone.getOutput(outputSide)
  184.          remoteData.forcedRun = forcedRun
  185.          remoteData.runTime = forcedRun and (forcedStartTime + secsToRun - nowTime) or 0
  186.          remoteData.MFC_STATUS = true
  187.          modem.transmit(remoteDisplayChannel, 0, textutils.serialize(remoteData))
  188.       end
  189.       pollTimerId = os.startTimer(1)
  190.    end
  191. end
Add Comment
Please, Sign In to add comment