Advertisement
chaos511

yatqp.lua

Feb 18th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.67 KB | None | 0 0
  1. os.loadAPI("flex")
  2. os.loadAPI("dig")
  3.  
  4. local args = {...}
  5. if #args < 1 then
  6.     flex.send("Usage: start sethome home",colors.lightBlue)
  7.     return
  8. end --if
  9.  
  10.  
  11.  
  12. local home = {}
  13. local autofuel= "false"
  14. local xmax = nil
  15. local zmax = nil
  16. local ymin = nil
  17. local fuelLevel,requiredFuel,c,x,y,z,r,loc
  18. local xdir,zdir  = 1, 1
  19.  
  20. if fs.exists("quarry_home.txt") then
  21.     local homefile = fs.open("quarry_home.txt","r")
  22.     home[1] = tonumber(homefile.readLine())
  23.     home[2] = tonumber(homefile.readLine())
  24.     home[3] = tonumber(homefile.readLine())
  25.     home[4] = 180
  26.     home[5] = "?"
  27.     homefile.close()
  28.  
  29. end --if
  30.  
  31. local function loadCoords()
  32.     local file,loc,x
  33.     quarryfile = fs.open("quarry_save.txt","r")
  34.     dig.goto(tonumber(quarryfile.readLine()),tonumber(quarryfile.readLine()),tonumber(quarryfile.readLine()),tonumber(quarryfile.readLine()))
  35.    
  36.     xmax=tonumber(quarryfile.readLine())
  37.     ymax=tonumber(quarryfile.readLine())
  38.     zmax=tonumber(quarryfile.readLine())
  39.    
  40.     xdir=tonumber(quarryfile.readLine())
  41.     zdir=tonumber(quarryfile.readLine())
  42.     quarryfile.close()
  43. end
  44.  
  45. local function saveCoords()
  46.     quarryfile = fs.open("quarry_save.txt","w")
  47.     quarryfile.writeLine(tostring(dig.getx()))
  48.     quarryfile.writeLine(tostring(dig.gety()))
  49.     quarryfile.writeLine(tostring(dig.getz()))
  50.     quarryfile.writeLine(tostring(dig.getr()))
  51.    
  52.     quarryfile.writeLine(tostring(xmax))
  53.     quarryfile.writeLine(tostring(ymax))
  54.     quarryfile.writeLine(tostring(zmax))
  55.  
  56.    
  57.     quarryfile.writeLine(tostring(xdir))
  58.     quarryfile.writeLine(tostring(zdir))
  59.     quarryfile.close()
  60. end
  61.  
  62. if args[1] == "home" then                   --home
  63.     flex.send("Home loaded: "
  64.     ..tostring(home[1])
  65.     .." , "
  66.     ..tostring(home[2])
  67.     .." , "
  68.     ..tostring(home[3])
  69.     )
  70.     if fs.exists("dig_save.txt") then
  71.         dig.loadCoords()
  72.     end
  73.     dig.goto(home)
  74.     return
  75. elseif args[1] == "resume" then             --resume
  76.     dig.loadCoords()
  77.     flex.send("Home loaded: "
  78.     ..tostring(home[1])
  79.     .." , "
  80.     ..tostring(home[2])
  81.     .." , "
  82.     ..tostring(home[3])
  83.     )
  84.     if fs.exists("quarry_save.txt") then
  85.         loadCoords()
  86.     elseif 1==1 then
  87.         flex.send("Unable to resume quarry",colors.red)
  88.         return
  89.     end
  90. elseif args[1] == "sethome" then            --sethome
  91.     if #args < 4 then
  92.         flex.send("Usage quarry sethome <x> <y> <z>",colors.lightBlue)
  93.         return
  94.     end --if
  95.     home[1] = tonumber(args[2]) or 0
  96.     home[2] = tonumber(args[3]) or 0
  97.     home[3] = tonumber(args[4]) or 0
  98.     local homefile = fs.open("quarry_home.txt","w")
  99.     for x=1,#home do
  100.         homefile.writeLine(tostring(home[x]))
  101.     end --for
  102.     homefile.close()
  103.  
  104.     flex.send("Home set: "
  105.     ..tostring(home[1])
  106.     .." , "
  107.     ..tostring(home[2])
  108.     .." , "
  109.     ..tostring(home[3])
  110.     )
  111.     return
  112. elseif args[1] == "setautofuel" then            --setautofuel
  113.     if #args < 4 then
  114.         flex.send("Usage: setautofuel <top,bottom,front,back,left,right,false>",colors.lightBlue)
  115.         return
  116.     end --if
  117.     local homefile = fs.open("quarry_settings.txt","w")
  118.     autofuel=args[2] or "false"
  119.     homefile.writeLine(autofuel)
  120.     homefile.close()
  121.  
  122.     flex.send("Auto Fuel Set: "..autofuel)
  123.     return
  124. elseif args[1] == "start" then
  125.     xmax = tonumber(args[2])
  126.     zmax = tonumber(args[3]) or xmax
  127.     ymin = -(tonumber(args[4]) or 256)
  128.     flex.send("Home loaded: "
  129.     ..tostring(home[1])
  130.     .." , "
  131.     ..tostring(home[2])
  132.     .." , "
  133.     ..tostring(home[3])
  134.     )
  135. else
  136.     flex.send("Usage: start sethome home ",colors.lightBlue)
  137.     return
  138. end
  139.  
  140. if xmax == nil or zmax == nil then
  141.     flex.send("Invalid dimensions: "
  142.     ..tostring(xmax)
  143.     .." , "
  144.     ..tostring(ymax)
  145.     .." , "
  146.     ..tostring(zmax)
  147.     ,colors.red)
  148.     return
  149. end
  150.  
  151.  
  152. if fs.exists("startup") and
  153.     fs.exists("dig_save.txt") then
  154.     dig.loadCoords()
  155. end --if
  156.  
  157. dig.makeStartup("yatqp resume",{})
  158.  
  159.  
  160. local function dropNotFuel()
  161.     flex.condense()
  162.     local a,x
  163.     a = false
  164.     for x=1,16 do
  165.         turtle.select(x)
  166.         if turtle.refuel(0) then
  167.             if a then turtle.drop() end
  168.                 a = true
  169.             else --if
  170.             turtle.drop()
  171.         end --if
  172.     end --for
  173. turtle.select(1)
  174. end --function
  175.  
  176.  
  177.     local file,loc,x
  178.  
  179.  
  180.  
  181. local done = false
  182.  
  183. while not done and not dig.isStuck() do
  184.     fuelLevel = turtle.getFuelLevel()-1
  185.     requiredFuel = math.abs(dig.getx())
  186.     + math.abs(dig.gety())
  187.     + math.abs(dig.getz())*2
  188.     + 200
  189.     c = true
  190.  
  191.     while fuelLevel <= requiredFuel and c do
  192.         for x=1,16 do
  193.             turtle.select(x)
  194.             if turtle.refuel(1) then
  195.                 break
  196.             end --if
  197.             if x == 16 then
  198.                 c = false
  199.             end --if
  200.         end --for
  201.         fuelLevel = turtle.getFuelLevel()-1
  202.     end --while
  203.  
  204.     if fuelLevel <= requiredFuel then           -- refuel
  205.         loc = dig.location()
  206.         flex.send("Fuel low; returning to home",colors.yellow)
  207.         dig.goto(home)
  208.         dropNotFuel()
  209.         flex.send("Waiting for fuel...",colors.orange)
  210.         while turtle.getFuelLevel()-1 <= requiredFuel do
  211.             for x=1,16 do
  212.                 turtle.select(x)
  213.                 if turtle.refuel(1) then break end
  214.             end --for
  215.  
  216.             local rotate = nil
  217.             if autofuel == "front" then rotate=180 end
  218.             if autofuel == "back"  then rotate=0   end
  219.             if autofuel == "left"  then rotate=90  end
  220.             if autofuel == "right" then rotate=270 end
  221.             if autofuel == "up"    then rotate=180 end
  222.             if autofuel == "down"  then rotate=180 end
  223.  
  224.             if rotate ~= nil then
  225.                 dig.gotor(rotate)
  226.                 for x=1,16 do
  227.                     turtle.select(x)
  228.                     if turtle.getItemCount(x)==0 then
  229.                    
  230.                         local suck
  231.                         if autofuel == "up"    then     suck=turtle.suckUp(64)
  232.                         elseif autofuel == "down"  then suck=turtle.suckDown(64)
  233.                         else
  234.                             suck=turtle.suck(64)
  235.                         end
  236.                        
  237.                         if suck==true then break end
  238.                        
  239.                     end
  240.                 end
  241.  
  242.             end
  243.            
  244.         end --while
  245.         flex.send("Refueled",colors.lime)
  246.         dig.gotoy(loc[2])
  247.         dig.goto(loc)
  248.     end                                                 --end refuel
  249.      
  250.     turtle.select(1)
  251.     if zdir == 1 then
  252.         dig.gotor(0)
  253.         while dig.getz() < zmax-1 do
  254.             dig.fwd()
  255.             if dig.isStuck() then
  256.                 done = true
  257.                 break
  258.             end --if
  259.         end --while
  260.     elseif zdir == -1 then
  261.         dig.gotor(180)
  262.         while dig.getz() > 0 do
  263.             dig.fwd()
  264.             if dig.isStuck() then
  265.                 done = true
  266.                 break
  267.             end --if
  268.         end --while
  269.     end --if/else
  270.  
  271.     if done then break end
  272.  
  273.     zdir = -zdir
  274.  
  275.     if dig.getx() == 0 and xdir == -1 then
  276.         dig.down()
  277.         xdir = 1
  278.     elseif dig.getx() == xmax-1 and xdir == 1 then
  279.         dig.down()
  280.         xdir = -1
  281.     else
  282.         dig.gotox(dig.getx()+xdir)
  283.     end --if/else
  284.     saveCoords()
  285.    
  286.     if turtle.getItemCount(15) > 0 then
  287.         loc = dig.location()
  288.         flex.send("Inventory full; returning to home",colors.yellow)
  289.         dig.goto(home)
  290.         dropNotFuel()
  291.         while turtle.getItemCount(15) > 0 do
  292.             dropNotFuel()
  293.         end --while
  294.         flex.send("Emptied",colors.yellow)
  295.         dig.gotoy(loc[2])
  296.         dig.goto(loc)
  297.     end --if
  298. end --while
  299.  
  300.  
  301. dig.goto(home)
  302. for x=1,16 do
  303.     turtle.select(x)
  304.     turtle.drop()
  305. end
  306.  
  307. turtle.select(1)
  308. dig.gotor(0)
  309.  
  310. if fs.exists("startup") then
  311.     shell.run("rm startup")
  312. end
  313. os.unloadAPI("dig")
  314. os.unloadAPI("flex")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement