blunderhund

craplib

Mar 30th, 2021
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.95 KB | None | 0 0
  1. local craplib = {}
  2.  
  3. local unloaded = 0
  4. local collected = 0
  5.  
  6. local FUEL_CHEST = false
  7. local FUEL_LAVA = false
  8.  
  9. local fuelSlot = 1
  10.  
  11. local xPos, zPos = 0, 0
  12. local xDir, zDir = 0, 1
  13.  
  14. local height = 0
  15. local altitude = 0
  16. local downwards = true
  17.  
  18. local isInitialized = false
  19.  
  20. function craplib.getCollectedItems() return collected + unloaded end
  21.  
  22. function craplib.getFuelSlot() return fuelSlot end
  23.  
  24. function craplib.getAltitude() return altitude end
  25.  
  26. function craplib.setFuelChest() FUEL_CHEST = true end
  27.  
  28. function craplib.setFuelLava() FUEL_LAVA = true end
  29.  
  30. function craplib.setFuelSlot(nFuelSlot) fuelSlot = nFuelSlot end
  31.  
  32. function craplib.setAltitude(nAltitide) altitude = nAltitide end
  33.  
  34. function craplib.init(nFuelSlot)
  35.     if not isInitialized then
  36.         fuelSlot = nFuelSlot or nil
  37.  
  38.         if not craplib.refuel() then
  39.             print("Out of Fuel")
  40.             return
  41.         end
  42.     end
  43. end
  44.  
  45. function craplib.unload(_bKeepOneFuelStack)
  46.     print("Unloading items...")
  47.     for n = 1, 16 do
  48.         nCount = turtle.getItemCount(n)
  49.         if nCount > 0 then
  50.             turtle.select(n)
  51.             bDrop = true
  52.             if _bKeepOneFuelStack and turtle.refuel(0) then
  53.                 i = turtle.getItemSpace(n)
  54.                 turtle.suck(i)
  55.                 bDrop = false
  56.                 _bKeepOneFuelStack = false
  57.             end
  58.             if bDrop then
  59.                 turtle.drop()
  60.                 unloaded = unloaded + nCount
  61.             end
  62.         end
  63.     end
  64.     collected = 0
  65.     turtle.select(1)
  66. end
  67.  
  68. function craplib.supplyChest()
  69.     local x, y, z, xd, zd = xPos, height, zPos, xDir, zDir
  70.     craplib.goTo(0, 0, 0, -1, 0)
  71.     if fuelSlot then
  72.         turtle.select(fuelSlot)
  73.     else
  74.         turtle.select(1)
  75.     end
  76.     if FUEL_CHEST and not FUEL_LAVA then
  77.         craplib.turnRight()
  78.         if not turtle.refuel(0) then turtle.transferTo(16) end
  79.         i = turtle.getItemSpace(turtle.getSelectedSlot())
  80.         turtle.suck(i)
  81.         craplib.turnLeft()
  82.     elseif FUEL_CHEST and FUEL_LAVA then
  83.         craplib.turnRight()
  84.         while turtle.getFuelLimit() * 0.75 < turtle.getFuelLevel() do
  85.             turtle.drop()
  86.             sleep(1)
  87.             turtle.suck()
  88.             if not turtle.refuel() then break end
  89.         end
  90.         craplib.turnLeft()
  91.     end
  92.     craplib.goTo(x, y, z, xd, zd)
  93. end
  94.  
  95. function craplib.returnSupplies()
  96.     local x, y, z, xd, zd = xPos, height, zPos, xDir, zDir
  97.     print("Returning to starting point...")
  98.     craplib.goTo(0, 0, 0, 0, -1)
  99.  
  100.     craplib.supplyChest()
  101.  
  102.     fuelNeeded = 2 * (x + math.abs(y) + z) + 1
  103.     if not craplib.refuel(fuelNeeded) then
  104.         craplib.unload(true)
  105.         print("Waiting for fuel")
  106.         while not craplib.refuel(fuelNeeded) do
  107.             os.pullEvent("turtle_inventory")
  108.         end
  109.     else
  110.         craplib.unload(true)
  111.     end
  112.  
  113.     craplib.supplyChest()
  114.  
  115.     print("Resuming mining...")
  116.     craplib.goTo(x, y, z, xd, zd)
  117. end
  118.  
  119. function craplib.endTour()
  120.     craplib.goTo(0, 0, 0, 0, -1)
  121.     craplib.unload(true)
  122.     craplib.goTo(0, 0, 0, 0, 1)
  123.  
  124.     print("Collected " .. (collected + unloaded) .. " items total.")
  125. end
  126.  
  127. function craplib.collect()
  128.     bFull = true
  129.     nTotalItems = 0
  130.     for n = 1, 16 do
  131.         nCount = turtle.getItemCount(n)
  132.         if nCount == 0 then bFull = false end
  133.         nTotalItems = nTotalItems + nCount
  134.     end
  135.  
  136.     if nTotalItems > collected then
  137.         collected = nTotalItems
  138.         if math.fmod(collected + unloaded, 50) == 0 then
  139.             print("Mined " .. (collected + unloaded) .. " items.")
  140.         end
  141.     end
  142.  
  143.     if bFull then
  144.         print("No empty slots left.")
  145.         return false
  146.     end
  147.     return true
  148. end
  149.  
  150. function craplib.refuel(ammount)
  151.     fuelLevel = turtle.getFuelLevel()
  152.     if fuelLevel == "unlimited" then return true end
  153.  
  154.     needed = ammount or (xPos + zPos + math.abs(height) + 2)
  155.     if turtle.getFuelLevel() < needed then
  156.         local selectedSlot = turtle.getSelectedSlot()
  157.         if fuelSlot then
  158.             turtle.select(fuelSlot)
  159.             while turtle.getItemCount(fuelSlot) > 0 and turtle.getFuelLevel() <
  160.                 needed do turtle.refuel(1) end
  161.             if turtle.getFuelLevel() >= needed then
  162.                 turtle.select(selectedSlot)
  163.                 return true
  164.             end
  165.         else
  166.             for n = 1, 16 do
  167.                 if turtle.getItemCount(n) > 0 then
  168.                     turtle.select(n)
  169.                     if turtle.refuel(1) then
  170.                         while turtle.getItemCount(n) > 0 and
  171.                             turtle.getFuelLevel() < needed do
  172.                             turtle.refuel(1)
  173.                         end
  174.                         if turtle.getFuelLevel() >= needed then
  175.                             turtle.select(1)
  176.                             return true
  177.                         end
  178.                     end
  179.                 end
  180.             end
  181.         end
  182.         turtle.select(selectedSlot)
  183.         return false
  184.     end
  185.  
  186.     return true
  187. end
  188.  
  189. function craplib.tryForwards()
  190.     if not craplib.refuel() then
  191.         print("Not enough Fuel")
  192.         craplib.returnSupplies()
  193.     end
  194.  
  195.     while not turtle.forward() do
  196.         if turtle.detect() then
  197.             if turtle.dig() then
  198.                 if not craplib.collect() then
  199.                     craplib.returnSupplies()
  200.                 end
  201.             else
  202.                 return false
  203.             end
  204.         elseif turtle.attack() then
  205.             if not craplib.collect() then craplib.returnSupplies() end
  206.         else
  207.             sleep(0.5)
  208.         end
  209.     end
  210.  
  211.     xPos = xPos + xDir
  212.     zPos = zPos + zDir
  213.     return true
  214. end
  215.  
  216. function craplib.digUp()
  217.     if turtle.detectUp() then
  218.         if turtle.digUp() then
  219.             if not craplib.collect() then craplib.returnSupplies() end
  220.         end
  221.     end
  222. end
  223.  
  224. function craplib.digDown()
  225.     if turtle.detectDown() then
  226.         if turtle.digDown() then
  227.             if not craplib.collect() then craplib.returnSupplies() end
  228.         end
  229.     end
  230. end
  231.  
  232. function craplib.fallingBlocks()
  233.     turtle.suckUp()
  234.     while turtle.detectUp() do
  235.         if turtle.digUp() then
  236.             if not craplib.collect() then craplib.returnSupplies() end
  237.         end
  238.         sleep(1)
  239.     end
  240. end
  241.  
  242. function craplib.digForwards()
  243.     turtle.suckUp()
  244.     turtle.suck()
  245.     turtle.suckDown()
  246.     if altitude == 0 then
  247.         craplib.digUp()
  248.         craplib.digDown()
  249.         craplib.fallingBlocks()
  250.     elseif downwards then
  251.         craplib.digUp()
  252.         if (height > altitude) then
  253.             craplib.digDown()
  254.             craplib.fallingBlocks()
  255.         end
  256.     else
  257.         if (height < altitude) then craplib.digUp() end
  258.         craplib.digDown()
  259.         if (height < altitude) then craplib.fallingBlocks() end
  260.     end
  261.  
  262.     return craplib.tryForwards()
  263. end
  264.  
  265. function craplib.tryDown()
  266.     if not craplib.refuel() then
  267.         print("Not enough Fuel")
  268.         craplib.returnSupplies()
  269.     end
  270.  
  271.     while not turtle.down() do
  272.         if turtle.detectDown() then
  273.             if turtle.digDown() then
  274.                 if not craplib.collect() then
  275.                     craplib.returnSupplies()
  276.                 end
  277.             else
  278.                 return false
  279.             end
  280.         elseif turtle.attackDown() then
  281.             if not craplib.collect() then craplib.returnSupplies() end
  282.         else
  283.             sleep(0.5)
  284.         end
  285.     end
  286.  
  287.     height = height - 1
  288.  
  289.     return true
  290. end
  291.  
  292. function craplib.tryUp()
  293.     if not craplib.refuel() then
  294.         print("Not enough Fuel")
  295.         craplib.returnSupplies()
  296.     end
  297.  
  298.     while not turtle.up() do
  299.         if turtle.detectUp() then
  300.             if turtle.digUp() then
  301.                 if not craplib.collect() then
  302.                     craplib.returnSupplies()
  303.                 end
  304.             else
  305.                 return false
  306.             end
  307.         elseif turtle.attackUp() then
  308.             if not craplib.collect() then craplib.returnSupplies() end
  309.         else
  310.             sleep(0.5)
  311.         end
  312.     end
  313.  
  314.     height = height + 1
  315.  
  316.     return true
  317. end
  318.  
  319. function craplib.changeAltitude()
  320.     if downwards then
  321.         if not (height + 1 > 0) then craplib.digUp() end
  322.         if altitude == 0 then
  323.             return craplib.tryDown()
  324.         elseif (height > altitude) then
  325.             return craplib.tryDown()
  326.         end
  327.     else
  328.         if not (height - 1 < 0) then craplib.digDown() end
  329.         if altitude == 0 then
  330.             return craplib.tryUp()
  331.         elseif (height < altitude) then
  332.             return craplib.tryUp()
  333.         end
  334.     end
  335. end
  336.  
  337. function craplib.turnLeft()
  338.     turtle.turnLeft()
  339.     xDir, zDir = -zDir, xDir
  340. end
  341.  
  342. function craplib.turnRight()
  343.     turtle.turnRight()
  344.     xDir, zDir = zDir, -xDir
  345. end
  346.  
  347. function craplib.goTo(x, y, z, xd, zd)
  348.     while height < y do
  349.         if turtle.up() then
  350.             height = height + 1
  351.         elseif turtle.digUp() or turtle.attackUp() then
  352.             craplib.collect()
  353.         else
  354.             sleep(0.5)
  355.         end
  356.     end
  357.  
  358.     if xPos > x then
  359.         while xDir ~= -1 do craplib.turnLeft() end
  360.         while xPos > x do
  361.             if turtle.forward() then
  362.                 xPos = xPos - 1
  363.             elseif turtle.dig() or turtle.attack() then
  364.                 craplib.collect()
  365.             else
  366.                 sleep(0.5)
  367.             end
  368.         end
  369.     elseif xPos < x then
  370.         while xDir ~= 1 do craplib.turnLeft() end
  371.         while xPos < x do
  372.             if turtle.forward() then
  373.                 xPos = xPos + 1
  374.             elseif turtle.dig() or turtle.attack() then
  375.                 craplib.collect()
  376.             else
  377.                 sleep(0.5)
  378.             end
  379.         end
  380.     end
  381.  
  382.     if zPos > z then
  383.         while zDir ~= -1 do craplib.turnLeft() end
  384.         while zPos > z do
  385.             if turtle.forward() then
  386.                 zPos = zPos - 1
  387.             elseif turtle.dig() or turtle.attack() then
  388.                 craplib.collect()
  389.             else
  390.                 sleep(0.5)
  391.             end
  392.         end
  393.     elseif zPos < z then
  394.         while zDir ~= 1 do craplib.turnLeft() end
  395.         while zPos < z do
  396.             if turtle.forward() then
  397.                 zPos = zPos + 1
  398.             elseif turtle.dig() or turtle.attack() then
  399.                 craplib.collect()
  400.             else
  401.                 sleep(0.5)
  402.             end
  403.         end
  404.     end
  405.  
  406.     while height > y do
  407.         if turtle.down() then
  408.             height = height - 1
  409.         elseif turtle.digDown() or turtle.attackDown() then
  410.             craplib.collect()
  411.         else
  412.             sleep(0.5)
  413.         end
  414.     end
  415.  
  416.     while zDir ~= zd or xDir ~= xd do craplib.turnLeft() end
  417. end
  418.  
  419. function craplib.quarry(size, direction, fEnd)
  420.     local endFunction = fEnd or nil
  421.     local direction = direction or "down"
  422.     local alternate = 0
  423.     local done = false
  424.  
  425.     if direction == "up" then
  426.         downwards = false
  427.     elseif direction == "down" then
  428.         downwards = true
  429.     else
  430.         print("Direction can only be up or down!")
  431.         return
  432.     end
  433.  
  434.     craplib.changeAltitude()
  435.  
  436.     local x, y, z, xd, zd = xPos, height, zPos, xDir, zDir
  437.  
  438.     while not done do
  439.         for n = 1, size do
  440.             for m = 1, size - 1 do
  441.                 if not craplib.digForwards() then
  442.                     done = true
  443.                     break
  444.                 end
  445.             end
  446.             if done then break end
  447.             if n < size then
  448.                 if math.fmod(n + alternate, 2) == 0 then
  449.                     craplib.turnLeft()
  450.                     if not craplib.digForwards() then
  451.                         done = true
  452.                         break
  453.                     end
  454.                     craplib.turnLeft()
  455.                 else
  456.                     craplib.turnRight()
  457.                     if not craplib.digForwards() then
  458.                         done = true
  459.                         break
  460.                     end
  461.                     craplib.turnRight()
  462.                 end
  463.             end
  464.         end
  465.         if done then break end
  466.  
  467.         if size > 1 then
  468.             if math.fmod(size, 2) == 0 then
  469.                 craplib.turnRight()
  470.             else
  471.                 if alternate == 0 then
  472.                     craplib.turnLeft()
  473.                 else
  474.                     craplib.turnRight()
  475.                 end
  476.                 alternate = 1 - alternate
  477.             end
  478.         end
  479.  
  480.         craplib.changeAltitude()
  481.  
  482.         if not craplib.changeAltitude() then
  483.             done = true
  484.             break
  485.         elseif endFunction then
  486.             if endFunction() then
  487.                 done = true
  488.                 break
  489.             end
  490.         end
  491.         craplib.changeAltitude()
  492.     end
  493.  
  494.     craplib.goTo(x, y, z, xd, zd)
  495. end
  496.  
  497. if package.loaded["libs.craplib"] then
  498.     return craplib
  499. else
  500.     print("Nothing happens...")
  501. end
Add Comment
Please, Sign In to add comment