Advertisement
Ewgeniy

receive

Feb 10th, 2023
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.40 KB | None | 0 0
  1. local component = require("component")
  2. local robot = require("robot")
  3. local event = require("event")
  4. local fs = require("filesystem")
  5. local port = 512
  6. local keyWord = "ECSGrief"
  7. local modem
  8. local redstone = component.redstone
  9. local redstoneState = false
  10.  
  11. if component.isAvailable("modem") then
  12.     modem = component.modem
  13. else
  14.     error("Этой программе требуется беспроводной модем для работы!")
  15. end
  16.  
  17. modem.open(port)
  18.  
  19. -------------------------------------------------------------------------------------
  20.  
  21. local commands = {
  22.     forward = robot.forward,
  23.     back = robot.back,
  24.     turnRight = robot.turnRight,
  25.     turnLeft = robot.turnLeft,
  26.     up = robot.up,
  27.     down = robot.down,
  28. }
  29.  
  30. local function redstoneControl()
  31.     if not redstone then return end
  32.     if redstoneState then
  33.         for i = 0, 5 do
  34.             redstone.setOutput(i, 0)
  35.         end
  36.         print("Сигнал редстоуна включен со всех сторон робота!")
  37.         redstoneState = false
  38.     else
  39.         for i = 0, 5 do
  40.             redstone.setOutput(i, 15)
  41.         end
  42.         print("Сигнал редстоуна отключен.")
  43.         redstoneState = true
  44.     end
  45. end
  46.  
  47. local function receive()
  48.     while true do
  49.         local eventData = { event.pull() }
  50.         if eventData[1] == "modem_message" and eventData[4] == port and eventData[6] == keyWord then
  51.             local message = eventData[7]
  52.             if commands[message] then
  53.                 commands[message]()
  54.             else
  55.                 if message == "selfDestroy" then
  56.                     local fs = require("filesystem")
  57.                     for file in fs.list("") do
  58.                         print("Уничтожаю \"" .. file .. "\"")
  59.                         fs.remove(file)
  60.                     end
  61.                     require("term").clear()
  62.                     require("computer").shutdown()
  63.                 elseif message == "use" then
  64.                     robot.use()
  65.                     robot.useUp()
  66.                     robot.useDown()
  67.                 elseif message == "exit" then
  68.                     return
  69.                 elseif message == "redstone" then
  70.                     redstoneControl()
  71.                 end
  72.             end
  73.         end
  74.     end
  75. end
  76.  
  77. local function main()
  78.     print(" ")
  79.     print("Добро пожаловать в программу ECS! Идет ожидание команд с беспроводного устройства.")
  80.     print(" ")
  81.     receive()
  82.     print(" ")
  83.     print("Программа приема сообщений завершена!")
  84. end
  85.  
  86. -------------------------------------------------------------------------------------
  87.  
  88. main()
  89.  
  90. -------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement