Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.30 KB | None | 0 0
  1. --[[
  2. Slot 1: Stone
  3. Slot 2: Dirt
  4. Slot 3: Sand
  5. Slot 4: Gravel
  6. Slot 15: Bucket
  7. Slot 16: Fuel
  8. ]]--
  9.  
  10. local ok, tArgs, ignoredFuel, oldprint, fuelAmount, nSlots = true, { ... }, 0, print, nil
  11.  
  12. for i = 1, 13 do
  13.     if turtle.getItemCount( i ) == 0 then
  14.         nSlots = i - 1
  15.         print( "You have "..nSlots.." stacks of waste blocks, is this correct? Y/N" )
  16.         while true do
  17.             local _, char = os.pullEvent( "char" )
  18.             if char:lower() == "n" then
  19.                 error()
  20.             elseif char:lower() == "y" then
  21.                 break
  22.             end
  23.         end
  24.         break
  25.     end
  26. end
  27.  
  28. if turtle.getItemCount( 15 ) ~= 1 then
  29.     error( "Place a single bucket in slot 15" )
  30. end
  31. if turtle.getItemCount( 16 ) == 0 then
  32.     print( "Are you sure you wish to continue with no fuel in slot 16? Y/N" )
  33.     while true do
  34.         local _, char = os.pullEvent( "char" )
  35.         if char:lower() == "n" then
  36.             error()
  37.         elseif char:lower() == "y" then
  38.             break
  39.         end
  40.     end
  41. end
  42.  
  43. local function print( text )
  44.     oldprint( "[" .. os.time() .. "]" .. text )
  45.     local file = fs.open( "turtleLog", "a" )
  46.     file.writeLine( "[" .. os.time() .. "]" .. text )
  47.     file.close()
  48. end
  49.  
  50. function dumpWaste()
  51.     while ok do
  52.         for i = 1, nSlots do
  53.             local count = turtle.getItemCount( i )
  54.             if count > 1 then
  55.                 turtle.select( i )
  56.                 turtle.drop( count - 1 )
  57.             end
  58.         end
  59.         local id = os.startTimer( 10 )
  60.         while true do
  61.             local _, tid = os.pullEvent( "timer" )
  62.             if tid == id then
  63.                 break
  64.             end
  65.         end
  66.     end
  67. end
  68.  
  69. function notwaste( func )
  70.     for i = 1, nSlots do
  71.         turtle.select( i )
  72.         if func() then
  73.             return false
  74.         end
  75.     end
  76.     if func == turtle.compare then
  77.         return turtle.detect()
  78.     elseif func == turtle.compareDown then
  79.         return turtle.detectDown()
  80.     elseif func == turtle.compareUp then
  81.         return turtle.detectUp()
  82.     end
  83. end
  84.  
  85. function check( nLevel )
  86.     if not nLevel then
  87.         nLevel = 1
  88.     elseif nLevel > 200 then
  89.         return
  90.     end
  91.     if not ok then return end
  92.     --check for lava
  93.     turtle.select( 14 )
  94.     if turtle.getItemCount( 14 ) == 0 and not turtle.compare() and not turtle.detect() then
  95.         turtle.select( 15 )
  96.         if turtle.place() then
  97.             print( "[check]: Liquid detected!" )
  98.             if turtle.refuel() then
  99.                 print( "[check]: Refueled using lava source!" )
  100.                 turtle.forward()
  101.                 check( nLevel + 1 )
  102.                 while not turtle.back() do end
  103.                 ignoredFuel = ignoredFuel + 2
  104.             else
  105.                 print( "[check]: Liquid was not lava!" )
  106.                 turtle.place()
  107.             end
  108.         end
  109.     end
  110.     --check for inventories
  111.     if turtle.detect() and turtle.suck() then
  112.         while turtle.suck() do end
  113.     end
  114.     --check for ore
  115.     if notwaste( turtle.compare ) then
  116.         print( "[check]: Ore Detected!" )
  117.         repeat turtle.dig() until turtle.forward()
  118.         print( "[check]: Dug ore!" )
  119.         check( nLevel + 1 )
  120.         while not turtle.back() do end
  121.         ignoredFuel = ignoredFuel + 2
  122.     end
  123.     if not ok then return end
  124.     turtle.turnLeft()
  125.     --check for lava
  126.     turtle.select( 14 )
  127.     if turtle.getItemCount( 14 ) == 0 and not turtle.compare() and not turtle.detect() then
  128.         turtle.select( 15 )
  129.         if turtle.place() then
  130.             print( "[check]: Liquid detected!" )
  131.             if turtle.refuel() then
  132.                 print( "[check]: Refueled using lava source!" )
  133.                 turtle.forward()
  134.                 check( nLevel + 1 )
  135.                 while not turtle.back() do end
  136.                 ignoredFuel = ignoredFuel + 2
  137.             else
  138.                 print( "[check]: Liquid was not lava!" )
  139.                 turtle.place()
  140.             end
  141.         end
  142.     end
  143.     --check for inventories
  144.     if turtle.detect() and turtle.suck() then
  145.         while turtle.suck() do end
  146.     end
  147.     --check for ore
  148.     if notwaste( turtle.compare ) then
  149.         print( "[check]: Ore Detected!" )
  150.         repeat turtle.dig() until turtle.forward()
  151.         print( "[check]: Dug ore!" )
  152.         check( nLevel + 1 )
  153.         while not turtle.back() do end
  154.         ignoredFuel = ignoredFuel + 2
  155.     end
  156.     turtle.turnRight()
  157.     if not ok then return end
  158.     turtle.turnRight()
  159.     --check for lava
  160.     turtle.select( 14 )
  161.     if turtle.getItemCount( 14 ) == 0 and not turtle.compare() and not turtle.detect() then
  162.         turtle.select( 15 )
  163.         if turtle.place() then
  164.             print( "[check]: Liquid detected!" )
  165.             if turtle.refuel() then
  166.                 print( "[check]: Refueled using lava source!" )
  167.                 turtle.forward()
  168.                 check( nLevel + 1 )
  169.                 while not turtle.back() do end
  170.                 ignoredFuel = ignoredFuel + 2
  171.             else
  172.                 print( "[check]: Liquid was not lava!" )
  173.                 turtle.place()
  174.             end
  175.         end
  176.     end
  177.     --check for inventories
  178.     if turtle.detect() and turtle.suck() then
  179.         while turtle.suck() do end
  180.     end
  181.     --check for ore
  182.     if notwaste( turtle.compare ) then
  183.         print( "[check]: Ore Detected!" )
  184.         repeat turtle.dig() until turtle.forward()
  185.         print( "[check]: Dug ore!" )
  186.         check( nLevel + 1 )
  187.         while not turtle.back() do end
  188.         ignoredFuel = ignoredFuel + 2
  189.     end
  190.     turtle.turnLeft()
  191.     if not ok then return end
  192.     --check for lava
  193.     turtle.select( 14 )
  194.     if turtle.getItemCount( 14 ) == 0 and not turtle.compareUp() and not turtle.detectUp() then
  195.         turtle.select( 15 )
  196.         if turtle.placeUp() then
  197.             print( "[check]: Liquid detected!" )
  198.             if turtle.refuel() then
  199.                 print( "[check]: Refueled using lava source!" )
  200.                 turtle.up()
  201.                 check( nLevel + 1 )
  202.                 while not turtle.down() do end
  203.                 ignoredFuel = ignoredFuel + 2
  204.             else
  205.                 print( "[check]: Liquid was not lava!" )
  206.                 turtle.placeUp()
  207.             end
  208.         end
  209.     end
  210.     --check for inventories
  211.     if turtle.detectUp() and turtle.suckUp() then
  212.         while turtle.suckUp() do end
  213.     end
  214.     --check for ore
  215.     if notwaste( turtle.compareUp ) then
  216.         print( "[check]: Ore Detected!" )
  217.         repeat turtle.digUp() until turtle.up()
  218.         print( "[check]: Dug ore!" )
  219.         check( nLevel + 1 )
  220.         while not turtle.down() do end
  221.         ignoredFuel = ignoredFuel + 2
  222.     end
  223.     if not ok then return end
  224.     --check for lava
  225.     turtle.select( 14 )
  226.     if turtle.getItemCount( 14 ) == 0 and not turtle.compareDown() and not turtle.detectDown() then
  227.         turtle.select( 15 )
  228.         if turtle.placeDown() then
  229.             print( "[check]: Liquid detected!" )
  230.             if turtle.refuel() then
  231.                 print( "[check]: Refueled using lava source!" )
  232.                 turtle.down()
  233.                 check( nLevel + 1 )
  234.                 while not turtle.up() do end
  235.                 ignoredFuel = ignoredFuel + 2
  236.             else
  237.                 print( "[check]: Liquid was not lava!" )
  238.                 turtle.placeDown()
  239.             end
  240.         end
  241.     end
  242.     --check for inventories
  243.     if turtle.detectDown() and turtle.suckDown() then
  244.         while turtle.suckDown() do end
  245.     end
  246.     --check for ore
  247.     if notwaste( turtle.compareDown ) then
  248.         print( "[check]: Ore Detected!" )
  249.         repeat turtle.digDown() until turtle.down()
  250.         print( "[check]: Dug ore!" )
  251.         check( nLevel + 1 )
  252.         while not turtle.up() do end
  253.         ignoredFuel = ignoredFuel + 2
  254.     end
  255. end
  256.  
  257. function branch()
  258.     local gone = 0
  259.     for i = 1, 15 do
  260.         repeat turtle.dig() until turtle.forward()
  261.         print( "[branch]: Dug forward!" )
  262.         gone = gone + 1
  263.         if not ok then break end
  264.         check()
  265.         if not ok then break end
  266.     end
  267.     print( "[branch]: Returning!" )
  268.     turtle.turnLeft()
  269.     turtle.turnLeft()
  270.     for i = 1, gone do
  271.         while not turtle.forward() do
  272.             while turtle.dig() do end
  273.             while turtle.attack() do end
  274.         end
  275.     end
  276.     ignoredFuel = ignoredFuel + ( gone * 2 )
  277.     print( "[branch]: Returned!" )
  278. end
  279.  
  280. function main()
  281.     local gone = 0
  282.     while ok do
  283.         for i = 1, 3 do
  284.             repeat turtle.dig() until turtle.forward()
  285.             print( "[main]: Dug forward!" )
  286.             gone = gone + 1
  287.             if not ok then break end --not ok, don't keep running
  288.             check()
  289.             if not ok then break end
  290.         end
  291.         if not ok then break end
  292.         turtle.turnLeft()
  293.         print( "[main]: Initiating branch!" )
  294.         branch()
  295.         turtle.turnLeft()
  296.         if not ok then break end --not ok, don't run second branch
  297.         turtle.turnRight()
  298.         print( "[main]: Intiating branch!" )
  299.         branch()
  300.         turtle.turnRight()
  301.     end
  302.     --not ok, return to base
  303.     print( "[main]: Returning to base!" )
  304.     turtle.turnLeft()
  305.     turtle.turnLeft()
  306.     repeat
  307.         while not turtle.forward() do
  308.             while turtle.attack() do end
  309.             while turtle.dig() do end
  310.         end
  311.         gone = gone - 1
  312.     until gone == 0
  313. end
  314.  
  315.  
  316. function findMaxLevel()
  317.     local level = turtle.getFuelLevel()
  318.     if turtle.getItemCount( 16 ) > 1 then
  319.         if not fuelAmount then
  320.             turtle.select( 16 )
  321.             turtle.refuel( 1 )
  322.             fuelAmount = turtle.getFuelLevel() - level
  323.             print( "[findMaxLevel]: Found fuelAmount: "..fuelAmount)
  324.         end
  325.         print( "[findMaxLevel]: Found max level: " .. turtle.getItemCount( 16 ) * fuelAmount + turtle.getFuelLevel() .. "!")
  326.         return turtle.getItemCount( 16 ) * fuelAmount + turtle.getFuelLevel()
  327.     else
  328.         print( "[findMaxLevel]: Found max level: " .. turtle.getFuelLevel() .. "!" )
  329.         return turtle.getFuelLevel()
  330.     end
  331. end
  332.  
  333. function isOk()
  334.     local okLevel = findMaxLevel() / 2 + 10
  335.     while ok do
  336.         local currentLevel = turtle.getFuelLevel()
  337.         if currentLevel < 100 then --check fuel
  338.             print( "[isOk]: Fuel Level Low!" )
  339.             if turtle.getItemCount( 16 ) > 0 then
  340.                 print( "[isOk]: Refueling!" )
  341.                 repeat
  342.                     turtle.select( 16 )
  343.                 until turtle.refuel( 1 ) or turtle.getSelectedSlot() == 16
  344.                 if turtle.getFuelLevel() > currentLevel then
  345.                     print( "[isOk]: Refuel Successful!" )
  346.                 else
  347.                     print( "[isOk]: Refuel Unsuccessful, Initiating return!" )
  348.                     ok = false
  349.                 end
  350.             end
  351.         elseif okLevel - ignoredFuel > findMaxLevel()  then
  352.             print("[isOk]: Fuel Reserves Depleted!  Initiating return!")
  353.             ok = false
  354.         end
  355.         --make sure turtle can take new items
  356.         local hasSpace = false
  357.         for i = 5, 15 do
  358.             if turtle.getItemCount( i ) == 0 then
  359.                 hasSpace = true
  360.             end
  361.         end
  362.         if not hasSpace then
  363.             print( "[isOk]: Out of space!  Intiating return!" )
  364.             ok = false
  365.         elseif ok then
  366.             print( "[isOk]: Everything is OK!" )
  367.             local id = os.startTimer( 10 )
  368.             while true do
  369.                 local _, tid = os.pullEvent( "timer" )
  370.                 if tid == id then
  371.                     break
  372.                 end
  373.             end
  374.         end
  375.     end
  376. end
  377.  
  378.  
  379. function trackTime()
  380.     local sTime = table.concat( tArgs, " " )
  381.     local nSeconds = 0
  382.     for i, period in sTime:gmatch( "(%d+)%s+(%a+)s?" ) do
  383.         if period:lower() == "second" then
  384.             nSeconds = nSeconds + i
  385.         elseif period:lower() == "minute" then
  386.             nSeconds = nSeconds + ( i * 60 )
  387.         elseif period:lower() == "hour" then
  388.             nSeconds = nSeconds + ( i * 3600 )
  389.         end
  390.     end
  391.     print( "[trackTime]: Starting timer for 7200 seconds!" )
  392.     local id = os.startTimer( 7200 )
  393.     while ok do
  394.         local _, tid = os.pullEvent( "timer" )
  395.         if id == tid then
  396.             print( "[trackTime]: End of session reached!  Returning to base!" )
  397.             ok = false
  398.         end
  399.     end
  400. end
  401.  
  402. parallel.waitForAll( trackTime, isOk, main, dumpWaste )
  403. for i = 5, 14 do
  404.     turtle.select( i )
  405.     turtle.dropDown()
  406. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement