Guest User

Goto

a guest
Sep 1st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.51 KB | None | 0 0
  1. -- Goto
  2. -- by SukkaiPoppuGo
  3.  
  4. assert(pocket, "Require pocket computer")
  5.  
  6. local target = vector.new(-100, 50, -25)
  7. local playerData = pocket.getEntity()
  8. local player = vector.new( playerData.x, playerData.y, playerData.z )
  9.  
  10. --https://math.stackexchange.com/questions/42640/calculate-distance-in-3d-space
  11. function distance(A, B)
  12.     return math.sqrt(math.pow(B.x - A.x, 2) + math.pow(B.y - A.y, 2) + math.pow(B.z - A.z, 2))
  13. end
  14.  
  15. local function isColinear( A, B )
  16.    return A.x * B.z == B.x * A.z
  17. end
  18.  
  19. local function convertXZToScreen(x, z, yaw)
  20.     local angle = math.rad(-yaw % 360)
  21.     local px = math.cos(angle) * -x - math.sin(angle) * -z
  22.     local py = math.sin(angle) * -x + math.cos(angle) * -z
  23.     return px, py
  24. end
  25.  
  26. local function abs2rel(A, B)
  27.     return B - A
  28. end
  29.  
  30. --local Rel = abs2rel(player, target)
  31. --local px, py = convertXZToScreen(Rel.x, Rel.z, player.yaw) --si player.yaw existe bien sous ce nom
  32.  
  33. ------------------------
  34. -- Numbers
  35. -- by SukaiPoppuGo
  36.  
  37. -- Pixel API Wizard
  38. os.loadAPI("/api/pixel.lua")
  39. if not pixel then
  40.     local confirm
  41.     repeat
  42.         print("Pixel API required")
  43.         print("pastebin get Fa9KJqS4 /api/pixel.lua")
  44.         print("Download ? Y/N")
  45.         confirm = string.lower(read())
  46.         if confirm ~= "y" and confirm ~= "n" then
  47.             printError(confirm, "Invalid\n")
  48.         end
  49.     until confirm == "y" or confirm == "n"
  50.     if confirm == "y" then
  51.         shell.run("pastebin get Fa9KJqS4 /api/pixel.lua")
  52.         sleep(1)
  53.         os.loadAPI("/api/pixel.lua")
  54.         if not pixel then
  55.             printError("Setup failed..")
  56.         end
  57.     else
  58.         print("Abort")
  59.         return
  60.     end
  61. end
  62. -- End Wizard
  63.  
  64. --Pocket screen
  65. --local screenW, screenH = term.getSize()
  66. local screenW, screenH = 26, 20
  67. -- Native char pixel: 26 x 20 borderless ------------------->  88 outline borderless
  68. -- With pixel API   : 26 x 30 borderless -> 24 x 28 framed -> 108 outline borderless -> 100 outline framed
  69. -- With blittle API : 52 x 60 borderless -> 48 x 54 framed -> 220 outline borderless -> 200 outline framed
  70.  
  71. local _n = {
  72. {"000","ff0","000","000","0f0","000","000","000","000","000",},
  73. {"0f0","ff0","ff0","ff0","0f0","0ff","0ff","ff0","0f0","0f0",},
  74. {"0f0","ff0","000","000","000","000","000","ff0","000","000",},
  75. {"0f0","ff0","0ff","ff0","ff0","ff0","0f0","ff0","0f0","ff0",},
  76. {"000","ff0","000","000","ff0","000","000","ff0","000","000",},
  77. }
  78. local tNum = {}
  79. for i=0,9 do
  80.     tNum[i] = pixel.compute({_n[1][i],_n[2][i],_n[3][i],_n[4][i],_n[5][i]})
  81. end
  82.  
  83. function test()
  84.     term.clear()
  85.     local x,y = 4,6
  86.     for i=0,9 do
  87.         term.setCursorPos(x + ((i%5)*4), i > 4 and y+6 or y)
  88.         pixel.draw(tNum[i])
  89.     end
  90.     term.setCursorPos(3,18)
  91.     read()
  92. end
  93.  
  94. local maxLen = 6
  95. function display(num, x, y)
  96.     assert(tonumber(num), string.format([[! "%s" NaN]], num))
  97.     assert(string.len(tostring(num)) <= 6, string.format([[! "%s" too long (5 digits)]], num))
  98.    
  99.     --debug
  100.     term.setCursorPos(1,y < 10 and 2 or 17)
  101.     term.clearLine()
  102.     print("num:", num, "s:", tostring(num), "n:", tonumber(num))
  103.     term.setCursorPos(1,y < 10 and 3 or 18)
  104.     term.clearLine()
  105.     term.write("split: ")
  106.     num = string.sub(tostring(num)..string.rep(" ",maxLen),1,maxLen)
  107.     local line = {"","","","",""}
  108.     for i=1,string.len(num) do
  109.         if string.sub(num,i,i) == " " then
  110.             local pad = i == string.len(num) and "" or " "
  111.             line[1] = line[1].." "..pad
  112.             line[2] = line[2].." "..pad
  113.             line[3] = line[3].." "..pad
  114.             line[4] = line[4].." "..pad
  115.             line[5] = line[5].." "..pad
  116.         elseif i == 1 and tonumber(num) < 0 then --nombre négatif
  117.             line = string.len(num) == maxLen and {" ff "," ff "," 00 "," ff "," ff "} or {"fff ","fff ","000 ","fff ","fff "}
  118.             term.write("-")
  119.         else
  120.             term.write(tonumber(string.sub(num,i,i)).." ")
  121.             local n = tonumber(string.sub(num,i,i))
  122.             local pad = i == string.len(num) and "" or " "
  123.             line[1] = line[1].._n[1][n+1]..pad
  124.             line[2] = line[2].._n[2][n+1]..pad
  125.             line[3] = line[3].._n[3][n+1]..pad
  126.             line[4] = line[4].._n[4][n+1]..pad
  127.             line[5] = line[5].._n[5][n+1]..pad
  128.         end
  129.     end
  130.     term.setCursorPos(x, y)
  131.     pixel.draw(pixel.compute(line))
  132. end
  133.  
  134. function alignLeft()
  135.     return margin
  136. end
  137. function alignCenter(v)
  138.     local w = string.len(tostring(v))*4
  139.     return math.floor((screenW - w) / 2)
  140. end
  141. function alignRight(v, margin)
  142.     local w = string.len(tostring(v))*4
  143.     return screenW - w - margin
  144. end
  145.  
  146. local maxPX,maxPY,minPX,minPY = 0,0,math.huge,math.huge
  147.  
  148. local w,h = term.getSize()
  149. local function screenConv(px,py, dist)
  150.    
  151.     local _x = math.min(w,math.max(1,math.floor(px+(dist/2)*w/dist)))
  152.     local _y = math.min(h,math.max(1,math.floor(py+(dist/2)*h/dist)))
  153.     term.setCursorPos(_x,_y)
  154.     term.blit("\7","5","f")
  155. end
  156.  
  157. target = vector.new(-4989,50,-2564)
  158. local function cursor(p)
  159.     local px,py = convertXZToScreen(target.x-p.x, target.z-p.z, p.yaw)
  160.     term.setCursorPos(1,1)
  161.     term.write(string.format("x: %s, y: %s",px,py))
  162.    
  163.     maxPX = math.max(px,maxPX)
  164.     maxPY = math.max(py,maxPY)
  165.     minPX = math.min(px,minPX)
  166.     minPY = math.min(py,minPY)
  167.     term.setCursorPos(1,20)
  168.     term.write(string.format(
  169.         "X: %s~%s Y: %s~%s",
  170.         math.floor(minPX*10)/10, math.floor(maxPX*10)/10,
  171.         math.floor(minPY*10)/10, math.floor(maxPY*10)/10
  172.     ))
  173.     screenConv(px,py, distance(target,p))
  174. end
  175.  
  176. term.clear()
  177. while true do
  178.     local p = pocket.getEntity()
  179.     term.clear()
  180.     term.setTextColor(colors.gray)
  181.     display(math.floor(p.x), alignCenter(math.floor(p.x), 4), 6)
  182.     display(math.floor(p.z), alignCenter(math.floor(p.z), 4), 12)
  183.  
  184.  term.setCursorPos(1,19)
  185.  term.write("Distance: "..distance(target,p))
  186.  
  187.  cursor(p)
  188.     sleep(.1)
  189. end
Advertisement
Add Comment
Please, Sign In to add comment