Advertisement
shirkit

Room

Apr 12th, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.38 KB | None | 0 0
  1. local tArgs = {...}
  2. local length = tonumber(tArgs[1]) or 0
  3. local width = tonumber(tArgs[2]) or 0
  4. local height = tonumber(tArgs[3]) or 0
  5. local slot = 0
  6.  
  7. local qu_x = 1
  8. local qu_y = 0
  9. local qu_z = 1
  10. local facing = 2
  11.  
  12. --[[
  13. 13 - pavement
  14. 14 - walls
  15. 15 - roof
  16. ]]--
  17.  
  18.  
  19.  
  20. function move (dir)
  21.     if dir == "up" then
  22.         if turtle.up() then
  23.             qu_y = qu_y + 1
  24.             return true
  25.         else
  26.             return false
  27.         end
  28.     elseif dir == "down" then
  29.         if turtle.down() then
  30.             qu_y = qu_y - 1
  31.             return true
  32.         else
  33.             return false
  34.         end
  35.     elseif dir == "north" or dir == "south" or dir == "west" or dir == "east" then
  36.         turnTo(dir)
  37.         local moved = turtle.forward()
  38.         if moved then
  39.             if dir == "north" then qu_z = qu_z + 1 elseif dir == "south" then qu_z = qu_z - 1 elseif dir == "west" then qu_x = qu_x - 1 elseif dir == "east" then qu_x = qu_x + 1 end
  40.         return true
  41.         else
  42.             return false
  43.         end
  44.     end
  45.     return false
  46. end
  47.  
  48. function turn(direction)
  49.     if direction == "left" or direction == "right" then
  50.         local d = 0
  51.         if direction == "left" then d = 1; turtle.turnLeft() else d = -1; turtle.turnRight() end
  52.         facing = facing + d
  53.         if (facing == -1) then facing = 3 elseif (facing == 4) then facing = 0 end
  54.         return true
  55.     end
  56.     return false
  57. end
  58.  
  59. -- Function turns to the specified facing location.
  60. function turnToN (face)
  61.     if face == facing then return true end
  62.     local lOrR = (facing - face + 4) % 4
  63.     if lOrR > 2 then
  64.         turn("left")
  65.     elseif lOrR < 2 then
  66.         turn("right")
  67.     else
  68.         turn("left")
  69.         turn("left")
  70.     end
  71.     return true
  72. end
  73. function turnTo (nesw)
  74.     if nesw == "north" then
  75.         turnToN(0)
  76.     elseif nesw == "south" then
  77.         turnToN(2)
  78.     elseif nesw == "west" then
  79.         turnToN(1)
  80.     elseif nesw == "east" then
  81.         turnToN(3)
  82.     end
  83. end
  84.  
  85. function start()
  86.     turtle.select(1)
  87.     buildRoof()
  88.     print("x"..qu_x)
  89.     print("z"..qu_z)
  90. end
  91.  
  92. function spinAndPlace(dir,s)
  93.     turtle.turnRight()
  94.     turtle.turnRight()
  95.     place(dir,s)
  96. end
  97.  
  98. function buildRoof()
  99.     turnTo("north")
  100.     for x = 1, width, 1 do
  101.         for z = 1, length - 1, 1 do
  102.             if ( x % 2 == 1 ) then
  103.                 move("south")
  104.             else
  105.                 move("north")
  106.             end
  107.             spinAndPlace("front",15)
  108.         end
  109.        
  110.         move("west")
  111.         spinAndPlace("front",15)
  112.        
  113.     end
  114. end
  115.  
  116. function place(dir, s)
  117.     turtle.select(s)
  118.     if turtle.getItemCount(s) > 1 or s == 16 then
  119.         if dir == "up" then
  120.             turtle.placeUp()
  121.         elseif dir == "down" then
  122.             turtle.placeDown()
  123.         else
  124.             turtle.place()
  125.         end
  126.     else
  127.         local didTransfer = false
  128.         for i=1,12,1 do
  129.             if  i ~= s and turtle.compareTo(i)then
  130.                 turtle.select(i)
  131.                 didTransfer = didTransfer or turtle.transferTo(s)
  132.                 turtle.select(s)
  133.             end
  134.         end
  135.        
  136.         if didTransfer then
  137.             turtle.select(s)
  138.             place(dir,s)
  139.         else
  140.             restock()
  141.             place(dir,s)
  142.         end
  143.     end
  144. end
  145.  
  146. function suck(dir)
  147.     if dir == "up" then
  148.         turtle.suckUp()
  149.     elseif dir == "down" then
  150.         turtle.suckDown()
  151.     else
  152.         turtle.suck()
  153.     end
  154. end
  155.  
  156. function dig(dir)
  157.     if dir == "up" then
  158.         turtle.digUp()
  159.     elseif dir == "down" then
  160.         turtle.digDown()
  161.     else
  162.         turtle.dig()
  163.     end
  164. end
  165.  
  166. function restock()
  167.     turtle.select(16)
  168.     local dir = ""
  169.     if not turtle.detectUp() then
  170.         dir = "up"
  171.     elseif not turtle.detect() then
  172.         dir = "front"
  173.     else
  174.         dir = "down"
  175.         turtle.digDown()
  176.         turtle.placeDown()
  177.     end
  178.     dig(dir)
  179.     place(dir,16)
  180.     turtle.select(1)
  181.     for i=1,3 do
  182.         suck(dir)
  183.     end
  184.  
  185.     turtle.select(16)
  186.     turtle.drop()
  187.     dig(dir)
  188. end
  189.  
  190. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement