StreamerYT

robot.pult.lua

Nov 5th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.83 KB | None | 0 0
  1. local component = require("component")
  2. local computer = require("computer")
  3. local gpu = component.gpu
  4. local event = require("event")
  5.  
  6. local b_color, f_color = gpu.getBackground()
  7.  
  8. local modem;
  9. local powerrobot = math.ceil(0);
  10. local power1robot = math.floor(0 / 10);
  11. local indicatorrobot = "";
  12.  
  13. if component.isAvailable("modem") then
  14.     modem = component.modem
  15. else
  16.     error("Этой программе требуется беспроводной модем для работы!")
  17. end
  18.  
  19. print('Введите ключ')
  20.  
  21. local port = 512
  22. local keyWord = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" .. io.read() .. "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
  23.  
  24. require("term").clear()
  25.  
  26. modem.open(port)
  27.  
  28. -------------------------------------------------------------------------------------
  29.  
  30. local commands = {
  31.     [17] = {
  32.         messageToRobot = "forward",
  33.         screenText = "↑",
  34.     },
  35.     [31] = {
  36.         messageToRobot = "back",
  37.         screenText = "↓",
  38.     },
  39.     [30] = {
  40.         messageToRobot = "turnLeft",
  41.         screenText = "←",
  42.     },
  43.     [32] = {
  44.         messageToRobot = "turnRight",
  45.         screenText = "→",
  46.     },
  47.     [57] = {
  48.         messageToRobot = "up",
  49.         screenText = "▲",
  50.     },
  51.     [42] = {
  52.         messageToRobot = "down",
  53.         screenText = "▼",
  54.     },
  55.     [47] = {
  56.         messageToRobot = "use",
  57.         screenText = ">",
  58.     },
  59.     [46] = {
  60.         messageToRobot = "swing",
  61.         screenText = "<",
  62.     },
  63.     [14] = {
  64.         messageToRobot = "exit",
  65.         screenText = "",
  66.     },
  67.     [19] = {
  68.         messageToRobot = "redstone",
  69.         screenText = "Приказываю роботу включить/выключить редстоун вокруг себя",
  70.     },
  71. }
  72.  
  73. function fill(x,y,w,h,cb,cf,t)
  74.     gpu.setBackground(cb)
  75.     gpu.setForeground(cf)
  76.     gpu.fill(x,y,w,h,t)
  77.     gpu.setBackground(b_color)
  78.     gpu.setForeground(f_color)
  79. end
  80.  
  81. function set(x,y,cb,cf,t)
  82.     gpu.setBackground(cb)
  83.     gpu.setForeground(cf)
  84.     gpu.set(x,y,t)
  85.     gpu.setBackground(b_color)
  86.     gpu.setForeground(f_color)
  87. end
  88.  
  89. local function send()
  90.     while true do
  91.         local eventData = { event.pull() }
  92.         if eventData[1] == "key_down" then         
  93.             local oneprocent = computer.maxEnergy() /100;
  94.             local power = math.ceil(computer.energy() / oneprocent);
  95.             local power1 = math.floor(computer.energy() / oneprocent / 10);
  96.             local indicator = "";
  97.             if power1>=9 then
  98.                 indicator="█"
  99.             elseif power1>=7 then
  100.                 indicator="▆"
  101.             elseif power1>=5 then
  102.                 indicator="▅"
  103.             elseif power1>=3 then
  104.                 indicator="▃"
  105.             elseif power1>=1 then
  106.                 indicator="▂"
  107.             elseif power1>=0 then
  108.                 indicator="▁"
  109.             end
  110.             gpu.fill(2,2,78,9," ")
  111.             gpu.set(2, 2, "Планшет: " .. indicator .. power .. "%" )
  112.             gpu.set(2, 3, "Робот:   " .. indicatorrobot .. powerrobot .. "%" )
  113.             gpu.set(69, 10, "Кнопка:" .. eventData[4])
  114.             if commands[eventData[4]] then
  115.                 gpu.set(79, 10, commands[eventData[4]].screenText .. "" )
  116.                 modem.broadcast(port, keyWord, commands[eventData[4]].messageToRobot)
  117.                 if commands[eventData[4]].messageToRobot == "exit" then
  118.                     return
  119.                 end
  120.             end
  121.         end
  122.         if eventData[1] == "modem_message" and eventData[4] == port and eventData[6] == keyWord then
  123.             powerrobot = math.ceil(eventData[7])
  124.             power1robot = math.floor(eventData[7] / 10);
  125.             indicatorrobot = "";
  126.             if power1robot>=9 then
  127.                 indicatorrobot="█"
  128.             elseif power1robot>=7 then
  129.                 indicatorrobot="▆"
  130.             elseif power1robot>=5 then
  131.                 indicatorrobot="▅"
  132.             elseif power1robot>=3 then
  133.                 indicatorrobot="▃"
  134.             elseif power1robot>=1 then
  135.                 indicatorrobot="▂"
  136.             elseif power1robot>=0 then
  137.                 indicatorrobot="▁"
  138.             end
  139.         end
  140.     end
  141. end
  142.  
  143. local function main()
  144.     send()
  145. end
  146.  
  147. -------------------------------------------------------------------------------------
  148.  
  149. main()
  150.  
  151. -------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment