Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem = peripheral.find "modem"
- local websocket = http.websocket "ws://osmarks.ml:8774"
- local geofenceChannel = 40560
- modem.open(geofenceChannel)
- function loadPastebinAPI(paste, name)
- shell.run("pastebin get " .. paste .. " " .. name)
- os.loadAPI(name)
- end
- loadPastebinAPI("4nRg9CHU", "json")
- local designation = tostring(os.getComputerID())
- os.setComputerLabel(designation)
- -- Closest waypoint currently
- local closest = {"None", 1000}
- -- Threshold for going away
- local threshold = 20
- -- Report to control system
- function report(data)
- data["id"] = os.getComputerID()
- local enc = json.encode(data)
- websocket.send(enc)
- end
- report({type = "connect", ["node-type"] = "turtle"})
- function distance(x1, x2, y1, y2, z1, z2)
- local dx, dy, dz = x1 - x2, y1 - y2, z1 - z2
- return math.sqrt(dx ^ 2 + dy ^ 2 + dz ^ 2)
- end
- -- Repeats a turtle movement function X times.
- -- Will turn if a wall is hit.
- function repeatF(i, f)
- for _ = 1, i do
- if not f() then
- turtle.turnLeft()
- end
- end
- end
- -- If number of times is less than zero, perform a different function
- function pick(i, f1, f2)
- if i < 0 then
- repeatF(math.abs(i), f2)
- else
- repeatF(i, f1)
- end
- end
- function move(x, y, z)
- pick(x, turtle.forward, turtle.back)
- pick(y, turtle.up, turtle.down)
- if z < 0 then
- turtle.turnRight()
- elseif z > 0 then
- turtle.turnLeft()
- end
- repeatF(math.abs(z), turtle.forward)
- if closest[2] < threshold then
- move(-x, -y, -z)
- end
- end
- -- Check whether kill code enabled and if so shutdown
- function killCode()
- while true do
- http.request "https://osmarks.ml/killcode"
- _, _, h = os.pullEvent "http_success"
- if h then
- local t = h.readAll()
- if t == "KILLCODE" then
- os.shutdown()
- elseif t == "REBOOT" then
- sleep(5)
- os.reboot()
- end
- end
- sleep(20)
- end
- end
- -- Report back position
- function reportPos()
- while true do
- local x, y, z = gps.locate()
- report({type = "position", position = {x = x, y = y, z = z}})
- sleep(15)
- end
- end
- -- Upon a modem message being transmitted (presumably on waypoint channels),
- -- log its identifier + distance, and attempt to find nearest waypoint
- function updateWaypoints()
- local waypoints = {}
- while true do
- local _, _, chan, _, msg, distance = os.pullEvent "modem_message"
- if chan == geofenceChannel then
- waypoints[msg] = distance
- closest = {msg, distance}
- for identifier, d in pairs(waypoints) do
- if d < closest[2] then closest = {identifier, d} end
- end
- end
- end
- end
- -- Listen to websocketed commands
- function runCommands()
- while true do
- local msg = websocket.receive()
- local data = json.decode(msg)
- print(textutils.serialise(data))
- if data and data.command then
- local cmd = data.command
- if cmd == "reboot" then
- os.reboot()
- end
- end
- end
- end
- local run = {reportPos, killCode, updateWaypoints, runCommands}
- -- If it has ended, it's not as if it's NOT going to be with an error.
- local _, err = pcall(parallel.waitForAll, unpack(run))
- report({type = "report-error", error = err})
- sleep(10)
- os.reboot()
Add Comment
Please, Sign In to add comment