Advertisement
aharries

Startup -- Turtle

Apr 17th, 2024 (edited)
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.28 KB | None | 0 0
  1. -- Load the Rednet API
  2. rednet.open("left")
  3.  
  4. function captureOutput(func, ...)
  5.     local oldTerm = term.redirect(term.native())
  6.     local filter = function() return true end
  7.     local co = coroutine.create(func)
  8.    
  9.     local function resume(...)
  10.         local ok, event = coroutine.resume(co, ...)
  11.         if not ok then
  12.             error(event, 0)
  13.         end
  14.         return event
  15.     end
  16.  
  17.     resume(...)  -- Initial call to start the coroutine
  18.  
  19.     while coroutine.status(co) ~= 'dead' do
  20.         local eventData = {os.pullEvent()}
  21.         if eventData[1] == "char" or eventData[1] == "key" then
  22.             -- We ignore keypresses to the terminal
  23.         else
  24.             -- Send non-keypress terminal data to the computer
  25.             if eventData[1] == "print" then
  26.                 rednet.send(computer_id, table.concat(eventData, ", ", 2))
  27.             end
  28.             resume(unpack(eventData))
  29.         end
  30.     end
  31.  
  32.     term.redirect(oldTerm)
  33. end
  34.  
  35. -- Infinite loop to listen for commands
  36. while true do
  37.     local id, command = rednet.receive()
  38.     if command:find("excavate ") then
  39.         computer_id = id  -- Remember the computer's ID
  40.         local size = command:gsub("excavate ", "")
  41.         captureOutput(function() shell.run(command) end)
  42.     end
  43. end
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement