solidity

layminer

May 23rd, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.30 KB | None | 0 0
  1. -- efficient mining program
  2. -- slot 1: stone
  3. -- slot 2: chest
  4. -- if enderchest
  5. -- slot 3: dump chest
  6. -- slot 4: refuel chest
  7.  
  8. local sstone = 1
  9. local schest = 2
  10. local sdump = 3
  11. local sfuel = 4
  12.  
  13. local yreset = 90
  14. local controlID = 871
  15.  
  16. local args = {...}  -- Xchunk, Zchunk
  17. x,y,z,f = 0,0,0,0
  18.  
  19.  
  20. rednet.close("right")
  21. rednet.open("right")
  22.  
  23. -- set default values
  24. local yTravel = 90
  25. local ender = true
  26.  
  27. -- some functions
  28. function dump()
  29.     if ender then
  30.         cx,cy,cz = loc()
  31.         sendStatus("dumping inventory",cx,cy,cz)
  32.         print("dumping inventory")
  33.         -- put down dump chest
  34.         turtle.select(sdump)
  35.         while turtle.detectDown() do
  36.             turtle.digDown()
  37.             sleep(0.2)
  38.         end
  39.         while not turtle.placeDown() do
  40.             turtle.attackDown()
  41.             sleep(0.2)
  42.         end
  43.         -- drop stuff
  44.         for s = 16,5,-1 do
  45.             if turtle.getItemCount(s) > 0 then
  46.                 turtle.select(s)
  47.                 while not turtle.dropDown() do
  48.                     sleep(0.2)
  49.                 end
  50.             end
  51.         end
  52.         -- pick up dump chest
  53.         turtle.select(sdump)
  54.         turtle.digDown()
  55.     end
  56. end
  57.  
  58. function refuel(level)
  59.     if ender then
  60.         -- free up inventory
  61.         dump()
  62.         -- put down fuel chest
  63.         cx,cy,cz = gps.locate()
  64.         sendStatus("refueling",cx,cy,cz)
  65.         print("refueling")
  66.         turtle.select(sfuel)
  67.         while turtle.detectDown() do
  68.             turtle.digDown()
  69.             sleep(0.2)
  70.         end
  71.         while not turtle.placeDown() do
  72.             turtle.attack()
  73.             sleep(0.2)
  74.         end
  75.         -- get fuel from chest
  76.         while turtle.getFuelLevel() < level do
  77.             if turtle.getItemCount(sfuel) < 1 then
  78.                 turtle.suckDown()
  79.             end
  80.             turtle.refuel(1)
  81.             print("fueled up to "..turtle.getFuelLevel().." / "..level)
  82.         end
  83.         -- put remaining fuel back in chest and move it out of the way if the chest is full
  84.         turtle.dropDown()
  85.         if turtle.getItemCount(sfuel) > 0 then
  86.             for s = 16,5,-1 do
  87.                 if turtle.getItemCount(s) == 0 then
  88.                     turtle.transferTo(s)
  89.                     break
  90.                 end
  91.             end
  92.         end
  93.         -- pick up fuel chest
  94.         turtle.select(sfuel)
  95.         turtle.digDown()
  96.         turtle.select(schest)   -- select normal chest in inventory
  97.         sendStatus("digging / moving",cx,cy,cz)
  98.     end    
  99. end
  100.  
  101. function left(turns)
  102.     for i = 1,turns do
  103.         turtle.turnLeft()
  104.         f = f - 1
  105.         if f < 0 then
  106.             f = 3
  107.         end
  108.     end
  109.     return f
  110. end
  111.  
  112. function right(turns)
  113.     for i = 1,turns do
  114.         turtle.turnRight()
  115.         f = f + 1
  116.         if f > 3 then
  117.             f = 0
  118.         end
  119.     end
  120.     return f
  121. end
  122.  
  123. function move(dir, dist, cf)
  124.     turtle.select(schest)
  125.     for d = 1, dist do
  126.         if dir == "f" then
  127.             while not turtle.forward() do
  128.                 if turtle.getFuelLevel() == 0 then
  129.                     refuel(1000)
  130.                 elseif turtle.compare() then
  131.                     emptyChest(dir)
  132.                 else
  133.                     if not turtle.dig() then
  134.                         turtle.attack()
  135.                         sleep(0.2)
  136.                     end
  137.                 end
  138.             end
  139.             if cf == 0 then
  140.                 z = z + 1
  141.             elseif f == 1 then
  142.                 x = x - 1
  143.             elseif f == 2 then
  144.                 z = z - 1
  145.             elseif f == 3 then
  146.                 x = x + 1
  147.             end
  148.         elseif dir == "u" then
  149.             while not turtle.up() do
  150.                 if turtle.getFuelLevel() == 0 then
  151.                     refuel(1000)
  152.                 elseif turtle.compareUp() then
  153.                     emptyChest(dir)
  154.                 else
  155.                     if not turtle.digUp() then
  156.                         turtle.attack()
  157.                         sleep(0.2)
  158.                     end
  159.                 end
  160.             end
  161.             y = y + 1
  162.         elseif dir == "d" then
  163.             while not turtle.down() do
  164.                 if turtle.getFuelLevel() == 0 then
  165.                     refuel(1000)
  166.                 elseif turtle.compareDown() then
  167.                     emptyChest(dir)
  168.                 else
  169.                     if not turtle.digDown() then
  170.                         turtle.attack()
  171.                         sleep(0.2)
  172.                     end
  173.                 end
  174.             end
  175.             y = y - 1
  176.         elseif dir == "b" then
  177.             while not turtle.back() do
  178.                 if turtle.getFuelLevel() == 0 then
  179.                     refuel(1000)
  180.                 else
  181.                     left(2)
  182.                     if not turtle.dig() then
  183.                         turtle.attack()
  184.                         sleep(0.2)
  185.                     end
  186.                     right(2)
  187.                 end
  188.             end
  189.             if cf == 0 then
  190.                 z = z - 1
  191.             elseif f == 1 then
  192.                 x = x + 1
  193.             elseif f == 2 then
  194.                 z = z + 1
  195.             elseif f == 3 then
  196.                 x = x - 1
  197.             end
  198.         end
  199.     end
  200.     return x,y,z
  201. end
  202.  
  203. function findDir()
  204.     x1,_,z1 = loc()
  205.     local moved = false
  206.     local dir = -1
  207.     for d = 1,4 do
  208.         if not turtle.detect() then
  209.             while not turtle.forward() do
  210.                 if turtle.getFuelLevel() == 0 then
  211.                     refuel(2)
  212.                 else
  213.                     turtle.attack()
  214.                     sleep(0.2)
  215.                 end
  216.             end
  217.             moved = true
  218.             break
  219.         else
  220.             left(1)
  221.         end
  222.     end
  223.     if not moved then
  224.         move("f",1,dir)
  225.     end
  226.     x2,_,z2 = loc()
  227.     move("b",1)
  228.     if x2 > x1 then
  229.         dir = 3
  230.     elseif x2 < x1 then
  231.         dir = 1
  232.     elseif z2 > z1 then
  233.         dir = 0
  234.     elseif z2 < z1 then
  235.         dir = 2
  236.     end
  237.     return dir
  238. end
  239.  
  240. function turnTo(tf, cf)
  241.     if tf ~= cf then
  242.         print("turning to "..tf)
  243.         local deltaf = tf - cf
  244.         if (deltaf == 1) or (deltaf == -3) then
  245.             right(1)
  246.         elseif (deltaf == -1) or (deltaf == 3) then
  247.             left(1)
  248.         elseif (deltaf == 2) or (deltaf == -2) then
  249.             left(2)
  250.         end
  251.     end
  252.     return tf
  253. end
  254.  
  255. function goto(tx,ty,tz,cf)
  256.     local cx,cy,cz = loc()
  257.     sendStatus("going to "..tx.."/"..ty.."/"..tz,cx,cy,cz)
  258.     print("going to "..tx.."/"..ty.."/"..tz)
  259.     if (cx ~= tx) or (cz ~= tz) then
  260.         -- goto y travel
  261.         if yTravel > cy then
  262.             local deltay = yTravel - cy
  263.             cx,cy,cz = move("u",deltay,cf)
  264.         elseif y > yTravel then
  265.             local deltay = y - yTravel
  266.             cx,cy,cz = move("d",deltay,cf)
  267.         end
  268.         -- move to tx
  269.         if tx ~= cx then
  270.             local deltax = math.abs(tx - cx)
  271.             if tx > cx then
  272.                 cf = turnTo(3,cf)
  273.             elseif tx < cx then
  274.                 cf = turnTo(1,cf)
  275.             end
  276.             cx,cy,cz = move("f",deltax,cf)
  277.         end
  278.         -- move to tz
  279.         if tz ~= cz then
  280.             local deltaz = math.abs(tz - cz)
  281.             if tz > cz then
  282.                 cf = turnTo(0,cf)
  283.             elseif tz < cz then
  284.                 cf = turnTo(2,cf)
  285.             end
  286.             cx,cy,cz = move("f",deltaz,cf)
  287.         end
  288.     end
  289.     -- goto y target
  290.     if ty > cy then
  291.         local deltay = ty - cy
  292.         cx,cy,cz = move("u",deltay,cf)
  293.     elseif y > ty then
  294.         local deltay = y - ty
  295.         cx,cy,cz = move("d",deltay,cf)
  296.     end
  297.     return cx,cy,cz,cf
  298. end
  299.  
  300. function compdig()
  301.     if turtle.getItemCount(16) > 0 then
  302.         dump()
  303.         sendStatus("digging",cx,cy,cz)
  304.     end
  305.     turtle.select(schest)
  306.     if turtle.detectUp() then
  307.         if turtle.compareUp() then
  308.             emptyChest("u")
  309.             sendStatus("digging",cx,cy,cz)
  310.         end
  311.     end
  312.     if turtle.detectDown() then
  313.         if turtle.compareDown() then
  314.             emptyChest("d")
  315.             sendStatus("digging",cx,cy,cz)
  316.         end
  317.     end
  318.     turtle.select(sstone)
  319.     if turtle.getItemCount(16) > 0 then
  320.         dump()
  321.         sendStatus("digging",cx,cy,cz)
  322.     end
  323.     if not turtle.compareUp() then
  324.         turtle.digUp()
  325.     end
  326.     if turtle.getItemCount(16) > 0 then
  327.         dump()
  328.         sendStatus("digging",cx,cy,cz)
  329.     end
  330.     if not turtle.compareDown() then
  331.         turtle.digDown()
  332.     end
  333. end
  334.  
  335. function digLayer(cx,cy,cz,cf)
  336.     --refuel(256)
  337.     sendStatus("digging",cx,cy,cz)
  338.     for dx = 1,16 do
  339.         for dz = 1,16 do
  340.             compdig()
  341.             if dz < 16 then
  342.                 cx,cy,cz = move("f",1,cf)
  343.             end
  344.         end
  345.         if dx < 16 then
  346.             if dx % 2 == 1 then
  347.                 cf = left(1)
  348.                 cx,cy,cz = move("f",1,cf)
  349.                 cf = left(1)
  350.             else
  351.                 cf = right(1)
  352.                 cx,cy,cz = move("f",1,cf)
  353.                 cf = right(1)
  354.             end
  355.         end
  356.     end
  357.     return cx,cy,cz,cf
  358. end
  359.  
  360. function digLayers(cx,cy,cz,cf)
  361.     -- get into correct layer
  362.     while cy % 3 ~= 2 do
  363.         cx,cy,cz = move("d",1,cf)
  364.     end
  365.     while cy > 5 do
  366.         cx,cy,cz,cf = digLayer(cx,cy,cz,cf)
  367.         if cy > 5 then
  368.             cf = left(1)
  369.             cx,cy,cz,cf = move("d",3,cf)
  370.         end
  371.     end
  372.     return cx,cy,cz,cf
  373. end
  374.  
  375. function emptyChest(dir)
  376.     cx,cy,cz = loc()
  377.     sendStatus("found Chest",cx,cy,cz)
  378.     print("found a chest, retrieveing items")
  379.     if ender then
  380.         if dir ~= "d" then
  381.             turtle.select(sdump)
  382.             while turtle.detectDown() do
  383.                 turtle.digDown()
  384.                 sleep(0.2)
  385.             end
  386.             while not turtle.placeDown() do
  387.                 turtle.attackDown()
  388.                 sleep(0.2)
  389.             end
  390.             -- drop stuff
  391.             for s = 16,5,-1 do
  392.                 if turtle.getItemCount(s) > 0 then
  393.                     turtle.select(s)
  394.                     while not turtle.dropDown() do
  395.                         sleep(0.2)
  396.                     end
  397.                 end
  398.             end
  399.             -- transfer stuff from chest into enderchest
  400.             if dir == "u" then
  401.                 while turtle.suckUp() do
  402.                     if turtle.getItemCount(5) > 0 then
  403.                         while not turtle.dropDown() do
  404.                             sleep(1)
  405.                         end
  406.                     end
  407.                 end
  408.             elseif dir == "f" then
  409.                 while turtle.suck() do
  410.                     if turtle.getItemCount(5) > 0 then
  411.                         while not turtle.dropDown() do
  412.                             sleep(1)
  413.                         end
  414.                     end
  415.                 end
  416.             end
  417.             -- pick up dump chest
  418.             turtle.select(sdump)
  419.             turtle.digDown()
  420.         else
  421.             turtle.select(sdump)
  422.             while turtle.detect() do
  423.                 turtle.dig()
  424.                 sleep(0.2)
  425.             end
  426.             while not turtle.place() do
  427.                 turtle.attack()
  428.                 sleep(0.2)
  429.             end
  430.             -- drop stuff
  431.             for s = 16,5,-1 do
  432.                 if turtle.getItemCount(s) > 0 then
  433.                     turtle.select(s)
  434.                     while not turtle.drop() do
  435.                         sleep(0.2)
  436.                     end
  437.                 end
  438.             end
  439.             -- transfer stuff from chest into enderchest
  440.             while turtle.suckDown() do
  441.                 if turtle.getItemCount(5) > 0 then
  442.                     while not turtle.drop() do
  443.                         sleep(1)
  444.                     end
  445.                 end
  446.             end
  447.             -- pick up dump chest
  448.             turtle.select(sdump)
  449.             turtle.dig()
  450.         end
  451.     end
  452. end
  453.  
  454. function sendStatus(status,cx,cy,cz)
  455.     local chunkX = math.floor(cx/16)
  456.     local chunkZ = math.floor(cz/16)
  457.     rednet.send(controlID, chunkX.." / "..chunkZ.."   "..cy.."   "..status)
  458. end
  459.  
  460. function loc()
  461.     x = nil
  462.     while x == nil do
  463.         x,y,z = gps.locate(1)
  464.         if x == nil then
  465.             print("couldn't find position, waiting for weather to clear")
  466.             sleep(60)
  467.         end
  468.     end
  469.     return x,y,z
  470. end
  471.  
  472. -- parse commandline
  473. if #args == 1 then
  474.     local x,y,z = loc()
  475.     local f = findDir()
  476.     local chunkx = math.floor(x / 16)
  477.     local chunkz = math.floor(z / 16)
  478.     if args[1] == "getchunk" then
  479.         print("i'm in chunk "..chunkx.."/"..chunkz.." at "..x.."/"..y.."/"..z.."/"..f)
  480.     elseif args[1] == "chunkstart" then
  481.         local xstart = chunkx * 16
  482.         local zstart = chunkz * 16
  483.         goto(xstart,y,zstart)
  484.     end
  485. end
  486.  
  487. -- check if items are complete
  488. local complete = true
  489. for s = 4,1,-1 do
  490.     if turtle.getItemCount(s) < 1 then
  491.         complete = false
  492.         break
  493.     end
  494. end
  495.  
  496. if complete then
  497.     x,y,z = loc()
  498.     local chunkx = math.floor(x / 16)
  499.     local chunkz = math.floor(z / 16)
  500.     local xstart = chunkx * 16
  501.     local zstart = chunkz * 16
  502.     local f = findDir()
  503.     print("i'm in chunk "..chunkx.."/"..chunkz.." at "..x.."/"..y.."/"..z.."/"..f)
  504.     x,y,z,f = goto(xstart,y,zstart,f)
  505.     f = turnTo(0,f)
  506.     x,y,z,f = digLayers(x,y,z,f)
  507.     x,y,z,f = goto(xstart,yreset,zstart,f)
  508.     dump()
  509.     --print("i'm in chunk "..chunkx.."/"..chunkz.." at "..x.."/"..y.."/"..z.."/"..f)
  510.     sendStatus("done",x,y,z)
  511. else
  512.     x,y,z = loc()
  513.     sendStatus("incomplete",x,y,z)
  514.     print("gimme my stuff! (stone, chest, dumpchest, fuelchest)")
  515. end
Advertisement
Add Comment
Please, Sign In to add comment