Advertisement
Ezteyh

apocalypse

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