Advertisement
Andronio12

Remote Drone Control Interface (WIP) [OpenComputers]

Apr 12th, 2017
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.05 KB | None | 0 0
  1. local tr_port = 53646 -- Порт передатчика(это устройство)
  2. local rc_port = 22866 -- Порт приемника(дрон)
  3. -- НЕ ТРОГАТЬ!!!
  4. local isize = 1
  5. local s_slot = 1
  6. local mod_st = 8
  7. local acceleration = 2
  8. local isavail = false
  9. local step = 2
  10. local mod_st_memory = 8
  11.  
  12. local comp = require('computer')
  13. local cmp = require('component')
  14. local gpu = cmp.gpu
  15. local event = require('event')
  16. local kb = require('keyboard')
  17.  
  18. if not cmp.isAvailable('modem') then
  19.   io.stderr:write('Компонент \'Плата беспроводной сети\' не обнаружен!')
  20.   os.exit()
  21. end
  22.  
  23. local m = cmp.modem
  24.  
  25. if not m.open(tr_port) and not m.isOpen(tr_port) then
  26.   io.stderr:write('Не удалось открыть порт '..tr_port)
  27.   os.exit()
  28. end
  29. m.setStrength(mod_st)
  30.  
  31. function send(...)
  32.   m.broadcast(rc_port,...)
  33. end
  34.  
  35. local wason = false
  36. function cls_ob(_,_,_,_,S,msg,eng,accel,invsize,slot_sel,st_in_slot,m_st)
  37.   gpu.setForeground(0xFFFFFF)
  38.   if msg == 'Info' then
  39.     if eng > 66 then
  40.       gpu.setForeground(0x19FF19)
  41.     elseif eng < 67 and eng > 20 then
  42.       gpu.setForeground(0xFFFF00)
  43.     elseif eng < 21 then
  44.       gpu.setForeground(0xFF0000)
  45.     end
  46.     gpu.set(28,4,eng..'%              ')
  47.     gpu.setForeground(0xFFFFFF)
  48.     gpu.set(19,6,accel..' м/с              ')
  49.     gpu.set(20,8,invsize..' слотов              ')
  50.     isize = invsize
  51.     gpu.set(18,9,slot_sel..'                  ')
  52.     s_slot = slot_sel
  53.     gpu.set(28,10,st_in_slot..' предметов              ')
  54.     gpu.set(23,7,math.floor(S + 0.5)..' м                          ')
  55.     local was = gpu.getForeground()
  56.     gpu.setForeground(0x19FF19)
  57.     gpu.set(18,3,'Включён                ')
  58.     mod_st = m_st
  59.     gpu.setForeground(was)
  60.     wason = true
  61.   end
  62.   isavail = true
  63. end
  64. function key(ev,_,_,code,_)
  65.   local knum = tonumber(code)
  66.   if knum == 211 then --211 Delete
  67.     comp.beep(1000,0.35)
  68.     os.sleep(0.15)
  69.     comp.shutdown(true)
  70.   end
  71.   local ky = kb.keys[code]
  72.   if ky == 'w' then
  73.     send('move',0,0,step)
  74.   end
  75.   if ky == 's' then
  76.     send('move',0,0,-step)
  77.   end
  78.   if ky == 'a' then
  79.     send('move',step,0,0)
  80.   end
  81.   if ky == 'd' then
  82.     send('move',-step,0,0)
  83.   end
  84.   if ky == 'space' then
  85.     send('move',0,step,0)
  86.   end
  87.   if ky == 'lshift' then
  88.     send('move',0,-step,0)
  89.   end
  90.   if ky == 'home' then
  91.     gpu.setResolution(1,1)
  92.     resized = true
  93.   end
  94.   if ky == 'end' then
  95.     draw_int()
  96.   end
  97. end
  98. function mouse(_,_,x,y,tc_tp,_)
  99.   if isavail then
  100.     if x == 44 then
  101.       if y == 6 then
  102.         if acceleration*10 < 20 then
  103.           acceleration = acceleration + 0.5
  104.         end
  105.         send('Accel',acceleration)
  106.       elseif y == 11 then
  107.         if mod_st < 1000 then
  108.           mod_st = mod_st + 8
  109.         end
  110.         send('Streng',mod_st)
  111.       elseif y == 9 then
  112.         if isize > 0 then
  113.           if s_slot < isize then
  114.             s_slot = s_slot + 1
  115.           end
  116.           send('Slot',s_slot)
  117.         end
  118.       end
  119.     elseif x == 46 then
  120.       if y == 6 then
  121.         if acceleration*10 > 5 then
  122.           acceleration = acceleration - 0.5
  123.         end
  124.         send('Accel',acceleration)
  125.       elseif y == 11 then
  126.         if mod_st > 8 then
  127.           mod_st = mod_st - 8
  128.         end
  129.         send('Streng',mod_st)
  130.       elseif y == 9 then
  131.         if isize > 0 then
  132.           if s_slot > 1 then
  133.             s_slot = s_slot - 1
  134.           end
  135.           send('Slot',s_slot)
  136.         end
  137.       end
  138.     end
  139.   end
  140.   step = acceleration/1
  141.   if (x > 1 and x < 19) and (y > 12 and y < 16) then
  142.     send('Power Off!')
  143.     wason = false
  144.     event.ignore('modem_message')
  145.     os.sleep(0.25)
  146.     event.listen('modem_message',cls_ob)
  147.   end
  148.   if (x > 1 and x < 19) and (y > 16 and y < 20) then
  149.     send('Power On!')
  150.   end
  151. end
  152.  
  153. function chkfor_avail()
  154.   if isavail then
  155.     gpu.setBackground(0x00E600)
  156.     gpu.set(44,6,'+')
  157.     gpu.set(44,11,'+')
  158.     gpu.set(44,9,'+')
  159.     gpu.setBackground(0xFF2400)
  160.     gpu.set(46,6,'-')
  161.     gpu.set(46,11,'-')
  162.     gpu.set(46,9,'-')
  163.     gpu.setBackground(0x333333)
  164.   else
  165.     gpu.setBackground(0x333333)
  166.     gpu.set(44,6,'+')
  167.     gpu.set(44,11,'+')
  168.     gpu.set(44,9,'+')
  169.     gpu.set(46,6,'-')
  170.     gpu.set(46,11,'-')
  171.     gpu.set(46,9,'-')
  172.   end
  173. end
  174.  
  175. event.listen('modem_message',cls_ob)
  176. event.listen('key_down',key)
  177. event.listen('touch',mouse)
  178. local cfa = event.timer(0.1,chkfor_avail,math.huge)
  179.  
  180. function bg_fill(color,x,y,x1,y1,symbol)
  181.   local fg_was = gpu.getForeground()
  182.   local bg_was = gpu.getBackground()
  183.   gpu.setForeground(color)
  184.   gpu.setBackground(color)
  185.   gpu.fill(x,y,x1,y1,symbol)
  186.   gpu.setForeground(fg_was)
  187.   gpu.setBackground(bg_was)
  188. end
  189.  
  190. function draw_int()
  191. gpu.setResolution(80,25)
  192. bg_fill(0x333333,1,1,80,25,' ')
  193. bg_fill(0x4D4D4D,1,1,80,1,' ')
  194. gpu.setBackground(0x333333)
  195. gpu.set(2,3,'Приемник(дрон): Недоступно')
  196. gpu.set(2,4,'Уровень заряда приемника: Недоступно')
  197. gpu.set(2,5,'Уровень заряда передатчика: Вычисление...')
  198. gpu.set(2,6,'Ускорение дрона: Недоступно')
  199. gpu.set(2,7,'Расстояние до дрона: Недоступно')
  200. gpu.set(2,8,'Размер инвентаря: Недоступно')
  201. gpu.set(2,9,'Выбранный слот: Недоступно')
  202. gpu.set(2,10,'Кол-во предметов в слоте: Недоступно')
  203. gpu.set(2,11,'Дальность передачи: '..mod_st..' м')
  204. gpu.setBackground(0x00E600)
  205. gpu.set(44,6,'+')
  206. gpu.set(44,11,'+')
  207. gpu.set(44,9,'+')
  208. gpu.setBackground(0xFF2400)
  209. gpu.set(46,6,'-')
  210. gpu.set(46,11,'-')
  211. gpu.set(46,9,'-')
  212. gpu.setBackground(0x333333)
  213. bg_fill(0xFF0000,2,13,17,3,' ')
  214. bg_fill(0x00FF00,2,17,17,3,' ')
  215. gpu.setBackground(0xFF0000)
  216. gpu.set(3,14,'Выключить дрона')
  217. gpu.setBackground(0x00FF00)
  218. gpu.set(3,18,'Разбудить дрона')
  219. gpu.setBackground(0x333333)
  220. end
  221.  
  222. draw_int()
  223. while true do
  224.   a = {comp.pullSignal(2)}
  225.   if a[1] ~= 'modem_message' and a[1] ~= 'touch' and a[1] ~= 'key_down' and a[1] ~= 'key_up' then
  226.     cls_ob()
  227.     gpu.setForeground(0xFF0000)
  228.     gpu.set(28,4,'Недоступно        ')
  229.     gpu.set(19,6,'Недоступно        ')
  230.     gpu.set(20,8,'Недоступно        ')
  231.     gpu.set(18,9,'Недоступно        ')
  232.     gpu.set(28,10,'Недоступно        ')
  233.     gpu.set(23,7,'Недоступно               ')
  234.     if wason then
  235.       gpu.set(18,3,'Сигнал потерян!         ')
  236.     else
  237.       gpu.set(18,3,'Выключен                ')
  238.     end
  239.     mod_st_memory = mod_st
  240.     gpu.setForeground(0xFFFFFF)
  241.     isavail = false
  242.     was_lost = true
  243.   end
  244.   if isavail and was_lost then
  245.     was_lost = false
  246.     send('Streng',mod_st_memory)
  247.   end
  248.   os.sleep()
  249.   local cp_eng = math.floor(comp.energy()/comp.maxEnergy()*100 + 0.5)
  250.   if cp_eng > 66 then
  251.     gpu.setForeground(0x19FF19)
  252.   elseif cp_eng < 67 and cp_eng > 10 then
  253.     gpu.setForeground(0xFFFF00)
  254.   elseif cp_eng < 11 then
  255.     gpu.setForeground(0xFF0000)
  256.   end
  257.   gpu.set(30,5,cp_eng..'%                   ')
  258.   gpu.setForeground(0xFFFFFF)
  259.   m.setStrength(mod_st)
  260.   gpu.set(22,11,m.getStrength()..' м                  ')
  261. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement