Advertisement
Ezteyh

bombardierNSEW

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