Advertisement
psychedelixx

Minecraft Turtle: Circle

Jan 8th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.35 KB | None | 0 0
  1.     --[[
  2.     2015 (c) psychedelixx
  3.     Minecraft Turtle: Circle
  4.     2015-01-08
  5.  
  6.     Builds a circle
  7.  
  8.     Robust API:
  9.     http://computercraft.info/wiki/Robust_Turtle_API
  10.  
  11.     Usage:
  12.     - use turtle and type "label set <name>"
  13.     (to give your turtle an unique name so it remembers its programs)
  14.     - type "pastebin get wgFfCNUq circle"
  15.     - place blocks in turtle inventory
  16.     - type "circle <radius>"
  17.     ]]
  18.  
  19.    
  20. -------------------------------------------------------------------------------------
  21. -- INIT -----------------------------------------------------------------------------
  22. -------------------------------------------------------------------------------------
  23. slot = 1
  24. posX = 0
  25. posY = 0
  26. posH = 0
  27.  
  28. height = 1
  29. debug = 0
  30. x = {}
  31. y = {}
  32.  
  33. local args = { ... }
  34. if #args < 1 or #args > 3 then
  35.     print("")
  36.     print("circle <radius> [<height>] [<debug {0|1}>]")
  37.     print("place items in inventory")
  38.     print("")
  39.  
  40.     error()
  41. end
  42.  
  43. radius = tonumber(args[1])
  44.  
  45. if radius > 100 or radius < 1 then
  46.     print("")
  47.     print("Are you mad? I won't do this...")
  48.     print("")
  49.     error()
  50. end
  51.  
  52. if #args == 2 then
  53.     height = tonumber(args[2])
  54. end
  55.  
  56. if #args == 3 then
  57.     debug = tonumber(args[2])
  58. end
  59.  
  60. -------------------------------------------------------------------------------------
  61. -- ROUND ----------------------------------------------------------------------------
  62. -------------------------------------------------------------------------------------
  63.  
  64. function round(num, idp)
  65.   local mult = 10^(idp or 0)
  66.   return math.floor(num * mult + 0.5) / mult
  67. end
  68.  
  69. -------------------------------------------------------------------------------------
  70. -- CALC CIRCLE ----------------------------------------------------------------------
  71. -------------------------------------------------------------------------------------
  72.  
  73. function calcCircle()
  74.     d_ = -radius
  75.     x_ = radius
  76.     y_ = 0
  77.    
  78.     -- 1/8 -------------------
  79.     while y_ <= x_ do
  80.         table.insert(x, x_)
  81.         table.insert(y, y_)
  82.        
  83.         d_ = d_ + 2 * x_ * y_ + 1;
  84.         y_ = y_ + 1;
  85.        
  86.         if d_ > 0 then
  87.             d_ = d_ - 2 * x_ * x_ + 2;
  88.             x_ = x_ - 1;
  89.         end
  90.     end
  91.    
  92.     length = table.getn(x)
  93.    
  94.     -- 2/8 -------------------
  95.     for i = length, 1, -1 do
  96.         table.insert(x, y[i])
  97.         table.insert(y, x[i])
  98.     end
  99.    
  100.     -- 3/8 -------------------
  101.     for i = 1, length, 1 do
  102.         table.insert(x, -y[i])
  103.         table.insert(y, x[i])
  104.     end
  105.    
  106.     -- 4/8 -------------------
  107.     for i = length, 1, -1 do
  108.         table.insert(x, -x[i])
  109.         table.insert(y, y[i])
  110.     end
  111.    
  112.     -- 5/8 -------------------
  113.     for i = 1, length, 1 do
  114.         table.insert(x, -x[i])
  115.         table.insert(y, -y[i])
  116.     end
  117.    
  118.     -- 6/8 -------------------
  119.     for i = length, 1, -1 do
  120.         table.insert(x, -y[i])
  121.         table.insert(y, -x[i])
  122.     end
  123.    
  124.     -- 7/8 -------------------
  125.     for i = 1, length, 1 do
  126.         table.insert(x, y[i])
  127.         table.insert(y, -x[i])
  128.     end
  129.    
  130.     -- 8/8 -------------------
  131.     for i = length, 1, -1 do
  132.         table.insert(x, x[i])
  133.         table.insert(y, -y[i])
  134.     end
  135. end
  136.  
  137. -------------------------------------------------------------------------------------
  138. -- CALC RESOURCES -------------------------------------------------------------------
  139. -------------------------------------------------------------------------------------
  140.  
  141. function calcResources()
  142.     count = 0
  143.     for i=2, table.getn(x) do
  144.         if not (x[i] == x[i-1] and y[i] == y[i-1]) then
  145.             count = count + 1
  146.         end
  147.     end
  148.     return count
  149. end
  150.  
  151. -------------------------------------------------------------------------------------
  152. -- CHECK RESOURCES ------------------------------------------------------------------
  153. -------------------------------------------------------------------------------------
  154.  
  155. function checkResources()
  156.     repeat
  157.         overwrite = 0
  158.         sumItems = 0
  159.         for i=1, 16, 1 do
  160.             sumItems = sumItems + turtle.getItemCount(i)
  161.         end      
  162.        
  163.         if sumItems < calcResources() then
  164.             print("Not enough Resources for this layer")
  165.             print("I need " .. (calcResources() - sumItems) .. " more blocks (" .. calcResources() .. " total)")
  166.             print()
  167.             print("Fill up and press enter")
  168.             print("or type in '1' to start anyway")
  169.             overwrite = read()
  170.         end
  171.        
  172.         if overwrite == '1' then
  173.             break
  174.         end
  175.     until sumItems >= calcResources()
  176. end
  177.  
  178. -------------------------------------------------------------------------------------
  179. -- PLACE ----------------------------------------------------------------------------
  180. -------------------------------------------------------------------------------------
  181.  
  182. function place()
  183.     if posX == x[i] and posY == y[i] and not(x[i] == x[i-1] and y[i] == y[i-1]) then
  184.         if debug == 1 then
  185.             print(posH .. ' # ' .. i .. ' # ' .. x[i] .. ' # ' .. y[i] .. ' # ' .. x[i+1] .. ' # ' .. y[i+1] .. ' # ' .. posX .. ' # ' .. posY)
  186.         else
  187.             print(round(((posH*table.getn(x) + i)/table.getn(x)-1)/height*100, 1) .. " %")
  188.         end
  189.         while turtle.getItemCount(slot) == 0 and slot < 16 do
  190.             slot = slot + 1
  191.         end
  192.         if turtle.getItemCount(slot) == 0 and slot == 16 then
  193.             print('Please put items into slot 1 and press enter')
  194.             read()
  195.             slot = 1
  196.         end
  197.         turtle.select(slot)
  198.         turtle.digDown()
  199.         turtle.placeDown()
  200.     end
  201. end
  202.  
  203. -------------------------------------------------------------------------------------
  204. -- MOVE -----------------------------------------------------------------------------
  205. -------------------------------------------------------------------------------------
  206.  
  207. function move(var, op, turn)
  208.     if turn == 'lr' then
  209.         t.left()
  210.         t.forward()
  211.         t.right()
  212.     elseif turn == 'rl' then
  213.         t.right()
  214.         t.forward()
  215.         t.left()
  216.     elseif turn == 'no' then
  217.         t.forward()
  218.     end
  219.    
  220.     if op == 'inc' then
  221.         var = var + 1
  222.     elseif op == 'dec' then
  223.         var = var - 1
  224.     end
  225.     return var
  226. end
  227.  
  228. -------------------------------------------------------------------------------------
  229. -- MAIN -----------------------------------------------------------------------------
  230. -------------------------------------------------------------------------------------
  231.  
  232. calcCircle()
  233.  
  234. if turtle.getFuelLevel() < calcResources()*height then
  235.     print("I need " .. math.ceil(calcResources()*height - turtle.getFuelLevel()) .. " more fuel!")
  236. else
  237.     print("======== 2015 (c) psychedelixx ========")
  238.     print("Let's go!")    
  239.     print("Building a circle...")
  240.     print("")
  241.    
  242.     checkResources()
  243.     t.forward(radius)
  244.     posX = posX + radius
  245.     t.left()
  246.    
  247.     while posH < height do
  248.    
  249.         if posH > 0 then
  250.             checkResources()
  251.         end
  252.        
  253.         posH = posH + 1
  254.        
  255.         t.up()
  256.         i = 1
  257.        
  258.         print("Building layer " .. posH .. " of " .. height)
  259.        
  260.         -- 1/8 --------------------------------------------
  261.         while y[i] < x[i] do
  262.             place()
  263.            
  264.             if y[i] < y[i+1] then
  265.                 posY = move(posY, 'inc', 'no')
  266.             end
  267.             if x[i] > x[i+1] then
  268.                 posX = move(posX, 'dec', 'lr')
  269.             end
  270.                
  271.             i = i+1
  272.         end
  273.        
  274.         t.left()
  275.        
  276.         -- 2/8 --------------------------------------------
  277.         while x[i] > 0 do
  278.             place()
  279.            
  280.             if y[i] < y[i+1] then
  281.                 posY = move(posY, 'inc', 'rl')
  282.             end
  283.             if x[i] > x[i+1] then
  284.                 posX = move(posX, 'dec', 'no')
  285.             end
  286.                
  287.             i = i+1
  288.         end
  289.        
  290.         -- 3/8 --------------------------------------------
  291.         while math.abs(x[i]) < y[i] do
  292.             place()
  293.            
  294.             if y[i] > y[i+1] then
  295.                 posY = move(posY, 'dec', 'lr')
  296.             end
  297.             if x[i] > x[i+1] then
  298.                 posX = move(posX, 'dec', 'no')
  299.             end
  300.                
  301.             i = i+1
  302.         end
  303.        
  304.         t.left()
  305.        
  306.         -- 4/8 --------------------------------------------
  307.         while y[i] > 0 do
  308.             place()
  309.            
  310.             if y[i] > y[i+1] then
  311.                 posY = move(posY, 'dec', 'no')
  312.             end
  313.             if x[i] > x[i+1] then
  314.                 posX = move(posX, 'dec', 'rl')
  315.             end
  316.                
  317.             i = i+1
  318.         end
  319.        
  320.         -- 5/8 --------------------------------------------
  321.         while y[i] < x[i] do
  322.             place()
  323.            
  324.             if y[i] > y[i+1] then
  325.                 posY = move(posY, 'dec', 'no')
  326.             end
  327.             if x[i] < x[i+1] then
  328.                 posX = move(posX, 'inc', 'lr')
  329.             end
  330.                
  331.             i = i+1
  332.         end
  333.        
  334.         t.left()
  335.        
  336.         -- 6/8 --------------------------------------------
  337.         while x[i] < 0 do
  338.             place()
  339.            
  340.             if y[i] > y[i+1] then
  341.                 posY = move(posY, 'dec', 'rl')
  342.             end
  343.             if x[i] < x[i+1] then
  344.                 posX = move(posX, 'inc', 'no')
  345.             end
  346.                
  347.             i = i+1
  348.         end
  349.        
  350.         -- 7/8 --------------------------------------------
  351.         while math.abs(y[i]) > x[i] do
  352.             place()
  353.            
  354.             if y[i] < y[i+1] then
  355.                 posY = move(posY, 'inc', 'lr')
  356.             end
  357.             if x[i] < x[i+1] then
  358.                 posX = move(posX, 'inc', 'no')
  359.             end
  360.                
  361.             i = i+1
  362.         end
  363.        
  364.         t.left()
  365.        
  366.         -- 8/8 --------------------------------------------
  367.         while y[i] < 0 do
  368.             place()
  369.            
  370.             if y[i] < y[i+1] then
  371.                 posY = move(posY, 'inc', 'no')
  372.             end
  373.             if x[i] < x[i+1] then
  374.                 posX = move(posX, 'inc', 'rl')
  375.             end
  376.                
  377.             i = i+1
  378.         end
  379.     end
  380.    
  381.     print("Finished")
  382. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement