GauHelldragon

BioWatch2 v0.1

Sep 5th, 2014
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.48 KB | None | 0 0
  1. -- Biowatch Program
  2. -- Watches 2 Bioreactors and turns them on/off depending on how many items are in the reactor
  3. -- The first reactor will only try to keep one stack of each type of item in its buffer, it will push all
  4. --  duplicate stacks into the second reactor.
  5. -- The first reactor will only turn on when a certain number of items are being processed (default 8)
  6.  
  7. -- The Second reactor will only turn on if there are 8 (defatult) or more items being processed, or if there are
  8. --  6 or more item stacks in the inventory buffer
  9.  
  10.  
  11. -- In the way it is configured now, the first reactor is behind the computer,
  12. -- The second reactor is directly to the north of the first reactor, and is connected to
  13. --   the computer via a modem cable. The modem is on the bottom face of the computer
  14. -- Connected to the top of the computer is a rednet cable which connects to the two reactors
  15. --   The first reactor's connection is colored orange, the second is magenta
  16.  
  17. -- Note: in order to connect a modem to the second reactor, a Peripheral Proxy is required
  18.  
  19.  
  20.  
  21. --local bio = peripheral.wrap("back") -- Location of first reactor
  22. local modem = peripheral.wrap("bottom") -- Location of modem for second reactor
  23. --local machine = "tile_mfr_machine_bioreactor_name_0" -- Network name of second reactor (this is the default)
  24. local CableSide = "top" -- location of rednet cable
  25.  
  26. local pushDirection = "north" -- Location of the second reactor relative to the first
  27.  
  28. local varietyNeeded = 8
  29.  
  30. local maxID = 3
  31.  
  32.  
  33. function getMachineName(ID)
  34.     return "tile_mfr_machine_bioreactor_name_" .. ( ID - 1 )
  35. end
  36.  
  37. function getStack(machineID,slot)
  38.    return modem.callRemote(getMachineName(machineID),"getStackInSlot",stack)
  39. end
  40.  
  41. function pushCall(machineID,slot,newSlot)
  42.    local machine = getMachineName(ID)
  43.    if ( newSlot ~= -1 ) then modem.callRemote(machine,"pushItem",pushDirection,slot,64,newSlot) end
  44.    modem.callRemote(machine,"pushItem",pushDirection,slot)
  45. end
  46.  
  47. function isIn(var,tab)
  48.  for i,v in ipairs(tab) do
  49.     if ( v == var ) then return true end
  50.  end
  51.  return false
  52. end
  53.  
  54. function FindItemType(machineID,itemstack)
  55.   for slot = 10,18 do
  56.     local IS = getStack(machineID,slot)
  57.     if ( IS ~= nil and IS.id == itemstack.id and IS.dmg == itemstack.dmg and IS.qty < 64 ) then return slot end
  58.   end
  59.   return -1
  60. end
  61.  
  62. function PushItem(machineID,slot)
  63.   local item = getStack(machineID,slot)
  64.   if ( item == nil ) return end
  65.   local newSlot = -1
  66.   if ( machineID < maxID ) then
  67.      newSlot = FindItemType(machineID+1,item)
  68.   end
  69.   pushCall(machineID,slot,newSlot)
  70. end
  71.  
  72.  
  73. function checkReactorVariety(machineID)
  74.   local count = 0
  75.   for i=10,18 do
  76.     local item = getStack(machineID,i)
  77.     if ( item ~= nil ) then count = count + 1 end
  78.   end
  79.   if ( count >= varietyNeeded ) then return true end
  80.   return false
  81. end
  82.  
  83. function checkSimpleReactorInventory(machineID)
  84.  
  85.   local count = 0
  86.   for i=1,9 do
  87.     if ( getStack(machineID,i) ~= nil ) then count = count + 1 end
  88.   end
  89.   if ( count >= 9 ) then PushItem(machineID,math.random(1,9)) end
  90.   if ( count >= 8 ) then return true end
  91.   return false  
  92. end
  93.  
  94. function checkReactorInventory(machineID)
  95.   if ( machineID == maxID ) then return checkSimpleReactorInventory(machineID) end
  96.  
  97.   local idList = {}
  98.   local count = 0
  99.  
  100.   -- Check the inventory of the first reactor, push duplicate stacks to the second reactor.
  101.   for i=1,9 do
  102.     local item = getStack(machineID,i)
  103.     if (  item ~= nil ) then
  104.       count = count + 1
  105.       local code = item.id .. "." .. item.dmg
  106.       if ( isIn(code,idList) ) then
  107.     --    print("Pushing " .. i .. " " .. item.id)
  108.         count = count - 1
  109.         PushItem(machineID,i)
  110.       else
  111.   --      print("Adding " .. i .. " ".. item.id)
  112.         table.insert(idList,code)      
  113.       end
  114.     end
  115.   end
  116.   if ( count >= 9 ) then PushItem(machineID,math.random(1,9)) end
  117.   if ( count >= 8 ) then return true end
  118.   return false
  119.  
  120. end
  121.  
  122. function checkReactor(machineID)
  123.   local turnOff = true
  124.  
  125.   if ( checkReactorInventory(machineID) == true )
  126.      then turnOff = false
  127.   else
  128.      turnOff = ~checkReactorVariety(machineID)
  129.   end
  130.  
  131.   return turnOff
  132. end
  133.  
  134.  
  135.  
  136. -- Main Loop
  137. while true do
  138.    local colorCode = 0
  139.    
  140.    for mID = 1,maxID do
  141.       local turnOff = checkReactor(mID)
  142.       if ( turnOff == true ) then
  143.          local myColor = bit.blshift(1,mID-1)
  144.          colorCode = colorCode + myColor
  145.       end
  146.      
  147.    
  148.    end
  149.   rs.setBundledOutput(CableSide,colorCode)
  150.  
  151.   os.sleep(10)
  152. end
Add Comment
Please, Sign In to add comment