Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local blacklist = {
- "minecraft:stone",
- "minecraft:dirt",
- "minecraft:diorite",
- "minecraft:andesite",
- "minecraft:granite",
- "minecraft:gravel",
- "quark:slate"
- }
- local function veinmine()
- local tx,ty,tz = 0,0,1
- local rot = 0
- local ores = {}
- local non_ores = {}
- local air = {{0,0,1}}
- local function compare_tables(t1, t2)
- if not t1 or not t2 then return false end
- for i = 1, #t1 do
- if t1[i] ~= t2[i] then return false end
- end
- return true
- end
- local function find_in_table(t, val)
- for _, v in pairs(t) do
- if compare_tables(v, val) then return _ end
- end
- return false
- end
- local function check_ore(direction)
- if direction == "up" then
- _, j = turtle.inspectUp()
- if not _ then return "air" end
- for i,v in pairs(blacklist) do
- if j.name == v then
- return false
- end
- end
- return true
- elseif direction == "down" then
- _, j = turtle.inspectDown()
- if not _ then return "air" end
- for i,v in pairs(blacklist) do
- if _ and j.name == v then
- return false
- end
- end
- return true
- elseif direction == "forward" then
- _, j = turtle.inspect()
- if not _ then return "air" end
- for i,v in pairs(blacklist) do
- if _ and j.name == v then
- return false
- end
- end
- return true
- end
- end
- local function find_path(pos)
- local path = {
- {tx,ty,tz}
- }
- local x,y,z = pos[1], pos[2], pos[3]
- while not compare_tables(path[#path], {x,y,z}) do
- if tx < x then
- for i = 1, x-tx do
- if find_in_table(air, {tx+i, ty, tz}) or compare_tables({tx+i, ty, tz}, pos) then
- table.insert(path, {tx+i, ty, tz})
- else
- break
- end
- end
- elseif tx > x then
- for i = 1, tx-x do
- if find_in_table(air, {tx-i, ty, tz}) or compare_tables({tx-i, ty, tz}, pos) then
- table.insert(path, {tx-i, ty, tz})
- else
- break
- end
- end
- end
- if ty < y then
- for i = 1, y-ty do
- if find_in_table(air,{tx, ty+i, tz}) or compare_tables({tx, ty+1, tz}, pos) then
- table.insert(path, {tx, ty+i, tz})
- else
- break
- end
- end
- elseif ty > y then
- for i = 1, ty-y do
- if find_in_table(air,{tx, ty-1, tz}) or compare_tables({tx, ty-1, tz}, pos) then
- table.insert(path, {tx, ty-1, tz})
- else
- break
- end
- end
- end
- if tz < z then
- for i = 1, z-tz do
- if find_in_table(air,{tx, ty, tz+i}) or compare_tables({tx, ty, tz+1}, pos) then
- table.insert(path, {tx, ty, tz+i})
- else
- break
- end
- end
- elseif tz > z then
- for i = 1, tz-z do
- if find_in_table(air,{tx, ty, tz-1}) or compare_tables({tx, ty, tz-1}, pos) then
- table.insert(path, {tx, ty, tz-1})
- else
- break
- end
- end
- end
- end
- print("Generated path:")
- for i,v in pairs(path) do
- print(v[1], v[2], v[3])
- end
- return path
- end
- local function destroy(pos)
- if tx < pos[1] then
- if rot == 0 then
- turtle.turnRight()
- rot = 1
- elseif rot > 1 then
- for i = 1, rot - 1 do
- turtle.turnLeft()
- end
- rot = 1
- end
- while not turtle.forward() do
- turtle.attack()
- turtle.dig()
- end
- elseif tx > pos[1] then
- if rot == 0 then
- turtle.turnLeft()
- rot = 3
- elseif rot < 3 then
- for i = 1, 3 - rot do
- turtle.turnRight()
- end
- rot = 3
- end
- while not turtle.forward() do
- turtle.attack()
- turtle.dig()
- end
- end
- if rot == 1 then
- tx = tx + 1
- elseif rot == 3 then
- tx = tx - 1
- end
- if tx == pos[1] and ty == pos[2] and tz == pos[3] then return end
- if tz < pos[3] then
- if rot == 3 then
- turtle.turnRight()
- rot = 0
- elseif rot < 3 then
- for i = 1, rot do
- turtle.turnLeft()
- end
- rot = 0
- end
- while not turtle.forward() do
- turtle.attack()
- turtle.dig()
- end
- elseif tz > pos[3] then
- if rot == 3 then
- turtle.turnLeft()
- rot = 2
- elseif rot < 2 then
- for i = 1, 2 - rot do
- turtle.turnRight()
- end
- rot = 2
- end
- while not turtle.forward() do
- turtle.attack()
- turtle.dig()
- end
- end
- if rot == 0 then
- tz = tz + 1
- elseif rot == 2 then
- tz = tz - 1
- end
- if tx == pos[1] and ty == pos[2] and tz == pos[3] then return end
- if ty < pos[2] then
- while not turtle.up() do
- turtle.digUp()
- turtle.attackUp()
- end
- ty = ty + 1
- if tx == pos[1] and ty == pos[2] and tz == pos[3] then return end
- elseif ty > pos[2] then
- while not turtle.down() do
- turtle.digDown()
- turtle.attackDown()
- end
- ty = ty - 1
- if tx == pos[1] and ty == pos[2] and tz == pos[3] then return end
- end
- end
- local function look_around()
- print("c1")
- checks = {
- {tx+1,ty,tz},
- {tx-1,ty,tz},
- {tx,ty+1,tz},
- {tx,ty-1,tz},
- {tx,ty,tz+1},
- {tx,ty,tz-1}
- }
- for i,v in pairs(air) do
- if find_in_table(checks, v) then
- table.remove(checks, find_in_table(checks, v))
- end
- end
- print("c2", #checks)
- for i,v in pairs(checks) do
- print("recursing")
- if tx < v[1] then
- if rot == 0 then
- turtle.turnRight()
- rot = 1
- elseif rot > 1 then
- for i = 1, rot - 1 do
- turtle.turnLeft()
- end
- rot = 1
- end
- local result = check_ore("forward")
- if result == "air" then
- table.insert(air, v)
- elseif result == true then
- table.insert(ores, v)
- end
- elseif tx > v[1] then
- if rot == 0 then
- turtle.turnLeft()
- rot = 3
- elseif rot < 3 then
- for i = 1, 3 - rot do
- turtle.turnRight()
- end
- rot = 3
- end
- local result = check_ore("forward")
- if result == "air" then
- table.insert(air, v)
- elseif result == true then
- table.insert(ores, v)
- end
- end
- if tz < v[3] then
- if rot == 3 then
- turtle.turnRight()
- rot = 0
- elseif rot < 3 then
- for i = 1, rot do
- turtle.turnLeft()
- end
- rot = 0
- end
- local result = check_ore("forward")
- if result == "air" then
- table.insert(air, v)
- elseif result == true then
- table.insert(ores, v)
- end
- elseif tz > v[3] then
- if rot == 3 then
- turtle.turnLeft()
- rot = 2
- elseif rot < 2 then
- for i = 1, 2 - rot do
- turtle.turnRight()
- end
- rot = 2
- end
- local result = check_ore("forward")
- if result == "air" then
- table.insert(air, v)
- elseif not result then
- table.insert(non_ores, v)
- elseif result == true then
- table.insert(ores, v)
- end
- end
- if ty < v[2] then
- local result = check_ore("up")
- if result == "air" then
- table.insert(air, v)
- elseif not result then
- table.insert(non_ores, v)
- elseif result == true then
- table.insert(ores, v)
- end
- elseif ty > v[2] then
- local result = check_ore("down")
- if result == "air" then
- table.insert(air, v)
- elseif not result then
- table.insert(non_ores, v)
- elseif result == true then
- table.insert(ores, v)
- end
- end
- end
- end
- local function find_closest_ore()
- local closest
- local distance
- for i,v in pairs(ores) do
- if not closest then
- closest = v
- distance = math.sqrt((v[1]-tx)^2 + (v[2]-ty)^2 + (v[3]-tz)^2)
- else
- if math.sqrt((v[1]-tx)^2 + (v[2]-ty)^2 + (v[3]-tz)^2) < distance then
- distance = math.sqrt((v[1]-tx)^2 + (v[2]-ty)^2 + (v[3]-tz)^2)
- closest = v
- end
- end
- end
- print("Closest ore position:", closest[1], closest[2], closest[3])
- return closest
- end
- turtle.dig()
- turtle.forward()
- look_around()
- while #ores ~= 0 do
- print("check 1")
- local ore = find_closest_ore()
- print("check 2")
- local path = find_path(ore)
- for i,v in pairs(path) do
- print("Current position:", tx, ty, tz, rot)
- destroy(v)
- end
- print("check 3")
- look_around()
- end
- end
- veinmine()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement