Advertisement
HappySunChild

receiver

Apr 14th, 2025 (edited)
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.69 KB | None | 0 0
  1. local COMMAND_PROTOCOL = "controller.command"
  2. local REQUEST_PROTOCOL = "controller.request"
  3. local RESPONSE_PROTOCOL = "controller.response"
  4.  
  5. local ERR_PROCESS_COMMAND = "\nError running command '%s';\n%s"
  6.  
  7. local DIRECTION_FUNCTIONS = {
  8.     move = {
  9.         turtle.up,
  10.         turtle.forward,
  11.         turtle.down,
  12.         turtle.back,
  13.         turtle.turnLeft,
  14.         turtle.turnRight,
  15.     },
  16.     dig = {
  17.         turtle.digUp,
  18.         turtle.dig,
  19.         turtle.digDown,
  20.     },
  21.     place = {
  22.         turtle.placeUp,
  23.         turtle.place,
  24.         turtle.placeDown,
  25.     },
  26.     drop = {
  27.         turtle.dropUp,
  28.         turtle.drop,
  29.         turtle.dropDown,
  30.     },
  31.     suck = {
  32.         turtle.suckUp,
  33.         turtle.suck,
  34.         turtle.suckDown,
  35.     },
  36.     attack = {
  37.         turtle.attackUp,
  38.         turtle.attack,
  39.         turtle.attackDown,
  40.     },
  41. }
  42.  
  43. ---@type table<string, fun(...: rednet.transmittable): nil>
  44. local COMMANDS = {
  45.     -- run lua code
  46.     loadstring = function(code)
  47.         local func = loadstring(code)
  48.  
  49.         if func then
  50.             func()
  51.         end
  52.     end,
  53.     -- run a program
  54.     program = function(name, args)
  55.         shell.execute(name, args)
  56.     end,
  57.  
  58.     -- move the turtle
  59.     move = function(direction)
  60.         DIRECTION_FUNCTIONS.move[tonumber(direction)]()
  61.     end,
  62.     -- dig a block
  63.     dig = function(direction)
  64.         DIRECTION_FUNCTIONS.dig[tonumber(direction)]()
  65.     end,
  66.     -- place a block
  67.     place = function(direction, text)
  68.         DIRECTION_FUNCTIONS.place[tonumber(direction)](text)
  69.     end,
  70.     -- select a slot
  71.     select = function(slot)
  72.         turtle.select(tonumber(slot))
  73.     end,
  74.     -- drop an item
  75.     drop = function(direction, amount)
  76.         DIRECTION_FUNCTIONS.drop[tonumber(direction)](amount)
  77.     end,
  78.     -- suck an item
  79.     suck = function(direction, amount)
  80.         DIRECTION_FUNCTIONS.suck[tonumber(direction)](amount)
  81.     end,
  82.     attack = function(direction, side)
  83.         DIRECTION_FUNCTIONS.attack[tonumber(direction)](side)
  84.     end,
  85.  
  86.     shutdown = os.shutdown,
  87.     reboot = os.reboot,
  88. }
  89.  
  90. local function awaitController()
  91.     rednet.broadcast("connect", REQUEST_PROTOCOL)
  92.  
  93.     local id = rednet.receive(RESPONSE_PROTOCOL, 5)
  94.  
  95.     if not id then
  96.         return awaitController()
  97.     end
  98.  
  99.     return id
  100. end
  101.  
  102. ---@param command command|rednet.transmittable
  103. local function processCommand(command)
  104.     local key = command.key
  105.     local func = COMMANDS[key]
  106.  
  107.     local success, err = pcall(func, unpack(command.arguments))
  108.  
  109.     if not success then
  110.         printError(ERR_PROCESS_COMMAND:format(key, err))
  111.     end
  112. end
  113.  
  114. local function receiver()
  115.     term.clear()
  116.     term.setCursorPos(1, 1)
  117.  
  118.     peripheral.find("modem", rednet.open)
  119.  
  120.     print("Awaiting controller...")
  121.  
  122.     local activeControllerId = awaitController()
  123.  
  124.     print("Got controller.", activeControllerId)
  125.  
  126.     while true do
  127.         local id, command = rednet.receive(COMMAND_PROTOCOL)
  128.  
  129.         if id == activeControllerId then
  130.             processCommand(command)
  131.         end
  132.     end
  133. end
  134.  
  135. receiver()
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement