Advertisement
nainmagic

sphere

Jul 26th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.11 KB | None | 0 0
  1. -- v. 1.0 : Build a sphere with radius r
  2.  
  3. local tArgs = {...}
  4. if #tArgs < 1 then
  5.     print("Usage : sphere <radius> \n        sphere <radius> <wallSlotId>")
  6. end
  7.  
  8. local radius = tonumber(tArgs[1])
  9. local slot = 0
  10. if #tArgs > 1 then
  11.     slot = tonumber(tArgs[2])
  12. end
  13.  
  14. local FRONT = 0
  15. local LEFT = 1
  16. local BACK = 2
  17. local RIGHT = 3
  18.  
  19. local x = 0
  20. local y = 0
  21. local z = -radius
  22. local direction = 0
  23.  
  24. -- Fuel methods
  25.  
  26. local function checkFuel()
  27.     local minFuelLevel = math.abs(x) + math.abs(y) + math.abs(z)
  28.  
  29.     if turtle.getFuelLevel() <= minFuelLevel then
  30.         refuel()
  31.     end
  32.  
  33.     if turtle.getFuelLevel() <= minFuelLevel then
  34.         waitForFuel()
  35.     end
  36. end
  37.  
  38. local function waitForFuel()
  39.     print("Waiting for fuel")
  40.  
  41.     while refuel() == false do
  42.         os.sleep(1)
  43.     end
  44. end
  45.  
  46. local function refuel()
  47.     local fueled = false
  48.     for i=1,16 do
  49.         if i ~= slot then
  50.             turtle.select(i)
  51.             fueled = fueled or turtle.refuel()
  52.         end
  53.     end
  54.     return fueled
  55. end
  56.  
  57. -- Inventory methods
  58.  
  59. local function checkItems()
  60.     if slot ~= 0 and turtle.getItemCount(slot) < 5 then
  61.        
  62.         for i=1,16 do
  63.             if i ~= slot then
  64.                 turtle.select(i)
  65.                 if turtle.compareTo(slot) then
  66.                     turtle.transferTo(slot)
  67.                 end
  68.             end
  69.         end
  70.  
  71.         local wallItemMessageShown = false
  72.         while turtle.getItemCount(slot) < 5 do
  73.             os.sleep(1)
  74.             if wallItemMessageShown == false then
  75.                 print("Waiting for more wall items")
  76.                 wallItemMessageShown = true
  77.             end
  78.         end
  79.     end
  80.  
  81.     while true do
  82.         local messageShown = false
  83.    
  84.         for i=1,16 do
  85.             if i ~= slot then
  86.                 if turtle.getItemSpace(i) > 5 then
  87.                     return
  88.                 end
  89.             end
  90.         end
  91.        
  92.         if messageShown == false then
  93.             print("Please empty some items")
  94.             messageShown = true
  95.         end    
  96.  
  97.         os.sleep(1)
  98.     end
  99. end
  100.  
  101. -- Move method
  102.  
  103. local function setDirection(targetDirection)
  104.     local testDirection = direction
  105.     local moveCountLeft = 0
  106.  
  107.     while testDirection ~= targetDirection do
  108.         testDirection = testDirection + 1
  109.         if testDirection > 3 then
  110.             testDirection = 0
  111.         end
  112.         moveCountLeft = moveCountLeft + 1
  113.     end
  114.  
  115.     testDirection = direction
  116.     local moveCountRight = 0
  117.     while testDirection ~= targetDirection do
  118.         testDirection = testDirection - 1
  119.         if testDirection < 0 then
  120.             testDirection = 3
  121.         end
  122.         moveCountRight = moveCountRight + 1
  123.     end
  124.  
  125.     if moveCountLeft < moveCountRight then
  126.         for i=1,moveCountLeft do
  127.             turtle.turnLeft()
  128.         end
  129.     else
  130.         for i=1,moveCountRight do
  131.             turtle.turnRight()
  132.         end    
  133.     end
  134.  
  135.     direction = targetDirection
  136. end
  137.  
  138. local function moveUp()
  139.     checkFuel()
  140.     checkItems()
  141.  
  142.     turtle.digUp()
  143.     turtle.up()
  144. end
  145.  
  146. local function moveDown()
  147.     checkFuel()
  148.     checkItems()
  149.  
  150.     turtle.digDown()
  151.     turtle.down()
  152. end
  153.  
  154. local function moveForward()
  155.     checkFuel()
  156.     checkItems()
  157.  
  158.     while turtle.forward() == false do
  159.         turtle.dig()
  160.         os.sleep(1)
  161.     end
  162. end
  163.  
  164. local function goto(targetX,targetY,targetZ)
  165.     local currentX = x
  166.     local currentY = y
  167.     local currentZ = z
  168.    
  169.    
  170.  
  171.     if currentZ > targetZ then
  172.         while currentZ > targetZ do
  173.             moveDown()
  174.             currentZ = currentZ - 1
  175.         end
  176.     else
  177.         while currentZ < targetZ do
  178.             moveUp()
  179.             currentZ = currentZ + 1
  180.         end
  181.     end
  182.    
  183.     local doXFirst = math.abs(x) > math.abs(y)
  184.     for i=1,2 do
  185.         if (doXFirst and i == 1) or i == 2 then
  186.             if currentX > targetX then
  187.                 setDirection(RIGHT)
  188.                 while currentX > targetX do
  189.                     moveForward()
  190.                     currentX = currentX - 1
  191.                 end
  192.             else
  193.                 setDirection(LEFT)
  194.                 while currentX < targetX do
  195.                     moveForward()
  196.                     currentX = currentX + 1
  197.                 end
  198.             end
  199.         end
  200.  
  201.         if (doXFirst and i == 2) or i == 1 then
  202.             if currentY > targetY then
  203.                 setDirection(BACK)
  204.                 while currentY > targetY do
  205.                     moveForward()
  206.                     currentY = currentY - 1
  207.                 end
  208.             else
  209.                 setDirection(FRONT)
  210.                 while currentY < targetY do
  211.                     moveForward()
  212.                     currentY = currentY + 1
  213.                 end
  214.             end
  215.         end
  216.     end
  217.  
  218.     x = targetX
  219.     y = targetY
  220.     z = targetZ
  221. end
  222.  
  223. -- Drawing methods
  224.  
  225. local function testIsInShape(targetX,targetY,targetZ)
  226.     local localRadius = radius - math.abs(targetZ)
  227.     local targetRadius = math.abs(targetX) + math.abs(targetY)
  228.  
  229.     return targetRadius <= localRadius
  230. end
  231.  
  232. local function checkWall()
  233.     if slot ~= 0 then
  234.         turtle.select(slot)
  235.  
  236.         if testIsInShape(x, y + 1, z) == false then
  237.             setDirection(FRONT)
  238.             if turtle.compare(slot) == false then
  239.                 turtle.dig()
  240.                 turtle.place()
  241.             end
  242.         end
  243.  
  244.         if testIsInShape(x + 1, y, z) == false then
  245.             setDirection(LEFT)
  246.             if turtle.compare(slot) == false then
  247.                 turtle.dig()
  248.                 turtle.place()
  249.             end
  250.         end
  251.  
  252.         if testIsInShape(x, y - 1, z) == false then
  253.             setDirection(BACK)
  254.             if turtle.compare(slot) == false then
  255.                 turtle.dig()
  256.                 turtle.place()
  257.             end
  258.         end
  259.  
  260.         if testIsInShape(x - 1, y, z) == false then
  261.             setDirection(RIGHT)
  262.             if turtle.compare(slot) == false then
  263.                 turtle.dig()
  264.                 turtle.place()
  265.             end
  266.         end
  267.     end
  268. end
  269.  
  270. -- Render
  271. for i=-radius,radius do
  272.     goto(0,0,i)
  273.     for j=-radius,radius do
  274.         for k=-radius,radius do
  275.             local yValue = k
  276.             if j % 2 == 0 then
  277.                 yValue = -k
  278.             end
  279.            
  280.             if testIsInShape(j,yValue,i) then
  281.                 goto(j,yValue,i)
  282.                 checkWall()
  283.             end
  284.         end
  285.     end
  286. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement