MrCervelo

9x9 Builder

Jan 24th, 2013
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.73 KB | None | 0 0
  1. function buildLayer()
  2.   moveUp()
  3.   for i=1,4 do
  4.     turtle.placeDown()
  5.     checkBuildingSupply()
  6.     for j=1,8 do
  7.       moveForward()
  8.       turtle.placeDown()
  9.       checkBuildingSupply()
  10.     end
  11.     turtle.turnRight()
  12.   end
  13. end
  14.  
  15. function checkBuildingSupply()
  16.   if ( turtle.getItemCount(itemSlot) == 0 ) then
  17.     itemSlot = itemSlot + 1
  18.     turtle.select(itemSlot)
  19.   end
  20. end
  21.  
  22. function digFloor()
  23.   for i=1,9 do
  24.     for j=1,8 do
  25.       moveForward()
  26.     end
  27.     if ( i % 2 == 1 ) then
  28.       if ( i ~= 9 ) then
  29.         turtle.turnRight()
  30.         moveForward()
  31.         turtle.turnRight()
  32.       end
  33.     else
  34.       turtle.turnLeft()
  35.       moveForward()
  36.       turtle.turnLeft()
  37.     end
  38.   end
  39. end
  40.  
  41. function moveForward()
  42.   while ( not turtle.forward() ) do
  43.     turtle.dig()
  44.   end
  45. end
  46.  
  47. function moveUp()
  48.   while ( not turtle.up() ) do
  49.     turtle.digUp()
  50.   end
  51. end
  52.  
  53. function moveDown()
  54.   while ( not turtle.down() ) do
  55.     turtle.digDown()
  56.   end
  57. end
  58.  
  59. function digDown()
  60.   while ( turtle.detectDown() ) do
  61.     turtle.digDown()
  62.   end
  63. end
  64.  
  65. function layFloor()
  66.   -- lays the 9x9 floor
  67.   for i=1,9 do
  68.     for j=1,8 do
  69.       digDown()
  70.       turtle.placeDown()
  71.       checkBuildingSupply()
  72.       moveForward()
  73.     end
  74.     digDown()
  75.     turtle.placeDown()
  76.     checkBuildingSupply()
  77.     if ( i%2 == 0 ) then
  78.       turtle.turnLeft()
  79.       moveForward()
  80.       turtle.turnLeft()
  81.     else
  82.       if ( i ~= 9 ) then
  83.         turtle.turnRight()
  84.         moveForward()
  85.         turtle.turnRight()
  86.       end
  87.     end
  88.   end
  89. end
  90.  
  91. function buildRoof()
  92.   --if building from the top down, then turtle stays on roof
  93.   --if building from the bottom or from the sides, needs to do the edges first, otherwise one side can't get done
  94.   --can it be done by a spiral, so that the turtle finishes in the centre block of the newly built house
  95.   design={}
  96.   design[1]={'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'}
  97.   design[2]={'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'}
  98.   design[3]={'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'}
  99.   design[4]={'b', 'b', 'b', 'b', 'b', 'b', 'b'}
  100.   design[5]={'b', 'b', 'b', 'b', 'b', 'b', 'b'}
  101.   design[6]={'b', 'b', 'b', 'b', 'b', 'b'}
  102.   design[7]={'b', 'b', 'b', 'b', 'b', 'b'}
  103.   design[8]={'b', 'b', 'b', 'b', 'b'}
  104.   design[9]={'g', 'g', 'b', 'g', 'g'}
  105.   design[10]={'g', 'b', 'g', 'g'}
  106.   design[11]={'g', 'b', 'g', 'g'}
  107.   design[12]={'g', 'b', 'g'}
  108.   design[13]={'g', 'b', 'g'}
  109.   design[14]={'b', 'g'}
  110.   design[15]={'b', 'g'}
  111.   design[16]={'b'}
  112.   turtle.select(itemSlot)
  113.   moveUp()
  114.   for i=1,table.getn(design) do
  115.     for j=1,table.getn(design[i]) do
  116.       if ( design[i][j] == 'b' ) then
  117.         turtle.placeDown()
  118.         checkBuildingSupply()
  119.       elseif ( design[i][j] == 'g' ) then
  120.         turtle.select(16)
  121.         turtle.placeDown()
  122.         turtle.select(itemSlot)
  123.       end
  124.       if ( j == table.getn(design[i]) ) then
  125.         turtle.turnRight()
  126.       end
  127.       moveForward()
  128.     end
  129.   end
  130.   moveDown()
  131.   moveDown()
  132.   turtle.placeUp()
  133. end
  134.  
  135. function placeTorches()
  136.   moveForward()
  137.   turtle.turnRight()
  138.   moveForward()
  139.   turtle.turnLeft()
  140.   moveDown()
  141.   turtle.select(15)
  142.   for i=1,4 do
  143.     for j=1,6 do
  144.       if ( j % 2 == 0 ) then
  145.              while ( turtle.detectDown() ) do
  146.           turtle.digDown()
  147.         end
  148.         turtle.placeDown()
  149.       end
  150.       moveForward()
  151.     end
  152.     turtle.turnRight()
  153.   end
  154.   moveUp()
  155.   turtle.turnLeft()
  156.   moveForward()
  157.   turtle.turnRight()
  158.   turtle.back()
  159.   turtle.select(itemSlot)
  160. end
  161.  
  162. function checkInventory()
  163.   -- check that there are 306 identical blocks in slots 1-6
  164.   -- check that there are 12 items in slot 15 (torches)
  165.   -- check that there are 16 items in slot 16 (glass)
  166.   blockCount=turtle.getItemCount(1)
  167.   turtle.select(1)
  168.   for i=2,5 do
  169.     if ( turtle.getItemCount(i) == 0 ) then
  170.       print("Insufficient Base Blocks for building, slots 1-5 must contain identical blocks")
  171.       os.exit(1)
  172.     end
  173.     if ( not turtle.compareTo(i) ) then
  174.       print("Blocks in slot 1 - 5 must be identical")
  175.       os.exit(1)
  176.     end
  177.     blockCount = blockCount + turtle.getItemCount(i)
  178.   end
  179.   if ( blockCount < 306 ) then
  180.     print("Insufficient Base Blocks for building")
  181.     os.exit(1)
  182.   end
  183.   if ( turtle.getItemCount(15) < 12 ) then
  184.     print("Insufficient Torches in slot 15")
  185.     os.exit(1)
  186.   end
  187.   if ( turtle.getItemCount(16) < 16 ) then
  188.     print("Insufficient Glass in slot 16")
  189.     os.exit(1)
  190.   end
  191. end
  192.  
  193. function rotate()
  194.   for i=1,2 do
  195.     turtle.turnLeft()
  196.   end
  197. end
  198.  
  199. itemSlot=1
  200. checkInventory()
  201. --moveDown()
  202. --digFloor()
  203. --moveUp()
  204. turtle.select(itemSlot)
  205. --rotate()
  206. layFloor()
  207. rotate()
  208. for i=1,5 do
  209.   buildLayer()
  210. end
  211. placeTorches()
  212. buildRoof()
  213. for i=1,4 do
  214.   moveDown()
  215. end
  216. print("9x9 built")
Advertisement
Add Comment
Please, Sign In to add comment