9551

simple block finder

Sep 29th, 2021 (edited)
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.72 KB | None | 0 0
  1. local block = ...
  2. local scanSpeed = 0.5
  3. local doHighLight = true
  4. local findDamage =  ({...})[2]
  5. local damage = ({...})[4]
  6. local highlighter = colors.green
  7. local customDisplay = ({...})[3]
  8. if customDisplay == "nil" then customDisplay = block end
  9. if not block or block == "" then error("use: find blockname",0) end
  10. if type(damage) == "string" then damage = tonumber(damage) end
  11. local findDamage = tonumber(findDamage)
  12. if type(customDisplay) ~= "string" or customDisplay == "" then customDisplay = nil end
  13. if not damage or damage == "" then damage = nil end
  14. local per = peripheral.wrap("back")
  15. if not per then error("missing modules! (glasses,scanner)",0) end
  16. if not per.scan or not per.canvas then error("missing modules! (glasses,scanner)",0) end
  17. local c = per.canvas()
  18. local c3d = per.canvas3d()
  19. local obj
  20. c.clear()
  21. local main = function()
  22.     local rec = c.addRectangle(0,0,60,20)
  23.     local text = c.addText({3,6},"")
  24.     rec.setAlpha(122)
  25.     rec.setColor(0,0,0)
  26.     local function getClosest(tbl)
  27.         local pyth = function(x,y,z)
  28.             return math.sqrt(x^2+y^2+z^2)  
  29.         end
  30.         local result = {}
  31.         for k,v in pairs(tbl) do
  32.             result[k] = pyth(v.x,v.y,v.z)
  33.         end
  34.         local minv = math.min(table.unpack(result))
  35.         local resultFinal
  36.         local resIndex
  37.         for k,v in pairs(tbl) do
  38.             if pyth(v.x,v.y,v.z) == minv then
  39.                 resultFinal = v
  40.                 resIndex = k
  41.                 break
  42.             end
  43.         end
  44.         return resultFinal, resIndex
  45.     end
  46.     local function conv(ins)
  47.         local r,g,b = term.getPaletteColor(ins)
  48.         return r*255,g*255,b*255
  49.     end
  50.     local blc
  51.     local offset
  52.     local stabilize = function()
  53.         local x, y, z = gps.locate(0.2)
  54.         if x then
  55.             offset = {
  56.                 -((x%1)-0.5),
  57.                 -((y%1)-0.5),
  58.                 -((z%1)-0.5)
  59.             }
  60.         else
  61.             offset = nil
  62.         end
  63.     end
  64.     local scanBlocks = function()
  65.         blc = per.scan()
  66.     end
  67.     while true do
  68.         parallel.waitForAll(stabilize,scanBlocks)
  69.         c3d.clear()
  70.         local matches = {}
  71.         for k,v in pairs(blc) do
  72.             if type(findDamage) == "number" then
  73.                 if v.name == block and v.metadata == findDamage then
  74.                     table.insert(matches,v)
  75.                 end
  76.             else
  77.                 if v.name == block then
  78.                     table.insert(matches,v)
  79.                 end
  80.             end
  81.         end
  82.         if not next(matches) then matches = 'no block "'..block..'" was found' end
  83.         do
  84.             local v,xs,ys,zs,c,str,usedIndex
  85.             if type(matches) ~= "string" then
  86.                 local obj = c3d.create(offset)
  87.                 v, usedIndex = getClosest(matches)
  88.                 xs,ys,zs = tostring(v.x),tostring(v.y),tostring(v.z)
  89.                 c = xs.." "..ys.." "..zs  
  90.                 str = block.." found at: "..c
  91.                 for k,v in pairs(matches) do
  92.                     if k ~= usedIndex or not doHighLight then
  93.                         local item = obj.addItem({v.x,v.y,v.z},customDisplay or block,damage or findDamage)
  94.                         item.setDepthTested(false)
  95.                     end
  96.                 end
  97.                 local item = obj.addItem({v.x,v.y,v.z},customDisplay or block,damage or findDamage)
  98.                 if doHighLight then
  99.                     local highlight = obj.addBox(v.x-0.6,v.y-0.6,v.z-0.6,1.2,1.2,1.2)
  100.                     highlight.setDepthTested(false)
  101.                     highlight.setColor(conv(highlighter))
  102.                     highlight.setAlpha(64)
  103.                 end
  104.                 item.setDepthTested(false)
  105.             else
  106.                 str = matches
  107.             end
  108.             rec.setSize(#str*5+2,20)
  109.             text.setText(str)
  110.         end
  111.         sleep(scanSpeed or 0.5)
  112.     end
  113. end
  114. local ok,err = pcall(main)
  115. local clear = function()
  116.     if c then c.clear() end
  117.     if c3d then c3d.clear() end
  118. end
  119. clear()
  120. if not ok then
  121.     error(err,0)
  122. end
Add Comment
Please, Sign In to add comment