Posted 07 April 2012 - 07:54 PM
[media]http://youtu.be/Lgri3H55PYk[/media]
common.lib
flatterrain
Spoiler
t.lib
--[[
Main turtle utils
x growh to left,
y growh to up,
z is height
angle is anticlockwise, pi == 2, 0 is up
and we allways start at 0,0,0,0
]]
moveMatrix = {
{0,1}, -- up
{1,0}, -- left
{0,-1}, -- down
{-1,0} -- right
}
t = {} -- singleton
function t.init()
t.x=0
t.y=0
t.z=0
t.a=0
t.m=moveMatrix[1]
end
function t.left()
t.a=t.a+1
t.a = t.a % 4
t.m = moveMatrix[t.a+1] -- fucked lua table indexes
turtle.turnLeft()
end
t.l = t.left
function t.right()
t.a=t.a-1
t.a = t.a % 4
t.m = moveMatrix[t.a+1]
turtle.turnRight()
end
t.r = t.right
function t.tryForward()
if turtle.forward() then
t.x = t.x + t.m[1]
t.y = t.y + t.m[2]
return true
end
return false
end
t.tf = t.tryForward
function t.digForward()
repeat
local _ = turtle.detect() and turtle.dig()
until t.tryForward()
end
t.df = t.digForward
function t.tryUp()
if turtle.up() then
t.z = t.z + 1
return true
end
return false
end
t.tu = t.tryUp
function t.digUp()
repeat
local _ = turtle.detectUp() and turtle.digUp()
until t.tryUp()
end
t.du = t.digUp
function t.tryDown()
if turtle.down() then
t.z = t.z - 1
return true
end
return false
end
t.td = t.tryDown
function t.digdown()
repeat
local _ = turtle.detectDown() and turtle.digDown()
until t.tryDown()
end
t.dd = t.digdown
function t.faceTo(direction)
local turnfunc = (direction > t.a) and t.l or t.r
while(direction ~= t.a) do turnfunc() end
end
t.ft = t.faceTo
function t.hasSpace()
return(turtle.getItemCount(9) == 0)
end
t.hs = t.hasSpace
function t.strCoord()
return t.x .. ", " .. t.y .. " (" .. t.z .. ") a:" .. t.a
end
function t.getPos()
return {x=t.x, y=t.y, z=t.z, a=t.a}
end
t.init()
common.lib
common = {}
function common.dump(obj)
print(obj,"n", textutils.serialize(obj))
end
flatterrain
--[[
flatterns some space, dump mined at 0,0,0 facing down
so, obsidian pipe must be at 0,-1,-1 and payload allways be safe
]]
local args = {...}
if #args == 0 then
print("flatterrain <widthx> <widthy>")
shell.exit()
end
local maxx,maxy = tonumber(args[1]), tonumber(args[2])
if not t then shell.run("t.lib") end
if not common then shell.run("common.lib") end
t.init()
local empty_levels = 0 -- levels without digging anything
function LOG(...) print(t.strCoord(), " ", ...) end
function ReturnToBase()
while(t.z > 0) do t.dd() end
t.ft(3)
while(t.x > 0) do t.df() end
t.ft(2)
while(t.y > 0) do t.df() end
end
function GoToPosition(p)
t.ft(0);
while(t.y < p.y) do t.df() end
t.ft(1)
while(t.x < p.x) do t.df() end
t.ft(p.a)
while(t.z > p.z) do t.du() end
end
function DumpStomach()
local i
for i=1,9 do
turtle.select(i)
turtle.drop()
end
turtle.select(1)
end
function EmptyRun()
local p = t.getPos()
ReturnToBase()
DumpStomach()
GoToPosition(p)
end
t.df()
while(empty_levels < 3) do -- until there is only air
for x=1, maxx do
for y=2, maxy do -- alredy digged 1 block
t.df()
if not t.hs() then LOG("emptyRun"); EmptyRun(); end
end
local turnfunc = (x % 2 == 1) and t.l or t.r
turnfunc(); t.df(); turnfunc()
end
local z = t.getPos().z
if turtle.getItemCount(1) == 0 then empty_levels = empty_levels + 1 end
ReturnToBase()
DumpStomach()
LOG("Level " .. z .." done");
t.ft(0)
for i=1,z+1 do t.du() end
t.df()
end
ReturnToBase()
LOG("All done")