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

Gleeson's Practical Turtle Programs [NEW: Advanced Room Builder]

Started by Gleeson9, 22 October 2012 - 12:11 PM
Gleeson9 #1
Posted 22 October 2012 - 02:11 PM
Hey everyone, recently started a series on my channel called "Discovering ComputerCraft" which involved me learning what it is I can achieve with ComputerCraft. Here I will document the programs that I complete for discussion and use for the public.




Advanced Room Builder


Being my most complex program yet, I'm quite proud of this one. This program will either build or hollow out a room for you, lining the walls, roof and floor with a material of your choice, with EnderStorage utilisation to achieve almost infinite amount of blocks for the turtle to use, a place to dump all of the resources it gathers, and to keep it self well fueled. You even get to specify the length, width and height of the room (with no maximum yet discovered!). Note: Largest test has been 128x128x6, so as far as I can tell the size can has no maximum other than keeping the chunks the room is in loaded.


Showcase video: Coming

Making Of videos and code:
SpoilerCode:
Spoiler
function makeWall(x,count)
function detectGravity()
  sleep(.8)
  for i=15,16 do
	turtle.select(i)
	while (turtle.detect()) and (turtle.compare()) do
	  turtle.dig()
	  sleep(0.5)
	end
	while (turtle.detectUp()) and (turtle.compareUp()) do
	  turtle.digUp()
	  sleep(0.5)
	end
  end
end

function clearLayer(l,w,h)
  restock()
  for i=1,l-1 do
	if turtle.detect() then
	  turtle.dig()
	  detectGravity()
	end
	buildForward()
	forward()
  end
  turtle.turnRight()
  restock()
  dump()
  refuel()
  for i=1,w-1 do
	if turtle.detect() then
	  turtle.dig()
	  detectGravity()
	end
	buildForward()
	forward()
  end
  turtle.turnRight()
  restock()
  dump()
  refuel()
  for i=1,l-1 do
	if turtle.detect() then
	  turtle.dig()
	  detectGravity()
	end
	buildForward()
	forward()
  end
  turtle.turnRight()
  restock()
  dump()
  refuel()
  for i=1,w-1 do
	if turtle.detect() then
	  turtle.dig()
	  detectGravity()
	end
	buildForward()
	forward()
  end
  turtle.turnRight()
  restock()
  dump()
  refuel()
end

function forward()
  while not turtle.forward() do
	turtle.attack()
  end
end

function buildForward()
  restock()
  refuel()
  turtle.digDown()
  turtle.placeDown()
end

function dump()
  turtle.select(1)
  turtle.digDown()
  turtle.select(12)
  turtle.placeDown()
  for i=1,10 do
	turtle.select(14)
	if not turtle.compareTo(i) then
	  turtle.select(i)
	  turtle.dropDown()
	end
  end
  turtle.select(12)
  turtle.digDown()
  selectMaterial()
  turtle.placeDown()
end

function selectMaterial()
  x=0
  for i=1,10 do
	turtle.select(14)
	if turtle.compareTo(i) then
	  if turtle.getItemCount(i)>1 then
		turtle.select(i)
		x=0
		return true
	  else
		turtle.select(1)
		x = 1
	  end
	else
	  turtle.select(1)
	  x = 1
	end
  end
  if x == 1 then
	return false
  end
end

function spiral(l,w,h,x)
  local p = 0
  for o=1,w-2 do
	restock()
	refuel()
	for i=1,l-3 do
	  if x==1 then
		buildRoofIf()
	  elseif x==2 then
	  end
	  if turtle.detect() then
		turtle.dig()
		detectGravity()
	  end
	  forward()
	end
	if x==1 then
	  buildRoofIf()
	elseif x==2 then
	end
	dump()
	restock()
	refuel()
	if (p%2==0) and (o~=w-2) then
	  turtle.turnRight()
	  if turtle.detect() then
		turtle.dig()
		detectGravity()
	  end
	  forward()
	  turtle.turnRight()
	  p = p + 1
	elseif (p%2~=0) and (o~=w-2) then
	  turtle.turnLeft()
	  turtle.dig()
	  detectGravity()
	  forward()
	  turtle.turnLeft()
	  p = p + 1
	end
  end
end

function buildRoofIf()
  turtle.digDown()
  restock()
  turtle.placeDown()
end

function buildRoof(l,w,h)
  turtle.down()
  turtle.turnRight()
  turtle.forward()
  turtle.turnLeft()
  turtle.dig()
  detectGravity()
  turtle.forward()
  spiral(l,w,h,1)
  selectMaterial()
  turtle.placeDown()
  returnSpiral(l,w,h)
end

function returnSpiral(l,w,h)
  refuel()
  w=w+0
  if w%2==0 then
	turtle.turnRight()
	for i=1,w-3 do
	  forward()
	end
	turtle.turnRight()
  elseif w%2~=0 then
	turtle.turnRight()
	turtle.turnRight()
	for i=1,l-3 do
	  forward()
	end
	turtle.turnRight()
	for i=1,w-3 do
	  forward()
	end
	turtle.turnRight()
  end	
end

