UNOBTANIUM

BarrelComputer

Jun 14th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.91 KB | None | 0 0
  1. local barrel = {}
  2. local state = {false,false,false}
  3.  
  4. rednet.open("right")
  5.  
  6. function clear()
  7.  term.clear()
  8.  term.setCursorPos(1,1)
  9. end
  10.  
  11. function saveVar()
  12.  local file = fs.open("BeeBarrelStorage","w")
  13.  for b=1,3 do
  14.   file.writeLine(barrel[b])
  15.   file.writeLine(state[b])
  16.  end
  17.  file.close()
  18. end
  19.  
  20. function loadVar()
  21.  if not fs.exists("BeeBarrelStorage") then setVar() return end
  22.  local file = fs.open("BeeBarrelStorage","r")
  23.   for b=1,3 do
  24.    barrel[b] = file.readLine()
  25.    c = file.readLine()
  26.    if c == "true" then
  27.     state[b] = true
  28.    else
  29.     state[b] = false
  30.    end
  31.   end
  32.  file.close()
  33. end
  34.  
  35. function setVar()
  36.  clear()
  37.  print("Which product is in the left barrel?")
  38.  barrel[3] = read()
  39.  print("Which product is in the middle barrel?")
  40.  barrel[2] = read()
  41.  print("Which product is in the right barrel?")
  42.  barrel[1] = read()
  43.  print("Is this right?")
  44.  print("Y/N")
  45.  while true do
  46.   local event = {os.pullEvent("char")}
  47.   if event[2] == "n" then
  48.    setVar()
  49.    return
  50.   else
  51.    saveVar()
  52.    return
  53.   end
  54.  end
  55. end
  56.  
  57. function sendFull(b)
  58.  rednet.broadcast(textutils.serialize({"full",barrel[b]}))
  59.  sleep(0.5)
  60. end
  61.  
  62. function sendNotFull(b)
  63.  rednet.broadcast(textutils.serialize({"notFull", barrel[b]}))
  64.  sleep(0.5)
  65. end
  66.  
  67.  
  68. function check()
  69.  print("Left: " .. barrel[3])
  70.  print("Middle: " .. barrel[2])
  71.  print("Right: " .. barrel[1])
  72.  local event = {os.pullEvent("redstone")}
  73.  local b = 0
  74.  local side = ""
  75.  if not state[1] == redstone.getInput("front") then
  76.   b = 1
  77.   side = "front"
  78.  elseif not state[2] == redstone.getInput("left") then
  79.   b = 2
  80.   side = "left"
  81.  elseif not state[3] == redstone.getInput("back") then
  82.   b = 3
  83.   side = "back"
  84.  end
  85.  if b == 0 then
  86.   print("Wrong side!")
  87.   return
  88.  end
  89.  if redstone.getInput(side) then
  90.   sendFull(b)
  91.   state[b] = true
  92.  else
  93.   sendNotFull(b)
  94.   state[b] = false
  95.  end
  96.  saveVar()
  97. end
  98.  
  99. loadVar()
  100. while true do
  101.  check()
  102. end
Advertisement
Add Comment
Please, Sign In to add comment