alesandreo

geoScan_scanBlocks.lua

Mar 30th, 2022 (edited)
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.93 KB | None | 0 0
  1. -- 3NhnY0Km
  2.  
  3. -- Example Usage: scan netherite
  4. -- will scan for netherite within the maximum freeRadius
  5.  
  6. -- Stores arguments passed from the command line into a table.
  7. args = {...}
  8. scanner = peripheral.wrap("back")
  9. -- the first argument is [1]
  10. target_block = args[1]
  11. if not scanner then
  12.     error("Requires Scanner")
  13. end
  14.  
  15. -- function compare_distance(a, b)
  16. --     return a.distance < b.distance
  17. -- end
  18.  
  19. -- function that searches all returned blocks for the substring passed.
  20. function search(tbl, k2)
  21.     if tbl then
  22.         local results = {}
  23.         for k, v in ipairs(tbl) do
  24.             if string.find(v.name, k2) then
  25.                 -- Makes the terminal turn green if you find anything.
  26.                 term.setBackgroundColor(colors.green)
  27.                 v.distance = math.floor(math.sqrt(math.pow(math.abs(v.x), 2) + math.pow(math.abs(v.y), 2) + math.pow(math.abs(v.z), 2)))
  28.                 table.insert(results, v)
  29.             end
  30.         end
  31.         if #results == 0 then
  32.             term.setBackgroundColor(colors.red)
  33.         else
  34.             -- Eventually, I'll make the tables sort by distance
  35.             for k, v in ipairs(results) do
  36.                 print(v.distance.." "..v.name.."\n".."("..v.x..","..v.y..","..v.z..")")
  37.             end
  38.         end
  39.     end
  40. end
  41. count = 0
  42. term.setBackgroundColor(colors.red)
  43. while true do
  44.     term.setCursorPos(1,1)
  45.     term.clear()
  46.     count = count + 1
  47.     print("Scan #"..count)
  48.     -- scanner.scan(radius) returns all blocks in the given radius, and their relative coordinates.
  49.     -- The geoscanner has a maximum range for free scans which can be read from scanner.getConfiguration()
  50.     search(scanner.scan(scanner.getConfiguration().scanBlocks.maxFreeRadius), target_block)
  51.     -- scanBlocks by default has a cooldown, this has the program wait until it has cooled for usage.
  52.     while scanner.getOperationCooldown("scanBlocks") > 0 do
  53.         os.sleep(0.1)
  54.     end
  55. end
Add Comment
Please, Sign In to add comment