IMarvinTPA

Light Placer

Jan 10th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.34 KB | None | 0 0
  1. --pastebin get mFD6ygbm Lighter
  2.  
  3. local tArgs = { ... }
  4. local filename = shell.getRunningProgram()
  5.  
  6. if #tArgs < 3 then
  7.     print( "Usage: " .. filename .. " <spacing> <length> <width>" )
  8.     print( "Slot 1 should be a stack of fuel." )
  9.     print( "All other slots are light blocks to place below at interval." )
  10.     return
  11. end
  12.  
  13. -- Mine in a quarry pattern until we hit something we can't dig
  14. local digType = "Count"
  15. local nDelay = tonumber( tArgs[1] )
  16. local inLength = tonumber( tArgs[2] )
  17. local inWidth = tonumber( tArgs[3] )
  18. local relCurFacing = 0
  19. local movedCnt = 0
  20.  
  21. if tall == nil then
  22.     tall = 3
  23. end
  24.  
  25. if inLength < 1 then
  26.     inLength = 1
  27. end
  28.  
  29. local length = inLength
  30.  
  31. if inWidth < 1 then
  32.     inWidth = 1
  33. end
  34.  
  35. local width = inWidth
  36.  
  37.  
  38. if tall < 1 then
  39.     tall = 1
  40. end
  41.        
  42. local depth = 0
  43. local collected = 0
  44. local unloaded = 0
  45.  
  46. local faceForward = 0
  47. local faceBack = -2
  48. local faceLeft = -1
  49. local faceRight = 1
  50.  
  51. local relX, relY, relZ = 0, 0, 0
  52. local absX, absY, absZ = 0, 0, 0
  53.  
  54. local North = 2
  55. local South = 0
  56. local East = 3
  57. local West = 1
  58. local realFace = South
  59.  
  60. local activity = ""
  61.  
  62. local planX, planY, planZ = 0, 0, 0
  63.  
  64. local planFace = South
  65.  
  66. local function saveState()
  67.     return true
  68. end
  69.  
  70. local function isFull()
  71.         local bFull = true
  72.         local nTotalItems = 0
  73.         for n=1,15 do
  74.             local nCount = turtle.getItemCount(n)
  75.             if nCount == 0 then
  76.                 bFull = false
  77.             end
  78.             nTotalItems = nTotalItems + nCount
  79.         end
  80.              
  81.         if bFull then
  82.             print( "No empty slots left." )
  83.             return true
  84.         end
  85.         return false
  86. end
  87.  
  88. local function relTurnLeft()
  89.     turtle.turnLeft()
  90.     relCurFacing = relCurFacing - 1
  91.     while relCurFacing <= -3 do
  92.         relCurFacing = relCurFacing + 4
  93.     end
  94.    
  95.     realFace = realFace - 1
  96.    
  97.     while realFace < South do
  98.         realFace = realFace + 4
  99.     end
  100.    
  101.     saveState()
  102.    
  103. end
  104.  
  105. local function relTurnRight()
  106.     turtle.turnRight()
  107.     relCurFacing = relCurFacing + 1
  108.     while relCurFacing >= 2 do
  109.         relCurFacing = relCurFacing - 4
  110.     end
  111.    
  112.     realFace = realFace + 1
  113.     while realFace > East do
  114.         realFace = realFace - 4
  115.     end
  116.    
  117.     saveState()
  118.    
  119. end
  120.  
  121. local function relTurnFacing(wantDir)
  122.     while wantDir <= -3 do
  123.         wantDir = wantDir + 4
  124.     end
  125.     while wantDir >= 2 do
  126.         wantDir = wantDir - 4
  127.     end
  128.    
  129.     --Turn right once more to face behind the general dirction of travel.
  130.     if relCurFacing == faceRight and wantDir == faceBack then
  131.         relTurnRight()
  132.     end
  133.  
  134.     if relCurFacing == faceBack and wantDir == faceRight then
  135.         relTurnLeft()
  136.     end
  137.    
  138.     --Turn left until we are facing our desired direction
  139.     while relCurFacing > wantDir do
  140.         --print("Left: " .. relCurFacing .. " Want: " .. wantDir)
  141.         relTurnLeft()      
  142.     end
  143.  
  144.     --Turn right until we are facing our desired direction
  145.     while relCurFacing < wantDir do
  146.         --print("Right: " .. relCurFacing .. " Want: " .. wantDir)
  147.         relTurnRight()
  148.     end
  149.    
  150. end
  151.  
  152. local function absTurnFacing(wantDir)
  153.     --print("Absolute Turning: Want: ", wantDir, " At: ", realFace )
  154.     while wantDir < South do
  155.         wantDir = wantDir + 4
  156.     end
  157.     while wantDir > East do
  158.         wantDir = wantDir - 4
  159.     end
  160.    
  161.     --Turn right once more to face behind the general dirction of travel.
  162.     if realFace == East and wantDir == South then
  163.         relTurnRight()
  164.     end
  165.  
  166.     if realFace == South and wantDir == East then
  167.         relTurnLeft()
  168.     end
  169.    
  170.     --Turn left until we are facing our desired direction
  171.     while realFace > wantDir do
  172.         --print("Left: " .. realFace .. " Want: " .. wantDir)
  173.         relTurnLeft()      
  174.     end
  175.  
  176.     --Turn right until we are facing our desired direction
  177.     while realFace < wantDir do
  178.         --print("Right: " .. realFace .. " Want: " .. wantDir)
  179.         relTurnRight()
  180.     end
  181.    
  182. end
  183.  
  184.  
  185. local function simpleDig()
  186.     while turtle.detect() do
  187.         if turtle.dig() then
  188.             sleep(1)
  189.         else
  190.             return false
  191.         end
  192.     end
  193.     return true
  194. end
  195.  
  196. local function unload()
  197.     if alreadyUnloading then
  198.         return false
  199.     end
  200.     print( "Unloading items..." )
  201.     --1: Left
  202.     --2: Right
  203.     --3: Chests
  204.     --4: Fuel
  205.     for n=5,16 do
  206.         unloaded = unloaded + turtle.getItemCount(n)
  207.         turtle.select(n)
  208.         if turtle.getItemCount(n) > 0 then
  209.             while not turtle.drop() do
  210.                 print("The target chest is full")
  211.                 sleep(30)
  212.             end            
  213.         end
  214.     end
  215.     turtle.select(1)
  216. end
  217.  
  218. local function collect()
  219.     collected = collected + 1
  220.     if math.fmod(collected, 25) == 0 then
  221.         print( "Mined "..collected.." items." )
  222.     end
  223.    
  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=1,1 do
  285.             if n ~= 17 and n ~= 27 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. local function placeAny()
  541.     for n=2,16 do
  542.         if turtle.getItemCount(n) > 1 then
  543.             turtle.select(n)
  544.             tryDigDown()
  545.             if turtle.placeDown() then
  546.                     turtle.select(1)
  547.                     return true
  548.             end
  549.         end
  550.     end
  551.     turtle.select(1)
  552.     return false
  553. end
  554.  
  555.  
  556. function main()
  557.     digType = string.lower(digType)
  558.    
  559.     haveRealGPS = false
  560.    
  561.     planFace = realFace
  562.    
  563.     originalFace = realFace
  564.  
  565.     while width >= 0 do
  566.         local dropCnt = 0
  567.         for lPos = 1,length do         
  568.             if dropCnt == 0 then
  569.                 placeAny()
  570.             end
  571.            
  572.             dropCnt = dropCnt + 1
  573.            
  574.             if dropCnt == nDelay then
  575.                 dropCnt = 0
  576.             end
  577.            
  578.             tryForward()
  579.            
  580.         end
  581.  
  582.         if dropCnt == 0 then
  583.             placeAny()
  584.         end
  585.    
  586.         if width < nDelay then
  587.             width = width - nDelay
  588.             relTurnLeft()
  589.             relTurnLeft()
  590.         elseif realFace == originalFace then
  591.             relTurnLeft()
  592.             for lPos = 1,nDelay do
  593.                 tryForward()
  594.                 width = width - 1
  595.             end
  596.             relTurnLeft()
  597.         else
  598.             relTurnRight()
  599.             for lPos = 1,nDelay do
  600.                 tryForward()
  601.                 width = width - 1
  602.             end
  603.             relTurnRight()
  604.         end
  605.        
  606.     end
  607.    
  608.      
  609.     print( "Lighting complete." )
  610.     print( "Blocking "..collected.." items total." )
  611.  
  612.    
  613. end
  614.  
  615. main()
Advertisement
Add Comment
Please, Sign In to add comment