View difference between Paste ID: DusN47gJ and GuA4yZsV
SHOW: | | - or go back to the newest paste.
1
scanner = peripheral.wrap("back")
2
3
-- block to search for
4-
TARGET_BLOCK = "minecraft:deepslate_diamond_ore"
4+
TARGET_BLOCK = "allthemodium:suspicious_soul_sand"
5
6
-- range of scanner, 16 is max possible
7
SCAN_RANGE = 16
8
9
-- don't consider ore veins above this y value
10
-- because it's not as easy digging upward as it is downward
11
-- so set this to 5 to make it so that all found veins
12
-- that are above you are within reach from your current height
13-
MAX_Y = 5
13+
MAX_Y = 12
14
15
function scan(name)
16
    local blocks, err
17
    blocks, err = scanner.scan(16)
18
    local positions = {}
19
    if blocks then
20
        for _, b in pairs(blocks) do
21
            if b["name"] == name and b["y"] <= MAX_Y then
22
                table.insert(positions, {b["x"], b["y"], b["z"]})
23
            end
24
        end
25
    else
26
        positions = nil
27
    end
28
    return positions
29
end
30
31
function arePositionsClustered(pos1, pos2)
32
    local dx = math.abs(pos2[1] - pos1[1])
33
    local dy = math.abs(pos2[2] - pos1[2])
34
    local dz = math.abs(pos2[3] - pos1[3])
35
    return dx < 2 and dy < 2 and dz < 2
36
end
37
38
function l1Dist(pos)
39
    return math.abs(pos[1]) + math.abs(pos[2]) + math.abs(pos[3])
40
end
41
42
function l1DistWeighted(pos)
43
    return 2 * (math.abs(pos[1]) + math.abs(pos[3])) + math.abs(pos[2])
44
end
45
46
function closestToOrigin(tab)
47
    local closest = tab[1]
48
    local closestDist = l1Dist(tab[1])
49
    for i = 2, #tab do
50
        local dist = l1Dist(tab[i])
51
        if dist < closestDist then
52
            closestDist = dist
53
            closest = tab[i]
54
        end
55
    end
56
    return closest
57
end
58
59
function findClusters(tab)
60
    local clusters = {}
61
    for _, pos in pairs(tab) do
62
        table.insert(clusters, {pos})
63
    end
64
    local changed = true
65
    while changed do
66
        changed = false
67
        for c1i = #clusters - 1, 1, -1 do
68
            local c1 = clusters[c1i]
69
            for c2i = #clusters, c1i + 1, -1 do
70
                local c2 = clusters[c2i]
71
                for i1 = 1, #c1 do
72
                    local pos1 = c1[i1]
73
                    for i2 = 1, #c2 do
74
                        local pos2 = c2[i2]
75
                        if arePositionsClustered(pos1, pos2) then
76
                            changed = true
77
                            for _, pos in pairs(c2) do
78
                                table.insert(c1, pos)
79
                            end
80
                            table.remove(clusters, c2i)
81
                            break
82
                        end
83
                    end
84
                    if changed then break end
85
                end
86
            end
87
        end
88
    end
89
    return clusters
90
end
91
92
function padInt(x, w)
93
    local s = tostring(x)
94
    for i = 1, w - #s do
95
        s = " " .. s
96
    end
97
    return s
98
end
99
100
SCAN_RANGE = math.min(SCAN_RANGE, 16)
101
SCAN_RANGE = math.max(SCAN_RANGE, 1)
102
103
while true do
104
    term.clear()
105
    term.setCursorPos(1, 1)
106
    print("Scanning for:\n" .. TARGET_BLOCK)
107
    print("Within range of " .. SCAN_RANGE .. " blocks")
108
109
    positions = scan(TARGET_BLOCK, SCAN_RANGE)
110
    if positions then
111
        --for _, pos in pairs(positions) do
112
        --    print(pos[1] .. ", " .. pos[2] .. ", " .. pos[3])
113
        --end
114
115
        if #positions == 0 then
116
            print("Nothing found")
117
        else
118
            print("Veins (x, z, y) (size):")
119
            local clusters = findClusters(positions)
120
            -- sort the clusters by size divided by distance
121
            -- in order of least to greatest
122
            -- meaning the last printed vein
123
            -- is the largest for how close it is
124
            -- to mine toward
125
            table.sort(clusters, function(c1, c2) return #c1 / l1DistWeighted(closestToOrigin(c1)) < #c2 / l1DistWeighted(closestToOrigin(c2)) end)
126
            for clusterId, cluster in pairs(clusters) do
127
                pos = closestToOrigin(cluster)
128
                print(padInt(pos[1], 3) .. ", " .. padInt(pos[3], 3) .. ", " .. padInt(pos[2], 3) .. " (" .. #cluster .. " blocks)")
129
            end
130
            local pathing = false
131
            print("facing north, best path:")
132
            if pos[1] < 0 then
133
                pathing = true
134
                print("go left " .. (-pos[1]))
135
            elseif pos[1] > 0 then
136
                pathing = true
137
                print("go right " .. (pos[1]))
138
            end
139
            if pos[3] < 0 then
140
                pathing = true
141
                print("go forward " .. (-pos[3]))
142
            elseif pos[3] > 0 then
143
                pathing = true
144
                print("go backward " .. (pos[3]))
145
            end
146
            if pos[2] > 2 then
147
                pathing = true
148
                print("mine straight up " .. (pos[2] - 2))
149
            elseif pos[2] < -1 then
150
                pathing = true
151
                print("mine straight down " .. (-pos[2] - 1))
152
            end
153
            if not pathing then
154
                print("You're there!")
155
            end
156
        end
157
    else
158
        print("scanner is being used too quickly!")
159
    end
160
161
    sleep(3)
162
end
163