|
--[[ |
|
Copyright (c) 2022 object-Object |
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
]] |
|
|
|
local prettyPrefix = "$" |
|
local prefix = "^%$" -- prettyPrefix with ^ at the start and all characters escaped with % |
|
local maxNum = 1048575.9375 -- fffff.f |
|
local turtleProtocol = "teleport" |
|
local backupRequestProtocol = "request_teleport_home" |
|
local backupResponseProtocol = "request_teleport_response" |
|
local hostname = "object_Object" |
|
|
|
local waypoints = {} |
|
if fs.exists("waypoints.txt") then |
|
local f = fs.open("waypoints.txt", "r") |
|
waypoints = textutils.unserialize(f.readAll()) |
|
f.close() |
|
end |
|
|
|
local chat = peripheral.wrap("right") |
|
chat.uncapture() |
|
chat.capture(prefix) |
|
|
|
rednet.host(backupRequestProtocol, hostname) |
|
rednet.open("bottom") |
|
|
|
local function say(id, message, isError, isDone) |
|
if id == -1 then |
|
if isError then |
|
chat.say("§c"..message.."§r") |
|
else |
|
chat.say(message) |
|
end |
|
else |
|
rednet.send(id, { |
|
content = message, |
|
color = isError and colors.red or colors.white, |
|
final = isDone, |
|
}, backupResponseProtocol) |
|
end |
|
end |
|
|
|
local function sendToTurtle(label, value) |
|
rednet.send(rednet.lookup(turtleProtocol, label), value, turtleProtocol) |
|
end |
|
|
|
local function sendHex(name, decimalNum) |
|
if decimalNum > maxNum or decimalNum < -maxNum then |
|
error("Number out of range: "..decimalNum) |
|
end |
|
|
|
local sign = decimalNum >= 0 and 1 or -1 |
|
decimalNum = math.floor(math.abs(decimalNum) * 16 + 0.5) |
|
|
|
local digits = {0, 0, 0, 0, 0, 0} |
|
do |
|
local index = 1 |
|
while decimalNum ~= 0 do |
|
local digit = decimalNum % 16 |
|
digits[index] = digit |
|
decimalNum = math.floor(decimalNum / 16) |
|
index = index + 1 |
|
end |
|
end |
|
|
|
if #digits > 6 then |
|
error("Failed to parse number: "..decimalNum) |
|
end |
|
|
|
sendToTurtle(name.."_sign", sign) |
|
for index, digit in ipairs(digits) do |
|
sendToTurtle(name.."_"..index-2, digit) |
|
end |
|
end |
|
|
|
local function teleportTo(x, y, z, id) |
|
say(id, "Preparing...", false, false) |
|
|
|
redstone.setOutput("left", true) |
|
|
|
sendHex("x", x) |
|
sendHex("y", y) |
|
sendHex("z", z) |
|
sleep(2/10) |
|
|
|
redstone.setOutput("left", false) |
|
sleep(2/10) |
|
|
|
local result = parallel.waitForAny( |
|
function() os.pullEvent("redstone") end, |
|
function() sleep(30) end |
|
) |
|
|
|
if result == 1 then |
|
say(id, "Teleporting.", false, true) |
|
else |
|
say(id, "Timed out. The circle probably didn't run.", true, true) |
|
end |
|
sleep(1) |
|
|
|
sendHex("x", 0) |
|
sendHex("y", 0) |
|
sendHex("z", 0) |
|
end |
|
|
|
local function saveWaypoints() |
|
local f = fs.open("waypoints.txt", "w") |
|
f.write(textutils.serialize(waypoints)) |
|
f.close() |
|
end |
|
|
|
local function strPos(pos) |
|
return "("..pos.x..", "..pos.y..", "..pos.z..")" |
|
end |
|
|
|
local function strXYZ(x, y, z) |
|
return strPos{x=x, y=y, z=z} |
|
end |
|
|
|
local coordPattern = "([%-%d%.]+)%s+([%-%d%.]+)%s+([%-%d%.]+)" |
|
|
|
local commands = { |
|
tp = function(args, id) |
|
if args == "" then |
|
error("Missing coordinates") |
|
end |
|
|
|
local pos = waypoints[args] |
|
if pos then |
|
teleportTo(pos.x, pos.y, pos.z, id) |
|
return |
|
end |
|
|
|
local x, y, z = args:match("^"..coordPattern) |
|
x, y, z = tonumber(x), tonumber(y), tonumber(z) |
|
|
|
if not (x and y and z) then |
|
error("Invalid coordinates: "..args) |
|
end |
|
|
|
teleportTo(x, y, z, id) |
|
end, |
|
waypoint = function(args, id) |
|
local action, rest = args:match("^(%S+)%s*(.*)") |
|
rest = rest or "" |
|
if action == "list" then |
|
if not next(waypoints) then |
|
error("No waypoints have been created.") |
|
end |
|
|
|
say(id, "Waypoints:", false, false) |
|
local toSend = {} |
|
for waypoint, pos in pairs(waypoints) do |
|
table.insert(toSend, " "..waypoint..": "..strPos(pos)) |
|
end |
|
for i, message in ipairs(toSend) do |
|
say(id, message, false, i == #toSend) |
|
end |
|
elseif action == "add" or action == "set" then |
|
local waypoint, x, y, z = rest:match("^(.+)%s+"..coordPattern) |
|
x, y, z = tonumber(x), tonumber(y), tonumber(z) |
|
|
|
if not waypoint then |
|
error("Invalid args for "..action..": "..rest) |
|
elseif not (x and y and z) then |
|
error("Invalid coordinates in args for "..action..": "..rest) |
|
end |
|
|
|
local pos = waypoints[waypoint] |
|
if pos and action == "add" then |
|
error("Waypoint "..waypoint.." already exists at "..strPos(pos)..".") |
|
elseif not pos and action == "set" then |
|
error("Invalid waypoint: "..waypoint) |
|
end |
|
|
|
waypoints[waypoint] = {x=x, y=y, z=z} |
|
saveWaypoints() |
|
if action == "add" then |
|
say(id, "Added waypoint "..waypoint.." at "..strXYZ(x, y, z)..".", false, true) |
|
elseif action == "set" then |
|
say(id, "Set waypoint "..waypoint.." to "..strXYZ(x, y, z)..", was "..strPos(pos)..".", false, true) |
|
end |
|
elseif action == "remove" or action == "delete" then |
|
local pos = waypoints[rest] |
|
if not pos then |
|
error("Invalid waypoint: "..rest) |
|
end |
|
|
|
waypoints[rest] = nil |
|
saveWaypoints() |
|
say(id, "Removed waypoint "..rest..", was "..strPos(pos)..".", false, true) |
|
else |
|
error("Invalid args: "..args) |
|
end |
|
end, |
|
help = function(args, id) |
|
say(id, "Commands:", false, false) |
|
say(id, " $help", false, false) |
|
say(id, " $tp <x> <y> <z>", false, false) |
|
say(id, " $tp waypoint <waypoint name>", false, false) |
|
say(id, " $waypoint list", false, false) |
|
say(id, " $waypoint add <waypoint name> <x> <y> <z>", false, false) |
|
say(id, " $waypoint set <waypoint name> <x> <y> <z>", false, false) |
|
say(id, " $waypoint (delete | remove) <waypoint name>", false, true) |
|
end, |
|
} |
|
|
|
print("Waiting for commands with prefix "..prettyPrefix) |
|
while true do |
|
local _, id, message |
|
local result = parallel.waitForAny( |
|
function() |
|
id = -1 |
|
_, message = os.pullEvent("chat_capture") |
|
end, |
|
function() |
|
id = rednet.receive(backupRequestProtocol) |
|
end |
|
) |
|
|
|
local command, args |
|
if result == 1 then |
|
command, args = message:match(prefix.."(%S+)%s+(.+)") |
|
command = command or message:match(prefix.."(.+)") |
|
args = args or "" |
|
else |
|
command, args = "tp", "home" |
|
end |
|
|
|
local func = commands[command] |
|
if func then |
|
local success, err = pcall(func, args, id) |
|
if not success then |
|
say(id, err, true, true) |
|
end |
|
else |
|
say(id, "Unknown command: "..command, true, true) |
|
end |
|
end |