Advertisement
heimdall116

Untitled

Aug 24th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Heimdall116, University of Michigan, August 2014
  2.     -- Stole several local functions from dan200's ComputerCraft 1.58 package.
  3.     local tArgs = { ... }
  4.     if #tArgs ~= 1 then
  5.         print( "Usage: dome <radius>" )
  6.         return
  7.     end
  8.  
  9.     -- Read in the radius
  10.     local radius = tonumber( tArgs[1] )
  11.     if radius < 1 then
  12.         print( "Radius must be positive" )
  13.         return
  14.     end
  15.  
  16.     -- To be implemented later
  17.     local depth = 0
  18.     local collected = 0
  19.     local facing = 1
  20.     local xRel = 0
  21.     local yRel = 0
  22.  
  23.     local function collect()
  24.         collected = collected + 1
  25.         if math.fmod(collected, 25) == 0 then
  26.             print( "Mined "..collected.." items." )
  27.         end
  28.     end
  29.  
  30.     local function tryDig()
  31.         while turtle.detect() do
  32.             if turtle.dig() then
  33.                 collect()
  34.                 sleep(0.5)
  35.             else
  36.                 return false
  37.             end
  38.         end
  39.         return true
  40.     end
  41.  
  42.     local function tryDigUp()
  43.         while turtle.detectUp() do
  44.             if turtle.digUp() then
  45.                 collect()
  46.                 sleep(0.5)
  47.             else
  48.                 return false
  49.             end
  50.         end
  51.         return true
  52.     end
  53.  
  54.     local function tryDigDown()
  55.         while turtle.detectDown() do
  56.             if turtle.digDown() then
  57.                 collect()
  58.                 sleep(0.5)
  59.             else
  60.                 return false
  61.             end
  62.         end
  63.         return true
  64.     end
  65.  
  66.     local function refuel()
  67.         local fuelLevel = turtle.getFuelLevel()
  68.         if fuelLevel == "unlimited" or fuelLevel > 0 then
  69.             return
  70.         end
  71.        
  72.         local function tryRefuel()
  73.             for n=1,16 do
  74.                 if turtle.getItemCount(n) > 0 then
  75.                     turtle.select(n)
  76.                     if turtle.refuel(1) then
  77.                         turtle.select(1)
  78.                         return true
  79.                     end
  80.                 end
  81.             end
  82.             turtle.select(1)
  83.             return false
  84.         end
  85.        
  86.         if not tryRefuel() then
  87.             print( "Add more fuel to continue." )
  88.             while not tryRefuel() do
  89.                 sleep(1)
  90.             end
  91.             print( "Resuming Tunnel." )
  92.         end
  93.     end
  94.  
  95.     local function tryUp()
  96.         refuel()
  97.         while not turtle.up() do
  98.             if turtle.detectUp() then
  99.                 if not tryDigUp() then
  100.                     return false
  101.                 end
  102.             elseif turtle.attackUp() then
  103.                 collect()
  104.             else
  105.                 sleep( 0.5 )
  106.             end
  107.         end
  108.         return true
  109.     end
  110.  
  111.     local function tryDown()
  112.         refuel()
  113.         while not turtle.down() do
  114.             if turtle.detectDown() then
  115.                 if not tryDigDown() then
  116.                     return false
  117.                 end
  118.             elseif turtle.attackDown() then
  119.                 collect()
  120.             else
  121.                 sleep( 0.5 )
  122.             end
  123.         end
  124.         return true
  125.     end
  126.  
  127.     local function tryForward()
  128.         refuel()
  129.         while not turtle.forward() do
  130.             if turtle.detect() then
  131.                 if not tryDig() then
  132.                     return false
  133.                 end
  134.             elseif turtle.attack() then
  135.                 collect()
  136.             else
  137.                 sleep( 0.5 )
  138.             end
  139.         end
  140.         return true
  141.     end
  142.  
  143.     local function round(q)
  144.         if q == 0 then return 0 end
  145.         b = math.abs(q)
  146.         if q < 0 then a = -1 else a = 1 end
  147.        
  148.         if b-math.floor(b) < .5 then
  149.             return math.floor(b) * a
  150.         else
  151.             return math.ceil(b) * a
  152.         end
  153.     end
  154.  
  155.     local function face(f)
  156.         while f ~= facing do
  157.             turtle.turnRight()
  158.             if facing == 4 then
  159.                 facing = 1
  160.             else
  161.                 facing = facing + 1
  162.             end
  163.         end
  164.     end
  165.  
  166.     local function face1(n)
  167.     -- Re-orient the turtle WRT original facing: 1=forward; 2=right; 3=back; 4=left
  168.         if n-facing == 0 then return
  169.         elseif n-facing > 0 and n-facing < 3 then
  170.             while n-facing ~= 0 do
  171.                 turtle.turnRight()
  172.                 facing = facing + 1
  173.                 if facing == 5 then facing = 1 end
  174.             end
  175.         else
  176.             turtle.turnLeft()
  177.             facing = facing - 1
  178.             if facing == 0 then facing = 4 end
  179.         end
  180.     end
  181.  
  182.     local function goTo(x2,y2)
  183.         local dx = round(x2-xRel)
  184.         local dy = round(y2-yRel)
  185.        
  186.         if dx == 0 then
  187.             -- don't bother turning
  188.         elseif dx > 0 then
  189.             face(1)
  190.         else
  191.             face(3)
  192.         end
  193.        
  194.         for n = 1,math.abs(dx) do
  195.             tryDig()
  196.             tryForward()
  197.         end
  198.        
  199.         if dy == 0 then
  200.             -- don't turn
  201.         elseif dy > 0 then
  202.             face(4)
  203.         else
  204.             face(2)
  205.         end
  206.        
  207.         for n = 1,math.abs(dy) do
  208.             tryDig()
  209.             turtle.forward()
  210.         end
  211.        
  212.         xRel = xRel + dx
  213.         yRel = yRel + dy
  214.     end
  215.  
  216.     print( "Building dome..." )
  217.  
  218.     for h=1,radius do
  219.         -- r is the "local" radius for height "h"
  220.         local r = round(radius * math.cos((h-1)/radius * math.pi/2))
  221.         print('r='..r)
  222.         tryDigUp()
  223.         turtle.up()
  224.        
  225.         -- Plot circle in increments of angle theta
  226.         local theta = math.atan(1/r)
  227.        
  228.         for c = 0,(2*math.pi / theta) do
  229.             xNew = r - round(r*math.cos(c*theta))
  230.             yNew = round(r*math.sin(c*theta))
  231.             goTo(xNew,yNew)
  232.             turtle.placeDown()
  233.         end
  234.     end
  235.  
  236. print( "Work complete..." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement