Advertisement
Andrakon

navi api movement utilities (Computercraft)

Mar 25th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.11 KB | None | 0 0
  1. -- navi movement utilities 1.01 by Andrakon http://www.youtube.com/user/BadashGames
  2.  
  3. --[[ Variable and Function list:
  4. getPosition() returns the vector "position"; to compare make sure to assign to a variable and use yourvariable:tostring() in the if statement
  5. setPostion(x,y,z) sets the position, expects numbers
  6. getFacing() returns a string or nil if its empty
  7. setFacing(string) set to n,s,e, or w example: setFacing("n")
  8. getBedrock() returns the number that the turtle will not navigate to or below in the y position to avoid bedrock
  9. setBedrock(number) sets the postion the turtle will not navigate to or below, incase your bedrock is lower
  10. fuelCheck(number) checks the fuel level while compairing it to a number given; 320 by default
  11. facingCheck() ensures the turtle knows what direction it is facing and attempts to figure it out on its own if a GPS is available
  12. gpsUpdate() updates the vector "position" with gps cordinates; can accept a number for acceptable delay, default 5
  13. positionCheck() used to check if the y position is 0, if so it will update the position with gps or ask the player to do so
  14. face(string) orients the turtle, accepts l,r,n,s,e,w, or b for back; usage: face("n") to face north; updates variable "facing"
  15. move(string) does not break blocks or kill mobs, but allows movement while updating x,y,z cordinates with variable "position"; accepts f,b,u,d; forward by default
  16. go(string, number) extends move()'s functionality, breaking blocks, attacks mobs in the way. accepts f,b,u,d and an optional number; forward by default, 1 block by default; usage: go("f", 3)
  17. dig(string), attack(string), detect(string), and compare(string); locally wrapped versions of turtle functions; accepts f,b,u,d; usage detect("b") to detect behind the turtle, it turns around to do so
  18. getToX(number), getToY(number), and getToZ(number) navigate to specific cordinates given to them; returns number of blocks traveled
  19. ]]
  20.  
  21. -- Variables
  22. local position = vector.new(0,0,0) -- make sure to update before moving
  23. local facing = nil -- n,s,e,w make sure to update before moving
  24. local bedrock = 5 -- will not navigate below this height
  25. -- vectors used by move() to update vector "position"; don't change these
  26. local av = vector.new(1, 0, 0) -- add to go east
  27. local bv = vector.new(0, 1, 0) -- add to go up
  28. local cv = vector.new(0, 0, 1) -- add to go south  
  29.  
  30. -- Functions
  31. -- get and set variables
  32. function getPosition()
  33.   return position
  34. end
  35.  
  36. function setPosition(sx, sy, sz)
  37.     if type(sx) ~= "number" then error("number expected", 2) end
  38.     if type(sy) ~= "number" then error("number expected", 2) end
  39.     if type(sz) ~= "number" then error("number expected", 2) end
  40.   local tempvar = vector.new(sx, sy, sz)
  41.   position = tempvar
  42. end
  43.  
  44. function getFacing()
  45.   return facing
  46. end
  47.  
  48. function setFacing(someway)
  49.   if type(someway) ~= "string" then error("string expected", 2) end
  50.   facing = someway
  51. end
  52.  
  53. function getBedrock()
  54.     return bedrock
  55. end
  56.  
  57. function setBedrock(set)
  58.     if type(set) ~= "number" then error("number expected", 2) end
  59.     bedrock = set
  60. end
  61.  
  62. -- fuelCheck() checks the fuel level while compairing it to the given ammount
  63. function fuelCheck(minFuel)
  64.   local minFuel = minFuel or 320
  65.   if type(minFuel) ~= "number" then error("expecting a number", 2) end
  66.   if minFuel > turtle.getFuelLimit() then minFuel = turtle.getFuelLimit() end
  67.   term.clear()
  68.   term.setCursorPos(1,1)
  69.   print("My fuel level is ", turtle.getFuelLevel())
  70.   local saveSlot = turtle.getSelectedSlot()
  71.   turtle.select(16)
  72.   if turtle.getFuelLevel() < minFuel then
  73.     print("Refueling...")
  74.     while turtle.getFuelLevel() < minFuel do
  75.       turtle.refuel(1)
  76.     end
  77.     print("My fuel level is now ", turtle.getFuelLevel())
  78.     turtle.select(saveSlot)
  79.   end
  80. end
  81.  
  82. -- facingCheck() ensures the turtle knows what direction it is facing and attempts to figure it out on its own if a GPS is available
  83. function facingCheck()
  84.     if facing ~= "e" then
  85.         if facing ~= "w" then
  86.             if facing ~= "s" then
  87.                 if facing ~= "n" then
  88.                     local pos1 = vector.new(gps.locate(5))
  89.                     if pos1.y ~= 0 then
  90.                         if turtle.detect() then
  91.                           if not turtle.dig() then
  92.                             error("Turtle encountered undiggable block while finding orientation")
  93.                             os.exit()
  94.                           end
  95.                           while not turtle.forward() do
  96.                             turtle.attack()
  97.                           end
  98.                         else
  99.                           while not turtle.forward() do
  100.                            turtle.attack()
  101.                           end
  102.                         end
  103.                         os.sleep(5)
  104.                         local pos2 = vector.new(gps.locate(5))
  105.                         local victor = pos2 - pos1
  106.                         if victor.x == 1 then facing = "e" end
  107.                         if victor.x == -1 then facing = "w" end
  108.                         if victor.z == 1 then facing =  "s" end
  109.                         if victor.z == -1 then facing = "n" end
  110.                     end
  111.                     if facing ~= "e" then
  112.                         if facing ~= "w" then
  113.                             if facing ~= "s" then
  114.                                 if facing ~= "n" then
  115.                                     print("What direction am I facing? n,s,e,w \n")
  116.                                     input = read()
  117.                                     if input == "n" or input == "s" or input == "e" or input == "w" then
  118.                                     facing = input
  119.                                         term.clear()
  120.                                         term.setCursorPos(1,1)
  121.                                         os.sleep(2)
  122.                                     else
  123.                                        print("Unnacceptable facing, stoping.\n")
  124.                                        os.exit()
  125.                                     end
  126.                                 end
  127.                             end
  128.                         end
  129.                     end
  130.                 end
  131.             end
  132.         end
  133.     end
  134.     return facing
  135. end
  136.  
  137. -- gpsUpdate() updates the vector "position" with gps cordinates
  138. function gpsUpdate(tm)
  139.     local tm = tm or 5
  140.     if type(tm) ~= "number" then error("number expected", 2) end
  141.     local posGet = vector.new(gps.locate(tm))
  142.     if posGet.y ~= 0 then
  143.         position = posGet
  144.     else
  145.         return false
  146.     end
  147. end
  148.  
  149. -- positionCheck() used to check if the y position is 0, if so it will update the position with gps or ask the player to do so
  150. function positionCheck()  
  151.     if position.y == 0 then gpsUpdate() end
  152.     if position.y == 0 then
  153.         while input ~= "y" do
  154.             term.clear()
  155.             term.setCursorPos(1,1)
  156.             print("What is my x position?")
  157.             local myX = read()
  158.             print("What is my y position?")
  159.             local myY = read()
  160.             print("what is my z position?")
  161.             local myZ = read()
  162.             local newPos = vector.new(myX, myY, myZ)
  163.             print("Is ", newPos, " correct? y/n")
  164.             input = read()
  165.             position = newPos
  166.         end
  167.     end
  168.     return true
  169. end
  170.  
  171.  -- faceWrap() is used to keep track of the n,s,e,w facing of the turtle; ment to be used by face()
  172. local function faceWrap(lr)
  173.     if lr ~= "l" then
  174.         if lr ~= "r" then error("l or r expected", 2) end
  175.     end
  176.     if lr == "l" then
  177.         turtle.turnLeft()
  178.         if facing == "n" then facing = "w"; return end
  179.         if facing == "w" then facing = "s"; return end
  180.         if facing == "s" then facing = "e"; return end
  181.         if facing == "e" then facing = "n"; return end
  182.     end
  183.     if lr == "r" then
  184.         turtle.turnRight()
  185.         if facing == "n" then facing = "e"; return end
  186.         if facing == "e" then facing = "s"; return end
  187.         if facing == "s" then facing = "w"; return end
  188.         if facing == "w" then facing = "n"; return end
  189.     end
  190.     return true
  191. end
  192.  
  193. -- face() orients the turtle, accepts l,r,n,s,e,w, or b for back; usage: face("n") to face north; updates variable "facing"
  194. function face(way)
  195.     if way ~= "n" then
  196.         if way ~= "s" then
  197.             if way ~= "e" then
  198.                 if way ~= "w" then
  199.                     if way ~= "l" then
  200.                         if way ~= "r" then
  201.                             if way ~= "b" then error("l,r,n,s,e,w expected", 2) end
  202.                         end
  203.                     end
  204.                 end
  205.             end
  206.         end
  207.     end
  208.     if way == "l" then faceWrap("l")
  209.     elseif way == "r" then faceWrap("r")
  210.     elseif way == "b" then
  211.         faceWrap("r")
  212.         faceWrap("r")
  213.     elseif way == "n" then
  214.         if facing == "e" then faceWrap("l")
  215.         else while way ~= facing do faceWrap("r") end
  216.         end
  217.     elseif way == "e" then
  218.         if facing == "s" then faceWrap("l")
  219.         else while way ~= facing do faceWrap("r") end
  220.         end
  221.     elseif way == "s" then
  222.         if facing == "w" then faceWrap("l")
  223.         else while way ~= facing do faceWrap("r") end
  224.         end
  225.     elseif way == "w" then
  226.         if facing == "n" then faceWrap("l")
  227.         else while way ~= facing do faceWrap("r") end
  228.         end
  229.     end
  230.     return true
  231. end
  232.  
  233. -- move() does not break blocks or kill mobs, but allows movement while updating x,y,z cordinates with variable vector "position"; accepts f,b,u,d; forward by default
  234. function move(dir) -- f,b,u,d
  235.     local dir = dir or "f"
  236.     if dir ~= "f" then
  237.         if dir ~= "b" then
  238.             if dir ~= "u" then
  239.                 if dir ~= "d" then error("f,b,u,d expected", 2) end
  240.             end
  241.         end
  242.     end
  243.   if dir == "f" then
  244.     if turtle.forward() then
  245.       if facing == "s" then
  246.         position = position + cv
  247.       elseif facing == "w" then
  248.         position = position - av
  249.       elseif facing == "n" then
  250.         position = position - cv
  251.       elseif facing == "e" then
  252.         position = position + av
  253.       else
  254.         error("Wrong Facing Data")
  255.         return false
  256.       end        
  257.     else
  258.       return false
  259.     end
  260.   elseif dir == "b" then
  261.     if turtle.back() then
  262.       if facing == "s" then
  263.         position = position - cv
  264.       elseif facing == "w" then
  265.         position = position + av
  266.       elseif facing == "n" then
  267.         position = position + cv
  268.       elseif facing == "e" then
  269.         position = position - av
  270.       else
  271.         error("Wrong Facing Data")
  272.         return false
  273.       end    
  274.     else
  275.       return false
  276.     end
  277.   elseif dir == "u" then
  278.     if turtle.up() then
  279.       position = position + bv
  280.     else
  281.       return false
  282.     end
  283.   elseif dir == "d" then
  284.     if position.y > bedrock then
  285.         if turtle.down() then
  286.             position = position - bv
  287.         else
  288.             return false
  289.         end
  290.     else
  291.         return false
  292.     end
  293.   else
  294.     error("Wrong Direction Data")
  295.     return false
  296.   end
  297.   return true
  298. end
  299.  
  300. -- forward, up, and down versions of dig, attack, detect, and compare
  301. function dig(dir)
  302.   local dir = dir or "f"
  303.   if dir ~= "f" then
  304.         if dir ~= "b" then
  305.             if dir ~= "u" then
  306.                 if dir ~= "d" then error("f,b,u,d expected", 2) end
  307.             end
  308.         end
  309.     end
  310.   if dir == "f" then
  311.     if not turtle.dig() then
  312.       return false
  313.     end
  314.   elseif dir == "b" then
  315.     local failState = false
  316.     face("b")
  317.     if not turtle.dig() then
  318.         failState = true
  319.     end
  320.     face("b")
  321.     if failState == true then return false end
  322.   elseif dir == "u" then
  323.     if not turtle.digUp() then
  324.       return false
  325.     end
  326.   elseif dir == "d" then
  327.     if not turtle.digDown() then
  328.       return false
  329.     end
  330.   else
  331.     error("Wrong Direction Data")
  332.     return false
  333.   end
  334.   return true
  335. end
  336.  
  337. function attack(dir)
  338.   local dir = dir or "f"
  339.   if dir ~= "f" then
  340.         if dir ~= "b" then
  341.             if dir ~= "u" then
  342.                 if dir ~= "d" then error("f,b,u,d expected", 2) end
  343.             end
  344.         end
  345.     end
  346.   if dir == "f" then
  347.     if not turtle.attack() then
  348.       return false
  349.     end
  350.   elseif dir == "b" then
  351.     local failState = false
  352.     face("b")
  353.     if not turtle.attack() then
  354.         failState = true
  355.     end
  356.     face("b")
  357.     if failState == true then return false end
  358.   elseif dir == "u" then
  359.     if not turtle.attackUp() then
  360.       return false
  361.     end
  362.   elseif dir == "d" then
  363.     if not turtle.attackDown() then
  364.       return false
  365.     end
  366.   else
  367.     error("Wrong Direction Data")
  368.       return false
  369.   end
  370.   return true
  371. end
  372.  
  373. function detect(dir)
  374.   local dir = dir or "f"
  375.   if dir ~= "f" then
  376.         if dir ~= "b" then
  377.             if dir ~= "u" then
  378.                 if dir ~= "d" then error("f,b,u,d expected", 2) end
  379.             end
  380.         end
  381.     end
  382.   if dir == "f" then
  383.     if not turtle.detect() then
  384.       return false
  385.     end
  386.   elseif dir == "b" then
  387.     local failState = false
  388.     face("b")
  389.     if not turtle.detect() then
  390.         failState = true
  391.     end
  392.     face("b")
  393.     if failState == true then return false end
  394.   elseif dir == "u" then
  395.     if not turtle.detectUp() then
  396.       return false
  397.     end
  398.   elseif dir == "d" then
  399.     if not turtle.detectDown() then
  400.       return false
  401.     end
  402.   else
  403.     error("Wrong Direction Data")
  404.       return false
  405.   end
  406.   return true
  407. end
  408.  
  409. function compare(dir)
  410.   local dir = dir or "f"
  411.   if dir ~= "f" then
  412.         if dir ~= "b" then
  413.             if dir ~= "u" then
  414.                 if dir ~= "d" then error("f,b,u,d expected", 2) end
  415.             end
  416.         end
  417.     end
  418.   if dir == "f" then
  419.     if not turtle.compare() then
  420.       return false
  421.     end
  422.   elseif dir == "b" then
  423.     local failState = false
  424.     face("b")
  425.     if not turtle.compare() then
  426.         failState = true
  427.     end
  428.     face("b")
  429.     if failState == true then return false end
  430.   elseif dir == "u" then
  431.     if not turtle.compareUp() then
  432.       return false
  433.     end
  434.   elseif dir == "d" then
  435.     if not turtle.compareDown() then
  436.       return false
  437.     end
  438.   else
  439.     error("Wrong Direction Data")
  440.       return false
  441.   end
  442.   return true
  443. end
  444.  
  445. -- go() extends move()'s functionality, breaking blocks, attacks mobs in the way. accepts f,b,u,d and an optional number; forward by default, 1 block by default; usage: go("f", 3)
  446. function go(way, blocks)
  447.   local way = way or "f"
  448.   if way ~= "f" then
  449.         if way ~= "b" then
  450.             if way ~= "u" then
  451.                 if way ~= "d" then error("f,b,u,d expected", 2) end
  452.             end
  453.         end
  454.     end
  455.   local blocks = blocks or 1
  456.   if type(blocks) ~= "number" then error("number expected as second number for go(direction, number)", 2) end
  457.   for i = 1 , blocks do
  458.     if detect(way) then
  459.       if not dig(way) then
  460.         -- error("Undiggable Block")
  461.         return false
  462.       end
  463.       while not move(way) do
  464.         attack(way)
  465.         dig(way)
  466.       end
  467.     else
  468.         while not move(way) do
  469.         attack(way)
  470.         dig(way)
  471.         end
  472.     end
  473.   end
  474.   return true
  475. end
  476.  
  477. -- getToX(), getToY(), and getToZ() navigate to specific cordinates given to them
  478. function getToX(numb)
  479.     if type(numb) ~= "number" then error("expecting a number", 2) end
  480.     numb = position.x - numb
  481.     if numb > 0 then
  482.         face("w")
  483.         go("f", numb)
  484.     elseif numb < 0 then
  485.         numb = math.abs(numb)
  486.         face("e")
  487.         go("f", numb)
  488.     end
  489.     return numb
  490. end
  491.  
  492. function getToY(numb)
  493.     if type(numb) ~= "number" then error("expecting a number", 2) end
  494.     numb = position.y - numb
  495.     if numb > 0 then
  496.         go("d", numb)
  497.     elseif numb < 0 then
  498.         numb = math.abs(numb)
  499.         go("u", numb)
  500.     end
  501.     return numb
  502. end
  503.  
  504. function getToZ(numb)
  505.     if type(numb) ~= "number" then error("expecting a number", 2) end
  506.     numb = position.z - numb
  507.     if numb > 0 then
  508.         face("n")
  509.         go("f", numb)
  510.     elseif numb < 0 then
  511.         numb = math.abs(numb)
  512.         face("s")
  513.         go("f", numb)
  514.     end
  515.     return numb
  516. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement