Advertisement
UNOBTANIUM

ControlComputer

Jun 13th, 2013
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.27 KB | None | 0 0
  1. -- VERSION 0.3.1 by unobtanium
  2. local alveary = {}
  3. local product = {}
  4. local selectedHeight = 1
  5. local autoShutdown = true
  6. local mode = "turnOnOrOff"
  7. local colorNumber = 2
  8. local temporaryAlveary = {}
  9. local colorName = {colors.white, colors.orange, colors.magenta, colors.lightBlue, colors.yellow, colors.lime, colors.pink, colors.lightGray, colors.cyan, colors.purple, colors.blue, colors.brown, colors.green, colors.red, colors.black}
  10.  
  11. rednet.open("back")
  12. local monitor = peripheral.wrap("right")
  13. term.redirect(monitor)
  14.  
  15. -- product[productname] = amount
  16. -- alveary[height][x][y][BeeType|Number|Product|Color|Mode]
  17. for h = 1,3 do
  18.  alveary[h] = {}
  19.  for x= 1,4 do
  20.   alveary[h][x] = {}
  21.   for y=1,4 do
  22.    alveary[h][x][y] = {}
  23.    for var=1,5 do
  24.     if var == 2 or var == 5 then
  25.      alveary[h][x][y][var] = 0
  26.     elseif var == 4 then
  27.      alveary[h][x][y][var] = colors.white
  28.     else
  29.      alveary[h][x][y][var] = "NONE"
  30.     end
  31.    end
  32.   end
  33.  end
  34. end
  35.  
  36. function saveVar()
  37.  local file = fs.open("beeSystemVariables","w")
  38.  file.writeLine(tostring(autoShutdown))
  39.  file.writeLine(colorNumber)
  40.  for x = 1,4 do
  41.   for y= 1,4 do
  42.    for h=1,3 do
  43.     for var=1,5 do
  44.      file.writeLine(alveary[h][x][y][var])
  45.     end
  46.    end
  47.   end
  48.  end
  49.  for k,v in pairs(product) do
  50.   file.writeLine(k)
  51.   file.writeLine(v)
  52.  end
  53.  file.close()
  54. end
  55.  
  56. function loadVar()
  57.  if not fs.exists("beeSystemVariables") then return end
  58.  local file = fs.open("beeSystemVariables","r")
  59.  autoShutdown = file.readLine()
  60.  colorNumber = tonumber(file.readLine())
  61.  for x = 1,4 do
  62.   for y= 1,4 do
  63.    for h=1,3 do
  64.     for var=1,5 do
  65.      if var == 2 or var == 4 or var == 5 then
  66.       alveary[h][x][y][var] = tonumber(file.readLine())
  67.      else
  68.       alveary[h][x][y][var] = file.readLine()
  69.      end
  70.     end
  71.    end
  72.   end
  73.  end
  74.  local k = file.readLine()
  75.  while k do
  76.   v = tonumber(file.readLine())
  77.   product[k] = v
  78.   k = file.readLine()
  79.  end
  80.  file.close()
  81. end
  82.  
  83. function centeredAt(text,x,y,c)
  84.  term.setTextColor(c)
  85.  term.setCursorPos(x - #text/2, y)
  86.  term.write(text)
  87. end
  88.  
  89. -- ADD
  90.  
  91. function addAlveary(x,y)
  92.  clear()
  93.  term.setCursorPos(1,12)
  94.  term.write("What do you want to do?")
  95.  term.setCursorPos(1,14)
  96.  if alveary[selectedHeight][x][y][2] == 0 then
  97.   term.write("ADD      QUIT")
  98.  else
  99.   term.write("CHANGE   QUIT   DELETE   SWITCH")
  100.  end
  101.  while true do
  102.   local event = {os.pullEvent("monitor_touch")}
  103.   if event[4] == 14 then
  104.    if event[3] >= 1 and event[3] <= 6 then
  105.     break
  106.    elseif event[3] >= 10 and event[3] <= 13 then
  107.     return
  108.    elseif alveary[selectedHeight][x][y][2] > 0 and event[3] >= 17 and event[3] <= 22 then
  109.     clear()
  110.     term.setCursorPos(1,12)
  111.     term.write("Do you really want to delete this alveary slot?")
  112.     term.setCursorPos(1,14)
  113.     term.write("YES")
  114.     term.setCursorPos(1,15)
  115.     term.write("NO")
  116.     sleep(0.1)
  117.     while true do
  118.      local e = {os.pullEvent("monitor_touch")}
  119.      if e[4] == 14 then
  120.       alveary[selectedHeight][x][y] = {"NONE",0,"NONE",colors.white,0}
  121.       return
  122.      elseif e[4] == 15 then
  123.       return
  124.      end
  125.     end
  126.    elseif alveary[selectedHeight][x][y][2] > 0 and event[3] >= 26 and event[3] <= 31 then
  127.     temporaryAlveary = {selectedHeight, x, y}
  128.     mode = "switch"
  129.     return
  130.    end
  131.   end
  132.  end
  133.  term.clear()
  134.  term.setCursorPos(16,13)
  135.  term.write("Tipe into the computer screen!")
  136.  repeat
  137.   term.restore()
  138.   local w, h = term.getSize()
  139.  until w == 51 and h == 19
  140.  term.setBackgroundColor(colors.black)
  141.  clear()
  142.  term.setTextColor(colors.white)
  143.  term.setCursorPos(1,1)
  144.  term.write("Enter the name of the bee type in the alveary:")
  145.  term.setCursorPos(1,2)
  146.  local newName = read()
  147.  term.setCursorPos(1,3)
  148.  term.write("Enter the individual number of " .. newName)
  149.  term.setCursorPos(1,4)
  150.  local newNumber = tonumber(read())
  151.  term.setCursorPos(1,5)
  152.  print("Enter the name of the product, which gets produced by this bee:")
  153.  term.setCursorPos(1,7)
  154.  local newProduct = read()
  155.  term.setCursorPos(1,8)
  156.  print("Select a color for this bee (click it)")
  157.  for c=1,14 do
  158.   term.setBackgroundColor(colorName[c])
  159.   term.setCursorPos(2*c-1,10)
  160.   term.write("  ")
  161.   term.setCursorPos(2*c-1,11)
  162.   term.write("  ")
  163.  end
  164.  term.setBackgroundColor(colors.black)
  165.  local newColor = colors.white
  166.  while true do
  167.   local event = {os.pullEvent("mouse_click")}
  168.   if event[4] == 10 or event[4] == 11 and event[3] <= 28 then
  169.    newColor = colorName[math.ceil(event[3]/2)]
  170.    break
  171.   end
  172.  end
  173.  term.setCursorPos(1,13)
  174.  term.setTextColor(newColor)
  175.  term.write("Do you want to add this alveary to your list? (click it)")
  176.  term.setCursorPos(1,14)
  177.  term.write("YES")
  178.  term.setCursorPos(1,15)
  179.  term.write("NO")
  180.  while true do
  181.   local event = {os.pullEvent("mouse_click")}
  182.   if event[4] == 14 then
  183.    clear()
  184.    term.redirect(monitor)
  185.    alveary[selectedHeight][x][y] = {newName, newNumber, newProduct, newColor, 0}
  186.    if type(product[newProduct]) == "nil" then
  187.     product[newProduct] = 0
  188.    end
  189.    saveVar()
  190.    return
  191.   elseif event[4] == 15 then
  192.    clear()
  193.    term.redirect(monitor)
  194.    return
  195.   end
  196.  end
  197. end
  198.  
  199. -- DRAW
  200.  
  201. function clear()
  202.  term.clear()
  203.  term.setCursorPos(1,1)
  204. end
  205.  
  206. function draw()
  207.  -- BUTTONS
  208.  term.clear()
  209.  term.setBackgroundColor(colorName[colorNumber])
  210.  for y=1,29 do
  211.   term.setCursorPos(1,y)
  212.   term.write("     ")
  213.  end
  214.  for button=1,3 do
  215.   local color = colors.black
  216.   if button == selectedHeight then
  217.    color = colors.lime
  218.   end
  219.   term.setBackgroundColor(color)
  220.   term.setCursorPos(1,5*button-4)
  221.   term.write("    ")
  222.   term.setCursorPos(1,5*button)
  223.   term.write("    ")
  224.   for y=5*button-3, 5*button-1 do
  225.    term.setCursorPos(4,y)
  226.    term.write(" ")
  227.   end
  228.  end
  229.  local color = colors.black
  230.  if mode == "add" then
  231.   color = colors.lime
  232.  elseif mode == "switch" then
  233.   color = colors.red
  234.  end
  235.  term.setBackgroundColor(color)
  236.  term.setCursorPos(1,17)
  237.  term.write("    ")
  238.  term.setCursorPos(1,21)
  239.  term.write("    ")
  240.  for y=18,20  do
  241.   term.setCursorPos(4,y)
  242.   term.write(" ")
  243.  end
  244.  local color = colors.black
  245.  if autoShutdown then
  246.   color = colors.lime
  247.  end
  248.  term.setBackgroundColor(color)
  249.  term.setCursorPos(1,22)
  250.  term.write("    ")
  251.  term.setCursorPos(1,26)
  252.  term.write("    ")
  253.  for y=23,25  do
  254.   term.setCursorPos(4,y)
  255.   term.write(" ")
  256.  end
  257.  if colorNumber == 15 then
  258.   term.setTextColor(colors.white)
  259.  else
  260.   term.setTextColor(colors.black)
  261.  end
  262.  term.setBackgroundColor(colorName[colorNumber])
  263.  term.setCursorPos(2,3)
  264.  term.write("A")
  265.  term.setCursorPos(2,8)
  266.  term.write("B")
  267.  term.setCursorPos(2,13)
  268.  term.write("C")
  269.  term.setCursorPos(2,19)
  270.  term.write("+")
  271.  term.setCursorPos(2,24)
  272.  term.write("A")
  273.  
  274. -- ALVEARIES
  275.  term.setBackgroundColor(colors.black)
  276.  for x=1,4 do
  277.   for y=1,4 do
  278.    if alveary[selectedHeight][x][y][2] == 0 and (mode == "add" or mode == "switch") then
  279.     centeredAt("NONE", x*13, y*6-2, colors.white)
  280.    end
  281.    if alveary[selectedHeight][x][y][2] > 0 then
  282.     centeredAt(tostring(alveary[selectedHeight][x][y][1] .. " " .. alveary[selectedHeight][x][y][2]) , x*13, y*6-2, alveary[selectedHeight][x][y][4])
  283.     if alveary[selectedHeight][x][y][5] == 1 then
  284.      centeredAt("ON", x*13, y*6-1, colors.lime)
  285.     else
  286.      centeredAt("OFF", x*13, y*6-1, colors.red)
  287.     end
  288.     if product[alveary[selectedHeight][x][y][3]] == 1 then
  289.      centeredAt("FULL", x*13, y*6, colors.red)
  290.     end
  291.    end
  292.   end
  293.  end
  294.  term.setTextColor(colors.white)
  295. end
  296.  
  297.  
  298. -- CHECK TOUCH
  299.  
  300. function checkForAlvearySlot(xPos,yPos)
  301.  local var = {0,0}
  302.  for i=1,4 do
  303.   for j=1,4 do
  304.    if xPos >= (13*i)-5 and xPos <= (13*i)+5 and yPos >= (6*j)-3 and yPos <= (6*j) then
  305.     if mode == "add" or mode == "switch" or alveary[selectedHeight][i][j][2] > 0 then
  306.      var[1], var[2] = i,j
  307.      break
  308.     end
  309.    end
  310.   end
  311.  end
  312.  return var
  313. end
  314.  
  315. function checkTouch()
  316.  saveVar()
  317.  local event = { os.pullEvent() }
  318.  if event[1] == "monitor_touch" then
  319.   local xPos, yPos = event[3], event[4]
  320.   if xPos <= 4 then
  321.    if yPos >= 1 and yPos <= 5 then
  322.     selectedHeight = 1
  323.    elseif yPos >= 6 and yPos <= 10 then
  324.     selectedHeight = 2
  325.    elseif yPos >= 11 and yPos <= 15 then
  326.     selectedHeight = 3
  327.    elseif yPos >= 17 and yPos <= 21 then
  328.     if mode == "turnOnOrOff" then
  329.      mode = "add"
  330.     else
  331.      mode = "turnOnOrOff"
  332.     end
  333.    elseif yPos >= 22 and yPos <= 26 then
  334.     autoShutdown = not autoShutdown
  335.    end
  336.   elseif xPos == 5 then
  337.    colorNumber = colorNumber + 1
  338.    if colorNumber == 16 then
  339.     colorNumber = 1
  340.    end
  341.   else
  342.    local var = checkForAlvearySlot(xPos,yPos)
  343.    local x,y = var[1], var[2]
  344.    if x > 0 then
  345.     if mode == "turnOnOrOff" then
  346.      if alveary[selectedHeight][x][y][5] == 1 then
  347.       alveary[selectedHeight][x][y][5] = 0
  348.       rednet.broadcast(textutils.serialize({"TurnOff", alveary[selectedHeight][x][y][1], alveary[selectedHeight][x][y][2] }))
  349.      elseif alveary[selectedHeight][x][y][5] == 0 then
  350.       alveary[selectedHeight][x][y][5] = 1
  351.       rednet.broadcast(textutils.serialize({"TurnOn", alveary[selectedHeight][x][y][1], alveary[selectedHeight][x][y][2] }))
  352.      end
  353.      sleep(0.1)
  354.     elseif mode == "add" then
  355.      addAlveary(x,y)
  356.     elseif mode == "switch" then
  357.      local temp = alveary[selectedHeight][x][y]
  358.      alveary[selectedHeight][x][y] = alveary[temporaryAlveary[1]][temporaryAlveary[2]][temporaryAlveary[3]]
  359.      alveary[temporaryAlveary[1]][temporaryAlveary[2]][temporaryAlveary[3]] = temp
  360.      mode = "add"
  361.     end
  362.    end
  363.   end
  364.  elseif event[1] == "rednet_message" then
  365.   local message = textutils.unserialize(event[3])
  366.   if type(message[1]) == "string" and message[1] == "full" then
  367.    local selectedAlveary = {}
  368.    local times = 0
  369.    for h=1,3 do
  370.     for x=1,4 do
  371.      for y=1,4 do
  372.       if alveary[h][x][y][3] == message[2] then
  373.        table.insert(selectedAlveary, 1, {h,x,y})
  374.        times = times + 1
  375.       end
  376.      end
  377.     end
  378.    end
  379.    if times == 0 then return end
  380.    for k,v in pairs(selectedAlveary) do
  381.     product[alveary[selectedAlveary[k][1]][selectedAlveary[k][2]][selectedAlveary[k][3]][3]] = 1
  382.     if autoShutdown then
  383.      alveary[selectedAlveary[k][1]][selectedAlveary[k][2]][selectedAlveary[k][3]][5] = 0
  384.       rednet.broadcast(textutils.serialize({"TurnOff", alveary[selectedAlveary[k][1]][selectedAlveary[k][2]][selectedAlveary[k][3]][1], alveary[selectedAlveary[k][1]][selectedAlveary[k][2]][selectedAlveary[k][3]][2] }))
  385.     end  
  386.    end
  387.   elseif type(message[1]) == "string" and message[1] == "notFull" then
  388.    local selectedAlveary = {}
  389.    local times = 0
  390.    for h=1,3 do
  391.     for x=1,4 do
  392.      for y=1,4 do
  393.       if alveary[h][x][y][3] == message[2] then
  394.        table.insert(selectedAlveary, 1, {h,x,y})
  395.        times = times + 1
  396.       end
  397.      end
  398.     end
  399.    end
  400.    if times == 0 then return end
  401.    for k,v in pairs(selectedAlveary) do
  402.     product[alveary[selectedAlveary[k][1]][selectedAlveary[k][2]][selectedAlveary[k][3]][3]] = 0
  403.     if autoShutdown then
  404.      alveary[selectedAlveary[k][1]][selectedAlveary[k][2]][selectedAlveary[k][3]][5] = 1
  405.       rednet.broadcast(textutils.serialize({"TurnOn", alveary[selectedAlveary[k][1]][selectedAlveary[k][2]][selectedAlveary[k][3]][1], alveary[selectedAlveary[k][1]][selectedAlveary[k][2]][selectedAlveary[k][3]][2] }))
  406.     end  
  407.    end
  408.   end
  409.  end
  410. end
  411.  
  412.  
  413. -- RUN
  414.  
  415. loadVar()
  416. while true do
  417.  draw()
  418.  checkTouch()
  419. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement