SHOW:
|
|
- or go back to the newest paste.
| 1 | - | local modules = peripheral.wrap "right" |
| 1 | + | local modem = peripheral.find "modem" |
| 2 | - | local other = peripheral.wrap "left" |
| 2 | + | local websocket = http.websocket "ws://osmarks.ml:8774" |
| 3 | ||
| 4 | - | for k, v in pairs(other) do |
| 4 | + | local geofenceChannel = 40560 |
| 5 | - | modules[k] = v |
| 5 | + | modem.open(geofenceChannel) |
| 6 | ||
| 7 | function loadPastebinAPI(paste, name) | |
| 8 | - | os.setComputerLabel("AB" .. tostring(os.getComputerID()))
|
| 8 | + | shell.run("pastebin get " .. paste .. " " .. name)
|
| 9 | os.loadAPI(name) | |
| 10 | - | function distance(e) |
| 10 | + | |
| 11 | - | return math.sqrt(e.x ^ 2 + e.y ^ 2 + e.z ^ 2) |
| 11 | + | |
| 12 | loadPastebinAPI("4nRg9CHU", "json")
| |
| 13 | ||
| 14 | - | function yawPitch(e) |
| 14 | + | local designation = tostring(os.getComputerID()) |
| 15 | - | local x, y, z = e.x, e.y, e.z |
| 15 | + | |
| 16 | - | local yaw = -math.atan2(y, math.sqrt(x * x + z * z)) |
| 16 | + | os.setComputerLabel(designation) |
| 17 | - | local pitch = -math.atan2(-x, z) |
| 17 | + | |
| 18 | - | |
| 18 | + | -- Closest waypoint currently |
| 19 | - | return math.deg(yaw), math.deg(pitch) |
| 19 | + | local closest = {"None", 1000}
|
| 20 | -- Threshold for going away | |
| 21 | local threshold = 20 | |
| 22 | ||
| 23 | - | for _ = 1, i do |
| 23 | + | -- Report to control system |
| 24 | - | f() |
| 24 | + | function report(data) |
| 25 | - | end |
| 25 | + | data["id"] = os.getComputerID() |
| 26 | local enc = json.encode(data) | |
| 27 | websocket.send(enc) | |
| 28 | end | |
| 29 | - | if i < 0 then |
| 29 | + | |
| 30 | - | repeatF(math.abs(i), f2) |
| 30 | + | report({type = "connect", ["node-type"] = "turtle"})
|
| 31 | - | else |
| 31 | + | |
| 32 | - | repeatF(i, f1) |
| 32 | + | function distance(x1, x2, y1, y2, z1, z2) |
| 33 | - | end |
| 33 | + | local dx, dy, dz = x1 - x2, y1 - y2, z1 - z2 |
| 34 | return math.sqrt(dx ^ 2 + dy ^ 2 + dz ^ 2) | |
| 35 | end | |
| 36 | ||
| 37 | - | pick(x, turtle.forward, turtle.back) |
| 37 | + | -- Repeats a turtle movement function X times. |
| 38 | - | pick(y, turtle.up, turtle.down) |
| 38 | + | -- Will turn if a wall is hit. |
| 39 | - | |
| 39 | + | |
| 40 | - | if z < 0 then |
| 40 | + | for _ = 1, i do |
| 41 | - | turtle.turnRight() |
| 41 | + | if not f() then |
| 42 | - | elseif z > 0 then |
| 42 | + | turtle.turnLeft() |
| 43 | - | turtle.turnLeft() |
| 43 | + | end |
| 44 | - | end |
| 44 | + | end |
| 45 | - | |
| 45 | + | |
| 46 | - | repeatF(math.abs(z), turtle.forward) |
| 46 | + | |
| 47 | -- If number of times is less than zero, perform a different function | |
| 48 | function pick(i, f1, f2) | |
| 49 | - | move(5, 0, 0) |
| 49 | + | if i < 0 then |
| 50 | repeatF(math.abs(i), f2) | |
| 51 | - | local power = 1 |
| 51 | + | else |
| 52 | repeatF(i, f1) | |
| 53 | - | function shootStuff() |
| 53 | + | end |
| 54 | - | while true do |
| 54 | + | |
| 55 | - | local es = modules.sense() |
| 55 | + | |
| 56 | function move(x, y, z) | |
| 57 | - | if #es == 0 then |
| 57 | + | pick(x, turtle.forward, turtle.back) |
| 58 | - | power = 0.5 |
| 58 | + | pick(y, turtle.up, turtle.down) |
| 59 | - | end |
| 59 | + | |
| 60 | - | |
| 60 | + | if z < 0 then |
| 61 | - | for _, e in pairs(es) do |
| 61 | + | turtle.turnRight() |
| 62 | - | local y, p = yawPitch(e) |
| 62 | + | elseif z > 0 then |
| 63 | - | if distance(e) < 5 and e.displayName ~= "gollark" then |
| 63 | + | turtle.turnLeft() |
| 64 | - | print(e.displayName) |
| 64 | + | end |
| 65 | - | pcall(modules.fire, y, p, power) |
| 65 | + | |
| 66 | - | power = math.min(power + 0.5, 4) |
| 66 | + | repeatF(math.abs(z), turtle.forward) |
| 67 | - | end |
| 67 | + | |
| 68 | - | end |
| 68 | + | if closest[2] < threshold then |
| 69 | - | |
| 69 | + | move(-x, -y, -z) |
| 70 | - | sleep(0.8) |
| 70 | + | end |
| 71 | - | end |
| 71 | + | |
| 72 | ||
| 73 | -- Check whether kill code enabled and if so shutdown | |
| 74 | - | function moveAround() |
| 74 | + | |
| 75 | - | while true do |
| 75 | + | while true do |
| 76 | - | move(math.random(-15, 15), math.random(-2, 2), math.random(-15, 15)) |
| 76 | + | http.request "https://osmarks.ml/killcode" |
| 77 | - | sleep(math.random(0, 20)) |
| 77 | + | _, _, h = os.pullEvent "http_success" |
| 78 | - | end |
| 78 | + | |
| 79 | if h then | |
| 80 | local t = h.readAll() | |
| 81 | if t == "KILLCODE" then | |
| 82 | - | while true do |
| 82 | + | os.shutdown() |
| 83 | - | http.request "https://osmarks.ml/killcode" |
| 83 | + | elseif t == "REBOOT" then |
| 84 | - | _, _, h = os.pullEvent "http_success" |
| 84 | + | sleep(5) |
| 85 | - | if h and h.readAll and h.readAll() == "KILLCODE" then os.shutdown() end |
| 85 | + | os.reboot() |
| 86 | - | sleep(3) |
| 86 | + | end |
| 87 | - | end |
| 87 | + | end |
| 88 | sleep(20) | |
| 89 | end | |
| 90 | - | parallel.waitForAll(moveAround, shootStuff, killCode) |
| 90 | + | |
| 91 | ||
| 92 | -- Report back position | |
| 93 | function reportPos() | |
| 94 | while true do | |
| 95 | local x, y, z = gps.locate() | |
| 96 | report({type = "position", position = {x = x, y = y, z = z}})
| |
| 97 | sleep(15) | |
| 98 | end | |
| 99 | end | |
| 100 | ||
| 101 | -- Upon a modem message being transmitted (presumably on waypoint channels), | |
| 102 | -- log its identifier + distance, and attempt to find nearest waypoint | |
| 103 | function updateWaypoints() | |
| 104 | local waypoints = {}
| |
| 105 | ||
| 106 | while true do | |
| 107 | local _, _, chan, _, msg, distance = os.pullEvent "modem_message" | |
| 108 | ||
| 109 | if chan == geofenceChannel then | |
| 110 | waypoints[msg] = distance | |
| 111 | ||
| 112 | closest = {msg, distance}
| |
| 113 | ||
| 114 | for identifier, d in pairs(waypoints) do | |
| 115 | if d < closest[2] then closest = {identifier, d} end
| |
| 116 | end | |
| 117 | end | |
| 118 | end | |
| 119 | end | |
| 120 | ||
| 121 | -- Listen to websocketed commands | |
| 122 | function runCommands() | |
| 123 | while true do | |
| 124 | local msg = websocket.receive() | |
| 125 | local data = json.decode(msg) | |
| 126 | ||
| 127 | print(textutils.serialise(data)) | |
| 128 | ||
| 129 | if data and data.command then | |
| 130 | local cmd = data.command | |
| 131 | ||
| 132 | if cmd == "reboot" then | |
| 133 | os.reboot() | |
| 134 | end | |
| 135 | end | |
| 136 | end | |
| 137 | end | |
| 138 | ||
| 139 | local run = {reportPos, killCode, updateWaypoints, runCommands}
| |
| 140 | ||
| 141 | -- If it has ended, it's not as if it's NOT going to be with an error. | |
| 142 | local _, err = pcall(parallel.waitForAll, unpack(run)) | |
| 143 | ||
| 144 | report({type = "report-error", error = err})
| |
| 145 | ||
| 146 | sleep(10) | |
| 147 | os.reboot() |