Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Goto
- -- by SukaiPoppuGo
- assert(pocket, "Require pocket computer")
- local target = {name="Origin",x=0,y=0,z=0}
- local playerData = pocket.getEntity()
- local player = vector.new( playerData.x, playerData.y, playerData.z )
- ------------------------
- --Pocket screen
- --local screenW, screenH = term.getSize()
- local screenW, screenH = 26, 20
- ------------------------
- -- API Mgr
- --
- -- Wizard
- local function wizard(api, name, code, path)
- if not api then
- local confirm
- repeat
- print(name,"API required")
- print("pastebin get", code, path)
- print("Download ? Y/N")
- confirm = string.lower(read())
- if confirm ~= "y" and confirm ~= "n" then
- printError(confirm, "Invalid\n")
- end
- until confirm == "y" or confirm == "n"
- if confirm == "y" then
- shell.run("pastebin get", code, path)
- sleep(1)
- if not os.loadAPI(path) then
- printError("Setup failed..")
- end
- else
- print("Abort")
- return
- end
- end
- end
- -- Load APIs
- os.loadAPI("/api/utils.lua")
- wizard(utils, "Utils", "ng7cqnjg", "/api/utils.lua")
- assert(utils, "Utils API required\npastebin get ng7cqnjg /api/utils.lua")
- os.loadAPI("/api/ui.lua")
- wizard(utils, "UI", "ipGrW02P", "/api/ui.lua")
- assert(utils, "Utils API required\npastebin get ipGrW02P /api/ui.lua")
- os.loadAPI("/api/pixel.lua")
- wizard(pixel, "Pixel", "Fa9KJqS4", "/api/pixel.lua")
- assert(pixel, "Pixel API required\npastebin get Fa9KJqS4 /api/pixel.lua")
- os.loadAPI("/api/numbers.lua")
- wizard(numbers, "Numbers", "e0k0ZVQy", "/api/numbers.lua")
- assert(numbers, "Numbers API required\npastebin get e0k0ZVQy /api/numbers.lua")
- ------------------------
- -- UTILS
- local function isColinear( A, B )
- return A.x * B.z == B.x * A.z
- end
- local function convertXZToScreen(x, z, yaw)
- local angle = math.rad(-yaw % 360)
- local px = math.cos(angle) * -x - math.sin(angle) * -z
- local py = math.sin(angle) * -x + math.cos(angle) * -z
- return px, py
- end
- local function abs2rel(A, B)
- local a,b = vector.new(A.x,A.y,A.z), vector.new(B.x,B.y,B.z)
- return b - a
- end
- --local Rel = abs2rel(player, target)
- --local px, py = convertXZToScreen(Rel.x, Rel.z, player.yaw) --si player.yaw existe bien sous ce nom
- ------------------------
- -- DEBUG
- local _debug = {}
- _debug.visible = false
- local function debugPXPY(px,py)
- if _debug.visible then
- term.setCursorPos(1,19)
- term.clearLine()
- term.write(string.format("x: %s, y: %s", px, py))
- end
- end
- local function debugMaxMinPXPY(px,py)
- if not _debug.pxpy then
- _debug.pxpy = {
- maxPX = 0, maxPY = 0,
- minPX = math.huge, minPY = math.huge,
- }
- end
- _debug.pxpy.maxPX = math.max(px,_debug.pxpy.maxPX)
- _debug.pxpy.maxPY = math.max(py,_debug.pxpy.maxPY)
- _debug.pxpy.minPX = math.min(px,_debug.pxpy.minPX)
- _debug.pxpy.minPY = math.min(py,_debug.pxpy.minPY)
- if _debug.visible then
- term.setCursorPos(1,20)
- term.clearLine()
- term.write(string.format(
- "X: %s~%s Y: %s~%s",
- math.floor(_debug.pxpy.minPX*10)/10, math.floor(_debug.pxpy.maxPX*10)/10,
- math.floor(_debug.pxpy.minPY*10)/10, math.floor(_debug.pxpy.maxPY*10)/10
- ))
- end
- end
- local function debugDisplay(num, x, y)
- if _debug.visible then
- term.setCursorPos(1,y < 10 and 2 or 17)
- term.clearLine()
- print("num:", num, "s:", tostring(num), "n:", tonumber(num))
- term.setCursorPos(1,y < 10 and 3 or 18)
- term.clearLine()
- term.write("split: ")
- end
- end
- ------------------------
- -- Numbers
- -- by SukaiPoppuGo
- local function minus(sp)
- sp = sp or ""
- return {
- {" "..sp, "0"..sp, "f"..sp},
- {"\140"..sp, "0"..sp, "f"..sp},
- {" "..sp, "0"..sp, "f"..sp},
- }
- end
- local maxLen = 6
- function display(num, x, y)
- assert(tonumber(num), string.format([[! "%s" NaN]], num))
- if string.len(tostring(num)) > 6 then --too long
- term.setCursorPos(2, y)
- print(num)
- return
- end
- debugDisplay(num, x, y)
- num = string.sub(tostring(num)..string.rep(" ",maxLen),1,maxLen)
- term.setCursorPos(x, y)
- local line = {"","",""}
- for cx=1,string.len(num) do
- if string.sub(num,cx,cx) == " " then
- local pad = cx == string.len(num) and "" or " "
- for cy=1, 3 do
- term.setCursorPos(x + ((cx-1)*4), y + cy -1)
- term.write(" "..pad)
- end
- elseif cx == 1 and tonumber(num) < 0 then --nombre négatif
- for cy,c in ipairs(minus(string.len(num) == maxLen and " " or "")) do
- term.setCursorPos(x + ((cx-1)*4), y + cy -1)
- term.blit(unpack(c))
- end
- else
- local pad = cx == string.len(num) and {"","",""} or {"f","f","f"}
- for cy,c in ipairs(numbers.get(tonumber(string.sub(num,cx,cx)))) do
- term.setCursorPos(x + ((cx-1)*4), y + cy -1)
- term.blit(c[1]..pad[1], c[2]..pad[2], c[3]..pad[3])
- end
- end
- end
- end
- -------------------
- -- Aligns modes (return left margin width)
- function alignLeft(v, margin)
- return margin
- end
- function alignCenter(v)
- local w = string.len(tostring(v))*4
- return math.floor((screenW - w) / 2)
- end
- function alignRight(v, margin)
- local w = string.len(tostring(v))*4
- return screenW - w - margin
- end
- -------------------
- -- Compas cursor
- local function cursor(p)
- local rel = abs2rel(target, p)
- local px, py = convertXZToScreen(rel.x, rel.z, p.yaw)
- debugPXPY(px, py)
- debugMaxMinPXPY(px, py)
- local dist = math.sqrt(rel.x^2 + rel.z^2) --plane distance (2D)
- local dist_half = dist/2
- local _x = utils.constraint(math.round((-px + dist_half) * screenW / dist), 1, screenW)
- local _y = utils.constraint(math.round((-py + dist_half) * screenH / dist), 1, screenH)
- for cy=-1,0,1 do
- term.setCursorPos(_x-1,_y + cy)
- term.blit(" ","fff","555") --term.blit("\7","5","f")
- end
- end
- -------------------
- -- Toggleable standard output
- local output = window.create(term.current(), 2,14, 22,5, false)
- output.visible = false
- function output.print(...)
- local x,y = term.getCursorPos()
- local old = term.redirect(output)
- print(...)
- term.redirect(old)
- term.setCursorPos(x,y)
- end
- -------------------
- -- About
- local Skypop = window.create(term.current(), 2,15, 22,4, false)
- local nativePink = {term.getPaletteColor( colors.pink )}
- Skypop.setPaletteColor(colors.pink, 215/255, 138/255, 127/255)
- Skypop.visible = false
- for _y,_line in ipairs({
- { "\159\143\143\143\144", "ffff6", "6666f" },
- { "\149\151 \148\149", "f00f6", "6f60f" },
- { "\149\136\143\132\149", "ff6f6", "66f6f" },
- { "\130\131\131\131\129", "66666", "fffff" },
- }) do
- Skypop.setCursorPos(1, _y)
- Skypop.blit(unpack(_line))
- end
- Skypop.setCursorPos(6, 2)
- Skypop.write(fs.getName(shell.getRunningProgram()))
- Skypop.setCursorPos(6, 3)
- Skypop.write("by SukaiPoppuGo")
- term.setPaletteColor(colors.pink, unpack(nativePink))
- -------------------
- -- Waypoint list
- local waypoints = window.create(term.current(), 2,3, 22,15, false)
- waypoints.visible = false
- waypoints.width, waypoints.height = waypoints.getSize()
- -- Reload
- settings.load(".settings")
- waypoints.data = settings.get("waypoints.data", {})
- -- List selected item
- waypoints.selection = settings.get("waypoints.selection", 0)
- if waypoints.data[waypoints.selection]
- and vector.new(
- waypoints.data[waypoints.selection].x,
- waypoints.data[waypoints.selection].y,
- waypoints.data[waypoints.selection].z
- ) then
- target = waypoints.data[waypoints.selection]
- end
- -- List position display
- waypoints.index = settings.get("waypoints.index", 1)
- function waypoints.emptyList() return {"","","","","","","","","","","",""} end
- function waypoints.list()
- ui.box({waypoints.menu, unpack(waypoints.emptyList())}, waypoints.width, "Waypoints", waypoints)
- local y=3
- for i=waypoints.index, waypoints.index + waypoints.height-4 do
- if waypoints.data[i] then
- local wp = waypoints.data[i]
- --Selected item
- if i == waypoints.selection then
- waypoints.setTextColor(colors.yellow)
- end
- local line = string.sub(wp.name..string.rep(" ",waypoints.width),0,waypoints.width-4).."[\215"
- waypoints.setCursorPos(2,y)
- waypoints.write(line)
- waypoints.setTextColor(colors.white)
- end
- y = y+1
- end
- end
- waypoints.form = window.create(waypoints, 1,1, waypoints.width, waypoints.height, false)
- -- Update list
- function waypoints.update(e)
- if waypoints.visible then
- --Params
- local height = waypoints.height-3
- local x,y = waypoints.getPosition()
- -- List scrolling
- if e[1] == "mouse_scroll" then
- output.print(" List scrolling")
- --List constraint
- waypoints.index = math.max(waypoints.index + e[2], 1)
- waypoints.index = math.min(waypoints.index, #waypoints.data)
- -- Add waypoint
- elseif e[1] == "mouse_click"
- and e[2] == 1
- and e[3] >= x and e[3] <= x + waypoints.width
- and e[4] == y+1
- then
- output.print(" Add waypoint")
- waypoints.form.clear()
- waypoints.form.setCursorPos(1,1)
- ui.box({"Name:","","X:","Y:","Z:",""},waypoints.width-2,"New waypoint", waypoints.form)
- waypoints.form.setVisible(true)
- waypoints.form.redraw()
- local wp = {}
- --input name
- waypoints.form.setCursorPos(2,3)
- wp.name = tostring(read())
- --input coords
- waypoints.form.setCursorPos(5,4)
- wp.x = tonumber(read())
- waypoints.form.setCursorPos(5,5)
- wp.y = tonumber(read())
- waypoints.form.setCursorPos(5,6)
- wp.z = tonumber(read())
- if vector.new(wp.x, wp.y, wp.z) then
- table.insert(waypoints.data, wp)
- settings.set("waypoints.data", waypoints.data)
- settings.save(".settings")
- waypoints.form.setCursorPos(2,7)
- waypoints.form.write("Saved, press any key")
- --waypoints.form.redraw()
- ui.waitPressAnyKey()
- else
- waypoints.form.setCursorPos(2,7)
- waypoints.form.write("Coords error")
- --waypoints.form.redraw()
- ui.waitPressAnyKey()
- end
- sleep(1)
- waypoints.form.clear()
- waypoints.form.setVisible(false)
- waypoints.form.redraw()
- waypoints.setVisible(true)
- waypoints.visible = true
- waypoints.redraw()
- -- Delete waypoint
- elseif e[1] == "mouse_click"
- and e[2] == 1
- and e[3] == x + waypoints.width -2
- and e[4] >= y+2 and e[4] <= y + height + 1
- and waypoints.data[e[4] - (y+2) + waypoints.index]
- then
- output.print(" Delete waypoint")
- table.remove(waypoints.data, e[4] - (y+2) + waypoints.index)
- settings.set("waypoints.data", waypoints.data)
- settings.save(".settings")
- waypoints.setCursorPos(2, e[4] - y +1)
- waypoints.write("deleted ")
- waypoints.redraw()
- sleep(1)
- -- Select waypoint
- elseif e[1] == "mouse_click"
- and e[2] == 1
- and e[3] >= x and e[3] <= x + waypoints.width-3
- and e[4] >= y+2 and e[4] <= y + height + 1
- then
- output.print(" Select waypoint")
- local selection = e[4] - (y+2) + waypoints.index
- if waypoints.data[selection] then
- waypoints.selection = selection
- settings.set("waypoints.selection", selection)
- settings.set("waypoints.index", waypoints.index)
- settings.save(".settings")
- target = waypoints.data[selection]
- end
- end
- end
- --Init display
- waypoints.setCursorPos(1,1)
- waypoints.clear()
- ui.box({
- string.format(
- "<New point> (%s/%s)",
- #waypoints.data == 0 and 0 or waypoints.index,
- #waypoints.data
- ),
- unpack(waypoints.emptyList())
- },
- waypoints.width,
- "Waypoints",
- waypoints
- )
- waypoints.list()
- waypoints.redraw()
- end
- -------------------
- -- Menu bar
- local menu = window.create(term.current(), 2,19, 22,1, true)
- local items = {
- {
- name = "\2",
- clicked = false,
- action = function()
- Skypop.visible = not Skypop.visible --toggle display state
- Skypop.setVisible(Skypop.visible)
- Skypop.redraw()
- end,
- },
- {
- name = "List",
- clicked = false,
- action = function()
- waypoints.visible = not waypoints.visible --toggle display state
- waypoints.setVisible(waypoints.visible)
- waypoints.redraw()
- end,
- },
- {
- name = "Output",
- clicked = false,
- action = function()
- output.visible = not output.visible --toggle display state
- output.setVisible(output.visible)
- output.redraw()
- end,
- },
- {
- name = "Exit",
- clicked = false,
- action = function()
- os.queueEvent("terminate")
- end,
- }
- }
- function menu.update(e)
- local x,y = term.getCursorPos()
- -- Handle mouse_click
- local _x,_y = menu.getPosition()
- local cx = 1
- if e and e[1] == "mouse_click" and e[2] == 1 and e[4] == _y then
- for i,item in ipairs(items) do
- local x1, x2 = cx, cx + string.len(item.name) +1
- items[i].clicked = (e[3] >= x1 and e[3] <= x2)
- cx = x2+1
- end
- elseif e and e[1] == "mouse_up" then
- for i,item in ipairs(items) do
- local x1, x2 = cx, cx + string.len(item.name) +1
- if items[i].clicked
- and e[3] >= x1 and e[3] <= x2
- and e[2] == 1 and e[4] == _y
- and type(items[i].action) == "function" then
- if _debug.visible then
- term.setCursorPos(2,2)
- print(items[i].name, "click")
- sleep(.3)
- end
- items[i].action()
- end
- cx = x2+1
- items[i].clicked = false
- end
- end
- menu.setCursorPos(1,1)
- menu.clearLine()
- for i,item in ipairs(items) do
- ui.btn(item.name, item.clicked, menu)
- end
- menu.redraw()
- term.setCursorPos(x,y)
- end
- -------------------
- -- Main loop
- local e = nil
- repeat
- local p = pocket.getEntity()
- term.clear()
- menu.update(e)
- if waypoints.visible then
- waypoints.update(e)
- else
- local dist = math.floor(utils.distance(target,p))
- term.setTextColor(colors.lightGray)
- term.setCursorPos(3,4)
- term.write("Target: "..target.name)
- term.setCursorPos(3,5)
- local strPos = string.format("x:%s, y:%s, z:%s", target.x, target.y, target.z)
- print(strPos)
- local x,y = term.getCursorPos()
- term.setCursorPos(3,y)
- term.write("Distance:")
- display(dist, alignCenter(dist, 4), y+1)
- cursor(p)
- end
- Skypop.redraw()
- --PLayer coordinates
- term.setTextColor(colors.lightGray)
- term.setCursorPos(2,2)
- term.write("Pos: "..math.floor(p.x)..","..math.floor(p.y)..","..math.floor(p.z)) -- distance 3D
- output.redraw()
- local tick = os.startTimer(0.1)
- e = {os.pullEventRaw()}
- os.cancelTimer(tick)
- --trace events
- if not (e[1] == "timer" and e[2] == tick) then
- output.print(unpack(e))
- end
- until e[1] == "terminate"
- term.setCursorPos(1,1)
- term.clear()
- printError("Terminated")
Add Comment
Please, Sign In to add comment