Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Biowatch Program
- -- Watches 2 Bioreactors and turns them on/off depending on how many items are in the reactor
- -- The first reactor will only try to keep one stack of each type of item in its buffer, it will push all
- -- duplicate stacks into the second reactor.
- -- The first reactor will only turn on when a certain number of items are being processed (default 8)
- -- The Second reactor will only turn on if there are 8 (defatult) or more items being processed, or if there are
- -- 6 or more item stacks in the inventory buffer
- -- In the way it is configured now, the first reactor is behind the computer,
- -- The second reactor is directly to the north of the first reactor, and is connected to
- -- the computer via a modem cable. The modem is on the bottom face of the computer
- -- Connected to the top of the computer is a rednet cable which connects to the two reactors
- -- The first reactor's connection is colored orange, the second is magenta
- -- Note: in order to connect a modem to the second reactor, a Peripheral Proxy is required
- --local bio = peripheral.wrap("back") -- Location of first reactor
- local modem = peripheral.wrap("bottom") -- Location of modem for second reactor
- --local machine = "tile_mfr_machine_bioreactor_name_0" -- Network name of second reactor (this is the default)
- local CableSide = "top" -- location of rednet cable
- local pushDirection = "north" -- Location of the second reactor relative to the first
- local varietyNeeded = 8
- local maxID = 3
- function getMachineName(ID)
- return "tile_mfr_machine_bioreactor_name_" .. ( ID - 1 )
- end
- function getStack(machineID,slot)
- return modem.callRemote(getMachineName(machineID),"getStackInSlot",stack)
- end
- function pushCall(machineID,slot,newSlot)
- local machine = getMachineName(ID)
- if ( newSlot ~= -1 ) then modem.callRemote(machine,"pushItem",pushDirection,slot,64,newSlot) end
- modem.callRemote(machine,"pushItem",pushDirection,slot)
- end
- function isIn(var,tab)
- for i,v in ipairs(tab) do
- if ( v == var ) then return true end
- end
- return false
- end
- function FindItemType(machineID,itemstack)
- for slot = 10,18 do
- local IS = getStack(machineID,slot)
- if ( IS ~= nil and IS.id == itemstack.id and IS.dmg == itemstack.dmg and IS.qty < 64 ) then return slot end
- end
- return -1
- end
- function PushItem(machineID,slot)
- local item = getStack(machineID,slot)
- if ( item == nil ) return end
- local newSlot = -1
- if ( machineID < maxID ) then
- newSlot = FindItemType(machineID+1,item)
- end
- pushCall(machineID,slot,newSlot)
- end
- function checkReactorVariety(machineID)
- local count = 0
- for i=10,18 do
- local item = getStack(machineID,i)
- if ( item ~= nil ) then count = count + 1 end
- end
- if ( count >= varietyNeeded ) then return true end
- return false
- end
- function checkSimpleReactorInventory(machineID)
- local count = 0
- for i=1,9 do
- if ( getStack(machineID,i) ~= nil ) then count = count + 1 end
- end
- if ( count >= 9 ) then PushItem(machineID,math.random(1,9)) end
- if ( count >= 8 ) then return true end
- return false
- end
- function checkReactorInventory(machineID)
- if ( machineID == maxID ) then return checkSimpleReactorInventory(machineID) end
- local idList = {}
- local count = 0
- -- Check the inventory of the first reactor, push duplicate stacks to the second reactor.
- for i=1,9 do
- local item = getStack(machineID,i)
- if ( item ~= nil ) then
- count = count + 1
- local code = item.id .. "." .. item.dmg
- if ( isIn(code,idList) ) then
- -- print("Pushing " .. i .. " " .. item.id)
- count = count - 1
- PushItem(machineID,i)
- else
- -- print("Adding " .. i .. " ".. item.id)
- table.insert(idList,code)
- end
- end
- end
- if ( count >= 9 ) then PushItem(machineID,math.random(1,9)) end
- if ( count >= 8 ) then return true end
- return false
- end
- function checkReactor(machineID)
- local turnOff = true
- if ( checkReactorInventory(machineID) == true )
- then turnOff = false
- else
- turnOff = ~checkReactorVariety(machineID)
- end
- return turnOff
- end
- -- Main Loop
- while true do
- local colorCode = 0
- for mID = 1,maxID do
- local turnOff = checkReactor(mID)
- if ( turnOff == true ) then
- local myColor = bit.blshift(1,mID-1)
- colorCode = colorCode + myColor
- end
- end
- rs.setBundledOutput(CableSide,colorCode)
- os.sleep(10)
- end
Add Comment
Please, Sign In to add comment