gknova61

Go Program (Turtle)

Nov 18th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.46 KB | None | 0 0
  1. local tArgs = { ... }
  2. local direction = tArgs[1]
  3. local times = tonumber(tArgs[2])
  4.  
  5. if not turtle then
  6.     error("You need a turtle!")
  7. end
  8.  
  9. if times == nil then
  10.   times = 1
  11. end
  12.  
  13. local possibleDirections = {"left","right","up","down","forward","back"}
  14. local translateDirection = {"turnLeft","turnRight","up","down","forward","back"}
  15.  
  16. if #tArgs < 1 then
  17.   print("Usage:")
  18.   print(shell.getRunningProgram().." <direction> (times)")
  19.   print("Parameters:")
  20.   print("<> = required")
  21.   print("() = optional")
  22.   return false
  23. end
  24.  
  25. local function go(aDirection,aTimes,detect)
  26.   print("Going "..direction.." for "..times.." blocks")
  27.     if detect == nil then
  28.         local detect = "detectUp"
  29.         for i=1,aTimes do
  30.             turtle[aDirection]()
  31.         end
  32.    
  33.     elseif detect ~= nil then
  34.         for i=1,aTimes do
  35.             if turtle[detect]() == false then
  36.                 turtle[aDirection]()
  37.             else
  38.                 print("Something in the way!")
  39.                 return false
  40.             end
  41.         end
  42.     end
  43. end
  44.            
  45. for i=1,#possibleDirections do
  46.   if direction == possibleDirections[i] then
  47.     if direction == "forward" then detectIt = "detect"
  48.     elseif direction == "up" then detectIt = "detectUp"
  49.     elseif direction == "down" then detectIt = "detectDown"
  50.     else detectIt = nil
  51.     end
  52.     if go(translateDirection[i],times,detectIt) == true then
  53.         return true
  54.     else
  55.         return false
  56.     end
  57.   end
  58. end
  59.  
  60. print("Invalid Direction")
  61. print("Possible Directions:")
  62. for i=1,#possibleDirections do
  63.     print(possibleDirections[i])
  64. end
  65. return false
Advertisement
Add Comment
Please, Sign In to add comment