Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- scanner = peripheral.wrap("back")
- -- block to search for
- TARGET_BLOCK = "allthemodium:suspicious_soul_sand"
- -- range of scanner, 16 is max possible
- SCAN_RANGE = 16
- -- don't consider ore veins above this y value
- -- because it's not as easy digging upward as it is downward
- -- so set this to 5 to make it so that all found veins
- -- that are above you are within reach from your current height
- MAX_Y = 12
- function scan(name)
- local blocks, err
- blocks, err = scanner.scan(16)
- local positions = {}
- if blocks then
- for _, b in pairs(blocks) do
- if b["name"] == name and b["y"] <= MAX_Y then
- table.insert(positions, {b["x"], b["y"], b["z"]})
- end
- end
- else
- positions = nil
- end
- return positions
- end
- function arePositionsClustered(pos1, pos2)
- local dx = math.abs(pos2[1] - pos1[1])
- local dy = math.abs(pos2[2] - pos1[2])
- local dz = math.abs(pos2[3] - pos1[3])
- return dx < 2 and dy < 2 and dz < 2
- end
- function l1Dist(pos)
- return math.abs(pos[1]) + math.abs(pos[2]) + math.abs(pos[3])
- end
- function l1DistWeighted(pos)
- return 2 * (math.abs(pos[1]) + math.abs(pos[3])) + math.abs(pos[2])
- end
- function closestToOrigin(tab)
- local closest = tab[1]
- local closestDist = l1Dist(tab[1])
- for i = 2, #tab do
- local dist = l1Dist(tab[i])
- if dist < closestDist then
- closestDist = dist
- closest = tab[i]
- end
- end
- return closest
- end
- function findClusters(tab)
- local clusters = {}
- for _, pos in pairs(tab) do
- table.insert(clusters, {pos})
- end
- local changed = true
- while changed do
- changed = false
- for c1i = #clusters - 1, 1, -1 do
- local c1 = clusters[c1i]
- for c2i = #clusters, c1i + 1, -1 do
- local c2 = clusters[c2i]
- for i1 = 1, #c1 do
- local pos1 = c1[i1]
- for i2 = 1, #c2 do
- local pos2 = c2[i2]
- if arePositionsClustered(pos1, pos2) then
- changed = true
- for _, pos in pairs(c2) do
- table.insert(c1, pos)
- end
- table.remove(clusters, c2i)
- break
- end
- end
- if changed then break end
- end
- end
- end
- end
- return clusters
- end
- function padInt(x, w)
- local s = tostring(x)
- for i = 1, w - #s do
- s = " " .. s
- end
- return s
- end
- SCAN_RANGE = math.min(SCAN_RANGE, 16)
- SCAN_RANGE = math.max(SCAN_RANGE, 1)
- while true do
- term.clear()
- term.setCursorPos(1, 1)
- print("Scanning for:\n" .. TARGET_BLOCK)
- print("Within range of " .. SCAN_RANGE .. " blocks")
- positions = scan(TARGET_BLOCK, SCAN_RANGE)
- if positions then
- --for _, pos in pairs(positions) do
- -- print(pos[1] .. ", " .. pos[2] .. ", " .. pos[3])
- --end
- if #positions == 0 then
- print("Nothing found")
- else
- print("Veins (x, z, y) (size):")
- local clusters = findClusters(positions)
- -- sort the clusters by size divided by distance
- -- in order of least to greatest
- -- meaning the last printed vein
- -- is the largest for how close it is
- -- to mine toward
- table.sort(clusters, function(c1, c2) return #c1 / l1DistWeighted(closestToOrigin(c1)) < #c2 / l1DistWeighted(closestToOrigin(c2)) end)
- for clusterId, cluster in pairs(clusters) do
- pos = closestToOrigin(cluster)
- print(padInt(pos[1], 3) .. ", " .. padInt(pos[3], 3) .. ", " .. padInt(pos[2], 3) .. " (" .. #cluster .. " blocks)")
- end
- local pathing = false
- print("facing north, best path:")
- if pos[1] < 0 then
- pathing = true
- print("go left " .. (-pos[1]))
- elseif pos[1] > 0 then
- pathing = true
- print("go right " .. (pos[1]))
- end
- if pos[3] < 0 then
- pathing = true
- print("go forward " .. (-pos[3]))
- elseif pos[3] > 0 then
- pathing = true
- print("go backward " .. (pos[3]))
- end
- if pos[2] > 2 then
- pathing = true
- print("mine straight up " .. (pos[2] - 2))
- elseif pos[2] < -1 then
- pathing = true
- print("mine straight down " .. (-pos[2] - 1))
- end
- if not pathing then
- print("You're there!")
- end
- end
- else
- print("scanner is being used too quickly!")
- end
- sleep(3)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement