Skip to content

Instantly share code, notes, and snippets.

@h3po
Last active July 7, 2016 08:45
Show Gist options
  • Save h3po/d6f434473cf34e7b53db to your computer and use it in GitHub Desktop.
Save h3po/d6f434473cf34e7b53db to your computer and use it in GitHub Desktop.
Distance and Volume measuring tool for an OpenComputers Tablet (Minecraft)
local event = require("event")
local term = require("term")
local component = require("component")
gpu = component.gpu
local w, h = gpu.getResolution()
local prevX = nil
local prevY = nil
local prevZ = nil
local locked = false
function printStatic()
term.clear()
term.setCursor(2,h)
print("Q: Quit R: Reset L: Lock first Coordinate")
end
function printfAt(x, y, s, ...)
sf = string.format(s, ...)
sfp = sf--text.padRight(sf, w-x)
term.setCursor(x,y)
print(sfp)
end
function tablet_use(_, data)
if (prevX ~= nil) then
X = data["posX"]
Y = data["posY"]
Z = data["posZ"]
printfAt(2, 3, "Point B: X=%d Z=%d Y=%d", X, Z, Y)
diffX = X - prevX
diffY = Y - prevY
diffZ = Z - prevZ
printfAt(2, 4, "Difference: X=%d Z=%d Y=%d", diffX, diffZ, diffY)
printfAt(2, 5, "Distance X-Z: %.2f", math.sqrt(diffX^2 + diffZ^2))
dir = ""
if (diffZ < 0) then
dir = "North"
elseif (diffZ > 0) then
dir = "South"
end
if (diffZ ~= 0) then
dir = dir .. "-"
end
if (diffX < 0) then
dir = dir .. "West"
elseif (diffX > 0) then
dir = dir .. "East"
end
if (diffY ~= 0) then
dir = dir .. " + "
if (diffY < 0) then
dir = dir .. "Down"
else
dir = dir .. "Up"
end
end
printfAt(2, 6, "Direction: %s", dir)
printfAt(2, 7, "Distance 3D: %.2f", math.sqrt(diffX^2 + diffY^2 + diffZ^2))
printfAt(2, 8, "Volume: %d", (math.abs(diffX) + 1) * (math.abs(diffY) + 1) * (math.abs(diffZ) + 1))
if (locked == false) then
prevX = nil
end
elseif (locked == false) then
prevX = data["posX"]
prevY = data["posY"]
prevZ = data["posZ"]
printStatic()
printfAt(2, 2, "Point A: X=%d Z=%d Y=%d", prevX, prevZ, prevY)
end
end
function cleanExit()
event.ignore("tablet_use", tablet_use)
term.clear()
os.exit()
end
function registerEvents()
event.listen("tablet_use", tablet_use)
event.listen("interrupted", cleanExit)
end
registerEvents()
while true do
_, _, x, _, _ = event.pull("key_down")
if (x ~= nil) then
c = string.char(x)
if (c == "Q") then
cleanExit()
elseif (c == "R") then
term.clear()
printStatic()
prevX = nil
locked = false
elseif (c == "L" and prevX ~= nil) then
printfAt(2, 2, "Point A: X=%d Z=%d Y=%d (locked)", prevX, prevZ, prevY)
locked = true
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment