Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function readFile(path)
- local file = fs.open(path, "r")
- local contents = file.readAll()
- file.close()
- return contents
- end
- local function writeFile(path, data)
- local file = fs.open(path, "w")
- file.write(data)
- file.close()
- end
- local function skipAfter(contents, pattern)
- local start = string.find(contents.data, pattern, contents.start, true)
- contents.start = start + string.len(pattern)
- end
- local function readEncodedBlock(contents)
- if string.sub(contents.data, contents.start, contents.start) ~= "$" then
- return nil
- end
- local escaped = false
- local unescapedContent = ""
- for i = contents.start + 1, #contents.data do
- local c = contents.data:sub(i, i)
- if escaped then
- unescapedContent = unescapedContent .. c
- escaped = false
- elseif c == "\\" then
- escaped = true
- elseif c == "$" then
- contents.start = i + 1
- return unescapedContent
- else
- unescapedContent = unescapedContent .. c
- end
- end
- end
- local thisFilePath = shell.getRunningProgram()
- local contents = { data = readFile(thisFilePath), start = 0 }
- skipAfter(contents, "===FI" .. "LES===")
- skipAfter(contents, "--[[")
- while true do
- local fileName = readEncodedBlock(contents)
- if fileName == nil then
- break
- end
- local fileContents = readEncodedBlock(contents)
- writeFile(fileName, fileContents)
- end
- --===FILES===
- --[[$scan.lua$$local args = { ... }
- local scanner = require("scanLib")
- local clusters = scanner.scanClusters(16, args\[1\])
- 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
- local 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()
- term.setCursorPos(1,1)
- for _, cluster in pairs(clusters) do
- print(toShortName(cluster.name), tostring(cluster.entryCount) .. ":", cluster.pos.x, cluster.pos.z, cluster.pos.y)
- end$$scanLib.lua$$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 function scanClusters(radius, searchString)
- local scanner = peripheral.find("geoScanner")
- local result = scanner.scan(radius)
- local map = {}
- for _, v in pairs(result) do
- local name = v.name
- if string.find(name, searchString) 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 pos = { x = x, y = y, z = z }
- local cluster = { name = entry.name, entryCount = 0, pos = pos, locations = { pos } }
- crawl(map, x, y, z, cluster)
- table.insert(clusters, cluster)
- end
- end
- end
- end
- return clusters
- end
- return {
- scanClusters = scanClusters,
- info = "scanClusters(<radius>)->{name='iron', entryCount=0, pos={x,y,z}, locations={{x,y,z}}}"
- }
- $]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement