Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --FUNC
- function point(x,y)
- return {["x"]=x,["y"]=y}
- end
- function isEqual(a,b)
- return ((a.x == b.x) and (a.y == b.y))
- end
- function format(a)
- return '('..a.x..', '..a.y..')'
- end
- --
- function forward()
- while not turtle.forward() do
- if not turtle.dig() then
- turtle.attack()
- end
- sleep(0.1)
- end
- end
- function up()
- while not turtle.up() do
- if not turtle.digUp() then
- turtle.attackUp()
- end
- sleep(0.1)
- end
- end
- function down()
- while not turtle.down() do
- if not turtle.digDown() then
- turtle.attackDown()
- end
- sleep(0.1)
- end
- end
- --
- function face(cur, dest)
- while not (cur == dest) do
- if math.abs(((cur+1)%4)-dest) <= math.abs(((cur-1))%4-dest) then
- turtle.turnRight()
- cur = cur+1
- else
- turtle.turnLeft()
- cur = cur-1
- end
- cur = cur%4
- end
- return cur
- end
- function goTo(move, pos, f, dest)
- if not isEqual(pos, dest) then
- if (pos.y > 0 and dest.y > pos.y) or ( pos.y < 0 and dest.y < pos.y) then
- while not (pos.x == dest.x) do
- if pos.x < dest.x then
- pos, f = move.Xp(pos, f)
- elseif pos.x > dest.x then
- pos, f = move.Xm(pos, f)
- end
- end
- while not (pos.y == dest.y) do
- if pos.y < dest.y then
- pos, f = move.Yp(pos, f)
- elseif pos.y > dest.y then
- pos, f = move.Ym(pos, f)
- end
- end
- else
- while not (pos.y == dest.y) do
- if pos.y < dest.y then
- pos, f = move.Yp(pos, f)
- elseif pos.y > dest.y then
- pos, f = move.Ym(pos, f)
- end
- end
- while not (pos.x == dest.x) do
- if pos.x < dest.x then
- pos, f = move.Xp(pos, f)
- elseif pos.x > dest.x then
- pos, f = move.Xm(pos, f)
- end
- end
- end
- end
- return pos, f
- end
- --
- local tArgs = { ... }
- if #tArgs < 2 then
- print("Use: digCylinder <radius> <height> <direction>")
- return
- end
- if tonumber(tArgs[1]) == nil then
- print("Error: argument <radius> is not a number")
- return
- elseif tonumber(tArgs[2]) == nil then
- print("Error: argument <radius> is not a number")
- return
- elseif tArgs[3] ~= "up" and tArgs[3] ~= "down" and tArgs[3] ~= "front" then
- print("<direction> must be up, down or front")
- return
- end
- tArgs[1] = tonumber(tArgs[1])
- tArgs[2] = tonumber(tArgs[2])
- do
- local fuel = math.ceil((2*math.pi*(tArgs[1]^2)*tArgs[2])+tArgs[2]+2*tArgs[1])
- if turtle.getFuelLevel() < fuel then
- print("Not enough fuel. Need at least "..fuel.." units (rough estimate)")
- return
- end
- end
- --
- local pts = {}
- print("Calculating pixels")
- do --Locals
- local E, X, Y
- E = -tArgs[1]
- X = tArgs[1]
- Y = 0
- while Y <= X do
- table.insert(pts, point(X, Y))
- E = E+2*Y+1
- Y = Y+1
- if E >= 0 then
- E = E-2*X-1
- X = X-1
- end
- end
- --First pixels
- for i = #pts, 1, -1 do
- table.insert(pts, point(pts[i].y, pts[i].x)) --On inverse les coordonnees
- end
- --Finishing quarter
- for i = #pts-1, 1, -1 do --On saute le dernier
- table.insert(pts, point(-pts[i].x, pts[i].y))
- end
- --Finishing other half
- for i = #pts-1, 2, -1 do --On saute le dernier et le premier
- table.insert(pts, point(pts[i].x, -pts[i].y))
- end
- end
- print("Done")
- print("Defining lines...")
- table.sort(pts, function(a,b) return a.y > b.y end)
- local lines = {}
- do --Locals
- local t = {}
- for i = tArgs[1], -tArgs[1], -1 do
- table.insert(t, {})
- for k, v in ipairs(pts) do
- if i == v.y then
- table.insert(t[#t], v)
- end
- end
- table.sort(t[#t], function(a,b) return a.x > b.x end)
- end
- for k, v in ipairs(t) do --Alternating lines starts and ends
- if k/2 == math.floor(k/2) then
- table.insert(lines, {v[1], v[#v], ["n"]=k})
- else
- table.insert(lines, {v[#v], v[1], ["n"]=k})
- end
- end
- end
- print("Done")
- local pos = point(0,0)
- local facing = 0
- --[[ 0 -> y++
- 1 -> x++
- 2 -> y--
- 3 -> x-- ]]
- local move = {}
- if tArgs[3] == "front" then
- move.Vp = function(p, f) --Vertical +
- f = face(f, 0)
- forward()
- return p, f
- end
- move.Vm = function(p, f) --Vertical -
- f = face(f, 2)
- forward()
- return p, f
- end
- move.Xp = function(p, f)
- f = face(f, 1)
- forward()
- p.x = p.x+1
- return p, f
- end
- move.Xm = function(p, f)
- f = face(f, 3)
- forward()
- p.x = p.x-1
- return p, f
- end
- move.Yp = function(p, f)
- up()
- p.y = p.y+1
- return p, f
- end
- move.Ym = function(p, f)
- down()
- p.y = p.y-1
- return p, f
- end
- elseif tArgs[3] == "up" then
- move.Vp = function(...)
- up()
- return ...
- end
- move.Vm = function(...)
- down()
- return ...
- end
- move.Xp = function(p, f)
- f = face(f, 1)
- forward()
- p.x = p.x+1
- return p, f
- end
- move.Xm = function(p, f)
- f = face(f, 3)
- forward()
- p.x = p.x-1
- return p, f
- end
- move.Yp = function(p, f)
- f = face(f, 0)
- forward()
- p.y = p.y+1
- return p, f
- end
- move.Ym = function(p, f)
- f = face(f, 2)
- forward()
- p.y = p.y-1
- return p, f
- end
- else
- move.Vp = function(...)
- down()
- return ...
- end
- move.Vm = function(...)
- up()
- return ...
- end
- move.Xp = function(p, f)
- f = face(f, 1)
- forward()
- p.x = p.x+1
- return p, f
- end
- move.Xm = function(p, f)
- f = face(f, 3)
- forward()
- p.x = p.x-1
- return p, f
- end
- move.Yp = function(p, f)
- f = face(f, 0)
- forward()
- p.y = p.y+1
- return p, f
- end
- move.Ym = function(p, f)
- f = face(f, 2)
- forward()
- p.y = p.y-1
- return p, f
- end
- end
- pos, facing = goTo(move, pos, facing, lines[1][1]) --First move, don't want digging
- print("Start")
- for i = 1, tArgs[2] do
- pos, facing = move.Vp(pos, facing) --Vertical +
- for k, v in ipairs(lines) do
- --Keeping first two slots with only one item inside (we don't want cobble or dirt -> put blocks you don't want to keep in these slots)
- if turtle.getItemCount(1) > 0 then
- turtle.select(1)
- turtle.dropUp(turtle.getItemCount(1)-1)
- end
- if turtle.getItemCount(2) > 0 then
- turtle.select(2)
- turtle.dropUp(turtle.getItemCount(2)-1)
- end
- turtle.select(1)
- print("Pos: "..format(pos).."; Dest: "..format(v[1]))
- pos, facing = goTo(move, pos, facing, v[1]) --Start of the line
- print("Pos: "..format(pos).."; Dest: "..format(v[2]))
- pos,facing = goTo(move, pos, facing, v[2]) --End of the line
- end
- print("\tLayer "..i.." done")
- --Reversing table
- if i/2 == math.floor(i/2) then
- table.sort(lines, function(a, b) return a.n < b.n end)
- else
- table.sort(lines, function(a, b) return a.n > b.n end)
- end
- --We start on opposite side of line
- for k, v in ipairs(lines) do
- local buffer = v[1]
- lines[k][1] = v[2]
- lines[k][2] = buffer
- end
- end
- pos, facing = goTo(move, pos, facing, point(0,0))
- for i = tArgs[2], 1, -1 do
- pos, facing = move.Vm(pos, facing)
- end
- facing = face(facing, 0)
- print("End")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement