pulchroxloom

Turtle Client

Jan 26th, 2025 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.28 KB | None | 0 0
  1. LOGS_RECV = 1                        -- general purpose logger
  2. CLIENT_RECV = 10                     -- all clients receive messages on this channel
  3. MY_RECV = math.random(300, 900) * 10 -- this client receives messages on this channel
  4. EXIT_COMMAND = "exit" -- shared exit command where clients do not respond
  5.  
  6. local modem = nil
  7. local peripherals = peripheral.getNames()
  8. for name = 1, #peripherals, 1 do
  9.     if (peripheral.getType(peripherals[name]) == "modem") then
  10.         modem = peripheral.wrap(peripherals[name])
  11.         break
  12.     end
  13. end
  14.  
  15. if (modem == nil) then
  16.     error("Error, this program requires a Modem!")
  17. end
  18.  
  19. modem.open(CLIENT_RECV)
  20. modem.open(MY_RECV)
  21.  
  22. modem.transmit(LOGS_RECV, MY_RECV, "client started")
  23.  
  24. local function refuel()
  25.     if turtle.getFuelLevel() > 0 then
  26.         return
  27.     end
  28.  
  29.     for i = 1, 16 do -- loop through the slots
  30.         turtle.select(i) -- change to the slot
  31.         if turtle.refuel(0) then -- if it's valid fuel
  32.             turtle.refuel(1)     -- refuel using one
  33.             return
  34.         end
  35.       end
  36. end
  37.  
  38. repeat
  39.     local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  40.  
  41.     local log = nil
  42.     if message == "forward" then
  43.         refuel()
  44.         log = turtle.forward() and "moved forward" or "could not move forward"
  45.     elseif message == "back" then
  46.         refuel()
  47.         log = turtle.back() and "moved back" or "could not move back"
  48.     elseif message == "up" then
  49.         refuel()
  50.         log = turtle.up() and "moved up" or "could not move up"
  51.     elseif message == "down" then
  52.         refuel()
  53.         log = turtle.down() and "moved down" or "could not move down"
  54.     elseif message == "left" then
  55.         refuel()
  56.         log = turtle.turnLeft() and "turned left" or "could not turn left"
  57.     elseif message == "right" then
  58.         refuel()
  59.         log = turtle.turnRight() and "turned right" or "could not turn right"
  60.     elseif message == "dig" then
  61.         log = turtle.dig() and "dug" or "could not dig"
  62.     else
  63.         log = "unknown command - " .. message
  64.     end
  65.  
  66.     modem.transmit(LOGS_RECV, MY_RECV, log)
  67.  
  68. until message == EXIT_COMMAND
  69.  
  70. modem.transmit(LOGS_RECV, MY_RECV, "client ended")
  71.  
  72. modem.close(MY_RECV)
  73. modem.close(CLIENT_RECV)
  74.  
Add Comment
Please, Sign In to add comment