Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- EnderMine v0.3, by The Lone Wolfling
- -- v0.2: Added dirt detection, improved gravel detection, mining slots.
- -- v0.3: Doesn't detect to side until ore found. (Trades eff for speed)
- -- Designed for use with chunkloading mining turtles + ender chests.
- -- To start, point turtle in the direction and height you wish to mine,
- -- place blocks in slots 13-16 as follows, then run the program.
- -- Slots 1-13: Mining slots
- -- Slot 13: Dirt
- -- Slot 14: Smoothstone (Not cobble!)
- -- Slot 15: Ore/stone enderchest
- -- Slot 16: Refuel enderchest
- -- Bugs/ Todos:
- -- * EnderMine may restart with wrong orientation if in ore-gathering mode when
- -- server restarts
- -- * Todo: add persistance file
- -- * Recursive ore finder is rather ineffecient
- -- * Todo: add cache of checked blocks
- -- * Recursive ore finder will always return to starting block
- -- * Todo: make ore finder return to first unchecked block in mining line
- -- * No way to deal with situation where refueling requires unload
- -- * No way to deal with non-fuels in fuel chest.
- -- * Todo: Add coroutine to display / broadcast stats/location.
- if not shell.getRunningProgram() == 'startup' then
- if fs.exists("startup") then
- if not fs.exists("startup.back") then -- Makes program restart on server restart
- fs.move("startup", "startup.back")
- fs.copy(shell.getRunningProgram(), "startup")
- end
- else
- fs.copy(shell.getRunningProgram(), "startup")
- end
- end
- minFuel = 100 -- Refuels when current fuel level reaches this number
- maxFuel = 10000 -- Refuels to this target when refueling (may excede it)
- lastOreSlot = 12
- function enderUnload()
- local function _enderUnload(drop, suck) -- No need to pollute the namespace
- for i=1,lastOreSlot do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- while not drop() do
- sleep(1)
- end
- end
- end
- end
- doWithEnderChest(_enderUnload, 15) -- 15=Ore/stone enderchest
- end
- function enderRefuel()
- local function _enderRefuel(drop, suck) -- No need to pollute the namespace
- if turtle.getItemCount(lastOreSlot) == 0 then
- turtle.select(lastOreSlot)
- else
- return -- Can't do anything about it :(
- end
- while turtle.getFuelLevel() < maxFuel do
- if not suck() then -- No fuel in chest?
- sleep(1)
- end
- turtle.refuel() -- Assuming that fuel is valid
- end
- end
- doWithEnderChest(_enderRefuel, 16) -- 16=Refueling enderchest
- end
- function doWithEnderChest(func, slot, doDig)
- if doDig == nil then doDig = false end
- if (tryDoWithEnderChest(func, slot, doDig, turtle.digUp, turtle.placeUp, turtle.dropUp, turtle.suckUp) or
- tryDoWithEnderChest(func, slot, doDig, turtle.digDown, turtle.placeDown, turtle.dropDown, turtle.suckDown)) then
- return
- else
- for i=1,4 do
- if tryDoWithEnderChest(func, slot, doDig, turtle.dig, turtle.place, turtle.drop, turtle.suck) then
- for j=i,4 do
- turtle.turnLeft()
- end
- return
- end
- turtle.turnLeft()
- end
- end
- doWithEnderChest(func, slot, true) -- Try again (with digging) if all blocks are obstructed.
- end
- function tryDoWithEnderChest(func, slot, doDig, dig, place, drop, suck)
- if doDig then
- dig() -- In case of gravel, etc.
- end
- turtle.select(slot)
- if not place() then -- Can't place enderchest
- return false
- end
- func(drop, suck)
- turtle.select(slot) -- func may change slot
- dig()
- return true
- end
- function moveWrap(move)
- return (function ()
- if turtle.getFuelLevel() < minFuel then
- if turtle.getItemCount(lastOreSlot) > 0 then
- enderUnload()
- end
- enderRefuel()
- end
- return move()
- end)
- end
- function digWrap(dig, detect)
- return (function ()
- if turtle.getItemCount(lastOreSlot) > 0 then
- enderUnload()
- end
- turtle.select(1)
- return dig()
- end)
- end
- forward = moveWrap(turtle.forward)
- back = moveWrap(turtle.back)
- up = moveWrap(turtle.up)
- down = moveWrap(turtle.down)
- dig = digWrap(turtle.dig, turtle.detect) -- I had a nasty little bug here where
- digUp = digWrap(turtle.digUp, turtle.detectUp) -- it called moveWrap for dig
- digDown = digWrap(turtle.digDown, turtle.detectDown) -- instead.
- function isStone(compare)
- turtle.select(14)
- return compare()
- end
- function isDirt(compare)
- turtle.select(13)
- return compare()
- end
- function isInteresting(detect, compare)
- return detect() and (not (isStone(compare) or isDirt(compare)))
- -- Detect for eff.
- end
- function recurse(recurseLimit, detect, compare, dig, digOpp, forward, back)
- if recurseLimit <= 0 then return false end
- if isInteresting(detect, compare) and dig() then
- while not forward() do dig() end -- Stupid gravel...
- if not digRecursively(recurseLimit-1, true) then return false end
- while not back() do -- Stupid gravel x2...
- turtle.turnLeft()
- turtle.turnLeft()
- digOpp()
- turtle.turnLeft()
- turtle.turnLeft()
- end
- end
- return true
- end
- function digRecursively(recurseLimit, doRotate)
- if doRotate == nil then doRotate = false end
- if recurseLimit == nil then recurseLimit = 200 end -- Seems to be 256 total
- if recurseLimit <= 0 then return false end
- if not (recurse(recurseLimit-1, turtle.detectDown, turtle.compareDown, digDown, digUp, down, up) and
- recurse(recurseLimit-1, turtle.detectUp, turtle.compareUp, digUp, digDown, up, down)) then
- return false
- end
- for i=1,4 do
- if not recurse(recurseLimit-1, turtle.detect, turtle.compare, dig, dig, forward, back) then
- for j=i,4 do -- Orientation strikes again
- turtle.turnRight()
- end
- return false
- end
- if not doRotate then
- break
- end
- turtle.turnRight()
- end
- return true
- end
- while true do -- I love it when everything comes together!
- digRecursively()
- dig()
- forward()
- end
Advertisement
Add Comment
Please, Sign In to add comment