Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Heimdall116, University of Michigan, August 2014
- -- Stole several local functions from dan200's ComputerCraft 1.58 package.
- 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
- -- To be implemented later
- local depth = 0
- local collected = 0
- local facing = 1
- local xRel = 0
- local yRel = 0
- local function collect()
- 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 round(q)
- if q == 0 then return 0 end
- b = math.abs(q)
- if q < 0 then a = -1 else a = 1 end
- if b-math.floor(b) < .5 then
- return math.floor(b) * a
- else
- return math.ceil(b) * a
- end
- end
- local function face(f)
- while f ~= facing do
- turtle.turnRight()
- if facing == 4 then
- facing = 1
- else
- facing = facing + 1
- end
- end
- end
- local function face1(n)
- -- Re-orient the turtle WRT original facing: 1=forward; 2=right; 3=back; 4=left
- if n-facing == 0 then return
- elseif n-facing > 0 and n-facing < 3 then
- while n-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 goTo(x2,y2)
- local dx = round(x2-xRel)
- local dy = round(y2-yRel)
- if dx == 0 then
- -- don't bother turning
- elseif dx > 0 then
- face(1)
- else
- face(3)
- end
- for n = 1,math.abs(dx) do
- tryDig()
- tryForward()
- end
- if dy == 0 then
- -- don't turn
- elseif dy > 0 then
- face(4)
- else
- face(2)
- end
- for n = 1,math.abs(dy) do
- tryDig()
- turtle.forward()
- end
- xRel = xRel + dx
- yRel = yRel + dy
- 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))
- print('r='..r)
- tryDigUp()
- turtle.up()
- -- Plot circle in increments of angle theta
- local theta = math.atan(1/r)
- for c = 0,(2*math.pi / theta) do
- xNew = r - round(r*math.cos(c*theta))
- yNew = round(r*math.sin(c*theta))
- goTo(xNew,yNew)
- turtle.placeDown()
- end
- end
- print( "Work complete..." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement