Xgamer911

Elevator

Jan 19th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.69 KB | None | 0 0
  1. local floornum = 1
  2. local floordescription1 = "1 floor"
  3. local floordescription2 = "2 floor"
  4. local floordescription3 = "3 floor"
  5. local floordescription4 = "4 floor"
  6. local floordescription5 = "5 floor"
  7. local floordescription6 = "6 floor"
  8. local floordescription7 = "7 floor"
  9. local floordescription8 = "8 floor"
  10. local floordescription9 = "9 floor"
  11.  
  12. mouseWidth = 0
  13. mouseHeight = 0
  14. monitor = peripheral.wrap("back")
  15. monitor.clear()
  16. monitor.setCursorPos(1,1)
  17. w,h=monitor.getSize()
  18. print(w)
  19. print(h)
  20. modem = peripheral.wrap("right")
  21. rednet.open("right")
  22.  
  23. delay=math.random(10)
  24. sleep(delay)
  25. rednet.broadcast(floornum .. " active")
  26.  
  27.  
  28. local floor1available = 0
  29. local floor2available = 0
  30. local floor3available = 0
  31. local floor4available = 0
  32. local floor5available = 0
  33. local floor6available = 0
  34. local floor7available = 0
  35. local floor8available = 0
  36. local floor9available = 0
  37.  
  38. --This sets cobblestone out
  39. rs.setOutput("bottom",true)
  40.  
  41.  
  42. function drawfloors()
  43. --BUTTON SELECTION
  44.     monitor.setBackgroundColour((colours.lime))
  45.    
  46. --button1  
  47.     if floor1available==1 then
  48.         monitor.setCursorPos(1,2)
  49.         monitor.write(floordescription1)
  50.     end
  51. --button2
  52.     if floor2available==1 then
  53.         monitor.setCursorPos(1,4)
  54.         monitor.write(floordescription2)
  55.     end
  56. --button3
  57.     if floor3available==1 then
  58.         monitor.setCursorPos(1,6)
  59.         monitor.write(floordescription3)
  60.     end
  61. --button4
  62.     if floor4available==1 then
  63.         monitor.setCursorPos(1,8)
  64.         monitor.write(floordescription4)
  65.     end
  66. --button5
  67.     if floor5available==1 then
  68.         monitor.setCursorPos(1,10)
  69.         monitor.write(floordescription5)
  70.     end
  71. --button6
  72.     if floor6available==1 then
  73.         monitor.setCursorPos(1,12)
  74.         monitor.write(floordescription6)
  75.     end
  76. --button7
  77.     if floor7available==1 then
  78.         monitor.setCursorPos(1,14)
  79.         monitor.write(floordescription7)
  80.     end
  81. --button8
  82.     if floor8available==1 then
  83.         monitor.setCursorPos(1,16)
  84.         monitor.write(floordescription8)
  85.     end
  86. --button9
  87.     if floor9available==1 then
  88.         monitor.setCursorPos(1,18)
  89.         monitor.write(floordescription9)
  90.     end
  91.    
  92.     monitor.setBackgroundColour((colours.black))
  93. end
  94.  
  95.  
  96. function up()
  97.     --set to go up
  98.     rs.setOutput("bottom",false)
  99.     sleep(0.5)
  100.     rs.setOutput("left",true)
  101.     sleep(2.5)
  102.     rs.setOutput("left",false)
  103.     sleep(0.5)
  104.     rs.setOutput("bottom",true)
  105. end
  106.  
  107. function down()
  108.     --set to go down
  109.     rs.setOutput("bottom",false)
  110.     sleep(0.5)
  111.     rs.setOutput("left",true)
  112.     sleep(0.5)
  113.     rs.setOutput("bottom",true)
  114.     sleep(2.5)
  115.     rs.setOutput("left",false)
  116.     rs.setOutput("bottom",false)
  117.     sleep(0.5)
  118.     rs.setOutput("bottom",true)
  119. end
  120.  
  121. function thru()
  122.     --set to go down
  123.     rs.setOutput("top",true)
  124.     sleep(2.5)
  125.     rs.setOutput("top",false)
  126. end
  127.  
  128. function checkClickPosition()
  129.     --button1
  130.     if mouseWidth > 0 and mouseWidth < 18 and mouseHeight == 2 then
  131.         local floor1=1
  132.         if floornum == 1 then
  133.             print("You are here")
  134.             rednet.broadcast(floornum .. " active")
  135.         end    
  136.         rednet.broadcast(floornum .. " 1")
  137.         if floornum > floor1 then
  138.             up()
  139.         end
  140.         if floornum < floor1 then
  141.             down()
  142.         end    
  143.     --button2  
  144.     elseif mouseWidth > 0 and mouseWidth < 18 and mouseHeight == 4 then
  145.         local floor2=2
  146.         if floornum == 2 then
  147.             print("You are here")
  148.             rednet.broadcast(floornum .. " active")
  149.         end    
  150.         rednet.broadcast(floornum .. " 2")
  151.         if floornum > floor2 then
  152.             up()
  153.         end
  154.         if floornum < floor2 then
  155.             down()
  156.         end
  157.     --button3
  158.     elseif mouseWidth > 0 and mouseWidth < 18 and mouseHeight == 6 then
  159.         local floor3=3
  160.         if floornum == 3 then
  161.             print("You are here")
  162.             rednet.broadcast(floornum .. " active")
  163.         end    
  164.         rednet.broadcast(floornum .. " 3")
  165.         if floornum > floor3 then
  166.             up()
  167.         end
  168.         if floornum < floor3 then
  169.             down()
  170.         end
  171.     --button4  
  172.     elseif mouseWidth > 0 and mouseWidth < 18 and mouseHeight == 8 then
  173.         local floor4=4
  174.         if floornum == 4 then
  175.             print("You are here")
  176.             rednet.broadcast(floornum .. " active")
  177.         end
  178.         rednet.broadcast(floornum .. " 4")
  179.         if floornum > floor4 then
  180.             up()
  181.         end
  182.         if floornum < floor4 then
  183.             down()
  184.         end
  185.     --button5  
  186.     elseif mouseWidth > 0 and mouseWidth < 18 and mouseHeight == 10 then
  187.         local floor5=5
  188.         if floornum == 5 then
  189.             print("You are here")
  190.             rednet.broadcast(floornum .. " active")
  191.         end
  192.         --from to broadcast
  193.         rednet.broadcast(floornum .. " 5")
  194.  
  195.         if floornum > floor5 then
  196.             up()
  197.         end
  198.         if floornum < floor5 then
  199.             down()
  200.         end
  201.  
  202.  
  203.  
  204.     --button6
  205.     elseif mouseWidth > 0 and mouseWidth < 18 and mouseHeight == 12 then
  206.         local floor6=6
  207.         if floornum == 6 then
  208.             print("You are here")
  209.             rednet.broadcast(floornum .. " active")
  210.         end    
  211.         rednet.broadcast(floornum .. " 6")
  212.         if floornum > floor6 then
  213.             up()
  214.         end
  215.         if floornum < floor6 then
  216.             down()
  217.         end
  218.        
  219.  
  220.  
  221.     --button7  
  222.     elseif mouseWidth > 0 and mouseWidth < 18 and mouseHeight == 14 then
  223.         local floor7=7
  224.         if floornum == 7 then
  225.             print("You are here")
  226.             rednet.broadcast(floornum .. " active")
  227.         end    
  228.         rednet.broadcast(floornum .. " 7")
  229.         if floornum > floor7 then
  230.             up()
  231.         end
  232.         if floornum < floor7 then
  233.             down()
  234.         end
  235.  
  236.     --button8  
  237.     elseif mouseWidth > 0 and mouseWidth < 18 and mouseHeight == 16 then
  238.         local floor8=8
  239.         if floornum == 8 then
  240.             print("You are here")
  241.             rednet.broadcast(floornum .. " active")
  242.         end    
  243.         rednet.broadcast(floornum .. " 8")
  244.         if floornum > floor8 then
  245.             up()
  246.         end
  247.         if floornum < floor8 then
  248.             down()
  249.         end
  250.  
  251.     --button9  
  252.     elseif mouseWidth > 0 and mouseWidth < 18 and mouseHeight == 18 then
  253.         local floor9=9
  254.         if floornum == 9 then
  255.             print("You are here")
  256.             rednet.broadcast(floornum .. " active")
  257.         end    
  258.         rednet.broadcast(floornum .. " 9")
  259.         if floornum > floor9 then
  260.             up()
  261.         end
  262.         if floornum < floor9 then
  263.             down()
  264.         end
  265.     end --endif
  266. end --end function
  267.  
  268. function clickevent()
  269.     if event=="monitor_touch" then
  270.         mouseWidth = p2 -- sets mouseWidth
  271.         mouseHeight = p3 -- and mouseHeight
  272.         print("click at "..mouseWidth .. " X " ..mouseHeight)
  273.         checkClickPosition() -- this runs our function
  274.     end
  275. end
  276.  
  277. function main()
  278.     repeat
  279.         drawfloors()
  280.         event,p1,p2,p3 = os.pullEvent()
  281.         parallel.waitForAny(clickevent)
  282.  
  283.         if event == "rednet_message" then
  284.             print(p2)
  285.             if p2 == "1 active" then
  286.                 floor1available=1
  287.             end
  288.             if p2 == "2 active" then
  289.                 floor2available=1
  290.             end
  291.             if p2 == "3 active" then
  292.                 floor3available=1
  293.             end
  294.             if p2 == "4 active" then
  295.                 floor4available=1
  296.             end
  297.             if p2 == "5 active" then
  298.                 floor5available=1
  299.             end
  300.             if p2 == "6 active" then
  301.                 floor6available=1
  302.             end
  303.             if p2 == "7 active" then
  304.                 floor7available=1
  305.             end
  306.             if p2 == "8 active" then
  307.                 floor8available=1
  308.             end
  309.             if p2 == "9 active" then
  310.                 floor9available=1
  311.             end
  312.  
  313.             if p2 ~= "1 active" then
  314.             if p2 ~= "2 active" then
  315.             if p2 ~= "3 active" then
  316.             if p2 ~= "4 active" then
  317.             if p2 ~= "5 active" then
  318.             if p2 ~= "6 active" then
  319.             if p2 ~= "7 active" then
  320.             if p2 ~= "8 active" then
  321.             if p2 ~= "9 active" then
  322.             --this splits to and from floor messages
  323.                 local fromfloor
  324.                 local tofloor
  325.             --parses out message
  326.                 local tWords = {}
  327.                 for match in string.gmatch(p2, "[^ t]+") do
  328.                     table.insert( tWords, match )
  329.                 end
  330.             --assigns to variables
  331.                 fromfloor=tWords[1]
  332.                 tofloor=tWords[2]
  333.                 print(fromfloor.. " to floor " .. tofloor)
  334.  
  335.                 if tonumber(floornum) ~= tonumber(fromfloor) and tonumber(floornum) ~= tonumber(tofloor) then
  336.                     print("from floor")
  337.                     thru()
  338.                 end
  339.  
  340.  
  341.                 if tonumber(floornum) ~= tonumber(tofloor) and tonumber(floornum) ~= tonumber(fromfloor) then
  342.                     print("to floor")
  343.                     thru()
  344.                 end
  345.             end
  346.             end
  347.             end
  348.             end
  349.             end
  350.             end
  351.             end
  352.             end
  353.             end
  354.  
  355.         end --end if for rednetmessages
  356.     until event=="char" and p1==("x")
  357. end
  358.  
  359. --start program
  360. main()
Advertisement
Add Comment
Please, Sign In to add comment