Advertisement
Sharplook

Pig's Airline (Планшет)

Jun 13th, 2015
674
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.13 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local term = require("term")
  4. local keyboard = require("keyboard")
  5. local gpu = component.gpu
  6.  
  7. local port = 36
  8.  
  9. local resw, resh = gpu.getResolution()
  10. local wresw, wresh = 2, 2
  11.  
  12. local help = 1
  13.  
  14. local loc = {}
  15.  
  16. local commands = {
  17.     ["w"] = function() send("w") end,
  18.     ["a"] = function() send("a") end,
  19.     ["s"] = function() send("s") end,
  20.     ["d"] = function() send("d") end,
  21.     ["e"] = function() send("e") end,
  22.     ["r"] = function() send("r") end,
  23.     ["i"] = function() exitProgramm(false) end,
  24.     ["p"] =
  25.     function()
  26.         gpu.setResolution(resw, resh); term.clear(); print(
  27.         "Введите координаты дрона:")
  28.         io.write("X: "); loc.x = tonumber(io.read()) or "unknown"
  29.         io.write("Z: "); loc.z = tonumber(io.read()) or "unknown"
  30.         term.clear(); print(
  31.         "Введите координаты точки, к которой вы собираетесь отправиться:")
  32.         io.write("toX: "); loc.toX = tonumber(io.read()) or "unknown"
  33.         io.write("toZ: "); loc.toZ = tonumber(io.read()) or "unknown"
  34.         term.clear(); print(
  35.         "На сколько блоков дрон должен подняться вверх, чтобы между указанными точками не было препятствий "..
  36.         "(к указанному значению прибавляется 5, если вы хотите, чтобы дрон не перемещался вверх, укажите -5):")
  37.         io.write("toY: "); loc.toY = tonumber(io.read()) or "unknown"
  38.         for k, v in pairs(loc) do
  39.             if v == "unknown" then
  40.                 print("Неверно указана координата: "..k); os.sleep(3)
  41.                 gpu.setResolution(wresw, wresh); term.clear()
  42.                 return
  43.             else
  44.                 if k == "toY" then loc.toY = loc.toY + 5 end
  45.             end
  46.         end
  47.         send("toY", loc.toY);
  48.         print("Лечу на указанные координаты");
  49.         print("Чтобы прервать полет, перезапустите дрона вручную"); os.sleep(3)
  50.         term.clear(); gpu.setResolution(wresw, wresh)
  51.     end,
  52.     ["c"] = function() send("c") end,
  53.     ["v"] = function() send("v") end,
  54.     ["u"] = function() send("ohshit") end,
  55.     ["z"] = function() send("z") end,
  56.     ["h"] = function() showHelp() help = help + 1 end
  57. }
  58.  
  59. function showHelp()
  60.     if help % 2 == 0 then term.clear(); gpu.setResolution(wresw, wresh) return end
  61.     gpu.setResolution(34, 12)
  62.     local help = {
  63.     "Помощь по управлению:",
  64.     "W/S - перемещение дрона по оси Z",
  65.     "A/D - перемещение дрона по оси X",
  66.     "E/R - перемещение дрона по оси Y",
  67.     "C - поймать транспорт",
  68.     "Z - отпустить транспорт",
  69.     "P - перемещение по координатам",
  70.     "V - выключить дрона",
  71.     "U - включить дрона",
  72.     "I - выход из программы"
  73.     }
  74.     for s = 1, #help do
  75.         str = help[s]
  76.         print(str)
  77.     end
  78. end
  79.  
  80. function message(ev, adr, sender, port, dist, msg)
  81.     if msg == "onpos" then
  82.         gpu.setResolution(20, 2); print(
  83.         "Прибыл к точке."); send("onpos"); os.sleep(3)
  84.         term.clear(); gpu.setResolution(wresw, wresh);
  85.     elseif msg == "onY" then
  86.         send("p", loc.toY, loc.x, loc.z, loc.toX, loc.toZ)
  87.     end
  88. end
  89.  
  90. function exitProgramm(isError)
  91.     i = function() event.ignore("modem_message", message) end
  92.     gpu.setResolution(resw, resh); term.clear()
  93.     if isError then i() os.exit() return end
  94.     print("Введите Y для выхода из программы или другой любой символ для отмены."); local a = io.read()
  95.     if a == "Y" or a == "y" then
  96.         term.clear(); i(); os.exit()
  97.     else
  98.         print("Вы не подтвердили выход из программы."); os.sleep(2)
  99.         term.clear(); gpu.setResolution(wresw, wresh)
  100.     end
  101. end
  102.  
  103. function printError(...)
  104.     local arg = table.pack(...)
  105.     if arg[1] == "nocomp" then
  106.         print("Отсутствует небходимый компонент: "..arg[2].."."); os.sleep(3)
  107.         exitProgramm(true)
  108.     elseif arg[1] == "cantsend" then
  109.         print("Не могу отправить команду дрону. Введите Y для выхода из программы,\nN для попытки повторной отправки команды.")
  110.         local a = io.read()
  111.         if a == "Y" or a == "y" then
  112.             exitProgramm(true)
  113.         elseif a == "N" or a == "n" then
  114.             send(arg[2])
  115.         else
  116.             printError("cantsend", arg[2])
  117.         end
  118.     end
  119. end
  120.  
  121. function getComponent(name)
  122.     if component.isAvailable(name) then
  123.         return component.getPrimary(name)
  124.     else
  125.         return nil
  126.     end
  127. end
  128.  
  129. local modem = getComponent("modem")
  130.  
  131. if modem == nil then
  132.     printError("nocomp", "modem")
  133. end
  134.  
  135. modem.open(port)
  136.  
  137. function send(...)
  138.     if not modem.broadcast(port, ...) then
  139.         printError("cantsend", command)
  140.     end
  141. end
  142.  
  143. event.listen("modem_message", message)
  144.  
  145. function start()
  146.     term.clear(); gpu.setResolution(wresw, wresh)
  147.     while true do
  148.         ev, _, _, code, _ = event.pull()
  149.         key = tostring(keyboard.keys[code])
  150.         if ev == "key_down" then
  151.             if commands[key] then
  152.                 commands[key](); os.sleep(0.002)
  153.             end
  154.         end
  155.     end
  156. end
  157.  
  158. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement