Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local P = require "SimplifyPathfinding"
- local pathfinder = P.New("Test")
- require "SimplifyPathfinding.Util.Turtle" (pathfinder, true)
- local TRANSMISSION_CHANNEL = 4589
- local modem = peripheral.find("modem")
- modem.open(TRANSMISSION_CHANNEL)
- local ok, err = turtle.locate()
- if not ok then
- printError(err)
- error("Failed to locate turtle position.", 0)
- end
- local function sendReply(data)
- modem.transmit(
- TRANSMISSION_CHANNEL,
- TRANSMISSION_CHANNEL,
- data
- )
- end
- local actions = {
- GOTO = function(msg)
- if type(msg.Position) == "table" and msg.Position[1] and msg.Position[2] and msg.Position[3] then
- sendReply{Ok = true}
- local ok, err = pcall(
- turtle.simpleGoTo,
- msg.Position[1],
- msg.Position[2],
- msg.Position[3],
- false,
- false,
- true
- )
- sendReply{Ok = ok, Action = "ARRIVED", Error = "Goto failed: " .. tostring(err)}
- else
- if type(msg) ~= "table" then
- sendReply{Ok = false, Error = "Expected table message."}
- elseif not msg.Position.X then
- sendReply{Ok = false, Error = "Message missing X position."}
- elseif not msg.Position.Y then
- sendReply{Ok = false, Error = "Message missing Y position."}
- elseif not msg.Position.Z then
- sendReply{Ok = false, Error = "Message missing Z position."}
- else
- sendReply{Ok = false, Error = "Malformed message."}
- end
- end
- end,
- FOLLOW_PATH = function(msg)
- if type(msg.Path) == "table" then
- sendReply{Ok = true}
- local ok, err = pcall(
- turtle.followPath,
- msg.Path,
- false,
- false,
- true
- )
- sendReply{Ok = ok, Error = err, Action = "ARRIVED"}
- else
- sendReply{Ok = false, Error = "Malformed message."}
- end
- end
- }
- while true do
- local _, _, _, _, msg = os.pullEvent("modem_message")
- if type(msg) == "table" then
- if actions[msg.Action] then
- print("Action:", msg.Action)
- actions[msg.Action](msg)
- else
- printError("Action", msg.Action, "does not exist!")
- end
- else
- printError("Received non-table message:")
- printError(msg)
- end
- end
Add Comment
Please, Sign In to add comment