Advertisement
Romanok2805

Untitled

Mar 25th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.57 KB | None | 0 0
  1. --Universal drone remote control(tablet) version: 1.1 by Litvinov and Romanok2805
  2. local com = require('component')
  3. local event = require('event')
  4. local keyboard = require('keyboard')
  5. local term = require('term')
  6.  
  7. local gpu = com.gpu
  8. local nav
  9. local m
  10.  
  11. print("Введите 5-ти значный код, появившийся на мониторе дрона")
  12. io.write(">>")
  13.  
  14. local receivePort = io.read() + 1
  15. local sendPort = receivePort - 1
  16. local wRes, hRes = gpu.maxResolution()
  17. local step = 1
  18. local acceleration
  19. local sideInt = 0
  20. local navigationSides = false
  21.  
  22. local colors = {
  23.   white = 0xFFFFFF,
  24.   black = 0x000000,
  25.   gray = 0x2D2D2D,
  26.   green = 0x00FF00,
  27.   yellow = 0xFFFF00
  28. }
  29.  
  30. local navSides = {
  31.   ['forward'] = {
  32.     [2] = {['z'] = '-'},
  33.     [3] = {['z'] = ''},
  34.     [4] = {['x'] = '-'},
  35.     [5] = {['x'] = ''}
  36.   },
  37.   ['back'] = {
  38.     [2] = {['z'] = ''},
  39.     [3] = {['z'] = '-'},
  40.     [4] = {['x'] = ''},
  41.     [5] = {['x'] = '-'}
  42.   },
  43.   ['right'] = {
  44.     [2] = {['x'] = ''},
  45.     [3] = {['x'] = '-'},
  46.     [4] = {['z'] = '-'},
  47.     [5] = {['z'] = ''}
  48.   },
  49.   ['left'] = {
  50.     [2] = {['x'] = '-'},
  51.     [3] = {['x'] = ''},
  52.     [4] = {['z'] = ''},
  53.     [5] = {['z'] = '-'}
  54.   }
  55. }
  56.  
  57. local answers = {
  58.   ['ping'] = function(_, _, _, _, distance)
  59.     gpu.fill(1, 12, wRes, 1, ' ')
  60.     gpu.set(1, 12, 'Расстояние к дрону: '..math.floor(distance))
  61.   end,
  62.   ['cAcceleration'] = function(_, _, _, _, _, _, cAcceleration)
  63.     gpu.setForeground(colors.green)
  64.     gpu.fill(13, 5, 4, 1, ' ')
  65.     gpu.set(13, 5, ''..cAcceleration)
  66.     gpu.setForeground(colors.white)
  67.     acceleration = cAcceleration
  68.   end,
  69.   ['cSelectSlot'] = function(_, _, _, _, _, _, cSelectSlot, selectCount)
  70.     gpu.setForeground(colors.green)
  71.     gpu.set(23, 7, ''..cSelectSlot)
  72.     gpu.fill(41, 8, 2, 1, ' ')
  73.     gpu.set(41, 8, ''..selectCount)
  74.     gpu.setForeground(colors.white)
  75.   end,
  76. }
  77.  
  78. local function droneMsg(...)
  79.   local msg = {...}
  80.   for answer in pairs(answers) do
  81.     if answer == msg[6] then
  82.       answers[msg[6]](...)
  83.       break
  84.     end
  85.   end
  86. end
  87.  
  88. local function send(...)
  89.   m.broadcast(sendPort, ...)
  90. end
  91.  
  92. local function programExit()
  93.   gpu.setResolution(wRes, hRes)
  94.   term.clear()
  95.   m.close(receivePort)
  96.   event.ignore('modem_message', droneMsg)
  97.   os.exit()
  98. end
  99.  
  100. local function infoInit()
  101.   gpu.fill(1, 2, wRes, hRes - 2, ' ')
  102.   gpu.setForeground(colors.gray)
  103.   gpu.fill(1, 3, wRes, 1, '-')
  104.   gpu.fill(1, 10, wRes, 1, '-')
  105.   gpu.setForeground(colors.white)
  106.   gpu.set(1, 4, 'Шаг - ')
  107.   gpu.set(1, 5, 'Ускорение - ')
  108.   gpu.set(1, 6, 'Сторона взаимодействия - ')
  109.   gpu.set(1, 7, 'Активный слот дрона - ')
  110.   gpu.set(1, 8, 'Количество предметов в активном слоте - ')
  111.   gpu.set(1, 9, 'Управления с учётом направления взгляда - ')
  112.   gpu.setForeground(colors.green)
  113.   gpu.set(7, 4, ''..step)
  114.   send('getAcceleration')
  115.   gpu.set(26, 6, ''..sideInt)
  116.   send('select', 1)
  117.   gpu.set(43, 9, ''..tostring(navigationSides))
  118.   gpu.setForeground(colors.white)
  119. end
  120.  
  121. local function initInterface()
  122.   gpu.setResolution(wRes, hRes)
  123.   gpu.setBackground(colors.black)
  124.   term.clear()
  125.   gpu.setBackground(colors.gray)
  126.   gpu.fill(1, 1, wRes, 1, ' ')
  127.   gpu.fill(1, hRes, wRes, 1, ' ')
  128.   gpu.setForeground(colors.yellow)
  129.   gpu.set((wRes - 30) / 2, 1, 'Universal drone remote control')
  130.   gpu.setForeground(colors.green)
  131.   gpu.set(1, hRes, 'Q')
  132.   gpu.set(24, hRes, 'M')
  133.   gpu.setForeground(colors.white)
  134.   gpu.set(3, hRes, '- закрыть программу;')
  135.   gpu.set(26, hRes, '- показать инструкцию;')
  136.   gpu.setBackground(colors.black)
  137.   infoInit()
  138. end
  139.  
  140. local function initialization()
  141.   if com.isAvailable('modem') then
  142.     m = com.modem
  143.     m.open(receivePort)
  144.     event.listen('modem_message', droneMsg)
  145.     if com.isAvailable('navigation') then
  146.       nav = com.navigation
  147.     end
  148.     initInterface()
  149.   else
  150.     io.stderr:write('Плата беспроводной сети не обнаружена!')
  151.     os.exit()
  152.   end
  153. end
  154.  
  155. local function manual()
  156.   if gpu.getResolution() == gpu.maxResolution() then
  157.     gpu.fill(1, 2, wRes, hRes - 2, ' ')
  158.     gpu.setForeground(colors.green)
  159.     gpu.set(1, 3, 'WASD')
  160.     gpu.set(1, 4, 'LShift/LCtrl')
  161.     gpu.set(1, 5, 'R/F')
  162.     gpu.set(1, 6, 'T/G')
  163.     gpu.set(1, 7, 'Y/H')
  164.     gpu.set(1, 8, 'C/X')
  165.     gpu.set(1, 9, 'U/J')
  166.     gpu.set(1, 10, 'I/K')
  167.     gpu.set(1, 11, 'O/L')
  168.     gpu.set(1, 12, '1-8')
  169.     gpu.set(1, 13, 'B')
  170.     gpu.set(1, 14, 'N')
  171.     gpu.set(1, 15, 'P')
  172.     gpu.set(1, 16, 'V')
  173.     gpu.set(1, 17, 'Z')
  174.     gpu.set(1, 18, 'E')
  175.     gpu.setForeground(colors.white)
  176.     gpu.set(6, 3, '- горизонтальное перемещение')
  177.     gpu.set(14, 4, '- вверх/вниз')
  178.     gpu.set(5, 5, '- сломать/установить блок')
  179.     gpu.set(5, 6, '- захватить/выбросить предмет')
  180.     gpu.set(5, 7, '- захватить все возможные предметы/выбросить все возможные предметы')
  181.     gpu.set(5, 8, '- попытаться захватить со всех сторон существо поводком/сбросить')
  182.     gpu.set(5, 9, '- увеличть/уменишить шаг')
  183.     gpu.set(5, 10, '- увеличть/уменишить ускорение')
  184.     gpu.set(5, 11, '- изменить сторону взаимодействия')
  185.     gpu.set(5, 12, '- сделать активным слот')
  186.     gpu.set(3, 13, '- перемещение на координаты')
  187.     gpu.set(3, 14, '- вкл/выкл управление с учётом направления взгляда')
  188.     gpu.set(3, 15, '- показать расстояние к дрону')
  189.     gpu.set(3, 16, '- установить случайный цвет дрона')
  190.     gpu.set(3, 17, '- выкл/вкл дрон')
  191.     gpu.set(3, 18, '- уменьшить/увеличить экран')
  192.     gpu.set(1, 20, 'Нажмите любую клавишу что бы скрыть инструкцию...')
  193.     event.pull('key_down')
  194.     infoInit()
  195.   end
  196. end
  197.  
  198. local function changeResolution()
  199.   if gpu.getResolution() == gpu.maxResolution() then
  200.     gpu.setResolution(2, 2)
  201.   else
  202.     initInterface()
  203.   end
  204. end
  205.  
  206. local function changeStep(plus)
  207.   if plus then
  208.     if step < 1 then
  209.       step = step + 0.1
  210.     elseif step >= 1 and step < 10 then
  211.       step = step + 1
  212.     elseif step >= 10 and step < 100 then
  213.       step = step + 10
  214.     elseif step >= 100 and step < 1000 then
  215.       step = step + 100
  216.     end
  217.   else
  218.     if step > 0.3 and step <= 1 then
  219.       step = step - 0.1
  220.     elseif step > 1 and step <= 10 then
  221.       step = step - 1
  222.     elseif step > 10 and step <= 100 then
  223.       step = step - 10
  224.     elseif step > 100 and step <= 1000 then
  225.       step = step - 100
  226.     end
  227.   end
  228.   gpu.fill(7, 4, 4, 1, ' ')
  229.   gpu.setForeground(colors.green)
  230.   gpu.set(7, 4, ''..step)
  231.   gpu.setForeground(colors.white)
  232. end
  233.  
  234. local function changeAcceleration(plus)
  235.   if plus then
  236.     if acceleration < 2 then
  237.       acceleration = acceleration + 0.5
  238.       send('setAcceleration', acceleration)
  239.     end
  240.   else
  241.     if acceleration > 0.5 then
  242.       acceleration = acceleration - 0.5
  243.       send('setAcceleration', acceleration)
  244.     end
  245.   end
  246. end
  247.  
  248. local function changeSideInt(plus)
  249.   if plus then
  250.     if sideInt < 5 then
  251.       sideInt = sideInt + 1
  252.     end
  253.   else
  254.     if sideInt > 0 then
  255.       sideInt = sideInt - 1
  256.     end
  257.   end
  258.   gpu.setForeground(colors.green)
  259.   gpu.set(26, 6, ''..sideInt)
  260.   gpu.setForeground(colors.white)
  261. end
  262.  
  263. local function changeNavigation()
  264.   if navigationSides then
  265.     navigationSides = false
  266.   else
  267.     if nav then
  268.       navigationSides = true
  269.     else
  270.       term.setCursor(1, 12)
  271.       io.stderr:write('Улучшение "Навигация" отсуствует')
  272.     end
  273.   end
  274.   gpu.fill(43, 9, 5, 1, ' ')
  275.   gpu.setForeground(colors.green)
  276.   gpu.set(43, 9, ''..tostring(navigationSides))
  277.   gpu.setForeground(colors.white)
  278. end
  279.  
  280. local function goTo()
  281.   term.setCursor(1, 12)
  282.   io.write('Введите координаты дрона:\n')
  283.   io.write('Позиция X = ')
  284.   local posX = tonumber(io.read()) or 0
  285.   io.write('Позиция Z = ')
  286.   local posZ = tonumber(io.read()) or 0
  287.   io.write('Введите координаты точки назначения:\n')
  288.   io.write('Позиция X = ')
  289.   local goX = tonumber(io.read()) or 0
  290.   io.write('Позиция Z = ')
  291.   local goZ = tonumber(io.read()) or 0
  292.   io.write('Введите высоту полёта на которой нет препятсвий для робота:\n')
  293.   io.write('Высота Y = ')
  294.   local goY = tonumber(io.read()) or 0
  295.   io.write('\nПеремещение на высоту: Y = '..goY..';')
  296.   send('move', 0, goY, 0)
  297.   os.sleep(goY / 10)
  298.   io.write('\nПеремещение на координаты: X = '..goX..'; Z = '..goZ..';')
  299.   send('move', goX - posX, 0, goZ - posZ)
  300.   os.sleep(5)
  301.   gpu.fill(1, 12, wRes, 11, ' ')
  302. end
  303.  
  304. local function droneMove(side)
  305.   local playerDir = nav.getFacing()
  306.   local x = tonumber(navSides[side][playerDir]['x'] and navSides[side][playerDir]['x']..step or 0)
  307.   local z = tonumber(navSides[side][playerDir]['z'] and navSides[side][playerDir]['z']..step or 0)
  308.   send('move', x, 0, z)
  309. end
  310.  
  311. local commands = {
  312.   ['w'] = function() if navigationSides then droneMove('forward') else send('move', step, 0, 0) end end,
  313.   ['s'] = function() if navigationSides then droneMove('back') else send('move', -step, 0, 0) end end,
  314.   ['a'] = function() if navigationSides then droneMove('left') else send('move', 0, 0, -step) end end,
  315.   ['d'] = function() if navigationSides then droneMove('right') else send('move', 0, 0, step) end end,
  316.   ['lshift'] = function() send('move', 0, step, 0) end,
  317.   ['lcontrol'] = function() send('move', 0, -step, 0) end,
  318.   ['r'] = function() send('swing', sideInt) end,
  319.   ['f'] = function() send('place', sideInt) end,
  320.   ['t'] = function() send('suck', sideInt) end,
  321.   ['g'] = function() send('drop', sideInt) end,
  322.   ['y'] = function() send('suckAll', sideInt) end,
  323.   ['h'] = function() send('dropAll', sideInt) end,
  324.   ['c'] = function() send('leash') end,
  325.   ['x'] = function() send('unleash') end,
  326.   ['u'] = function() changeStep(true) end,
  327.   ['j'] = function() changeStep() end,
  328.   ['o'] = function() changeSideInt(true) end,
  329.   ['l'] = function() changeSideInt() end,
  330.   ['i'] = function() changeAcceleration(true) end,
  331.   ['k'] = function() changeAcceleration() end,
  332.   ['1'] = function() send('select', 1) end,
  333.   ['2'] = function() send('select', 2) end,
  334.   ['3'] = function() send('select', 3) end,
  335.   ['4'] = function() send('select', 4) end,
  336.   ['5'] = function() send('select', 5) end,
  337.   ['6'] = function() send('select', 6) end,
  338.   ['7'] = function() send('select', 7) end,
  339.   ['8'] = function() send('select', 8) end,
  340.   ['b'] = function() goTo() end,
  341.   ['n'] = function() changeNavigation() end,
  342.   ['p'] = function() send('getPing') end,
  343.   ['v'] = function() send('changeColor', math.random(0x0, 0xFFFFFF)) end,
  344.   ['z'] = function() send('droneSwitch') end,
  345.   ['e'] = function() changeResolution() end,
  346.   ['m'] = function() manual() end,
  347.   ['q'] = function() programExit() end
  348. }
  349.  
  350. local function start()
  351.  
  352. initialization()
  353.   while true do
  354.     local event, _, _, code = event.pull('key_down')
  355.     local key = keyboard.keys[code]
  356.     for command in pairs(commands) do
  357.       if command == key then
  358.           commands[key]()
  359.         break
  360.       end
  361.     end
  362.   end
  363. end
  364.  
  365. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement