robocyclone

Turtle Move

Dec 29th, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.08 KB | None | 0 0
  1. local x,y = 0,0
  2. local direction = "north"
  3. local dirInstruct = ...
  4.  
  5. function testForAvailableFuel()
  6.     for i = 1, 16 do
  7.         turtle.select(i)
  8.         if turtle.refuel(0) then
  9.             return true
  10.         end
  11.     end
  12. end
  13.  
  14. function fuel()
  15.     if not turtle.getFuelLevel() <= math.floor(turtle.getFuelLimit()/2) or not testForAvailableFuel() then return end
  16.     for i = 1, 16 do
  17.         turtle.select(i)
  18.         if turtle.refuel(0) then
  19.             local halfStack = math.floor(turtle.getItemCount(i)/2)
  20.             turtle.refuel(halfStack)
  21.         end
  22.     end
  23. end
  24.  
  25. function forward(dirToGo)
  26.     if dirToGo == "north" then
  27.         y = y + 1
  28.         if direction == "north" then
  29.             turtle.forward()
  30.         elseif direction == "south" then
  31.             turtle.turnLeft()
  32.             turtle.turnLeft()
  33.             turtle.forward()
  34.         elseif direction == "west" then
  35.             turtle.turnRight()
  36.             turtle.forward()
  37.         elseif direction == "east" then
  38.             turtle.turnLeft()
  39.             turtle.forward()
  40.         end
  41.         direction = "north"
  42.     elseif dirToGo == "east" then
  43.         x = x + 1
  44.         if direction == "north" then
  45.             turtle.turnRight()
  46.             turtle.forward()
  47.         elseif direction == "west" then
  48.             turtle.turnLeft()
  49.             turtle.turnLeft()
  50.             turtle.forward()
  51.         elseif direction == "east" then
  52.             turtle.forward()
  53.         elseif direction == "south" then
  54.             turtle.turnLeft()
  55.             turtle.forward()
  56.         end
  57.         direction = "east"
  58.     elseif dirToGo == "south" then
  59.         y = y - 1
  60.         if direction == "south" then
  61.             turtle.forward()
  62.         elseif direction == "north" then
  63.             turtle.turnLeft()
  64.             turtle.turnLeft()
  65.             turtle.forward()
  66.         elseif direction == "east" then
  67.             turtle.turnRight()
  68.             turtle.forward()
  69.         elseif direction == "west" then
  70.             turtle.turnLeft()
  71.             turtle.forward()
  72.         end
  73.         direction = "south"
  74.     elseif dirToGo == "west" then
  75.         x = x - 1
  76.         if direction == "west" then
  77.             turtle.forward()
  78.         elseif direction == "east" then
  79.             turtle.turnLeft()
  80.             turtle.turnLeft()
  81.             turtle.forward()
  82.         elseif direction == "south" then
  83.             turtle.turnRight()
  84.             turtle.forward()
  85.         elseif direction == "north" then
  86.             turtle.turnLeft()
  87.             turtle.forward()
  88.         end
  89.         direction = "west"
  90.     end
  91.     fuel()
  92. end
  93.  
  94. forward(dirInstruct)
Advertisement
Add Comment
Please, Sign In to add comment