Advertisement
HandieAndy

elevator

Nov 2nd, 2016
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.79 KB | None | 0 0
  1. local prot = "elev1"
  2. local floorNumber = 0
  3. local totalFloors = 9--Start counting at 0
  4. local launchCode = colors.magenta
  5. local cartDetectCode = colors.orange
  6. local elevatorCode = colors.white
  7.  
  8. local RS_SIDE = "back"
  9.  
  10. rednet.open("right")
  11. mon = peripheral.wrap("monitor_0")
  12. local sizeX,sizeY = mon.getSize()
  13.  
  14. function isCartHere()
  15.   return redstone.testBundledInput(RS_SIDE,cartDetectCode)
  16. end
  17.  
  18. function releaseCart()
  19.   redstone.setBundledOutput(RS_SIDE,launchCode)
  20.   sleep(0.5)
  21.   redstone.setBundledOutput(RS_SIDE,0)
  22. end
  23.  
  24. function callCart()
  25.   redstone.setBundledOutput(RS_SIDE,elevatorCode)
  26.   rednet.broadcast("CALL",prot)
  27. end
  28.  
  29. function goToFloor(f)
  30.   if ((f <= totalFloors) and (f ~= floorNumber) and (isCartHere())) then
  31.     rednet.broadcast("SEND "..f,prot)
  32.     releaseCart()
  33.   end
  34. end
  35.  
  36. function update()
  37.   local event,p1,p2,p3,p4,p5 = os.pullEvent()
  38.   if (event == "monitor_touch") then
  39.     local x = p2
  40.     local y = p3
  41.     if (x == sizeX) then
  42.       if (not isCartHere()) then
  43.         callCart()
  44.       end
  45.     elseif (x >= 4) then
  46.       local f = (y*2)-1
  47.       if (f <= totalFloors) then
  48.         goToFloor(f)
  49.       end
  50.     else
  51.       local f = (y*2)-2
  52.       if (f <= totalFloors) then
  53.         goToFloor(f)
  54.       end
  55.     end
  56.   elseif ((event == "rednet_message") and (p3 == prot)) then
  57.     local keywords = {}
  58.     for i in string.gmatch(p2,"%S+") do
  59.       table.insert(keywords,i)
  60.     end
  61.     if (keywords[1] == "CALL") then
  62.       if (isCartHere()) then
  63.         releaseCart()
  64.       end
  65.     elseif ((keywords[1] == "SEND") and (tonumber(keywords[2]) == floorNumber)) then
  66.       redstone.setBundledOutput(RS_SIDE,elevatorCode)
  67.     end
  68.   elseif (event == "redstone") then
  69.     if (isCartHere()) then
  70.       --Cart Arrived
  71.       redstone.setBundledOutput(RS_SIDE,0)
  72.     end
  73.   end
  74. end
  75.  
  76. function display()
  77.   mon.setBackgroundColor(colors.black)
  78.   mon.clear()
  79.   mon.setCursorPos(1,1)
  80.   mon.setTextColor(colors.white)
  81.   local c = colors.black
  82.   for i=0,totalFloors do
  83.     if ((i % 4 == 0) or ((i+1) % 4 == 0)) then
  84.       c = colors.black
  85.     else
  86.       c = colors.gray
  87.     end
  88.     local x,y = mon.getCursorPos()
  89.     if (x > sizeX-1) then
  90.       mon.setCursorPos(1,y+1)
  91.     end
  92.     mon.setBackgroundColor(c)
  93.     if (i == floorNumber) then
  94.       mon.setTextColor(colors.lightGray)
  95.     else
  96.       mon.setTextColor(colors.white)
  97.     end
  98.     mon.write(" "..tostring(i).." ")
  99.   end
  100.   mon.setBackgroundColor(colors.white)
  101.   for i=1,sizeY do
  102.     mon.setCursorPos(sizeX,i)
  103.     mon.write(" ")
  104.   end
  105.   mon.setTextColor(colors.blue)
  106.   mon.setCursorPos(sizeX,1)
  107.   mon.write("C")
  108.   mon.setCursorPos(sizeX,2)
  109.   mon.write("A")
  110.   mon.setCursorPos(sizeX,3)
  111.   mon.write("L")
  112.   mon.setCursorPos(sizeX,4)
  113.   mon.write("L")
  114. end
  115.  
  116. display()
  117. while (true) do
  118.   update()
  119. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement