Advertisement
Ranger15

goto

Sep 13th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.97 KB | None | 0 0
  1. -- Wireless Miner Turtle GPS goto program
  2.  
  3. local tArgs = {...}
  4. local x = tonumber(tArgs[1])
  5. local y = tonumber(tArgs[3])
  6. local z = tonumber(tArgs[2])
  7. local dirs = string
  8. local dir, disx, disy, disz = 0, 0, 0, 0
  9. local height, forward = 0, 0
  10. local ox, oy, oz = 0, 0, 0
  11.  
  12. if #tArgs < 3 or #tArgs > 3 then
  13.  print( "Usage: <Program> <x> <z> <y>" )
  14.  return
  15. end
  16.  
  17. function fuellevel()
  18.  fuel = turtle.getFuelLevel()
  19.  if fuel == "unlimited" then
  20.   print("Fuel Level: Unlimited")
  21.   return true
  22.  end
  23.  if fuel > 79 then
  24.   print("Fuel Level: "..tostring(fuel))
  25.   return true
  26.  else
  27.   print("Fuel Level: Low")
  28.   print("Please refuel Turtle")
  29.   return false
  30.  end
  31. end
  32.  
  33. function calc()
  34. -- Getting current location
  35. -- Original x (ox)...
  36.  ox, oy, oz = gps.locate(3)
  37. -- Solving for distance by subtracting new location from current location
  38. -- Distance x (disx)...
  39.  disx = ox - x
  40.  disy = oy - y
  41.  disz = oz - z
  42.  getdir()
  43. end
  44.  
  45. function getdir()
  46. -- Solving for what direction the turtle is faceing
  47.  if turtle.detect() == true then
  48.   if not turtle.back() then
  49.    print( "Please remove block in front or back of turtle" )
  50.    sleep(5)
  51.    os.reboot()
  52.   else
  53. -- When forward = 1, means the turtle went backwards
  54.    forward = 1
  55. -- Getting new GPS location
  56. -- Directional new x (dnx) ...
  57.    dnx, dny, dnz = gps.locate(3)
  58.    sleep(0.2)
  59.    turtle.forward()
  60.    sleep(0.2)
  61.   end
  62.  else
  63. -- When forward = 0, means the turtle went forwards
  64.   forward = 0
  65.   turtle.forward()
  66. -- Getting new GPS location
  67. -- Directional new x (dnx) ...
  68.   dnx, dny, dnz = gps.locate(3)
  69.   sleep(0.2)
  70.   turtle.back()
  71.   sleep(0.2)
  72.  end
  73. -- Solving direction by subtracting directional x/y from original x/y
  74. -- Directional curent x (dcx)...
  75.  dcx = ox - dnx
  76.  dcz = oz - dnz
  77. -- If the diferances in dcx or dcz are negative/Positive and if the turtle went backwards/forwards determins the direction
  78. -- Direction (dir) and Direction String (dirs)
  79.  if dcx > 0 then
  80.   if forward == 0 then
  81.    dirs = "West"
  82.    dir = 1
  83.   else
  84.    dirs = "East"
  85.    dir = 3
  86.   end
  87.  end
  88.  if dcx < 0 then
  89.   if forward == 0 then
  90.    dirs = "East"
  91.    dir = 3
  92.   else
  93.    dirs = "West"
  94.    dir = 1
  95.   end
  96.  end
  97.  if dcz > 0 then
  98.   if forward == 0 then
  99.    dirs = "North"
  100.    dir = 2
  101.   else
  102.    dirs = "South"
  103.    dir = 0
  104.   end
  105.  end
  106.  if dcz < 0 then
  107.   if forward == 0 then
  108.    dirs = "South"
  109.    dir = 0
  110.   else
  111.    dirs = "North"
  112.    dir = 2
  113.   end
  114.  end
  115. end
  116.  
  117. function tryfwd()
  118.  while turtle.detect() == true do
  119.   tryup()
  120.   turtle.up()
  121.   height = height + 1
  122.  end
  123. end
  124.  
  125. function dobck()
  126.  while not turtle.back() do
  127.   tryup()
  128.   turtle.up()
  129.   height = height + 1
  130.  end
  131. end
  132.  
  133. function tryup()
  134.  if turtle.detectUp() == true then
  135.   turtle.digUp()
  136.  end
  137. end
  138.  
  139. function trydwn()
  140.  if turtle.detectDown() == true then
  141.   turtle.digDown()
  142.  end
  143. end
  144.  
  145. function updown()
  146. -- If distance y is a neagtive number then make it positive
  147. -- Modified Distance Y (mdisy)...
  148.  if disy < 0 then
  149.   mdisy = disy * -1
  150.   for a = 1, mdisy do
  151.    tryup()
  152.    turtle.up()
  153.   end
  154.  end
  155.  if disy > 0 then
  156.   for b = 1, disy do
  157.    trydwn()
  158.    turtle.down()
  159.   end
  160.  end
  161. end
  162.  
  163. -- This block of code determins how far to go and what direction to go, baised off of what direction the turtle is facing,
  164. -- and if disx/disy/disz is positive or negative
  165. function move()
  166. -- If facing West (1)
  167.  if dir == 1 then
  168.   updown()
  169.   if disx < 0 then
  170.    mdisx = disx * -1
  171.    for c = 1, mdisx do
  172.     dobck()
  173.    end
  174.   end
  175.   if disx > 0 then
  176.    for d = 1, disx do
  177.     tryfwd()
  178.     turtle.forward()
  179.    end
  180.   end
  181.   if disz < 0 then
  182.    mdisz = disz * -1
  183.    turtle.turnLeft()
  184.    for e = 1, mdisz do
  185.     tryfwd()
  186.     turtle.forward()
  187.    end
  188.   end
  189.   if disz > 0 then
  190.    turtle.turnRight()
  191.    for f = 1, disz do
  192.     tryfwd()
  193.     turtle.forward()
  194.    end
  195.   end
  196.  end
  197. -- If facing North (2)
  198.  if dir == 2 then
  199.   updown()
  200.   if disz < 0 then
  201.    mdisz = disz * -1
  202.    for i = 1, mdisz do
  203.     dobck()
  204.    end
  205.   end
  206.   if disz > 0 then
  207.    for j = 1, disz do
  208.     tryfwd()
  209.     turtle.forward()
  210.    end
  211.   end
  212.   if disx < 0 then
  213.    mdisx = disx * -1
  214.    turtle.turnRight()
  215.    for k = 1, mdisx do
  216.     tryfwd()
  217.     turtle.forward()
  218.    end
  219.   end
  220.   if disx > 0 then
  221.    turtle.turnLeft()
  222.    for l = 1, disx do
  223.     tryfwd()
  224.     turtle.forward()
  225.    end
  226.   end
  227.  end
  228. -- If facing East (3)
  229.  if dir == 3 then
  230.   updown()
  231.   if disx < 0 then
  232.    mdisx = disx * -1
  233.    for o = 1, mdisx do
  234.     tryfwd()
  235.     turtle.forward()
  236.    end
  237.   end
  238.   if disx > 0 then
  239.    for p = 1, disx do
  240.     dobck()
  241.    end
  242.   end
  243.   if disz < 0 then
  244.    mdisz = disz * -1
  245.    turtle.turnRight()
  246.    for q = 1, mdisz do
  247.     tryfwd()
  248.     turtle.forward()
  249.    end
  250.   end
  251.   if disz > 0 then
  252.    turtle.turnLeft()
  253.    for r = 1, disz do
  254.     tryfwd()
  255.     turtle.forward()
  256.    end
  257.   end
  258.  end
  259.  -- If facing South (0)
  260.  if dir == 0 then
  261.   updown()
  262.   if disz < 0 then
  263.    mdisz = disz * -1
  264.    for u = 1, mdisz do
  265.     tryfwd()
  266.     turtle.forward()
  267.    end
  268.   end
  269.   if disz > 0 then
  270.    for v = 1, disz do
  271.     dobck()
  272.    end
  273.   end
  274.   if disx < 0 then
  275.    mdisx = disx * -1
  276.    turtle.turnLeft()
  277.    for w = 1, mdisx do
  278.     tryfwd()
  279.     turtle.forward()
  280.    end
  281.   end
  282.   if disx > 0 then
  283.    turtle.turnRight()
  284.    for aa = 1, disx do
  285.     tryfwd()
  286.     turtle.forward()
  287.    end
  288.   end
  289.  end
  290. -- If the turtle had to go up because of an obstacle this will make the turtle go down to it's destination
  291.  for ab = 1, height do
  292.   trydwn()
  293.   turtle.down()
  294.  end
  295. end
  296.  
  297. --rednet.open("left")
  298. rednet.broadcast("going")
  299. term.clear()
  300. term.setCursorPos(1, 1)
  301. if fuellevel() == true then
  302.  calc()
  303.  sleep(0.2)
  304.  print("Facing: "..dirs)
  305.  print("Distance: X = "..disx..", Z = "..disz..", Y = "..disy)
  306.  print("")
  307.  sleep(0.2)
  308.  move()
  309. -- Checking if the program was successful
  310. -- Destination x (dx)...
  311.  dx, dy, dz = gps.locate(3)
  312.  if dx == x and dy == y and dz == z then
  313.   print("Have arrived at "..x..", "..z..", "..y )
  314.  else
  315.   print("Something went wrong, try again")
  316.   os.reboot()
  317.  end
  318. end
  319.  
  320. os.version()
  321. print( "exit" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement