Advertisement
kilovirus

Untitled

Apr 18th, 2014
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.82 KB | None | 0 0
  1. --http://pastebin.com/p51K7GEK
  2.  
  3. rednet.close("right")
  4. rednet.open("right")
  5.  
  6. function getDir()
  7.  print("EXTREMELY IMPORTANT!")
  8.  print("Enter the direction. . .")
  9.  print("Options")
  10.  print("[ 1 = North, 2 = East, 3 = South, 4 = West ]")
  11.  local continue = true
  12.  while continue == true do
  13.   local input = read()
  14.   if input == "1" then
  15.    continue = false
  16.    return 1
  17.   elseif input == "2" then
  18.    continue = false
  19.    return 2
  20.   elseif input == "3" then
  21.    continue = false
  22.    return 3
  23.   elseif input == "4" then
  24.    continue = false
  25.    return 4
  26.   else
  27.    print("Not valid input!")
  28.   end
  29.  end
  30. end
  31.  
  32. function getTravel()
  33.  print("Enter the height at which drones travel to and from quarry.")
  34.  print("Enter Number >")
  35.  local continue = true
  36.  while continue == true do
  37.   local input = read()
  38.   if tonumber(input) == nil then
  39.    print("Not valid input!")
  40.   elseif tonumber(input) < 0 then
  41.    print("Enter a positive number")
  42.   elseif tonumber(input) > 0 then
  43.    continue = false
  44.    return input
  45.   end
  46.  end
  47. end
  48.  
  49. function getNum()
  50.  print("Enter a number >")
  51.  local continue = true
  52.  while continue == true do
  53.   local input = read()
  54.   if tonumber(input) == nil then
  55.    print("Not valid input!")
  56.   elseif tonumber(input) > 0 or tonumber(input) < 0 then
  57.    continue = false
  58.    return input
  59.   end
  60.  end
  61. end
  62.  
  63. function getDumpLoc()
  64.  local x,y,z = 1
  65.  print("Type Dump Location Coordinates")
  66.  print("X")
  67.  x = getNum()
  68.  print("Y")
  69.  y = getNum()
  70.  print("Z")
  71.  z = getNum()
  72.  return x,y,z
  73. end
  74.  
  75. -- Variables --
  76.  
  77. isServerRunning = true
  78. dir = getDir()
  79. startDir = dir
  80. directions ={"north","east","south","west"}
  81. travelHeight = getTravel()
  82. hole = 2
  83. dumpLoc = vector.new(getDumpLoc())
  84. print("Type Dump Direction")
  85. dumpDir = getDir()
  86. startLoc = vector.new(gps.locate())
  87. loc = vector.new(gps.locate())
  88.  
  89. -- END Vars --
  90.  
  91. -- Hacked excavate functions --
  92.  
  93. local function turnLeft()
  94.  dir=dir-1
  95.  dir=(dir-1)%4
  96.  dir=dir+1
  97.  turtle.turnLeft()
  98. end
  99.  
  100. local function turnRight()
  101.  dir=dir-1
  102.  dir=(dir+1)%4
  103.  dir=dir+1
  104.  turtle.turnRight()
  105. end
  106.  
  107. function refuel(ammount)
  108.     local fuelLevel = turtle.getFuelLevel()
  109.     if fuelLevel == "unlimited" then
  110.         return true
  111.     end
  112.    
  113.     local needed = ammount
  114.     if turtle.getFuelLevel() < needed then
  115.         local fueled = false
  116.                 while fueled == false do
  117.         for n=1,16 do
  118.             if turtle.getItemCount(n) > 0 then
  119.                 turtle.select(n)
  120.                 if turtle.refuel(1) then
  121.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  122.                         turtle.refuel(1)
  123.                     end
  124.                     if turtle.getFuelLevel() >= needed then
  125.                         turtle.select(1)
  126.                         return true
  127.                     end
  128.                 end
  129.             end
  130.         end
  131.         end
  132.         turtle.select(1)
  133.         return false
  134.     end
  135.    
  136.     return true
  137. end
  138.  
  139.  
  140. local function unload()
  141.     print( "Unloading items..." )
  142.        local bKeepFuel = true
  143.     for n=1,16 do
  144.         local nCount = turtle.getItemCount(n)
  145.         if nCount > 0 then
  146.             turtle.select(n)           
  147.             local bDrop = true
  148.             if bKeepFuel and turtle.refuel(0) then
  149.                 bDrop = false
  150.                               bKeepFuel = false
  151.             end        
  152.             if bDrop then
  153.                 turtle.drop()
  154.             end
  155.         end
  156.     end
  157.     turtle.select(1)
  158. end
  159.  
  160. local function returnSupplies()
  161.     for i=1,20 do
  162.         turtle.up()
  163.     end
  164.     local resume = vector.new(gps.locate())
  165.         local resumeDir = dir
  166.     print( "Returning supplies..." )
  167.     goTo( dumpLoc.x,dumpLoc.y+travelHeight,dumpLoc.z, directions[dumpDir])
  168.         goTo( dumpLoc.x,dumpLoc.y-20,dumpLoc.z, directions[dumpDir])
  169.    
  170.     local fuelNeeded = 2*(dumpLoc.x+dumpLoc.y+dumpLoc.z) + 1
  171.     if not refuel( fuelNeeded ) then
  172.         unload( true )
  173.         print( "Waiting for fuel" )
  174.         while not refuel( fuelNeeded ) do
  175.             sleep(1)
  176.         end
  177.     else
  178.         unload( true ) 
  179.     end
  180.     print( "Going back to location..." )
  181.     goTo(resume.x,resume.y,resume.z,directions[resumeDir])
  182. end
  183.  
  184. local function collect()   
  185.     local bFull = true
  186.     local nTotalItems = 0
  187.     for n=1,16 do
  188.         local nCount = turtle.getItemCount(n)
  189.         if nCount == 0 then
  190.             bFull = false
  191.         end
  192.         nTotalItems = nTotalItems + nCount
  193.     end
  194.     if bFull then
  195.         print( "No empty slots left." )
  196.         return false
  197.     end
  198.     return true
  199. end
  200.  
  201. local function tryForwards()
  202.     if not refuel(100) then
  203.         print( "Not enough Fuel" )
  204.         returnSupplies()
  205.     end
  206.     while not turtle.forward() do
  207.         if turtle.detect() then
  208.             if turtle.dig() then
  209.                 if not collect() then
  210.                     returnSupplies()
  211.                 end
  212.             else
  213.                 return false
  214.             end
  215.         elseif turtle.attack() then
  216.             if not collect() then
  217.                 returnSupplies()
  218.             end
  219.         else
  220.             sleep( 0.5 )
  221.         end
  222.     end
  223.     return true
  224. end
  225.  
  226. local function tryDown()
  227.     if not refuel(100) then
  228.         print( "Not enough Fuel" )
  229.         returnSupplies()
  230.     end
  231.     while not turtle.down() do
  232.         if turtle.detectDown() then
  233.             if turtle.digDown() then
  234.                 if not collect() then
  235.                     returnSupplies()
  236.                 end
  237.             else
  238.                 return false
  239.             end
  240.         elseif turtle.attackDown() then
  241.             if not collect() then
  242.                 returnSupplies()
  243.             end
  244.         else
  245.             sleep( 0.5 )
  246.         end
  247.     end
  248.     return true
  249. end
  250.  
  251. function goTo( x, y, z, face)
  252.        local x,y,z = tonumber(x),tonumber(y),tonumber(z)
  253.        loc = vector.new(gps.locate())
  254.        local xPos,depth,zPos = loc.x,loc.y,loc.z
  255.        if not refuel(100) then
  256.         print("Waiting for Fuel. . .")
  257.        end
  258.  
  259.     while depth < y do
  260.         if turtle.up() then
  261.             depth = depth + 1
  262.         elseif turtle.digUp() or turtle.attackUp() then
  263.             collect()
  264.         else
  265.             sleep( 0.5 )
  266.         end
  267.     end
  268.  
  269.     loc = vector.new(gps.locate())
  270.         local xPos,depth,zPos = loc.x,loc.y,loc.z
  271.  
  272.     if xPos > x then
  273.         while directions[dir] ~= "west" do
  274.             turnLeft()
  275.         end
  276.         while xPos > x do
  277.             if turtle.forward() then
  278.                 xPos = xPos - 1
  279.             elseif turtle.dig() or turtle.attack() then
  280.                 collect()
  281.             else
  282.                 sleep( 0.5 )
  283.             end
  284.         end
  285.     elseif xPos < x then
  286.         while directions[dir] ~= "east" do
  287.             turnLeft()
  288.         end
  289.         while xPos < x do
  290.             if turtle.forward() then
  291.                 xPos = xPos + 1
  292.             elseif turtle.dig() or turtle.attack() then
  293.                 collect()
  294.             else
  295.                 sleep( 0.5 )
  296.             end
  297.         end
  298.     end
  299.    
  300.     if zPos > z then
  301.         while directions[dir] ~= "north" do
  302.             turnLeft()
  303.         end
  304.         while zPos > z do
  305.             if turtle.forward() then
  306.                 zPos = zPos - 1
  307.             elseif turtle.dig() or turtle.attack() then
  308.                 collect()
  309.             else
  310.                 sleep( 0.5 )
  311.             end
  312.         end
  313.     elseif zPos < z then
  314.         while directions[dir] ~= "south" do
  315.             turnLeft()
  316.         end
  317.         while zPos < z do
  318.             if turtle.forward() then
  319.                         zPos = zPos + 1
  320.             elseif turtle.dig() or turtle.attack() then
  321.                 collect()
  322.             else
  323.                 sleep( 0.5 )
  324.             end
  325.         end
  326.     end
  327.    
  328.     while depth > y do
  329.         if turtle.down() then
  330.                     depth = depth - 1
  331.         elseif turtle.digDown() or turtle.attackDown() then
  332.             collect()
  333.         else
  334.             sleep( 0.5 )
  335.         end
  336.     end
  337.    
  338.     while directions[dir] ~= face do
  339.         turnLeft()
  340.     end
  341. end
  342.  
  343. function excavate()
  344. local alternate = 0
  345. local done = false
  346. local size = tonumber(hole)
  347. while not done do
  348.     for n=1,size do
  349.         for m=1,size-1 do
  350.             if not tryForwards() then
  351.                 done = true
  352.                 break
  353.             end
  354.         end
  355.         if done then
  356.             break
  357.         end
  358.         if n < size then
  359.             if math.fmod(n + alternate,2) == 0 then
  360.                 turnLeft()
  361.                 if not tryForwards() then
  362.                     done = true
  363.                     break
  364.                 end
  365.                 turnLeft()
  366.             else
  367.                 turnRight()
  368.                 if not tryForwards() then
  369.                     done = true
  370.                     break
  371.                 end
  372.                 turnRight()
  373.             end
  374.         end
  375.     end
  376.     if done then
  377.         break
  378.     end
  379.    
  380.     if size > 1 then
  381.         if math.fmod(size,2) == 0 then
  382.             turnRight()
  383.         else
  384.             if alternate == 0 then
  385.                 turnLeft()
  386.             else
  387.                 turnRight()
  388.             end
  389.             alternate = 1 - alternate
  390.         end
  391.     end
  392.    
  393.     if not tryDown() then
  394.         done = true
  395.         break
  396.     end
  397. end
  398. print( "Job's Done" )
  399. end
  400.  
  401. function mine(x,y,z)
  402.  refuel(100)
  403.  goTo(x,y+travelHeight,z,"north")
  404.  goTo(x,y,z,"north")
  405.  excavate()
  406.  goTo(startLoc.x,startLoc.y+travelHeight,startLoc.z, directions[startDir])
  407.  goTo(startLoc.x,startLoc.y,startLoc.z, directions[startDir])
  408.  returnSupplies()
  409. end
  410.  
  411. -- End Movement--
  412.  
  413. -- Botnet Functions --
  414.  
  415. function establishConnection()
  416.  print("Waiting for server response...")
  417.  local continue = true
  418.  while continue == true do
  419.   rednet.broadcast("Query: CNC ID")
  420.   id,message,dist = rednet.receive(2)
  421.   if message == "ID Sent" then
  422.    continue = false
  423.    return id
  424.   end
  425.  end
  426. end
  427.  
  428. function job()
  429.  while isServerRunning == true do
  430.   rednet.send(establishConnection(),"Query: Job")
  431.   id,message,dist = rednet.receive()
  432.   if message == "Yes" then
  433.    --Receive the hole size
  434.    id,message,diet = rednet.receive()
  435.    hole = message
  436.    -- Receive the X Y Z coordinates
  437.    id,message,dist = rednet.receive()
  438.    x = message
  439.    id,message,dist = rednet.receive()
  440.    y = tonumber(message)
  441.    id,message,dist = rednet.receive()
  442.    z = message
  443.    print("Location X: "..x.." Y: "..y.." Z: "..z)
  444.    rednet.send(id, "Quarry Starting")
  445.    mine(x,y,z)  
  446.   end
  447.  end
  448. end
  449.  
  450. --End Botnet--
  451.  
  452. -- MAIN --
  453.  
  454. job()
  455.  
  456. -- End Main --
  457.  
  458. -- END of File --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement