SHARE
TWEET

pokey.lua

a guest Feb 29th, 2020 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ---------------------------------------------------------------
  2. -- Efficient Chunk Mining v3
  3. -- Written by LogicEngineer
  4. -- Started 02/27/20 in Minecraft version 1.12.2
  5. ---------------------------------------------------------------
  6.  
  7. local args = {...}
  8. local turnQueue
  9. local startingHeight
  10. local minHeight
  11. local pos
  12. local rPos
  13. local fuelEstimate = 0
  14. --local moveAttemptTryout = 25
  15. local GPSLoseSleepPingFrequency = 180
  16. local forward = "forward"
  17. local up = "up"
  18. local down = "down"
  19. local left = "left"
  20. local right = "right"
  21. local moved
  22. local chestSlot  = 16
  23. local cobbleSlot = 1
  24. local flatBedrock = false
  25. local regularChests = false
  26. local spinCount = 1
  27. local chunkStartLoc
  28.  
  29. function GPSLostSleepCycle()
  30.     pos = vector.new(gps.locate(5))
  31.     while not pos.x do
  32.         sleep(GPSLoseSleepPingFrequency)
  33.     end
  34. end
  35.  
  36. function GPSLocate(timeout)
  37.     pos = vector.new(gps.locate(timeout))
  38.     if not pos.x then
  39.         print("Cannot find GPS network! Stopping work unitl GPS network can be located.")
  40.         GPSLostSleepCycle()
  41.     else
  42.         rPos = RoundVectorComsToInt(pos)
  43.         return rPos
  44.     end
  45.     return false
  46. end
  47.  
  48. function GetHeight()
  49.     rPos = GPSLocate(5)
  50.     return  rPos.y
  51. end
  52.  
  53. function GetMinHeight()
  54.     if flatBedrock then
  55.         return (2)
  56.     else
  57.         return (5)
  58.     end
  59. end
  60.  
  61. function AmIHigh()
  62.     if GetHeight() >= startingHeight-((startingHeight-minHeight)/2) then
  63.         return true
  64.     else
  65.         return false
  66.     end
  67. end
  68.  
  69. function SpinAround()
  70.     turtle.turnLeft()
  71.     turtle.turnLeft()
  72.     return
  73. end
  74.  
  75. function TrackSpin()
  76.     spinCount = spinCount + 1
  77.     if spinCount == 0 then
  78.         spinCount = 4
  79.     end
  80.     if spinCount == 5 then
  81.         spinCount = 1
  82.     end
  83. end
  84.  
  85. function CorrectDirection(current)
  86.     print(current)
  87.     if current == 4 then
  88.         turtle.turnLeft()
  89.     else
  90.         if current == 3 then
  91.             SpinAround()
  92.         else
  93.             if current == 2 then
  94.                 turtle.turnRight()
  95.             else
  96.                 return
  97.             end
  98.         end
  99.     end
  100.     return
  101. end
  102.  
  103. function ForceMove(distance, direction)
  104.     for i=1, distance do
  105.         while not moved do
  106.             if direction == down then
  107.                 turtle.digDown()
  108.                 if turtle.down() then
  109.                     moved = true
  110.                 end
  111.             else
  112.                 if direction == up then
  113.                     turtle.digUp()
  114.                     if turtle.up() then
  115.                         moved = true
  116.                     end
  117.                 else
  118.                     if direction == forward then
  119.                         turtle.dig()
  120.                         if turtle.forward() then
  121.                             moved = true
  122.                         end
  123.                     else
  124.                         return false
  125.                     end
  126.                 end
  127.             end
  128.         end
  129.         moved = false
  130.     end
  131.     return
  132. end
  133.  
  134. function RoundVectorComsToInt(vec)
  135.     return vector.new(math.floor(vec.x+0.5),math.floor(vec.y+0.5),math.floor(vec.z+0.5))
  136. end
  137.  
  138. function IsInt(num)
  139.     return num==math.floor(num)
  140. end
  141.  
  142. function Turn21() -- Time to party?
  143.     if turnQueue == right then
  144.         turtle.turnRight()
  145.         ForceMove(1,forward)
  146.         turtle.turnRight()
  147.         turnQueue = left
  148.     else
  149.         turtle.turnLeft()
  150.         ForceMove(1,forward)
  151.         turtle.turnLeft()
  152.         turnQueue = right
  153.     end
  154. end
  155.  
  156. function FuelCheck()
  157.     fuelEstimate = 16*16+52*startingHeight
  158.     print("Current Fuel:", turtle.getFuelLevel())
  159.         print("Estimated Fuel:", fuelEstimate)
  160.     if (fuelEstimate) >= turtle.getFuelLevel() then
  161.         print("Need more fuel for requested chunkmine.")
  162.         return false
  163.     end
  164.     return true
  165. end
  166.  
  167. function DropSide(slotNum, side)
  168.     turtle.select(slotNum)
  169.     side = string.lower(side)
  170.     local success = false
  171.     if side == "front" then
  172.       success = turtle.drop()
  173.     elseif side == "top" then
  174.       success = turtle.dropUp()
  175.     elseif side == "bottom" then
  176.       success = turtle.dropDown()
  177.     else
  178.       success = turtle.drop()
  179.     end
  180.     return success
  181.   end
  182.  
  183. function EmptyInv(slotsToEmpty, side)
  184.     local count
  185.     local oldSlot = turtle.getSelectedSlot()
  186.     if not side then
  187.       side = "back"
  188.     end
  189.     if not slotsToEmpty then
  190.       count = 16
  191.     else
  192.       count = table.getn(slotsToEmpty)
  193.     end
  194.     for i = 1, count do
  195.       DropSide(slotsToEmpty[i], side)
  196.     end
  197.     turtle.select(oldSlot)
  198.   end
  199.  
  200. function DoChest()
  201.     turtle.select(chestSlot)
  202.     while not turtle.placeUp() do
  203.         turtle.digUp()
  204.         sleep(.5)
  205.     end
  206.     local slotsToEmpty = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
  207.     EmptyInv(slotsToEmpty, "top")
  208.     turtle.select(chestSlot)
  209.     if not regularChests then
  210.         turtle.digUp()
  211.         turtle.select(cobbleSlot)
  212.         turtle.placeUp()
  213.     end
  214. end
  215.  
  216. function CheckOre()
  217.     local success, block = turtle.inspect()
  218.     if not success then return false end
  219.     local name = string.lower(block.name)
  220.     return (string.match(name, "ore"))
  221. end
  222.  
  223. function Scan()
  224.     for i=1, 4 do
  225.         if CheckOre() then
  226.             turtle.dig()
  227.         end
  228.     end
  229. end
  230.  
  231. function PokeHole(updown)
  232.     spinCount = 1
  233.     if updown == down then
  234.         turtle.select(cobbleSlot)
  235.         turtle.placeUp()
  236.     end
  237.     for i=1, (startingHeight-minHeight) do
  238.         SpinScan()
  239.         if updown == up then
  240.             ForceMove(1,up)
  241.         else
  242.             ForceMove(1,down)
  243.         end
  244.     end
  245.     SpinScan()
  246.     print("Correcting facing direction.")
  247.     CorrectDirection(spinCount)
  248.     if updown == up then
  249.         DoChest()
  250.     end
  251. end
  252.  
  253. function SpinScan()
  254.     for j=1, 3 do
  255.         Scan()
  256.         turtle.turnRight()
  257.     end
  258.     Scan()
  259.     TrackSpin()
  260. end
  261.  
  262. function DigIfShould()
  263.     if ShouldIDigHere() then
  264.         if AmIHigh() then
  265.             PokeHole(down)
  266.         else
  267.             PokeHole(up)
  268.         end
  269.     end
  270.     return
  271. end
  272.  
  273. function ShouldIDigHere()
  274.     GPSLocate()
  275.     if IsInt(rPos.x/5) and IsInt(rPos.z/5) then
  276.         return true
  277.     else
  278.         if IsInt((rPos.x-2)/5) and IsInt((rPos.z-1)/5) then
  279.             return true
  280.         else
  281.             if IsInt((rPos.x+1)/5) and IsInt((rPos.z-2)/5) then
  282.                 return true
  283.             else
  284.                 if IsInt((rPos.x-1)/5) and IsInt((rPos.z+2)/5) then
  285.                     return true
  286.                 else
  287.                     if IsInt((rPos.x+2)/5) and IsInt((rPos.z+1)/5) then
  288.                         return true
  289.                     else
  290.                       return false
  291.                     end
  292.                 end
  293.             end
  294.         end
  295.     end
  296. end
  297.  
  298. function ProcessChunk()
  299.     for i=1, 15 do
  300.         for j=1, 15 do
  301.             DigIfShould()
  302.             ForceMove(1,forward)
  303.         end
  304.         DigIfShould()
  305.         Turn21()
  306.     end
  307.     for i=1, 15 do
  308.         DigIfShould()
  309.         turtle.forward()
  310.     end
  311.     DigIfShould()
  312.     SpinAround()
  313.     turtle.turnLeft()
  314.     turtle.turnLeft()
  315.     return
  316. end
  317.  
  318. function CheckArgs()
  319.     if not args[1] then
  320.         print("ERROR: Need turtle start location!")
  321.         return false
  322.     else
  323.         chunkStartLoc = args[1]
  324.     end
  325.  
  326.     if args[2] == "flat" then
  327.         flatBedrock = true
  328.         print("Flat bedrock is enabled.")
  329.     else
  330.         print("Flat bedrock is not enabled.")
  331.     end
  332.  
  333.     if args[3] then
  334.         regularChests = true
  335.         print("Turtle is using regular chests.")
  336.     end
  337.     return true
  338. end
  339.  
  340. function HeightInit()
  341.     startingHeight = GetHeight()
  342.     print("Starting Height:", startingHeight)
  343.     minHeight = GetMinHeight()
  344.     print("Minimum Height:", minHeight)
  345. end
  346.  
  347. function Main()
  348.     if not CheckArgs() then
  349.         return
  350.     end
  351.  
  352.     HeightInit()
  353.  
  354.     if not FuelCheck() then
  355.         return
  356.     end
  357.  
  358.     if chunkStartLoc == left then
  359.         turnQueue = right
  360.     else
  361.         turnQueue = left
  362.     end
  363.  
  364.     ProcessChunk()
  365.  
  366. end
  367.  
  368. Main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top