TheLoneWolfling

EnderMine v0.3

Jan 16th, 2013
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.73 KB | None | 0 0
  1. -- EnderMine v0.3, by The Lone Wolfling
  2.  
  3. -- v0.2: Added dirt detection, improved gravel detection, mining slots.
  4. -- v0.3: Doesn't detect to side until ore found. (Trades eff for speed)
  5.  
  6. -- Designed for use with chunkloading mining turtles + ender chests.
  7. -- To start, point turtle in the direction and height you wish to mine,
  8. -- place blocks in slots 13-16 as follows, then run the program.
  9.  
  10. -- Slots 1-13: Mining slots
  11. -- Slot 13: Dirt
  12. -- Slot 14: Smoothstone (Not cobble!)
  13. -- Slot 15: Ore/stone enderchest
  14. -- Slot 16: Refuel enderchest
  15.  
  16. -- Bugs/ Todos:
  17. -- * EnderMine may restart with wrong orientation if in ore-gathering mode when
  18. --       server restarts
  19. --     * Todo: add persistance file
  20. -- * Recursive ore finder is rather ineffecient
  21. --     * Todo: add cache of checked blocks
  22. -- * Recursive ore finder will always return to starting block
  23. --     * Todo: make ore finder return to first unchecked block in mining line
  24. -- * No way to deal with situation where refueling requires unload
  25. -- * No way to deal with non-fuels in fuel chest.
  26. -- * Todo: Add coroutine to display / broadcast stats/location.
  27. if not shell.getRunningProgram() == 'startup' then
  28.     if fs.exists("startup") then
  29.         if not fs.exists("startup.back") then -- Makes program restart on server restart
  30.             fs.move("startup", "startup.back")
  31.             fs.copy(shell.getRunningProgram(), "startup")
  32.         end
  33.     else
  34.         fs.copy(shell.getRunningProgram(), "startup")
  35.     end
  36. end
  37.  
  38.  
  39.  
  40. minFuel = 100 -- Refuels when current fuel level reaches this number
  41. maxFuel = 10000 -- Refuels to this target when refueling (may excede it)
  42.  
  43. lastOreSlot = 12
  44.  
  45. function enderUnload()
  46.     local function _enderUnload(drop, suck) -- No need to pollute the namespace
  47.         for i=1,lastOreSlot do
  48.             if turtle.getItemCount(i) > 0 then
  49.                 turtle.select(i)
  50.                 while not drop() do
  51.                     sleep(1)
  52.                 end
  53.             end
  54.         end
  55.     end
  56.     doWithEnderChest(_enderUnload, 15) -- 15=Ore/stone enderchest
  57. end
  58.  
  59.  
  60.  
  61. function enderRefuel()
  62.         local function _enderRefuel(drop, suck) -- No need to pollute the namespace
  63.             if turtle.getItemCount(lastOreSlot) == 0 then
  64.                 turtle.select(lastOreSlot)
  65.             else
  66.                 return -- Can't do anything about it :(
  67.             end
  68.             while turtle.getFuelLevel() < maxFuel do
  69.                 if not suck() then -- No fuel in chest?
  70.                     sleep(1)
  71.                 end
  72.                 turtle.refuel() -- Assuming that fuel is valid
  73.             end
  74.         end
  75.     doWithEnderChest(_enderRefuel, 16) -- 16=Refueling enderchest
  76. end
  77.  
  78.  
  79.  
  80. function doWithEnderChest(func, slot, doDig)
  81.     if doDig == nil then doDig = false end
  82.    
  83.     if (tryDoWithEnderChest(func, slot, doDig, turtle.digUp, turtle.placeUp, turtle.dropUp, turtle.suckUp) or
  84.             tryDoWithEnderChest(func, slot, doDig, turtle.digDown, turtle.placeDown, turtle.dropDown, turtle.suckDown)) then
  85.         return
  86.     else
  87.         for i=1,4 do
  88.             if tryDoWithEnderChest(func, slot, doDig, turtle.dig, turtle.place, turtle.drop, turtle.suck) then
  89.                 for j=i,4 do
  90.                     turtle.turnLeft()
  91.                 end
  92.                 return
  93.             end
  94.             turtle.turnLeft()
  95.         end
  96.     end
  97.     doWithEnderChest(func, slot, true) -- Try again (with digging) if all blocks are obstructed.
  98. end
  99.  
  100. function tryDoWithEnderChest(func, slot, doDig, dig, place, drop, suck)
  101.     if doDig then
  102.         dig() -- In case of gravel, etc.
  103.     end
  104.     turtle.select(slot)
  105.     if not place() then -- Can't place enderchest
  106.         return false
  107.     end
  108.     func(drop, suck)
  109.     turtle.select(slot) -- func may change slot
  110.     dig()
  111.     return true
  112. end
  113.  
  114. function moveWrap(move)
  115.     return (function ()
  116.                 if turtle.getFuelLevel() < minFuel then
  117.                     if turtle.getItemCount(lastOreSlot) > 0 then
  118.                         enderUnload()
  119.                     end
  120.                     enderRefuel()
  121.                 end
  122.                 return move()
  123.             end)
  124. end
  125.  
  126. function digWrap(dig, detect)
  127.     return (function ()
  128.                 if turtle.getItemCount(lastOreSlot) > 0 then
  129.                     enderUnload()
  130.                 end
  131.                 turtle.select(1)
  132.                 return dig()
  133.             end)
  134. end
  135.  
  136. forward = moveWrap(turtle.forward)
  137. back = moveWrap(turtle.back)
  138. up = moveWrap(turtle.up)
  139. down = moveWrap(turtle.down)
  140.  
  141. dig = digWrap(turtle.dig, turtle.detect) -- I had a nasty little bug here where
  142. digUp = digWrap(turtle.digUp, turtle.detectUp) -- it called moveWrap for dig
  143. digDown = digWrap(turtle.digDown, turtle.detectDown) -- instead.
  144.  
  145. function isStone(compare)
  146.     turtle.select(14)
  147.     return compare()
  148. end
  149.  
  150. function isDirt(compare)
  151.     turtle.select(13)
  152.     return compare()
  153. end
  154.  
  155. function isInteresting(detect, compare)
  156.     return detect() and (not (isStone(compare) or isDirt(compare)))
  157.     -- Detect for eff.
  158. end
  159.  
  160. function recurse(recurseLimit, detect, compare, dig, digOpp, forward, back)
  161.     if recurseLimit <= 0 then return false end
  162.     if isInteresting(detect, compare) and dig() then
  163.         while not forward() do dig() end -- Stupid gravel...
  164.         if not digRecursively(recurseLimit-1, true) then return false end
  165.         while not back() do -- Stupid gravel x2...
  166.             turtle.turnLeft()
  167.             turtle.turnLeft()
  168.             digOpp()
  169.             turtle.turnLeft()
  170.             turtle.turnLeft()
  171.         end  
  172.     end
  173.     return true
  174. end
  175.  
  176. function digRecursively(recurseLimit, doRotate)
  177.     if doRotate == nil then doRotate = false end
  178.     if recurseLimit == nil then recurseLimit = 200 end -- Seems to be 256 total
  179.     if recurseLimit <= 0 then return false end
  180.     if not (recurse(recurseLimit-1, turtle.detectDown, turtle.compareDown, digDown, digUp, down, up) and
  181.     recurse(recurseLimit-1, turtle.detectUp, turtle.compareUp, digUp, digDown, up, down)) then
  182.         return false
  183.     end
  184.     for i=1,4 do
  185.         if not recurse(recurseLimit-1, turtle.detect, turtle.compare, dig, dig, forward, back) then
  186.             for j=i,4 do -- Orientation strikes again
  187.                 turtle.turnRight()
  188.             end
  189.             return false
  190.         end
  191.         if not doRotate then
  192.             break
  193.         end
  194.         turtle.turnRight()
  195.     end
  196.     return true
  197. end
  198.  
  199. while true do -- I love it when everything comes together!
  200.     digRecursively()
  201.     dig()
  202.     forward()
  203. end
Advertisement
Add Comment
Please, Sign In to add comment