function hollow(l,w,h)
  h=h+0
  turtle.digDown()
  turtle.down()
  turtle.digDown()
  turtle.down()
  restock()
  refuel()
  turtle.placeUp()
  while h>1 do
	spiral(l,w,h,2)  
	h=h-1
	returnSpiral(l,w,h)
	if h>1 then
	  turtle.digDown()
	  turtle.down()
	end
  end
  spiral(l,w,h,1)
  returnSpiral(l,w,h)
end

function restock()
  if not selectMaterial() then
	turtle.digDown()
	turtle.select(13)
	turtle.placeDown()
	turtle.select(1)
	for i=1,3 do
	  turtle.suckDown()
	end
	turtle.select(13)
	turtle.digDown()
	selectMaterial()
	turtle.placeDown()
  end
end

function refuel()
  if turtle.getFuelLevel()<100 then
	dump()
	turtle.select(11)
	turtle.digDown()
	turtle.placeDown()
	turtle.select(1)
	turtle.suckDown()
	turtle.suckDown()
	turtle.suckDown()
	turtle.select(11)
	turtle.digDown()
	restock()
	turtle.placeDown()
	for i=1,10 do
	  turtle.select(i)
	  turtle.refuel()
	end
  end
end

local tArgs = {...}
local l = tonumber(tArgs[1])
local w = tonumber(tArgs[2])
local h = tonumber(tArgs[3])

refuel()
for i=1,h+1 do
  dump()
  clearLayer(l,w,h)
  if turtle.detectUp() then
	turtle.digUp()
	detectGravity()
  end
  turtle.digUp()
  turtle.up()
end
buildRoof(l,w,h)
hollow(l,w,h)

Coming..



Stairs


Stairs is basic (and messily written as it was practically my first) program that will dig a stairway downwards into the world. It includes multiple failsafes such as gravity-block detection and walling up the stairwell so that it would not get flooded with water or lava. It also included lighting.

Showcase video:
[media]http://www.youtube.com/watch?v=0j2dWif0Q9M[/media]

Making Of videos and code:
SpoilerCode:
Spoiler
for i=1,70 do
  turtle.digDown()
  turtle.down()
  turtle.turnRight()
  if turtle.detect()==false then
  turtle.select(1)
  turtle.place()
  end
  turtle.turnLeft()
  turtle.turnLeft()
  if turtle.detect()==false then
  turtle.select(1)
  turtle.place()
  end
  turtle.turnRight()
  turtle.select(1)
  turtle.placeDown()
  turtle.dig()
  sleep(.4)
  while turtle.detect()==true do
  turtle.dig()
  sleep(.4)
  end
  turtle.forward()
  turtle.turnRight()
  if turtle.detect()==false then
  turtle.select(1)
  turtle.place()
  end
  turtle.turnLeft()
  turtle.turnLeft()
  if turtle.detect()==false then
  turtle.select(1)
  turtle.place()
  end
  turtle.turnRight()
  turtle.digUp()
  sleep(0.4)
  while turtle.detectUp()==true do
  turtle.digUp()
  sleep(0.4)
  end
  turtle.up()
  turtle.turnRight()
  turtle.turnRight()
  if i%6==0 then
  turtle.select(2)
  turtle.place()
  end
  turtle.turnLeft()
  if turtle.detect()==false then
  turtle.select(1)
  turtle.place()
  end
  turtle.turnLeft()
  if turtle.detect()==false then
  turtle.select(1)
  turtle.place()
  end
  turtle.turnLeft()
  if turtle.detect()==false then
  turtle.select(1)
  turtle.place()
  end  
  turtle.turnRight()
  turtle.down()
end

[media]http://www.youtube.com/watch?v=fEPgayjaglQ[/media]
[media]http://www.youtube.com/watch?v=ra58ZY8Rwd0[/media]




Tunnel


Tunnel is a program makes a turtle dig out (or create) a 3x3 tunnel in any direction for a specified amount of blocks. It will dig out the tunnel, putting a nice row of lighting in the roof every 6 blocks, and if it runs into open air, such as a cave, ravine, or you just start it in the air, it will wall off the tunnel around it, which also prevents liquid leaks. Using the EnderStorage mod, the turtle also has some inventory management capabilities, such as replenishing it's block or lighting supplies if they run low. Note: Something I did not account for when making this program was fuel, so depending on what you guys think, I may add another EnderChest to handle the turtle's fuel.

Showcase video:
[media]http://www.youtube.com/watch?v=fTV-v9ImyyU[/media]

Making Of videos and code:
SpoilerCode:
Spoiler
function makeWall(x,count)
  if x==1 then
	--Right wall
	turtle.turnRight()
	if not turtle.detect() then
	  selectGlass()
	  turtle.place()
	end
	turtle.turnLeft()
  elseif x==2 then
	--Above
	if count%6==0 then
	  turtle.digUp()
	  sleep(0.4)
	  while turtle.detectUp() do
		turtle.digUp()
		sleep(0.4)
	  end
	  selectGlow()
	  turtle.placeUp()
	end
	if not turtle.detectUp() then
	  selectGlass()
	  turtle.placeUp()
	end
  elseif x==3 then
	--Left wall
	turtle.turnLeft()
	if not turtle.detect() then
	  selectGlass()
	  turtle.place()
	end
	turtle.turnRight()
  elseif x==4 then
	--Bottom
	if not turtle.detectDown() then
	  selectGlass()
	  turtle.placeDown()
	end
  end

end

function selectGlass()
   local x = 0
   for i = 2,12 do
	turtle.select(16)
	if turtle.compareTo(i) then
	  if turtle.getItemCount(i)>16 then
		turtle.select(i)
		x = 1
		return true
	  end
	end
   end	  
   if x == 0 then
	 return false
   end
end

function renewGlass()
  while not selectGlass() do
	selectChest()
	turtle.placeDown()
	turtle.suckDown()
	turtle.suckDown()
	if turtle.getItemSpace(11)<64 then
	  selectGlow()
	  turtle.dropDown()
	end
	turtle.dropDown()
	collectChest()
  end
end

function selectGlow()
  local x = 0
  for i = 2,12 do
	turtle.select(15)
	if turtle.compareTo(i) then
	  if turtle.getItemCount(i)>16 then
		turtle.select(i)
		x = 1
		return true
	  end
	end
  end
  if x == 0 then
	return false
  end
end

function renewGlow()
  while not selectGlow() do
	selectChest()
	turtle.placeDown()
	turtle.suckDown()
	turtle.suckDown()
	if turtle.getItemSpace(11)<64 then
	  selectGlass()
	  turtle.dropDown()
	end
	turtle.dropDown()
	collectChest()
  end
end

function selectChest()
  turtle.select(1)
end

function selectChest1()
  for i=1,12 do
	turtle.select(14)
	if turtle.compareTo(i) then
	  turtle.select(i)
	end
  end
end

function collectChest()
  turtle.select(1)
  turtle.digDown()  
end

function forward()
  renewGlass()
  renewGlow()
  if turtle.getItemCount(12)>=1 then
	turtle.select(12)
	turtle.drop()
  end
  makeWall(1,count)
  makeWall(2,count)
  turtle.dig()
  turtle.down()
  makeWall(1,count)
  turtle.dig()
  turtle.down()
  makeWall(1,count)
  makeWall(4,count)
  turtle.dig()
  sleep(0.6)
  while turtle.detect() do
	turtle.dig()
	sleep(0.4)
  end
  turtle.turnLeft()
  turtle.forward()
  turtle.turnRight()
  makeWall(4,count)
  turtle.dig()
  sleep(0.4)
  while turtle.detect() do
	turtle.dig()
	sleep(0.4)
  end
  turtle.up()
  turtle.dig()
  turtle.up()
  makeWall(2,count)
  turtle.dig()
  turtle.turnLeft()
  turtle.forward()
  turtle.turnRight()
  makeWall(2,count)
  makeWall(3,count)
  turtle.dig()
  turtle.down()
  makeWall(3,count)
  turtle.dig()
  turtle.down()
  makeWall(3,count)
  makeWall(4,count)
  turtle.dig()
  sleep(0.4)
  while turtle.detect() do
	turtle.dig()
	sleep(0.4)
  end
  turtle.turnRight()
  turtle.forward()
  turtle.forward()
  turtle.turnLeft()
  turtle.up()
  turtle.up()
  turtle.forward()
  count=count+1
  return true
end

count = 0
term.clear()
term.setCursorPos(1,1)
print("Enter how many blocks forward you would like to mine: ")
blocks = io.read()
for i=1,blocks do
  forward()
end

[media]http://www.youtube.com/watch?v=zJ5gDrnmzgs[/media]
[media]http://www.youtube.com/watch?v=KqhaGzwQR3M[/media]
[media]http://www.youtube.com/watch?v=RHIo05YioM4[/media]
[media]http://www.youtube.com/watch?v=GsqNrdeVhjo[/media]
[media]http://www.youtube.com/watch?v=nE-QhiIH5d8[/media]
Ralnick #2
Posted 31 October 2012 - 06:17 AM
tried out the advanced room, atempted to refuel once it did that it errored out attempt to something nil line 279
this function
Spoilerrefuel()
for i=1,h+1 do
dump()
clearLayer(l,w,h)
if turtle.detectUp() then
turtle.digUp()
detectGravity()

would not run while it is fueled.

also no instructions on where or how to place the chest. (not even in code)
Kotawolf #3
Posted 20 February 2013 - 08:41 AM
Tried this out today.. the Advanced Room Builder. The turtle wouldn't moved.
Just sits there… acts like the program is running.
DavEdward #4
Posted 28 December 2013 - 06:32 PM
Hi Gleeson,

I used this program a good while back and enjoyed it, and got into minecraft again. However I can't get your code to work. Lines 105 and 218 have corrupt text in them and I don't know what the values are supposed to be.
Any chance you can fix it?

Thanks

EDIT: I found the issue. The forums ate some of the code and left HTML garbage in it's place. It replaced the lessthan/greaterthan symbols with '<' and '>' respectively which broke the code.
Edited on 28 December 2013 - 05:47 PM