Advertisement
jille_Jr

CC: 9 by 9 house builder

Oct 24th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.87 KB | None | 0 0
  1. -- 9 by 9 house
  2. -- by jag_e_nummer_ett
  3.  
  4. local tArgs = {...}
  5.  
  6. function table_contains(table, element)
  7.   for _, value in pairs(table) do
  8.     if value == element then
  9.       return true
  10.     end
  11.   end
  12.   return false
  13. end
  14.  
  15. function printUsage()
  16.   print("Usage: "..shell.getRunningProgram().." <option> [setting]")
  17.   print("Options:")
  18.   print("- 0: Door in front of turtle")
  19.   print("- 1: Left corner in front of turtle")
  20.   print("- 2: Include a bridge")
  21.   print("Settings:")
  22.   print("- Bridge length\n + (Requres Option to be 2)")
  23. end
  24.  
  25. function selectItem()
  26.   for a = 1, 16 do
  27.     if turtle.getItemCount(a) > 0 then
  28.       turtle.select(a)
  29.       break
  30.     end
  31.   end
  32. end
  33.  
  34. function goUp()
  35.   while not turtle.up() do
  36.     if not turtle.digUp() then
  37.       turtle.attackUp()
  38.     end
  39.     sleep(.5)
  40.   end
  41. end
  42.  
  43. function goDown()
  44.   while not turtle.down() do
  45.     if not turtle.digDown() then
  46.       turtle.attackDown()
  47.     end
  48.     sleep(.5)
  49.   end
  50. end
  51.  
  52. function buildBelow()
  53.   if not turtle.detectDown() then
  54.     selectItem()
  55.     while not turtle.placeDown() do
  56.       if not turtle.digDown() then
  57.         turtle.attackDown()
  58.       end
  59.       sleep(.5)
  60.       selectItem()
  61.     end
  62.   end
  63. end
  64.  
  65. function buildFront()
  66.   if not turtle.detect() then
  67.     while not turtle.place() do
  68.       if not turtle.dig() then
  69.         turtle.attack()
  70.       end
  71.       sleep(.5)
  72.       selectItem()
  73.     end
  74.   end
  75. end
  76.  
  77. function buildTop()
  78.   if not turtle.detectUp() then
  79.     while not turtle.placeUp() do
  80.       if not turtle.digUp() then
  81.         turtle.attackUp()
  82.       end
  83.       sleep(.5)
  84.       selectItem()
  85.     end
  86.   end
  87. end
  88.  
  89. function goForward()
  90.   while not turtle.forward() do
  91.     if not turtle.dig() then
  92.       turtle.attack()
  93.     end
  94.     sleep(.5)
  95.   end
  96. end
  97.  
  98. function bridge(count)
  99.   count = tonumber(count)
  100.   if count < 1 then
  101.     return false
  102.   end
  103.   turtle.turnLeft()
  104.   goForward()
  105.   turtle.turnRight()
  106.   goForward()
  107.   for b = 1, count do
  108.     if b % 2 == 0 then
  109.       turtle.turnLeft()
  110.     else
  111.       turtle.turnRight()
  112.     end
  113.     for e = 1, 3 do
  114.       buildBelow()
  115.       if e ~= 3 then
  116.         goForward()
  117.       end
  118.     end
  119.     if b % 2 == 0 then
  120.       turtle.turnRight()
  121.       if b == count then
  122.         turtle.turnRight()
  123.         goForward()
  124.       end
  125.       goForward()
  126.     else
  127.       turtle.turnLeft()
  128.       goForward()
  129.       if b == count then
  130.         turtle.turnLeft()
  131.         goForward()
  132.         for aa = 1,2 do
  133.           turtle.turnLeft()
  134.         end
  135.       end
  136.     end
  137.   end
  138. end
  139.  
  140. function houseFloor()
  141.   for h = 1, 9 do
  142.     if h % 2 == 0 then
  143.       for j = 1, 9 do
  144.         buildBelow()
  145.         if j ~= 9 then
  146.           goForward()
  147.         end
  148.       end
  149.       if h ~= 9 then
  150.         turtle.turnRight()
  151.         goForward()
  152.         turtle.turnRight()
  153.       end
  154.     else
  155.       for i = 1, 9 do
  156.         buildBelow()
  157.         if i ~= 9 then
  158.           goForward()
  159.         end
  160.       end
  161.       if h ~= 9 then
  162.         turtle.turnLeft()
  163.         goForward()
  164.         turtle.turnLeft()
  165.       end
  166.     end
  167.   end
  168.   for aj = 1, 2 do
  169.     turtle.turnLeft()
  170.   end
  171. end
  172.  
  173. function houseWall(door)
  174.   for t = 1, 4 do
  175.     for u = 1, 8 do
  176.       if t == 2 and
  177.       u == 5 and
  178.       door == true then
  179.         goForward()
  180.       else
  181.         buildBelow()
  182.         goForward()
  183.       end
  184.     end
  185.     turtle.turnLeft()
  186.   end
  187. end
  188.  
  189. function houseRoof()
  190.   local skyLights = {
  191.     3,4,
  192.     6,7,
  193.   }
  194.   for y = 1,9 do
  195.     for x = 1,9 do
  196.       if not table_contains(skyLights,x) or
  197.       not table_contains(skyLights,y) then
  198.         buildBelow()
  199.       end
  200.       if x ~= 9 then
  201.         goForward()
  202.       else
  203.         if y % 2 == 0 and y ~= 9 then
  204.           turtle.turnRight()
  205.           goForward()
  206.           turtle.turnRight()
  207.         elseif y % 2 ~= 0 and y ~= 9 then
  208.           turtle.turnLeft()
  209.           goForward()
  210.           turtle.turnLeft()
  211.         end
  212.       end
  213.     end
  214.   end
  215. end
  216.  
  217. function buildHouse(ac)
  218.   if ac == 0 then
  219.     turtle.turnRight()
  220.   end
  221.   for ad = 1,4 do
  222.     goForward()
  223.   end
  224.   turtle.turnLeft()
  225.   houseFloor()
  226.   goUp()
  227.   for ae = 1,2 do
  228.     houseWall(true)
  229.     goUp()
  230.   end
  231.   for ae = 1,3 do
  232.     houseWall()
  233.     goUp()
  234.   end
  235.   houseRoof()
  236.   goForward()
  237.   for af = 1,6 do
  238.     goDown()
  239.   end
  240.   turtle.turnRight()
  241.   for ag = 1,4 do
  242.     goForward()
  243.   end
  244.   turtle.turnRight()
  245.   goForward()
  246.   for ah = 1,2 do
  247.     turtle.turnRight()
  248.   end
  249. end
  250.  
  251. if #tArgs < 1 then
  252.   printUsage()
  253.   return
  254. end
  255.  
  256. tArgs[1] = tonumber(tArgs[1])
  257. tArgs[2] = tonumber(tArgs[2])
  258.  
  259. if tArgs[1] == 0 then
  260.   buildHouse(0)
  261. elseif tArgs[1] == 1 then
  262.   turtle.turnLeft()
  263.   for ab = 1,4 do
  264.     goForward()
  265.   end
  266.   buildHouse(1)
  267. elseif tArgs[1] == 2 then
  268.   if type(tArgs[2]) ~= "number" then
  269.     printUsage()
  270.     return
  271.   else
  272.     bridge(tArgs[2])
  273.     buildHouse(1)
  274.   end
  275. else
  276.   printUsage()
  277.   return
  278. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement