Phoenix_Tekkit

chamber

Aug 21st, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.24 KB | None | 0 0
  1. print ("How long should the room be?")
  2. local length = tonumber(io.read())
  3. print ("How wide should the room be?")
  4. local width = tonumber(io.read())
  5. print ("How high should the room be?")
  6. local height = tonumber(io.read())
  7.  
  8. xPos = 0 -- Initalise positions. Positive x = forward from starting direction
  9. yPos = 0 -- Positive y = right from starting position
  10. zPos = 0 -- Positive z = up from starting position
  11. dir = 1  -- initalize direction 1 = ahead, 2 = right, 3 = back, 4 = left, relative to inital position
  12. xMods = {1,0,-1,0} -- index xMods[dir] indicates how x will change on a turtle.forward()
  13. yMods = {0,1,0,-1} --  index xMods[dir] indicates how x will change on a turtle.forward()
  14. fuelThreshold = 10 -- below what fuel level will the turtle refuel itself?
  15.  
  16.  
  17. local function setHeight(targetZ) -- moves the turtle to the target height, bashing any obstructions clear
  18.     while zPos < targetZ do
  19.         if turtle.detectUp() then
  20.             turtle.digUp()
  21.         end
  22.         if turtle.up() then
  23.             zPos = zPos + 1
  24.         end
  25.     end
  26.     while zPos > targetZ do
  27.         if turtle.detectDown() then
  28.             turtle.digDown()
  29.         end
  30.         if turtle.down() then
  31.             zPos = zPos - 1
  32.         end
  33.     end
  34. end
  35.  
  36. local function right() -- turns right once
  37.     turtle.turnRight()
  38.     dir = dir + 1
  39.     if dir > 4 then --makes sure dir is in the 1-4 range
  40.         dir = dir - 4
  41.     end
  42. end
  43.  
  44. local function left() -- turns left once
  45.     turtle.turnLeft()
  46.     dir = dir - 1
  47.     if dir < 1 then --makes sure dir is in the 1-4 range
  48.         dir = dir + 4
  49.     end
  50. end
  51.  
  52.  
  53. local function turnTo(targetDir) -- turns right untl facing the right way (TODO: Find a way of making it turn the shortest way whish isn't just one long list of special cases)
  54.     while  targetDir ~= dir do
  55.         right()
  56.     end
  57. end
  58.  
  59. local function checkFuel() -- ensure the turtle as enough fuel
  60.     if turtle.getFuelLevel ~= "unlimited" -- check that turtles even need fuel
  61.     then
  62.         if turtle.getFuelLevel() < fuelThreshold -- if fuel is below the threshold...
  63.         then
  64.             i = 1
  65.             needFuel = true
  66.             while  needFuel do
  67.                 turtle.select(i) -- then step through the slots
  68.                 if turtle.refuel(1) -- and try to use 1 fuel
  69.                 then
  70.                     if turtle.getFuelLevel() > fuelThreshold            -- if fueled, then done
  71.                     then
  72.                         needFuel = false
  73.                     end
  74.                 else
  75.                     if i < 16
  76.                     then
  77.                         i = i + 1
  78.                     else
  79.                         print("No fuel left!")
  80.                         sleep(1)
  81.                         needFuel = false
  82.                     end
  83.                 end
  84.             end
  85.         end
  86.     end
  87. end
  88.  
  89. local function forward(distance)  -- Simple go forward, smash anything in the way. (TODO: add a simple evasion routine)
  90.     local i = 0
  91.     while i < distance do
  92.         if turtle.detect() then --  is there anything in the way? Smash it then
  93.             turtle.dig()
  94.         end
  95.         checkFuel()
  96.         if turtle.forward() then  -- go forward, count if you actually move
  97.             i = i + 1
  98.             xPos = xPos + xMods[dir]
  99.             yPos = yPos + yMods[dir]
  100.         end
  101.     end
  102.     -- print("At end of forward command, I am at ("..xPos..","..yPos..","..zPos..")") -- decomment this to see where the turtle thinks it is
  103. end
  104.  
  105. local function navigateTo(targetX,targetY,targetZ)  -- navigates to a relative coordinate, smashing everything it it's way. USE WITH CAUTION
  106.     -- print("Moving to (" .. targetX ..","..targetY..","..targetZ..")")  -- decomment this to see where the turtle thinks it is going
  107.     if xPos < targetX then
  108.         turnTo(1)
  109.         forward(targetX - xPos)
  110.     end
  111.     if xPos > targetX then
  112.         turnTo(3)
  113.         forward(xPos - targetX)
  114.     end
  115.     if yPos < targetY then
  116.         turnTo(2)
  117.         forward(targetY - yPos)
  118.     end
  119.     if yPos > targetY then
  120.         turnTo(4)
  121.         forward(yPos - targetY)
  122.     end
  123.     setHeight(targetZ)
  124. end
  125.  
  126. for z=0,(height-1),3 do
  127.     if z < (height -1) then
  128.         zLevel = z+1
  129.         digDown = true
  130.         if z < (height - 2)
  131.         then
  132.             digUp = true
  133.         else
  134.             digUp = false
  135.         end
  136.     else
  137.         zLevel = z
  138.         digUp = false
  139.         digDown = false
  140.     end
  141.     upTheRow = true
  142.     for y = 0, (width - 1) do
  143.         if upTheRow
  144.         then
  145.             startX = 0
  146.             endX = (length -1)
  147.             modX = 1
  148.             upTheRow = false
  149.         else
  150.             startX = (length -1)
  151.             endX = 0
  152.             modX = -1
  153.             upTheRow = true
  154.         end
  155.         for x = startX, endX, modX do
  156.             navigateTo(x,y,zLevel)
  157.             if digUp then
  158.                 while turtle.detectUp() do
  159.                     turtle.digUp()
  160.                 end
  161.             end
  162.             if digDown then
  163.                 while turtle.detectDown() do
  164.                     turtle.digDown()
  165.                 end
  166.             end
  167.         end
  168.     end
  169. end
  170.  
  171. navigateTo(0,0,0)
Advertisement
Add Comment
Please, Sign In to add comment