Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local term = require("term")
- local keyboard = require("keyboard")
- local gpu = component.gpu
- local port = 36
- local resw, resh = gpu.getResolution()
- local wresw, wresh = 2, 2
- local help = 1
- local loc = {}
- local commands = {
- ["w"] = function() send("w") end,
- ["a"] = function() send("a") end,
- ["s"] = function() send("s") end,
- ["d"] = function() send("d") end,
- ["e"] = function() send("e") end,
- ["r"] = function() send("r") end,
- ["i"] = function() exitProgramm(false) end,
- ["p"] =
- function()
- gpu.setResolution(resw, resh); term.clear(); print(
- "Введите координаты дрона:")
- io.write("X: "); loc.x = tonumber(io.read()) or "unknown"
- io.write("Z: "); loc.z = tonumber(io.read()) or "unknown"
- term.clear(); print(
- "Введите координаты точки, к которой вы собираетесь отправиться:")
- io.write("toX: "); loc.toX = tonumber(io.read()) or "unknown"
- io.write("toZ: "); loc.toZ = tonumber(io.read()) or "unknown"
- term.clear(); print(
- "На сколько блоков дрон должен подняться вверх, чтобы между указанными точками не было препятствий "..
- "(к указанному значению прибавляется 5, если вы хотите, чтобы дрон не перемещался вверх, укажите -5):")
- io.write("toY: "); loc.toY = tonumber(io.read()) or "unknown"
- for k, v in pairs(loc) do
- if v == "unknown" then
- print("Неверно указана координата: "..k); os.sleep(3)
- gpu.setResolution(wresw, wresh); term.clear()
- return
- else
- if k == "toY" then loc.toY = loc.toY + 5 end
- end
- end
- send("toY", loc.toY);
- print("Лечу на указанные координаты");
- print("Чтобы прервать полет, перезапустите дрона вручную"); os.sleep(3)
- term.clear(); gpu.setResolution(wresw, wresh)
- end,
- ["c"] = function() send("c") end,
- ["v"] = function() send("v") end,
- ["u"] = function() send("ohshit") end,
- ["z"] = function() send("z") end,
- ["h"] = function() showHelp() help = help + 1 end
- }
- function showHelp()
- if help % 2 == 0 then term.clear(); gpu.setResolution(wresw, wresh) return end
- gpu.setResolution(34, 12)
- local help = {
- "Помощь по управлению:",
- "W/S - перемещение дрона по оси Z",
- "A/D - перемещение дрона по оси X",
- "E/R - перемещение дрона по оси Y",
- "C - поймать транспорт",
- "Z - отпустить транспорт",
- "P - перемещение по координатам",
- "V - выключить дрона",
- "U - включить дрона",
- "I - выход из программы"
- }
- for s = 1, #help do
- str = help[s]
- print(str)
- end
- end
- function message(ev, adr, sender, port, dist, msg)
- if msg == "onpos" then
- gpu.setResolution(20, 2); print(
- "Прибыл к точке."); send("onpos"); os.sleep(3)
- term.clear(); gpu.setResolution(wresw, wresh);
- elseif msg == "onY" then
- send("p", loc.toY, loc.x, loc.z, loc.toX, loc.toZ)
- end
- end
- function exitProgramm(isError)
- i = function() event.ignore("modem_message", message) end
- gpu.setResolution(resw, resh); term.clear()
- if isError then i() os.exit() return end
- print("Введите Y для выхода из программы или другой любой символ для отмены."); local a = io.read()
- if a == "Y" or a == "y" then
- term.clear(); i(); os.exit()
- else
- print("Вы не подтвердили выход из программы."); os.sleep(2)
- term.clear(); gpu.setResolution(wresw, wresh)
- end
- end
- function printError(...)
- local arg = table.pack(...)
- if arg[1] == "nocomp" then
- print("Отсутствует небходимый компонент: "..arg[2].."."); os.sleep(3)
- exitProgramm(true)
- elseif arg[1] == "cantsend" then
- print("Не могу отправить команду дрону. Введите Y для выхода из программы,\nN для попытки повторной отправки команды.")
- local a = io.read()
- if a == "Y" or a == "y" then
- exitProgramm(true)
- elseif a == "N" or a == "n" then
- send(arg[2])
- else
- printError("cantsend", arg[2])
- end
- end
- end
- function getComponent(name)
- if component.isAvailable(name) then
- return component.getPrimary(name)
- else
- return nil
- end
- end
- local modem = getComponent("modem")
- if modem == nil then
- printError("nocomp", "modem")
- end
- modem.open(port)
- function send(...)
- if not modem.broadcast(port, ...) then
- printError("cantsend", command)
- end
- end
- event.listen("modem_message", message)
- function start()
- term.clear(); gpu.setResolution(wresw, wresh)
- while true do
- ev, _, _, code, _ = event.pull()
- key = tostring(keyboard.keys[code])
- if ev == "key_down" then
- if commands[key] then
- commands[key](); os.sleep(0.002)
- end
- end
- end
- end
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement