fatboychummy

turtle.lua

Oct 9th, 2021 (edited)
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.19 KB | None | 0 0
  1. local P = require "SimplifyPathfinding"
  2. local pathfinder = P.New("Test")
  3. require "SimplifyPathfinding.Util.Turtle" (pathfinder, true)
  4.  
  5. local TRANSMISSION_CHANNEL = 4589
  6. local modem = peripheral.find("modem")
  7. modem.open(TRANSMISSION_CHANNEL)
  8.  
  9. local ok, err = turtle.locate()
  10. if not ok then
  11.   printError(err)
  12.   error("Failed to locate turtle position.", 0)
  13. end
  14.  
  15. local function sendReply(data)
  16.   modem.transmit(
  17.     TRANSMISSION_CHANNEL,
  18.     TRANSMISSION_CHANNEL,
  19.     data
  20.   )
  21. end
  22.  
  23. local actions = {
  24.   GOTO = function(msg)
  25.     if type(msg.Position) == "table" and msg.Position[1] and msg.Position[2] and msg.Position[3] then
  26.       sendReply{Ok = true}
  27.       local ok, err = pcall(
  28.         turtle.simpleGoTo,
  29.         msg.Position[1],
  30.         msg.Position[2],
  31.         msg.Position[3],
  32.         false,
  33.         false,
  34.         true
  35.       )
  36.       sendReply{Ok = ok, Action = "ARRIVED", Error = "Goto failed: " .. tostring(err)}
  37.     else
  38.       if type(msg) ~= "table" then
  39.         sendReply{Ok = false, Error = "Expected table message."}
  40.       elseif not msg.Position.X then
  41.         sendReply{Ok = false, Error = "Message missing X position."}
  42.       elseif not msg.Position.Y then
  43.         sendReply{Ok = false, Error = "Message missing Y position."}
  44.       elseif not msg.Position.Z then
  45.         sendReply{Ok = false, Error = "Message missing Z position."}
  46.       else
  47.         sendReply{Ok = false, Error = "Malformed message."}
  48.       end
  49.     end
  50.   end,
  51.   FOLLOW_PATH = function(msg)
  52.     if type(msg.Path) == "table" then
  53.       sendReply{Ok = true}
  54.       local ok, err = pcall(
  55.         turtle.followPath,
  56.         msg.Path,
  57.         false,
  58.         false,
  59.         true
  60.       )
  61.       sendReply{Ok = ok, Error = err, Action = "ARRIVED"}
  62.     else
  63.       sendReply{Ok = false, Error = "Malformed message."}
  64.     end
  65.   end
  66. }
  67.  
  68. while true do
  69.   local _, _, _, _, msg = os.pullEvent("modem_message")
  70.   if type(msg) == "table" then
  71.     if actions[msg.Action] then
  72.       print("Action:", msg.Action)
  73.       actions[msg.Action](msg)
  74.     else
  75.       printError("Action", msg.Action, "does not exist!")
  76.     end
  77.   else
  78.     printError("Received non-table message:")
  79.     printError(msg)
  80.   end
  81. end
  82.  
Add Comment
Please, Sign In to add comment