Advertisement
cing5000

moveRandom

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