iconmaster

geoscanner.lua

Dec 22nd, 2023 (edited)
1,298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.04 KB | Gaming | 0 0
  1. local completion = require "cc.completion"
  2. local scanner = peripheral.find("geoScanner")
  3.  
  4. local args = { ... }
  5. if #args < 1 then
  6.     printError("error: needs 1 argument (radius)")
  7.     return
  8. end
  9. local radius = tonumber(args[1])
  10. if not radius then
  11.     printError("error: invalid radius")
  12.     return
  13. end
  14.  
  15. local cost, err = scanner.cost(radius)
  16. if err then
  17.     printError("error: when calculating cost: " .. err)
  18.     return
  19. end
  20. print("Cost: " .. cost .. " FE")
  21.  
  22. local function possiblyRefuel(cost)
  23.     local fuel = turtle.getFuelLevel();
  24.     if fuel ~= "unlimited" then
  25.         print("Fuel: " .. fuel .. " FE")
  26.         if cost > fuel then
  27.             print("Energy deficit: " .. (fuel - cost) .. " FE")
  28.             write("Refuel? [Yn] ")
  29.             local choice = read()
  30.             if choice == "" or choice == "y" or choice == "Y" then
  31.                 for i = 1, 16 do
  32.                     turtle.select(i)
  33.                     turtle.refuel()
  34.                 end
  35.                 print("Turtle refueled.")
  36.                 return possiblyRefuel(cost)
  37.             else
  38.                 printError("error: not enough fuel")
  39.                 return false
  40.             end
  41.         end
  42.     end
  43.     return true
  44. end
  45.  
  46. if not possiblyRefuel(cost) then return end
  47.  
  48. if scanner.getScanCooldown and scanner.getScanCooldown() > 0 then
  49.     print("Waiting for scanner cooldown (" .. scanner.getScanCooldown() .. "s)...")
  50.     sleep(scanner.getScanCooldown() + 1)
  51. end
  52.  
  53. print("Scanning...")
  54. local scanResult, err = scanner.scan(radius)
  55. if err then
  56.     printError("error: when scanning: " .. err)
  57.     return
  58. end
  59.  
  60. print("Scan complete. Analyzing...")
  61. local totals = {}
  62. local sum = 0
  63. for i, block in ipairs(scanResult) do
  64.     if totals[block.name] then
  65.         totals[block.name] = totals[block.name] + 1
  66.     else
  67.         totals[block.name] = 1
  68.     end
  69.     sum = sum + 1
  70. end
  71.  
  72. print("Scanned " .. sum .. " blocks. Results:")
  73. local TERM_ROWS = 13
  74. local row = 0
  75. for name, amount in pairs(totals) do
  76.     print("  " .. name .. ": " .. amount)
  77.     row = row + 1
  78.     if row >= TERM_ROWS - 2 then
  79.         read()
  80.         row = 0
  81.         print("Scanned " .. sum .. " blocks. Results:")
  82.     end
  83. end
  84.  
  85. local function get_keys(t)
  86.     local keys = {}
  87.     for key, _ in pairs(t) do
  88.         table.insert(keys, key)
  89.     end
  90.     return keys
  91. end
  92.  
  93. local keys = get_keys(totals)
  94. while true do
  95.     write("Get positions of block? ")
  96.     local result = read(nil, nil, function(s) return completion.choice(s, keys) end)
  97.     if result == "" then
  98.         break
  99.     end
  100.     if not totals[result] then
  101.         printError("error: unknown block")
  102.     else
  103.         local row = 0
  104.         for i, block in ipairs(scanResult) do
  105.             if block.name == result then
  106.                 print("  " .. i .. ": (" .. block.x .. ", " .. block.y .. ", " .. block.z .. ")")
  107.                 row = row + 1
  108.                 if row >= TERM_ROWS - 1 then
  109.                     read()
  110.                     row = 0
  111.                 end
  112.             end
  113.         end
  114.     end
  115. end
  116.  
Advertisement
Add Comment
Please, Sign In to add comment