Advertisement
Blazuno

Movement Library

Mar 15th, 2022 (edited)
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.12 KB | None | 0 0
  1.  
  2. ----------movement library
  3. curXPos = 0
  4. curYPos = 0
  5. curZPos = 0
  6. orientation = "north"
  7.  
  8.  
  9. forward = function()
  10.     local k = turtle.forward()
  11.     if k == true then
  12.         if orientation == "north" then
  13.             curXPos = curXPos + 1
  14.         elseif orientation == "east" then
  15.             curYPos = curYPos + 1
  16.         elseif orientation == "south" then
  17.             curXPos = curXPos - 1
  18.         elseif  orientation == "west" then
  19.             curYPos = curYPos - 1
  20.         end
  21.     end
  22.     return k
  23. end
  24.  
  25. up = function()
  26.     local k = turtle.up()
  27.     if k == true then
  28.         curZPos = curZPos + 1
  29.     end
  30.     return k
  31. end
  32.  
  33. down = function()
  34.     local k = turtle.down()
  35.     if k == true then
  36.         curZPos = curZPos - 1
  37.     end
  38.     return k
  39. end
  40.  
  41. turnLeft = function()
  42.     local k = turtle.turnLeft()
  43.     if orientation == "north" then
  44.         orientation = "west"
  45.     elseif orientation == "west" then
  46.         orientation = "south"
  47.     elseif orientation == "south" then
  48.         orientation = "east"
  49.     elseif orientation == "east" then
  50.         orientation = "north"
  51.     end
  52.     return k
  53. end
  54.  
  55. turnRight = function()
  56.     local k = turtle.turnRight()
  57.     if orientation == "north" then
  58.         orientation = "east"
  59.     elseif orientation == "east" then
  60.         orientation = "south"
  61.     elseif orientation == "south" then
  62.         orientation = "west"
  63.     elseif orientation == "west" then
  64.         orientation = "north"
  65.     end
  66.     return k
  67. end
  68. ---------------these functions are useful for specific situations (NOTE: you should probably use faceDirection() instead of turtle.turn as it updates orientation which is relevant for the other functions)
  69.  
  70.  
  71. goHome = function()
  72.     local savedPos = {}
  73.     savedPos.x = curXPos
  74.     savedPos.y = curYPos
  75.     savedPos.z = curZPos
  76.     print("Facing direction: "..orientation)
  77.     if curYPos > 0 then
  78.         faceDirection("west")
  79.         for i=1,savedPos.y do
  80.             forward()
  81.         end
  82.     elseif curYPos < 0 then
  83.         faceDirection("east")
  84.         for i=1,math.abs(savedPos.y) do
  85.             forward()
  86.         end
  87.     end
  88.     if curXPos > 0 then
  89.         faceDirection("south")
  90.         for i=1,savedPos.x do
  91.             forward()
  92.         end
  93.     elseif curXPos < 0 then
  94.         faceDirection("north")
  95.         for i=1,math.abs(savedPos.x) do
  96.             forward()
  97.         end
  98.     end
  99. end
  100.  
  101. faceDirection = function(orientation)
  102.     if orientation == "north" then
  103.         if orientation == "south" then
  104.             for i=1,2 do
  105.                 turtle.turnLeft()
  106.             end
  107.         elseif orientation == "east" then
  108.             turtle.turnLeft()
  109.         elseif orientation == "west" then
  110.             turtle.turnRight()
  111.         end
  112.         orientation = "north"
  113.     elseif orientation == "west" then
  114.         if orientation == "south" then
  115.             turtle.turnRight()
  116.         elseif orientation =="east" then
  117.             for i=1,2 do
  118.                 turtle.turnLeft()
  119.             end
  120.         elseif orientation == "north" then
  121.             turtle.turnLeft()
  122.         end
  123.         orientation = "west"
  124.     elseif orientation == "south" then
  125.         if orientation == "east" then
  126.             turtle.turnRight()
  127.         elseif orientation == "north"  then
  128.             for i=1,2 do
  129.                 turtle.turnLeft()
  130.             end
  131.         elseif orientation == "west" then
  132.             turtle.turnLeft()
  133.         end
  134.         orientation = "south"
  135.     elseif orientation == "east" then
  136.         if orientation == "north" then
  137.             turtle.turnRight()
  138.         elseif orientation == "west" then
  139.             for i=1,2 do
  140.                 turtle.turnLeft()
  141.             end
  142.         elseif orientation == "south" then
  143.             turtle.turnLeft()
  144.         end
  145.         orientation = "east"
  146.     end
  147. end
  148.  
  149.  
  150. goToCoords = function(toX, toY, toZ, orientation, coordFirst, coordSecond, coordThird)
  151.     local savedPos = {}
  152.     local changeInPos = {}
  153.     savedPos.toX = toX
  154.     savedPos.toY = toY
  155.     savedPos.toZ = toZ
  156.     savedPos.orient = orientation
  157.     local placeholder = {}
  158.     placeholder.x = math.abs(savedPos.toX - curXPos)
  159.     placeholder.y = math.abs(savedPos.toY - curYPos)
  160.     placeholder.z = math.abs(savedPos.toZ - curZPos)
  161.     if coordFirst == "z" then
  162.         if savedPos.z > curZPos then
  163.             for i=1,placeholder.z do
  164.                 turtle.digUp()
  165.                 up()
  166.             end
  167.         elseif savedPos.z < curZPos then
  168.             for i=1,placeholder.z do
  169.                 down()
  170.             end
  171.         end
  172.         if coordSecond == "y"
  173.             if savedPos.y > curYPos then
  174.                 faceDirection("east")
  175.                 for i=1, placeholder.y do
  176.                     forward()
  177.                 end
  178.             elseif savedPos.y < curYPos then
  179.                 faceDirection("west")
  180.                 for i=1,placeholder.y do
  181.                     forward()
  182.                 end
  183.             end
  184.             if savedPos.x > curXPos then
  185.                 faceDirection("north")
  186.                 for i=1,placeholder.x do
  187.                     forward()
  188.                 end
  189.             elseif savedPos.x < curXPos then
  190.                 faceDirection("south")
  191.                 for i=1,placeholder.x do
  192.                     forward()
  193.                 end
  194.             end
  195.            
  196.         else
  197.             if savedPos.x > curXPos then
  198.                 faceDirection("north")
  199.                 for i=1,placeholder.x do
  200.                     forward()
  201.                 end
  202.             elseif savedPos.x < curXPos then
  203.                 faceDirection("south")
  204.                 for i=1,placeholder.x do
  205.                     forward()
  206.                 end
  207.             end
  208.             if savedPos.y > curYPos then
  209.                 faceDirection("east")
  210.                 for i=1, placeholder.y do
  211.                     forward()
  212.                 end
  213.             elseif savedPos.y < curYPos then
  214.                 faceDirection("west")
  215.                 for i=1,placeholder.y do
  216.                     forward()
  217.                 end
  218.             end
  219.         end
  220.     elseif coordFirst == "y"
  221.         if savedPos.y > curYPos then
  222.             faceDirection("east")
  223.             for i=1, placeholder.y do
  224.                 forward()
  225.             end
  226.         elseif savedPos.y < curYPos then
  227.             faceDirection("west")
  228.             for i=1,placeholder.y do
  229.                 forward()
  230.             end
  231.         end
  232.         if coordSecond == "x" then
  233.             if savedPos.x > curXPos then
  234.                 faceDirection("north")
  235.                 for i=1,placeholder.x do
  236.                     forward()
  237.                 end
  238.             elseif savedPos.x < curXPos then
  239.                 faceDirection("south")
  240.                 for i=1,placeholder.x do
  241.                     forward()
  242.                 end
  243.             end
  244.             if savedPos.z > curZPos then
  245.                 for i=1,placeholder.z do
  246.                     turtle.digUp()
  247.                     up()
  248.                 end
  249.             elseif savedPos.z < curZPos then
  250.                 for i=1,placeholder.z do
  251.                     down()
  252.                 end
  253.             end
  254.         else
  255.             if savedPos.z > curZPos then
  256.                 for i=1,placeholder.z do
  257.                     turtle.digUp()
  258.                     up()
  259.                 end
  260.             elseif savedPos.z < curZPos then
  261.                 for i=1,placeholder.z do
  262.                     down()
  263.                 end
  264.             end
  265.             if savedPos.x > curXPos then
  266.                 faceDirection("north")
  267.                 for i=1,placeholder.x do
  268.                     forward()
  269.                 end
  270.             elseif savedPos.x < curXPos then
  271.                 faceDirection("south")
  272.                 for i=1,placeholder.x do
  273.                     forward()
  274.                 end
  275.             end
  276.         end
  277.     else
  278.         if savedPos.x > curXPos then
  279.             faceDirection("north")
  280.             for i=1,placeholder.x do
  281.                 forward()
  282.             end
  283.         elseif savedPos.x < curXPos then
  284.             faceDirection("south")
  285.             for i=1,placeholder.x do
  286.                 forward()
  287.             end
  288.         end
  289.         if coordSecond == "y" then
  290.             if savedPos.y > curYPos then
  291.                 faceDirection("east")
  292.                 for i=1, placeholder.y do
  293.                     forward()
  294.                 end
  295.             elseif savedPos.y < curYPos then
  296.                 faceDirection("west")
  297.                 for i=1,placeholder.y do
  298.                     forward()
  299.                 end
  300.             end
  301.             if savedPos.z > curZPos then
  302.                 for i=1,placeholder.z do
  303.                     turtle.digUp()
  304.                     up()
  305.                 end
  306.             elseif savedPos.z < curZPos then
  307.                 for i=1,placeholder.z do
  308.                     down()
  309.                 end
  310.             end
  311.         else
  312.             if savedPos.z > curZPos then
  313.                 for i=1,placeholder.z do
  314.                     turtle.digUp()
  315.                     up()
  316.                 end
  317.             elseif savedPos.z < curZPos then
  318.                 for i=1,placeholder.z do
  319.                     down()
  320.                 end
  321.             end
  322.             if savedPos.y > curYPos then
  323.                 faceDirection("east")
  324.                 for i=1, placeholder.y do
  325.                     forward()
  326.                 end
  327.             elseif savedPos.y < curYPos then
  328.                 faceDirection("west")
  329.                 for i=1,placeholder.y do
  330.                     forward()
  331.                 end
  332.             end
  333.         end
  334.     end
  335. end
  336.     faceDirection(savedPos.orient)
  337. end
  338.  
  339.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement