Guest User

Untitled

a guest
Sep 22nd, 2012
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. tArgs = {...}
  2. face = 0
  3. local up = 0
  4. local fuel
  5. local gx = tonumber(tArgs[1]) --target x pos
  6. local gy = tonumber(tArgs[2]) --target y pos
  7. local gz = tonumber(tArgs[3]) --target z pos
  8. local x, y, z --defines the amount to move
  9. function prepare() --setup
  10.     rednet.open("right") --activate right modem
  11.     cx,cy,cz = gps.locate(3) -- sets current x, y, z
  12.     if #tArgs ~= 3 then -- if incorrect amount of args
  13.         print("Please input x, y, z coordinates")
  14.     end
  15.     fuel = turtle.getFuelLevel() -- the fuel level of robot
  16.     print("Fuel Level: "..fuel)
  17.     if fuel < 1000 then -- random # choice should be enough for long distance
  18.         print("Fuel Level might be too low")
  19.         print("Would you like to refuel? yes/no")
  20.         input = read() --looking at user input
  21.         if  input == "yes" then
  22.   turtle.select(1)
  23.             print("Place fuel source in selected slot")
  24.             turtle.refuel(20) --refuel with 20
  25.             print("Refuelling")
  26.             fuel = turtle.getFuelLevel() -- rechecking fuel level
  27.             print("Fuel Level is now: ".. fuel)
  28.         else
  29.             print("Refueling Canceled") -- if person type no
  30.         end
  31.     else
  32.         print("There should be enough fuel for the travel")
  33.         print("No refuelling necessary")
  34.     end
  35.     print("Current position: "..cx..", "..cy..", "..cz) --print cur pos
  36.     print("Going to: "..gx..", "..gy..", "..gz) --print target pos
  37. end
  38. function currentFace() --find what pos the turtle is facing
  39.     while not turtle.forward() do -- try go forward
  40.         if not turtle.up() then -- try to do up
  41.             turtle.digUp() -- dig up
  42.         end
  43.     end
  44.     local nx,ny,nz = gps.locate(3) --take new pos
  45.     print("New position: "..nx..", "..ny..", "..nz)
  46.     if cx < nx then -- this determines where the turtle is facing
  47.             face = 3
  48.     elseif cx > nx then
  49.         face = 1
  50.     elseif cz < nz then
  51.         face = 0
  52.     elseif cz > nz then
  53.         face = 2    
  54.     end
  55.     print("I am facing: "..face)
  56. end
  57. function turnToFace(f) --turn until facing desired direction
  58.     if face ~= f then
  59.         while face ~= f do
  60.             turtle.turnRight()
  61.     os.sleep(.1)
  62.             face = face + 1
  63. if face == 5 then
  64. face = 0
  65. end  
  66.         end
  67.     else
  68.     print("Facing the right way")
  69.     end
  70. end
  71. function determineFaceX() -- determine the desired way to face on the x axis (1st step)
  72.  nx,ny,nz = gps.locate(3)
  73.    
  74.     if gx < nx then
  75.         turnToFace(1)
  76.     elseif gx > nx then
  77.         turnToFace(3)
  78.     end
  79. end
  80. function determineFaceZ() -- determine the desired way to face on the z axis (2st step)
  81.     if gz < nz then
  82.         turnToFace(2)
  83.     elseif gz > nz then
  84.         turnToFace(4)
  85.     end
  86. end
  87. function determineMoveX() -- determine how much to move on the x axis
  88.     x = gx - nx
  89.     if x < 0 then
  90.         x = -x
  91.     end
  92. end
  93. function determineMoveY() -- determine how much to move on the y axis
  94.     y = gy - ny
  95. end
  96. function determineMoveZ() -- determine how much to move on the z axis
  97.     z = gz - nz
  98.     if z < 0 then
  99.         z = -z
  100.     end
  101. end
  102. function move (m) -- the actual movement on x and z axis
  103. if x ==0 or z == 0 then
  104. print("Don't need to move")
  105. else
  106.     for i=1, m do
  107.         while not turtle.forward() do
  108.             up = up + 1
  109.             if not turtle.up() then
  110.                 turtle.digUp()
  111.             end
  112.         end
  113. end
  114.     end
  115. end
  116. function moveY (b) -- the actual movement on y axis
  117. --print(y)
  118.     if y > 0 then
  119.         for i=1, y do
  120.             turtle.up()
  121.         end
  122.     elseif y < 0 then
  123.         for i = 1,-y do
  124.             while not turtle.down() do
  125.                 turtle.digDown()
  126.             end
  127.         end
  128.     end
  129.     for p=1, up do
  130.         turtle.down()
  131.     end
  132. end
  133. -- calling all necessary function
  134. prepare()
  135. currentFace()
  136. determineFaceX()
  137. determineMoveX()
  138. move(x)
  139. determineFaceZ()
  140. determineMoveZ()
  141. move(z)
  142. determineMoveY()
  143. moveY(y)
Advertisement
Add Comment
Please, Sign In to add comment