Posted 30 August 2012 - 08:26 PM
Hi all,
As a newbie in CC i would like to share my simple
vertical miner program.
http://pastebin.com/uRc089Uq
If this seems to be working ok, I might consider expanding it.
Any suggestions, bugs reports and comments are welcome.
Cheers.
Edit: Fixed small bug in logic
As a newbie in CC i would like to share my simple
vertical miner program.
http://pastebin.com/uRc089Uq
--[[
Vertical shaft miner 0.1
Copyleft Blastbot 2012
Description:
Digs a vertical shaft.
To start place it in an area that you would like to mine.
Type the name of the program, and the turtle will start
searching forward for the deposit location.
The deposit location will be where the turtle bumps
into something. From here he will head back to the "starting tile".
At the "starting tile" the turtle will start mining down and unload
at the deposit location if he can't carry anymore.
It will send a redstone pulse for every item it drops, so any
transposer or filter can pick it up.
It will stop when all the tiles have been traversed (from start to deposit).
]]--
local data = {
params = {...},
currentDepth = 1,
currentTile = 1,
lastTile = 1,
lastSlot = 9,
dug = 0,
unload = false,
msg = "%s @ %d-%d"
}
local function report(msg)
rednet.broadcast(msg)
print(msg)
end
local function forward()
if not turtle.forward() then
return false
end
data.currentTile = data.currentTile - 1
return true
end
local function back()
if not turtle.back() then
return false
end
data.currentTile = data.currentTile + 1
return true
end
local function up()
if not turtle.up() then
return false
end
data.currentDepth = data.currentDepth - 1
return true
end
local function down()
if not turtle.down() then
return false
end
data.currentDepth = data.currentDepth + 1
return true
end
local function getCap()
local capacity = 0
for i = 1, data.lastSlot do
capacity = capacity + turtle.getItemSpace(i)
end
return capacity
end
local function unloadInventory()
local itemCount = 0
local slotItemCount = nil
for i = 1, data.lastSlot do
slotItemCount = turtle.getItemCount(i)
if slotItemCount > 0 then
redstone.setOutput("front", true)
turtle.select(i)
turtle.drop()
sleep(0.2)
redstone.setOutput("front", false)
sleep(0.2)
itemCount = itemCount + slotItemCount
end
end
report("Deposited " .. itemCount .. " items")
end
local function canMine()
local slotsTaken = 0
for i = 1, data.lastSlot do
if turtle.getItemCount(i) > 0 then
turtle.select(i)
if turtle.compareDown() then
return true
else
slotsTaken = slotsTaken + 1
end
end
end
turtle.select(1)
return slotsTaken ~= data.lastSlot
end
local function surface()
while data.currentDepth ~= 1 do
if not up() then
return false
end
sleep(0.2)
end
return true
end
local function moveToDeposit()
if not surface() then
return false
end
while data.currentTile ~= 1 do
if not forward() then
return false
end
end
return true
end
local function moveToNextTile()
if not surface() then
--report("moveToNextTile, could not surface")
return false
end
if forward() then
data.lastTile = data.lastTile - 1
else
--report("moveToNextTile, could not move forward")
return false
end
return true
end
local function moveToLastTile()
if not surface() then
return false
end
while data.currentTile < data.lastTile do
if not back() then
return false
end
end
return true
end
-- returns busy, full, moveerror or digerror
local function shaft()
if 0 == getCap() then
return "full"
end
if turtle.detectDown() then
if canMine() then
if not turtle.digDown() then
return "digerror"
end
data.dug = data.dug + 1
report("Dug " .. data.dug .. " blocks so far")
return "busy"
else
return "full"
end
elseif not down() then
return "moveerror"
else
return "busy"
end
end
local function init()
-- Enable rednet
rednet.open("right")
-- Detect params
if 1 == #data.params then
data.lastSlot = 16
end
-- Find deposit location
while not turtle.detect() do
turtle.forward()
data.lastTile = data.lastTile + 1
end
if data.lastTile < 2 then
report("Invalid mining area")
return
end
report("Found deposit location")
report("Gathering...")
-- Go back to the last tile
if not moveToLastTile() then
report("Error moving to mining area")
return
end
local status = shaft()
report(data.msg:format(status, data.currentDepth, data.currentTile))
-- Work
while "busy" == status do
status = shaft()
report(data.msg:format(status, data.currentDepth, data.currentTile))
if "full" == status then
if not moveToDeposit() then
if not surface() then
status = "dpfullstuck"
else
status = "dpfullsurfaced"
end
else
unloadInventory()
if not moveToLastTile() then
if not surface() then
status = "ltfullstuck"
else
status = "ltfullsurfaced"
end
else
status = "busy"
end
end
elseif status ~= "busy" then
if 1 == data.currentTile then
if not moveToDeposit() then
if not surface() then
status = "!fulldpstuck"
else
status = "!fulldpsurfaced"
end
else
status = "done"
end
else
if not moveToNextTile() then
if not surface() then
status = "!fullltstuck"
else
status = "!fullltsurfaced"
end
else
status = "busy"
end
end
end
report(data.msg:format(status, data.currentDepth, data.currentTile))
end
rednet.close("right")
end
init()
If this seems to be working ok, I might consider expanding it.
Any suggestions, bugs reports and comments are welcome.
Cheers.
Edit: Fixed small bug in logic