wv1106

airstrike turtle computercraft

Apr 17th, 2021 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.95 KB | None | 0 0
  1. local passcode = "1234" --change this code please don't leave it at 1234
  2. rednet.open("right")
  3. local id = os.getComputerID()
  4. local false_pincode_atempts = 0
  5. function checkfuel()
  6.     if turtle.getFuelLevel() < 20 then
  7.         turtle.select(1)
  8.         turtle.refuel()
  9.     end
  10. end
  11. os.setComputerLabel("")
  12. function recieve()
  13.     orders = false
  14.    
  15.     while orders == false do
  16.        
  17.         local id, msg = rednet.receive("airstrike")
  18.         local cords = textutils.unserialise(msg)
  19.         if #cords == 5 then
  20.             if passcode == cords[1] then
  21.                 tx = tonumber(cords[2])
  22.                 ty = tonumber(cords[3])
  23.                 tz = tonumber(cords[4])
  24.                 tnt = tonumber(cords[5])
  25.                 orders = true
  26.                 rednet.send(id, "turtle "..id.." on there way", "airstrike")
  27.             else
  28.                 rednet.send(id, "acces denied", "airstrike")
  29.                 false_pincode_atempts = false_pincode_atempts + 1
  30.                 if false_pincode_atempts == 1 then
  31.                     os.setComputerLabel("1 false pincode recieved")
  32.                 else
  33.                     os.setComputerLabel(false_pincode_atempts.." false pincodes recieved")
  34.                 end
  35.                
  36.             end
  37.         end
  38.     end
  39.     os.setComputerLabel("")
  40. end
  41. --here starts the code
  42. local hx, hy, hz = gps.locate()
  43. function bomb()
  44.     redstone.setOutput("bottom", true)
  45.     turtle.select(2)
  46.     for i=1, tnt do
  47.         turtle.placeDown()
  48.         sleep(1)
  49.     end
  50.     turtle.select(1)
  51.     redstone.setOutput("bottom", false)
  52. end
  53.        
  54. function up()
  55.     if hy > ty then
  56.         for i=1,10 do
  57.             checkfuel()
  58.             while not turtle.up() do
  59.                 turtle.sigUp()
  60.  
  61.             end
  62.         end
  63.     else
  64.         for i=1,((ty+20) - hy) do
  65.             checkfuel()
  66.             while not turtle.up() do
  67.                 turtle.digUp()
  68.  
  69.             end
  70.         end
  71.     end
  72. end
  73. function forward()
  74.     checkfuel()
  75.     while not turtle.forward() do
  76.         turtle.dig()
  77.  
  78.     end
  79. end
  80. function down()
  81.     checkfuel()
  82.     while not turtle.detectDown() do
  83.         turtle.down()
  84.     end
  85. end
  86. function calcdir()
  87.     local hx, hy, hz = gps.locate()
  88.     dx = hx - tx
  89.     dz = hz - tz
  90. end
  91. function go()
  92.    
  93.     if dx<=0 then
  94.         turtle.turnRight()
  95.         mx = math.abs(dx)
  96.         for i=1,mx do
  97.             forward()
  98.         end
  99.         if dz<=0 then
  100.             turtle.turnRight()
  101.             mz = math.abs(dz)
  102.             for i=1,mz do
  103.                 forward()
  104.             end
  105.         elseif dz>0 then
  106.             turtle.turnLeft()
  107.             mz = math.abs(dz)
  108.             for i=1,mz do
  109.                 forward()
  110.             end
  111.         end
  112.     elseif dx>0 then
  113.         turtle.turnLeft()
  114.         mx = math.abs(dx)
  115.        
  116.         for i=1,mx do
  117.             forward()
  118.         end
  119.         if dz>0 then
  120.             turtle.turnRight()
  121.             mz = math.abs(dz)
  122.             for i=1,dz do
  123.                 forward()
  124.             end
  125.         elseif dz<=0 then
  126.             turtle.turnLeft()
  127.             mz = math.abs(dz)
  128.             for i=1,mz do
  129.                 forward()
  130.             end
  131.         end
  132.        
  133.     end
  134. end
  135. function rtrn()
  136.     turtle.turnLeft()
  137.     turtle.turnLeft()
  138.     for i=1,mz do
  139.         forward()
  140.     end
  141.     if dz>0 then
  142.         if dx<0 then
  143.             turtle.turnRight()
  144.         elseif dx>0 then
  145.             turtle.turnLeft()
  146.         end
  147.     elseif dz<=0 then
  148.         if dx>0 then
  149.             turtle.turnRight()
  150.         elseif dx<0 then
  151.             turtle.turnLeft()
  152.         end
  153.     end
  154.    
  155.     for i=1,mx do
  156.         forward()
  157.     end
  158.     if dx>0 then
  159.         turtle.turnLeft()  
  160.     elseif dx<0 then
  161.         turtle.turnRight()    
  162.     end
  163. end
  164.  
  165. --script
  166.  
  167. while 1 do
  168.     local hx, hy, hz = gps.locate()
  169.     recieve()
  170.     calcdir()
  171.  
  172.    
  173.     up()
  174.     go()
  175.     bomb()
  176.  
  177.     rtrn()
  178.     down()
  179. end
  180.  
  181.  
Add Comment
Please, Sign In to add comment