Advertisement
zilvar2k11

makeRoom

Jun 15th, 2013
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 KB | None | 0 0
  1. local tArgs = {...}
  2. if #tArgs ~= 3 then
  3.    print("Usage: makeRoom <x> <y> <z>")
  4.    return
  5. end
  6.  
  7. if (tonumber(tArgs[3]) < 3) then tArgs[3] = "3" end
  8.  
  9. -- Local Variables
  10. local movesNeeded = tonumber(tArgs[1])*tonumber(tArgs[2])*(tonumber(tArgs[3]/3))
  11. local xPos, yPos, xyDir
  12.  
  13. print("Fuel Needed for this room: "..movesNeeded)
  14.  
  15. if (turtle.getFuelLevel() < movesNeeded) then
  16.   print("Need more fuel.")
  17.   print("Current Fuel Levels: "..turtle.getFuelLevel())
  18.   return
  19. end
  20.  
  21. local function updatePos()
  22.   -- xyDir 0 is forward, relative to start (x++)
  23.   -- xyDir 1 is right, relative to start (y++)
  24.   -- xyDir 2 is back, relative to start (x--)
  25.   -- xyDir 3 is left, relative to start (y--)
  26.   if xyDir == 0 then
  27.      xPos = xPos + 1
  28.   elseif xyDir == 1 then
  29.      yPos = yPos +1
  30.   elseif xyDir == 2 then
  31.      xPos = xPos - 1
  32.   else yPos = yPos - 1
  33.   end
  34. end
  35.  
  36. local function changeDir( lorr )
  37.   if lorr == "L" then
  38.     xyDir = xyDir - 1
  39.     if xyDir == -1 then xyDir = 3 end
  40.   else
  41.     xyDir = xyDir + 1
  42.     if xyDir == 4 then xyDir = 0 end
  43.   end
  44. end
  45.  
  46. local function turnRight()
  47.   turtle.turnRight()
  48.   changeDir("R")
  49. end
  50.  
  51. local function turnLeft()
  52.   turtle.turnLeft()
  53.   changeDir("L")
  54. end
  55.  
  56. local function dforward()
  57.   while turtle.detect() == true do
  58.     turtle.dig()
  59.     sleep(0.1)
  60.   end
  61.   turtle.forward()
  62.   updatePos()
  63. end
  64.  
  65. local function goToStart()
  66.   if xyDir == 0 then
  67.      turnLeft()
  68.   end
  69.   if xyDir == 1 then
  70.      turnRight()
  71.   end
  72.   while (xPos > 1) or (yPos > 0) do
  73.     print(xyDir)
  74.     if (xyDir == 2) and (xPos >1) then
  75.       dforward()
  76.     elseif (xPos == 1) and (xyDir == 2) then
  77.       turnRight()
  78.     elseif (xyDir == 3) and (yPos >0) then
  79.       dforward()
  80.     elseif (xyDir == 3) and (yPos == 0) then
  81.       turnLeft()
  82.     end
  83.   end
  84. end
  85.  
  86. local function digUD()
  87.   while turtle.detectUp() == true do
  88.     turtle.digUp()
  89.     sleep(0.1)
  90.   end
  91.   turtle.digDown()
  92. end
  93.  
  94. local function digLine( _fnDist )
  95.   local move = 0
  96.   for move = 1, _fnDist do
  97.     digUD()
  98.     dforward()
  99.   end
  100. end
  101.  
  102. local function digRoomLL ( _x, _y )
  103.   local x1 = _x;
  104.   local y1 = _y;
  105.   while (x1>0) and (y1>0) do
  106.     digLine(x1)
  107.     turnRight()
  108.     digLine(y1-1)
  109.     turnRight()
  110.     digLine(x1-1)
  111.     turnRight()
  112.     digLine(y1-2)
  113.     turnRight()
  114.     x1 = x1 - 2
  115.     y1 = y1 - 2
  116.   end
  117.   digUD()
  118. end
  119.  
  120. function checkGoUp()
  121.   while turtle.detectUp() == true do
  122.     turtle.digUp()
  123.     sleep(0.1)
  124.   end
  125.  
  126.   turtle.up()
  127. end
  128.  
  129. -- program starts here.
  130. -- init variables
  131.  
  132.   local z1 = tonumber(tArgs[3]);
  133.  
  134.   -- Start at (0,0) facting 0
  135.   xPos = 0
  136.   yPos = 0
  137.   xyDir = 0
  138.  
  139. -- move into starting position
  140.   checkGoUp()
  141.   dforward()
  142.  
  143. --vertical logic.  Repeat while counter > 0
  144.  
  145. while ( z1 > 2 ) do
  146.   xPos = 1
  147.   digRoomLL( tonumber(tArgs[1]), tonumber(tArgs[2]) )
  148.   goToStart()
  149.   turnRight()
  150.   turnRight()
  151.   z1 = z1 - 3;
  152.   if (z1>=3) then
  153.     checkGoUp()
  154.     checkGoUp()
  155.     checkGoUp()
  156.   end
  157. end
  158.  
  159. if (z1 == 2) then
  160.     checkGoUp()
  161.     checkGoUp()
  162.     digRoomLL( tonumber(tArgs[1]), tonumber(tArgs[2]) )
  163. end
  164.  
  165. if (z1 == 1) then
  166.    checkGoUp()
  167.    digRoomLL( tonumber(tArgs[1]), tonumber(tArgs[2]) )
  168. end
  169.  
  170.   goToStart()
  171.   for gd = 1,tonumber(tArgs[3]) do
  172.     turtle.down()
  173.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement