This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
VanySerezhkin's profile picture

flattener

Started by VanySerezhkin, 07 April 2012 - 05:54 PM
VanySerezhkin #1
Posted 07 April 2012 - 07:54 PM
[media]http://youtu.be/Lgri3H55PYk[/media]
Spoilert.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")
Iscariah #2
Posted 19 April 2012 - 03:02 AM
I can't believe nobody responded to this thread. Let me be the first to say thank you! You did a great job on this. I've tested it and it kinda works. I wanted a large flat terrain for IC2 and BC stuff. So I went to the hills and started to set it up like you did in your video. It worked perfectly for about two layers. After that, two of the four turtles returned to their home base, displaying that they were finished what wasn't the case. They should have gone to the next layer. After all, it wasn't a problem because I just had also some fun with the laser from IC2. If you wondering how many I let those turtles flatten the area, I've put 48X48 in all of them. That's a decent amount, maybe that's why it did fail on me. :)/>/> By now, it is flattened but next time I choose MCEdit again for this kind of work. No offense, the script was certainly fun to use!
VanySerezhkin #3
Posted 22 April 2012 - 11:47 AM
1. cc computers can't survive reboot of the game.
2. there was two layers without anything in it, so tertles decide, that they is in air and finished work.
grumpysmurf #4
Posted 22 April 2012 - 06:48 PM
Awesome. Much easier to use this with obsidian pipe than the Buildcraft Filler or IC2 terraformer! Plus I don't have to run around picking up blocks. Added the programs to server so we don't need to lug around the computers and disks. Thanks!