Advertisement
guitarplayer616

MoveToProgram

Apr 26th, 2015
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local tArgs = {...}
  2. trans={}
  3. trans[0]="north"
  4. trans[1]="west"
  5. trans[2]="south"
  6. trans[3]="east"
  7.  
  8. if #tArgs ~= 9 then
  9.     print("Not enough Arguments")
  10.     error()
  11. end
  12.  
  13. --[[if tArgs[4] ~= "north" then
  14. elseif tArgs[4] ~= "east" then
  15. elseif tArgs[4] ~= "south" then
  16. elseif tArgs[4] ~= "west" then
  17. for k,v in pairs(trans) do  
  18.   if tArgs[4] == k
  19.     then tArgs[4] = v
  20.   end
  21. end
  22. else
  23.   print("Argument 4 must be = north, east, south or west.")
  24.   error()
  25. end]]--
  26.  
  27. for k,v in pairs(trans) do
  28.   if tArgs[4] == v
  29.     then tArgs[4] = k
  30.   end
  31. end
  32.  
  33. local cDir = tonumber(tArgs[4])
  34. local cX = tonumber(tArgs[1])
  35. local cY = tonumber(tArgs[2])
  36. local cZ = tonumber(tArgs[3])
  37.  
  38. function turnRight()
  39.   if cDir == 0 then
  40.     cDir = 3
  41.   elseif cDir == 3 then
  42.     cDir = 2
  43.   elseif cDir == 2 then
  44.     cDir = 1
  45.   elseif cDir == 1 then
  46.     cDir = 0
  47.   else
  48.     print("TurnrightError!!")
  49.     return nil
  50.   end
  51.   turtle.turnRight()
  52. end
  53.  
  54. function turnLeft()
  55.     if cDir == 0 then
  56.         cDir = 1
  57.     elseif cDir == 3 then
  58.         cDir = 0
  59.     elseif cDir == 2 then
  60.         cDir = 3
  61.     elseif cDir == 1 then
  62.         cDir = 2
  63.     else
  64.         print("TurnLeftError!!")
  65.         return nil
  66.     end
  67.     turtle.turnLeft()
  68. end
  69.  
  70. function turnTo(dir)
  71.   if cDir ~= dir then
  72.     repeat
  73.      turnRight()
  74.     until cDir == dir
  75.   end
  76. end
  77.  
  78. function moveForward()
  79.   if cDir == 0 then
  80.     cZ = cZ-1
  81.   elseif cDir == 3 then
  82.     cX = cX+1
  83.   elseif cDir == 2 then
  84.     cZ = cZ+1
  85.   elseif cDir == 1 then
  86.     cX = cX-1
  87.   else
  88.     print("forwardError!!")
  89.     return nil
  90.   end
  91.   while not turtle.forward() do
  92.     turtle.dig()
  93.   end
  94. end
  95.  
  96. function moveUp()
  97.   cY = cY+1
  98.   while not turtle.up() do
  99.     turtle.digUp()
  100.   end
  101. end
  102.  
  103. function moveDown()
  104.   cY = cY-1
  105.   while not turtle.down() do
  106.     turtle.digDown()
  107.   end
  108. end
  109.    
  110. function moveToX(x)
  111.   if cX < x then
  112.     turnTo(3)
  113.     repeat
  114.       moveForward()
  115.     until cX == x
  116.   elseif cX > x then
  117.     turnTo(1)
  118.     repeat
  119.       moveForward()
  120.     until cX == x
  121.   else
  122.     --print("already there")
  123.   end
  124. end
  125.  
  126. function moveToZ(z)
  127.   if cZ < z then
  128.     turnTo(2)
  129.     repeat
  130.       moveForward()
  131.     until cZ == z
  132.   elseif cZ > z then
  133.     turnTo(0)
  134.     repeat
  135.       moveForward()
  136.     until cZ == z
  137.   else
  138.    --print("already there")
  139.   end
  140. end    
  141.  
  142. function moveToY(y)
  143.   if cY < y then
  144.     repeat
  145.       moveUp()
  146.     until cY == y
  147.   elseif cY > y then
  148.     repeat
  149.       moveDown()
  150.     until cY == y
  151.   else
  152.    --print("already there")
  153.   end
  154. end
  155.  
  156. function goTo(x,y,z,dir)
  157.   moveToX(x)  
  158.   moveToZ(z)
  159.   moveToY(y)
  160.   turnTo(dir)
  161. end
  162.  
  163. function returnTo(x,y,z,dir)
  164.   moveToY(y)
  165.   moveToX(x)  
  166.   moveToZ(z)
  167.   turnTo(dir)
  168. end
  169.  
  170. if tArgs[9]=="return" then
  171.   returnTo(tonumber(tArgs[5]), tonumber(tArgs[6]), tonumber(tArgs[7]),tonumber(tArgs[8]))
  172. elseif tArgs[9]==nil then
  173.   goTo(tonumber(tArgs[5]), tonumber(tArgs[6]), tonumber(tArgs[7]),tonumber(tArgs[8]))
  174. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement