Advertisement
Programist135

Lift

Jul 17th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.70 KB | None | 0 0
  1. local component = require("component")
  2. local rs = component.redstone
  3. local gpu = component.gpu
  4. local event = require("event")
  5. local string = require("string")
  6.  
  7. local lift_mon = "11e4d71b-9c75-4ed5-9576-f91d93a8f504"
  8.  
  9. local lift_gpu = "194dd0b2-0a9e-4980-80e3-7d84ea9c90f2"
  10. local floor_gpu = "b20f1ae5-ebf1-40e2-a384-ec2319842644"
  11.  
  12. local motors_side = require("sides").back
  13. local buttons_side = require("sides").front
  14.  
  15. local buttons = {}
  16. buttons[241] = 1
  17. buttons[231] = 2
  18. buttons[221] = 3
  19. buttons[211] = 4
  20. buttons[201] = 5
  21. buttons[191] = 6
  22.  
  23. local motors = {}
  24. motors["up"] = 0
  25. motors["down"] = 1
  26. motors["1open"] = 2
  27. motors["1close"] = 3
  28. motors["2open"] = 4
  29. motors["2close"] = 5
  30. motors["3open"] = 6
  31. motors["3close"] = 7
  32. motors["4open"] = 8
  33. motors["4close"] = 9
  34. motors["5open"] = 10
  35. motors["5close"] = 11
  36. motors["6open"] = 12
  37. motors["6close"] = 13
  38.  
  39. local monitors = {}
  40. monitors[1] = "978fd6b3-c7fe-4341-b76a-5b895005edb1"
  41. monitors[2] = "770fe293-bfee-4066-bca2-e8fa9b6aea91"
  42. monitors[3] = "eb33d09f-1ef5-41b1-a602-06b8f099b811"
  43. monitors[4] = "b33e19f1-e1aa-4016-9d83-f0bb62571304"
  44. monitors[5] = "5872fbd2-5884-4286-a4bc-6232434724c2"
  45. monitors[6] = "f940e1bf-3d6a-47ce-8ed7-7f7dd89369b0"
  46.  
  47. local up_sym = "▲"
  48. local down_sym = "▼"
  49.  
  50. local lift_gpu = component.proxy(lift_gpu)
  51. local floor_gpu = component.proxy(floor_gpu)
  52. lift_gpu.bind(lift_mon)
  53. lift_gpu.setResolution(1,3)
  54.  
  55. local function started(_, address)
  56.     if address == lift_mon then
  57.         a=true
  58.     end
  59. end
  60.  
  61. local function floor_info(cur_fl, dest_fl, updown)
  62.     for _, addr in pairs(monitors) do
  63.         floor_gpu.bind(addr)
  64.         if updown ~= "clear" then
  65.             floor_gpu.setResolution(3,3)
  66.             floor_gpu.set(1,2,tostring(cur_fl))
  67.             floor_gpu.set(2,2,"→")
  68.             floor_gpu.set(3,2,tostring(dest_fl))
  69.         end
  70.         if updown == "up" then
  71.             floor_gpu.set(1,1,up_sym)
  72.         elseif updown == "down" then
  73.             floor_gpu.set(1,3,down_sym)
  74.         elseif updown == "clear" then
  75.             floor_gpu.set(1,1," ")
  76.             floor_gpu.set(1,3," ")
  77.             floor_gpu.setResolution(1,1)
  78.             floor_gpu.set(1,1,tostring(cur_fl))
  79.         end
  80.     end
  81. end
  82.  
  83. local function start(floor_num, current_floor)
  84.     print("Запуск лифта")
  85.     print("Закрытие двери на "..current_floor.." этаже")
  86.     rs.setBundledOutput(motors_side, motors[current_floor.."open"], 0)
  87.     rs.setBundledOutput(motors_side, motors[current_floor.."close"], 255)
  88.     os.sleep(3)
  89.     a2b = (floor_num - current_floor)
  90.     print("Отладочная информация: значение a2b = "..a2b)
  91.     io.write("Направление: ")
  92.     if string.sub(a2b,1,1) == "-" then
  93.         print("вниз, "..floor_num.." этаж")
  94.         floor_info(current_floor, floor_num, "down")
  95.         a2b = string.sub(a2b, 2, string.len(a2b))
  96.         for i=1, a2b do
  97.             rs.setBundledOutput(motors_side, motors["down"], 255)
  98.             os.sleep(10)
  99.             rs.setBundledOutput(motors_side, motors["down"], 0)
  100.             current_floor = current_floor-1
  101.             floor_info(current_floor, floor_num)
  102.             os.sleep(1)
  103.         end
  104.     else
  105.         print("вверх, "..floor_num.." этаж")
  106.         floor_info(current_floor, floor_num, "up")
  107.         for i=1, a2b do
  108.             rs.setBundledOutput(motors_side, motors["up"], 255)
  109.             os.sleep(10)
  110.             rs.setBundledOutput(motors_side, motors["up"], 0)
  111.             current_floor = current_floor+1
  112.             floor_info(current_floor, floor_num)
  113.             os.sleep(1)
  114.         end
  115.     end
  116.     floor_info(current_floor, floor_num, "clear")
  117.     print("Прибыл к этажу "..floor_num)
  118.     print("-------------")
  119.     os.sleep(1)
  120.     lift_gpu.bind(lift_mon)
  121.     lift_gpu.setResolution(1,3)
  122.     lift_gpu.setForeground(0x00FF00)
  123.     lift_gpu.set(1,2,tostring(current_floor))
  124.     rs.setBundledOutput(motors_side, motors[current_floor.."open"], 255)
  125.     rs.setBundledOutput(motors_side, motors[current_floor.."close"], 0)
  126.     return current_floor
  127. end
  128.  
  129. print("Калибровка лифта")
  130. print("Закрытие дверей")
  131.  
  132. for door=6, 2, -1 do
  133.     rs.setBundledOutput(motors_side, motors[door.."open"], 0)
  134.     rs.setBundledOutput(motors_side, motors[door.."close"], 255)
  135. end
  136.  
  137. print("Спуск лифта")
  138. rs.setBundledOutput(motors_side, motors["down"], 255)
  139. event.listen("component_removed", started)
  140. os.sleep(0.5)
  141. if a==true then
  142.     print("Ожидание спуска лифта")
  143.     event.pull("component_added")
  144.     print("Лифт спущен")
  145. else
  146.     print("Спуск не требуется")
  147. end
  148. event.ignore("component_removed", started)
  149. rs.setBundledOutput(motors_side, motors["down"], 0)
  150. local current_floor = 1
  151. floor_num = current_floor
  152.  
  153. print("Открытие нижней двери")
  154. rs.setBundledOutput(motors_side, motors["1close"], 0)
  155. rs.setBundledOutput(motors_side, motors["1open"], 255)
  156. os.sleep(3)
  157.  
  158. print("Лифт готов к работе")
  159. print("-------------")
  160. floor_info(current_floor, _, "clear")
  161. lift_gpu.set(1,1,up_sym)
  162. lift_gpu.set(1,3,down_sym)
  163. lift_gpu.setForeground(0x00FF00)
  164. lift_gpu.set(1,2,tostring(current_floor))
  165.  
  166. while true do
  167.     ev, add, s, y, signal = event.pull()
  168.     if ev == "touch" and add == lift_mon then
  169.         lift_gpu.setForeground(0xFF0000)
  170.         if y == 1 then
  171.             if floor_num ~= 6 then
  172.                 floor_num = floor_num+1
  173.                 lift_gpu.set(1,2,tostring(floor_num))
  174.             end
  175.         elseif y == 3 then
  176.             if floor_num ~= 1 then
  177.                 floor_num = floor_num-1
  178.                 lift_gpu.set(1,2,tostring(floor_num))
  179.             end
  180.         elseif y == 2 then
  181.             if current_floor ~= floor_num then
  182.                 current_floor = start(floor_num, current_floor)
  183.             else
  184.                 lift_gpu.setResolution(1,3)
  185.                 lift_gpu.setForeground(0x00FF00)
  186.                 lift_gpu.set(1,2,tostring(current_floor))
  187.             end
  188.         end
  189.     elseif ev == "redstone_changed" and s == buttons_side then
  190.         fl = buttons[signal]
  191.         --if fl ~= current_floor then
  192.             print("Вызов на этаж "..fl)
  193.             current_floor = start(fl, current_floor)
  194.             floor_num = current_floor
  195.         --end
  196.     end
  197. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement