Advertisement
cduclaux

Computercraft Turtle Movement Wrapper

May 7th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.37 KB | None | 0 0
  1. -- Turtle Self-tracking System created by Arclight306
  2.  
  3. local xPos, yPos, zPos = nil
  4. local face = nil
  5. local cal = false
  6.  
  7. function gpsLocation(facing) -- get gps using other computers
  8.    xPos, yPos, zPos = gps.locate()
  9.    if xPos == nil then
  10.       cal = false
  11.       return false
  12.    else
  13.       if facing == "north" or facing == "east" or facing == "south" or facing == "west" then
  14.          face = facing
  15.          cal = true
  16.          return true
  17.       else
  18.          cal=false
  19.          assert(false, "Facing must be north, south, east or west")
  20.       end
  21.       return false
  22.    end
  23. end
  24.  
  25. function manSetLocation(x, y, z, facing) -- manually set location
  26.    
  27.    assert(type(x) == "number", "x must be a number")
  28.    assert(type(y) == "number", "y must be a number")
  29.    assert(type(z) == "number", "z must be a number")
  30.        
  31.    if facing == "north" or facing == "east" or facing == "south" or facing == "west" then
  32.       face = facing
  33.       cal = true
  34.    else
  35.       cal=false
  36.       assert(false, "Facing must be north, south, east or west")
  37.    end
  38.    
  39.    xPos = x
  40.    yPos = y
  41.    zPos = z
  42.    face = facing
  43. end
  44.  
  45. function getLocation() -- return the location
  46.    if xPos ~= nil then
  47.       return xPos, yPos, zPos, face
  48.    elseif xPos == nil then
  49.       return nil
  50.    end
  51. end
  52.  
  53. function turnLeft() -- turn left
  54.    if(turtle.turnLeft()) then
  55.       if face == "north" then
  56.          face = "west"
  57.       elseif face == "west" then
  58.          face = "south"
  59.       elseif face == "south" then
  60.          face = "east"
  61.       elseif face == "east" then
  62.          face = "north"
  63.       end
  64.       return true
  65.    end
  66.    return false
  67. end
  68.  
  69. function turnRight() -- turn right
  70.    if(turtle.turnRight()) then
  71.       if face == "north" then
  72.          face = "east"
  73.       elseif face == "east" then
  74.          face = "south"
  75.       elseif face == "south" then
  76.          face = "west"
  77.       elseif face == "west" then
  78.          face = "north"
  79.       end
  80.       return true
  81.    end
  82.    return false
  83. end
  84.  
  85. function forward() -- go forward
  86.  
  87.    if(turtle.forward()) then
  88.       if cal == true then
  89.          if face == "north" then
  90.             zPos = zPos - 1
  91.          elseif face == "west" then
  92.             xPos = xPos - 1
  93.          elseif face == "south" then
  94.             zPos = zPos + 1
  95.          elseif face == "east" then
  96.             xPos = xPos + 1
  97.          end
  98.       else
  99.          print("Not Calibrated.")
  100.       end
  101.       return true
  102.    end
  103.    return false
  104. end
  105.  
  106. function back() -- go back
  107.  
  108.    if(turtle.back()) then
  109.       if cal == true then
  110.          if face == "north" then
  111.             zPos = zPos + 1
  112.          elseif face == "west" then
  113.             xPos = xPos + 1
  114.          elseif face == "south" then
  115.             zPos = zPos - 1
  116.          elseif face == "east" then
  117.             xPos = xPos - 1
  118.          end
  119.       else
  120.          print("Not Calibrated.")
  121.       end
  122.       return true
  123.    else
  124.       return false
  125.    end
  126. end
  127.  
  128. function up() -- go up
  129.    if(turtle.up()) then
  130.       if cal == true then
  131.          yPos = yPos + 1
  132.       else
  133.          print("Not Calibrated.")
  134.       end
  135.       return true
  136.    else
  137.       return false
  138.    end
  139. end
  140.  
  141. function down() -- go down
  142.    if(turtle.down()) then
  143.       if cal == true then
  144.          yPos = yPos - 1
  145.       else
  146.          print("Not Calibrated.")
  147.       end
  148.       return true
  149.    else
  150.       return false
  151.    end
  152. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement