Advertisement
Guest User

moveTo

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