Advertisement
melzneni

scanPackaged

Nov 7th, 2024 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.73 KB | None | 0 0
  1. local function readFile(path)
  2.     local file = fs.open(path, "r")
  3.     local contents = file.readAll()
  4.     file.close()
  5.     return contents
  6. end
  7.  
  8. local function writeFile(path, data)
  9.     local file = fs.open(path, "w")
  10.     file.write(data)
  11.     file.close()
  12. end
  13.  
  14. local function skipAfter(contents, pattern)
  15.     local start = string.find(contents.data, pattern, contents.start, true)
  16.     contents.start = start + string.len(pattern)
  17. end
  18.  
  19. local function readEncodedBlock(contents)
  20.     if string.sub(contents.data, contents.start, contents.start) ~= "$" then
  21.         return nil
  22.     end
  23.     local escaped = false
  24.     local unescapedContent = ""
  25.     for i = contents.start + 1, #contents.data do
  26.         local c = contents.data:sub(i, i)
  27.         if escaped then
  28.             unescapedContent = unescapedContent .. c
  29.             escaped = false
  30.         elseif c == "\\" then
  31.             escaped = true
  32.         elseif c == "$" then
  33.             contents.start = i + 1
  34.             return unescapedContent
  35.         else
  36.             unescapedContent = unescapedContent .. c
  37.         end
  38.     end
  39. end
  40.  
  41. local thisFilePath = shell.getRunningProgram()
  42.  
  43. local contents = { data = readFile(thisFilePath), start = 0 }
  44.  
  45. skipAfter(contents, "===FI" .. "LES===")
  46. skipAfter(contents, "--[[")
  47.  
  48. while true do
  49.     local fileName = readEncodedBlock(contents)
  50.     if fileName == nil then
  51.         break
  52.     end
  53.     local fileContents = readEncodedBlock(contents)
  54.  
  55.     writeFile(fileName, fileContents)
  56. end
  57.  
  58. --===FILES===
  59. --[[$scan.lua$$local args = { ... }
  60. local scanner = require("scanLib")
  61.  
  62. local clusters = scanner.scanClusters(16, args\[1\])
  63.  
  64. if args\[2\] == "d" then
  65.     table.sort(clusters, function(k1, k2)
  66.         local p1 = k1.pos
  67.         local p2 = k2.pos
  68.         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
  69.     end)
  70. else
  71.     table.sort(clusters, function(k1, k2)
  72.         return k1.entryCount < k2.entryCount
  73.     end)
  74. end
  75.  
  76. local function toShortName(name)
  77.     local index = string.find(name, ":")
  78.     if index ~= nil then
  79.         name = string.sub(name, index + 1)
  80.     end
  81.     local l = string.len(name)
  82.     if l > 10 then
  83.         return string.sub(name, l - 10)
  84.     end
  85.     return name
  86. end
  87.  
  88. term.clear()
  89. term.setCursorPos(1,1)
  90. for _, cluster in pairs(clusters) do
  91.     print(toShortName(cluster.name), tostring(cluster.entryCount) .. ":", cluster.pos.x, cluster.pos.z, cluster.pos.y)
  92. end$$scanLib.lua$$local function getOrCreate(tbl, key)
  93.     if tbl\[key\] == nil then
  94.         tbl\[key\] = {}
  95.     end
  96.     return tbl\[key\]
  97. end
  98.  
  99. local function insert(map, x, y, z, name)
  100.     getOrCreate(getOrCreate(map, x), y)\[z\] = { name = name, done = false }
  101. end
  102.  
  103. local function getOrNull(tbl, key)
  104.     if tbl == nil or key == nil then
  105.         return nil
  106.     end
  107.     return tbl\[key\]
  108. end
  109.  
  110. local function get(map, x, y, z)
  111.     return getOrNull(getOrNull(getOrNull(map, x), y), z)
  112. end
  113.  
  114. local function length(x, y, z)
  115.     return x * x + y * y + z * z
  116. end
  117.  
  118. local function updatePos(pos, x, y, z)
  119.     if length(x, y, z) < length(pos.x, pos.y, pos.z) then
  120.         pos.x = x
  121.         pos.y = y
  122.         pos.z = z
  123.     end
  124. end
  125.  
  126. local function crawl(map, x, y, z, cluster)
  127.     local entry = get(map, x, y, z)
  128.     if entry == nil or entry.done then
  129.         return
  130.     end
  131.     updatePos(cluster.pos, x, y, z)
  132.     table.insert(cluster, { x = x, y = y, z = z })
  133.     cluster.entryCount = cluster.entryCount + 1
  134.     entry.done = true
  135.     crawl(map, x - 1, y, z, cluster)
  136.     crawl(map, x + 1, y, z, cluster)
  137.     crawl(map, x, y - 1, z, cluster)
  138.     crawl(map, x, y + 1, z, cluster)
  139.     crawl(map, x, y, z - 1, cluster)
  140.     crawl(map, x, y, z + 1, cluster)
  141. end
  142.  
  143. local function scanClusters(radius, searchString)
  144.     local scanner = peripheral.find("geoScanner")
  145.     local result = scanner.scan(radius)
  146.  
  147.     local map = {}
  148.     for _, v in pairs(result) do
  149.         local name = v.name
  150.         if string.find(name, searchString) then
  151.             insert(map, v.x, v.y, v.z, name)
  152.         end
  153.     end
  154.  
  155.     local clusters = {}
  156.     for x, valuesX in pairs(map) do
  157.         for y, valuesY in pairs(valuesX) do
  158.             for z, entry in pairs(valuesY) do
  159.                 if not entry.done then
  160.                     local pos = { x = x, y = y, z = z }
  161.                     local cluster = { name = entry.name, entryCount = 0, pos = pos, locations = { pos } }
  162.                     crawl(map, x, y, z, cluster)
  163.                     table.insert(clusters, cluster)
  164.                 end
  165.             end
  166.         end
  167.     end
  168.  
  169.     return clusters
  170. end
  171.  
  172. return {
  173.     scanClusters = scanClusters,
  174.     info = "scanClusters(<radius>)->{name='iron', entryCount=0, pos={x,y,z}, locations={{x,y,z}}}"
  175. }
  176. $]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement