Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = { ... }
- local scanner = peripheral.find("geoScanner")
- local radius = 16
- local result = scanner.scan(radius)
- local function getOrCreate(tbl, key)
- if tbl[key] == nil then
- tbl[key] = {}
- end
- return tbl[key]
- end
- local function insert(map, x, y, z, name)
- getOrCreate(getOrCreate(map, x), y)[z] = { name = name, done = false }
- end
- local function getOrNull(tbl, key)
- if tbl == nil or key == nil then
- return nil
- end
- return tbl[key]
- end
- local function get(map, x, y, z)
- return getOrNull(getOrNull(getOrNull(map, x), y), z)
- end
- local function length(x, y, z)
- return x * x + y * y + z * z
- end
- local function updatePos(pos, x, y, z)
- if length(x, y, z) < length(pos.x, pos.y, pos.z) then
- pos.x = x
- pos.y = y
- pos.z = z
- end
- end
- local function crawl(map, x, y, z, cluster)
- local entry = get(map, x, y, z)
- if entry == nil or entry.done then
- return
- end
- updatePos(cluster.pos, x, y, z)
- table.insert(cluster, { x = x, y = y, z = z })
- cluster.entryCount = cluster.entryCount + 1
- entry.done = true
- crawl(map, x - 1, y, z, cluster)
- crawl(map, x + 1, y, z, cluster)
- crawl(map, x, y - 1, z, cluster)
- crawl(map, x, y + 1, z, cluster)
- crawl(map, x, y, z - 1, cluster)
- crawl(map, x, y, z + 1, cluster)
- end
- local map = {}
- for _, v in pairs(result) do
- local name = v.name
- if string.find(name, args[1]) then
- insert(map, v.x, v.y, v.z, name)
- end
- end
- local clusters = {}
- for x, valuesX in pairs(map) do
- for y, valuesY in pairs(valuesX) do
- for z, entry in pairs(valuesY) do
- if not entry.done then
- local cluster = { name = entry.name, entryCount = 0, pos = { x = x, y = y, z = z } }
- crawl(map, x, y, z, cluster)
- table.insert(clusters, cluster)
- end
- end
- end
- end
- if args[2]=="d" then
- table.sort(clusters, function(k1,k2)
- local p1=k1.pos
- local p2=k2.pos
- 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
- end)
- else
- table.sort(clusters, function(k1, k2)
- return k1.entryCount < k2.entryCount
- end)
- end
- function toShortName(name)
- local index = string.find(name,":")
- if index~=nil then
- name = string.sub(name,index+1)
- end
- local l=string.len(name)
- if l>10 then
- return string.sub(name,l-10)
- end
- return name
- end
- term.clear()
- for _, cluster in pairs(clusters) do
- print(toShortName(cluster.name), tostring(cluster.entryCount) .. ":", cluster.pos.x, cluster.pos.z, cluster.pos.y)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement