Advertisement
Ezteyh

goto

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