Advertisement
jamawie

Farmprogramm

May 10th, 2016
2,573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.89 KB | None | 0 0
  1. --Farmsteuerung
  2. local monitorSide = "back"
  3. local bundledSide = "right"
  4. local bundledSide1 = "front"
  5. local bundledSide2 = "left"
  6.  
  7. --bitte nichts mehr aendern
  8. local jwood,nwarts,rgblumen,flax,kakao,kartoffeln,karotten,weizen = 0
  9. local mon = peripheral.wrap(monitorSide)
  10. local x,y = mon.getSize()
  11. local gui = 1
  12. local buttonUI = 1
  13.  
  14. --useful functions
  15. function bdraw(xp1,yp1,xp2,yp2,txt,col)
  16.  if col==nil then
  17.  mon.setBackgroundColor(colors.lime)
  18.  else
  19.  mon.setBackgroundColor(col)
  20.  end
  21.    mon.setCursorPos(xp1,yp1)
  22.    for i=1,(xp2-xp1) do
  23.     mon.write("-")
  24.    end
  25.    mon.setCursorPos(xp1,yp2)
  26.    for i=1,(xp2-xp1) do
  27.     mon.write("-")
  28.    end
  29.    for i=yp1,yp2 do
  30.     mon.setCursorPos(xp1,i)
  31.     mon.write("|")
  32.     mon.setCursorPos(xp2,i)
  33.     mon.write("|")
  34.    end
  35.  if yp1 == yp2 then
  36.     yp1 = yp1-1
  37.     yp2 = yp2+1
  38.  end
  39.  for i=(yp1+1),(yp2-1) do
  40.   mon.setCursorPos((xp1+1),i)
  41.   for j=(xp1+1),(xp2-1) do
  42.    mon.write(" ")
  43.    j = j+1
  44.   end
  45.   i = i+1
  46.  end
  47.  if math.floor(((xp2-xp1)-#txt)/2)==0 then
  48.   xp1 = xp1+1
  49.  end
  50.  mon.setCursorPos(math.floor(((xp2-xp1)-#txt)/2)+xp1,math.floor((yp2-yp1)/2)+yp1)
  51.  --print(math.floor(((xp2-xp1)-#txt)/2))
  52.  mon.write(txt)
  53. end
  54.  
  55. function bdrawnb(x1,y1,x2,y2,text,color,tcol)
  56.     mon.setBackgroundColor(color)
  57.  
  58.     for i=y1,y2 do
  59.         mon.setCursorPos(x1,i)
  60.         for j=x1,x2 do
  61.             mon.write(" ")
  62.         end
  63.     end
  64.  
  65.     if (x2-x1)<=#text then
  66.         mon.setCursorPos(x1,math.floor((y2-y1)/2)+y1)
  67.     else
  68.         mon.setCursorPos(math.floor(((x2-x1)-#text)/2)+x1,math.floor((y2-y1)/2)+y1)
  69.     end
  70.     if tcol==nil then
  71.         mon.setTextColor(colors.gray)
  72.     else
  73.         mon.setTextColor(tcol)
  74.     end
  75.     mon.write(text)
  76.     mon.setTextColor(colors.gray)
  77. end
  78.  
  79. function sdraw(x1,y1,x2,y2,text,status,status2)
  80.     if not status then
  81.         mon.setBackgroundColor(colors.lime)
  82.     elseif status2 then
  83.         mon.setBackgroundColor(colors.orange)
  84.     else
  85.         mon.setBackgroundColor(colors.red)
  86.     end
  87.  
  88.     for i=y1,y2 do
  89.         mon.setCursorPos(x1,i)
  90.         for j=x1,x2 do
  91.             mon.write(" ")
  92.         end
  93.     end
  94.  
  95.     if (x2-x1)<=#text then
  96.         mon.setCursorPos(x1,math.floor((y2-y1)/2)+y1)
  97.     else
  98.         mon.setCursorPos(math.floor(((x2-x1)-#text)/2)+x1,math.floor((y2-y1)/2)+y1)
  99.     end
  100.     mon.setTextColor(colors.gray)
  101.     mon.write(text)
  102. end
  103.  
  104. function checkb(xPos,yPos,x1,y1,x2,y2,text)
  105.     if xPos>=x1 and xPos<=x2 and yPos>=y1 and yPos<=y2 then
  106.         bdrawnb(x1,y1,x2,y2,text,colors.lightGray)
  107.         sleep(0.25)
  108.         return true
  109.     else
  110.         return false
  111.     end
  112. end
  113.  
  114. function impuls(color, side)
  115.     rs.setBundledOutput(side,rs.getBundledOutput("front")+color)
  116.     sleep(0.25)
  117.     rs.setBundledOutput(side,rs.getBundledOutput("front")-color)
  118. end
  119.  
  120. function log(text)
  121.     if not fs.exists("error.log") then
  122.         filee = fs.open("error.log","w")
  123.         filee.write(" ")
  124.         filee.close()
  125.     end
  126.     file = fs.open("error.log","r")
  127.     line = file.readLine()
  128.     file.close()
  129.     if not string.match(line,text) then
  130.         if line == " " then
  131.             filew = fs.open("error.log","w")
  132.         else
  133.             filew = fs.open("error.log","a")
  134.         end
  135.         filew.write(text .. "\n")
  136.         filew.close()
  137.     end
  138. end
  139.  
  140. --program
  141. function drawGUI1()
  142.     for i=1,y do
  143.         mon.setCursorPos(1,i)
  144.         mon.setBackgroundColor(colors.yellow)
  145.         for j=1,x do
  146.             mon.write(" ")
  147.         end
  148.     end
  149.  
  150.     mon.setTextColor(colors.gray)
  151.     mon.setCursorPos(math.floor((x-22)/2),1)
  152.     mon.write("Farmsteuerung - Status")
  153.  
  154.     local abstand = 2
  155.     local breite = 12
  156.     local pos1 = 2+abstand
  157.     local pos2 = 2+abstand+breite
  158.     sdraw(pos1,y-13,pos2,y-13,"Junglewood",rs.testBundledInput(bundledSide,colors.white))
  159.     local pos1 = pos1+abstand+breite
  160.     local pos2 = pos2+abstand+breite
  161.     sdraw(pos1,y-13,pos2,y-13,"Nether Warts",rs.testBundledInput(bundledSide,colors.orange))
  162.     local pos1 = pos1+abstand+breite
  163.     local pos2 = pos2+abstand+breite
  164.     sdraw(pos1,y-13,pos2,y-13,"R/Y Blumen",rs.testBundledInput(bundledSide,colors.magenta),rs.testBundledInput("left",colors.magenta))
  165.     if rs.testBundledInput("left",colors.magenta) then
  166.         log("Bonemeal is empty.")
  167.     end
  168.     local pos1 = pos1+abstand+breite
  169.     local pos2 = pos2+abstand+breite
  170.     sdraw(pos1,y-13,pos2,y-13,"Flax",rs.testBundledInput(bundledSide,colors.lightBlue))
  171.     local pos1 = pos1+abstand+breite
  172.     local pos2 = pos2+abstand+breite
  173.     local abstand = 2
  174.     local breite = 12
  175.     local pos1 = 2+abstand
  176.     local pos2 = 2+abstand+breite
  177.     sdraw(pos1,y-11,pos2,y-11,"Kakao",rs.testBundledInput(bundledSide,colors.yellow))
  178.     local pos1 = pos1+abstand+breite
  179.     local pos2 = pos2+abstand+breite
  180.     sdraw(pos1,y-11,pos2,y-11,"Kartoffeln",rs.testBundledInput(bundledSide,colors.lime))
  181.     local pos1 = pos1+abstand+breite
  182.     local pos2 = pos2+abstand+breite
  183.     sdraw(pos1,y-11,pos2,y-11,"Weizen",rs.testBundledInput(bundledSide,colors.pink))
  184.     local pos1 = pos1+abstand+breite
  185.     local pos2 = pos2+abstand+breite
  186.     sdraw(pos1,y-11,pos2,y-11,"Karotten",rs.testBundledInput(bundledSide,colors.gray))
  187.     local abstand = 2
  188.     local breite = 12
  189.     local pos1 = 2+abstand
  190.     local pos2 = 2+abstand+breite
  191.     sdraw(pos1,y-9,pos2,y-9,"",rs.testBundledInput(bundledSide,colors.lightGray))
  192.     local pos1 = pos1+abstand+breite
  193.     local pos2 = pos2+abstand+breite
  194.     sdraw(pos1,y-9,pos2,y-9,"",rs.testBundledInput(bundledSide,colors.cyan))
  195.     local pos1 = pos1+abstand+breite
  196.     local pos2 = pos2+abstand+breite
  197.     sdraw(pos1,y-9,pos2,y-9,"",rs.testBundledInput(bundledSide,colors.purple))
  198.     local pos1 = pos1+abstand+breite
  199.     local pos2 = pos2+abstand+breite
  200.     sdraw(pos1,y-9,pos2,y-9,"",rs.testBundledInput(bundledSide,colors.blue))
  201.     local abstand = 2
  202.     local breite = 12
  203.     local pos1 = 2+abstand
  204.     local pos2 = 2+abstand+breite
  205.     sdraw(pos1,y-7,pos2,y-7,"",rs.testBundledInput(bundledSide,colors.brown))
  206.     local pos1 = pos1+abstand+breite
  207.     local pos2 = pos2+abstand+breite
  208.     sdraw(pos1,y-7,pos2,y-7,"",rs.testBundledInput(bundledSide,colors.green))
  209.     local pos1 = pos1+abstand+breite
  210.     local pos2 = pos2+abstand+breite
  211.     sdraw(pos1,y-7,pos2,y-7,"",rs.testBundledInput(bundledSide,colors.red))
  212.     local pos1 = pos1+abstand+breite
  213.     local pos2 = pos2+abstand+breite
  214.     sdraw(pos1,y-7,pos2,y-7,"",rs.testBundledInput(bundledSide,colors.black))
  215.    
  216.     bdraw(2,y-3,18,y-1,"Menu",colors.cyan)
  217.     bdraw(20,y-3,x-21,y-1,"Not-Aus",colors.red)
  218.     bdraw(x-19,y-3,x-1,y-1,"Update",colors.lightGray)
  219. end
  220.  
  221. function drawGUI2()
  222.     for i=1,y do
  223.         mon.setCursorPos(1,i)
  224.         mon.setBackgroundColor(colors.yellow)
  225.         for j=1,x do
  226.             mon.write(" ")
  227.         end
  228.     end
  229.  
  230.     mon.setTextColor(colors.gray)
  231.     mon.setCursorPos(math.floor((x-20)/2),1)
  232.     mon.write("Farmsteuerung - Steuerung")
  233.  
  234.     mon.setTextColor(colors.gray)
  235.     mon.setCursorPos(2,4)
  236.     mon.write("Klicke auf die Farm, deren Status du umschalten moechtest.")
  237.  
  238.     bdrawnb(x-10,3,x-3,3,"error.log",colors.lightGray)
  239.  
  240.     local abstand = 2
  241.     local breite = 12
  242.     local pos1 = 2+abstand
  243.     local pos2 = 2+abstand+breite
  244.     bdrawnb(pos1,y-13,pos2,y-13,"Junglewood",colors.white)
  245.     local pos1 = pos1+abstand+breite
  246.     local pos2 = pos2+abstand+breite
  247.     bdrawnb(pos1,y-13,pos2,y-13,"Nether Warts",colors.orange)
  248.     local pos1 = pos1+abstand+breite
  249.     local pos2 = pos2+abstand+breite
  250.     bdrawnb(pos1,y-13,pos2,y-13,"R/Y Blumen",colors.magenta)
  251.     local pos1 = pos1+abstand+breite
  252.     local pos2 = pos2+abstand+breite
  253.     bdrawnb(pos1,y-13,pos2,y-13,"Flax",colors.lightBlue)
  254.     local pos1 = pos1+abstand+breite
  255.     local pos2 = pos2+abstand+breite
  256.     local abstand = 2
  257.     local breite = 12
  258.     local pos1 = 2+abstand
  259.     local pos2 = 2+abstand+breite
  260.     bdrawnb(pos1,y-11,pos2,y-11,"Kakao",colors.red)
  261.     local pos1 = pos1+abstand+breite
  262.     local pos2 = pos2+abstand+breite
  263.     bdrawnb(pos1,y-11,pos2,y-11,"Kartoffeln",colors.lime)
  264.     local pos1 = pos1+abstand+breite
  265.     local pos2 = pos2+abstand+breite
  266.     bdrawnb(pos1,y-11,pos2,y-11,"Weizen",colors.pink)
  267.     local pos1 = pos1+abstand+breite
  268.     local pos2 = pos2+abstand+breite
  269.     bdrawnb(pos1,y-11,pos2,y-11,"Karotten",colors.gray,colors.lightGray)
  270.     local abstand = 2
  271.     local breite = 12
  272.     local pos1 = 2+abstand
  273.     local pos2 = 2+abstand+breite
  274.     bdrawnb(pos1,y-9,pos2,y-9,"",colors.lightGray)
  275.     local pos1 = pos1+abstand+breite
  276.     local pos2 = pos2+abstand+breite
  277.     bdrawnb(pos1,y-9,pos2,y-9,"",colors.cyan)
  278.     local pos1 = pos1+abstand+breite
  279.     local pos2 = pos2+abstand+breite
  280.     bdrawnb(pos1,y-9,pos2,y-9,"",colors.purple)
  281.     local pos1 = pos1+abstand+breite
  282.     local pos2 = pos2+abstand+breite
  283.     bdrawnb(pos1,y-9,pos2,y-9,"",colors.blue)
  284.     local abstand = 2
  285.     local breite = 12
  286.     local pos1 = 2+abstand
  287.     local pos2 = 2+abstand+breite
  288.     bdrawnb(pos1,y-7,pos2,y-7,"",colors.brown)
  289.     local pos1 = pos1+abstand+breite
  290.     local pos2 = pos2+abstand+breite
  291.     bdrawnb(pos1,y-7,pos2,y-7,"",colors.green)
  292.     local pos1 = pos1+abstand+breite
  293.     local pos2 = pos2+abstand+breite
  294.     bdrawnb(pos1,y-7,pos2,y-7,"",colors.red)
  295.     local pos1 = pos1+abstand+breite
  296.     local pos2 = pos2+abstand+breite
  297.     bdrawnb(pos1,y-7,pos2,y-7,"",colors.black)
  298.  
  299.     bdraw(x-5,y-2,x,y,"Back",colors.red)
  300. end
  301.  
  302. function drawGUI3()
  303.     for i=1,y do
  304.         mon.setCursorPos(1,i)
  305.         mon.setBackgroundColor(colors.yellow)
  306.         for j=1,x do
  307.             mon.write(" ")
  308.         end
  309.     end
  310.  
  311.     mon.setTextColor(colors.gray)
  312.     mon.setCursorPos(math.floor((x-20)/2),1)
  313.     mon.write("Farmsteuerung - error.log")
  314.  
  315.     bdraw(x-5,y-2,x,y,"Back",colors.red)
  316.  
  317.     if not fs.exists("error.log") then
  318.         filee = fs.open("error.log","w")
  319.         filee.write(" ")
  320.         filee.close()
  321.     end
  322.     file = fs.open("error.log","r")
  323.     lines = {}
  324.     line = " "
  325.     while not line==nil do
  326.         line = file.readLine()
  327.         lines[#lines+1] = line
  328.     end
  329.     file.close()
  330.  
  331.     mon.setTextColor(colors.gray)
  332.     mon.setBackgroundColor(colors.yellow)
  333.     if #lines>10 then
  334.         pos = 6
  335.         for i=#lines-10,#lines do
  336.             mon.setCursorPos(4,pos)
  337.             pos = pos+1
  338.             mon.write(lines[i])
  339.         end
  340.     else
  341.         for i=1,#lines do
  342.             mon.setCursorPos(4,3+i)
  343.             mon.write(lines[i])
  344.         end
  345.     end
  346. end
  347.  
  348. function checkButtons(xPos, yPos)
  349.   if buttonUI==1 then
  350.     if not event=="monitor_touch" then
  351.         return
  352.     end
  353.     if xPos>=2 and xPos<=18 and yPos>=y-3 and yPos<=y-1 then
  354.         bdraw(2,y-3,18,y-1,"Menu",colors.magenta)
  355.         sleep(0.25)
  356.         gui = 2
  357.         buttonUI=2
  358.     end
  359.     if xPos>=20 and xPos<=x-21 and yPos>=y-3 and yPos<=y-1 then
  360.         bdraw(20,y-3,x-21,y-1,"Not-Aus",colors.magenta)
  361.         defcolors = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768}
  362.         rs.setBundledOutput("front",0)
  363.         for i=1,#defcolors do
  364.             if not colors.test(rs.getBundledInput("right"),defcolors[i]) then
  365.                 rs.setBundledOutput("front",rs.getBundledOutput("front")+defcolors[i])
  366.             end
  367.         end
  368.         sleep(0.25)
  369.         rs.setBundledOutput("front",0)
  370.     end
  371.     if xPos>=x-19 and xPos<=x-1 and yPos>=y-3 and yPos<=y-1 then
  372.         mon.setTextColor(colors.lightGray)
  373.         bdraw(x-19,y-3,x-1,y-1,"Update",colors.gray)
  374.         mon.setTextColor(colors.gray)
  375.         sleep(0.25)
  376.         os.reboot()
  377.     end
  378.   elseif buttonUI==2 then
  379.     if checkb(xPos,yPos,x-10,3,x-3,3,"error.log") then
  380.         gui = 3
  381.         buttonUI = 3
  382.     end
  383.  
  384.     if xPos>=x-5 and xPos<=x and y>=y-2 and y<=y then
  385.         bdraw(x-5,y-2,x,y,"Back",colors.lightGray)
  386.         sleep(0.25)
  387.         gui = 1
  388.         buttonUI = 1
  389.     end
  390.  
  391.     local abstand = 2
  392.     local breite = 12
  393.     local pos1 = 2+abstand
  394.     local pos2 = 2+abstand+breite
  395.     if checkb(xPos,yPos,pos1,y-13,pos2,y-13,"Junglewood") then
  396.         impuls(colors.white,"front")
  397.         gui = 1
  398.         buttonUI=1
  399.     end
  400.     local pos1 = pos1+abstand+breite
  401.     local pos2 = pos2+abstand+breite
  402.     if checkb(xPos,yPos,pos1,y-13,pos2,y-13,"Nether Warts") then
  403.         impuls(colors.orange,"front")
  404.         gui = 1
  405.         buttonUI=1
  406.     end
  407.     local pos1 = pos1+abstand+breite
  408.     local pos2 = pos2+abstand+breite
  409.     if checkb(xPos,yPos,pos1,y-13,pos2,y-13,"R/Y Blumen") then
  410.         impuls(colors.magenta,"front")
  411.         gui = 1
  412.         buttonUI=1
  413.     end
  414.     local pos1 = pos1+abstand+breite
  415.     local pos2 = pos2+abstand+breite
  416.     if checkb(xPos,yPos,pos1,y-13,pos2,y-13,"Flax") then
  417.         impuls(colors.lightBlue,"front")
  418.         gui = 1
  419.         buttonUI=1
  420.     end
  421.     local pos1 = pos1+abstand+breite
  422.     local pos2 = pos2+abstand+breite
  423.     local abstand = 2
  424.     local breite = 12
  425.     local pos1 = 2+abstand
  426.     local pos2 = 2+abstand+breite
  427.     if checkb(xPos,yPos,pos1,y-11,pos2,y-11,"Kakao") then
  428.         impuls(colors.yellow,"front")
  429.         gui = 1
  430.         buttonUI=1
  431.     end
  432.     local pos1 = pos1+abstand+breite
  433.     local pos2 = pos2+abstand+breite
  434.     if checkb(xPos,yPos,pos1,y-11,pos2,y-11,"Kartoffeln") then
  435.         impuls(colors.lime,"front")
  436.         gui = 1
  437.         buttonUI=1
  438.     end
  439.     local pos1 = pos1+abstand+breite
  440.     local pos2 = pos2+abstand+breite
  441.     if checkb(xPos,yPos,pos1,y-11,pos2,y-11,"Weizen") then
  442.         impuls(colors.pink,"front")
  443.         gui = 1
  444.         buttonUI=1
  445.     end
  446.     local pos1 = pos1+abstand+breite
  447.     local pos2 = pos2+abstand+breite
  448.     if checkb(xPos,yPos,pos1,y-11,pos2,y-11,"Karotten") then
  449.         impuls(colors.gray,"front")
  450.         gui = 1
  451.         buttonUI=1
  452.     end
  453.  
  454.   elseif buttonUI==3 then
  455.     if xPos>=x-5 and xPos<=x and y>=y-2 and y<=y then
  456.         bdraw(x-5,y-2,x,y,"Back",colors.red)
  457.         sleep(0.25)
  458.         gui = 1
  459.         buttonUI = 1
  460.     end
  461.   else
  462.     print("ERROR: wrong UI")
  463.   end
  464. end
  465.  
  466.  
  467. --main program
  468. while true do
  469.     if gui==1 then
  470.         drawGUI1()
  471.     elseif gui==2 then
  472.         drawGUI2()
  473.     elseif gui==3 then
  474.         drawGUI3()
  475.     else
  476.         print("ERROR: wrong GUI")
  477.     end
  478.     event, side, xPos, yPos = os.pullEvent()
  479.     if event=="monitor_touch" then
  480.         checkButtons(xPos, yPos)
  481.         print("X: " .. xPos .. ",Y: " .. yPos .. ",GUI: " .. gui .. ",BUI: " .. buttonUI)
  482.     end
  483. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement