IMarvinTPA

Farm By Marker

Mar 26th, 2013
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.84 KB | None | 0 0
  1. --pastebin get PhHYgTaE Farm
  2.  
  3. local tArgs = { ... }
  4. local filename = shell.getRunningProgram()
  5.  
  6. if #tArgs < 1 then
  7.     print( "Usage: " .. filename .. " <Grow Delay>" )
  8.     print( "Slot 1 should be a sample block to turn Left when blocking." )
  9.     print( "Slot 2 should be a sample block to turn right." )
  10.     print( "Slot 3 should be a sample drop-off chest." )
  11.     print( "Slot 4 should be a stack of fuel." )
  12.     return
  13. end
  14.  
  15. -- Mine in a quarry pattern until we hit something we can't dig
  16. local digType = "Bounce"
  17. local nDelay = tonumber( tArgs[1] )
  18. local inLength = tonumber( tArgs[2] )
  19. local inWidth = tonumber( tArgs[3] )
  20. local tall = tonumber( tArgs[4] )
  21. local useEnderChest = tonumber( tArgs[5] )
  22. local relCurFacing = 0
  23. local movedCnt = 0
  24.  
  25. if tall == nil then
  26.     tall = 3
  27. end
  28.  
  29. if inLength < 1 then
  30.     inLength = 1
  31. end
  32.  
  33. local length = inLength
  34.  
  35. if tall < 1 then
  36.     tall = 1
  37. end
  38.        
  39. local depth = 0
  40. local collected = 0
  41. local unloaded = 0
  42.  
  43. local faceForward = 0
  44. local faceBack = -2
  45. local faceLeft = -1
  46. local faceRight = 1
  47.  
  48. local relX, relY, relZ = 0, 0, 0
  49. local absX, absY, absZ = 0, 0, 0
  50.  
  51. local North = 2
  52. local South = 0
  53. local East = 3
  54. local West = 1
  55. local realFace = South
  56.  
  57. local activity = ""
  58.  
  59. local planX, planY, planZ = 0, 0, 0
  60.  
  61. local planFace = South
  62.  
  63. local function saveState()
  64.     return true
  65. end
  66.  
  67. local function isFull()
  68.         local bFull = true
  69.         local nTotalItems = 0
  70.         for n=1,15 do
  71.             local nCount = turtle.getItemCount(n)
  72.             if nCount == 0 then
  73.                 bFull = false
  74.             end
  75.             nTotalItems = nTotalItems + nCount
  76.         end
  77.              
  78.         if bFull then
  79.             print( "No empty slots left." )
  80.             return true
  81.         end
  82.         return false
  83. end
  84.  
  85. local function relTurnLeft()
  86.     turtle.turnLeft()
  87.     relCurFacing = relCurFacing - 1
  88.     while relCurFacing <= -3 do
  89.         relCurFacing = relCurFacing + 4
  90.     end
  91.    
  92.     realFace = realFace - 1
  93.    
  94.     while realFace < South do
  95.         realFace = realFace + 4
  96.     end
  97.    
  98.     saveState()
  99.    
  100. end
  101.  
  102. local function relTurnRight()
  103.     turtle.turnRight()
  104.     relCurFacing = relCurFacing + 1
  105.     while relCurFacing >= 2 do
  106.         relCurFacing = relCurFacing - 4
  107.     end
  108.    
  109.     realFace = realFace + 1
  110.     while realFace > East do
  111.         realFace = realFace - 4
  112.     end
  113.    
  114.     saveState()
  115.    
  116. end
  117.  
  118. local function relTurnFacing(wantDir)
  119.     while wantDir <= -3 do
  120.         wantDir = wantDir + 4
  121.     end
  122.     while wantDir >= 2 do
  123.         wantDir = wantDir - 4
  124.     end
  125.    
  126.     --Turn right once more to face behind the general dirction of travel.
  127.     if relCurFacing == faceRight and wantDir == faceBack then
  128.         relTurnRight()
  129.     end
  130.  
  131.     if relCurFacing == faceBack and wantDir == faceRight then
  132.         relTurnLeft()
  133.     end
  134.    
  135.     --Turn left until we are facing our desired direction
  136.     while relCurFacing > wantDir do
  137.         --print("Left: " .. relCurFacing .. " Want: " .. wantDir)
  138.         relTurnLeft()      
  139.     end
  140.  
  141.     --Turn right until we are facing our desired direction
  142.     while relCurFacing < wantDir do
  143.         --print("Right: " .. relCurFacing .. " Want: " .. wantDir)
  144.         relTurnRight()
  145.     end
  146.    
  147. end
  148.  
  149. local function absTurnFacing(wantDir)
  150.     --print("Absolute Turning: Want: ", wantDir, " At: ", realFace )
  151.     while wantDir < South do
  152.         wantDir = wantDir + 4
  153.     end
  154.     while wantDir > East do
  155.         wantDir = wantDir - 4
  156.     end
  157.    
  158.     --Turn right once more to face behind the general dirction of travel.
  159.     if realFace == East and wantDir == South then
  160.         relTurnRight()
  161.     end
  162.  
  163.     if realFace == South and wantDir == East then
  164.         relTurnLeft()
  165.     end
  166.    
  167.     --Turn left until we are facing our desired direction
  168.     while realFace > wantDir do
  169.         --print("Left: " .. realFace .. " Want: " .. wantDir)
  170.         relTurnLeft()      
  171.     end
  172.  
  173.     --Turn right until we are facing our desired direction
  174.     while realFace < wantDir do
  175.         --print("Right: " .. realFace .. " Want: " .. wantDir)
  176.         relTurnRight()
  177.     end
  178.    
  179. end
  180.  
  181.  
  182. local function simpleDig()
  183.     while turtle.detect() do
  184.         if turtle.dig() then
  185.             sleep(1)
  186.         else
  187.             return false
  188.         end
  189.     end
  190.     return true
  191. end
  192.  
  193. local function unload()
  194.     if alreadyUnloading then
  195.         return false
  196.     end
  197.     print( "Unloading items..." )
  198.     --1: Left
  199.     --2: Right
  200.     --3: Chests
  201.     --4: Fuel
  202.     for n=5,16 do
  203.         unloaded = unloaded + turtle.getItemCount(n)
  204.         turtle.select(n)
  205.         if turtle.getItemCount(n) > 0 then
  206.             while not turtle.drop() do
  207.                 print("The target chest is full")
  208.                 sleep(30)
  209.             end            
  210.         end
  211.     end
  212.     turtle.select(1)
  213. end
  214.  
  215. local function collect()
  216.     collected = collected + 1
  217.     if math.fmod(collected, 25) == 0 then
  218.         print( "Mined "..collected.." items." )
  219.     end
  220.    
  221.     if isFull() then
  222.         unload()
  223.     end
  224. end
  225.  
  226. local function tryDig()
  227.     --Collect stuff from chests.
  228.     local cnt = false
  229.     while turtle.suck() do
  230.         collect()
  231.     end
  232.    
  233.     while turtle.detect() do
  234.         if turtle.dig() then
  235.             collect()
  236.             cnt = true
  237.             --sleep(0.5)
  238.         else
  239.             return false, cnt
  240.         end
  241.     end
  242.  
  243.     return true, cnt
  244. end
  245.  
  246. local function tryDigUp()
  247.     while turtle.suckUp() do
  248.         collect()
  249.     end
  250.     while turtle.detectUp() do
  251.         if turtle.digUp() then
  252.             collect()
  253.             --sleep(0.5)
  254.         else
  255.                 return false
  256.         end
  257.     end
  258.     return true
  259. end
  260.  
  261. local function tryDigDown()
  262.     while turtle.suckDown() do
  263.         collect()
  264.     end
  265.     while turtle.detectDown() do
  266.         if turtle.digDown() then
  267.             collect()
  268.             --sleep(0.5)
  269.         else
  270.             return false
  271.         end
  272.     end
  273.     return true
  274. end
  275.  
  276.  
  277. local function refuel()
  278.     local fuelLevel = turtle.getFuelLevel()
  279.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  280.         return
  281.     end
  282.    
  283.     local function tryRefuel()
  284.         for n=4,16 do
  285.             if n ~= 1 and n ~= 2 then
  286.                 if turtle.getItemCount(n) > 0 then
  287.                     turtle.select(n)
  288.                     if turtle.refuel(1) then
  289.                         turtle.select(1)
  290.                         return true
  291.                     end
  292.                 end
  293.             end
  294.         end
  295.         turtle.select(1)
  296.         return false
  297.     end
  298.    
  299.     if not tryRefuel() then
  300.         print( "Add more fuel to continue." )
  301.         while not tryRefuel() do
  302.             sleep(1)
  303.         end
  304.         print( "Resuming Tunnel." )
  305.     end
  306. end
  307.  
  308. local function tryUp()
  309.     refuel()
  310.     while not turtle.up() do
  311.         if turtle.detectUp() then
  312.             if not tryDigUp() then
  313.                 return false
  314.             end
  315.         elseif turtle.attackUp() then
  316.             collect()
  317.         else
  318.             sleep( 0.5 )
  319.         end
  320.     end
  321.     relY = relY + 1
  322.     absY = absY + 1
  323.     saveState()
  324.     --print("rel: ", relX, ", ", relY, ", ", relZ)
  325.     --print("abs: ", absX, ", ", absY, ", ", absZ)
  326.     --print("route pos: ", routePos)
  327.     return true
  328. end
  329.  
  330. local function tryDown()
  331.     refuel()
  332.     while not turtle.down() do
  333.         if turtle.detectDown() then
  334.             if not tryDigDown() then
  335.                 return false
  336.             end
  337.         elseif turtle.attackDown() then
  338.             collect()
  339.         else
  340.             sleep( 0.5 )
  341.         end
  342.     end
  343.     relY = relY - 1
  344.     absY = absY - 1
  345.     saveState()
  346.     return true
  347. end
  348.  
  349. local function absVector(inDir)
  350.  
  351.     if inDir == North then
  352.         return 0, -1
  353.     elseif inDir == South then
  354.         return 0, 1
  355.     elseif inDir == East then
  356.         return 1, 0
  357.     elseif inDir == West then
  358.         return -1, 0
  359.     end
  360.    
  361.     return nil, nil
  362.  
  363. end
  364.  
  365.  
  366. local function tryForward()
  367.     refuel()
  368.     while not turtle.forward() do
  369.         if turtle.detect() then
  370.             if not tryDig() then
  371.                 return false
  372.             end
  373.         elseif turtle.attack() then
  374.             collect()
  375.         else
  376.             sleep( 0.5 )
  377.         end
  378.     end
  379.    
  380.     movedCnt = movedCnt + 1
  381.     local dx, dz = absVector(realFace)
  382.     relX = relX + dx
  383.     absX = absX + dx
  384.     relZ = relZ + dz
  385.     absZ = absZ + dz
  386.     saveState()
  387.  
  388.     return true
  389. end
  390.  
  391. local function tryBack()
  392.     refuel()
  393.     while not turtle.back() do
  394.         relTurnRight()
  395.         relTurnRight()
  396.         if turtle.detect() then
  397.             if not tryDig() then
  398.                 return false
  399.             end
  400.         elseif turtle.attack() then
  401.             collect()
  402.         else
  403.             sleep( 0.5 )
  404.         end
  405.         relTurnRight()
  406.         relTurnRight()
  407.     end
  408.    
  409.     movedCnt = movedCnt - 1
  410.     local dx, dz = absVector(realFace)
  411.     relX = relX - dx
  412.     absX = absX - dx
  413.     relZ = relZ - dz
  414.     absZ = absZ - dz
  415.     saveState()
  416.  
  417.     return true
  418. end
  419.  
  420. local function readNumber(sf)
  421.     print("Read Number")
  422.     local x = tonumber(sf.readLine()) + 0
  423.     print("Read ", x)
  424.     return x
  425. end
  426.  
  427. local function tryFace(targX, targZ)
  428.     print("Try Face")
  429.     local dx, dz = targX - relX, targZ - relZ
  430.     print("X: ", relX, " to ", targX, "  Z: ", relZ, " to ", targZ)
  431.  
  432.     if math.abs(dx) > math.abs(dz) then
  433.         if dx > 0 then
  434.             absTurnFacing(East)
  435.             print("Turnig to the East")
  436.         elseif dx < 0 then
  437.             absTurnFacing(West)
  438.             print("Turnig to the West")
  439.         end
  440.     else
  441.         if dz > 0 then
  442.             absTurnFacing(South)
  443.             print("Turnig to the South")
  444.         elseif dz < 0 then
  445.             absTurnFacing(North)
  446.             print("Turnig to the North")
  447.         end
  448.     end
  449.     --sleep(5)
  450.     return true
  451. end
  452.  
  453. local function DoGo(x, y, z)
  454.     print("Do Go")
  455.     local dx, dy, dz = x - relX, y - relY, z - relZ
  456.     local adx, ady, adz, tryRes
  457.     while dx ~= 0 or dy ~= 0 or dz ~= 0 do
  458.         print(dx, dy, dz)
  459.         adx = math.abs(dx)
  460.         ady = math.abs(dy)
  461.         adz = math.abs(dz)
  462.         tryRes = true
  463.         if dy ~= 0 and (ady <= adx or dx == 0) and (ady <= adz or dz ==0) then
  464.             if dy > 0 then
  465.                 tryRes = tryUp()
  466.             else
  467.                 tryRes = tryDown()
  468.             end
  469.         elseif dx ~= 0 and (adx <= ady or dy == 0) and (adx <= adz or dz ==0) then
  470.             if dx > 0 then
  471.                 absTurnFacing(East)
  472.             else
  473.                 absTurnFacing(West)
  474.             end
  475.             relCurFacing = faceForward
  476.             tryRes = tryForward()
  477.         elseif dz ~= 0 and (adz <= ady or dy == 0) and (adz <= adx or dx ==0) then
  478.             if dz > 0 then
  479.                 absTurnFacing(South)
  480.             else
  481.                 absTurnFacing(North)
  482.             end
  483.             relCurFacing = faceForward
  484.             tryRes = tryForward()
  485.         end
  486.         if tryRes == false then
  487.             print("Unable to go to desired location!")
  488.             return false
  489.         end
  490.         dx, dy, dz = x - relX, y - relY, z - relZ
  491.     end
  492.     return true
  493. end
  494.  
  495. local function DoGoAbs(x, y, z)
  496.     print("Do Go")
  497.     local dx, dy, dz = x - absX, y - absY, z - absZ
  498.     local adx, ady, adz, tryRes
  499.     while dx ~= 0 or dy ~= 0 or dz ~= 0 do
  500.         print(dx, dy, dz)
  501.         adx = math.abs(dx)
  502.         ady = math.abs(dy)
  503.         adz = math.abs(dz)
  504.         tryRes = true
  505.         if dy ~= 0 and ady < adx and ady < adz then
  506.             if dy > 0 then
  507.                 tryRes = tryUp()
  508.             else
  509.                 tryRes = tryDown()
  510.             end
  511.         elseif dx ~= 0 and adx < ady and adx < adz then
  512.             if dx > 0 then
  513.                 absTurnFacing(East)
  514.             else
  515.                 absTurnFacing(West)
  516.             end
  517.             tryRes = tryForward()
  518.         elseif dz ~= 0 and adz < ady and adz < adx then
  519.             if dz > 0 then
  520.                 absTurnFacing(South)
  521.             else
  522.                 absTurnFacing(North)
  523.             end
  524.             tryRes = tryForward()
  525.         end
  526.         if tryRes == false then
  527.             print("Unable to go to desired location!")
  528.             return false
  529.         end
  530.         dx, dy, dz = x - absX, y - absY, z - absZ
  531.     end
  532.     return true
  533. end
  534.  
  535. function compareFrontToSlot(inSlot)
  536.     turtle.select(inSlot)
  537.     return turtle.compare()
  538. end
  539.  
  540. function main()
  541.     digType = string.lower(digType)
  542.    
  543.     haveRealGPS = false
  544.    
  545.     planFace = realFace
  546.  
  547.     while true do
  548.         if compareFrontToSlot(1) then
  549.             relTurnLeft()
  550.         elseif compareFrontToSlot(2) then
  551.             relTurnRight()
  552.         elseif compareFrontToSlot(3) then
  553.             --End of the line, unload.
  554.             unload()
  555.             relTurnRight()
  556.             relTurnRight()
  557.             sleep(nDelay)
  558.         else
  559.             --Dig and go foward.
  560.             tryForward()
  561.         end
  562.     end
  563.    
  564.      
  565.     print( "Farming complete." )
  566.     print( "Farmed "..collected.." items total." )
  567.  
  568.    
  569. end
  570.  
  571. main()
Advertisement
Add Comment
Please, Sign In to add comment