Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Heimdall116, August 2014
- 1) Stole several functions from dan200's tunneling routine
- to provide consistent turtle operation.
- 2) Bresenham circle algorithm adapted from C# code on Wikipedia.
- 3) Thanks to computercraft.info forums for help with nesting tables.
- 4) Efficient pathing method inspired by demonpants (
- 5) Remaining code is original, and is now public domain.
- M Glow Blue.
- ]]--
- local tArgs = { ... }
- if #tArgs ~= 1 then
- print( "Usage: dome <radius>" )
- return
- end
- -- Read in the radius
- local radius = tonumber( tArgs[1] )
- if radius < 1 then
- print( "Radius must be positive" )
- return
- end
- local depth = 0
- local collected = 0
- local facing = 1
- local xRel = 0
- local yRel = 0
- local function collect() -- Modified to constantly drop all junk
- for i=1,9 do
- if turtle.getItemCount(i) == 1 then turtle.drop(i) end
- end
- --collected = collected + 1
- --if math.fmod(collected, 25) == 0 then
- -- print( "Mined "..collected.." items." )
- --end
- end
- local function tryDig()
- while turtle.detect() do
- if turtle.dig() then
- collect()
- sleep(0.5)
- else
- return false
- end
- end
- return true
- end
- local function tryDigUp()
- while turtle.detectUp() do
- if turtle.digUp() then
- collect()
- sleep(0.5)
- else
- return false
- end
- end
- return true
- end
- local function tryDigDown()
- while turtle.detectDown() do
- if turtle.digDown() then
- collect()
- sleep(0.5)
- else
- return false
- end
- end
- return true
- end
- local function refuel()
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" or fuelLevel > 0 then
- return
- end
- local function tryRefuel()
- for n=1,16 do
- if turtle.getItemCount(n) > 0 then
- turtle.select(n)
- if turtle.refuel(1) then
- turtle.select(1)
- return true
- end
- end
- end
- turtle.select(1)
- return false
- end
- if not tryRefuel() then
- print( "Add more fuel to continue." )
- while not tryRefuel() do
- sleep(1)
- end
- print( "Resuming Tunnel." )
- end
- end
- local function tryUp()
- refuel()
- while not turtle.up() do
- if turtle.detectUp() then
- if not tryDigUp() then
- return false
- end
- elseif turtle.attackUp() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- return true
- end
- local function tryDown()
- refuel()
- while not turtle.down() do
- if turtle.detectDown() then
- if not tryDigDown() then
- return false
- end
- elseif turtle.attackDown() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- return true
- end
- local function tryForward()
- refuel()
- while not turtle.forward() do
- if turtle.detect() then
- if not tryDig() then
- return false
- end
- elseif turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- return true
- end
- local function tryPlaceDown() -- Added by Heimdall116
- if turtle.detectDown() then return true
- elseif not turtle.placeDown() then
- if turtle.attackDown() then
- collect()
- sleep(0.5)
- return turtle.placeDown()
- else
- for i=1,9 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- print('Selecting ',i)
- return turtle.placeDown()
- end
- end
- end
- end
- return true
- end
- local function round(q)
- if q == 0 then return 0 end
- local a = 1
- if q < 0 then a = -1 end
- b = math.abs(q)
- if b-math.floor(b) < .5 then
- return math.floor(b) * a
- else
- return math.ceil(b) * a
- end
- end
- local function face(f)
- -- Re-orient the turtle WRT original facing: 1=forward; 2=right; 3=back; 4=left
- if f-facing == 0 then return
- elseif math.abs(f-facing) == 2 or f-facing == 1 or f-facing == -3 then
- while f-facing ~= 0 do
- turtle.turnRight()
- facing = facing + 1
- if facing == 5 then facing = 1 end
- end
- else
- turtle.turnLeft()
- facing = facing - 1
- if facing == 0 then facing = 4 end
- end
- end
- local function getThereX(dist)
- if dist == 0 then
- return
- elseif dist > 0 then
- face(1)
- else
- face(3)
- end
- for n = 1,math.abs(dist) do
- tryDig()
- tryForward()
- end
- xRel = xRel + dist
- return 0
- end
- local function getThereY(dist)
- if dist == 0 then
- return
- elseif dist > 0 then
- face(4)
- else
- face(2)
- end
- for n = 1,math.abs(dist) do
- tryDig()
- tryForward()
- end
- yRel = yRel + dist
- return 0
- end
- local function goTo(x2,y2)
- local dx = round(x2-xRel)
- local dy = round(y2-yRel)
- -- Check for low-hanging fruit first
- if facing == 1 and dx > 0 then
- dx = getThereX(dx)
- elseif facing == 2 and dy < 0 then
- dy = getThereY(dy)
- elseif facing == 3 and dx < 0 then
- dx = getThereX(dx)
- elseif facing == 4 and dy > 0 then
- dy = getThereY(dy)
- end
- -- do the shorter angle next
- if facing % 2 == 1 then
- dy = getThereY(dy)
- dx = getThereX(dx)
- else
- dx = getThereX(dx)
- dy = getThereY(dy)
- end
- end
- print( "Building dome..." )
- for h=1,radius do
- -- r is the "local" radius for height "h"
- local r = round(radius * math.cos((h-1)/radius * math.pi/2))
- --local r = radius
- --print('r='..r)
- tryDigUp()
- turtle.up()
- -- Bresenham's circle algorithm, adapted
- -- from Wikipedia 8/25/14
- local x0 = radius
- local y0 = 0
- local x = r
- local y = 0
- local rErr = 1-x
- local points = {}
- while x >= y do
- --[[ use theta to sort ordered pairs for efficient turtle
- motion. Thanks to hilburn on computercraft.info forums
- for helping me nest these tables... ]]--
- --local x1 = 0
- --local y1 = 0
- --local t1 = 0
- --local r1 = 0
- points[#points+1] = { ['x']= x+x0, ['y']= y+y0, ['theta']=math.atan2( x+x0, y+y0) }
- points[#points+1] = { ['x']= y+x0, ['y']= x+y0, ['theta']=math.atan2( y+x0, x+y0) }
- points[#points+1] = { ['x']=-x+x0, ['y']= y+y0, ['theta']=math.atan2(-x+x0, y+y0) }
- points[#points+1] = { ['x']=-y+x0, ['y']= x+y0, ['theta']=math.atan2(-y+x0, x+y0) }
- points[#points+1] = { ['x']=-x+x0, ['y']=-y+y0, ['theta']=math.atan2(-x+x0,-y+y0) }
- points[#points+1] = { ['x']=-y+x0, ['y']=-x+y0, ['theta']=math.atan2(-y+x0,-x+y0) }
- points[#points+1] = { ['x']= x+x0, ['y']=-y+y0, ['theta']=math.atan2( x+x0,-y+y0) }
- points[#points+1] = { ['x']= y+x0, ['y']=-x+y0, ['theta']=math.atan2( y+x0,-x+y0) }
- y=y+1
- if rErr < 0 then
- rErr = rErr+2*y+1
- else
- x=x-1
- rErr = rErr+2*(y-x+1)
- end
- end
- -- Calculate theta
- --for i=1,#points do
- -- if points[i].x <= 0 and points[i].y >= 0 then
- -- points[i].theta = math.acos(r/math.sqrt(points[i].x^2+points[i].y^2))
- --
- -- elseif points[i].x >= 0 and points[i].y >= 0 then
- -- points[i].theta = math.pi/2 + math.acos(r/math.sqrt(points[i].x^2+points[i].y^2))
- --
- -- elseif points[i].x >= 0 and points[i].y <= 0 then
- -- points[i].theta = 3*math.pi/2 + math.acos(r/math.sqrt(points[i].x^2+points[i].y^2))
- --
- -- elseif points[i].x <= 0 and points[i].y <= 0 then
- -- points[i].theta = 3*math.pi/2 + math.acos(r/math.sqrt(points[i].x^2+points[i].y^2))
- --
- -- else
- -- print('Invalid theta: x=',points.i.x,', y=',points.i.y,', theta=',theta)
- -- end
- --end
- table.sort(points, function(p1,p2) return p1.theta<p2.theta end)
- -- method blatantly stolen from demonpants.
- for i = 1, #points - 1 do
- local c = i + 1
- local minDist = math.abs(points[c].x-points[i].x) + math.abs(points[c].y-points[i].y)
- for j = i+2, #points - 1 do
- local dist = math.abs(points[j].x-points[i].x) + math.abs(points[j].y-points[i].y)
- if dist < minDist then
- c = j
- minDist = dist
- end
- end
- swap = points[i+1].x
- points[i+1].x = points[c].x
- points[c].x = swap
- swap = points[i+1].y
- points[i+1].y = points[c].y
- points[c].y = swap
- end
- for i=1,#points do
- --print('x=',points[i].x,', y=',points[i].y,', theta=',points[i].theta)
- goTo(points[i].x,points[i].y)
- if not tryPlaceDown() then
- print('Failed at x=',points[i].x,', y=',points[i].y,', theta=',points[i].theta)
- return
- end
- end
- end
- print( "Work complete..." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement