Advertisement
melzneni

OreScanner

Oct 23rd, 2024 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 KB | None | 0 0
  1. local args = { ... }
  2. local scanner = peripheral.find("geoScanner")
  3. local radius = 16
  4. local result = scanner.scan(radius)
  5.  
  6. local function getOrCreate(tbl, key)
  7.     if tbl[key] == nil then
  8.         tbl[key] = {}
  9.     end
  10.     return tbl[key]
  11. end
  12.  
  13. local function insert(map, x, y, z, name)
  14.     getOrCreate(getOrCreate(map, x), y)[z] = { name = name, done = false }
  15. end
  16.  
  17. local function getOrNull(tbl, key)
  18.     if tbl == nil or key == nil then
  19.         return nil
  20.     end
  21.     return tbl[key]
  22. end
  23.  
  24. local function get(map, x, y, z)
  25.     return getOrNull(getOrNull(getOrNull(map, x), y), z)
  26. end
  27.  
  28. local function length(x, y, z)
  29.     return x * x + y * y + z * z
  30. end
  31.  
  32. local function updatePos(pos, x, y, z)
  33.     if length(x, y, z) < length(pos.x, pos.y, pos.z) then
  34.         pos.x = x
  35.         pos.y = y
  36.         pos.z = z
  37.     end
  38. end
  39.  
  40. local function crawl(map, x, y, z, cluster)
  41.     local entry = get(map, x, y, z)
  42.     if entry == nil or entry.done then
  43.         return
  44.     end
  45.     updatePos(cluster.pos, x, y, z)
  46.     table.insert(cluster, { x = x, y = y, z = z })
  47.     cluster.entryCount = cluster.entryCount + 1
  48.     entry.done = true
  49.     crawl(map, x - 1, y, z, cluster)
  50.     crawl(map, x + 1, y, z, cluster)
  51.     crawl(map, x, y - 1, z, cluster)
  52.     crawl(map, x, y + 1, z, cluster)
  53.     crawl(map, x, y, z - 1, cluster)
  54.     crawl(map, x, y, z + 1, cluster)
  55. end
  56.  
  57. local map = {}
  58.  
  59. for _, v in pairs(result) do
  60.     local name = v.name
  61.     if string.find(name, args[1]) then
  62.         insert(map, v.x, v.y, v.z, name)
  63.     end
  64. end
  65.  
  66. local clusters = {}
  67. for x, valuesX in pairs(map) do
  68.     for y, valuesY in pairs(valuesX) do
  69.         for z, entry in pairs(valuesY) do
  70.             if not entry.done then
  71.                 local cluster = { name = entry.name, entryCount = 0, pos = { x = x, y = y, z = z } }
  72.                 crawl(map, x, y, z, cluster)
  73.                 table.insert(clusters, cluster)
  74.             end
  75.         end
  76.     end
  77. end
  78.  
  79. if args[2]=="d" then
  80.     table.sort(clusters, function(k1,k2)
  81.         local p1=k1.pos
  82.         local p2=k2.pos
  83.         return p1.x*p1.x+p1.y*p1.y+p1.z*p1.z>p2.x*p2.x+p2.y*p2.y+p2.z*p2.z
  84.     end)
  85. else
  86.     table.sort(clusters, function(k1, k2)
  87.         return k1.entryCount < k2.entryCount
  88.     end)
  89. end
  90.  
  91. function toShortName(name)
  92.     local index = string.find(name,":")
  93.     if index~=nil then
  94.         name = string.sub(name,index+1)
  95.     end
  96.     local l=string.len(name)
  97.     if l>10 then
  98.         return string.sub(name,l-10)
  99.     end
  100.     return name
  101. end
  102.  
  103. term.clear()
  104. for _, cluster in pairs(clusters) do
  105.     print(toShortName(cluster.name), tostring(cluster.entryCount) .. ":", cluster.pos.x, cluster.pos.z, cluster.pos.y)
  106. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement