Advertisement
m3Zz

ChestControl

May 3rd, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.94 KB | None | 0 0
  1. chestSide = "left"
  2. signalSide1 = "top"
  3. signalSide2 = "bottom"
  4. signalSide3 = "right"
  5. updateDelay = 5 --in seconds
  6. fill = 0.98 --in % --> 0-1
  7. x,y = term.getSize()
  8.  
  9. function clear() --GUI stuff.
  10.  term.clear()
  11.  term.setCursorPos(1,1)
  12. end
  13.  
  14. function drawLine() --GUI stuff.
  15.  for i=1,x-1 do
  16.   write("-")
  17.  end
  18. end
  19.  
  20. function valueExists(tbl,value) --Checks if a value exists in a table.
  21.  for k,v in ipairs(tbl) do
  22.   if value == v then
  23.    return true
  24.   end
  25.  end
  26.  return false
  27. end
  28.  
  29. function setup()
  30.  clear()
  31.  drawLine()
  32.  print("Monitoring on side: "..chestSide)
  33.  print("Sending signal to side: "..signalSide1.."|"..signalSide2.."|"..signalSide3.." every "..updateDelay.." seconds.")
  34.  print("Outputting if above "..(fill*100).."%")
  35.  drawLine()
  36.  emptying = false
  37.  chest = peripheral.wrap(chestSide)
  38.  if chest == nil then
  39.   clear()
  40.   drawLine()
  41.   print("No chest found!")
  42.   drawLine()
  43.   error()
  44.  end
  45.  sleep(2)
  46. end
  47.  
  48. function display()
  49.  clear()
  50.  drawLine()
  51.  print("Status:")
  52.  print("Chest: "..currentContent.."/"..maxContent*64)
  53.  percentage = (currentContent *100) / (maxContent*64)
  54.  print("Chest is "..math.floor(percentage).."% full.")
  55.  print("Threshold is: "..(fill*100).."%")
  56.  signal = currentContent > ((maxContent*64)*fill)
  57.  if signal then
  58.   print("Above Threshold!")
  59.  else
  60.   print("Below Threshold.")
  61.  end
  62.  print("")
  63.  print("Update Interval: "..updateDelay.."s")
  64.  drawLine()
  65. end
  66.  
  67. function main()
  68.  while true do
  69.   maxContent = chest.getInventorySize()
  70.   currentContent = 0
  71.   for i = 1,maxContent do
  72.    data = chest.getStackInSlot(i)
  73.    if data == nil then
  74.     data = 0
  75.    else
  76.     currentContent = currentContent + data.qty
  77.    end
  78.   end
  79.   display()
  80.   if signal then
  81.    emptying = true
  82.   end
  83.   if currentContent == 0 then
  84.    emptying = false
  85.   end
  86.   rs.setOutput(signalSide1,emptying)
  87.   rs.setOutput(signalSide2,emptying)
  88.   rs.setOutput(signalSide3,emptying)
  89.   sleep(updateDelay)
  90.  end
  91. end
  92.  
  93. setup()
  94. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement